blob: 2b6d5c3aa50fe4ceba2eaa5578b33814d4436f06 [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 K. Duttabcdd0032010-02-21 21:10:25 -0800336void show_nandroid_restore_menu()
337{
Koushik Duttadf1e4062010-12-18 17:42:31 -0800338 if (ensure_path_mounted("/sdcard") != 0) {
Koushik K. Dutta54305a82010-03-12 17:43:26 -0800339 LOGE ("Can't mount /sdcard\n");
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 K. Dutta49f56892010-02-25 14:51:05 -0800348 char* file = choose_file_menu("/sdcard/clockworkmod/backup/", NULL, headers);
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800349 if (file == NULL)
350 return;
Koushik Duttad63eaef2010-07-14 21:01:21 -0700351
352 if (confirm_selection("Confirm restore?", "Yes - Restore"))
Koushik Dutta5b7f34a2010-12-29 23:36:03 -0800353 nandroid_restore(file, 1, 1, 1, 1, 1, 0);
Koushik K. Dutta03173782010-02-26 14:14:23 -0800354}
355
jt113447d86a32011-02-27 18:54:09 -0600356#ifndef BOARD_UMS_LUNFILE
357#define BOARD_UMS_LUNFILE "/sys/devices/platform/usb_mass_storage/lun0/file"
358#endif
359
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700360void show_mount_usb_storage_menu()
Koushik K. Dutta03173782010-02-26 14:14:23 -0800361{
Koushik Duttaf1cb0d52011-01-01 20:03:30 -0800362 int fd;
Koushik Duttadf1e4062010-12-18 17:42:31 -0800363 Volume *vol = volume_for_path("/sdcard");
jt113447d86a32011-02-27 18:54:09 -0600364 if ((fd = open(BOARD_UMS_LUNFILE, O_WRONLY)) < 0) {
Koushik Duttaf1cb0d52011-01-01 20:03:30 -0800365 LOGE("Unable to open ums lunfile (%s)", strerror(errno));
366 return -1;
367 }
368
mik_osdf3004b2011-04-26 19:15:22 +0300369 if ((write(fd, vol->device, strlen(vol->device)) < 0) &&
370 (!vol->device2 || (write(fd, vol->device, strlen(vol->device2)) < 0))) {
Koushik Duttaf1cb0d52011-01-01 20:03:30 -0800371 LOGE("Unable to write to ums lunfile (%s)", strerror(errno));
372 close(fd);
373 return -1;
374 }
Koushik K. Dutta03173782010-02-26 14:14:23 -0800375 static char* headers[] = { "USB Mass Storage device",
376 "Leaving this menu unmount",
377 "your SD card from your PC.",
378 "",
Steve Kondik4123b582010-11-14 03:18:40 -0500379 NULL
Koushik K. Dutta03173782010-02-26 14:14:23 -0800380 };
Steve Kondik4123b582010-11-14 03:18:40 -0500381
Koushik K. Dutta03173782010-02-26 14:14:23 -0800382 static char* list[] = { "Unmount", NULL };
Steve Kondik4123b582010-11-14 03:18:40 -0500383
Koushik K. Dutta03173782010-02-26 14:14:23 -0800384 for (;;)
385 {
Koushik Duttadf1e4062010-12-18 17:42:31 -0800386 int chosen_item = get_menu_selection(headers, list, 0, 0);
Koushik K. Dutta03173782010-02-26 14:14:23 -0800387 if (chosen_item == GO_BACK || chosen_item == 0)
388 break;
389 }
Steve Kondik4123b582010-11-14 03:18:40 -0500390
jt113447d86a32011-02-27 18:54:09 -0600391 if ((fd = open(BOARD_UMS_LUNFILE, O_WRONLY)) < 0) {
Koushik Duttaf1cb0d52011-01-01 20:03:30 -0800392 LOGE("Unable to open ums lunfile (%s)", strerror(errno));
393 return -1;
394 }
395
396 char ch = 0;
397 if (write(fd, &ch, 1) < 0) {
398 LOGE("Unable to write to ums lunfile (%s)", strerror(errno));
399 close(fd);
400 return -1;
401 }
Koushik K. Duttae81cb752010-02-26 17:44:33 -0800402}
Koushik K. Duttaf68aaaf2010-03-07 13:39:21 -0800403
Koushik Duttad63eaef2010-07-14 21:01:21 -0700404int confirm_selection(const char* title, const char* confirm)
Koushik K. Dutta16f0b492010-03-19 14:37:11 -0700405{
Koushik Duttaceddcd52010-08-23 16:13:14 -0700406 struct stat info;
407 if (0 == stat("/sdcard/clockworkmod/.no_confirm", &info))
408 return 1;
409
Koushik Duttad63eaef2010-07-14 21:01:21 -0700410 char* confirm_headers[] = { title, " THIS CAN NOT BE UNDONE.", "", NULL };
411 char* items[] = { "No",
412 "No",
413 "No",
414 "No",
415 "No",
416 "No",
417 "No",
Koushik Duttaecd32fa2010-07-14 18:38:02 -0700418 confirm, //" Yes -- wipe partition", // [7
Koushik Duttad63eaef2010-07-14 21:01:21 -0700419 "No",
420 "No",
421 "No",
Koushik K. Dutta16f0b492010-03-19 14:37:11 -0700422 NULL };
423
Koushik Duttadf1e4062010-12-18 17:42:31 -0800424 int chosen_item = get_menu_selection(confirm_headers, items, 0, 0);
Koushik K. Dutta16f0b492010-03-19 14:37:11 -0700425 return chosen_item == 7;
426}
427
Koushik Duttada32b542011-01-01 17:55:22 -0800428#define MKE2FS_BIN "/sbin/mke2fs"
429#define TUNE2FS_BIN "/sbin/tune2fs"
430#define E2FSCK_BIN "/sbin/e2fsck"
431
Koushik Dutta30a937a2011-09-05 21:14:06 -0700432int format_device(const char *device, const char *path, const char *fs_type) {
433 Volume* v = volume_for_path(path);
434 if (v == NULL) {
435 // no /sdcard? let's assume /data/media
436 if (strstr(path, "/sdcard") == path && is_data_media()) {
437 return format_unknown_device(NULL, path, NULL);
438 }
439 // silent failure for sd-ext
440 if (strcmp(path, "/sd-ext") == 0)
441 return -1;
442 LOGE("unknown volume \"%s\"\n", path);
443 return -1;
444 }
445 if (strcmp(fs_type, "ramdisk") == 0) {
446 // you can't format the ramdisk.
447 LOGE("can't format_volume \"%s\"", path);
448 return -1;
449 }
450
451 if (strcmp(v->mount_point, path) != 0) {
452 return format_unknown_device(v->device, path, NULL);
453 }
454
455 if (ensure_path_unmounted(path) != 0) {
456 LOGE("format_volume failed to unmount \"%s\"\n", v->mount_point);
457 return -1;
458 }
459
460 if (strcmp(fs_type, "yaffs2") == 0 || strcmp(fs_type, "mtd") == 0) {
461 mtd_scan_partitions();
462 const MtdPartition* partition = mtd_find_partition_by_name(device);
463 if (partition == NULL) {
464 LOGE("format_volume: no MTD partition \"%s\"\n", device);
465 return -1;
466 }
467
468 MtdWriteContext *write = mtd_write_partition(partition);
469 if (write == NULL) {
470 LOGW("format_volume: can't open MTD \"%s\"\n", device);
471 return -1;
472 } else if (mtd_erase_blocks(write, -1) == (off_t) -1) {
473 LOGW("format_volume: can't erase MTD \"%s\"\n", device);
474 mtd_write_close(write);
475 return -1;
476 } else if (mtd_write_close(write)) {
477 LOGW("format_volume: can't close MTD \"%s\"\n",device);
478 return -1;
479 }
480 return 0;
481 }
482
483 if (strcmp(fs_type, "ext4") == 0) {
484 reset_ext4fs_info();
485 int result = make_ext4fs(device, NULL, NULL, 0, 0, 0);
486 if (result != 0) {
487 LOGE("format_volume: make_extf4fs failed on %s\n", device);
488 return -1;
489 }
490 return 0;
491 }
492
493 return format_unknown_device(device, path, fs_type);
494}
495
Koushik Duttada32b542011-01-01 17:55:22 -0800496int format_unknown_device(const char *device, const char* path, const char *fs_type)
Koushik K. Dutta3f995392010-03-30 23:29:43 -0700497{
Koushik Dutta2e454492011-01-01 18:06:17 -0800498 LOGI("Formatting unknown device.\n");
Koushik Duttaa8708c62011-01-01 18:00:27 -0800499
Koushik Duttafb3bd712011-06-16 14:13:02 -0700500 if (fs_type != NULL && get_flash_type(fs_type) != UNSUPPORTED)
Koushik Dutta8a6bc772011-05-26 11:14:15 -0700501 return erase_raw_partition(fs_type, device);
Koushik Duttada32b542011-01-01 17:55:22 -0800502
Koushik Dutta9f52e5f2011-01-02 14:11:24 -0800503 // if this is SDEXT:, don't worry about it if it does not exist.
Koushik Duttadf1e4062010-12-18 17:42:31 -0800504 if (0 == strcmp(path, "/sd-ext"))
Koushik K. Duttaf3534d02010-04-18 18:06:24 -0700505 {
506 struct stat st;
Koushik Duttadf1e4062010-12-18 17:42:31 -0800507 Volume *vol = volume_for_path("/sd-ext");
508 if (vol == NULL || 0 != stat(vol->device, &st))
Koushik K. Duttaf3534d02010-04-18 18:06:24 -0700509 {
510 ui_print("No app2sd partition found. Skipping format of /sd-ext.\n");
511 return 0;
512 }
513 }
Koushik Dutta2f73e582010-04-18 16:00:21 -0700514
Koushik Dutta9f52e5f2011-01-02 14:11:24 -0800515 if (NULL != fs_type) {
516 if (strcmp("ext3", fs_type) == 0) {
517 LOGI("Formatting ext3 device.\n");
518 if (0 != ensure_path_unmounted(path)) {
519 LOGE("Error while unmounting %s.\n", path);
520 return -12;
521 }
522 return format_ext3_device(device);
523 }
524
525 if (strcmp("ext2", fs_type) == 0) {
526 LOGI("Formatting ext2 device.\n");
527 if (0 != ensure_path_unmounted(path)) {
528 LOGE("Error while unmounting %s.\n", path);
529 return -12;
530 }
531 return format_ext2_device(device);
532 }
533 }
534
Koushik Duttadf1e4062010-12-18 17:42:31 -0800535 if (0 != ensure_path_mounted(path))
Koushik K. Dutta3f995392010-03-30 23:29:43 -0700536 {
537 ui_print("Error mounting %s!\n", path);
Koushik Duttacd44ab92010-06-23 00:02:14 -0700538 ui_print("Skipping format...\n");
539 return 0;
Koushik K. Dutta3f995392010-03-30 23:29:43 -0700540 }
541
542 static char tmp[PATH_MAX];
Koushik Dutta50822992011-06-08 19:03:27 -0700543 if (strcmp(path, "/data") == 0) {
544 sprintf(tmp, "cd /data ; for f in $(ls -a | grep -v ^media$); do rm -rf $f; done");
545 __system(tmp);
546 }
547 else {
548 sprintf(tmp, "rm -rf %s/*", path);
549 __system(tmp);
550 sprintf(tmp, "rm -rf %s/.*", path);
551 __system(tmp);
552 }
Steve Kondik4123b582010-11-14 03:18:40 -0500553
Koushik Duttadf1e4062010-12-18 17:42:31 -0800554 ensure_path_unmounted(path);
Koushik K. Dutta3f995392010-03-30 23:29:43 -0700555 return 0;
556}
557
Koushik Duttadf1e4062010-12-18 17:42:31 -0800558//#define MOUNTABLE_COUNT 5
559//#define DEVICE_COUNT 4
560//#define MMC_COUNT 2
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700561
Kolja Dummann92796ec2011-02-13 20:59:30 +0100562typedef struct {
563 char mount[255];
564 char unmount[255];
565 Volume* v;
566} MountMenuEntry;
567
568typedef struct {
569 char txt[255];
570 Volume* v;
571} FormatMenuEntry;
572
Steve Kondik0d878512011-04-09 01:41:45 -0400573int is_safe_to_format(char* name)
574{
Brandon Bennett9ad8ba22011-05-07 23:42:58 -0600575 char str[255];
576 char* partition;
j_r0dd14d9e752011-06-09 18:37:28 -0400577 property_get("ro.cwm.forbid_format", str, "/misc,/radio,/bootloader,/recovery,/efs");
Brandon Bennett9ad8ba22011-05-07 23:42:58 -0600578
579 partition = strtok(str, ", ");
580 while (partition != NULL) {
581 if (strcmp(name, partition) == 0) {
582 return 0;
583 }
584 partition = strtok(NULL, ", ");
585 }
586
587 return 1;
Steve Kondik0d878512011-04-09 01:41:45 -0400588}
589
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700590void show_partition_menu()
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700591{
Koushik Duttaceddcd52010-08-23 16:13:14 -0700592 static char* headers[] = { "Mounts and Storage Menu",
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700593 "",
Steve Kondik4123b582010-11-14 03:18:40 -0500594 NULL
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700595 };
596
Kolja Dummann92796ec2011-02-13 20:59:30 +0100597 static MountMenuEntry* mount_menue = NULL;
598 static FormatMenuEntry* format_menue = NULL;
599
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700600 typedef char* string;
Steve Kondik4123b582010-11-14 03:18:40 -0500601
Kolja Dummann92796ec2011-02-13 20:59:30 +0100602 int i, mountable_volumes, formatable_volumes;
603 int num_volumes;
604 Volume* device_volumes;
Steve Kondik4123b582010-11-14 03:18:40 -0500605
Kolja Dummann92796ec2011-02-13 20:59:30 +0100606 num_volumes = get_num_volumes();
607 device_volumes = get_device_volumes();
608
609 string options[255];
610
611 if(!device_volumes)
612 return;
613
614 mountable_volumes = 0;
615 formatable_volumes = 0;
616
617 mount_menue = malloc(num_volumes * sizeof(MountMenuEntry));
618 format_menue = malloc(num_volumes * sizeof(FormatMenuEntry));
619
620 for (i = 0; i < num_volumes; ++i) {
621 Volume* v = &device_volumes[i];
Koushik Dutta67fa0c32011-03-17 11:37:21 -0700622 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 +0100623 {
624 sprintf(&mount_menue[mountable_volumes].mount, "mount %s", v->mount_point);
625 sprintf(&mount_menue[mountable_volumes].unmount, "unmount %s", v->mount_point);
626 mount_menue[mountable_volumes].v = &device_volumes[i];
627 ++mountable_volumes;
Steve Kondik0d878512011-04-09 01:41:45 -0400628 if (is_safe_to_format(v->mount_point)) {
629 sprintf(&format_menue[formatable_volumes].txt, "format %s", v->mount_point);
630 format_menue[formatable_volumes].v = &device_volumes[i];
631 ++formatable_volumes;
632 }
Kolja Dummann92796ec2011-02-13 20:59:30 +0100633 }
Steve Kondik0d878512011-04-09 01:41:45 -0400634 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 +0100635 {
636 sprintf(&format_menue[formatable_volumes].txt, "format %s", v->mount_point);
637 format_menue[formatable_volumes].v = &device_volumes[i];
638 ++formatable_volumes;
639 }
640 }
641
Koushik Duttadf1e4062010-12-18 17:42:31 -0800642
Koushik Duttad63eaef2010-07-14 21:01:21 -0700643 static char* confirm_format = "Confirm format?";
Koushik Duttaecd32fa2010-07-14 18:38:02 -0700644 static char* confirm = "Yes - Format";
wjbcea6eba2011-04-30 21:04:04 -0400645 char confirm_string[255];
Steve Kondik4123b582010-11-14 03:18:40 -0500646
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700647 for (;;)
648 {
Steve Kondik4123b582010-11-14 03:18:40 -0500649
Kolja Dummann92796ec2011-02-13 20:59:30 +0100650 for (i = 0; i < mountable_volumes; i++)
651 {
652 MountMenuEntry* e = &mount_menue[i];
653 Volume* v = e->v;
654 if(is_path_mounted(v->mount_point))
655 options[i] = e->unmount;
656 else
657 options[i] = e->mount;
658 }
Steve Kondik4123b582010-11-14 03:18:40 -0500659
Kolja Dummann92796ec2011-02-13 20:59:30 +0100660 for (i = 0; i < formatable_volumes; i++)
661 {
662 FormatMenuEntry* e = &format_menue[i];
Steve Kondik4123b582010-11-14 03:18:40 -0500663
Kolja Dummann92796ec2011-02-13 20:59:30 +0100664 options[mountable_volumes+i] = e->txt;
665 }
666
667 options[mountable_volumes+formatable_volumes] = "mount USB storage";
668 options[mountable_volumes+formatable_volumes + 1] = NULL;
669
670 int chosen_item = get_menu_selection(headers, &options, 0, 0);
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700671 if (chosen_item == GO_BACK)
672 break;
Kolja Dummann92796ec2011-02-13 20:59:30 +0100673 if (chosen_item == (mountable_volumes+formatable_volumes))
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700674 {
675 show_mount_usb_storage_menu();
676 }
Kolja Dummann92796ec2011-02-13 20:59:30 +0100677 else if (chosen_item < mountable_volumes)
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700678 {
Kolja Dummann92796ec2011-02-13 20:59:30 +0100679 MountMenuEntry* e = &mount_menue[chosen_item];
680 Volume* v = e->v;
681
682 if (is_path_mounted(v->mount_point))
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700683 {
Kolja Dummann92796ec2011-02-13 20:59:30 +0100684 if (0 != ensure_path_unmounted(v->mount_point))
685 ui_print("Error unmounting %s!\n", v->mount_point);
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700686 }
687 else
688 {
Kolja Dummann92796ec2011-02-13 20:59:30 +0100689 if (0 != ensure_path_mounted(v->mount_point))
690 ui_print("Error mounting %s!\n", v->mount_point);
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700691 }
692 }
Kolja Dummann92796ec2011-02-13 20:59:30 +0100693 else if (chosen_item < (mountable_volumes + formatable_volumes))
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700694 {
Kolja Dummann92796ec2011-02-13 20:59:30 +0100695 chosen_item = chosen_item - mountable_volumes;
696 FormatMenuEntry* e = &format_menue[chosen_item];
697 Volume* v = e->v;
698
wjbcea6eba2011-04-30 21:04:04 -0400699 sprintf(confirm_string, "%s - %s", v->mount_point, confirm_format);
700
701 if (!confirm_selection(confirm_string, confirm))
Koushik K. Dutta16f0b492010-03-19 14:37:11 -0700702 continue;
Kolja Dummann92796ec2011-02-13 20:59:30 +0100703 ui_print("Formatting %s...\n", v->mount_point);
704 if (0 != format_volume(v->mount_point))
705 ui_print("Error formatting %s!\n", v->mount_point);
Koushik K. Dutta3f995392010-03-30 23:29:43 -0700706 else
707 ui_print("Done.\n");
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700708 }
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700709 }
Kolja Dummann92796ec2011-02-13 20:59:30 +0100710
711 free(mount_menue);
712 free(format_menue);
713
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700714}
715
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700716void show_nandroid_advanced_restore_menu()
717{
Koushik Duttadf1e4062010-12-18 17:42:31 -0800718 if (ensure_path_mounted("/sdcard") != 0) {
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700719 LOGE ("Can't mount /sdcard\n");
720 return;
721 }
722
723 static char* advancedheaders[] = { "Choose an image to restore",
724 "",
725 "Choose an image to restore",
726 "first. The next menu will",
727 "you more options.",
728 "",
729 NULL
730 };
731
732 char* file = choose_file_menu("/sdcard/clockworkmod/backup/", NULL, advancedheaders);
733 if (file == NULL)
734 return;
735
736 static char* headers[] = { "Nandroid Advanced Restore",
737 "",
738 NULL
739 };
740
741 static char* list[] = { "Restore boot",
742 "Restore system",
743 "Restore data",
744 "Restore cache",
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700745 "Restore sd-ext",
agrabren1f76a5d2010-12-29 12:15:18 -0600746 "Restore wimax",
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700747 NULL
748 };
Koushik Dutta5b7f34a2010-12-29 23:36:03 -0800749
750 char tmp[PATH_MAX];
751 if (0 != get_partition_device("wimax", tmp)) {
752 // disable wimax restore option
753 list[5] = NULL;
754 }
Koushik Duttad63eaef2010-07-14 21:01:21 -0700755
756 static char* confirm_restore = "Confirm restore?";
757
Koushik Duttadf1e4062010-12-18 17:42:31 -0800758 int chosen_item = get_menu_selection(headers, list, 0, 0);
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700759 switch (chosen_item)
760 {
761 case 0:
Koushik Duttad63eaef2010-07-14 21:01:21 -0700762 if (confirm_selection(confirm_restore, "Yes - Restore boot"))
agrabren1f76a5d2010-12-29 12:15:18 -0600763 nandroid_restore(file, 1, 0, 0, 0, 0, 0);
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700764 break;
765 case 1:
Koushik Duttad63eaef2010-07-14 21:01:21 -0700766 if (confirm_selection(confirm_restore, "Yes - Restore system"))
agrabren1f76a5d2010-12-29 12:15:18 -0600767 nandroid_restore(file, 0, 1, 0, 0, 0, 0);
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700768 break;
769 case 2:
Koushik Duttad63eaef2010-07-14 21:01:21 -0700770 if (confirm_selection(confirm_restore, "Yes - Restore data"))
agrabren1f76a5d2010-12-29 12:15:18 -0600771 nandroid_restore(file, 0, 0, 1, 0, 0, 0);
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700772 break;
773 case 3:
Koushik Duttad63eaef2010-07-14 21:01:21 -0700774 if (confirm_selection(confirm_restore, "Yes - Restore cache"))
agrabren1f76a5d2010-12-29 12:15:18 -0600775 nandroid_restore(file, 0, 0, 0, 1, 0, 0);
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700776 break;
Koushik Dutta2f73e582010-04-18 16:00:21 -0700777 case 4:
Koushik Duttad63eaef2010-07-14 21:01:21 -0700778 if (confirm_selection(confirm_restore, "Yes - Restore sd-ext"))
agrabren1f76a5d2010-12-29 12:15:18 -0600779 nandroid_restore(file, 0, 0, 0, 0, 1, 0);
780 break;
781 case 5:
782 if (confirm_selection(confirm_restore, "Yes - Restore wimax"))
783 nandroid_restore(file, 0, 0, 0, 0, 0, 1);
Koushik Dutta2f73e582010-04-18 16:00:21 -0700784 break;
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700785 }
786}
787
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700788void show_nandroid_menu()
789{
790 static char* headers[] = { "Nandroid",
791 "",
Steve Kondik4123b582010-11-14 03:18:40 -0500792 NULL
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700793 };
794
Steve Kondik4123b582010-11-14 03:18:40 -0500795 static char* list[] = { "Backup",
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700796 "Restore",
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700797 "Advanced Restore",
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700798 NULL
799 };
800
Koushik Duttadf1e4062010-12-18 17:42:31 -0800801 int chosen_item = get_menu_selection(headers, list, 0, 0);
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700802 switch (chosen_item)
803 {
804 case 0:
805 {
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700806 char backup_path[PATH_MAX];
Koushik K. Dutta6a26e7c2010-03-27 15:26:11 -0700807 time_t t = time(NULL);
808 struct tm *tmp = localtime(&t);
809 if (tmp == NULL)
810 {
811 struct timeval tp;
812 gettimeofday(&tp, NULL);
813 sprintf(backup_path, "/sdcard/clockworkmod/backup/%d", tp.tv_sec);
814 }
815 else
816 {
817 strftime(backup_path, sizeof(backup_path), "/sdcard/clockworkmod/backup/%F.%H.%M.%S", tmp);
818 }
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700819 nandroid_backup(backup_path);
820 }
821 break;
822 case 1:
823 show_nandroid_restore_menu();
824 break;
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700825 case 2:
826 show_nandroid_advanced_restore_menu();
827 break;
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700828 }
Koushik K. Duttaa496b512010-03-19 14:51:45 -0700829}
830
Koushik Duttafd1579b2010-05-01 12:46:55 -0700831void wipe_battery_stats()
832{
Koushik Duttadf1e4062010-12-18 17:42:31 -0800833 ensure_path_mounted("/data");
Koushik Duttafd1579b2010-05-01 12:46:55 -0700834 remove("/data/system/batterystats.bin");
Koushik Duttadf1e4062010-12-18 17:42:31 -0800835 ensure_path_unmounted("/data");
j_r0dd3f2e2862011-07-10 22:48:39 -0400836 ui_print("Battery Stats wiped.\n");
Koushik Duttafd1579b2010-05-01 12:46:55 -0700837}
838
Koushik K. Duttaa496b512010-03-19 14:51:45 -0700839void show_advanced_menu()
840{
841 static char* headers[] = { "Advanced and Debugging Menu",
842 "",
843 NULL
844 };
845
Koushik K. Dutta7fe4d7b2010-04-06 23:04:52 -0700846 static char* list[] = { "Reboot Recovery",
Koushik Dutta49af23c2010-06-21 13:45:51 -0700847 "Wipe Dalvik Cache",
Koushik Duttafd1579b2010-05-01 12:46:55 -0700848 "Wipe Battery Stats",
Koushik Dutta598cfc72010-06-20 09:42:47 -0700849 "Report Error",
Koushik K. Dutta7fe4d7b2010-04-06 23:04:52 -0700850 "Key Test",
Tanguy Pruvot67d358c2011-06-10 20:26:44 +0200851 "Show log",
Koushik Duttae17a78d2010-08-29 12:35:10 -0700852#ifndef BOARD_HAS_SMALL_RECOVERY
Koushik Duttaceddcd52010-08-23 16:13:14 -0700853 "Partition SD Card",
854 "Fix Permissions",
Brint E. Kriebeld3522332010-12-11 20:52:06 -0700855#ifdef BOARD_HAS_SDCARD_INTERNAL
856 "Partition Internal SD Card",
857#endif
Koushik Duttae17a78d2010-08-29 12:35:10 -0700858#endif
Koushik K. Duttaa496b512010-03-19 14:51:45 -0700859 NULL
860 };
861
862 for (;;)
863 {
Koushik Duttadf1e4062010-12-18 17:42:31 -0800864 int chosen_item = get_menu_selection(headers, list, 0, 0);
Koushik K. Duttaa496b512010-03-19 14:51:45 -0700865 if (chosen_item == GO_BACK)
866 break;
867 switch (chosen_item)
868 {
869 case 0:
Tanguy Pruvot67d358c2011-06-10 20:26:44 +0200870 {
Austen Dicken7126ef42011-06-14 15:47:40 -0500871 reboot_wrapper("recovery");
Koushik K. Dutta7fe4d7b2010-04-06 23:04:52 -0700872 break;
Tanguy Pruvot67d358c2011-06-10 20:26:44 +0200873 }
Koushik K. Dutta7fe4d7b2010-04-06 23:04:52 -0700874 case 1:
Koushik Duttad63eaef2010-07-14 21:01:21 -0700875 {
Koushik Duttadf1e4062010-12-18 17:42:31 -0800876 if (0 != ensure_path_mounted("/data"))
Koushik Dutta49af23c2010-06-21 13:45:51 -0700877 break;
Koushik Duttadf1e4062010-12-18 17:42:31 -0800878 ensure_path_mounted("/sd-ext");
879 ensure_path_mounted("/cache");
Koushik Dutta6c7745d2010-08-07 12:17:13 -0700880 if (confirm_selection( "Confirm wipe?", "Yes - Wipe Dalvik Cache")) {
Koushik Duttad63eaef2010-07-14 21:01:21 -0700881 __system("rm -r /data/dalvik-cache");
Koushik Dutta6c7745d2010-08-07 12:17:13 -0700882 __system("rm -r /cache/dalvik-cache");
883 __system("rm -r /sd-ext/dalvik-cache");
Koushik Dutta30a937a2011-09-05 21:14:06 -0700884 ui_print("Dalvik Cache wiped.\n");
Koushik Dutta6c7745d2010-08-07 12:17:13 -0700885 }
Koushik Duttadf1e4062010-12-18 17:42:31 -0800886 ensure_path_unmounted("/data");
Koushik Duttafd1579b2010-05-01 12:46:55 -0700887 break;
Koushik Duttad63eaef2010-07-14 21:01:21 -0700888 }
Koushik Duttafd1579b2010-05-01 12:46:55 -0700889 case 2:
Koushik Duttad63eaef2010-07-14 21:01:21 -0700890 {
891 if (confirm_selection( "Confirm wipe?", "Yes - Wipe Battery Stats"))
892 wipe_battery_stats();
Koushik Dutta598cfc72010-06-20 09:42:47 -0700893 break;
Koushik Duttad63eaef2010-07-14 21:01:21 -0700894 }
Koushik Dutta598cfc72010-06-20 09:42:47 -0700895 case 3:
Koushik Dutta49af23c2010-06-21 13:45:51 -0700896 handle_failure(1);
897 break;
898 case 4:
Koushik K. Duttaa496b512010-03-19 14:51:45 -0700899 {
900 ui_print("Outputting key codes.\n");
901 ui_print("Go back to end debugging.\n");
902 int key;
903 int action;
904 do
905 {
906 key = ui_wait_key();
907 action = device_handle_key(key, 1);
908 ui_print("Key: %d\n", key);
909 }
910 while (action != GO_BACK);
Koushik Dutta56606a22010-08-23 16:15:33 -0700911 break;
Koushik K. Duttaa496b512010-03-19 14:51:45 -0700912 }
Koushik Duttaf0e31b82010-08-17 16:55:38 -0700913 case 5:
914 {
Tanguy Pruvot67d358c2011-06-10 20:26:44 +0200915 ui_printlogtail(12);
916 break;
917 }
918 case 6:
919 {
Koushik Duttaceddcd52010-08-23 16:13:14 -0700920 static char* ext_sizes[] = { "128M",
921 "256M",
922 "512M",
923 "1024M",
Brint E. Kriebel0b233312010-11-14 15:31:17 -0700924 "2048M",
925 "4096M",
Koushik Duttaceddcd52010-08-23 16:13:14 -0700926 NULL };
927
928 static char* swap_sizes[] = { "0M",
929 "32M",
930 "64M",
931 "128M",
932 "256M",
933 NULL };
934
935 static char* ext_headers[] = { "Ext Size", "", NULL };
936 static char* swap_headers[] = { "Swap Size", "", NULL };
937
Koushik Duttadf1e4062010-12-18 17:42:31 -0800938 int ext_size = get_menu_selection(ext_headers, ext_sizes, 0, 0);
Koushik Duttaceddcd52010-08-23 16:13:14 -0700939 if (ext_size == GO_BACK)
940 continue;
Steve Kondik4123b582010-11-14 03:18:40 -0500941
Koushik Duttadf1e4062010-12-18 17:42:31 -0800942 int swap_size = get_menu_selection(swap_headers, swap_sizes, 0, 0);
Koushik Duttaceddcd52010-08-23 16:13:14 -0700943 if (swap_size == GO_BACK)
944 continue;
945
946 char sddevice[256];
Koushik Duttadf1e4062010-12-18 17:42:31 -0800947 Volume *vol = volume_for_path("/sdcard");
948 strcpy(sddevice, vol->device);
Koushik Duttaceddcd52010-08-23 16:13:14 -0700949 // we only want the mmcblk, not the partition
950 sddevice[strlen("/dev/block/mmcblkX")] = NULL;
951 char cmd[PATH_MAX];
952 setenv("SDPATH", sddevice, 1);
953 sprintf(cmd, "sdparted -es %s -ss %s -efs ext3 -s", ext_sizes[ext_size], swap_sizes[swap_size]);
954 ui_print("Partitioning SD Card... please wait...\n");
955 if (0 == __system(cmd))
956 ui_print("Done!\n");
957 else
958 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 -0700959 break;
Koushik Duttaf0e31b82010-08-17 16:55:38 -0700960 }
Tanguy Pruvot67d358c2011-06-10 20:26:44 +0200961 case 7:
Koushik Dutta38e8b2b2010-08-17 22:21:53 -0700962 {
Koushik Duttadf1e4062010-12-18 17:42:31 -0800963 ensure_path_mounted("/system");
964 ensure_path_mounted("/data");
Koushik Duttaceddcd52010-08-23 16:13:14 -0700965 ui_print("Fixing permissions...\n");
966 __system("fix_permissions");
967 ui_print("Done!\n");
Koushik Dutta56606a22010-08-23 16:15:33 -0700968 break;
Koushik Dutta38e8b2b2010-08-17 22:21:53 -0700969 }
Tanguy Pruvot67d358c2011-06-10 20:26:44 +0200970 case 8:
Brint E. Kriebeld3522332010-12-11 20:52:06 -0700971 {
972 static char* ext_sizes[] = { "128M",
973 "256M",
974 "512M",
975 "1024M",
976 "2048M",
977 "4096M",
978 NULL };
979
980 static char* swap_sizes[] = { "0M",
981 "32M",
982 "64M",
983 "128M",
984 "256M",
985 NULL };
986
987 static char* ext_headers[] = { "Data Size", "", NULL };
988 static char* swap_headers[] = { "Swap Size", "", NULL };
989
Koushik Duttadf1e4062010-12-18 17:42:31 -0800990 int ext_size = get_menu_selection(ext_headers, ext_sizes, 0, 0);
Brint E. Kriebeld3522332010-12-11 20:52:06 -0700991 if (ext_size == GO_BACK)
992 continue;
993
994 int swap_size = 0;
995 if (swap_size == GO_BACK)
996 continue;
997
998 char sddevice[256];
Koushik Duttadf1e4062010-12-18 17:42:31 -0800999 Volume *vol = volume_for_path("/emmc");
1000 strcpy(sddevice, vol->device);
Brint E. Kriebeld3522332010-12-11 20:52:06 -07001001 // we only want the mmcblk, not the partition
1002 sddevice[strlen("/dev/block/mmcblkX")] = NULL;
1003 char cmd[PATH_MAX];
1004 setenv("SDPATH", sddevice, 1);
1005 sprintf(cmd, "sdparted -es %s -ss %s -efs ext3 -s", ext_sizes[ext_size], swap_sizes[swap_size]);
1006 ui_print("Partitioning Internal SD Card... please wait...\n");
1007 if (0 == __system(cmd))
1008 ui_print("Done!\n");
1009 else
1010 ui_print("An error occured while partitioning your Internal SD Card. Please see /tmp/recovery.log for more details.\n");
1011 break;
1012 }
Koushik K. Duttaa496b512010-03-19 14:51:45 -07001013 }
1014 }
Koushik Dutta14239d22010-06-14 15:02:48 -07001015}
1016
Koushik Duttab643e4f2010-12-18 18:39:39 -08001017void write_fstab_root(char *path, FILE *file)
Koushik Dutta14239d22010-06-14 15:02:48 -07001018{
Koushik Duttab643e4f2010-12-18 18:39:39 -08001019 Volume *vol = volume_for_path(path);
1020 if (vol == NULL) {
Koushik Dutta9765a032010-12-18 21:31:02 -08001021 LOGW("Unable to get recovery.fstab info for %s during fstab generation!\n", path);
Koushik Dutta598cfc72010-06-20 09:42:47 -07001022 return;
Koushik Duttaa6522b32010-06-20 13:16:06 -07001023 }
Steve Kondik4123b582010-11-14 03:18:40 -05001024
Koushik Dutta31b74112010-12-20 08:49:20 -08001025 char device[200];
1026 if (vol->device[0] != '/')
1027 get_partition_device(vol->device, device);
1028 else
1029 strcpy(device, vol->device);
1030
1031 fprintf(file, "%s ", device);
Koushik Duttab643e4f2010-12-18 18:39:39 -08001032 fprintf(file, "%s ", path);
Koushik Duttaab1f8b82011-02-27 17:28:30 -08001033 // special case rfs cause auto will mount it as vfat on samsung.
1034 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 -07001035}
1036
1037void create_fstab()
1038{
Koushik Duttab643e4f2010-12-18 18:39:39 -08001039 struct stat info;
Koushik Duttacd44ab92010-06-23 00:02:14 -07001040 __system("touch /etc/mtab");
Koushik Dutta14239d22010-06-14 15:02:48 -07001041 FILE *file = fopen("/etc/fstab", "w");
Koushik Duttaa6522b32010-06-20 13:16:06 -07001042 if (file == NULL) {
Koushik Duttab643e4f2010-12-18 18:39:39 -08001043 LOGW("Unable to create /etc/fstab!\n");
Koushik Dutta598cfc72010-06-20 09:42:47 -07001044 return;
Koushik Duttaa6522b32010-06-20 13:16:06 -07001045 }
Brandon Bennett6d0604b2011-01-28 13:39:10 -07001046 Volume *vol = volume_for_path("/boot");
Koushik Duttaa9f0e7f2011-02-12 10:21:11 -08001047 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 -07001048 write_fstab_root("/boot", file);
Koushik Duttab643e4f2010-12-18 18:39:39 -08001049 write_fstab_root("/cache", file);
1050 write_fstab_root("/data", file);
Koushik Dutta5460f0c2010-12-18 22:37:49 -08001051 if (has_datadata()) {
1052 write_fstab_root("/datadata", file);
1053 }
Koushik Duttab643e4f2010-12-18 18:39:39 -08001054 write_fstab_root("/system", file);
1055 write_fstab_root("/sdcard", file);
1056 write_fstab_root("/sd-ext", file);
Koushik Dutta14239d22010-06-14 15:02:48 -07001057 fclose(file);
Koushik Dutta9765a032010-12-18 21:31:02 -08001058 LOGI("Completed outputting fstab.\n");
Koushik Dutta598cfc72010-06-20 09:42:47 -07001059}
1060
Koushik Dutta4196e0f2010-12-19 03:38:29 -08001061int bml_check_volume(const char *path) {
1062 ui_print("Checking %s...\n", path);
1063 ensure_path_unmounted(path);
1064 if (0 == ensure_path_mounted(path)) {
1065 ensure_path_unmounted(path);
1066 return 0;
1067 }
1068
1069 Volume *vol = volume_for_path(path);
1070 if (vol == NULL) {
1071 LOGE("Unable process volume! Skipping...\n");
1072 return 0;
1073 }
1074
Koushik Duttabec09952010-12-19 20:37:57 -08001075 ui_print("%s may be rfs. Checking...\n", path);
Koushik Dutta4196e0f2010-12-19 03:38:29 -08001076 char tmp[PATH_MAX];
1077 sprintf(tmp, "mount -t rfs %s %s", vol->device, path);
1078 int ret = __system(tmp);
1079 printf("%d\n", ret);
1080 return ret == 0 ? 1 : 0;
1081}
1082
1083void process_volumes() {
1084 create_fstab();
Koushik Dutta38a92142011-06-10 09:45:52 -07001085
1086 if (is_data_media()) {
1087 setup_data_media();
Koushik Dutta94caefe2011-06-10 09:17:30 -07001088 }
1089
Koushik Duttae8bc2c82011-02-27 14:00:19 -08001090 return;
1091
1092 // dead code.
Koushik Dutta4196e0f2010-12-19 03:38:29 -08001093 if (device_flash_type() != BML)
1094 return;
1095
1096 ui_print("Checking for ext4 partitions...\n");
1097 int ret = 0;
1098 ret = bml_check_volume("/system");
1099 ret |= bml_check_volume("/data");
1100 if (has_datadata())
1101 ret |= bml_check_volume("/datadata");
1102 ret |= bml_check_volume("/cache");
1103
1104 if (ret == 0) {
1105 ui_print("Done!\n");
1106 return;
1107 }
1108
Koushik Dutta745b5ff2010-12-21 00:58:43 -08001109 char backup_path[PATH_MAX];
1110 time_t t = time(NULL);
1111 char backup_name[PATH_MAX];
1112 struct timeval tp;
1113 gettimeofday(&tp, NULL);
1114 sprintf(backup_name, "before-ext4-convert-%d", tp.tv_sec);
1115 sprintf(backup_path, "/sdcard/clockworkmod/backup/%s", backup_name);
1116
Koushik Dutta4196e0f2010-12-19 03:38:29 -08001117 ui_set_show_text(1);
1118 ui_print("Filesystems need to be converted to ext4.\n");
1119 ui_print("A backup and restore will now take place.\n");
Koushik Duttabec09952010-12-19 20:37:57 -08001120 ui_print("If anything goes wrong, your backup will be\n");
Koushik Dutta745b5ff2010-12-21 00:58:43 -08001121 ui_print("named %s. Try restoring it\n", backup_name);
Koushik Duttabec09952010-12-19 20:37:57 -08001122 ui_print("in case of error.\n");
Koushik Dutta4196e0f2010-12-19 03:38:29 -08001123
Koushik Dutta745b5ff2010-12-21 00:58:43 -08001124 nandroid_backup(backup_path);
Koushik Dutta5b7f34a2010-12-29 23:36:03 -08001125 nandroid_restore(backup_path, 1, 1, 1, 1, 1, 0);
Koushik Dutta4196e0f2010-12-19 03:38:29 -08001126 ui_set_show_text(0);
1127}
1128
Koushik Dutta598cfc72010-06-20 09:42:47 -07001129void handle_failure(int ret)
1130{
1131 if (ret == 0)
1132 return;
Koushik Duttadf1e4062010-12-18 17:42:31 -08001133 if (0 != ensure_path_mounted("/sdcard"))
Koushik Dutta598cfc72010-06-20 09:42:47 -07001134 return;
Koushik Duttaa6522b32010-06-20 13:16:06 -07001135 mkdir("/sdcard/clockworkmod", S_IRWXU);
1136 __system("cp /tmp/recovery.log /sdcard/clockworkmod/recovery.log");
Koushik Dutta38e8b2b2010-08-17 22:21:53 -07001137 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 -05001138}
Koushik Duttadf1e4062010-12-18 17:42:31 -08001139
Koushik Duttadf1e4062010-12-18 17:42:31 -08001140int is_path_mounted(const char* path) {
1141 Volume* v = volume_for_path(path);
1142 if (v == NULL) {
1143 return 0;
1144 }
1145 if (strcmp(v->fs_type, "ramdisk") == 0) {
1146 // the ramdisk is always mounted.
1147 return 1;
1148 }
1149
1150 int result;
1151 result = scan_mounted_volumes();
1152 if (result < 0) {
1153 LOGE("failed to scan mounted volumes\n");
1154 return 0;
1155 }
1156
1157 const MountedVolume* mv =
1158 find_mounted_volume_by_mount_point(v->mount_point);
1159 if (mv) {
1160 // volume is already mounted
1161 return 1;
1162 }
1163 return 0;
1164}
Koushik Dutta5460f0c2010-12-18 22:37:49 -08001165
1166int has_datadata() {
1167 Volume *vol = volume_for_path("/datadata");
1168 return vol != NULL;
Koushik Duttad8e21c32011-01-04 10:46:23 -08001169}
1170
1171int volume_main(int argc, char **argv) {
1172 load_volume_table();
1173 return 0;
1174}