recovery代码分析之三:try_update_binary

来源:互联网 发布:韩顺平 php大牛班课程 编辑:程序博客网 时间:2024/05/18 01:38

http://blog.csdn.net/sjz_iron/article/details/8861773

OTA升级包路径META-INF\com\google\android中,存在着两个关键的文件:update-script和update-binary。在这两个脚本文件中,update-script记载着系统升级所需要执行的命令(如图1所示),而update-binary则是对于每条命令的解析。进入recovery模式后,系统将会执行文件中记载的命令,完成升级。

图1 update-script内容截图

由http://blog.csdn.net/liudekuan/article/details/8707044可知,在文件./bootable/recovery/install.c中定义了对应于每条命令的执行函数(如图2所示),即在recovery模式下

,系统会将这些命令转换为相应的函数去执行。而RegisterInstallFunctions函数在./bootable/recovery/update.c中被调用,在源码编译过程中,./bootable/recovery/updater目录下的代码被编译为可执行文件:out/target/product/sp8825ea/system/bin/updater,供recovery模式下系统使用。

图2 函数注册

[cpp] view plaincopyprint?
  1. static int  
  2. try_update_binary(const char *path, ZipArchive *zip, int* wipe_cache) {  
  3.     const ZipEntry* binary_entry =  
  4.             mzFindZipEntry(zip, ASSUMED_UPDATE_BINARY_NAME);  
  5.     if (binary_entry == NULL) {  
  6.         mzCloseZipArchive(zip);  
  7.         return INSTALL_CORRUPT;  
  8.     }  
  9.   
  10.     char* binary = "/tmp/update_binary";  
  11.     unlink(binary);  
  12.     int fd = creat(binary, 0755);  
  13.     if (fd < 0) {  
  14.         mzCloseZipArchive(zip);  
  15.         LOGE("Can't make %s\n", binary);  
  16.         return INSTALL_ERROR;  
  17.     }  
  18.     bool ok = mzExtractZipEntryToFile(zip, binary_entry, fd);  
  19.     close(fd);  
  20.     mzCloseZipArchive(zip);  
  21.   
  22.     if (!ok) {  
  23.         LOGE("Can't copy %s\n", ASSUMED_UPDATE_BINARY_NAME);  
  24.         return INSTALL_ERROR;  
  25.     }  
  26.   
  27.     int pipefd[2];  
  28.     pipe(pipefd);  
  29.   
  30.     // When executing the update binary contained in the package, the  
  31.     // arguments passed are:  
  32.     //  
  33.     //   - the version number for this interface  
  34.     //  
  35.     //   - an fd to which the program can write in order to update the  
  36.     //     progress bar.  The program can write single-line commands:  
  37.     //  
  38.     //        progress <frac> <secs>  
  39.     //            fill up the next <frac> part of of the progress bar  
  40.     //            over <secs> seconds.  If <secs> is zero, use  
  41.     //            set_progress commands to manually control the  
  42.     //            progress of this segment of the bar  
  43.     //  
  44.     //        set_progress <frac>  
  45.     //            <frac> should be between 0.0 and 1.0; sets the  
  46.     //            progress bar within the segment defined by the most  
  47.     //            recent progress command.  
  48.     //  
  49.     //        firmware <"hboot"|"radio"> <filename>  
  50.     //            arrange to install the contents of <filename> in the  
  51.     //            given partition on reboot.  
  52.     //  
  53.     //            (API v2: <filename> may start with "PACKAGE:" to  
  54.     //            indicate taking a file from the OTA package.)  
  55.     //  
  56.     //            (API v3: this command no longer exists.)  
  57.     //  
  58.     //        ui_print <string>  
  59.     //            display <string> on the screen.  
  60.     //  
  61.     //   - the name of the package zip file.  
  62.     //  
  63.   
  64.     char** args = malloc(sizeof(char*) * 5);  
  65.     args[0] = binary;  
  66.     args[1] = EXPAND(RECOVERY_API_VERSION);   // defined in Android.mk  
  67.     args[2] = malloc(10);  
  68.     sprintf(args[2], "%d", pipefd[1]);  
  69.     args[3] = (char*)path;  
  70.     args[4] = NULL;  
  71.   
  72.     pid_t pid = fork();  
  73.     if (pid == 0) {  
  74.         close(pipefd[0]);  
  75.         execv(binary, args);  
  76.         fprintf(stdout, "E:Can't run %s (%s)\n", binary, strerror(errno));  
  77.         _exit(-1);  
  78.     }  
  79.     close(pipefd[1]);  
  80.   
  81.     *wipe_cache = 0;  
  82.   
  83.     char buffer[1024];  
  84.     FILE* from_child = fdopen(pipefd[0], "r");  
  85.     while (fgets(buffer, sizeof(buffer), from_child) != NULL) {  
  86.         char* command = strtok(buffer, " \n");  
  87.         if (command == NULL) {  
  88.             continue;  
  89.         } else if (strcmp(command, "progress") == 0) {  
  90.             char* fraction_s = strtok(NULL, " \n");  
  91.             char* seconds_s = strtok(NULL, " \n");  
  92.   
  93.             float fraction = strtof(fraction_s, NULL);  
  94.             int seconds = strtol(seconds_s, NULL, 10);  
  95.   
  96.             ui_show_progress(fraction * (1-VERIFICATION_PROGRESS_FRACTION),  
  97.                              seconds);  
  98.         } else if (strcmp(command, "set_progress") == 0) {  
  99.             char* fraction_s = strtok(NULL, " \n");  
  100.             float fraction = strtof(fraction_s, NULL);  
  101.             ui_set_progress(fraction);  
  102.         } else if (strcmp(command, "ui_print") == 0) {  
  103.             char* str = strtok(NULL, "\n");  
  104.             if (str) {  
  105.                 ui_print("%s", str);  
  106.             } else {  
  107.                 ui_print("\n");  
  108.             }  
  109.         } else if (strcmp(command, "wipe_cache") == 0) {  
  110.             *wipe_cache = 1;  
  111.         } else {  
  112.             LOGE("unknown command [%s]\n", command);  
  113.         }  
  114.     }  
  115.     fclose(from_child);  
  116.   
  117.     int status;  
  118.     waitpid(pid, &status, 0);  
  119.     if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {  
  120.         LOGE("Error in %s\n(Status %d)\n", path, WEXITSTATUS(status));  
  121.         return INSTALL_ERROR;  
  122.     }  
  123.   
  124.     return INSTALL_SUCCESS;  
  125. }  

代码段1 try_update_binary函数

        ./bootable/recovery/install.c中的try_update_binary函数,是真正实现读取升级包中的脚本文件并执行相应的函数的地方。在此函数中,通过调用fork函数创建出一个子进程(代码第72行),在子进程中开始读取并执行升级脚本文件(代码73-78)。在此需要注意的是函数fork的用法,fork被调用一次,将做两次返回,在父进程中返回的是子进程的进程ID,为正数;而在子进程中,则返回0。因此代码73-78事实是子进程中所进行的操作,即execv(binary, args)。子进程创建成功后,开始执行升级代码,并通过管道与父进程交互(代码27-28,创建管道,并将pipefd[1]作为参数传递给子进程,子进程则将相关信息写入到此管道描述符中);而父进程则通过读取子进程传递过来的信息更新UI(代码84-114)。

0 0
原创粉丝点击