Android5.0 Recovery源代码分析与定制(一)【转】

本文转载自:http://blog.csdn.net/morixinguan/article/details/72858346

在Tiny4412的Android5.0源代码中:

bootable/recovery/recovery.cpp是recovery程序的主文件。

仔细一看,对比了其它平台的recovery源代码,除了MTK对Recovery做了相应的定制外,其它的平台几乎没有看到,关于MTK平台,后续再分析。

关于Android5.0的recovery,有什么功能,在recovery.cpp中开头就已经做了详细的说明,我们来看看:

[plain] view plain copy
 
 print?
  1. /*  
  2.  * The recovery tool communicates with the main system through /cache files.  
  3.  *   /cache/recovery/command - INPUT - command line for tool, one arg per line  
  4.  *   /cache/recovery/log - OUTPUT - combined log file from recovery run(s)  
  5.  *   /cache/recovery/intent - OUTPUT - intent that was passed in  
  6.  *  
  7.  * The arguments which may be supplied in the recovery.command file:  
  8.  *   --send_intent=anystring - write the text out to recovery.intent  
  9.  *   --update_package=path - verify install an OTA package file  
  10.  *   --wipe_data - erase user data (and cache), then reboot  
  11.  *   --wipe_cache - wipe cache (but not user data), then reboot  
  12.  *   --set_encrypted_filesystem=on|off - enables / diasables encrypted fs  
  13.  *   --just_exit - do nothing; exit and reboot  
  14.  *  
  15.  * After completing, we remove /cache/recovery/command and reboot.  
  16.  * Arguments may also be supplied in the bootloader control block (BCB).  
  17.  * These important scenarios must be safely restartable at any point:  
  18.  *  
  19.  * FACTORY RESET  
  20.  * 1. user selects "factory reset"  
  21.  * 2. main system writes "--wipe_data" to /cache/recovery/command  
  22.  * 3. main system reboots into recovery  
  23.  * 4. get_args() writes BCB with "boot-recovery" and "--wipe_data"  
  24.  *    -- after this, rebooting will restart the erase --  
  25.  * 5. erase_volume() reformats /data  
  26.  * 6. erase_volume() reformats /cache  
  27.  * 7. finish_recovery() erases BCB  
  28.  *    -- after this, rebooting will restart the main system --  
  29.  * 8. main() calls reboot() to boot main system  
  30.  *  
  31.  * OTA INSTALL  
  32.  * 1. main system downloads OTA package to /cache/some-filename.zip  
  33.  * 2. main system writes "--update_package=/cache/some-filename.zip"  
  34.  * 3. main system reboots into recovery  
  35.  * 4. get_args() writes BCB with "boot-recovery" and "--update_package=..."  
  36.  *    -- after this, rebooting will attempt to reinstall the update --  
  37.  * 5. install_package() attempts to install the update  
  38.  *    NOTE: the package install must itself be restartable from any point  
  39.  * 6. finish_recovery() erases BCB  
  40.  *    -- after this, rebooting will (try to) restart the main system --  
  41.  * 7. ** if install failed **  
  42.  *    7a. prompt_and_wait() shows an error icon and waits for the user  
  43.  *    7b; the user reboots (pulling the battery, etc) into the main system  
  44.  * 8. main() calls maybe_install_firmware_update()  
  45.  *    ** if the update contained radio/hboot firmware **:  
  46.  *    8a. m_i_f_u() writes BCB with "boot-recovery" and "--wipe_cache"  
  47.  *        -- after this, rebooting will reformat cache & restart main system --  
  48.  *    8b. m_i_f_u() writes firmware image into raw cache partition  
  49.  *    8c. m_i_f_u() writes BCB with "update-radio/hboot" and "--wipe_cache"  
  50.  *        -- after this, rebooting will attempt to reinstall firmware --  
  51.  *    8d. bootloader tries to flash firmware  
  52.  *    8e. bootloader writes BCB with "boot-recovery" (keeping "--wipe_cache")  
  53.  *        -- after this, rebooting will reformat cache & restart main system --  
  54.  *    8f. erase_volume() reformats /cache  
  55.  *    8g. finish_recovery() erases BCB  
  56.  *        -- after this, rebooting will (try to) restart the main system --  
  57.  * 9. main() calls reboot() to boot main system  
  58.  */  
在这段英文注释里,详细的说明了factory_reset(Android的恢复出厂设置功能)的流程以及OTA系统更新的流程。

在这段注释得最前面说得很明白,我们只要往/cache/recovery/command中写入相应的命令:

[plain] view plain copy
 
 print?
  1. * The arguments which may be supplied in the recovery.command file:  
  2. *   --send_intent=anystring - write the text out to recovery.intent  
  3. *   --update_package=path - verify install an OTA package file  
  4. *   --wipe_data - erase user data (and cache), then reboot  
  5. *   --wipe_cache - wipe cache (but not user data), then reboot  
  6. *   --set_encrypted_filesystem=on|off - enables / diasables encrypted fs  
  7. *   --just_exit - do nothing; exit and reboot  
比如写入: 

--update_package=path(对应的OTA更新的路径)

例如:

--update_package=/mnt/external_sd/xxx.zip

将这条命令写入后,再重启Android系统,recovery检测到有这个命令存在,就会去搜索这个路径,然后将这个路径做路径转换,接下来获取转换后的路径后,就挂载这个路径,然后挂载这个路径,获取OTA包,解包,校验,然后最后实现真正的更新。

如果我们往这个文件写入: --wipe_data

那么就会做出厂设置,格式化/data分区的内容。

接下来,我们来看看代码,从main函数开始分析:

进入main函数后,会将recovery产生的log信息重定向到/tmp/recovery.log这个文件里,具体代码实现如下:

[cpp] view plain copy
 
 print?
  1. //重定向标准输出和标准出错到/tmp/recovery.log 这个文件里  
  2.     //static const char *TEMPORARY_LOG_FILE = "/tmp/recovery.log";  
  3.     redirect_stdio(TEMPORARY_LOG_FILE);  
redirect_stdio函数源代码:
[cpp] view plain copy
 
 print?
  1. static void redirect_stdio(const char* filename) {  
  2.     // If these fail, there's not really anywhere to complain...  
  3.     freopen(filename, "a", stdout); setbuf(stdout, NULL);  
  4.     freopen(filename, "a", stderr); setbuf(stderr, NULL);  
  5. }  
我们看到,所有产生来自stdout和stderr的信息会使用freopen这个函数重定向到/tmp/recovery.log这个文件里。

stdout就是标准输出,stdout就是标准出错。标准输出就是我们平时使用的printf输出的信息。

当然也可以使用fprintf(stdout,"hello world ");也是一样的

标准出错就是fprintf(stderr,"hello world! ");类似的代码。

接下下来,将会判断是否使用adb的sideload来传入,通过参数--adbd来判断:

[cpp] view plain copy
 
 print?
  1. // If this binary is started with the single argument "--adbd",  
  2. // instead of being the normal recovery binary, it turns into kind  
  3. // of a stripped-down version of adbd that only supports the  
  4. // 'sideload' command.  Note this must be a real argument, not  
  5. // anything in the command file or bootloader control block; the  
  6. // only way recovery should be run with this argument is when it  
  7. // starts a copy of itself from the apply_from_adb() function.  
  8. if (argc == 2 && strcmp(argv[1], "--adbd") == 0) {  
  9.     adb_main();  
  10.     return 0;  
  11. }  
做完这些步骤以后,会初始化并装载recovery的分区表recovery.fstab,然后挂载/cache/recovery/last_log这个文件,用来输出log。
[cpp] view plain copy
 
 print?
  1.    printf("Starting recovery (pid %d) on %s", getpid(), ctime(&start));  
  2. //装载recovery的分区表recovery.fstab  
  3.    load_volume_table();  
  4. //在recovery中挂载/cache/recovery/last_log这个文件  
  5. //#define LAST_LOG_FILE "/cache/recovery/last_log"  
  6.    ensure_path_mounted(LAST_LOG_FILE);  
  7.    rotate_last_logs(KEEP_LOG_COUNT);  
这里主要看如何装载分区表的流程,先来看看recovery.fstab
[cpp] view plain copy
 
 print?
  1. /dev/block/by-name/boot         /boot         emmc     defaults                                                                defaults  
  2. /dev/block/by-name/recovery     /recovery     emmc     defaults                                                                defaults  
  3. /dev/block/by-name/splashscreen /splashscreen emmc     defaults                                                                defaults  
  4. /dev/block/by-name/fastboot     /fastboot     emmc     defaults                                                                defaults  
  5. /dev/block/by-name/misc         /misc         emmc     defaults                                                                defaults  
  6. /dev/block/by-name/system       /system       ext4     ro,noatime                                                              wait  
  7. /dev/block/by-name/cache        /cache        ext4     nosuid,nodev,noatime,barrier=1,data=ordered                             wait,check  
  8. /dev/block/by-name/userdata     /data         ext4     nosuid,nodev,noatime,discard,barrier=1,data=ordered,noauto_da_alloc     wait,check  
  9. /dev/block/by-name/factory      /factory      ext4     nosuid,nodev,noatime,barrier=1,data=ordered                             wait  

接下来看是如果挂载的:

