1 /*
2  ** Copyright 2018, 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 #include <unistd.h>
18 
19 #define LOG_TAG "wait_for_strongbox"
20 #include <android-base/logging.h>
21 
22 #include <keymasterV4_1/Keymaster.h>
23 
24 using android::hardware::keymaster::V4_1::SecurityLevel;
25 using android::hardware::keymaster::V4_1::support::Keymaster;
26 
27 useconds_t kWaitTimeMicroseconds = 1 * 1000;  // 1 milliseconds
28 
main()29 int main() {
30     for (unsigned cycleCount = 0; /* Forever */; ++cycleCount) {
31         auto keymasters = Keymaster::enumerateAvailableDevices();
32 
33         bool foundStrongBox = false;
34         bool foundTee = false;
35         for (auto &dev : keymasters) {
36             SecurityLevel securityLevel = dev->halVersion().securityLevel;
37             uint8_t majorVersion = dev->halVersion().majorVersion;
38             if (securityLevel == SecurityLevel::STRONGBOX && majorVersion == 4) {
39                 foundStrongBox = true;
40             }
41             if (securityLevel == SecurityLevel::TRUSTED_ENVIRONMENT && majorVersion == 4) {
42                 foundTee = true;
43             }
44         }
45 
46         if (foundTee && foundStrongBox) {
47             return 0;
48         }
49         if (cycleCount % 10 == 1) {
50             if (!foundStrongBox) {
51                 LOG(WARNING) << "Still waiting for StrongBox Keymaster";
52             }
53             if (!foundTee) {
54                 LOG(WARNING) << "Still waiting for TEE Keymaster";
55             }
56         }
57         usleep(kWaitTimeMicroseconds);
58     }
59 }
60