blob: df22448de451c72e8ab431915b84953976ee6da7 [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 K. Duttaee57bbc2010-03-12 23:21:12 -080039
Koushik K. Dutta6060e5c2010-02-11 22:27:06 -080040int signature_check_enabled = 1;
41int script_assert_enabled = 1;
Koushik Duttadf1e4062010-12-18 17:42:31 -080042static const char *SDCARD_UPDATE_FILE = "/sdcard/update.zip";
Koushik K. Dutta6060e5c2010-02-11 22:27:06 -080043
44void
45toggle_signature_check()
46{
47 signature_check_enabled = !signature_check_enabled;
48 ui_print("Signature Check: %s\n", signature_check_enabled ? "Enabled" : "Disabled");
49}
50
51void toggle_script_asserts()
52{
53 script_assert_enabled = !script_assert_enabled;
Koushik K. Duttae9234872010-02-12 00:43:24 -080054 ui_print("Script Asserts: %s\n", script_assert_enabled ? "Enabled" : "Disabled");
55}
56
Koushik K. Duttaea46fe22010-03-08 02:58:04 -080057int install_zip(const char* packagefilepath)
Koushik K. Duttae9234872010-02-12 00:43:24 -080058{
Koushik K. Duttabcdd0032010-02-21 21:10:25 -080059 ui_print("\n-- Installing: %s\n", packagefilepath);
Koushik Dutta5aaa8232010-07-20 16:23:18 -070060#ifndef BOARD_HAS_NO_MISC_PARTITION
Koushik K. Duttabcdd0032010-02-21 21:10:25 -080061 set_sdcard_update_bootloader_message();
Koushik Dutta5aaa8232010-07-20 16:23:18 -070062#endif
Koushik K. Duttabcdd0032010-02-21 21:10:25 -080063 int status = install_package(packagefilepath);
Koushik K. Duttaf3534d02010-04-18 18:06:24 -070064 ui_reset_progress();
Koushik K. Duttabcdd0032010-02-21 21:10:25 -080065 if (status != INSTALL_SUCCESS) {
66 ui_set_background(BACKGROUND_ICON_ERROR);
67 ui_print("Installation aborted.\n");
Koushik K. Duttaea46fe22010-03-08 02:58:04 -080068 return 1;
Steve Kondik4123b582010-11-14 03:18:40 -050069 }
Koushik K. Dutta001c5b52010-02-25 14:53:57 -080070 ui_set_background(BACKGROUND_ICON_NONE);
Koushik K. Dutta99fb6fe2010-03-03 00:42:58 -080071 ui_print("\nInstall from sdcard complete.\n");
Koushik K. Duttaea46fe22010-03-08 02:58:04 -080072 return 0;
Koushik K. Duttabcdd0032010-02-21 21:10:25 -080073}
74
Koushik Duttadf1e4062010-12-18 17:42:31 -080075char* INSTALL_MENU_ITEMS[] = { "apply /sdcard/update.zip",
Koushik K. Duttabcdd0032010-02-21 21:10:25 -080076 "choose zip from sdcard",
77 "toggle signature verification",
78 "toggle script asserts",
79 NULL };
80#define ITEM_APPLY_SDCARD 0
81#define ITEM_CHOOSE_ZIP 1
82#define ITEM_SIG_CHECK 2
83#define ITEM_ASSERTS 3
84
85void show_install_update_menu()
86{
87 static char* headers[] = { "Apply update from .zip file on SD card",
88 "",
Steve Kondik4123b582010-11-14 03:18:40 -050089 NULL
Koushik K. Duttabcdd0032010-02-21 21:10:25 -080090 };
91 for (;;)
92 {
Koushik Duttadf1e4062010-12-18 17:42:31 -080093 int chosen_item = get_menu_selection(headers, INSTALL_MENU_ITEMS, 0, 0);
Koushik K. Duttabcdd0032010-02-21 21:10:25 -080094 switch (chosen_item)
95 {
96 case ITEM_ASSERTS:
97 toggle_script_asserts();
98 break;
99 case ITEM_SIG_CHECK:
100 toggle_signature_check();
101 break;
102 case ITEM_APPLY_SDCARD:
Koushik Duttad63eaef2010-07-14 21:01:21 -0700103 {
104 if (confirm_selection("Confirm install?", "Yes - Install /sdcard/update.zip"))
Koushik Duttadf1e4062010-12-18 17:42:31 -0800105 install_zip(SDCARD_UPDATE_FILE);
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800106 break;
Koushik Duttad63eaef2010-07-14 21:01:21 -0700107 }
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800108 case ITEM_CHOOSE_ZIP:
109 show_choose_zip_menu();
110 break;
111 default:
112 return;
113 }
Steve Kondik4123b582010-11-14 03:18:40 -0500114
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800115 }
116}
117
Koushik K. Dutta1d53c4e2010-04-02 16:46:21 -0700118void free_string_array(char** array)
119{
120 if (array == NULL)
121 return;
122 char* cursor = array[0];
123 int i = 0;
124 while (cursor != NULL)
125 {
126 free(cursor);
127 cursor = array[++i];
128 }
129 free(array);
130}
131
Koushik K. Dutta49f56892010-02-25 14:51:05 -0800132char** gather_files(const char* directory, const char* fileExtensionOrDirectory, int* numFiles)
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800133{
Koushik K. Duttae9234872010-02-12 00:43:24 -0800134 char path[PATH_MAX] = "";
135 DIR *dir;
136 struct dirent *de;
137 int total = 0;
138 int i;
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800139 char** files = NULL;
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800140 int pass;
141 *numFiles = 0;
142 int dirLen = strlen(directory);
Koushik K. Duttae9234872010-02-12 00:43:24 -0800143
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800144 dir = opendir(directory);
145 if (dir == NULL) {
146 ui_print("Couldn't open directory.\n");
147 return NULL;
148 }
Steve Kondik4123b582010-11-14 03:18:40 -0500149
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800150 int extension_length = 0;
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800151 if (fileExtensionOrDirectory != NULL)
152 extension_length = strlen(fileExtensionOrDirectory);
Steve Kondik4123b582010-11-14 03:18:40 -0500153
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800154 int isCounting = 1;
155 i = 0;
156 for (pass = 0; pass < 2; pass++) {
157 while ((de=readdir(dir)) != NULL) {
158 // skip hidden files
159 if (de->d_name[0] == '.')
160 continue;
Steve Kondik4123b582010-11-14 03:18:40 -0500161
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800162 // NULL means that we are gathering directories, so skip this
163 if (fileExtensionOrDirectory != NULL)
164 {
165 // make sure that we can have the desired extension (prevent seg fault)
166 if (strlen(de->d_name) < extension_length)
167 continue;
168 // compare the extension
169 if (strcmp(de->d_name + strlen(de->d_name) - extension_length, fileExtensionOrDirectory) != 0)
170 continue;
171 }
172 else
173 {
174 struct stat info;
Koushik K. Dutta1d53c4e2010-04-02 16:46:21 -0700175 char fullFileName[PATH_MAX];
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800176 strcpy(fullFileName, directory);
177 strcat(fullFileName, de->d_name);
178 stat(fullFileName, &info);
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800179 // make sure it is a directory
180 if (!(S_ISDIR(info.st_mode)))
181 continue;
182 }
Steve Kondik4123b582010-11-14 03:18:40 -0500183
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800184 if (pass == 0)
185 {
186 total++;
187 continue;
188 }
Steve Kondik4123b582010-11-14 03:18:40 -0500189
Koushik K. Dutta49f56892010-02-25 14:51:05 -0800190 files[i] = (char*) malloc(dirLen + strlen(de->d_name) + 2);
191 strcpy(files[i], directory);
192 strcat(files[i], de->d_name);
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800193 if (fileExtensionOrDirectory == NULL)
194 strcat(files[i], "/");
195 i++;
196 }
197 if (pass == 1)
198 break;
199 if (total == 0)
200 break;
201 rewinddir(dir);
202 *numFiles = total;
203 files = (char**) malloc((total+1)*sizeof(char*));
204 files[total]=NULL;
205 }
Koushik K. Dutta49f56892010-02-25 14:51:05 -0800206
207 if(closedir(dir) < 0) {
208 LOGE("Failed to close directory.");
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800209 }
210
211 if (total==0) {
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800212 return NULL;
213 }
214
Koushik Dutta1e8aaba2010-07-01 00:23:25 -0700215 // sort the result
216 if (files != NULL) {
217 for (i = 0; i < total; i++) {
218 int curMax = -1;
219 int j;
220 for (j = 0; j < total - i; j++) {
221 if (curMax == -1 || strcmp(files[curMax], files[j]) < 0)
222 curMax = j;
223 }
224 char* temp = files[curMax];
225 files[curMax] = files[total - i - 1];
226 files[total - i - 1] = temp;
227 }
228 }
229
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800230 return files;
Koushik K. Dutta49f56892010-02-25 14:51:05 -0800231}
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800232
Koushik K. Dutta49f56892010-02-25 14:51:05 -0800233// pass in NULL for fileExtensionOrDirectory and you will get a directory chooser
234char* choose_file_menu(const char* directory, const char* fileExtensionOrDirectory, const char* headers[])
235{
236 char path[PATH_MAX] = "";
237 DIR *dir;
238 struct dirent *de;
239 int numFiles = 0;
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800240 int numDirs = 0;
Koushik K. Dutta49f56892010-02-25 14:51:05 -0800241 int i;
Koushik K. Dutta1d53c4e2010-04-02 16:46:21 -0700242 char* return_value = NULL;
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800243 int dir_len = strlen(directory);
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800244
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800245 char** files = gather_files(directory, fileExtensionOrDirectory, &numFiles);
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800246 char** dirs = NULL;
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800247 if (fileExtensionOrDirectory != NULL)
248 dirs = gather_files(directory, NULL, &numDirs);
249 int total = numDirs + numFiles;
250 if (total == 0)
251 {
252 ui_print("No files found.\n");
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800253 }
Koushik K. Dutta1d53c4e2010-04-02 16:46:21 -0700254 else
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800255 {
Koushik K. Dutta1d53c4e2010-04-02 16:46:21 -0700256 char** list = (char**) malloc((total + 1) * sizeof(char*));
257 list[total] = NULL;
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800258
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800259
Koushik K. Dutta1d53c4e2010-04-02 16:46:21 -0700260 for (i = 0 ; i < numDirs; i++)
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800261 {
Koushik K. Dutta1d53c4e2010-04-02 16:46:21 -0700262 list[i] = strdup(dirs[i] + dir_len);
263 }
264
265 for (i = 0 ; i < numFiles; i++)
266 {
267 list[numDirs + i] = strdup(files[i] + dir_len);
268 }
269
270 for (;;)
271 {
Koushik Duttadf1e4062010-12-18 17:42:31 -0800272 int chosen_item = get_menu_selection(headers, list, 0, 0);
Koushik K. Dutta1d53c4e2010-04-02 16:46:21 -0700273 if (chosen_item == GO_BACK)
274 break;
275 static char ret[PATH_MAX];
276 if (chosen_item < numDirs)
277 {
278 char* subret = choose_file_menu(dirs[chosen_item], fileExtensionOrDirectory, headers);
279 if (subret != NULL)
280 {
281 strcpy(ret, subret);
282 return_value = ret;
283 break;
284 }
285 continue;
Steve Kondik4123b582010-11-14 03:18:40 -0500286 }
Koushik K. Dutta1d53c4e2010-04-02 16:46:21 -0700287 strcpy(ret, files[chosen_item - numDirs]);
288 return_value = ret;
289 break;
290 }
291 free_string_array(list);
Koushik K. Dutta981b0cd2010-02-22 08:53:34 -0800292 }
Koushik K. Dutta1d53c4e2010-04-02 16:46:21 -0700293
294 free_string_array(files);
295 free_string_array(dirs);
296 return return_value;
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800297}
298
299void show_choose_zip_menu()
300{
Koushik Duttadf1e4062010-12-18 17:42:31 -0800301 if (ensure_path_mounted("/sdcard") != 0) {
Koushik K. Duttae9234872010-02-12 00:43:24 -0800302 LOGE ("Can't mount /sdcard\n");
303 return;
304 }
305
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800306 static char* headers[] = { "Choose a zip to apply",
307 "",
Steve Kondik4123b582010-11-14 03:18:40 -0500308 NULL
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800309 };
Steve Kondik4123b582010-11-14 03:18:40 -0500310
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800311 char* file = choose_file_menu("/sdcard/", ".zip", headers);
312 if (file == NULL)
313 return;
Koushik Duttad63eaef2010-07-14 21:01:21 -0700314 static char* confirm_install = "Confirm install?";
315 static char confirm[PATH_MAX];
316 sprintf(confirm, "Yes - Install %s", basename(file));
317 if (confirm_selection(confirm_install, confirm))
Koushik Duttadf1e4062010-12-18 17:42:31 -0800318 install_zip(file);
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800319}
320
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800321void show_nandroid_restore_menu()
322{
Koushik Duttadf1e4062010-12-18 17:42:31 -0800323 if (ensure_path_mounted("/sdcard") != 0) {
Koushik K. Dutta54305a82010-03-12 17:43:26 -0800324 LOGE ("Can't mount /sdcard\n");
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800325 return;
Koushik K. Dutta54305a82010-03-12 17:43:26 -0800326 }
Steve Kondik4123b582010-11-14 03:18:40 -0500327
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800328 static char* headers[] = { "Choose an image to restore",
329 "",
Steve Kondik4123b582010-11-14 03:18:40 -0500330 NULL
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800331 };
Koushik K. Duttae9234872010-02-12 00:43:24 -0800332
Koushik K. Dutta49f56892010-02-25 14:51:05 -0800333 char* file = choose_file_menu("/sdcard/clockworkmod/backup/", NULL, headers);
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800334 if (file == NULL)
335 return;
Koushik Duttad63eaef2010-07-14 21:01:21 -0700336
337 if (confirm_selection("Confirm restore?", "Yes - Restore"))
338 nandroid_restore(file, 1, 1, 1, 1, 1);
Koushik K. Dutta03173782010-02-26 14:14:23 -0800339}
340
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700341void show_mount_usb_storage_menu()
Koushik K. Dutta03173782010-02-26 14:14:23 -0800342{
Koushik Dutta598cfc72010-06-20 09:42:47 -0700343 char command[PATH_MAX];
Koushik Duttadf1e4062010-12-18 17:42:31 -0800344 Volume *vol = volume_for_path("/sdcard");
345 sprintf(command, "echo %s > /sys/devices/platform/usb_mass_storage/lun0/file", vol->device);
Koushik Dutta598cfc72010-06-20 09:42:47 -0700346 __system(command);
Koushik K. Dutta03173782010-02-26 14:14:23 -0800347 static char* headers[] = { "USB Mass Storage device",
348 "Leaving this menu unmount",
349 "your SD card from your PC.",
350 "",
Steve Kondik4123b582010-11-14 03:18:40 -0500351 NULL
Koushik K. Dutta03173782010-02-26 14:14:23 -0800352 };
Steve Kondik4123b582010-11-14 03:18:40 -0500353
Koushik K. Dutta03173782010-02-26 14:14:23 -0800354 static char* list[] = { "Unmount", NULL };
Steve Kondik4123b582010-11-14 03:18:40 -0500355
Koushik K. Dutta03173782010-02-26 14:14:23 -0800356 for (;;)
357 {
Koushik Duttadf1e4062010-12-18 17:42:31 -0800358 int chosen_item = get_menu_selection(headers, list, 0, 0);
Koushik K. Dutta03173782010-02-26 14:14:23 -0800359 if (chosen_item == GO_BACK || chosen_item == 0)
360 break;
361 }
Steve Kondik4123b582010-11-14 03:18:40 -0500362
Koushik K. Duttaf7215942010-03-16 13:34:51 -0700363 __system("echo '' > /sys/devices/platform/usb_mass_storage/lun0/file");
364 __system("echo 0 > /sys/devices/platform/usb_mass_storage/lun0/enable");
Koushik K. Duttae81cb752010-02-26 17:44:33 -0800365}
Koushik K. Duttaf68aaaf2010-03-07 13:39:21 -0800366
Koushik Duttad63eaef2010-07-14 21:01:21 -0700367int confirm_selection(const char* title, const char* confirm)
Koushik K. Dutta16f0b492010-03-19 14:37:11 -0700368{
Koushik Duttaceddcd52010-08-23 16:13:14 -0700369 struct stat info;
370 if (0 == stat("/sdcard/clockworkmod/.no_confirm", &info))
371 return 1;
372
Koushik Duttad63eaef2010-07-14 21:01:21 -0700373 char* confirm_headers[] = { title, " THIS CAN NOT BE UNDONE.", "", NULL };
374 char* items[] = { "No",
375 "No",
376 "No",
377 "No",
378 "No",
379 "No",
380 "No",
Koushik Duttaecd32fa2010-07-14 18:38:02 -0700381 confirm, //" Yes -- wipe partition", // [7
Koushik Duttad63eaef2010-07-14 21:01:21 -0700382 "No",
383 "No",
384 "No",
Koushik K. Dutta16f0b492010-03-19 14:37:11 -0700385 NULL };
386
Koushik Duttadf1e4062010-12-18 17:42:31 -0800387 int chosen_item = get_menu_selection(confirm_headers, items, 0, 0);
Koushik K. Dutta16f0b492010-03-19 14:37:11 -0700388 return chosen_item == 7;
389}
390
Koushik Duttadf1e4062010-12-18 17:42:31 -0800391int format_unknown_device(const char* path)
Koushik K. Dutta3f995392010-03-30 23:29:43 -0700392{
Koushik K. Duttaf3534d02010-04-18 18:06:24 -0700393 // if this is SDEXT:, don't worry about it.
Koushik Duttadf1e4062010-12-18 17:42:31 -0800394 if (0 == strcmp(path, "/sd-ext"))
Koushik K. Duttaf3534d02010-04-18 18:06:24 -0700395 {
396 struct stat st;
Koushik Duttadf1e4062010-12-18 17:42:31 -0800397 Volume *vol = volume_for_path("/sd-ext");
398 if (vol == NULL || 0 != stat(vol->device, &st))
Koushik K. Duttaf3534d02010-04-18 18:06:24 -0700399 {
400 ui_print("No app2sd partition found. Skipping format of /sd-ext.\n");
401 return 0;
402 }
403 }
Koushik Dutta2f73e582010-04-18 16:00:21 -0700404
Koushik Duttadf1e4062010-12-18 17:42:31 -0800405 if (0 != ensure_path_mounted(path))
Koushik K. Dutta3f995392010-03-30 23:29:43 -0700406 {
407 ui_print("Error mounting %s!\n", path);
Koushik Duttacd44ab92010-06-23 00:02:14 -0700408 ui_print("Skipping format...\n");
409 return 0;
Koushik K. Dutta3f995392010-03-30 23:29:43 -0700410 }
411
412 static char tmp[PATH_MAX];
413 sprintf(tmp, "rm -rf %s/*", path);
414 __system(tmp);
415 sprintf(tmp, "rm -rf %s/.*", path);
416 __system(tmp);
Steve Kondik4123b582010-11-14 03:18:40 -0500417
Koushik Duttadf1e4062010-12-18 17:42:31 -0800418 ensure_path_unmounted(path);
Koushik K. Dutta3f995392010-03-30 23:29:43 -0700419 return 0;
420}
421
Koushik Duttadf1e4062010-12-18 17:42:31 -0800422//#define MOUNTABLE_COUNT 5
423//#define DEVICE_COUNT 4
424//#define MMC_COUNT 2
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700425
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700426void show_partition_menu()
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700427{
Koushik Duttaceddcd52010-08-23 16:13:14 -0700428 static char* headers[] = { "Mounts and Storage Menu",
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700429 "",
Steve Kondik4123b582010-11-14 03:18:40 -0500430 NULL
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700431 };
432
433 typedef char* string;
Koushik Duttadf1e4062010-12-18 17:42:31 -0800434 string mounts[][3] = {
435 { "mount /system", "unmount /system", "/system" },
436 { "mount /data", "unmount /data", "/data" },
437 { "mount /cache", "unmount /cache", "/cache" },
438 { "mount /sdcard", "unmount /sdcard", "/sdcard:" },
Brint E. Kriebeld3522332010-12-11 20:52:06 -0700439#ifdef BOARD_HAS_SDCARD_INTERNAL
Koushik Duttadf1e4062010-12-18 17:42:31 -0800440 { "mount /emmc", "unmount /emmc", "/emmc" },
Brint E. Kriebeld3522332010-12-11 20:52:06 -0700441#endif
Koushik Duttadf1e4062010-12-18 17:42:31 -0800442 { "mount /sd-ext", "unmount /sd-ext", "/sd-ext" }
Koushik K. Dutta3f995392010-03-30 23:29:43 -0700443 };
Steve Kondik4123b582010-11-14 03:18:40 -0500444
Koushik Duttadf1e4062010-12-18 17:42:31 -0800445 string devices[][2] = {
446 { "format boot", "boot:" },
447 { "format system", "/system" },
448 { "format data", "/data" },
449 { "format cache", "/cache" },
450 { "format sdcard", "/sdcard" },
451 { "format sd-ext", "/sd-ext" },
Brint E. Kriebeld3522332010-12-11 20:52:06 -0700452#ifdef BOARD_HAS_SDCARD_INTERNAL
Koushik Duttadf1e4062010-12-18 17:42:31 -0800453 { "format internal sdcard", "/emmc" }
Brint E. Kriebeld3522332010-12-11 20:52:06 -0700454#endif
Koushik K. Dutta16f0b492010-03-19 14:37:11 -0700455 };
Steve Kondik4123b582010-11-14 03:18:40 -0500456
Koushik Duttadf1e4062010-12-18 17:42:31 -0800457 const int MOUNTABLE_COUNT = sizeof(mounts) / sizeof(string) / 3;
458 const int DEVICE_COUNT = sizeof(devices) / sizeof(string) / 2;
459
Koushik Duttad63eaef2010-07-14 21:01:21 -0700460 static char* confirm_format = "Confirm format?";
Koushik Duttaecd32fa2010-07-14 18:38:02 -0700461 static char* confirm = "Yes - Format";
Steve Kondik4123b582010-11-14 03:18:40 -0500462
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700463 for (;;)
464 {
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700465 int ismounted[MOUNTABLE_COUNT];
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700466 int i;
Koushik Duttadf1e4062010-12-18 17:42:31 -0800467 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 -0700468 for (i = 0; i < MOUNTABLE_COUNT; i++)
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700469 {
Koushik Duttadf1e4062010-12-18 17:42:31 -0800470 ismounted[i] = is_path_mounted(mounts[i][2]);
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700471 options[i] = ismounted[i] ? mounts[i][1] : mounts[i][0];
472 }
Steve Kondik4123b582010-11-14 03:18:40 -0500473
Koushik Duttadf1e4062010-12-18 17:42:31 -0800474 for (i = 0; i < DEVICE_COUNT; i++)
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700475 {
Koushik Duttadf1e4062010-12-18 17:42:31 -0800476 options[DEVICE_COUNT + i] = devices[i][0];
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700477 }
Steve Kondik4123b582010-11-14 03:18:40 -0500478
Koushik Duttadf1e4062010-12-18 17:42:31 -0800479 options[MOUNTABLE_COUNT + DEVICE_COUNT] = "mount USB storage";
480 options[MOUNTABLE_COUNT + DEVICE_COUNT + 1] = NULL;
Steve Kondik4123b582010-11-14 03:18:40 -0500481
Koushik Duttadf1e4062010-12-18 17:42:31 -0800482 int chosen_item = get_menu_selection(headers, options, 0, 0);
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700483 if (chosen_item == GO_BACK)
484 break;
Koushik Duttadf1e4062010-12-18 17:42:31 -0800485 if (chosen_item == MOUNTABLE_COUNT + DEVICE_COUNT)
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700486 {
487 show_mount_usb_storage_menu();
488 }
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700489 else if (chosen_item < MOUNTABLE_COUNT)
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700490 {
491 if (ismounted[chosen_item])
492 {
Koushik Duttadf1e4062010-12-18 17:42:31 -0800493 if (0 != ensure_path_unmounted(mounts[chosen_item][2]))
Koushik K. Dutta16f0b492010-03-19 14:37:11 -0700494 ui_print("Error unmounting %s!\n", mounts[chosen_item][2]);
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700495 }
496 else
497 {
Koushik Duttadf1e4062010-12-18 17:42:31 -0800498 if (0 != ensure_path_mounted(mounts[chosen_item][2]))
Koushik K. Dutta16f0b492010-03-19 14:37:11 -0700499 ui_print("Error mounting %s!\n", mounts[chosen_item][2]);
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700500 }
501 }
Koushik Duttadf1e4062010-12-18 17:42:31 -0800502 else if (chosen_item < MOUNTABLE_COUNT + DEVICE_COUNT)
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700503 {
504 chosen_item = chosen_item - MOUNTABLE_COUNT;
Koushik Duttaecd32fa2010-07-14 18:38:02 -0700505 if (!confirm_selection(confirm_format, confirm))
Koushik K. Dutta16f0b492010-03-19 14:37:11 -0700506 continue;
Koushik Duttadf1e4062010-12-18 17:42:31 -0800507 ui_print("Formatting %s...\n", devices[chosen_item][1]);
508 if (0 != format_device(devices[chosen_item][1]))
509 ui_print("Error formatting %s!\n", devices[chosen_item][1]);
Koushik K. Dutta3f995392010-03-30 23:29:43 -0700510 else
511 ui_print("Done.\n");
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700512 }
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700513 }
514}
515
Koushik K. Dutta72a1db62010-03-07 14:10:26 -0800516#define EXTENDEDCOMMAND_SCRIPT "/cache/recovery/extendedcommand"
517
518int extendedcommand_file_exists()
Koushik K. Duttaf68aaaf2010-03-07 13:39:21 -0800519{
Koushik K. Duttaf68aaaf2010-03-07 13:39:21 -0800520 struct stat file_info;
Koushik K. Dutta72a1db62010-03-07 14:10:26 -0800521 return 0 == stat(EXTENDEDCOMMAND_SCRIPT, &file_info);
522}
523
Koushik K. Dutta3836f722010-03-11 22:17:43 -0800524int run_script_from_buffer(char* script_data, int script_len, char* filename)
Koushik K. Dutta72a1db62010-03-07 14:10:26 -0800525{
Koushik Duttadf1e4062010-12-18 17:42:31 -0800526 ui_print("not yet implemented.\n");
527 return -1;
528
529 /*
Koushik K. Duttaf68aaaf2010-03-07 13:39:21 -0800530 const AmCommandList *commands = parseAmendScript(script_data, script_len);
531 if (commands == NULL) {
532 printf("Syntax error in update script\n");
533 return 1;
534 } else {
Koushik K. Dutta72a1db62010-03-07 14:10:26 -0800535 printf("Parsed %.*s\n", script_len, filename);
Koushik K. Duttaf68aaaf2010-03-07 13:39:21 -0800536 }
537
Koushik K. Duttaf68aaaf2010-03-07 13:39:21 -0800538 int ret = execCommandList((ExecContext *)1, commands);
539 if (ret != 0) {
540 int num = ret;
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800541 char *line = NULL, *next = script_data;
Koushik K. Duttaf68aaaf2010-03-07 13:39:21 -0800542 while (next != NULL && ret-- > 0) {
543 line = next;
544 next = memchr(line, '\n', script_data + script_len - line);
545 if (next != NULL) *next++ = '\0';
546 }
547 printf("Failure at line %d:\n%s\n", num, next ? line : "(not found)");
548 return 1;
Steve Kondik4123b582010-11-14 03:18:40 -0500549 }
Koushik Duttadf1e4062010-12-18 17:42:31 -0800550 */
Koushik K. Duttaf68aaaf2010-03-07 13:39:21 -0800551}
Koushik K. Dutta72a1db62010-03-07 14:10:26 -0800552
Koushik K. Dutta6771aca2010-04-03 23:28:39 -0700553int run_script(char* filename)
Koushik K. Dutta3836f722010-03-11 22:17:43 -0800554{
555 struct stat file_info;
556 if (0 != stat(filename, &file_info)) {
557 printf("Error executing stat on file: %s\n", filename);
558 return 1;
559 }
Steve Kondik4123b582010-11-14 03:18:40 -0500560
Koushik K. Dutta3836f722010-03-11 22:17:43 -0800561 int script_len = file_info.st_size;
Koushik K. Dutta707fa6d2010-03-23 11:44:33 -0700562 char* script_data = (char*)malloc(script_len + 1);
Koushik K. Dutta3836f722010-03-11 22:17:43 -0800563 FILE *file = fopen(filename, "rb");
564 fread(script_data, script_len, 1, file);
Koushik K. Dutta707fa6d2010-03-23 11:44:33 -0700565 // supposedly not necessary, but let's be safe.
566 script_data[script_len] = '\0';
Koushik K. Dutta3836f722010-03-11 22:17:43 -0800567 fclose(file);
Koushik Duttae25908b2010-06-21 08:16:19 -0700568 LOGI("Running script:\n");
569 LOGI("\n%s\n", script_data);
Koushik K. Dutta3836f722010-03-11 22:17:43 -0800570
Koushik K. Duttaf3534d02010-04-18 18:06:24 -0700571 int ret = run_script_from_buffer(script_data, script_len, filename);
Koushik K. Dutta1d53c4e2010-04-02 16:46:21 -0700572 free(script_data);
573 return ret;
Koushik K. Dutta3836f722010-03-11 22:17:43 -0800574}
575
Koushik K. Dutta72a1db62010-03-07 14:10:26 -0800576int run_and_remove_extendedcommand()
577{
Koushik K. Dutta6771aca2010-04-03 23:28:39 -0700578 char tmp[PATH_MAX];
579 sprintf(tmp, "cp %s /tmp/%s", EXTENDEDCOMMAND_SCRIPT, basename(EXTENDEDCOMMAND_SCRIPT));
580 __system(tmp);
581 remove(EXTENDEDCOMMAND_SCRIPT);
Koushik K. Dutta3a25cf52010-03-08 19:22:41 -0800582 int i = 0;
583 for (i = 20; i > 0; i--) {
584 ui_print("Waiting for SD Card to mount (%ds)\n", i);
Koushik Duttadf1e4062010-12-18 17:42:31 -0800585 if (ensure_path_mounted("/sdcard") == 0) {
Koushik K. Dutta3a25cf52010-03-08 19:22:41 -0800586 ui_print("SD Card mounted...\n");
587 break;
588 }
589 sleep(1);
590 }
Koushik Dutta92077c12010-07-15 00:10:08 -0700591 remove("/sdcard/clockworkmod/.recoverycheckpoint");
Koushik K. Dutta3a25cf52010-03-08 19:22:41 -0800592 if (i == 0) {
593 ui_print("Timed out waiting for SD card... continuing anyways.");
594 }
Steve Kondik4123b582010-11-14 03:18:40 -0500595
Koushik K. Dutta6771aca2010-04-03 23:28:39 -0700596 sprintf(tmp, "/tmp/%s", basename(EXTENDEDCOMMAND_SCRIPT));
597 return run_script(tmp);
Koushik K. Dutta72a1db62010-03-07 14:10:26 -0800598}
599
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700600void show_nandroid_advanced_restore_menu()
601{
Koushik Duttadf1e4062010-12-18 17:42:31 -0800602 if (ensure_path_mounted("/sdcard") != 0) {
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700603 LOGE ("Can't mount /sdcard\n");
604 return;
605 }
606
607 static char* advancedheaders[] = { "Choose an image to restore",
608 "",
609 "Choose an image to restore",
610 "first. The next menu will",
611 "you more options.",
612 "",
613 NULL
614 };
615
616 char* file = choose_file_menu("/sdcard/clockworkmod/backup/", NULL, advancedheaders);
617 if (file == NULL)
618 return;
619
620 static char* headers[] = { "Nandroid Advanced Restore",
621 "",
622 NULL
623 };
624
625 static char* list[] = { "Restore boot",
626 "Restore system",
627 "Restore data",
628 "Restore cache",
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700629 "Restore sd-ext",
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700630 NULL
631 };
632
Koushik Duttad63eaef2010-07-14 21:01:21 -0700633
634 static char* confirm_restore = "Confirm restore?";
635
Koushik Duttadf1e4062010-12-18 17:42:31 -0800636 int chosen_item = get_menu_selection(headers, list, 0, 0);
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700637 switch (chosen_item)
638 {
639 case 0:
Koushik Duttad63eaef2010-07-14 21:01:21 -0700640 if (confirm_selection(confirm_restore, "Yes - Restore boot"))
641 nandroid_restore(file, 1, 0, 0, 0, 0);
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700642 break;
643 case 1:
Koushik Duttad63eaef2010-07-14 21:01:21 -0700644 if (confirm_selection(confirm_restore, "Yes - Restore system"))
645 nandroid_restore(file, 0, 1, 0, 0, 0);
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700646 break;
647 case 2:
Koushik Duttad63eaef2010-07-14 21:01:21 -0700648 if (confirm_selection(confirm_restore, "Yes - Restore data"))
649 nandroid_restore(file, 0, 0, 1, 0, 0);
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700650 break;
651 case 3:
Koushik Duttad63eaef2010-07-14 21:01:21 -0700652 if (confirm_selection(confirm_restore, "Yes - Restore cache"))
653 nandroid_restore(file, 0, 0, 0, 1, 0);
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700654 break;
Koushik Dutta2f73e582010-04-18 16:00:21 -0700655 case 4:
Koushik Duttad63eaef2010-07-14 21:01:21 -0700656 if (confirm_selection(confirm_restore, "Yes - Restore sd-ext"))
657 nandroid_restore(file, 0, 0, 0, 0, 1);
Koushik Dutta2f73e582010-04-18 16:00:21 -0700658 break;
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700659 }
660}
661
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700662void show_nandroid_menu()
663{
664 static char* headers[] = { "Nandroid",
665 "",
Steve Kondik4123b582010-11-14 03:18:40 -0500666 NULL
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700667 };
668
Steve Kondik4123b582010-11-14 03:18:40 -0500669 static char* list[] = { "Backup",
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700670 "Restore",
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700671 "Advanced Restore",
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700672 NULL
673 };
674
Koushik Duttadf1e4062010-12-18 17:42:31 -0800675 int chosen_item = get_menu_selection(headers, list, 0, 0);
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700676 switch (chosen_item)
677 {
678 case 0:
679 {
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700680 char backup_path[PATH_MAX];
Koushik K. Dutta6a26e7c2010-03-27 15:26:11 -0700681 time_t t = time(NULL);
682 struct tm *tmp = localtime(&t);
683 if (tmp == NULL)
684 {
685 struct timeval tp;
686 gettimeofday(&tp, NULL);
687 sprintf(backup_path, "/sdcard/clockworkmod/backup/%d", tp.tv_sec);
688 }
689 else
690 {
691 strftime(backup_path, sizeof(backup_path), "/sdcard/clockworkmod/backup/%F.%H.%M.%S", tmp);
692 }
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700693 nandroid_backup(backup_path);
694 }
695 break;
696 case 1:
697 show_nandroid_restore_menu();
698 break;
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700699 case 2:
700 show_nandroid_advanced_restore_menu();
701 break;
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700702 }
Koushik K. Duttaa496b512010-03-19 14:51:45 -0700703}
704
Koushik Duttafd1579b2010-05-01 12:46:55 -0700705void wipe_battery_stats()
706{
Koushik Duttadf1e4062010-12-18 17:42:31 -0800707 ensure_path_mounted("/data");
Koushik Duttafd1579b2010-05-01 12:46:55 -0700708 remove("/data/system/batterystats.bin");
Koushik Duttadf1e4062010-12-18 17:42:31 -0800709 ensure_path_unmounted("/data");
Koushik Duttafd1579b2010-05-01 12:46:55 -0700710}
711
Koushik K. Duttaa496b512010-03-19 14:51:45 -0700712void show_advanced_menu()
713{
714 static char* headers[] = { "Advanced and Debugging Menu",
715 "",
716 NULL
717 };
718
Koushik K. Dutta7fe4d7b2010-04-06 23:04:52 -0700719 static char* list[] = { "Reboot Recovery",
Koushik Dutta49af23c2010-06-21 13:45:51 -0700720 "Wipe Dalvik Cache",
Koushik Duttafd1579b2010-05-01 12:46:55 -0700721 "Wipe Battery Stats",
Koushik Dutta598cfc72010-06-20 09:42:47 -0700722 "Report Error",
Koushik K. Dutta7fe4d7b2010-04-06 23:04:52 -0700723 "Key Test",
Koushik Duttae17a78d2010-08-29 12:35:10 -0700724#ifndef BOARD_HAS_SMALL_RECOVERY
Koushik Duttaceddcd52010-08-23 16:13:14 -0700725 "Partition SD Card",
726 "Fix Permissions",
Brint E. Kriebeld3522332010-12-11 20:52:06 -0700727#ifdef BOARD_HAS_SDCARD_INTERNAL
728 "Partition Internal SD Card",
729#endif
Koushik Duttae17a78d2010-08-29 12:35:10 -0700730#endif
Koushik K. Duttaa496b512010-03-19 14:51:45 -0700731 NULL
732 };
733
734 for (;;)
735 {
Koushik Duttadf1e4062010-12-18 17:42:31 -0800736 int chosen_item = get_menu_selection(headers, list, 0, 0);
Koushik K. Duttaa496b512010-03-19 14:51:45 -0700737 if (chosen_item == GO_BACK)
738 break;
739 switch (chosen_item)
740 {
741 case 0:
Koushik K. Dutta7fe4d7b2010-04-06 23:04:52 -0700742 __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, "recovery");
743 break;
744 case 1:
Koushik Duttad63eaef2010-07-14 21:01:21 -0700745 {
Koushik Duttadf1e4062010-12-18 17:42:31 -0800746 if (0 != ensure_path_mounted("/data"))
Koushik Dutta49af23c2010-06-21 13:45:51 -0700747 break;
Koushik Duttadf1e4062010-12-18 17:42:31 -0800748 ensure_path_mounted("/sd-ext");
749 ensure_path_mounted("/cache");
Koushik Dutta6c7745d2010-08-07 12:17:13 -0700750 if (confirm_selection( "Confirm wipe?", "Yes - Wipe Dalvik Cache")) {
Koushik Duttad63eaef2010-07-14 21:01:21 -0700751 __system("rm -r /data/dalvik-cache");
Koushik Dutta6c7745d2010-08-07 12:17:13 -0700752 __system("rm -r /cache/dalvik-cache");
753 __system("rm -r /sd-ext/dalvik-cache");
754 }
Koushik Duttadf1e4062010-12-18 17:42:31 -0800755 ensure_path_unmounted("/data");
Koushik Dutta6c7745d2010-08-07 12:17:13 -0700756 ui_print("Dalvik Cache wiped.\n");
Koushik Duttafd1579b2010-05-01 12:46:55 -0700757 break;
Koushik Duttad63eaef2010-07-14 21:01:21 -0700758 }
Koushik Duttafd1579b2010-05-01 12:46:55 -0700759 case 2:
Koushik Duttad63eaef2010-07-14 21:01:21 -0700760 {
761 if (confirm_selection( "Confirm wipe?", "Yes - Wipe Battery Stats"))
762 wipe_battery_stats();
Koushik Dutta598cfc72010-06-20 09:42:47 -0700763 break;
Koushik Duttad63eaef2010-07-14 21:01:21 -0700764 }
Koushik Dutta598cfc72010-06-20 09:42:47 -0700765 case 3:
Koushik Dutta49af23c2010-06-21 13:45:51 -0700766 handle_failure(1);
767 break;
768 case 4:
Koushik K. Duttaa496b512010-03-19 14:51:45 -0700769 {
770 ui_print("Outputting key codes.\n");
771 ui_print("Go back to end debugging.\n");
772 int key;
773 int action;
774 do
775 {
776 key = ui_wait_key();
777 action = device_handle_key(key, 1);
778 ui_print("Key: %d\n", key);
779 }
780 while (action != GO_BACK);
Koushik Dutta56606a22010-08-23 16:15:33 -0700781 break;
Koushik K. Duttaa496b512010-03-19 14:51:45 -0700782 }
Koushik Duttaf0e31b82010-08-17 16:55:38 -0700783 case 5:
784 {
Koushik Duttaceddcd52010-08-23 16:13:14 -0700785 static char* ext_sizes[] = { "128M",
786 "256M",
787 "512M",
788 "1024M",
Brint E. Kriebel0b233312010-11-14 15:31:17 -0700789 "2048M",
790 "4096M",
Koushik Duttaceddcd52010-08-23 16:13:14 -0700791 NULL };
792
793 static char* swap_sizes[] = { "0M",
794 "32M",
795 "64M",
796 "128M",
797 "256M",
798 NULL };
799
800 static char* ext_headers[] = { "Ext Size", "", NULL };
801 static char* swap_headers[] = { "Swap Size", "", NULL };
802
Koushik Duttadf1e4062010-12-18 17:42:31 -0800803 int ext_size = get_menu_selection(ext_headers, ext_sizes, 0, 0);
Koushik Duttaceddcd52010-08-23 16:13:14 -0700804 if (ext_size == GO_BACK)
805 continue;
Steve Kondik4123b582010-11-14 03:18:40 -0500806
Koushik Duttadf1e4062010-12-18 17:42:31 -0800807 int swap_size = get_menu_selection(swap_headers, swap_sizes, 0, 0);
Koushik Duttaceddcd52010-08-23 16:13:14 -0700808 if (swap_size == GO_BACK)
809 continue;
810
811 char sddevice[256];
Koushik Duttadf1e4062010-12-18 17:42:31 -0800812 Volume *vol = volume_for_path("/sdcard");
813 strcpy(sddevice, vol->device);
Koushik Duttaceddcd52010-08-23 16:13:14 -0700814 // we only want the mmcblk, not the partition
815 sddevice[strlen("/dev/block/mmcblkX")] = NULL;
816 char cmd[PATH_MAX];
817 setenv("SDPATH", sddevice, 1);
818 sprintf(cmd, "sdparted -es %s -ss %s -efs ext3 -s", ext_sizes[ext_size], swap_sizes[swap_size]);
819 ui_print("Partitioning SD Card... please wait...\n");
820 if (0 == __system(cmd))
821 ui_print("Done!\n");
822 else
823 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 -0700824 break;
Koushik Duttaf0e31b82010-08-17 16:55:38 -0700825 }
Koushik Dutta38e8b2b2010-08-17 22:21:53 -0700826 case 6:
827 {
Koushik Duttadf1e4062010-12-18 17:42:31 -0800828 ensure_path_mounted("/system");
829 ensure_path_mounted("/data");
Koushik Duttaceddcd52010-08-23 16:13:14 -0700830 ui_print("Fixing permissions...\n");
831 __system("fix_permissions");
832 ui_print("Done!\n");
Koushik Dutta56606a22010-08-23 16:15:33 -0700833 break;
Koushik Dutta38e8b2b2010-08-17 22:21:53 -0700834 }
Brint E. Kriebeld3522332010-12-11 20:52:06 -0700835 case 7:
836 {
837 static char* ext_sizes[] = { "128M",
838 "256M",
839 "512M",
840 "1024M",
841 "2048M",
842 "4096M",
843 NULL };
844
845 static char* swap_sizes[] = { "0M",
846 "32M",
847 "64M",
848 "128M",
849 "256M",
850 NULL };
851
852 static char* ext_headers[] = { "Data Size", "", NULL };
853 static char* swap_headers[] = { "Swap Size", "", NULL };
854
Koushik Duttadf1e4062010-12-18 17:42:31 -0800855 int ext_size = get_menu_selection(ext_headers, ext_sizes, 0, 0);
Brint E. Kriebeld3522332010-12-11 20:52:06 -0700856 if (ext_size == GO_BACK)
857 continue;
858
859 int swap_size = 0;
860 if (swap_size == GO_BACK)
861 continue;
862
863 char sddevice[256];
Koushik Duttadf1e4062010-12-18 17:42:31 -0800864 Volume *vol = volume_for_path("/emmc");
865 strcpy(sddevice, vol->device);
Brint E. Kriebeld3522332010-12-11 20:52:06 -0700866 // we only want the mmcblk, not the partition
867 sddevice[strlen("/dev/block/mmcblkX")] = NULL;
868 char cmd[PATH_MAX];
869 setenv("SDPATH", sddevice, 1);
870 sprintf(cmd, "sdparted -es %s -ss %s -efs ext3 -s", ext_sizes[ext_size], swap_sizes[swap_size]);
871 ui_print("Partitioning Internal SD Card... please wait...\n");
872 if (0 == __system(cmd))
873 ui_print("Done!\n");
874 else
875 ui_print("An error occured while partitioning your Internal SD Card. Please see /tmp/recovery.log for more details.\n");
876 break;
877 }
Koushik K. Duttaa496b512010-03-19 14:51:45 -0700878 }
879 }
Koushik Dutta14239d22010-06-14 15:02:48 -0700880}
881
Koushik Duttadf1e4062010-12-18 17:42:31 -0800882/*
Koushik Dutta14239d22010-06-14 15:02:48 -0700883void write_fstab_root(char *root_path, FILE *file)
884{
885 RootInfo *info = get_root_info_for_path(root_path);
Koushik Duttaa6522b32010-06-20 13:16:06 -0700886 if (info == NULL) {
887 LOGW("Unable to get root info for %s during fstab generation!", root_path);
Koushik Dutta598cfc72010-06-20 09:42:47 -0700888 return;
Koushik Duttaa6522b32010-06-20 13:16:06 -0700889 }
Koushik Dutta19447c02010-11-10 10:40:44 -0800890 char device[PATH_MAX];
891 int ret = get_root_partition_device(root_path, device);
892 if (ret == 0)
Koushik Dutta14239d22010-06-14 15:02:48 -0700893 {
Koushik Dutta19447c02010-11-10 10:40:44 -0800894 fprintf(file, "%s ", device);
Koushik Dutta14239d22010-06-14 15:02:48 -0700895 }
896 else
897 {
Koushik Dutta19447c02010-11-10 10:40:44 -0800898 fprintf(file, "%s ", info->device);
Koushik Dutta14239d22010-06-14 15:02:48 -0700899 }
Steve Kondik4123b582010-11-14 03:18:40 -0500900
Koushik Dutta14239d22010-06-14 15:02:48 -0700901 fprintf(file, "%s ", info->mount_point);
Steve Kondik4123b582010-11-14 03:18:40 -0500902 fprintf(file, "%s %s\n", info->filesystem, info->filesystem_options == NULL ? "rw" : info->filesystem_options);
Koushik Dutta14239d22010-06-14 15:02:48 -0700903}
904
905void create_fstab()
906{
Koushik Duttacd44ab92010-06-23 00:02:14 -0700907 __system("touch /etc/mtab");
Koushik Dutta14239d22010-06-14 15:02:48 -0700908 FILE *file = fopen("/etc/fstab", "w");
Koushik Duttaa6522b32010-06-20 13:16:06 -0700909 if (file == NULL) {
910 LOGW("Unable to create /etc/fstab!");
Koushik Dutta598cfc72010-06-20 09:42:47 -0700911 return;
Koushik Duttaa6522b32010-06-20 13:16:06 -0700912 }
Koushik Dutta14239d22010-06-14 15:02:48 -0700913 write_fstab_root("CACHE:", file);
914 write_fstab_root("DATA:", file);
Koushik Dutta19447c02010-11-10 10:40:44 -0800915#ifdef BOARD_HAS_DATADATA
Koushik Duttaa6522b32010-06-20 13:16:06 -0700916 write_fstab_root("DATADATA:", file);
917#endif
Koushik Dutta14239d22010-06-14 15:02:48 -0700918 write_fstab_root("SYSTEM:", file);
919 write_fstab_root("SDCARD:", file);
920 write_fstab_root("SDEXT:", file);
921 fclose(file);
Koushik Dutta598cfc72010-06-20 09:42:47 -0700922}
Koushik Duttadf1e4062010-12-18 17:42:31 -0800923*/
Koushik Dutta598cfc72010-06-20 09:42:47 -0700924
925void handle_failure(int ret)
926{
927 if (ret == 0)
928 return;
Koushik Duttadf1e4062010-12-18 17:42:31 -0800929 if (0 != ensure_path_mounted("/sdcard"))
Koushik Dutta598cfc72010-06-20 09:42:47 -0700930 return;
Koushik Duttaa6522b32010-06-20 13:16:06 -0700931 mkdir("/sdcard/clockworkmod", S_IRWXU);
932 __system("cp /tmp/recovery.log /sdcard/clockworkmod/recovery.log");
Koushik Dutta38e8b2b2010-08-17 22:21:53 -0700933 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 -0500934}
Koushik Duttadf1e4062010-12-18 17:42:31 -0800935
936int format_device(const char* device) {
937 if (device == NULL)
938 return -1;
939 if (device[0] == '/') {
940 Volume *vol = volume_for_path(device);
941 if (vol == NULL)
942 return -1;
943 return erase_partition(device, vol->fs_type);
944 }
945 return erase_raw_partition(device);
946}
947
948int is_path_mounted(const char* path) {
949 Volume* v = volume_for_path(path);
950 if (v == NULL) {
951 return 0;
952 }
953 if (strcmp(v->fs_type, "ramdisk") == 0) {
954 // the ramdisk is always mounted.
955 return 1;
956 }
957
958 int result;
959 result = scan_mounted_volumes();
960 if (result < 0) {
961 LOGE("failed to scan mounted volumes\n");
962 return 0;
963 }
964
965 const MountedVolume* mv =
966 find_mounted_volume_by_mount_point(v->mount_point);
967 if (mv) {
968 // volume is already mounted
969 return 1;
970 }
971 return 0;
972}