blob: d263405aa17f87539e289ef2170da7109b6ff7f9 [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
jt113447d86a32011-02-27 18:54:09 -0600343#ifndef BOARD_UMS_LUNFILE
344#define BOARD_UMS_LUNFILE "/sys/devices/platform/usb_mass_storage/lun0/file"
345#endif
346
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700347void show_mount_usb_storage_menu()
Koushik K. Dutta03173782010-02-26 14:14:23 -0800348{
Koushik Duttaf1cb0d52011-01-01 20:03:30 -0800349 int fd;
Koushik Duttadf1e4062010-12-18 17:42:31 -0800350 Volume *vol = volume_for_path("/sdcard");
jt113447d86a32011-02-27 18:54:09 -0600351 if ((fd = open(BOARD_UMS_LUNFILE, O_WRONLY)) < 0) {
Koushik Duttaf1cb0d52011-01-01 20:03:30 -0800352 LOGE("Unable to open ums lunfile (%s)", strerror(errno));
353 return -1;
354 }
355
mik_osdf3004b2011-04-26 19:15:22 +0300356 if ((write(fd, vol->device, strlen(vol->device)) < 0) &&
357 (!vol->device2 || (write(fd, vol->device, strlen(vol->device2)) < 0))) {
Koushik Duttaf1cb0d52011-01-01 20:03:30 -0800358 LOGE("Unable to write to ums lunfile (%s)", strerror(errno));
359 close(fd);
360 return -1;
361 }
Koushik K. Dutta03173782010-02-26 14:14:23 -0800362 static char* headers[] = { "USB Mass Storage device",
363 "Leaving this menu unmount",
364 "your SD card from your PC.",
365 "",
Steve Kondik4123b582010-11-14 03:18:40 -0500366 NULL
Koushik K. Dutta03173782010-02-26 14:14:23 -0800367 };
Steve Kondik4123b582010-11-14 03:18:40 -0500368
Koushik K. Dutta03173782010-02-26 14:14:23 -0800369 static char* list[] = { "Unmount", NULL };
Steve Kondik4123b582010-11-14 03:18:40 -0500370
Koushik K. Dutta03173782010-02-26 14:14:23 -0800371 for (;;)
372 {
Koushik Duttadf1e4062010-12-18 17:42:31 -0800373 int chosen_item = get_menu_selection(headers, list, 0, 0);
Koushik K. Dutta03173782010-02-26 14:14:23 -0800374 if (chosen_item == GO_BACK || chosen_item == 0)
375 break;
376 }
Steve Kondik4123b582010-11-14 03:18:40 -0500377
jt113447d86a32011-02-27 18:54:09 -0600378 if ((fd = open(BOARD_UMS_LUNFILE, O_WRONLY)) < 0) {
Koushik Duttaf1cb0d52011-01-01 20:03:30 -0800379 LOGE("Unable to open ums lunfile (%s)", strerror(errno));
380 return -1;
381 }
382
383 char ch = 0;
384 if (write(fd, &ch, 1) < 0) {
385 LOGE("Unable to write to ums lunfile (%s)", strerror(errno));
386 close(fd);
387 return -1;
388 }
Koushik K. Duttae81cb752010-02-26 17:44:33 -0800389}
Koushik K. Duttaf68aaaf2010-03-07 13:39:21 -0800390
Koushik Duttad63eaef2010-07-14 21:01:21 -0700391int confirm_selection(const char* title, const char* confirm)
Koushik K. Dutta16f0b492010-03-19 14:37:11 -0700392{
Koushik Duttaceddcd52010-08-23 16:13:14 -0700393 struct stat info;
394 if (0 == stat("/sdcard/clockworkmod/.no_confirm", &info))
395 return 1;
396
Koushik Duttad63eaef2010-07-14 21:01:21 -0700397 char* confirm_headers[] = { title, " THIS CAN NOT BE UNDONE.", "", NULL };
398 char* items[] = { "No",
399 "No",
400 "No",
401 "No",
402 "No",
403 "No",
404 "No",
Koushik Duttaecd32fa2010-07-14 18:38:02 -0700405 confirm, //" Yes -- wipe partition", // [7
Koushik Duttad63eaef2010-07-14 21:01:21 -0700406 "No",
407 "No",
408 "No",
Koushik K. Dutta16f0b492010-03-19 14:37:11 -0700409 NULL };
410
Koushik Duttadf1e4062010-12-18 17:42:31 -0800411 int chosen_item = get_menu_selection(confirm_headers, items, 0, 0);
Koushik K. Dutta16f0b492010-03-19 14:37:11 -0700412 return chosen_item == 7;
413}
414
Koushik Duttada32b542011-01-01 17:55:22 -0800415#define MKE2FS_BIN "/sbin/mke2fs"
416#define TUNE2FS_BIN "/sbin/tune2fs"
417#define E2FSCK_BIN "/sbin/e2fsck"
418
Koushik Duttada32b542011-01-01 17:55:22 -0800419int format_unknown_device(const char *device, const char* path, const char *fs_type)
Koushik K. Dutta3f995392010-03-30 23:29:43 -0700420{
Koushik Dutta2e454492011-01-01 18:06:17 -0800421 LOGI("Formatting unknown device.\n");
Koushik Duttaa8708c62011-01-01 18:00:27 -0800422
Koushik Duttada32b542011-01-01 17:55:22 -0800423 // device may simply be a name, like "system"
424 if (device[0] != '/')
Koushik Duttafdd47662011-04-17 00:49:15 -0700425 return erase_raw_partition(device);
Koushik Duttada32b542011-01-01 17:55:22 -0800426
Koushik Dutta9f52e5f2011-01-02 14:11:24 -0800427 // if this is SDEXT:, don't worry about it if it does not exist.
Koushik Duttadf1e4062010-12-18 17:42:31 -0800428 if (0 == strcmp(path, "/sd-ext"))
Koushik K. Duttaf3534d02010-04-18 18:06:24 -0700429 {
430 struct stat st;
Koushik Duttadf1e4062010-12-18 17:42:31 -0800431 Volume *vol = volume_for_path("/sd-ext");
432 if (vol == NULL || 0 != stat(vol->device, &st))
Koushik K. Duttaf3534d02010-04-18 18:06:24 -0700433 {
434 ui_print("No app2sd partition found. Skipping format of /sd-ext.\n");
435 return 0;
436 }
437 }
Koushik Dutta2f73e582010-04-18 16:00:21 -0700438
Koushik Dutta9f52e5f2011-01-02 14:11:24 -0800439 if (NULL != fs_type) {
440 if (strcmp("ext3", fs_type) == 0) {
441 LOGI("Formatting ext3 device.\n");
442 if (0 != ensure_path_unmounted(path)) {
443 LOGE("Error while unmounting %s.\n", path);
444 return -12;
445 }
446 return format_ext3_device(device);
447 }
448
449 if (strcmp("ext2", fs_type) == 0) {
450 LOGI("Formatting ext2 device.\n");
451 if (0 != ensure_path_unmounted(path)) {
452 LOGE("Error while unmounting %s.\n", path);
453 return -12;
454 }
455 return format_ext2_device(device);
456 }
457 }
458
Koushik Duttadf1e4062010-12-18 17:42:31 -0800459 if (0 != ensure_path_mounted(path))
Koushik K. Dutta3f995392010-03-30 23:29:43 -0700460 {
461 ui_print("Error mounting %s!\n", path);
Koushik Duttacd44ab92010-06-23 00:02:14 -0700462 ui_print("Skipping format...\n");
463 return 0;
Koushik K. Dutta3f995392010-03-30 23:29:43 -0700464 }
465
466 static char tmp[PATH_MAX];
467 sprintf(tmp, "rm -rf %s/*", path);
468 __system(tmp);
469 sprintf(tmp, "rm -rf %s/.*", path);
470 __system(tmp);
Steve Kondik4123b582010-11-14 03:18:40 -0500471
Koushik Duttadf1e4062010-12-18 17:42:31 -0800472 ensure_path_unmounted(path);
Koushik K. Dutta3f995392010-03-30 23:29:43 -0700473 return 0;
474}
475
Koushik Duttadf1e4062010-12-18 17:42:31 -0800476//#define MOUNTABLE_COUNT 5
477//#define DEVICE_COUNT 4
478//#define MMC_COUNT 2
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700479
Kolja Dummann92796ec2011-02-13 20:59:30 +0100480typedef struct {
481 char mount[255];
482 char unmount[255];
483 Volume* v;
484} MountMenuEntry;
485
486typedef struct {
487 char txt[255];
488 Volume* v;
489} FormatMenuEntry;
490
Steve Kondik0d878512011-04-09 01:41:45 -0400491int is_safe_to_format(char* name)
492{
493 return !(strcmp(name, "/misc") == 0 || strcmp(name, "/radio") == 0
494 || strcmp(name, "/bootloader") == 0 || strcmp(name, "/recovery") == 0);
495}
496
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700497void show_partition_menu()
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700498{
Koushik Duttaceddcd52010-08-23 16:13:14 -0700499 static char* headers[] = { "Mounts and Storage Menu",
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700500 "",
Steve Kondik4123b582010-11-14 03:18:40 -0500501 NULL
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700502 };
503
Kolja Dummann92796ec2011-02-13 20:59:30 +0100504 static MountMenuEntry* mount_menue = NULL;
505 static FormatMenuEntry* format_menue = NULL;
506
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700507 typedef char* string;
Steve Kondik4123b582010-11-14 03:18:40 -0500508
Kolja Dummann92796ec2011-02-13 20:59:30 +0100509 int i, mountable_volumes, formatable_volumes;
510 int num_volumes;
511 Volume* device_volumes;
Steve Kondik4123b582010-11-14 03:18:40 -0500512
Kolja Dummann92796ec2011-02-13 20:59:30 +0100513 num_volumes = get_num_volumes();
514 device_volumes = get_device_volumes();
515
516 string options[255];
517
518 if(!device_volumes)
519 return;
520
521 mountable_volumes = 0;
522 formatable_volumes = 0;
523
524 mount_menue = malloc(num_volumes * sizeof(MountMenuEntry));
525 format_menue = malloc(num_volumes * sizeof(FormatMenuEntry));
526
527 for (i = 0; i < num_volumes; ++i) {
528 Volume* v = &device_volumes[i];
Koushik Dutta67fa0c32011-03-17 11:37:21 -0700529 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)
Kolja Dummann92796ec2011-02-13 20:59:30 +0100530 {
531 sprintf(&mount_menue[mountable_volumes].mount, "mount %s", v->mount_point);
532 sprintf(&mount_menue[mountable_volumes].unmount, "unmount %s", v->mount_point);
533 mount_menue[mountable_volumes].v = &device_volumes[i];
534 ++mountable_volumes;
Steve Kondik0d878512011-04-09 01:41:45 -0400535 if (is_safe_to_format(v->mount_point)) {
536 sprintf(&format_menue[formatable_volumes].txt, "format %s", v->mount_point);
537 format_menue[formatable_volumes].v = &device_volumes[i];
538 ++formatable_volumes;
539 }
Kolja Dummann92796ec2011-02-13 20:59:30 +0100540 }
Steve Kondik0d878512011-04-09 01:41:45 -0400541 else if (strcmp("ramdisk", v->fs_type) != 0 && strcmp("mtd", v->fs_type) == 0 && is_safe_to_format(v->mount_point))
Kolja Dummann92796ec2011-02-13 20:59:30 +0100542 {
543 sprintf(&format_menue[formatable_volumes].txt, "format %s", v->mount_point);
544 format_menue[formatable_volumes].v = &device_volumes[i];
545 ++formatable_volumes;
546 }
547 }
548
Koushik Duttadf1e4062010-12-18 17:42:31 -0800549
Koushik Duttad63eaef2010-07-14 21:01:21 -0700550 static char* confirm_format = "Confirm format?";
Koushik Duttaecd32fa2010-07-14 18:38:02 -0700551 static char* confirm = "Yes - Format";
wjbcea6eba2011-04-30 21:04:04 -0400552 char confirm_string[255];
Steve Kondik4123b582010-11-14 03:18:40 -0500553
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700554 for (;;)
555 {
Steve Kondik4123b582010-11-14 03:18:40 -0500556
Kolja Dummann92796ec2011-02-13 20:59:30 +0100557 for (i = 0; i < mountable_volumes; i++)
558 {
559 MountMenuEntry* e = &mount_menue[i];
560 Volume* v = e->v;
561 if(is_path_mounted(v->mount_point))
562 options[i] = e->unmount;
563 else
564 options[i] = e->mount;
565 }
Steve Kondik4123b582010-11-14 03:18:40 -0500566
Kolja Dummann92796ec2011-02-13 20:59:30 +0100567 for (i = 0; i < formatable_volumes; i++)
568 {
569 FormatMenuEntry* e = &format_menue[i];
Steve Kondik4123b582010-11-14 03:18:40 -0500570
Kolja Dummann92796ec2011-02-13 20:59:30 +0100571 options[mountable_volumes+i] = e->txt;
572 }
573
574 options[mountable_volumes+formatable_volumes] = "mount USB storage";
575 options[mountable_volumes+formatable_volumes + 1] = NULL;
576
577 int chosen_item = get_menu_selection(headers, &options, 0, 0);
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700578 if (chosen_item == GO_BACK)
579 break;
Kolja Dummann92796ec2011-02-13 20:59:30 +0100580 if (chosen_item == (mountable_volumes+formatable_volumes))
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700581 {
582 show_mount_usb_storage_menu();
583 }
Kolja Dummann92796ec2011-02-13 20:59:30 +0100584 else if (chosen_item < mountable_volumes)
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700585 {
Kolja Dummann92796ec2011-02-13 20:59:30 +0100586 MountMenuEntry* e = &mount_menue[chosen_item];
587 Volume* v = e->v;
588
589 if (is_path_mounted(v->mount_point))
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700590 {
Kolja Dummann92796ec2011-02-13 20:59:30 +0100591 if (0 != ensure_path_unmounted(v->mount_point))
592 ui_print("Error unmounting %s!\n", v->mount_point);
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700593 }
594 else
595 {
Kolja Dummann92796ec2011-02-13 20:59:30 +0100596 if (0 != ensure_path_mounted(v->mount_point))
597 ui_print("Error mounting %s!\n", v->mount_point);
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700598 }
599 }
Kolja Dummann92796ec2011-02-13 20:59:30 +0100600 else if (chosen_item < (mountable_volumes + formatable_volumes))
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700601 {
Kolja Dummann92796ec2011-02-13 20:59:30 +0100602 chosen_item = chosen_item - mountable_volumes;
603 FormatMenuEntry* e = &format_menue[chosen_item];
604 Volume* v = e->v;
605
wjbcea6eba2011-04-30 21:04:04 -0400606 sprintf(confirm_string, "%s - %s", v->mount_point, confirm_format);
607
608 if (!confirm_selection(confirm_string, confirm))
Koushik K. Dutta16f0b492010-03-19 14:37:11 -0700609 continue;
Kolja Dummann92796ec2011-02-13 20:59:30 +0100610 ui_print("Formatting %s...\n", v->mount_point);
611 if (0 != format_volume(v->mount_point))
612 ui_print("Error formatting %s!\n", v->mount_point);
Koushik K. Dutta3f995392010-03-30 23:29:43 -0700613 else
614 ui_print("Done.\n");
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700615 }
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700616 }
Kolja Dummann92796ec2011-02-13 20:59:30 +0100617
618 free(mount_menue);
619 free(format_menue);
620
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700621}
622
Koushik K. Dutta72a1db62010-03-07 14:10:26 -0800623#define EXTENDEDCOMMAND_SCRIPT "/cache/recovery/extendedcommand"
624
625int extendedcommand_file_exists()
Koushik K. Duttaf68aaaf2010-03-07 13:39:21 -0800626{
Koushik K. Duttaf68aaaf2010-03-07 13:39:21 -0800627 struct stat file_info;
Koushik K. Dutta72a1db62010-03-07 14:10:26 -0800628 return 0 == stat(EXTENDEDCOMMAND_SCRIPT, &file_info);
629}
630
Koushik Duttabec09952010-12-19 20:37:57 -0800631int edify_main(int argc, char** argv) {
632 load_volume_table();
633 process_volumes();
634 RegisterBuiltins();
635 RegisterRecoveryHooks();
636 FinishRegistration();
Koushik Duttadf1e4062010-12-18 17:42:31 -0800637
Koushik Duttabec09952010-12-19 20:37:57 -0800638 if (argc != 2) {
639 printf("edify <filename>\n");
Koushik K. Duttaf68aaaf2010-03-07 13:39:21 -0800640 return 1;
Koushik K. Duttaf68aaaf2010-03-07 13:39:21 -0800641 }
642
Koushik Duttabec09952010-12-19 20:37:57 -0800643 FILE* f = fopen(argv[1], "r");
644 if (f == NULL) {
645 printf("%s: %s: No such file or directory\n", argv[0], argv[1]);
646 return 1;
647 }
648 char buffer[8192];
649 int size = fread(buffer, 1, 8191, f);
650 fclose(f);
651 buffer[size] = '\0';
652
653 Expr* root;
654 int error_count = 0;
655 yy_scan_bytes(buffer, size);
656 int error = yyparse(&root, &error_count);
657 printf("parse returned %d; %d errors encountered\n", error, error_count);
658 if (error == 0 || error_count > 0) {
659
660 //ExprDump(0, root, buffer);
661
662 State state;
663 state.cookie = NULL;
664 state.script = buffer;
665 state.errmsg = NULL;
666
667 char* result = Evaluate(&state, root);
668 if (result == NULL) {
669 printf("result was NULL, message is: %s\n",
670 (state.errmsg == NULL ? "(NULL)" : state.errmsg));
671 free(state.errmsg);
672 } else {
673 printf("result is [%s]\n", result);
Koushik K. Duttaf68aaaf2010-03-07 13:39:21 -0800674 }
Steve Kondik4123b582010-11-14 03:18:40 -0500675 }
Koushik Duttabec09952010-12-19 20:37:57 -0800676 return 0;
Koushik K. Duttaf68aaaf2010-03-07 13:39:21 -0800677}
Koushik K. Dutta72a1db62010-03-07 14:10:26 -0800678
Koushik K. Dutta6771aca2010-04-03 23:28:39 -0700679int run_script(char* filename)
Koushik K. Dutta3836f722010-03-11 22:17:43 -0800680{
681 struct stat file_info;
682 if (0 != stat(filename, &file_info)) {
683 printf("Error executing stat on file: %s\n", filename);
684 return 1;
685 }
Steve Kondik4123b582010-11-14 03:18:40 -0500686
Koushik K. Dutta3836f722010-03-11 22:17:43 -0800687 int script_len = file_info.st_size;
Koushik K. Dutta707fa6d2010-03-23 11:44:33 -0700688 char* script_data = (char*)malloc(script_len + 1);
Koushik K. Dutta3836f722010-03-11 22:17:43 -0800689 FILE *file = fopen(filename, "rb");
690 fread(script_data, script_len, 1, file);
Koushik K. Dutta707fa6d2010-03-23 11:44:33 -0700691 // supposedly not necessary, but let's be safe.
692 script_data[script_len] = '\0';
Koushik K. Dutta3836f722010-03-11 22:17:43 -0800693 fclose(file);
Koushik Duttae25908b2010-06-21 08:16:19 -0700694 LOGI("Running script:\n");
695 LOGI("\n%s\n", script_data);
Koushik K. Dutta3836f722010-03-11 22:17:43 -0800696
Koushik K. Duttaf3534d02010-04-18 18:06:24 -0700697 int ret = run_script_from_buffer(script_data, script_len, filename);
Koushik K. Dutta1d53c4e2010-04-02 16:46:21 -0700698 free(script_data);
699 return ret;
Koushik K. Dutta3836f722010-03-11 22:17:43 -0800700}
701
Koushik K. Dutta72a1db62010-03-07 14:10:26 -0800702int run_and_remove_extendedcommand()
703{
Koushik K. Dutta6771aca2010-04-03 23:28:39 -0700704 char tmp[PATH_MAX];
705 sprintf(tmp, "cp %s /tmp/%s", EXTENDEDCOMMAND_SCRIPT, basename(EXTENDEDCOMMAND_SCRIPT));
706 __system(tmp);
707 remove(EXTENDEDCOMMAND_SCRIPT);
Koushik K. Dutta3a25cf52010-03-08 19:22:41 -0800708 int i = 0;
709 for (i = 20; i > 0; i--) {
710 ui_print("Waiting for SD Card to mount (%ds)\n", i);
Koushik Duttadf1e4062010-12-18 17:42:31 -0800711 if (ensure_path_mounted("/sdcard") == 0) {
Koushik K. Dutta3a25cf52010-03-08 19:22:41 -0800712 ui_print("SD Card mounted...\n");
713 break;
714 }
715 sleep(1);
716 }
Koushik Dutta92077c12010-07-15 00:10:08 -0700717 remove("/sdcard/clockworkmod/.recoverycheckpoint");
Koushik K. Dutta3a25cf52010-03-08 19:22:41 -0800718 if (i == 0) {
719 ui_print("Timed out waiting for SD card... continuing anyways.");
720 }
Steve Kondik4123b582010-11-14 03:18:40 -0500721
Koushik K. Dutta6771aca2010-04-03 23:28:39 -0700722 sprintf(tmp, "/tmp/%s", basename(EXTENDEDCOMMAND_SCRIPT));
723 return run_script(tmp);
Koushik K. Dutta72a1db62010-03-07 14:10:26 -0800724}
725
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700726void show_nandroid_advanced_restore_menu()
727{
Koushik Duttadf1e4062010-12-18 17:42:31 -0800728 if (ensure_path_mounted("/sdcard") != 0) {
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700729 LOGE ("Can't mount /sdcard\n");
730 return;
731 }
732
733 static char* advancedheaders[] = { "Choose an image to restore",
734 "",
735 "Choose an image to restore",
736 "first. The next menu will",
737 "you more options.",
738 "",
739 NULL
740 };
741
742 char* file = choose_file_menu("/sdcard/clockworkmod/backup/", NULL, advancedheaders);
743 if (file == NULL)
744 return;
745
746 static char* headers[] = { "Nandroid Advanced Restore",
747 "",
748 NULL
749 };
750
751 static char* list[] = { "Restore boot",
752 "Restore system",
753 "Restore data",
754 "Restore cache",
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700755 "Restore sd-ext",
agrabren1f76a5d2010-12-29 12:15:18 -0600756 "Restore wimax",
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700757 NULL
758 };
Koushik Dutta5b7f34a2010-12-29 23:36:03 -0800759
760 char tmp[PATH_MAX];
761 if (0 != get_partition_device("wimax", tmp)) {
762 // disable wimax restore option
763 list[5] = NULL;
764 }
Koushik Duttad63eaef2010-07-14 21:01:21 -0700765
766 static char* confirm_restore = "Confirm restore?";
767
Koushik Duttadf1e4062010-12-18 17:42:31 -0800768 int chosen_item = get_menu_selection(headers, list, 0, 0);
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700769 switch (chosen_item)
770 {
771 case 0:
Koushik Duttad63eaef2010-07-14 21:01:21 -0700772 if (confirm_selection(confirm_restore, "Yes - Restore boot"))
agrabren1f76a5d2010-12-29 12:15:18 -0600773 nandroid_restore(file, 1, 0, 0, 0, 0, 0);
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700774 break;
775 case 1:
Koushik Duttad63eaef2010-07-14 21:01:21 -0700776 if (confirm_selection(confirm_restore, "Yes - Restore system"))
agrabren1f76a5d2010-12-29 12:15:18 -0600777 nandroid_restore(file, 0, 1, 0, 0, 0, 0);
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700778 break;
779 case 2:
Koushik Duttad63eaef2010-07-14 21:01:21 -0700780 if (confirm_selection(confirm_restore, "Yes - Restore data"))
agrabren1f76a5d2010-12-29 12:15:18 -0600781 nandroid_restore(file, 0, 0, 1, 0, 0, 0);
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700782 break;
783 case 3:
Koushik Duttad63eaef2010-07-14 21:01:21 -0700784 if (confirm_selection(confirm_restore, "Yes - Restore cache"))
agrabren1f76a5d2010-12-29 12:15:18 -0600785 nandroid_restore(file, 0, 0, 0, 1, 0, 0);
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700786 break;
Koushik Dutta2f73e582010-04-18 16:00:21 -0700787 case 4:
Koushik Duttad63eaef2010-07-14 21:01:21 -0700788 if (confirm_selection(confirm_restore, "Yes - Restore sd-ext"))
agrabren1f76a5d2010-12-29 12:15:18 -0600789 nandroid_restore(file, 0, 0, 0, 0, 1, 0);
790 break;
791 case 5:
792 if (confirm_selection(confirm_restore, "Yes - Restore wimax"))
793 nandroid_restore(file, 0, 0, 0, 0, 0, 1);
Koushik Dutta2f73e582010-04-18 16:00:21 -0700794 break;
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700795 }
796}
797
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700798void show_nandroid_menu()
799{
800 static char* headers[] = { "Nandroid",
801 "",
Steve Kondik4123b582010-11-14 03:18:40 -0500802 NULL
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700803 };
804
Steve Kondik4123b582010-11-14 03:18:40 -0500805 static char* list[] = { "Backup",
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700806 "Restore",
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700807 "Advanced Restore",
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700808 NULL
809 };
810
Koushik Duttadf1e4062010-12-18 17:42:31 -0800811 int chosen_item = get_menu_selection(headers, list, 0, 0);
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700812 switch (chosen_item)
813 {
814 case 0:
815 {
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700816 char backup_path[PATH_MAX];
Koushik K. Dutta6a26e7c2010-03-27 15:26:11 -0700817 time_t t = time(NULL);
818 struct tm *tmp = localtime(&t);
819 if (tmp == NULL)
820 {
821 struct timeval tp;
822 gettimeofday(&tp, NULL);
823 sprintf(backup_path, "/sdcard/clockworkmod/backup/%d", tp.tv_sec);
824 }
825 else
826 {
827 strftime(backup_path, sizeof(backup_path), "/sdcard/clockworkmod/backup/%F.%H.%M.%S", tmp);
828 }
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700829 nandroid_backup(backup_path);
830 }
831 break;
832 case 1:
833 show_nandroid_restore_menu();
834 break;
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700835 case 2:
836 show_nandroid_advanced_restore_menu();
837 break;
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700838 }
Koushik K. Duttaa496b512010-03-19 14:51:45 -0700839}
840
Koushik Duttafd1579b2010-05-01 12:46:55 -0700841void wipe_battery_stats()
842{
Koushik Duttadf1e4062010-12-18 17:42:31 -0800843 ensure_path_mounted("/data");
Koushik Duttafd1579b2010-05-01 12:46:55 -0700844 remove("/data/system/batterystats.bin");
Koushik Duttadf1e4062010-12-18 17:42:31 -0800845 ensure_path_unmounted("/data");
Koushik Duttafd1579b2010-05-01 12:46:55 -0700846}
847
Koushik K. Duttaa496b512010-03-19 14:51:45 -0700848void show_advanced_menu()
849{
850 static char* headers[] = { "Advanced and Debugging Menu",
851 "",
852 NULL
853 };
854
Koushik K. Dutta7fe4d7b2010-04-06 23:04:52 -0700855 static char* list[] = { "Reboot Recovery",
Koushik Dutta49af23c2010-06-21 13:45:51 -0700856 "Wipe Dalvik Cache",
Koushik Duttafd1579b2010-05-01 12:46:55 -0700857 "Wipe Battery Stats",
Koushik Dutta598cfc72010-06-20 09:42:47 -0700858 "Report Error",
Koushik K. Dutta7fe4d7b2010-04-06 23:04:52 -0700859 "Key Test",
Koushik Duttae17a78d2010-08-29 12:35:10 -0700860#ifndef BOARD_HAS_SMALL_RECOVERY
Koushik Duttaceddcd52010-08-23 16:13:14 -0700861 "Partition SD Card",
862 "Fix Permissions",
Brint E. Kriebeld3522332010-12-11 20:52:06 -0700863#ifdef BOARD_HAS_SDCARD_INTERNAL
864 "Partition Internal SD Card",
865#endif
Koushik Duttae17a78d2010-08-29 12:35:10 -0700866#endif
Koushik K. Duttaa496b512010-03-19 14:51:45 -0700867 NULL
868 };
869
870 for (;;)
871 {
Koushik Duttadf1e4062010-12-18 17:42:31 -0800872 int chosen_item = get_menu_selection(headers, list, 0, 0);
Koushik K. Duttaa496b512010-03-19 14:51:45 -0700873 if (chosen_item == GO_BACK)
874 break;
875 switch (chosen_item)
876 {
877 case 0:
Koushik K. Dutta7fe4d7b2010-04-06 23:04:52 -0700878 __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, "recovery");
879 break;
880 case 1:
Koushik Duttad63eaef2010-07-14 21:01:21 -0700881 {
Koushik Duttadf1e4062010-12-18 17:42:31 -0800882 if (0 != ensure_path_mounted("/data"))
Koushik Dutta49af23c2010-06-21 13:45:51 -0700883 break;
Koushik Duttadf1e4062010-12-18 17:42:31 -0800884 ensure_path_mounted("/sd-ext");
885 ensure_path_mounted("/cache");
Koushik Dutta6c7745d2010-08-07 12:17:13 -0700886 if (confirm_selection( "Confirm wipe?", "Yes - Wipe Dalvik Cache")) {
Koushik Duttad63eaef2010-07-14 21:01:21 -0700887 __system("rm -r /data/dalvik-cache");
Koushik Dutta6c7745d2010-08-07 12:17:13 -0700888 __system("rm -r /cache/dalvik-cache");
889 __system("rm -r /sd-ext/dalvik-cache");
890 }
Koushik Duttadf1e4062010-12-18 17:42:31 -0800891 ensure_path_unmounted("/data");
Koushik Dutta6c7745d2010-08-07 12:17:13 -0700892 ui_print("Dalvik Cache wiped.\n");
Koushik Duttafd1579b2010-05-01 12:46:55 -0700893 break;
Koushik Duttad63eaef2010-07-14 21:01:21 -0700894 }
Koushik Duttafd1579b2010-05-01 12:46:55 -0700895 case 2:
Koushik Duttad63eaef2010-07-14 21:01:21 -0700896 {
897 if (confirm_selection( "Confirm wipe?", "Yes - Wipe Battery Stats"))
898 wipe_battery_stats();
Koushik Dutta598cfc72010-06-20 09:42:47 -0700899 break;
Koushik Duttad63eaef2010-07-14 21:01:21 -0700900 }
Koushik Dutta598cfc72010-06-20 09:42:47 -0700901 case 3:
Koushik Dutta49af23c2010-06-21 13:45:51 -0700902 handle_failure(1);
903 break;
904 case 4:
Koushik K. Duttaa496b512010-03-19 14:51:45 -0700905 {
906 ui_print("Outputting key codes.\n");
907 ui_print("Go back to end debugging.\n");
908 int key;
909 int action;
910 do
911 {
912 key = ui_wait_key();
913 action = device_handle_key(key, 1);
914 ui_print("Key: %d\n", key);
915 }
916 while (action != GO_BACK);
Koushik Dutta56606a22010-08-23 16:15:33 -0700917 break;
Koushik K. Duttaa496b512010-03-19 14:51:45 -0700918 }
Koushik Duttaf0e31b82010-08-17 16:55:38 -0700919 case 5:
920 {
Koushik Duttaceddcd52010-08-23 16:13:14 -0700921 static char* ext_sizes[] = { "128M",
922 "256M",
923 "512M",
924 "1024M",
Brint E. Kriebel0b233312010-11-14 15:31:17 -0700925 "2048M",
926 "4096M",
Koushik Duttaceddcd52010-08-23 16:13:14 -0700927 NULL };
928
929 static char* swap_sizes[] = { "0M",
930 "32M",
931 "64M",
932 "128M",
933 "256M",
934 NULL };
935
936 static char* ext_headers[] = { "Ext Size", "", NULL };
937 static char* swap_headers[] = { "Swap Size", "", NULL };
938
Koushik Duttadf1e4062010-12-18 17:42:31 -0800939 int ext_size = get_menu_selection(ext_headers, ext_sizes, 0, 0);
Koushik Duttaceddcd52010-08-23 16:13:14 -0700940 if (ext_size == GO_BACK)
941 continue;
Steve Kondik4123b582010-11-14 03:18:40 -0500942
Koushik Duttadf1e4062010-12-18 17:42:31 -0800943 int swap_size = get_menu_selection(swap_headers, swap_sizes, 0, 0);
Koushik Duttaceddcd52010-08-23 16:13:14 -0700944 if (swap_size == GO_BACK)
945 continue;
946
947 char sddevice[256];
Koushik Duttadf1e4062010-12-18 17:42:31 -0800948 Volume *vol = volume_for_path("/sdcard");
949 strcpy(sddevice, vol->device);
Koushik Duttaceddcd52010-08-23 16:13:14 -0700950 // we only want the mmcblk, not the partition
951 sddevice[strlen("/dev/block/mmcblkX")] = NULL;
952 char cmd[PATH_MAX];
953 setenv("SDPATH", sddevice, 1);
954 sprintf(cmd, "sdparted -es %s -ss %s -efs ext3 -s", ext_sizes[ext_size], swap_sizes[swap_size]);
955 ui_print("Partitioning SD Card... please wait...\n");
956 if (0 == __system(cmd))
957 ui_print("Done!\n");
958 else
959 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 -0700960 break;
Koushik Duttaf0e31b82010-08-17 16:55:38 -0700961 }
Koushik Dutta38e8b2b2010-08-17 22:21:53 -0700962 case 6:
963 {
Koushik Duttadf1e4062010-12-18 17:42:31 -0800964 ensure_path_mounted("/system");
965 ensure_path_mounted("/data");
Koushik Duttaceddcd52010-08-23 16:13:14 -0700966 ui_print("Fixing permissions...\n");
967 __system("fix_permissions");
968 ui_print("Done!\n");
Koushik Dutta56606a22010-08-23 16:15:33 -0700969 break;
Koushik Dutta38e8b2b2010-08-17 22:21:53 -0700970 }
Brint E. Kriebeld3522332010-12-11 20:52:06 -0700971 case 7:
972 {
973 static char* ext_sizes[] = { "128M",
974 "256M",
975 "512M",
976 "1024M",
977 "2048M",
978 "4096M",
979 NULL };
980
981 static char* swap_sizes[] = { "0M",
982 "32M",
983 "64M",
984 "128M",
985 "256M",
986 NULL };
987
988 static char* ext_headers[] = { "Data Size", "", NULL };
989 static char* swap_headers[] = { "Swap Size", "", NULL };
990
Koushik Duttadf1e4062010-12-18 17:42:31 -0800991 int ext_size = get_menu_selection(ext_headers, ext_sizes, 0, 0);
Brint E. Kriebeld3522332010-12-11 20:52:06 -0700992 if (ext_size == GO_BACK)
993 continue;
994
995 int swap_size = 0;
996 if (swap_size == GO_BACK)
997 continue;
998
999 char sddevice[256];
Koushik Duttadf1e4062010-12-18 17:42:31 -08001000 Volume *vol = volume_for_path("/emmc");
1001 strcpy(sddevice, vol->device);
Brint E. Kriebeld3522332010-12-11 20:52:06 -07001002 // we only want the mmcblk, not the partition
1003 sddevice[strlen("/dev/block/mmcblkX")] = NULL;
1004 char cmd[PATH_MAX];
1005 setenv("SDPATH", sddevice, 1);
1006 sprintf(cmd, "sdparted -es %s -ss %s -efs ext3 -s", ext_sizes[ext_size], swap_sizes[swap_size]);
1007 ui_print("Partitioning Internal SD Card... please wait...\n");
1008 if (0 == __system(cmd))
1009 ui_print("Done!\n");
1010 else
1011 ui_print("An error occured while partitioning your Internal SD Card. Please see /tmp/recovery.log for more details.\n");
1012 break;
1013 }
Koushik K. Duttaa496b512010-03-19 14:51:45 -07001014 }
1015 }
Koushik Dutta14239d22010-06-14 15:02:48 -07001016}
1017
Koushik Duttab643e4f2010-12-18 18:39:39 -08001018void write_fstab_root(char *path, FILE *file)
Koushik Dutta14239d22010-06-14 15:02:48 -07001019{
Koushik Duttab643e4f2010-12-18 18:39:39 -08001020 Volume *vol = volume_for_path(path);
1021 if (vol == NULL) {
Koushik Dutta9765a032010-12-18 21:31:02 -08001022 LOGW("Unable to get recovery.fstab info for %s during fstab generation!\n", path);
Koushik Dutta598cfc72010-06-20 09:42:47 -07001023 return;
Koushik Duttaa6522b32010-06-20 13:16:06 -07001024 }
Steve Kondik4123b582010-11-14 03:18:40 -05001025
Koushik Dutta31b74112010-12-20 08:49:20 -08001026 char device[200];
1027 if (vol->device[0] != '/')
1028 get_partition_device(vol->device, device);
1029 else
1030 strcpy(device, vol->device);
1031
1032 fprintf(file, "%s ", device);
Koushik Duttab643e4f2010-12-18 18:39:39 -08001033 fprintf(file, "%s ", path);
Koushik Duttaab1f8b82011-02-27 17:28:30 -08001034 // special case rfs cause auto will mount it as vfat on samsung.
1035 fprintf(file, "%s rw\n", vol->fs_type2 != NULL && strcmp(vol->fs_type, "rfs") != 0 ? "auto" : vol->fs_type);
Koushik Dutta14239d22010-06-14 15:02:48 -07001036}
1037
1038void create_fstab()
1039{
Koushik Duttab643e4f2010-12-18 18:39:39 -08001040 struct stat info;
Koushik Duttacd44ab92010-06-23 00:02:14 -07001041 __system("touch /etc/mtab");
Koushik Dutta14239d22010-06-14 15:02:48 -07001042 FILE *file = fopen("/etc/fstab", "w");
Koushik Duttaa6522b32010-06-20 13:16:06 -07001043 if (file == NULL) {
Koushik Duttab643e4f2010-12-18 18:39:39 -08001044 LOGW("Unable to create /etc/fstab!\n");
Koushik Dutta598cfc72010-06-20 09:42:47 -07001045 return;
Koushik Duttaa6522b32010-06-20 13:16:06 -07001046 }
Brandon Bennett6d0604b2011-01-28 13:39:10 -07001047 Volume *vol = volume_for_path("/boot");
Koushik Duttaa9f0e7f2011-02-12 10:21:11 -08001048 if (NULL != vol && strcmp(vol->fs_type, "mtd") != 0 && strcmp(vol->fs_type, "emmc") != 0 && strcmp(vol->fs_type, "bml") != 0)
Brandon Bennett6d0604b2011-01-28 13:39:10 -07001049 write_fstab_root("/boot", file);
Koushik Duttab643e4f2010-12-18 18:39:39 -08001050 write_fstab_root("/cache", file);
1051 write_fstab_root("/data", file);
Koushik Dutta5460f0c2010-12-18 22:37:49 -08001052 if (has_datadata()) {
1053 write_fstab_root("/datadata", file);
1054 }
Koushik Duttab643e4f2010-12-18 18:39:39 -08001055 write_fstab_root("/system", file);
1056 write_fstab_root("/sdcard", file);
1057 write_fstab_root("/sd-ext", file);
Koushik Dutta14239d22010-06-14 15:02:48 -07001058 fclose(file);
Koushik Dutta9765a032010-12-18 21:31:02 -08001059 LOGI("Completed outputting fstab.\n");
Koushik Dutta598cfc72010-06-20 09:42:47 -07001060}
1061
Koushik Dutta4196e0f2010-12-19 03:38:29 -08001062int bml_check_volume(const char *path) {
1063 ui_print("Checking %s...\n", path);
1064 ensure_path_unmounted(path);
1065 if (0 == ensure_path_mounted(path)) {
1066 ensure_path_unmounted(path);
1067 return 0;
1068 }
1069
1070 Volume *vol = volume_for_path(path);
1071 if (vol == NULL) {
1072 LOGE("Unable process volume! Skipping...\n");
1073 return 0;
1074 }
1075
Koushik Duttabec09952010-12-19 20:37:57 -08001076 ui_print("%s may be rfs. Checking...\n", path);
Koushik Dutta4196e0f2010-12-19 03:38:29 -08001077 char tmp[PATH_MAX];
1078 sprintf(tmp, "mount -t rfs %s %s", vol->device, path);
1079 int ret = __system(tmp);
1080 printf("%d\n", ret);
1081 return ret == 0 ? 1 : 0;
1082}
1083
1084void process_volumes() {
1085 create_fstab();
1086
Koushik Duttae8bc2c82011-02-27 14:00:19 -08001087 return;
1088
1089 // dead code.
Koushik Dutta4196e0f2010-12-19 03:38:29 -08001090 if (device_flash_type() != BML)
1091 return;
1092
1093 ui_print("Checking for ext4 partitions...\n");
1094 int ret = 0;
1095 ret = bml_check_volume("/system");
1096 ret |= bml_check_volume("/data");
1097 if (has_datadata())
1098 ret |= bml_check_volume("/datadata");
1099 ret |= bml_check_volume("/cache");
1100
1101 if (ret == 0) {
1102 ui_print("Done!\n");
1103 return;
1104 }
1105
Koushik Dutta745b5ff2010-12-21 00:58:43 -08001106 char backup_path[PATH_MAX];
1107 time_t t = time(NULL);
1108 char backup_name[PATH_MAX];
1109 struct timeval tp;
1110 gettimeofday(&tp, NULL);
1111 sprintf(backup_name, "before-ext4-convert-%d", tp.tv_sec);
1112 sprintf(backup_path, "/sdcard/clockworkmod/backup/%s", backup_name);
1113
Koushik Dutta4196e0f2010-12-19 03:38:29 -08001114 ui_set_show_text(1);
1115 ui_print("Filesystems need to be converted to ext4.\n");
1116 ui_print("A backup and restore will now take place.\n");
Koushik Duttabec09952010-12-19 20:37:57 -08001117 ui_print("If anything goes wrong, your backup will be\n");
Koushik Dutta745b5ff2010-12-21 00:58:43 -08001118 ui_print("named %s. Try restoring it\n", backup_name);
Koushik Duttabec09952010-12-19 20:37:57 -08001119 ui_print("in case of error.\n");
Koushik Dutta4196e0f2010-12-19 03:38:29 -08001120
Koushik Dutta745b5ff2010-12-21 00:58:43 -08001121 nandroid_backup(backup_path);
Koushik Dutta5b7f34a2010-12-29 23:36:03 -08001122 nandroid_restore(backup_path, 1, 1, 1, 1, 1, 0);
Koushik Dutta4196e0f2010-12-19 03:38:29 -08001123 ui_set_show_text(0);
1124}
1125
Koushik Dutta598cfc72010-06-20 09:42:47 -07001126void handle_failure(int ret)
1127{
1128 if (ret == 0)
1129 return;
Koushik Duttadf1e4062010-12-18 17:42:31 -08001130 if (0 != ensure_path_mounted("/sdcard"))
Koushik Dutta598cfc72010-06-20 09:42:47 -07001131 return;
Koushik Duttaa6522b32010-06-20 13:16:06 -07001132 mkdir("/sdcard/clockworkmod", S_IRWXU);
1133 __system("cp /tmp/recovery.log /sdcard/clockworkmod/recovery.log");
Koushik Dutta38e8b2b2010-08-17 22:21:53 -07001134 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 -05001135}
Koushik Duttadf1e4062010-12-18 17:42:31 -08001136
Koushik Duttadf1e4062010-12-18 17:42:31 -08001137int is_path_mounted(const char* path) {
1138 Volume* v = volume_for_path(path);
1139 if (v == NULL) {
1140 return 0;
1141 }
1142 if (strcmp(v->fs_type, "ramdisk") == 0) {
1143 // the ramdisk is always mounted.
1144 return 1;
1145 }
1146
1147 int result;
1148 result = scan_mounted_volumes();
1149 if (result < 0) {
1150 LOGE("failed to scan mounted volumes\n");
1151 return 0;
1152 }
1153
1154 const MountedVolume* mv =
1155 find_mounted_volume_by_mount_point(v->mount_point);
1156 if (mv) {
1157 // volume is already mounted
1158 return 1;
1159 }
1160 return 0;
1161}
Koushik Dutta5460f0c2010-12-18 22:37:49 -08001162
1163int has_datadata() {
1164 Volume *vol = volume_for_path("/datadata");
1165 return vol != NULL;
Koushik Duttad8e21c32011-01-04 10:46:23 -08001166}
1167
1168int volume_main(int argc, char **argv) {
1169 load_volume_table();
1170 return 0;
1171}