forked from Tasssadar/multirom
/
bootmgr.c
834 lines (722 loc) · 21 KB
/
bootmgr.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
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/mount.h>
#include <cutils/memory.h>
#include <dirent.h>
#include <sys/poll.h>
#include <sys/reboot.h>
#include <stdarg.h>
#include <linux/input.h>
#include <linux/kd.h>
#include <pthread.h>
#include <hardware_legacy/power.h>
#include "init.h"
#include "bootmgr.h"
#include "bootmgr_shared.h"
#include "tetris.h"
#include "keywords.h"
#define SD_EXT_BLOCK "/dev/block/mmcblk0p99"
#define SD_FAT_BLOCK "/dev/block/mmcblk0p98"
int8_t bootmgr_selected = 0;
volatile uint8_t bootmgr_input_run = 1;
volatile uint8_t bootmgr_time_run = 0;
volatile uint8_t bootmgr_run = 1;
uint8_t bootmgr_phase = BOOTMGR_MAIN;
uint8_t total_backups = 0;
char *backups[BOOTMGR_BACKUPS_MAX];
uint8_t backups_loaded = 0;
uint8_t backups_has_active = 0;
int8_t selected;
char sleep_mode = 0;
bootmgr_settings_t settings;
uint8_t ums_enabled = 0;
pthread_t t_time;
uint8_t force_update_time = 0;
void bootmgr_start(int charger)
{
settings.default_boot_sd = (char*)malloc(256);
bootmgr_load_settings();
bootmgr_init_display();
bootmgr_set_brightness(settings.brightness);
int key = 0;
int8_t last_selected = -1;
int8_t last_phase = -1;
uint8_t key_pressed = (settings.timeout_seconds == -1);
int16_t timer = settings.timeout_seconds*10;
uint16_t x, y;
uint8_t touch;
selected = -1;
pthread_t t_input;
pthread_create(&t_input, NULL, bootmgr_input_thread, NULL);
bootmgr_set_time_thread(1);
bootmgr_selected = settings.default_boot;
if(charger && (settings.charger_settings & CHARGER_AUTO_START))
{
char status[50];
bootmgr_get_file(battery_status, status, 50);
if(strstr(status, "Charging") == status)
{
key_pressed = 1;
bootmgr_charger_init();
}
}
disable_lg_charger = (settings.charger_settings & CHARGER_DISABLE_LG);
while(bootmgr_run)
{
if(last_selected != bootmgr_selected)
{
bootmgr_draw();
last_selected = bootmgr_selected;
}
if(last_phase != bootmgr_phase)
{
bootmgr_setup_touch();
bootmgr_draw();
last_phase = bootmgr_phase;
}
key = bootmgr_get_last_key();
touch = bootmgr_get_last_touch(&x, &y);
if(key != -1 || touch)
{
if(!key_pressed)
{
bootmgr_erase_text(25);
bootmgr_draw();
key_pressed = 1;
}
if(bootmgr_handle_key(key))
break;
if(touch && !sleep_mode)
{
key = bootmgr_check_touch(x, y);
if(key & TCALL_EXIT_MGR)
break;
}
}
usleep(100000);
if(!key_pressed)
{
if(timer%10 == 0)
{
bootmgr_printf(-1, 25, WHITE, "Boot from %s in %us", bootmgr_selected == 0 ? "internal mem" : "SD card", timer/10);
bootmgr_draw();
}
if(--timer <= 0)
{
bootmgr_erase_text(25);
if(bootmgr_selected == 0)
{
bootmgr_boot_internal();
break;
}
else
{
if(bootmgr_boot_sd_auto())
break;
}
key_pressed = 1;
}
}
}
bootmgr_exit();
}
void bootmgr_exit(void)
{
bootmgr_set_time_thread(0);
bootmgr_destroy_display();
bootmgr_input_run = 0;
free(settings.default_boot_sd);
}
void bootmgr_set_time_thread(uint8_t start)
{
if(start == bootmgr_time_run)
return;
if(start)
{
bootmgr_time_run = 1;
pthread_create(&t_time, NULL, bootmgr_time_thread, NULL);
}
else
{
bootmgr_time_run = 0;
pthread_join(t_time, NULL);
}
}
void *bootmgr_time_thread(void *cookie)
{
time_t tm;
char status[50];
int8_t hours;
int8_t mins;
int battery;
const uint16_t update_val = settings.show_seconds ? 10 : 600;
uint16_t timer = update_val;
while(bootmgr_time_run)
{
if(sleep_mode)
{
usleep(500000);
continue;
}
if(timer == update_val || force_update_time)
{
time(&tm);
battery = bootmgr_get_battery_pct();
bootmgr_get_file(battery_status, status, 50);
// Timezone lame handling
hours = (tm%86400/60/60) + settings.timezone;
mins = tm%3600/60 + settings.timezone_mins;
if (mins >= 60) { mins -= 60; ++hours; }
else if(mins < 0) { mins = 60 - mins; --hours; }
if (hours >= 24) hours -= 24;
else if(hours < 0) hours = 24 + hours;
if(settings.show_seconds)
bootmgr_printf(0, 0, WHITE, "%2u:%02u:%02u Battery: %u%%, %s", hours, mins, tm%60, battery, status);
else
bootmgr_printf(0, 0, WHITE, "%2u:%02u Battery: %u%%, %s", hours, mins, battery, status);
bootmgr_draw();
timer = 0;
force_update_time = 0;
}
usleep(100000);
++timer;
}
return NULL;
}
uint8_t bootmgr_handle_key(int key)
{
if(sleep_mode)
{
bootmgr_do_sleep(0);
return 0;
}
switch(bootmgr_phase)
{
case BOOTMGR_MAIN:
{
switch(key)
{
case KEY_VOLUMEDOWN:
{
if(++bootmgr_selected == 4)
bootmgr_selected = 0;
break;
}
case KEY_VOLUMEUP:
{
if(--bootmgr_selected == -1)
bootmgr_selected = 3;
break;
}
case KEY_BACK:
bootmgr_printf(-1, 25, WHITE, "Rebooting...");
bootmgr_draw();
__reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, "recovery");
return 1;
case KEY_END:
{
bootmgr_do_sleep(!sleep_mode);
break;
}
case KEY_POWER:
{
reboot(RB_POWER_OFF);
return 1;
}
case KEY_MENU:
{
switch(bootmgr_selected)
{
case 0: bootmgr_boot_internal(); return 1;
case 1:
if(bootmgr_show_rom_list())
return 1;
break;
case 2: bootmgr_touch_ums(); break;
case 3: bootmgr_touch_misc(); break;
}
break;
}
case KEY_SEARCH:
{
bootmgr_charger_init();
break;
}
default: break;
}
break;
}
case BOOTMGR_SD_SEL:
{
switch(key)
{
case KEY_VOLUMEDOWN:
bootmgr_touch_sd_down();
break;
case KEY_VOLUMEUP:
bootmgr_touch_sd_up();
break;
case KEY_MENU:
return bootmgr_boot_sd();
case KEY_BACK:
bootmgr_touch_sd_exit();
break;
default:break;
}
break;
}
case BOOTMGR_TETRIS:
{
tetris_key(key);
break;
}
case BOOTMGR_UMS:
{
if(key != KEY_SEARCH)
break;
bootmgr_touch_exit_ums();
break;
}
case BOOTMGR_CHARGER: return bootmgr_charger_key(key);
case BOOTMGR_MISC: return bootmgr_misc_key(key);
}
return 0;
}
void bootmgr_do_sleep(char on)
{
if(sleep_mode == on)
return;
FILE *file = fopen("/sys/power/state", "w");
fputs(on ? "mem" : "on", file);
fclose(file);
usleep(500000);
bootmgr_fill_fb_black();
fb_update(&fb);
usleep(100000);
bootmgr_draw();
sleep_mode = on;
if(!on)
{
bootmgr_reset_input_iters();
force_update_time = 1;
}
}
void bootmgr_select(int8_t line)
{
bootmgr_line *ln = NULL;
if(selected != -1 && (ln = _bootmgr_get_line(selected)))
ln->color = WHITE;
ln = _bootmgr_get_line(line);
if(ln)
ln->color = BLACK;
if(line == -1)
bootmgr_erase_fill(BOOTMGR_FILL_SELECT);
else
bootmgr_print_fill(0, line*ISO_CHAR_HEIGHT, BOOTMGR_DIS_W, ISO_CHAR_HEIGHT, WHITE, BOOTMGR_FILL_SELECT);
selected = line;
}
uint8_t bootmgr_show_rom_list(void)
{
bootmgr_set_time_thread(0);
bootmgr_phase = BOOTMGR_SD_SEL;
bootmgr_display->bg_img = 0;
bootmgr_printf(-1, 20, WHITE, "Mounting sd-ext...");
bootmgr_draw();
if(!backups_loaded)
{
// mknod
mknod(SD_EXT_BLOCK, (0666 | S_IFBLK), makedev(179, 2));
//mkdir
mkdir("/sdroot", (mode_t)0775);
uid_t uid = decode_uid("system");
gid_t gid = decode_uid("system");
chown("/sdroot", uid, gid);
//mount
static const char *mount_args[] = { NULL, "ext4", SD_EXT_BLOCK, "/sdroot" };
int res = do_mount(4, mount_args);
if(res < 0)
{
bootmgr_printf(-1, 20, WHITE, "Failed to mount sd-ext!");
bootmgr_printf(-1, 21, WHITE, "Press back to return.");
return 0;
}
DIR *dir = opendir("/sdroot/multirom/backup");
if(dir)
{
struct dirent * de = NULL;
while ((de = readdir(dir)) != NULL)
{
if (de->d_name[0] == '.')
continue;
backups[total_backups] = (char*)malloc(128);
strcpy(backups[total_backups++], de->d_name);
if(total_backups >= BOOTMGR_BACKUPS_MAX-1)
break;
}
closedir(dir);
backups[total_backups] = NULL;
}
dir = opendir("/sdroot/multirom/rom");
if(dir)
backups_has_active = 1;
}
backups_loaded = 1;
bootmgr_printf(0, 0, (0x3F << 11), "Select ROM to boot. Press back to return");
if(backups_has_active)
{
bootmgr_printf(0, 2, WHITE, "Current active ROM");
bootmgr_select(2);
}
bootmgr_printf(0, 4, (0x3F << 11), "Backup folder:");
uint16_t i = 0;
for(; i <= 25 && i < total_backups; ++i)
bootmgr_printf(0, i + 5, WHITE, "%s", backups[i]);
if(total_backups)
{
if(!backups_has_active)
{
bootmgr_printf(-1, 2, WHITE, "No active ROM");
bootmgr_select(5);
}
bootmgr_erase_text(20);
}
// Useless to print this, because it will be deleted immediately
//else if(backups_has_active)
// bootmgr_printf(-1, 19, WHITE, "No backups present.");
else
{
bootmgr_printf(-1, 20, WHITE, "No active ROM nor backups present.");
bootmgr_printf(-1, 21, WHITE, "Press \"back\" to return");
}
while(bootmgr_get_last_key() != -1); // clear key queue
while(bootmgr_get_last_touch(&i, &i)); // clear touch queue
if(!total_backups && backups_has_active)
return bootmgr_boot_sd();
bootmgr_draw();
return 0;
}
uint8_t bootmgr_boot_sd(void)
{
bootmgr_set_lines_count(0);
bootmgr_set_fills_count(0);
char *path = (char*)malloc(200);
if(selected == 2)
{
bootmgr_printf(-1, 20, WHITE, "Booting from SD-card...");
sprintf(path, "/sdroot/multirom/rom");
}
else
{
sprintf(path, "/sdroot/multirom/backup/%s", backups[selected-5]);
bootmgr_printf(-1, 20, WHITE, "Booting \"%s\"...", backups[selected-5]);
}
selected = -1;
bootmgr_draw();
char *p = (char*)malloc(200);
char *s = (char*)malloc(50);
sprintf(p, "%s/boot", path);
bootmgr_import_boot(p);
char * mount_args[] = { NULL, "ext4", p, s, "bind" };
// /system
sprintf(p, "%s/system", path);
strcpy(s, "/system");
if(do_mount(5, mount_args) < 0)
{
bootmgr_printf(-1, 20, WHITE, "Mount %s failed", mount_args[2]);
bootmgr_draw();
return 0;
}
// /data
sprintf(p, "%s/data", path);
strcpy(s, "/data");
do_mount(5, mount_args);
// /cache
sprintf(p, "%s/cache", path);
strcpy(s, "/cache");
do_mount(5, mount_args);
free(p);
free(s);
free(path);
return 1;
}
void bootmgr_import_boot(char *path)
{
DIR *d = opendir(path);
if(d == NULL)
return;
struct dirent *dp;
char to[100];
char from[100];
// copy init binary
INFO("Copy init binary to ramdisk");
sprintf(from, "%s/init", path);
__copy(from, "/main_init");
chmod("/main_init", 0750);
// /default.prop
sprintf(from, "%s/default.prop", path);
__copy(from, "/default.prop");
// /sbin/adbd
sprintf(from, "%s/adbd", path);
__copy(from, "/sbin/adbd");
while(dp = readdir(d))
{
if(strstr(dp->d_name, ".rc") == NULL)
continue;
// copy to our ramdisk
INFO("Copy %s to ramdisk", dp->d_name);
sprintf(from, "%s/%s", path, dp->d_name);
sprintf(to, "/%s", dp->d_name);
__copy(from, to);
chmod(to, 0750);
}
closedir(d);
}
void bootmgr_load_settings(void)
{
settings.timezone = 0;
settings.timeout_seconds = 3;
settings.show_seconds = 0;
settings.touch_ui = 1;
settings.tetris_max_score = 0;
settings.brightness = 100;
settings.charger_settings = (CHARGER_AUTO_START | CHARGER_DISABLE_LG);
strcpy(settings.default_boot_sd, "");
if(!bootmgr_toggle_sdcard(1, 0))
{
FILE *f = fopen("/sdrt/multirom.txt", "r");
if(f)
{
fseek (f, 0, SEEK_END);
int size = ftell(f);
char *con = (char*)malloc(size+1);
rewind(f);
if(fread(con, 1, size, f))
{
con[size] = 0;
char *p = strtok (con, "=\n");
char *n = p;
for(; p != NULL; n = p)
{
if(!(p = strtok (NULL, "=\n")))
break;
if(strstr(n, "timeout"))
settings.timeout_seconds = atoi(p);
else if(strstr(n, "timezone"))
{
double timezone = atof(p);
settings.timezone = (int8_t)timezone;
settings.timezone_mins = (timezone - settings.timezone)*60;
}
else if(strstr(n, "show_seconds"))
settings.show_seconds = atoi(p);
else if(strstr(n, "touch_ui"))
settings.touch_ui = atoi(p);
else if(strstr(n, "tetris_max_score"))
settings.tetris_max_score = atoi(p);
else if(strstr(n, "brightness"))
settings.brightness = atoi(p);
else if(strstr(n, "default_boot_sd"))
{
char *buff = (char*)malloc(256);
strcpy(buff, p);
char *first = strchr(buff, '"');
if(first == NULL)
{
free(buff);
continue;
}
char *second = strchr(first+1, '"');
if(second == NULL)
{
free(buff);
continue;
}
*second = 0;
strcpy(settings.default_boot_sd, first+1);
free(buff);
}
else if(strstr(n, "default_boot"))
{
settings.default_boot = atoi(p);
if(settings.default_boot > 1)
settings.default_boot = 0;
}
else if(strstr(n, "charger_settings"))
settings.charger_settings = atoi(p);
p = strtok (NULL, "=\n");
}
}
free(con);
fclose(f);
}
}
bootmgr_toggle_sdcard(0, 0);
}
void bootmgr_save_settings(void)
{
if(!bootmgr_toggle_sdcard(1, 0))
{
FILE *f = fopen("/sdrt/multirom.txt", "w");
if(f)
{
float timezone = settings.timezone + settings.timezone_mins/60.f;
fprintf(f, "timeout = %u\r\n", settings.timeout_seconds);
fprintf(f, "timezone = %.2f\r\n", timezone);
fprintf(f, "show_seconds = %u\r\n", (uint8_t)settings.show_seconds);
fprintf(f, "touch_ui = %u\r\n", (uint8_t)settings.touch_ui);
fprintf(f, "tetris_max_score = %u\r\n", settings.tetris_max_score);
fprintf(f, "brightness = %u\r\n", settings.brightness);
fprintf(f, "default_boot = %u\r\n", settings.default_boot);
fprintf(f, "default_boot_sd = \"%s\"\r\n", settings.default_boot_sd);
fprintf(f, "charger_settings = %u\r\b", settings.charger_settings);
fclose(f);
}
}
bootmgr_toggle_sdcard(0, 0);
}
int8_t bootmgr_get_file(char *name, char *buffer, uint8_t len)
{
FILE *f = fopen(name, "r");
if(!f)
return 0;
int res = fread(buffer, 1, len, f);
fclose(f);
if(res > 0)
buffer[res] = 0;
return res;
}
uint8_t bootmgr_toggle_ums(void)
{
bootmgr_printf(-1, 21, WHITE, "%sabling USB mass storage...", ums_enabled ? "dis" : "en");
bootmgr_draw();
sync();
static const char* gadget_files[] =
{
"/sys/devices/platform/msm_hsusb/gadget/lun0/file",
"/sys/devices/platform/usb_mass_storage/lun0/file",
NULL
};
FILE *f = NULL;
uint8_t i = 0;
for(; gadget_files[i] != NULL && f == NULL; ++i)
f = fopen(gadget_files[i], "w+");
if(!f)
{
bootmgr_erase_text(21);
return 0;
}
if(!ums_enabled)
{
bootmgr_toggle_sdcard(1, 1);
fputs(SD_FAT_BLOCK, f);
bootmgr_printf(-1, 20, WHITE, "USB mass storage enabled");
bootmgr_printf(-1, 21, WHITE, "Press \"search\" again to exit");
}
else
{
fputc(0, f);
bootmgr_erase_text(20);
bootmgr_erase_text(21);
bootmgr_toggle_sdcard(0, 1);
}
fclose(f);
bootmgr_display->bg_img = ums_enabled;
bootmgr_draw();
ums_enabled = !ums_enabled;
return 1;
}
int bootmgr_toggle_sdcard(uint8_t on, uint8_t mknod_only)
{
if(on)
{
int res = mknod(SD_FAT_BLOCK, (0666 | S_IFBLK), makedev(179, 1));
if(mknod_only)
return res;
mkdir("/sdrt", (mode_t)0775);
uint8_t i = 0;
for(; i < 20; ++i)
{
res = mount(SD_FAT_BLOCK, "/sdrt", "vfat", 0, NULL);
if(!res)
break;
usleep(500000);
}
return res;
}
else
{
if(!mknod_only)
{
umount("/sdrt");
rmdir("/sdrt");
}
unlink(SD_FAT_BLOCK);
}
return 0;
}
void bootmgr_clear(void)
{
bootmgr_set_lines_count(0);
bootmgr_set_fills_count(0);
bootmgr_set_imgs_count(0);
bootmgr_set_touches_count(0);
}
void bootmgr_set_brightness(uint8_t pct)
{
unsigned short value = 30 + (225*pct)/100;
if(value > 255)
value = 255;
bootmgr_set_brightness_helper(value);
}
void bootmgr_set_brightness_helper(uint16_t value)
{
static const char* BRIGHT_FILE[] =
{
"/sys/devices/platform/i2c-gpio.2/i2c-2/2-0060/leds/lcd-backlight/brightness",
"/sys/devices/platform/i2c-gpio.2/i2c-2/2-0060/leds/lcd-backlight/device/leds/lcd-backlight/brightness",
};
FILE *f = NULL;
char i = 0;
for(; !f && i < 2; ++i)
f = fopen(BRIGHT_FILE[i], "w");
if(!f)
return;
fprintf(f, "%u", value);
fclose(f);
}
void bootmgr_boot_internal(void)
{
bootmgr_printf(-1, 25, WHITE, "Booting from internal memory...");
bootmgr_draw();
}
uint8_t bootmgr_boot_sd_auto(void)
{
if(bootmgr_show_rom_list())
return 1;
if(strlen(settings.default_boot_sd) != 0)
{
uint8_t i = 0;
for(; i <= 25 && i < total_backups; ++i)
{
if(strcmp(backups[i], settings.default_boot_sd) == 0)
{
selected = i+5;
i = 0xFF;
break;
}
}
if(i == 0xFF)
return bootmgr_boot_sd();
}
if(backups_has_active)
{
selected = 2;
return bootmgr_boot_sd();
}
return 0;
}