程序入口:为bootable/recovery/recovery_main.cpp 下的main函数;
313 int main(int argc, char** argv) {
314 // We don't have logcat yet under recovery; so we'll print error on screen and log to stdout
315 // (which is redirected to recovery.log) as we used to do.
316 android::base::InitLogging(argv, &UiLogger);
317 //初始化log输出,是的log可以格式化输出。
318 // Take last pmsg contents and rewrite it to the current pmsg session.
319 static constexpr const char filter[] = "recovery/";
320 // Do we need to rotate?
321 bool do_rotate = false;
322
323 __android_log_pmsg_file_read(LOG_ID_SYSTEM, ANDROID_LOG_INFO, filter, logbasename, &do_rotate);
324 // Take action to refresh pmsg contents
325 __android_log_pmsg_file_read(LOG_ID_SYSTEM, ANDROID_LOG_INFO, filter, logrotate, &do_rotate);
326
327 time_t start = time(nullptr);
328
329 // redirect_stdio should be called only in non-sideload mode. Otherwise we may have two logger
330 // instances with different timestamps.
//保存recovery log文件
331 redirect_stdio(Paths::Get().temporary_log_file().c_str());
332
33 Paths::Paths()
34 : cache_log_directory_(kDefaultCacheLogDirectory),
35 cache_temp_source_(kDefaultCacheTempSource),
36 last_command_file_(kDefaultLastCommandFile),
37 resource_dir_(kDefaultResourceDirectory),
38 stash_directory_base_(kDefaultStashDirectoryBase),
39 temporary_install_file_(kDefaultTemporaryInstallFile),
40 temporary_log_file_(kDefaultTemporaryLogFile),
41 temporary_update_binary_(kDefaultTemporaryUpdateBinary) {}
//***********************************************************************************
227 static void redirect_stdio(const char* filename) {
228 android::base::unique_fd pipe_read, pipe_write;
229 // Create a pipe that allows parent process sending logs over.
//创建input/output管道,失败后
230 if (!android::base::Pipe(&pipe_read, &pipe_write)) {
231 PLOG(ERROR) << “Failed to create pipe for redirecting stdio”;
232
233 // Fall back to traditional logging mode without timestamps. If these fail, there’s not really
234 // anywhere to complain…
235 freopen(filename, “a”, stdout);
236 setbuf(stdout, nullptr);
237 freopen(filename, “a”, stderr);
238 setbuf(stderr, nullptr);
239 / 把输入输出流映射到某个打开的文件
240 return;
241 }
242
243 pid_t pid = fork();
244 if (pid == -1) {
245 PLOG(ERROR) << “Failed to fork for redirecting stdio”;
246
247 // Fall back to traditional logging mode without timestamps. If these fail, there’s not really
248 // anywhere to complain…
249 freopen(filename, “a”, stdout);
250 setbuf(stdout, nullptr);
251 freopen(filename, “a”, stderr);
252 setbuf(stderr, nullptr);
253
254 return;
255 }
256
257 if (pid == 0) {
258 // Child process reads the incoming logs and doesn’t write to the pipe.
259 pipe_write.reset();
//p.reset () //若p是唯一指向其对象的shared_ptr,reset会释放此对象。
//p.reset(q) //若传递了可选的参数内置指针q,会令P指向q,否则会将P置为空。
//p.reset(q, d) //若还传递了参数d,将会调用d而不是delete 来释放q
260
261 auto start = std::chrono::steady_clock::now();
262
263 // Child logger to actually write to the log file.
264 FILE* log_fp = fopen(filename, “ae”);
265 if (log_fp == nullptr) {
266 PLOG(ERROR) << “fopen “” << filename << “” failed”;
267 _exit(EXIT_FAILURE);
268 }
269
270 FILE* pipe_fp = android::base::Fdopen(std::move(pipe_read), “r”);
271 if (pipe_fp == nullptr) {
272 PLOG(ERROR) << “fdopen failed”;
273 check_and_fclose(log_fp, filename);
274 _exit(EXIT_FAILURE);
275 }
276
277 char* line = nullptr;
278 size_t len = 0;
279 while (getline(&line, &len, pipe_fp) != -1) {
280 auto now = std::chrono::steady_clock::now();
281 double duration =
282 std::chrono::duration_cast
283 if (line[0] == ‘\n’) {
284 fprintf(log_fp, “[%12.6lf]\n”, duration);
285 } else {
286 fprintf(log_fp, “[%12.6lf] %s”, duration, line);
287 }
288 fflush(log_fp);
289 }
290
291 PLOG(ERROR) << “getline failed”;
292
293 fclose(pipe_fp);
294 free(line);
295 check_and_fclose(log_fp, filename);
296 _exit(EXIT_FAILURE);
297 } else {
298 // Redirect stdout/stderr to the logger process. Close the unused read end.
299 pipe_read.reset();
300
301 setbuf(stdout, nullptr);
302 setbuf(stderr, nullptr);
303
304 if (dup2(pipe_write.get(), STDOUT_FILENO) == -1) {
305 PLOG(ERROR) << “dup2 stdout failed”;
306 }
307 if (dup2(pipe_write.get(), STDERR_FILENO) == -1) {
308 PLOG(ERROR) << “dup2 stderr failed”;
309 }
310 }
311 }
//加载fstab文件夹,读取fstab文件中的各行表示的分区信息。
333 load_volume_table();
63 void load_volume_table() {
64 if (!ReadDefaultFstab(&fstab)) {
65 LOG(ERROR) << “Failed to read default fstab”;
66 return;
67 }
68
69 fstab.emplace_back(FstabEntry{
70 .mount_point = “/tmp”, .fs_type = “ramdisk”, .blk_device = “ramdisk”, .length = 0 });
71
72 std::cout << “recovery filesystem table” << std::endl << “=========================” << std::endl;
73
74 for (size_t i = 0; i < fstab.size(); ++i) {
75 //const auto& entry = fstab[i];
76 auto& entry = fstab[i];
77 std::cout << " " << i << " " << entry.mount_point << " "
78 << " " << entry.fs_type << " " << entry.blk_device << " " << entry.length
79 << std::endl;
80 const char* MOUNT_POINT_SD = entry.mount_point.c_str();
81 const char* BLK_DEVICE_SD = entry.blk_device.c_str();
82 std::string FS_TYPE_SD= “exfat”;
83 if (strcmp(MOUNT_POINT_SD, “/storage/sdcard0”) == 0) {
84 blkid_cache cache = NULL;
85
86 if (blkid_get_cache(&cache, NULL) < 0) {
87 std::cout << “blkid_get_cache failed” << std::endl;
88 }
89
90 blkid_dev dev = blkid_get_dev(cache, BLK_DEVICE_SD,BLKID_DEV_NORMAL);
91 if(dev && dev->bid_type) {
92 if (strcmp(dev->bid_type, “exfat”) == 0) {
93 entry.fs_type = FS_TYPE_SD;
94 }
95 blkid_put_cache(cache);
96 }
97 else
98 {
99 blkid_put_cache(cache);
100 }
101 }
102 std::cout << std::endl;
103 }
104 }
//判断是佛有cache分区
334 has_cache = volume_for_mount_point(CACHE_ROOT) != nullptr;
335
336 std::vector<std::string> args = get_args(argc, argv);
88 static std::vectorstd::string get_args(const int argc, char** const argv) {
89 CHECK_GT(argc, 0);
90
91 bootloader_message boot = {};
92 std::string err;
93 if (!read_bootloader_message(&boot, &err)) {
/*
148 bool read_bootloader_message(bootloader_message* boot, std::string* err) {
149 std::string misc_blk_device = get_misc_blk_device(err);
/*
43 static std::string get_misc_blk_device(std::string* err) {
44 if (!g_misc_device_for_test.empty()) {
45 return g_misc_device_for_test;
46 }
47 Fstab fstab;
48 if (!ReadDefaultFstab(&fstab)) {
49 *err = “failed to read default fstab”;
50 return “”;
51 }
52 for (const auto& entry : fstab) {
53 if (entry.mount_point == “/misc”) {
54 return entry.blk_device;
55 }
56 }
57
58 *err = “failed to find /misc partition”;
59 return “”;
60 }
*/
150 if (misc_blk_device.empty()) {
151 return false;
152 }
153 return read_bootloader_message_from(boot, misc_blk_device, err);
154 }
*/
94 LOG(ERROR) << err;
95 // If fails, leave a zeroed bootloader_message.
96 boot = {};
97 }
98 stage = std::string(boot.stage);
99
100 std::string boot_command;
101 if (boot.command[0] != 0) {
102 if (memchr(boot.command, ‘\0’, sizeof(boot.command))) {
103 boot_command = std::string(boot.command);
104 } else {
105 boot_command = std::string(boot.command, sizeof(boot.command));
106 }
107 LOG(INFO) << “Boot command: " << boot_command;
108 }
109
110 if (boot.status[0] != 0) {
111 std::string boot_status = std::string(boot.status, sizeof(boot.status));
112 LOG(INFO) << “Boot status: " << boot_status;
113 }
114
115 std::vectorstd::string args(argv, argv + argc);
116
117 // — if arguments weren’t supplied, look in the bootloader control block
118 if (args.size() == 1) {
119 boot.recovery[sizeof(boot.recovery) - 1] = ‘\0’; // Ensure termination
120 std::string boot_recovery(boot.recovery);
121 std::vectorstd::string tokens = android::base::Split(boot_recovery, “\n”);
122 if (!tokens.empty() && tokens[0] == “recovery”) {
123 for (auto it = tokens.begin() + 1; it != tokens.end(); it++) {
124 // Skip empty and ‘\0’-filled tokens.
125 if (!it->empty() && (*it)[0] != ‘\0’) args.push_back(std::move(*it));
126 }
127 LOG(INFO) << “Got " << args.size() << " arguments from boot message”;
128 } else if (boot.recovery[0] != 0) {
129 LOG(ERROR) << “Bad boot message: “” << boot_recovery << “””;
130 }
131 }
132
133 // — if that doesn’t work, try the command file (if we have /cache).
134 if (args.size() == 1 && has_cache) {
135 std::string content;
136 if (ensure_path_mounted(COMMAND_FILE) == 0 &&
137 android::base::ReadFileToString(COMMAND_FILE, &content)) {
138 std::vectorstd::string tokens = android::base::Split(content, “\n”);
139 // All the arguments in COMMAND_FILE are needed (unlike the BCB message,
140 // COMMAND_FILE doesn’t use filename as the first argument).
141 for (auto it = tokens.begin(); it != tokens.end(); it++) {
142 // Skip empty and ‘\0’-filled tokens.
143 if (!it->empty() && (*it)[0] != ‘\0’) args.push_back(std::move(*it));
144 }
145 LOG(INFO) << “Got " << args.size() << " arguments from " << COMMAND_FILE;
146 }
147 }
148
149 // Write the arguments (excluding the filename in args[0]) back into the
150 // bootloader control block. So the device will always boot into recovery to
151 // finish the pending work, until finish_recovery() is called.
152 std::vectorstd::string options(args.cbegin() + 1, args.cend());
153 if (!update_bootloader_message(options, &err)) {
154 LOG(ERROR) << “Failed to set BCB message: " << err;
155 }
156
157 // Finally, if no arguments were specified, check whether we should boot
158 // into fastboot or rescue mode.
159 if (args.size() == 1 && boot_command == “boot-fastboot”) {
160 args.emplace_back(”–fastboot”);
161 } else if (args.size() == 1 && boot_command == “boot-rescue”) {
162 args.emplace_back(”–rescue”);
163 }
164
165 return args;
166 }
337 auto args_to_parse = StringVectorToNullTerminatedArray(args);
339 static constexpr struct option OPTIONS[] = {
340 { "fastboot", no_argument, nullptr, 0 },
341 { "locale", required_argument, nullptr, 0 },
342 { "show_text", no_argument, nullptr, 't' },
343 { nullptr, 0, nullptr, 0 },
344 };
345
346 bool show_text = false;
347 bool fastboot = false;
348 std::string locale;
349
350 int arg;
351 int option_index;
352 while ((arg = getopt_long(args_to_parse.size() - 1, args_to_parse.data(), "", OPTIONS,
353 &option_index)) != -1) {
354 switch (arg) {
355 case 't':
356 show_text = true;
357 break;
358 case 0: {
359 std::string option = OPTIONS[option_index].name;
360 if (option == "locale") {
361 locale = optarg;
362 } else if (option == "fastboot" &&
363 android::base::GetBoolProperty("ro.boot.dynamic_partitions", false)) {
364 fastboot = true;
365 }
366 break;
367 }
368 }
369 }
370 optind = 1;
371
372 if (locale.empty()) {
373 if (has_cache) {
374 locale = load_locale_from_cache();
375 }
376
377 if (locale.empty()) {
378 static constexpr const char* DEFAULT_LOCALE = "en-US";
379 locale = DEFAULT_LOCALE;
380 }
381 }
382
383 static constexpr const char* kDefaultLibRecoveryUIExt = "librecovery_ui_ext.so";
384 // Intentionally not calling dlclose(3) to avoid potential gotchas (e.g. `make_device` may have
385 // handed out pointers to code or static [or thread-local] data and doesn't collect them all back
386 // in on dlclose).
387 void* librecovery_ui_ext = dlopen(kDefaultLibRecoveryUIExt, RTLD_NOW);
388
389 using MakeDeviceType = decltype(&make_device);
390 MakeDeviceType make_device_func = nullptr;
391 if (librecovery_ui_ext == nullptr) {
392 printf("Failed to dlopen %s: %s\n", kDefaultLibRecoveryUIExt, dlerror());
393 } else {
394 reinterpret_cast<void*&>(make_device_func) = dlsym(librecovery_ui_ext, "make_device");
395 if (make_device_func == nullptr) {
396 printf("Failed to dlsym make_device: %s\n", dlerror());
397 }
398 }
399
400 Device* device;
401 if (make_device_func == nullptr) {
402 printf("Falling back to the default make_device() instead\n");
403 device = make_device();
404 } else {
405 printf("Loading make_device from %s\n", kDefaultLibRecoveryUIExt);
406 device = (*make_device_func)();
407 }
408
409 if (android::base::GetBoolProperty("ro.boot.quiescent", false)) {
410 printf("Quiescent recovery mode.\n");
411 device->ResetUI(new StubRecoveryUI());
412 } else {
413 if (!device->GetUI()->Init(locale)) {
414 printf("Failed to initialize UI; using stub UI instead.\n");
415 device->ResetUI(new StubRecoveryUI());
416 }
417 }
418 ui = device->GetUI();
419
420 if (!has_cache) {
421 device->RemoveMenuItemForAction(Device::WIPE_CACHE);
422 }
423
424 if (!android::base::GetBoolProperty("ro.boot.dynamic_partitions", false)) {
425 device->RemoveMenuItemForAction(Device::ENTER_FASTBOOT);
426 }
427
428 if (!is_ro_debuggable()) {
429 device->RemoveMenuItemForAction(Device::ENTER_RESCUE);
430 }
431
432 ui->SetBackground(RecoveryUI::NONE);
433 if (show_text) ui->ShowText(true);
434
435 LOG(INFO) << "Starting recovery (pid " << getpid() << ") on " << ctime(&start);
436 LOG(INFO) << "locale is [" << locale << "]";
437
438 sehandle = selinux_android_file_context_handle();
439 selinux_android_set_sehandle(sehandle);
440 if (!sehandle) {
441 ui->Print("Warning: No file_contexts\n");
442 }
443
444 SetLoggingSehandle(sehandle);
445
446 std::atomic<Device::BuiltinAction> action;
447 std::thread listener_thread(ListenRecoverySocket, ui, std::ref(action));
448 listener_thread.detach();
449
450 while (true) {
451 std::string usb_config = fastboot ? "fastboot" : is_ro_debuggable() ? "adb" : "none";
452 std::string usb_state = android::base::GetProperty("sys.usb.state", "none");
453 if (usb_config != usb_state) {
454 if (!SetUsbConfig("none")) {
455 LOG(ERROR) << "Failed to clear USB config";
456 }
457 if (!SetUsbConfig(usb_config)) {
458 LOG(ERROR) << "Failed to set USB config to " << usb_config;
459 }
460 }
461
462 ui->SetEnableFastbootdLogo(fastboot);
463
464 auto ret = fastboot ? StartFastboot(device, args) : start_recovery(device, args);
465
466 if (ret == Device::KEY_INTERRUPTED) {
467 ret = action.exchange(ret);
468 if (ret == Device::NO_ACTION) {
469 continue;
470 }
471 }
472 switch (ret) {
473 case Device::SHUTDOWN:
474 ui->Print("Shutting down...\n");
475 // TODO: Move all the reboots to reboot(), which should conditionally set quiescent flag.
476 android::base::SetProperty(ANDROID_RB_PROPERTY, "shutdown,");
477 break;
478
479 case Device::REBOOT_BOOTLOADER:
480 ui->Print("Rebooting to bootloader...\n");
481 android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot,bootloader");
482 break;
483
484 case Device::REBOOT_FASTBOOT:
485 ui->Print("Rebooting to recovery/fastboot...\n");
486 android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot,fastboot");
487 break;
488
489 case Device::REBOOT_RECOVERY:
490 ui->Print("Rebooting to recovery...\n");
491 reboot("reboot,recovery");
492 break;
493
494 case Device::REBOOT_RESCUE: {
495 // Not using `reboot("reboot,rescue")`, as it requires matching support in kernel and/or
496 // bootloader.
497 bootloader_message boot = {};
498 strlcpy(boot.command, "boot-rescue", sizeof(boot.command));
499 std::string err;
500 if (!write_bootloader_message(boot, &err)) {
501 LOG(ERROR) << "Failed to write bootloader message: " << err;
502 // Stay under recovery on failure.
503 continue;
504 }
505 ui->Print("Rebooting to recovery/rescue...\n");
506 reboot("reboot,recovery");
507 break;
508 }
509
510 case Device::ENTER_FASTBOOT:
511 if (logical_partitions_mapped()) {
512 ui->Print("Partitions may be mounted - rebooting to enter fastboot.");
513 android::base::SetProperty(ANDROID_RB_PROPERTY, "reboot,fastboot");
514 } else {
515 LOG(INFO) << "Entering fastboot";
516 fastboot = true;
517 }
518 break;
519
520 case Device::ENTER_RECOVERY:
521 LOG(INFO) << "Entering recovery";
522 fastboot = false;
523 break;
524
525 default:
526 ui->Print("Rebooting...\n");
527 reboot("reboot,");
528 break;
529 }
530 }
531
532 // Should be unreachable.
533 return EXIT_SUCCESS;
534 }
535