blob: 762b15a898055407ba9e01fc26b0700922d8e5f3 [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 K. Duttaee57bbc2010-03-12 23:21:12 -080040
Koushik K. Dutta6060e5c2010-02-11 22:27:06 -080041int signature_check_enabled = 1;
42int script_assert_enabled = 1;
Koushik Duttadf1e4062010-12-18 17:42:31 -080043static const char *SDCARD_UPDATE_FILE = "/sdcard/update.zip";
Koushik K. Dutta6060e5c2010-02-11 22:27:06 -080044
45void
46toggle_signature_check()
47{
48 signature_check_enabled = !signature_check_enabled;
49 ui_print("Signature Check: %s\n", signature_check_enabled ? "Enabled" : "Disabled");
50}
51
52void toggle_script_asserts()
53{
54 script_assert_enabled = !script_assert_enabled;
Koushik K. Duttae9234872010-02-12 00:43:24 -080055 ui_print("Script Asserts: %s\n", script_assert_enabled ? "Enabled" : "Disabled");
56}
57
Koushik K. Duttaea46fe22010-03-08 02:58:04 -080058int install_zip(const char* packagefilepath)
Koushik K. Duttae9234872010-02-12 00:43:24 -080059{
Koushik K. Duttabcdd0032010-02-21 21:10:25 -080060 ui_print("\n-- Installing: %s\n", packagefilepath);
Koushik Dutta9765a032010-12-18 21:31:02 -080061 if (device_flash_type() == MTD) {
62 set_sdcard_update_bootloader_message();
63 }
Koushik K. Duttabcdd0032010-02-21 21:10:25 -080064 int status = install_package(packagefilepath);
Koushik K. Duttaf3534d02010-04-18 18:06:24 -070065 ui_reset_progress();
Koushik K. Duttabcdd0032010-02-21 21:10:25 -080066 if (status != INSTALL_SUCCESS) {
67 ui_set_background(BACKGROUND_ICON_ERROR);
68 ui_print("Installation aborted.\n");
Koushik K. Duttaea46fe22010-03-08 02:58:04 -080069 return 1;
Steve Kondik4123b582010-11-14 03:18:40 -050070 }
Koushik K. Dutta001c5b52010-02-25 14:53:57 -080071 ui_set_background(BACKGROUND_ICON_NONE);
Koushik K. Dutta99fb6fe2010-03-03 00:42:58 -080072 ui_print("\nInstall from sdcard complete.\n");
Koushik K. Duttaea46fe22010-03-08 02:58:04 -080073 return 0;
Koushik K. Duttabcdd0032010-02-21 21:10:25 -080074}
75
Koushik Duttadf1e4062010-12-18 17:42:31 -080076char* INSTALL_MENU_ITEMS[] = { "apply /sdcard/update.zip",
Koushik K. Duttabcdd0032010-02-21 21:10:25 -080077 "choose zip from sdcard",
78 "toggle signature verification",
79 "toggle script asserts",
80 NULL };
81#define ITEM_APPLY_SDCARD 0
82#define ITEM_CHOOSE_ZIP 1
83#define ITEM_SIG_CHECK 2
84#define ITEM_ASSERTS 3
85
86void show_install_update_menu()
87{
88 static char* headers[] = { "Apply update from .zip file on SD card",
89 "",
Steve Kondik4123b582010-11-14 03:18:40 -050090 NULL
Koushik K. Duttabcdd0032010-02-21 21:10:25 -080091 };
92 for (;;)
93 {
Koushik Duttadf1e4062010-12-18 17:42:31 -080094 int chosen_item = get_menu_selection(headers, INSTALL_MENU_ITEMS, 0, 0);
Koushik K. Duttabcdd0032010-02-21 21:10:25 -080095 switch (chosen_item)
96 {
97 case ITEM_ASSERTS:
98 toggle_script_asserts();
99 break;
100 case ITEM_SIG_CHECK:
101 toggle_signature_check();
102 break;
103 case ITEM_APPLY_SDCARD:
Koushik Duttad63eaef2010-07-14 21:01:21 -0700104 {
105 if (confirm_selection("Confirm install?", "Yes - Install /sdcard/update.zip"))
Koushik Duttadf1e4062010-12-18 17:42:31 -0800106 install_zip(SDCARD_UPDATE_FILE);
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800107 break;
Koushik Duttad63eaef2010-07-14 21:01:21 -0700108 }
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800109 case ITEM_CHOOSE_ZIP:
110 show_choose_zip_menu();
111 break;
112 default:
113 return;
114 }
Steve Kondik4123b582010-11-14 03:18:40 -0500115
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800116 }
117}
118
Koushik K. Dutta1d53c4e2010-04-02 16:46:21 -0700119void free_string_array(char** array)
120{
121 if (array == NULL)
122 return;
123 char* cursor = array[0];
124 int i = 0;
125 while (cursor != NULL)
126 {
127 free(cursor);
128 cursor = array[++i];
129 }
130 free(array);
131}
132
Koushik K. Dutta49f56892010-02-25 14:51:05 -0800133char** gather_files(const char* directory, const char* fileExtensionOrDirectory, int* numFiles)
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800134{
Koushik K. Duttae9234872010-02-12 00:43:24 -0800135 char path[PATH_MAX] = "";
136 DIR *dir;
137 struct dirent *de;
138 int total = 0;
139 int i;
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800140 char** files = NULL;
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800141 int pass;
142 *numFiles = 0;
143 int dirLen = strlen(directory);
Koushik K. Duttae9234872010-02-12 00:43:24 -0800144
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800145 dir = opendir(directory);
146 if (dir == NULL) {
147 ui_print("Couldn't open directory.\n");
148 return NULL;
149 }
Steve Kondik4123b582010-11-14 03:18:40 -0500150
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800151 int extension_length = 0;
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800152 if (fileExtensionOrDirectory != NULL)
153 extension_length = strlen(fileExtensionOrDirectory);
Steve Kondik4123b582010-11-14 03:18:40 -0500154
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800155 int isCounting = 1;
156 i = 0;
157 for (pass = 0; pass < 2; pass++) {
158 while ((de=readdir(dir)) != NULL) {
159 // skip hidden files
160 if (de->d_name[0] == '.')
161 continue;
Steve Kondik4123b582010-11-14 03:18:40 -0500162
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800163 // NULL means that we are gathering directories, so skip this
164 if (fileExtensionOrDirectory != NULL)
165 {
166 // make sure that we can have the desired extension (prevent seg fault)
167 if (strlen(de->d_name) < extension_length)
168 continue;
169 // compare the extension
170 if (strcmp(de->d_name + strlen(de->d_name) - extension_length, fileExtensionOrDirectory) != 0)
171 continue;
172 }
173 else
174 {
175 struct stat info;
Koushik K. Dutta1d53c4e2010-04-02 16:46:21 -0700176 char fullFileName[PATH_MAX];
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800177 strcpy(fullFileName, directory);
178 strcat(fullFileName, de->d_name);
179 stat(fullFileName, &info);
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800180 // make sure it is a directory
181 if (!(S_ISDIR(info.st_mode)))
182 continue;
183 }
Steve Kondik4123b582010-11-14 03:18:40 -0500184
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800185 if (pass == 0)
186 {
187 total++;
188 continue;
189 }
Steve Kondik4123b582010-11-14 03:18:40 -0500190
Koushik K. Dutta49f56892010-02-25 14:51:05 -0800191 files[i] = (char*) malloc(dirLen + strlen(de->d_name) + 2);
192 strcpy(files[i], directory);
193 strcat(files[i], de->d_name);
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800194 if (fileExtensionOrDirectory == NULL)
195 strcat(files[i], "/");
196 i++;
197 }
198 if (pass == 1)
199 break;
200 if (total == 0)
201 break;
202 rewinddir(dir);
203 *numFiles = total;
204 files = (char**) malloc((total+1)*sizeof(char*));
205 files[total]=NULL;
206 }
Koushik K. Dutta49f56892010-02-25 14:51:05 -0800207
208 if(closedir(dir) < 0) {
209 LOGE("Failed to close directory.");
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800210 }
211
212 if (total==0) {
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800213 return NULL;
214 }
215
Koushik Dutta1e8aaba2010-07-01 00:23:25 -0700216 // sort the result
217 if (files != NULL) {
218 for (i = 0; i < total; i++) {
219 int curMax = -1;
220 int j;
221 for (j = 0; j < total - i; j++) {
222 if (curMax == -1 || strcmp(files[curMax], files[j]) < 0)
223 curMax = j;
224 }
225 char* temp = files[curMax];
226 files[curMax] = files[total - i - 1];
227 files[total - i - 1] = temp;
228 }
229 }
230
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800231 return files;
Koushik K. Dutta49f56892010-02-25 14:51:05 -0800232}
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800233
Koushik K. Dutta49f56892010-02-25 14:51:05 -0800234// pass in NULL for fileExtensionOrDirectory and you will get a directory chooser
235char* choose_file_menu(const char* directory, const char* fileExtensionOrDirectory, const char* headers[])
236{
237 char path[PATH_MAX] = "";
238 DIR *dir;
239 struct dirent *de;
240 int numFiles = 0;
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800241 int numDirs = 0;
Koushik K. Dutta49f56892010-02-25 14:51:05 -0800242 int i;
Koushik K. Dutta1d53c4e2010-04-02 16:46:21 -0700243 char* return_value = NULL;
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800244 int dir_len = strlen(directory);
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800245
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800246 char** files = gather_files(directory, fileExtensionOrDirectory, &numFiles);
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800247 char** dirs = NULL;
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800248 if (fileExtensionOrDirectory != NULL)
249 dirs = gather_files(directory, NULL, &numDirs);
250 int total = numDirs + numFiles;
251 if (total == 0)
252 {
253 ui_print("No files found.\n");
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800254 }
Koushik K. Dutta1d53c4e2010-04-02 16:46:21 -0700255 else
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800256 {
Koushik K. Dutta1d53c4e2010-04-02 16:46:21 -0700257 char** list = (char**) malloc((total + 1) * sizeof(char*));
258 list[total] = NULL;
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800259
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800260
Koushik K. Dutta1d53c4e2010-04-02 16:46:21 -0700261 for (i = 0 ; i < numDirs; i++)
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800262 {
Koushik K. Dutta1d53c4e2010-04-02 16:46:21 -0700263 list[i] = strdup(dirs[i] + dir_len);
264 }
265
266 for (i = 0 ; i < numFiles; i++)
267 {
268 list[numDirs + i] = strdup(files[i] + dir_len);
269 }
270
271 for (;;)
272 {
Koushik Duttadf1e4062010-12-18 17:42:31 -0800273 int chosen_item = get_menu_selection(headers, list, 0, 0);
Koushik K. Dutta1d53c4e2010-04-02 16:46:21 -0700274 if (chosen_item == GO_BACK)
275 break;
276 static char ret[PATH_MAX];
277 if (chosen_item < numDirs)
278 {
279 char* subret = choose_file_menu(dirs[chosen_item], fileExtensionOrDirectory, headers);
280 if (subret != NULL)
281 {
282 strcpy(ret, subret);
283 return_value = ret;
284 break;
285 }
286 continue;
Steve Kondik4123b582010-11-14 03:18:40 -0500287 }
Koushik K. Dutta1d53c4e2010-04-02 16:46:21 -0700288 strcpy(ret, files[chosen_item - numDirs]);
289 return_value = ret;
290 break;
291 }
292 free_string_array(list);
Koushik K. Dutta981b0cd2010-02-22 08:53:34 -0800293 }
Koushik K. Dutta1d53c4e2010-04-02 16:46:21 -0700294
295 free_string_array(files);
296 free_string_array(dirs);
297 return return_value;
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800298}
299
300void show_choose_zip_menu()
301{
Koushik Duttadf1e4062010-12-18 17:42:31 -0800302 if (ensure_path_mounted("/sdcard") != 0) {
Koushik K. Duttae9234872010-02-12 00:43:24 -0800303 LOGE ("Can't mount /sdcard\n");
304 return;
305 }
306
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800307 static char* headers[] = { "Choose a zip to apply",
308 "",
Steve Kondik4123b582010-11-14 03:18:40 -0500309 NULL
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800310 };
Steve Kondik4123b582010-11-14 03:18:40 -0500311
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800312 char* file = choose_file_menu("/sdcard/", ".zip", headers);
313 if (file == NULL)
314 return;
Koushik Duttad63eaef2010-07-14 21:01:21 -0700315 static char* confirm_install = "Confirm install?";
316 static char confirm[PATH_MAX];
317 sprintf(confirm, "Yes - Install %s", basename(file));
318 if (confirm_selection(confirm_install, confirm))
Koushik Duttadf1e4062010-12-18 17:42:31 -0800319 install_zip(file);
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800320}
321
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800322void show_nandroid_restore_menu()
323{
Koushik Duttadf1e4062010-12-18 17:42:31 -0800324 if (ensure_path_mounted("/sdcard") != 0) {
Koushik K. Dutta54305a82010-03-12 17:43:26 -0800325 LOGE ("Can't mount /sdcard\n");
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800326 return;
Koushik K. Dutta54305a82010-03-12 17:43:26 -0800327 }
Steve Kondik4123b582010-11-14 03:18:40 -0500328
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800329 static char* headers[] = { "Choose an image to restore",
330 "",
Steve Kondik4123b582010-11-14 03:18:40 -0500331 NULL
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800332 };
Koushik K. Duttae9234872010-02-12 00:43:24 -0800333
Koushik K. Dutta49f56892010-02-25 14:51:05 -0800334 char* file = choose_file_menu("/sdcard/clockworkmod/backup/", NULL, headers);
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800335 if (file == NULL)
336 return;
Koushik Duttad63eaef2010-07-14 21:01:21 -0700337
338 if (confirm_selection("Confirm restore?", "Yes - Restore"))
339 nandroid_restore(file, 1, 1, 1, 1, 1);
Koushik K. Dutta03173782010-02-26 14:14:23 -0800340}
341
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700342void show_mount_usb_storage_menu()
Koushik K. Dutta03173782010-02-26 14:14:23 -0800343{
Koushik Dutta598cfc72010-06-20 09:42:47 -0700344 char command[PATH_MAX];
Koushik Duttadf1e4062010-12-18 17:42:31 -0800345 Volume *vol = volume_for_path("/sdcard");
346 sprintf(command, "echo %s > /sys/devices/platform/usb_mass_storage/lun0/file", vol->device);
Koushik Dutta598cfc72010-06-20 09:42:47 -0700347 __system(command);
Koushik K. Dutta03173782010-02-26 14:14:23 -0800348 static char* headers[] = { "USB Mass Storage device",
349 "Leaving this menu unmount",
350 "your SD card from your PC.",
351 "",
Steve Kondik4123b582010-11-14 03:18:40 -0500352 NULL
Koushik K. Dutta03173782010-02-26 14:14:23 -0800353 };
Steve Kondik4123b582010-11-14 03:18:40 -0500354
Koushik K. Dutta03173782010-02-26 14:14:23 -0800355 static char* list[] = { "Unmount", NULL };
Steve Kondik4123b582010-11-14 03:18:40 -0500356
Koushik K. Dutta03173782010-02-26 14:14:23 -0800357 for (;;)
358 {
Koushik Duttadf1e4062010-12-18 17:42:31 -0800359 int chosen_item = get_menu_selection(headers, list, 0, 0);
Koushik K. Dutta03173782010-02-26 14:14:23 -0800360 if (chosen_item == GO_BACK || chosen_item == 0)
361 break;
362 }
Steve Kondik4123b582010-11-14 03:18:40 -0500363
Koushik K. Duttaf7215942010-03-16 13:34:51 -0700364 __system("echo '' > /sys/devices/platform/usb_mass_storage/lun0/file");
365 __system("echo 0 > /sys/devices/platform/usb_mass_storage/lun0/enable");
Koushik K. Duttae81cb752010-02-26 17:44:33 -0800366}
Koushik K. Duttaf68aaaf2010-03-07 13:39:21 -0800367
Koushik Duttad63eaef2010-07-14 21:01:21 -0700368int confirm_selection(const char* title, const char* confirm)
Koushik K. Dutta16f0b492010-03-19 14:37:11 -0700369{
Koushik Duttaceddcd52010-08-23 16:13:14 -0700370 struct stat info;
371 if (0 == stat("/sdcard/clockworkmod/.no_confirm", &info))
372 return 1;
373
Koushik Duttad63eaef2010-07-14 21:01:21 -0700374 char* confirm_headers[] = { title, " THIS CAN NOT BE UNDONE.", "", NULL };
375 char* items[] = { "No",
376 "No",
377 "No",
378 "No",
379 "No",
380 "No",
381 "No",
Koushik Duttaecd32fa2010-07-14 18:38:02 -0700382 confirm, //" Yes -- wipe partition", // [7
Koushik Duttad63eaef2010-07-14 21:01:21 -0700383 "No",
384 "No",
385 "No",
Koushik K. Dutta16f0b492010-03-19 14:37:11 -0700386 NULL };
387
Koushik Duttadf1e4062010-12-18 17:42:31 -0800388 int chosen_item = get_menu_selection(confirm_headers, items, 0, 0);
Koushik K. Dutta16f0b492010-03-19 14:37:11 -0700389 return chosen_item == 7;
390}
391
Koushik Duttadf1e4062010-12-18 17:42:31 -0800392int format_unknown_device(const char* path)
Koushik K. Dutta3f995392010-03-30 23:29:43 -0700393{
Koushik K. Duttaf3534d02010-04-18 18:06:24 -0700394 // if this is SDEXT:, don't worry about it.
Koushik Duttadf1e4062010-12-18 17:42:31 -0800395 if (0 == strcmp(path, "/sd-ext"))
Koushik K. Duttaf3534d02010-04-18 18:06:24 -0700396 {
397 struct stat st;
Koushik Duttadf1e4062010-12-18 17:42:31 -0800398 Volume *vol = volume_for_path("/sd-ext");
399 if (vol == NULL || 0 != stat(vol->device, &st))
Koushik K. Duttaf3534d02010-04-18 18:06:24 -0700400 {
401 ui_print("No app2sd partition found. Skipping format of /sd-ext.\n");
402 return 0;
403 }
404 }
Koushik Dutta2f73e582010-04-18 16:00:21 -0700405
Koushik Duttadf1e4062010-12-18 17:42:31 -0800406 if (0 != ensure_path_mounted(path))
Koushik K. Dutta3f995392010-03-30 23:29:43 -0700407 {
408 ui_print("Error mounting %s!\n", path);
Koushik Duttacd44ab92010-06-23 00:02:14 -0700409 ui_print("Skipping format...\n");
410 return 0;
Koushik K. Dutta3f995392010-03-30 23:29:43 -0700411 }
412
413 static char tmp[PATH_MAX];
414 sprintf(tmp, "rm -rf %s/*", path);
415 __system(tmp);
416 sprintf(tmp, "rm -rf %s/.*", path);
417 __system(tmp);
Steve Kondik4123b582010-11-14 03:18:40 -0500418
Koushik Duttadf1e4062010-12-18 17:42:31 -0800419 ensure_path_unmounted(path);
Koushik K. Dutta3f995392010-03-30 23:29:43 -0700420 return 0;
421}
422
Koushik Duttadf1e4062010-12-18 17:42:31 -0800423//#define MOUNTABLE_COUNT 5
424//#define DEVICE_COUNT 4
425//#define MMC_COUNT 2
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700426
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700427void show_partition_menu()
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700428{
Koushik Duttaceddcd52010-08-23 16:13:14 -0700429 static char* headers[] = { "Mounts and Storage Menu",
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700430 "",
Steve Kondik4123b582010-11-14 03:18:40 -0500431 NULL
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700432 };
433
434 typedef char* string;
Koushik Duttadf1e4062010-12-18 17:42:31 -0800435 string mounts[][3] = {
436 { "mount /system", "unmount /system", "/system" },
437 { "mount /data", "unmount /data", "/data" },
438 { "mount /cache", "unmount /cache", "/cache" },
439 { "mount /sdcard", "unmount /sdcard", "/sdcard:" },
Brint E. Kriebeld3522332010-12-11 20:52:06 -0700440#ifdef BOARD_HAS_SDCARD_INTERNAL
Koushik Duttadf1e4062010-12-18 17:42:31 -0800441 { "mount /emmc", "unmount /emmc", "/emmc" },
Brint E. Kriebeld3522332010-12-11 20:52:06 -0700442#endif
Koushik Duttadf1e4062010-12-18 17:42:31 -0800443 { "mount /sd-ext", "unmount /sd-ext", "/sd-ext" }
Koushik K. Dutta3f995392010-03-30 23:29:43 -0700444 };
Steve Kondik4123b582010-11-14 03:18:40 -0500445
Koushik Duttadf1e4062010-12-18 17:42:31 -0800446 string devices[][2] = {
447 { "format boot", "boot:" },
448 { "format system", "/system" },
449 { "format data", "/data" },
450 { "format cache", "/cache" },
451 { "format sdcard", "/sdcard" },
452 { "format sd-ext", "/sd-ext" },
Brint E. Kriebeld3522332010-12-11 20:52:06 -0700453#ifdef BOARD_HAS_SDCARD_INTERNAL
Koushik Duttadf1e4062010-12-18 17:42:31 -0800454 { "format internal sdcard", "/emmc" }
Brint E. Kriebeld3522332010-12-11 20:52:06 -0700455#endif
Koushik K. Dutta16f0b492010-03-19 14:37:11 -0700456 };
Steve Kondik4123b582010-11-14 03:18:40 -0500457
Koushik Duttadf1e4062010-12-18 17:42:31 -0800458 const int MOUNTABLE_COUNT = sizeof(mounts) / sizeof(string) / 3;
459 const int DEVICE_COUNT = sizeof(devices) / sizeof(string) / 2;
460
Koushik Duttad63eaef2010-07-14 21:01:21 -0700461 static char* confirm_format = "Confirm format?";
Koushik Duttaecd32fa2010-07-14 18:38:02 -0700462 static char* confirm = "Yes - Format";
Steve Kondik4123b582010-11-14 03:18:40 -0500463
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700464 for (;;)
465 {
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700466 int ismounted[MOUNTABLE_COUNT];
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700467 int i;
Koushik Duttadf1e4062010-12-18 17:42:31 -0800468 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 -0700469 for (i = 0; i < MOUNTABLE_COUNT; i++)
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700470 {
Koushik Duttadf1e4062010-12-18 17:42:31 -0800471 ismounted[i] = is_path_mounted(mounts[i][2]);
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700472 options[i] = ismounted[i] ? mounts[i][1] : mounts[i][0];
473 }
Steve Kondik4123b582010-11-14 03:18:40 -0500474
Koushik Duttadf1e4062010-12-18 17:42:31 -0800475 for (i = 0; i < DEVICE_COUNT; i++)
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700476 {
Koushik Duttadf1e4062010-12-18 17:42:31 -0800477 options[DEVICE_COUNT + i] = devices[i][0];
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700478 }
Steve Kondik4123b582010-11-14 03:18:40 -0500479
Koushik Duttadf1e4062010-12-18 17:42:31 -0800480 options[MOUNTABLE_COUNT + DEVICE_COUNT] = "mount USB storage";
481 options[MOUNTABLE_COUNT + DEVICE_COUNT + 1] = NULL;
Steve Kondik4123b582010-11-14 03:18:40 -0500482
Koushik Duttadf1e4062010-12-18 17:42:31 -0800483 int chosen_item = get_menu_selection(headers, options, 0, 0);
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700484 if (chosen_item == GO_BACK)
485 break;
Koushik Duttadf1e4062010-12-18 17:42:31 -0800486 if (chosen_item == MOUNTABLE_COUNT + DEVICE_COUNT)
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700487 {
488 show_mount_usb_storage_menu();
489 }
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700490 else if (chosen_item < MOUNTABLE_COUNT)
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700491 {
492 if (ismounted[chosen_item])
493 {
Koushik Duttadf1e4062010-12-18 17:42:31 -0800494 if (0 != ensure_path_unmounted(mounts[chosen_item][2]))
Koushik K. Dutta16f0b492010-03-19 14:37:11 -0700495 ui_print("Error unmounting %s!\n", mounts[chosen_item][2]);
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700496 }
497 else
498 {
Koushik Duttadf1e4062010-12-18 17:42:31 -0800499 if (0 != ensure_path_mounted(mounts[chosen_item][2]))
Koushik K. Dutta16f0b492010-03-19 14:37:11 -0700500 ui_print("Error mounting %s!\n", mounts[chosen_item][2]);
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700501 }
502 }
Koushik Duttadf1e4062010-12-18 17:42:31 -0800503 else if (chosen_item < MOUNTABLE_COUNT + DEVICE_COUNT)
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700504 {
505 chosen_item = chosen_item - MOUNTABLE_COUNT;
Koushik Duttaecd32fa2010-07-14 18:38:02 -0700506 if (!confirm_selection(confirm_format, confirm))
Koushik K. Dutta16f0b492010-03-19 14:37:11 -0700507 continue;
Koushik Duttadf1e4062010-12-18 17:42:31 -0800508 ui_print("Formatting %s...\n", devices[chosen_item][1]);
509 if (0 != format_device(devices[chosen_item][1]))
510 ui_print("Error formatting %s!\n", devices[chosen_item][1]);
Koushik K. Dutta3f995392010-03-30 23:29:43 -0700511 else
512 ui_print("Done.\n");
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700513 }
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700514 }
515}
516
Koushik K. Dutta72a1db62010-03-07 14:10:26 -0800517#define EXTENDEDCOMMAND_SCRIPT "/cache/recovery/extendedcommand"
518
519int extendedcommand_file_exists()
Koushik K. Duttaf68aaaf2010-03-07 13:39:21 -0800520{
Koushik K. Duttaf68aaaf2010-03-07 13:39:21 -0800521 struct stat file_info;
Koushik K. Dutta72a1db62010-03-07 14:10:26 -0800522 return 0 == stat(EXTENDEDCOMMAND_SCRIPT, &file_info);
523}
524
Koushik K. Dutta3836f722010-03-11 22:17:43 -0800525int run_script_from_buffer(char* script_data, int script_len, char* filename)
Koushik K. Dutta72a1db62010-03-07 14:10:26 -0800526{
Koushik Duttadf1e4062010-12-18 17:42:31 -0800527 ui_print("not yet implemented.\n");
528 return -1;
529
530 /*
Koushik K. Duttaf68aaaf2010-03-07 13:39:21 -0800531 const AmCommandList *commands = parseAmendScript(script_data, script_len);
532 if (commands == NULL) {
533 printf("Syntax error in update script\n");
534 return 1;
535 } else {
Koushik K. Dutta72a1db62010-03-07 14:10:26 -0800536 printf("Parsed %.*s\n", script_len, filename);
Koushik K. Duttaf68aaaf2010-03-07 13:39:21 -0800537 }
538
Koushik K. Duttaf68aaaf2010-03-07 13:39:21 -0800539 int ret = execCommandList((ExecContext *)1, commands);
540 if (ret != 0) {
541 int num = ret;
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800542 char *line = NULL, *next = script_data;
Koushik K. Duttaf68aaaf2010-03-07 13:39:21 -0800543 while (next != NULL && ret-- > 0) {
544 line = next;
545 next = memchr(line, '\n', script_data + script_len - line);
546 if (next != NULL) *next++ = '\0';
547 }
548 printf("Failure at line %d:\n%s\n", num, next ? line : "(not found)");
549 return 1;
Steve Kondik4123b582010-11-14 03:18:40 -0500550 }
Koushik Duttadf1e4062010-12-18 17:42:31 -0800551 */
Koushik K. Duttaf68aaaf2010-03-07 13:39:21 -0800552}
Koushik K. Dutta72a1db62010-03-07 14:10:26 -0800553
Koushik K. Dutta6771aca2010-04-03 23:28:39 -0700554int run_script(char* filename)
Koushik K. Dutta3836f722010-03-11 22:17:43 -0800555{
556 struct stat file_info;
557 if (0 != stat(filename, &file_info)) {
558 printf("Error executing stat on file: %s\n", filename);
559 return 1;
560 }
Steve Kondik4123b582010-11-14 03:18:40 -0500561
Koushik K. Dutta3836f722010-03-11 22:17:43 -0800562 int script_len = file_info.st_size;
Koushik K. Dutta707fa6d2010-03-23 11:44:33 -0700563 char* script_data = (char*)malloc(script_len + 1);
Koushik K. Dutta3836f722010-03-11 22:17:43 -0800564 FILE *file = fopen(filename, "rb");
565 fread(script_data, script_len, 1, file);
Koushik K. Dutta707fa6d2010-03-23 11:44:33 -0700566 // supposedly not necessary, but let's be safe.
567 script_data[script_len] = '\0';
Koushik K. Dutta3836f722010-03-11 22:17:43 -0800568 fclose(file);
Koushik Duttae25908b2010-06-21 08:16:19 -0700569 LOGI("Running script:\n");
570 LOGI("\n%s\n", script_data);
Koushik K. Dutta3836f722010-03-11 22:17:43 -0800571
Koushik K. Duttaf3534d02010-04-18 18:06:24 -0700572 int ret = run_script_from_buffer(script_data, script_len, filename);
Koushik K. Dutta1d53c4e2010-04-02 16:46:21 -0700573 free(script_data);
574 return ret;
Koushik K. Dutta3836f722010-03-11 22:17:43 -0800575}
576
Koushik K. Dutta72a1db62010-03-07 14:10:26 -0800577int run_and_remove_extendedcommand()
578{
Koushik K. Dutta6771aca2010-04-03 23:28:39 -0700579 char tmp[PATH_MAX];
580 sprintf(tmp, "cp %s /tmp/%s", EXTENDEDCOMMAND_SCRIPT, basename(EXTENDEDCOMMAND_SCRIPT));
581 __system(tmp);
582 remove(EXTENDEDCOMMAND_SCRIPT);
Koushik K. Dutta3a25cf52010-03-08 19:22:41 -0800583 int i = 0;
584 for (i = 20; i > 0; i--) {
585 ui_print("Waiting for SD Card to mount (%ds)\n", i);
Koushik Duttadf1e4062010-12-18 17:42:31 -0800586 if (ensure_path_mounted("/sdcard") == 0) {
Koushik K. Dutta3a25cf52010-03-08 19:22:41 -0800587 ui_print("SD Card mounted...\n");
588 break;
589 }
590 sleep(1);
591 }
Koushik Dutta92077c12010-07-15 00:10:08 -0700592 remove("/sdcard/clockworkmod/.recoverycheckpoint");
Koushik K. Dutta3a25cf52010-03-08 19:22:41 -0800593 if (i == 0) {
594 ui_print("Timed out waiting for SD card... continuing anyways.");
595 }
Steve Kondik4123b582010-11-14 03:18:40 -0500596
Koushik K. Dutta6771aca2010-04-03 23:28:39 -0700597 sprintf(tmp, "/tmp/%s", basename(EXTENDEDCOMMAND_SCRIPT));
598 return run_script(tmp);
Koushik K. Dutta72a1db62010-03-07 14:10:26 -0800599}
600
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700601void show_nandroid_advanced_restore_menu()
602{
Koushik Duttadf1e4062010-12-18 17:42:31 -0800603 if (ensure_path_mounted("/sdcard") != 0) {
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700604 LOGE ("Can't mount /sdcard\n");
605 return;
606 }
607
608 static char* advancedheaders[] = { "Choose an image to restore",
609 "",
610 "Choose an image to restore",
611 "first. The next menu will",
612 "you more options.",
613 "",
614 NULL
615 };
616
617 char* file = choose_file_menu("/sdcard/clockworkmod/backup/", NULL, advancedheaders);
618 if (file == NULL)
619 return;
620
621 static char* headers[] = { "Nandroid Advanced Restore",
622 "",
623 NULL
624 };
625
626 static char* list[] = { "Restore boot",
627 "Restore system",
628 "Restore data",
629 "Restore cache",
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700630 "Restore sd-ext",
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700631 NULL
632 };
633
Koushik Duttad63eaef2010-07-14 21:01:21 -0700634
635 static char* confirm_restore = "Confirm restore?";
636
Koushik Duttadf1e4062010-12-18 17:42:31 -0800637 int chosen_item = get_menu_selection(headers, list, 0, 0);
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700638 switch (chosen_item)
639 {
640 case 0:
Koushik Duttad63eaef2010-07-14 21:01:21 -0700641 if (confirm_selection(confirm_restore, "Yes - Restore boot"))
642 nandroid_restore(file, 1, 0, 0, 0, 0);
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700643 break;
644 case 1:
Koushik Duttad63eaef2010-07-14 21:01:21 -0700645 if (confirm_selection(confirm_restore, "Yes - Restore system"))
646 nandroid_restore(file, 0, 1, 0, 0, 0);
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700647 break;
648 case 2:
Koushik Duttad63eaef2010-07-14 21:01:21 -0700649 if (confirm_selection(confirm_restore, "Yes - Restore data"))
650 nandroid_restore(file, 0, 0, 1, 0, 0);
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700651 break;
652 case 3:
Koushik Duttad63eaef2010-07-14 21:01:21 -0700653 if (confirm_selection(confirm_restore, "Yes - Restore cache"))
654 nandroid_restore(file, 0, 0, 0, 1, 0);
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700655 break;
Koushik Dutta2f73e582010-04-18 16:00:21 -0700656 case 4:
Koushik Duttad63eaef2010-07-14 21:01:21 -0700657 if (confirm_selection(confirm_restore, "Yes - Restore sd-ext"))
658 nandroid_restore(file, 0, 0, 0, 0, 1);
Koushik Dutta2f73e582010-04-18 16:00:21 -0700659 break;
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700660 }
661}
662
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700663void show_nandroid_menu()
664{
665 static char* headers[] = { "Nandroid",
666 "",
Steve Kondik4123b582010-11-14 03:18:40 -0500667 NULL
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700668 };
669
Steve Kondik4123b582010-11-14 03:18:40 -0500670 static char* list[] = { "Backup",
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700671 "Restore",
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700672 "Advanced Restore",
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700673 NULL
674 };
675
Koushik Duttadf1e4062010-12-18 17:42:31 -0800676 int chosen_item = get_menu_selection(headers, list, 0, 0);
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700677 switch (chosen_item)
678 {
679 case 0:
680 {
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700681 char backup_path[PATH_MAX];
Koushik K. Dutta6a26e7c2010-03-27 15:26:11 -0700682 time_t t = time(NULL);
683 struct tm *tmp = localtime(&t);
684 if (tmp == NULL)
685 {
686 struct timeval tp;
687 gettimeofday(&tp, NULL);
688 sprintf(backup_path, "/sdcard/clockworkmod/backup/%d", tp.tv_sec);
689 }
690 else
691 {
692 strftime(backup_path, sizeof(backup_path), "/sdcard/clockworkmod/backup/%F.%H.%M.%S", tmp);
693 }
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700694 nandroid_backup(backup_path);
695 }
696 break;
697 case 1:
698 show_nandroid_restore_menu();
699 break;
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700700 case 2:
701 show_nandroid_advanced_restore_menu();
702 break;
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700703 }
Koushik K. Duttaa496b512010-03-19 14:51:45 -0700704}
705
Koushik Duttafd1579b2010-05-01 12:46:55 -0700706void wipe_battery_stats()
707{
Koushik Duttadf1e4062010-12-18 17:42:31 -0800708 ensure_path_mounted("/data");
Koushik Duttafd1579b2010-05-01 12:46:55 -0700709 remove("/data/system/batterystats.bin");
Koushik Duttadf1e4062010-12-18 17:42:31 -0800710 ensure_path_unmounted("/data");
Koushik Duttafd1579b2010-05-01 12:46:55 -0700711}
712
Koushik K. Duttaa496b512010-03-19 14:51:45 -0700713void show_advanced_menu()
714{
715 static char* headers[] = { "Advanced and Debugging Menu",
716 "",
717 NULL
718 };
719
Koushik K. Dutta7fe4d7b2010-04-06 23:04:52 -0700720 static char* list[] = { "Reboot Recovery",
Koushik Dutta49af23c2010-06-21 13:45:51 -0700721 "Wipe Dalvik Cache",
Koushik Duttafd1579b2010-05-01 12:46:55 -0700722 "Wipe Battery Stats",
Koushik Dutta598cfc72010-06-20 09:42:47 -0700723 "Report Error",
Koushik K. Dutta7fe4d7b2010-04-06 23:04:52 -0700724 "Key Test",
Koushik Duttae17a78d2010-08-29 12:35:10 -0700725#ifndef BOARD_HAS_SMALL_RECOVERY
Koushik Duttaceddcd52010-08-23 16:13:14 -0700726 "Partition SD Card",
727 "Fix Permissions",
Brint E. Kriebeld3522332010-12-11 20:52:06 -0700728#ifdef BOARD_HAS_SDCARD_INTERNAL
729 "Partition Internal SD Card",
730#endif
Koushik Duttae17a78d2010-08-29 12:35:10 -0700731#endif
Koushik K. Duttaa496b512010-03-19 14:51:45 -0700732 NULL
733 };
734
735 for (;;)
736 {
Koushik Duttadf1e4062010-12-18 17:42:31 -0800737 int chosen_item = get_menu_selection(headers, list, 0, 0);
Koushik K. Duttaa496b512010-03-19 14:51:45 -0700738 if (chosen_item == GO_BACK)
739 break;
740 switch (chosen_item)
741 {
742 case 0:
Koushik K. Dutta7fe4d7b2010-04-06 23:04:52 -0700743 __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, "recovery");
744 break;
745 case 1:
Koushik Duttad63eaef2010-07-14 21:01:21 -0700746 {
Koushik Duttadf1e4062010-12-18 17:42:31 -0800747 if (0 != ensure_path_mounted("/data"))
Koushik Dutta49af23c2010-06-21 13:45:51 -0700748 break;
Koushik Duttadf1e4062010-12-18 17:42:31 -0800749 ensure_path_mounted("/sd-ext");
750 ensure_path_mounted("/cache");
Koushik Dutta6c7745d2010-08-07 12:17:13 -0700751 if (confirm_selection( "Confirm wipe?", "Yes - Wipe Dalvik Cache")) {
Koushik Duttad63eaef2010-07-14 21:01:21 -0700752 __system("rm -r /data/dalvik-cache");
Koushik Dutta6c7745d2010-08-07 12:17:13 -0700753 __system("rm -r /cache/dalvik-cache");
754 __system("rm -r /sd-ext/dalvik-cache");
755 }
Koushik Duttadf1e4062010-12-18 17:42:31 -0800756 ensure_path_unmounted("/data");
Koushik Dutta6c7745d2010-08-07 12:17:13 -0700757 ui_print("Dalvik Cache wiped.\n");
Koushik Duttafd1579b2010-05-01 12:46:55 -0700758 break;
Koushik Duttad63eaef2010-07-14 21:01:21 -0700759 }
Koushik Duttafd1579b2010-05-01 12:46:55 -0700760 case 2:
Koushik Duttad63eaef2010-07-14 21:01:21 -0700761 {
762 if (confirm_selection( "Confirm wipe?", "Yes - Wipe Battery Stats"))
763 wipe_battery_stats();
Koushik Dutta598cfc72010-06-20 09:42:47 -0700764 break;
Koushik Duttad63eaef2010-07-14 21:01:21 -0700765 }
Koushik Dutta598cfc72010-06-20 09:42:47 -0700766 case 3:
Koushik Dutta49af23c2010-06-21 13:45:51 -0700767 handle_failure(1);
768 break;
769 case 4:
Koushik K. Duttaa496b512010-03-19 14:51:45 -0700770 {
771 ui_print("Outputting key codes.\n");
772 ui_print("Go back to end debugging.\n");
773 int key;
774 int action;
775 do
776 {
777 key = ui_wait_key();
778 action = device_handle_key(key, 1);
779 ui_print("Key: %d\n", key);
780 }
781 while (action != GO_BACK);
Koushik Dutta56606a22010-08-23 16:15:33 -0700782 break;
Koushik K. Duttaa496b512010-03-19 14:51:45 -0700783 }
Koushik Duttaf0e31b82010-08-17 16:55:38 -0700784 case 5:
785 {
Koushik Duttaceddcd52010-08-23 16:13:14 -0700786 static char* ext_sizes[] = { "128M",
787 "256M",
788 "512M",
789 "1024M",
Brint E. Kriebel0b233312010-11-14 15:31:17 -0700790 "2048M",
791 "4096M",
Koushik Duttaceddcd52010-08-23 16:13:14 -0700792 NULL };
793
794 static char* swap_sizes[] = { "0M",
795 "32M",
796 "64M",
797 "128M",
798 "256M",
799 NULL };
800
801 static char* ext_headers[] = { "Ext Size", "", NULL };
802 static char* swap_headers[] = { "Swap Size", "", NULL };
803
Koushik Duttadf1e4062010-12-18 17:42:31 -0800804 int ext_size = get_menu_selection(ext_headers, ext_sizes, 0, 0);
Koushik Duttaceddcd52010-08-23 16:13:14 -0700805 if (ext_size == GO_BACK)
806 continue;
Steve Kondik4123b582010-11-14 03:18:40 -0500807
Koushik Duttadf1e4062010-12-18 17:42:31 -0800808 int swap_size = get_menu_selection(swap_headers, swap_sizes, 0, 0);
Koushik Duttaceddcd52010-08-23 16:13:14 -0700809 if (swap_size == GO_BACK)
810 continue;
811
812 char sddevice[256];
Koushik Duttadf1e4062010-12-18 17:42:31 -0800813 Volume *vol = volume_for_path("/sdcard");
814 strcpy(sddevice, vol->device);
Koushik Duttaceddcd52010-08-23 16:13:14 -0700815 // we only want the mmcblk, not the partition
816 sddevice[strlen("/dev/block/mmcblkX")] = NULL;
817 char cmd[PATH_MAX];
818 setenv("SDPATH", sddevice, 1);
819 sprintf(cmd, "sdparted -es %s -ss %s -efs ext3 -s", ext_sizes[ext_size], swap_sizes[swap_size]);
820 ui_print("Partitioning SD Card... please wait...\n");
821 if (0 == __system(cmd))
822 ui_print("Done!\n");
823 else
824 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 -0700825 break;
Koushik Duttaf0e31b82010-08-17 16:55:38 -0700826 }
Koushik Dutta38e8b2b2010-08-17 22:21:53 -0700827 case 6:
828 {
Koushik Duttadf1e4062010-12-18 17:42:31 -0800829 ensure_path_mounted("/system");
830 ensure_path_mounted("/data");
Koushik Duttaceddcd52010-08-23 16:13:14 -0700831 ui_print("Fixing permissions...\n");
832 __system("fix_permissions");
833 ui_print("Done!\n");
Koushik Dutta56606a22010-08-23 16:15:33 -0700834 break;
Koushik Dutta38e8b2b2010-08-17 22:21:53 -0700835 }
Brint E. Kriebeld3522332010-12-11 20:52:06 -0700836 case 7:
837 {
838 static char* ext_sizes[] = { "128M",
839 "256M",
840 "512M",
841 "1024M",
842 "2048M",
843 "4096M",
844 NULL };
845
846 static char* swap_sizes[] = { "0M",
847 "32M",
848 "64M",
849 "128M",
850 "256M",
851 NULL };
852
853 static char* ext_headers[] = { "Data Size", "", NULL };
854 static char* swap_headers[] = { "Swap Size", "", NULL };
855
Koushik Duttadf1e4062010-12-18 17:42:31 -0800856 int ext_size = get_menu_selection(ext_headers, ext_sizes, 0, 0);
Brint E. Kriebeld3522332010-12-11 20:52:06 -0700857 if (ext_size == GO_BACK)
858 continue;
859
860 int swap_size = 0;
861 if (swap_size == GO_BACK)
862 continue;
863
864 char sddevice[256];
Koushik Duttadf1e4062010-12-18 17:42:31 -0800865 Volume *vol = volume_for_path("/emmc");
866 strcpy(sddevice, vol->device);
Brint E. Kriebeld3522332010-12-11 20:52:06 -0700867 // we only want the mmcblk, not the partition
868 sddevice[strlen("/dev/block/mmcblkX")] = NULL;
869 char cmd[PATH_MAX];
870 setenv("SDPATH", sddevice, 1);
871 sprintf(cmd, "sdparted -es %s -ss %s -efs ext3 -s", ext_sizes[ext_size], swap_sizes[swap_size]);
872 ui_print("Partitioning Internal SD Card... please wait...\n");
873 if (0 == __system(cmd))
874 ui_print("Done!\n");
875 else
876 ui_print("An error occured while partitioning your Internal SD Card. Please see /tmp/recovery.log for more details.\n");
877 break;
878 }
Koushik K. Duttaa496b512010-03-19 14:51:45 -0700879 }
880 }
Koushik Dutta14239d22010-06-14 15:02:48 -0700881}
882
Koushik Duttab643e4f2010-12-18 18:39:39 -0800883void write_fstab_root(char *path, FILE *file)
Koushik Dutta14239d22010-06-14 15:02:48 -0700884{
Koushik Duttab643e4f2010-12-18 18:39:39 -0800885 Volume *vol = volume_for_path(path);
886 if (vol == NULL) {
Koushik Dutta9765a032010-12-18 21:31:02 -0800887 LOGW("Unable to get recovery.fstab info for %s during fstab generation!\n", path);
Koushik Dutta598cfc72010-06-20 09:42:47 -0700888 return;
Koushik Duttaa6522b32010-06-20 13:16:06 -0700889 }
Steve Kondik4123b582010-11-14 03:18:40 -0500890
Koushik Duttab643e4f2010-12-18 18:39:39 -0800891 fprintf(file, "%s ", vol->device);
892 fprintf(file, "%s ", path);
893 fprintf(file, "%s rw\n", vol->fs_type);
Koushik Dutta14239d22010-06-14 15:02:48 -0700894}
895
896void create_fstab()
897{
Koushik Duttab643e4f2010-12-18 18:39:39 -0800898 struct stat info;
Koushik Duttacd44ab92010-06-23 00:02:14 -0700899 __system("touch /etc/mtab");
Koushik Dutta14239d22010-06-14 15:02:48 -0700900 FILE *file = fopen("/etc/fstab", "w");
Koushik Duttaa6522b32010-06-20 13:16:06 -0700901 if (file == NULL) {
Koushik Duttab643e4f2010-12-18 18:39:39 -0800902 LOGW("Unable to create /etc/fstab!\n");
Koushik Dutta598cfc72010-06-20 09:42:47 -0700903 return;
Koushik Duttaa6522b32010-06-20 13:16:06 -0700904 }
Koushik Duttab643e4f2010-12-18 18:39:39 -0800905 write_fstab_root("/cache", file);
906 write_fstab_root("/data", file);
Koushik Dutta5460f0c2010-12-18 22:37:49 -0800907 if (has_datadata()) {
908 write_fstab_root("/datadata", file);
909 }
Koushik Duttab643e4f2010-12-18 18:39:39 -0800910 write_fstab_root("/system", file);
911 write_fstab_root("/sdcard", file);
912 write_fstab_root("/sd-ext", file);
Koushik Dutta14239d22010-06-14 15:02:48 -0700913 fclose(file);
Koushik Dutta9765a032010-12-18 21:31:02 -0800914 LOGI("Completed outputting fstab.\n");
Koushik Dutta598cfc72010-06-20 09:42:47 -0700915}
916
917void handle_failure(int ret)
918{
919 if (ret == 0)
920 return;
Koushik Duttadf1e4062010-12-18 17:42:31 -0800921 if (0 != ensure_path_mounted("/sdcard"))
Koushik Dutta598cfc72010-06-20 09:42:47 -0700922 return;
Koushik Duttaa6522b32010-06-20 13:16:06 -0700923 mkdir("/sdcard/clockworkmod", S_IRWXU);
924 __system("cp /tmp/recovery.log /sdcard/clockworkmod/recovery.log");
Koushik Dutta38e8b2b2010-08-17 22:21:53 -0700925 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 -0500926}
Koushik Duttadf1e4062010-12-18 17:42:31 -0800927
928int format_device(const char* device) {
929 if (device == NULL)
930 return -1;
931 if (device[0] == '/') {
932 Volume *vol = volume_for_path(device);
933 if (vol == NULL)
934 return -1;
935 return erase_partition(device, vol->fs_type);
936 }
937 return erase_raw_partition(device);
938}
939
940int is_path_mounted(const char* path) {
941 Volume* v = volume_for_path(path);
942 if (v == NULL) {
943 return 0;
944 }
945 if (strcmp(v->fs_type, "ramdisk") == 0) {
946 // the ramdisk is always mounted.
947 return 1;
948 }
949
950 int result;
951 result = scan_mounted_volumes();
952 if (result < 0) {
953 LOGE("failed to scan mounted volumes\n");
954 return 0;
955 }
956
957 const MountedVolume* mv =
958 find_mounted_volume_by_mount_point(v->mount_point);
959 if (mv) {
960 // volume is already mounted
961 return 1;
962 }
963 return 0;
964}
Koushik Dutta5460f0c2010-12-18 22:37:49 -0800965
966int has_datadata() {
967 Volume *vol = volume_for_path("/datadata");
968 return vol != NULL;
969}