本文转载自:http://blog.csdn.net/mu0206mu/article/details/7465551
Android系统Recovery工作原理之使用update.zip升级过程分析(八)---升级程序update_binary的执行过程
一、update_binary的执行过程分析
上一篇幅中的子进程所执行的程序binary实际上就是update.zip包中的update-binary。我们在上文中也说过,Recovery服务在做这一部分工作的时候是先将包中update-binary拷贝到内存文件系统中的/tmp/update_binary,然后再执行的。update_binary程序的源码位于gingerbread0919/bootable/recovery/updater/updater.c,源码如下:
- /*
- * Copyright (C) 2009 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- #include <stdio.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include "edify/expr.h"
- #include "updater.h"
- #include "install.h"
- #include "minzip/Zip.h"
- // Generated by the makefile, this function defines the
- // RegisterDeviceExtensions() function, which calls all the
- // registration functions for device-specific extensions.
- #include "register.inc"
- // Where in the package we expect to find the edify script to execute.
- // (Note it's "updateR-script", not the older "update-script".)
- #define SCRIPT_NAME "META-INF/com/google/android/updater-script"
- int main(int argc, char** argv) {
- // Various things log information to stdout or stderr more or less
- // at random. The log file makes more sense if buffering is
- // turned off so things appear in the right order.
- setbuf(stdout, NULL);
- setbuf(stderr, NULL);
- if (argc != 4) {
- fprintf(stderr, "unexpected number of arguments (%d)\n", argc);
- return 1;
- }
- char* version = argv[1];
- if ((version[0] != '1' && version[0] != '2' && version[0] != '3') ||
- version[1] != '\0') {
- // We support version 1, 2, or 3.
- fprintf(stderr, "wrong updater binary API; expected 1, 2, or 3; "
- "got %s\n",
- argv[1]);
- return 2;
- }
- // Set up the pipe for sending commands back to the parent process.
- int fd = atoi(argv[2]);
- FILE* cmd_pipe = fdopen(fd, "wb");
- setlinebuf(cmd_pipe);
- // Extract the script from the package.
- char* package_data = argv[3];
- ZipArchive za;
- int err;
- err = mzOpenZipArchive(package_data, &za);
- if (err != 0) {
- fprintf(stderr, "failed to open package %s: %s\n",
- package_data, strerror(err));
- return 3;
- }
- const ZipEntry* script_entry = mzFindZipEntry(&za, SCRIPT_NAME);
- if (script_entry == NULL) {
- fprintf(stderr, "failed to find %s in %s\n", SCRIPT_NAME, package_data);
- return 4;
- }
- char* script = malloc(script_entry->uncompLen+1);
- if (!mzReadZipEntry(&za, script_entry, script, script_entry->uncompLen)) {
- fprintf(stderr, "failed to read script from package\n");
- return 5;
- }
- script[script_entry->uncompLen] = '\0';
- // Configure edify's functions.
- RegisterBuiltins();
- RegisterInstallFunctions();
- RegisterDeviceExtensions();
- FinishRegistration();
- // Parse the script.
- Expr* root;
- int error_count = 0;
- yy_scan_string(script);
- int error = yyparse(&root, &error_count);
- if (error != 0 || error_count > 0) {
- fprintf(stderr, "%d parse errors\n", error_count);
- return 6;
- }
- // Evaluate the parsed script.
- UpdaterInfo updater_info;
- updater_info.cmd_pipe = cmd_pipe;
- updater_info.package_zip = &za;
- updater_info.version = atoi(version);
- State state;
- state.cookie = &updater_info;
- state.script = script;
- state.errmsg = NULL;
- char* result = Evaluate(&state, root);
- if (result == NULL) {
- if (state.errmsg == NULL) {
- fprintf(stderr, "script aborted (no error message)\n");
- fprintf(cmd_pipe, "ui_print script aborted (no error message)\n");
- } else {
- fprintf(stderr, "script aborted: %s\n", state.errmsg);
- char* line = strtok(state.errmsg, "\n");
- while (line) {
- fprintf(cmd_pipe, "ui_print %s\n", line);
- line = strtok(NULL, "\n");
- }
- fprintf(cmd_pipe, "ui_print\n");
- }
- free(state.errmsg);
- return 7;
- } else {
- fprintf(stderr, "script result was [%s]\n", result);
- free(result);
- }
- if (updater_info.package_zip) {
- mzCloseZipArchive(updater_info.package_zip);
- }
- free(script);
- return 0;
- }
通过上面的源码来分析下这个程序的执行过程:
①函数参数以及版本的检查:当前updater binary API所支持的版本号有1,2,3这三个。
②获取管道并打开:在执行此程序的过程中向该管道写入命令,用于通知其父进程根据命令去更新UI显示。
③读取updater-script脚本:从update.zip包中将updater-script脚本读到一块动态内存中,供后面执行。
④Configure edify’s functions:注册脚本中的语句处理函数,即识别脚本中命令的函数。主要有以下几类
RegisterBuiltins():注册程序中控制流程的语句,如ifelse、assert、abort、stdout等。
RegisterInstallFunctions():实际安装过程中安装所需的功能函数,比如mount、format、set_progress、set_perm等等。
RegisterDeviceExtensions():与设备相关的额外添加項,在源码中并没有任何实现。
FinishRegistration():结束注册。
⑤Parsethe script:调用yy*库函数解析脚本,并将解析后的内容存放到一个Expr类型的Python类中。主要函数是yy_scan_string()和yyparse()。
⑥执行脚本:核心函数是Evaluate(),它会调用其他的callback函数,而这些callback函数又会去调用Evaluate去解析不同的脚本片段,从而实现一个简单的脚本解释器。
⑦错误信息提示:最后就是根据Evaluate()执行后的返回值,给出一些打印信息。
这一执行过程非常简单,最主要的函数就是Evaluate。它负责最终执行解析的脚本命令。而安装过程中的命令就是updater-script。
下一篇幅将介绍updater-script脚本中的语法以及这个脚本在具体升级中的执行流程。