blob: 9b4c5e96d75230c0e6dff78a51d482a247f9cefe [file] [log] [blame]
Koushik K. Dutta6060e5c2010-02-11 22:27:06 -08001#include <ctype.h>
2#include <errno.h>
3#include <fcntl.h>
4#include <getopt.h>
5#include <limits.h>
6#include <linux/input.h>
7#include <stdio.h>
8#include <stdlib.h>
9#include <string.h>
10#include <sys/reboot.h>
Austen Dicken7126ef42011-06-14 15:47:40 -050011#include <reboot/reboot.h>
Koushik K. Dutta6060e5c2010-02-11 22:27:06 -080012#include <sys/types.h>
13#include <time.h>
14#include <unistd.h>
15
Koushik K. Duttae9234872010-02-12 00:43:24 -080016#include <sys/wait.h>
17#include <sys/limits.h>
18#include <dirent.h>
Koushik K. Dutta49f56892010-02-25 14:51:05 -080019#include <sys/stat.h>
Koushik K. Duttae9234872010-02-12 00:43:24 -080020
Koushik K. Dutta33370db2010-02-25 11:39:07 -080021#include <signal.h>
22#include <sys/wait.h>
23
Koushik K. Dutta6060e5c2010-02-11 22:27:06 -080024#include "bootloader.h"
25#include "common.h"
26#include "cutils/properties.h"
27#include "firmware.h"
28#include "install.h"
29#include "minui/minui.h"
30#include "minzip/DirUtil.h"
31#include "roots.h"
32#include "recovery_ui.h"
33
Koushik K. Duttaa85d7cc2010-03-12 17:00:58 -080034#include "../../external/yaffs2/yaffs2/utils/mkyaffs2image.h"
Koushik K. Dutta54305a82010-03-12 17:43:26 -080035#include "../../external/yaffs2/yaffs2/utils/unyaffs.h"
Koushik K. Duttaa85d7cc2010-03-12 17:00:58 -080036
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -080037#include "extendedcommands.h"
38#include "nandroid.h"
Koushik Duttadf1e4062010-12-18 17:42:31 -080039#include "mounts.h"
Koushik Dutta9765a032010-12-18 21:31:02 -080040#include "flashutils/flashutils.h"
Koushik Duttabec09952010-12-19 20:37:57 -080041#include "edify/expr.h"
Koushik Dutta7adeadc2011-05-26 11:47:56 -070042#include <libgen.h>
Koushik Dutta30a937a2011-09-05 21:14:06 -070043#include "mtdutils/mtdutils.h"
Koushik Dutta7adeadc2011-05-26 11:47:56 -070044
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -080045
Koushik K. Dutta6060e5c2010-02-11 22:27:06 -080046int signature_check_enabled = 1;
47int script_assert_enabled = 1;
Koushik Duttadf1e4062010-12-18 17:42:31 -080048static const char *SDCARD_UPDATE_FILE = "/sdcard/update.zip";
Koushik K. Dutta6060e5c2010-02-11 22:27:06 -080049
50void
51toggle_signature_check()
52{
53 signature_check_enabled = !signature_check_enabled;
54 ui_print("Signature Check: %s\n", signature_check_enabled ? "Enabled" : "Disabled");
55}
56
57void toggle_script_asserts()
58{
59 script_assert_enabled = !script_assert_enabled;
Koushik K. Duttae9234872010-02-12 00:43:24 -080060 ui_print("Script Asserts: %s\n", script_assert_enabled ? "Enabled" : "Disabled");
61}
62
Koushik K. Duttaea46fe22010-03-08 02:58:04 -080063int install_zip(const char* packagefilepath)
Koushik K. Duttae9234872010-02-12 00:43:24 -080064{
Koushik K. Duttabcdd0032010-02-21 21:10:25 -080065 ui_print("\n-- Installing: %s\n", packagefilepath);
Koushik Dutta9765a032010-12-18 21:31:02 -080066 if (device_flash_type() == MTD) {
67 set_sdcard_update_bootloader_message();
68 }
Koushik K. Duttabcdd0032010-02-21 21:10:25 -080069 int status = install_package(packagefilepath);
Koushik K. Duttaf3534d02010-04-18 18:06:24 -070070 ui_reset_progress();
Koushik K. Duttabcdd0032010-02-21 21:10:25 -080071 if (status != INSTALL_SUCCESS) {
72 ui_set_background(BACKGROUND_ICON_ERROR);
73 ui_print("Installation aborted.\n");
Koushik K. Duttaea46fe22010-03-08 02:58:04 -080074 return 1;
Steve Kondik4123b582010-11-14 03:18:40 -050075 }
Koushik K. Dutta001c5b52010-02-25 14:53:57 -080076 ui_set_background(BACKGROUND_ICON_NONE);
Koushik K. Dutta99fb6fe2010-03-03 00:42:58 -080077 ui_print("\nInstall from sdcard complete.\n");
Koushik K. Duttaea46fe22010-03-08 02:58:04 -080078 return 0;
Koushik K. Duttabcdd0032010-02-21 21:10:25 -080079}
80
Tanguy Pruvotaaad77f2011-06-18 10:26:32 +020081char* INSTALL_MENU_ITEMS[] = { "choose zip from sdcard",
82 "apply /sdcard/update.zip",
Koushik K. Duttabcdd0032010-02-21 21:10:25 -080083 "toggle signature verification",
84 "toggle script asserts",
Koushik Dutta30a937a2011-09-05 21:14:06 -070085 "choose zip from internal sdcard",
Koushik K. Duttabcdd0032010-02-21 21:10:25 -080086 NULL };
Tanguy Pruvotaaad77f2011-06-18 10:26:32 +020087#define ITEM_CHOOSE_ZIP 0
88#define ITEM_APPLY_SDCARD 1
Koushik K. Duttabcdd0032010-02-21 21:10:25 -080089#define ITEM_SIG_CHECK 2
90#define ITEM_ASSERTS 3
Koushik Dutta30a937a2011-09-05 21:14:06 -070091#define ITEM_CHOOSE_ZIP_INT 4
Koushik K. Duttabcdd0032010-02-21 21:10:25 -080092
93void show_install_update_menu()
94{
95 static char* headers[] = { "Apply update from .zip file on SD card",
96 "",
Steve Kondik4123b582010-11-14 03:18:40 -050097 NULL
Koushik K. Duttabcdd0032010-02-21 21:10:25 -080098 };
Koushik Dutta30a937a2011-09-05 21:14:06 -070099
100 if (volume_for_path("/emmc") == NULL)
101 INSTALL_MENU_ITEMS[ITEM_CHOOSE_ZIP_INT] = NULL;
102
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800103 for (;;)
104 {
Koushik Duttadf1e4062010-12-18 17:42:31 -0800105 int chosen_item = get_menu_selection(headers, INSTALL_MENU_ITEMS, 0, 0);
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800106 switch (chosen_item)
107 {
108 case ITEM_ASSERTS:
109 toggle_script_asserts();
110 break;
111 case ITEM_SIG_CHECK:
112 toggle_signature_check();
113 break;
114 case ITEM_APPLY_SDCARD:
Koushik Duttad63eaef2010-07-14 21:01:21 -0700115 {
116 if (confirm_selection("Confirm install?", "Yes - Install /sdcard/update.zip"))
Koushik Duttadf1e4062010-12-18 17:42:31 -0800117 install_zip(SDCARD_UPDATE_FILE);
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800118 break;
Koushik Duttad63eaef2010-07-14 21:01:21 -0700119 }
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800120 case ITEM_CHOOSE_ZIP:
Koushik Dutta30a937a2011-09-05 21:14:06 -0700121 show_choose_zip_menu("/sdcard/");
122 break;
123 case ITEM_CHOOSE_ZIP_INT:
124 show_choose_zip_menu("/emmc/");
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800125 break;
126 default:
127 return;
128 }
Steve Kondik4123b582010-11-14 03:18:40 -0500129
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800130 }
131}
132
Koushik K. Dutta1d53c4e2010-04-02 16:46:21 -0700133void free_string_array(char** array)
134{
135 if (array == NULL)
136 return;
137 char* cursor = array[0];
138 int i = 0;
139 while (cursor != NULL)
140 {
141 free(cursor);
142 cursor = array[++i];
143 }
144 free(array);
145}
146
Koushik K. Dutta49f56892010-02-25 14:51:05 -0800147char** gather_files(const char* directory, const char* fileExtensionOrDirectory, int* numFiles)
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800148{
Koushik K. Duttae9234872010-02-12 00:43:24 -0800149 char path[PATH_MAX] = "";
150 DIR *dir;
151 struct dirent *de;
152 int total = 0;
153 int i;
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800154 char** files = NULL;
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800155 int pass;
156 *numFiles = 0;
157 int dirLen = strlen(directory);
Koushik K. Duttae9234872010-02-12 00:43:24 -0800158
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800159 dir = opendir(directory);
160 if (dir == NULL) {
161 ui_print("Couldn't open directory.\n");
162 return NULL;
163 }
Steve Kondik4123b582010-11-14 03:18:40 -0500164
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800165 int extension_length = 0;
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800166 if (fileExtensionOrDirectory != NULL)
167 extension_length = strlen(fileExtensionOrDirectory);
Steve Kondik4123b582010-11-14 03:18:40 -0500168
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800169 int isCounting = 1;
170 i = 0;
171 for (pass = 0; pass < 2; pass++) {
172 while ((de=readdir(dir)) != NULL) {
173 // skip hidden files
174 if (de->d_name[0] == '.')
175 continue;
Steve Kondik4123b582010-11-14 03:18:40 -0500176
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800177 // NULL means that we are gathering directories, so skip this
178 if (fileExtensionOrDirectory != NULL)
179 {
180 // make sure that we can have the desired extension (prevent seg fault)
181 if (strlen(de->d_name) < extension_length)
182 continue;
183 // compare the extension
184 if (strcmp(de->d_name + strlen(de->d_name) - extension_length, fileExtensionOrDirectory) != 0)
185 continue;
186 }
187 else
188 {
189 struct stat info;
Koushik K. Dutta1d53c4e2010-04-02 16:46:21 -0700190 char fullFileName[PATH_MAX];
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800191 strcpy(fullFileName, directory);
192 strcat(fullFileName, de->d_name);
193 stat(fullFileName, &info);
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800194 // make sure it is a directory
195 if (!(S_ISDIR(info.st_mode)))
196 continue;
197 }
Steve Kondik4123b582010-11-14 03:18:40 -0500198
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800199 if (pass == 0)
200 {
201 total++;
202 continue;
203 }
Steve Kondik4123b582010-11-14 03:18:40 -0500204
Koushik K. Dutta49f56892010-02-25 14:51:05 -0800205 files[i] = (char*) malloc(dirLen + strlen(de->d_name) + 2);
206 strcpy(files[i], directory);
207 strcat(files[i], de->d_name);
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800208 if (fileExtensionOrDirectory == NULL)
209 strcat(files[i], "/");
210 i++;
211 }
212 if (pass == 1)
213 break;
214 if (total == 0)
215 break;
216 rewinddir(dir);
217 *numFiles = total;
218 files = (char**) malloc((total+1)*sizeof(char*));
219 files[total]=NULL;
220 }
Koushik K. Dutta49f56892010-02-25 14:51:05 -0800221
222 if(closedir(dir) < 0) {
223 LOGE("Failed to close directory.");
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800224 }
225
226 if (total==0) {
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800227 return NULL;
228 }
229
agrabren1f76a5d2010-12-29 12:15:18 -0600230 // sort the result
231 if (files != NULL) {
232 for (i = 0; i < total; i++) {
233 int curMax = -1;
234 int j;
235 for (j = 0; j < total - i; j++) {
236 if (curMax == -1 || strcmp(files[curMax], files[j]) < 0)
237 curMax = j;
238 }
239 char* temp = files[curMax];
240 files[curMax] = files[total - i - 1];
241 files[total - i - 1] = temp;
242 }
243 }
Koushik Dutta1e8aaba2010-07-01 00:23:25 -0700244
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800245 return files;
Koushik K. Dutta49f56892010-02-25 14:51:05 -0800246}
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800247
Koushik K. Dutta49f56892010-02-25 14:51:05 -0800248// pass in NULL for fileExtensionOrDirectory and you will get a directory chooser
249char* choose_file_menu(const char* directory, const char* fileExtensionOrDirectory, const char* headers[])
250{
251 char path[PATH_MAX] = "";
252 DIR *dir;
253 struct dirent *de;
254 int numFiles = 0;
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800255 int numDirs = 0;
Koushik K. Dutta49f56892010-02-25 14:51:05 -0800256 int i;
Koushik K. Dutta1d53c4e2010-04-02 16:46:21 -0700257 char* return_value = NULL;
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800258 int dir_len = strlen(directory);
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800259
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800260 char** files = gather_files(directory, fileExtensionOrDirectory, &numFiles);
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800261 char** dirs = NULL;
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800262 if (fileExtensionOrDirectory != NULL)
263 dirs = gather_files(directory, NULL, &numDirs);
264 int total = numDirs + numFiles;
265 if (total == 0)
266 {
267 ui_print("No files found.\n");
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800268 }
Koushik K. Dutta1d53c4e2010-04-02 16:46:21 -0700269 else
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800270 {
Koushik K. Dutta1d53c4e2010-04-02 16:46:21 -0700271 char** list = (char**) malloc((total + 1) * sizeof(char*));
272 list[total] = NULL;
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800273
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800274
Koushik K. Dutta1d53c4e2010-04-02 16:46:21 -0700275 for (i = 0 ; i < numDirs; i++)
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800276 {
Koushik K. Dutta1d53c4e2010-04-02 16:46:21 -0700277 list[i] = strdup(dirs[i] + dir_len);
278 }
279
280 for (i = 0 ; i < numFiles; i++)
281 {
282 list[numDirs + i] = strdup(files[i] + dir_len);
283 }
284
285 for (;;)
286 {
Koushik Duttadf1e4062010-12-18 17:42:31 -0800287 int chosen_item = get_menu_selection(headers, list, 0, 0);
Koushik K. Dutta1d53c4e2010-04-02 16:46:21 -0700288 if (chosen_item == GO_BACK)
289 break;
290 static char ret[PATH_MAX];
291 if (chosen_item < numDirs)
292 {
293 char* subret = choose_file_menu(dirs[chosen_item], fileExtensionOrDirectory, headers);
294 if (subret != NULL)
295 {
296 strcpy(ret, subret);
297 return_value = ret;
298 break;
299 }
300 continue;
Steve Kondik4123b582010-11-14 03:18:40 -0500301 }
Koushik K. Dutta1d53c4e2010-04-02 16:46:21 -0700302 strcpy(ret, files[chosen_item - numDirs]);
303 return_value = ret;
304 break;
305 }
306 free_string_array(list);
Koushik K. Dutta981b0cd2010-02-22 08:53:34 -0800307 }
Koushik K. Dutta1d53c4e2010-04-02 16:46:21 -0700308
309 free_string_array(files);
310 free_string_array(dirs);
311 return return_value;
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800312}
313
Koushik Dutta30a937a2011-09-05 21:14:06 -0700314void show_choose_zip_menu(const char *mount_point)
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800315{
Koushik Dutta30a937a2011-09-05 21:14:06 -0700316 if (ensure_path_mounted(mount_point) != 0) {
317 LOGE ("Can't mount %s\n", mount_point);
Koushik K. Duttae9234872010-02-12 00:43:24 -0800318 return;
319 }
320
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800321 static char* headers[] = { "Choose a zip to apply",
322 "",
Steve Kondik4123b582010-11-14 03:18:40 -0500323 NULL
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800324 };
Steve Kondik4123b582010-11-14 03:18:40 -0500325
Koushik Dutta30a937a2011-09-05 21:14:06 -0700326 char* file = choose_file_menu(mount_point, ".zip", headers);
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800327 if (file == NULL)
328 return;
Koushik Duttad63eaef2010-07-14 21:01:21 -0700329 static char* confirm_install = "Confirm install?";
330 static char confirm[PATH_MAX];
331 sprintf(confirm, "Yes - Install %s", basename(file));
332 if (confirm_selection(confirm_install, confirm))
Koushik Duttadf1e4062010-12-18 17:42:31 -0800333 install_zip(file);
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800334}
335
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800336void show_nandroid_restore_menu()
337{
Koushik Duttadf1e4062010-12-18 17:42:31 -0800338 if (ensure_path_mounted("/sdcard") != 0) {
Koushik K. Dutta54305a82010-03-12 17:43:26 -0800339 LOGE ("Can't mount /sdcard\n");
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800340 return;
Koushik K. Dutta54305a82010-03-12 17:43:26 -0800341 }
Steve Kondik4123b582010-11-14 03:18:40 -0500342
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800343 static char* headers[] = { "Choose an image to restore",
344 "",
Steve Kondik4123b582010-11-14 03:18:40 -0500345 NULL
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800346 };
Koushik K. Duttae9234872010-02-12 00:43:24 -0800347
Koushik K. Dutta49f56892010-02-25 14:51:05 -0800348 char* file = choose_file_menu("/sdcard/clockworkmod/backup/", NULL, headers);
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800349 if (file == NULL)
350 return;
Koushik Duttad63eaef2010-07-14 21:01:21 -0700351
352 if (confirm_selection("Confirm restore?", "Yes - Restore"))
Koushik Dutta5b7f34a2010-12-29 23:36:03 -0800353 nandroid_restore(file, 1, 1, 1, 1, 1, 0);
Koushik K. Dutta03173782010-02-26 14:14:23 -0800354}
355
jt113447d86a32011-02-27 18:54:09 -0600356#ifndef BOARD_UMS_LUNFILE
357#define BOARD_UMS_LUNFILE "/sys/devices/platform/usb_mass_storage/lun0/file"
358#endif
359
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700360void show_mount_usb_storage_menu()
Koushik K. Dutta03173782010-02-26 14:14:23 -0800361{
Koushik Duttaf1cb0d52011-01-01 20:03:30 -0800362 int fd;
Koushik Duttadf1e4062010-12-18 17:42:31 -0800363 Volume *vol = volume_for_path("/sdcard");
jt113447d86a32011-02-27 18:54:09 -0600364 if ((fd = open(BOARD_UMS_LUNFILE, O_WRONLY)) < 0) {
Koushik Duttaf1cb0d52011-01-01 20:03:30 -0800365 LOGE("Unable to open ums lunfile (%s)", strerror(errno));
366 return -1;
367 }
368
mik_osdf3004b2011-04-26 19:15:22 +0300369 if ((write(fd, vol->device, strlen(vol->device)) < 0) &&
370 (!vol->device2 || (write(fd, vol->device, strlen(vol->device2)) < 0))) {
Koushik Duttaf1cb0d52011-01-01 20:03:30 -0800371 LOGE("Unable to write to ums lunfile (%s)", strerror(errno));
372 close(fd);
373 return -1;
374 }
Koushik K. Dutta03173782010-02-26 14:14:23 -0800375 static char* headers[] = { "USB Mass Storage device",
376 "Leaving this menu unmount",
377 "your SD card from your PC.",
378 "",
Steve Kondik4123b582010-11-14 03:18:40 -0500379 NULL
Koushik K. Dutta03173782010-02-26 14:14:23 -0800380 };
Steve Kondik4123b582010-11-14 03:18:40 -0500381
Koushik K. Dutta03173782010-02-26 14:14:23 -0800382 static char* list[] = { "Unmount", NULL };
Steve Kondik4123b582010-11-14 03:18:40 -0500383
Koushik K. Dutta03173782010-02-26 14:14:23 -0800384 for (;;)
385 {
Koushik Duttadf1e4062010-12-18 17:42:31 -0800386 int chosen_item = get_menu_selection(headers, list, 0, 0);
Koushik K. Dutta03173782010-02-26 14:14:23 -0800387 if (chosen_item == GO_BACK || chosen_item == 0)
388 break;
389 }
Steve Kondik4123b582010-11-14 03:18:40 -0500390
jt113447d86a32011-02-27 18:54:09 -0600391 if ((fd = open(BOARD_UMS_LUNFILE, O_WRONLY)) < 0) {
Koushik Duttaf1cb0d52011-01-01 20:03:30 -0800392 LOGE("Unable to open ums lunfile (%s)", strerror(errno));
393 return -1;
394 }
395
396 char ch = 0;
397 if (write(fd, &ch, 1) < 0) {
398 LOGE("Unable to write to ums lunfile (%s)", strerror(errno));
399 close(fd);
400 return -1;
401 }
Koushik K. Duttae81cb752010-02-26 17:44:33 -0800402}
Koushik K. Duttaf68aaaf2010-03-07 13:39:21 -0800403
Koushik Duttad63eaef2010-07-14 21:01:21 -0700404int confirm_selection(const char* title, const char* confirm)
Koushik K. Dutta16f0b492010-03-19 14:37:11 -0700405{
Koushik Duttaceddcd52010-08-23 16:13:14 -0700406 struct stat info;
407 if (0 == stat("/sdcard/clockworkmod/.no_confirm", &info))
408 return 1;
409
Koushik Duttad63eaef2010-07-14 21:01:21 -0700410 char* confirm_headers[] = { title, " THIS CAN NOT BE UNDONE.", "", NULL };
411 char* items[] = { "No",
412 "No",
413 "No",
414 "No",
415 "No",
416 "No",
417 "No",
Koushik Duttaecd32fa2010-07-14 18:38:02 -0700418 confirm, //" Yes -- wipe partition", // [7
Koushik Duttad63eaef2010-07-14 21:01:21 -0700419 "No",
420 "No",
421 "No",
Koushik K. Dutta16f0b492010-03-19 14:37:11 -0700422 NULL };
423
Koushik Duttadf1e4062010-12-18 17:42:31 -0800424 int chosen_item = get_menu_selection(confirm_headers, items, 0, 0);
Koushik K. Dutta16f0b492010-03-19 14:37:11 -0700425 return chosen_item == 7;
426}
427
Koushik Duttada32b542011-01-01 17:55:22 -0800428#define MKE2FS_BIN "/sbin/mke2fs"
429#define TUNE2FS_BIN "/sbin/tune2fs"
430#define E2FSCK_BIN "/sbin/e2fsck"
431
Koushik Dutta30a937a2011-09-05 21:14:06 -0700432int format_device(const char *device, const char *path, const char *fs_type) {
433 Volume* v = volume_for_path(path);
434 if (v == NULL) {
435 // no /sdcard? let's assume /data/media
436 if (strstr(path, "/sdcard") == path && is_data_media()) {
437 return format_unknown_device(NULL, path, NULL);
438 }
439 // silent failure for sd-ext
440 if (strcmp(path, "/sd-ext") == 0)
441 return -1;
442 LOGE("unknown volume \"%s\"\n", path);
443 return -1;
444 }
445 if (strcmp(fs_type, "ramdisk") == 0) {
446 // you can't format the ramdisk.
447 LOGE("can't format_volume \"%s\"", path);
448 return -1;
449 }
450
451 if (strcmp(v->mount_point, path) != 0) {
452 return format_unknown_device(v->device, path, NULL);
453 }
454
455 if (ensure_path_unmounted(path) != 0) {
456 LOGE("format_volume failed to unmount \"%s\"\n", v->mount_point);
457 return -1;
458 }
459
460 if (strcmp(fs_type, "yaffs2") == 0 || strcmp(fs_type, "mtd") == 0) {
461 mtd_scan_partitions();
462 const MtdPartition* partition = mtd_find_partition_by_name(device);
463 if (partition == NULL) {
464 LOGE("format_volume: no MTD partition \"%s\"\n", device);
465 return -1;
466 }
467
468 MtdWriteContext *write = mtd_write_partition(partition);
469 if (write == NULL) {
470 LOGW("format_volume: can't open MTD \"%s\"\n", device);
471 return -1;
472 } else if (mtd_erase_blocks(write, -1) == (off_t) -1) {
473 LOGW("format_volume: can't erase MTD \"%s\"\n", device);
474 mtd_write_close(write);
475 return -1;
476 } else if (mtd_write_close(write)) {
477 LOGW("format_volume: can't close MTD \"%s\"\n",device);
478 return -1;
479 }
480 return 0;
481 }
482
483 if (strcmp(fs_type, "ext4") == 0) {
484 reset_ext4fs_info();
485 int result = make_ext4fs(device, NULL, NULL, 0, 0, 0);
486 if (result != 0) {
487 LOGE("format_volume: make_extf4fs failed on %s\n", device);
488 return -1;
489 }
490 return 0;
491 }
492
493 return format_unknown_device(device, path, fs_type);
494}
495
Koushik Duttada32b542011-01-01 17:55:22 -0800496int format_unknown_device(const char *device, const char* path, const char *fs_type)
Koushik K. Dutta3f995392010-03-30 23:29:43 -0700497{
Koushik Dutta2e454492011-01-01 18:06:17 -0800498 LOGI("Formatting unknown device.\n");
Koushik Duttaa8708c62011-01-01 18:00:27 -0800499
Koushik Duttafb3bd712011-06-16 14:13:02 -0700500 if (fs_type != NULL && get_flash_type(fs_type) != UNSUPPORTED)
Koushik Dutta8a6bc772011-05-26 11:14:15 -0700501 return erase_raw_partition(fs_type, device);
Koushik Duttada32b542011-01-01 17:55:22 -0800502
Koushik Dutta9f52e5f2011-01-02 14:11:24 -0800503 // if this is SDEXT:, don't worry about it if it does not exist.
Koushik Duttadf1e4062010-12-18 17:42:31 -0800504 if (0 == strcmp(path, "/sd-ext"))
Koushik K. Duttaf3534d02010-04-18 18:06:24 -0700505 {
506 struct stat st;
Koushik Duttadf1e4062010-12-18 17:42:31 -0800507 Volume *vol = volume_for_path("/sd-ext");
508 if (vol == NULL || 0 != stat(vol->device, &st))
Koushik K. Duttaf3534d02010-04-18 18:06:24 -0700509 {
510 ui_print("No app2sd partition found. Skipping format of /sd-ext.\n");
511 return 0;
512 }
513 }
Koushik Dutta2f73e582010-04-18 16:00:21 -0700514
Koushik Dutta9f52e5f2011-01-02 14:11:24 -0800515 if (NULL != fs_type) {
516 if (strcmp("ext3", fs_type) == 0) {
517 LOGI("Formatting ext3 device.\n");
518 if (0 != ensure_path_unmounted(path)) {
519 LOGE("Error while unmounting %s.\n", path);
520 return -12;
521 }
522 return format_ext3_device(device);
523 }
524
525 if (strcmp("ext2", fs_type) == 0) {
526 LOGI("Formatting ext2 device.\n");
527 if (0 != ensure_path_unmounted(path)) {
528 LOGE("Error while unmounting %s.\n", path);
529 return -12;
530 }
531 return format_ext2_device(device);
532 }
533 }
534
Koushik Duttadf1e4062010-12-18 17:42:31 -0800535 if (0 != ensure_path_mounted(path))
Koushik K. Dutta3f995392010-03-30 23:29:43 -0700536 {
537 ui_print("Error mounting %s!\n", path);
Koushik Duttacd44ab92010-06-23 00:02:14 -0700538 ui_print("Skipping format...\n");
539 return 0;
Koushik K. Dutta3f995392010-03-30 23:29:43 -0700540 }
541
542 static char tmp[PATH_MAX];
Koushik Dutta50822992011-06-08 19:03:27 -0700543 if (strcmp(path, "/data") == 0) {
544 sprintf(tmp, "cd /data ; for f in $(ls -a | grep -v ^media$); do rm -rf $f; done");
545 __system(tmp);
546 }
547 else {
548 sprintf(tmp, "rm -rf %s/*", path);
549 __system(tmp);
550 sprintf(tmp, "rm -rf %s/.*", path);
551 __system(tmp);
552 }
Steve Kondik4123b582010-11-14 03:18:40 -0500553
Koushik Duttadf1e4062010-12-18 17:42:31 -0800554 ensure_path_unmounted(path);
Koushik K. Dutta3f995392010-03-30 23:29:43 -0700555 return 0;
556}
557
Koushik Duttadf1e4062010-12-18 17:42:31 -0800558//#define MOUNTABLE_COUNT 5
559//#define DEVICE_COUNT 4
560//#define MMC_COUNT 2
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700561
Kolja Dummann92796ec2011-02-13 20:59:30 +0100562typedef struct {
563 char mount[255];
564 char unmount[255];
565 Volume* v;
566} MountMenuEntry;
567
568typedef struct {
569 char txt[255];
570 Volume* v;
571} FormatMenuEntry;
572
Steve Kondik0d878512011-04-09 01:41:45 -0400573int is_safe_to_format(char* name)
574{
Brandon Bennett9ad8ba22011-05-07 23:42:58 -0600575 char str[255];
576 char* partition;
j_r0dd14d9e752011-06-09 18:37:28 -0400577 property_get("ro.cwm.forbid_format", str, "/misc,/radio,/bootloader,/recovery,/efs");
Brandon Bennett9ad8ba22011-05-07 23:42:58 -0600578
579 partition = strtok(str, ", ");
580 while (partition != NULL) {
581 if (strcmp(name, partition) == 0) {
582 return 0;
583 }
584 partition = strtok(NULL, ", ");
585 }
586
587 return 1;
Steve Kondik0d878512011-04-09 01:41:45 -0400588}
589
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700590void show_partition_menu()
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700591{
Koushik Duttaceddcd52010-08-23 16:13:14 -0700592 static char* headers[] = { "Mounts and Storage Menu",
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700593 "",
Steve Kondik4123b582010-11-14 03:18:40 -0500594 NULL
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700595 };
596
Kolja Dummann92796ec2011-02-13 20:59:30 +0100597 static MountMenuEntry* mount_menue = NULL;
598 static FormatMenuEntry* format_menue = NULL;
599
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700600 typedef char* string;
Steve Kondik4123b582010-11-14 03:18:40 -0500601
Kolja Dummann92796ec2011-02-13 20:59:30 +0100602 int i, mountable_volumes, formatable_volumes;
603 int num_volumes;
604 Volume* device_volumes;
Steve Kondik4123b582010-11-14 03:18:40 -0500605
Kolja Dummann92796ec2011-02-13 20:59:30 +0100606 num_volumes = get_num_volumes();
607 device_volumes = get_device_volumes();
608
609 string options[255];
610
611 if(!device_volumes)
612 return;
613
614 mountable_volumes = 0;
615 formatable_volumes = 0;
616
617 mount_menue = malloc(num_volumes * sizeof(MountMenuEntry));
618 format_menue = malloc(num_volumes * sizeof(FormatMenuEntry));
619
620 for (i = 0; i < num_volumes; ++i) {
621 Volume* v = &device_volumes[i];
Koushik Dutta67fa0c32011-03-17 11:37:21 -0700622 if(strcmp("ramdisk", v->fs_type) != 0 && strcmp("mtd", v->fs_type) != 0 && strcmp("emmc", v->fs_type) != 0 && strcmp("bml", v->fs_type) != 0)
Kolja Dummann92796ec2011-02-13 20:59:30 +0100623 {
624 sprintf(&mount_menue[mountable_volumes].mount, "mount %s", v->mount_point);
625 sprintf(&mount_menue[mountable_volumes].unmount, "unmount %s", v->mount_point);
626 mount_menue[mountable_volumes].v = &device_volumes[i];
627 ++mountable_volumes;
Steve Kondik0d878512011-04-09 01:41:45 -0400628 if (is_safe_to_format(v->mount_point)) {
629 sprintf(&format_menue[formatable_volumes].txt, "format %s", v->mount_point);
630 format_menue[formatable_volumes].v = &device_volumes[i];
631 ++formatable_volumes;
632 }
Kolja Dummann92796ec2011-02-13 20:59:30 +0100633 }
Steve Kondik0d878512011-04-09 01:41:45 -0400634 else if (strcmp("ramdisk", v->fs_type) != 0 && strcmp("mtd", v->fs_type) == 0 && is_safe_to_format(v->mount_point))
Kolja Dummann92796ec2011-02-13 20:59:30 +0100635 {
636 sprintf(&format_menue[formatable_volumes].txt, "format %s", v->mount_point);
637 format_menue[formatable_volumes].v = &device_volumes[i];
638 ++formatable_volumes;
639 }
640 }
641
Koushik Duttadf1e4062010-12-18 17:42:31 -0800642
Koushik Duttad63eaef2010-07-14 21:01:21 -0700643 static char* confirm_format = "Confirm format?";
Koushik Duttaecd32fa2010-07-14 18:38:02 -0700644 static char* confirm = "Yes - Format";
wjbcea6eba2011-04-30 21:04:04 -0400645 char confirm_string[255];
Steve Kondik4123b582010-11-14 03:18:40 -0500646
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700647 for (;;)
648 {
Steve Kondik4123b582010-11-14 03:18:40 -0500649
Kolja Dummann92796ec2011-02-13 20:59:30 +0100650 for (i = 0; i < mountable_volumes; i++)
651 {
652 MountMenuEntry* e = &mount_menue[i];
653 Volume* v = e->v;
654 if(is_path_mounted(v->mount_point))
655 options[i] = e->unmount;
656 else
657 options[i] = e->mount;
658 }
Steve Kondik4123b582010-11-14 03:18:40 -0500659
Kolja Dummann92796ec2011-02-13 20:59:30 +0100660 for (i = 0; i < formatable_volumes; i++)
661 {
662 FormatMenuEntry* e = &format_menue[i];
Steve Kondik4123b582010-11-14 03:18:40 -0500663
Kolja Dummann92796ec2011-02-13 20:59:30 +0100664 options[mountable_volumes+i] = e->txt;
665 }
666
667 options[mountable_volumes+formatable_volumes] = "mount USB storage";
668 options[mountable_volumes+formatable_volumes + 1] = NULL;
669
670 int chosen_item = get_menu_selection(headers, &options, 0, 0);
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700671 if (chosen_item == GO_BACK)
672 break;
Kolja Dummann92796ec2011-02-13 20:59:30 +0100673 if (chosen_item == (mountable_volumes+formatable_volumes))
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700674 {
675 show_mount_usb_storage_menu();
676 }
Kolja Dummann92796ec2011-02-13 20:59:30 +0100677 else if (chosen_item < mountable_volumes)
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700678 {
Kolja Dummann92796ec2011-02-13 20:59:30 +0100679 MountMenuEntry* e = &mount_menue[chosen_item];
680 Volume* v = e->v;
681
682 if (is_path_mounted(v->mount_point))
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700683 {
Kolja Dummann92796ec2011-02-13 20:59:30 +0100684 if (0 != ensure_path_unmounted(v->mount_point))
685 ui_print("Error unmounting %s!\n", v->mount_point);
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700686 }
687 else
688 {
Kolja Dummann92796ec2011-02-13 20:59:30 +0100689 if (0 != ensure_path_mounted(v->mount_point))
690 ui_print("Error mounting %s!\n", v->mount_point);
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700691 }
692 }
Kolja Dummann92796ec2011-02-13 20:59:30 +0100693 else if (chosen_item < (mountable_volumes + formatable_volumes))
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700694 {
Kolja Dummann92796ec2011-02-13 20:59:30 +0100695 chosen_item = chosen_item - mountable_volumes;
696 FormatMenuEntry* e = &format_menue[chosen_item];
697 Volume* v = e->v;
698
wjbcea6eba2011-04-30 21:04:04 -0400699 sprintf(confirm_string, "%s - %s", v->mount_point, confirm_format);
700
701 if (!confirm_selection(confirm_string, confirm))
Koushik K. Dutta16f0b492010-03-19 14:37:11 -0700702 continue;
Kolja Dummann92796ec2011-02-13 20:59:30 +0100703 ui_print("Formatting %s...\n", v->mount_point);
704 if (0 != format_volume(v->mount_point))
705 ui_print("Error formatting %s!\n", v->mount_point);
Koushik K. Dutta3f995392010-03-30 23:29:43 -0700706 else
707 ui_print("Done.\n");
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700708 }
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700709 }
Kolja Dummann92796ec2011-02-13 20:59:30 +0100710
711 free(mount_menue);
712 free(format_menue);
713
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700714}
715
Koushik K. Dutta72a1db62010-03-07 14:10:26 -0800716#define EXTENDEDCOMMAND_SCRIPT "/cache/recovery/extendedcommand"
717
718int extendedcommand_file_exists()
Koushik K. Duttaf68aaaf2010-03-07 13:39:21 -0800719{
Koushik K. Duttaf68aaaf2010-03-07 13:39:21 -0800720 struct stat file_info;
Koushik K. Dutta72a1db62010-03-07 14:10:26 -0800721 return 0 == stat(EXTENDEDCOMMAND_SCRIPT, &file_info);
722}
723
Koushik Duttabec09952010-12-19 20:37:57 -0800724int edify_main(int argc, char** argv) {
725 load_volume_table();
726 process_volumes();
727 RegisterBuiltins();
728 RegisterRecoveryHooks();
729 FinishRegistration();
Koushik Duttadf1e4062010-12-18 17:42:31 -0800730
Koushik Duttabec09952010-12-19 20:37:57 -0800731 if (argc != 2) {
732 printf("edify <filename>\n");
Koushik K. Duttaf68aaaf2010-03-07 13:39:21 -0800733 return 1;
Koushik K. Duttaf68aaaf2010-03-07 13:39:21 -0800734 }
735
Koushik Duttabec09952010-12-19 20:37:57 -0800736 FILE* f = fopen(argv[1], "r");
737 if (f == NULL) {
738 printf("%s: %s: No such file or directory\n", argv[0], argv[1]);
739 return 1;
740 }
741 char buffer[8192];
742 int size = fread(buffer, 1, 8191, f);
743 fclose(f);
744 buffer[size] = '\0';
745
746 Expr* root;
747 int error_count = 0;
748 yy_scan_bytes(buffer, size);
749 int error = yyparse(&root, &error_count);
750 printf("parse returned %d; %d errors encountered\n", error, error_count);
751 if (error == 0 || error_count > 0) {
752
753 //ExprDump(0, root, buffer);
754
755 State state;
756 state.cookie = NULL;
757 state.script = buffer;
758 state.errmsg = NULL;
759
760 char* result = Evaluate(&state, root);
761 if (result == NULL) {
762 printf("result was NULL, message is: %s\n",
763 (state.errmsg == NULL ? "(NULL)" : state.errmsg));
764 free(state.errmsg);
765 } else {
766 printf("result is [%s]\n", result);
Koushik K. Duttaf68aaaf2010-03-07 13:39:21 -0800767 }
Steve Kondik4123b582010-11-14 03:18:40 -0500768 }
Koushik Duttabec09952010-12-19 20:37:57 -0800769 return 0;
Koushik K. Duttaf68aaaf2010-03-07 13:39:21 -0800770}
Koushik K. Dutta72a1db62010-03-07 14:10:26 -0800771
Koushik K. Dutta6771aca2010-04-03 23:28:39 -0700772int run_script(char* filename)
Koushik K. Dutta3836f722010-03-11 22:17:43 -0800773{
774 struct stat file_info;
775 if (0 != stat(filename, &file_info)) {
776 printf("Error executing stat on file: %s\n", filename);
777 return 1;
778 }
Steve Kondik4123b582010-11-14 03:18:40 -0500779
Koushik K. Dutta3836f722010-03-11 22:17:43 -0800780 int script_len = file_info.st_size;
Koushik K. Dutta707fa6d2010-03-23 11:44:33 -0700781 char* script_data = (char*)malloc(script_len + 1);
Koushik K. Dutta3836f722010-03-11 22:17:43 -0800782 FILE *file = fopen(filename, "rb");
783 fread(script_data, script_len, 1, file);
Koushik K. Dutta707fa6d2010-03-23 11:44:33 -0700784 // supposedly not necessary, but let's be safe.
785 script_data[script_len] = '\0';
Koushik K. Dutta3836f722010-03-11 22:17:43 -0800786 fclose(file);
Koushik Duttae25908b2010-06-21 08:16:19 -0700787 LOGI("Running script:\n");
788 LOGI("\n%s\n", script_data);
Koushik K. Dutta3836f722010-03-11 22:17:43 -0800789
Koushik K. Duttaf3534d02010-04-18 18:06:24 -0700790 int ret = run_script_from_buffer(script_data, script_len, filename);
Koushik K. Dutta1d53c4e2010-04-02 16:46:21 -0700791 free(script_data);
792 return ret;
Koushik K. Dutta3836f722010-03-11 22:17:43 -0800793}
794
Koushik K. Dutta72a1db62010-03-07 14:10:26 -0800795int run_and_remove_extendedcommand()
796{
Koushik K. Dutta6771aca2010-04-03 23:28:39 -0700797 char tmp[PATH_MAX];
798 sprintf(tmp, "cp %s /tmp/%s", EXTENDEDCOMMAND_SCRIPT, basename(EXTENDEDCOMMAND_SCRIPT));
799 __system(tmp);
800 remove(EXTENDEDCOMMAND_SCRIPT);
Koushik K. Dutta3a25cf52010-03-08 19:22:41 -0800801 int i = 0;
802 for (i = 20; i > 0; i--) {
803 ui_print("Waiting for SD Card to mount (%ds)\n", i);
Koushik Duttadf1e4062010-12-18 17:42:31 -0800804 if (ensure_path_mounted("/sdcard") == 0) {
Koushik K. Dutta3a25cf52010-03-08 19:22:41 -0800805 ui_print("SD Card mounted...\n");
806 break;
807 }
808 sleep(1);
809 }
Koushik Dutta92077c12010-07-15 00:10:08 -0700810 remove("/sdcard/clockworkmod/.recoverycheckpoint");
Koushik K. Dutta3a25cf52010-03-08 19:22:41 -0800811 if (i == 0) {
812 ui_print("Timed out waiting for SD card... continuing anyways.");
813 }
Steve Kondik4123b582010-11-14 03:18:40 -0500814
Koushik Dutta30a937a2011-09-05 21:14:06 -0700815 ui_print("Verifying SD Card marker...\n");
816 struct stat st;
817 if (stat("/sdcard/clockworkmod/.salted_hash", &st) != 0) {
818 ui_print("SD Card marker not found...\n");
819 if (volume_for_path("/emmc") != NULL) {
820 ui_print("Checking Internal SD Card marker...\n");
821 ensure_path_unmounted("/sdcard");
822 if (ensure_path_mounted_at_mount_point("/emmc", "/sdcard") != 0) {
823 ui_print("Internal SD Card marker not found... continuing anyways.\n");
824 // unmount everything, and remount as normal
825 ensure_path_unmounted("/emmc");
826 ensure_path_unmounted("/sdcard");
827
828 ensure_path_mounted("/sdcard");
829 }
830 }
831 }
832
Koushik K. Dutta6771aca2010-04-03 23:28:39 -0700833 sprintf(tmp, "/tmp/%s", basename(EXTENDEDCOMMAND_SCRIPT));
Koushik Dutta30a937a2011-09-05 21:14:06 -0700834 int ret;
835#ifdef I_AM_KOUSH
836 if (0 != (ret = before_run_script(tmp))) {
Koushik Dutta81a61512011-09-22 15:10:20 -0700837 ui_print("Error processing ROM Manager script. Please verify that you are performing the backup, restore, or ROM installation from ROM Manager v4.4.0.0 or higher.\n");
Koushik Dutta30a937a2011-09-05 21:14:06 -0700838 return ret;
839 }
840#endif
Koushik K. Dutta6771aca2010-04-03 23:28:39 -0700841 return run_script(tmp);
Koushik K. Dutta72a1db62010-03-07 14:10:26 -0800842}
843
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700844void show_nandroid_advanced_restore_menu()
845{
Koushik Duttadf1e4062010-12-18 17:42:31 -0800846 if (ensure_path_mounted("/sdcard") != 0) {
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700847 LOGE ("Can't mount /sdcard\n");
848 return;
849 }
850
851 static char* advancedheaders[] = { "Choose an image to restore",
852 "",
853 "Choose an image to restore",
854 "first. The next menu will",
855 "you more options.",
856 "",
857 NULL
858 };
859
860 char* file = choose_file_menu("/sdcard/clockworkmod/backup/", NULL, advancedheaders);
861 if (file == NULL)
862 return;
863
864 static char* headers[] = { "Nandroid Advanced Restore",
865 "",
866 NULL
867 };
868
869 static char* list[] = { "Restore boot",
870 "Restore system",
871 "Restore data",
872 "Restore cache",
Koushik K. Dutta68b01902010-04-01 12:20:39 -0700873 "Restore sd-ext",
agrabren1f76a5d2010-12-29 12:15:18 -0600874 "Restore wimax",
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700875 NULL
876 };
Koushik Dutta5b7f34a2010-12-29 23:36:03 -0800877
878 char tmp[PATH_MAX];
879 if (0 != get_partition_device("wimax", tmp)) {
880 // disable wimax restore option
881 list[5] = NULL;
882 }
Koushik Duttad63eaef2010-07-14 21:01:21 -0700883
884 static char* confirm_restore = "Confirm restore?";
885
Koushik Duttadf1e4062010-12-18 17:42:31 -0800886 int chosen_item = get_menu_selection(headers, list, 0, 0);
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700887 switch (chosen_item)
888 {
889 case 0:
Koushik Duttad63eaef2010-07-14 21:01:21 -0700890 if (confirm_selection(confirm_restore, "Yes - Restore boot"))
agrabren1f76a5d2010-12-29 12:15:18 -0600891 nandroid_restore(file, 1, 0, 0, 0, 0, 0);
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700892 break;
893 case 1:
Koushik Duttad63eaef2010-07-14 21:01:21 -0700894 if (confirm_selection(confirm_restore, "Yes - Restore system"))
agrabren1f76a5d2010-12-29 12:15:18 -0600895 nandroid_restore(file, 0, 1, 0, 0, 0, 0);
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700896 break;
897 case 2:
Koushik Duttad63eaef2010-07-14 21:01:21 -0700898 if (confirm_selection(confirm_restore, "Yes - Restore data"))
agrabren1f76a5d2010-12-29 12:15:18 -0600899 nandroid_restore(file, 0, 0, 1, 0, 0, 0);
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700900 break;
901 case 3:
Koushik Duttad63eaef2010-07-14 21:01:21 -0700902 if (confirm_selection(confirm_restore, "Yes - Restore cache"))
agrabren1f76a5d2010-12-29 12:15:18 -0600903 nandroid_restore(file, 0, 0, 0, 1, 0, 0);
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700904 break;
Koushik Dutta2f73e582010-04-18 16:00:21 -0700905 case 4:
Koushik Duttad63eaef2010-07-14 21:01:21 -0700906 if (confirm_selection(confirm_restore, "Yes - Restore sd-ext"))
agrabren1f76a5d2010-12-29 12:15:18 -0600907 nandroid_restore(file, 0, 0, 0, 0, 1, 0);
908 break;
909 case 5:
910 if (confirm_selection(confirm_restore, "Yes - Restore wimax"))
911 nandroid_restore(file, 0, 0, 0, 0, 0, 1);
Koushik Dutta2f73e582010-04-18 16:00:21 -0700912 break;
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700913 }
914}
915
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700916void show_nandroid_menu()
917{
918 static char* headers[] = { "Nandroid",
919 "",
Steve Kondik4123b582010-11-14 03:18:40 -0500920 NULL
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700921 };
922
Steve Kondik4123b582010-11-14 03:18:40 -0500923 static char* list[] = { "Backup",
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700924 "Restore",
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700925 "Advanced Restore",
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700926 NULL
927 };
928
Koushik Duttadf1e4062010-12-18 17:42:31 -0800929 int chosen_item = get_menu_selection(headers, list, 0, 0);
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700930 switch (chosen_item)
931 {
932 case 0:
933 {
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700934 char backup_path[PATH_MAX];
Koushik K. Dutta6a26e7c2010-03-27 15:26:11 -0700935 time_t t = time(NULL);
936 struct tm *tmp = localtime(&t);
937 if (tmp == NULL)
938 {
939 struct timeval tp;
940 gettimeofday(&tp, NULL);
941 sprintf(backup_path, "/sdcard/clockworkmod/backup/%d", tp.tv_sec);
942 }
943 else
944 {
945 strftime(backup_path, sizeof(backup_path), "/sdcard/clockworkmod/backup/%F.%H.%M.%S", tmp);
946 }
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700947 nandroid_backup(backup_path);
948 }
949 break;
950 case 1:
951 show_nandroid_restore_menu();
952 break;
Koushik K. Duttafe84a7f2010-03-25 18:19:23 -0700953 case 2:
954 show_nandroid_advanced_restore_menu();
955 break;
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700956 }
Koushik K. Duttaa496b512010-03-19 14:51:45 -0700957}
958
Koushik Duttafd1579b2010-05-01 12:46:55 -0700959void wipe_battery_stats()
960{
Koushik Duttadf1e4062010-12-18 17:42:31 -0800961 ensure_path_mounted("/data");
Koushik Duttafd1579b2010-05-01 12:46:55 -0700962 remove("/data/system/batterystats.bin");
Koushik Duttadf1e4062010-12-18 17:42:31 -0800963 ensure_path_unmounted("/data");
j_r0dd3f2e2862011-07-10 22:48:39 -0400964 ui_print("Battery Stats wiped.\n");
Koushik Duttafd1579b2010-05-01 12:46:55 -0700965}
966
Koushik K. Duttaa496b512010-03-19 14:51:45 -0700967void show_advanced_menu()
968{
969 static char* headers[] = { "Advanced and Debugging Menu",
970 "",
971 NULL
972 };
973
Koushik K. Dutta7fe4d7b2010-04-06 23:04:52 -0700974 static char* list[] = { "Reboot Recovery",
Koushik Dutta49af23c2010-06-21 13:45:51 -0700975 "Wipe Dalvik Cache",
Koushik Duttafd1579b2010-05-01 12:46:55 -0700976 "Wipe Battery Stats",
Koushik Dutta598cfc72010-06-20 09:42:47 -0700977 "Report Error",
Koushik K. Dutta7fe4d7b2010-04-06 23:04:52 -0700978 "Key Test",
Tanguy Pruvot67d358c2011-06-10 20:26:44 +0200979 "Show log",
Koushik Duttae17a78d2010-08-29 12:35:10 -0700980#ifndef BOARD_HAS_SMALL_RECOVERY
Koushik Duttaceddcd52010-08-23 16:13:14 -0700981 "Partition SD Card",
982 "Fix Permissions",
Brint E. Kriebeld3522332010-12-11 20:52:06 -0700983#ifdef BOARD_HAS_SDCARD_INTERNAL
984 "Partition Internal SD Card",
985#endif
Koushik Duttae17a78d2010-08-29 12:35:10 -0700986#endif
Koushik K. Duttaa496b512010-03-19 14:51:45 -0700987 NULL
988 };
989
990 for (;;)
991 {
Koushik Duttadf1e4062010-12-18 17:42:31 -0800992 int chosen_item = get_menu_selection(headers, list, 0, 0);
Koushik K. Duttaa496b512010-03-19 14:51:45 -0700993 if (chosen_item == GO_BACK)
994 break;
995 switch (chosen_item)
996 {
997 case 0:
Tanguy Pruvot67d358c2011-06-10 20:26:44 +0200998 {
Austen Dicken7126ef42011-06-14 15:47:40 -0500999 reboot_wrapper("recovery");
Koushik K. Dutta7fe4d7b2010-04-06 23:04:52 -07001000 break;
Tanguy Pruvot67d358c2011-06-10 20:26:44 +02001001 }
Koushik K. Dutta7fe4d7b2010-04-06 23:04:52 -07001002 case 1:
Koushik Duttad63eaef2010-07-14 21:01:21 -07001003 {
Koushik Duttadf1e4062010-12-18 17:42:31 -08001004 if (0 != ensure_path_mounted("/data"))
Koushik Dutta49af23c2010-06-21 13:45:51 -07001005 break;
Koushik Duttadf1e4062010-12-18 17:42:31 -08001006 ensure_path_mounted("/sd-ext");
1007 ensure_path_mounted("/cache");
Koushik Dutta6c7745d2010-08-07 12:17:13 -07001008 if (confirm_selection( "Confirm wipe?", "Yes - Wipe Dalvik Cache")) {
Koushik Duttad63eaef2010-07-14 21:01:21 -07001009 __system("rm -r /data/dalvik-cache");
Koushik Dutta6c7745d2010-08-07 12:17:13 -07001010 __system("rm -r /cache/dalvik-cache");
1011 __system("rm -r /sd-ext/dalvik-cache");
Koushik Dutta30a937a2011-09-05 21:14:06 -07001012 ui_print("Dalvik Cache wiped.\n");
Koushik Dutta6c7745d2010-08-07 12:17:13 -07001013 }
Koushik Duttadf1e4062010-12-18 17:42:31 -08001014 ensure_path_unmounted("/data");
Koushik Duttafd1579b2010-05-01 12:46:55 -07001015 break;
Koushik Duttad63eaef2010-07-14 21:01:21 -07001016 }
Koushik Duttafd1579b2010-05-01 12:46:55 -07001017 case 2:
Koushik Duttad63eaef2010-07-14 21:01:21 -07001018 {
1019 if (confirm_selection( "Confirm wipe?", "Yes - Wipe Battery Stats"))
1020 wipe_battery_stats();
Koushik Dutta598cfc72010-06-20 09:42:47 -07001021 break;
Koushik Duttad63eaef2010-07-14 21:01:21 -07001022 }
Koushik Dutta598cfc72010-06-20 09:42:47 -07001023 case 3:
Koushik Dutta49af23c2010-06-21 13:45:51 -07001024 handle_failure(1);
1025 break;
1026 case 4:
Koushik K. Duttaa496b512010-03-19 14:51:45 -07001027 {
1028 ui_print("Outputting key codes.\n");
1029 ui_print("Go back to end debugging.\n");
1030 int key;
1031 int action;
1032 do
1033 {
1034 key = ui_wait_key();
1035 action = device_handle_key(key, 1);
1036 ui_print("Key: %d\n", key);
1037 }
1038 while (action != GO_BACK);
Koushik Dutta56606a22010-08-23 16:15:33 -07001039 break;
Koushik K. Duttaa496b512010-03-19 14:51:45 -07001040 }
Koushik Duttaf0e31b82010-08-17 16:55:38 -07001041 case 5:
1042 {
Tanguy Pruvot67d358c2011-06-10 20:26:44 +02001043 ui_printlogtail(12);
1044 break;
1045 }
1046 case 6:
1047 {
Koushik Duttaceddcd52010-08-23 16:13:14 -07001048 static char* ext_sizes[] = { "128M",
1049 "256M",
1050 "512M",
1051 "1024M",
Brint E. Kriebel0b233312010-11-14 15:31:17 -07001052 "2048M",
1053 "4096M",
Koushik Duttaceddcd52010-08-23 16:13:14 -07001054 NULL };
1055
1056 static char* swap_sizes[] = { "0M",
1057 "32M",
1058 "64M",
1059 "128M",
1060 "256M",
1061 NULL };
1062
1063 static char* ext_headers[] = { "Ext Size", "", NULL };
1064 static char* swap_headers[] = { "Swap Size", "", NULL };
1065
Koushik Duttadf1e4062010-12-18 17:42:31 -08001066 int ext_size = get_menu_selection(ext_headers, ext_sizes, 0, 0);
Koushik Duttaceddcd52010-08-23 16:13:14 -07001067 if (ext_size == GO_BACK)
1068 continue;
Steve Kondik4123b582010-11-14 03:18:40 -05001069
Koushik Duttadf1e4062010-12-18 17:42:31 -08001070 int swap_size = get_menu_selection(swap_headers, swap_sizes, 0, 0);
Koushik Duttaceddcd52010-08-23 16:13:14 -07001071 if (swap_size == GO_BACK)
1072 continue;
1073
1074 char sddevice[256];
Koushik Duttadf1e4062010-12-18 17:42:31 -08001075 Volume *vol = volume_for_path("/sdcard");
1076 strcpy(sddevice, vol->device);
Koushik Duttaceddcd52010-08-23 16:13:14 -07001077 // we only want the mmcblk, not the partition
1078 sddevice[strlen("/dev/block/mmcblkX")] = NULL;
1079 char cmd[PATH_MAX];
1080 setenv("SDPATH", sddevice, 1);
1081 sprintf(cmd, "sdparted -es %s -ss %s -efs ext3 -s", ext_sizes[ext_size], swap_sizes[swap_size]);
1082 ui_print("Partitioning SD Card... please wait...\n");
1083 if (0 == __system(cmd))
1084 ui_print("Done!\n");
1085 else
1086 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 -07001087 break;
Koushik Duttaf0e31b82010-08-17 16:55:38 -07001088 }
Tanguy Pruvot67d358c2011-06-10 20:26:44 +02001089 case 7:
Koushik Dutta38e8b2b2010-08-17 22:21:53 -07001090 {
Koushik Duttadf1e4062010-12-18 17:42:31 -08001091 ensure_path_mounted("/system");
1092 ensure_path_mounted("/data");
Koushik Duttaceddcd52010-08-23 16:13:14 -07001093 ui_print("Fixing permissions...\n");
1094 __system("fix_permissions");
1095 ui_print("Done!\n");
Koushik Dutta56606a22010-08-23 16:15:33 -07001096 break;
Koushik Dutta38e8b2b2010-08-17 22:21:53 -07001097 }
Tanguy Pruvot67d358c2011-06-10 20:26:44 +02001098 case 8:
Brint E. Kriebeld3522332010-12-11 20:52:06 -07001099 {
1100 static char* ext_sizes[] = { "128M",
1101 "256M",
1102 "512M",
1103 "1024M",
1104 "2048M",
1105 "4096M",
1106 NULL };
1107
1108 static char* swap_sizes[] = { "0M",
1109 "32M",
1110 "64M",
1111 "128M",
1112 "256M",
1113 NULL };
1114
1115 static char* ext_headers[] = { "Data Size", "", NULL };
1116 static char* swap_headers[] = { "Swap Size", "", NULL };
1117
Koushik Duttadf1e4062010-12-18 17:42:31 -08001118 int ext_size = get_menu_selection(ext_headers, ext_sizes, 0, 0);
Brint E. Kriebeld3522332010-12-11 20:52:06 -07001119 if (ext_size == GO_BACK)
1120 continue;
1121
1122 int swap_size = 0;
1123 if (swap_size == GO_BACK)
1124 continue;
1125
1126 char sddevice[256];
Koushik Duttadf1e4062010-12-18 17:42:31 -08001127 Volume *vol = volume_for_path("/emmc");
1128 strcpy(sddevice, vol->device);
Brint E. Kriebeld3522332010-12-11 20:52:06 -07001129 // we only want the mmcblk, not the partition
1130 sddevice[strlen("/dev/block/mmcblkX")] = NULL;
1131 char cmd[PATH_MAX];
1132 setenv("SDPATH", sddevice, 1);
1133 sprintf(cmd, "sdparted -es %s -ss %s -efs ext3 -s", ext_sizes[ext_size], swap_sizes[swap_size]);
1134 ui_print("Partitioning Internal SD Card... please wait...\n");
1135 if (0 == __system(cmd))
1136 ui_print("Done!\n");
1137 else
1138 ui_print("An error occured while partitioning your Internal SD Card. Please see /tmp/recovery.log for more details.\n");
1139 break;
1140 }
Koushik K. Duttaa496b512010-03-19 14:51:45 -07001141 }
1142 }
Koushik Dutta14239d22010-06-14 15:02:48 -07001143}
1144
Koushik Duttab643e4f2010-12-18 18:39:39 -08001145void write_fstab_root(char *path, FILE *file)
Koushik Dutta14239d22010-06-14 15:02:48 -07001146{
Koushik Duttab643e4f2010-12-18 18:39:39 -08001147 Volume *vol = volume_for_path(path);
1148 if (vol == NULL) {
Koushik Dutta9765a032010-12-18 21:31:02 -08001149 LOGW("Unable to get recovery.fstab info for %s during fstab generation!\n", path);
Koushik Dutta598cfc72010-06-20 09:42:47 -07001150 return;
Koushik Duttaa6522b32010-06-20 13:16:06 -07001151 }
Steve Kondik4123b582010-11-14 03:18:40 -05001152
Koushik Dutta31b74112010-12-20 08:49:20 -08001153 char device[200];
1154 if (vol->device[0] != '/')
1155 get_partition_device(vol->device, device);
1156 else
1157 strcpy(device, vol->device);
1158
1159 fprintf(file, "%s ", device);
Koushik Duttab643e4f2010-12-18 18:39:39 -08001160 fprintf(file, "%s ", path);
Koushik Duttaab1f8b82011-02-27 17:28:30 -08001161 // special case rfs cause auto will mount it as vfat on samsung.
1162 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 -07001163}
1164
1165void create_fstab()
1166{
Koushik Duttab643e4f2010-12-18 18:39:39 -08001167 struct stat info;
Koushik Duttacd44ab92010-06-23 00:02:14 -07001168 __system("touch /etc/mtab");
Koushik Dutta14239d22010-06-14 15:02:48 -07001169 FILE *file = fopen("/etc/fstab", "w");
Koushik Duttaa6522b32010-06-20 13:16:06 -07001170 if (file == NULL) {
Koushik Duttab643e4f2010-12-18 18:39:39 -08001171 LOGW("Unable to create /etc/fstab!\n");
Koushik Dutta598cfc72010-06-20 09:42:47 -07001172 return;
Koushik Duttaa6522b32010-06-20 13:16:06 -07001173 }
Brandon Bennett6d0604b2011-01-28 13:39:10 -07001174 Volume *vol = volume_for_path("/boot");
Koushik Duttaa9f0e7f2011-02-12 10:21:11 -08001175 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 -07001176 write_fstab_root("/boot", file);
Koushik Duttab643e4f2010-12-18 18:39:39 -08001177 write_fstab_root("/cache", file);
1178 write_fstab_root("/data", file);
Koushik Dutta5460f0c2010-12-18 22:37:49 -08001179 if (has_datadata()) {
1180 write_fstab_root("/datadata", file);
1181 }
Koushik Duttab643e4f2010-12-18 18:39:39 -08001182 write_fstab_root("/system", file);
1183 write_fstab_root("/sdcard", file);
1184 write_fstab_root("/sd-ext", file);
Koushik Dutta14239d22010-06-14 15:02:48 -07001185 fclose(file);
Koushik Dutta9765a032010-12-18 21:31:02 -08001186 LOGI("Completed outputting fstab.\n");
Koushik Dutta598cfc72010-06-20 09:42:47 -07001187}
1188
Koushik Dutta4196e0f2010-12-19 03:38:29 -08001189int bml_check_volume(const char *path) {
1190 ui_print("Checking %s...\n", path);
1191 ensure_path_unmounted(path);
1192 if (0 == ensure_path_mounted(path)) {
1193 ensure_path_unmounted(path);
1194 return 0;
1195 }
1196
1197 Volume *vol = volume_for_path(path);
1198 if (vol == NULL) {
1199 LOGE("Unable process volume! Skipping...\n");
1200 return 0;
1201 }
1202
Koushik Duttabec09952010-12-19 20:37:57 -08001203 ui_print("%s may be rfs. Checking...\n", path);
Koushik Dutta4196e0f2010-12-19 03:38:29 -08001204 char tmp[PATH_MAX];
1205 sprintf(tmp, "mount -t rfs %s %s", vol->device, path);
1206 int ret = __system(tmp);
1207 printf("%d\n", ret);
1208 return ret == 0 ? 1 : 0;
1209}
1210
1211void process_volumes() {
1212 create_fstab();
Koushik Dutta38a92142011-06-10 09:45:52 -07001213
1214 if (is_data_media()) {
1215 setup_data_media();
Koushik Dutta94caefe2011-06-10 09:17:30 -07001216 }
1217
Koushik Duttae8bc2c82011-02-27 14:00:19 -08001218 return;
1219
1220 // dead code.
Koushik Dutta4196e0f2010-12-19 03:38:29 -08001221 if (device_flash_type() != BML)
1222 return;
1223
1224 ui_print("Checking for ext4 partitions...\n");
1225 int ret = 0;
1226 ret = bml_check_volume("/system");
1227 ret |= bml_check_volume("/data");
1228 if (has_datadata())
1229 ret |= bml_check_volume("/datadata");
1230 ret |= bml_check_volume("/cache");
1231
1232 if (ret == 0) {
1233 ui_print("Done!\n");
1234 return;
1235 }
1236
Koushik Dutta745b5ff2010-12-21 00:58:43 -08001237 char backup_path[PATH_MAX];
1238 time_t t = time(NULL);
1239 char backup_name[PATH_MAX];
1240 struct timeval tp;
1241 gettimeofday(&tp, NULL);
1242 sprintf(backup_name, "before-ext4-convert-%d", tp.tv_sec);
1243 sprintf(backup_path, "/sdcard/clockworkmod/backup/%s", backup_name);
1244
Koushik Dutta4196e0f2010-12-19 03:38:29 -08001245 ui_set_show_text(1);
1246 ui_print("Filesystems need to be converted to ext4.\n");
1247 ui_print("A backup and restore will now take place.\n");
Koushik Duttabec09952010-12-19 20:37:57 -08001248 ui_print("If anything goes wrong, your backup will be\n");
Koushik Dutta745b5ff2010-12-21 00:58:43 -08001249 ui_print("named %s. Try restoring it\n", backup_name);
Koushik Duttabec09952010-12-19 20:37:57 -08001250 ui_print("in case of error.\n");
Koushik Dutta4196e0f2010-12-19 03:38:29 -08001251
Koushik Dutta745b5ff2010-12-21 00:58:43 -08001252 nandroid_backup(backup_path);
Koushik Dutta5b7f34a2010-12-29 23:36:03 -08001253 nandroid_restore(backup_path, 1, 1, 1, 1, 1, 0);
Koushik Dutta4196e0f2010-12-19 03:38:29 -08001254 ui_set_show_text(0);
1255}
1256
Koushik Dutta598cfc72010-06-20 09:42:47 -07001257void handle_failure(int ret)
1258{
1259 if (ret == 0)
1260 return;
Koushik Duttadf1e4062010-12-18 17:42:31 -08001261 if (0 != ensure_path_mounted("/sdcard"))
Koushik Dutta598cfc72010-06-20 09:42:47 -07001262 return;
Koushik Duttaa6522b32010-06-20 13:16:06 -07001263 mkdir("/sdcard/clockworkmod", S_IRWXU);
1264 __system("cp /tmp/recovery.log /sdcard/clockworkmod/recovery.log");
Koushik Dutta38e8b2b2010-08-17 22:21:53 -07001265 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 -05001266}
Koushik Duttadf1e4062010-12-18 17:42:31 -08001267
Koushik Duttadf1e4062010-12-18 17:42:31 -08001268int is_path_mounted(const char* path) {
1269 Volume* v = volume_for_path(path);
1270 if (v == NULL) {
1271 return 0;
1272 }
1273 if (strcmp(v->fs_type, "ramdisk") == 0) {
1274 // the ramdisk is always mounted.
1275 return 1;
1276 }
1277
1278 int result;
1279 result = scan_mounted_volumes();
1280 if (result < 0) {
1281 LOGE("failed to scan mounted volumes\n");
1282 return 0;
1283 }
1284
1285 const MountedVolume* mv =
1286 find_mounted_volume_by_mount_point(v->mount_point);
1287 if (mv) {
1288 // volume is already mounted
1289 return 1;
1290 }
1291 return 0;
1292}
Koushik Dutta5460f0c2010-12-18 22:37:49 -08001293
1294int has_datadata() {
1295 Volume *vol = volume_for_path("/datadata");
1296 return vol != NULL;
Koushik Duttad8e21c32011-01-04 10:46:23 -08001297}
1298
1299int volume_main(int argc, char **argv) {
1300 load_volume_table();
1301 return 0;
1302}