Android系统Recovery工作原理之使用update.zip升级过程分析(八)---解析并执行升级脚本updater-script【转】

时间:2021-12-07 21:46:59

本文转载自: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,源码如下:

  1. /*
  2. * Copyright (C) 2009 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. #include <stdio.h>
  17. #include <unistd.h>
  18. #include <stdlib.h>
  19. #include "edify/expr.h"
  20. #include "updater.h"
  21. #include "install.h"
  22. #include "minzip/Zip.h"
  23. // Generated by the makefile, this function defines the
  24. // RegisterDeviceExtensions() function, which calls all the
  25. // registration functions for device-specific extensions.
  26. #include "register.inc"
  27. // Where in the package we expect to find the edify script to execute.
  28. // (Note it's "updateR-script", not the older "update-script".)
  29. #define SCRIPT_NAME "META-INF/com/google/android/updater-script"
  30. int main(int argc, char** argv) {
  31. // Various things log information to stdout or stderr more or less
  32. // at random.  The log file makes more sense if buffering is
  33. // turned off so things appear in the right order.
  34. setbuf(stdout, NULL);
  35. setbuf(stderr, NULL);
  36. if (argc != 4) {
  37. fprintf(stderr, "unexpected number of arguments (%d)\n", argc);
  38. return 1;
  39. }
  40. char* version = argv[1];
  41. if ((version[0] != '1' && version[0] != '2' && version[0] != '3') ||
  42. version[1] != '\0') {
  43. // We support version 1, 2, or 3.
  44. fprintf(stderr, "wrong updater binary API; expected 1, 2, or 3; "
  45. "got %s\n",
  46. argv[1]);
  47. return 2;
  48. }
  49. // Set up the pipe for sending commands back to the parent process.
  50. int fd = atoi(argv[2]);
  51. FILE* cmd_pipe = fdopen(fd, "wb");
  52. setlinebuf(cmd_pipe);
  53. // Extract the script from the package.
  54. char* package_data = argv[3];
  55. ZipArchive za;
  56. int err;
  57. err = mzOpenZipArchive(package_data, &za);
  58. if (err != 0) {
  59. fprintf(stderr, "failed to open package %s: %s\n",
  60. package_data, strerror(err));
  61. return 3;
  62. }
  63. const ZipEntry* script_entry = mzFindZipEntry(&za, SCRIPT_NAME);
  64. if (script_entry == NULL) {
  65. fprintf(stderr, "failed to find %s in %s\n", SCRIPT_NAME, package_data);
  66. return 4;
  67. }
  68. char* script = malloc(script_entry->uncompLen+1);
  69. if (!mzReadZipEntry(&za, script_entry, script, script_entry->uncompLen)) {
  70. fprintf(stderr, "failed to read script from package\n");
  71. return 5;
  72. }
  73. script[script_entry->uncompLen] = '\0';
  74. // Configure edify's functions.
  75. RegisterBuiltins();
  76. RegisterInstallFunctions();
  77. RegisterDeviceExtensions();
  78. FinishRegistration();
  79. // Parse the script.
  80. Expr* root;
  81. int error_count = 0;
  82. yy_scan_string(script);
  83. int error = yyparse(&root, &error_count);
  84. if (error != 0 || error_count > 0) {
  85. fprintf(stderr, "%d parse errors\n", error_count);
  86. return 6;
  87. }
  88. // Evaluate the parsed script.
  89. UpdaterInfo updater_info;
  90. updater_info.cmd_pipe = cmd_pipe;
  91. updater_info.package_zip = &za;
  92. updater_info.version = atoi(version);
  93. State state;
  94. state.cookie = &updater_info;
  95. state.script = script;
  96. state.errmsg = NULL;
  97. char* result = Evaluate(&state, root);
  98. if (result == NULL) {
  99. if (state.errmsg == NULL) {
  100. fprintf(stderr, "script aborted (no error message)\n");
  101. fprintf(cmd_pipe, "ui_print script aborted (no error message)\n");
  102. } else {
  103. fprintf(stderr, "script aborted: %s\n", state.errmsg);
  104. char* line = strtok(state.errmsg, "\n");
  105. while (line) {
  106. fprintf(cmd_pipe, "ui_print %s\n", line);
  107. line = strtok(NULL, "\n");
  108. }
  109. fprintf(cmd_pipe, "ui_print\n");
  110. }
  111. free(state.errmsg);
  112. return 7;
  113. } else {
  114. fprintf(stderr, "script result was [%s]\n", result);
  115. free(result);
  116. }
  117. if (updater_info.package_zip) {
  118. mzCloseZipArchive(updater_info.package_zip);
  119. }
  120. free(script);
  121. return 0;
  122. }

通过上面的源码来分析下这个程序的执行过程:

①函数参数以及版本的检查:当前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脚本中的语法以及这个脚本在具体升级中的执行流程。