Android系统Recovery工作原理之使用update.zip升级过程分析(八)---解析并执行升级脚本updater-script【转】
本文转载自: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脚本中的语法以及这个脚本在具体升级中的执行流程。
Android系统Recovery工作原理之使用update.zip升级过程分析(八)---解析并执行升级脚本updater-script【转】的更多相关文章
- Android系统Recovery工作原理之使用update.zip升级过程分析(九)---updater-script脚本语法简介以及执行流程【转】
本文转载自:http://blog.csdn.net/mu0206mu/article/details/7465603 Android系统Recovery工作原理之使用update.zip ...
- Android系统Recovery工作原理之使用update.zip升级过程分析(六)---Recovery服务流程细节【转】
本文转载自:http://blog.csdn.net/mu0206mu/article/details/7465439 Android系统Recovery工作原理之使用update.zip升级过程分 ...
- Android系统Recovery工作原理之使用update.zip升级过程分析(一)
通过分析update.zip包在具体Android系统升级的过程,来理解Android系统中Recovery模式服务的工作原理.我们先从update.zip包的制作开始,然后是Android系统的启动 ...
- Android系统Recovery工作原理之使用update.zip升级过程分析(一)---update.zip包的制作【转】
本文转载自:http://blog.csdn.net/mu0206mu/article/details/7399822 这篇及以后的篇幅将通过分析update.zip包在具体Android系统升级的过 ...
- Android系统Recovery工作原理之使用update.zip升级过程---updater-script脚本语法简介以及执行流程(转)
目前update-script脚本格式是edify,其与amend有何区别,暂不讨论,我们只分析其中主要的语法,以及脚本的流程控制. 一.update-script脚本语法简介: 我们顺着所生成的脚本 ...
- Android系统Recovery工作原理之使用update.zip升级过程分析(三)【转】
本文转载自:http://blog.csdn.net/mu0206mu/article/details/7464699 以下的篇幅开始分析我们在上两个篇幅中生成的update.zip包在具体更新中所经 ...
- Android系统Recovery工作原理之使用update.zip升级过程分析(七)---Recovery服务的核心install_package函数【转】
本文转载自:http://blog.csdn.net/mu0206mu/article/details/7465514 一. Recovery服务的核心install_package(升级 ...
- Android系统Recovery工作原理
Android系统Recovery工作原理之使用update.zip升级过程分析(一)---update.zip包的制作 http://blog.csdn.net/mu0206mu/article/d ...
- (转)Android 系统 root 破解原理分析
现在Android系统的root破解基本上成为大家的必备技能!网上也有很多中一键破解的软件,使root破解越来越容易.但是你思考过root破解的 原理吗?root破解的本质是什么呢?难道是利用了Lin ...
随机推荐
- React容器组件和展示组件
Presentational and Container Components 展示组件 - 只关心它们的样子. - 可能同时包含子级容器组件和展示组件,一般含DOM标签和自定的样式. ...
- 【PostgreSQL-9.6.3】触发器实例
1. 创建一个触发器,表中的行在任何时候被插入或更新时,当前用户名和时间也会被标记在该行中.并且它会检查雇员的姓名以及薪水. --创建测试表 CREATE TABLE emp ( empname te ...
- 【PL/SQL】用星号拼出金字塔
代码中首先声明了几个变量,然后使用嵌套循环去输出空格和星号,其中: 每层空格数=总层数-该层层数 每层星号数=当前层数*2-1 代码如下: declare v_number1 ); --外层循环控制金 ...
- c指针之内存释放
// 1.正常使用包含指针的结构体 // 2.正常使用元素类型为指针的vector #include<string.h> #include<stdio.h> #include& ...
- Matrix computations in C
meschach配置使用 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !im ...
- antiSMASH数据库:微生物次生代谢物合成基因组簇查询和预测
2017年4月28日,核酸研究(Nucleic Acids Research)杂志上,在线公布了一个可搜索微生物次生代谢物合成基因组簇的综合性数据库antiSMASH数据库 4.0版,前3版年均引用2 ...
- Vue和JQuery相比,除了节省了开发成本,还有什么优点?
1.模块化,变量都是私有作用域,JQuery只能用全局变量.闭包,影响性能 2.组件化 3.因为1,所以方便维护 vuex 要注意刷新清空的问题 vue-router是局部刷新,window.loca ...
- pandas写入多组数据到excel不同的sheet
今天朋友问了我个需求,就是如何将多个分析后的结果,也就是多个DataFrame,写入同一个excel工作簿中呢? 之前我只写过放在一个sheet中,但是怎么放在多个sheet中呢?下面我在本地wind ...
- Linux之强大的selinux
简单点说,SELinux就是用来加强系统安全性的.它给一些特定程序(这些程序也在不断增加)做了一个沙箱,它将文件打上了一个安全标签,这些标签属于不同的类,也只能执行特定的操作,也就是规定了某个应用程序 ...
- 基本数据类型:字符串(str)
一.字符串的定义和创建 字符串是一个有序的字符的集合,用于存储和表示基本的文本信息,' '或'' ''或''' '''中间包含的内容称之为字符串,总之加了引号的字符都被认为是字符串! 创建: > ...