[cpp] view plain copy
 
 print?
  1. void load_volume_table()  
  2. {  
  3.     int i;  
  4.     int ret;  
  5.     //读recovery.fstab 这个分区表  
  6.     fstab = fs_mgr_read_fstab("/etc/recovery.fstab");  
  7.     if (!fstab) {  
  8.         LOGE("failed to read /etc/recovery.fstab ");  
  9.         return;  
  10.     }  
  11.     //将对应的信息加入到一条链表中  
  12.     ret = fs_mgr_add_entry(fstab, "/tmp", "ramdisk", "ramdisk");  
  13.     //如果load到的分区表为空,后面做释放操作  
  14.     if (ret < 0 ) {  
  15.         LOGE("failed to add /tmp entry to fstab ");  
  16.         fs_mgr_free_fstab(fstab);  
  17.         fstab = NULL;  
  18.         return;  
  19.     }  
  20.   
  21.     printf("recovery filesystem table ");  
  22.     printf("========================= ");  
  23.     //到这一步,打印分区表信息,这类信息在  
  24.     //recovery启动的时候的log可以看到  
  25.     //分别是以下  
  26.     //编号|   挂载节点|  文件系统类型|  块设备|   长度  
  27.     for (i = 0; i < fstab->num_entries; ++i) {  
  28.         Volume* v = &fstab->recs[i];  
  29.         printf("  %d %s %s %s %lld ", i, v->mount_point, v->fs_type,  
  30.                v->blk_device, v->length);  
  31.     }  
  32.     printf(" ");  
  33. }  

挂载完相应的分区以后,就需要获取命令参数,因为只有挂载了对应的分区,才能访问到前面要写入command的这个文件,这样我们才能正确的打开文件,如果分区都没找到,那么当然就找不到分区上的文件,上面这个步骤是至关重要的。

[cpp] view plain copy
 
 print?
  1. //获取参数  
  2.     //这个参数也可能是从/cache/recovery/command文件中得到相应的命令  
  3.     //也就是可以往command这个文件写入对应的格式的命令即可  
  4.     get_args(&argc, &argv);  
  5.   
  6.     const char *send_intent = NULL;  
  7.     const char *update_package = NULL;  
  8.     int wipe_data = 0, wipe_cache = 0, show_text = 0;  
  9.     bool just_exit = false;  
  10.     bool shutdown_after = false;  
  11.   
  12.     int arg;  
  13.     //参数有擦除分区,OTA更新等  
  14.     while ((arg = getopt_long(argc, argv, "", OPTIONS, NULL)) != -1) {  
  15.         switch (arg) {  
  16.         case 's': send_intent = optarg; break;  
  17.         case 'u': update_package = optarg; break;  
  18.         case 'w': wipe_data = wipe_cache = 1; break;  
  19.         case 'c': wipe_cache = 1; break;  
  20.         case 't': show_text = 1; break;  
  21.         case 'x': just_exit = true; break;  
  22.         case 'l': locale = optarg; break;  
  23.         case 'g': {  
  24.             if (stage == NULL || *stage == '') {  
  25.                 char buffer[20] = "1/";  
  26.                 strncat(buffer, optarg, sizeof(buffer)-3);  
  27.                 stage = strdup(buffer);  
  28.             }  
  29.             break;  
  30.         }  
  31.         case 'p': shutdown_after = true; break;  
  32.         case 'r': reason = optarg; break;  
  33.         case '?':  
  34.             LOGE("Invalid command argument ");  
  35.             continue;  
  36.         }  
  37.     }  
获取到对应的命令,就会执行对应的标志,后面会根据标志来执行对应的操作。

做完以上的流程后,下面就是创建设备,设置语言信息,初始化recovery的UI界面,设置Selinux权限,代码如下:

[cpp] view plain copy
 
 print?
  1. //设置语言  
  2.     if (locale == NULL) {  
  3.         load_locale_from_cache();  
  4.     }  
  5.     printf("locale is [%s] ", locale);  
  6.     printf("stage is [%s] ", stage);  
  7.     printf("reason is [%s] ", reason);  
  8.     //创建设备  
  9.     Device* device = make_device();  
  10.     //获取UI  
  11.     ui = device->GetUI();  
  12.     //设置当前的UI  
  13.     gCurrentUI = ui;  
  14.     //设置UI的语言信息  
  15.     ui->SetLocale(locale);  
  16.     //UI初始化  
  17.     ui->Init();  
  18.   
  19.     int st_cur, st_max;  
  20.     if (stage != NULL && sscanf(stage, "%d/%d", &st_cur, &st_max) == 2) {  
  21.         ui->SetStage(st_cur, st_max);  
  22.     }  
  23.     //设置recovery的背景图  
  24.     ui->SetBackground(RecoveryUI::NONE);  
  25.     //设置界面上是否能够显示字符,使能ui->print函数开关  
  26.     if (show_text) ui->ShowText(true);  
  27.     //设置selinux权限,一般我会把selinux 给disabled  
  28.     struct selinux_opt seopts[] = {  
  29.       { SELABEL_OPT_PATH, "/file_contexts" }  
  30.     };  
  31.   
  32.     sehandle = selabel_open(SELABEL_CTX_FILE, seopts, 1);  
  33.   
  34.     if (!sehandle) {  
  35.         ui->Print("Warning: No file_contexts ");  
  36.     }  
  37.     //虚函数,没有做什么流程  
  38.     device->StartRecovery();  
  39.   
  40.     printf("Command:");  
  41.     for (arg = 0; arg < argc; arg++) {  
  42.         printf(" "%s"", argv[arg]);  
  43.     }  
  44.     printf(" ");  
接下来是重要的环节,这个环节将会根据上面命令参数来做真正的事情了,比如恢复出厂设置,OTA更新等。
[cpp] view plain copy
 
 print?
  1. //如果update_package(也就是要升级的OTA包)不为空的情况下  
  2.     //这里要对升级包的路径做一下路径转换,这里可以自由定制自己升级包的路径  
  3.     if (update_package) {  
  4.         // For backwards compatibility on the cache partition only, if  
  5.         // we're given an old 'root' path "CACHE:foo", change it to  
  6.         // "/cache/foo".  
  7.   
  8.     //这里就是做转换的方法  
  9.     //先比较传进来的recovery参数的前6个byte是否是CACHE  
  10.     //如果是将其路径转化为/cache/CACHE: ......  
  11.         if (strncmp(update_package, "CACHE:", 6) == 0) {  
  12.             int len = strlen(update_package) + 10;  
  13.             char* modified_path = (char*)malloc(len);  
  14.             strlcpy(modified_path, "/cache/", len);  
  15.             strlcat(modified_path, update_package+6, len);  
  16.             printf("(replacing path "%s" with "%s") ",  
  17.                    update_package, modified_path);  
  18.             //这个update_package就是转换后的路径  
  19.             update_package = modified_path;  
  20.         }  
  21.     }  
  22.     printf(" ");  
  23.     property_list(print_property, NULL);  
  24.     //获取属性,这里应该是从一个文件中找到ro.build.display.id  
  25.     //获取recovery的版本信息  
  26.     property_get("ro.build.display.id", recovery_version, "");  
  27.     printf(" ");  
  28.   
  29.     //定义一个安装成功的标志位INSTALL_SUCCESS  ----> 其实是个枚举,值为0  
  30.     int status = INSTALL_SUCCESS;  
  31.     //判断转换后的OTA升级包的路径是否不为空,如果不为空  
  32.     //执行install_package 函数进行升级  
  33.     if (update_package != NULL) {  
  34.         status = install_package(update_package, &wipe_cache, TEMPORARY_INSTALL_FILE, true);  
  35.         //判断是否升级成功  
  36.         if (status == INSTALL_SUCCESS && wipe_cache) {  
  37.             //擦除这个路径,相当于删除了这个路径下的OTA升级包  
  38.             if (erase_volume("/cache")) {  
  39.                 LOGE("Cache wipe (requested by package) failed.");  
  40.             }  
  41.         }  
  42.         //如果安装不成功  
  43.         if (status != INSTALL_SUCCESS) {  
  44.             ui->Print("Installation aborted. ");  
  45.   
  46.             // If this is an eng or userdebug build, then automatically  
  47.             // turn the text display on if the script fails so the error  
  48.             // message is visible.  
  49.             char buffer[PROPERTY_VALUE_MAX+1];  
  50.             property_get("ro.build.fingerprint", buffer, "");  
  51.             if (strstr(buffer, ":userdebug/") || strstr(buffer, ":eng/")) {  
  52.                 ui->ShowText(true);  
  53.             }  
  54.         }  
  55.     }  
  56.     //如果跑的是格式化数据区,那么就走这个流程  
  57.     else if (wipe_data) {  
  58.         if (device->WipeData()) status = INSTALL_ERROR;  
  59.         //格式化/data分区  
  60.         if (erase_volume("/data")) status = INSTALL_ERROR;  
  61.         if (wipe_cache && erase_volume("/cache")) status = INSTALL_ERROR;  
  62.         if (erase_persistent_partition() == -1 ) status = INSTALL_ERROR;  
  63.         if (status != INSTALL_SUCCESS) ui->Print("Data wipe failed. ");  
  64.     }   
  65.     //格式化cache分区  
  66.     else if (wipe_cache) {  
  67.         if (wipe_cache && erase_volume("/cache")) status = INSTALL_ERROR;  
  68.         if (status != INSTALL_SUCCESS) ui->Print("Cache wipe failed. ");  
  69.     }   
  70.     else if (!just_exit) {  
  71.         status = INSTALL_NONE;  // No command specified  
  72.         ui->SetBackground(RecoveryUI::NO_COMMAND);  
  73.     }  
  74.     //如果安装失败或者。。。  
  75.     if (status == INSTALL_ERROR || status == INSTALL_CORRUPT) {  
  76.         copy_logs();  
  77.         //显示错误的LOGO  
  78.         ui->SetBackground(RecoveryUI::ERROR);  
  79.     }  
  80.     Device::BuiltinAction after = shutdown_after ? Device::SHUTDOWN : Device::REBOOT;  
  81.     if (status != INSTALL_SUCCESS || ui->IsTextVisible()) {  
  82.         Device::BuiltinAction temp = prompt_and_wait(device, status);  
  83.         if (temp != Device::NO_ACTION) after = temp;  
  84.     }  
  85.       
  86.     // Save logs and clean up before rebooting or shutting down.  
  87.     //完成recovery升级  
  88.     finish_recovery(send_intent);  
  89.   
  90.     switch (after) {  
  91.         case Device::SHUTDOWN:  
  92.             ui->Print("Shutting down... ");  
  93.             property_set(ANDROID_RB_PROPERTY, "shutdown,");  
  94.             break;  
  95.   
  96.         case Device::REBOOT_BOOTLOADER:  
  97.             ui->Print("Rebooting to bootloader... ");  
  98.             property_set(ANDROID_RB_PROPERTY, "reboot,bootloader");  
  99.             break;  
  100.   
  101.         default:  
  102.             ui->Print("Rebooting... ");  
  103.             property_set(ANDROID_RB_PROPERTY, "reboot,");  
  104.             break;  
  105.     }  
  106.     sleep(5); // should reboot before this finishes  
  107.     return EXIT_SUCCESS;  
