blob: aa9cbc64cb03074c24286883e21427c400ec43eb [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 Dutta14239d22010-06-14 15:02:48 -070036#include "mtdutils/mtdutils.h"
Koushik K. Duttaa85d7cc2010-03-12 17:00:58 -080037#include "mtdutils/dump_image.h"
38#include "../../external/yaffs2/yaffs2/utils/mkyaffs2image.h"
Koushik K. Dutta54305a82010-03-12 17:43:26 -080039#include "../../external/yaffs2/yaffs2/utils/unyaffs.h"
Koushik K. Duttaa85d7cc2010-03-12 17:00:58 -080040
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -080041#include "extendedcommands.h"
42#include "nandroid.h"
43
Koushik K. Dutta6060e5c2010-02-11 22:27:06 -080044int signature_check_enabled = 1;
45int script_assert_enabled = 1;
Koushik K. Duttabcdd0032010-02-21 21:10:25 -080046static const char *SDCARD_PACKAGE_FILE = "SDCARD:update.zip";
Koushik K. Dutta6060e5c2010-02-11 22:27:06 -080047
48void
49toggle_signature_check()
50{
51 signature_check_enabled = !signature_check_enabled;
52 ui_print("Signature Check: %s\n", signature_check_enabled ? "Enabled" : "Disabled");
53}
54
55void toggle_script_asserts()
56{
57 script_assert_enabled = !script_assert_enabled;
Koushik K. Duttae9234872010-02-12 00:43:24 -080058 ui_print("Script Asserts: %s\n", script_assert_enabled ? "Enabled" : "Disabled");
59}
60
Koushik K. Duttaea46fe22010-03-08 02:58:04 -080061int install_zip(const char* packagefilepath)
Koushik K. Duttae9234872010-02-12 00:43:24 -080062{
Koushik K. Duttabcdd0032010-02-21 21:10:25 -080063 ui_print("\n-- Installing: %s\n", packagefilepath);
64 set_sdcard_update_bootloader_message();
65 int status = install_package(packagefilepath);
Koushik K. Duttaf3534d02010-04-18 18:06:24 -070066 ui_reset_progress();
Koushik K. Duttabcdd0032010-02-21 21:10:25 -080067 if (status != INSTALL_SUCCESS) {
68 ui_set_background(BACKGROUND_ICON_ERROR);
69 ui_print("Installation aborted.\n");
Koushik K. Duttaea46fe22010-03-08 02:58:04 -080070 return 1;
Koushik K. Dutta79ce82c2010-02-25 12:03:17 -080071 }
72 if (firmware_update_pending()) {
73 ui_print("\nReboot via menu to complete\ninstallation.\n");
Koushik K. Dutta001c5b52010-02-25 14:53:57 -080074 }
Koushik K. Dutta001c5b52010-02-25 14:53:57 -080075 ui_set_background(BACKGROUND_ICON_NONE);
Koushik K. Dutta99fb6fe2010-03-03 00:42:58 -080076 ui_print("\nInstall from sdcard complete.\n");
Koushik K. Duttaea46fe22010-03-08 02:58:04 -080077 return 0;
Koushik K. Duttabcdd0032010-02-21 21:10:25 -080078}
79
80char* INSTALL_MENU_ITEMS[] = { "apply sdcard:update.zip",
81 "choose zip from sdcard",
82 "toggle signature verification",
83 "toggle script asserts",
84 NULL };
85#define ITEM_APPLY_SDCARD 0
86#define ITEM_CHOOSE_ZIP 1
87#define ITEM_SIG_CHECK 2
88#define ITEM_ASSERTS 3
89
90void show_install_update_menu()
91{
92 static char* headers[] = { "Apply update from .zip file on SD card",
93 "",
94 NULL
95 };
96 for (;;)
97 {
98 int chosen_item = get_menu_selection(headers, INSTALL_MENU_ITEMS, 0);
99 switch (chosen_item)
100 {
101 case ITEM_ASSERTS:
102 toggle_script_asserts();
103 break;
104 case ITEM_SIG_CHECK:
105 toggle_signature_check();
106 break;
107 case ITEM_APPLY_SDCARD:
108 install_zip(SDCARD_PACKAGE_FILE);
109 break;
110 case ITEM_CHOOSE_ZIP:
111 show_choose_zip_menu();
112 break;
113 default:
114 return;
115 }
116
117 }
118}
119
Koushik K. Dutta1d53c4e2010-04-02 16:46:21 -0700120void free_string_array(char** array)
121{
122 if (array == NULL)
123 return;
124 char* cursor = array[0];
125 int i = 0;
126 while (cursor != NULL)
127 {
128 free(cursor);
129 cursor = array[++i];
130 }
131 free(array);
132}
133
Koushik K. Dutta49f56892010-02-25 14:51:05 -0800134char** gather_files(const char* directory, const char* fileExtensionOrDirectory, int* numFiles)
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800135{
Koushik K. Duttae9234872010-02-12 00:43:24 -0800136 char path[PATH_MAX] = "";
137 DIR *dir;
138 struct dirent *de;
139 int total = 0;
140 int i;
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800141 char** files = NULL;
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800142 int pass;
143 *numFiles = 0;
144 int dirLen = strlen(directory);
Koushik K. Duttae9234872010-02-12 00:43:24 -0800145
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800146 dir = opendir(directory);
147 if (dir == NULL) {
148 ui_print("Couldn't open directory.\n");
149 return NULL;
150 }
151
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800152 int extension_length = 0;
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800153 if (fileExtensionOrDirectory != NULL)
154 extension_length = strlen(fileExtensionOrDirectory);
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800155
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800156 int isCounting = 1;
157 i = 0;
158 for (pass = 0; pass < 2; pass++) {
159 while ((de=readdir(dir)) != NULL) {
160 // skip hidden files
161 if (de->d_name[0] == '.')
162 continue;
163
164 // NULL means that we are gathering directories, so skip this
165 if (fileExtensionOrDirectory != NULL)
166 {
167 // make sure that we can have the desired extension (prevent seg fault)
168 if (strlen(de->d_name) < extension_length)
169 continue;
170 // compare the extension
171 if (strcmp(de->d_name + strlen(de->d_name) - extension_length, fileExtensionOrDirectory) != 0)
172 continue;
173 }
174 else
175 {
176 struct stat info;
Koushik K. Dutta1d53c4e2010-04-02 16:46:21 -0700177 char fullFileName[PATH_MAX];
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800178 strcpy(fullFileName, directory);
179 strcat(fullFileName, de->d_name);
180 stat(fullFileName, &info);
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800181 // make sure it is a directory
182 if (!(S_ISDIR(info.st_mode)))
183 continue;
184 }
185
186 if (pass == 0)
187 {
188 total++;
189 continue;
190 }
191
Koushik K. Dutta49f56892010-02-25 14:51:05 -0800192 files[i] = (char*) malloc(dirLen + strlen(de->d_name) + 2);
193 strcpy(files[i], directory);
194 strcat(files[i], de->d_name);
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800195 if (fileExtensionOrDirectory == NULL)
196 strcat(files[i], "/");
197 i++;
198 }
199 if (pass == 1)
200 break;
201 if (total == 0)
202 break;
203 rewinddir(dir);
204 *numFiles = total;
205 files = (char**) malloc((total+1)*sizeof(char*));
206 files[total]=NULL;
207 }
Koushik K. Dutta49f56892010-02-25 14:51:05 -0800208
209 if(closedir(dir) < 0) {
210 LOGE("Failed to close directory.");
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800211 }
212
213 if (total==0) {
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800214 return NULL;
215 }
216
Koushik Dutta1e8aaba2010-07-01 00:23:25 -0700217 // sort the result
218 if (files != NULL) {
219 for (i = 0; i < total; i++) {
220 int curMax = -1;
221 int j;
222 for (j = 0; j < total - i; j++) {
223 if (curMax == -1 || strcmp(files[curMax], files[j]) < 0)
224 curMax = j;
225 }
226 char* temp = files[curMax];
227 files[curMax] = files[total - i - 1];
228 files[total - i - 1] = temp;
229 }
230 }
231
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800232 return files;
Koushik K. Dutta49f56892010-02-25 14:51:05 -0800233}
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800234
Koushik K. Dutta49f56892010-02-25 14:51:05 -0800235// pass in NULL for fileExtensionOrDirectory and you will get a directory chooser
236char* choose_file_menu(const char* directory, const char* fileExtensionOrDirectory, const char* headers[])
237{
238 char path[PATH_MAX] = "";
239 DIR *dir;
240 struct dirent *de;
241 int numFiles = 0;
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800242 int numDirs = 0;
Koushik K. Dutta49f56892010-02-25 14:51:05 -0800243 int i;
Koushik K. Dutta1d53c4e2010-04-02 16:46:21 -0700244 char* return_value = NULL;
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800245 int dir_len = strlen(directory);
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800246
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800247 char** files = gather_files(directory, fileExtensionOrDirectory, &numFiles);
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800248 char** dirs = NULL;
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800249 if (fileExtensionOrDirectory != NULL)
250 dirs = gather_files(directory, NULL, &numDirs);
251 int total = numDirs + numFiles;
252 if (total == 0)
253 {
254 ui_print("No files found.\n");
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800255 }
Koushik K. Dutta1d53c4e2010-04-02 16:46:21 -0700256 else
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800257 {
Koushik K. Dutta1d53c4e2010-04-02 16:46:21 -0700258 char** list = (char**) malloc((total + 1) * sizeof(char*));
259 list[total] = NULL;
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800260
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800261
Koushik K. Dutta1d53c4e2010-04-02 16:46:21 -0700262 for (i = 0 ; i < numDirs; i++)
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800263 {
Koushik K. Dutta1d53c4e2010-04-02 16:46:21 -0700264 list[i] = strdup(dirs[i] + dir_len);
265 }
266
267 for (i = 0 ; i < numFiles; i++)
268 {
269 list[numDirs + i] = strdup(files[i] + dir_len);
270 }
271
272 for (;;)
273 {
274 int chosen_item = get_menu_selection(headers, list, 0);
275 if (chosen_item == GO_BACK)
276 break;
277 static char ret[PATH_MAX];
278 if (chosen_item < numDirs)
279 {
280 char* subret = choose_file_menu(dirs[chosen_item], fileExtensionOrDirectory, headers);
281 if (subret != NULL)
282 {
283 strcpy(ret, subret);
284 return_value = ret;
285 break;
286 }
287 continue;
288 }
289 strcpy(ret, files[chosen_item - numDirs]);
290 return_value = ret;
291 break;
292 }
293 free_string_array(list);
Koushik K. Dutta981b0cd2010-02-22 08:53:34 -0800294 }
Koushik K. Dutta1d53c4e2010-04-02 16:46:21 -0700295
296 free_string_array(files);
297 free_string_array(dirs);
298 return return_value;
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800299}
300
301void show_choose_zip_menu()
302{
Koushik K. Duttae9234872010-02-12 00:43:24 -0800303 if (ensure_root_path_mounted("SDCARD:") != 0) {
304 LOGE ("Can't mount /sdcard\n");
305 return;
306 }
307
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800308 static char* headers[] = { "Choose a zip to apply",
309 "",
310 NULL
311 };
312
313 char* file = choose_file_menu("/sdcard/", ".zip", headers);
314 if (file == NULL)
315 return;
316 char sdcard_package_file[1024];
317 strcpy(sdcard_package_file, "SDCARD:");
318 strcat(sdcard_package_file, file + strlen("/sdcard/"));
319 install_zip(sdcard_package_file);
320}
321
Koushik K. Dutta33370db2010-02-25 11:39:07 -0800322// This was pulled from bionic: The default system command always looks
323// for shell in /system/bin/sh. This is bad.
324#define _PATH_BSHELL "/sbin/sh"
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800325
Koushik K. Dutta33370db2010-02-25 11:39:07 -0800326extern char **environ;
327int
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800328__system(const char *command)
Koushik K. Dutta33370db2010-02-25 11:39:07 -0800329{
330 pid_t pid;
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800331 sig_t intsave, quitsave;
332 sigset_t mask, omask;
333 int pstat;
334 char *argp[] = {"sh", "-c", NULL, NULL};
Koushik K. Dutta33370db2010-02-25 11:39:07 -0800335
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800336 if (!command) /* just checking... */
337 return(1);
Koushik K. Dutta33370db2010-02-25 11:39:07 -0800338
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800339 argp[2] = (char *)command;
Koushik K. Dutta33370db2010-02-25 11:39:07 -0800340
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800341 sigemptyset(&mask);
342 sigaddset(&mask, SIGCHLD);
343 sigprocmask(SIG_BLOCK, &mask, &omask);
344 switch (pid = vfork()) {
345 case -1: /* error */
346 sigprocmask(SIG_SETMASK, &omask, NULL);
347 return(-1);
348 case 0: /* child */
349 sigprocmask(SIG_SETMASK, &omask, NULL);
350 execve(_PATH_BSHELL, argp, environ);
Koushik K. Dutta33370db2010-02-25 11:39:07 -0800351 _exit(127);
352 }
353
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800354 intsave = (sig_t) bsd_signal(SIGINT, SIG_IGN);
355 quitsave = (sig_t) bsd_signal(SIGQUIT, SIG_IGN);
356 pid = waitpid(pid, (int *)&pstat, 0);
357 sigprocmask(SIG_SETMASK, &omask, NULL);
358 (void)bsd_signal(SIGINT, intsave);
359 (void)bsd_signal(SIGQUIT, quitsave);
360 return (pid == -1 ? -1 : pstat);
Koushik K. Dutta33370db2010-02-25 11:39:07 -0800361}
362
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800363void show_nandroid_restore_menu()
364{
Koushik K. Dutta54305a82010-03-12 17:43:26 -0800365 if (ensure_root_path_mounted("SDCARD:") != 0) {
366 LOGE ("Can't mount /sdcard\n");
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800367 return;
Koushik K. Dutta54305a82010-03-12 17:43:26 -0800368 }
369
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800370 static char* headers[] = { "Choose an image to restore",
371 "",
372 NULL
373 };
Koushik K. Duttae9234872010-02-12 00:43:24 -0800374
Koushik K. Dutta49f56892010-02-25 14:51:05 -0800375 char* file = choose_file_menu("/sdcard/clockworkmod/backup/", NULL, headers);
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800376 if (file == NULL)
377 return;
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700378 nandroid_restore(file, 1, 1, 1, 1, 1);
Koushik K. Dutta03173782010-02-26 14:14:23 -0800379}
380
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700381void show_mount_usb_storage_menu()
Koushik K. Dutta03173782010-02-26 14:14:23 -0800382{
Koushik Dutta598cfc72010-06-20 09:42:47 -0700383 char command[PATH_MAX];
384 sprintf(command, "echo %s > /sys/devices/platform/usb_mass_storage/lun0/file", SDCARD_DEVICE_PRIMARY);
385 __system(command);
Koushik K. Dutta03173782010-02-26 14:14:23 -0800386 static char* headers[] = { "USB Mass Storage device",
387 "Leaving this menu unmount",
388 "your SD card from your PC.",
389 "",
390 NULL
391 };
392
393 static char* list[] = { "Unmount", NULL };
394
395 for (;;)
396 {
397 int chosen_item = get_menu_selection(headers, list, 0);
398 if (chosen_item == GO_BACK || chosen_item == 0)
399 break;
400 }
401
Koushik K. Duttaf7215942010-03-16 13:34:51 -0700402 __system("echo '' > /sys/devices/platform/usb_mass_storage/lun0/file");
403 __system("echo 0 > /sys/devices/platform/usb_mass_storage/lun0/enable");
Koushik K. Duttae81cb752010-02-26 17:44:33 -0800404}
Koushik K. Duttaf68aaaf2010-03-07 13:39:21 -0800405
Koushik Duttaecd32fa2010-07-14 18:38:02 -0700406int confirm_selection(char* title_headers[], char* confirm)
Koushik K. Dutta16f0b492010-03-19 14:37:11 -0700407{
Koushik K. Dutta16f0b492010-03-19 14:37:11 -0700408 char* items[] = { " No",
409 " No",
410 " No",
411 " No",
412 " No",
413 " No",
414 " No",
Koushik Duttaecd32fa2010-07-14 18:38:02 -0700415 confirm, //" Yes -- wipe partition", // [7
Koushik K. Dutta16f0b492010-03-19 14:37:11 -0700416 " No",
417 " No",
418 " No",
419 NULL };
420
421 int chosen_item = get_menu_selection(title_headers, items, 0);
422 return chosen_item == 7;
423}
424
Koushik Dutta2f73e582010-04-18 16:00:21 -0700425int format_non_mtd_device(const char* root)
Koushik K. Dutta3f995392010-03-30 23:29:43 -0700426{
Koushik K. Duttaf3534d02010-04-18 18:06:24 -0700427 // if this is SDEXT:, don't worry about it.
428 if (0 == strcmp(root, "SDEXT:"))
429 {
430 struct stat st;
Koushik Dutta598cfc72010-06-20 09:42:47 -0700431 if (0 != stat(SDEXT_DEVICE, &st))
Koushik K. Duttaf3534d02010-04-18 18:06:24 -0700432 {
433 ui_print("No app2sd partition found. Skipping format of /sd-ext.\n");
434 return 0;
435 }
436 }
Koushik Dutta2f73e582010-04-18 16:00:21 -0700437
Koushik K. Dutta3f995392010-03-30 23:29:43 -0700438 char path[PATH_MAX];
439 translate_root_path(root, path, PATH_MAX);
440 if (0 != ensure_root_path_mounted(root))
441 {
442 ui_print("Error mounting %s!\n", path);
Koushik Duttacd44ab92010-06-23 00:02:14 -0700443 ui_print("Skipping format...\n");
444 return 0;
Koushik K. Dutta3f995392010-03-30 23:29:43 -0700445 }
446
447 static char tmp[PATH_MAX];
448 sprintf(tmp, "rm -rf %s/*", path);
449 __system(tmp);
450 sprintf(tmp, "rm -rf %s/.*", path);
451 __system(tmp);
452
453 ensure_root_path_unmounted(root);
454 return 0;
455}
456
457#define MOUNTABLE_COUNT 5
458#define MTD_COUNT 4
459#define MMC_COUNT 2
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700460
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700461void show_partition_menu()
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700462{
463 static char* headers[] = { "Mount and unmount partitions",
464 "",
465 NULL
466 };
467
468 typedef char* string;
Koushik K. Dutta16f0b492010-03-19 14:37:11 -0700469 string mounts[MOUNTABLE_COUNT][3] = {
470 { "mount /system", "unmount /system", "SYSTEM:" },
471 { "mount /data", "unmount /data", "DATA:" },
472 { "mount /cache", "unmount /cache", "CACHE:" },
Koushik K. Dutta3f995392010-03-30 23:29:43 -0700473 { "mount /sdcard", "unmount /sdcard", "SDCARD:" },
474 { "mount /sd-ext", "unmount /sd-ext", "SDEXT:" }
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700475 };
476
Koushik K. Dutta16f0b492010-03-19 14:37:11 -0700477 string mtds[MTD_COUNT][2] = {
Koushik K. Dutta3f995392010-03-30 23:29:43 -0700478 { "format boot", "BOOT:" },
479 { "format system", "SYSTEM:" },
480 { "format data", "DATA:" },
481 { "format cache", "CACHE:" },
482 };
483
484 string mmcs[MMC_COUNT][3] = {
485 { "format sdcard", "SDCARD:" },
Koushik K. Duttaa5f64e62010-04-05 15:44:57 -0700486 { "format sd-ext", "SDEXT:" }
Koushik K. Dutta16f0b492010-03-19 14:37:11 -0700487 };
Koushik Duttaecd32fa2010-07-14 18:38:02 -0700488
489 static char* confirm_format[] = { "Confirm format?",
490 " THIS CAN NOT BE UNDONE.",
491 "",
492 NULL,
493 };
494 static char* confirm = "Yes - Format";
Koushik K. Dutta16f0b492010-03-19 14:37:11 -0700495
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700496 for (;;)
497 {
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700498 int ismounted[MOUNTABLE_COUNT];
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700499 int i;
Koushik K. Dutta3f995392010-03-30 23:29:43 -0700500 static string options[MOUNTABLE_COUNT + MTD_COUNT + MMC_COUNT + 1 + 1]; // mountables, format mtds, format mmcs, usb storage, null
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700501 for (i = 0; i < MOUNTABLE_COUNT; i++)
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700502 {
Koushik K. Dutta16f0b492010-03-19 14:37:11 -0700503 ismounted[i] = is_root_path_mounted(mounts[i][2]);
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700504 options[i] = ismounted[i] ? mounts[i][1] : mounts[i][0];
505 }
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700506
507 for (i = 0; i < MTD_COUNT; i++)
508 {
Koushik K. Dutta16f0b492010-03-19 14:37:11 -0700509 options[MOUNTABLE_COUNT + i] = mtds[i][0];
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700510 }
Koushik K. Dutta3f995392010-03-30 23:29:43 -0700511
512 for (i = 0; i < MMC_COUNT; i++)
513 {
514 options[MOUNTABLE_COUNT + MTD_COUNT + i] = mmcs[i][0];
515 }
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700516
Koushik K. Dutta3f995392010-03-30 23:29:43 -0700517 options[MOUNTABLE_COUNT + MTD_COUNT + MMC_COUNT] = "mount USB storage";
518 options[MOUNTABLE_COUNT + MTD_COUNT + MMC_COUNT + 1] = NULL;
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700519
520 int chosen_item = get_menu_selection(headers, options, 0);
521 if (chosen_item == GO_BACK)
522 break;
Koushik K. Dutta3f995392010-03-30 23:29:43 -0700523 if (chosen_item == MOUNTABLE_COUNT + MTD_COUNT + MMC_COUNT)
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700524 {
525 show_mount_usb_storage_menu();
526 }
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700527 else if (chosen_item < MOUNTABLE_COUNT)
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700528 {
529 if (ismounted[chosen_item])
530 {
Koushik K. Dutta16f0b492010-03-19 14:37:11 -0700531 if (0 != ensure_root_path_unmounted(mounts[chosen_item][2]))
532 ui_print("Error unmounting %s!\n", mounts[chosen_item][2]);
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700533 }
534 else
535 {
Koushik K. Dutta16f0b492010-03-19 14:37:11 -0700536 if (0 != ensure_root_path_mounted(mounts[chosen_item][2]))
537 ui_print("Error mounting %s!\n", mounts[chosen_item][2]);
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700538 }
539 }
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700540 else if (chosen_item < MOUNTABLE_COUNT + MTD_COUNT)
541 {
542 chosen_item = chosen_item - MOUNTABLE_COUNT;
Koushik Duttaecd32fa2010-07-14 18:38:02 -0700543 if (!confirm_selection(confirm_format, confirm))
Koushik K. Dutta16f0b492010-03-19 14:37:11 -0700544 continue;
545 ui_print("Formatting %s...\n", mtds[chosen_item][1]);
Koushik K. Dutta3f995392010-03-30 23:29:43 -0700546 if (0 != format_root_device(mtds[chosen_item][1]))
547 ui_print("Error formatting %s!\n", mtds[chosen_item][1]);
548 else
549 ui_print("Done.\n");
550 }
551 else if (chosen_item < MOUNTABLE_COUNT + MTD_COUNT + MMC_COUNT)
552 {
553 chosen_item = chosen_item - MOUNTABLE_COUNT - MTD_COUNT;
Koushik Duttaecd32fa2010-07-14 18:38:02 -0700554 if (!confirm_selection(confirm_format, confirm))
Koushik K. Dutta3f995392010-03-30 23:29:43 -0700555 continue;
556 ui_print("Formatting %s...\n", mmcs[chosen_item][1]);
Koushik Dutta2f73e582010-04-18 16:00:21 -0700557 if (0 != format_non_mtd_device(mmcs[chosen_item][1]))
Koushik K. Dutta3f995392010-03-30 23:29:43 -0700558 ui_print("Error formatting %s!\n", mmcs[chosen_item][1]);
559 else
560 ui_print("Done.\n");
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700561 }
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700562 }
563}
564
Koushik K. Dutta72a1db62010-03-07 14:10:26 -0800565#define EXTENDEDCOMMAND_SCRIPT "/cache/recovery/extendedcommand"
566
567int extendedcommand_file_exists()
Koushik K. Duttaf68aaaf2010-03-07 13:39:21 -0800568{
Koushik K. Duttaf68aaaf2010-03-07 13:39:21 -0800569 struct stat file_info;
Koushik K. Dutta72a1db62010-03-07 14:10:26 -0800570 return 0 == stat(EXTENDEDCOMMAND_SCRIPT, &file_info);
571}
572
Koushik K. Dutta3836f722010-03-11 22:17:43 -0800573int run_script_from_buffer(char* script_data, int script_len, char* filename)
Koushik K. Dutta72a1db62010-03-07 14:10:26 -0800574{
Koushik K. Duttaf68aaaf2010-03-07 13:39:21 -0800575 /* Parse the script. Note that the script and parse tree are never freed.
576 */
577 const AmCommandList *commands = parseAmendScript(script_data, script_len);
578 if (commands == NULL) {
579 printf("Syntax error in update script\n");
580 return 1;
581 } else {
Koushik K. Dutta72a1db62010-03-07 14:10:26 -0800582 printf("Parsed %.*s\n", script_len, filename);
Koushik K. Duttaf68aaaf2010-03-07 13:39:21 -0800583 }
584
585 /* Execute the script.
586 */
587 int ret = execCommandList((ExecContext *)1, commands);
588 if (ret != 0) {
589 int num = ret;
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800590 char *line = NULL, *next = script_data;
Koushik K. Duttaf68aaaf2010-03-07 13:39:21 -0800591 while (next != NULL && ret-- > 0) {
592 line = next;
593 next = memchr(line, '\n', script_data + script_len - line);
594 if (next != NULL) *next++ = '\0';
595 }
596 printf("Failure at line %d:\n%s\n", num, next ? line : "(not found)");
597 return 1;
598 }
599
600 return 0;
601}
Koushik K. Dutta72a1db62010-03-07 14:10:26 -0800602
Koushik K. Dutta6771aca2010-04-03 23:28:39 -0700603int run_script(char* filename)
Koushik K. Dutta3836f722010-03-11 22:17:43 -0800604{
605 struct stat file_info;
606 if (0 != stat(filename, &file_info)) {
607 printf("Error executing stat on file: %s\n", filename);
608 return 1;
609 }
610
611 int script_len = file_info.st_size;
Koushik K. Dutta707fa6d2010-03-23 11:44:33 -0700612 char* script_data = (char*)malloc(script_len + 1);
Koushik K. Dutta3836f722010-03-11 22:17:43 -0800613 FILE *file = fopen(filename, "rb");
614 fread(script_data, script_len, 1, file);
Koushik K. Dutta707fa6d2010-03-23 11:44:33 -0700615 // supposedly not necessary, but let's be safe.
616 script_data[script_len] = '\0';
Koushik K. Dutta3836f722010-03-11 22:17:43 -0800617 fclose(file);
Koushik Duttae25908b2010-06-21 08:16:19 -0700618 LOGI("Running script:\n");
619 LOGI("\n%s\n", script_data);
Koushik K. Dutta3836f722010-03-11 22:17:43 -0800620
Koushik K. Duttaf3534d02010-04-18 18:06:24 -0700621 int ret = run_script_from_buffer(script_data, script_len, filename);
Koushik K. Dutta1d53c4e2010-04-02 16:46:21 -0700622 free(script_data);
623 return ret;
Koushik K. Dutta3836f722010-03-11 22:17:43 -0800624}
625
Koushik K. Dutta72a1db62010-03-07 14:10:26 -0800626int run_and_remove_extendedcommand()
627{
Koushik K. Dutta6771aca2010-04-03 23:28:39 -0700628 char tmp[PATH_MAX];
629 sprintf(tmp, "cp %s /tmp/%s", EXTENDEDCOMMAND_SCRIPT, basename(EXTENDEDCOMMAND_SCRIPT));
630 __system(tmp);
Koushik Dutta1bf4f692010-07-14 18:37:33 -0700631 __system("rm /sdcard/clockworkmod/.recoverycheckpoint");
Koushik K. Dutta6771aca2010-04-03 23:28:39 -0700632 remove(EXTENDEDCOMMAND_SCRIPT);
Koushik K. Dutta3a25cf52010-03-08 19:22:41 -0800633 int i = 0;
634 for (i = 20; i > 0; i--) {
635 ui_print("Waiting for SD Card to mount (%ds)\n", i);
636 if (ensure_root_path_mounted("SDCARD:") == 0) {
637 ui_print("SD Card mounted...\n");
638 break;
639 }
640 sleep(1);
641 }
642 if (i == 0) {
643 ui_print("Timed out waiting for SD card... continuing anyways.");
644 }
645
Koushik K. Dutta6771aca2010-04-03 23:28:39 -0700646 sprintf(tmp, "/tmp/%s", basename(EXTENDEDCOMMAND_SCRIPT));
647 return run_script(tmp);
Koushik K. Dutta72a1db62010-03-07 14:10:26 -0800648}
649
650int amend_main(int argc, char** argv)
651{
652 if (argc != 2)
653 {
654 printf("Usage: amend <script>\n");
655 return 0;
656 }
657
658 RecoveryCommandContext ctx = { NULL };
659 if (register_update_commands(&ctx)) {
660 LOGE("Can't install update commands\n");
661 }
Koushik K. Dutta6771aca2010-04-03 23:28:39 -0700662 return run_script(argv[1]);
Koushik K. Dutta5306db52010-03-08 14:09:35 -0800663}
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700664
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700665void show_nandroid_advanced_restore_menu()
666{
667 if (ensure_root_path_mounted("SDCARD:") != 0) {
668 LOGE ("Can't mount /sdcard\n");
669 return;
670 }
671
672 static char* advancedheaders[] = { "Choose an image to restore",
673 "",
674 "Choose an image to restore",
675 "first. The next menu will",
676 "you more options.",
677 "",
678 NULL
679 };
680
681 char* file = choose_file_menu("/sdcard/clockworkmod/backup/", NULL, advancedheaders);
682 if (file == NULL)
683 return;
684
685 static char* headers[] = { "Nandroid Advanced Restore",
686 "",
687 NULL
688 };
689
690 static char* list[] = { "Restore boot",
691 "Restore system",
692 "Restore data",
693 "Restore cache",
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700694 "Restore sd-ext",
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700695 NULL
696 };
697
698 int chosen_item = get_menu_selection(headers, list, 0);
699 switch (chosen_item)
700 {
701 case 0:
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700702 nandroid_restore(file, 1, 0, 0, 0, 0);
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700703 break;
704 case 1:
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700705 nandroid_restore(file, 0, 1, 0, 0, 0);
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700706 break;
707 case 2:
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700708 nandroid_restore(file, 0, 0, 1, 0, 0);
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700709 break;
710 case 3:
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700711 nandroid_restore(file, 0, 0, 0, 1, 0);
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700712 break;
Koushik Dutta2f73e582010-04-18 16:00:21 -0700713 case 4:
714 nandroid_restore(file, 0, 0, 0, 0, 1);
715 break;
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700716 }
717}
718
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700719void show_nandroid_menu()
720{
721 static char* headers[] = { "Nandroid",
722 "",
723 NULL
724 };
725
726 static char* list[] = { "Backup",
727 "Restore",
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700728 "Advanced Restore",
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700729 NULL
730 };
731
732 int chosen_item = get_menu_selection(headers, list, 0);
733 switch (chosen_item)
734 {
735 case 0:
736 {
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700737 char backup_path[PATH_MAX];
Koushik K. Dutta6a26e7c2010-03-27 15:26:11 -0700738 time_t t = time(NULL);
739 struct tm *tmp = localtime(&t);
740 if (tmp == NULL)
741 {
742 struct timeval tp;
743 gettimeofday(&tp, NULL);
744 sprintf(backup_path, "/sdcard/clockworkmod/backup/%d", tp.tv_sec);
745 }
746 else
747 {
748 strftime(backup_path, sizeof(backup_path), "/sdcard/clockworkmod/backup/%F.%H.%M.%S", tmp);
749 }
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700750 nandroid_backup(backup_path);
751 }
752 break;
753 case 1:
754 show_nandroid_restore_menu();
755 break;
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700756 case 2:
757 show_nandroid_advanced_restore_menu();
758 break;
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700759 }
Koushik K. Duttaa496b512010-03-19 14:51:45 -0700760}
761
Koushik Duttafd1579b2010-05-01 12:46:55 -0700762void wipe_battery_stats()
763{
764 ensure_root_path_mounted("DATA:");
765 remove("/data/system/batterystats.bin");
766 ensure_root_path_unmounted("DATA:");
767}
768
Koushik K. Duttaa496b512010-03-19 14:51:45 -0700769void show_advanced_menu()
770{
771 static char* headers[] = { "Advanced and Debugging Menu",
772 "",
773 NULL
774 };
775
Koushik K. Dutta7fe4d7b2010-04-06 23:04:52 -0700776 static char* list[] = { "Reboot Recovery",
Koushik Dutta49af23c2010-06-21 13:45:51 -0700777 "Wipe Dalvik Cache",
Koushik Duttafd1579b2010-05-01 12:46:55 -0700778 "Wipe Battery Stats",
Koushik Dutta598cfc72010-06-20 09:42:47 -0700779 "Report Error",
Koushik K. Dutta7fe4d7b2010-04-06 23:04:52 -0700780 "Key Test",
Koushik K. Duttaa496b512010-03-19 14:51:45 -0700781 NULL
782 };
783
784 for (;;)
785 {
786 int chosen_item = get_menu_selection(headers, list, 0);
787 if (chosen_item == GO_BACK)
788 break;
789 switch (chosen_item)
790 {
791 case 0:
Koushik K. Dutta7fe4d7b2010-04-06 23:04:52 -0700792 __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, "recovery");
793 break;
794 case 1:
Koushik Dutta49af23c2010-06-21 13:45:51 -0700795 if (0 != ensure_root_path_mounted("DATA:"))
796 break;
797 __system("rm -r /data/dalvik-cache");
798 ensure_root_path_unmounted("DATA:");
Koushik Duttafd1579b2010-05-01 12:46:55 -0700799 break;
800 case 2:
Koushik Dutta49af23c2010-06-21 13:45:51 -0700801 wipe_battery_stats();
Koushik Dutta598cfc72010-06-20 09:42:47 -0700802 break;
803 case 3:
Koushik Dutta49af23c2010-06-21 13:45:51 -0700804 handle_failure(1);
805 break;
806 case 4:
Koushik K. Duttaa496b512010-03-19 14:51:45 -0700807 {
808 ui_print("Outputting key codes.\n");
809 ui_print("Go back to end debugging.\n");
810 int key;
811 int action;
812 do
813 {
814 key = ui_wait_key();
815 action = device_handle_key(key, 1);
816 ui_print("Key: %d\n", key);
817 }
818 while (action != GO_BACK);
819 }
820 }
821 }
Koushik Dutta14239d22010-06-14 15:02:48 -0700822}
823
824void write_fstab_root(char *root_path, FILE *file)
825{
826 RootInfo *info = get_root_info_for_path(root_path);
Koushik Duttaa6522b32010-06-20 13:16:06 -0700827 if (info == NULL) {
828 LOGW("Unable to get root info for %s during fstab generation!", root_path);
Koushik Dutta598cfc72010-06-20 09:42:47 -0700829 return;
Koushik Duttaa6522b32010-06-20 13:16:06 -0700830 }
Koushik Dutta14239d22010-06-14 15:02:48 -0700831 MtdPartition *mtd = get_root_mtd_partition(root_path);
832 if (mtd != NULL)
833 {
834 fprintf(file, "/dev/block/mtdblock%d ", mtd->device_index);
835 }
836 else
837 {
Koushik Dutta8ec94182010-06-21 12:27:43 -0700838 // only SDCARD: seems to be using device2.
839 // and mmcblkXp1 is the fallback/device2.
840 // However, generally, mmcblkXp1 is usually where the
841 // FAT partition is located... so favor that.
842 if (NULL == info->device2)
843 fprintf(file, "%s ", info->device);
844 else
845 fprintf(file, "%s ", info->device2);
Koushik Dutta14239d22010-06-14 15:02:48 -0700846 }
847
848 fprintf(file, "%s ", info->mount_point);
849 fprintf(file, "%s rw\n", info->filesystem);
850}
851
852void create_fstab()
853{
Koushik Duttacd44ab92010-06-23 00:02:14 -0700854 __system("touch /etc/mtab");
Koushik Dutta14239d22010-06-14 15:02:48 -0700855 FILE *file = fopen("/etc/fstab", "w");
Koushik Duttaa6522b32010-06-20 13:16:06 -0700856 if (file == NULL) {
857 LOGW("Unable to create /etc/fstab!");
Koushik Dutta598cfc72010-06-20 09:42:47 -0700858 return;
Koushik Duttaa6522b32010-06-20 13:16:06 -0700859 }
Koushik Dutta14239d22010-06-14 15:02:48 -0700860 write_fstab_root("CACHE:", file);
861 write_fstab_root("DATA:", file);
Koushik Duttaa6522b32010-06-20 13:16:06 -0700862#ifdef HAS_DATADATA
863 write_fstab_root("DATADATA:", file);
864#endif
Koushik Dutta14239d22010-06-14 15:02:48 -0700865 write_fstab_root("SYSTEM:", file);
866 write_fstab_root("SDCARD:", file);
867 write_fstab_root("SDEXT:", file);
868 fclose(file);
Koushik Dutta598cfc72010-06-20 09:42:47 -0700869}
870
871void handle_failure(int ret)
872{
873 if (ret == 0)
874 return;
875 if (0 != ensure_root_path_mounted("SDCARD:"))
876 return;
Koushik Duttaa6522b32010-06-20 13:16:06 -0700877 mkdir("/sdcard/clockworkmod", S_IRWXU);
878 __system("cp /tmp/recovery.log /sdcard/clockworkmod/recovery.log");
Koushik Dutta598cfc72010-06-20 09:42:47 -0700879 ui_print("/tmp/recovery.log was copied to /sdcard/clockworkmod/recovery.log. Please open ROM Manager to report the issue.");
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700880}