• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 /* this program is used to read a set of system properties and their values
18  * from the emulator program and set them in the currently-running emulated
19  * system. It does so by connecting to the 'boot-properties' qemud service.
20  *
21  * This program should be run as root and called from
22  * /system/etc/init.ranchu.rc exclusively.
23  */
24 
25 #define LOG_TAG  "qemu-adb-keys"
26 
27 #define DEBUG  0
28 //#define LOG_NDEBUG 0
29 
30 #include <sys/stat.h>
31 
32 #include <fstream>
33 #include <iostream>
34 #include <string.h>
35 
36 #include <log/log.h>
37 
38 #if DEBUG
39 #  define  DD(...)    ALOGD(__VA_ARGS__)
40 #else
41 #  define  DD(...)    ((void)0)
42 #endif
43 
44 #define ADB_PUBKEY_PROP "qemu.adb.pubkey"
45 // init will copy over this file to /data/misc/adb/adb_keys
46 #define ADB_KEYS_FILE "/data/vendor/adb/adb_keys"
47 
48 extern bool fs_mgr_get_boot_config(const std::string& key, std::string* out_val);
49 
main(void)50 int main(void) {
51     struct stat st;
52     if (stat(ADB_KEYS_FILE, &st) == 0) {
53         ALOGV("%s already exists", ADB_KEYS_FILE);
54         return 0;
55     }
56 
57     std::string adbkey_pub;
58     if (!fs_mgr_get_boot_config(ADB_PUBKEY_PROP, &adbkey_pub)) {
59         ALOGE("Failed to read %s bootconfig prop", ADB_PUBKEY_PROP);
60         exit(1);
61     }
62 
63     std::ofstream f;
64     f.open(ADB_KEYS_FILE);
65     if (!f.is_open()) {
66         ALOGE("Failed to open %s\n", ADB_KEYS_FILE);
67         exit(1);
68     }
69 
70     ALOGV("Got %s=[%s]", ADB_PUBKEY_PROP, adbkey_pub.c_str());
71     f << adbkey_pub;
72     f.close();
73     return 0;
74 }
75