在这里面,我们最常用的即是OTA更新和恢复出厂设置,先来说说恢复出厂设置,这个功能就是所谓的手机双清,众所周知,Android手机在使用很久后,由于垃圾数据,以及其它的因素会导致手机的反应越来越慢,这让人烦恼不已,所以就需要双清,双清一般就是清除

/data分区和/cache分区,代码流程很详细,有兴趣可以自己去分析。

接下来看看OTA是如何实现更新的,我们看到install_ota_package这个函数,执行到这个函数,看到源码:

[cpp] view plain copy
 
 print?
  1. //安装更新包  
  2. int  
  3. install_package(const char* path, int* wipe_cache, const char* install_file,  
  4.                 bool needs_mount)  
  5. {  
  6.     FILE* install_log = fopen_path(install_file, "w");  
  7.     if (install_log) {  
  8.         fputs(path, install_log);  
  9.         fputc(' ', install_log);  
  10.     } else {  
  11.         LOGE("failed to open last_install: %s ", strerror(errno));  
  12.     }  
  13.     int result;  
  14.     //设置安装挂载对应的节点  
  15.     //这一步是关键  
  16.     if (setup_install_mounts() != 0) {  
  17.         LOGE("failed to set up expected mounts for install; aborting ");  
  18.         result = INSTALL_ERROR;  
  19.     } else {  
  20.         //到这里才是真正的去安装OTA包  
  21.         result = really_install_package(path, wipe_cache, needs_mount);  
  22.     }  
  23.     //如果返回结果为0,那么安装就成功了  
  24.     if (install_log) {  
  25.         fputc(result == INSTALL_SUCCESS ? '1' : '0', install_log);  
  26.         fputc(' ', install_log);  
  27.         fclose(install_log);  
  28.     }  
  29.     return result;  
  30. }  
其实到了really_install_package这一步,才是真正做到OTA更新,但是在OTA更新之前至关重要的一步就是设置安装挂载对应的节点了,我曾经掉入此坑,现在拿出来分析一下,我们来看看setup_install_mounts这个函数:
[cpp] view plain copy
 
 print?
  1. //设置安装挂载的节点  
  2. int setup_install_mounts() {  
  3.     if (fstab == NULL) {  
  4.         LOGE("can't set up install mounts: no fstab loaded ");  
  5.         return -1;  
  6.     }  
  7.     for (int i = 0; i < fstab->num_entries; ++i) {  
  8.         Volume* v = fstab->recs + i;  
  9.     //如果判断挂载的路径是/tmp 或者/cache  
  10.     //那么就挂载对应的节点,而其它的节点都不会去挂载  
  11.         if (strcmp(v->mount_point, "/tmp") == 0 ||  
  12.             strcmp(v->mount_point, "/cache") == 0) {  
  13.             if (ensure_path_mounted(v->mount_point) != 0) {  
  14.                 LOGE("failed to mount %s ", v->mount_point);  
  15.                 return -1;  
  16.             }  
  17.   
  18.         }  
  19.         //如果不是/tmp或者/cache这两个节点,则默认就会卸载所有的挂载节点  
  20.         else {  
  21.             //卸载所有的挂载节点  
  22.             if (ensure_path_unmounted(v->mount_point) != 0) {  
  23.                 LOGE("failed to unmount %s ", v->mount_point);  
  24.                 return -1;  
  25.             }  
  26.         }  
  27.     }  
  28.     return 0;  
  29. }  
如果在安装更新的时候,OTA包经过路径转换后不是放在/tmp和/cache这个路径下的时候,那么就会走else分支,从而卸载所有的挂载节点,这样就会导致,传的路径正确,却OTA更新不成功,如果是做自己定制的路径,这一步一定要小心,我们可以在这里继续添加定制的挂载点。

那么,执行完设置挂载节点的函数后,接下来就是执行真正的OTA更新了,我们来看看:

[cpp] view plain copy
 
 print?
  1. static int  
  2. really_install_package(const char *path, int* wipe_cache, bool needs_mount)  
  3. {  
  4.     //设置更新时的背景  
  5.     ui->SetBackground(RecoveryUI::INSTALLING_UPDATE);  
  6.     ui->Print("Finding update package... ");  
  7.     // Give verification half the progress bar...  
  8.     //设置进度条的类型  
  9.     ui->SetProgressType(RecoveryUI::DETERMINATE);  
  10.     //显示进度条  
  11.     ui->ShowProgress(VERIFICATION_PROGRESS_FRACTION, VERIFICATION_PROGRESS_TIME);  
  12.     LOGI("Update location: %s ", path);  
  13.     //在屏幕上打印 Opening update package..  
  14.     // Map the update package into memory.  
  15.     ui->Print("Opening update package... ");  
  16.     //patch是OTA的路径,need_mount参数表示是否需要挂载,1挂载,0,不挂载  
  17.     if (path && needs_mount) {  
  18.         if (path[0] == '@') {  
  19.             ensure_path_mounted(path+1);  
  20.         } else {  
  21.             //挂载OTA升级包的路径------> 一般是执行这个流程  
  22.             ensure_path_mounted(path);  
  23.         }  
  24.     }  
  25.   
  26.     MemMapping map;  
  27.     if (sysMapFile(path, &map) != 0) {  
  28.         LOGE("failed to map file ");  
  29.         return INSTALL_CORRUPT;  
  30.     }  
  31.   
  32.     int numKeys;  
  33.     //获取校验公钥文件  
  34.     Certificate* loadedKeys = load_keys(PUBLIC_KEYS_FILE, &numKeys);  
  35.     if (loadedKeys == NULL) {  
  36.         LOGE("Failed to load keys ");  
  37.         return INSTALL_CORRUPT;  
  38.     }  
  39.     LOGI("%d key(s) loaded from %s ", numKeys, PUBLIC_KEYS_FILE);  
  40.   
  41.     ui->Print("Verifying update package... ");  
  42.   
  43.     int err;  
  44.     //校验文件  
  45.     err = verify_file(map.addr, map.length, loadedKeys, numKeys);  
  46.     free(loadedKeys);  
  47.     LOGI("verify_file returned %d ", err);  
  48.     //如果校验不成功  
  49.     if (err != VERIFY_SUCCESS) {  
  50.         //打印签名失败  
  51.         LOGE("signature verification failed ");  
  52.         sysReleaseMap(&map);  
  53.         return INSTALL_CORRUPT;  
  54.     }  
  55.   
  56.     /* Try to open the package. 
  57.      */  
  58.     //尝试去打开ota压缩包  
  59.     ZipArchive zip;  
  60.     err = mzOpenZipArchive(map.addr, map.length, &zip);  
  61.     if (err != 0) {  
  62.         LOGE("Can't open %s (%s) ", path, err != -1 ? strerror(err) : "bad");  
  63.         sysReleaseMap(&map);  
  64.         return INSTALL_CORRUPT;  
  65.     }  
  66.   
  67.     /* Verify and install the contents of the package. 
  68.      */  
  69.     //开始安装升级包  
  70.     ui->Print("Installing update... ");  
  71.     ui->SetEnableReboot(false);  
  72.     int result = try_update_binary(path, &zip, wipe_cache);  
  73.     //安装成功后自动重启  
  74.     ui->SetEnableReboot(true);  
  75.     ui->Print(" ");  
  76.   
  77.     sysReleaseMap(&map);  
  78.     //返回结果  
  79.     return result;  
  80. }  
关于recovery的大致流程,我们分析至此,关于如何像MTK平台一样,定制recovery,这就需要读者能够读懂recovery的流程,然后加入自己的代码进行定制,当然我们也会看到,一些recovery花样百出,很多UI做了自己的,而不是用安卓系统原生态的,安卓系统recovery原生态的UI如下:




如何定制相应的UI,后续我们会对recovery源代码中的UI显示做进一步的分析。。。。

接下来,贴出Android5.0的recovery.cpp代码和注释:

