zoukankan      html  css  js  c++  java
  • Android系统Recovery工作原理之使用update.zip升级过程分析(七)---Recovery服务的核心install_package函数【转】

    本文转载自:http://blog.csdn.net/mu0206mu/article/details/7465514

    一、       Recovery服务的核心install_package(升级update.zip特有)

                  和Recovery服务中的wipe_data、wipe_cache不同,install_package()是升级update.zip特有的一部分,也是最核心的部分。在这一步才真正开始对我们的update.zip包进行处理。下面就开始分析这一部分。还是先看图例:

                              

                这一部分的源码文件位于:/gingerbread0919/bootable/recovery/install.c。这是一个没有main函数的源码文件,还是把源码先贴出来如下:

    [cpp] view plain copy
     
    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 <errno.h>  
    19. #include <fcntl.h>  
    20. #include <limits.h>  
    21. #include <sys/stat.h>  
    22. #include <sys/wait.h>  
    23. #include <unistd.h>  
    24.   
    25. #include "common.h"  
    26. #include "install.h"  
    27. #include "mincrypt/rsa.h"  
    28. #include "minui/minui.h"  
    29. #include "minzip/SysUtil.h"  
    30. #include "minzip/Zip.h"  
    31. #include "mtdutils/mounts.h"  
    32. #include "mtdutils/mtdutils.h"  
    33. #include "roots.h"  
    34. #include "verifier.h"  
    35.   
    36. #define ASSUMED_UPDATE_BINARY_NAME  "META-INF/com/google/android/update-binary"  
    37. #define PUBLIC_KEYS_FILE "/res/keys"  
    38.   
    39. // If the package contains an update binary, extract it and run it.  
    40. static int  
    41. try_update_binary(const char *path, ZipArchive *zip) {  
    42.     const ZipEntry* binary_entry =  
    43.             mzFindZipEntry(zip, ASSUMED_UPDATE_BINARY_NAME);  
    44.     if (binary_entry == NULL) {  
    45.         mzCloseZipArchive(zip);  
    46.         return INSTALL_CORRUPT;  
    47.     }  
    48.   
    49.     char* binary = "/tmp/update_binary";  
    50.     unlink(binary);  
    51.     int fd = creat(binary, 0755);  
    52.     if (fd < 0) {  
    53.         mzCloseZipArchive(zip);  
    54.         LOGE("Can't make %s ", binary);  
    55.         return 1;  
    56.     }  
    57.     bool ok = mzExtractZipEntryToFile(zip, binary_entry, fd);  
    58.     close(fd);  
    59.     mzCloseZipArchive(zip);  
    60.   
    61.     if (!ok) {  
    62.         LOGE("Can't copy %s ", ASSUMED_UPDATE_BINARY_NAME);  
    63.         return 1;  
    64.     }  
    65.   
    66.     int pipefd[2];  
    67.     pipe(pipefd);  
    68.   
    69.     // When executing the update binary contained in the package, the  
    70.     // arguments passed are:  
    71.     //  
    72.     //   - the version number for this interface  
    73.     //  
    74.     //   - an fd to which the program can write in order to update the  
    75.     //     progress bar.  The program can write single-line commands:  
    76.     //  
    77.     //        progress <frac> <secs>  
    78.     //            fill up the next <frac> part of of the progress bar  
    79.     //            over <secs> seconds.  If <secs> is zero, use  
    80.     //            set_progress commands to manually control the  
    81.     //            progress of this segment of the bar  
    82.     //  
    83.     //        set_progress <frac>  
    84.     //            <frac> should be between 0.0 and 1.0; sets the  
    85.     //            progress bar within the segment defined by the most  
    86.     //            recent progress command.  
    87.     //  
    88.     //        firmware <"hboot"|"radio"> <filename>  
    89.     //            arrange to install the contents of <filename> in the  
    90.     //            given partition on reboot.  
    91.     //  
    92.     //            (API v2: <filename> may start with "PACKAGE:" to  
    93.     //            indicate taking a file from the OTA package.)  
    94.     //  
    95.     //            (API v3: this command no longer exists.)  
    96.     //  
    97.     //        ui_print <string>  
    98.     //            display <string> on the screen.  
    99.     //  
    100.     //   - the name of the package zip file.  
    101.     //  
    102.   
    103.     char** args = malloc(sizeof(char*) * 5);  
    104.     args[0] = binary;  
    105.     args[1] = EXPAND(RECOVERY_API_VERSION);   // defined in Android.mk  
    106.     args[2] = malloc(10);  
    107.     sprintf(args[2], "%d", pipefd[1]);  
    108.     args[3] = (char*)path;  
    109.     args[4] = NULL;  
    110.   
    111.     pid_t pid = fork();  
    112.     if (pid == 0) {  
    113.         close(pipefd[0]);  
    114.         execv(binary, args);  
    115.         fprintf(stdout, "E:Can't run %s (%s) ", binary, strerror(errno));  
    116.         _exit(-1);  
    117.     }  
    118.     close(pipefd[1]);  
    119.   
    120.     char buffer[1024];  
    121.     FILE* from_child = fdopen(pipefd[0], "r");  
    122.     while (fgets(buffer, sizeof(buffer), from_child) != NULL) {  
    123.         char* command = strtok(buffer, "  ");  
    124.         if (command == NULL) {  
    125.             continue;  
    126.         } else if (strcmp(command, "progress") == 0) {  
    127.             char* fraction_s = strtok(NULL, "  ");  
    128.             char* seconds_s = strtok(NULL, "  ");  
    129.   
    130.             float fraction = strtof(fraction_s, NULL);  
    131.             int seconds = strtol(seconds_s, NULL, 10);  
    132.   
    133.             ui_show_progress(fraction * (1-VERIFICATION_PROGRESS_FRACTION),  
    134.                              seconds);  
    135.         } else if (strcmp(command, "set_progress") == 0) {  
    136.             char* fraction_s = strtok(NULL, "  ");  
    137.             float fraction = strtof(fraction_s, NULL);  
    138.             ui_set_progress(fraction);  
    139.         } else if (strcmp(command, "ui_print") == 0) {  
    140.             char* str = strtok(NULL, " ");  
    141.             if (str) {  
    142.                 ui_print("%s", str);  
    143.             } else {  
    144.                 ui_print(" ");  
    145.             }  
    146.         } else {  
    147.             LOGE("unknown command [%s] ", command);  
    148.         }  
    149.     }  
    150.     fclose(from_child);  
    151.   
    152.     int status;  
    153.     waitpid(pid, &status, 0);  
    154.     if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {  
    155.         LOGE("Error in %s (Status %d) ", path, WEXITSTATUS(status));  
    156.         return INSTALL_ERROR;  
    157.     }  
    158.   
    159.     return INSTALL_SUCCESS;  
    160. }  
    161.   
    162. // Reads a file containing one or more public keys as produced by  
    163. // DumpPublicKey:  this is an RSAPublicKey struct as it would appear  
    164. // as a C source literal, eg:  
    165. //  
    166. //  "{64,0xc926ad21,{1795090719,...,-695002876},{-857949815,...,1175080310}}"  
    167. //  
    168. // (Note that the braces and commas in this example are actual  
    169. // characters the parser expects to find in the file; the ellipses  
    170. // indicate more numbers omitted from this example.)  
    171. //  
    172. // The file may contain multiple keys in this format, separated by  
    173. // commas.  The last key must not be followed by a comma.  
    174. //  
    175. // Returns NULL if the file failed to parse, or if it contain zero keys.  
    176. static RSAPublicKey*  
    177. load_keys(const char* filename, int* numKeys) {  
    178.     RSAPublicKey* out = NULL;  
    179.     *numKeys = 0;  
    180.   
    181.     FILE* f = fopen(filename, "r");  
    182.     if (f == NULL) {  
    183.         LOGE("opening %s: %s ", filename, strerror(errno));  
    184.         goto exit;  
    185.     }  
    186.   
    187.     int i;  
    188.     bool done = false;  
    189.     while (!done) {  
    190.         ++*numKeys;  
    191.         out = realloc(out, *numKeys * sizeof(RSAPublicKey));  
    192.         RSAPublicKey* key = out + (*numKeys - 1);  
    193.         if (fscanf(f, " { %i , 0x%x , { %u",  
    194.                    &(key->len), &(key->n0inv), &(key->n[0])) != 3) {  
    195.             goto exit;  
    196.         }  
    197.         if (key->len != RSANUMWORDS) {  
    198.             LOGE("key length (%d) does not match expected size ", key->len);  
    199.             goto exit;  
    200.         }  
    201.         for (i = 1; i < key->len; ++i) {  
    202.             if (fscanf(f, " , %u", &(key->n[i])) != 1) goto exit;  
    203.         }  
    204.         if (fscanf(f, " } , { %u", &(key->rr[0])) != 1) goto exit;  
    205.         for (i = 1; i < key->len; ++i) {  
    206.             if (fscanf(f, " , %u", &(key->rr[i])) != 1) goto exit;  
    207.         }  
    208.         fscanf(f, " } } ");  
    209.   
    210.         // if the line ends in a comma, this file has more keys.  
    211.         switch (fgetc(f)) {  
    212.             case ',':  
    213.                 // more keys to come.  
    214.                 break;  
    215.   
    216.             case EOF:  
    217.                 done = true;  
    218.                 break;  
    219.   
    220.             default:  
    221.                 LOGE("unexpected character between keys ");  
    222.                 goto exit;  
    223.         }  
    224.     }  
    225.   
    226.     fclose(f);  
    227.     return out;  
    228.   
    229. exit:  
    230.     if (f) fclose(f);  
    231.     free(out);  
    232.     *numKeys = 0;  
    233.     return NULL;  
    234. }  
    235.   
    236. int  
    237. install_package(const char *path)  
    238. {  
    239.     ui_set_background(BACKGROUND_ICON_INSTALLING);  
    240.     ui_print("Finding update package... ");  
    241.     ui_show_indeterminate_progress();  
    242.     LOGI("Update location: %s ", path);  
    243.   
    244.     if (ensure_path_mounted(path) != 0) {  
    245.         LOGE("Can't mount %s ", path);  
    246.         return INSTALL_CORRUPT;  
    247.     }  
    248.   
    249.     ui_print("Opening update package... ");  
    250.   
    251.     int numKeys;  
    252.     RSAPublicKey* loadedKeys = load_keys(PUBLIC_KEYS_FILE, &numKeys);  
    253.     if (loadedKeys == NULL) {  
    254.         LOGE("Failed to load keys ");  
    255.         return INSTALL_CORRUPT;  
    256.     }  
    257.     LOGI("%d key(s) loaded from %s ", numKeys, PUBLIC_KEYS_FILE);  
    258.   
    259.     // Give verification half the progress bar...  
    260.     ui_print("Verifying update package... ");  
    261.     ui_show_progress(  
    262.             VERIFICATION_PROGRESS_FRACTION,  
    263.             VERIFICATION_PROGRESS_TIME);  
    264.   
    265.     int err;  
    266.     err = verify_file(path, loadedKeys, numKeys);  
    267.     free(loadedKeys);  
    268.     LOGI("verify_file returned %d ", err);  
    269.     if (err != VERIFY_SUCCESS) {  
    270.         LOGE("signature verification failed ");  
    271.         return INSTALL_CORRUPT;  
    272.     }  
    273.   
    274.     /* Try to open the package. 
    275.      */  
    276.     ZipArchive zip;  
    277.     err = mzOpenZipArchive(path, &zip);  
    278.     if (err != 0) {  
    279.         LOGE("Can't open %s (%s) ", path, err != -1 ? strerror(err) : "bad");  
    280.         return INSTALL_CORRUPT;  
    281.     }  
    282.   
    283.     /* Verify and install the contents of the package. 
    284.      */  
    285.     ui_print("Installing update... ");  
    286.     return try_update_binary(path, &zip);  
    287. }  



                 下面顺着上面的流程图和源码来分析这一流程:

                ①ensure_path_mount():先判断所传的update.zip包路径所在的分区是否已经挂载。如果没有则先挂载。

                ②load_keys():加载公钥源文件,路径位于/res/keys。这个文件在Recovery镜像的根文件系统中。

                ③verify_file():对升级包update.zip包进行签名验证。

                ④mzOpenZipArchive():打开升级包,并将相关的信息拷贝到一个临时的ZipArchinve变量中。这一步并未对我们的update.zip包解压。

                ⑤try_update_binary():在这个函数中才是对我们的update.zip升级的地方。这个函数一开始先根据我们上一步获得的zip包信息,以及升级包的绝对路径将update_binary文件拷贝到内存文件系统的/tmp/update_binary中。以便后面使用。

                ⑥pipe():创建管道,用于下面的子进程和父进程之间的通信。

                ⑦fork():创建子进程。其中的子进程主要负责执行binary(execv(binary,args),即执行我们的安装命令脚本),父进程负责接受子进程发送的命令去更新ui显示(显示当前的进度)。子父进程间通信依靠管道。

                ⑧其中,在创建子进程后,父进程有两个作用。一是通过管道接受子进程发送的命令来更新UI显示。二是等待子进程退出并返回INSTALL SUCCESS。其中子进程在解析执行安装脚本的同时所发送的命令有以下几种:

                           progress  <frac> <secs>:根据第二个参数secs(秒)来设置进度条。

                           set_progress  <frac>:直接设置进度条,frac取值在0.0到0.1之间。

                           firmware <”hboot”|”radio”><filename>:升级firmware时使用,在API  V3中不再使用。

                           ui_print <string>:在屏幕上显示字符串,即打印更新过程。

                     execv(binary,args)的作用就是去执行binary程序,这个程序的实质就是去解析update.zip包中的updater-script脚本中的命令并执行。由此,Recovery服务就进入了实际安装update.zip包的过程。

                     下一篇继续分析使用update-binary解析并执行updater-script的过程。

  • 相关阅读:
    MySql 获取当前节点及递归所有上级节点
    MySql创建树结构递归查询存储过程
    F2工作流引擎Web层全新扁平化UI上线
    F2工作流引擎参与者类型成员的交、并、互拆计算规则
    F2工作流引擎之组织用户模型(四)
    F2工作流引擎之 工作流运转模型(三)
    F2工作流引擎之 概述(一)
    离线安装docker,并导入docker镜像
    sudo: /usr/bin/sudo must be owned by uid 0 and have the setuid bit set 的解决办法
    yml 文件中使用环境变量
  • 原文地址:https://www.cnblogs.com/zzb-Dream-90Time/p/7298796.html
Copyright © 2011-2022 走看看