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 #pragma once
18 
19 #include <interface/system_state/system_state.h>
20 #include <lk/compiler.h>
21 #include <stdbool.h>
22 #include <sys/types.h>
23 
24 __BEGIN_CDECLS
25 
26 /**
27  * system_state_get_flag() - Get the current value of a system flag
28  * @flag:   Identifier for flag to get. One of @enum system_state_flag.
29  * @valuep: Pointer to return value in.
30  *
31  * Return: 0 on success, or an error code < 0 on failure.
32  */
33 int system_state_get_flag(enum system_state_flag flag, uint64_t* valuep);
34 
35 /**
36  * system_state_get_flag_default() - Get the current value of a system flag
37  * @flag:           Identifier for flag to get. One of @enum system_state_flag.
38  * @default_value:  Value to return if system_state_get_flag() returns an error.
39  *
40  * Return: the current value of the flag if it was successfully read, or
41  * @default_value if the flag could not be read.
42  */
system_state_get_flag_default(enum system_state_flag flag,uint64_t default_value)43 static inline uint64_t system_state_get_flag_default(
44         enum system_state_flag flag,
45         uint64_t default_value) {
46     uint64_t value = default_value;
47     system_state_get_flag(flag, &value);
48     /* Ignore return code, value is unchanged on any error. */
49     return value;
50 }
51 
52 /**
53  * system_state_provisioning_allowed() - Check if provisioning is allowed.
54  *
55  * Return: %true if provisioning is currently allowed, %false otherwise.
56  */
system_state_provisioning_allowed(void)57 static inline bool system_state_provisioning_allowed(void) {
58     return system_state_get_flag_default(
59                    SYSTEM_STATE_FLAG_PROVISIONING_ALLOWED,
60                    SYSTEM_STATE_FLAG_PROVISIONING_ALLOWED_VALUE_NOT_ALLOWED) ==
61            SYSTEM_STATE_FLAG_PROVISIONING_ALLOWED_VALUE_ALLOWED;
62 }
63 
64 /**
65  * system_state_app_loading_unlocked() - Check if loading dev apps is allowed
66  *
67  * Return: %true if app loading is unlocked and dev signing are enabled, %false
68  * otherwise.
69  */
system_state_app_loading_unlocked(void)70 static inline bool system_state_app_loading_unlocked(void) {
71     return system_state_get_flag_default(SYSTEM_STATE_FLAG_APP_LOADING_UNLOCKED,
72                                          false);
73 }
74 
75 /**
76  * system_state_app_loading_skip_version_check() - Check if rollback version
77  * check should be skipped when loading apps.
78  *
79  * Return: %true if the version check should be skipped, %false otherwise.
80  */
system_state_app_loading_skip_version_check(void)81 static inline bool system_state_app_loading_skip_version_check(void) {
82     return system_state_get_flag_default(
83                    SYSTEM_STATE_FLAG_APP_LOADING_VERSION_CHECK,
84                    SYSTEM_STATE_FLAG_APP_LOADING_VERSION_CHECK_VALUE_REQUIRED) ==
85            SYSTEM_STATE_FLAG_APP_LOADING_VERSION_CHECK_VALUE_SKIP_CHECK;
86 }
87 
88 /**
89  * system_state_app_loading_skip_version_update() - Check if rollback version
90  * update should be skipped when loading apps.
91  *
92  * Version update is always skipped when
93  * system_state_app_loading_skip_version_check() returns true.
94  *
95  * Return: %true if the version update should be skipped, %false otherwise.
96  */
system_state_app_loading_skip_version_update(void)97 static inline bool system_state_app_loading_skip_version_update(void) {
98     uint64_t value = system_state_get_flag_default(
99             SYSTEM_STATE_FLAG_APP_LOADING_VERSION_CHECK,
100             SYSTEM_STATE_FLAG_APP_LOADING_VERSION_CHECK_VALUE_REQUIRED);
101     return value ==
102                    SYSTEM_STATE_FLAG_APP_LOADING_VERSION_CHECK_VALUE_SKIP_CHECK ||
103            value ==
104                    SYSTEM_STATE_FLAG_APP_LOADING_VERSION_CHECK_VALUE_SKIP_UPDATE;
105 }
106 
107 __END_CDECLS
108