• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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 "util.h"
18 
19 #include <dirent.h>
20 #include <sys/ioctl.h>
21 #include <sys/types.h>
22 
23 #include <thread>
24 
25 #include <android-base/logging.h>
26 #include <android-base/stringprintf.h>
27 #include <android-base/unique_fd.h>
28 #include <linux/fs.h>
29 
30 namespace android {
31 namespace fs_mgr {
32 
NibbleValue(const char & c,uint8_t * value)33 bool NibbleValue(const char& c, uint8_t* value) {
34     CHECK(value != nullptr);
35 
36     switch (c) {
37         case '0' ... '9':
38             *value = c - '0';
39             break;
40         case 'a' ... 'f':
41             *value = c - 'a' + 10;
42             break;
43         case 'A' ... 'F':
44             *value = c - 'A' + 10;
45             break;
46         default:
47             return false;
48     }
49 
50     return true;
51 }
52 
HexToBytes(uint8_t * bytes,size_t bytes_len,const std::string & hex)53 bool HexToBytes(uint8_t* bytes, size_t bytes_len, const std::string& hex) {
54     CHECK(bytes != nullptr);
55 
56     if (hex.size() % 2 != 0) {
57         return false;
58     }
59     if (hex.size() / 2 > bytes_len) {
60         return false;
61     }
62     for (size_t i = 0, j = 0, n = hex.size(); i < n; i += 2, ++j) {
63         uint8_t high;
64         if (!NibbleValue(hex[i], &high)) {
65             return false;
66         }
67         uint8_t low;
68         if (!NibbleValue(hex[i + 1], &low)) {
69             return false;
70         }
71         bytes[j] = (high << 4) | low;
72     }
73     return true;
74 }
75 
BytesToHex(const uint8_t * bytes,size_t bytes_len)76 std::string BytesToHex(const uint8_t* bytes, size_t bytes_len) {
77     CHECK(bytes != nullptr);
78 
79     static const char* hex_digits = "0123456789abcdef";
80     std::string hex;
81 
82     for (size_t i = 0; i < bytes_len; i++) {
83         hex.push_back(hex_digits[(bytes[i] & 0xF0) >> 4]);
84         hex.push_back(hex_digits[bytes[i] & 0x0F]);
85     }
86     return hex;
87 }
88 
89 // TODO: remove duplicate code with fs_mgr_wait_for_file
WaitForFile(const std::string & filename,const std::chrono::milliseconds relative_timeout,FileWaitMode file_wait_mode)90 bool WaitForFile(const std::string& filename, const std::chrono::milliseconds relative_timeout,
91                  FileWaitMode file_wait_mode) {
92     auto start_time = std::chrono::steady_clock::now();
93 
94     while (true) {
95         int rv = access(filename.c_str(), F_OK);
96         if (file_wait_mode == FileWaitMode::Exists) {
97             if (!rv || errno != ENOENT) return true;
98         } else if (file_wait_mode == FileWaitMode::DoesNotExist) {
99             if (rv && errno == ENOENT) return true;
100         }
101 
102         std::this_thread::sleep_for(50ms);
103 
104         auto now = std::chrono::steady_clock::now();
105         auto time_elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(now - start_time);
106         if (time_elapsed > relative_timeout) return false;
107     }
108 }
109 
IsDeviceUnlocked()110 bool IsDeviceUnlocked() {
111     std::string verified_boot_state;
112 
113     if (fs_mgr_get_boot_config("verifiedbootstate", &verified_boot_state)) {
114         return verified_boot_state == "orange";
115     }
116     return false;
117 }
118 
SetBlockDeviceReadOnly(const std::string & blockdev)119 bool SetBlockDeviceReadOnly(const std::string& blockdev) {
120     android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(blockdev.c_str(), O_RDONLY | O_CLOEXEC)));
121     if (fd < 0) {
122         return false;
123     }
124 
125     int ON = 1;
126     return ioctl(fd, BLKROSET, &ON) == 0;
127 }
128 
ListFiles(const std::string & dir)129 Result<std::vector<std::string>> ListFiles(const std::string& dir) {
130     struct dirent* de;
131     std::vector<std::string> files;
132 
133     std::unique_ptr<DIR, int (*)(DIR*)> dirp(opendir(dir.c_str()), closedir);
134     if (!dirp) {
135         return ErrnoError() << "Failed to opendir: " << dir;
136     }
137 
138     while ((de = readdir(dirp.get()))) {
139         if (de->d_type != DT_REG) continue;
140         std::string full_path = android::base::StringPrintf("%s/%s", dir.c_str(), de->d_name);
141         files.emplace_back(std::move(full_path));
142     }
143 
144     return files;
145 }
146 
147 }  // namespace fs_mgr
148 }  // namespace android
149