blob: 717d8d27ecd673361bf4480cd7934c23d82a7cce [file] [log] [blame]
preludedrew38058dc2011-01-29 23:30:44 -07001#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
15#include <sys/wait.h>
16#include <sys/limits.h>
17#include <dirent.h>
18#include <sys/stat.h>
19
20#include <signal.h>
21#include <sys/wait.h>
22
23#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
33#include "../../external/yaffs2/yaffs2/utils/mkyaffs2image.h"
34#include "../../external/yaffs2/yaffs2/utils/unyaffs.h"
35
36#include "extendedcommands.h"
37#include "nandroid.h"
38#include "mounts.h"
39#include "flashutils/flashutils.h"
40#include "edify/expr.h"
41
42int signature_check_enabled = 1;
43int script_assert_enabled = 1;
44static const char *SDCARD_UPDATE_FILE = "/sdcard/update.zip";
45
46void
47toggle_signature_check()
48{
49 signature_check_enabled = !signature_check_enabled;
50 ui_print("Signature Check: %s\n", signature_check_enabled ? "Enabled" : "Disabled");
51}
52
53void toggle_script_asserts()
54{
55 script_assert_enabled = !script_assert_enabled;
56 ui_print("Script Asserts: %s\n", script_assert_enabled ? "Enabled" : "Disabled");
57}
58
59int install_zip(const char* packagefilepath)
60{
61 ui_print("\n-- Installing: %s\n", packagefilepath);
62 if (device_flash_type() == MTD) {
63 set_sdcard_update_bootloader_message();
64 }
65 int status = install_package(packagefilepath);
66 ui_reset_progress();
67 if (status != INSTALL_SUCCESS) {
68 ui_set_background(BACKGROUND_ICON_ERROR);
69 ui_print("Installation aborted.\n");
70 return 1;
71 }
72 ui_set_background(BACKGROUND_ICON_NONE);
73 ui_print("\nInstall from sdcard complete.\n");
74 return 0;
75}
76
77char* INSTALL_MENU_ITEMS[] = { "apply /sdcard/update.zip",
78 "choose zip from sdcard",
79 "toggle signature verification",
80 "toggle script asserts",
81 NULL };
82#define ITEM_APPLY_SDCARD 0
83#define ITEM_CHOOSE_ZIP 1
84#define ITEM_SIG_CHECK 2
85#define ITEM_ASSERTS 3
86
87void show_install_update_menu()
88{
89 static char* headers[] = { "Apply update from .zip file on SD card",
90 "",
91 NULL
92 };
93 for (;;)
94 {
95 int chosen_item = get_menu_selection(headers, INSTALL_MENU_ITEMS, 0, 0);
96 switch (chosen_item)
97 {
98 case ITEM_ASSERTS:
99 toggle_script_asserts();
100 break;
101 case ITEM_SIG_CHECK:
102 toggle_signature_check();
103 break;
104 case ITEM_APPLY_SDCARD:
105 {
106 if (confirm_selection("Confirm install?", "Yes - Install /sdcard/update.zip"))
107 install_zip(SDCARD_UPDATE_FILE);
108 break;
109 }
110 case ITEM_CHOOSE_ZIP:
111 show_choose_zip_menu();
112 break;
113 default:
114 return;
115 }
116
117 }
118}
119
120void 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
134char** gather_files(const char* directory, const char* fileExtensionOrDirectory, int* numFiles)
135{
136 char path[PATH_MAX] = "";
137 DIR *dir;
138 struct dirent *de;
139 int total = 0;
140 int i;
141 char** files = NULL;
142 int pass;
143 *numFiles = 0;
144 int dirLen = strlen(directory);
145
146 dir = opendir(directory);
147 if (dir == NULL) {
148 ui_print("Couldn't open directory.\n");
149 return NULL;
150 }
151
152 int extension_length = 0;
153 if (fileExtensionOrDirectory != NULL)
154 extension_length = strlen(fileExtensionOrDirectory);
155
156 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;
177 char fullFileName[PATH_MAX];
178 strcpy(fullFileName, directory);
179 strcat(fullFileName, de->d_name);
180 stat(fullFileName, &info);
181 // 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
192 files[i] = (char*) malloc(dirLen + strlen(de->d_name) + 2);
193 strcpy(files[i], directory);
194 strcat(files[i], de->d_name);
195 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 }
208
209 if(closedir(dir) < 0) {
210 LOGE("Failed to close directory.");
211 }
212
213 if (total==0) {
214 return NULL;
215 }
216
217 // 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
232 return files;
233}
234
235// 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;
242 int numDirs = 0;
243 int i;
244 char* return_value = NULL;
245 int dir_len = strlen(directory);
246
247 char** files = gather_files(directory, fileExtensionOrDirectory, &numFiles);
248 char** dirs = NULL;
249 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");
255 }
256 else
257 {
258 char** list = (char**) malloc((total + 1) * sizeof(char*));
259 list[total] = NULL;
260
261
262 for (i = 0 ; i < numDirs; i++)
263 {
264 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, 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);
294 }
295
296 free_string_array(files);
297 free_string_array(dirs);
298 return return_value;
299}
300
301void show_choose_zip_menu()
302{
303 if (ensure_path_mounted("/sdcard") != 0) {
304 LOGE ("Can't mount /sdcard\n");
305 return;
306 }
307
308 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 static char* confirm_install = "Confirm install?";
317 static char confirm[PATH_MAX];
318 sprintf(confirm, "Yes - Install %s", basename(file));
319 if (confirm_selection(confirm_install, confirm))
320 install_zip(file);
321}
322
323void show_nandroid_restore_menu()
324{
325 if (ensure_path_mounted("/sdcard") != 0) {
326 LOGE ("Can't mount /sdcard\n");
327 return;
328 }
329
330 static char* headers[] = { "Choose an image to restore",
331 "",
332 NULL
333 };
334
335 char* file = choose_file_menu("/sdcard/clockworkmod/backup/", NULL, headers);
336 if (file == NULL)
337 return;
338
339 if (confirm_selection("Confirm restore?", "Yes - Restore"))
340 nandroid_restore(file, 1, 1, 1, 1, 1, 0);
341}
342
andrew.boren1acd15e2011-03-31 21:41:22 -0700343#ifndef BOARD_UMS_LUNFILE
344#define BOARD_UMS_LUNFILE "/sys/devices/platform/usb_mass_storage/lun0/file"
345#endif
346
preludedrew38058dc2011-01-29 23:30:44 -0700347void show_mount_usb_storage_menu()
348{
349 int fd;
350 Volume *vol = volume_for_path("/sdcard");
andrew.boren1acd15e2011-03-31 21:41:22 -0700351 if ((fd = open(BOARD_UMS_LUNFILE, O_WRONLY)) < 0) {
preludedrew38058dc2011-01-29 23:30:44 -0700352 LOGE("Unable to open ums lunfile (%s)", strerror(errno));
353 return -1;
354 }
355
356 if (write(fd, vol->device, strlen(vol->device)) < 0) {
357 LOGE("Unable to write to ums lunfile (%s)", strerror(errno));
358 close(fd);
359 return -1;
360 }
361 static char* headers[] = { "USB Mass Storage device",
362 "Leaving this menu unmount",
363 "your SD card from your PC.",
364 "",
365 NULL
366 };
367
368 static char* list[] = { "Unmount", NULL };
369
370 for (;;)
371 {
372 int chosen_item = get_menu_selection(headers, list, 0, 0);
373 if (chosen_item == GO_BACK || chosen_item == 0)
374 break;
375 }
376
andrew.boren1acd15e2011-03-31 21:41:22 -0700377 if ((fd = open(BOARD_UMS_LUNFILE, O_WRONLY)) < 0) {
preludedrew38058dc2011-01-29 23:30:44 -0700378 LOGE("Unable to open ums lunfile (%s)", strerror(errno));
379 return -1;
380 }
381
382 char ch = 0;
383 if (write(fd, &ch, 1) < 0) {
384 LOGE("Unable to write to ums lunfile (%s)", strerror(errno));
385 close(fd);
386 return -1;
387 }
388}
389
390int confirm_selection(const char* title, const char* confirm)
391{
392 struct stat info;
393 if (0 == stat("/sdcard/clockworkmod/.no_confirm", &info))
394 return 1;
395
396 char* confirm_headers[] = { title, " THIS CAN NOT BE UNDONE.", "", NULL };
397 char* items[] = { "No",
398 "No",
399 "No",
400 "No",
401 "No",
402 "No",
403 "No",
404 confirm, //" Yes -- wipe partition", // [7
405 "No",
406 "No",
407 "No",
408 NULL };
409
410 int chosen_item = get_menu_selection(confirm_headers, items, 0, 0);
411 return chosen_item == 7;
412}
413
414#define MKE2FS_BIN "/sbin/mke2fs"
415#define TUNE2FS_BIN "/sbin/tune2fs"
416#define E2FSCK_BIN "/sbin/e2fsck"
417
418int format_unknown_device(const char *device, const char* path, const char *fs_type)
419{
420 LOGI("Formatting unknown device.\n");
421
422 // device may simply be a name, like "system"
423 if (device[0] != '/')
424 return erase_raw_partition(device);
425
426 // if this is SDEXT:, don't worry about it if it does not exist.
427 if (0 == strcmp(path, "/sd-ext"))
428 {
429 struct stat st;
430 Volume *vol = volume_for_path("/sd-ext");
431 if (vol == NULL || 0 != stat(vol->device, &st))
432 {
433 ui_print("No app2sd partition found. Skipping format of /sd-ext.\n");
434 return 0;
435 }
436 }
437
438 if (NULL != fs_type) {
439 if (strcmp("ext3", fs_type) == 0) {
440 LOGI("Formatting ext3 device.\n");
441 if (0 != ensure_path_unmounted(path)) {
442 LOGE("Error while unmounting %s.\n", path);
443 return -12;
444 }
445 return format_ext3_device(device);
446 }
447
448 if (strcmp("ext2", fs_type) == 0) {
449 LOGI("Formatting ext2 device.\n");
450 if (0 != ensure_path_unmounted(path)) {
451 LOGE("Error while unmounting %s.\n", path);
452 return -12;
453 }
454 return format_ext2_device(device);
455 }
456 }
457
458 if (0 != ensure_path_mounted(path))
459 {
460 ui_print("Error mounting %s!\n", path);
461 ui_print("Skipping format...\n");
462 return 0;
463 }
464
465 static char tmp[PATH_MAX];
466 sprintf(tmp, "rm -rf %s/*", path);
467 __system(tmp);
468 sprintf(tmp, "rm -rf %s/.*", path);
469 __system(tmp);
470
471 ensure_path_unmounted(path);
472 return 0;
473}
474
475//#define MOUNTABLE_COUNT 5
476//#define DEVICE_COUNT 4
477//#define MMC_COUNT 2
478
andrew.boren1acd15e2011-03-31 21:41:22 -0700479typedef struct {
480 char mount[255];
481 char unmount[255];
482 Volume* v;
483} MountMenuEntry;
484
485typedef struct {
486 char txt[255];
487 Volume* v;
488} FormatMenuEntry;
489
preludedrew38058dc2011-01-29 23:30:44 -0700490void show_partition_menu()
491{
492 static char* headers[] = { "Mounts and Storage Menu",
493 "",
494 NULL
495 };
496
andrew.boren1acd15e2011-03-31 21:41:22 -0700497 static MountMenuEntry* mount_menue = NULL;
498 static FormatMenuEntry* format_menue = NULL;
499
preludedrew38058dc2011-01-29 23:30:44 -0700500 typedef char* string;
preludedrew38058dc2011-01-29 23:30:44 -0700501
andrew.boren1acd15e2011-03-31 21:41:22 -0700502 int i, mountable_volumes, formatable_volumes;
503 int num_volumes;
504 Volume* device_volumes;
preludedrew38058dc2011-01-29 23:30:44 -0700505
andrew.boren1acd15e2011-03-31 21:41:22 -0700506 num_volumes = get_num_volumes();
507 device_volumes = get_device_volumes();
508
509 string options[255];
510
511 if(!device_volumes)
512 return;
513
514 mountable_volumes = 0;
515 formatable_volumes = 0;
516
517 mount_menue = malloc(num_volumes * sizeof(MountMenuEntry));
518 format_menue = malloc(num_volumes * sizeof(FormatMenuEntry));
519
520 for (i = 0; i < num_volumes; ++i) {
521 Volume* v = &device_volumes[i];
522 if(strcmp("ramdisk", v->fs_type) != 0 && strcmp("mtd", v->fs_type) != 0 && strcmp("emmc", v->fs_type) != 0 && strcmp("bml", v->fs_type) != 0)
523 {
524 sprintf(&mount_menue[mountable_volumes].mount, "mount %s", v->mount_point);
525 sprintf(&mount_menue[mountable_volumes].unmount, "unmount %s", v->mount_point);
526 mount_menue[mountable_volumes].v = &device_volumes[i];
527 ++mountable_volumes;
528 sprintf(&format_menue[formatable_volumes].txt, "format %s", v->mount_point);
529 format_menue[formatable_volumes].v = &device_volumes[i];
530 ++formatable_volumes;
531 }
532 else if (strcmp("ramdisk", v->fs_type) != 0 && strcmp("misc", v->mount_point) != 0 && strcmp("mtd", v->fs_type) == 0)
533 {
534 sprintf(&format_menue[formatable_volumes].txt, "format %s", v->mount_point);
535 format_menue[formatable_volumes].v = &device_volumes[i];
536 ++formatable_volumes;
537 }
538 }
539
preludedrew38058dc2011-01-29 23:30:44 -0700540
541 static char* confirm_format = "Confirm format?";
542 static char* confirm = "Yes - Format";
543
544 for (;;)
545 {
preludedrew38058dc2011-01-29 23:30:44 -0700546
andrew.boren1acd15e2011-03-31 21:41:22 -0700547 for (i = 0; i < mountable_volumes; i++)
548 {
549 MountMenuEntry* e = &mount_menue[i];
550 Volume* v = e->v;
551 if(is_path_mounted(v->mount_point))
552 options[i] = e->unmount;
553 else
554 options[i] = e->mount;
555 }
preludedrew38058dc2011-01-29 23:30:44 -0700556
andrew.boren1acd15e2011-03-31 21:41:22 -0700557 for (i = 0; i < formatable_volumes; i++)
558 {
559 FormatMenuEntry* e = &format_menue[i];
preludedrew38058dc2011-01-29 23:30:44 -0700560
andrew.boren1acd15e2011-03-31 21:41:22 -0700561 options[mountable_volumes+i] = e->txt;
562 }
563
564 options[mountable_volumes+formatable_volumes] = "mount USB storage";
565 options[mountable_volumes+formatable_volumes + 1] = NULL;
566
567 int chosen_item = get_menu_selection(headers, &options, 0, 0);
preludedrew38058dc2011-01-29 23:30:44 -0700568 if (chosen_item == GO_BACK)
569 break;
andrew.boren1acd15e2011-03-31 21:41:22 -0700570 if (chosen_item == (mountable_volumes+formatable_volumes))
preludedrew38058dc2011-01-29 23:30:44 -0700571 {
572 show_mount_usb_storage_menu();
573 }
andrew.boren1acd15e2011-03-31 21:41:22 -0700574 else if (chosen_item < mountable_volumes)
preludedrew38058dc2011-01-29 23:30:44 -0700575 {
andrew.boren1acd15e2011-03-31 21:41:22 -0700576 MountMenuEntry* e = &mount_menue[chosen_item];
577 Volume* v = e->v;
578
579 if (is_path_mounted(v->mount_point))
preludedrew38058dc2011-01-29 23:30:44 -0700580 {
andrew.boren1acd15e2011-03-31 21:41:22 -0700581 if (0 != ensure_path_unmounted(v->mount_point))
582 ui_print("Error unmounting %s!\n", v->mount_point);
preludedrew38058dc2011-01-29 23:30:44 -0700583 }
584 else
585 {
andrew.boren1acd15e2011-03-31 21:41:22 -0700586 if (0 != ensure_path_mounted(v->mount_point))
587 ui_print("Error mounting %s!\n", v->mount_point);
preludedrew38058dc2011-01-29 23:30:44 -0700588 }
589 }
andrew.boren1acd15e2011-03-31 21:41:22 -0700590 else if (chosen_item < (mountable_volumes + formatable_volumes))
preludedrew38058dc2011-01-29 23:30:44 -0700591 {
andrew.boren1acd15e2011-03-31 21:41:22 -0700592 chosen_item = chosen_item - mountable_volumes;
593 FormatMenuEntry* e = &format_menue[chosen_item];
594 Volume* v = e->v;
595
preludedrew38058dc2011-01-29 23:30:44 -0700596 if (!confirm_selection(confirm_format, confirm))
597 continue;
andrew.boren1acd15e2011-03-31 21:41:22 -0700598 ui_print("Formatting %s...\n", v->mount_point);
599 if (0 != format_volume(v->mount_point))
600 ui_print("Error formatting %s!\n", v->mount_point);
preludedrew38058dc2011-01-29 23:30:44 -0700601 else
602 ui_print("Done.\n");
603 }
604 }
andrew.boren1acd15e2011-03-31 21:41:22 -0700605
606 free(mount_menue);
607 free(format_menue);
608
preludedrew38058dc2011-01-29 23:30:44 -0700609}
610
611#define EXTENDEDCOMMAND_SCRIPT "/cache/recovery/extendedcommand"
612
613int extendedcommand_file_exists()
614{
615 struct stat file_info;
616 return 0 == stat(EXTENDEDCOMMAND_SCRIPT, &file_info);
617}
618
619int edify_main(int argc, char** argv) {
620 load_volume_table();
621 process_volumes();
622 RegisterBuiltins();
623 RegisterRecoveryHooks();
624 FinishRegistration();
625
626 if (argc != 2) {
627 printf("edify <filename>\n");
628 return 1;
629 }
630
631 FILE* f = fopen(argv[1], "r");
632 if (f == NULL) {
633 printf("%s: %s: No such file or directory\n", argv[0], argv[1]);
634 return 1;
635 }
636 char buffer[8192];
637 int size = fread(buffer, 1, 8191, f);
638 fclose(f);
639 buffer[size] = '\0';
640
641 Expr* root;
642 int error_count = 0;
643 yy_scan_bytes(buffer, size);
644 int error = yyparse(&root, &error_count);
645 printf("parse returned %d; %d errors encountered\n", error, error_count);
646 if (error == 0 || error_count > 0) {
647
648 //ExprDump(0, root, buffer);
649
650 State state;
651 state.cookie = NULL;
652 state.script = buffer;
653 state.errmsg = NULL;
654
655 char* result = Evaluate(&state, root);
656 if (result == NULL) {
657 printf("result was NULL, message is: %s\n",
658 (state.errmsg == NULL ? "(NULL)" : state.errmsg));
659 free(state.errmsg);
660 } else {
661 printf("result is [%s]\n", result);
662 }
663 }
664 return 0;
665}
666
667int run_script(char* filename)
668{
669 struct stat file_info;
670 if (0 != stat(filename, &file_info)) {
671 printf("Error executing stat on file: %s\n", filename);
672 return 1;
673 }
674
675 int script_len = file_info.st_size;
676 char* script_data = (char*)malloc(script_len + 1);
677 FILE *file = fopen(filename, "rb");
678 fread(script_data, script_len, 1, file);
679 // supposedly not necessary, but let's be safe.
680 script_data[script_len] = '\0';
681 fclose(file);
682 LOGI("Running script:\n");
683 LOGI("\n%s\n", script_data);
684
685 int ret = run_script_from_buffer(script_data, script_len, filename);
686 free(script_data);
687 return ret;
688}
689
690int run_and_remove_extendedcommand()
691{
692 char tmp[PATH_MAX];
693 sprintf(tmp, "cp %s /tmp/%s", EXTENDEDCOMMAND_SCRIPT, basename(EXTENDEDCOMMAND_SCRIPT));
694 __system(tmp);
695 remove(EXTENDEDCOMMAND_SCRIPT);
696 int i = 0;
697 for (i = 20; i > 0; i--) {
698 ui_print("Waiting for SD Card to mount (%ds)\n", i);
699 if (ensure_path_mounted("/sdcard") == 0) {
700 ui_print("SD Card mounted...\n");
701 break;
702 }
703 sleep(1);
704 }
705 remove("/sdcard/clockworkmod/.recoverycheckpoint");
706 if (i == 0) {
707 ui_print("Timed out waiting for SD card... continuing anyways.");
708 }
709
710 sprintf(tmp, "/tmp/%s", basename(EXTENDEDCOMMAND_SCRIPT));
711 return run_script(tmp);
712}
713
714void show_nandroid_advanced_restore_menu()
715{
716 if (ensure_path_mounted("/sdcard") != 0) {
717 LOGE ("Can't mount /sdcard\n");
718 return;
719 }
720
721 static char* advancedheaders[] = { "Choose an image to restore",
722 "",
723 "Choose an image to restore",
724 "first. The next menu will",
725 "you more options.",
726 "",
727 NULL
728 };
729
730 char* file = choose_file_menu("/sdcard/clockworkmod/backup/", NULL, advancedheaders);
731 if (file == NULL)
732 return;
733
734 static char* headers[] = { "Nandroid Advanced Restore",
735 "",
736 NULL
737 };
738
739 static char* list[] = { "Restore boot",
740 "Restore system",
741 "Restore data",
742 "Restore cache",
743 "Restore sd-ext",
744 "Restore wimax",
745 NULL
746 };
747
748 char tmp[PATH_MAX];
749 if (0 != get_partition_device("wimax", tmp)) {
750 // disable wimax restore option
751 list[5] = NULL;
752 }
753
754 static char* confirm_restore = "Confirm restore?";
755
756 int chosen_item = get_menu_selection(headers, list, 0, 0);
757 switch (chosen_item)
758 {
759 case 0:
760 if (confirm_selection(confirm_restore, "Yes - Restore boot"))
761 nandroid_restore(file, 1, 0, 0, 0, 0, 0);
762 break;
763 case 1:
764 if (confirm_selection(confirm_restore, "Yes - Restore system"))
765 nandroid_restore(file, 0, 1, 0, 0, 0, 0);
766 break;
767 case 2:
768 if (confirm_selection(confirm_restore, "Yes - Restore data"))
769 nandroid_restore(file, 0, 0, 1, 0, 0, 0);
770 break;
771 case 3:
772 if (confirm_selection(confirm_restore, "Yes - Restore cache"))
773 nandroid_restore(file, 0, 0, 0, 1, 0, 0);
774 break;
775 case 4:
776 if (confirm_selection(confirm_restore, "Yes - Restore sd-ext"))
777 nandroid_restore(file, 0, 0, 0, 0, 1, 0);
778 break;
779 case 5:
780 if (confirm_selection(confirm_restore, "Yes - Restore wimax"))
781 nandroid_restore(file, 0, 0, 0, 0, 0, 1);
782 break;
783 }
784}
785
786void show_nandroid_menu()
787{
788 static char* headers[] = { "Nandroid",
789 "",
790 NULL
791 };
792
793 static char* list[] = { "Backup",
794 "Restore",
795 "Advanced Restore",
796 NULL
797 };
798
799 int chosen_item = get_menu_selection(headers, list, 0, 0);
800 switch (chosen_item)
801 {
802 case 0:
803 {
804 char backup_path[PATH_MAX];
805 time_t t = time(NULL);
806 struct tm *tmp = localtime(&t);
807 if (tmp == NULL)
808 {
809 struct timeval tp;
810 gettimeofday(&tp, NULL);
811 sprintf(backup_path, "/sdcard/clockworkmod/backup/%d", tp.tv_sec);
812 }
813 else
814 {
815 strftime(backup_path, sizeof(backup_path), "/sdcard/clockworkmod/backup/%F.%H.%M.%S", tmp);
816 }
817 nandroid_backup(backup_path);
818 }
819 break;
820 case 1:
821 show_nandroid_restore_menu();
822 break;
823 case 2:
824 show_nandroid_advanced_restore_menu();
825 break;
826 }
827}
828
829void wipe_battery_stats()
830{
831 ensure_path_mounted("/data");
832 remove("/data/system/batterystats.bin");
833 ensure_path_unmounted("/data");
834}
835
836void show_advanced_menu()
837{
838 static char* headers[] = { "Advanced and Debugging Menu",
839 "",
840 NULL
841 };
842
843 static char* list[] = { "Reboot Recovery",
844 "Wipe Dalvik Cache",
845 "Wipe Battery Stats",
846 "Report Error",
847 "Key Test",
848#ifndef BOARD_HAS_SMALL_RECOVERY
849 "Partition SD Card",
850 "Fix Permissions",
851#ifdef BOARD_HAS_SDCARD_INTERNAL
852 "Partition Internal SD Card",
853#endif
854#endif
855 NULL
856 };
857
858 for (;;)
859 {
860 int chosen_item = get_menu_selection(headers, list, 0, 0);
861 if (chosen_item == GO_BACK)
862 break;
863 switch (chosen_item)
864 {
865 case 0:
866 __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, "recovery");
867 break;
868 case 1:
869 {
870 if (0 != ensure_path_mounted("/data"))
871 break;
872 ensure_path_mounted("/sd-ext");
873 ensure_path_mounted("/cache");
874 if (confirm_selection( "Confirm wipe?", "Yes - Wipe Dalvik Cache")) {
875 __system("rm -r /data/dalvik-cache");
876 __system("rm -r /cache/dalvik-cache");
877 __system("rm -r /sd-ext/dalvik-cache");
878 }
879 ensure_path_unmounted("/data");
880 ui_print("Dalvik Cache wiped.\n");
881 break;
882 }
883 case 2:
884 {
885 if (confirm_selection( "Confirm wipe?", "Yes - Wipe Battery Stats"))
886 wipe_battery_stats();
887 break;
888 }
889 case 3:
890 handle_failure(1);
891 break;
892 case 4:
893 {
894 ui_print("Outputting key codes.\n");
895 ui_print("Go back to end debugging.\n");
896 int key;
897 int action;
898 do
899 {
900 key = ui_wait_key();
901 action = device_handle_key(key, 1);
902 ui_print("Key: %d\n", key);
903 }
904 while (action != GO_BACK);
905 break;
906 }
907 case 5:
908 {
909 static char* ext_sizes[] = { "128M",
910 "256M",
911 "512M",
912 "1024M",
913 "2048M",
914 "4096M",
915 NULL };
916
917 static char* swap_sizes[] = { "0M",
918 "32M",
919 "64M",
920 "128M",
921 "256M",
922 NULL };
923
924 static char* ext_headers[] = { "Ext Size", "", NULL };
925 static char* swap_headers[] = { "Swap Size", "", NULL };
926
927 int ext_size = get_menu_selection(ext_headers, ext_sizes, 0, 0);
928 if (ext_size == GO_BACK)
929 continue;
930
931 int swap_size = get_menu_selection(swap_headers, swap_sizes, 0, 0);
932 if (swap_size == GO_BACK)
933 continue;
934
935 char sddevice[256];
936 Volume *vol = volume_for_path("/sdcard");
937 strcpy(sddevice, vol->device);
938 // we only want the mmcblk, not the partition
939 sddevice[strlen("/dev/block/mmcblkX")] = NULL;
940 char cmd[PATH_MAX];
941 setenv("SDPATH", sddevice, 1);
942 sprintf(cmd, "sdparted -es %s -ss %s -efs ext3 -s", ext_sizes[ext_size], swap_sizes[swap_size]);
943 ui_print("Partitioning SD Card... please wait...\n");
944 if (0 == __system(cmd))
945 ui_print("Done!\n");
946 else
947 ui_print("An error occured while partitioning your SD Card. Please see /tmp/recovery.log for more details.\n");
948 break;
949 }
950 case 6:
951 {
952 ensure_path_mounted("/system");
953 ensure_path_mounted("/data");
954 ui_print("Fixing permissions...\n");
955 __system("fix_permissions");
956 ui_print("Done!\n");
957 break;
958 }
959 case 7:
960 {
961 static char* ext_sizes[] = { "128M",
962 "256M",
963 "512M",
964 "1024M",
965 "2048M",
966 "4096M",
967 NULL };
968
969 static char* swap_sizes[] = { "0M",
970 "32M",
971 "64M",
972 "128M",
973 "256M",
974 NULL };
975
976 static char* ext_headers[] = { "Data Size", "", NULL };
977 static char* swap_headers[] = { "Swap Size", "", NULL };
978
979 int ext_size = get_menu_selection(ext_headers, ext_sizes, 0, 0);
980 if (ext_size == GO_BACK)
981 continue;
982
983 int swap_size = 0;
984 if (swap_size == GO_BACK)
985 continue;
986
987 char sddevice[256];
988 Volume *vol = volume_for_path("/emmc");
989 strcpy(sddevice, vol->device);
990 // we only want the mmcblk, not the partition
991 sddevice[strlen("/dev/block/mmcblkX")] = NULL;
992 char cmd[PATH_MAX];
993 setenv("SDPATH", sddevice, 1);
994 sprintf(cmd, "sdparted -es %s -ss %s -efs ext3 -s", ext_sizes[ext_size], swap_sizes[swap_size]);
995 ui_print("Partitioning Internal SD Card... please wait...\n");
996 if (0 == __system(cmd))
997 ui_print("Done!\n");
998 else
999 ui_print("An error occured while partitioning your Internal SD Card. Please see /tmp/recovery.log for more details.\n");
1000 break;
1001 }
1002 }
1003 }
1004}
1005
1006void write_fstab_root(char *path, FILE *file)
1007{
1008 Volume *vol = volume_for_path(path);
1009 if (vol == NULL) {
1010 LOGW("Unable to get recovery.fstab info for %s during fstab generation!\n", path);
1011 return;
1012 }
1013
1014 char device[200];
1015 if (vol->device[0] != '/')
1016 get_partition_device(vol->device, device);
1017 else
1018 strcpy(device, vol->device);
1019
1020 fprintf(file, "%s ", device);
1021 fprintf(file, "%s ", path);
andrew.boren1acd15e2011-03-31 21:41:22 -07001022 // special case rfs cause auto will mount it as vfat on samsung.
1023 fprintf(file, "%s rw\n", vol->fs_type2 != NULL && strcmp(vol->fs_type, "rfs") != 0 ? "auto" : vol->fs_type);
preludedrew38058dc2011-01-29 23:30:44 -07001024}
1025
1026void create_fstab()
1027{
1028 struct stat info;
1029 __system("touch /etc/mtab");
1030 FILE *file = fopen("/etc/fstab", "w");
1031 if (file == NULL) {
1032 LOGW("Unable to create /etc/fstab!\n");
1033 return;
1034 }
preludedrewf1ec34a2011-02-17 22:42:33 -07001035 Volume *vol = volume_for_path("/boot");
1036 if (NULL != vol && strcmp(vol->fs_type, "mtd") != 0 && strcmp(vol->fs_type, "emmc") != 0 && strcmp(vol->fs_type, "bml") != 0)
1037 write_fstab_root("/boot", file);
preludedrew38058dc2011-01-29 23:30:44 -07001038 write_fstab_root("/cache", file);
1039 write_fstab_root("/data", file);
1040 if (has_datadata()) {
1041 write_fstab_root("/datadata", file);
1042 }
1043 write_fstab_root("/system", file);
1044 write_fstab_root("/sdcard", file);
1045 write_fstab_root("/sd-ext", file);
1046 fclose(file);
1047 LOGI("Completed outputting fstab.\n");
1048}
1049
1050int bml_check_volume(const char *path) {
1051 ui_print("Checking %s...\n", path);
1052 ensure_path_unmounted(path);
1053 if (0 == ensure_path_mounted(path)) {
1054 ensure_path_unmounted(path);
1055 return 0;
1056 }
1057
1058 Volume *vol = volume_for_path(path);
1059 if (vol == NULL) {
1060 LOGE("Unable process volume! Skipping...\n");
1061 return 0;
1062 }
1063
1064 ui_print("%s may be rfs. Checking...\n", path);
1065 char tmp[PATH_MAX];
1066 sprintf(tmp, "mount -t rfs %s %s", vol->device, path);
1067 int ret = __system(tmp);
1068 printf("%d\n", ret);
1069 return ret == 0 ? 1 : 0;
1070}
1071
1072void process_volumes() {
1073 create_fstab();
1074
andrew.boren1acd15e2011-03-31 21:41:22 -07001075 return;
1076
1077 // dead code.
preludedrew38058dc2011-01-29 23:30:44 -07001078 if (device_flash_type() != BML)
1079 return;
1080
1081 ui_print("Checking for ext4 partitions...\n");
1082 int ret = 0;
1083 ret = bml_check_volume("/system");
1084 ret |= bml_check_volume("/data");
1085 if (has_datadata())
1086 ret |= bml_check_volume("/datadata");
1087 ret |= bml_check_volume("/cache");
1088
1089 if (ret == 0) {
1090 ui_print("Done!\n");
1091 return;
1092 }
1093
1094 char backup_path[PATH_MAX];
1095 time_t t = time(NULL);
1096 char backup_name[PATH_MAX];
1097 struct timeval tp;
1098 gettimeofday(&tp, NULL);
1099 sprintf(backup_name, "before-ext4-convert-%d", tp.tv_sec);
1100 sprintf(backup_path, "/sdcard/clockworkmod/backup/%s", backup_name);
1101
1102 ui_set_show_text(1);
1103 ui_print("Filesystems need to be converted to ext4.\n");
1104 ui_print("A backup and restore will now take place.\n");
1105 ui_print("If anything goes wrong, your backup will be\n");
1106 ui_print("named %s. Try restoring it\n", backup_name);
1107 ui_print("in case of error.\n");
1108
1109 nandroid_backup(backup_path);
1110 nandroid_restore(backup_path, 1, 1, 1, 1, 1, 0);
1111 ui_set_show_text(0);
1112}
1113
1114void handle_failure(int ret)
1115{
1116 if (ret == 0)
1117 return;
1118 if (0 != ensure_path_mounted("/sdcard"))
1119 return;
1120 mkdir("/sdcard/clockworkmod", S_IRWXU);
1121 __system("cp /tmp/recovery.log /sdcard/clockworkmod/recovery.log");
1122 ui_print("/tmp/recovery.log was copied to /sdcard/clockworkmod/recovery.log. Please open ROM Manager to report the issue.\n");
1123}
1124
1125int is_path_mounted(const char* path) {
1126 Volume* v = volume_for_path(path);
1127 if (v == NULL) {
1128 return 0;
1129 }
1130 if (strcmp(v->fs_type, "ramdisk") == 0) {
1131 // the ramdisk is always mounted.
1132 return 1;
1133 }
1134
1135 int result;
1136 result = scan_mounted_volumes();
1137 if (result < 0) {
1138 LOGE("failed to scan mounted volumes\n");
1139 return 0;
1140 }
1141
1142 const MountedVolume* mv =
1143 find_mounted_volume_by_mount_point(v->mount_point);
1144 if (mv) {
1145 // volume is already mounted
1146 return 1;
1147 }
1148 return 0;
1149}
1150
1151int has_datadata() {
1152 Volume *vol = volume_for_path("/datadata");
1153 return vol != NULL;
1154}
1155
1156int volume_main(int argc, char **argv) {
1157 load_volume_table();
1158 return 0;
1159}
1160
1161void handle_chargemode() {
1162 const char* filename = "/proc/cmdline";
1163 struct stat file_info;
1164 if (0 != stat(filename, &file_info))
1165 return;
1166
1167 int file_len = file_info.st_size;
1168 char* file_data = (char*)malloc(file_len + 1);
1169 FILE *file = fopen(filename, "rb");
1170 if (file == NULL)
1171 return;
1172 fread(file_data, file_len, 1, file);
1173 // supposedly not necessary, but let's be safe.
1174 file_data[file_len] = '\0';
1175 fclose(file);
1176
1177 if (strstr(file_data, "androidboot.mode=offmode_charging") != NULL)
1178 reboot(RB_POWER_OFF);
andrew.boren1acd15e2011-03-31 21:41:22 -07001179 }