rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | diff --git a/update_verifier/include/update_verifier/update_verifier.h b/update_verifier/include/update_verifier/update_verifier.h |
| 2 | index 16b394e98..c700a170b 100644 |
| 3 | --- a/update_verifier/include/update_verifier/update_verifier.h |
| 4 | +++ b/update_verifier/include/update_verifier/update_verifier.h |
| 5 | @@ -22,3 +22,4 @@ int update_verifier(int argc, char** argv); |
| 6 | |
| 7 | // Exposed for testing purpose. |
| 8 | bool verify_image(const std::string& care_map_name); |
| 9 | +std::string GetProperty(const char* strProperity); |
| 10 | diff --git a/update_verifier/update_verifier.cpp b/update_verifier/update_verifier.cpp |
| 11 | index 92d931371..45b12cb9f 100644 |
| 12 | --- a/update_verifier/update_verifier.cpp |
| 13 | +++ b/update_verifier/update_verifier.cpp |
| 14 | @@ -34,9 +34,6 @@ |
| 15 | * The current slot will be marked as having booted successfully if the |
| 16 | * verifier reaches the end after the verification. |
| 17 | */ |
| 18 | - |
| 19 | -#include "update_verifier/update_verifier.h" |
| 20 | - |
| 21 | #include <dirent.h> |
| 22 | #include <errno.h> |
| 23 | #include <fcntl.h> |
| 24 | @@ -52,20 +49,22 @@ |
| 25 | #include <android-base/file.h> |
| 26 | #include <android-base/logging.h> |
| 27 | #include <android-base/parseint.h> |
| 28 | -#include <android-base/properties.h> |
| 29 | #include <android-base/strings.h> |
| 30 | #include <android-base/unique_fd.h> |
| 31 | -#include <android/hardware/boot/1.0/IBootControl.h> |
| 32 | -#include <cutils/android_reboot.h> |
| 33 | +#include <hardware/boot_control.h> |
| 34 | +#include <hardware/hardware.h> |
| 35 | +#include <sys/reboot.h> |
| 36 | |
| 37 | #include "otautil/rangeset.h" |
| 38 | +#include "update_verifier/update_verifier.h" |
| 39 | |
| 40 | -using android::sp; |
| 41 | -using android::hardware::boot::V1_0::IBootControl; |
| 42 | -using android::hardware::boot::V1_0::BoolResult; |
| 43 | -using android::hardware::boot::V1_0::CommandResult; |
| 44 | +using std::string; |
| 45 | |
| 46 | // Find directories in format of "/sys/block/dm-X". |
| 47 | + |
| 48 | +extern const hw_module_t HAL_MODULE_INFO_SYM; |
| 49 | +boot_control_module_t* module; |
| 50 | + |
| 51 | static int dm_name_filter(const dirent* de) { |
| 52 | if (android::base::StartsWith(de->d_name, "dm-")) { |
| 53 | return 1; |
| 54 | @@ -73,6 +72,72 @@ static int dm_name_filter(const dirent* de) { |
| 55 | return 0; |
| 56 | } |
| 57 | |
| 58 | +#define COMMAND_LINE_PATH "/proc/cmdline" |
| 59 | +#define COMMAND_LINE_SIZE 2048 |
| 60 | + |
| 61 | +std::string GetProperty(const char* strProperity) |
| 62 | +{ |
| 63 | + int fd, err, slot; |
| 64 | + ssize_t size = COMMAND_LINE_SIZE, sz; |
| 65 | + char *buf, *ptr; |
| 66 | + char *str; |
| 67 | + std::string res; |
| 68 | + |
| 69 | + fd = open(COMMAND_LINE_PATH, O_RDONLY); |
| 70 | + if (fd < 0) { |
| 71 | + err = -errno; |
| 72 | + LOG(ERROR) << "error reading commandline\n"; |
| 73 | + return NULL; |
| 74 | + } |
| 75 | + ptr = buf = (char *)malloc(size); |
| 76 | + if (!buf) { |
| 77 | + err = -errno; |
| 78 | + LOG(ERROR) << "Error allocating memory"; |
| 79 | + close(fd); |
| 80 | + return NULL; |
| 81 | + } |
| 82 | + do { |
| 83 | + sz = read(fd, buf, size); |
| 84 | + if (sz == 0) { |
| 85 | + break; |
| 86 | + } else if (sz < 0) { |
| 87 | + if (errno == EINTR) { |
| 88 | + continue; |
| 89 | + } |
| 90 | + err = -errno; |
| 91 | + LOG(ERROR) << "Error reading file"; |
| 92 | + free(ptr); |
| 93 | + close(fd); |
| 94 | + return NULL; |
| 95 | + } |
| 96 | + size -= sz; |
| 97 | + buf += sz; |
| 98 | + } while(size > 0); |
| 99 | + |
| 100 | + str = strstr((char *)ptr, strProperity); |
| 101 | + if (!str) { |
| 102 | + err = -EIO; |
| 103 | + LOG(ERROR) << "Cannot find %s in kernel commandline" << strProperity; |
| 104 | + free(ptr); |
| 105 | + close(fd); |
| 106 | + return NULL; |
| 107 | + } |
| 108 | + str += strlen(strProperity); |
| 109 | + while(1){ |
| 110 | + str++; |
| 111 | + if(((*str == ' ') || (*str == '\0' ) || (*str == '\n'))){ |
| 112 | + break; |
| 113 | + }else{ |
| 114 | + res.push_back(*str); |
| 115 | + } |
| 116 | + } |
| 117 | + LOG(INFO) << "Find kernel commandline" << res; |
| 118 | + |
| 119 | + free(ptr); |
| 120 | + close(fd); |
| 121 | + return res; |
| 122 | +} |
| 123 | + |
| 124 | static bool read_blocks(const std::string& partition, const std::string& range_str) { |
| 125 | if (partition != "system" && partition != "vendor" && partition != "product") { |
| 126 | LOG(ERROR) << "Invalid partition name \"" << partition << "\""; |
| 127 | @@ -103,6 +168,7 @@ static bool read_blocks(const std::string& partition, const std::string& range_s |
| 128 | PLOG(WARNING) << "Failed to read " << path; |
| 129 | } else { |
| 130 | std::string dm_block_name = android::base::Trim(content); |
| 131 | + LOG(ERROR) << "dm_block_name namelist[n]->d_name " << dm_block_name << namelist[n]->d_name; |
| 132 | #ifdef BOARD_AVB_ENABLE |
| 133 | // AVB is using 'vroot' for the root block device but we're expecting 'system'. |
| 134 | if (dm_block_name == "vroot") { |
| 135 | @@ -135,7 +201,7 @@ static bool read_blocks(const std::string& partition, const std::string& range_s |
| 136 | LOG(ERROR) << "Error parsing RangeSet string " << range_str; |
| 137 | return false; |
| 138 | } |
| 139 | - |
| 140 | + |
| 141 | // RangeSet::Split() splits the ranges into multiple groups with same number of blocks (except for |
| 142 | // the last group). |
| 143 | size_t thread_num = std::thread::hardware_concurrency() ?: 4; |
| 144 | @@ -147,13 +213,27 @@ static bool read_blocks(const std::string& partition, const std::string& range_s |
| 145 | android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(dm_block_device.c_str(), O_RDONLY))); |
| 146 | if (fd.get() == -1) { |
| 147 | PLOG(ERROR) << "Error reading " << dm_block_device << " for partition " << partition; |
| 148 | - return false; |
| 149 | + //return false; |
| 150 | + std::string path = "/sys/class/block/dm-0/dev"; |
| 151 | + std::string content; |
| 152 | + if (!android::base::ReadFileToString(path, &content)) { |
| 153 | + PLOG(WARNING) << "Failed to read " << path; |
| 154 | + } else { |
| 155 | + std::string dev_name = android::base::Trim(content); |
| 156 | + dm_block_device = DEV_PATH + dev_name; |
| 157 | + } |
| 158 | } |
| 159 | - |
| 160 | + fd.reset(open(dm_block_device.c_str(), O_RDONLY)); |
| 161 | + if (fd.get() == -1) { |
| 162 | + PLOG(ERROR) << "Error reading " << dm_block_device << " for partition " << partition; |
| 163 | + return false; |
| 164 | + } |
| 165 | + |
| 166 | static constexpr size_t kBlockSize = 4096; |
| 167 | std::vector<uint8_t> buf(1024 * kBlockSize); |
| 168 | |
| 169 | size_t block_count = 0; |
| 170 | + |
| 171 | for (const auto& range : group) { |
| 172 | size_t range_start = range.first; |
| 173 | size_t range_end = range.second; |
| 174 | @@ -161,7 +241,7 @@ static bool read_blocks(const std::string& partition, const std::string& range_s |
| 175 | PLOG(ERROR) << "lseek to " << range_start << " failed"; |
| 176 | return false; |
| 177 | } |
| 178 | - |
| 179 | + PLOG(ERROR) << "range start " << range_start << " range_end " << range_end; |
| 180 | size_t remain = (range_end - range_start) * kBlockSize; |
| 181 | while (remain > 0) { |
| 182 | size_t to_read = std::min(remain, 1024 * kBlockSize); |
| 183 | @@ -173,7 +253,7 @@ static bool read_blocks(const std::string& partition, const std::string& range_s |
| 184 | } |
| 185 | block_count += (range_end - range_start); |
| 186 | } |
| 187 | - LOG(INFO) << "Finished reading " << block_count << " blocks on " << dm_block_device; |
| 188 | + LOG(ERROR) << "Finished reading " << block_count << " blocks on " << dm_block_device ; |
| 189 | return true; |
| 190 | }; |
| 191 | |
| 192 | @@ -238,10 +318,13 @@ bool verify_image(const std::string& care_map_name) { |
| 193 | } |
| 194 | |
| 195 | static int reboot_device() { |
| 196 | - if (android_reboot(ANDROID_RB_RESTART2, 0, nullptr) == -1) { |
| 197 | - LOG(ERROR) << "Failed to reboot."; |
| 198 | - return -1; |
| 199 | - } |
| 200 | + //if (android_reboot(ANDROID_RB_RESTART2, 0, nullptr) == -1) { |
| 201 | + // LOG(ERROR) << "Failed to reboot."; |
| 202 | + // return -1; |
| 203 | + //} |
| 204 | + //system("reboot"); |
| 205 | + reboot(RB_AUTOBOOT); |
| 206 | + |
| 207 | while (true) pause(); |
| 208 | } |
| 209 | |
| 210 | @@ -250,23 +333,45 @@ int update_verifier(int argc, char** argv) { |
| 211 | LOG(INFO) << "Started with arg " << i << ": " << argv[i]; |
| 212 | } |
| 213 | |
| 214 | - sp<IBootControl> module = IBootControl::getService(); |
| 215 | + /************* Bootctrl Init Start *************/ |
| 216 | + //sp<IBootControl> module = IBootControl::getService(); |
| 217 | + const hw_module_t* hw_module; |
| 218 | + int ret; |
| 219 | + |
| 220 | + // For update_engine_sideload, we simulate the hw_get_module() by accessing it |
| 221 | + // from the current process directly. |
| 222 | + hw_module = &HAL_MODULE_INFO_SYM; |
| 223 | + ret = 0; |
| 224 | + if (!hw_module || |
| 225 | + strcmp(BOOT_CONTROL_HARDWARE_MODULE_ID, hw_module->id) != 0) { |
| 226 | + ret = -EINVAL; |
| 227 | + } |
| 228 | + if (ret != 0) { |
| 229 | + LOG(ERROR) << "Error loading boot_control HAL implementation."; |
| 230 | + return false; |
| 231 | + } |
| 232 | + |
| 233 | + module = reinterpret_cast<boot_control_module_t*>( |
| 234 | + const_cast<hw_module_t*>(hw_module)); |
| 235 | + module->init(module); |
| 236 | + |
| 237 | if (module == nullptr) { |
| 238 | LOG(ERROR) << "Error getting bootctrl module."; |
| 239 | return reboot_device(); |
| 240 | } |
| 241 | + /************* Bootctrl Init End *************/ |
| 242 | |
| 243 | - uint32_t current_slot = module->getCurrentSlot(); |
| 244 | - BoolResult is_successful = module->isSlotMarkedSuccessful(current_slot); |
| 245 | + uint32_t current_slot = module->getCurrentSlot(module); |
| 246 | + //bool is_successful = module->isSlotMarkedSuccessful(current_slot); |
| 247 | + bool is_successful = module->isSlotMarkedSuccessful(module, current_slot); |
| 248 | LOG(INFO) << "Booting slot " << current_slot << ": isSlotMarkedSuccessful=" |
| 249 | - << static_cast<int32_t>(is_successful); |
| 250 | + << is_successful; |
| 251 | |
| 252 | - if (is_successful == BoolResult::FALSE) { |
| 253 | + if (is_successful == false) { |
| 254 | // The current slot has not booted successfully. |
| 255 | - |
| 256 | #if defined(PRODUCT_SUPPORTS_VERITY) || defined(BOARD_AVB_ENABLE) |
| 257 | bool skip_verification = false; |
| 258 | - std::string verity_mode = android::base::GetProperty("ro.boot.veritymode", ""); |
| 259 | + std::string verity_mode = GetProperty("androidboot.veritymode"); |
| 260 | if (verity_mode.empty()) { |
| 261 | // With AVB it's possible to disable verification entirely and |
| 262 | // in this case ro.boot.veritymode is empty. |
| 263 | @@ -290,7 +395,7 @@ int update_verifier(int argc, char** argv) { |
| 264 | } |
| 265 | |
| 266 | if (!skip_verification) { |
| 267 | - static constexpr auto CARE_MAP_FILE = "/data/ota_package/care_map.txt"; |
| 268 | + static constexpr auto CARE_MAP_FILE = "/var/lib/update_engine/care_map.txt"; |
| 269 | if (!verify_image(CARE_MAP_FILE)) { |
| 270 | LOG(ERROR) << "Failed to verify all blocks in care map file."; |
| 271 | return reboot_device(); |
| 272 | @@ -300,10 +405,10 @@ int update_verifier(int argc, char** argv) { |
| 273 | LOG(WARNING) << "dm-verity not enabled; marking without verification."; |
| 274 | #endif |
| 275 | |
| 276 | - CommandResult cr; |
| 277 | - module->markBootSuccessful([&cr](CommandResult result) { cr = result; }); |
| 278 | - if (!cr.success) { |
| 279 | - LOG(ERROR) << "Error marking booted successfully: " << cr.errMsg; |
| 280 | + //bool cr = module->markBootSuccessful([&cr](CommandResult result) { cr = result; }); |
| 281 | + int cr = module->markBootSuccessful(module); |
| 282 | + if (cr < 0) { |
| 283 | + LOG(ERROR) << "Error marking booted successfully: " << cr; |
| 284 | return reboot_device(); |
| 285 | } |
| 286 | LOG(INFO) << "Marked slot " << current_slot << " as booted successfully."; |
| 287 | diff --git a/update_verifier/update_verifier_main.cpp b/update_verifier/update_verifier_main.cpp |
| 288 | index a86203bfb..c22582341 100644 |
| 289 | --- a/update_verifier/update_verifier_main.cpp |
| 290 | +++ b/update_verifier/update_verifier_main.cpp |
| 291 | @@ -19,12 +19,15 @@ |
| 292 | #include <string> |
| 293 | |
| 294 | #include <android-base/logging.h> |
| 295 | -#include <android-base/properties.h> |
| 296 | +//#include <android-base/properties.h> |
| 297 | |
| 298 | #include "update_verifier/update_verifier.h" |
| 299 | |
| 300 | int main(int argc, char** argv) { |
| 301 | - std::string s = android::base::GetProperty("ro.boot.slot_suffix", ""); |
| 302 | + //std::string s = android::base::GetProperty("ro.boot.slot_suffix", ""); |
| 303 | + |
| 304 | + |
| 305 | + std::string s = GetProperty("androidboot.slot"); |
| 306 | |
| 307 | if (s.empty()) { |
| 308 | return 0; // non-A/B update device, so we quit |
| 309 | @@ -32,7 +35,7 @@ int main(int argc, char** argv) { |
| 310 | |
| 311 | // Set up update_verifier logging to be written to kmsg; because we may not have Logd during |
| 312 | // boot time. |
| 313 | - android::base::InitLogging(argv, &android::base::KernelLogger); |
| 314 | + android::base::InitLogging(argv, android::base::StderrLogger); |
| 315 | |
| 316 | return update_verifier(argc, argv); |
| 317 | } |