blob: cb44cb17124c52e1b502bf179cf94bb549d72dac [file] [log] [blame]
Koushik K. Dutta6060e5c2010-02-11 22:27:06 -08001#include <ctype.h>
2#include <errno.h>
3#include <fcntl.h>
4#include <getopt.h>
5#include <limits.h>
6#include <linux/input.h>
7#include <stdio.h>
8#include <stdlib.h>
9#include <string.h>
10#include <sys/reboot.h>
11#include <sys/types.h>
12#include <time.h>
13#include <unistd.h>
14
Koushik K. Duttae9234872010-02-12 00:43:24 -080015#include <sys/wait.h>
16#include <sys/limits.h>
17#include <dirent.h>
Koushik K. Dutta49f56892010-02-25 14:51:05 -080018#include <sys/stat.h>
Koushik K. Duttae9234872010-02-12 00:43:24 -080019
Koushik K. Dutta33370db2010-02-25 11:39:07 -080020#include <signal.h>
21#include <sys/wait.h>
22
Koushik K. Dutta6060e5c2010-02-11 22:27:06 -080023#include "bootloader.h"
24#include "common.h"
25#include "cutils/properties.h"
26#include "firmware.h"
27#include "install.h"
28#include "minui/minui.h"
29#include "minzip/DirUtil.h"
30#include "roots.h"
31#include "recovery_ui.h"
32
Koushik K. Duttaa85d7cc2010-03-12 17:00:58 -080033#include "../../external/yaffs2/yaffs2/utils/mkyaffs2image.h"
Koushik K. Dutta54305a82010-03-12 17:43:26 -080034#include "../../external/yaffs2/yaffs2/utils/unyaffs.h"
Koushik K. Duttaa85d7cc2010-03-12 17:00:58 -080035
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -080036#include "extendedcommands.h"
37#include "nandroid.h"
Koushik Duttadf1e4062010-12-18 17:42:31 -080038#include "mounts.h"
Koushik Dutta9765a032010-12-18 21:31:02 -080039#include "flashutils/flashutils.h"
Koushik Duttabec09952010-12-19 20:37:57 -080040#include "edify/expr.h"
Koushik Dutta7adeadc2011-05-26 11:47:56 -070041#include <libgen.h>
42
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -080043
Koushik K. Dutta6060e5c2010-02-11 22:27:06 -080044int signature_check_enabled = 1;
45int script_assert_enabled = 1;
Koushik Duttadf1e4062010-12-18 17:42:31 -080046static const char *SDCARD_UPDATE_FILE = "/sdcard/update.zip";
Koushik K. Dutta6060e5c2010-02-11 22:27:06 -080047
48void
49toggle_signature_check()
50{
51 signature_check_enabled = !signature_check_enabled;
52 ui_print("Signature Check: %s\n", signature_check_enabled ? "Enabled" : "Disabled");
53}
54
55void toggle_script_asserts()
56{
57 script_assert_enabled = !script_assert_enabled;
Koushik K. Duttae9234872010-02-12 00:43:24 -080058 ui_print("Script Asserts: %s\n", script_assert_enabled ? "Enabled" : "Disabled");
59}
60
Koushik K. Duttaea46fe22010-03-08 02:58:04 -080061int install_zip(const char* packagefilepath)
Koushik K. Duttae9234872010-02-12 00:43:24 -080062{
Koushik K. Duttabcdd0032010-02-21 21:10:25 -080063 ui_print("\n-- Installing: %s\n", packagefilepath);
Koushik Dutta9765a032010-12-18 21:31:02 -080064 if (device_flash_type() == MTD) {
65 set_sdcard_update_bootloader_message();
66 }
Koushik K. Duttabcdd0032010-02-21 21:10:25 -080067 int status = install_package(packagefilepath);
Koushik K. Duttaf3534d02010-04-18 18:06:24 -070068 ui_reset_progress();
Koushik K. Duttabcdd0032010-02-21 21:10:25 -080069 if (status != INSTALL_SUCCESS) {
70 ui_set_background(BACKGROUND_ICON_ERROR);
71 ui_print("Installation aborted.\n");
Koushik K. Duttaea46fe22010-03-08 02:58:04 -080072 return 1;
Steve Kondik4123b582010-11-14 03:18:40 -050073 }
Koushik K. Dutta001c5b52010-02-25 14:53:57 -080074 ui_set_background(BACKGROUND_ICON_NONE);
Koushik K. Dutta99fb6fe2010-03-03 00:42:58 -080075 ui_print("\nInstall from sdcard complete.\n");
Koushik K. Duttaea46fe22010-03-08 02:58:04 -080076 return 0;
Koushik K. Duttabcdd0032010-02-21 21:10:25 -080077}
78
Koushik Duttadf1e4062010-12-18 17:42:31 -080079char* INSTALL_MENU_ITEMS[] = { "apply /sdcard/update.zip",
Koushik K. Duttabcdd0032010-02-21 21:10:25 -080080 "choose zip from sdcard",
81 "toggle signature verification",
82 "toggle script asserts",
83 NULL };
84#define ITEM_APPLY_SDCARD 0
85#define ITEM_CHOOSE_ZIP 1
86#define ITEM_SIG_CHECK 2
87#define ITEM_ASSERTS 3
88
89void show_install_update_menu()
90{
91 static char* headers[] = { "Apply update from .zip file on SD card",
92 "",
Steve Kondik4123b582010-11-14 03:18:40 -050093 NULL
Koushik K. Duttabcdd0032010-02-21 21:10:25 -080094 };
95 for (;;)
96 {
Koushik Duttadf1e4062010-12-18 17:42:31 -080097 int chosen_item = get_menu_selection(headers, INSTALL_MENU_ITEMS, 0, 0);
Koushik K. Duttabcdd0032010-02-21 21:10:25 -080098 switch (chosen_item)
99 {
100 case ITEM_ASSERTS:
101 toggle_script_asserts();
102 break;
103 case ITEM_SIG_CHECK:
104 toggle_signature_check();
105 break;
106 case ITEM_APPLY_SDCARD:
Koushik Duttad63eaef2010-07-14 21:01:21 -0700107 {
108 if (confirm_selection("Confirm install?", "Yes - Install /sdcard/update.zip"))
Koushik Duttadf1e4062010-12-18 17:42:31 -0800109 install_zip(SDCARD_UPDATE_FILE);
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800110 break;
Koushik Duttad63eaef2010-07-14 21:01:21 -0700111 }
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800112 case ITEM_CHOOSE_ZIP:
113 show_choose_zip_menu();
114 break;
115 default:
116 return;
117 }
Steve Kondik4123b582010-11-14 03:18:40 -0500118
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800119 }
120}
121
Koushik K. Dutta1d53c4e2010-04-02 16:46:21 -0700122void free_string_array(char** array)
123{
124 if (array == NULL)
125 return;
126 char* cursor = array[0];
127 int i = 0;
128 while (cursor != NULL)
129 {
130 free(cursor);
131 cursor = array[++i];
132 }
133 free(array);
134}
135
Koushik K. Dutta49f56892010-02-25 14:51:05 -0800136char** gather_files(const char* directory, const char* fileExtensionOrDirectory, int* numFiles)
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800137{
Koushik K. Duttae9234872010-02-12 00:43:24 -0800138 char path[PATH_MAX] = "";
139 DIR *dir;
140 struct dirent *de;
141 int total = 0;
142 int i;
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800143 char** files = NULL;
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800144 int pass;
145 *numFiles = 0;
146 int dirLen = strlen(directory);
Koushik K. Duttae9234872010-02-12 00:43:24 -0800147
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800148 dir = opendir(directory);
149 if (dir == NULL) {
150 ui_print("Couldn't open directory.\n");
151 return NULL;
152 }
Steve Kondik4123b582010-11-14 03:18:40 -0500153
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800154 int extension_length = 0;
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800155 if (fileExtensionOrDirectory != NULL)
156 extension_length = strlen(fileExtensionOrDirectory);
Steve Kondik4123b582010-11-14 03:18:40 -0500157
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800158 int isCounting = 1;
159 i = 0;
160 for (pass = 0; pass < 2; pass++) {
161 while ((de=readdir(dir)) != NULL) {
162 // skip hidden files
163 if (de->d_name[0] == '.')
164 continue;
Steve Kondik4123b582010-11-14 03:18:40 -0500165
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800166 // NULL means that we are gathering directories, so skip this
167 if (fileExtensionOrDirectory != NULL)
168 {
169 // make sure that we can have the desired extension (prevent seg fault)
170 if (strlen(de->d_name) < extension_length)
171 continue;
172 // compare the extension
173 if (strcmp(de->d_name + strlen(de->d_name) - extension_length, fileExtensionOrDirectory) != 0)
174 continue;
175 }
176 else
177 {
178 struct stat info;
Koushik K. Dutta1d53c4e2010-04-02 16:46:21 -0700179 char fullFileName[PATH_MAX];
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800180 strcpy(fullFileName, directory);
181 strcat(fullFileName, de->d_name);
182 stat(fullFileName, &info);
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800183 // make sure it is a directory
184 if (!(S_ISDIR(info.st_mode)))
185 continue;
186 }
Steve Kondik4123b582010-11-14 03:18:40 -0500187
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800188 if (pass == 0)
189 {
190 total++;
191 continue;
192 }
Steve Kondik4123b582010-11-14 03:18:40 -0500193
Koushik K. Dutta49f56892010-02-25 14:51:05 -0800194 files[i] = (char*) malloc(dirLen + strlen(de->d_name) + 2);
195 strcpy(files[i], directory);
196 strcat(files[i], de->d_name);
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800197 if (fileExtensionOrDirectory == NULL)
198 strcat(files[i], "/");
199 i++;
200 }
201 if (pass == 1)
202 break;
203 if (total == 0)
204 break;
205 rewinddir(dir);
206 *numFiles = total;
207 files = (char**) malloc((total+1)*sizeof(char*));
208 files[total]=NULL;
209 }
Koushik K. Dutta49f56892010-02-25 14:51:05 -0800210
211 if(closedir(dir) < 0) {
212 LOGE("Failed to close directory.");
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800213 }
214
215 if (total==0) {
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800216 return NULL;
217 }
218
agrabren1f76a5d2010-12-29 12:15:18 -0600219 // sort the result
220 if (files != NULL) {
221 for (i = 0; i < total; i++) {
222 int curMax = -1;
223 int j;
224 for (j = 0; j < total - i; j++) {
225 if (curMax == -1 || strcmp(files[curMax], files[j]) < 0)
226 curMax = j;
227 }
228 char* temp = files[curMax];
229 files[curMax] = files[total - i - 1];
230 files[total - i - 1] = temp;
231 }
232 }
Koushik Dutta1e8aaba2010-07-01 00:23:25 -0700233
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800234 return files;
Koushik K. Dutta49f56892010-02-25 14:51:05 -0800235}
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800236
Koushik K. Dutta49f56892010-02-25 14:51:05 -0800237// pass in NULL for fileExtensionOrDirectory and you will get a directory chooser
238char* choose_file_menu(const char* directory, const char* fileExtensionOrDirectory, const char* headers[])
239{
240 char path[PATH_MAX] = "";
241 DIR *dir;
242 struct dirent *de;
243 int numFiles = 0;
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800244 int numDirs = 0;
Koushik K. Dutta49f56892010-02-25 14:51:05 -0800245 int i;
Koushik K. Dutta1d53c4e2010-04-02 16:46:21 -0700246 char* return_value = NULL;
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800247 int dir_len = strlen(directory);
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800248
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800249 char** files = gather_files(directory, fileExtensionOrDirectory, &numFiles);
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800250 char** dirs = NULL;
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800251 if (fileExtensionOrDirectory != NULL)
252 dirs = gather_files(directory, NULL, &numDirs);
253 int total = numDirs + numFiles;
254 if (total == 0)
255 {
256 ui_print("No files found.\n");
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800257 }
Koushik K. Dutta1d53c4e2010-04-02 16:46:21 -0700258 else
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800259 {
Koushik K. Dutta1d53c4e2010-04-02 16:46:21 -0700260 char** list = (char**) malloc((total + 1) * sizeof(char*));
261 list[total] = NULL;
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800262
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800263
Koushik K. Dutta1d53c4e2010-04-02 16:46:21 -0700264 for (i = 0 ; i < numDirs; i++)
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800265 {
Koushik K. Dutta1d53c4e2010-04-02 16:46:21 -0700266 list[i] = strdup(dirs[i] + dir_len);
267 }
268
269 for (i = 0 ; i < numFiles; i++)
270 {
271 list[numDirs + i] = strdup(files[i] + dir_len);
272 }
273
274 for (;;)
275 {
Koushik Duttadf1e4062010-12-18 17:42:31 -0800276 int chosen_item = get_menu_selection(headers, list, 0, 0);
Koushik K. Dutta1d53c4e2010-04-02 16:46:21 -0700277 if (chosen_item == GO_BACK)
278 break;
279 static char ret[PATH_MAX];
280 if (chosen_item < numDirs)
281 {
282 char* subret = choose_file_menu(dirs[chosen_item], fileExtensionOrDirectory, headers);
283 if (subret != NULL)
284 {
285 strcpy(ret, subret);
286 return_value = ret;
287 break;
288 }
289 continue;
Steve Kondik4123b582010-11-14 03:18:40 -0500290 }
Koushik K. Dutta1d53c4e2010-04-02 16:46:21 -0700291 strcpy(ret, files[chosen_item - numDirs]);
292 return_value = ret;
293 break;
294 }
295 free_string_array(list);
Koushik K. Dutta981b0cd2010-02-22 08:53:34 -0800296 }
Koushik K. Dutta1d53c4e2010-04-02 16:46:21 -0700297
298 free_string_array(files);
299 free_string_array(dirs);
300 return return_value;
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800301}
302
303void show_choose_zip_menu()
304{
Koushik Duttadf1e4062010-12-18 17:42:31 -0800305 if (ensure_path_mounted("/sdcard") != 0) {
Koushik K. Duttae9234872010-02-12 00:43:24 -0800306 LOGE ("Can't mount /sdcard\n");
307 return;
308 }
309
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800310 static char* headers[] = { "Choose a zip to apply",
311 "",
Steve Kondik4123b582010-11-14 03:18:40 -0500312 NULL
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800313 };
Steve Kondik4123b582010-11-14 03:18:40 -0500314
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800315 char* file = choose_file_menu("/sdcard/", ".zip", headers);
316 if (file == NULL)
317 return;
Koushik Duttad63eaef2010-07-14 21:01:21 -0700318 static char* confirm_install = "Confirm install?";
319 static char confirm[PATH_MAX];
320 sprintf(confirm, "Yes - Install %s", basename(file));
321 if (confirm_selection(confirm_install, confirm))
Koushik Duttadf1e4062010-12-18 17:42:31 -0800322 install_zip(file);
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800323}
324
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800325void show_nandroid_restore_menu()
326{
Koushik Duttadf1e4062010-12-18 17:42:31 -0800327 if (ensure_path_mounted("/sdcard") != 0) {
Koushik K. Dutta54305a82010-03-12 17:43:26 -0800328 LOGE ("Can't mount /sdcard\n");
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800329 return;
Koushik K. Dutta54305a82010-03-12 17:43:26 -0800330 }
Steve Kondik4123b582010-11-14 03:18:40 -0500331
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800332 static char* headers[] = { "Choose an image to restore",
333 "",
Steve Kondik4123b582010-11-14 03:18:40 -0500334 NULL
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800335 };
Koushik K. Duttae9234872010-02-12 00:43:24 -0800336
Koushik K. Dutta49f56892010-02-25 14:51:05 -0800337 char* file = choose_file_menu("/sdcard/clockworkmod/backup/", NULL, headers);
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800338 if (file == NULL)
339 return;
Koushik Duttad63eaef2010-07-14 21:01:21 -0700340
341 if (confirm_selection("Confirm restore?", "Yes - Restore"))
Koushik Dutta5b7f34a2010-12-29 23:36:03 -0800342 nandroid_restore(file, 1, 1, 1, 1, 1, 0);
Koushik K. Dutta03173782010-02-26 14:14:23 -0800343}
344
jt113447d86a32011-02-27 18:54:09 -0600345#ifndef BOARD_UMS_LUNFILE
346#define BOARD_UMS_LUNFILE "/sys/devices/platform/usb_mass_storage/lun0/file"
347#endif
348
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700349void show_mount_usb_storage_menu()
Koushik K. Dutta03173782010-02-26 14:14:23 -0800350{
Koushik Duttaf1cb0d52011-01-01 20:03:30 -0800351 int fd;
Koushik Duttadf1e4062010-12-18 17:42:31 -0800352 Volume *vol = volume_for_path("/sdcard");
jt113447d86a32011-02-27 18:54:09 -0600353 if ((fd = open(BOARD_UMS_LUNFILE, O_WRONLY)) < 0) {
Koushik Duttaf1cb0d52011-01-01 20:03:30 -0800354 LOGE("Unable to open ums lunfile (%s)", strerror(errno));
355 return -1;
356 }
357
mik_osdf3004b2011-04-26 19:15:22 +0300358 if ((write(fd, vol->device, strlen(vol->device)) < 0) &&
359 (!vol->device2 || (write(fd, vol->device, strlen(vol->device2)) < 0))) {
Koushik Duttaf1cb0d52011-01-01 20:03:30 -0800360 LOGE("Unable to write to ums lunfile (%s)", strerror(errno));
361 close(fd);
362 return -1;
363 }
Koushik K. Dutta03173782010-02-26 14:14:23 -0800364 static char* headers[] = { "USB Mass Storage device",
365 "Leaving this menu unmount",
366 "your SD card from your PC.",
367 "",
Steve Kondik4123b582010-11-14 03:18:40 -0500368 NULL
Koushik K. Dutta03173782010-02-26 14:14:23 -0800369 };
Steve Kondik4123b582010-11-14 03:18:40 -0500370
Koushik K. Dutta03173782010-02-26 14:14:23 -0800371 static char* list[] = { "Unmount", NULL };
Steve Kondik4123b582010-11-14 03:18:40 -0500372
Koushik K. Dutta03173782010-02-26 14:14:23 -0800373 for (;;)
374 {
Koushik Duttadf1e4062010-12-18 17:42:31 -0800375 int chosen_item = get_menu_selection(headers, list, 0, 0);
Koushik K. Dutta03173782010-02-26 14:14:23 -0800376 if (chosen_item == GO_BACK || chosen_item == 0)
377 break;
378 }
Steve Kondik4123b582010-11-14 03:18:40 -0500379
jt113447d86a32011-02-27 18:54:09 -0600380 if ((fd = open(BOARD_UMS_LUNFILE, O_WRONLY)) < 0) {
Koushik Duttaf1cb0d52011-01-01 20:03:30 -0800381 LOGE("Unable to open ums lunfile (%s)", strerror(errno));
382 return -1;
383 }
384
385 char ch = 0;
386 if (write(fd, &ch, 1) < 0) {
387 LOGE("Unable to write to ums lunfile (%s)", strerror(errno));
388 close(fd);
389 return -1;
390 }
Koushik K. Duttae81cb752010-02-26 17:44:33 -0800391}
Koushik K. Duttaf68aaaf2010-03-07 13:39:21 -0800392
Koushik Duttad63eaef2010-07-14 21:01:21 -0700393int confirm_selection(const char* title, const char* confirm)
Koushik K. Dutta16f0b492010-03-19 14:37:11 -0700394{
Koushik Duttaceddcd52010-08-23 16:13:14 -0700395 struct stat info;
396 if (0 == stat("/sdcard/clockworkmod/.no_confirm", &info))
397 return 1;
398
Koushik Duttad63eaef2010-07-14 21:01:21 -0700399 char* confirm_headers[] = { title, " THIS CAN NOT BE UNDONE.", "", NULL };
400 char* items[] = { "No",
401 "No",
402 "No",
403 "No",
404 "No",
405 "No",
406 "No",
Koushik Duttaecd32fa2010-07-14 18:38:02 -0700407 confirm, //" Yes -- wipe partition", // [7
Koushik Duttad63eaef2010-07-14 21:01:21 -0700408 "No",
409 "No",
410 "No",
Koushik K. Dutta16f0b492010-03-19 14:37:11 -0700411 NULL };
412
Koushik Duttadf1e4062010-12-18 17:42:31 -0800413 int chosen_item = get_menu_selection(confirm_headers, items, 0, 0);
Koushik K. Dutta16f0b492010-03-19 14:37:11 -0700414 return chosen_item == 7;
415}
416
Koushik Duttada32b542011-01-01 17:55:22 -0800417#define MKE2FS_BIN "/sbin/mke2fs"
418#define TUNE2FS_BIN "/sbin/tune2fs"
419#define E2FSCK_BIN "/sbin/e2fsck"
420
Koushik Duttada32b542011-01-01 17:55:22 -0800421int format_unknown_device(const char *device, const char* path, const char *fs_type)
Koushik K. Dutta3f995392010-03-30 23:29:43 -0700422{
Koushik Dutta2e454492011-01-01 18:06:17 -0800423 LOGI("Formatting unknown device.\n");
Koushik Duttaa8708c62011-01-01 18:00:27 -0800424
Koushik Duttada32b542011-01-01 17:55:22 -0800425 // device may simply be a name, like "system"
Koushik Dutta41873472011-05-26 17:30:43 -0700426 if (get_flash_type(fs_type) != UNSUPPORTED)
Koushik Dutta8a6bc772011-05-26 11:14:15 -0700427 return erase_raw_partition(fs_type, device);
Koushik Duttada32b542011-01-01 17:55:22 -0800428
Koushik Dutta9f52e5f2011-01-02 14:11:24 -0800429 // if this is SDEXT:, don't worry about it if it does not exist.
Koushik Duttadf1e4062010-12-18 17:42:31 -0800430 if (0 == strcmp(path, "/sd-ext"))
Koushik K. Duttaf3534d02010-04-18 18:06:24 -0700431 {
432 struct stat st;
Koushik Duttadf1e4062010-12-18 17:42:31 -0800433 Volume *vol = volume_for_path("/sd-ext");
434 if (vol == NULL || 0 != stat(vol->device, &st))
Koushik K. Duttaf3534d02010-04-18 18:06:24 -0700435 {
436 ui_print("No app2sd partition found. Skipping format of /sd-ext.\n");
437 return 0;
438 }
439 }
Koushik Dutta2f73e582010-04-18 16:00:21 -0700440
Koushik Dutta9f52e5f2011-01-02 14:11:24 -0800441 if (NULL != fs_type) {
442 if (strcmp("ext3", fs_type) == 0) {
443 LOGI("Formatting ext3 device.\n");
444 if (0 != ensure_path_unmounted(path)) {
445 LOGE("Error while unmounting %s.\n", path);
446 return -12;
447 }
448 return format_ext3_device(device);
449 }
450
451 if (strcmp("ext2", fs_type) == 0) {
452 LOGI("Formatting ext2 device.\n");
453 if (0 != ensure_path_unmounted(path)) {
454 LOGE("Error while unmounting %s.\n", path);
455 return -12;
456 }
457 return format_ext2_device(device);
458 }
459 }
460
Koushik Duttadf1e4062010-12-18 17:42:31 -0800461 if (0 != ensure_path_mounted(path))
Koushik K. Dutta3f995392010-03-30 23:29:43 -0700462 {
463 ui_print("Error mounting %s!\n", path);
Koushik Duttacd44ab92010-06-23 00:02:14 -0700464 ui_print("Skipping format...\n");
465 return 0;
Koushik K. Dutta3f995392010-03-30 23:29:43 -0700466 }
467
468 static char tmp[PATH_MAX];
469 sprintf(tmp, "rm -rf %s/*", path);
470 __system(tmp);
471 sprintf(tmp, "rm -rf %s/.*", path);
472 __system(tmp);
Steve Kondik4123b582010-11-14 03:18:40 -0500473
Koushik Duttadf1e4062010-12-18 17:42:31 -0800474 ensure_path_unmounted(path);
Koushik K. Dutta3f995392010-03-30 23:29:43 -0700475 return 0;
476}
477
Koushik Duttadf1e4062010-12-18 17:42:31 -0800478//#define MOUNTABLE_COUNT 5
479//#define DEVICE_COUNT 4
480//#define MMC_COUNT 2
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700481
Kolja Dummann92796ec2011-02-13 20:59:30 +0100482typedef struct {
483 char mount[255];
484 char unmount[255];
485 Volume* v;
486} MountMenuEntry;
487
488typedef struct {
489 char txt[255];
490 Volume* v;
491} FormatMenuEntry;
492
Steve Kondik0d878512011-04-09 01:41:45 -0400493int is_safe_to_format(char* name)
494{
Brandon Bennett9ad8ba22011-05-07 23:42:58 -0600495 char str[255];
496 char* partition;
Brandon Bennettcdcb7732011-06-05 16:40:00 -0600497 property_get("ro.cwm.forbid_format", str, "/misc,/radio,/bootloader,/recovery");
Brandon Bennett9ad8ba22011-05-07 23:42:58 -0600498
499 partition = strtok(str, ", ");
500 while (partition != NULL) {
501 if (strcmp(name, partition) == 0) {
502 return 0;
503 }
504 partition = strtok(NULL, ", ");
505 }
506
507 return 1;
Steve Kondik0d878512011-04-09 01:41:45 -0400508}
509
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700510void show_partition_menu()
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700511{
Koushik Duttaceddcd52010-08-23 16:13:14 -0700512 static char* headers[] = { "Mounts and Storage Menu",
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700513 "",
Steve Kondik4123b582010-11-14 03:18:40 -0500514 NULL
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700515 };
516
Kolja Dummann92796ec2011-02-13 20:59:30 +0100517 static MountMenuEntry* mount_menue = NULL;
518 static FormatMenuEntry* format_menue = NULL;
519
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700520 typedef char* string;
Steve Kondik4123b582010-11-14 03:18:40 -0500521
Kolja Dummann92796ec2011-02-13 20:59:30 +0100522 int i, mountable_volumes, formatable_volumes;
523 int num_volumes;
524 Volume* device_volumes;
Steve Kondik4123b582010-11-14 03:18:40 -0500525
Kolja Dummann92796ec2011-02-13 20:59:30 +0100526 num_volumes = get_num_volumes();
527 device_volumes = get_device_volumes();
528
529 string options[255];
530
531 if(!device_volumes)
532 return;
533
534 mountable_volumes = 0;
535 formatable_volumes = 0;
536
537 mount_menue = malloc(num_volumes * sizeof(MountMenuEntry));
538 format_menue = malloc(num_volumes * sizeof(FormatMenuEntry));
539
540 for (i = 0; i < num_volumes; ++i) {
541 Volume* v = &device_volumes[i];
Koushik Dutta67fa0c32011-03-17 11:37:21 -0700542 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 +0100543 {
544 sprintf(&mount_menue[mountable_volumes].mount, "mount %s", v->mount_point);
545 sprintf(&mount_menue[mountable_volumes].unmount, "unmount %s", v->mount_point);
546 mount_menue[mountable_volumes].v = &device_volumes[i];
547 ++mountable_volumes;
Steve Kondik0d878512011-04-09 01:41:45 -0400548 if (is_safe_to_format(v->mount_point)) {
549 sprintf(&format_menue[formatable_volumes].txt, "format %s", v->mount_point);
550 format_menue[formatable_volumes].v = &device_volumes[i];
551 ++formatable_volumes;
552 }
Kolja Dummann92796ec2011-02-13 20:59:30 +0100553 }
Steve Kondik0d878512011-04-09 01:41:45 -0400554 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 +0100555 {
556 sprintf(&format_menue[formatable_volumes].txt, "format %s", v->mount_point);
557 format_menue[formatable_volumes].v = &device_volumes[i];
558 ++formatable_volumes;
559 }
560 }
561
Koushik Duttadf1e4062010-12-18 17:42:31 -0800562
Koushik Duttad63eaef2010-07-14 21:01:21 -0700563 static char* confirm_format = "Confirm format?";
Koushik Duttaecd32fa2010-07-14 18:38:02 -0700564 static char* confirm = "Yes - Format";
wjbcea6eba2011-04-30 21:04:04 -0400565 char confirm_string[255];
Steve Kondik4123b582010-11-14 03:18:40 -0500566
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700567 for (;;)
568 {
Steve Kondik4123b582010-11-14 03:18:40 -0500569
Kolja Dummann92796ec2011-02-13 20:59:30 +0100570 for (i = 0; i < mountable_volumes; i++)
571 {
572 MountMenuEntry* e = &mount_menue[i];
573 Volume* v = e->v;
574 if(is_path_mounted(v->mount_point))
575 options[i] = e->unmount;
576 else
577 options[i] = e->mount;
578 }
Steve Kondik4123b582010-11-14 03:18:40 -0500579
Kolja Dummann92796ec2011-02-13 20:59:30 +0100580 for (i = 0; i < formatable_volumes; i++)
581 {
582 FormatMenuEntry* e = &format_menue[i];
Steve Kondik4123b582010-11-14 03:18:40 -0500583
Kolja Dummann92796ec2011-02-13 20:59:30 +0100584 options[mountable_volumes+i] = e->txt;
585 }
586
587 options[mountable_volumes+formatable_volumes] = "mount USB storage";
588 options[mountable_volumes+formatable_volumes + 1] = NULL;
589
590 int chosen_item = get_menu_selection(headers, &options, 0, 0);
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700591 if (chosen_item == GO_BACK)
592 break;
Kolja Dummann92796ec2011-02-13 20:59:30 +0100593 if (chosen_item == (mountable_volumes+formatable_volumes))
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700594 {
595 show_mount_usb_storage_menu();
596 }
Kolja Dummann92796ec2011-02-13 20:59:30 +0100597 else if (chosen_item < mountable_volumes)
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700598 {
Kolja Dummann92796ec2011-02-13 20:59:30 +0100599 MountMenuEntry* e = &mount_menue[chosen_item];
600 Volume* v = e->v;
601
602 if (is_path_mounted(v->mount_point))
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700603 {
Kolja Dummann92796ec2011-02-13 20:59:30 +0100604 if (0 != ensure_path_unmounted(v->mount_point))
605 ui_print("Error unmounting %s!\n", v->mount_point);
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700606 }
607 else
608 {
Kolja Dummann92796ec2011-02-13 20:59:30 +0100609 if (0 != ensure_path_mounted(v->mount_point))
610 ui_print("Error mounting %s!\n", v->mount_point);
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700611 }
612 }
Kolja Dummann92796ec2011-02-13 20:59:30 +0100613 else if (chosen_item < (mountable_volumes + formatable_volumes))
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700614 {
Kolja Dummann92796ec2011-02-13 20:59:30 +0100615 chosen_item = chosen_item - mountable_volumes;
616 FormatMenuEntry* e = &format_menue[chosen_item];
617 Volume* v = e->v;
618
wjbcea6eba2011-04-30 21:04:04 -0400619 sprintf(confirm_string, "%s - %s", v->mount_point, confirm_format);
620
621 if (!confirm_selection(confirm_string, confirm))
Koushik K. Dutta16f0b492010-03-19 14:37:11 -0700622 continue;
Kolja Dummann92796ec2011-02-13 20:59:30 +0100623 ui_print("Formatting %s...\n", v->mount_point);
624 if (0 != format_volume(v->mount_point))
625 ui_print("Error formatting %s!\n", v->mount_point);
Koushik K. Dutta3f995392010-03-30 23:29:43 -0700626 else
627 ui_print("Done.\n");
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700628 }
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700629 }
Kolja Dummann92796ec2011-02-13 20:59:30 +0100630
631 free(mount_menue);
632 free(format_menue);
633
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700634}
635
Koushik K. Dutta72a1db62010-03-07 14:10:26 -0800636#define EXTENDEDCOMMAND_SCRIPT "/cache/recovery/extendedcommand"
637
638int extendedcommand_file_exists()
Koushik K. Duttaf68aaaf2010-03-07 13:39:21 -0800639{
Koushik K. Duttaf68aaaf2010-03-07 13:39:21 -0800640 struct stat file_info;
Koushik K. Dutta72a1db62010-03-07 14:10:26 -0800641 return 0 == stat(EXTENDEDCOMMAND_SCRIPT, &file_info);
642}
643
Koushik Duttabec09952010-12-19 20:37:57 -0800644int edify_main(int argc, char** argv) {
645 load_volume_table();
646 process_volumes();
647 RegisterBuiltins();
648 RegisterRecoveryHooks();
649 FinishRegistration();
Koushik Duttadf1e4062010-12-18 17:42:31 -0800650
Koushik Duttabec09952010-12-19 20:37:57 -0800651 if (argc != 2) {
652 printf("edify <filename>\n");
Koushik K. Duttaf68aaaf2010-03-07 13:39:21 -0800653 return 1;
Koushik K. Duttaf68aaaf2010-03-07 13:39:21 -0800654 }
655
Koushik Duttabec09952010-12-19 20:37:57 -0800656 FILE* f = fopen(argv[1], "r");
657 if (f == NULL) {
658 printf("%s: %s: No such file or directory\n", argv[0], argv[1]);
659 return 1;
660 }
661 char buffer[8192];
662 int size = fread(buffer, 1, 8191, f);
663 fclose(f);
664 buffer[size] = '\0';
665
666 Expr* root;
667 int error_count = 0;
668 yy_scan_bytes(buffer, size);
669 int error = yyparse(&root, &error_count);
670 printf("parse returned %d; %d errors encountered\n", error, error_count);
671 if (error == 0 || error_count > 0) {
672
673 //ExprDump(0, root, buffer);
674
675 State state;
676 state.cookie = NULL;
677 state.script = buffer;
678 state.errmsg = NULL;
679
680 char* result = Evaluate(&state, root);
681 if (result == NULL) {
682 printf("result was NULL, message is: %s\n",
683 (state.errmsg == NULL ? "(NULL)" : state.errmsg));
684 free(state.errmsg);
685 } else {
686 printf("result is [%s]\n", result);
Koushik K. Duttaf68aaaf2010-03-07 13:39:21 -0800687 }
Steve Kondik4123b582010-11-14 03:18:40 -0500688 }
Koushik Duttabec09952010-12-19 20:37:57 -0800689 return 0;
Koushik K. Duttaf68aaaf2010-03-07 13:39:21 -0800690}
Koushik K. Dutta72a1db62010-03-07 14:10:26 -0800691
Koushik K. Dutta6771aca2010-04-03 23:28:39 -0700692int run_script(char* filename)
Koushik K. Dutta3836f722010-03-11 22:17:43 -0800693{
694 struct stat file_info;
695 if (0 != stat(filename, &file_info)) {
696 printf("Error executing stat on file: %s\n", filename);
697 return 1;
698 }
Steve Kondik4123b582010-11-14 03:18:40 -0500699
Koushik K. Dutta3836f722010-03-11 22:17:43 -0800700 int script_len = file_info.st_size;
Koushik K. Dutta707fa6d2010-03-23 11:44:33 -0700701 char* script_data = (char*)malloc(script_len + 1);
Koushik K. Dutta3836f722010-03-11 22:17:43 -0800702 FILE *file = fopen(filename, "rb");
703 fread(script_data, script_len, 1, file);
Koushik K. Dutta707fa6d2010-03-23 11:44:33 -0700704 // supposedly not necessary, but let's be safe.
705 script_data[script_len] = '\0';
Koushik K. Dutta3836f722010-03-11 22:17:43 -0800706 fclose(file);
Koushik Duttae25908b2010-06-21 08:16:19 -0700707 LOGI("Running script:\n");
708 LOGI("\n%s\n", script_data);
Koushik K. Dutta3836f722010-03-11 22:17:43 -0800709
Koushik K. Duttaf3534d02010-04-18 18:06:24 -0700710 int ret = run_script_from_buffer(script_data, script_len, filename);
Koushik K. Dutta1d53c4e2010-04-02 16:46:21 -0700711 free(script_data);
712 return ret;
Koushik K. Dutta3836f722010-03-11 22:17:43 -0800713}
714
Koushik K. Dutta72a1db62010-03-07 14:10:26 -0800715int run_and_remove_extendedcommand()
716{
Koushik K. Dutta6771aca2010-04-03 23:28:39 -0700717 char tmp[PATH_MAX];
718 sprintf(tmp, "cp %s /tmp/%s", EXTENDEDCOMMAND_SCRIPT, basename(EXTENDEDCOMMAND_SCRIPT));
719 __system(tmp);
720 remove(EXTENDEDCOMMAND_SCRIPT);
Koushik K. Dutta3a25cf52010-03-08 19:22:41 -0800721 int i = 0;
722 for (i = 20; i > 0; i--) {
723 ui_print("Waiting for SD Card to mount (%ds)\n", i);
Koushik Duttadf1e4062010-12-18 17:42:31 -0800724 if (ensure_path_mounted("/sdcard") == 0) {
Koushik K. Dutta3a25cf52010-03-08 19:22:41 -0800725 ui_print("SD Card mounted...\n");
726 break;
727 }
728 sleep(1);
729 }
Koushik Dutta92077c12010-07-15 00:10:08 -0700730 remove("/sdcard/clockworkmod/.recoverycheckpoint");
Koushik K. Dutta3a25cf52010-03-08 19:22:41 -0800731 if (i == 0) {
732 ui_print("Timed out waiting for SD card... continuing anyways.");
733 }
Steve Kondik4123b582010-11-14 03:18:40 -0500734
Koushik K. Dutta6771aca2010-04-03 23:28:39 -0700735 sprintf(tmp, "/tmp/%s", basename(EXTENDEDCOMMAND_SCRIPT));
736 return run_script(tmp);
Koushik K. Dutta72a1db62010-03-07 14:10:26 -0800737}
738
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700739void show_nandroid_advanced_restore_menu()
740{
Koushik Duttadf1e4062010-12-18 17:42:31 -0800741 if (ensure_path_mounted("/sdcard") != 0) {
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700742 LOGE ("Can't mount /sdcard\n");
743 return;
744 }
745
746 static char* advancedheaders[] = { "Choose an image to restore",
747 "",
748 "Choose an image to restore",
749 "first. The next menu will",
750 "you more options.",
751 "",
752 NULL
753 };
754
755 char* file = choose_file_menu("/sdcard/clockworkmod/backup/", NULL, advancedheaders);
756 if (file == NULL)
757 return;
758
759 static char* headers[] = { "Nandroid Advanced Restore",
760 "",
761 NULL
762 };
763
764 static char* list[] = { "Restore boot",
765 "Restore system",
766 "Restore data",
767 "Restore cache",
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700768 "Restore sd-ext",
agrabren1f76a5d2010-12-29 12:15:18 -0600769 "Restore wimax",
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700770 NULL
771 };
Koushik Dutta5b7f34a2010-12-29 23:36:03 -0800772
773 char tmp[PATH_MAX];
774 if (0 != get_partition_device("wimax", tmp)) {
775 // disable wimax restore option
776 list[5] = NULL;
777 }
Koushik Duttad63eaef2010-07-14 21:01:21 -0700778
779 static char* confirm_restore = "Confirm restore?";
780
Koushik Duttadf1e4062010-12-18 17:42:31 -0800781 int chosen_item = get_menu_selection(headers, list, 0, 0);
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700782 switch (chosen_item)
783 {
784 case 0:
Koushik Duttad63eaef2010-07-14 21:01:21 -0700785 if (confirm_selection(confirm_restore, "Yes - Restore boot"))
agrabren1f76a5d2010-12-29 12:15:18 -0600786 nandroid_restore(file, 1, 0, 0, 0, 0, 0);
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700787 break;
788 case 1:
Koushik Duttad63eaef2010-07-14 21:01:21 -0700789 if (confirm_selection(confirm_restore, "Yes - Restore system"))
agrabren1f76a5d2010-12-29 12:15:18 -0600790 nandroid_restore(file, 0, 1, 0, 0, 0, 0);
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700791 break;
792 case 2:
Koushik Duttad63eaef2010-07-14 21:01:21 -0700793 if (confirm_selection(confirm_restore, "Yes - Restore data"))
agrabren1f76a5d2010-12-29 12:15:18 -0600794 nandroid_restore(file, 0, 0, 1, 0, 0, 0);
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700795 break;
796 case 3:
Koushik Duttad63eaef2010-07-14 21:01:21 -0700797 if (confirm_selection(confirm_restore, "Yes - Restore cache"))
agrabren1f76a5d2010-12-29 12:15:18 -0600798 nandroid_restore(file, 0, 0, 0, 1, 0, 0);
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700799 break;
Koushik Dutta2f73e582010-04-18 16:00:21 -0700800 case 4:
Koushik Duttad63eaef2010-07-14 21:01:21 -0700801 if (confirm_selection(confirm_restore, "Yes - Restore sd-ext"))
agrabren1f76a5d2010-12-29 12:15:18 -0600802 nandroid_restore(file, 0, 0, 0, 0, 1, 0);
803 break;
804 case 5:
805 if (confirm_selection(confirm_restore, "Yes - Restore wimax"))
806 nandroid_restore(file, 0, 0, 0, 0, 0, 1);
Koushik Dutta2f73e582010-04-18 16:00:21 -0700807 break;
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700808 }
809}
810
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700811void show_nandroid_menu()
812{
813 static char* headers[] = { "Nandroid",
814 "",
Steve Kondik4123b582010-11-14 03:18:40 -0500815 NULL
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700816 };
817
Steve Kondik4123b582010-11-14 03:18:40 -0500818 static char* list[] = { "Backup",
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700819 "Restore",
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700820 "Advanced Restore",
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700821 NULL
822 };
823
Koushik Duttadf1e4062010-12-18 17:42:31 -0800824 int chosen_item = get_menu_selection(headers, list, 0, 0);
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700825 switch (chosen_item)
826 {
827 case 0:
828 {
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700829 char backup_path[PATH_MAX];
Koushik K. Dutta6a26e7c2010-03-27 15:26:11 -0700830 time_t t = time(NULL);
831 struct tm *tmp = localtime(&t);
832 if (tmp == NULL)
833 {
834 struct timeval tp;
835 gettimeofday(&tp, NULL);
836 sprintf(backup_path, "/sdcard/clockworkmod/backup/%d", tp.tv_sec);
837 }
838 else
839 {
840 strftime(backup_path, sizeof(backup_path), "/sdcard/clockworkmod/backup/%F.%H.%M.%S", tmp);
841 }
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700842 nandroid_backup(backup_path);
843 }
844 break;
845 case 1:
846 show_nandroid_restore_menu();
847 break;
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700848 case 2:
849 show_nandroid_advanced_restore_menu();
850 break;
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700851 }
Koushik K. Duttaa496b512010-03-19 14:51:45 -0700852}
853
Koushik Duttafd1579b2010-05-01 12:46:55 -0700854void wipe_battery_stats()
855{
Koushik Duttadf1e4062010-12-18 17:42:31 -0800856 ensure_path_mounted("/data");
Koushik Duttafd1579b2010-05-01 12:46:55 -0700857 remove("/data/system/batterystats.bin");
Koushik Duttadf1e4062010-12-18 17:42:31 -0800858 ensure_path_unmounted("/data");
Koushik Duttafd1579b2010-05-01 12:46:55 -0700859}
860
Koushik K. Duttaa496b512010-03-19 14:51:45 -0700861void show_advanced_menu()
862{
863 static char* headers[] = { "Advanced and Debugging Menu",
864 "",
865 NULL
866 };
867
Koushik K. Dutta7fe4d7b2010-04-06 23:04:52 -0700868 static char* list[] = { "Reboot Recovery",
Koushik Dutta49af23c2010-06-21 13:45:51 -0700869 "Wipe Dalvik Cache",
Koushik Duttafd1579b2010-05-01 12:46:55 -0700870 "Wipe Battery Stats",
Koushik Dutta598cfc72010-06-20 09:42:47 -0700871 "Report Error",
Koushik K. Dutta7fe4d7b2010-04-06 23:04:52 -0700872 "Key Test",
Koushik Duttae17a78d2010-08-29 12:35:10 -0700873#ifndef BOARD_HAS_SMALL_RECOVERY
Koushik Duttaceddcd52010-08-23 16:13:14 -0700874 "Partition SD Card",
875 "Fix Permissions",
Brint E. Kriebeld3522332010-12-11 20:52:06 -0700876#ifdef BOARD_HAS_SDCARD_INTERNAL
877 "Partition Internal SD Card",
878#endif
Koushik Duttae17a78d2010-08-29 12:35:10 -0700879#endif
Koushik K. Duttaa496b512010-03-19 14:51:45 -0700880 NULL
881 };
882
883 for (;;)
884 {
Koushik Duttadf1e4062010-12-18 17:42:31 -0800885 int chosen_item = get_menu_selection(headers, list, 0, 0);
Koushik K. Duttaa496b512010-03-19 14:51:45 -0700886 if (chosen_item == GO_BACK)
887 break;
888 switch (chosen_item)
889 {
890 case 0:
atinm54815c92011-05-03 00:25:01 -0400891#ifdef TARGET_RECOVERY_PRE_COMMAND
892 __system( TARGET_RECOVERY_PRE_COMMAND );
893#endif
Koushik K. Dutta7fe4d7b2010-04-06 23:04:52 -0700894 __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, "recovery");
895 break;
896 case 1:
Koushik Duttad63eaef2010-07-14 21:01:21 -0700897 {
Koushik Duttadf1e4062010-12-18 17:42:31 -0800898 if (0 != ensure_path_mounted("/data"))
Koushik Dutta49af23c2010-06-21 13:45:51 -0700899 break;
Koushik Duttadf1e4062010-12-18 17:42:31 -0800900 ensure_path_mounted("/sd-ext");
901 ensure_path_mounted("/cache");
Koushik Dutta6c7745d2010-08-07 12:17:13 -0700902 if (confirm_selection( "Confirm wipe?", "Yes - Wipe Dalvik Cache")) {
Koushik Duttad63eaef2010-07-14 21:01:21 -0700903 __system("rm -r /data/dalvik-cache");
Koushik Dutta6c7745d2010-08-07 12:17:13 -0700904 __system("rm -r /cache/dalvik-cache");
905 __system("rm -r /sd-ext/dalvik-cache");
906 }
Koushik Duttadf1e4062010-12-18 17:42:31 -0800907 ensure_path_unmounted("/data");
Koushik Dutta6c7745d2010-08-07 12:17:13 -0700908 ui_print("Dalvik Cache wiped.\n");
Koushik Duttafd1579b2010-05-01 12:46:55 -0700909 break;
Koushik Duttad63eaef2010-07-14 21:01:21 -0700910 }
Koushik Duttafd1579b2010-05-01 12:46:55 -0700911 case 2:
Koushik Duttad63eaef2010-07-14 21:01:21 -0700912 {
913 if (confirm_selection( "Confirm wipe?", "Yes - Wipe Battery Stats"))
914 wipe_battery_stats();
Koushik Dutta598cfc72010-06-20 09:42:47 -0700915 break;
Koushik Duttad63eaef2010-07-14 21:01:21 -0700916 }
Koushik Dutta598cfc72010-06-20 09:42:47 -0700917 case 3:
Koushik Dutta49af23c2010-06-21 13:45:51 -0700918 handle_failure(1);
919 break;
920 case 4:
Koushik K. Duttaa496b512010-03-19 14:51:45 -0700921 {
922 ui_print("Outputting key codes.\n");
923 ui_print("Go back to end debugging.\n");
924 int key;
925 int action;
926 do
927 {
928 key = ui_wait_key();
929 action = device_handle_key(key, 1);
930 ui_print("Key: %d\n", key);
931 }
932 while (action != GO_BACK);
Koushik Dutta56606a22010-08-23 16:15:33 -0700933 break;
Koushik K. Duttaa496b512010-03-19 14:51:45 -0700934 }
Koushik Duttaf0e31b82010-08-17 16:55:38 -0700935 case 5:
936 {
Koushik Duttaceddcd52010-08-23 16:13:14 -0700937 static char* ext_sizes[] = { "128M",
938 "256M",
939 "512M",
940 "1024M",
Brint E. Kriebel0b233312010-11-14 15:31:17 -0700941 "2048M",
942 "4096M",
Koushik Duttaceddcd52010-08-23 16:13:14 -0700943 NULL };
944
945 static char* swap_sizes[] = { "0M",
946 "32M",
947 "64M",
948 "128M",
949 "256M",
950 NULL };
951
952 static char* ext_headers[] = { "Ext Size", "", NULL };
953 static char* swap_headers[] = { "Swap Size", "", NULL };
954
Koushik Duttadf1e4062010-12-18 17:42:31 -0800955 int ext_size = get_menu_selection(ext_headers, ext_sizes, 0, 0);
Koushik Duttaceddcd52010-08-23 16:13:14 -0700956 if (ext_size == GO_BACK)
957 continue;
Steve Kondik4123b582010-11-14 03:18:40 -0500958
Koushik Duttadf1e4062010-12-18 17:42:31 -0800959 int swap_size = get_menu_selection(swap_headers, swap_sizes, 0, 0);
Koushik Duttaceddcd52010-08-23 16:13:14 -0700960 if (swap_size == GO_BACK)
961 continue;
962
963 char sddevice[256];
Koushik Duttadf1e4062010-12-18 17:42:31 -0800964 Volume *vol = volume_for_path("/sdcard");
965 strcpy(sddevice, vol->device);
Koushik Duttaceddcd52010-08-23 16:13:14 -0700966 // we only want the mmcblk, not the partition
967 sddevice[strlen("/dev/block/mmcblkX")] = NULL;
968 char cmd[PATH_MAX];
969 setenv("SDPATH", sddevice, 1);
970 sprintf(cmd, "sdparted -es %s -ss %s -efs ext3 -s", ext_sizes[ext_size], swap_sizes[swap_size]);
971 ui_print("Partitioning SD Card... please wait...\n");
972 if (0 == __system(cmd))
973 ui_print("Done!\n");
974 else
975 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 -0700976 break;
Koushik Duttaf0e31b82010-08-17 16:55:38 -0700977 }
Koushik Dutta38e8b2b2010-08-17 22:21:53 -0700978 case 6:
979 {
Koushik Duttadf1e4062010-12-18 17:42:31 -0800980 ensure_path_mounted("/system");
981 ensure_path_mounted("/data");
Koushik Duttaceddcd52010-08-23 16:13:14 -0700982 ui_print("Fixing permissions...\n");
983 __system("fix_permissions");
984 ui_print("Done!\n");
Koushik Dutta56606a22010-08-23 16:15:33 -0700985 break;
Koushik Dutta38e8b2b2010-08-17 22:21:53 -0700986 }
Brint E. Kriebeld3522332010-12-11 20:52:06 -0700987 case 7:
988 {
989 static char* ext_sizes[] = { "128M",
990 "256M",
991 "512M",
992 "1024M",
993 "2048M",
994 "4096M",
995 NULL };
996
997 static char* swap_sizes[] = { "0M",
998 "32M",
999 "64M",
1000 "128M",
1001 "256M",
1002 NULL };
1003
1004 static char* ext_headers[] = { "Data Size", "", NULL };
1005 static char* swap_headers[] = { "Swap Size", "", NULL };
1006
Koushik Duttadf1e4062010-12-18 17:42:31 -08001007 int ext_size = get_menu_selection(ext_headers, ext_sizes, 0, 0);
Brint E. Kriebeld3522332010-12-11 20:52:06 -07001008 if (ext_size == GO_BACK)
1009 continue;
1010
1011 int swap_size = 0;
1012 if (swap_size == GO_BACK)
1013 continue;
1014
1015 char sddevice[256];
Koushik Duttadf1e4062010-12-18 17:42:31 -08001016 Volume *vol = volume_for_path("/emmc");
1017 strcpy(sddevice, vol->device);
Brint E. Kriebeld3522332010-12-11 20:52:06 -07001018 // we only want the mmcblk, not the partition
1019 sddevice[strlen("/dev/block/mmcblkX")] = NULL;
1020 char cmd[PATH_MAX];
1021 setenv("SDPATH", sddevice, 1);
1022 sprintf(cmd, "sdparted -es %s -ss %s -efs ext3 -s", ext_sizes[ext_size], swap_sizes[swap_size]);
1023 ui_print("Partitioning Internal SD Card... please wait...\n");
1024 if (0 == __system(cmd))
1025 ui_print("Done!\n");
1026 else
1027 ui_print("An error occured while partitioning your Internal SD Card. Please see /tmp/recovery.log for more details.\n");
1028 break;
1029 }
Koushik K. Duttaa496b512010-03-19 14:51:45 -07001030 }
1031 }
Koushik Dutta14239d22010-06-14 15:02:48 -07001032}
1033
Koushik Duttab643e4f2010-12-18 18:39:39 -08001034void write_fstab_root(char *path, FILE *file)
Koushik Dutta14239d22010-06-14 15:02:48 -07001035{
Koushik Duttab643e4f2010-12-18 18:39:39 -08001036 Volume *vol = volume_for_path(path);
1037 if (vol == NULL) {
Koushik Dutta9765a032010-12-18 21:31:02 -08001038 LOGW("Unable to get recovery.fstab info for %s during fstab generation!\n", path);
Koushik Dutta598cfc72010-06-20 09:42:47 -07001039 return;
Koushik Duttaa6522b32010-06-20 13:16:06 -07001040 }
Steve Kondik4123b582010-11-14 03:18:40 -05001041
Koushik Dutta31b74112010-12-20 08:49:20 -08001042 char device[200];
1043 if (vol->device[0] != '/')
1044 get_partition_device(vol->device, device);
1045 else
1046 strcpy(device, vol->device);
1047
1048 fprintf(file, "%s ", device);
Koushik Duttab643e4f2010-12-18 18:39:39 -08001049 fprintf(file, "%s ", path);
Koushik Duttaab1f8b82011-02-27 17:28:30 -08001050 // special case rfs cause auto will mount it as vfat on samsung.
1051 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 -07001052}
1053
1054void create_fstab()
1055{
Koushik Duttab643e4f2010-12-18 18:39:39 -08001056 struct stat info;
Koushik Duttacd44ab92010-06-23 00:02:14 -07001057 __system("touch /etc/mtab");
Koushik Dutta14239d22010-06-14 15:02:48 -07001058 FILE *file = fopen("/etc/fstab", "w");
Koushik Duttaa6522b32010-06-20 13:16:06 -07001059 if (file == NULL) {
Koushik Duttab643e4f2010-12-18 18:39:39 -08001060 LOGW("Unable to create /etc/fstab!\n");
Koushik Dutta598cfc72010-06-20 09:42:47 -07001061 return;
Koushik Duttaa6522b32010-06-20 13:16:06 -07001062 }
Brandon Bennett6d0604b2011-01-28 13:39:10 -07001063 Volume *vol = volume_for_path("/boot");
Koushik Duttaa9f0e7f2011-02-12 10:21:11 -08001064 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 -07001065 write_fstab_root("/boot", file);
Koushik Duttab643e4f2010-12-18 18:39:39 -08001066 write_fstab_root("/cache", file);
1067 write_fstab_root("/data", file);
Koushik Dutta5460f0c2010-12-18 22:37:49 -08001068 if (has_datadata()) {
1069 write_fstab_root("/datadata", file);
1070 }
Koushik Duttab643e4f2010-12-18 18:39:39 -08001071 write_fstab_root("/system", file);
1072 write_fstab_root("/sdcard", file);
1073 write_fstab_root("/sd-ext", file);
Koushik Dutta14239d22010-06-14 15:02:48 -07001074 fclose(file);
Koushik Dutta9765a032010-12-18 21:31:02 -08001075 LOGI("Completed outputting fstab.\n");
Koushik Dutta598cfc72010-06-20 09:42:47 -07001076}
1077
Koushik Dutta4196e0f2010-12-19 03:38:29 -08001078int bml_check_volume(const char *path) {
1079 ui_print("Checking %s...\n", path);
1080 ensure_path_unmounted(path);
1081 if (0 == ensure_path_mounted(path)) {
1082 ensure_path_unmounted(path);
1083 return 0;
1084 }
1085
1086 Volume *vol = volume_for_path(path);
1087 if (vol == NULL) {
1088 LOGE("Unable process volume! Skipping...\n");
1089 return 0;
1090 }
1091
Koushik Duttabec09952010-12-19 20:37:57 -08001092 ui_print("%s may be rfs. Checking...\n", path);
Koushik Dutta4196e0f2010-12-19 03:38:29 -08001093 char tmp[PATH_MAX];
1094 sprintf(tmp, "mount -t rfs %s %s", vol->device, path);
1095 int ret = __system(tmp);
1096 printf("%d\n", ret);
1097 return ret == 0 ? 1 : 0;
1098}
1099
1100void process_volumes() {
1101 create_fstab();
1102
Koushik Duttae8bc2c82011-02-27 14:00:19 -08001103 return;
1104
1105 // dead code.
Koushik Dutta4196e0f2010-12-19 03:38:29 -08001106 if (device_flash_type() != BML)
1107 return;
1108
1109 ui_print("Checking for ext4 partitions...\n");
1110 int ret = 0;
1111 ret = bml_check_volume("/system");
1112 ret |= bml_check_volume("/data");
1113 if (has_datadata())
1114 ret |= bml_check_volume("/datadata");
1115 ret |= bml_check_volume("/cache");
1116
1117 if (ret == 0) {
1118 ui_print("Done!\n");
1119 return;
1120 }
1121
Koushik Dutta745b5ff2010-12-21 00:58:43 -08001122 char backup_path[PATH_MAX];
1123 time_t t = time(NULL);
1124 char backup_name[PATH_MAX];
1125 struct timeval tp;
1126 gettimeofday(&tp, NULL);
1127 sprintf(backup_name, "before-ext4-convert-%d", tp.tv_sec);
1128 sprintf(backup_path, "/sdcard/clockworkmod/backup/%s", backup_name);
1129
Koushik Dutta4196e0f2010-12-19 03:38:29 -08001130 ui_set_show_text(1);
1131 ui_print("Filesystems need to be converted to ext4.\n");
1132 ui_print("A backup and restore will now take place.\n");
Koushik Duttabec09952010-12-19 20:37:57 -08001133 ui_print("If anything goes wrong, your backup will be\n");
Koushik Dutta745b5ff2010-12-21 00:58:43 -08001134 ui_print("named %s. Try restoring it\n", backup_name);
Koushik Duttabec09952010-12-19 20:37:57 -08001135 ui_print("in case of error.\n");
Koushik Dutta4196e0f2010-12-19 03:38:29 -08001136
Koushik Dutta745b5ff2010-12-21 00:58:43 -08001137 nandroid_backup(backup_path);
Koushik Dutta5b7f34a2010-12-29 23:36:03 -08001138 nandroid_restore(backup_path, 1, 1, 1, 1, 1, 0);
Koushik Dutta4196e0f2010-12-19 03:38:29 -08001139 ui_set_show_text(0);
1140}
1141
Koushik Dutta598cfc72010-06-20 09:42:47 -07001142void handle_failure(int ret)
1143{
1144 if (ret == 0)
1145 return;
Koushik Duttadf1e4062010-12-18 17:42:31 -08001146 if (0 != ensure_path_mounted("/sdcard"))
Koushik Dutta598cfc72010-06-20 09:42:47 -07001147 return;
Koushik Duttaa6522b32010-06-20 13:16:06 -07001148 mkdir("/sdcard/clockworkmod", S_IRWXU);
1149 __system("cp /tmp/recovery.log /sdcard/clockworkmod/recovery.log");
Koushik Dutta38e8b2b2010-08-17 22:21:53 -07001150 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 -05001151}
Koushik Duttadf1e4062010-12-18 17:42:31 -08001152
Koushik Duttadf1e4062010-12-18 17:42:31 -08001153int is_path_mounted(const char* path) {
1154 Volume* v = volume_for_path(path);
1155 if (v == NULL) {
1156 return 0;
1157 }
1158 if (strcmp(v->fs_type, "ramdisk") == 0) {
1159 // the ramdisk is always mounted.
1160 return 1;
1161 }
1162
1163 int result;
1164 result = scan_mounted_volumes();
1165 if (result < 0) {
1166 LOGE("failed to scan mounted volumes\n");
1167 return 0;
1168 }
1169
1170 const MountedVolume* mv =
1171 find_mounted_volume_by_mount_point(v->mount_point);
1172 if (mv) {
1173 // volume is already mounted
1174 return 1;
1175 }
1176 return 0;
1177}
Koushik Dutta5460f0c2010-12-18 22:37:49 -08001178
1179int has_datadata() {
1180 Volume *vol = volume_for_path("/datadata");
1181 return vol != NULL;
Koushik Duttad8e21c32011-01-04 10:46:23 -08001182}
1183
1184int volume_main(int argc, char **argv) {
1185 load_volume_table();
1186 return 0;
1187}