[cpp] view plain copy
 
 print?
  1. /* 
  2.  * Copyright (C) 2007 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 <ctype.h>  
  18. #include <dirent.h>  
  19. #include <errno.h>  
  20. #include <fcntl.h>  
  21. #include <getopt.h>  
  22. #include <limits.h>  
  23. #include <linux/input.h>  
  24. #include <stdarg.h>  
  25. #include <stdio.h>  
  26. #include <stdlib.h>  
  27. #include <string.h>  
  28. #include <sys/stat.h>  
  29. #include <sys/types.h>  
  30. #include <time.h>  
  31. #include <unistd.h>  
  32.   
  33. #include "bootloader.h"  
  34. #include "common.h"  
  35. #include "cutils/properties.h"  
  36. #include "cutils/android_reboot.h"  
  37. #include "install.h"  
  38. #include "minui/minui.h"  
  39. #include "minzip/DirUtil.h"  
  40. #include "roots.h"  
  41. #include "ui.h"  
  42. #include "screen_ui.h"  
  43. #include "device.h"  
  44. #include "adb_install.h"  
  45. extern "C" {  
  46. #include "minadbd/adb.h"  
  47. #include "fuse_sideload.h"  
  48. #include "fuse_sdcard_provider.h"  
  49. }  
  50.   
  51. struct selabel_handle *sehandle;  
  52.   
  53. static const struct option OPTIONS[] = {  
  54.   { "send_intent", required_argument, NULL, 's' },  
  55.   { "update_package", required_argument, NULL, 'u' },  
  56.   { "wipe_data", no_argument, NULL, 'w' },  
  57.   { "wipe_cache", no_argument, NULL, 'c' },  
  58.   { "show_text", no_argument, NULL, 't' },  
  59.   { "just_exit", no_argument, NULL, 'x' },  
  60.   { "locale", required_argument, NULL, 'l' },  
  61.   { "stages", required_argument, NULL, 'g' },  
  62.   { "shutdown_after", no_argument, NULL, 'p' },  
  63.   { "reason", required_argument, NULL, 'r' },  
  64.   { NULL, 0, NULL, 0 },  
  65. };  
  66.   
  67. #define LAST_LOG_FILE "/cache/recovery/last_log"  
  68.   
  69. static const char *CACHE_LOG_DIR = "/cache/recovery";  
  70. static const char *COMMAND_FILE = "/cache/recovery/command";  
  71. static const char *INTENT_FILE = "/cache/recovery/intent";  
  72. static const char *LOG_FILE = "/cache/recovery/log";  
  73. static const char *LAST_INSTALL_FILE = "/cache/recovery/last_install";  
  74. static const char *LOCALE_FILE = "/cache/recovery/last_locale";  
  75. static const char *CACHE_ROOT = "/cache";  
  76. static const char *SDCARD_ROOT = "/sdcard";  
  77. static const char *TEMPORARY_LOG_FILE = "/tmp/recovery.log";  
  78. static const char *TEMPORARY_INSTALL_FILE = "/tmp/last_install";  
  79.   
  80. #define KEEP_LOG_COUNT 10  
  81.   
  82. RecoveryUI* ui = NULL;  
  83. char* locale = NULL;  
  84. char recovery_version[PROPERTY_VALUE_MAX+1];  
  85. char* stage = NULL;  
  86. char* reason = NULL;  
  87.   
  88. /* 
  89.  * The recovery tool communicates with the main system through /cache files. 
  90.  *   /cache/recovery/command - INPUT - command line for tool, one arg per line 
  91.  *   /cache/recovery/log - OUTPUT - combined log file from recovery run(s) 
  92.  *   /cache/recovery/intent - OUTPUT - intent that was passed in 
  93.  * 
  94.  * The arguments which may be supplied in the recovery.command file: 
  95.  *   --send_intent=anystring - write the text out to recovery.intent 
  96.  *   --update_package=path - verify install an OTA package file 
  97.  *   --wipe_data - erase user data (and cache), then reboot 
  98.  *   --wipe_cache - wipe cache (but not user data), then reboot 
  99.  *   --set_encrypted_filesystem=on|off - enables / diasables encrypted fs 
  100.  *   --just_exit - do nothing; exit and reboot 
  101.  * 
  102.  * After completing, we remove /cache/recovery/command and reboot. 
  103.  * Arguments may also be supplied in the bootloader control block (BCB). 
  104.  * These important scenarios must be safely restartable at any point: 
  105.  * 
  106.  * FACTORY RESET 
  107.  * 1. user selects "factory reset" 
  108.  * 2. main system writes "--wipe_data" to /cache/recovery/command 
  109.  * 3. main system reboots into recovery 
  110.  * 4. get_args() writes BCB with "boot-recovery" and "--wipe_data" 
  111.  *    -- after this, rebooting will restart the erase -- 
  112.  * 5. erase_volume() reformats /data 
  113.  * 6. erase_volume() reformats /cache 
  114.  * 7. finish_recovery() erases BCB 
  115.  *    -- after this, rebooting will restart the main system -- 
  116.  * 8. main() calls reboot() to boot main system 
  117.  * 
  118.  * OTA INSTALL 
  119.  * 1. main system downloads OTA package to /cache/some-filename.zip 
  120.  * 2. main system writes "--update_package=/cache/some-filename.zip" 
  121.  * 3. main system reboots into recovery 
  122.  * 4. get_args() writes BCB with "boot-recovery" and "--update_package=..." 
  123.  *    -- after this, rebooting will attempt to reinstall the update -- 
  124.  * 5. install_package() attempts to install the update 
  125.  *    NOTE: the package install must itself be restartable from any point 
  126.  * 6. finish_recovery() erases BCB 
  127.  *    -- after this, rebooting will (try to) restart the main system -- 
  128.  * 7. ** if install failed ** 
  129.  *    7a. prompt_and_wait() shows an error icon and waits for the user 
  130.  *    7b; the user reboots (pulling the battery, etc) into the main system 
  131.  * 8. main() calls maybe_install_firmware_update() 
  132.  *    ** if the update contained radio/hboot firmware **: 
  133.  *    8a. m_i_f_u() writes BCB with "boot-recovery" and "--wipe_cache" 
  134.  *        -- after this, rebooting will reformat cache & restart main system -- 
  135.  *    8b. m_i_f_u() writes firmware image into raw cache partition 
  136.  *    8c. m_i_f_u() writes BCB with "update-radio/hboot" and "--wipe_cache" 
  137.  *        -- after this, rebooting will attempt to reinstall firmware -- 
  138.  *    8d. bootloader tries to flash firmware 
  139.  *    8e. bootloader writes BCB with "boot-recovery" (keeping "--wipe_cache") 
  140.  *        -- after this, rebooting will reformat cache & restart main system -- 
  141.  *    8f. erase_volume() reformats /cache 
  142.  *    8g. finish_recovery() erases BCB 
  143.  *        -- after this, rebooting will (try to) restart the main system -- 
  144.  * 9. main() calls reboot() to boot main system 
  145.  */  
  146.   
  147. static const int MAX_ARG_LENGTH = 4096;  
  148. static const int MAX_ARGS = 100;  
  149.   
  150. // open a given path, mounting partitions as necessary  
  151. FILE*  
  152. fopen_path(const char *path, const char *mode) {  
  153.     if (ensure_path_mounted(path) != 0) {  
  154.         LOGE("Can't mount %s ", path);  
  155.         return NULL;  
  156.     }  
  157.   
  158.     // When writing, try to create the containing directory, if necessary.  
  159.     // Use generous permissions, the system (init.rc) will reset them.  
  160.     if (strchr("wa", mode[0])) dirCreateHierarchy(path, 0777, NULL, 1, sehandle);  
  161.   
  162.     FILE *fp = fopen(path, mode);  
  163.     return fp;  
  164. }  
  165.   
  166. static void redirect_stdio(const char* filename) {  
  167.     // If these fail, there's not really anywhere to complain...  
  168.     freopen(filename, "a", stdout); setbuf(stdout, NULL);  
  169.     freopen(filename, "a", stderr); setbuf(stderr, NULL);  
  170. }  
  171.   
  172. // close a file, log an error if the error indicator is set  
  173. static void  
  174. check_and_fclose(FILE *fp, const char *name) {  
  175.     fflush(fp);  
  176.     if (ferror(fp)) LOGE("Error in %s (%s) ", name, strerror(errno));  
  177.     fclose(fp);  
  178. }  
  179.   
  180. // command line args come from, in decreasing precedence:  
  181. //   - the actual command line  
  182. //   - the bootloader control block (one per line, after "recovery")  
  183. //   - the contents of COMMAND_FILE (one per line)  
  184. static void  
  185. get_args(int *argc, char ***argv) {  
  186.     struct bootloader_message boot;  
  187.     memset(&boot, 0, sizeof(boot));  
  188.     get_bootloader_message(&boot);  // this may fail, leaving a zeroed structure  
  189.     stage = strndup(boot.stage, sizeof(boot.stage));  
  190.   
  191.     if (boot.command[0] != 0 && boot.command[0] != 255) {  
  192.         LOGI("Boot command: %.*s ", (int)sizeof(boot.command), boot.command);  
  193.     }  
  194.   
  195.     if (boot.status[0] != 0 && boot.status[0] != 255) {  
  196.         LOGI("Boot status: %.*s ", (int)sizeof(boot.status), boot.status);  
  197.     }  
  198.   
  199.     // --- if arguments weren't supplied, look in the bootloader control block  
  200.     if (*argc <= 1) {  
  201.         boot.recovery[sizeof(boot.recovery) - 1] = '';  // Ensure termination  
  202.         const char *arg = strtok(boot.recovery, " ");  
  203.         if (arg != NULL && !strcmp(arg, "recovery")) {  
  204.             *argv = (char **) malloc(sizeof(char *) * MAX_ARGS);  
  205.             (*argv)[0] = strdup(arg);  
  206.             for (*argc = 1; *argc < MAX_ARGS; ++*argc) {  
  207.                 if ((arg = strtok(NULL, " ")) == NULL) break;  
  208.                 (*argv)[*argc] = strdup(arg);  
  209.             }  
  210.             LOGI("Got arguments from boot message ");  
  211.         } else if (boot.recovery[0] != 0 && boot.recovery[0] != 255) {  
  212.             LOGE("Bad boot message "%.20s" ", boot.recovery);  
  213.         }  
  214.     }  
  215.   
  216.     // --- if that doesn't work, try the command file  
  217.     if (*argc <= 1) {  
  218.         FILE *fp = fopen_path(COMMAND_FILE, "r");  
  219.         if (fp != NULL) {  
  220.             char *token;  
  221.             char *argv0 = (*argv)[0];  
  222.             *argv = (char **) malloc(sizeof(char *) * MAX_ARGS);  
  223.             (*argv)[0] = argv0;  // use the same program name  
  224.   
  225.             char buf[MAX_ARG_LENGTH];  
  226.             for (*argc = 1; *argc < MAX_ARGS; ++*argc) {  
  227.                 if (!fgets(buf, sizeof(buf), fp)) break;  
  228.                 token = strtok(buf, " ");  
  229.                 if (token != NULL) {  
  230.                     (*argv)[*argc] = strdup(token);  // Strip newline.  
  231.                 } else {  
  232.                     --*argc;  
  233.                 }  
  234.             }  
  235.   
  236.             check_and_fclose(fp, COMMAND_FILE);  
  237.             LOGI("Got arguments from %s ", COMMAND_FILE);  
  238.         }  
  239.     }  
  240.   
  241.     // --> write the arguments we have back into the bootloader control block  
  242.     // always boot into recovery after this (until finish_recovery() is called)  
  243.     strlcpy(boot.command, "boot-recovery", sizeof(boot.command));  
  244.     strlcpy(boot.recovery, "recovery ", sizeof(boot.recovery));  
  245.     int i;  
  246.     for (i = 1; i < *argc; ++i) {  
  247.         strlcat(boot.recovery, (*argv)[i], sizeof(boot.recovery));  
  248.         strlcat(boot.recovery, " ", sizeof(boot.recovery));  
  249.     }  
  250.     set_bootloader_message(&boot);  
  251. }  
  252.   
  253. static void  
  254. set_sdcard_update_bootloader_message() {  
  255.     struct bootloader_message boot;  
  256.     memset(&boot, 0, sizeof(boot));  
  257.     strlcpy(boot.command, "boot-recovery", sizeof(boot.command));  
  258.     strlcpy(boot.recovery, "recovery ", sizeof(boot.recovery));  
  259.     set_bootloader_message(&boot);  
  260. }  
  261.   
  262. // How much of the temp log we have copied to the copy in cache.  
  263. static long tmplog_offset = 0;  
  264.   
  265. static void  
  266. copy_log_file(const char* source, const char* destination, int append) {  
  267.     FILE *log = fopen_path(destination, append ? "a" : "w");  
  268.     if (log == NULL) {  
  269.         LOGE("Can't open %s ", destination);  
  270.     } else {  
  271.         FILE *tmplog = fopen(source, "r");  
  272.         if (tmplog != NULL) {  
  273.             if (append) {  
  274.                 fseek(tmplog, tmplog_offset, SEEK_SET);  // Since last write  
  275.             }  
  276.             char buf[4096];  
  277.             while (fgets(buf, sizeof(buf), tmplog)) fputs(buf, log);  
  278.             if (append) {  
  279.                 tmplog_offset = ftell(tmplog);  
  280.             }  
  281.             check_and_fclose(tmplog, source);  
  282.         }  
  283.         check_and_fclose(log, destination);  
  284.     }  
  285. }  
  286.   
  287. // Rename last_log -> last_log.1 -> last_log.2 -> ... -> last_log.$max  
  288. // Overwrites any existing last_log.$max.  
  289. static void  
  290. rotate_last_logs(int max) {  
  291.     char oldfn[256];  
  292.     char newfn[256];  
  293.   
  294.     int i;  
  295.     for (i = max-1; i >= 0; --i) {  
  296.         snprintf(oldfn, sizeof(oldfn), (i==0) ? LAST_LOG_FILE : (LAST_LOG_FILE ".%d"), i);  
  297.         snprintf(newfn, sizeof(newfn), LAST_LOG_FILE ".%d", i+1);  
  298.         // ignore errors  
  299.         rename(oldfn, newfn);  
  300.     }  
  301. }  
  302.   
  303. static void  
  304. copy_logs() {  
  305.     // Copy logs to cache so the system can find out what happened.  
  306.     copy_log_file(TEMPORARY_LOG_FILE, LOG_FILE, true);  
  307.     copy_log_file(TEMPORARY_LOG_FILE, LAST_LOG_FILE, false);  
  308.     copy_log_file(TEMPORARY_INSTALL_FILE, LAST_INSTALL_FILE, false);  
  309.     chmod(LOG_FILE, 0600);  
  310.     chown(LOG_FILE, 1000, 1000);   // system user  
  311.     chmod(LAST_LOG_FILE, 0640);  
  312.     chmod(LAST_INSTALL_FILE, 0644);  
  313.     sync();  
  314. }  
  315.   
  316. // clear the recovery command and prepare to boot a (hopefully working) system,  
  317. // copy our log file to cache as well (for the system to read), and  
  318. // record any intent we were asked to communicate back to the system.  
  319. // this function is idempotent: call it as many times as you like.  
  320. static void  
  321. finish_recovery(const char *send_intent) {  
  322.     // By this point, we're ready to return to the main system...  
  323.     if (send_intent != NULL) {  
  324.         FILE *fp = fopen_path(INTENT_FILE, "w");  
  325.         if (fp == NULL) {  
  326.             LOGE("Can't open %s ", INTENT_FILE);  
  327.         } else {  
  328.             fputs(send_intent, fp);  
  329.             check_and_fclose(fp, INTENT_FILE);  
  330.         }  
  331.     }  
  332.   
  333.     // Save the locale to cache, so if recovery is next started up  
  334.     // without a --locale argument (eg, directly from the bootloader)  
  335.     // it will use the last-known locale.  
  336.     if (locale != NULL) {  
  337.         LOGI("Saving locale "%s" ", locale);  
  338.         FILE* fp = fopen_path(LOCALE_FILE, "w");  
  339.         fwrite(locale, 1, strlen(locale), fp);  
  340.         fflush(fp);  
  341.         fsync(fileno(fp));  
  342.         check_and_fclose(fp, LOCALE_FILE);  
  343.     }  
  344.   
  345.     copy_logs();  
  346.   
  347.     // Reset to normal system boot so recovery won't cycle indefinitely.  
  348.     struct bootloader_message boot;  
  349.     memset(&boot, 0, sizeof(boot));  
  350.     set_bootloader_message(&boot);  
  351.   
  352.     // Remove the command file, so recovery won't repeat indefinitely.  
  353.     if (ensure_path_mounted(COMMAND_FILE) != 0 ||  
  354.         (unlink(COMMAND_FILE) && errno != ENOENT)) {  
  355.         LOGW("Can't unlink %s ", COMMAND_FILE);  
  356.     }  
  357.   
  358.     ensure_path_unmounted(CACHE_ROOT);  
  359.     sync();  // For good measure.  
  360. }  
  361.   
  362. typedef struct _saved_log_file {  
  363.     char* name;  
  364.     struct stat st;  
  365.     unsigned char* data;  
  366.     struct _saved_log_file* next;  
  367. } saved_log_file;  
  368.   
  369. static int  
  370. erase_volume(const char *volume) {  
  371.     bool is_cache = (strcmp(volume, CACHE_ROOT) == 0);  
  372.   
  373.     ui->SetBackground(RecoveryUI::ERASING);  
  374.     ui->SetProgressType(RecoveryUI::INDETERMINATE);  
  375.   
  376.     saved_log_file* head = NULL;  
  377.   
  378.     if (is_cache) {  
  379.         // If we're reformatting /cache, we load any  
  380.         // "/cache/recovery/last*" files into memory, so we can restore  
  381.         // them after the reformat.  
  382.   
  383.         ensure_path_mounted(volume);  
  384.   
  385.         DIR* d;  
  386.         struct dirent* de;  
  387.         d = opendir(CACHE_LOG_DIR);  
  388.         if (d) {  
  389.             char path[PATH_MAX];  
  390.             strcpy(path, CACHE_LOG_DIR);  
  391.             strcat(path, "/");  
  392.             int path_len = strlen(path);  
  393.             while ((de = readdir(d)) != NULL) {  
  394.                 if (strncmp(de->d_name, "last", 4) == 0) {  
  395.                     saved_log_file* p = (saved_log_file*) malloc(sizeof(saved_log_file));  
  396.                     strcpy(path+path_len, de->d_name);  
  397.                     p->name = strdup(path);  
  398.                     if (stat(path, &(p->st)) == 0) {  
  399.                         // truncate files to 512kb  
  400.                         if (p->st.st_size > (1 << 19)) {  
  401.                             p->st.st_size = 1 << 19;  
  402.                         }  
  403.                         p->data = (unsigned char*) malloc(p->st.st_size);  
  404.                         FILE* f = fopen(path, "rb");  
  405.                         fread(p->data, 1, p->st.st_size, f);  
  406.                         fclose(f);  
  407.                         p->next = head;  
  408.                         head = p;  
  409.                     } else {  
  410.                         free(p);  
  411.                     }  
  412.                 }  
  413.             }  
  414.             closedir(d);  
  415.         } else {  
  416.             if (errno != ENOENT) {  
  417.                 printf("opendir failed: %s ", strerror(errno));  
  418.             }  
  419.         }  
  420.     }  
  421.   
  422.     ui->Print("Formatting %s... ", volume);  
  423.   
  424.     ensure_path_unmounted(volume);  
  425.     int result = format_volume(volume);  
  426.   
  427.     if (is_cache) {  
  428.         while (head) {  
  429.             FILE* f = fopen_path(head->name, "wb");  
  430.             if (f) {  
  431.                 fwrite(head->data, 1, head->st.st_size, f);  
  432.                 fclose(f);  
  433.                 chmod(head->name, head->st.st_mode);  
  434.                 chown(head->name, head->st.st_uid, head->st.st_gid);  
  435.             }  
  436.             free(head->name);  
  437.             free(head->data);  
  438.             saved_log_file* temp = head->next;  
  439.             free(head);  
  440.             head = temp;  
  441.         }  
  442.   
  443.         // Any part of the log we'd copied to cache is now gone.  
  444.         // Reset the pointer so we copy from the beginning of the temp  
  445.         // log.  
  446.         tmplog_offset = 0;  
  447.         copy_logs();  
  448.     }  
  449.   
  450.     return result;  
  451. }  
  452.   
  453. static const char**  
  454. prepend_title(const char* const* headers) {  
  455.     // count the number of lines in our title, plus the  
  456.     // caller-provided headers.  
  457.     int count = 3;   // our title has 3 lines  
  458.     const char* const* p;  
  459.     for (p = headers; *p; ++p, ++count);  
  460.   
  461.     const char** new_headers = (const char**)malloc((count+1) * sizeof(char*));  
  462.     const char** h = new_headers;  
  463.     *(h++) = "Android system recovery <" EXPAND(RECOVERY_API_VERSION) "e>";  
  464.     *(h++) = recovery_version;  
  465.     *(h++) = "";  
  466.     for (p = headers; *p; ++p, ++h) *h = *p;  
  467.     *h = NULL;  
  468.   
  469.     return new_headers;  
  470. }  
  471.   
  472. static int  
  473. get_menu_selection(const char* const * headers, const char* const * items,  
  474.                    int menu_only, int initial_selection, Device* device) {  
  475.     // throw away keys pressed previously, so user doesn't  
  476.     // accidentally trigger menu items.  
  477.     ui->FlushKeys();  
  478.   
  479.     ui->StartMenu(headers, items, initial_selection);  
  480.     int selected = initial_selection;  
  481.     int chosen_item = -1;  
  482.   
  483.     while (chosen_item < 0) {  
  484.         int key = ui->WaitKey();  
  485.         int visible = ui->IsTextVisible();  
  486.   
  487.         if (key == -1) {   // ui_wait_key() timed out  
  488.             if (ui->WasTextEverVisible()) {  
  489.                 continue;  
  490.             } else {  
  491.                 LOGI("timed out waiting for key input; rebooting. ");  
  492.                 ui->EndMenu();  
  493.                 return 0; // XXX fixme  
  494.             }  
  495.         }  
  496.   
  497.         int action = device->HandleMenuKey(key, visible);  
  498.   
  499.         if (action < 0) {  
  500.             switch (action) {  
  501.                 case Device::kHighlightUp:  
  502.                     --selected;  
  503.                     selected = ui->SelectMenu(selected);  
  504.                     break;  
  505.                 case Device::kHighlightDown:  
  506.                     ++selected;  
  507.                     selected = ui->SelectMenu(selected);  
  508.                     break;  
  509.                 case Device::kInvokeItem:  
  510.                     chosen_item = selected;  
  511.                     break;  
  512.                 case Device::kNoAction:  
  513.                     break;  
  514.             }  
  515.         } else if (!menu_only) {  
  516.             chosen_item = action;  
  517.         }  
  518.     }  
  519.   
  520.     ui->EndMenu();  
  521.     return chosen_item;  
  522. }  
  523.   
  524. static int compare_string(const void* a, const void* b) {  
  525.     return strcmp(*(const char**)a, *(const char**)b);  
  526. }  
  527.   
  528. // Returns a malloc'd path, or NULL.  
  529. static char*  
  530. browse_directory(const char* path, Device* device) {  
  531.     ensure_path_mounted(path);  
  532.   
  533.     const char* MENU_HEADERS[] = { "Choose a package to install:",  
  534.                                    path,  
  535.                                    "",  
  536.                                    NULL };  
  537.     DIR* d;  
  538.     struct dirent* de;  
  539.     d = opendir(path);  
  540.     if (d == NULL) {  
  541.         LOGE("error opening %s: %s ", path, strerror(errno));  
  542.         return NULL;  
  543.     }  
  544.   
  545.     const char** headers = prepend_title(MENU_HEADERS);  
  546.   
  547.     int d_size = 0;  
  548.     int d_alloc = 10;  
  549.     char** dirs = (char**)malloc(d_alloc * sizeof(char*));  
  550.     int z_size = 1;  
  551.     int z_alloc = 10;  
  552.     char** zips = (char**)malloc(z_alloc * sizeof(char*));  
  553.     zips[0] = strdup("../");  
  554.   
  555.     while ((de = readdir(d)) != NULL) {  
  556.         int name_len = strlen(de->d_name);  
  557.   
  558.         if (de->d_type == DT_DIR) {  
  559.             // skip "." and ".." entries  
  560.             if (name_len == 1 && de->d_name[0] == '.') continue;  
  561.             if (name_len == 2 && de->d_name[0] == '.' &&  
  562.                 de->d_name[1] == '.') continue;  
  563.   
  564.             if (d_size >= d_alloc) {  
  565.                 d_alloc *= 2;  
  566.                 dirs = (char**)realloc(dirs, d_alloc * sizeof(char*));  
  567.             }  
  568.             dirs[d_size] = (char*)malloc(name_len + 2);  
  569.             strcpy(dirs[d_size], de->d_name);  
  570.             dirs[d_size][name_len] = '/';  
  571.             dirs[d_size][name_len+1] = '';  
  572.             ++d_size;  
  573.         } else if (de->d_type == DT_REG &&  
  574.                    name_len >= 4 &&  
  575.                    strncasecmp(de->d_name + (name_len-4), ".zip", 4) == 0) {  
  576.             if (z_size >= z_alloc) {  
  577.                 z_alloc *= 2;  
  578.                 zips = (char**)realloc(zips, z_alloc * sizeof(char*));  
  579.             }  
  580.             zips[z_size++] = strdup(de->d_name);  
  581.         }  
  582.     }  
  583.     closedir(d);  
  584.   
  585.     qsort(dirs, d_size, sizeof(char*), compare_string);  
  586.     qsort(zips, z_size, sizeof(char*), compare_string);  
  587.   
  588.     // append dirs to the zips list  
  589.     if (d_size + z_size + 1 > z_alloc) {  
  590.         z_alloc = d_size + z_size + 1;  
  591.         zips = (char**)realloc(zips, z_alloc * sizeof(char*));  
  592.     }  
  593.     memcpy(zips + z_size, dirs, d_size * sizeof(char*));  
  594.     free(dirs);  
  595.     z_size += d_size;  
  596.     zips[z_size] = NULL;  
  597.   
  598.     char* result;  
  599.     int chosen_item = 0;  
  600.     while (true) {  
  601.         chosen_item = get_menu_selection(headers, zips, 1, chosen_item, device);  
  602.   
  603.         char* item = zips[chosen_item];  
  604.         int item_len = strlen(item);  
  605.         if (chosen_item == 0) {          // item 0 is always "../"  
  606.             // go up but continue browsing (if the caller is update_directory)  
  607.             result = NULL;  
  608.             break;  
  609.         }  
  610.   
  611.         char new_path[PATH_MAX];  
  612.         strlcpy(new_path, path, PATH_MAX);  
  613.         strlcat(new_path, "/", PATH_MAX);  
  614.         strlcat(new_path, item, PATH_MAX);  
  615.   
  616.         if (item[item_len-1] == '/') {  
  617.             // recurse down into a subdirectory  
  618.             new_path[strlen(new_path)-1] = '';  // truncate the trailing '/'  
  619.             result = browse_directory(new_path, device);  
  620.             if (result) break;  
  621.         } else {  
  622.             // selected a zip file: return the malloc'd path to the caller.  
  623.             result = strdup(new_path);  
  624.             break;  
  625.         }  
  626.     }  
  627.   
  628.     int i;  
  629.     for (i = 0; i < z_size; ++i) free(zips[i]);  
  630.     free(zips);  
  631.     free(headers);  
  632.   
  633.     return result;  
  634. }  
  635.   
  636. static void  
  637. wipe_data(int confirm, Device* device) {  
  638.     if (confirm) {  
  639.         static const char** title_headers = NULL;  
  640.   
  641.         if (title_headers == NULL) {  
  642.             const char* headers[] = { "Confirm wipe of all user data?",  
  643.                                       "  THIS CAN NOT BE UNDONE.",  
  644.                                       "",  
  645.                                       NULL };  
  646.             title_headers = prepend_title((const char**)headers);  
  647.         }  
  648.   
  649.         const char* items[] = { " No",  
  650.                                 " No",  
  651.                                 " No",  
  652.                                 " No",  
  653.                                 " No",  
  654.                                 " No",  
  655.                                 " No",  
  656.                                 " Yes -- delete all user data",   // [7]  
  657.                                 " No",  
  658.                                 " No",  
  659.                                 " No",  
  660.                                 NULL };  
  661.   
  662.         int chosen_item = get_menu_selection(title_headers, items, 1, 0, device);  
  663.         if (chosen_item != 7) {  
  664.             return;  
  665.         }  
  666.     }  
  667.   
  668.     ui->Print(" -- Wiping data... ");  
  669.     device->WipeData();  
  670.     erase_volume("/data");  
  671.     erase_volume("/cache");  
  672.     erase_persistent_partition();  
  673.     ui->Print("Data wipe complete. ");  
  674. }  
  675.   
  676. static void file_to_ui(const char* fn) {  
  677.     FILE *fp = fopen_path(fn, "re");  
  678.     if (fp == NULL) {  
  679.         ui->Print("  Unable to open %s: %s ", fn, strerror(errno));  
  680.         return;  
  681.     }  
  682.     char line[1024];  
  683.     int ct = 0;  
  684.     redirect_stdio("/dev/null");  
  685.     while(fgets(line, sizeof(line), fp) != NULL) {  
  686.         ui->Print("%s", line);  
  687.         ct++;  
  688.         if (ct % 30 == 0) {  
  689.             // give the user time to glance at the entries  
  690.             ui->WaitKey();  
  691.         }  
  692.     }  
  693.     redirect_stdio(TEMPORARY_LOG_FILE);  
  694.     fclose(fp);  
  695. }  
  696.   
  697. static void choose_recovery_file(Device* device) {  
  698.     int i;  
  699.     static const char** title_headers = NULL;  
  700.     char *filename;  
  701.     const char* headers[] = { "Select file to view",  
  702.                               "",  
  703.                               NULL };  
  704.     char* entries[KEEP_LOG_COUNT + 2];  
  705.     memset(entries, 0, sizeof(entries));  
  706.   
  707.     for (i = 0; i < KEEP_LOG_COUNT; i++) {  
  708.         char *filename;  
  709.         if (asprintf(&filename, (i==0) ? LAST_LOG_FILE : (LAST_LOG_FILE ".%d"), i) == -1) {  
  710.             // memory allocation failure - return early. Should never happen.  
  711.             return;  
  712.         }  
  713.         if ((ensure_path_mounted(filename) != 0) || (access(filename, R_OK) == -1)) {  
  714.             free(filename);  
  715.             entries[i+1] = NULL;  
  716.             break;  
  717.         }  
  718.         entries[i+1] = filename;  
  719.     }  
  720.   
  721.     entries[0] = strdup("Go back");  
  722.     title_headers = prepend_title((const char**)headers);  
  723.   
  724.     while(1) {  
  725.         int chosen_item = get_menu_selection(title_headers, entries, 1, 0, device);  
  726.         if (chosen_item == 0) break;  
  727.         file_to_ui(entries[chosen_item]);  
  728.     }  
  729.   
  730.     for (i = 0; i < KEEP_LOG_COUNT + 1; i++) {  
  731.         free(entries[i]);  
  732.     }  
  733. }  
  734.   
  735. // Return REBOOT, SHUTDOWN, or REBOOT_BOOTLOADER.  Returning NO_ACTION  
  736. // means to take the default, which is to reboot or shutdown depending  
  737. // on if the --shutdown_after flag was passed to recovery.  
  738. static Device::BuiltinAction  
  739. prompt_and_wait(Device* device, int status) {  
  740.     const char* const* headers = prepend_title(device->GetMenuHeaders());  
  741.   
  742.     for (;;) {  
  743.         finish_recovery(NULL);  
  744.         switch (status) {  
  745.             case INSTALL_SUCCESS:  
  746.             case INSTALL_NONE:  
  747.                 ui->SetBackground(RecoveryUI::NO_COMMAND);  
  748.                 break;  
  749.   
  750.             case INSTALL_ERROR:  
  751.             case INSTALL_CORRUPT:  
  752.                 ui->SetBackground(RecoveryUI::ERROR);  
  753.                 break;  
  754.         }  
  755.         ui->SetProgressType(RecoveryUI::EMPTY);  
  756.   
  757.         int chosen_item = get_menu_selection(headers, device->GetMenuItems(), 0, 0, device);  
  758.   
  759.         // device-specific code may take some action here.  It may  
  760.         // return one of the core actions handled in the switch  
  761.         // statement below.  
  762.         Device::BuiltinAction chosen_action = device->InvokeMenuItem(chosen_item);  
  763.   
  764.         int wipe_cache = 0;  
  765.         switch (chosen_action) {  
  766.             case Device::NO_ACTION:  
  767.                 break;  
  768.   
  769.             case Device::REBOOT:  
  770.             case Device::SHUTDOWN:  
  771.             case Device::REBOOT_BOOTLOADER:  
  772.                 return chosen_action;  
  773.   
  774.             case Device::WIPE_DATA:  
  775.                 wipe_data(ui->IsTextVisible(), device);  
  776.                 if (!ui->IsTextVisible()) return Device::NO_ACTION;  
  777.                 break;  
  778.   
  779.             case Device::WIPE_CACHE:  
  780.                 ui->Print(" -- Wiping cache... ");  
  781.                 erase_volume("/cache");  
  782.                 ui->Print("Cache wipe complete. ");  
  783.                 if (!ui->IsTextVisible()) return Device::NO_ACTION;  
  784.                 break;  
  785.   
  786.             case Device::APPLY_EXT: {  
  787.                 ensure_path_mounted(SDCARD_ROOT);  
  788.                 char* path = browse_directory(SDCARD_ROOT, device);  
  789.                 if (path == NULL) {  
  790.                     ui->Print(" -- No package file selected. ", path);  
  791.                     break;  
  792.                 }  
  793.   
  794.                 ui->Print(" -- Install %s ... ", path);  
  795.                 set_sdcard_update_bootloader_message();  
  796.                 void* token = start_sdcard_fuse(path);  
  797.   
  798.                 int status = install_package(FUSE_SIDELOAD_HOST_PATHNAME, &wipe_cache,  
  799.                                              TEMPORARY_INSTALL_FILE, false);  
  800.   
  801.                 finish_sdcard_fuse(token);  
  802.                 ensure_path_unmounted(SDCARD_ROOT);  
  803.   
  804.                 if (status == INSTALL_SUCCESS && wipe_cache) {  
  805.                     ui->Print(" -- Wiping cache (at package request)... ");  
  806.                     if (erase_volume("/cache")) {  
  807.                         ui->Print("Cache wipe failed. ");  
  808.                     } else {  
  809.                         ui->Print("Cache wipe complete. ");  
  810.                     }  
  811.                 }  
  812.   
  813.                 if (status >= 0) {  
  814.                     if (status != INSTALL_SUCCESS) {  
  815.                         ui->SetBackground(RecoveryUI::ERROR);  
  816.                         ui->Print("Installation aborted. ");  
  817.                     } else if (!ui->IsTextVisible()) {  
  818.                         return Device::NO_ACTION;  // reboot if logs aren't visible  
  819.                     } else {  
  820.                         ui->Print(" Install from sdcard complete. ");  
  821.                     }  
  822.                 }  
  823.                 break;  
  824.             }  
  825.   
  826.             case Device::APPLY_CACHE:  
  827.                 ui->Print(" APPLY_CACHE is deprecated. ");  
  828.                 break;  
  829.   
  830.             case Device::READ_RECOVERY_LASTLOG:  
  831.                 choose_recovery_file(device);  
  832.                 break;  
  833.   
  834.             case Device::APPLY_ADB_SIDELOAD:  
  835.                 status = apply_from_adb(ui, &wipe_cache, TEMPORARY_INSTALL_FILE);  
  836.                 if (status >= 0) {  
  837.                     if (status != INSTALL_SUCCESS) {  
  838.                         ui->SetBackground(RecoveryUI::ERROR);  
  839.                         ui->Print("Installation aborted. ");  
  840.                         copy_logs();  
  841.                     } else if (!ui->IsTextVisible()) {  
  842.                         return Device::NO_ACTION;  // reboot if logs aren't visible  
  843.                     } else {  
  844.                         ui->Print(" Install from ADB complete. ");  
  845.                     }  
  846.                 }  
  847.                 break;  
  848.         }  
  849.     }  
  850. }  
  851.   
  852. static void  
  853. print_property(const char *key, const char *name, void *cookie) {  
  854.     printf("%s=%s ", key, name);  
  855. }  
  856.   
  857. static void  
  858. load_locale_from_cache() {  
  859.     FILE* fp = fopen_path(LOCALE_FILE, "r");  
  860.     char buffer[80];  
  861.     if (fp != NULL) {  
  862.         fgets(buffer, sizeof(buffer), fp);  
  863.         int j = 0;  
  864.         unsigned int i;  
  865.         for (i = 0; i < sizeof(buffer) && buffer[i]; ++i) {  
  866.             if (!isspace(buffer[i])) {  
  867.                 buffer[j++] = buffer[i];  
  868.             }  
  869.         }  
  870.         buffer[j] = 0;  
  871.         locale = strdup(buffer);  
  872.         check_and_fclose(fp, LOCALE_FILE);  
  873.     }  
  874. }  
  875.   
  876. static RecoveryUI* gCurrentUI = NULL;  
  877.   
  878. void  
  879. ui_print(const char* format, ...) {  
  880.     char buffer[256];  
  881.   
  882.     va_list ap;  
  883.     va_start(ap, format);  
  884.     vsnprintf(buffer, sizeof(buffer), format, ap);  
  885.     va_end(ap);  
  886.   
  887.     if (gCurrentUI != NULL) {  
  888.         gCurrentUI->Print("%s", buffer);  
  889.     } else {  
  890.         fputs(buffer, stdout);  
  891.     }  
  892. }  
  893.   
  894. int  
  895. main(int argc, char **argv) {  
  896.     time_t start = time(NULL);  
  897.   
  898.     //重定向标准输出和标准出错到/tmp/recovery.log 这个文件里  
  899.     //static const char *TEMPORARY_LOG_FILE = "/tmp/recovery.log";  
  900.     redirect_stdio(TEMPORARY_LOG_FILE);  
  901.   
  902.     // If this binary is started with the single argument "--adbd",  
  903.     // instead of being the normal recovery binary, it turns into kind  
  904.     // of a stripped-down version of adbd that only supports the  
  905.     // 'sideload' command.  Note this must be a real argument, not  
  906.     // anything in the command file or bootloader control block; the  
  907.     // only way recovery should be run with this argument is when it  
  908.     // starts a copy of itself from the apply_from_adb() function.  
  909.     if (argc == 2 && strcmp(argv[1], "--adbd") == 0) {  
  910.         adb_main();  
  911.         return 0;  
  912.     }  
  913.   
  914.     printf("Starting recovery (pid %d) on %s", getpid(), ctime(&start));  
  915.     //装载recovery的分区表recovery.fstab  
  916.     load_volume_table();  
  917.     //在recovery中挂载/cache/recovery/last_log这个文件  
  918.     //#define LAST_LOG_FILE "/cache/recovery/last_log"  
  919.     ensure_path_mounted(LAST_LOG_FILE);  
  920.     rotate_last_logs(KEEP_LOG_COUNT);  
  921.     //获取参数  
  922.     //这个参数也可能是从/cache/recovery/command文件中得到相应的命令  
  923.     //也就是可以往command这个文件写入对应的格式的命令即可  
  924.     get_args(&argc, &argv);  
  925.   
  926.     const char *send_intent = NULL;  
  927.     const char *update_package = NULL;  
  928.     int wipe_data = 0, wipe_cache = 0, show_text = 0;  
  929.     bool just_exit = false;  
  930.     bool shutdown_after = false;  
  931.   
  932.     int arg;  
  933.     //参数有擦除分区,OTA更新等  
  934.     while ((arg = getopt_long(argc, argv, "", OPTIONS, NULL)) != -1) {  
  935.         switch (arg) {  
  936.         case 's': send_intent = optarg; break;  
  937.         case 'u': update_package = optarg; break;  
  938.         case 'w': wipe_data = wipe_cache = 1; break;  
  939.         case 'c': wipe_cache = 1; break;  
  940.         case 't': show_text = 1; break;  
  941.         case 'x': just_exit = true; break;  
  942.         case 'l': locale = optarg; break;  
  943.         case 'g': {  
  944.             if (stage == NULL || *stage == '') {  
  945.                 char buffer[20] = "1/";  
  946.                 strncat(buffer, optarg, sizeof(buffer)-3);  
  947.                 stage = strdup(buffer);  
  948.             }  
  949.             break;  
  950.         }  
  951.         case 'p': shutdown_after = true; break;  
  952.         case 'r': reason = optarg; break;  
  953.         case '?':  
  954.             LOGE("Invalid command argument ");  
  955.             continue;  
  956.         }  
  957.     }  
  958.     //设置语言  
  959.     if (locale == NULL) {  
  960.         load_locale_from_cache();  
  961.     }  
  962.     printf("locale is [%s] ", locale);  
  963.     printf("stage is [%s] ", stage);  
  964.     printf("reason is [%s] ", reason);  
  965.     //创建设备  
  966.     Device* device = make_device();  
  967.     //获取UI  
  968.     ui = device->GetUI();  
  969.     //设置当前的UI  
  970.     gCurrentUI = ui;  
  971.     //设置UI的语言信息  
  972.     ui->SetLocale(locale);  
  973.     //UI初始化  
  974.     ui->Init();  
  975.   
  976.     int st_cur, st_max;  
  977.     if (stage != NULL && sscanf(stage, "%d/%d", &st_cur, &st_max) == 2) {  
  978.         ui->SetStage(st_cur, st_max);  
  979.     }  
  980.     //设置recovery的背景图  
  981.     ui->SetBackground(RecoveryUI::NONE);  
  982.     //设置界面上是否能够显示字符,使能ui->print函数开关  
  983.     if (show_text) ui->ShowText(true);  
  984.     //设置selinux权限,一般我会把selinux 给disabled  
  985.     struct selinux_opt seopts[] = {  
  986.       { SELABEL_OPT_PATH, "/file_contexts" }  
  987.     };  
  988.   
  989.     sehandle = selabel_open(SELABEL_CTX_FILE, seopts, 1);  
  990.   
  991.     if (!sehandle) {  
  992.         ui->Print("Warning: No file_contexts ");  
  993.     }  
  994.     //虚函数,没有做什么流程  
  995.     device->StartRecovery();  
  996.   
  997.     printf("Command:");  
  998.     for (arg = 0; arg < argc; arg++) {  
  999.         printf(" "%s"", argv[arg]);  
  1000.     }  
  1001.     printf(" ");  
  1002.     //如果update_package(也就是要升级的OTA包)不为空的情况下  
  1003.     //这里要对升级包的路径做一下路径转换,这里可以自由定制自己升级包的路径  
  1004.     if (update_package) {  
  1005.         // For backwards compatibility on the cache partition only, if  
  1006.         // we're given an old 'root' path "CACHE:foo", change it to  
  1007.         // "/cache/foo".  
  1008.   
  1009.         //这里就是做转换的方法  
  1010.         //先比较传进来的recovery参数的前6个byte是否是CACHE  
  1011.         //如果是将其路径转化为/cache/CACHE: ......  
  1012.         if (strncmp(update_package, "CACHE:", 6) == 0) {  
  1013.             int len = strlen(update_package) + 10;  
  1014.             char* modified_path = (char*)malloc(len);  
  1015.             strlcpy(modified_path, "/cache/", len);  
  1016.             strlcat(modified_path, update_package+6, len);  
  1017.             printf("(replacing path "%s" with "%s") ",  
  1018.                    update_package, modified_path);  
  1019.             //这个update_package就是转换后的路径  
  1020.             update_package = modified_path;  
  1021.         }  
  1022.     }  
  1023.     printf(" ");  
  1024.     property_list(print_property, NULL);  
  1025.     //获取属性,这里应该是从一个文件中找到ro.build.display.id  
  1026.     //获取recovery的版本信息  
  1027.     property_get("ro.build.display.id", recovery_version, "");  
  1028.     printf(" ");  
  1029.   
  1030.     //定义一个安装成功的标志位INSTALL_SUCCESS  ----> 其实是个枚举,值为0  
  1031.     int status = INSTALL_SUCCESS;  
  1032.     //判断转换后的OTA升级包的路径是否不为空,如果不为空  
  1033.     //执行install_package 函数进行升级  
  1034.     if (update_package != NULL) {  
  1035.         status = install_package(update_package, &wipe_cache, TEMPORARY_INSTALL_FILE, true);  
  1036.         //判断是否升级成功  
  1037.         if (status == INSTALL_SUCCESS && wipe_cache) {  
  1038.             //擦除这个路径,相当于删除了这个路径下的OTA升级包  
  1039.             if (erase_volume("/cache")) {  
  1040.                 LOGE("Cache wipe (requested by package) failed.");  
  1041.             }  
  1042.         }  
  1043.         //如果安装不成功  
  1044.         if (status != INSTALL_SUCCESS) {  
  1045.             ui->Print("Installation aborted. ");  
  1046.   
  1047.             // If this is an eng or userdebug build, then automatically  
  1048.             // turn the text display on if the script fails so the error  
  1049.             // message is visible.  
  1050.             char buffer[PROPERTY_VALUE_MAX+1];  
  1051.             property_get("ro.build.fingerprint", buffer, "");  
  1052.             if (strstr(buffer, ":userdebug/") || strstr(buffer, ":eng/")) {  
  1053.                 ui->ShowText(true);  
  1054.             }  
  1055.         }  
  1056.     }  
  1057.     //如果跑的是格式化数据区,那么就走这个流程  
  1058.     else if (wipe_data) {  
  1059.         if (device->WipeData()) status = INSTALL_ERROR;  
  1060.         //格式化/data分区  
  1061.         if (erase_volume("/data")) status = INSTALL_ERROR;  
  1062.         if (wipe_cache && erase_volume("/cache")) status = INSTALL_ERROR;  
  1063.         if (erase_persistent_partition() == -1 ) status = INSTALL_ERROR;  
  1064.         if (status != INSTALL_SUCCESS) ui->Print("Data wipe failed. ");  
  1065.     }   
  1066.     //格式化cache分区  
  1067.     else if (wipe_cache) {  
  1068.         if (wipe_cache && erase_volume("/cache")) status = INSTALL_ERROR;  
  1069.         if (status != INSTALL_SUCCESS) ui->Print("Cache wipe failed. ");  
  1070.     }   
  1071.     else if (!just_exit) {  
  1072.         status = INSTALL_NONE;  // No command specified  
  1073.         ui->SetBackground(RecoveryUI::NO_COMMAND);  
  1074.     }  
  1075.     //如果安装失败或者。。。  
  1076.     if (status == INSTALL_ERROR || status == INSTALL_CORRUPT) {  
  1077.         copy_logs();  
  1078.         //显示错误的LOGO  
  1079.         ui->SetBackground(RecoveryUI::ERROR);  
  1080.     }  
  1081.     Device::BuiltinAction after = shutdown_after ? Device::SHUTDOWN : Device::REBOOT;  
  1082.     if (status != INSTALL_SUCCESS || ui->IsTextVisible()) {  
  1083.         Device::BuiltinAction temp = prompt_and_wait(device, status);  
  1084.         if (temp != Device::NO_ACTION) after = temp;  
  1085.     }  
  1086.       
  1087.     // Save logs and clean up before rebooting or shutting down.  
  1088.     //完成recovery升级  
  1089.     finish_recovery(send_intent);  
  1090.   
  1091.     switch (after) {  
  1092.         case Device::SHUTDOWN:  
  1093.             ui->Print("Shutting down... ");  
  1094.             property_set(ANDROID_RB_PROPERTY, "shutdown,");  
  1095.             break;  
  1096.   
  1097.         case Device::REBOOT_BOOTLOADER:  
  1098.             ui->Print("Rebooting to bootloader... ");  
  1099.             property_set(ANDROID_RB_PROPERTY, "reboot,bootloader");  
  1100.             break;  
  1101.   
  1102.         default:  
  1103.             ui->Print("Rebooting... ");  
  1104.             property_set(ANDROID_RB_PROPERTY, "reboot,");  
  1105.             break;  
  1106.     }  
  1107.     sleep(5); // should reboot before this finishes  
  1108.     return EXIT_SUCCESS;  
  1109. }  
原文地址:https://www.cnblogs.com/zzb-Dream-90Time/p/7300790.html