blob: 3c0fac0d92a923fc9474d5895c3dca53d818bbde [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>
Austen Dicken7126ef42011-06-14 15:47:40 -050011#include <reboot/reboot.h>
Koushik K. Dutta6060e5c2010-02-11 22:27:06 -080012#include <sys/types.h>
13#include <time.h>
14#include <unistd.h>
15
Koushik K. Duttae9234872010-02-12 00:43:24 -080016#include <sys/wait.h>
17#include <sys/limits.h>
18#include <dirent.h>
Koushik K. Dutta49f56892010-02-25 14:51:05 -080019#include <sys/stat.h>
Koushik K. Duttae9234872010-02-12 00:43:24 -080020
Koushik K. Dutta33370db2010-02-25 11:39:07 -080021#include <signal.h>
22#include <sys/wait.h>
23
Koushik K. Dutta6060e5c2010-02-11 22:27:06 -080024#include "bootloader.h"
25#include "common.h"
26#include "cutils/properties.h"
27#include "firmware.h"
28#include "install.h"
29#include "minui/minui.h"
30#include "minzip/DirUtil.h"
31#include "roots.h"
32#include "recovery_ui.h"
33
Koushik K. Duttaa85d7cc2010-03-12 17:00:58 -080034#include "../../external/yaffs2/yaffs2/utils/mkyaffs2image.h"
Koushik K. Dutta54305a82010-03-12 17:43:26 -080035#include "../../external/yaffs2/yaffs2/utils/unyaffs.h"
Koushik K. Duttaa85d7cc2010-03-12 17:00:58 -080036
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -080037#include "extendedcommands.h"
38#include "nandroid.h"
Koushik Duttadf1e4062010-12-18 17:42:31 -080039#include "mounts.h"
Koushik Dutta9765a032010-12-18 21:31:02 -080040#include "flashutils/flashutils.h"
Koushik Duttabec09952010-12-19 20:37:57 -080041#include "edify/expr.h"
Koushik Dutta7adeadc2011-05-26 11:47:56 -070042#include <libgen.h>
Koushik Dutta30a937a2011-09-05 21:14:06 -070043#include "mtdutils/mtdutils.h"
Koushik Dutta7adeadc2011-05-26 11:47:56 -070044
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -080045
Koushik K. Dutta6060e5c2010-02-11 22:27:06 -080046int signature_check_enabled = 1;
47int script_assert_enabled = 1;
Koushik Duttadf1e4062010-12-18 17:42:31 -080048static const char *SDCARD_UPDATE_FILE = "/sdcard/update.zip";
Koushik K. Dutta6060e5c2010-02-11 22:27:06 -080049
50void
51toggle_signature_check()
52{
53 signature_check_enabled = !signature_check_enabled;
54 ui_print("Signature Check: %s\n", signature_check_enabled ? "Enabled" : "Disabled");
55}
56
57void toggle_script_asserts()
58{
59 script_assert_enabled = !script_assert_enabled;
Koushik K. Duttae9234872010-02-12 00:43:24 -080060 ui_print("Script Asserts: %s\n", script_assert_enabled ? "Enabled" : "Disabled");
61}
62
Koushik K. Duttaea46fe22010-03-08 02:58:04 -080063int install_zip(const char* packagefilepath)
Koushik K. Duttae9234872010-02-12 00:43:24 -080064{
Koushik K. Duttabcdd0032010-02-21 21:10:25 -080065 ui_print("\n-- Installing: %s\n", packagefilepath);
Koushik Dutta9765a032010-12-18 21:31:02 -080066 if (device_flash_type() == MTD) {
67 set_sdcard_update_bootloader_message();
68 }
Koushik K. Duttabcdd0032010-02-21 21:10:25 -080069 int status = install_package(packagefilepath);
Koushik K. Duttaf3534d02010-04-18 18:06:24 -070070 ui_reset_progress();
Koushik K. Duttabcdd0032010-02-21 21:10:25 -080071 if (status != INSTALL_SUCCESS) {
72 ui_set_background(BACKGROUND_ICON_ERROR);
73 ui_print("Installation aborted.\n");
Koushik K. Duttaea46fe22010-03-08 02:58:04 -080074 return 1;
Steve Kondik4123b582010-11-14 03:18:40 -050075 }
Koushik K. Dutta001c5b52010-02-25 14:53:57 -080076 ui_set_background(BACKGROUND_ICON_NONE);
Koushik K. Dutta99fb6fe2010-03-03 00:42:58 -080077 ui_print("\nInstall from sdcard complete.\n");
Koushik K. Duttaea46fe22010-03-08 02:58:04 -080078 return 0;
Koushik K. Duttabcdd0032010-02-21 21:10:25 -080079}
80
Tanguy Pruvotaaad77f2011-06-18 10:26:32 +020081char* INSTALL_MENU_ITEMS[] = { "choose zip from sdcard",
82 "apply /sdcard/update.zip",
Koushik K. Duttabcdd0032010-02-21 21:10:25 -080083 "toggle signature verification",
84 "toggle script asserts",
Koushik Dutta30a937a2011-09-05 21:14:06 -070085 "choose zip from internal sdcard",
Koushik K. Duttabcdd0032010-02-21 21:10:25 -080086 NULL };
Tanguy Pruvotaaad77f2011-06-18 10:26:32 +020087#define ITEM_CHOOSE_ZIP 0
88#define ITEM_APPLY_SDCARD 1
Koushik K. Duttabcdd0032010-02-21 21:10:25 -080089#define ITEM_SIG_CHECK 2
90#define ITEM_ASSERTS 3
Koushik Dutta30a937a2011-09-05 21:14:06 -070091#define ITEM_CHOOSE_ZIP_INT 4
Koushik K. Duttabcdd0032010-02-21 21:10:25 -080092
93void show_install_update_menu()
94{
95 static char* headers[] = { "Apply update from .zip file on SD card",
96 "",
Steve Kondik4123b582010-11-14 03:18:40 -050097 NULL
Koushik K. Duttabcdd0032010-02-21 21:10:25 -080098 };
Koushik Dutta30a937a2011-09-05 21:14:06 -070099
100 if (volume_for_path("/emmc") == NULL)
101 INSTALL_MENU_ITEMS[ITEM_CHOOSE_ZIP_INT] = NULL;
102
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800103 for (;;)
104 {
Koushik Duttadf1e4062010-12-18 17:42:31 -0800105 int chosen_item = get_menu_selection(headers, INSTALL_MENU_ITEMS, 0, 0);
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800106 switch (chosen_item)
107 {
108 case ITEM_ASSERTS:
109 toggle_script_asserts();
110 break;
111 case ITEM_SIG_CHECK:
112 toggle_signature_check();
113 break;
114 case ITEM_APPLY_SDCARD:
Koushik Duttad63eaef2010-07-14 21:01:21 -0700115 {
116 if (confirm_selection("Confirm install?", "Yes - Install /sdcard/update.zip"))
Koushik Duttadf1e4062010-12-18 17:42:31 -0800117 install_zip(SDCARD_UPDATE_FILE);
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800118 break;
Koushik Duttad63eaef2010-07-14 21:01:21 -0700119 }
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800120 case ITEM_CHOOSE_ZIP:
Koushik Dutta30a937a2011-09-05 21:14:06 -0700121 show_choose_zip_menu("/sdcard/");
122 break;
123 case ITEM_CHOOSE_ZIP_INT:
124 show_choose_zip_menu("/emmc/");
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800125 break;
126 default:
127 return;
128 }
Steve Kondik4123b582010-11-14 03:18:40 -0500129
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800130 }
131}
132
Koushik K. Dutta1d53c4e2010-04-02 16:46:21 -0700133void free_string_array(char** array)
134{
135 if (array == NULL)
136 return;
137 char* cursor = array[0];
138 int i = 0;
139 while (cursor != NULL)
140 {
141 free(cursor);
142 cursor = array[++i];
143 }
144 free(array);
145}
146
Koushik K. Dutta49f56892010-02-25 14:51:05 -0800147char** gather_files(const char* directory, const char* fileExtensionOrDirectory, int* numFiles)
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800148{
Koushik K. Duttae9234872010-02-12 00:43:24 -0800149 char path[PATH_MAX] = "";
150 DIR *dir;
151 struct dirent *de;
152 int total = 0;
153 int i;
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800154 char** files = NULL;
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800155 int pass;
156 *numFiles = 0;
157 int dirLen = strlen(directory);
Koushik K. Duttae9234872010-02-12 00:43:24 -0800158
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800159 dir = opendir(directory);
160 if (dir == NULL) {
161 ui_print("Couldn't open directory.\n");
162 return NULL;
163 }
Steve Kondik4123b582010-11-14 03:18:40 -0500164
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800165 int extension_length = 0;
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800166 if (fileExtensionOrDirectory != NULL)
167 extension_length = strlen(fileExtensionOrDirectory);
Steve Kondik4123b582010-11-14 03:18:40 -0500168
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800169 int isCounting = 1;
170 i = 0;
171 for (pass = 0; pass < 2; pass++) {
172 while ((de=readdir(dir)) != NULL) {
173 // skip hidden files
174 if (de->d_name[0] == '.')
175 continue;
Steve Kondik4123b582010-11-14 03:18:40 -0500176
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800177 // NULL means that we are gathering directories, so skip this
178 if (fileExtensionOrDirectory != NULL)
179 {
180 // make sure that we can have the desired extension (prevent seg fault)
181 if (strlen(de->d_name) < extension_length)
182 continue;
183 // compare the extension
184 if (strcmp(de->d_name + strlen(de->d_name) - extension_length, fileExtensionOrDirectory) != 0)
185 continue;
186 }
187 else
188 {
189 struct stat info;
Koushik K. Dutta1d53c4e2010-04-02 16:46:21 -0700190 char fullFileName[PATH_MAX];
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800191 strcpy(fullFileName, directory);
192 strcat(fullFileName, de->d_name);
193 stat(fullFileName, &info);
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800194 // make sure it is a directory
195 if (!(S_ISDIR(info.st_mode)))
196 continue;
197 }
Steve Kondik4123b582010-11-14 03:18:40 -0500198
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800199 if (pass == 0)
200 {
201 total++;
202 continue;
203 }
Steve Kondik4123b582010-11-14 03:18:40 -0500204
Koushik K. Dutta49f56892010-02-25 14:51:05 -0800205 files[i] = (char*) malloc(dirLen + strlen(de->d_name) + 2);
206 strcpy(files[i], directory);
207 strcat(files[i], de->d_name);
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800208 if (fileExtensionOrDirectory == NULL)
209 strcat(files[i], "/");
210 i++;
211 }
212 if (pass == 1)
213 break;
214 if (total == 0)
215 break;
216 rewinddir(dir);
217 *numFiles = total;
218 files = (char**) malloc((total+1)*sizeof(char*));
219 files[total]=NULL;
220 }
Koushik K. Dutta49f56892010-02-25 14:51:05 -0800221
222 if(closedir(dir) < 0) {
223 LOGE("Failed to close directory.");
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800224 }
225
226 if (total==0) {
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800227 return NULL;
228 }
229
agrabren1f76a5d2010-12-29 12:15:18 -0600230 // sort the result
231 if (files != NULL) {
232 for (i = 0; i < total; i++) {
233 int curMax = -1;
234 int j;
235 for (j = 0; j < total - i; j++) {
236 if (curMax == -1 || strcmp(files[curMax], files[j]) < 0)
237 curMax = j;
238 }
239 char* temp = files[curMax];
240 files[curMax] = files[total - i - 1];
241 files[total - i - 1] = temp;
242 }
243 }
Koushik Dutta1e8aaba2010-07-01 00:23:25 -0700244
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800245 return files;
Koushik K. Dutta49f56892010-02-25 14:51:05 -0800246}
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800247
Koushik K. Dutta49f56892010-02-25 14:51:05 -0800248// pass in NULL for fileExtensionOrDirectory and you will get a directory chooser
249char* choose_file_menu(const char* directory, const char* fileExtensionOrDirectory, const char* headers[])
250{
251 char path[PATH_MAX] = "";
252 DIR *dir;
253 struct dirent *de;
254 int numFiles = 0;
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800255 int numDirs = 0;
Koushik K. Dutta49f56892010-02-25 14:51:05 -0800256 int i;
Koushik K. Dutta1d53c4e2010-04-02 16:46:21 -0700257 char* return_value = NULL;
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800258 int dir_len = strlen(directory);
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800259
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800260 char** files = gather_files(directory, fileExtensionOrDirectory, &numFiles);
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800261 char** dirs = NULL;
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800262 if (fileExtensionOrDirectory != NULL)
263 dirs = gather_files(directory, NULL, &numDirs);
264 int total = numDirs + numFiles;
265 if (total == 0)
266 {
267 ui_print("No files found.\n");
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800268 }
Koushik K. Dutta1d53c4e2010-04-02 16:46:21 -0700269 else
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800270 {
Koushik K. Dutta1d53c4e2010-04-02 16:46:21 -0700271 char** list = (char**) malloc((total + 1) * sizeof(char*));
272 list[total] = NULL;
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800273
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800274
Koushik K. Dutta1d53c4e2010-04-02 16:46:21 -0700275 for (i = 0 ; i < numDirs; i++)
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800276 {
Koushik K. Dutta1d53c4e2010-04-02 16:46:21 -0700277 list[i] = strdup(dirs[i] + dir_len);
278 }
279
280 for (i = 0 ; i < numFiles; i++)
281 {
282 list[numDirs + i] = strdup(files[i] + dir_len);
283 }
284
285 for (;;)
286 {
Koushik Duttadf1e4062010-12-18 17:42:31 -0800287 int chosen_item = get_menu_selection(headers, list, 0, 0);
Koushik K. Dutta1d53c4e2010-04-02 16:46:21 -0700288 if (chosen_item == GO_BACK)
289 break;
290 static char ret[PATH_MAX];
291 if (chosen_item < numDirs)
292 {
293 char* subret = choose_file_menu(dirs[chosen_item], fileExtensionOrDirectory, headers);
294 if (subret != NULL)
295 {
296 strcpy(ret, subret);
297 return_value = ret;
298 break;
299 }
300 continue;
Steve Kondik4123b582010-11-14 03:18:40 -0500301 }
Koushik K. Dutta1d53c4e2010-04-02 16:46:21 -0700302 strcpy(ret, files[chosen_item - numDirs]);
303 return_value = ret;
304 break;
305 }
306 free_string_array(list);
Koushik K. Dutta981b0cd2010-02-22 08:53:34 -0800307 }
Koushik K. Dutta1d53c4e2010-04-02 16:46:21 -0700308
309 free_string_array(files);
310 free_string_array(dirs);
311 return return_value;
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800312}
313
Koushik Dutta30a937a2011-09-05 21:14:06 -0700314void show_choose_zip_menu(const char *mount_point)
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800315{
Koushik Dutta30a937a2011-09-05 21:14:06 -0700316 if (ensure_path_mounted(mount_point) != 0) {
317 LOGE ("Can't mount %s\n", mount_point);
Koushik K. Duttae9234872010-02-12 00:43:24 -0800318 return;
319 }
320
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800321 static char* headers[] = { "Choose a zip to apply",
322 "",
Steve Kondik4123b582010-11-14 03:18:40 -0500323 NULL
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800324 };
Steve Kondik4123b582010-11-14 03:18:40 -0500325
Koushik Dutta30a937a2011-09-05 21:14:06 -0700326 char* file = choose_file_menu(mount_point, ".zip", headers);
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800327 if (file == NULL)
328 return;
Koushik Duttad63eaef2010-07-14 21:01:21 -0700329 static char* confirm_install = "Confirm install?";
330 static char confirm[PATH_MAX];
331 sprintf(confirm, "Yes - Install %s", basename(file));
332 if (confirm_selection(confirm_install, confirm))
Koushik Duttadf1e4062010-12-18 17:42:31 -0800333 install_zip(file);
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800334}
335
Koushik Dutta89ed0b72011-10-26 21:25:34 -0700336void show_nandroid_restore_menu(const char* path)
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800337{
Koushik Dutta89ed0b72011-10-26 21:25:34 -0700338 if (ensure_path_mounted(path) != 0) {
339 LOGE("Can't mount %s\n", path);
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800340 return;
Koushik K. Dutta54305a82010-03-12 17:43:26 -0800341 }
Steve Kondik4123b582010-11-14 03:18:40 -0500342
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800343 static char* headers[] = { "Choose an image to restore",
344 "",
Steve Kondik4123b582010-11-14 03:18:40 -0500345 NULL
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800346 };
Koushik K. Duttae9234872010-02-12 00:43:24 -0800347
Koushik Dutta89ed0b72011-10-26 21:25:34 -0700348 char tmp[PATH_MAX];
349 sprintf(tmp, "%s/clockworkmod/backup/", path);
350 char* file = choose_file_menu(tmp, NULL, headers);
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800351 if (file == NULL)
352 return;
Koushik Duttad63eaef2010-07-14 21:01:21 -0700353
354 if (confirm_selection("Confirm restore?", "Yes - Restore"))
Koushik Dutta5b7f34a2010-12-29 23:36:03 -0800355 nandroid_restore(file, 1, 1, 1, 1, 1, 0);
Koushik K. Dutta03173782010-02-26 14:14:23 -0800356}
357
jt113447d86a32011-02-27 18:54:09 -0600358#ifndef BOARD_UMS_LUNFILE
359#define BOARD_UMS_LUNFILE "/sys/devices/platform/usb_mass_storage/lun0/file"
360#endif
361
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700362void show_mount_usb_storage_menu()
Koushik K. Dutta03173782010-02-26 14:14:23 -0800363{
Koushik Duttaf1cb0d52011-01-01 20:03:30 -0800364 int fd;
Koushik Duttadf1e4062010-12-18 17:42:31 -0800365 Volume *vol = volume_for_path("/sdcard");
jt113447d86a32011-02-27 18:54:09 -0600366 if ((fd = open(BOARD_UMS_LUNFILE, O_WRONLY)) < 0) {
Koushik Duttaf1cb0d52011-01-01 20:03:30 -0800367 LOGE("Unable to open ums lunfile (%s)", strerror(errno));
368 return -1;
369 }
370
mik_osdf3004b2011-04-26 19:15:22 +0300371 if ((write(fd, vol->device, strlen(vol->device)) < 0) &&
372 (!vol->device2 || (write(fd, vol->device, strlen(vol->device2)) < 0))) {
Koushik Duttaf1cb0d52011-01-01 20:03:30 -0800373 LOGE("Unable to write to ums lunfile (%s)", strerror(errno));
374 close(fd);
375 return -1;
376 }
Koushik K. Dutta03173782010-02-26 14:14:23 -0800377 static char* headers[] = { "USB Mass Storage device",
378 "Leaving this menu unmount",
379 "your SD card from your PC.",
380 "",
Steve Kondik4123b582010-11-14 03:18:40 -0500381 NULL
Koushik K. Dutta03173782010-02-26 14:14:23 -0800382 };
Steve Kondik4123b582010-11-14 03:18:40 -0500383
Koushik K. Dutta03173782010-02-26 14:14:23 -0800384 static char* list[] = { "Unmount", NULL };
Steve Kondik4123b582010-11-14 03:18:40 -0500385
Koushik K. Dutta03173782010-02-26 14:14:23 -0800386 for (;;)
387 {
Koushik Duttadf1e4062010-12-18 17:42:31 -0800388 int chosen_item = get_menu_selection(headers, list, 0, 0);
Koushik K. Dutta03173782010-02-26 14:14:23 -0800389 if (chosen_item == GO_BACK || chosen_item == 0)
390 break;
391 }
Steve Kondik4123b582010-11-14 03:18:40 -0500392
jt113447d86a32011-02-27 18:54:09 -0600393 if ((fd = open(BOARD_UMS_LUNFILE, O_WRONLY)) < 0) {
Koushik Duttaf1cb0d52011-01-01 20:03:30 -0800394 LOGE("Unable to open ums lunfile (%s)", strerror(errno));
395 return -1;
396 }
397
398 char ch = 0;
399 if (write(fd, &ch, 1) < 0) {
400 LOGE("Unable to write to ums lunfile (%s)", strerror(errno));
401 close(fd);
402 return -1;
403 }
Koushik K. Duttae81cb752010-02-26 17:44:33 -0800404}
Koushik K. Duttaf68aaaf2010-03-07 13:39:21 -0800405
Koushik Duttad63eaef2010-07-14 21:01:21 -0700406int confirm_selection(const char* title, const char* confirm)
Koushik K. Dutta16f0b492010-03-19 14:37:11 -0700407{
Koushik Duttaceddcd52010-08-23 16:13:14 -0700408 struct stat info;
409 if (0 == stat("/sdcard/clockworkmod/.no_confirm", &info))
410 return 1;
411
Koushik Duttad63eaef2010-07-14 21:01:21 -0700412 char* confirm_headers[] = { title, " THIS CAN NOT BE UNDONE.", "", NULL };
413 char* items[] = { "No",
414 "No",
415 "No",
416 "No",
417 "No",
418 "No",
419 "No",
Koushik Duttaecd32fa2010-07-14 18:38:02 -0700420 confirm, //" Yes -- wipe partition", // [7
Koushik Duttad63eaef2010-07-14 21:01:21 -0700421 "No",
422 "No",
423 "No",
Koushik K. Dutta16f0b492010-03-19 14:37:11 -0700424 NULL };
425
Koushik Duttadf1e4062010-12-18 17:42:31 -0800426 int chosen_item = get_menu_selection(confirm_headers, items, 0, 0);
Koushik K. Dutta16f0b492010-03-19 14:37:11 -0700427 return chosen_item == 7;
428}
429
Koushik Duttada32b542011-01-01 17:55:22 -0800430#define MKE2FS_BIN "/sbin/mke2fs"
431#define TUNE2FS_BIN "/sbin/tune2fs"
432#define E2FSCK_BIN "/sbin/e2fsck"
433
Koushik Dutta30a937a2011-09-05 21:14:06 -0700434int format_device(const char *device, const char *path, const char *fs_type) {
435 Volume* v = volume_for_path(path);
436 if (v == NULL) {
437 // no /sdcard? let's assume /data/media
438 if (strstr(path, "/sdcard") == path && is_data_media()) {
439 return format_unknown_device(NULL, path, NULL);
440 }
441 // silent failure for sd-ext
442 if (strcmp(path, "/sd-ext") == 0)
443 return -1;
444 LOGE("unknown volume \"%s\"\n", path);
445 return -1;
446 }
447 if (strcmp(fs_type, "ramdisk") == 0) {
448 // you can't format the ramdisk.
449 LOGE("can't format_volume \"%s\"", path);
450 return -1;
451 }
452
453 if (strcmp(v->mount_point, path) != 0) {
454 return format_unknown_device(v->device, path, NULL);
455 }
456
457 if (ensure_path_unmounted(path) != 0) {
458 LOGE("format_volume failed to unmount \"%s\"\n", v->mount_point);
459 return -1;
460 }
461
462 if (strcmp(fs_type, "yaffs2") == 0 || strcmp(fs_type, "mtd") == 0) {
463 mtd_scan_partitions();
464 const MtdPartition* partition = mtd_find_partition_by_name(device);
465 if (partition == NULL) {
466 LOGE("format_volume: no MTD partition \"%s\"\n", device);
467 return -1;
468 }
469
470 MtdWriteContext *write = mtd_write_partition(partition);
471 if (write == NULL) {
472 LOGW("format_volume: can't open MTD \"%s\"\n", device);
473 return -1;
474 } else if (mtd_erase_blocks(write, -1) == (off_t) -1) {
475 LOGW("format_volume: can't erase MTD \"%s\"\n", device);
476 mtd_write_close(write);
477 return -1;
478 } else if (mtd_write_close(write)) {
479 LOGW("format_volume: can't close MTD \"%s\"\n",device);
480 return -1;
481 }
482 return 0;
483 }
484
485 if (strcmp(fs_type, "ext4") == 0) {
486 reset_ext4fs_info();
487 int result = make_ext4fs(device, NULL, NULL, 0, 0, 0);
488 if (result != 0) {
489 LOGE("format_volume: make_extf4fs failed on %s\n", device);
490 return -1;
491 }
492 return 0;
493 }
494
495 return format_unknown_device(device, path, fs_type);
496}
497
Koushik Duttada32b542011-01-01 17:55:22 -0800498int format_unknown_device(const char *device, const char* path, const char *fs_type)
Koushik K. Dutta3f995392010-03-30 23:29:43 -0700499{
Koushik Dutta2e454492011-01-01 18:06:17 -0800500 LOGI("Formatting unknown device.\n");
Koushik Duttaa8708c62011-01-01 18:00:27 -0800501
Koushik Duttafb3bd712011-06-16 14:13:02 -0700502 if (fs_type != NULL && get_flash_type(fs_type) != UNSUPPORTED)
Koushik Dutta8a6bc772011-05-26 11:14:15 -0700503 return erase_raw_partition(fs_type, device);
Koushik Duttada32b542011-01-01 17:55:22 -0800504
Koushik Dutta9f52e5f2011-01-02 14:11:24 -0800505 // if this is SDEXT:, don't worry about it if it does not exist.
Koushik Duttadf1e4062010-12-18 17:42:31 -0800506 if (0 == strcmp(path, "/sd-ext"))
Koushik K. Duttaf3534d02010-04-18 18:06:24 -0700507 {
508 struct stat st;
Koushik Duttadf1e4062010-12-18 17:42:31 -0800509 Volume *vol = volume_for_path("/sd-ext");
510 if (vol == NULL || 0 != stat(vol->device, &st))
Koushik K. Duttaf3534d02010-04-18 18:06:24 -0700511 {
512 ui_print("No app2sd partition found. Skipping format of /sd-ext.\n");
513 return 0;
514 }
515 }
Koushik Dutta2f73e582010-04-18 16:00:21 -0700516
Koushik Dutta9f52e5f2011-01-02 14:11:24 -0800517 if (NULL != fs_type) {
518 if (strcmp("ext3", fs_type) == 0) {
519 LOGI("Formatting ext3 device.\n");
520 if (0 != ensure_path_unmounted(path)) {
521 LOGE("Error while unmounting %s.\n", path);
522 return -12;
523 }
524 return format_ext3_device(device);
525 }
526
527 if (strcmp("ext2", fs_type) == 0) {
528 LOGI("Formatting ext2 device.\n");
529 if (0 != ensure_path_unmounted(path)) {
530 LOGE("Error while unmounting %s.\n", path);
531 return -12;
532 }
533 return format_ext2_device(device);
534 }
535 }
536
Koushik Duttadf1e4062010-12-18 17:42:31 -0800537 if (0 != ensure_path_mounted(path))
Koushik K. Dutta3f995392010-03-30 23:29:43 -0700538 {
539 ui_print("Error mounting %s!\n", path);
Koushik Duttacd44ab92010-06-23 00:02:14 -0700540 ui_print("Skipping format...\n");
541 return 0;
Koushik K. Dutta3f995392010-03-30 23:29:43 -0700542 }
543
544 static char tmp[PATH_MAX];
Koushik Dutta50822992011-06-08 19:03:27 -0700545 if (strcmp(path, "/data") == 0) {
546 sprintf(tmp, "cd /data ; for f in $(ls -a | grep -v ^media$); do rm -rf $f; done");
547 __system(tmp);
548 }
549 else {
550 sprintf(tmp, "rm -rf %s/*", path);
551 __system(tmp);
552 sprintf(tmp, "rm -rf %s/.*", path);
553 __system(tmp);
554 }
Steve Kondik4123b582010-11-14 03:18:40 -0500555
Koushik Duttadf1e4062010-12-18 17:42:31 -0800556 ensure_path_unmounted(path);
Koushik K. Dutta3f995392010-03-30 23:29:43 -0700557 return 0;
558}
559
Koushik Duttadf1e4062010-12-18 17:42:31 -0800560//#define MOUNTABLE_COUNT 5
561//#define DEVICE_COUNT 4
562//#define MMC_COUNT 2
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700563
Kolja Dummann92796ec2011-02-13 20:59:30 +0100564typedef struct {
565 char mount[255];
566 char unmount[255];
567 Volume* v;
568} MountMenuEntry;
569
570typedef struct {
571 char txt[255];
572 Volume* v;
573} FormatMenuEntry;
574
Steve Kondik0d878512011-04-09 01:41:45 -0400575int is_safe_to_format(char* name)
576{
Brandon Bennett9ad8ba22011-05-07 23:42:58 -0600577 char str[255];
578 char* partition;
j_r0dd14d9e752011-06-09 18:37:28 -0400579 property_get("ro.cwm.forbid_format", str, "/misc,/radio,/bootloader,/recovery,/efs");
Brandon Bennett9ad8ba22011-05-07 23:42:58 -0600580
581 partition = strtok(str, ", ");
582 while (partition != NULL) {
583 if (strcmp(name, partition) == 0) {
584 return 0;
585 }
586 partition = strtok(NULL, ", ");
587 }
588
589 return 1;
Steve Kondik0d878512011-04-09 01:41:45 -0400590}
591
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700592void show_partition_menu()
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700593{
Koushik Duttaceddcd52010-08-23 16:13:14 -0700594 static char* headers[] = { "Mounts and Storage Menu",
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700595 "",
Steve Kondik4123b582010-11-14 03:18:40 -0500596 NULL
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700597 };
598
Kolja Dummann92796ec2011-02-13 20:59:30 +0100599 static MountMenuEntry* mount_menue = NULL;
600 static FormatMenuEntry* format_menue = NULL;
601
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700602 typedef char* string;
Steve Kondik4123b582010-11-14 03:18:40 -0500603
Kolja Dummann92796ec2011-02-13 20:59:30 +0100604 int i, mountable_volumes, formatable_volumes;
605 int num_volumes;
606 Volume* device_volumes;
Steve Kondik4123b582010-11-14 03:18:40 -0500607
Kolja Dummann92796ec2011-02-13 20:59:30 +0100608 num_volumes = get_num_volumes();
609 device_volumes = get_device_volumes();
610
611 string options[255];
612
613 if(!device_volumes)
614 return;
615
616 mountable_volumes = 0;
617 formatable_volumes = 0;
618
619 mount_menue = malloc(num_volumes * sizeof(MountMenuEntry));
620 format_menue = malloc(num_volumes * sizeof(FormatMenuEntry));
621
622 for (i = 0; i < num_volumes; ++i) {
623 Volume* v = &device_volumes[i];
Koushik Dutta67fa0c32011-03-17 11:37:21 -0700624 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 +0100625 {
626 sprintf(&mount_menue[mountable_volumes].mount, "mount %s", v->mount_point);
627 sprintf(&mount_menue[mountable_volumes].unmount, "unmount %s", v->mount_point);
628 mount_menue[mountable_volumes].v = &device_volumes[i];
629 ++mountable_volumes;
Steve Kondik0d878512011-04-09 01:41:45 -0400630 if (is_safe_to_format(v->mount_point)) {
631 sprintf(&format_menue[formatable_volumes].txt, "format %s", v->mount_point);
632 format_menue[formatable_volumes].v = &device_volumes[i];
633 ++formatable_volumes;
634 }
Kolja Dummann92796ec2011-02-13 20:59:30 +0100635 }
Steve Kondik0d878512011-04-09 01:41:45 -0400636 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 +0100637 {
638 sprintf(&format_menue[formatable_volumes].txt, "format %s", v->mount_point);
639 format_menue[formatable_volumes].v = &device_volumes[i];
640 ++formatable_volumes;
641 }
642 }
643
Koushik Duttadf1e4062010-12-18 17:42:31 -0800644
Koushik Duttad63eaef2010-07-14 21:01:21 -0700645 static char* confirm_format = "Confirm format?";
Koushik Duttaecd32fa2010-07-14 18:38:02 -0700646 static char* confirm = "Yes - Format";
wjbcea6eba2011-04-30 21:04:04 -0400647 char confirm_string[255];
Steve Kondik4123b582010-11-14 03:18:40 -0500648
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700649 for (;;)
650 {
Steve Kondik4123b582010-11-14 03:18:40 -0500651
Kolja Dummann92796ec2011-02-13 20:59:30 +0100652 for (i = 0; i < mountable_volumes; i++)
653 {
654 MountMenuEntry* e = &mount_menue[i];
655 Volume* v = e->v;
656 if(is_path_mounted(v->mount_point))
657 options[i] = e->unmount;
658 else
659 options[i] = e->mount;
660 }
Steve Kondik4123b582010-11-14 03:18:40 -0500661
Kolja Dummann92796ec2011-02-13 20:59:30 +0100662 for (i = 0; i < formatable_volumes; i++)
663 {
664 FormatMenuEntry* e = &format_menue[i];
Steve Kondik4123b582010-11-14 03:18:40 -0500665
Kolja Dummann92796ec2011-02-13 20:59:30 +0100666 options[mountable_volumes+i] = e->txt;
667 }
668
669 options[mountable_volumes+formatable_volumes] = "mount USB storage";
670 options[mountable_volumes+formatable_volumes + 1] = NULL;
671
672 int chosen_item = get_menu_selection(headers, &options, 0, 0);
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700673 if (chosen_item == GO_BACK)
674 break;
Kolja Dummann92796ec2011-02-13 20:59:30 +0100675 if (chosen_item == (mountable_volumes+formatable_volumes))
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700676 {
677 show_mount_usb_storage_menu();
678 }
Kolja Dummann92796ec2011-02-13 20:59:30 +0100679 else if (chosen_item < mountable_volumes)
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700680 {
Kolja Dummann92796ec2011-02-13 20:59:30 +0100681 MountMenuEntry* e = &mount_menue[chosen_item];
682 Volume* v = e->v;
683
684 if (is_path_mounted(v->mount_point))
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700685 {
Kolja Dummann92796ec2011-02-13 20:59:30 +0100686 if (0 != ensure_path_unmounted(v->mount_point))
687 ui_print("Error unmounting %s!\n", v->mount_point);
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700688 }
689 else
690 {
Kolja Dummann92796ec2011-02-13 20:59:30 +0100691 if (0 != ensure_path_mounted(v->mount_point))
692 ui_print("Error mounting %s!\n", v->mount_point);
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700693 }
694 }
Kolja Dummann92796ec2011-02-13 20:59:30 +0100695 else if (chosen_item < (mountable_volumes + formatable_volumes))
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700696 {
Kolja Dummann92796ec2011-02-13 20:59:30 +0100697 chosen_item = chosen_item - mountable_volumes;
698 FormatMenuEntry* e = &format_menue[chosen_item];
699 Volume* v = e->v;
700
wjbcea6eba2011-04-30 21:04:04 -0400701 sprintf(confirm_string, "%s - %s", v->mount_point, confirm_format);
702
703 if (!confirm_selection(confirm_string, confirm))
Koushik K. Dutta16f0b492010-03-19 14:37:11 -0700704 continue;
Kolja Dummann92796ec2011-02-13 20:59:30 +0100705 ui_print("Formatting %s...\n", v->mount_point);
706 if (0 != format_volume(v->mount_point))
707 ui_print("Error formatting %s!\n", v->mount_point);
Koushik K. Dutta3f995392010-03-30 23:29:43 -0700708 else
709 ui_print("Done.\n");
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700710 }
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700711 }
Kolja Dummann92796ec2011-02-13 20:59:30 +0100712
713 free(mount_menue);
714 free(format_menue);
715
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700716}
717
Koushik Dutta89ed0b72011-10-26 21:25:34 -0700718void show_nandroid_advanced_restore_menu(const char* path)
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700719{
Koushik Duttac9ea1172011-10-29 18:48:18 -0700720 if (ensure_path_mounted(path) != 0) {
721 LOGE ("Can't mount sdcard\n");
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700722 return;
723 }
724
725 static char* advancedheaders[] = { "Choose an image to restore",
726 "",
727 "Choose an image to restore",
728 "first. The next menu will",
729 "you more options.",
730 "",
731 NULL
732 };
733
Koushik Duttac9ea1172011-10-29 18:48:18 -0700734 char tmp[PATH_MAX];
Koushik Dutta7ef88152011-11-11 00:26:32 -0800735 sprintf(tmp, "%s/clockworkmod/backup/", path);
Koushik Duttac9ea1172011-10-29 18:48:18 -0700736 char* file = choose_file_menu(tmp, NULL, advancedheaders);
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700737 if (file == NULL)
738 return;
739
740 static char* headers[] = { "Nandroid Advanced Restore",
741 "",
742 NULL
743 };
744
745 static char* list[] = { "Restore boot",
746 "Restore system",
747 "Restore data",
748 "Restore cache",
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700749 "Restore sd-ext",
agrabren1f76a5d2010-12-29 12:15:18 -0600750 "Restore wimax",
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700751 NULL
752 };
Koushik Dutta5b7f34a2010-12-29 23:36:03 -0800753
Koushik Dutta5b7f34a2010-12-29 23:36:03 -0800754 if (0 != get_partition_device("wimax", tmp)) {
755 // disable wimax restore option
756 list[5] = NULL;
757 }
Koushik Duttad63eaef2010-07-14 21:01:21 -0700758
759 static char* confirm_restore = "Confirm restore?";
760
Koushik Duttadf1e4062010-12-18 17:42:31 -0800761 int chosen_item = get_menu_selection(headers, list, 0, 0);
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700762 switch (chosen_item)
763 {
764 case 0:
Koushik Duttad63eaef2010-07-14 21:01:21 -0700765 if (confirm_selection(confirm_restore, "Yes - Restore boot"))
agrabren1f76a5d2010-12-29 12:15:18 -0600766 nandroid_restore(file, 1, 0, 0, 0, 0, 0);
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700767 break;
768 case 1:
Koushik Duttad63eaef2010-07-14 21:01:21 -0700769 if (confirm_selection(confirm_restore, "Yes - Restore system"))
agrabren1f76a5d2010-12-29 12:15:18 -0600770 nandroid_restore(file, 0, 1, 0, 0, 0, 0);
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700771 break;
772 case 2:
Koushik Duttad63eaef2010-07-14 21:01:21 -0700773 if (confirm_selection(confirm_restore, "Yes - Restore data"))
agrabren1f76a5d2010-12-29 12:15:18 -0600774 nandroid_restore(file, 0, 0, 1, 0, 0, 0);
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700775 break;
776 case 3:
Koushik Duttad63eaef2010-07-14 21:01:21 -0700777 if (confirm_selection(confirm_restore, "Yes - Restore cache"))
agrabren1f76a5d2010-12-29 12:15:18 -0600778 nandroid_restore(file, 0, 0, 0, 1, 0, 0);
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700779 break;
Koushik Dutta2f73e582010-04-18 16:00:21 -0700780 case 4:
Koushik Duttad63eaef2010-07-14 21:01:21 -0700781 if (confirm_selection(confirm_restore, "Yes - Restore sd-ext"))
agrabren1f76a5d2010-12-29 12:15:18 -0600782 nandroid_restore(file, 0, 0, 0, 0, 1, 0);
783 break;
784 case 5:
785 if (confirm_selection(confirm_restore, "Yes - Restore wimax"))
786 nandroid_restore(file, 0, 0, 0, 0, 0, 1);
Koushik Dutta2f73e582010-04-18 16:00:21 -0700787 break;
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700788 }
789}
790
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700791void show_nandroid_menu()
792{
793 static char* headers[] = { "Nandroid",
794 "",
Steve Kondik4123b582010-11-14 03:18:40 -0500795 NULL
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700796 };
797
Koushik Dutta89ed0b72011-10-26 21:25:34 -0700798 static char* list[] = { "backup",
799 "restore",
800 "advanced restore",
801 "backup to internal sdcard",
802 "restore from internal sdcard",
803 "advanced restore from internal sdcard",
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700804 NULL
805 };
806
Koushik Dutta89ed0b72011-10-26 21:25:34 -0700807 if (volume_for_path("/emmc") == NULL)
Koushik Dutta4fcd5232011-10-28 11:47:02 -0700808 list[3] = NULL;
Koushik Dutta89ed0b72011-10-26 21:25:34 -0700809
Koushik Duttadf1e4062010-12-18 17:42:31 -0800810 int chosen_item = get_menu_selection(headers, list, 0, 0);
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700811 switch (chosen_item)
812 {
813 case 0:
814 {
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700815 char backup_path[PATH_MAX];
Koushik K. Dutta6a26e7c2010-03-27 15:26:11 -0700816 time_t t = time(NULL);
817 struct tm *tmp = localtime(&t);
818 if (tmp == NULL)
819 {
820 struct timeval tp;
821 gettimeofday(&tp, NULL);
822 sprintf(backup_path, "/sdcard/clockworkmod/backup/%d", tp.tv_sec);
823 }
824 else
825 {
826 strftime(backup_path, sizeof(backup_path), "/sdcard/clockworkmod/backup/%F.%H.%M.%S", tmp);
827 }
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700828 nandroid_backup(backup_path);
829 }
830 break;
831 case 1:
Koushik Dutta89ed0b72011-10-26 21:25:34 -0700832 show_nandroid_restore_menu("/sdcard");
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700833 break;
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700834 case 2:
Koushik Dutta89ed0b72011-10-26 21:25:34 -0700835 show_nandroid_advanced_restore_menu("/sdcard");
836 break;
837 case 3:
838 {
839 char backup_path[PATH_MAX];
840 time_t t = time(NULL);
841 struct tm *tmp = localtime(&t);
842 if (tmp == NULL)
843 {
844 struct timeval tp;
845 gettimeofday(&tp, NULL);
846 sprintf(backup_path, "/emmc/clockworkmod/backup/%d", tp.tv_sec);
847 }
848 else
849 {
850 strftime(backup_path, sizeof(backup_path), "/emmc/clockworkmod/backup/%F.%H.%M.%S", tmp);
851 }
852 nandroid_backup(backup_path);
853 }
854 break;
855 case 4:
856 show_nandroid_restore_menu("/emmc");
857 break;
858 case 5:
859 show_nandroid_advanced_restore_menu("/emmc");
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700860 break;
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700861 }
Koushik K. Duttaa496b512010-03-19 14:51:45 -0700862}
863
Koushik Duttafd1579b2010-05-01 12:46:55 -0700864void wipe_battery_stats()
865{
Koushik Duttadf1e4062010-12-18 17:42:31 -0800866 ensure_path_mounted("/data");
Koushik Duttafd1579b2010-05-01 12:46:55 -0700867 remove("/data/system/batterystats.bin");
Koushik Duttadf1e4062010-12-18 17:42:31 -0800868 ensure_path_unmounted("/data");
j_r0dd3f2e2862011-07-10 22:48:39 -0400869 ui_print("Battery Stats wiped.\n");
Koushik Duttafd1579b2010-05-01 12:46:55 -0700870}
871
Koushik K. Duttaa496b512010-03-19 14:51:45 -0700872void show_advanced_menu()
873{
874 static char* headers[] = { "Advanced and Debugging Menu",
875 "",
876 NULL
877 };
878
Koushik K. Dutta7fe4d7b2010-04-06 23:04:52 -0700879 static char* list[] = { "Reboot Recovery",
Koushik Dutta49af23c2010-06-21 13:45:51 -0700880 "Wipe Dalvik Cache",
Koushik Duttafd1579b2010-05-01 12:46:55 -0700881 "Wipe Battery Stats",
Koushik Dutta598cfc72010-06-20 09:42:47 -0700882 "Report Error",
Koushik K. Dutta7fe4d7b2010-04-06 23:04:52 -0700883 "Key Test",
Tanguy Pruvot67d358c2011-06-10 20:26:44 +0200884 "Show log",
Koushik Duttae17a78d2010-08-29 12:35:10 -0700885#ifndef BOARD_HAS_SMALL_RECOVERY
Koushik Duttaceddcd52010-08-23 16:13:14 -0700886 "Partition SD Card",
887 "Fix Permissions",
Brint E. Kriebeld3522332010-12-11 20:52:06 -0700888#ifdef BOARD_HAS_SDCARD_INTERNAL
889 "Partition Internal SD Card",
890#endif
Koushik Duttae17a78d2010-08-29 12:35:10 -0700891#endif
Koushik K. Duttaa496b512010-03-19 14:51:45 -0700892 NULL
893 };
894
895 for (;;)
896 {
Koushik Duttadf1e4062010-12-18 17:42:31 -0800897 int chosen_item = get_menu_selection(headers, list, 0, 0);
Koushik K. Duttaa496b512010-03-19 14:51:45 -0700898 if (chosen_item == GO_BACK)
899 break;
900 switch (chosen_item)
901 {
902 case 0:
Tanguy Pruvot67d358c2011-06-10 20:26:44 +0200903 {
Austen Dicken7126ef42011-06-14 15:47:40 -0500904 reboot_wrapper("recovery");
Koushik K. Dutta7fe4d7b2010-04-06 23:04:52 -0700905 break;
Tanguy Pruvot67d358c2011-06-10 20:26:44 +0200906 }
Koushik K. Dutta7fe4d7b2010-04-06 23:04:52 -0700907 case 1:
Koushik Duttad63eaef2010-07-14 21:01:21 -0700908 {
Koushik Duttadf1e4062010-12-18 17:42:31 -0800909 if (0 != ensure_path_mounted("/data"))
Koushik Dutta49af23c2010-06-21 13:45:51 -0700910 break;
Koushik Duttadf1e4062010-12-18 17:42:31 -0800911 ensure_path_mounted("/sd-ext");
912 ensure_path_mounted("/cache");
Koushik Dutta6c7745d2010-08-07 12:17:13 -0700913 if (confirm_selection( "Confirm wipe?", "Yes - Wipe Dalvik Cache")) {
Koushik Duttad63eaef2010-07-14 21:01:21 -0700914 __system("rm -r /data/dalvik-cache");
Koushik Dutta6c7745d2010-08-07 12:17:13 -0700915 __system("rm -r /cache/dalvik-cache");
916 __system("rm -r /sd-ext/dalvik-cache");
Koushik Dutta30a937a2011-09-05 21:14:06 -0700917 ui_print("Dalvik Cache wiped.\n");
Koushik Dutta6c7745d2010-08-07 12:17:13 -0700918 }
Koushik Duttadf1e4062010-12-18 17:42:31 -0800919 ensure_path_unmounted("/data");
Koushik Duttafd1579b2010-05-01 12:46:55 -0700920 break;
Koushik Duttad63eaef2010-07-14 21:01:21 -0700921 }
Koushik Duttafd1579b2010-05-01 12:46:55 -0700922 case 2:
Koushik Duttad63eaef2010-07-14 21:01:21 -0700923 {
924 if (confirm_selection( "Confirm wipe?", "Yes - Wipe Battery Stats"))
925 wipe_battery_stats();
Koushik Dutta598cfc72010-06-20 09:42:47 -0700926 break;
Koushik Duttad63eaef2010-07-14 21:01:21 -0700927 }
Koushik Dutta598cfc72010-06-20 09:42:47 -0700928 case 3:
Koushik Dutta49af23c2010-06-21 13:45:51 -0700929 handle_failure(1);
930 break;
931 case 4:
Koushik K. Duttaa496b512010-03-19 14:51:45 -0700932 {
933 ui_print("Outputting key codes.\n");
934 ui_print("Go back to end debugging.\n");
935 int key;
936 int action;
937 do
938 {
939 key = ui_wait_key();
940 action = device_handle_key(key, 1);
941 ui_print("Key: %d\n", key);
942 }
943 while (action != GO_BACK);
Koushik Dutta56606a22010-08-23 16:15:33 -0700944 break;
Koushik K. Duttaa496b512010-03-19 14:51:45 -0700945 }
Koushik Duttaf0e31b82010-08-17 16:55:38 -0700946 case 5:
947 {
Tanguy Pruvot67d358c2011-06-10 20:26:44 +0200948 ui_printlogtail(12);
949 break;
950 }
951 case 6:
952 {
Koushik Duttaceddcd52010-08-23 16:13:14 -0700953 static char* ext_sizes[] = { "128M",
954 "256M",
955 "512M",
956 "1024M",
Brint E. Kriebel0b233312010-11-14 15:31:17 -0700957 "2048M",
958 "4096M",
Koushik Duttaceddcd52010-08-23 16:13:14 -0700959 NULL };
960
961 static char* swap_sizes[] = { "0M",
962 "32M",
963 "64M",
964 "128M",
965 "256M",
966 NULL };
967
968 static char* ext_headers[] = { "Ext Size", "", NULL };
969 static char* swap_headers[] = { "Swap Size", "", NULL };
970
Koushik Duttadf1e4062010-12-18 17:42:31 -0800971 int ext_size = get_menu_selection(ext_headers, ext_sizes, 0, 0);
Koushik Duttaceddcd52010-08-23 16:13:14 -0700972 if (ext_size == GO_BACK)
973 continue;
Steve Kondik4123b582010-11-14 03:18:40 -0500974
Koushik Duttadf1e4062010-12-18 17:42:31 -0800975 int swap_size = get_menu_selection(swap_headers, swap_sizes, 0, 0);
Koushik Duttaceddcd52010-08-23 16:13:14 -0700976 if (swap_size == GO_BACK)
977 continue;
978
979 char sddevice[256];
Koushik Duttadf1e4062010-12-18 17:42:31 -0800980 Volume *vol = volume_for_path("/sdcard");
981 strcpy(sddevice, vol->device);
Koushik Duttaceddcd52010-08-23 16:13:14 -0700982 // we only want the mmcblk, not the partition
983 sddevice[strlen("/dev/block/mmcblkX")] = NULL;
984 char cmd[PATH_MAX];
985 setenv("SDPATH", sddevice, 1);
986 sprintf(cmd, "sdparted -es %s -ss %s -efs ext3 -s", ext_sizes[ext_size], swap_sizes[swap_size]);
987 ui_print("Partitioning SD Card... please wait...\n");
988 if (0 == __system(cmd))
989 ui_print("Done!\n");
990 else
991 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 -0700992 break;
Koushik Duttaf0e31b82010-08-17 16:55:38 -0700993 }
Tanguy Pruvot67d358c2011-06-10 20:26:44 +0200994 case 7:
Koushik Dutta38e8b2b2010-08-17 22:21:53 -0700995 {
Koushik Duttadf1e4062010-12-18 17:42:31 -0800996 ensure_path_mounted("/system");
997 ensure_path_mounted("/data");
Koushik Duttaceddcd52010-08-23 16:13:14 -0700998 ui_print("Fixing permissions...\n");
999 __system("fix_permissions");
1000 ui_print("Done!\n");
Koushik Dutta56606a22010-08-23 16:15:33 -07001001 break;
Koushik Dutta38e8b2b2010-08-17 22:21:53 -07001002 }
Tanguy Pruvot67d358c2011-06-10 20:26:44 +02001003 case 8:
Brint E. Kriebeld3522332010-12-11 20:52:06 -07001004 {
1005 static char* ext_sizes[] = { "128M",
1006 "256M",
1007 "512M",
1008 "1024M",
1009 "2048M",
1010 "4096M",
1011 NULL };
1012
1013 static char* swap_sizes[] = { "0M",
1014 "32M",
1015 "64M",
1016 "128M",
1017 "256M",
1018 NULL };
1019
1020 static char* ext_headers[] = { "Data Size", "", NULL };
1021 static char* swap_headers[] = { "Swap Size", "", NULL };
1022
Koushik Duttadf1e4062010-12-18 17:42:31 -08001023 int ext_size = get_menu_selection(ext_headers, ext_sizes, 0, 0);
Brint E. Kriebeld3522332010-12-11 20:52:06 -07001024 if (ext_size == GO_BACK)
1025 continue;
1026
1027 int swap_size = 0;
1028 if (swap_size == GO_BACK)
1029 continue;
1030
1031 char sddevice[256];
Koushik Duttadf1e4062010-12-18 17:42:31 -08001032 Volume *vol = volume_for_path("/emmc");
1033 strcpy(sddevice, vol->device);
Brint E. Kriebeld3522332010-12-11 20:52:06 -07001034 // we only want the mmcblk, not the partition
1035 sddevice[strlen("/dev/block/mmcblkX")] = NULL;
1036 char cmd[PATH_MAX];
1037 setenv("SDPATH", sddevice, 1);
1038 sprintf(cmd, "sdparted -es %s -ss %s -efs ext3 -s", ext_sizes[ext_size], swap_sizes[swap_size]);
1039 ui_print("Partitioning Internal SD Card... please wait...\n");
1040 if (0 == __system(cmd))
1041 ui_print("Done!\n");
1042 else
1043 ui_print("An error occured while partitioning your Internal SD Card. Please see /tmp/recovery.log for more details.\n");
1044 break;
1045 }
Koushik K. Duttaa496b512010-03-19 14:51:45 -07001046 }
1047 }
Koushik Dutta14239d22010-06-14 15:02:48 -07001048}
1049
Koushik Duttab643e4f2010-12-18 18:39:39 -08001050void write_fstab_root(char *path, FILE *file)
Koushik Dutta14239d22010-06-14 15:02:48 -07001051{
Koushik Duttab643e4f2010-12-18 18:39:39 -08001052 Volume *vol = volume_for_path(path);
1053 if (vol == NULL) {
Koushik Dutta9765a032010-12-18 21:31:02 -08001054 LOGW("Unable to get recovery.fstab info for %s during fstab generation!\n", path);
Koushik Dutta598cfc72010-06-20 09:42:47 -07001055 return;
Koushik Duttaa6522b32010-06-20 13:16:06 -07001056 }
Steve Kondik4123b582010-11-14 03:18:40 -05001057
Koushik Dutta31b74112010-12-20 08:49:20 -08001058 char device[200];
1059 if (vol->device[0] != '/')
1060 get_partition_device(vol->device, device);
1061 else
1062 strcpy(device, vol->device);
1063
1064 fprintf(file, "%s ", device);
Koushik Duttab643e4f2010-12-18 18:39:39 -08001065 fprintf(file, "%s ", path);
Koushik Duttaab1f8b82011-02-27 17:28:30 -08001066 // special case rfs cause auto will mount it as vfat on samsung.
1067 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 -07001068}
1069
1070void create_fstab()
1071{
Koushik Duttab643e4f2010-12-18 18:39:39 -08001072 struct stat info;
Koushik Duttacd44ab92010-06-23 00:02:14 -07001073 __system("touch /etc/mtab");
Koushik Dutta14239d22010-06-14 15:02:48 -07001074 FILE *file = fopen("/etc/fstab", "w");
Koushik Duttaa6522b32010-06-20 13:16:06 -07001075 if (file == NULL) {
Koushik Duttab643e4f2010-12-18 18:39:39 -08001076 LOGW("Unable to create /etc/fstab!\n");
Koushik Dutta598cfc72010-06-20 09:42:47 -07001077 return;
Koushik Duttaa6522b32010-06-20 13:16:06 -07001078 }
Brandon Bennett6d0604b2011-01-28 13:39:10 -07001079 Volume *vol = volume_for_path("/boot");
Koushik Duttaa9f0e7f2011-02-12 10:21:11 -08001080 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 -07001081 write_fstab_root("/boot", file);
Koushik Duttab643e4f2010-12-18 18:39:39 -08001082 write_fstab_root("/cache", file);
1083 write_fstab_root("/data", file);
Koushik Dutta89ed0b72011-10-26 21:25:34 -07001084 write_fstab_root("/datadata", file);
1085 write_fstab_root("/emmc", file);
Koushik Duttab643e4f2010-12-18 18:39:39 -08001086 write_fstab_root("/system", file);
1087 write_fstab_root("/sdcard", file);
1088 write_fstab_root("/sd-ext", file);
Koushik Dutta14239d22010-06-14 15:02:48 -07001089 fclose(file);
Koushik Dutta9765a032010-12-18 21:31:02 -08001090 LOGI("Completed outputting fstab.\n");
Koushik Dutta598cfc72010-06-20 09:42:47 -07001091}
1092
Koushik Dutta4196e0f2010-12-19 03:38:29 -08001093int bml_check_volume(const char *path) {
1094 ui_print("Checking %s...\n", path);
1095 ensure_path_unmounted(path);
1096 if (0 == ensure_path_mounted(path)) {
1097 ensure_path_unmounted(path);
1098 return 0;
1099 }
1100
1101 Volume *vol = volume_for_path(path);
1102 if (vol == NULL) {
1103 LOGE("Unable process volume! Skipping...\n");
1104 return 0;
1105 }
1106
Koushik Duttabec09952010-12-19 20:37:57 -08001107 ui_print("%s may be rfs. Checking...\n", path);
Koushik Dutta4196e0f2010-12-19 03:38:29 -08001108 char tmp[PATH_MAX];
1109 sprintf(tmp, "mount -t rfs %s %s", vol->device, path);
1110 int ret = __system(tmp);
1111 printf("%d\n", ret);
1112 return ret == 0 ? 1 : 0;
1113}
1114
1115void process_volumes() {
1116 create_fstab();
Koushik Dutta38a92142011-06-10 09:45:52 -07001117
1118 if (is_data_media()) {
1119 setup_data_media();
Koushik Dutta94caefe2011-06-10 09:17:30 -07001120 }
1121
Koushik Duttae8bc2c82011-02-27 14:00:19 -08001122 return;
1123
1124 // dead code.
Koushik Dutta4196e0f2010-12-19 03:38:29 -08001125 if (device_flash_type() != BML)
1126 return;
1127
1128 ui_print("Checking for ext4 partitions...\n");
1129 int ret = 0;
1130 ret = bml_check_volume("/system");
1131 ret |= bml_check_volume("/data");
1132 if (has_datadata())
1133 ret |= bml_check_volume("/datadata");
1134 ret |= bml_check_volume("/cache");
1135
1136 if (ret == 0) {
1137 ui_print("Done!\n");
1138 return;
1139 }
1140
Koushik Dutta745b5ff2010-12-21 00:58:43 -08001141 char backup_path[PATH_MAX];
1142 time_t t = time(NULL);
1143 char backup_name[PATH_MAX];
1144 struct timeval tp;
1145 gettimeofday(&tp, NULL);
1146 sprintf(backup_name, "before-ext4-convert-%d", tp.tv_sec);
1147 sprintf(backup_path, "/sdcard/clockworkmod/backup/%s", backup_name);
1148
Koushik Dutta4196e0f2010-12-19 03:38:29 -08001149 ui_set_show_text(1);
1150 ui_print("Filesystems need to be converted to ext4.\n");
1151 ui_print("A backup and restore will now take place.\n");
Koushik Duttabec09952010-12-19 20:37:57 -08001152 ui_print("If anything goes wrong, your backup will be\n");
Koushik Dutta745b5ff2010-12-21 00:58:43 -08001153 ui_print("named %s. Try restoring it\n", backup_name);
Koushik Duttabec09952010-12-19 20:37:57 -08001154 ui_print("in case of error.\n");
Koushik Dutta4196e0f2010-12-19 03:38:29 -08001155
Koushik Dutta745b5ff2010-12-21 00:58:43 -08001156 nandroid_backup(backup_path);
Koushik Dutta5b7f34a2010-12-29 23:36:03 -08001157 nandroid_restore(backup_path, 1, 1, 1, 1, 1, 0);
Koushik Dutta4196e0f2010-12-19 03:38:29 -08001158 ui_set_show_text(0);
1159}
1160
Koushik Dutta598cfc72010-06-20 09:42:47 -07001161void handle_failure(int ret)
1162{
1163 if (ret == 0)
1164 return;
Koushik Duttadf1e4062010-12-18 17:42:31 -08001165 if (0 != ensure_path_mounted("/sdcard"))
Koushik Dutta598cfc72010-06-20 09:42:47 -07001166 return;
Koushik Duttaa6522b32010-06-20 13:16:06 -07001167 mkdir("/sdcard/clockworkmod", S_IRWXU);
1168 __system("cp /tmp/recovery.log /sdcard/clockworkmod/recovery.log");
Koushik Dutta38e8b2b2010-08-17 22:21:53 -07001169 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 -05001170}
Koushik Duttadf1e4062010-12-18 17:42:31 -08001171
Koushik Duttadf1e4062010-12-18 17:42:31 -08001172int is_path_mounted(const char* path) {
1173 Volume* v = volume_for_path(path);
1174 if (v == NULL) {
1175 return 0;
1176 }
1177 if (strcmp(v->fs_type, "ramdisk") == 0) {
1178 // the ramdisk is always mounted.
1179 return 1;
1180 }
1181
1182 int result;
1183 result = scan_mounted_volumes();
1184 if (result < 0) {
1185 LOGE("failed to scan mounted volumes\n");
1186 return 0;
1187 }
1188
1189 const MountedVolume* mv =
1190 find_mounted_volume_by_mount_point(v->mount_point);
1191 if (mv) {
1192 // volume is already mounted
1193 return 1;
1194 }
1195 return 0;
1196}
Koushik Dutta5460f0c2010-12-18 22:37:49 -08001197
1198int has_datadata() {
1199 Volume *vol = volume_for_path("/datadata");
1200 return vol != NULL;
Koushik Duttad8e21c32011-01-04 10:46:23 -08001201}
1202
1203int volume_main(int argc, char **argv) {
1204 load_volume_table();
1205 return 0;
1206}