blob: 50f43066e8766c067516df9026f539069c3437d9 [file] [log] [blame]
Koushik K. Dutta6060e5c2010-02-11 22:27:06 -08001#include <ctype.h>
2#include <errno.h>
3#include <fcntl.h>
4#include <getopt.h>
5#include <limits.h>
6#include <linux/input.h>
7#include <stdio.h>
8#include <stdlib.h>
9#include <string.h>
10#include <sys/reboot.h>
11#include <sys/types.h>
12#include <time.h>
13#include <unistd.h>
14
Koushik K. Duttae9234872010-02-12 00:43:24 -080015#include <sys/wait.h>
16#include <sys/limits.h>
17#include <dirent.h>
Koushik K. Dutta49f56892010-02-25 14:51:05 -080018#include <sys/stat.h>
Koushik K. Duttae9234872010-02-12 00:43:24 -080019
Koushik K. Dutta33370db2010-02-25 11:39:07 -080020#include <signal.h>
21#include <sys/wait.h>
22
Koushik K. Dutta6060e5c2010-02-11 22:27:06 -080023#include "bootloader.h"
24#include "common.h"
25#include "cutils/properties.h"
26#include "firmware.h"
27#include "install.h"
28#include "minui/minui.h"
29#include "minzip/DirUtil.h"
30#include "roots.h"
31#include "recovery_ui.h"
32
Koushik K. Duttaf68aaaf2010-03-07 13:39:21 -080033#include "commands.h"
34#include "amend/amend.h"
35
Koushik K. Duttaa85d7cc2010-03-12 17:00:58 -080036#include "mtdutils/dump_image.h"
37#include "../../external/yaffs2/yaffs2/utils/mkyaffs2image.h"
Koushik K. Dutta54305a82010-03-12 17:43:26 -080038#include "../../external/yaffs2/yaffs2/utils/unyaffs.h"
Koushik K. Duttaa85d7cc2010-03-12 17:00:58 -080039
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -080040#include "extendedcommands.h"
41#include "nandroid.h"
42
Koushik K. Dutta6060e5c2010-02-11 22:27:06 -080043int signature_check_enabled = 1;
44int script_assert_enabled = 1;
Koushik K. Duttabcdd0032010-02-21 21:10:25 -080045static const char *SDCARD_PACKAGE_FILE = "SDCARD:update.zip";
Koushik K. Dutta6060e5c2010-02-11 22:27:06 -080046
47void
48toggle_signature_check()
49{
50 signature_check_enabled = !signature_check_enabled;
51 ui_print("Signature Check: %s\n", signature_check_enabled ? "Enabled" : "Disabled");
52}
53
54void toggle_script_asserts()
55{
56 script_assert_enabled = !script_assert_enabled;
Koushik K. Duttae9234872010-02-12 00:43:24 -080057 ui_print("Script Asserts: %s\n", script_assert_enabled ? "Enabled" : "Disabled");
58}
59
Koushik K. Duttaea46fe22010-03-08 02:58:04 -080060int install_zip(const char* packagefilepath)
Koushik K. Duttae9234872010-02-12 00:43:24 -080061{
Koushik K. Duttabcdd0032010-02-21 21:10:25 -080062 ui_print("\n-- Installing: %s\n", packagefilepath);
63 set_sdcard_update_bootloader_message();
64 int status = install_package(packagefilepath);
Koushik K. Dutta99fb6fe2010-03-03 00:42:58 -080065 ui_reset_progress();
Koushik K. Duttabcdd0032010-02-21 21:10:25 -080066 if (status != INSTALL_SUCCESS) {
67 ui_set_background(BACKGROUND_ICON_ERROR);
68 ui_print("Installation aborted.\n");
Koushik K. Duttaea46fe22010-03-08 02:58:04 -080069 return 1;
Koushik K. Dutta79ce82c2010-02-25 12:03:17 -080070 }
71 if (firmware_update_pending()) {
72 ui_print("\nReboot via menu to complete\ninstallation.\n");
Koushik K. Dutta001c5b52010-02-25 14:53:57 -080073 }
Koushik K. Dutta001c5b52010-02-25 14:53:57 -080074 ui_set_background(BACKGROUND_ICON_NONE);
Koushik K. Dutta99fb6fe2010-03-03 00:42:58 -080075 ui_print("\nInstall from sdcard complete.\n");
Koushik K. Duttaea46fe22010-03-08 02:58:04 -080076 return 0;
Koushik K. Duttabcdd0032010-02-21 21:10:25 -080077}
78
79char* INSTALL_MENU_ITEMS[] = { "apply sdcard:update.zip",
80 "choose zip from sdcard",
81 "toggle signature verification",
82 "toggle script asserts",
83 NULL };
84#define ITEM_APPLY_SDCARD 0
85#define ITEM_CHOOSE_ZIP 1
86#define ITEM_SIG_CHECK 2
87#define ITEM_ASSERTS 3
88
89void show_install_update_menu()
90{
91 static char* headers[] = { "Apply update from .zip file on SD card",
92 "",
93 NULL
94 };
95 for (;;)
96 {
97 int chosen_item = get_menu_selection(headers, INSTALL_MENU_ITEMS, 0);
98 switch (chosen_item)
99 {
100 case ITEM_ASSERTS:
101 toggle_script_asserts();
102 break;
103 case ITEM_SIG_CHECK:
104 toggle_signature_check();
105 break;
106 case ITEM_APPLY_SDCARD:
107 install_zip(SDCARD_PACKAGE_FILE);
108 break;
109 case ITEM_CHOOSE_ZIP:
110 show_choose_zip_menu();
111 break;
112 default:
113 return;
114 }
115
116 }
117}
118
Koushik K. Dutta49f56892010-02-25 14:51:05 -0800119char** gather_files(const char* directory, const char* fileExtensionOrDirectory, int* numFiles)
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800120{
Koushik K. Duttae9234872010-02-12 00:43:24 -0800121 char path[PATH_MAX] = "";
122 DIR *dir;
123 struct dirent *de;
124 int total = 0;
125 int i;
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800126 char** files = NULL;
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800127 int pass;
128 *numFiles = 0;
129 int dirLen = strlen(directory);
Koushik K. Duttae9234872010-02-12 00:43:24 -0800130
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800131 dir = opendir(directory);
132 if (dir == NULL) {
133 ui_print("Couldn't open directory.\n");
134 return NULL;
135 }
136
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800137 int extension_length = 0;
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800138 if (fileExtensionOrDirectory != NULL)
139 extension_length = strlen(fileExtensionOrDirectory);
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800140
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800141 int isCounting = 1;
142 i = 0;
143 for (pass = 0; pass < 2; pass++) {
144 while ((de=readdir(dir)) != NULL) {
145 // skip hidden files
146 if (de->d_name[0] == '.')
147 continue;
148
149 // NULL means that we are gathering directories, so skip this
150 if (fileExtensionOrDirectory != NULL)
151 {
152 // make sure that we can have the desired extension (prevent seg fault)
153 if (strlen(de->d_name) < extension_length)
154 continue;
155 // compare the extension
156 if (strcmp(de->d_name + strlen(de->d_name) - extension_length, fileExtensionOrDirectory) != 0)
157 continue;
158 }
159 else
160 {
161 struct stat info;
162 char* fullFileName = (char*)malloc(strlen(de->d_name) + dirLen + 1);
163 strcpy(fullFileName, directory);
164 strcat(fullFileName, de->d_name);
165 stat(fullFileName, &info);
166 free(fullFileName);
167 // make sure it is a directory
168 if (!(S_ISDIR(info.st_mode)))
169 continue;
170 }
171
172 if (pass == 0)
173 {
174 total++;
175 continue;
176 }
177
Koushik K. Dutta49f56892010-02-25 14:51:05 -0800178 files[i] = (char*) malloc(dirLen + strlen(de->d_name) + 2);
179 strcpy(files[i], directory);
180 strcat(files[i], de->d_name);
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800181 if (fileExtensionOrDirectory == NULL)
182 strcat(files[i], "/");
183 i++;
184 }
185 if (pass == 1)
186 break;
187 if (total == 0)
188 break;
189 rewinddir(dir);
190 *numFiles = total;
191 files = (char**) malloc((total+1)*sizeof(char*));
192 files[total]=NULL;
193 }
Koushik K. Dutta49f56892010-02-25 14:51:05 -0800194
195 if(closedir(dir) < 0) {
196 LOGE("Failed to close directory.");
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800197 }
198
199 if (total==0) {
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800200 return NULL;
201 }
202
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800203 return files;
Koushik K. Dutta49f56892010-02-25 14:51:05 -0800204}
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800205
Koushik K. Dutta49f56892010-02-25 14:51:05 -0800206void free_string_array(char** array)
207{
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800208 char* cursor = array[0];
209 int i = 0;
210 while (cursor != NULL)
211 {
212 free(cursor);
213 cursor = array[++i];
214 }
215 free(array);
Koushik K. Dutta49f56892010-02-25 14:51:05 -0800216}
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800217
Koushik K. Dutta49f56892010-02-25 14:51:05 -0800218// pass in NULL for fileExtensionOrDirectory and you will get a directory chooser
219char* choose_file_menu(const char* directory, const char* fileExtensionOrDirectory, const char* headers[])
220{
221 char path[PATH_MAX] = "";
222 DIR *dir;
223 struct dirent *de;
224 int numFiles = 0;
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800225 int numDirs = 0;
Koushik K. Dutta49f56892010-02-25 14:51:05 -0800226 int i;
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800227
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800228 int dir_len = strlen(directory);
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800229
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800230 char** files = gather_files(directory, fileExtensionOrDirectory, &numFiles);
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800231 char** dirs = NULL;
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800232 if (fileExtensionOrDirectory != NULL)
233 dirs = gather_files(directory, NULL, &numDirs);
234 int total = numDirs + numFiles;
235 if (total == 0)
236 {
237 ui_print("No files found.\n");
238 return NULL;
239 }
240 char** list = (char**) malloc((total + 1) * sizeof(char*));
241 list[total] = NULL;
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800242
Koushik K. Dutta49f56892010-02-25 14:51:05 -0800243
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800244 for (i = 0 ; i < numDirs; i++)
245 {
246 list[i] = strdup(dirs[i] + dir_len);
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800247 }
248
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800249 for (i = 0 ; i < numFiles; i++)
250 {
251 list[numDirs + i] = strdup(files[i] + dir_len);
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800252 }
253
Koushik K. Dutta981b0cd2010-02-22 08:53:34 -0800254 for (;;)
255 {
256 int chosen_item = get_menu_selection(headers, list, 0);
257 if (chosen_item == GO_BACK)
258 break;
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800259 if (chosen_item < numDirs)
260 {
261 char* subret = choose_file_menu(dirs[chosen_item], fileExtensionOrDirectory, headers);
262 if (subret != NULL)
263 return subret;
264 continue;
265 }
Koushik K. Dutta981b0cd2010-02-22 08:53:34 -0800266 static char ret[PATH_MAX];
Koushik K. Dutta49f56892010-02-25 14:51:05 -0800267 strcpy(ret, files[chosen_item - numDirs]);
Koushik K. Dutta981b0cd2010-02-22 08:53:34 -0800268 return ret;
269 }
270 return NULL;
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800271}
272
273void show_choose_zip_menu()
274{
Koushik K. Duttae9234872010-02-12 00:43:24 -0800275 if (ensure_root_path_mounted("SDCARD:") != 0) {
276 LOGE ("Can't mount /sdcard\n");
277 return;
278 }
279
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800280 static char* headers[] = { "Choose a zip to apply",
281 "",
282 NULL
283 };
284
285 char* file = choose_file_menu("/sdcard/", ".zip", headers);
286 if (file == NULL)
287 return;
288 char sdcard_package_file[1024];
289 strcpy(sdcard_package_file, "SDCARD:");
290 strcat(sdcard_package_file, file + strlen("/sdcard/"));
291 install_zip(sdcard_package_file);
292}
293
Koushik K. Dutta33370db2010-02-25 11:39:07 -0800294// This was pulled from bionic: The default system command always looks
295// for shell in /system/bin/sh. This is bad.
296#define _PATH_BSHELL "/sbin/sh"
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800297
Koushik K. Dutta33370db2010-02-25 11:39:07 -0800298extern char **environ;
299int
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800300__system(const char *command)
Koushik K. Dutta33370db2010-02-25 11:39:07 -0800301{
302 pid_t pid;
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800303 sig_t intsave, quitsave;
304 sigset_t mask, omask;
305 int pstat;
306 char *argp[] = {"sh", "-c", NULL, NULL};
Koushik K. Dutta33370db2010-02-25 11:39:07 -0800307
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800308 if (!command) /* just checking... */
309 return(1);
Koushik K. Dutta33370db2010-02-25 11:39:07 -0800310
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800311 argp[2] = (char *)command;
Koushik K. Dutta33370db2010-02-25 11:39:07 -0800312
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800313 sigemptyset(&mask);
314 sigaddset(&mask, SIGCHLD);
315 sigprocmask(SIG_BLOCK, &mask, &omask);
316 switch (pid = vfork()) {
317 case -1: /* error */
318 sigprocmask(SIG_SETMASK, &omask, NULL);
319 return(-1);
320 case 0: /* child */
321 sigprocmask(SIG_SETMASK, &omask, NULL);
322 execve(_PATH_BSHELL, argp, environ);
Koushik K. Dutta33370db2010-02-25 11:39:07 -0800323 _exit(127);
324 }
325
Koushik K. Dutta001c5b52010-02-25 14:53:57 -0800326 intsave = (sig_t) bsd_signal(SIGINT, SIG_IGN);
327 quitsave = (sig_t) bsd_signal(SIGQUIT, SIG_IGN);
328 pid = waitpid(pid, (int *)&pstat, 0);
329 sigprocmask(SIG_SETMASK, &omask, NULL);
330 (void)bsd_signal(SIGINT, intsave);
331 (void)bsd_signal(SIGQUIT, quitsave);
332 return (pid == -1 ? -1 : pstat);
Koushik K. Dutta33370db2010-02-25 11:39:07 -0800333}
334
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800335void show_nandroid_restore_menu()
336{
Koushik K. Dutta54305a82010-03-12 17:43:26 -0800337 if (ensure_root_path_mounted("SDCARD:") != 0) {
338 LOGE ("Can't mount /sdcard\n");
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800339 return;
Koushik K. Dutta54305a82010-03-12 17:43:26 -0800340 }
341
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800342 static char* headers[] = { "Choose an image to restore",
343 "",
344 NULL
345 };
Koushik K. Duttae9234872010-02-12 00:43:24 -0800346
Koushik K. Dutta49f56892010-02-25 14:51:05 -0800347 char* file = choose_file_menu("/sdcard/clockworkmod/backup/", NULL, headers);
Koushik K. Duttabcdd0032010-02-21 21:10:25 -0800348 if (file == NULL)
349 return;
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800350 nandroid_restore(file);
Koushik K. Dutta03173782010-02-26 14:14:23 -0800351}
352
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700353void show_mount_usb_storage_menu()
Koushik K. Dutta03173782010-02-26 14:14:23 -0800354{
Koushik K. Duttaf7215942010-03-16 13:34:51 -0700355 __system("echo /dev/block/mmcblk0 > /sys/devices/platform/usb_mass_storage/lun0/file");
Koushik K. Dutta03173782010-02-26 14:14:23 -0800356 static char* headers[] = { "USB Mass Storage device",
357 "Leaving this menu unmount",
358 "your SD card from your PC.",
359 "",
360 NULL
361 };
362
363 static char* list[] = { "Unmount", NULL };
364
365 for (;;)
366 {
367 int chosen_item = get_menu_selection(headers, list, 0);
368 if (chosen_item == GO_BACK || chosen_item == 0)
369 break;
370 }
371
Koushik K. Duttaf7215942010-03-16 13:34:51 -0700372 __system("echo '' > /sys/devices/platform/usb_mass_storage/lun0/file");
373 __system("echo 0 > /sys/devices/platform/usb_mass_storage/lun0/enable");
Koushik K. Duttae81cb752010-02-26 17:44:33 -0800374}
Koushik K. Duttaf68aaaf2010-03-07 13:39:21 -0800375
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700376#define MOUNTABLE_COUNT 4
377#define MTD_COUNT 3
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700378
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700379void show_partition_menu()
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700380{
381 static char* headers[] = { "Mount and unmount partitions",
382 "",
383 NULL
384 };
385
386 typedef char* string;
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700387 string mounts[MOUNTABLE_COUNT][4] = {
388 { "mount /system", "unmount /system", "format SYSTEM:", "SYSTEM:" },
389 { "mount /data", "unmount /data", "format DATA:", "DATA:" },
390 { "mount /cache", "unmount /cache", "format CACHE:", "CACHE:" },
391 { "mount /sdcard", "unmount /sdcard", NULL, "SDCARD:" }
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700392 };
393
394 for (;;)
395 {
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700396 int ismounted[MOUNTABLE_COUNT];
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700397 int i;
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700398 static string options[MOUNTABLE_COUNT + MTD_COUNT + 1 + 1]; // mountables, format mtds, usb storage, null
399 for (i = 0; i < MOUNTABLE_COUNT; i++)
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700400 {
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700401 ismounted[i] = is_root_path_mounted(mounts[i][3]);
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700402 options[i] = ismounted[i] ? mounts[i][1] : mounts[i][0];
403 }
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700404
405 for (i = 0; i < MTD_COUNT; i++)
406 {
407 options[MOUNTABLE_COUNT + i] = mounts[i][2];
408 }
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700409
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700410 options[MOUNTABLE_COUNT + MTD_COUNT] = "mount USB storage";
411 options[MOUNTABLE_COUNT + MTD_COUNT + 1] = NULL;
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700412
413 int chosen_item = get_menu_selection(headers, options, 0);
414 if (chosen_item == GO_BACK)
415 break;
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700416 if (chosen_item == MOUNTABLE_COUNT + MTD_COUNT)
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700417 {
418 show_mount_usb_storage_menu();
419 }
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700420 else if (chosen_item < MOUNTABLE_COUNT)
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700421 {
422 if (ismounted[chosen_item])
423 {
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700424 if (0 != ensure_root_path_unmounted(mounts[chosen_item][3]))
425 ui_print("Error unmounting %s!\n", mounts[chosen_item][3]);
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700426 }
427 else
428 {
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700429 if (0 != ensure_root_path_mounted(mounts[chosen_item][3]))
430 ui_print("Error mounting %s!\n", mounts[chosen_item][3]);
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700431 }
432 }
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700433 else if (chosen_item < MOUNTABLE_COUNT + MTD_COUNT)
434 {
435 chosen_item = chosen_item - MOUNTABLE_COUNT;
436 }
Koushik K. Duttab9546a82010-03-14 22:42:30 -0700437 }
438}
439
Koushik K. Dutta72a1db62010-03-07 14:10:26 -0800440#define EXTENDEDCOMMAND_SCRIPT "/cache/recovery/extendedcommand"
441
442int extendedcommand_file_exists()
Koushik K. Duttaf68aaaf2010-03-07 13:39:21 -0800443{
Koushik K. Duttaf68aaaf2010-03-07 13:39:21 -0800444 struct stat file_info;
Koushik K. Dutta72a1db62010-03-07 14:10:26 -0800445 return 0 == stat(EXTENDEDCOMMAND_SCRIPT, &file_info);
446}
447
Koushik K. Dutta3836f722010-03-11 22:17:43 -0800448int run_script_from_buffer(char* script_data, int script_len, char* filename)
Koushik K. Dutta72a1db62010-03-07 14:10:26 -0800449{
Koushik K. Duttaf68aaaf2010-03-07 13:39:21 -0800450 /* Parse the script. Note that the script and parse tree are never freed.
451 */
452 const AmCommandList *commands = parseAmendScript(script_data, script_len);
453 if (commands == NULL) {
454 printf("Syntax error in update script\n");
455 return 1;
456 } else {
Koushik K. Dutta72a1db62010-03-07 14:10:26 -0800457 printf("Parsed %.*s\n", script_len, filename);
Koushik K. Duttaf68aaaf2010-03-07 13:39:21 -0800458 }
459
460 /* Execute the script.
461 */
462 int ret = execCommandList((ExecContext *)1, commands);
463 if (ret != 0) {
464 int num = ret;
Koushik K. Duttaee57bbc2010-03-12 23:21:12 -0800465 char *line = NULL, *next = script_data;
Koushik K. Duttaf68aaaf2010-03-07 13:39:21 -0800466 while (next != NULL && ret-- > 0) {
467 line = next;
468 next = memchr(line, '\n', script_data + script_len - line);
469 if (next != NULL) *next++ = '\0';
470 }
471 printf("Failure at line %d:\n%s\n", num, next ? line : "(not found)");
472 return 1;
473 }
474
475 return 0;
476}
Koushik K. Dutta72a1db62010-03-07 14:10:26 -0800477
Koushik K. Dutta3836f722010-03-11 22:17:43 -0800478int run_script(char* filename, int delete_file)
479{
480 struct stat file_info;
481 if (0 != stat(filename, &file_info)) {
482 printf("Error executing stat on file: %s\n", filename);
483 return 1;
484 }
485
486 int script_len = file_info.st_size;
487 char* script_data = (char*)malloc(script_len);
488 FILE *file = fopen(filename, "rb");
489 fread(script_data, script_len, 1, file);
490 fclose(file);
491 if (delete_file)
492 remove(filename);
493
494 return run_script_from_buffer(script_data, script_len, filename);
495}
496
Koushik K. Dutta72a1db62010-03-07 14:10:26 -0800497int run_and_remove_extendedcommand()
498{
Koushik K. Dutta3a25cf52010-03-08 19:22:41 -0800499 int i = 0;
500 for (i = 20; i > 0; i--) {
501 ui_print("Waiting for SD Card to mount (%ds)\n", i);
502 if (ensure_root_path_mounted("SDCARD:") == 0) {
503 ui_print("SD Card mounted...\n");
504 break;
505 }
506 sleep(1);
507 }
508 if (i == 0) {
509 ui_print("Timed out waiting for SD card... continuing anyways.");
510 }
511
Koushik K. Dutta3836f722010-03-11 22:17:43 -0800512 return run_script(EXTENDEDCOMMAND_SCRIPT, 1);
Koushik K. Dutta72a1db62010-03-07 14:10:26 -0800513}
514
515int amend_main(int argc, char** argv)
516{
517 if (argc != 2)
518 {
519 printf("Usage: amend <script>\n");
520 return 0;
521 }
522
523 RecoveryCommandContext ctx = { NULL };
524 if (register_update_commands(&ctx)) {
525 LOGE("Can't install update commands\n");
526 }
Koushik K. Dutta3836f722010-03-11 22:17:43 -0800527 return run_script(argv[1], 0);
Koushik K. Dutta5306db52010-03-08 14:09:35 -0800528}
Koushik K. Dutta5899ac92010-03-19 13:34:36 -0700529
530void show_nandroid_menu()
531{
532 static char* headers[] = { "Nandroid",
533 "",
534 NULL
535 };
536
537 static char* list[] = { "Backup",
538 "Restore",
539 NULL
540 };
541
542 int chosen_item = get_menu_selection(headers, list, 0);
543 switch (chosen_item)
544 {
545 case 0:
546 {
547 struct timeval tp;
548 gettimeofday(&tp, NULL);
549 char backup_path[PATH_MAX];
550 sprintf(backup_path, "/sdcard/clockworkmod/backup/%d", tp.tv_sec);
551 nandroid_backup(backup_path);
552 }
553 break;
554 case 1:
555 show_nandroid_restore_menu();
556 break;
557 }
558}