blob: e6d87e908599db517fb310af9da5fdd4fc90b623 [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 Dutta7adeadc2011-05-26 11:47:56 -070041#include <libgen.h>
Koushik Dutta30a937a2011-09-05 21:14:06 -070042#include "mtdutils/mtdutils.h"
Donovan Bartisha8f265d2011-11-10 12:55:22 -060043#include "bmlutils/bmlutils.h"
Koushik Dutta0df56f42011-11-16 16:00:35 -080044#include "cutils/android_reboot.h"
Koushik Dutta7adeadc2011-05-26 11:47:56 -070045
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -080046
Koushik K. Dutta6060e5c2010-02-11 22:27:06 -080047int signature_check_enabled = 1;
48int script_assert_enabled = 1;
Koushik Duttadf1e4062010-12-18 17:42:31 -080049static const char *SDCARD_UPDATE_FILE = "/sdcard/update.zip";
Koushik K. Dutta6060e5c2010-02-11 22:27:06 -080050
51void
52toggle_signature_check()
53{
54 signature_check_enabled = !signature_check_enabled;
55 ui_print("Signature Check: %s\n", signature_check_enabled ? "Enabled" : "Disabled");
56}
57
58void toggle_script_asserts()
59{
60 script_assert_enabled = !script_assert_enabled;
Koushik K. Duttae9234872010-02-12 00:43:24 -080061 ui_print("Script Asserts: %s\n", script_assert_enabled ? "Enabled" : "Disabled");
62}
63
Koushik K. Duttaea46fe22010-03-08 02:58:04 -080064int install_zip(const char* packagefilepath)
Koushik K. Duttae9234872010-02-12 00:43:24 -080065{
Koushik K. Duttabcdd0032010-02-21 21:10:25 -080066 ui_print("\n-- Installing: %s\n", packagefilepath);
Koushik Dutta9765a032010-12-18 21:31:02 -080067 if (device_flash_type() == MTD) {
68 set_sdcard_update_bootloader_message();
69 }
Koushik K. Duttabcdd0032010-02-21 21:10:25 -080070 int status = install_package(packagefilepath);
Koushik K. Duttaf3534d02010-04-18 18:06:24 -070071 ui_reset_progress();
Koushik K. Duttabcdd0032010-02-21 21:10:25 -080072 if (status != INSTALL_SUCCESS) {
73 ui_set_background(BACKGROUND_ICON_ERROR);
74 ui_print("Installation aborted.\n");
Koushik K. Duttaea46fe22010-03-08 02:58:04 -080075 return 1;
Steve Kondik4123b582010-11-14 03:18:40 -050076 }
Koushik K. Dutta001c5b52010-02-25 14:53:57 -080077 ui_set_background(BACKGROUND_ICON_NONE);
Koushik K. Dutta99fb6fe2010-03-03 00:42:58 -080078 ui_print("\nInstall from sdcard complete.\n");
Koushik K. Duttaea46fe22010-03-08 02:58:04 -080079 return 0;
Koushik K. Duttabcdd0032010-02-21 21:10:25 -080080}
81
Tanguy Pruvotaaad77f2011-06-18 10:26:32 +020082char* INSTALL_MENU_ITEMS[] = { "choose zip from sdcard",
83 "apply /sdcard/update.zip",
Koushik K. Duttabcdd0032010-02-21 21:10:25 -080084 "toggle signature verification",
85 "toggle script asserts",
Koushik Dutta30a937a2011-09-05 21:14:06 -070086 "choose zip from internal sdcard",
Koushik K. Duttabcdd0032010-02-21 21:10:25 -080087 NULL };
Tanguy Pruvotaaad77f2011-06-18 10:26:32 +020088#define ITEM_CHOOSE_ZIP 0
89#define ITEM_APPLY_SDCARD 1
Koushik K. Duttabcdd0032010-02-21 21:10:25 -080090#define ITEM_SIG_CHECK 2
91#define ITEM_ASSERTS 3
Koushik Dutta30a937a2011-09-05 21:14:06 -070092#define ITEM_CHOOSE_ZIP_INT 4
Koushik K. Duttabcdd0032010-02-21 21:10:25 -080093
94void show_install_update_menu()
95{
96 static char* headers[] = { "Apply update from .zip file on SD card",
97 "",
Steve Kondik4123b582010-11-14 03:18:40 -050098 NULL
Koushik K. Duttabcdd0032010-02-21 21:10:25 -080099 };
Koushik Dutta30a937a2011-09-05 21:14:06 -0700100
101 if (volume_for_path("/emmc") == NULL)
102 INSTALL_MENU_ITEMS[ITEM_CHOOSE_ZIP_INT] = NULL;
103
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800104 for (;;)
105 {
Koushik Duttadf1e4062010-12-18 17:42:31 -0800106 int chosen_item = get_menu_selection(headers, INSTALL_MENU_ITEMS, 0, 0);
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800107 switch (chosen_item)
108 {
109 case ITEM_ASSERTS:
110 toggle_script_asserts();
111 break;
112 case ITEM_SIG_CHECK:
113 toggle_signature_check();
114 break;
115 case ITEM_APPLY_SDCARD:
Koushik Duttad63eaef2010-07-14 21:01:21 -0700116 {
117 if (confirm_selection("Confirm install?", "Yes - Install /sdcard/update.zip"))
Koushik Duttadf1e4062010-12-18 17:42:31 -0800118 install_zip(SDCARD_UPDATE_FILE);
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800119 break;
Koushik Duttad63eaef2010-07-14 21:01:21 -0700120 }
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800121 case ITEM_CHOOSE_ZIP:
Koushik Dutta30a937a2011-09-05 21:14:06 -0700122 show_choose_zip_menu("/sdcard/");
123 break;
124 case ITEM_CHOOSE_ZIP_INT:
125 show_choose_zip_menu("/emmc/");
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800126 break;
127 default:
128 return;
129 }
Steve Kondik4123b582010-11-14 03:18:40 -0500130
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800131 }
132}
133
Koushik K. Dutta1d53c4e2010-04-02 16:46:21 -0700134void free_string_array(char** array)
135{
136 if (array == NULL)
137 return;
138 char* cursor = array[0];
139 int i = 0;
140 while (cursor != NULL)
141 {
142 free(cursor);
143 cursor = array[++i];
144 }
145 free(array);
146}
147
Koushik K. Dutta49f56892010-02-25 14:51:05 -0800148char** gather_files(const char* directory, const char* fileExtensionOrDirectory, int* numFiles)
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800149{
Koushik K. Duttae9234872010-02-12 00:43:24 -0800150 char path[PATH_MAX] = "";
151 DIR *dir;
152 struct dirent *de;
153 int total = 0;
154 int i;
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800155 char** files = NULL;
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800156 int pass;
157 *numFiles = 0;
158 int dirLen = strlen(directory);
Koushik K. Duttae9234872010-02-12 00:43:24 -0800159
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800160 dir = opendir(directory);
161 if (dir == NULL) {
162 ui_print("Couldn't open directory.\n");
163 return NULL;
164 }
Steve Kondik4123b582010-11-14 03:18:40 -0500165
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800166 int extension_length = 0;
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800167 if (fileExtensionOrDirectory != NULL)
168 extension_length = strlen(fileExtensionOrDirectory);
Steve Kondik4123b582010-11-14 03:18:40 -0500169
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800170 int isCounting = 1;
171 i = 0;
172 for (pass = 0; pass < 2; pass++) {
173 while ((de=readdir(dir)) != NULL) {
174 // skip hidden files
175 if (de->d_name[0] == '.')
176 continue;
Steve Kondik4123b582010-11-14 03:18:40 -0500177
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800178 // NULL means that we are gathering directories, so skip this
179 if (fileExtensionOrDirectory != NULL)
180 {
181 // make sure that we can have the desired extension (prevent seg fault)
182 if (strlen(de->d_name) < extension_length)
183 continue;
184 // compare the extension
185 if (strcmp(de->d_name + strlen(de->d_name) - extension_length, fileExtensionOrDirectory) != 0)
186 continue;
187 }
188 else
189 {
190 struct stat info;
Koushik K. Dutta1d53c4e2010-04-02 16:46:21 -0700191 char fullFileName[PATH_MAX];
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800192 strcpy(fullFileName, directory);
193 strcat(fullFileName, de->d_name);
194 stat(fullFileName, &info);
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800195 // make sure it is a directory
196 if (!(S_ISDIR(info.st_mode)))
197 continue;
198 }
Steve Kondik4123b582010-11-14 03:18:40 -0500199
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800200 if (pass == 0)
201 {
202 total++;
203 continue;
204 }
Steve Kondik4123b582010-11-14 03:18:40 -0500205
Koushik K. Dutta49f56892010-02-25 14:51:05 -0800206 files[i] = (char*) malloc(dirLen + strlen(de->d_name) + 2);
207 strcpy(files[i], directory);
208 strcat(files[i], de->d_name);
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800209 if (fileExtensionOrDirectory == NULL)
210 strcat(files[i], "/");
211 i++;
212 }
213 if (pass == 1)
214 break;
215 if (total == 0)
216 break;
217 rewinddir(dir);
218 *numFiles = total;
219 files = (char**) malloc((total+1)*sizeof(char*));
220 files[total]=NULL;
221 }
Koushik K. Dutta49f56892010-02-25 14:51:05 -0800222
223 if(closedir(dir) < 0) {
224 LOGE("Failed to close directory.");
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800225 }
226
227 if (total==0) {
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800228 return NULL;
229 }
230
agrabren1f76a5d2010-12-29 12:15:18 -0600231 // sort the result
232 if (files != NULL) {
233 for (i = 0; i < total; i++) {
234 int curMax = -1;
235 int j;
236 for (j = 0; j < total - i; j++) {
237 if (curMax == -1 || strcmp(files[curMax], files[j]) < 0)
238 curMax = j;
239 }
240 char* temp = files[curMax];
241 files[curMax] = files[total - i - 1];
242 files[total - i - 1] = temp;
243 }
244 }
Koushik Dutta1e8aaba2010-07-01 00:23:25 -0700245
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800246 return files;
Koushik K. Dutta49f56892010-02-25 14:51:05 -0800247}
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800248
Koushik K. Dutta49f56892010-02-25 14:51:05 -0800249// pass in NULL for fileExtensionOrDirectory and you will get a directory chooser
250char* choose_file_menu(const char* directory, const char* fileExtensionOrDirectory, const char* headers[])
251{
252 char path[PATH_MAX] = "";
253 DIR *dir;
254 struct dirent *de;
255 int numFiles = 0;
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800256 int numDirs = 0;
Koushik K. Dutta49f56892010-02-25 14:51:05 -0800257 int i;
Koushik K. Dutta1d53c4e2010-04-02 16:46:21 -0700258 char* return_value = NULL;
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800259 int dir_len = strlen(directory);
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800260
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800261 char** files = gather_files(directory, fileExtensionOrDirectory, &numFiles);
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800262 char** dirs = NULL;
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800263 if (fileExtensionOrDirectory != NULL)
264 dirs = gather_files(directory, NULL, &numDirs);
265 int total = numDirs + numFiles;
266 if (total == 0)
267 {
268 ui_print("No files found.\n");
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800269 }
Koushik K. Dutta1d53c4e2010-04-02 16:46:21 -0700270 else
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800271 {
Koushik K. Dutta1d53c4e2010-04-02 16:46:21 -0700272 char** list = (char**) malloc((total + 1) * sizeof(char*));
273 list[total] = NULL;
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800274
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800275
Koushik K. Dutta1d53c4e2010-04-02 16:46:21 -0700276 for (i = 0 ; i < numDirs; i++)
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800277 {
Koushik K. Dutta1d53c4e2010-04-02 16:46:21 -0700278 list[i] = strdup(dirs[i] + dir_len);
279 }
280
281 for (i = 0 ; i < numFiles; i++)
282 {
283 list[numDirs + i] = strdup(files[i] + dir_len);
284 }
285
286 for (;;)
287 {
Koushik Duttadf1e4062010-12-18 17:42:31 -0800288 int chosen_item = get_menu_selection(headers, list, 0, 0);
Koushik K. Dutta1d53c4e2010-04-02 16:46:21 -0700289 if (chosen_item == GO_BACK)
290 break;
291 static char ret[PATH_MAX];
292 if (chosen_item < numDirs)
293 {
294 char* subret = choose_file_menu(dirs[chosen_item], fileExtensionOrDirectory, headers);
295 if (subret != NULL)
296 {
297 strcpy(ret, subret);
298 return_value = ret;
299 break;
300 }
301 continue;
Steve Kondik4123b582010-11-14 03:18:40 -0500302 }
Koushik K. Dutta1d53c4e2010-04-02 16:46:21 -0700303 strcpy(ret, files[chosen_item - numDirs]);
304 return_value = ret;
305 break;
306 }
307 free_string_array(list);
Koushik K. Dutta981b0cd2010-02-22 08:53:34 -0800308 }
Koushik K. Dutta1d53c4e2010-04-02 16:46:21 -0700309
310 free_string_array(files);
311 free_string_array(dirs);
312 return return_value;
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800313}
314
Koushik Dutta30a937a2011-09-05 21:14:06 -0700315void show_choose_zip_menu(const char *mount_point)
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800316{
Koushik Dutta30a937a2011-09-05 21:14:06 -0700317 if (ensure_path_mounted(mount_point) != 0) {
318 LOGE ("Can't mount %s\n", mount_point);
Koushik K. Duttae9234872010-02-12 00:43:24 -0800319 return;
320 }
321
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800322 static char* headers[] = { "Choose a zip to apply",
323 "",
Steve Kondik4123b582010-11-14 03:18:40 -0500324 NULL
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800325 };
Steve Kondik4123b582010-11-14 03:18:40 -0500326
Koushik Dutta30a937a2011-09-05 21:14:06 -0700327 char* file = choose_file_menu(mount_point, ".zip", headers);
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800328 if (file == NULL)
329 return;
Koushik Duttad63eaef2010-07-14 21:01:21 -0700330 static char* confirm_install = "Confirm install?";
331 static char confirm[PATH_MAX];
332 sprintf(confirm, "Yes - Install %s", basename(file));
333 if (confirm_selection(confirm_install, confirm))
Koushik Duttadf1e4062010-12-18 17:42:31 -0800334 install_zip(file);
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800335}
336
Koushik Dutta89ed0b72011-10-26 21:25:34 -0700337void show_nandroid_restore_menu(const char* path)
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800338{
Koushik Dutta89ed0b72011-10-26 21:25:34 -0700339 if (ensure_path_mounted(path) != 0) {
340 LOGE("Can't mount %s\n", path);
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800341 return;
Koushik K. Dutta54305a82010-03-12 17:43:26 -0800342 }
Steve Kondik4123b582010-11-14 03:18:40 -0500343
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800344 static char* headers[] = { "Choose an image to restore",
345 "",
Steve Kondik4123b582010-11-14 03:18:40 -0500346 NULL
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800347 };
Koushik K. Duttae9234872010-02-12 00:43:24 -0800348
Koushik Dutta89ed0b72011-10-26 21:25:34 -0700349 char tmp[PATH_MAX];
350 sprintf(tmp, "%s/clockworkmod/backup/", path);
351 char* file = choose_file_menu(tmp, NULL, headers);
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800352 if (file == NULL)
353 return;
Koushik Duttad63eaef2010-07-14 21:01:21 -0700354
355 if (confirm_selection("Confirm restore?", "Yes - Restore"))
Koushik Dutta5b7f34a2010-12-29 23:36:03 -0800356 nandroid_restore(file, 1, 1, 1, 1, 1, 0);
Koushik K. Dutta03173782010-02-26 14:14:23 -0800357}
358
jt113447d86a32011-02-27 18:54:09 -0600359#ifndef BOARD_UMS_LUNFILE
360#define BOARD_UMS_LUNFILE "/sys/devices/platform/usb_mass_storage/lun0/file"
361#endif
362
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700363void show_mount_usb_storage_menu()
Koushik K. Dutta03173782010-02-26 14:14:23 -0800364{
Koushik Duttaf1cb0d52011-01-01 20:03:30 -0800365 int fd;
Koushik Duttadf1e4062010-12-18 17:42:31 -0800366 Volume *vol = volume_for_path("/sdcard");
jt113447d86a32011-02-27 18:54:09 -0600367 if ((fd = open(BOARD_UMS_LUNFILE, O_WRONLY)) < 0) {
Koushik Duttaf1cb0d52011-01-01 20:03:30 -0800368 LOGE("Unable to open ums lunfile (%s)", strerror(errno));
369 return -1;
370 }
371
mik_osdf3004b2011-04-26 19:15:22 +0300372 if ((write(fd, vol->device, strlen(vol->device)) < 0) &&
373 (!vol->device2 || (write(fd, vol->device, strlen(vol->device2)) < 0))) {
Koushik Duttaf1cb0d52011-01-01 20:03:30 -0800374 LOGE("Unable to write to ums lunfile (%s)", strerror(errno));
375 close(fd);
376 return -1;
377 }
Koushik K. Dutta03173782010-02-26 14:14:23 -0800378 static char* headers[] = { "USB Mass Storage device",
379 "Leaving this menu unmount",
380 "your SD card from your PC.",
381 "",
Steve Kondik4123b582010-11-14 03:18:40 -0500382 NULL
Koushik K. Dutta03173782010-02-26 14:14:23 -0800383 };
Steve Kondik4123b582010-11-14 03:18:40 -0500384
Koushik K. Dutta03173782010-02-26 14:14:23 -0800385 static char* list[] = { "Unmount", NULL };
Steve Kondik4123b582010-11-14 03:18:40 -0500386
Koushik K. Dutta03173782010-02-26 14:14:23 -0800387 for (;;)
388 {
Koushik Duttadf1e4062010-12-18 17:42:31 -0800389 int chosen_item = get_menu_selection(headers, list, 0, 0);
Koushik K. Dutta03173782010-02-26 14:14:23 -0800390 if (chosen_item == GO_BACK || chosen_item == 0)
391 break;
392 }
Steve Kondik4123b582010-11-14 03:18:40 -0500393
jt113447d86a32011-02-27 18:54:09 -0600394 if ((fd = open(BOARD_UMS_LUNFILE, O_WRONLY)) < 0) {
Koushik Duttaf1cb0d52011-01-01 20:03:30 -0800395 LOGE("Unable to open ums lunfile (%s)", strerror(errno));
396 return -1;
397 }
398
399 char ch = 0;
400 if (write(fd, &ch, 1) < 0) {
401 LOGE("Unable to write to ums lunfile (%s)", strerror(errno));
402 close(fd);
403 return -1;
404 }
Koushik K. Duttae81cb752010-02-26 17:44:33 -0800405}
Koushik K. Duttaf68aaaf2010-03-07 13:39:21 -0800406
Koushik Duttad63eaef2010-07-14 21:01:21 -0700407int confirm_selection(const char* title, const char* confirm)
Koushik K. Dutta16f0b492010-03-19 14:37:11 -0700408{
Koushik Duttaceddcd52010-08-23 16:13:14 -0700409 struct stat info;
410 if (0 == stat("/sdcard/clockworkmod/.no_confirm", &info))
411 return 1;
412
Koushik Duttad63eaef2010-07-14 21:01:21 -0700413 char* confirm_headers[] = { title, " THIS CAN NOT BE UNDONE.", "", NULL };
Chris41gd2eecca2011-11-15 10:33:17 -0600414 if (0 == stat("/sdcard/clockworkmod/.one_confirm", &info)) {
415 char* items[] = { "No",
416 confirm, //" Yes -- wipe partition", // [1]
417 NULL };
418 int chosen_item = get_menu_selection(confirm_headers, items, 0, 0);
419 return chosen_item == 1;
420 }
421 else {
422 char* items[] = { "No",
423 "No",
424 "No",
425 "No",
426 "No",
427 "No",
428 "No",
429 confirm, //" Yes -- wipe partition", // [7]
430 "No",
431 "No",
432 "No",
433 NULL };
434 int chosen_item = get_menu_selection(confirm_headers, items, 0, 0);
435 return chosen_item == 7;
436 }
437 }
Koushik K. Dutta16f0b492010-03-19 14:37:11 -0700438
Koushik Duttada32b542011-01-01 17:55:22 -0800439#define MKE2FS_BIN "/sbin/mke2fs"
440#define TUNE2FS_BIN "/sbin/tune2fs"
441#define E2FSCK_BIN "/sbin/e2fsck"
442
Koushik Dutta30a937a2011-09-05 21:14:06 -0700443int format_device(const char *device, const char *path, const char *fs_type) {
444 Volume* v = volume_for_path(path);
445 if (v == NULL) {
446 // no /sdcard? let's assume /data/media
447 if (strstr(path, "/sdcard") == path && is_data_media()) {
448 return format_unknown_device(NULL, path, NULL);
449 }
450 // silent failure for sd-ext
451 if (strcmp(path, "/sd-ext") == 0)
452 return -1;
453 LOGE("unknown volume \"%s\"\n", path);
454 return -1;
455 }
456 if (strcmp(fs_type, "ramdisk") == 0) {
457 // you can't format the ramdisk.
458 LOGE("can't format_volume \"%s\"", path);
459 return -1;
460 }
461
Donovan Bartisha8f265d2011-11-10 12:55:22 -0600462 if (strcmp(fs_type, "rfs") == 0) {
463 if (ensure_path_unmounted(path) != 0) {
464 LOGE("format_volume failed to unmount \"%s\"\n", v->mount_point);
465 return -1;
466 }
467 if (0 != format_rfs_device(device, path)) {
468 LOGE("format_volume: format_rfs_device failed on %s\n", device);
469 return -1;
470 }
471 return 0;
472 }
473
Koushik Dutta30a937a2011-09-05 21:14:06 -0700474 if (strcmp(v->mount_point, path) != 0) {
475 return format_unknown_device(v->device, path, NULL);
476 }
477
478 if (ensure_path_unmounted(path) != 0) {
479 LOGE("format_volume failed to unmount \"%s\"\n", v->mount_point);
480 return -1;
481 }
482
483 if (strcmp(fs_type, "yaffs2") == 0 || strcmp(fs_type, "mtd") == 0) {
484 mtd_scan_partitions();
485 const MtdPartition* partition = mtd_find_partition_by_name(device);
486 if (partition == NULL) {
487 LOGE("format_volume: no MTD partition \"%s\"\n", device);
488 return -1;
489 }
490
491 MtdWriteContext *write = mtd_write_partition(partition);
492 if (write == NULL) {
493 LOGW("format_volume: can't open MTD \"%s\"\n", device);
494 return -1;
495 } else if (mtd_erase_blocks(write, -1) == (off_t) -1) {
496 LOGW("format_volume: can't erase MTD \"%s\"\n", device);
497 mtd_write_close(write);
498 return -1;
499 } else if (mtd_write_close(write)) {
500 LOGW("format_volume: can't close MTD \"%s\"\n",device);
501 return -1;
502 }
503 return 0;
504 }
505
506 if (strcmp(fs_type, "ext4") == 0) {
507 reset_ext4fs_info();
508 int result = make_ext4fs(device, NULL, NULL, 0, 0, 0);
509 if (result != 0) {
510 LOGE("format_volume: make_extf4fs failed on %s\n", device);
511 return -1;
512 }
513 return 0;
514 }
515
516 return format_unknown_device(device, path, fs_type);
517}
518
Koushik Duttada32b542011-01-01 17:55:22 -0800519int format_unknown_device(const char *device, const char* path, const char *fs_type)
Koushik K. Dutta3f995392010-03-30 23:29:43 -0700520{
Koushik Dutta2e454492011-01-01 18:06:17 -0800521 LOGI("Formatting unknown device.\n");
Koushik Duttaa8708c62011-01-01 18:00:27 -0800522
Koushik Duttafb3bd712011-06-16 14:13:02 -0700523 if (fs_type != NULL && get_flash_type(fs_type) != UNSUPPORTED)
Koushik Dutta8a6bc772011-05-26 11:14:15 -0700524 return erase_raw_partition(fs_type, device);
Koushik Duttada32b542011-01-01 17:55:22 -0800525
Koushik Dutta9f52e5f2011-01-02 14:11:24 -0800526 // if this is SDEXT:, don't worry about it if it does not exist.
Koushik Duttadf1e4062010-12-18 17:42:31 -0800527 if (0 == strcmp(path, "/sd-ext"))
Koushik K. Duttaf3534d02010-04-18 18:06:24 -0700528 {
529 struct stat st;
Koushik Duttadf1e4062010-12-18 17:42:31 -0800530 Volume *vol = volume_for_path("/sd-ext");
531 if (vol == NULL || 0 != stat(vol->device, &st))
Koushik K. Duttaf3534d02010-04-18 18:06:24 -0700532 {
533 ui_print("No app2sd partition found. Skipping format of /sd-ext.\n");
534 return 0;
535 }
536 }
Koushik Dutta2f73e582010-04-18 16:00:21 -0700537
Koushik Dutta9f52e5f2011-01-02 14:11:24 -0800538 if (NULL != fs_type) {
539 if (strcmp("ext3", fs_type) == 0) {
540 LOGI("Formatting ext3 device.\n");
541 if (0 != ensure_path_unmounted(path)) {
542 LOGE("Error while unmounting %s.\n", path);
543 return -12;
544 }
545 return format_ext3_device(device);
546 }
547
548 if (strcmp("ext2", fs_type) == 0) {
549 LOGI("Formatting ext2 device.\n");
550 if (0 != ensure_path_unmounted(path)) {
551 LOGE("Error while unmounting %s.\n", path);
552 return -12;
553 }
554 return format_ext2_device(device);
555 }
556 }
557
Koushik Duttadf1e4062010-12-18 17:42:31 -0800558 if (0 != ensure_path_mounted(path))
Koushik K. Dutta3f995392010-03-30 23:29:43 -0700559 {
560 ui_print("Error mounting %s!\n", path);
Koushik Duttacd44ab92010-06-23 00:02:14 -0700561 ui_print("Skipping format...\n");
562 return 0;
Koushik K. Dutta3f995392010-03-30 23:29:43 -0700563 }
564
565 static char tmp[PATH_MAX];
Koushik Dutta50822992011-06-08 19:03:27 -0700566 if (strcmp(path, "/data") == 0) {
567 sprintf(tmp, "cd /data ; for f in $(ls -a | grep -v ^media$); do rm -rf $f; done");
568 __system(tmp);
569 }
570 else {
571 sprintf(tmp, "rm -rf %s/*", path);
572 __system(tmp);
573 sprintf(tmp, "rm -rf %s/.*", path);
574 __system(tmp);
575 }
Steve Kondik4123b582010-11-14 03:18:40 -0500576
Koushik Duttadf1e4062010-12-18 17:42:31 -0800577 ensure_path_unmounted(path);
Koushik K. Dutta3f995392010-03-30 23:29:43 -0700578 return 0;
579}
580
Koushik Duttadf1e4062010-12-18 17:42:31 -0800581//#define MOUNTABLE_COUNT 5
582//#define DEVICE_COUNT 4
583//#define MMC_COUNT 2
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700584
Kolja Dummann92796ec2011-02-13 20:59:30 +0100585typedef struct {
586 char mount[255];
587 char unmount[255];
588 Volume* v;
589} MountMenuEntry;
590
591typedef struct {
592 char txt[255];
593 Volume* v;
594} FormatMenuEntry;
595
Steve Kondik0d878512011-04-09 01:41:45 -0400596int is_safe_to_format(char* name)
597{
Brandon Bennett9ad8ba22011-05-07 23:42:58 -0600598 char str[255];
599 char* partition;
j_r0dd14d9e752011-06-09 18:37:28 -0400600 property_get("ro.cwm.forbid_format", str, "/misc,/radio,/bootloader,/recovery,/efs");
Brandon Bennett9ad8ba22011-05-07 23:42:58 -0600601
602 partition = strtok(str, ", ");
603 while (partition != NULL) {
604 if (strcmp(name, partition) == 0) {
605 return 0;
606 }
607 partition = strtok(NULL, ", ");
608 }
609
610 return 1;
Steve Kondik0d878512011-04-09 01:41:45 -0400611}
612
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700613void show_partition_menu()
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700614{
Koushik Duttaceddcd52010-08-23 16:13:14 -0700615 static char* headers[] = { "Mounts and Storage Menu",
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700616 "",
Steve Kondik4123b582010-11-14 03:18:40 -0500617 NULL
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700618 };
619
Kolja Dummann92796ec2011-02-13 20:59:30 +0100620 static MountMenuEntry* mount_menue = NULL;
621 static FormatMenuEntry* format_menue = NULL;
622
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700623 typedef char* string;
Steve Kondik4123b582010-11-14 03:18:40 -0500624
Kolja Dummann92796ec2011-02-13 20:59:30 +0100625 int i, mountable_volumes, formatable_volumes;
626 int num_volumes;
627 Volume* device_volumes;
Steve Kondik4123b582010-11-14 03:18:40 -0500628
Kolja Dummann92796ec2011-02-13 20:59:30 +0100629 num_volumes = get_num_volumes();
630 device_volumes = get_device_volumes();
631
632 string options[255];
633
634 if(!device_volumes)
635 return;
636
637 mountable_volumes = 0;
638 formatable_volumes = 0;
639
640 mount_menue = malloc(num_volumes * sizeof(MountMenuEntry));
641 format_menue = malloc(num_volumes * sizeof(FormatMenuEntry));
642
643 for (i = 0; i < num_volumes; ++i) {
644 Volume* v = &device_volumes[i];
Koushik Dutta67fa0c32011-03-17 11:37:21 -0700645 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 +0100646 {
647 sprintf(&mount_menue[mountable_volumes].mount, "mount %s", v->mount_point);
648 sprintf(&mount_menue[mountable_volumes].unmount, "unmount %s", v->mount_point);
649 mount_menue[mountable_volumes].v = &device_volumes[i];
650 ++mountable_volumes;
Steve Kondik0d878512011-04-09 01:41:45 -0400651 if (is_safe_to_format(v->mount_point)) {
652 sprintf(&format_menue[formatable_volumes].txt, "format %s", v->mount_point);
653 format_menue[formatable_volumes].v = &device_volumes[i];
654 ++formatable_volumes;
655 }
Kolja Dummann92796ec2011-02-13 20:59:30 +0100656 }
Steve Kondik0d878512011-04-09 01:41:45 -0400657 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 +0100658 {
659 sprintf(&format_menue[formatable_volumes].txt, "format %s", v->mount_point);
660 format_menue[formatable_volumes].v = &device_volumes[i];
661 ++formatable_volumes;
662 }
663 }
664
Koushik Duttadf1e4062010-12-18 17:42:31 -0800665
Koushik Duttad63eaef2010-07-14 21:01:21 -0700666 static char* confirm_format = "Confirm format?";
Koushik Duttaecd32fa2010-07-14 18:38:02 -0700667 static char* confirm = "Yes - Format";
wjbcea6eba2011-04-30 21:04:04 -0400668 char confirm_string[255];
Steve Kondik4123b582010-11-14 03:18:40 -0500669
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700670 for (;;)
671 {
Steve Kondik4123b582010-11-14 03:18:40 -0500672
Kolja Dummann92796ec2011-02-13 20:59:30 +0100673 for (i = 0; i < mountable_volumes; i++)
674 {
675 MountMenuEntry* e = &mount_menue[i];
676 Volume* v = e->v;
677 if(is_path_mounted(v->mount_point))
678 options[i] = e->unmount;
679 else
680 options[i] = e->mount;
681 }
Steve Kondik4123b582010-11-14 03:18:40 -0500682
Kolja Dummann92796ec2011-02-13 20:59:30 +0100683 for (i = 0; i < formatable_volumes; i++)
684 {
685 FormatMenuEntry* e = &format_menue[i];
Steve Kondik4123b582010-11-14 03:18:40 -0500686
Kolja Dummann92796ec2011-02-13 20:59:30 +0100687 options[mountable_volumes+i] = e->txt;
688 }
689
690 options[mountable_volumes+formatable_volumes] = "mount USB storage";
691 options[mountable_volumes+formatable_volumes + 1] = NULL;
692
693 int chosen_item = get_menu_selection(headers, &options, 0, 0);
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700694 if (chosen_item == GO_BACK)
695 break;
Kolja Dummann92796ec2011-02-13 20:59:30 +0100696 if (chosen_item == (mountable_volumes+formatable_volumes))
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700697 {
698 show_mount_usb_storage_menu();
699 }
Kolja Dummann92796ec2011-02-13 20:59:30 +0100700 else if (chosen_item < mountable_volumes)
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700701 {
Kolja Dummann92796ec2011-02-13 20:59:30 +0100702 MountMenuEntry* e = &mount_menue[chosen_item];
703 Volume* v = e->v;
704
705 if (is_path_mounted(v->mount_point))
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700706 {
Kolja Dummann92796ec2011-02-13 20:59:30 +0100707 if (0 != ensure_path_unmounted(v->mount_point))
708 ui_print("Error unmounting %s!\n", v->mount_point);
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700709 }
710 else
711 {
Kolja Dummann92796ec2011-02-13 20:59:30 +0100712 if (0 != ensure_path_mounted(v->mount_point))
713 ui_print("Error mounting %s!\n", v->mount_point);
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700714 }
715 }
Kolja Dummann92796ec2011-02-13 20:59:30 +0100716 else if (chosen_item < (mountable_volumes + formatable_volumes))
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700717 {
Kolja Dummann92796ec2011-02-13 20:59:30 +0100718 chosen_item = chosen_item - mountable_volumes;
719 FormatMenuEntry* e = &format_menue[chosen_item];
720 Volume* v = e->v;
721
wjbcea6eba2011-04-30 21:04:04 -0400722 sprintf(confirm_string, "%s - %s", v->mount_point, confirm_format);
723
724 if (!confirm_selection(confirm_string, confirm))
Koushik K. Dutta16f0b492010-03-19 14:37:11 -0700725 continue;
Kolja Dummann92796ec2011-02-13 20:59:30 +0100726 ui_print("Formatting %s...\n", v->mount_point);
727 if (0 != format_volume(v->mount_point))
728 ui_print("Error formatting %s!\n", v->mount_point);
Koushik K. Dutta3f995392010-03-30 23:29:43 -0700729 else
730 ui_print("Done.\n");
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700731 }
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700732 }
Kolja Dummann92796ec2011-02-13 20:59:30 +0100733
734 free(mount_menue);
735 free(format_menue);
736
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700737}
738
Koushik Dutta89ed0b72011-10-26 21:25:34 -0700739void show_nandroid_advanced_restore_menu(const char* path)
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700740{
Koushik Duttac9ea1172011-10-29 18:48:18 -0700741 if (ensure_path_mounted(path) != 0) {
742 LOGE ("Can't mount sdcard\n");
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700743 return;
744 }
745
746 static char* advancedheaders[] = { "Choose an image to restore",
747 "",
748 "Choose an image to restore",
749 "first. The next menu will",
750 "you more options.",
751 "",
752 NULL
753 };
754
Koushik Duttac9ea1172011-10-29 18:48:18 -0700755 char tmp[PATH_MAX];
Koushik Dutta7ef88152011-11-11 00:26:32 -0800756 sprintf(tmp, "%s/clockworkmod/backup/", path);
Koushik Duttac9ea1172011-10-29 18:48:18 -0700757 char* file = choose_file_menu(tmp, NULL, advancedheaders);
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700758 if (file == NULL)
759 return;
760
761 static char* headers[] = { "Nandroid Advanced Restore",
762 "",
763 NULL
764 };
765
766 static char* list[] = { "Restore boot",
767 "Restore system",
768 "Restore data",
769 "Restore cache",
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700770 "Restore sd-ext",
agrabren1f76a5d2010-12-29 12:15:18 -0600771 "Restore wimax",
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700772 NULL
773 };
Koushik Dutta5b7f34a2010-12-29 23:36:03 -0800774
Koushik Dutta5b7f34a2010-12-29 23:36:03 -0800775 if (0 != get_partition_device("wimax", tmp)) {
776 // disable wimax restore option
777 list[5] = NULL;
778 }
Koushik Duttad63eaef2010-07-14 21:01:21 -0700779
780 static char* confirm_restore = "Confirm restore?";
781
Koushik Duttadf1e4062010-12-18 17:42:31 -0800782 int chosen_item = get_menu_selection(headers, list, 0, 0);
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700783 switch (chosen_item)
784 {
785 case 0:
Koushik Duttad63eaef2010-07-14 21:01:21 -0700786 if (confirm_selection(confirm_restore, "Yes - Restore boot"))
agrabren1f76a5d2010-12-29 12:15:18 -0600787 nandroid_restore(file, 1, 0, 0, 0, 0, 0);
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700788 break;
789 case 1:
Koushik Duttad63eaef2010-07-14 21:01:21 -0700790 if (confirm_selection(confirm_restore, "Yes - Restore system"))
agrabren1f76a5d2010-12-29 12:15:18 -0600791 nandroid_restore(file, 0, 1, 0, 0, 0, 0);
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700792 break;
793 case 2:
Koushik Duttad63eaef2010-07-14 21:01:21 -0700794 if (confirm_selection(confirm_restore, "Yes - Restore data"))
agrabren1f76a5d2010-12-29 12:15:18 -0600795 nandroid_restore(file, 0, 0, 1, 0, 0, 0);
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700796 break;
797 case 3:
Koushik Duttad63eaef2010-07-14 21:01:21 -0700798 if (confirm_selection(confirm_restore, "Yes - Restore cache"))
agrabren1f76a5d2010-12-29 12:15:18 -0600799 nandroid_restore(file, 0, 0, 0, 1, 0, 0);
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700800 break;
Koushik Dutta2f73e582010-04-18 16:00:21 -0700801 case 4:
Koushik Duttad63eaef2010-07-14 21:01:21 -0700802 if (confirm_selection(confirm_restore, "Yes - Restore sd-ext"))
agrabren1f76a5d2010-12-29 12:15:18 -0600803 nandroid_restore(file, 0, 0, 0, 0, 1, 0);
804 break;
805 case 5:
806 if (confirm_selection(confirm_restore, "Yes - Restore wimax"))
807 nandroid_restore(file, 0, 0, 0, 0, 0, 1);
Koushik Dutta2f73e582010-04-18 16:00:21 -0700808 break;
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700809 }
810}
811
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700812void show_nandroid_menu()
813{
814 static char* headers[] = { "Nandroid",
815 "",
Steve Kondik4123b582010-11-14 03:18:40 -0500816 NULL
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700817 };
818
Koushik Dutta89ed0b72011-10-26 21:25:34 -0700819 static char* list[] = { "backup",
820 "restore",
821 "advanced restore",
822 "backup to internal sdcard",
823 "restore from internal sdcard",
824 "advanced restore from internal sdcard",
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700825 NULL
826 };
827
Koushik Dutta89ed0b72011-10-26 21:25:34 -0700828 if (volume_for_path("/emmc") == NULL)
Koushik Dutta4fcd5232011-10-28 11:47:02 -0700829 list[3] = NULL;
Koushik Dutta89ed0b72011-10-26 21:25:34 -0700830
Koushik Duttadf1e4062010-12-18 17:42:31 -0800831 int chosen_item = get_menu_selection(headers, list, 0, 0);
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700832 switch (chosen_item)
833 {
834 case 0:
835 {
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700836 char backup_path[PATH_MAX];
Koushik K. Dutta6a26e7c2010-03-27 15:26:11 -0700837 time_t t = time(NULL);
838 struct tm *tmp = localtime(&t);
839 if (tmp == NULL)
840 {
841 struct timeval tp;
842 gettimeofday(&tp, NULL);
843 sprintf(backup_path, "/sdcard/clockworkmod/backup/%d", tp.tv_sec);
844 }
845 else
846 {
847 strftime(backup_path, sizeof(backup_path), "/sdcard/clockworkmod/backup/%F.%H.%M.%S", tmp);
848 }
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700849 nandroid_backup(backup_path);
850 }
851 break;
852 case 1:
Koushik Dutta89ed0b72011-10-26 21:25:34 -0700853 show_nandroid_restore_menu("/sdcard");
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700854 break;
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700855 case 2:
Koushik Dutta89ed0b72011-10-26 21:25:34 -0700856 show_nandroid_advanced_restore_menu("/sdcard");
857 break;
858 case 3:
859 {
860 char backup_path[PATH_MAX];
861 time_t t = time(NULL);
862 struct tm *tmp = localtime(&t);
863 if (tmp == NULL)
864 {
865 struct timeval tp;
866 gettimeofday(&tp, NULL);
867 sprintf(backup_path, "/emmc/clockworkmod/backup/%d", tp.tv_sec);
868 }
869 else
870 {
871 strftime(backup_path, sizeof(backup_path), "/emmc/clockworkmod/backup/%F.%H.%M.%S", tmp);
872 }
873 nandroid_backup(backup_path);
874 }
875 break;
876 case 4:
877 show_nandroid_restore_menu("/emmc");
878 break;
879 case 5:
880 show_nandroid_advanced_restore_menu("/emmc");
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700881 break;
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700882 }
Koushik K. Duttaa496b512010-03-19 14:51:45 -0700883}
884
Koushik Duttafd1579b2010-05-01 12:46:55 -0700885void wipe_battery_stats()
886{
Koushik Duttadf1e4062010-12-18 17:42:31 -0800887 ensure_path_mounted("/data");
Koushik Duttafd1579b2010-05-01 12:46:55 -0700888 remove("/data/system/batterystats.bin");
Koushik Duttadf1e4062010-12-18 17:42:31 -0800889 ensure_path_unmounted("/data");
j_r0dd3f2e2862011-07-10 22:48:39 -0400890 ui_print("Battery Stats wiped.\n");
Koushik Duttafd1579b2010-05-01 12:46:55 -0700891}
892
Koushik K. Duttaa496b512010-03-19 14:51:45 -0700893void show_advanced_menu()
894{
895 static char* headers[] = { "Advanced and Debugging Menu",
896 "",
897 NULL
898 };
899
Koushik K. Dutta7fe4d7b2010-04-06 23:04:52 -0700900 static char* list[] = { "Reboot Recovery",
Koushik Dutta49af23c2010-06-21 13:45:51 -0700901 "Wipe Dalvik Cache",
Koushik Duttafd1579b2010-05-01 12:46:55 -0700902 "Wipe Battery Stats",
Koushik Dutta598cfc72010-06-20 09:42:47 -0700903 "Report Error",
Koushik K. Dutta7fe4d7b2010-04-06 23:04:52 -0700904 "Key Test",
Tanguy Pruvot67d358c2011-06-10 20:26:44 +0200905 "Show log",
Koushik Duttae17a78d2010-08-29 12:35:10 -0700906#ifndef BOARD_HAS_SMALL_RECOVERY
Koushik Duttaceddcd52010-08-23 16:13:14 -0700907 "Partition SD Card",
908 "Fix Permissions",
Brint E. Kriebeld3522332010-12-11 20:52:06 -0700909#ifdef BOARD_HAS_SDCARD_INTERNAL
910 "Partition Internal SD Card",
911#endif
Koushik Duttae17a78d2010-08-29 12:35:10 -0700912#endif
Koushik K. Duttaa496b512010-03-19 14:51:45 -0700913 NULL
914 };
915
916 for (;;)
917 {
Koushik Duttadf1e4062010-12-18 17:42:31 -0800918 int chosen_item = get_menu_selection(headers, list, 0, 0);
Koushik K. Duttaa496b512010-03-19 14:51:45 -0700919 if (chosen_item == GO_BACK)
920 break;
921 switch (chosen_item)
922 {
923 case 0:
Tanguy Pruvot67d358c2011-06-10 20:26:44 +0200924 {
Koushik Dutta0df56f42011-11-16 16:00:35 -0800925 android_reboot(ANDROID_RB_RESTART2, 0, "recovery");
Koushik K. Dutta7fe4d7b2010-04-06 23:04:52 -0700926 break;
Tanguy Pruvot67d358c2011-06-10 20:26:44 +0200927 }
Koushik K. Dutta7fe4d7b2010-04-06 23:04:52 -0700928 case 1:
Koushik Duttad63eaef2010-07-14 21:01:21 -0700929 {
Koushik Duttadf1e4062010-12-18 17:42:31 -0800930 if (0 != ensure_path_mounted("/data"))
Koushik Dutta49af23c2010-06-21 13:45:51 -0700931 break;
Koushik Duttadf1e4062010-12-18 17:42:31 -0800932 ensure_path_mounted("/sd-ext");
933 ensure_path_mounted("/cache");
Koushik Dutta6c7745d2010-08-07 12:17:13 -0700934 if (confirm_selection( "Confirm wipe?", "Yes - Wipe Dalvik Cache")) {
Koushik Duttad63eaef2010-07-14 21:01:21 -0700935 __system("rm -r /data/dalvik-cache");
Koushik Dutta6c7745d2010-08-07 12:17:13 -0700936 __system("rm -r /cache/dalvik-cache");
937 __system("rm -r /sd-ext/dalvik-cache");
Koushik Dutta30a937a2011-09-05 21:14:06 -0700938 ui_print("Dalvik Cache wiped.\n");
Koushik Dutta6c7745d2010-08-07 12:17:13 -0700939 }
Koushik Duttadf1e4062010-12-18 17:42:31 -0800940 ensure_path_unmounted("/data");
Koushik Duttafd1579b2010-05-01 12:46:55 -0700941 break;
Koushik Duttad63eaef2010-07-14 21:01:21 -0700942 }
Koushik Duttafd1579b2010-05-01 12:46:55 -0700943 case 2:
Koushik Duttad63eaef2010-07-14 21:01:21 -0700944 {
945 if (confirm_selection( "Confirm wipe?", "Yes - Wipe Battery Stats"))
946 wipe_battery_stats();
Koushik Dutta598cfc72010-06-20 09:42:47 -0700947 break;
Koushik Duttad63eaef2010-07-14 21:01:21 -0700948 }
Koushik Dutta598cfc72010-06-20 09:42:47 -0700949 case 3:
Koushik Dutta49af23c2010-06-21 13:45:51 -0700950 handle_failure(1);
951 break;
952 case 4:
Koushik K. Duttaa496b512010-03-19 14:51:45 -0700953 {
954 ui_print("Outputting key codes.\n");
955 ui_print("Go back to end debugging.\n");
956 int key;
957 int action;
958 do
959 {
960 key = ui_wait_key();
961 action = device_handle_key(key, 1);
962 ui_print("Key: %d\n", key);
963 }
964 while (action != GO_BACK);
Koushik Dutta56606a22010-08-23 16:15:33 -0700965 break;
Koushik K. Duttaa496b512010-03-19 14:51:45 -0700966 }
Koushik Duttaf0e31b82010-08-17 16:55:38 -0700967 case 5:
968 {
Tanguy Pruvot67d358c2011-06-10 20:26:44 +0200969 ui_printlogtail(12);
970 break;
971 }
972 case 6:
973 {
Koushik Duttaceddcd52010-08-23 16:13:14 -0700974 static char* ext_sizes[] = { "128M",
975 "256M",
976 "512M",
977 "1024M",
Brint E. Kriebel0b233312010-11-14 15:31:17 -0700978 "2048M",
979 "4096M",
Koushik Duttaceddcd52010-08-23 16:13:14 -0700980 NULL };
981
982 static char* swap_sizes[] = { "0M",
983 "32M",
984 "64M",
985 "128M",
986 "256M",
987 NULL };
988
989 static char* ext_headers[] = { "Ext Size", "", NULL };
990 static char* swap_headers[] = { "Swap Size", "", NULL };
991
Koushik Duttadf1e4062010-12-18 17:42:31 -0800992 int ext_size = get_menu_selection(ext_headers, ext_sizes, 0, 0);
Koushik Duttaceddcd52010-08-23 16:13:14 -0700993 if (ext_size == GO_BACK)
994 continue;
Steve Kondik4123b582010-11-14 03:18:40 -0500995
Koushik Duttadf1e4062010-12-18 17:42:31 -0800996 int swap_size = get_menu_selection(swap_headers, swap_sizes, 0, 0);
Koushik Duttaceddcd52010-08-23 16:13:14 -0700997 if (swap_size == GO_BACK)
998 continue;
999
1000 char sddevice[256];
Koushik Duttadf1e4062010-12-18 17:42:31 -08001001 Volume *vol = volume_for_path("/sdcard");
1002 strcpy(sddevice, vol->device);
Koushik Duttaceddcd52010-08-23 16:13:14 -07001003 // we only want the mmcblk, not the partition
1004 sddevice[strlen("/dev/block/mmcblkX")] = NULL;
1005 char cmd[PATH_MAX];
1006 setenv("SDPATH", sddevice, 1);
1007 sprintf(cmd, "sdparted -es %s -ss %s -efs ext3 -s", ext_sizes[ext_size], swap_sizes[swap_size]);
1008 ui_print("Partitioning SD Card... please wait...\n");
1009 if (0 == __system(cmd))
1010 ui_print("Done!\n");
1011 else
1012 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 -07001013 break;
Koushik Duttaf0e31b82010-08-17 16:55:38 -07001014 }
Tanguy Pruvot67d358c2011-06-10 20:26:44 +02001015 case 7:
Koushik Dutta38e8b2b2010-08-17 22:21:53 -07001016 {
Koushik Duttadf1e4062010-12-18 17:42:31 -08001017 ensure_path_mounted("/system");
1018 ensure_path_mounted("/data");
Koushik Duttaceddcd52010-08-23 16:13:14 -07001019 ui_print("Fixing permissions...\n");
1020 __system("fix_permissions");
1021 ui_print("Done!\n");
Koushik Dutta56606a22010-08-23 16:15:33 -07001022 break;
Koushik Dutta38e8b2b2010-08-17 22:21:53 -07001023 }
Tanguy Pruvot67d358c2011-06-10 20:26:44 +02001024 case 8:
Brint E. Kriebeld3522332010-12-11 20:52:06 -07001025 {
1026 static char* ext_sizes[] = { "128M",
1027 "256M",
1028 "512M",
1029 "1024M",
1030 "2048M",
1031 "4096M",
1032 NULL };
1033
1034 static char* swap_sizes[] = { "0M",
1035 "32M",
1036 "64M",
1037 "128M",
1038 "256M",
1039 NULL };
1040
1041 static char* ext_headers[] = { "Data Size", "", NULL };
1042 static char* swap_headers[] = { "Swap Size", "", NULL };
1043
Koushik Duttadf1e4062010-12-18 17:42:31 -08001044 int ext_size = get_menu_selection(ext_headers, ext_sizes, 0, 0);
Brint E. Kriebeld3522332010-12-11 20:52:06 -07001045 if (ext_size == GO_BACK)
1046 continue;
1047
1048 int swap_size = 0;
1049 if (swap_size == GO_BACK)
1050 continue;
1051
1052 char sddevice[256];
Koushik Duttadf1e4062010-12-18 17:42:31 -08001053 Volume *vol = volume_for_path("/emmc");
1054 strcpy(sddevice, vol->device);
Brint E. Kriebeld3522332010-12-11 20:52:06 -07001055 // we only want the mmcblk, not the partition
1056 sddevice[strlen("/dev/block/mmcblkX")] = NULL;
1057 char cmd[PATH_MAX];
1058 setenv("SDPATH", sddevice, 1);
1059 sprintf(cmd, "sdparted -es %s -ss %s -efs ext3 -s", ext_sizes[ext_size], swap_sizes[swap_size]);
1060 ui_print("Partitioning Internal SD Card... please wait...\n");
1061 if (0 == __system(cmd))
1062 ui_print("Done!\n");
1063 else
1064 ui_print("An error occured while partitioning your Internal SD Card. Please see /tmp/recovery.log for more details.\n");
1065 break;
1066 }
Koushik K. Duttaa496b512010-03-19 14:51:45 -07001067 }
1068 }
Koushik Dutta14239d22010-06-14 15:02:48 -07001069}
1070
Koushik Duttab643e4f2010-12-18 18:39:39 -08001071void write_fstab_root(char *path, FILE *file)
Koushik Dutta14239d22010-06-14 15:02:48 -07001072{
Koushik Duttab643e4f2010-12-18 18:39:39 -08001073 Volume *vol = volume_for_path(path);
1074 if (vol == NULL) {
Koushik Dutta9765a032010-12-18 21:31:02 -08001075 LOGW("Unable to get recovery.fstab info for %s during fstab generation!\n", path);
Koushik Dutta598cfc72010-06-20 09:42:47 -07001076 return;
Koushik Duttaa6522b32010-06-20 13:16:06 -07001077 }
Steve Kondik4123b582010-11-14 03:18:40 -05001078
Koushik Dutta31b74112010-12-20 08:49:20 -08001079 char device[200];
1080 if (vol->device[0] != '/')
1081 get_partition_device(vol->device, device);
1082 else
1083 strcpy(device, vol->device);
1084
1085 fprintf(file, "%s ", device);
Koushik Duttab643e4f2010-12-18 18:39:39 -08001086 fprintf(file, "%s ", path);
Koushik Duttaab1f8b82011-02-27 17:28:30 -08001087 // special case rfs cause auto will mount it as vfat on samsung.
1088 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 -07001089}
1090
1091void create_fstab()
1092{
Koushik Duttab643e4f2010-12-18 18:39:39 -08001093 struct stat info;
Koushik Duttacd44ab92010-06-23 00:02:14 -07001094 __system("touch /etc/mtab");
Koushik Dutta14239d22010-06-14 15:02:48 -07001095 FILE *file = fopen("/etc/fstab", "w");
Koushik Duttaa6522b32010-06-20 13:16:06 -07001096 if (file == NULL) {
Koushik Duttab643e4f2010-12-18 18:39:39 -08001097 LOGW("Unable to create /etc/fstab!\n");
Koushik Dutta598cfc72010-06-20 09:42:47 -07001098 return;
Koushik Duttaa6522b32010-06-20 13:16:06 -07001099 }
Brandon Bennett6d0604b2011-01-28 13:39:10 -07001100 Volume *vol = volume_for_path("/boot");
Koushik Duttaa9f0e7f2011-02-12 10:21:11 -08001101 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 -07001102 write_fstab_root("/boot", file);
Koushik Duttab643e4f2010-12-18 18:39:39 -08001103 write_fstab_root("/cache", file);
1104 write_fstab_root("/data", file);
Koushik Dutta89ed0b72011-10-26 21:25:34 -07001105 write_fstab_root("/datadata", file);
1106 write_fstab_root("/emmc", file);
Koushik Duttab643e4f2010-12-18 18:39:39 -08001107 write_fstab_root("/system", file);
1108 write_fstab_root("/sdcard", file);
1109 write_fstab_root("/sd-ext", file);
Koushik Dutta14239d22010-06-14 15:02:48 -07001110 fclose(file);
Koushik Dutta9765a032010-12-18 21:31:02 -08001111 LOGI("Completed outputting fstab.\n");
Koushik Dutta598cfc72010-06-20 09:42:47 -07001112}
1113
Koushik Dutta4196e0f2010-12-19 03:38:29 -08001114int bml_check_volume(const char *path) {
1115 ui_print("Checking %s...\n", path);
1116 ensure_path_unmounted(path);
1117 if (0 == ensure_path_mounted(path)) {
1118 ensure_path_unmounted(path);
1119 return 0;
1120 }
1121
1122 Volume *vol = volume_for_path(path);
1123 if (vol == NULL) {
1124 LOGE("Unable process volume! Skipping...\n");
1125 return 0;
1126 }
1127
Koushik Duttabec09952010-12-19 20:37:57 -08001128 ui_print("%s may be rfs. Checking...\n", path);
Koushik Dutta4196e0f2010-12-19 03:38:29 -08001129 char tmp[PATH_MAX];
1130 sprintf(tmp, "mount -t rfs %s %s", vol->device, path);
1131 int ret = __system(tmp);
1132 printf("%d\n", ret);
1133 return ret == 0 ? 1 : 0;
1134}
1135
1136void process_volumes() {
1137 create_fstab();
Koushik Dutta38a92142011-06-10 09:45:52 -07001138
1139 if (is_data_media()) {
1140 setup_data_media();
Koushik Dutta94caefe2011-06-10 09:17:30 -07001141 }
1142
Koushik Duttae8bc2c82011-02-27 14:00:19 -08001143 return;
1144
1145 // dead code.
Koushik Dutta4196e0f2010-12-19 03:38:29 -08001146 if (device_flash_type() != BML)
1147 return;
1148
1149 ui_print("Checking for ext4 partitions...\n");
1150 int ret = 0;
1151 ret = bml_check_volume("/system");
1152 ret |= bml_check_volume("/data");
1153 if (has_datadata())
1154 ret |= bml_check_volume("/datadata");
1155 ret |= bml_check_volume("/cache");
1156
1157 if (ret == 0) {
1158 ui_print("Done!\n");
1159 return;
1160 }
1161
Koushik Dutta745b5ff2010-12-21 00:58:43 -08001162 char backup_path[PATH_MAX];
1163 time_t t = time(NULL);
1164 char backup_name[PATH_MAX];
1165 struct timeval tp;
1166 gettimeofday(&tp, NULL);
1167 sprintf(backup_name, "before-ext4-convert-%d", tp.tv_sec);
1168 sprintf(backup_path, "/sdcard/clockworkmod/backup/%s", backup_name);
1169
Koushik Dutta4196e0f2010-12-19 03:38:29 -08001170 ui_set_show_text(1);
1171 ui_print("Filesystems need to be converted to ext4.\n");
1172 ui_print("A backup and restore will now take place.\n");
Koushik Duttabec09952010-12-19 20:37:57 -08001173 ui_print("If anything goes wrong, your backup will be\n");
Koushik Dutta745b5ff2010-12-21 00:58:43 -08001174 ui_print("named %s. Try restoring it\n", backup_name);
Koushik Duttabec09952010-12-19 20:37:57 -08001175 ui_print("in case of error.\n");
Koushik Dutta4196e0f2010-12-19 03:38:29 -08001176
Koushik Dutta745b5ff2010-12-21 00:58:43 -08001177 nandroid_backup(backup_path);
Koushik Dutta5b7f34a2010-12-29 23:36:03 -08001178 nandroid_restore(backup_path, 1, 1, 1, 1, 1, 0);
Koushik Dutta4196e0f2010-12-19 03:38:29 -08001179 ui_set_show_text(0);
1180}
1181
Koushik Dutta598cfc72010-06-20 09:42:47 -07001182void handle_failure(int ret)
1183{
1184 if (ret == 0)
1185 return;
Koushik Duttadf1e4062010-12-18 17:42:31 -08001186 if (0 != ensure_path_mounted("/sdcard"))
Koushik Dutta598cfc72010-06-20 09:42:47 -07001187 return;
Koushik Duttaa6522b32010-06-20 13:16:06 -07001188 mkdir("/sdcard/clockworkmod", S_IRWXU);
1189 __system("cp /tmp/recovery.log /sdcard/clockworkmod/recovery.log");
Koushik Dutta38e8b2b2010-08-17 22:21:53 -07001190 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 -05001191}
Koushik Duttadf1e4062010-12-18 17:42:31 -08001192
Koushik Duttadf1e4062010-12-18 17:42:31 -08001193int is_path_mounted(const char* path) {
1194 Volume* v = volume_for_path(path);
1195 if (v == NULL) {
1196 return 0;
1197 }
1198 if (strcmp(v->fs_type, "ramdisk") == 0) {
1199 // the ramdisk is always mounted.
1200 return 1;
1201 }
1202
1203 int result;
1204 result = scan_mounted_volumes();
1205 if (result < 0) {
1206 LOGE("failed to scan mounted volumes\n");
1207 return 0;
1208 }
1209
1210 const MountedVolume* mv =
1211 find_mounted_volume_by_mount_point(v->mount_point);
1212 if (mv) {
1213 // volume is already mounted
1214 return 1;
1215 }
1216 return 0;
1217}
Koushik Dutta5460f0c2010-12-18 22:37:49 -08001218
1219int has_datadata() {
1220 Volume *vol = volume_for_path("/datadata");
1221 return vol != NULL;
Koushik Duttad8e21c32011-01-04 10:46:23 -08001222}
1223
1224int volume_main(int argc, char **argv) {
1225 load_volume_table();
1226 return 0;
1227}