1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #define TLOG_TAG "system_state_server_static"
18 
19 #include <lib/system_state_server/system_state_server.h>
20 #include <lib/tipc/tipc_srv.h>
21 #include <lk/err_ptr.h>
22 #include <trusty_log.h>
23 #include <uapi/err.h>
24 
25 /**
26  * system_state_server_get_flag() - Get the value of a system flag
27  * @flag:   Identifier for flag to get. One of @enum system_state_flag.
28  * @valuep: Pointer to return value in.
29  *
30  * @flag values %SYSTEM_STATE_FLAG_PROVISIONING_ALLOWED and
31  * %SYSTEM_STATE_FLAG_APP_LOADING_UNLOCKED are handled by returning the
32  * corresponding %STATIC_SYSTEM_STATE_FLAG_... build flag. This is a simple
33  * implementation of the system state server that either always allows or always
34  * disallows provisioning and unlocked app loading, respectively. Real devices
35  * should base these state flags on fuses, or something similar.
36  *
37  * Return: 0 on success if @flag is supported, ERR_INVALID_ARGS is @flag is
38  * unknown.
39  */
system_state_server_get_flag(uint32_t flag,uint64_t * valuep)40 int system_state_server_get_flag(uint32_t flag, uint64_t* valuep) {
41     switch (flag) {
42     case SYSTEM_STATE_FLAG_PROVISIONING_ALLOWED:
43         *valuep = STATIC_SYSTEM_STATE_FLAG_PROVISIONING_ALLOWED;
44         return 0;
45     case SYSTEM_STATE_FLAG_APP_LOADING_UNLOCKED:
46         *valuep = STATIC_SYSTEM_STATE_FLAG_APP_LOADING_UNLOCKED;
47         return 0;
48     case SYSTEM_STATE_FLAG_APP_LOADING_VERSION_CHECK:
49         *valuep = STATIC_SYSTEM_STATE_FLAG_APP_LOADING_VERSION_CHECK;
50         return 0;
51     default:
52         return ERR_INVALID_ARGS;
53     }
54 }
55 
main(void)56 int main(void) {
57     int rc;
58     struct tipc_hset* hset;
59 
60     hset = tipc_hset_create();
61     if (IS_ERR(hset)) {
62         TLOGE("failed (%d) to create handle set\n", PTR_ERR(hset));
63         return PTR_ERR(hset);
64     }
65 
66     rc = add_system_state_service(hset);
67     if (rc != NO_ERROR) {
68         TLOGE("failed (%d) to initialize system state service\n", rc);
69         return rc;
70     }
71 
72     return tipc_run_event_loop(hset);
73 }
74