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 ...
随机推荐
- 1A课程笔记分享_StudyJams_2017
1A课程 概述 课程1A主要讲解了Android UI的三种基本控件:TextView.ImageView以及Button.笔记的主体内容主要根据课程内容的讲解顺序来组织,此外我对一些个人比较感兴趣的 ...
- Android HTTP 数据提交
在Android 项目中,使用HTTP协议获取数据或者处理数据,需要使用到多线程和配置相应的APP权限 1.使用线程,使用HTTP 提交数据 private Thread submitThread = ...
- ibatis知识点
1:ibatis是apache的一个开源的项目,是一个O/R mapping解决方案,优点,小巧,灵活.2:搭建环境:导入ibatis相关jar包,jdbc驱动包等3:配置文件: jdbc连接的属性文 ...
- VC++ 遍历文件夹
}; strcpy_s(szFind, MAX_PATH, m_szDir); strcat_s(szFind, "\\*.*"); WIN32_FIND_DATA wfd; HA ...
- (转)Arcgis for javascript实现百度地图ABCD marker的效果
概述: 在我的博客中,有一篇相关的文章,这段时间,有很多人问我求源码,只是时间过去已长,源代码已找不到,乘着这个9.3放假,又重新实现了下,并相关代码做了优化,在此贴出来,方便大家使用. 相关文章地址 ...
- apk的包名修改
今天,想在android手机上安装两个相同的应用,本以为可以安装不同版本的,试了几次,均相互覆盖了,于是,只能设法修改apk所对应的包名(package name). 目的声明:本文只是为了满足DIY ...
- (Entity framework 应用篇)把权限判断封装在数据库访问层
这里,我只是以一个例子,说一下简单权限控制,通过这个例子,大家可以设计庞大的权限管理层,把权限控制封装到数据库访问层,这样程序员就不用再写权限判断的代码了 首先,先看看我数据库DBContext的定义 ...
- mysql动态执行sql批量删除数据
CREATE PROCEDURE `sp_delete_pushmsg_data`() BEGIN ); ); declare l_dutyno int; ; ; ; ; day),'%Y-%m-%d ...
- Git学习总结一(下载、初始化、添加文件)
Git下载地址 安装完成后,还需要最后一步设置,在命令行输入: $ git config --global user.name "Your Name" $ git config - ...
- Sersync+Rsync实现数据文件实时同步
rsync+inotify-tools与rsync+sersync架构的区别1,rsync+inotify-tools只能记录下被监听的目录发生的变化(增删改)并没有把具体变化的文件或目录记录下来在同 ...