blob: ec4ddc7a65be9bea8d828797bb0205b62c5ce096 [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>
43
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -080044
Koushik K. Dutta6060e5c2010-02-11 22:27:06 -080045int signature_check_enabled = 1;
46int script_assert_enabled = 1;
Koushik Duttadf1e4062010-12-18 17:42:31 -080047static const char *SDCARD_UPDATE_FILE = "/sdcard/update.zip";
Koushik K. Dutta6060e5c2010-02-11 22:27:06 -080048
49void
50toggle_signature_check()
51{
52 signature_check_enabled = !signature_check_enabled;
53 ui_print("Signature Check: %s\n", signature_check_enabled ? "Enabled" : "Disabled");
54}
55
56void toggle_script_asserts()
57{
58 script_assert_enabled = !script_assert_enabled;
Koushik K. Duttae9234872010-02-12 00:43:24 -080059 ui_print("Script Asserts: %s\n", script_assert_enabled ? "Enabled" : "Disabled");
60}
61
Koushik K. Duttaea46fe22010-03-08 02:58:04 -080062int install_zip(const char* packagefilepath)
Koushik K. Duttae9234872010-02-12 00:43:24 -080063{
Koushik K. Duttabcdd0032010-02-21 21:10:25 -080064 ui_print("\n-- Installing: %s\n", packagefilepath);
Koushik Dutta9765a032010-12-18 21:31:02 -080065 if (device_flash_type() == MTD) {
66 set_sdcard_update_bootloader_message();
67 }
Koushik K. Duttabcdd0032010-02-21 21:10:25 -080068 int status = install_package(packagefilepath);
Koushik K. Duttaf3534d02010-04-18 18:06:24 -070069 ui_reset_progress();
Koushik K. Duttabcdd0032010-02-21 21:10:25 -080070 if (status != INSTALL_SUCCESS) {
71 ui_set_background(BACKGROUND_ICON_ERROR);
72 ui_print("Installation aborted.\n");
Koushik K. Duttaea46fe22010-03-08 02:58:04 -080073 return 1;
Steve Kondik4123b582010-11-14 03:18:40 -050074 }
Koushik K. Dutta001c5b52010-02-25 14:53:57 -080075 ui_set_background(BACKGROUND_ICON_NONE);
Koushik K. Dutta99fb6fe2010-03-03 00:42:58 -080076 ui_print("\nInstall from sdcard complete.\n");
Koushik K. Duttaea46fe22010-03-08 02:58:04 -080077 return 0;
Koushik K. Duttabcdd0032010-02-21 21:10:25 -080078}
79
Tanguy Pruvotaaad77f2011-06-18 10:26:32 +020080char* INSTALL_MENU_ITEMS[] = { "choose zip from sdcard",
81 "apply /sdcard/update.zip",
Koushik K. Duttabcdd0032010-02-21 21:10:25 -080082 "toggle signature verification",
83 "toggle script asserts",
84 NULL };
Tanguy Pruvotaaad77f2011-06-18 10:26:32 +020085#define ITEM_CHOOSE_ZIP 0
86#define ITEM_APPLY_SDCARD 1
Koushik K. Duttabcdd0032010-02-21 21:10:25 -080087#define ITEM_SIG_CHECK 2
88#define ITEM_ASSERTS 3
89
90void show_install_update_menu()
91{
92 static char* headers[] = { "Apply update from .zip file on SD card",
93 "",
Steve Kondik4123b582010-11-14 03:18:40 -050094 NULL
Koushik K. Duttabcdd0032010-02-21 21:10:25 -080095 };
96 for (;;)
97 {
Koushik Duttadf1e4062010-12-18 17:42:31 -080098 int chosen_item = get_menu_selection(headers, INSTALL_MENU_ITEMS, 0, 0);
Koushik K. Duttabcdd0032010-02-21 21:10:25 -080099 switch (chosen_item)
100 {
101 case ITEM_ASSERTS:
102 toggle_script_asserts();
103 break;
104 case ITEM_SIG_CHECK:
105 toggle_signature_check();
106 break;
107 case ITEM_APPLY_SDCARD:
Koushik Duttad63eaef2010-07-14 21:01:21 -0700108 {
109 if (confirm_selection("Confirm install?", "Yes - Install /sdcard/update.zip"))
Koushik Duttadf1e4062010-12-18 17:42:31 -0800110 install_zip(SDCARD_UPDATE_FILE);
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800111 break;
Koushik Duttad63eaef2010-07-14 21:01:21 -0700112 }
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800113 case ITEM_CHOOSE_ZIP:
114 show_choose_zip_menu();
115 break;
116 default:
117 return;
118 }
Steve Kondik4123b582010-11-14 03:18:40 -0500119
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800120 }
121}
122
Koushik K. Dutta1d53c4e2010-04-02 16:46:21 -0700123void free_string_array(char** array)
124{
125 if (array == NULL)
126 return;
127 char* cursor = array[0];
128 int i = 0;
129 while (cursor != NULL)
130 {
131 free(cursor);
132 cursor = array[++i];
133 }
134 free(array);
135}
136
Koushik K. Dutta49f56892010-02-25 14:51:05 -0800137char** gather_files(const char* directory, const char* fileExtensionOrDirectory, int* numFiles)
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800138{
Koushik K. Duttae9234872010-02-12 00:43:24 -0800139 char path[PATH_MAX] = "";
140 DIR *dir;
141 struct dirent *de;
142 int total = 0;
143 int i;
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800144 char** files = NULL;
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800145 int pass;
146 *numFiles = 0;
147 int dirLen = strlen(directory);
Koushik K. Duttae9234872010-02-12 00:43:24 -0800148
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800149 dir = opendir(directory);
150 if (dir == NULL) {
151 ui_print("Couldn't open directory.\n");
152 return NULL;
153 }
Steve Kondik4123b582010-11-14 03:18:40 -0500154
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800155 int extension_length = 0;
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800156 if (fileExtensionOrDirectory != NULL)
157 extension_length = strlen(fileExtensionOrDirectory);
Steve Kondik4123b582010-11-14 03:18:40 -0500158
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800159 int isCounting = 1;
160 i = 0;
161 for (pass = 0; pass < 2; pass++) {
162 while ((de=readdir(dir)) != NULL) {
163 // skip hidden files
164 if (de->d_name[0] == '.')
165 continue;
Steve Kondik4123b582010-11-14 03:18:40 -0500166
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800167 // NULL means that we are gathering directories, so skip this
168 if (fileExtensionOrDirectory != NULL)
169 {
170 // make sure that we can have the desired extension (prevent seg fault)
171 if (strlen(de->d_name) < extension_length)
172 continue;
173 // compare the extension
174 if (strcmp(de->d_name + strlen(de->d_name) - extension_length, fileExtensionOrDirectory) != 0)
175 continue;
176 }
177 else
178 {
179 struct stat info;
Koushik K. Dutta1d53c4e2010-04-02 16:46:21 -0700180 char fullFileName[PATH_MAX];
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800181 strcpy(fullFileName, directory);
182 strcat(fullFileName, de->d_name);
183 stat(fullFileName, &info);
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800184 // make sure it is a directory
185 if (!(S_ISDIR(info.st_mode)))
186 continue;
187 }
Steve Kondik4123b582010-11-14 03:18:40 -0500188
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800189 if (pass == 0)
190 {
191 total++;
192 continue;
193 }
Steve Kondik4123b582010-11-14 03:18:40 -0500194
Koushik K. Dutta49f56892010-02-25 14:51:05 -0800195 files[i] = (char*) malloc(dirLen + strlen(de->d_name) + 2);
196 strcpy(files[i], directory);
197 strcat(files[i], de->d_name);
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800198 if (fileExtensionOrDirectory == NULL)
199 strcat(files[i], "/");
200 i++;
201 }
202 if (pass == 1)
203 break;
204 if (total == 0)
205 break;
206 rewinddir(dir);
207 *numFiles = total;
208 files = (char**) malloc((total+1)*sizeof(char*));
209 files[total]=NULL;
210 }
Koushik K. Dutta49f56892010-02-25 14:51:05 -0800211
212 if(closedir(dir) < 0) {
213 LOGE("Failed to close directory.");
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800214 }
215
216 if (total==0) {
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800217 return NULL;
218 }
219
agrabren1f76a5d2010-12-29 12:15:18 -0600220 // sort the result
221 if (files != NULL) {
222 for (i = 0; i < total; i++) {
223 int curMax = -1;
224 int j;
225 for (j = 0; j < total - i; j++) {
226 if (curMax == -1 || strcmp(files[curMax], files[j]) < 0)
227 curMax = j;
228 }
229 char* temp = files[curMax];
230 files[curMax] = files[total - i - 1];
231 files[total - i - 1] = temp;
232 }
233 }
Koushik Dutta1e8aaba2010-07-01 00:23:25 -0700234
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800235 return files;
Koushik K. Dutta49f56892010-02-25 14:51:05 -0800236}
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800237
Koushik K. Dutta49f56892010-02-25 14:51:05 -0800238// pass in NULL for fileExtensionOrDirectory and you will get a directory chooser
239char* choose_file_menu(const char* directory, const char* fileExtensionOrDirectory, const char* headers[])
240{
241 char path[PATH_MAX] = "";
242 DIR *dir;
243 struct dirent *de;
244 int numFiles = 0;
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800245 int numDirs = 0;
Koushik K. Dutta49f56892010-02-25 14:51:05 -0800246 int i;
Koushik K. Dutta1d53c4e2010-04-02 16:46:21 -0700247 char* return_value = NULL;
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800248 int dir_len = strlen(directory);
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800249
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800250 char** files = gather_files(directory, fileExtensionOrDirectory, &numFiles);
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800251 char** dirs = NULL;
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800252 if (fileExtensionOrDirectory != NULL)
253 dirs = gather_files(directory, NULL, &numDirs);
254 int total = numDirs + numFiles;
255 if (total == 0)
256 {
257 ui_print("No files found.\n");
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800258 }
Koushik K. Dutta1d53c4e2010-04-02 16:46:21 -0700259 else
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800260 {
Koushik K. Dutta1d53c4e2010-04-02 16:46:21 -0700261 char** list = (char**) malloc((total + 1) * sizeof(char*));
262 list[total] = NULL;
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800263
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800264
Koushik K. Dutta1d53c4e2010-04-02 16:46:21 -0700265 for (i = 0 ; i < numDirs; i++)
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800266 {
Koushik K. Dutta1d53c4e2010-04-02 16:46:21 -0700267 list[i] = strdup(dirs[i] + dir_len);
268 }
269
270 for (i = 0 ; i < numFiles; i++)
271 {
272 list[numDirs + i] = strdup(files[i] + dir_len);
273 }
274
275 for (;;)
276 {
Koushik Duttadf1e4062010-12-18 17:42:31 -0800277 int chosen_item = get_menu_selection(headers, list, 0, 0);
Koushik K. Dutta1d53c4e2010-04-02 16:46:21 -0700278 if (chosen_item == GO_BACK)
279 break;
280 static char ret[PATH_MAX];
281 if (chosen_item < numDirs)
282 {
283 char* subret = choose_file_menu(dirs[chosen_item], fileExtensionOrDirectory, headers);
284 if (subret != NULL)
285 {
286 strcpy(ret, subret);
287 return_value = ret;
288 break;
289 }
290 continue;
Steve Kondik4123b582010-11-14 03:18:40 -0500291 }
Koushik K. Dutta1d53c4e2010-04-02 16:46:21 -0700292 strcpy(ret, files[chosen_item - numDirs]);
293 return_value = ret;
294 break;
295 }
296 free_string_array(list);
Koushik K. Dutta981b0cd2010-02-22 08:53:34 -0800297 }
Koushik K. Dutta1d53c4e2010-04-02 16:46:21 -0700298
299 free_string_array(files);
300 free_string_array(dirs);
301 return return_value;
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800302}
303
304void show_choose_zip_menu()
305{
Koushik Duttadf1e4062010-12-18 17:42:31 -0800306 if (ensure_path_mounted("/sdcard") != 0) {
Koushik K. Duttae9234872010-02-12 00:43:24 -0800307 LOGE ("Can't mount /sdcard\n");
308 return;
309 }
310
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800311 static char* headers[] = { "Choose a zip to apply",
312 "",
Steve Kondik4123b582010-11-14 03:18:40 -0500313 NULL
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800314 };
Steve Kondik4123b582010-11-14 03:18:40 -0500315
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800316 char* file = choose_file_menu("/sdcard/", ".zip", headers);
317 if (file == NULL)
318 return;
Koushik Duttad63eaef2010-07-14 21:01:21 -0700319 static char* confirm_install = "Confirm install?";
320 static char confirm[PATH_MAX];
321 sprintf(confirm, "Yes - Install %s", basename(file));
322 if (confirm_selection(confirm_install, confirm))
Koushik Duttadf1e4062010-12-18 17:42:31 -0800323 install_zip(file);
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800324}
325
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800326void show_nandroid_restore_menu()
327{
Koushik Duttadf1e4062010-12-18 17:42:31 -0800328 if (ensure_path_mounted("/sdcard") != 0) {
Koushik K. Dutta54305a82010-03-12 17:43:26 -0800329 LOGE ("Can't mount /sdcard\n");
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800330 return;
Koushik K. Dutta54305a82010-03-12 17:43:26 -0800331 }
Steve Kondik4123b582010-11-14 03:18:40 -0500332
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800333 static char* headers[] = { "Choose an image to restore",
334 "",
Steve Kondik4123b582010-11-14 03:18:40 -0500335 NULL
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800336 };
Koushik K. Duttae9234872010-02-12 00:43:24 -0800337
Koushik K. Dutta49f56892010-02-25 14:51:05 -0800338 char* file = choose_file_menu("/sdcard/clockworkmod/backup/", NULL, headers);
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800339 if (file == NULL)
340 return;
Koushik Duttad63eaef2010-07-14 21:01:21 -0700341
342 if (confirm_selection("Confirm restore?", "Yes - Restore"))
Koushik Dutta5b7f34a2010-12-29 23:36:03 -0800343 nandroid_restore(file, 1, 1, 1, 1, 1, 0);
Koushik K. Dutta03173782010-02-26 14:14:23 -0800344}
345
jt113447d86a32011-02-27 18:54:09 -0600346#ifndef BOARD_UMS_LUNFILE
347#define BOARD_UMS_LUNFILE "/sys/devices/platform/usb_mass_storage/lun0/file"
348#endif
349
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700350void show_mount_usb_storage_menu()
Koushik K. Dutta03173782010-02-26 14:14:23 -0800351{
Koushik Duttaf1cb0d52011-01-01 20:03:30 -0800352 int fd;
Koushik Duttadf1e4062010-12-18 17:42:31 -0800353 Volume *vol = volume_for_path("/sdcard");
jt113447d86a32011-02-27 18:54:09 -0600354 if ((fd = open(BOARD_UMS_LUNFILE, O_WRONLY)) < 0) {
Koushik Duttaf1cb0d52011-01-01 20:03:30 -0800355 LOGE("Unable to open ums lunfile (%s)", strerror(errno));
356 return -1;
357 }
358
mik_osdf3004b2011-04-26 19:15:22 +0300359 if ((write(fd, vol->device, strlen(vol->device)) < 0) &&
360 (!vol->device2 || (write(fd, vol->device, strlen(vol->device2)) < 0))) {
Koushik Duttaf1cb0d52011-01-01 20:03:30 -0800361 LOGE("Unable to write to ums lunfile (%s)", strerror(errno));
362 close(fd);
363 return -1;
364 }
Koushik K. Dutta03173782010-02-26 14:14:23 -0800365 static char* headers[] = { "USB Mass Storage device",
366 "Leaving this menu unmount",
367 "your SD card from your PC.",
368 "",
Steve Kondik4123b582010-11-14 03:18:40 -0500369 NULL
Koushik K. Dutta03173782010-02-26 14:14:23 -0800370 };
Steve Kondik4123b582010-11-14 03:18:40 -0500371
Koushik K. Dutta03173782010-02-26 14:14:23 -0800372 static char* list[] = { "Unmount", NULL };
Steve Kondik4123b582010-11-14 03:18:40 -0500373
Koushik K. Dutta03173782010-02-26 14:14:23 -0800374 for (;;)
375 {
Koushik Duttadf1e4062010-12-18 17:42:31 -0800376 int chosen_item = get_menu_selection(headers, list, 0, 0);
Koushik K. Dutta03173782010-02-26 14:14:23 -0800377 if (chosen_item == GO_BACK || chosen_item == 0)
378 break;
379 }
Steve Kondik4123b582010-11-14 03:18:40 -0500380
jt113447d86a32011-02-27 18:54:09 -0600381 if ((fd = open(BOARD_UMS_LUNFILE, O_WRONLY)) < 0) {
Koushik Duttaf1cb0d52011-01-01 20:03:30 -0800382 LOGE("Unable to open ums lunfile (%s)", strerror(errno));
383 return -1;
384 }
385
386 char ch = 0;
387 if (write(fd, &ch, 1) < 0) {
388 LOGE("Unable to write to ums lunfile (%s)", strerror(errno));
389 close(fd);
390 return -1;
391 }
Koushik K. Duttae81cb752010-02-26 17:44:33 -0800392}
Koushik K. Duttaf68aaaf2010-03-07 13:39:21 -0800393
Koushik Duttad63eaef2010-07-14 21:01:21 -0700394int confirm_selection(const char* title, const char* confirm)
Koushik K. Dutta16f0b492010-03-19 14:37:11 -0700395{
Koushik Duttaceddcd52010-08-23 16:13:14 -0700396 struct stat info;
397 if (0 == stat("/sdcard/clockworkmod/.no_confirm", &info))
398 return 1;
399
Koushik Duttad63eaef2010-07-14 21:01:21 -0700400 char* confirm_headers[] = { title, " THIS CAN NOT BE UNDONE.", "", NULL };
401 char* items[] = { "No",
402 "No",
403 "No",
404 "No",
405 "No",
406 "No",
407 "No",
Koushik Duttaecd32fa2010-07-14 18:38:02 -0700408 confirm, //" Yes -- wipe partition", // [7
Koushik Duttad63eaef2010-07-14 21:01:21 -0700409 "No",
410 "No",
411 "No",
Koushik K. Dutta16f0b492010-03-19 14:37:11 -0700412 NULL };
413
Koushik Duttadf1e4062010-12-18 17:42:31 -0800414 int chosen_item = get_menu_selection(confirm_headers, items, 0, 0);
Koushik K. Dutta16f0b492010-03-19 14:37:11 -0700415 return chosen_item == 7;
416}
417
Koushik Duttada32b542011-01-01 17:55:22 -0800418#define MKE2FS_BIN "/sbin/mke2fs"
419#define TUNE2FS_BIN "/sbin/tune2fs"
420#define E2FSCK_BIN "/sbin/e2fsck"
421
Koushik Duttada32b542011-01-01 17:55:22 -0800422int format_unknown_device(const char *device, const char* path, const char *fs_type)
Koushik K. Dutta3f995392010-03-30 23:29:43 -0700423{
Koushik Dutta2e454492011-01-01 18:06:17 -0800424 LOGI("Formatting unknown device.\n");
Koushik Duttaa8708c62011-01-01 18:00:27 -0800425
Koushik Duttafb3bd712011-06-16 14:13:02 -0700426 if (fs_type != NULL && 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];
Koushik Dutta50822992011-06-08 19:03:27 -0700469 if (strcmp(path, "/data") == 0) {
470 sprintf(tmp, "cd /data ; for f in $(ls -a | grep -v ^media$); do rm -rf $f; done");
471 __system(tmp);
472 }
473 else {
474 sprintf(tmp, "rm -rf %s/*", path);
475 __system(tmp);
476 sprintf(tmp, "rm -rf %s/.*", path);
477 __system(tmp);
478 }
Steve Kondik4123b582010-11-14 03:18:40 -0500479
Koushik Duttadf1e4062010-12-18 17:42:31 -0800480 ensure_path_unmounted(path);
Koushik K. Dutta3f995392010-03-30 23:29:43 -0700481 return 0;
482}
483
Koushik Duttadf1e4062010-12-18 17:42:31 -0800484//#define MOUNTABLE_COUNT 5
485//#define DEVICE_COUNT 4
486//#define MMC_COUNT 2
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700487
Kolja Dummann92796ec2011-02-13 20:59:30 +0100488typedef struct {
489 char mount[255];
490 char unmount[255];
491 Volume* v;
492} MountMenuEntry;
493
494typedef struct {
495 char txt[255];
496 Volume* v;
497} FormatMenuEntry;
498
Steve Kondik0d878512011-04-09 01:41:45 -0400499int is_safe_to_format(char* name)
500{
Brandon Bennett9ad8ba22011-05-07 23:42:58 -0600501 char str[255];
502 char* partition;
j_r0dd14d9e752011-06-09 18:37:28 -0400503 property_get("ro.cwm.forbid_format", str, "/misc,/radio,/bootloader,/recovery,/efs");
Brandon Bennett9ad8ba22011-05-07 23:42:58 -0600504
505 partition = strtok(str, ", ");
506 while (partition != NULL) {
507 if (strcmp(name, partition) == 0) {
508 return 0;
509 }
510 partition = strtok(NULL, ", ");
511 }
512
513 return 1;
Steve Kondik0d878512011-04-09 01:41:45 -0400514}
515
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700516void show_partition_menu()
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700517{
Koushik Duttaceddcd52010-08-23 16:13:14 -0700518 static char* headers[] = { "Mounts and Storage Menu",
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700519 "",
Steve Kondik4123b582010-11-14 03:18:40 -0500520 NULL
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700521 };
522
Kolja Dummann92796ec2011-02-13 20:59:30 +0100523 static MountMenuEntry* mount_menue = NULL;
524 static FormatMenuEntry* format_menue = NULL;
525
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700526 typedef char* string;
Steve Kondik4123b582010-11-14 03:18:40 -0500527
Kolja Dummann92796ec2011-02-13 20:59:30 +0100528 int i, mountable_volumes, formatable_volumes;
529 int num_volumes;
530 Volume* device_volumes;
Steve Kondik4123b582010-11-14 03:18:40 -0500531
Kolja Dummann92796ec2011-02-13 20:59:30 +0100532 num_volumes = get_num_volumes();
533 device_volumes = get_device_volumes();
534
535 string options[255];
536
537 if(!device_volumes)
538 return;
539
540 mountable_volumes = 0;
541 formatable_volumes = 0;
542
543 mount_menue = malloc(num_volumes * sizeof(MountMenuEntry));
544 format_menue = malloc(num_volumes * sizeof(FormatMenuEntry));
545
546 for (i = 0; i < num_volumes; ++i) {
547 Volume* v = &device_volumes[i];
Koushik Dutta67fa0c32011-03-17 11:37:21 -0700548 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 +0100549 {
550 sprintf(&mount_menue[mountable_volumes].mount, "mount %s", v->mount_point);
551 sprintf(&mount_menue[mountable_volumes].unmount, "unmount %s", v->mount_point);
552 mount_menue[mountable_volumes].v = &device_volumes[i];
553 ++mountable_volumes;
Steve Kondik0d878512011-04-09 01:41:45 -0400554 if (is_safe_to_format(v->mount_point)) {
555 sprintf(&format_menue[formatable_volumes].txt, "format %s", v->mount_point);
556 format_menue[formatable_volumes].v = &device_volumes[i];
557 ++formatable_volumes;
558 }
Kolja Dummann92796ec2011-02-13 20:59:30 +0100559 }
Steve Kondik0d878512011-04-09 01:41:45 -0400560 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 +0100561 {
562 sprintf(&format_menue[formatable_volumes].txt, "format %s", v->mount_point);
563 format_menue[formatable_volumes].v = &device_volumes[i];
564 ++formatable_volumes;
565 }
566 }
567
Koushik Duttadf1e4062010-12-18 17:42:31 -0800568
Koushik Duttad63eaef2010-07-14 21:01:21 -0700569 static char* confirm_format = "Confirm format?";
Koushik Duttaecd32fa2010-07-14 18:38:02 -0700570 static char* confirm = "Yes - Format";
wjbcea6eba2011-04-30 21:04:04 -0400571 char confirm_string[255];
Steve Kondik4123b582010-11-14 03:18:40 -0500572
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700573 for (;;)
574 {
Steve Kondik4123b582010-11-14 03:18:40 -0500575
Kolja Dummann92796ec2011-02-13 20:59:30 +0100576 for (i = 0; i < mountable_volumes; i++)
577 {
578 MountMenuEntry* e = &mount_menue[i];
579 Volume* v = e->v;
580 if(is_path_mounted(v->mount_point))
581 options[i] = e->unmount;
582 else
583 options[i] = e->mount;
584 }
Steve Kondik4123b582010-11-14 03:18:40 -0500585
Kolja Dummann92796ec2011-02-13 20:59:30 +0100586 for (i = 0; i < formatable_volumes; i++)
587 {
588 FormatMenuEntry* e = &format_menue[i];
Steve Kondik4123b582010-11-14 03:18:40 -0500589
Kolja Dummann92796ec2011-02-13 20:59:30 +0100590 options[mountable_volumes+i] = e->txt;
591 }
592
593 options[mountable_volumes+formatable_volumes] = "mount USB storage";
594 options[mountable_volumes+formatable_volumes + 1] = NULL;
595
596 int chosen_item = get_menu_selection(headers, &options, 0, 0);
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700597 if (chosen_item == GO_BACK)
598 break;
Kolja Dummann92796ec2011-02-13 20:59:30 +0100599 if (chosen_item == (mountable_volumes+formatable_volumes))
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700600 {
601 show_mount_usb_storage_menu();
602 }
Kolja Dummann92796ec2011-02-13 20:59:30 +0100603 else if (chosen_item < mountable_volumes)
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700604 {
Kolja Dummann92796ec2011-02-13 20:59:30 +0100605 MountMenuEntry* e = &mount_menue[chosen_item];
606 Volume* v = e->v;
607
608 if (is_path_mounted(v->mount_point))
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700609 {
Kolja Dummann92796ec2011-02-13 20:59:30 +0100610 if (0 != ensure_path_unmounted(v->mount_point))
611 ui_print("Error unmounting %s!\n", v->mount_point);
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700612 }
613 else
614 {
Kolja Dummann92796ec2011-02-13 20:59:30 +0100615 if (0 != ensure_path_mounted(v->mount_point))
616 ui_print("Error mounting %s!\n", v->mount_point);
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700617 }
618 }
Kolja Dummann92796ec2011-02-13 20:59:30 +0100619 else if (chosen_item < (mountable_volumes + formatable_volumes))
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700620 {
Kolja Dummann92796ec2011-02-13 20:59:30 +0100621 chosen_item = chosen_item - mountable_volumes;
622 FormatMenuEntry* e = &format_menue[chosen_item];
623 Volume* v = e->v;
624
wjbcea6eba2011-04-30 21:04:04 -0400625 sprintf(confirm_string, "%s - %s", v->mount_point, confirm_format);
626
627 if (!confirm_selection(confirm_string, confirm))
Koushik K. Dutta16f0b492010-03-19 14:37:11 -0700628 continue;
Kolja Dummann92796ec2011-02-13 20:59:30 +0100629 ui_print("Formatting %s...\n", v->mount_point);
630 if (0 != format_volume(v->mount_point))
631 ui_print("Error formatting %s!\n", v->mount_point);
Koushik K. Dutta3f995392010-03-30 23:29:43 -0700632 else
633 ui_print("Done.\n");
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700634 }
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700635 }
Kolja Dummann92796ec2011-02-13 20:59:30 +0100636
637 free(mount_menue);
638 free(format_menue);
639
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700640}
641
Koushik K. Dutta72a1db62010-03-07 14:10:26 -0800642#define EXTENDEDCOMMAND_SCRIPT "/cache/recovery/extendedcommand"
643
644int extendedcommand_file_exists()
Koushik K. Duttaf68aaaf2010-03-07 13:39:21 -0800645{
Koushik K. Duttaf68aaaf2010-03-07 13:39:21 -0800646 struct stat file_info;
Koushik K. Dutta72a1db62010-03-07 14:10:26 -0800647 return 0 == stat(EXTENDEDCOMMAND_SCRIPT, &file_info);
648}
649
Koushik Duttabec09952010-12-19 20:37:57 -0800650int edify_main(int argc, char** argv) {
651 load_volume_table();
652 process_volumes();
653 RegisterBuiltins();
654 RegisterRecoveryHooks();
655 FinishRegistration();
Koushik Duttadf1e4062010-12-18 17:42:31 -0800656
Koushik Duttabec09952010-12-19 20:37:57 -0800657 if (argc != 2) {
658 printf("edify <filename>\n");
Koushik K. Duttaf68aaaf2010-03-07 13:39:21 -0800659 return 1;
Koushik K. Duttaf68aaaf2010-03-07 13:39:21 -0800660 }
661
Koushik Duttabec09952010-12-19 20:37:57 -0800662 FILE* f = fopen(argv[1], "r");
663 if (f == NULL) {
664 printf("%s: %s: No such file or directory\n", argv[0], argv[1]);
665 return 1;
666 }
667 char buffer[8192];
668 int size = fread(buffer, 1, 8191, f);
669 fclose(f);
670 buffer[size] = '\0';
671
672 Expr* root;
673 int error_count = 0;
674 yy_scan_bytes(buffer, size);
675 int error = yyparse(&root, &error_count);
676 printf("parse returned %d; %d errors encountered\n", error, error_count);
677 if (error == 0 || error_count > 0) {
678
679 //ExprDump(0, root, buffer);
680
681 State state;
682 state.cookie = NULL;
683 state.script = buffer;
684 state.errmsg = NULL;
685
686 char* result = Evaluate(&state, root);
687 if (result == NULL) {
688 printf("result was NULL, message is: %s\n",
689 (state.errmsg == NULL ? "(NULL)" : state.errmsg));
690 free(state.errmsg);
691 } else {
692 printf("result is [%s]\n", result);
Koushik K. Duttaf68aaaf2010-03-07 13:39:21 -0800693 }
Steve Kondik4123b582010-11-14 03:18:40 -0500694 }
Koushik Duttabec09952010-12-19 20:37:57 -0800695 return 0;
Koushik K. Duttaf68aaaf2010-03-07 13:39:21 -0800696}
Koushik K. Dutta72a1db62010-03-07 14:10:26 -0800697
Koushik K. Dutta6771aca2010-04-03 23:28:39 -0700698int run_script(char* filename)
Koushik K. Dutta3836f722010-03-11 22:17:43 -0800699{
700 struct stat file_info;
701 if (0 != stat(filename, &file_info)) {
702 printf("Error executing stat on file: %s\n", filename);
703 return 1;
704 }
Steve Kondik4123b582010-11-14 03:18:40 -0500705
Koushik K. Dutta3836f722010-03-11 22:17:43 -0800706 int script_len = file_info.st_size;
Koushik K. Dutta707fa6d2010-03-23 11:44:33 -0700707 char* script_data = (char*)malloc(script_len + 1);
Koushik K. Dutta3836f722010-03-11 22:17:43 -0800708 FILE *file = fopen(filename, "rb");
709 fread(script_data, script_len, 1, file);
Koushik K. Dutta707fa6d2010-03-23 11:44:33 -0700710 // supposedly not necessary, but let's be safe.
711 script_data[script_len] = '\0';
Koushik K. Dutta3836f722010-03-11 22:17:43 -0800712 fclose(file);
Koushik Duttae25908b2010-06-21 08:16:19 -0700713 LOGI("Running script:\n");
714 LOGI("\n%s\n", script_data);
Koushik K. Dutta3836f722010-03-11 22:17:43 -0800715
Koushik K. Duttaf3534d02010-04-18 18:06:24 -0700716 int ret = run_script_from_buffer(script_data, script_len, filename);
Koushik K. Dutta1d53c4e2010-04-02 16:46:21 -0700717 free(script_data);
718 return ret;
Koushik K. Dutta3836f722010-03-11 22:17:43 -0800719}
720
Koushik K. Dutta72a1db62010-03-07 14:10:26 -0800721int run_and_remove_extendedcommand()
722{
Koushik K. Dutta6771aca2010-04-03 23:28:39 -0700723 char tmp[PATH_MAX];
724 sprintf(tmp, "cp %s /tmp/%s", EXTENDEDCOMMAND_SCRIPT, basename(EXTENDEDCOMMAND_SCRIPT));
725 __system(tmp);
726 remove(EXTENDEDCOMMAND_SCRIPT);
Koushik K. Dutta3a25cf52010-03-08 19:22:41 -0800727 int i = 0;
728 for (i = 20; i > 0; i--) {
729 ui_print("Waiting for SD Card to mount (%ds)\n", i);
Koushik Duttadf1e4062010-12-18 17:42:31 -0800730 if (ensure_path_mounted("/sdcard") == 0) {
Koushik K. Dutta3a25cf52010-03-08 19:22:41 -0800731 ui_print("SD Card mounted...\n");
732 break;
733 }
734 sleep(1);
735 }
Koushik Dutta92077c12010-07-15 00:10:08 -0700736 remove("/sdcard/clockworkmod/.recoverycheckpoint");
Koushik K. Dutta3a25cf52010-03-08 19:22:41 -0800737 if (i == 0) {
738 ui_print("Timed out waiting for SD card... continuing anyways.");
739 }
Steve Kondik4123b582010-11-14 03:18:40 -0500740
Koushik K. Dutta6771aca2010-04-03 23:28:39 -0700741 sprintf(tmp, "/tmp/%s", basename(EXTENDEDCOMMAND_SCRIPT));
742 return run_script(tmp);
Koushik K. Dutta72a1db62010-03-07 14:10:26 -0800743}
744
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700745void show_nandroid_advanced_restore_menu()
746{
Koushik Duttadf1e4062010-12-18 17:42:31 -0800747 if (ensure_path_mounted("/sdcard") != 0) {
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700748 LOGE ("Can't mount /sdcard\n");
749 return;
750 }
751
752 static char* advancedheaders[] = { "Choose an image to restore",
753 "",
754 "Choose an image to restore",
755 "first. The next menu will",
756 "you more options.",
757 "",
758 NULL
759 };
760
761 char* file = choose_file_menu("/sdcard/clockworkmod/backup/", NULL, advancedheaders);
762 if (file == NULL)
763 return;
764
765 static char* headers[] = { "Nandroid Advanced Restore",
766 "",
767 NULL
768 };
769
770 static char* list[] = { "Restore boot",
771 "Restore system",
772 "Restore data",
773 "Restore cache",
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700774 "Restore sd-ext",
agrabren1f76a5d2010-12-29 12:15:18 -0600775 "Restore wimax",
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700776 NULL
777 };
Koushik Dutta5b7f34a2010-12-29 23:36:03 -0800778
779 char tmp[PATH_MAX];
780 if (0 != get_partition_device("wimax", tmp)) {
781 // disable wimax restore option
782 list[5] = NULL;
783 }
Koushik Duttad63eaef2010-07-14 21:01:21 -0700784
785 static char* confirm_restore = "Confirm restore?";
786
Koushik Duttadf1e4062010-12-18 17:42:31 -0800787 int chosen_item = get_menu_selection(headers, list, 0, 0);
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700788 switch (chosen_item)
789 {
790 case 0:
Koushik Duttad63eaef2010-07-14 21:01:21 -0700791 if (confirm_selection(confirm_restore, "Yes - Restore boot"))
agrabren1f76a5d2010-12-29 12:15:18 -0600792 nandroid_restore(file, 1, 0, 0, 0, 0, 0);
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700793 break;
794 case 1:
Koushik Duttad63eaef2010-07-14 21:01:21 -0700795 if (confirm_selection(confirm_restore, "Yes - Restore system"))
agrabren1f76a5d2010-12-29 12:15:18 -0600796 nandroid_restore(file, 0, 1, 0, 0, 0, 0);
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700797 break;
798 case 2:
Koushik Duttad63eaef2010-07-14 21:01:21 -0700799 if (confirm_selection(confirm_restore, "Yes - Restore data"))
agrabren1f76a5d2010-12-29 12:15:18 -0600800 nandroid_restore(file, 0, 0, 1, 0, 0, 0);
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700801 break;
802 case 3:
Koushik Duttad63eaef2010-07-14 21:01:21 -0700803 if (confirm_selection(confirm_restore, "Yes - Restore cache"))
agrabren1f76a5d2010-12-29 12:15:18 -0600804 nandroid_restore(file, 0, 0, 0, 1, 0, 0);
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700805 break;
Koushik Dutta2f73e582010-04-18 16:00:21 -0700806 case 4:
Koushik Duttad63eaef2010-07-14 21:01:21 -0700807 if (confirm_selection(confirm_restore, "Yes - Restore sd-ext"))
agrabren1f76a5d2010-12-29 12:15:18 -0600808 nandroid_restore(file, 0, 0, 0, 0, 1, 0);
809 break;
810 case 5:
811 if (confirm_selection(confirm_restore, "Yes - Restore wimax"))
812 nandroid_restore(file, 0, 0, 0, 0, 0, 1);
Koushik Dutta2f73e582010-04-18 16:00:21 -0700813 break;
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700814 }
815}
816
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700817void show_nandroid_menu()
818{
819 static char* headers[] = { "Nandroid",
820 "",
Steve Kondik4123b582010-11-14 03:18:40 -0500821 NULL
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700822 };
823
Steve Kondik4123b582010-11-14 03:18:40 -0500824 static char* list[] = { "Backup",
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700825 "Restore",
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700826 "Advanced Restore",
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700827 NULL
828 };
829
Koushik Duttadf1e4062010-12-18 17:42:31 -0800830 int chosen_item = get_menu_selection(headers, list, 0, 0);
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700831 switch (chosen_item)
832 {
833 case 0:
834 {
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700835 char backup_path[PATH_MAX];
Koushik K. Dutta6a26e7c2010-03-27 15:26:11 -0700836 time_t t = time(NULL);
837 struct tm *tmp = localtime(&t);
838 if (tmp == NULL)
839 {
840 struct timeval tp;
841 gettimeofday(&tp, NULL);
842 sprintf(backup_path, "/sdcard/clockworkmod/backup/%d", tp.tv_sec);
843 }
844 else
845 {
846 strftime(backup_path, sizeof(backup_path), "/sdcard/clockworkmod/backup/%F.%H.%M.%S", tmp);
847 }
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700848 nandroid_backup(backup_path);
849 }
850 break;
851 case 1:
852 show_nandroid_restore_menu();
853 break;
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700854 case 2:
855 show_nandroid_advanced_restore_menu();
856 break;
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700857 }
Koushik K. Duttaa496b512010-03-19 14:51:45 -0700858}
859
Koushik Duttafd1579b2010-05-01 12:46:55 -0700860void wipe_battery_stats()
861{
Koushik Duttadf1e4062010-12-18 17:42:31 -0800862 ensure_path_mounted("/data");
Koushik Duttafd1579b2010-05-01 12:46:55 -0700863 remove("/data/system/batterystats.bin");
Koushik Duttadf1e4062010-12-18 17:42:31 -0800864 ensure_path_unmounted("/data");
Koushik Duttafd1579b2010-05-01 12:46:55 -0700865}
866
Koushik K. Duttaa496b512010-03-19 14:51:45 -0700867void show_advanced_menu()
868{
869 static char* headers[] = { "Advanced and Debugging Menu",
870 "",
871 NULL
872 };
873
Koushik K. Dutta7fe4d7b2010-04-06 23:04:52 -0700874 static char* list[] = { "Reboot Recovery",
Koushik Dutta49af23c2010-06-21 13:45:51 -0700875 "Wipe Dalvik Cache",
Koushik Duttafd1579b2010-05-01 12:46:55 -0700876 "Wipe Battery Stats",
Koushik Dutta598cfc72010-06-20 09:42:47 -0700877 "Report Error",
Koushik K. Dutta7fe4d7b2010-04-06 23:04:52 -0700878 "Key Test",
Tanguy Pruvot67d358c2011-06-10 20:26:44 +0200879 "Show log",
Koushik Duttae17a78d2010-08-29 12:35:10 -0700880#ifndef BOARD_HAS_SMALL_RECOVERY
Koushik Duttaceddcd52010-08-23 16:13:14 -0700881 "Partition SD Card",
882 "Fix Permissions",
Brint E. Kriebeld3522332010-12-11 20:52:06 -0700883#ifdef BOARD_HAS_SDCARD_INTERNAL
884 "Partition Internal SD Card",
885#endif
Koushik Duttae17a78d2010-08-29 12:35:10 -0700886#endif
Koushik K. Duttaa496b512010-03-19 14:51:45 -0700887 NULL
888 };
889
890 for (;;)
891 {
Koushik Duttadf1e4062010-12-18 17:42:31 -0800892 int chosen_item = get_menu_selection(headers, list, 0, 0);
Koushik K. Duttaa496b512010-03-19 14:51:45 -0700893 if (chosen_item == GO_BACK)
894 break;
895 switch (chosen_item)
896 {
897 case 0:
Tanguy Pruvot67d358c2011-06-10 20:26:44 +0200898 {
Austen Dicken7126ef42011-06-14 15:47:40 -0500899 reboot_wrapper("recovery");
Koushik K. Dutta7fe4d7b2010-04-06 23:04:52 -0700900 break;
Tanguy Pruvot67d358c2011-06-10 20:26:44 +0200901 }
Koushik K. Dutta7fe4d7b2010-04-06 23:04:52 -0700902 case 1:
Koushik Duttad63eaef2010-07-14 21:01:21 -0700903 {
Koushik Duttadf1e4062010-12-18 17:42:31 -0800904 if (0 != ensure_path_mounted("/data"))
Koushik Dutta49af23c2010-06-21 13:45:51 -0700905 break;
Koushik Duttadf1e4062010-12-18 17:42:31 -0800906 ensure_path_mounted("/sd-ext");
907 ensure_path_mounted("/cache");
Koushik Dutta6c7745d2010-08-07 12:17:13 -0700908 if (confirm_selection( "Confirm wipe?", "Yes - Wipe Dalvik Cache")) {
Koushik Duttad63eaef2010-07-14 21:01:21 -0700909 __system("rm -r /data/dalvik-cache");
Koushik Dutta6c7745d2010-08-07 12:17:13 -0700910 __system("rm -r /cache/dalvik-cache");
911 __system("rm -r /sd-ext/dalvik-cache");
912 }
Koushik Duttadf1e4062010-12-18 17:42:31 -0800913 ensure_path_unmounted("/data");
Koushik Dutta6c7745d2010-08-07 12:17:13 -0700914 ui_print("Dalvik Cache wiped.\n");
Koushik Duttafd1579b2010-05-01 12:46:55 -0700915 break;
Koushik Duttad63eaef2010-07-14 21:01:21 -0700916 }
Koushik Duttafd1579b2010-05-01 12:46:55 -0700917 case 2:
Koushik Duttad63eaef2010-07-14 21:01:21 -0700918 {
919 if (confirm_selection( "Confirm wipe?", "Yes - Wipe Battery Stats"))
920 wipe_battery_stats();
Koushik Dutta598cfc72010-06-20 09:42:47 -0700921 break;
Koushik Duttad63eaef2010-07-14 21:01:21 -0700922 }
Koushik Dutta598cfc72010-06-20 09:42:47 -0700923 case 3:
Koushik Dutta49af23c2010-06-21 13:45:51 -0700924 handle_failure(1);
925 break;
926 case 4:
Koushik K. Duttaa496b512010-03-19 14:51:45 -0700927 {
928 ui_print("Outputting key codes.\n");
929 ui_print("Go back to end debugging.\n");
930 int key;
931 int action;
932 do
933 {
934 key = ui_wait_key();
935 action = device_handle_key(key, 1);
936 ui_print("Key: %d\n", key);
937 }
938 while (action != GO_BACK);
Koushik Dutta56606a22010-08-23 16:15:33 -0700939 break;
Koushik K. Duttaa496b512010-03-19 14:51:45 -0700940 }
Koushik Duttaf0e31b82010-08-17 16:55:38 -0700941 case 5:
942 {
Tanguy Pruvot67d358c2011-06-10 20:26:44 +0200943 ui_printlogtail(12);
944 break;
945 }
946 case 6:
947 {
Koushik Duttaceddcd52010-08-23 16:13:14 -0700948 static char* ext_sizes[] = { "128M",
949 "256M",
950 "512M",
951 "1024M",
Brint E. Kriebel0b233312010-11-14 15:31:17 -0700952 "2048M",
953 "4096M",
Koushik Duttaceddcd52010-08-23 16:13:14 -0700954 NULL };
955
956 static char* swap_sizes[] = { "0M",
957 "32M",
958 "64M",
959 "128M",
960 "256M",
961 NULL };
962
963 static char* ext_headers[] = { "Ext Size", "", NULL };
964 static char* swap_headers[] = { "Swap Size", "", NULL };
965
Koushik Duttadf1e4062010-12-18 17:42:31 -0800966 int ext_size = get_menu_selection(ext_headers, ext_sizes, 0, 0);
Koushik Duttaceddcd52010-08-23 16:13:14 -0700967 if (ext_size == GO_BACK)
968 continue;
Steve Kondik4123b582010-11-14 03:18:40 -0500969
Koushik Duttadf1e4062010-12-18 17:42:31 -0800970 int swap_size = get_menu_selection(swap_headers, swap_sizes, 0, 0);
Koushik Duttaceddcd52010-08-23 16:13:14 -0700971 if (swap_size == GO_BACK)
972 continue;
973
974 char sddevice[256];
Koushik Duttadf1e4062010-12-18 17:42:31 -0800975 Volume *vol = volume_for_path("/sdcard");
976 strcpy(sddevice, vol->device);
Koushik Duttaceddcd52010-08-23 16:13:14 -0700977 // we only want the mmcblk, not the partition
978 sddevice[strlen("/dev/block/mmcblkX")] = NULL;
979 char cmd[PATH_MAX];
980 setenv("SDPATH", sddevice, 1);
981 sprintf(cmd, "sdparted -es %s -ss %s -efs ext3 -s", ext_sizes[ext_size], swap_sizes[swap_size]);
982 ui_print("Partitioning SD Card... please wait...\n");
983 if (0 == __system(cmd))
984 ui_print("Done!\n");
985 else
986 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 -0700987 break;
Koushik Duttaf0e31b82010-08-17 16:55:38 -0700988 }
Tanguy Pruvot67d358c2011-06-10 20:26:44 +0200989 case 7:
Koushik Dutta38e8b2b2010-08-17 22:21:53 -0700990 {
Koushik Duttadf1e4062010-12-18 17:42:31 -0800991 ensure_path_mounted("/system");
992 ensure_path_mounted("/data");
Koushik Duttaceddcd52010-08-23 16:13:14 -0700993 ui_print("Fixing permissions...\n");
994 __system("fix_permissions");
995 ui_print("Done!\n");
Koushik Dutta56606a22010-08-23 16:15:33 -0700996 break;
Koushik Dutta38e8b2b2010-08-17 22:21:53 -0700997 }
Tanguy Pruvot67d358c2011-06-10 20:26:44 +0200998 case 8:
Brint E. Kriebeld3522332010-12-11 20:52:06 -0700999 {
1000 static char* ext_sizes[] = { "128M",
1001 "256M",
1002 "512M",
1003 "1024M",
1004 "2048M",
1005 "4096M",
1006 NULL };
1007
1008 static char* swap_sizes[] = { "0M",
1009 "32M",
1010 "64M",
1011 "128M",
1012 "256M",
1013 NULL };
1014
1015 static char* ext_headers[] = { "Data Size", "", NULL };
1016 static char* swap_headers[] = { "Swap Size", "", NULL };
1017
Koushik Duttadf1e4062010-12-18 17:42:31 -08001018 int ext_size = get_menu_selection(ext_headers, ext_sizes, 0, 0);
Brint E. Kriebeld3522332010-12-11 20:52:06 -07001019 if (ext_size == GO_BACK)
1020 continue;
1021
1022 int swap_size = 0;
1023 if (swap_size == GO_BACK)
1024 continue;
1025
1026 char sddevice[256];
Koushik Duttadf1e4062010-12-18 17:42:31 -08001027 Volume *vol = volume_for_path("/emmc");
1028 strcpy(sddevice, vol->device);
Brint E. Kriebeld3522332010-12-11 20:52:06 -07001029 // we only want the mmcblk, not the partition
1030 sddevice[strlen("/dev/block/mmcblkX")] = NULL;
1031 char cmd[PATH_MAX];
1032 setenv("SDPATH", sddevice, 1);
1033 sprintf(cmd, "sdparted -es %s -ss %s -efs ext3 -s", ext_sizes[ext_size], swap_sizes[swap_size]);
1034 ui_print("Partitioning Internal SD Card... please wait...\n");
1035 if (0 == __system(cmd))
1036 ui_print("Done!\n");
1037 else
1038 ui_print("An error occured while partitioning your Internal SD Card. Please see /tmp/recovery.log for more details.\n");
1039 break;
1040 }
Koushik K. Duttaa496b512010-03-19 14:51:45 -07001041 }
1042 }
Koushik Dutta14239d22010-06-14 15:02:48 -07001043}
1044
Koushik Duttab643e4f2010-12-18 18:39:39 -08001045void write_fstab_root(char *path, FILE *file)
Koushik Dutta14239d22010-06-14 15:02:48 -07001046{
Koushik Duttab643e4f2010-12-18 18:39:39 -08001047 Volume *vol = volume_for_path(path);
1048 if (vol == NULL) {
Koushik Dutta9765a032010-12-18 21:31:02 -08001049 LOGW("Unable to get recovery.fstab info for %s during fstab generation!\n", path);
Koushik Dutta598cfc72010-06-20 09:42:47 -07001050 return;
Koushik Duttaa6522b32010-06-20 13:16:06 -07001051 }
Steve Kondik4123b582010-11-14 03:18:40 -05001052
Koushik Dutta31b74112010-12-20 08:49:20 -08001053 char device[200];
1054 if (vol->device[0] != '/')
1055 get_partition_device(vol->device, device);
1056 else
1057 strcpy(device, vol->device);
1058
1059 fprintf(file, "%s ", device);
Koushik Duttab643e4f2010-12-18 18:39:39 -08001060 fprintf(file, "%s ", path);
Koushik Duttaab1f8b82011-02-27 17:28:30 -08001061 // special case rfs cause auto will mount it as vfat on samsung.
1062 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 -07001063}
1064
1065void create_fstab()
1066{
Koushik Duttab643e4f2010-12-18 18:39:39 -08001067 struct stat info;
Koushik Duttacd44ab92010-06-23 00:02:14 -07001068 __system("touch /etc/mtab");
Koushik Dutta14239d22010-06-14 15:02:48 -07001069 FILE *file = fopen("/etc/fstab", "w");
Koushik Duttaa6522b32010-06-20 13:16:06 -07001070 if (file == NULL) {
Koushik Duttab643e4f2010-12-18 18:39:39 -08001071 LOGW("Unable to create /etc/fstab!\n");
Koushik Dutta598cfc72010-06-20 09:42:47 -07001072 return;
Koushik Duttaa6522b32010-06-20 13:16:06 -07001073 }
Brandon Bennett6d0604b2011-01-28 13:39:10 -07001074 Volume *vol = volume_for_path("/boot");
Koushik Duttaa9f0e7f2011-02-12 10:21:11 -08001075 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 -07001076 write_fstab_root("/boot", file);
Koushik Duttab643e4f2010-12-18 18:39:39 -08001077 write_fstab_root("/cache", file);
1078 write_fstab_root("/data", file);
Koushik Dutta5460f0c2010-12-18 22:37:49 -08001079 if (has_datadata()) {
1080 write_fstab_root("/datadata", file);
1081 }
Koushik Duttab643e4f2010-12-18 18:39:39 -08001082 write_fstab_root("/system", file);
1083 write_fstab_root("/sdcard", file);
1084 write_fstab_root("/sd-ext", file);
Koushik Dutta14239d22010-06-14 15:02:48 -07001085 fclose(file);
Koushik Dutta9765a032010-12-18 21:31:02 -08001086 LOGI("Completed outputting fstab.\n");
Koushik Dutta598cfc72010-06-20 09:42:47 -07001087}
1088
Koushik Dutta4196e0f2010-12-19 03:38:29 -08001089int bml_check_volume(const char *path) {
1090 ui_print("Checking %s...\n", path);
1091 ensure_path_unmounted(path);
1092 if (0 == ensure_path_mounted(path)) {
1093 ensure_path_unmounted(path);
1094 return 0;
1095 }
1096
1097 Volume *vol = volume_for_path(path);
1098 if (vol == NULL) {
1099 LOGE("Unable process volume! Skipping...\n");
1100 return 0;
1101 }
1102
Koushik Duttabec09952010-12-19 20:37:57 -08001103 ui_print("%s may be rfs. Checking...\n", path);
Koushik Dutta4196e0f2010-12-19 03:38:29 -08001104 char tmp[PATH_MAX];
1105 sprintf(tmp, "mount -t rfs %s %s", vol->device, path);
1106 int ret = __system(tmp);
1107 printf("%d\n", ret);
1108 return ret == 0 ? 1 : 0;
1109}
1110
1111void process_volumes() {
1112 create_fstab();
Koushik Dutta38a92142011-06-10 09:45:52 -07001113
1114 if (is_data_media()) {
1115 setup_data_media();
Koushik Dutta94caefe2011-06-10 09:17:30 -07001116 }
1117
Koushik Duttae8bc2c82011-02-27 14:00:19 -08001118 return;
1119
1120 // dead code.
Koushik Dutta4196e0f2010-12-19 03:38:29 -08001121 if (device_flash_type() != BML)
1122 return;
1123
1124 ui_print("Checking for ext4 partitions...\n");
1125 int ret = 0;
1126 ret = bml_check_volume("/system");
1127 ret |= bml_check_volume("/data");
1128 if (has_datadata())
1129 ret |= bml_check_volume("/datadata");
1130 ret |= bml_check_volume("/cache");
1131
1132 if (ret == 0) {
1133 ui_print("Done!\n");
1134 return;
1135 }
1136
Koushik Dutta745b5ff2010-12-21 00:58:43 -08001137 char backup_path[PATH_MAX];
1138 time_t t = time(NULL);
1139 char backup_name[PATH_MAX];
1140 struct timeval tp;
1141 gettimeofday(&tp, NULL);
1142 sprintf(backup_name, "before-ext4-convert-%d", tp.tv_sec);
1143 sprintf(backup_path, "/sdcard/clockworkmod/backup/%s", backup_name);
1144
Koushik Dutta4196e0f2010-12-19 03:38:29 -08001145 ui_set_show_text(1);
1146 ui_print("Filesystems need to be converted to ext4.\n");
1147 ui_print("A backup and restore will now take place.\n");
Koushik Duttabec09952010-12-19 20:37:57 -08001148 ui_print("If anything goes wrong, your backup will be\n");
Koushik Dutta745b5ff2010-12-21 00:58:43 -08001149 ui_print("named %s. Try restoring it\n", backup_name);
Koushik Duttabec09952010-12-19 20:37:57 -08001150 ui_print("in case of error.\n");
Koushik Dutta4196e0f2010-12-19 03:38:29 -08001151
Koushik Dutta745b5ff2010-12-21 00:58:43 -08001152 nandroid_backup(backup_path);
Koushik Dutta5b7f34a2010-12-29 23:36:03 -08001153 nandroid_restore(backup_path, 1, 1, 1, 1, 1, 0);
Koushik Dutta4196e0f2010-12-19 03:38:29 -08001154 ui_set_show_text(0);
1155}
1156
Koushik Dutta598cfc72010-06-20 09:42:47 -07001157void handle_failure(int ret)
1158{
1159 if (ret == 0)
1160 return;
Koushik Duttadf1e4062010-12-18 17:42:31 -08001161 if (0 != ensure_path_mounted("/sdcard"))
Koushik Dutta598cfc72010-06-20 09:42:47 -07001162 return;
Koushik Duttaa6522b32010-06-20 13:16:06 -07001163 mkdir("/sdcard/clockworkmod", S_IRWXU);
1164 __system("cp /tmp/recovery.log /sdcard/clockworkmod/recovery.log");
Koushik Dutta38e8b2b2010-08-17 22:21:53 -07001165 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 -05001166}
Koushik Duttadf1e4062010-12-18 17:42:31 -08001167
Koushik Duttadf1e4062010-12-18 17:42:31 -08001168int is_path_mounted(const char* path) {
1169 Volume* v = volume_for_path(path);
1170 if (v == NULL) {
1171 return 0;
1172 }
1173 if (strcmp(v->fs_type, "ramdisk") == 0) {
1174 // the ramdisk is always mounted.
1175 return 1;
1176 }
1177
1178 int result;
1179 result = scan_mounted_volumes();
1180 if (result < 0) {
1181 LOGE("failed to scan mounted volumes\n");
1182 return 0;
1183 }
1184
1185 const MountedVolume* mv =
1186 find_mounted_volume_by_mount_point(v->mount_point);
1187 if (mv) {
1188 // volume is already mounted
1189 return 1;
1190 }
1191 return 0;
1192}
Koushik Dutta5460f0c2010-12-18 22:37:49 -08001193
1194int has_datadata() {
1195 Volume *vol = volume_for_path("/datadata");
1196 return vol != NULL;
Koushik Duttad8e21c32011-01-04 10:46:23 -08001197}
1198
1199int volume_main(int argc, char **argv) {
1200 load_volume_table();
1201 return 0;
1202}