blob: e2e2605da8bfdfe0936bd3aa3990b14eaca740e9 [file] [log] [blame]
Koushik K. Dutta6060e5c2010-02-11 22:27:06 -08001#include <ctype.h>
2#include <errno.h>
3#include <fcntl.h>
4#include <getopt.h>
5#include <limits.h>
6#include <linux/input.h>
7#include <stdio.h>
8#include <stdlib.h>
9#include <string.h>
10#include <sys/reboot.h>
11#include <sys/types.h>
12#include <time.h>
13#include <unistd.h>
14
Koushik K. Duttae9234872010-02-12 00:43:24 -080015#include <sys/wait.h>
16#include <sys/limits.h>
17#include <dirent.h>
Koushik K. Dutta49f56892010-02-25 14:51:05 -080018#include <sys/stat.h>
Koushik K. Duttae9234872010-02-12 00:43:24 -080019
Koushik K. Dutta33370db2010-02-25 11:39:07 -080020#include <signal.h>
21#include <sys/wait.h>
22
Koushik K. Dutta6060e5c2010-02-11 22:27:06 -080023#include "bootloader.h"
24#include "common.h"
25#include "cutils/properties.h"
26#include "firmware.h"
27#include "install.h"
28#include "minui/minui.h"
29#include "minzip/DirUtil.h"
30#include "roots.h"
31#include "recovery_ui.h"
32
Koushik K. Duttaf68aaaf2010-03-07 13:39:21 -080033#include "commands.h"
34#include "amend/amend.h"
35
Koushik K. Duttaa85d7cc2010-03-12 17:00:58 -080036#include "mtdutils/dump_image.h"
37#include "../../external/yaffs2/yaffs2/utils/mkyaffs2image.h"
Koushik K. Dutta54305a82010-03-12 17:43:26 -080038#include "../../external/yaffs2/yaffs2/utils/unyaffs.h"
Koushik K. Duttaa85d7cc2010-03-12 17:00:58 -080039
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -080040#include "extendedcommands.h"
41#include "nandroid.h"
42
Koushik K. Dutta6060e5c2010-02-11 22:27:06 -080043int signature_check_enabled = 1;
44int script_assert_enabled = 1;
Koushik K. Duttabcdd0032010-02-21 21:10:25 -080045static const char *SDCARD_PACKAGE_FILE = "SDCARD:update.zip";
Koushik K. Dutta6060e5c2010-02-11 22:27:06 -080046
47void
48toggle_signature_check()
49{
50 signature_check_enabled = !signature_check_enabled;
51 ui_print("Signature Check: %s\n", signature_check_enabled ? "Enabled" : "Disabled");
52}
53
54void toggle_script_asserts()
55{
56 script_assert_enabled = !script_assert_enabled;
Koushik K. Duttae9234872010-02-12 00:43:24 -080057 ui_print("Script Asserts: %s\n", script_assert_enabled ? "Enabled" : "Disabled");
58}
59
Koushik K. Duttaea46fe22010-03-08 02:58:04 -080060int install_zip(const char* packagefilepath)
Koushik K. Duttae9234872010-02-12 00:43:24 -080061{
Koushik K. Duttabcdd0032010-02-21 21:10:25 -080062 ui_print("\n-- Installing: %s\n", packagefilepath);
63 set_sdcard_update_bootloader_message();
64 int status = install_package(packagefilepath);
Koushik K. Dutta99fb6fe2010-03-03 00:42:58 -080065 ui_reset_progress();
Koushik K. Duttabcdd0032010-02-21 21:10:25 -080066 if (status != INSTALL_SUCCESS) {
67 ui_set_background(BACKGROUND_ICON_ERROR);
68 ui_print("Installation aborted.\n");
Koushik K. Duttaea46fe22010-03-08 02:58:04 -080069 return 1;
Koushik K. Dutta79ce82c2010-02-25 12:03:17 -080070 }
71 if (firmware_update_pending()) {
72 ui_print("\nReboot via menu to complete\ninstallation.\n");
Koushik K. Dutta001c5b52010-02-25 14:53:57 -080073 }
Koushik K. Dutta001c5b52010-02-25 14:53:57 -080074 ui_set_background(BACKGROUND_ICON_NONE);
Koushik K. Dutta99fb6fe2010-03-03 00:42:58 -080075 ui_print("\nInstall from sdcard complete.\n");
Koushik K. Duttaea46fe22010-03-08 02:58:04 -080076 return 0;
Koushik K. Duttabcdd0032010-02-21 21:10:25 -080077}
78
79char* INSTALL_MENU_ITEMS[] = { "apply sdcard:update.zip",
80 "choose zip from sdcard",
81 "toggle signature verification",
82 "toggle script asserts",
83 NULL };
84#define ITEM_APPLY_SDCARD 0
85#define ITEM_CHOOSE_ZIP 1
86#define ITEM_SIG_CHECK 2
87#define ITEM_ASSERTS 3
88
89void show_install_update_menu()
90{
91 static char* headers[] = { "Apply update from .zip file on SD card",
92 "",
93 NULL
94 };
95 for (;;)
96 {
97 int chosen_item = get_menu_selection(headers, INSTALL_MENU_ITEMS, 0);
98 switch (chosen_item)
99 {
100 case ITEM_ASSERTS:
101 toggle_script_asserts();
102 break;
103 case ITEM_SIG_CHECK:
104 toggle_signature_check();
105 break;
106 case ITEM_APPLY_SDCARD:
107 install_zip(SDCARD_PACKAGE_FILE);
108 break;
109 case ITEM_CHOOSE_ZIP:
110 show_choose_zip_menu();
111 break;
112 default:
113 return;
114 }
115
116 }
117}
118
Koushik K. Dutta49f56892010-02-25 14:51:05 -0800119char** gather_files(const char* directory, const char* fileExtensionOrDirectory, int* numFiles)
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800120{
Koushik K. Duttae9234872010-02-12 00:43:24 -0800121 char path[PATH_MAX] = "";
122 DIR *dir;
123 struct dirent *de;
124 int total = 0;
125 int i;
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800126 char** files = NULL;
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800127 int pass;
128 *numFiles = 0;
129 int dirLen = strlen(directory);
Koushik K. Duttae9234872010-02-12 00:43:24 -0800130
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800131 dir = opendir(directory);
132 if (dir == NULL) {
133 ui_print("Couldn't open directory.\n");
134 return NULL;
135 }
136
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800137 int extension_length = 0;
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800138 if (fileExtensionOrDirectory != NULL)
139 extension_length = strlen(fileExtensionOrDirectory);
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800140
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800141 int isCounting = 1;
142 i = 0;
143 for (pass = 0; pass < 2; pass++) {
144 while ((de=readdir(dir)) != NULL) {
145 // skip hidden files
146 if (de->d_name[0] == '.')
147 continue;
148
149 // NULL means that we are gathering directories, so skip this
150 if (fileExtensionOrDirectory != NULL)
151 {
152 // make sure that we can have the desired extension (prevent seg fault)
153 if (strlen(de->d_name) < extension_length)
154 continue;
155 // compare the extension
156 if (strcmp(de->d_name + strlen(de->d_name) - extension_length, fileExtensionOrDirectory) != 0)
157 continue;
158 }
159 else
160 {
161 struct stat info;
162 char* fullFileName = (char*)malloc(strlen(de->d_name) + dirLen + 1);
163 strcpy(fullFileName, directory);
164 strcat(fullFileName, de->d_name);
165 stat(fullFileName, &info);
166 free(fullFileName);
167 // make sure it is a directory
168 if (!(S_ISDIR(info.st_mode)))
169 continue;
170 }
171
172 if (pass == 0)
173 {
174 total++;
175 continue;
176 }
177
Koushik K. Dutta49f56892010-02-25 14:51:05 -0800178 files[i] = (char*) malloc(dirLen + strlen(de->d_name) + 2);
179 strcpy(files[i], directory);
180 strcat(files[i], de->d_name);
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800181 if (fileExtensionOrDirectory == NULL)
182 strcat(files[i], "/");
183 i++;
184 }
185 if (pass == 1)
186 break;
187 if (total == 0)
188 break;
189 rewinddir(dir);
190 *numFiles = total;
191 files = (char**) malloc((total+1)*sizeof(char*));
192 files[total]=NULL;
193 }
Koushik K. Dutta49f56892010-02-25 14:51:05 -0800194
195 if(closedir(dir) < 0) {
196 LOGE("Failed to close directory.");
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800197 }
198
199 if (total==0) {
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800200 return NULL;
201 }
202
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800203 return files;
Koushik K. Dutta49f56892010-02-25 14:51:05 -0800204}
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800205
Koushik K. Dutta49f56892010-02-25 14:51:05 -0800206void free_string_array(char** array)
207{
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800208 char* cursor = array[0];
209 int i = 0;
210 while (cursor != NULL)
211 {
212 free(cursor);
213 cursor = array[++i];
214 }
215 free(array);
Koushik K. Dutta49f56892010-02-25 14:51:05 -0800216}
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800217
Koushik K. Dutta49f56892010-02-25 14:51:05 -0800218// pass in NULL for fileExtensionOrDirectory and you will get a directory chooser
219char* choose_file_menu(const char* directory, const char* fileExtensionOrDirectory, const char* headers[])
220{
221 char path[PATH_MAX] = "";
222 DIR *dir;
223 struct dirent *de;
224 int numFiles = 0;
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800225 int numDirs = 0;
Koushik K. Dutta49f56892010-02-25 14:51:05 -0800226 int i;
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800227
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800228 int dir_len = strlen(directory);
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800229
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800230 char** files = gather_files(directory, fileExtensionOrDirectory, &numFiles);
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800231 char** dirs = NULL;
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800232 if (fileExtensionOrDirectory != NULL)
233 dirs = gather_files(directory, NULL, &numDirs);
234 int total = numDirs + numFiles;
235 if (total == 0)
236 {
237 ui_print("No files found.\n");
238 return NULL;
239 }
240 char** list = (char**) malloc((total + 1) * sizeof(char*));
241 list[total] = NULL;
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800242
Koushik K. Dutta49f56892010-02-25 14:51:05 -0800243
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800244 for (i = 0 ; i < numDirs; i++)
245 {
246 list[i] = strdup(dirs[i] + dir_len);
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800247 }
248
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800249 for (i = 0 ; i < numFiles; i++)
250 {
251 list[numDirs + i] = strdup(files[i] + dir_len);
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800252 }
253
Koushik K. Dutta981b0cd2010-02-22 08:53:34 -0800254 for (;;)
255 {
256 int chosen_item = get_menu_selection(headers, list, 0);
257 if (chosen_item == GO_BACK)
258 break;
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800259 if (chosen_item < numDirs)
260 {
261 char* subret = choose_file_menu(dirs[chosen_item], fileExtensionOrDirectory, headers);
262 if (subret != NULL)
263 return subret;
264 continue;
265 }
Koushik K. Dutta981b0cd2010-02-22 08:53:34 -0800266 static char ret[PATH_MAX];
Koushik K. Dutta49f56892010-02-25 14:51:05 -0800267 strcpy(ret, files[chosen_item - numDirs]);
Koushik K. Dutta981b0cd2010-02-22 08:53:34 -0800268 return ret;
269 }
270 return NULL;
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800271}
272
273void show_choose_zip_menu()
274{
Koushik K. Duttae9234872010-02-12 00:43:24 -0800275 if (ensure_root_path_mounted("SDCARD:") != 0) {
276 LOGE ("Can't mount /sdcard\n");
277 return;
278 }
279
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800280 static char* headers[] = { "Choose a zip to apply",
281 "",
282 NULL
283 };
284
285 char* file = choose_file_menu("/sdcard/", ".zip", headers);
286 if (file == NULL)
287 return;
288 char sdcard_package_file[1024];
289 strcpy(sdcard_package_file, "SDCARD:");
290 strcat(sdcard_package_file, file + strlen("/sdcard/"));
291 install_zip(sdcard_package_file);
292}
293
Koushik K. Dutta33370db2010-02-25 11:39:07 -0800294// This was pulled from bionic: The default system command always looks
295// for shell in /system/bin/sh. This is bad.
296#define _PATH_BSHELL "/sbin/sh"
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800297
Koushik K. Dutta33370db2010-02-25 11:39:07 -0800298extern char **environ;
299int
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800300__system(const char *command)
Koushik K. Dutta33370db2010-02-25 11:39:07 -0800301{
302 pid_t pid;
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800303 sig_t intsave, quitsave;
304 sigset_t mask, omask;
305 int pstat;
306 char *argp[] = {"sh", "-c", NULL, NULL};
Koushik K. Dutta33370db2010-02-25 11:39:07 -0800307
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800308 if (!command) /* just checking... */
309 return(1);
Koushik K. Dutta33370db2010-02-25 11:39:07 -0800310
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800311 argp[2] = (char *)command;
Koushik K. Dutta33370db2010-02-25 11:39:07 -0800312
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800313 sigemptyset(&mask);
314 sigaddset(&mask, SIGCHLD);
315 sigprocmask(SIG_BLOCK, &mask, &omask);
316 switch (pid = vfork()) {
317 case -1: /* error */
318 sigprocmask(SIG_SETMASK, &omask, NULL);
319 return(-1);
320 case 0: /* child */
321 sigprocmask(SIG_SETMASK, &omask, NULL);
322 execve(_PATH_BSHELL, argp, environ);
Koushik K. Dutta33370db2010-02-25 11:39:07 -0800323 _exit(127);
324 }
325
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800326 intsave = (sig_t) bsd_signal(SIGINT, SIG_IGN);
327 quitsave = (sig_t) bsd_signal(SIGQUIT, SIG_IGN);
328 pid = waitpid(pid, (int *)&pstat, 0);
329 sigprocmask(SIG_SETMASK, &omask, NULL);
330 (void)bsd_signal(SIGINT, intsave);
331 (void)bsd_signal(SIGQUIT, quitsave);
332 return (pid == -1 ? -1 : pstat);
Koushik K. Dutta33370db2010-02-25 11:39:07 -0800333}
334
Koushik K. Duttaa85d7cc2010-03-12 17:00:58 -0800335int print_and_error(char* message)
336{
337 ui_print(message);
338 return 1;
339}
340
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800341void show_nandroid_restore_menu()
342{
Koushik K. Dutta54305a82010-03-12 17:43:26 -0800343 if (ensure_root_path_mounted("SDCARD:") != 0) {
344 LOGE ("Can't mount /sdcard\n");
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800345 return;
Koushik K. Dutta54305a82010-03-12 17:43:26 -0800346 }
347
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800348 static char* headers[] = { "Choose an image to restore",
349 "",
350 NULL
351 };
Koushik K. Duttae9234872010-02-12 00:43:24 -0800352
Koushik K. Dutta49f56892010-02-25 14:51:05 -0800353 char* file = choose_file_menu("/sdcard/clockworkmod/backup/", NULL, headers);
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800354 if (file == NULL)
355 return;
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800356 nandroid_restore(file);
Koushik K. Dutta03173782010-02-26 14:14:23 -0800357}
358
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700359void show_mount_usb_storage_menu()
Koushik K. Dutta03173782010-02-26 14:14:23 -0800360{
361 system("echo /dev/block/mmcblk0 > /sys/devices/platform/usb_mass_storage/lun0/file");
362 static char* headers[] = { "USB Mass Storage device",
363 "Leaving this menu unmount",
364 "your SD card from your PC.",
365 "",
366 NULL
367 };
368
369 static char* list[] = { "Unmount", NULL };
370
371 for (;;)
372 {
373 int chosen_item = get_menu_selection(headers, list, 0);
374 if (chosen_item == GO_BACK || chosen_item == 0)
375 break;
376 }
377
Koushik K. Duttae81cb752010-02-26 17:44:33 -0800378 system("echo '' > /sys/devices/platform/usb_mass_storage/lun0/file");
Koushik K. Dutta03173782010-02-26 14:14:23 -0800379 system("echo 0 > /sys/devices/platform/usb_mass_storage/lun0/enable");
Koushik K. Duttae81cb752010-02-26 17:44:33 -0800380}
Koushik K. Duttaf68aaaf2010-03-07 13:39:21 -0800381
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700382
383void show_mount_menu()
384{
385 static char* headers[] = { "Mount and unmount partitions",
386 "",
387 NULL
388 };
389
390 typedef char* string;
391 string mounts[3][3] = {
392 { "mount /system", "unmount /system", "SYSTEM:" },
393 { "mount /data", "unmount /data", "DATA:" },
394 { "mount /cache", "unmount /cache", "CACHE:" }
395 };
396
397 for (;;)
398 {
399 int ismounted[3];
400 int i;
401 static string options[5];
402 for (i = 0; i < 3; i++)
403 {
404 ismounted[i] = is_root_path_mounted(mounts[i][2]);
405 options[i] = ismounted[i] ? mounts[i][1] : mounts[i][0];
406 }
407
408 options[3] = "mount USB storage";
409 options[4] = NULL;
410
411 int chosen_item = get_menu_selection(headers, options, 0);
412 if (chosen_item == GO_BACK)
413 break;
414 if (chosen_item == 4)
415 {
416 show_mount_usb_storage_menu();
417 }
418 else if (chosen_item < 4)
419 {
420 if (ismounted[chosen_item])
421 {
422 if (0 != ensure_root_path_unmounted(mounts[chosen_item][2]))
423 ui_print("Error unmounting %s!\n", mounts[chosen_item][2]);
424 }
425 else
426 {
427 if (0 != ensure_root_path_mounted(mounts[chosen_item][2]))
428 ui_print("Error mounting %s!\n", mounts[chosen_item][2]);
429 }
430 }
431 }
432}
433
Koushik K. Dutta72a1db62010-03-07 14:10:26 -0800434#define EXTENDEDCOMMAND_SCRIPT "/cache/recovery/extendedcommand"
435
436int extendedcommand_file_exists()
Koushik K. Duttaf68aaaf2010-03-07 13:39:21 -0800437{
Koushik K. Duttaf68aaaf2010-03-07 13:39:21 -0800438 struct stat file_info;
Koushik K. Dutta72a1db62010-03-07 14:10:26 -0800439 return 0 == stat(EXTENDEDCOMMAND_SCRIPT, &file_info);
440}
441
Koushik K. Dutta3836f722010-03-11 22:17:43 -0800442int run_script_from_buffer(char* script_data, int script_len, char* filename)
Koushik K. Dutta72a1db62010-03-07 14:10:26 -0800443{
Koushik K. Duttaf68aaaf2010-03-07 13:39:21 -0800444 /* Parse the script. Note that the script and parse tree are never freed.
445 */
446 const AmCommandList *commands = parseAmendScript(script_data, script_len);
447 if (commands == NULL) {
448 printf("Syntax error in update script\n");
449 return 1;
450 } else {
Koushik K. Dutta72a1db62010-03-07 14:10:26 -0800451 printf("Parsed %.*s\n", script_len, filename);
Koushik K. Duttaf68aaaf2010-03-07 13:39:21 -0800452 }
453
454 /* Execute the script.
455 */
456 int ret = execCommandList((ExecContext *)1, commands);
457 if (ret != 0) {
458 int num = ret;
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800459 char *line = NULL, *next = script_data;
Koushik K. Duttaf68aaaf2010-03-07 13:39:21 -0800460 while (next != NULL && ret-- > 0) {
461 line = next;
462 next = memchr(line, '\n', script_data + script_len - line);
463 if (next != NULL) *next++ = '\0';
464 }
465 printf("Failure at line %d:\n%s\n", num, next ? line : "(not found)");
466 return 1;
467 }
468
469 return 0;
470}
Koushik K. Dutta72a1db62010-03-07 14:10:26 -0800471
Koushik K. Dutta3836f722010-03-11 22:17:43 -0800472int run_script(char* filename, int delete_file)
473{
474 struct stat file_info;
475 if (0 != stat(filename, &file_info)) {
476 printf("Error executing stat on file: %s\n", filename);
477 return 1;
478 }
479
480 int script_len = file_info.st_size;
481 char* script_data = (char*)malloc(script_len);
482 FILE *file = fopen(filename, "rb");
483 fread(script_data, script_len, 1, file);
484 fclose(file);
485 if (delete_file)
486 remove(filename);
487
488 return run_script_from_buffer(script_data, script_len, filename);
489}
490
Koushik K. Dutta72a1db62010-03-07 14:10:26 -0800491int run_and_remove_extendedcommand()
492{
Koushik K. Dutta3a25cf52010-03-08 19:22:41 -0800493 int i = 0;
494 for (i = 20; i > 0; i--) {
495 ui_print("Waiting for SD Card to mount (%ds)\n", i);
496 if (ensure_root_path_mounted("SDCARD:") == 0) {
497 ui_print("SD Card mounted...\n");
498 break;
499 }
500 sleep(1);
501 }
502 if (i == 0) {
503 ui_print("Timed out waiting for SD card... continuing anyways.");
504 }
505
Koushik K. Dutta3836f722010-03-11 22:17:43 -0800506 return run_script(EXTENDEDCOMMAND_SCRIPT, 1);
Koushik K. Dutta72a1db62010-03-07 14:10:26 -0800507}
508
509int amend_main(int argc, char** argv)
510{
511 if (argc != 2)
512 {
513 printf("Usage: amend <script>\n");
514 return 0;
515 }
516
517 RecoveryCommandContext ctx = { NULL };
518 if (register_update_commands(&ctx)) {
519 LOGE("Can't install update commands\n");
520 }
Koushik K. Dutta3836f722010-03-11 22:17:43 -0800521 return run_script(argv[1], 0);
Koushik K. Dutta5306db52010-03-08 14:09:35 -0800522}