blob: f22e35e38c74172cd85a7ae5bbfa790dd43c8a10 [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. Duttaa85d7cc2010-03-12 17:00:58 -080033#include "../../external/yaffs2/yaffs2/utils/mkyaffs2image.h"
Koushik K. Dutta54305a82010-03-12 17:43:26 -080034#include "../../external/yaffs2/yaffs2/utils/unyaffs.h"
Koushik K. Duttaa85d7cc2010-03-12 17:00:58 -080035
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -080036#include "extendedcommands.h"
37#include "nandroid.h"
Koushik Duttadf1e4062010-12-18 17:42:31 -080038#include "mounts.h"
Koushik Dutta9765a032010-12-18 21:31:02 -080039#include "flashutils/flashutils.h"
Koushik Duttabec09952010-12-19 20:37:57 -080040#include "edify/expr.h"
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -080041
Koushik K. Dutta6060e5c2010-02-11 22:27:06 -080042int signature_check_enabled = 1;
43int script_assert_enabled = 1;
Koushik Duttadf1e4062010-12-18 17:42:31 -080044static const char *SDCARD_UPDATE_FILE = "/sdcard/update.zip";
Koushik K. Dutta6060e5c2010-02-11 22:27:06 -080045
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;
Koushik K. Duttae9234872010-02-12 00:43:24 -080056 ui_print("Script Asserts: %s\n", script_assert_enabled ? "Enabled" : "Disabled");
57}
58
Koushik K. Duttaea46fe22010-03-08 02:58:04 -080059int install_zip(const char* packagefilepath)
Koushik K. Duttae9234872010-02-12 00:43:24 -080060{
Koushik K. Duttabcdd0032010-02-21 21:10:25 -080061 ui_print("\n-- Installing: %s\n", packagefilepath);
Koushik Dutta9765a032010-12-18 21:31:02 -080062 if (device_flash_type() == MTD) {
63 set_sdcard_update_bootloader_message();
64 }
Koushik K. Duttabcdd0032010-02-21 21:10:25 -080065 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;
Steve Kondik4123b582010-11-14 03:18:40 -050071 }
Koushik K. Dutta001c5b52010-02-25 14:53:57 -080072 ui_set_background(BACKGROUND_ICON_NONE);
Koushik K. Dutta99fb6fe2010-03-03 00:42:58 -080073 ui_print("\nInstall from sdcard complete.\n");
Koushik K. Duttaea46fe22010-03-08 02:58:04 -080074 return 0;
Koushik K. Duttabcdd0032010-02-21 21:10:25 -080075}
76
Koushik Duttadf1e4062010-12-18 17:42:31 -080077char* INSTALL_MENU_ITEMS[] = { "apply /sdcard/update.zip",
Koushik K. Duttabcdd0032010-02-21 21:10:25 -080078 "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 "",
Steve Kondik4123b582010-11-14 03:18:40 -050091 NULL
Koushik K. Duttabcdd0032010-02-21 21:10:25 -080092 };
93 for (;;)
94 {
Koushik Duttadf1e4062010-12-18 17:42:31 -080095 int chosen_item = get_menu_selection(headers, INSTALL_MENU_ITEMS, 0, 0);
Koushik K. Duttabcdd0032010-02-21 21:10:25 -080096 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:
Koushik Duttad63eaef2010-07-14 21:01:21 -0700105 {
106 if (confirm_selection("Confirm install?", "Yes - Install /sdcard/update.zip"))
Koushik Duttadf1e4062010-12-18 17:42:31 -0800107 install_zip(SDCARD_UPDATE_FILE);
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800108 break;
Koushik Duttad63eaef2010-07-14 21:01:21 -0700109 }
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800110 case ITEM_CHOOSE_ZIP:
111 show_choose_zip_menu();
112 break;
113 default:
114 return;
115 }
Steve Kondik4123b582010-11-14 03:18:40 -0500116
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800117 }
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 }
Steve Kondik4123b582010-11-14 03:18:40 -0500151
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);
Steve Kondik4123b582010-11-14 03:18:40 -0500155
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;
Steve Kondik4123b582010-11-14 03:18:40 -0500163
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800164 // 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 }
Steve Kondik4123b582010-11-14 03:18:40 -0500185
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800186 if (pass == 0)
187 {
188 total++;
189 continue;
190 }
Steve Kondik4123b582010-11-14 03:18:40 -0500191
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
agrabren1f76a5d2010-12-29 12:15:18 -0600217 // 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 }
Koushik Dutta1e8aaba2010-07-01 00:23:25 -0700231
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 {
Koushik Duttadf1e4062010-12-18 17:42:31 -0800274 int chosen_item = get_menu_selection(headers, list, 0, 0);
Koushik K. Dutta1d53c4e2010-04-02 16:46:21 -0700275 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;
Steve Kondik4123b582010-11-14 03:18:40 -0500288 }
Koushik K. Dutta1d53c4e2010-04-02 16:46:21 -0700289 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 Duttadf1e4062010-12-18 17:42:31 -0800303 if (ensure_path_mounted("/sdcard") != 0) {
Koushik K. Duttae9234872010-02-12 00:43:24 -0800304 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 "",
Steve Kondik4123b582010-11-14 03:18:40 -0500310 NULL
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800311 };
Steve Kondik4123b582010-11-14 03:18:40 -0500312
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800313 char* file = choose_file_menu("/sdcard/", ".zip", headers);
314 if (file == NULL)
315 return;
Koushik Duttad63eaef2010-07-14 21:01:21 -0700316 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))
Koushik Duttadf1e4062010-12-18 17:42:31 -0800320 install_zip(file);
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800321}
322
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800323void show_nandroid_restore_menu()
324{
Koushik Duttadf1e4062010-12-18 17:42:31 -0800325 if (ensure_path_mounted("/sdcard") != 0) {
Koushik K. Dutta54305a82010-03-12 17:43:26 -0800326 LOGE ("Can't mount /sdcard\n");
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800327 return;
Koushik K. Dutta54305a82010-03-12 17:43:26 -0800328 }
Steve Kondik4123b582010-11-14 03:18:40 -0500329
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800330 static char* headers[] = { "Choose an image to restore",
331 "",
Steve Kondik4123b582010-11-14 03:18:40 -0500332 NULL
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800333 };
Koushik K. Duttae9234872010-02-12 00:43:24 -0800334
Koushik K. Dutta49f56892010-02-25 14:51:05 -0800335 char* file = choose_file_menu("/sdcard/clockworkmod/backup/", NULL, headers);
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800336 if (file == NULL)
337 return;
Koushik Duttad63eaef2010-07-14 21:01:21 -0700338
339 if (confirm_selection("Confirm restore?", "Yes - Restore"))
Koushik Dutta5b7f34a2010-12-29 23:36:03 -0800340 nandroid_restore(file, 1, 1, 1, 1, 1, 0);
Koushik K. Dutta03173782010-02-26 14:14:23 -0800341}
342
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700343void show_mount_usb_storage_menu()
Koushik K. Dutta03173782010-02-26 14:14:23 -0800344{
Koushik Dutta598cfc72010-06-20 09:42:47 -0700345 char command[PATH_MAX];
Koushik Duttadf1e4062010-12-18 17:42:31 -0800346 Volume *vol = volume_for_path("/sdcard");
347 sprintf(command, "echo %s > /sys/devices/platform/usb_mass_storage/lun0/file", vol->device);
Koushik Dutta598cfc72010-06-20 09:42:47 -0700348 __system(command);
Koushik K. Dutta03173782010-02-26 14:14:23 -0800349 static char* headers[] = { "USB Mass Storage device",
350 "Leaving this menu unmount",
351 "your SD card from your PC.",
352 "",
Steve Kondik4123b582010-11-14 03:18:40 -0500353 NULL
Koushik K. Dutta03173782010-02-26 14:14:23 -0800354 };
Steve Kondik4123b582010-11-14 03:18:40 -0500355
Koushik K. Dutta03173782010-02-26 14:14:23 -0800356 static char* list[] = { "Unmount", NULL };
Steve Kondik4123b582010-11-14 03:18:40 -0500357
Koushik K. Dutta03173782010-02-26 14:14:23 -0800358 for (;;)
359 {
Koushik Duttadf1e4062010-12-18 17:42:31 -0800360 int chosen_item = get_menu_selection(headers, list, 0, 0);
Koushik K. Dutta03173782010-02-26 14:14:23 -0800361 if (chosen_item == GO_BACK || chosen_item == 0)
362 break;
363 }
Steve Kondik4123b582010-11-14 03:18:40 -0500364
Koushik K. Duttaf7215942010-03-16 13:34:51 -0700365 __system("echo '' > /sys/devices/platform/usb_mass_storage/lun0/file");
366 __system("echo 0 > /sys/devices/platform/usb_mass_storage/lun0/enable");
Koushik K. Duttae81cb752010-02-26 17:44:33 -0800367}
Koushik K. Duttaf68aaaf2010-03-07 13:39:21 -0800368
Koushik Duttad63eaef2010-07-14 21:01:21 -0700369int confirm_selection(const char* title, const char* confirm)
Koushik K. Dutta16f0b492010-03-19 14:37:11 -0700370{
Koushik Duttaceddcd52010-08-23 16:13:14 -0700371 struct stat info;
372 if (0 == stat("/sdcard/clockworkmod/.no_confirm", &info))
373 return 1;
374
Koushik Duttad63eaef2010-07-14 21:01:21 -0700375 char* confirm_headers[] = { title, " THIS CAN NOT BE UNDONE.", "", NULL };
376 char* items[] = { "No",
377 "No",
378 "No",
379 "No",
380 "No",
381 "No",
382 "No",
Koushik Duttaecd32fa2010-07-14 18:38:02 -0700383 confirm, //" Yes -- wipe partition", // [7
Koushik Duttad63eaef2010-07-14 21:01:21 -0700384 "No",
385 "No",
386 "No",
Koushik K. Dutta16f0b492010-03-19 14:37:11 -0700387 NULL };
388
Koushik Duttadf1e4062010-12-18 17:42:31 -0800389 int chosen_item = get_menu_selection(confirm_headers, items, 0, 0);
Koushik K. Dutta16f0b492010-03-19 14:37:11 -0700390 return chosen_item == 7;
391}
392
Koushik Duttada32b542011-01-01 17:55:22 -0800393#define MKE2FS_BIN "/sbin/mke2fs"
394#define TUNE2FS_BIN "/sbin/tune2fs"
395#define E2FSCK_BIN "/sbin/e2fsck"
396
397static int
398format_ext3_device (const char *device) {
399 // Run mke2fs
400 char *const mke2fs[] = {MKE2FS_BIN, "-j", device, NULL};
401 if(run_exec_process(mke2fs))
402 return -1;
403
404 // Run tune2fs
405 char *const tune2fs[] = {TUNE2FS_BIN, "-j", "-C", "1", device, NULL};
406 if(run_exec_process(tune2fs))
407 return -1;
408
409 // Run e2fsck
410 char *const e2fsck[] = {E2FSCK_BIN, "-fy", device, NULL};
411 if(run_exec_process(e2fsck))
412 return -1;
413
414 return 0;
415}
416
417static int
418format_ext2_device (const char *device) {
419 // Run mke2fs
420 char *const mke2fs[] = {MKE2FS_BIN, device, NULL};
421 if(run_exec_process(mke2fs))
422 return -1;
423
424 // Run tune2fs
425 char *const tune2fs[] = {TUNE2FS_BIN, "-C", "1", device, NULL};
426 if(run_exec_process(tune2fs))
427 return -1;
428
429 // Run e2fsck
430 char *const e2fsck[] = {E2FSCK_BIN, "-fy", device, NULL};
431 if(run_exec_process(e2fsck))
432 return -1;
433
434 return 0;
435}
436
437
438int format_unknown_device(const char *device, const char* path, const char *fs_type)
Koushik K. Dutta3f995392010-03-30 23:29:43 -0700439{
Koushik Duttaa8708c62011-01-01 18:00:27 -0800440 printf("Formatting unknown device.\n");
441
Koushik Duttada32b542011-01-01 17:55:22 -0800442 // device may simply be a name, like "system"
443 if (device[0] != '/')
444 return erase_partition(device, fs_type);
445
Koushik Duttaa8708c62011-01-01 18:00:27 -0800446 if (strcmp("ext3", fs_type) == 0) {
447 printf("Formatting ext3 device.\n");
Koushik Duttada32b542011-01-01 17:55:22 -0800448 if (0 != ensure_path_unmounted(path))
449 return -11;
450 return format_ext3_device(device);
451 }
452
Koushik Duttaa8708c62011-01-01 18:00:27 -0800453 if (strcmp("ext2", fs_type) == 0) {
454 printf("Formatting ext2 device.\n");
Koushik Duttada32b542011-01-01 17:55:22 -0800455 if (0 != ensure_path_unmounted(path))
456 return -12;
457 return format_ext2_device(device);
458 }
459
Koushik K. Duttaf3534d02010-04-18 18:06:24 -0700460 // if this is SDEXT:, don't worry about it.
Koushik Duttadf1e4062010-12-18 17:42:31 -0800461 if (0 == strcmp(path, "/sd-ext"))
Koushik K. Duttaf3534d02010-04-18 18:06:24 -0700462 {
463 struct stat st;
Koushik Duttadf1e4062010-12-18 17:42:31 -0800464 Volume *vol = volume_for_path("/sd-ext");
465 if (vol == NULL || 0 != stat(vol->device, &st))
Koushik K. Duttaf3534d02010-04-18 18:06:24 -0700466 {
467 ui_print("No app2sd partition found. Skipping format of /sd-ext.\n");
468 return 0;
469 }
470 }
Koushik Dutta2f73e582010-04-18 16:00:21 -0700471
Koushik Duttadf1e4062010-12-18 17:42:31 -0800472 if (0 != ensure_path_mounted(path))
Koushik K. Dutta3f995392010-03-30 23:29:43 -0700473 {
474 ui_print("Error mounting %s!\n", path);
Koushik Duttacd44ab92010-06-23 00:02:14 -0700475 ui_print("Skipping format...\n");
476 return 0;
Koushik K. Dutta3f995392010-03-30 23:29:43 -0700477 }
478
479 static char tmp[PATH_MAX];
480 sprintf(tmp, "rm -rf %s/*", path);
481 __system(tmp);
482 sprintf(tmp, "rm -rf %s/.*", path);
483 __system(tmp);
Steve Kondik4123b582010-11-14 03:18:40 -0500484
Koushik Duttadf1e4062010-12-18 17:42:31 -0800485 ensure_path_unmounted(path);
Koushik K. Dutta3f995392010-03-30 23:29:43 -0700486 return 0;
487}
488
Koushik Duttadf1e4062010-12-18 17:42:31 -0800489//#define MOUNTABLE_COUNT 5
490//#define DEVICE_COUNT 4
491//#define MMC_COUNT 2
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700492
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700493void show_partition_menu()
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700494{
Koushik Duttaceddcd52010-08-23 16:13:14 -0700495 static char* headers[] = { "Mounts and Storage Menu",
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700496 "",
Steve Kondik4123b582010-11-14 03:18:40 -0500497 NULL
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700498 };
499
500 typedef char* string;
Koushik Duttadf1e4062010-12-18 17:42:31 -0800501 string mounts[][3] = {
502 { "mount /system", "unmount /system", "/system" },
503 { "mount /data", "unmount /data", "/data" },
504 { "mount /cache", "unmount /cache", "/cache" },
Koushik Dutta33491ac2010-12-19 03:41:41 -0800505 { "mount /sdcard", "unmount /sdcard", "/sdcard" },
Brint E. Kriebeld3522332010-12-11 20:52:06 -0700506#ifdef BOARD_HAS_SDCARD_INTERNAL
Koushik Duttadf1e4062010-12-18 17:42:31 -0800507 { "mount /emmc", "unmount /emmc", "/emmc" },
Brint E. Kriebeld3522332010-12-11 20:52:06 -0700508#endif
Koushik Duttadf1e4062010-12-18 17:42:31 -0800509 { "mount /sd-ext", "unmount /sd-ext", "/sd-ext" }
Koushik K. Dutta3f995392010-03-30 23:29:43 -0700510 };
Steve Kondik4123b582010-11-14 03:18:40 -0500511
Koushik Duttadf1e4062010-12-18 17:42:31 -0800512 string devices[][2] = {
513 { "format boot", "boot:" },
514 { "format system", "/system" },
515 { "format data", "/data" },
516 { "format cache", "/cache" },
517 { "format sdcard", "/sdcard" },
518 { "format sd-ext", "/sd-ext" },
Brint E. Kriebeld3522332010-12-11 20:52:06 -0700519#ifdef BOARD_HAS_SDCARD_INTERNAL
Koushik Duttadf1e4062010-12-18 17:42:31 -0800520 { "format internal sdcard", "/emmc" }
Brint E. Kriebeld3522332010-12-11 20:52:06 -0700521#endif
Koushik K. Dutta16f0b492010-03-19 14:37:11 -0700522 };
Steve Kondik4123b582010-11-14 03:18:40 -0500523
Koushik Duttadf1e4062010-12-18 17:42:31 -0800524 const int MOUNTABLE_COUNT = sizeof(mounts) / sizeof(string) / 3;
525 const int DEVICE_COUNT = sizeof(devices) / sizeof(string) / 2;
526
Koushik Duttad63eaef2010-07-14 21:01:21 -0700527 static char* confirm_format = "Confirm format?";
Koushik Duttaecd32fa2010-07-14 18:38:02 -0700528 static char* confirm = "Yes - Format";
Steve Kondik4123b582010-11-14 03:18:40 -0500529
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700530 for (;;)
531 {
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700532 int ismounted[MOUNTABLE_COUNT];
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700533 int i;
Koushik Duttadf1e4062010-12-18 17:42:31 -0800534 static string options[MOUNTABLE_COUNT + DEVICE_COUNT + 1 + 1]; // mountables, format mtds, format mmcs, usb storage, null
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700535 for (i = 0; i < MOUNTABLE_COUNT; i++)
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700536 {
Koushik Duttadf1e4062010-12-18 17:42:31 -0800537 ismounted[i] = is_path_mounted(mounts[i][2]);
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700538 options[i] = ismounted[i] ? mounts[i][1] : mounts[i][0];
539 }
Steve Kondik4123b582010-11-14 03:18:40 -0500540
Koushik Duttadf1e4062010-12-18 17:42:31 -0800541 for (i = 0; i < DEVICE_COUNT; i++)
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700542 {
Koushik Duttabec09952010-12-19 20:37:57 -0800543 options[MOUNTABLE_COUNT + i] = devices[i][0];
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700544 }
Steve Kondik4123b582010-11-14 03:18:40 -0500545
Koushik Duttadf1e4062010-12-18 17:42:31 -0800546 options[MOUNTABLE_COUNT + DEVICE_COUNT] = "mount USB storage";
547 options[MOUNTABLE_COUNT + DEVICE_COUNT + 1] = NULL;
Steve Kondik4123b582010-11-14 03:18:40 -0500548
Koushik Duttadf1e4062010-12-18 17:42:31 -0800549 int chosen_item = get_menu_selection(headers, options, 0, 0);
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700550 if (chosen_item == GO_BACK)
551 break;
Koushik Duttadf1e4062010-12-18 17:42:31 -0800552 if (chosen_item == MOUNTABLE_COUNT + DEVICE_COUNT)
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700553 {
554 show_mount_usb_storage_menu();
555 }
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700556 else if (chosen_item < MOUNTABLE_COUNT)
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700557 {
558 if (ismounted[chosen_item])
559 {
Koushik Duttadf1e4062010-12-18 17:42:31 -0800560 if (0 != ensure_path_unmounted(mounts[chosen_item][2]))
Koushik K. Dutta16f0b492010-03-19 14:37:11 -0700561 ui_print("Error unmounting %s!\n", mounts[chosen_item][2]);
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700562 }
563 else
564 {
Koushik Duttadf1e4062010-12-18 17:42:31 -0800565 if (0 != ensure_path_mounted(mounts[chosen_item][2]))
Koushik K. Dutta16f0b492010-03-19 14:37:11 -0700566 ui_print("Error mounting %s!\n", mounts[chosen_item][2]);
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700567 }
568 }
Koushik Duttadf1e4062010-12-18 17:42:31 -0800569 else if (chosen_item < MOUNTABLE_COUNT + DEVICE_COUNT)
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700570 {
571 chosen_item = chosen_item - MOUNTABLE_COUNT;
Koushik Duttaecd32fa2010-07-14 18:38:02 -0700572 if (!confirm_selection(confirm_format, confirm))
Koushik K. Dutta16f0b492010-03-19 14:37:11 -0700573 continue;
Koushik Duttadf1e4062010-12-18 17:42:31 -0800574 ui_print("Formatting %s...\n", devices[chosen_item][1]);
575 if (0 != format_device(devices[chosen_item][1]))
576 ui_print("Error formatting %s!\n", devices[chosen_item][1]);
Koushik K. Dutta3f995392010-03-30 23:29:43 -0700577 else
578 ui_print("Done.\n");
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700579 }
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700580 }
581}
582
Koushik K. Dutta72a1db62010-03-07 14:10:26 -0800583#define EXTENDEDCOMMAND_SCRIPT "/cache/recovery/extendedcommand"
584
585int extendedcommand_file_exists()
Koushik K. Duttaf68aaaf2010-03-07 13:39:21 -0800586{
Koushik K. Duttaf68aaaf2010-03-07 13:39:21 -0800587 struct stat file_info;
Koushik K. Dutta72a1db62010-03-07 14:10:26 -0800588 return 0 == stat(EXTENDEDCOMMAND_SCRIPT, &file_info);
589}
590
Koushik Duttabec09952010-12-19 20:37:57 -0800591int edify_main(int argc, char** argv) {
592 load_volume_table();
593 process_volumes();
594 RegisterBuiltins();
595 RegisterRecoveryHooks();
596 FinishRegistration();
Koushik Duttadf1e4062010-12-18 17:42:31 -0800597
Koushik Duttabec09952010-12-19 20:37:57 -0800598 if (argc != 2) {
599 printf("edify <filename>\n");
Koushik K. Duttaf68aaaf2010-03-07 13:39:21 -0800600 return 1;
Koushik K. Duttaf68aaaf2010-03-07 13:39:21 -0800601 }
602
Koushik Duttabec09952010-12-19 20:37:57 -0800603 FILE* f = fopen(argv[1], "r");
604 if (f == NULL) {
605 printf("%s: %s: No such file or directory\n", argv[0], argv[1]);
606 return 1;
607 }
608 char buffer[8192];
609 int size = fread(buffer, 1, 8191, f);
610 fclose(f);
611 buffer[size] = '\0';
612
613 Expr* root;
614 int error_count = 0;
615 yy_scan_bytes(buffer, size);
616 int error = yyparse(&root, &error_count);
617 printf("parse returned %d; %d errors encountered\n", error, error_count);
618 if (error == 0 || error_count > 0) {
619
620 //ExprDump(0, root, buffer);
621
622 State state;
623 state.cookie = NULL;
624 state.script = buffer;
625 state.errmsg = NULL;
626
627 char* result = Evaluate(&state, root);
628 if (result == NULL) {
629 printf("result was NULL, message is: %s\n",
630 (state.errmsg == NULL ? "(NULL)" : state.errmsg));
631 free(state.errmsg);
632 } else {
633 printf("result is [%s]\n", result);
Koushik K. Duttaf68aaaf2010-03-07 13:39:21 -0800634 }
Steve Kondik4123b582010-11-14 03:18:40 -0500635 }
Koushik Duttabec09952010-12-19 20:37:57 -0800636 return 0;
Koushik K. Duttaf68aaaf2010-03-07 13:39:21 -0800637}
Koushik K. Dutta72a1db62010-03-07 14:10:26 -0800638
Koushik K. Dutta6771aca2010-04-03 23:28:39 -0700639int run_script(char* filename)
Koushik K. Dutta3836f722010-03-11 22:17:43 -0800640{
641 struct stat file_info;
642 if (0 != stat(filename, &file_info)) {
643 printf("Error executing stat on file: %s\n", filename);
644 return 1;
645 }
Steve Kondik4123b582010-11-14 03:18:40 -0500646
Koushik K. Dutta3836f722010-03-11 22:17:43 -0800647 int script_len = file_info.st_size;
Koushik K. Dutta707fa6d2010-03-23 11:44:33 -0700648 char* script_data = (char*)malloc(script_len + 1);
Koushik K. Dutta3836f722010-03-11 22:17:43 -0800649 FILE *file = fopen(filename, "rb");
650 fread(script_data, script_len, 1, file);
Koushik K. Dutta707fa6d2010-03-23 11:44:33 -0700651 // supposedly not necessary, but let's be safe.
652 script_data[script_len] = '\0';
Koushik K. Dutta3836f722010-03-11 22:17:43 -0800653 fclose(file);
Koushik Duttae25908b2010-06-21 08:16:19 -0700654 LOGI("Running script:\n");
655 LOGI("\n%s\n", script_data);
Koushik K. Dutta3836f722010-03-11 22:17:43 -0800656
Koushik K. Duttaf3534d02010-04-18 18:06:24 -0700657 int ret = run_script_from_buffer(script_data, script_len, filename);
Koushik K. Dutta1d53c4e2010-04-02 16:46:21 -0700658 free(script_data);
659 return ret;
Koushik K. Dutta3836f722010-03-11 22:17:43 -0800660}
661
Koushik K. Dutta72a1db62010-03-07 14:10:26 -0800662int run_and_remove_extendedcommand()
663{
Koushik K. Dutta6771aca2010-04-03 23:28:39 -0700664 char tmp[PATH_MAX];
665 sprintf(tmp, "cp %s /tmp/%s", EXTENDEDCOMMAND_SCRIPT, basename(EXTENDEDCOMMAND_SCRIPT));
666 __system(tmp);
667 remove(EXTENDEDCOMMAND_SCRIPT);
Koushik K. Dutta3a25cf52010-03-08 19:22:41 -0800668 int i = 0;
669 for (i = 20; i > 0; i--) {
670 ui_print("Waiting for SD Card to mount (%ds)\n", i);
Koushik Duttadf1e4062010-12-18 17:42:31 -0800671 if (ensure_path_mounted("/sdcard") == 0) {
Koushik K. Dutta3a25cf52010-03-08 19:22:41 -0800672 ui_print("SD Card mounted...\n");
673 break;
674 }
675 sleep(1);
676 }
Koushik Dutta92077c12010-07-15 00:10:08 -0700677 remove("/sdcard/clockworkmod/.recoverycheckpoint");
Koushik K. Dutta3a25cf52010-03-08 19:22:41 -0800678 if (i == 0) {
679 ui_print("Timed out waiting for SD card... continuing anyways.");
680 }
Steve Kondik4123b582010-11-14 03:18:40 -0500681
Koushik K. Dutta6771aca2010-04-03 23:28:39 -0700682 sprintf(tmp, "/tmp/%s", basename(EXTENDEDCOMMAND_SCRIPT));
683 return run_script(tmp);
Koushik K. Dutta72a1db62010-03-07 14:10:26 -0800684}
685
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700686void show_nandroid_advanced_restore_menu()
687{
Koushik Duttadf1e4062010-12-18 17:42:31 -0800688 if (ensure_path_mounted("/sdcard") != 0) {
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700689 LOGE ("Can't mount /sdcard\n");
690 return;
691 }
692
693 static char* advancedheaders[] = { "Choose an image to restore",
694 "",
695 "Choose an image to restore",
696 "first. The next menu will",
697 "you more options.",
698 "",
699 NULL
700 };
701
702 char* file = choose_file_menu("/sdcard/clockworkmod/backup/", NULL, advancedheaders);
703 if (file == NULL)
704 return;
705
706 static char* headers[] = { "Nandroid Advanced Restore",
707 "",
708 NULL
709 };
710
711 static char* list[] = { "Restore boot",
712 "Restore system",
713 "Restore data",
714 "Restore cache",
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700715 "Restore sd-ext",
agrabren1f76a5d2010-12-29 12:15:18 -0600716 "Restore wimax",
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700717 NULL
718 };
Koushik Dutta5b7f34a2010-12-29 23:36:03 -0800719
720 char tmp[PATH_MAX];
721 if (0 != get_partition_device("wimax", tmp)) {
722 // disable wimax restore option
723 list[5] = NULL;
724 }
Koushik Duttad63eaef2010-07-14 21:01:21 -0700725
726 static char* confirm_restore = "Confirm restore?";
727
Koushik Duttadf1e4062010-12-18 17:42:31 -0800728 int chosen_item = get_menu_selection(headers, list, 0, 0);
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700729 switch (chosen_item)
730 {
731 case 0:
Koushik Duttad63eaef2010-07-14 21:01:21 -0700732 if (confirm_selection(confirm_restore, "Yes - Restore boot"))
agrabren1f76a5d2010-12-29 12:15:18 -0600733 nandroid_restore(file, 1, 0, 0, 0, 0, 0);
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700734 break;
735 case 1:
Koushik Duttad63eaef2010-07-14 21:01:21 -0700736 if (confirm_selection(confirm_restore, "Yes - Restore system"))
agrabren1f76a5d2010-12-29 12:15:18 -0600737 nandroid_restore(file, 0, 1, 0, 0, 0, 0);
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700738 break;
739 case 2:
Koushik Duttad63eaef2010-07-14 21:01:21 -0700740 if (confirm_selection(confirm_restore, "Yes - Restore data"))
agrabren1f76a5d2010-12-29 12:15:18 -0600741 nandroid_restore(file, 0, 0, 1, 0, 0, 0);
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700742 break;
743 case 3:
Koushik Duttad63eaef2010-07-14 21:01:21 -0700744 if (confirm_selection(confirm_restore, "Yes - Restore cache"))
agrabren1f76a5d2010-12-29 12:15:18 -0600745 nandroid_restore(file, 0, 0, 0, 1, 0, 0);
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700746 break;
Koushik Dutta2f73e582010-04-18 16:00:21 -0700747 case 4:
Koushik Duttad63eaef2010-07-14 21:01:21 -0700748 if (confirm_selection(confirm_restore, "Yes - Restore sd-ext"))
agrabren1f76a5d2010-12-29 12:15:18 -0600749 nandroid_restore(file, 0, 0, 0, 0, 1, 0);
750 break;
751 case 5:
752 if (confirm_selection(confirm_restore, "Yes - Restore wimax"))
753 nandroid_restore(file, 0, 0, 0, 0, 0, 1);
Koushik Dutta2f73e582010-04-18 16:00:21 -0700754 break;
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700755 }
756}
757
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700758void show_nandroid_menu()
759{
760 static char* headers[] = { "Nandroid",
761 "",
Steve Kondik4123b582010-11-14 03:18:40 -0500762 NULL
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700763 };
764
Steve Kondik4123b582010-11-14 03:18:40 -0500765 static char* list[] = { "Backup",
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700766 "Restore",
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700767 "Advanced Restore",
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700768 NULL
769 };
770
Koushik Duttadf1e4062010-12-18 17:42:31 -0800771 int chosen_item = get_menu_selection(headers, list, 0, 0);
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700772 switch (chosen_item)
773 {
774 case 0:
775 {
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700776 char backup_path[PATH_MAX];
Koushik K. Dutta6a26e7c2010-03-27 15:26:11 -0700777 time_t t = time(NULL);
778 struct tm *tmp = localtime(&t);
779 if (tmp == NULL)
780 {
781 struct timeval tp;
782 gettimeofday(&tp, NULL);
783 sprintf(backup_path, "/sdcard/clockworkmod/backup/%d", tp.tv_sec);
784 }
785 else
786 {
787 strftime(backup_path, sizeof(backup_path), "/sdcard/clockworkmod/backup/%F.%H.%M.%S", tmp);
788 }
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700789 nandroid_backup(backup_path);
790 }
791 break;
792 case 1:
793 show_nandroid_restore_menu();
794 break;
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700795 case 2:
796 show_nandroid_advanced_restore_menu();
797 break;
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700798 }
Koushik K. Duttaa496b512010-03-19 14:51:45 -0700799}
800
Koushik Duttafd1579b2010-05-01 12:46:55 -0700801void wipe_battery_stats()
802{
Koushik Duttadf1e4062010-12-18 17:42:31 -0800803 ensure_path_mounted("/data");
Koushik Duttafd1579b2010-05-01 12:46:55 -0700804 remove("/data/system/batterystats.bin");
Koushik Duttadf1e4062010-12-18 17:42:31 -0800805 ensure_path_unmounted("/data");
Koushik Duttafd1579b2010-05-01 12:46:55 -0700806}
807
Koushik K. Duttaa496b512010-03-19 14:51:45 -0700808void show_advanced_menu()
809{
810 static char* headers[] = { "Advanced and Debugging Menu",
811 "",
812 NULL
813 };
814
Koushik K. Dutta7fe4d7b2010-04-06 23:04:52 -0700815 static char* list[] = { "Reboot Recovery",
Koushik Dutta49af23c2010-06-21 13:45:51 -0700816 "Wipe Dalvik Cache",
Koushik Duttafd1579b2010-05-01 12:46:55 -0700817 "Wipe Battery Stats",
Koushik Dutta598cfc72010-06-20 09:42:47 -0700818 "Report Error",
Koushik K. Dutta7fe4d7b2010-04-06 23:04:52 -0700819 "Key Test",
Koushik Duttae17a78d2010-08-29 12:35:10 -0700820#ifndef BOARD_HAS_SMALL_RECOVERY
Koushik Duttaceddcd52010-08-23 16:13:14 -0700821 "Partition SD Card",
822 "Fix Permissions",
Brint E. Kriebeld3522332010-12-11 20:52:06 -0700823#ifdef BOARD_HAS_SDCARD_INTERNAL
824 "Partition Internal SD Card",
825#endif
Koushik Duttae17a78d2010-08-29 12:35:10 -0700826#endif
Koushik K. Duttaa496b512010-03-19 14:51:45 -0700827 NULL
828 };
829
830 for (;;)
831 {
Koushik Duttadf1e4062010-12-18 17:42:31 -0800832 int chosen_item = get_menu_selection(headers, list, 0, 0);
Koushik K. Duttaa496b512010-03-19 14:51:45 -0700833 if (chosen_item == GO_BACK)
834 break;
835 switch (chosen_item)
836 {
837 case 0:
Koushik K. Dutta7fe4d7b2010-04-06 23:04:52 -0700838 __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, "recovery");
839 break;
840 case 1:
Koushik Duttad63eaef2010-07-14 21:01:21 -0700841 {
Koushik Duttadf1e4062010-12-18 17:42:31 -0800842 if (0 != ensure_path_mounted("/data"))
Koushik Dutta49af23c2010-06-21 13:45:51 -0700843 break;
Koushik Duttadf1e4062010-12-18 17:42:31 -0800844 ensure_path_mounted("/sd-ext");
845 ensure_path_mounted("/cache");
Koushik Dutta6c7745d2010-08-07 12:17:13 -0700846 if (confirm_selection( "Confirm wipe?", "Yes - Wipe Dalvik Cache")) {
Koushik Duttad63eaef2010-07-14 21:01:21 -0700847 __system("rm -r /data/dalvik-cache");
Koushik Dutta6c7745d2010-08-07 12:17:13 -0700848 __system("rm -r /cache/dalvik-cache");
849 __system("rm -r /sd-ext/dalvik-cache");
850 }
Koushik Duttadf1e4062010-12-18 17:42:31 -0800851 ensure_path_unmounted("/data");
Koushik Dutta6c7745d2010-08-07 12:17:13 -0700852 ui_print("Dalvik Cache wiped.\n");
Koushik Duttafd1579b2010-05-01 12:46:55 -0700853 break;
Koushik Duttad63eaef2010-07-14 21:01:21 -0700854 }
Koushik Duttafd1579b2010-05-01 12:46:55 -0700855 case 2:
Koushik Duttad63eaef2010-07-14 21:01:21 -0700856 {
857 if (confirm_selection( "Confirm wipe?", "Yes - Wipe Battery Stats"))
858 wipe_battery_stats();
Koushik Dutta598cfc72010-06-20 09:42:47 -0700859 break;
Koushik Duttad63eaef2010-07-14 21:01:21 -0700860 }
Koushik Dutta598cfc72010-06-20 09:42:47 -0700861 case 3:
Koushik Dutta49af23c2010-06-21 13:45:51 -0700862 handle_failure(1);
863 break;
864 case 4:
Koushik K. Duttaa496b512010-03-19 14:51:45 -0700865 {
866 ui_print("Outputting key codes.\n");
867 ui_print("Go back to end debugging.\n");
868 int key;
869 int action;
870 do
871 {
872 key = ui_wait_key();
873 action = device_handle_key(key, 1);
874 ui_print("Key: %d\n", key);
875 }
876 while (action != GO_BACK);
Koushik Dutta56606a22010-08-23 16:15:33 -0700877 break;
Koushik K. Duttaa496b512010-03-19 14:51:45 -0700878 }
Koushik Duttaf0e31b82010-08-17 16:55:38 -0700879 case 5:
880 {
Koushik Duttaceddcd52010-08-23 16:13:14 -0700881 static char* ext_sizes[] = { "128M",
882 "256M",
883 "512M",
884 "1024M",
Brint E. Kriebel0b233312010-11-14 15:31:17 -0700885 "2048M",
886 "4096M",
Koushik Duttaceddcd52010-08-23 16:13:14 -0700887 NULL };
888
889 static char* swap_sizes[] = { "0M",
890 "32M",
891 "64M",
892 "128M",
893 "256M",
894 NULL };
895
896 static char* ext_headers[] = { "Ext Size", "", NULL };
897 static char* swap_headers[] = { "Swap Size", "", NULL };
898
Koushik Duttadf1e4062010-12-18 17:42:31 -0800899 int ext_size = get_menu_selection(ext_headers, ext_sizes, 0, 0);
Koushik Duttaceddcd52010-08-23 16:13:14 -0700900 if (ext_size == GO_BACK)
901 continue;
Steve Kondik4123b582010-11-14 03:18:40 -0500902
Koushik Duttadf1e4062010-12-18 17:42:31 -0800903 int swap_size = get_menu_selection(swap_headers, swap_sizes, 0, 0);
Koushik Duttaceddcd52010-08-23 16:13:14 -0700904 if (swap_size == GO_BACK)
905 continue;
906
907 char sddevice[256];
Koushik Duttadf1e4062010-12-18 17:42:31 -0800908 Volume *vol = volume_for_path("/sdcard");
909 strcpy(sddevice, vol->device);
Koushik Duttaceddcd52010-08-23 16:13:14 -0700910 // we only want the mmcblk, not the partition
911 sddevice[strlen("/dev/block/mmcblkX")] = NULL;
912 char cmd[PATH_MAX];
913 setenv("SDPATH", sddevice, 1);
914 sprintf(cmd, "sdparted -es %s -ss %s -efs ext3 -s", ext_sizes[ext_size], swap_sizes[swap_size]);
915 ui_print("Partitioning SD Card... please wait...\n");
916 if (0 == __system(cmd))
917 ui_print("Done!\n");
918 else
919 ui_print("An error occured while partitioning your SD Card. Please see /tmp/recovery.log for more details.\n");
Koushik Dutta56606a22010-08-23 16:15:33 -0700920 break;
Koushik Duttaf0e31b82010-08-17 16:55:38 -0700921 }
Koushik Dutta38e8b2b2010-08-17 22:21:53 -0700922 case 6:
923 {
Koushik Duttadf1e4062010-12-18 17:42:31 -0800924 ensure_path_mounted("/system");
925 ensure_path_mounted("/data");
Koushik Duttaceddcd52010-08-23 16:13:14 -0700926 ui_print("Fixing permissions...\n");
927 __system("fix_permissions");
928 ui_print("Done!\n");
Koushik Dutta56606a22010-08-23 16:15:33 -0700929 break;
Koushik Dutta38e8b2b2010-08-17 22:21:53 -0700930 }
Brint E. Kriebeld3522332010-12-11 20:52:06 -0700931 case 7:
932 {
933 static char* ext_sizes[] = { "128M",
934 "256M",
935 "512M",
936 "1024M",
937 "2048M",
938 "4096M",
939 NULL };
940
941 static char* swap_sizes[] = { "0M",
942 "32M",
943 "64M",
944 "128M",
945 "256M",
946 NULL };
947
948 static char* ext_headers[] = { "Data Size", "", NULL };
949 static char* swap_headers[] = { "Swap Size", "", NULL };
950
Koushik Duttadf1e4062010-12-18 17:42:31 -0800951 int ext_size = get_menu_selection(ext_headers, ext_sizes, 0, 0);
Brint E. Kriebeld3522332010-12-11 20:52:06 -0700952 if (ext_size == GO_BACK)
953 continue;
954
955 int swap_size = 0;
956 if (swap_size == GO_BACK)
957 continue;
958
959 char sddevice[256];
Koushik Duttadf1e4062010-12-18 17:42:31 -0800960 Volume *vol = volume_for_path("/emmc");
961 strcpy(sddevice, vol->device);
Brint E. Kriebeld3522332010-12-11 20:52:06 -0700962 // we only want the mmcblk, not the partition
963 sddevice[strlen("/dev/block/mmcblkX")] = NULL;
964 char cmd[PATH_MAX];
965 setenv("SDPATH", sddevice, 1);
966 sprintf(cmd, "sdparted -es %s -ss %s -efs ext3 -s", ext_sizes[ext_size], swap_sizes[swap_size]);
967 ui_print("Partitioning Internal SD Card... please wait...\n");
968 if (0 == __system(cmd))
969 ui_print("Done!\n");
970 else
971 ui_print("An error occured while partitioning your Internal SD Card. Please see /tmp/recovery.log for more details.\n");
972 break;
973 }
Koushik K. Duttaa496b512010-03-19 14:51:45 -0700974 }
975 }
Koushik Dutta14239d22010-06-14 15:02:48 -0700976}
977
Koushik Duttab643e4f2010-12-18 18:39:39 -0800978void write_fstab_root(char *path, FILE *file)
Koushik Dutta14239d22010-06-14 15:02:48 -0700979{
Koushik Duttab643e4f2010-12-18 18:39:39 -0800980 Volume *vol = volume_for_path(path);
981 if (vol == NULL) {
Koushik Dutta9765a032010-12-18 21:31:02 -0800982 LOGW("Unable to get recovery.fstab info for %s during fstab generation!\n", path);
Koushik Dutta598cfc72010-06-20 09:42:47 -0700983 return;
Koushik Duttaa6522b32010-06-20 13:16:06 -0700984 }
Steve Kondik4123b582010-11-14 03:18:40 -0500985
Koushik Dutta31b74112010-12-20 08:49:20 -0800986 char device[200];
987 if (vol->device[0] != '/')
988 get_partition_device(vol->device, device);
989 else
990 strcpy(device, vol->device);
991
992 fprintf(file, "%s ", device);
Koushik Duttab643e4f2010-12-18 18:39:39 -0800993 fprintf(file, "%s ", path);
994 fprintf(file, "%s rw\n", vol->fs_type);
Koushik Dutta14239d22010-06-14 15:02:48 -0700995}
996
997void create_fstab()
998{
Koushik Duttab643e4f2010-12-18 18:39:39 -0800999 struct stat info;
Koushik Duttacd44ab92010-06-23 00:02:14 -07001000 __system("touch /etc/mtab");
Koushik Dutta14239d22010-06-14 15:02:48 -07001001 FILE *file = fopen("/etc/fstab", "w");
Koushik Duttaa6522b32010-06-20 13:16:06 -07001002 if (file == NULL) {
Koushik Duttab643e4f2010-12-18 18:39:39 -08001003 LOGW("Unable to create /etc/fstab!\n");
Koushik Dutta598cfc72010-06-20 09:42:47 -07001004 return;
Koushik Duttaa6522b32010-06-20 13:16:06 -07001005 }
Koushik Duttab643e4f2010-12-18 18:39:39 -08001006 write_fstab_root("/cache", file);
1007 write_fstab_root("/data", file);
Koushik Dutta5460f0c2010-12-18 22:37:49 -08001008 if (has_datadata()) {
1009 write_fstab_root("/datadata", file);
1010 }
Koushik Duttab643e4f2010-12-18 18:39:39 -08001011 write_fstab_root("/system", file);
1012 write_fstab_root("/sdcard", file);
1013 write_fstab_root("/sd-ext", file);
Koushik Dutta14239d22010-06-14 15:02:48 -07001014 fclose(file);
Koushik Dutta9765a032010-12-18 21:31:02 -08001015 LOGI("Completed outputting fstab.\n");
Koushik Dutta598cfc72010-06-20 09:42:47 -07001016}
1017
Koushik Dutta4196e0f2010-12-19 03:38:29 -08001018int bml_check_volume(const char *path) {
1019 ui_print("Checking %s...\n", path);
1020 ensure_path_unmounted(path);
1021 if (0 == ensure_path_mounted(path)) {
1022 ensure_path_unmounted(path);
1023 return 0;
1024 }
1025
1026 Volume *vol = volume_for_path(path);
1027 if (vol == NULL) {
1028 LOGE("Unable process volume! Skipping...\n");
1029 return 0;
1030 }
1031
Koushik Duttabec09952010-12-19 20:37:57 -08001032 ui_print("%s may be rfs. Checking...\n", path);
Koushik Dutta4196e0f2010-12-19 03:38:29 -08001033 char tmp[PATH_MAX];
1034 sprintf(tmp, "mount -t rfs %s %s", vol->device, path);
1035 int ret = __system(tmp);
1036 printf("%d\n", ret);
1037 return ret == 0 ? 1 : 0;
1038}
1039
1040void process_volumes() {
1041 create_fstab();
1042
1043 if (device_flash_type() != BML)
1044 return;
1045
1046 ui_print("Checking for ext4 partitions...\n");
1047 int ret = 0;
1048 ret = bml_check_volume("/system");
1049 ret |= bml_check_volume("/data");
1050 if (has_datadata())
1051 ret |= bml_check_volume("/datadata");
1052 ret |= bml_check_volume("/cache");
1053
1054 if (ret == 0) {
1055 ui_print("Done!\n");
1056 return;
1057 }
1058
Koushik Dutta745b5ff2010-12-21 00:58:43 -08001059 char backup_path[PATH_MAX];
1060 time_t t = time(NULL);
1061 char backup_name[PATH_MAX];
1062 struct timeval tp;
1063 gettimeofday(&tp, NULL);
1064 sprintf(backup_name, "before-ext4-convert-%d", tp.tv_sec);
1065 sprintf(backup_path, "/sdcard/clockworkmod/backup/%s", backup_name);
1066
Koushik Dutta4196e0f2010-12-19 03:38:29 -08001067 ui_set_show_text(1);
1068 ui_print("Filesystems need to be converted to ext4.\n");
1069 ui_print("A backup and restore will now take place.\n");
Koushik Duttabec09952010-12-19 20:37:57 -08001070 ui_print("If anything goes wrong, your backup will be\n");
Koushik Dutta745b5ff2010-12-21 00:58:43 -08001071 ui_print("named %s. Try restoring it\n", backup_name);
Koushik Duttabec09952010-12-19 20:37:57 -08001072 ui_print("in case of error.\n");
Koushik Dutta4196e0f2010-12-19 03:38:29 -08001073
Koushik Dutta745b5ff2010-12-21 00:58:43 -08001074 nandroid_backup(backup_path);
Koushik Dutta5b7f34a2010-12-29 23:36:03 -08001075 nandroid_restore(backup_path, 1, 1, 1, 1, 1, 0);
Koushik Dutta4196e0f2010-12-19 03:38:29 -08001076 ui_set_show_text(0);
1077}
1078
Koushik Dutta598cfc72010-06-20 09:42:47 -07001079void handle_failure(int ret)
1080{
1081 if (ret == 0)
1082 return;
Koushik Duttadf1e4062010-12-18 17:42:31 -08001083 if (0 != ensure_path_mounted("/sdcard"))
Koushik Dutta598cfc72010-06-20 09:42:47 -07001084 return;
Koushik Duttaa6522b32010-06-20 13:16:06 -07001085 mkdir("/sdcard/clockworkmod", S_IRWXU);
1086 __system("cp /tmp/recovery.log /sdcard/clockworkmod/recovery.log");
Koushik Dutta38e8b2b2010-08-17 22:21:53 -07001087 ui_print("/tmp/recovery.log was copied to /sdcard/clockworkmod/recovery.log. Please open ROM Manager to report the issue.\n");
Steve Kondik4123b582010-11-14 03:18:40 -05001088}
Koushik Duttadf1e4062010-12-18 17:42:31 -08001089
1090int format_device(const char* device) {
1091 if (device == NULL)
1092 return -1;
1093 if (device[0] == '/') {
1094 Volume *vol = volume_for_path(device);
1095 if (vol == NULL)
1096 return -1;
1097 return erase_partition(device, vol->fs_type);
1098 }
1099 return erase_raw_partition(device);
1100}
1101
1102int is_path_mounted(const char* path) {
1103 Volume* v = volume_for_path(path);
1104 if (v == NULL) {
1105 return 0;
1106 }
1107 if (strcmp(v->fs_type, "ramdisk") == 0) {
1108 // the ramdisk is always mounted.
1109 return 1;
1110 }
1111
1112 int result;
1113 result = scan_mounted_volumes();
1114 if (result < 0) {
1115 LOGE("failed to scan mounted volumes\n");
1116 return 0;
1117 }
1118
1119 const MountedVolume* mv =
1120 find_mounted_volume_by_mount_point(v->mount_point);
1121 if (mv) {
1122 // volume is already mounted
1123 return 1;
1124 }
1125 return 0;
1126}
Koushik Dutta5460f0c2010-12-18 22:37:49 -08001127
1128int has_datadata() {
1129 Volume *vol = volume_for_path("/datadata");
1130 return vol != NULL;
1131}