forked from raidzero/RZrecovery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall_menu.c
638 lines (579 loc) · 14.4 KB
/
install_menu.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <sys/limits.h>
#include <dirent.h>
#include <fcntl.h>
#include <linux/input.h>
#include <unistd.h>
#include "common.h"
#include "minui/minui.h"
#include "recovery_ui.h"
#include "roots.h"
#include "install.h"
#include <string.h>
#include <sys/reboot.h>
#include "nandroid_menu.h"
char** install_list;
int position = 0;
int backup = 0;
int dwipe = 0;
int cwipe = 0;
void set_backup()
{
backup ^= 1;
}
void set_dwipe()
{
dwipe ^= 1;
}
void set_cwipe()
{
cwipe ^= 1;
}
void init_list()
{
if (position == 0)
{
printf("Allocating memory for the queue...\n");
install_list = malloc (50 * sizeof(char*));
int i;
for (i=0; i<50; i++)
{
install_list[i] = malloc(512);
}
}
}
void update_list(int position, char* filename)
{
strcpy(install_list[position], filename);
char *basename = strrchr (filename, '/') + 1;
ui_print("Queued item %s in position %i.\n", basename, position+1);
}
void queue_item(char* filename)
{
if (position < 50 )
{
update_list(position, filename);
position++;
}
else
{
ui_print("Maximum queued items is 25.\n");
}
}
void clear_queue()
{
position = 0;
free(install_list);
ui_print("Install queue cleared!\n");
}
char **
sortlist (char **list, int total)
{
int i = 0;
if (list != NULL)
{
for (i = 0; i < total; i++)
{
int curMax = -1;
int j;
for (j = 0; j < total - i; j++)
{
if (curMax == -1
|| strcmp (list[curMax], list[j]) < 0)
curMax = j;
}
char *temp = list[curMax];
list[curMax] = list[total - i - 1];
list[total - i - 1] = temp;
}
}
return list;
}
void choose_file_menu (char *sdpath)
{
/*
this is just a dummy - to give user somewhere to return to when aborting preinstall/queueing
and hopefully not get them stuck in a loop of iterations equal to the number of times they
entered the preinstall menu
*/
choose_file_menu_final(sdpath);
}
void
choose_file_menu_final (char *sdpath)
{
init_list();
static char *headers[] = { "Choose item", "", NULL
};
char path[PATH_MAX] = "";
DIR * dir;
struct dirent *de;
int total = 0;
int ftotal = 0;
int dtotal = 0;
int i;
int j;
char **flist;
char **dlist;
char **list;
if (ensure_path_mounted ("/sdcard") != 0)
{
LOGE ("Can't mount /sdcard\n");
return;
}
dir = opendir (sdpath);
if (dir == NULL)
{
LOGE ("Couldn't open directory");
LOGE ("Please make sure it exists!");
return;
}
//count the number of valid files:
while ((de = readdir (dir)) != NULL)
{
if (de->d_type == DT_DIR && strcmp (de->d_name, ".") != 0
&& strcmp (de->d_name, "nandroid") != 0)
{
dtotal++;
}
else
{
if (strcmp
(de->d_name + strlen (de->d_name) - 4,
".zip") == 0)
{
ftotal++;
}
if (strcmp
(de->d_name + strlen (de->d_name) - 4,
".tar") == 0)
{
ftotal++;
}
if (strcmp
(de->d_name + strlen (de->d_name) - 4,
".apk") == 0)
{
ftotal++;
}
if (strcmp
(de->d_name + strlen (de->d_name) - 4,
".tgz") == 0)
{
ftotal++;
}
if (strcmp
(de->d_name + strlen (de->d_name) - 7,
"rec.img") == 0)
{
ftotal++;
}
if (strcmp
(de->d_name + strlen (de->d_name) - 8,
"boot.img") == 0)
{
ftotal++;
}
}
}
total = ftotal + dtotal;
if (total == 0)
{
ui_print ("\nNo valid files found!\n");
}
else
{
flist = (char **) malloc ((ftotal + 1) * sizeof (char *));
flist[ftotal] = NULL;
dlist = (char **) malloc ((dtotal + 1) * sizeof (char *));
dlist[dtotal] = NULL;
list = (char **) malloc ((total + 1) * sizeof (char *));
list[total] = NULL;
rewinddir (dir);
i = 0; //dir iterator
j = 0; //file iterator
while ((de = readdir (dir)) != NULL)
{
//create dirs list
if (de->d_type == DT_DIR
&& strcmp (de->d_name, ".") != 0
&& strcmp (de->d_name, "nandroid") != 0)
{
dlist[i] =
(char *) malloc (strlen (sdpath) +
strlen (de->d_name) + 1);
strcpy (dlist[i], sdpath);
strcat (dlist[i], de->d_name);
dlist[i] = (char *) malloc (strlen (de->d_name) + 2); //need one extra byte for the termnitaing char
strcat (de->d_name, "/\0"); //add "/" followed by terminating character since these are dirs
strcpy (dlist[i], de->d_name);
i++;
}
//create files list
if ((de->d_type == DT_REG
&&
(strcmp
(de->d_name + strlen (de->d_name) - 4,
".zip") == 0
|| strcmp (de->d_name + strlen (de->d_name) - 4,
".apk") == 0
|| strcmp (de->d_name + strlen (de->d_name) - 4,
".tar") == 0
|| strcmp (de->d_name + strlen (de->d_name) - 4,
".tgz") == 0
|| strcmp (de->d_name + strlen (de->d_name) - 7,
"rec.img") == 0
|| strcmp (de->d_name + strlen (de->d_name) - 8,
"boot.img") == 0)))
{
flist[j] =
(char *) malloc (strlen (sdpath) +
strlen (de->d_name) + 1);
strcpy (flist[j], sdpath);
strcat (flist[j], de->d_name);
flist[j] =
(char *) malloc (strlen (de->d_name) + 1);
strcpy (flist[j], de->d_name);
j++;
}
} if (closedir (dir) < 0)
{
LOGE ("Failure closing directory\n");
return;
}
//sort lists
sortlist (flist, ftotal);
sortlist (dlist, dtotal);
//join the file and dir list, with dirs on top - thanks cvpcs
i = 0;
j = 0;
int k;
for (k = 0; k < total; k++)
{
if (i < dtotal)
{
list[k] = strdup (dlist[i++]);
}
else
{
list[k] = strdup (flist[j++]);
}
}
int chosen_item = -1;
while (chosen_item < 0)
{
chosen_item =
get_menu_selection (headers, list, 0,
chosen_item <
0 ? 0 : chosen_item);
if (chosen_item >= 0 && chosen_item != ITEM_BACK)
{
char install_string[PATH_MAX]; //create real path from selection
strcpy (install_string, sdpath);
strcat (install_string, list[chosen_item]);
if (opendir (install_string) == NULL)
{ //handle selection
return preinstall_menu (install_string);
}
else
{
return choose_file_menu (install_string);
}
}
}
}
}
int
install_img (char *filename, char *partition)
{
ui_print ("\n-- Flash image from...\n");
ui_print ("%s", filename);
ui_print ("\n");
ensure_path_mounted ("/sdcard");
char *argv[] = { "/sbin/flash_image", partition, filename, NULL
};
char *envp[] = { NULL };
int status = runve ("/sbin/flash_image", argv, envp, 1);
ui_print ("\nFlash from sdcard complete.");
ui_print ("\nThanks for using RZrecovery.\n");
return 0;
}
int
install_apk (char *filename, char *location)
{
ui_print ("\n-- Installing APK from\n");
ui_print ("%s", filename);
ui_print ("\nTo\n");
ensure_path_mounted ("/sdcard");
ensure_path_mounted (location);
char locString[PATH_MAX];
char *basename = strrchr (filename, '/') + 1;
strcpy (locString, location);
strcat (locString, basename);
ui_print ("%s", locString);
char *permString;
if (strcmp (location, "/data/app/") == 0)
permString = "1000:1000";
if (strcmp (location, "/system/app/") == 0)
permString = "0:0";
char *argv[] = { "/sbin/busybox ", "cp", filename, location, NULL
};
char *envp[] = { NULL };
runve ("/sbin/busybox", argv, envp, 1);
char *argy[] =
{ "/sbin/busybox ", "chown", permString, locString, NULL
};
char *envy[] = { NULL };
runve ("/sbin/busybox", argy, envy, 1);
ui_print ("\nAPK install from sdcard complete.");
ui_print ("\nThanks for using RZrecovery.\n");
return 0;
}
void
install_update_package (char *filename)
{
char *extension = (filename + strlen (filename) - 3);
char *boot_extension = (filename + strlen (filename) - 8);
char *rec_extension = (filename + strlen (filename) - 7);
char *apk_extension = (filename + strlen (filename) - 3);
printf("Position: %i\n", position);
if (backup) nandroid("backup", "preinstall", DEFAULT, 1, 0); backup = 0;; //reset it after the first run
if (dwipe) wipe_partition("cache", 1); dwipe = 0;
if (cwipe) wipe_partition("data", 1); cwipe = 0;
if (strcmp (extension, "zip") == 0)
{
ui_print ("\nZIP detected.\n");
remove ("/block_update");
install_update_zip (filename);
}
if (strcmp (extension, "tar") == 0 || strcmp (extension, "tgz") == 0)
{
ui_print ("\nTAR detected.\n");
install_rom_from_tar (filename);
}
if (strcmp (apk_extension, "apk") == 0)
{
ui_print ("\nAPK detected.\n");
preinstall_apk (filename, "recovery");
}
if (strcmp (rec_extension, "rec.img") == 0)
{
ui_print ("\nRECOVERY IMAGE detected.\n");
install_img (filename, "recovery");
}
if (strcmp (boot_extension, "boot.img") == 0)
{
ui_print ("\nBOOT IMAGE detected.\n");
install_img (filename, "boot");
}
}
void install_queued_items()
{
int i;
char* filename;
for (i=0; i<position; i++)
{
filename = install_list[i];
ui_print("Installing item %i of %i from queue. \n", i+1, position);
install_update_package(filename);
}
clear_queue();
}
int
install_update_zip (char *filename)
{
char *path = NULL;
puts (filename);
ui_print ("\n-- Install update.zip from sdcard...\n");
//set_sdcard_update_bootloader_message ();
// ah ha! so THAT was the problem!
ui_print ("Attempting update from...\n");
ui_print ("%s", filename);
ui_print ("\n");
int status = install_package (filename);
if (status != INSTALL_SUCCESS)
{
ui_print ("Installation aborted.\n");
}
else
{
ui_print ("\nInstall from sdcard complete.\n");
ui_print ("\nThanks for using RZrecovery.\n");
}
return 0;
}
int
install_rom_from_tar (char *filename)
{
ui_print ("Attempting to install ROM from ");
ui_print ("%s", filename);
ui_print ("...\n");
char *argv[] =
{ "/sbin/nandroid-mobile.sh", "--install-rom", filename, "--progress",
NULL
};
char *envp[] = { NULL };
int status = runve ("/sbin/nandroid-mobile.sh", argv, envp, 1);
if (!WIFEXITED (status) || WEXITSTATUS (status) != 0)
{
ui_printf_int ("ERROR: install exited with status %d\n",
WEXITSTATUS (status));
__system("cd /tmp; for files in `find . | grep -v \"recovery\.log\"`; do rm -rf $files; done");
return WEXITSTATUS (status);
}
else
{
ui_print ("(done)\n");
}
__system("cd /tmp; for files in `find . | grep -v \"recovery\.log\"`; do rm -rf $files; done");
ui_reset_progress ();
return 0;
}
void
preinstall_apk (char *filename)
{
char *basename = strrchr (filename, '/') + 1;
char install_string[PATH_MAX];
char *location;
strcpy (install_string, "Install ");
strcat (install_string, basename);
char *headers[] =
{ "APK Install Menu", "Where will this APK go?", " ", NULL
};
char *items[] =
{ "Abort APK Install", "/data/app", "/system/app", NULL
};
#define ITEM_NO 0
#define ITEM_DATA 1
#define ITEM_SYSTEM 2
int chosen_item = -1;
while (chosen_item != ITEM_BACK)
{
chosen_item =
get_menu_selection (headers, items, 1,
chosen_item < 0 ? 0 : chosen_item);
switch (chosen_item)
{
case ITEM_NO:
chosen_item = ITEM_BACK;
return;
case ITEM_DATA:
ui_print ("Installing to DATA...\n");
location = "/data/app/";
install_apk (filename, location);
return;
case ITEM_SYSTEM:
ui_print ("Installing to SYSTEM...\n");
location = "/system/app/";
install_apk (filename, location);
return;
}
}
}
void
get_preinstall_menu_opts (char **preinstall_opts, int reboot_into_android)
{
char **tmp = malloc (5 * sizeof (char *));
int i;
for (i=0; i<4; i++)
{
tmp[i] = malloc (strlen ("(*) Backup before install") * sizeof (char));
}
sprintf (tmp[0], "(%c) Backup before install", backup ? '*':' ');
sprintf (tmp[1], "(%c) Wipe cache", cwipe ? '*':' ');
sprintf (tmp[2], "(%c) Wipe data", dwipe ? '*':' ');
sprintf (tmp[3], "(%c) Reboot afterwards", reboot_into_android ? '*':' ');
tmp[4] = NULL;
char **h = preinstall_opts;
char **j = tmp;
for (; *j; j++, h++)
*h = *j;
}
void
preinstall_menu (char *filename)
{
int reboot_into_android = 0;
char *basename = strrchr (filename, '/') + 1;
char *current_basename = strrchr (filename, '/') + 1;
char install_string[PATH_MAX];
if (position > 0 )
{
ui_print("\nCurrent queue: \n");
basename = "item(s) now";
int z;
char* install_list_base;
for (z=0; z<position; z++)
{
install_list_base = strrchr (install_list[z], '/') + 1;
ui_print("%i: %s\n", z+1, install_list_base);
}
ui_print("%i: %s\n", position+1, current_basename);
}
strcpy (install_string, "Install ");
strcat (install_string, basename);
char *headers[] =
{ "Preinstall Menu", "Please make your selections.", " ", NULL
};
char *preinstall_opts[] =
{
NULL,
NULL,
NULL,
NULL,
"Add to install queue",
"Clear install queue",
"Abort install",
install_string,
NULL
};
#define ITEM_BACKUP 0
#define ITEM_CWIPE 1
#define ITEM_DWIPE 2
#define ITEM_REBOOT 3
#define ITEM_QUEUE 4
#define ITEM_CLEAR 5
#define ITEM_NO 6
#define ITEM_INSTALL 7
int chosen_item = -1;
while (chosen_item != ITEM_BACK)
{
get_preinstall_menu_opts (preinstall_opts, reboot_into_android);
chosen_item = get_menu_selection (headers, preinstall_opts, 0, chosen_item < 0 ? 0 : chosen_item);
switch (chosen_item)
{
case ITEM_NO:
choose_file_menu("/sdcard/");
return;
case ITEM_BACKUP:
set_backup();
break;
case ITEM_DWIPE:
set_dwipe();
break;
case ITEM_CWIPE:
set_cwipe();
break;
case ITEM_QUEUE:
queue_item(filename);
choose_file_menu("/sdcard/");
return;
case ITEM_CLEAR:
clear_queue();
choose_file_menu("/sdcard/");
return;
case ITEM_INSTALL:
if (position > 0) install_queued_items();
install_update_package (filename);
if (reboot_into_android)
{
reboot_android();
}
return;
case ITEM_REBOOT:
reboot_into_android ^= 1;
break;
}
}
}