forked from Tasssadar/multirom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
multirom.c
3041 lines (2557 loc) · 83.7 KB
/
multirom.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
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This file is part of MultiROM.
*
* MultiROM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* MultiROM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with MultiROM. If not, see <http://www.gnu.org/licenses/>.
*/
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/mount.h>
#include <sys/klog.h>
#include <linux/loop.h>
#include <ctype.h>
#include <unistd.h>
// clone libbootimg to /system/extras/ from
// https://github.com/Tasssadar/libbootimg.git
#include <libbootimg.h>
#if LIBBOOTIMG_VERSION < 0x000200
#error "libbootimg version 0.2.0 or higher is required. Please update libbootimg."
#endif
#include "lib/containers.h"
#include "lib/framebuffer.h"
#include "lib/inject.h"
#include "lib/input.h"
#include "lib/log.h"
#include "lib/util.h"
#include "lib/mrom_data.h"
#include "multirom.h"
#include "multirom_ui.h"
#include "version.h"
#include "hooks.h"
#include "rom_quirks.h"
#include "kexec.h"
#define REALDATA "/realdata"
#define BUSYBOX_BIN "busybox"
#define KEXEC_BIN "kexec"
#define NTFS_BIN "ntfs-3g"
#define EXFAT_BIN "exfat-fuse"
#define INTERNAL_ROM_NAME "Internal"
#define MAX_ROM_NAME_LEN 26
#define LAYOUT_VERSION "/data/.layout_version"
#define BATTERY_CAP "/sys/class/power_supply/battery/capacity"
static char busybox_path[64] = { 0 };
static char kexec_path[64] = { 0 };
static char ntfs_path[64] = { 0 };
static char exfat_path[64] = { 0 };
static char partition_dir[64] = { 0 };
static volatile int run_usb_refresh = 0;
static pthread_t usb_refresh_thread;
static pthread_mutex_t parts_mutex = PTHREAD_MUTEX_INITIALIZER;
static void (*usb_refresh_handler)(void) = NULL;
int multirom_find_base_dir(void)
{
int i;
struct stat info;
static const char *paths[] = {
REALDATA"/media/0/multirom", // 4.2
REALDATA"/media/multirom",
"/data/media/0/multirom",
"/data/media/multirom",
NULL,
};
for(i = 0; paths[i]; ++i)
{
if(stat(paths[i], &info) < 0)
continue;
mrom_set_dir(paths[i]);
strncpy(partition_dir, paths[i], strchr(paths[i]+1, '/') - paths[i]);
sprintf(busybox_path, "%s/%s", paths[i], BUSYBOX_BIN);
sprintf(kexec_path, "%s/%s", paths[i], KEXEC_BIN);
sprintf(ntfs_path, "%s/%s", paths[i], NTFS_BIN);
sprintf(exfat_path, "%s/%s", paths[i], EXFAT_BIN);
chmod(kexec_path, 0755);
chmod(ntfs_path, 0755);
chmod(exfat_path, 0755);
return 0;
}
return -1;
}
#define MAX_LASTKMSG_LOGS 3
#define MAX_MROMKMSG_LOGS 5
enum
{
BACKUP_LAST_KMSG = 0,
BACKUP_EARLY_KLOG = 1,
BACKUP_LATE_KLOG = 2,
BACKUP_LAST_KEXEC = 3
};
void multirom_kmsg_rename_logs(const char *path_logs_dir, const char *base_name, const int max_count)
{
DIR *dp = opendir(path_logs_dir);
if (!dp)
return;
int i;
char path_file_1[256];
char path_file_2[256];
char tmp[5];
char *pos = NULL;
struct dirent *de = NULL;
// delete last one (max_count), and rename the older ones
while((de = readdir(dp)))
{
if (strncmp(de->d_name, base_name, strlen(base_name)) != 0)
continue;
sprintf(path_file_1, "%s/%s", path_logs_dir, de->d_name);
sprintf(path_file_2, "%s/%s", path_logs_dir, de->d_name);
for (i = max_count; i >= 0; i--)
{
sprintf(tmp, "_%i_", i);
pos = strstr(path_file_2, tmp);
if (pos != NULL)
{
sprintf(tmp, "_%i_", i+1);
strncpy(pos, tmp, strlen(tmp));
if (i == max_count)
INFO("Deleting oldest log '%s' res=%d\n", path_file_1, remove(path_file_1));
else
INFO("Renaming older log '%s' to '%s' res=%d\n", path_file_1, path_file_2, rename(path_file_1, path_file_2));
}
}
}
closedir(dp);
}
void multirom_kmsg_logging(int kmsg_backup_type)
{
// types of logging:
// BACKUP_LAST_KMSG -> backup last_kmsg
// BACKUP_EARLY_KMSG -> current klog upon entering mrom
// BACKUP_LATE_KMSG -> current klog upon exiting mrom
// BACKUP_LAST_KEXEC -> last kexec log
char path_logs_dir[256];
char path_log_file[256];
static const char *kmsg_paths[] = {
"/proc/last_kmsg",
"/sys/fs/pstore/console-ramoops",
NULL,
};
// make the logs folder visible outside multirom folder
static const char log_dir_name[] = "../multirom-klogs";
static const char ext[] = "txt";
// current date time
char datetime[] = "yyyy-mm-dd-HHMMSS";
time_t rawtime = time(NULL);
struct tm *timeinfo = localtime(&rawtime);
strftime(datetime, sizeof(datetime), "%Y-%m-%d-%H%M%S", timeinfo);
// filename: last_kmsg_n_2016-03-11-153600.txt
// filename: early_klg_0_2016-03-11-153600.txt (this is current klog upon enterting mrom)
// filename: mrom_klog_n_2016-03-11-153600.txt (this is current klog upon exiting mrom)
// filename: last_kexec_0_2016-03-11-153600.txt (same klog as pulled in multirom_load_kexec function)
char base_name[15];
if (kmsg_backup_type == BACKUP_LAST_KMSG)
strcpy(base_name, "last_kmsg");
else if (kmsg_backup_type == BACKUP_EARLY_KLOG)
strcpy(base_name, "early_klg");
else if (kmsg_backup_type == BACKUP_LATE_KLOG)
strcpy(base_name, "mrom_klog");
else if (kmsg_backup_type == BACKUP_LAST_KEXEC)
strcpy(base_name, "last_kexec");
else
return;
// set path and create if needed
sprintf(path_logs_dir, "%s/%s", mrom_dir(), log_dir_name);
mkdir(path_logs_dir, 0777);
if (kmsg_backup_type == BACKUP_LAST_KMSG)
{
int i;
multirom_kmsg_rename_logs(path_logs_dir, base_name, MAX_LASTKMSG_LOGS);
// now copy the last kernel msg
sprintf(path_log_file, "%s/%s_%i_%s.%s", path_logs_dir, base_name, 0, datetime, ext);
for(i = 0; kmsg_paths[i]; ++i)
{
if (access(kmsg_paths[i], R_OK) == 0)
{
INFO("Backing up last kmsg '%s' to '%s' res=%d\n", kmsg_paths[i], path_log_file, copy_file(kmsg_paths[i], path_log_file));
break;
}
}
}
else
{
multirom_kmsg_rename_logs(path_logs_dir, base_name, (kmsg_backup_type == BACKUP_LATE_KLOG) ? MAX_MROMKMSG_LOGS : 0);
// now copy current klog
sprintf(path_log_file, "%s/%s_%i_%s.%s", log_dir_name, base_name, 0, datetime, ext);
INFO("Backing up current klog to '%s' res=%d\n", path_log_file, multirom_copy_log(NULL, path_log_file));
}
}
#ifdef MR_ALLOW_NKK71_NOKEXEC_WORKAROUND
struct struct_nokexec nokexec_s;
struct struct_nokexec * nokexec(void)
{
return &nokexec_s;
}
// functions used by workaround
int nokexec_set_struct(struct multirom_status *s)
{
char path[256];
int has_kexec;
memset(&nokexec_s, 0, sizeof(struct struct_nokexec));
// primary boot.img backup
sprintf(path, "%s/%s", mrom_dir(), "primary_boot.img");
nokexec()->path_primary_bootimg = strdup(path);
INFO("nkk71: primary_boot.img location will be=%s\n", nokexec()->path_primary_bootimg);
// find boot mmcblk
nokexec()->path_boot_mmcblk = nokexec_find_boot_mmcblk_path(s);
// set flags
nokexec()->is_allowed = s->allow_nkk71_nokexec & NO_KEXEC_ALLOWED;
nokexec()->is_ask_confirm = s->allow_nkk71_nokexec & NO_KEXEC_CONFIRM;
nokexec()->is_ask_choice = s->allow_nkk71_nokexec & NO_KEXEC_CHOICE;
nokexec()->is_forced = s->allow_nkk71_nokexec & NO_KEXEC_FORCED;
nokexec()->is_disabled = !(nokexec()->is_allowed || nokexec()->is_ask_confirm || nokexec()->is_ask_choice || nokexec()->is_forced);
has_kexec = multirom_has_kexec();
if (!has_kexec)
nokexec()->selected_method = NO_KEXEC_BOOT_NOKEXEC;
else if (nokexec()->is_forced)
nokexec()->selected_method = NO_KEXEC_BOOT_NOKEXEC;
else
nokexec()->selected_method = NO_KEXEC_BOOT_KEXEC;
// not implemented
nokexec()->is_allow_kexec_primary = s->allow_nkk71_nokexec & NO_KEXEC_PRIMARY;
nokexec()->is_always_restore_primary = s->allow_nkk71_nokexec & NO_KEXEC_RESTORE;
return 1;
}
void nokexec_free_struct(void)
{
if (nokexec()->path_primary_bootimg) free(nokexec()->path_primary_bootimg);
if (nokexec()->path_boot_mmcblk) free(nokexec()->path_boot_mmcblk);
//if (nokexec_s) free(nokexec_s);
}
char *nokexec_find_boot_mmcblk_path(struct multirom_status *s)
{
struct fstab_part *boot;
INFO("nkk71: locating boot partition...\n");
boot = fstab_find_first_by_path(s->fstab, "/boot");
if (boot)
INFO("nkk71: found boot at '%s'\n", boot->device);
else
{
INFO("nkk71: not found in fstab, try looking at mrom.fstab...\n");
struct fstab *mrom_fstab;
char path_mrom_fstab[256];
sprintf(path_mrom_fstab, "%s/%s", mrom_dir(), "mrom.fstab");
mrom_fstab = fstab_load(path_mrom_fstab, 1);
if (!mrom_fstab)
{
ERROR("nkk71: couldn't load mrom.fstab '%s'\n", path_mrom_fstab);
return NULL;
}
boot = fstab_find_first_by_path(mrom_fstab, "/boot");
if (boot)
INFO("nkk71: found boot (using mrom.fstab) at '%s'\n", boot->device);
else
return NULL;
}
return strdup(boot->device);
}
int nokexec_cleanup(void)
{
int res = 0;
if (access(nokexec()->path_primary_bootimg, R_OK) == 0)
INFO("nkk71: deleting primary boot.img, the backup is no longer needed; res=%d\n", res |= remove(nokexec()->path_primary_bootimg));
// legacy
char path_last2nd_bootimg[256];
sprintf(path_last2nd_bootimg, "%s/%s", mrom_dir(), "last_rom_was_2nd");
if (access(path_last2nd_bootimg, R_OK) == 0)
INFO("nkk71: deleting info 'last_rom_was_2nd'; res=%d\n", res |= remove(path_last2nd_bootimg));
return res;
}
int nokexec_set_secondary_flag(void)
{
int res = -1;
struct bootimg img;
// make note that the primary slot now contains a secondary boot.img
// by tagging the BOOT_NAME at the very end, even after a null terminated "tr_verNN" string
INFO("nkk71: Going to tag the bootimg in primary slot as a secondary\n");
if (libbootimg_init_load(&img, nokexec()->path_boot_mmcblk, LIBBOOTIMG_LOAD_ALL) < 0)
{
ERROR("nkk71: Could not open boot image (%s)!\n", nokexec()->path_boot_mmcblk);
return -1;
}
// Update the boot.img
img.hdr.name[BOOT_NAME_SIZE-1] = 0x71;
INFO("nkk71: Writing boot.img updated with secondary flag set\n");
if (libbootimg_write_img(&img, nokexec()->path_boot_mmcblk) < 0)
{
ERROR("Failed to libbootimg_write_img!\n");
}
else
res = 0;
libbootimg_destroy(&img);
return res;
}
int nokexec_backup_primary(void)
{
int res;
INFO("nkk71: backing up primary boot.img; res=%d\n", res = copy_file(nokexec()->path_boot_mmcblk, nokexec()->path_primary_bootimg));
return res;
}
int nokexec_flash_to_primary(const char * source)
{
int res;
INFO("nkk71: flashing '%s' to boot partition; res=%d\n", source, res = copy_file(source, nokexec()->path_boot_mmcblk));
if (res == 0)
{
// Trampolines in ROM boot images may get out of sync, so we need to check it and
// update if needed. I can't do that during ZIP installation because of USB drives.
if(inject_bootimg(nokexec()->path_boot_mmcblk, 0) < 0)
{
ERROR("nkk71: Failed to inject bootimg!\n");
res = -1;
}
}
return res;
}
int nokexec_is_secondary_in_primary(void)
{
int res = 0;
struct bootimg img;
if (libbootimg_init_load(&img, nokexec()->path_boot_mmcblk, LIBBOOTIMG_LOAD_ALL) < 0)
{
ERROR("nkk71: Could not open boot image (%s)!\n", nokexec()->path_boot_mmcblk);
}
else
{
if (img.hdr.name[BOOT_NAME_SIZE-1] == 0x71)
res = 1;
libbootimg_destroy(&img);
}
INFO("nkk71: Checking the primary slot bootimg for the secondary tag; res=%d\n", res);
return res;
}
int nokexec_is_new_primary(void)
{
int is_new = 0;
// check if it even exists, otherwise it's "new" (1)
if (access(nokexec()->path_primary_bootimg, R_OK) != 0)
is_new = 1;
// if the current bootimg is not tagged, then it's new (2)
else if (!nokexec_is_secondary_in_primary())
is_new = 2;
INFO("nkk71: Checking if primary is new; is_new=%d\n", is_new);
return is_new;
}
#endif //MR_ALLOW_NKK71_NOKEXEC_WORKAROUND
int multirom(const char *rom_to_boot)
{
if(multirom_find_base_dir() == -1)
{
ERROR("Could not find multirom dir\n");
return -1;
}
struct multirom_status s;
memset(&s, 0, sizeof(struct multirom_status));
multirom_load_status(&s);
multirom_dump_status(&s);
if (s.enable_kmsg_logging != 0)
{
multirom_kmsg_logging(BACKUP_LAST_KMSG);
multirom_kmsg_logging(BACKUP_EARLY_KLOG);
}
struct multirom_rom *to_boot = NULL;
int exit = (EXIT_REBOOT | EXIT_UMOUNT);
#ifdef MR_ALLOW_NKK71_NOKEXEC_WORKAROUND
// setup global variables here
nokexec_set_struct(&s);
// check if a partition mount failed, in which case multirom usually just boots internal
if (s.is_second_boot != 0 && s.current_rom->type == ROM_DEFAULT && nokexec_is_secondary_in_primary())
{
ERROR("nkk71: Something went wrong in mounting the needed partition, so falling back...");
nokexec()->selected_method = NO_KEXEC_BOOT_PRIMARY;
s.is_second_boot = 0;
//ERROR("to Internal\n"); s.auto_boot_type = AUTOBOOT_FORCE_CURRENT; // force reboot to Internal (this would be mrom default behaviour)
//ERROR("to MultiROM GUI\n"); s.auto_boot_type = AUTOBOOT_NAME; // bring up the gui instead
ERROR("to Recovery\n"); exit = (EXIT_REBOOT_RECOVERY | EXIT_UMOUNT); goto finish; // reboot to recovery
}
#endif
if(rom_to_boot != NULL)
{
struct multirom_rom *rom = multirom_get_rom(&s, rom_to_boot, NULL);
if(rom)
{
#ifdef MR_ALLOW_NKK71_NOKEXEC_WORKAROUND
// nkk71: check for kexec support, otherwise use workaround
if ((nokexec()->selected_method == NO_KEXEC_BOOT_NOKEXEC) || (nokexec()->selected_method == NO_KEXEC_BOOT_PRIMARY))
{
to_boot = rom;
s.is_second_boot = 0;
INFO("nkk71: Re-Booting ROM %s...\n", rom_to_boot);
}
else
#endif
{
// Two possible scenarios: this ROM has kexec-hardboot and target
// ROM has boot image, so kexec it immediatelly or
// reboot and then proceed as usuall
if(((M(rom->type) & MASK_KEXEC) || rom->has_bootimg) && rom->type != ROM_DEFAULT && multirom_has_kexec())
{
to_boot = rom;
s.is_second_boot = 0;
INFO("Booting ROM %s...\n", rom_to_boot);
}
else
{
s.current_rom = rom;
s.auto_boot_type |= AUTOBOOT_FORCE_CURRENT;
INFO("Setting ROM %s to force autoboot\n", rom_to_boot);
}
}
}
else
{
ERROR("ROM %s was not found, force autoboot was not set!\n", rom_to_boot);
exit = EXIT_UMOUNT;
}
}
else if(s.is_second_boot != 0 || (s.auto_boot_type & AUTOBOOT_FORCE_CURRENT))
{
ERROR("Skipping ROM selection, is_second_boot=%d, auto_boot_type=0x%x\n", s.is_second_boot, s.auto_boot_type);
to_boot = s.current_rom;
}
else
{
// just to cache the result so that it does not take
// any time when the UI is up
multirom_has_kexec();
switch(multirom_ui(&s, &to_boot))
{
case UI_EXIT_BOOT_ROM: break;
case UI_EXIT_REBOOT:
exit = (EXIT_REBOOT | EXIT_UMOUNT);
break;
case UI_EXIT_REBOOT_RECOVERY:
exit = (EXIT_REBOOT_RECOVERY | EXIT_UMOUNT);
break;
case UI_EXIT_REBOOT_BOOTLOADER:
exit = (EXIT_REBOOT_BOOTLOADER | EXIT_UMOUNT);
break;
case UI_EXIT_SHUTDOWN:
exit = (EXIT_SHUTDOWN | EXIT_UMOUNT);
break;
}
}
if(to_boot)
{
s.auto_boot_type &= ~(AUTOBOOT_FORCE_CURRENT);
if(rom_to_boot == NULL)
multirom_run_scripts("run-on-boot", to_boot);
#ifdef MR_ALLOW_NKK71_NOKEXEC_WORKAROUND
//==========================================================================================================================
// nkk71: check for kexec support, otherwise use workaround
if ((nokexec()->selected_method == NO_KEXEC_BOOT_NOKEXEC) || (nokexec()->selected_method == NO_KEXEC_BOOT_PRIMARY))
{
// nkk71: workaround for lack of kexec-hb support:
// flash correct boot.img to primary slot,
// initiate a reboot, and autoboot the ROM
#define NKK71_ABORT { \
ERROR("nkk71: ERROR occurred in the above, aborting to recovery\n"); \
exit = (EXIT_REBOOT_RECOVERY | EXIT_UMOUNT); goto finish; \
}
// now the real workaround
if (s.is_second_boot == 0)
{
// make sure all the paths are set up, otherwise abort
if (!nokexec()->path_boot_mmcblk || !nokexec()->path_primary_bootimg) NKK71_ABORT
if (to_boot->type != ROM_DEFAULT)
{
char path_bootimg[256];
if (nokexec_is_new_primary()) nokexec_backup_primary();
// now flash the secondary boot.img to primary slot
sprintf(path_bootimg, "%s/%s", to_boot->base_path, "boot.img");
if (nokexec_flash_to_primary(path_bootimg)) NKK71_ABORT
// make note that the primary slot now contains a secondary boot.img
if (nokexec_set_secondary_flag()) NKK71_ABORT
}
else
{
// we're booting into primary
if (nokexec_is_secondary_in_primary())
{
// check if primary_boot.img exits , if it does then restore it
if (access(nokexec()->path_primary_bootimg, R_OK) == 0)
{
if (nokexec_flash_to_primary(nokexec()->path_primary_bootimg)) NKK71_ABORT
}
else
{
ERROR("nkk71: no primary boot.img was found, so cannot restore it\n");
// NOTE: theoretically we could try booting into primary, even with a secondary
// boot.img installed, if it's compatible it will boot, but we're not going to
NKK71_ABORT
}
}
// remove no longer needed files, in case of error, just log it
if(nokexec_cleanup())
ERROR("nkk71: WARNING: error in no_kexec_cleanup\n");
}
}
}
else if (s.is_second_boot == 0)
{
INFO("nkk71: kexec support is present so delete files used by workaround if they are there (thx CPTB)\n");
if(nokexec_cleanup())
ERROR("nkk71: WARNING: error in no_kexec_cleanup\n");
}
//==========================================================================================================================
#endif
exit = multirom_prepare_for_boot(&s, to_boot);
// Something went wrong, exit/reboot
if(exit == -1)
{
if(rom_to_boot == NULL)
{
multirom_emergency_reboot();
exit = EXIT_REBOOT;
}
else
exit = EXIT_UMOUNT;
goto finish;
}
s.current_rom = to_boot;
free(s.curr_rom_part);
s.curr_rom_part = NULL;
if(to_boot->partition)
s.curr_rom_part = strdup(to_boot->partition->uuid);
if(s.is_second_boot == 0 && (M(to_boot->type) & MASK_ANDROID) && (exit & EXIT_KEXEC))
{
s.is_second_boot = 1;
// mrom_kexecd=1 param might be lost if kernel does not have kexec patches
ERROR(SECOND_BOOT_KMESG);
}
#ifdef MR_ALLOW_NKK71_NOKEXEC_WORKAROUND
// nkk71: check for kexec support, otherwise use workaround
else if ((nokexec()->selected_method == NO_KEXEC_BOOT_NOKEXEC) || (nokexec()->selected_method == NO_KEXEC_BOOT_PRIMARY))
{
if (s.is_second_boot == 0 && ((M(to_boot->type) & MASK_ANDROID) || (to_boot->type == ROM_DEFAULT)))
{
// nkk71: force a full reboot (due to boot.img flashing from above), then autoboot the ROM
s.is_second_boot = 1;
exit = (EXIT_REBOOT | EXIT_UMOUNT);
INFO("nkk71: go for reboot second_boot!\n");
ERROR(SECOND_BOOT_KMESG); // go for second_boot = 1
}
}
#endif
else
s.is_second_boot = 0;
}
finish:
#ifdef MR_ALLOW_NKK71_NOKEXEC_WORKAROUND
// release stuff
nokexec_free_struct();
#endif
if (s.enable_kmsg_logging != 0)
multirom_kmsg_logging(BACKUP_LATE_KLOG);
multirom_save_status(&s);
multirom_free_status(&s);
sync();
return exit;
}
int multirom_init_fb(int rotation)
{
if(fb_open(rotation) < 0)
{
ERROR("Failed to open framebuffer!\n");
return -1;
}
fb_fill(BLACK);
return 0;
}
void multirom_emergency_reboot(void)
{
char *klog;
fb_text_proto *p;
fb_img *t;
char *tail;
char *last_end;
int cur_y;
unsigned int media_rw_id;
if(multirom_init_fb(0) < 0)
{
ERROR("Failed to init framebuffer in emergency reboot\n");
return;
}
fb_set_background(BLACK);
klog = multirom_get_klog();
t = fb_add_text(0, 120, WHITE, SIZE_NORMAL,
"An error occured.\nShutting down MultiROM to avoid data corruption.\n"
"Report this error to the developer!\nDebug info: /sdcard/multirom_log.txt\n\n"
"Press POWER button to reboot.");
t = fb_add_text(0, t->y + t->h + 100*DPI_MUL, GRAYISH, SIZE_SMALL, "Last lines from klog:");
fb_add_rect(0, t->y + t->h + 5*DPI_MUL, fb_width, 1, GRAYISH);
tail = klog+strlen(klog);
last_end = tail;
cur_y = fb_height;
const int start_y = (t->y + t->h + 2);
while(tail > klog)
{
--tail;
if(*tail == '\n')
{
p = fb_text_create(0, cur_y, GRAYISH, 4*4, NULL);
p->text = malloc(last_end - tail);
memcpy(p->text, tail + 1, last_end - (tail + 1));
p->text[last_end - (tail + 1)] = 0;
p->style = STYLE_MONOSPACE;
t = fb_text_finalize(p);
cur_y -= t->h;
t->y = cur_y;
last_end = tail;
if(cur_y < start_y)
{
fb_rm_text(t);
break;
}
}
}
fb_force_draw();
multirom_copy_log(klog, "../multirom_log.txt");
free(klog);
media_rw_id = decode_uid("media_rw");
if(media_rw_id != -1U)
chown("../multirom_log.txt", (uid_t)media_rw_id, (gid_t)media_rw_id);
chmod("../multirom_log.txt", 0666);
// Wait for power key
start_input_thread();
while(wait_for_key() != KEY_POWER);
stop_input_thread();
fb_clear();
fb_close();
}
static int find_idx(int c)
{
static const char *capital = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
static const char *normal = "abcdefghijklmnopqrstuvwxyz";
char *p;
if((p = strchr(capital, c)))
return p - capital;
else if((p = strchr(normal, c)))
return p - normal;
return -128 + c;
}
static int compare_rom_names(const void *a, const void *b)
{
struct multirom_rom *rom_a = *((struct multirom_rom **)a);
struct multirom_rom *rom_b = *((struct multirom_rom **)b);
if(rom_a->type == ROM_DEFAULT)
return -1;
else if(rom_b->type == ROM_DEFAULT)
return 1;
char *itr_a = rom_a->name;
char *itr_b = rom_b->name;
while(1)
{
if(*itr_a == 0)
return -1;
else if(*itr_b == 0)
return 1;
if(*itr_a == *itr_b)
{
++itr_a;
++itr_b;
continue;
}
int idx_a = find_idx(*itr_a);
int idx_b = find_idx(*itr_b);
if(idx_a == idx_b)
{
++itr_a;
++itr_b;
continue;
}
return idx_a < idx_b ? -1 : 1;
}
return 0;
}
int multirom_default_status(struct multirom_status *s)
{
s->is_second_boot = 0;
s->current_rom = NULL;
s->roms = NULL;
#ifdef MR_ALLOW_NKK71_NOKEXEC_WORKAROUND
s->allow_nkk71_nokexec = NO_KEXEC_DISABLED;
#endif
s->colors = 0;
s->brightness = MULTIROM_DEFAULT_BRIGHTNESS;
s->enable_adb = 0;
s->enable_kmsg_logging = 0;
s->rotation = MULTIROM_DEFAULT_ROTATION;
s->anim_duration_coef = 1.f;
s->fstab = fstab_auto_load();
if(!s->fstab)
return -1;
char roms_path[256];
sprintf(roms_path, "%s/roms/"INTERNAL_ROM_NAME, mrom_dir());
DIR *d = opendir(roms_path);
if(!d)
{
ERROR("Failed to open Internal ROM's folder, creating one with ROM from internal memory...\n");
multirom_import_internal();
}
else
closedir(d);
sprintf(roms_path, "%s/roms", mrom_dir());
d = opendir(roms_path);
if(!d)
{
ERROR("Failed to open roms dir!\n");
return -1;
}
struct dirent *dr;
char path[256];
struct multirom_rom **add_roms = NULL;
while((dr = readdir(d)))
{
if(dr->d_name[0] == '.')
continue;
if(dr->d_type != DT_DIR)
continue;
if(strlen(dr->d_name) > MAX_ROM_NAME_LEN)
{
ERROR("Skipping ROM %s, name is too long (max %d chars allowed)\n", dr->d_name, MAX_ROM_NAME_LEN);
continue;
}
INFO("Adding ROM %s\n", dr->d_name);
struct multirom_rom *rom = malloc(sizeof(struct multirom_rom));
memset(rom, 0, sizeof(struct multirom_rom));
rom->id = multirom_generate_rom_id();
rom->name = strdup(dr->d_name);
snprintf(path, sizeof(path), "%s/%s", roms_path, rom->name);
rom->base_path = strdup(path);
rom->type = multirom_get_rom_type(rom);
snprintf(path, sizeof(path), "%s/boot.img", rom->base_path);
rom->has_bootimg = access(path, R_OK) == 0 ? 1 : 0;
multirom_find_rom_icon(rom);
list_add(&add_roms, rom);
}
closedir(d);
if(add_roms)
{
// sort roms
qsort(add_roms, list_item_count(add_roms), sizeof(struct multirom_rom*), compare_rom_names);
// add them to main list
list_swap(&add_roms, &s->roms);
}
s->current_rom = multirom_get_internal(s);
if(!s->current_rom)
{
ERROR("No internal rom found!\n");
return -1;
}
s->auto_boot_rom = s->current_rom;
s->auto_boot_seconds = 5;
s->auto_boot_type = AUTOBOOT_NAME;
return 0;
}
int multirom_load_status(struct multirom_status *s)
{
INFO("Loading MultiROM status...\n");
multirom_default_status(s);
if(mrom_is_second_boot())
s->is_second_boot = 1;
// is_second_boot might be reset later, but we need to know if this
// is second boot when filling in kexec info
s->is_running_in_primary_rom = !s->is_second_boot;
char arg[256];
sprintf(arg, "%s/multirom.ini", mrom_dir());
FILE *f = fopen(arg, "re");
if(!f)
{
ERROR("Failed to open config file, using defaults!\n");
return -1;
}
char line[1024];
char current_rom[256] = { 0 };
char auto_boot_rom[256] = { 0 };
char name[64];
char *pch;
while((fgets(line, sizeof(line), f)))
{
pch = strtok (line, "=\n");
if(!pch) continue;
strcpy(name, pch);
pch = strtok (NULL, "=\n");
if(!pch) continue;
strcpy(arg, pch);
if(strstr(name, "current_rom"))
strcpy(current_rom, arg);
else if(strstr(name, "auto_boot_seconds"))
s->auto_boot_seconds = atoi(arg);
else if(strstr(name, "auto_boot_rom"))
strcpy(auto_boot_rom, arg);
else if(strstr(name, "auto_boot_type"))
s->auto_boot_type = atoi(arg);
else if(strstr(name, "curr_rom_part"))
s->curr_rom_part = strdup(arg);
#ifdef MR_ALLOW_NKK71_NOKEXEC_WORKAROUND
else if(strstr(name, "allow_nkk71_nokexec"))
s->allow_nkk71_nokexec = atoi(arg);
#endif
else if(strstr(name, "colors_v2"))
s->colors = atoi(arg);
else if(strstr(name, "brightness"))
s->brightness = atoi(arg);
else if(strstr(name, "enable_adb"))
s->enable_adb = atoi(arg);
else if(strstr(name, "enable_kmsg_logging"))
s->enable_kmsg_logging = atoi(arg);
else if(strstr(name, "hide_internal"))
s->hide_internal = atoi(arg);
else if(strstr(name, "int_display_name"))
s->int_display_name = strdup(arg);
else if(strstr(name, "rotation"))
s->rotation = atoi(arg);
else if(strstr(name, "force_generic_fb"))
s->force_generic_fb = atoi(arg);
else if(strstr(name, "anim_duration_coef_pct"))
s->anim_duration_coef = ((float)atoi(arg)) / 100;