1 /*
2 * Copyright (C) 2007 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
17 #include <ctype.h>
18 #include <errno.h>
19 #include <fcntl.h>
20 #include <limits.h>
21 #include <string.h>
22 #include <sys/stat.h>
23 #include <sys/wait.h>
24 #include <unistd.h>
25
26 #include "common.h"
27 #include "install.h"
28 #include "mincrypt/rsa.h"
29 #include "minui/minui.h"
30 #include "minzip/SysUtil.h"
31 #include "minzip/Zip.h"
32 #include "mtdutils/mounts.h"
33 #include "mtdutils/mtdutils.h"
34 #include "roots.h"
35 #include "verifier.h"
36 #include "ui.h"
37
38 extern RecoveryUI* ui;
39
40 #define ASSUMED_UPDATE_BINARY_NAME "META-INF/com/google/android/update-binary"
41 #define PUBLIC_KEYS_FILE "/res/keys"
42
43 // Default allocation of progress bar segments to operations
44 static const int VERIFICATION_PROGRESS_TIME = 60;
45 static const float VERIFICATION_PROGRESS_FRACTION = 0.25;
46 static const float DEFAULT_FILES_PROGRESS_FRACTION = 0.4;
47 static const float DEFAULT_IMAGE_PROGRESS_FRACTION = 0.1;
48
49 // If the package contains an update binary, extract it and run it.
50 static int
try_update_binary(const char * path,ZipArchive * zip,bool * wipe_cache)51 try_update_binary(const char* path, ZipArchive* zip, bool* wipe_cache) {
52 const ZipEntry* binary_entry =
53 mzFindZipEntry(zip, ASSUMED_UPDATE_BINARY_NAME);
54 if (binary_entry == NULL) {
55 mzCloseZipArchive(zip);
56 return INSTALL_CORRUPT;
57 }
58
59 const char* binary = "/tmp/update_binary";
60 unlink(binary);
61 int fd = creat(binary, 0755);
62 if (fd < 0) {
63 mzCloseZipArchive(zip);
64 LOGE("Can't make %s\n", binary);
65 return INSTALL_ERROR;
66 }
67 bool ok = mzExtractZipEntryToFile(zip, binary_entry, fd);
68 close(fd);
69 mzCloseZipArchive(zip);
70
71 if (!ok) {
72 LOGE("Can't copy %s\n", ASSUMED_UPDATE_BINARY_NAME);
73 return INSTALL_ERROR;
74 }
75
76 int pipefd[2];
77 pipe(pipefd);
78
79 // When executing the update binary contained in the package, the
80 // arguments passed are:
81 //
82 // - the version number for this interface
83 //
84 // - an fd to which the program can write in order to update the
85 // progress bar. The program can write single-line commands:
86 //
87 // progress <frac> <secs>
88 // fill up the next <frac> part of of the progress bar
89 // over <secs> seconds. If <secs> is zero, use
90 // set_progress commands to manually control the
91 // progress of this segment of the bar.
92 //
93 // set_progress <frac>
94 // <frac> should be between 0.0 and 1.0; sets the
95 // progress bar within the segment defined by the most
96 // recent progress command.
97 //
98 // firmware <"hboot"|"radio"> <filename>
99 // arrange to install the contents of <filename> in the
100 // given partition on reboot.
101 //
102 // (API v2: <filename> may start with "PACKAGE:" to
103 // indicate taking a file from the OTA package.)
104 //
105 // (API v3: this command no longer exists.)
106 //
107 // ui_print <string>
108 // display <string> on the screen.
109 //
110 // wipe_cache
111 // a wipe of cache will be performed following a successful
112 // installation.
113 //
114 // clear_display
115 // turn off the text display.
116 //
117 // enable_reboot
118 // packages can explicitly request that they want the user
119 // to be able to reboot during installation (useful for
120 // debugging packages that don't exit).
121 //
122 // - the name of the package zip file.
123 //
124
125 const char** args = (const char**)malloc(sizeof(char*) * 5);
126 args[0] = binary;
127 args[1] = EXPAND(RECOVERY_API_VERSION); // defined in Android.mk
128 char* temp = (char*)malloc(10);
129 sprintf(temp, "%d", pipefd[1]);
130 args[2] = temp;
131 args[3] = (char*)path;
132 args[4] = NULL;
133
134 pid_t pid = fork();
135 if (pid == 0) {
136 umask(022);
137 close(pipefd[0]);
138 execv(binary, (char* const*)args);
139 fprintf(stdout, "E:Can't run %s (%s)\n", binary, strerror(errno));
140 _exit(-1);
141 }
142 close(pipefd[1]);
143
144 *wipe_cache = false;
145
146 char buffer[1024];
147 FILE* from_child = fdopen(pipefd[0], "r");
148 while (fgets(buffer, sizeof(buffer), from_child) != NULL) {
149 char* command = strtok(buffer, " \n");
150 if (command == NULL) {
151 continue;
152 } else if (strcmp(command, "progress") == 0) {
153 char* fraction_s = strtok(NULL, " \n");
154 char* seconds_s = strtok(NULL, " \n");
155
156 float fraction = strtof(fraction_s, NULL);
157 int seconds = strtol(seconds_s, NULL, 10);
158
159 ui->ShowProgress(fraction * (1-VERIFICATION_PROGRESS_FRACTION), seconds);
160 } else if (strcmp(command, "set_progress") == 0) {
161 char* fraction_s = strtok(NULL, " \n");
162 float fraction = strtof(fraction_s, NULL);
163 ui->SetProgress(fraction);
164 } else if (strcmp(command, "ui_print") == 0) {
165 char* str = strtok(NULL, "\n");
166 if (str) {
167 ui->Print("%s", str);
168 } else {
169 ui->Print("\n");
170 }
171 fflush(stdout);
172 } else if (strcmp(command, "wipe_cache") == 0) {
173 *wipe_cache = true;
174 } else if (strcmp(command, "clear_display") == 0) {
175 ui->SetBackground(RecoveryUI::NONE);
176 } else if (strcmp(command, "enable_reboot") == 0) {
177 // packages can explicitly request that they want the user
178 // to be able to reboot during installation (useful for
179 // debugging packages that don't exit).
180 ui->SetEnableReboot(true);
181 } else {
182 LOGE("unknown command [%s]\n", command);
183 }
184 }
185 fclose(from_child);
186
187 int status;
188 waitpid(pid, &status, 0);
189 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
190 LOGE("Error in %s\n(Status %d)\n", path, WEXITSTATUS(status));
191 return INSTALL_ERROR;
192 }
193
194 return INSTALL_SUCCESS;
195 }
196
197 static int
really_install_package(const char * path,bool * wipe_cache,bool needs_mount)198 really_install_package(const char *path, bool* wipe_cache, bool needs_mount)
199 {
200 ui->SetBackground(RecoveryUI::INSTALLING_UPDATE);
201 ui->Print("Finding update package...\n");
202 // Give verification half the progress bar...
203 ui->SetProgressType(RecoveryUI::DETERMINATE);
204 ui->ShowProgress(VERIFICATION_PROGRESS_FRACTION, VERIFICATION_PROGRESS_TIME);
205 LOGI("Update location: %s\n", path);
206
207 // Map the update package into memory.
208 ui->Print("Opening update package...\n");
209
210 if (path && needs_mount) {
211 if (path[0] == '@') {
212 ensure_path_mounted(path+1);
213 } else {
214 ensure_path_mounted(path);
215 }
216 }
217
218 MemMapping map;
219 if (sysMapFile(path, &map) != 0) {
220 LOGE("failed to map file\n");
221 return INSTALL_CORRUPT;
222 }
223
224 int numKeys;
225 Certificate* loadedKeys = load_keys(PUBLIC_KEYS_FILE, &numKeys);
226 if (loadedKeys == NULL) {
227 LOGE("Failed to load keys\n");
228 return INSTALL_CORRUPT;
229 }
230 LOGI("%d key(s) loaded from %s\n", numKeys, PUBLIC_KEYS_FILE);
231
232 ui->Print("Verifying update package...\n");
233
234 int err;
235 err = verify_file(map.addr, map.length, loadedKeys, numKeys);
236 free(loadedKeys);
237 LOGI("verify_file returned %d\n", err);
238 if (err != VERIFY_SUCCESS) {
239 LOGE("signature verification failed\n");
240 sysReleaseMap(&map);
241 return INSTALL_CORRUPT;
242 }
243
244 /* Try to open the package.
245 */
246 ZipArchive zip;
247 err = mzOpenZipArchive(map.addr, map.length, &zip);
248 if (err != 0) {
249 LOGE("Can't open %s\n(%s)\n", path, err != -1 ? strerror(err) : "bad");
250 sysReleaseMap(&map);
251 return INSTALL_CORRUPT;
252 }
253
254 /* Verify and install the contents of the package.
255 */
256 ui->Print("Installing update...\n");
257 ui->SetEnableReboot(false);
258 int result = try_update_binary(path, &zip, wipe_cache);
259 ui->SetEnableReboot(true);
260 ui->Print("\n");
261
262 sysReleaseMap(&map);
263
264 return result;
265 }
266
267 int
install_package(const char * path,bool * wipe_cache,const char * install_file,bool needs_mount)268 install_package(const char* path, bool* wipe_cache, const char* install_file,
269 bool needs_mount)
270 {
271 modified_flash = true;
272
273 FILE* install_log = fopen_path(install_file, "w");
274 if (install_log) {
275 fputs(path, install_log);
276 fputc('\n', install_log);
277 } else {
278 LOGE("failed to open last_install: %s\n", strerror(errno));
279 }
280 int result;
281 if (setup_install_mounts() != 0) {
282 LOGE("failed to set up expected mounts for install; aborting\n");
283 result = INSTALL_ERROR;
284 } else {
285 result = really_install_package(path, wipe_cache, needs_mount);
286 }
287 if (install_log) {
288 fputc(result == INSTALL_SUCCESS ? '1' : '0', install_log);
289 fputc('\n', install_log);
290 fclose(install_log);
291 }
292 return result;
293 }
294