天天看點

recovery代碼分析之三:try_update_binary

        OTA更新包路徑META-INF\com\google\android中,存在着兩個關鍵的檔案:update-script和update-binary。在這兩個腳本檔案中,update-script記載着系統更新所需要執行的指令(如圖1所示),而update-binary則是對于每條指令的解析。進入recovery模式後,系統将會執行檔案中記載的指令,完成更新。

recovery代碼分析之三:try_update_binary

圖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模式下系統使用。

recovery代碼分析之三:try_update_binary

圖2 函數注冊

static int
try_update_binary(const char *path, ZipArchive *zip, int* wipe_cache) {
    const ZipEntry* binary_entry =
            mzFindZipEntry(zip, ASSUMED_UPDATE_BINARY_NAME);
    if (binary_entry == NULL) {
        mzCloseZipArchive(zip);
        return INSTALL_CORRUPT;
    }

    char* binary = "/tmp/update_binary";
    unlink(binary);
    int fd = creat(binary, 0755);
    if (fd < 0) {
        mzCloseZipArchive(zip);
        LOGE("Can't make %s\n", binary);
        return INSTALL_ERROR;
    }
    bool ok = mzExtractZipEntryToFile(zip, binary_entry, fd);
    close(fd);
    mzCloseZipArchive(zip);

    if (!ok) {
        LOGE("Can't copy %s\n", ASSUMED_UPDATE_BINARY_NAME);
        return INSTALL_ERROR;
    }

    int pipefd[2];
    pipe(pipefd);

    // When executing the update binary contained in the package, the
    // arguments passed are:
    //
    //   - the version number for this interface
    //
    //   - an fd to which the program can write in order to update the
    //     progress bar.  The program can write single-line commands:
    //
    //        progress <frac> <secs>
    //            fill up the next <frac> part of of the progress bar
    //            over <secs> seconds.  If <secs> is zero, use
    //            set_progress commands to manually control the
    //            progress of this segment of the bar
    //
    //        set_progress <frac>
    //            <frac> should be between 0.0 and 1.0; sets the
    //            progress bar within the segment defined by the most
    //            recent progress command.
    //
    //        firmware <"hboot"|"radio"> <filename>
    //            arrange to install the contents of <filename> in the
    //            given partition on reboot.
    //
    //            (API v2: <filename> may start with "PACKAGE:" to
    //            indicate taking a file from the OTA package.)
    //
    //            (API v3: this command no longer exists.)
    //
    //        ui_print <string>
    //            display <string> on the screen.
    //
    //   - the name of the package zip file.
    //

    char** args = malloc(sizeof(char*) * 5);
    args[0] = binary;
    args[1] = EXPAND(RECOVERY_API_VERSION);   // defined in Android.mk
    args[2] = malloc(10);
    sprintf(args[2], "%d", pipefd[1]);
    args[3] = (char*)path;
    args[4] = NULL;

    pid_t pid = fork();
    if (pid == 0) {
        close(pipefd[0]);
        execv(binary, args);
        fprintf(stdout, "E:Can't run %s (%s)\n", binary, strerror(errno));
        _exit(-1);
    }
    close(pipefd[1]);

    *wipe_cache = 0;

    char buffer[1024];
    FILE* from_child = fdopen(pipefd[0], "r");
    while (fgets(buffer, sizeof(buffer), from_child) != NULL) {
        char* command = strtok(buffer, " \n");
        if (command == NULL) {
            continue;
        } else if (strcmp(command, "progress") == 0) {
            char* fraction_s = strtok(NULL, " \n");
            char* seconds_s = strtok(NULL, " \n");

            float fraction = strtof(fraction_s, NULL);
            int seconds = strtol(seconds_s, NULL, 10);

            ui_show_progress(fraction * (1-VERIFICATION_PROGRESS_FRACTION),
                             seconds);
        } else if (strcmp(command, "set_progress") == 0) {
            char* fraction_s = strtok(NULL, " \n");
            float fraction = strtof(fraction_s, NULL);
            ui_set_progress(fraction);
        } else if (strcmp(command, "ui_print") == 0) {
            char* str = strtok(NULL, "\n");
            if (str) {
                ui_print("%s", str);
            } else {
                ui_print("\n");
            }
        } else if (strcmp(command, "wipe_cache") == 0) {
            *wipe_cache = 1;
        } else {
            LOGE("unknown command [%s]\n", command);
        }
    }
    fclose(from_child);

    int status;
    waitpid(pid, &status, 0);
    if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
        LOGE("Error in %s\n(Status %d)\n", path, WEXITSTATUS(status));
        return INSTALL_ERROR;
    }

    return INSTALL_SUCCESS;
}
           

代碼段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)。