forked from Tasssadar/multirom
-
Notifications
You must be signed in to change notification settings - Fork 12
/
multirom.c
3117 lines (2625 loc) · 85.4 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 <sys/vfs.h>
#include <sys/statvfs.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"
#ifdef MR_NO_KEXEC
#include "no_kexec.h"
#endif
#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)
remove(path_file_1); //INFO("Deleting oldest log '%s' res=%d\n", path_file_1, remove(path_file_1));
else
rename(path_file_1, path_file_2); //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));
}
}
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_NO_KEXEC
// setup global variables here
nokexec_set_struct(&s);
// check if a partition mount failed, in which case multirom usually just boots internal
if (s.current_rom && s.current_rom->type == ROM_DEFAULT && nokexec_is_second_boot())
{
ERROR(NO_KEXEC_LOG_TEXT ": Something went wrong in mounting the needed partition, so falling back...");
nokexec()->selected_method = NO_KEXEC_BOOT_NORMAL;
s.is_second_boot = 0;
//ERROR(NO_KEXEC_LOG_TEXT ": to Internal\n"); s.auto_boot_type = AUTOBOOT_FORCE_CURRENT; // force reboot to Internal (this would be mrom default behaviour)
//ERROR(NO_KEXEC_LOG_TEXT ": to MultiROM GUI\n"); s.auto_boot_type = AUTOBOOT_NAME; // bring up the gui instead
ERROR(NO_KEXEC_LOG_TEXT ": to Recovery\n"); exit = (EXIT_REBOOT_RECOVERY | EXIT_UMOUNT); goto finish; // reboot to recovery
}
#endif
if(rom_to_boot != NULL)
{
struct multirom_rom *rom = NULL;
// Parse rom_to_boot should be: Name_of_ROM++uuid=xxxx (the '++uuid=xxxx' is optional but needed for external ROMs)
char * uuid_ptr = strstr(rom_to_boot, "++uuid=");
if(uuid_ptr == NULL)
{
rom = multirom_get_rom(&s, rom_to_boot, NULL);
}
else
{
char name[MAX_ROM_NAME_LEN + 1];
char part_uuid[36+4+1];
strncpy(name, rom_to_boot, uuid_ptr - rom_to_boot);
name[uuid_ptr - rom_to_boot] = 0;
strcpy(part_uuid, uuid_ptr + sizeof("++uuid=") - 1);
// Note: if the current ROM is on the same external partition as the one being booted
// then this function would already have been called in multirom_load_status
multirom_update_and_scan_for_external_roms(&s, part_uuid);
rom = multirom_get_rom(&s, name, part_uuid);
}
if(rom)
{
// 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);
}
#ifdef MR_NO_KEXEC
else if(((M(rom->type) & MASK_KEXEC) || rom->has_bootimg) && rom->type != ROM_DEFAULT)
{
to_boot = rom;
s.is_second_boot = 0;
INFO(NO_KEXEC_LOG_TEXT " Booting ROM %s...\n", rom_to_boot);
}
#endif
else
{
s.current_rom = rom;
free(s.curr_rom_part);
s.curr_rom_part = NULL;
if(rom->partition)
s.curr_rom_part = strdup(rom->partition->uuid);
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);
#ifdef MR_NO_KEXEC
#define MR_NO_KEXEC_ABORT { \
if(rom_to_boot == NULL) { \
ERROR(NO_KEXEC_LOG_TEXT ": ERROR occurred in the above, aborting to recovery\n"); \
exit = (EXIT_REBOOT_RECOVERY | EXIT_UMOUNT); \
} \
else \
{ \
ERROR(NO_KEXEC_LOG_TEXT ": ERROR occurred in the above, failed to boot '%s'\n", rom_to_boot); \
exit = EXIT_UMOUNT; \
} \
goto finish; \
}
if (s.is_second_boot != 0)
{
// Restore primary boot.img, and continue
if (nokexec_restore_primary_and_cleanup() < 0)
MR_NO_KEXEC_ABORT;
}
else
{
if ((to_boot->type != ROM_DEFAULT) && to_boot->has_bootimg)
{
// Flash secondary boot.img, and reboot
// note: a secondary boot.img in primary slot will trigger second_boot=1
// so we don't need to worry about that any more
if (nokexec_flash_secondary_bootimg(to_boot) < 0)
MR_NO_KEXEC_ABORT;
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);
exit = (EXIT_REBOOT | EXIT_UMOUNT);
goto finish;
}
}
#endif
if(rom_to_boot == NULL)
multirom_run_scripts("run-on-boot", to_boot);
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);
}
else
s.is_second_boot = 0;
}
finish:
#ifdef MR_NO_KEXEC
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_apk_get_roms(struct multirom_status *s)
{
if(multirom_find_base_dir() == -1)
{
printf("Could not find multirom dir\n");
return -1;
}
char current_rom[256] = { 0 };
s->curr_rom_part = NULL;
/* Read multirom.ini to find current_rom */
char arg[256];
sprintf(arg, "%s/multirom.ini", mrom_dir());
FILE *f = fopen(arg, "re");
if(f)
{
char line[1024];
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, "curr_rom_part"))
s->curr_rom_part = strdup(arg);
}
printf("current_rom='%s' curr_rom_part='%s'\n", current_rom, (s->curr_rom_part ? s->curr_rom_part : ""));
fclose(f);
}
else
{
printf("Failed to open config file, setting current_rom to null!\n");
}
/* Get Internal ROM */
char roms_path[256];
sprintf(roms_path, "%s/roms/"INTERNAL_ROM_NAME, mrom_dir());
DIR *d = opendir(roms_path);
if(!d)
{
printf("Failed to open Internal ROM's folder, creating one with ROM from internal memory...\n");
multirom_import_internal();
}
else
closedir(d);
/* Get Internal Storage ROMs */
sprintf(roms_path, "%s/roms", mrom_dir());
d = opendir(roms_path);
if(!d)
{
printf("Failed to open roms dir!\n");
//return -1;
}
else
{
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)
{
printf("Skipping ROM %s, name is too long (max %d chars allowed)\n", dr->d_name, MAX_ROM_NAME_LEN);
continue;
}
//printf("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);
}
}
/* Get External ROMs */
multirom_update_partitions(s);
int i;
pthread_mutex_lock(&parts_mutex);
for(i = 0; s->partitions && s->partitions[i]; ++i)
{
s->partitions[i]->keep_mounted = 1; // don't unmount on exit, the APK will need access to the folders
multirom_scan_partition_for_roms(s, s->partitions[i]);
}
pthread_mutex_unlock(&parts_mutex);
s->current_rom = multirom_get_rom(s, current_rom, s->curr_rom_part);
if(!s->current_rom)
{
printf("Failed to find current rom (%s, part %s)!\n", current_rom, (s->curr_rom_part) ? s->curr_rom_part : "");
free(s->curr_rom_part);
s->curr_rom_part = NULL;
}
return 0;
}
int multirom_default_status(struct multirom_status *s)
{
s->is_second_boot = 0;
s->current_rom = NULL;
s->roms = NULL;
#ifdef MR_NO_KEXEC
s->no_kexec = MR_NO_KEXEC;
#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_NO_KEXEC
else if(strstr(name, "no_kexec"))
s->no_kexec = 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;
}
fclose(f);
// find USB drive if we're booting from it
if(s->curr_rom_part) // && s->is_second_boot)
{
multirom_update_and_scan_for_external_roms(s, s->curr_rom_part);
}
s->current_rom = multirom_get_rom(s, current_rom, s->curr_rom_part);
if(!s->current_rom)
{
ERROR("Failed to select current rom (%s, part %s), using Internal!\n", current_rom, s->curr_rom_part);
s->current_rom = multirom_get_internal(s);
free(s->curr_rom_part);
s->curr_rom_part = NULL;
if(!s->current_rom)
{
ERROR("No internal rom found!\n");
return -1;
}
}
if((s->auto_boot_type & AUTOBOOT_LAST)) // && !s->curr_rom_part)
{
s->auto_boot_rom = s->current_rom;
}
else
{
s->auto_boot_rom = multirom_get_rom(s, auto_boot_rom, NULL);
if(!s->auto_boot_rom)
ERROR("Could not find rom %s to auto-boot\n", auto_boot_rom);
}
if(s->int_display_name)
{
struct multirom_rom *r = multirom_get_internal(s);
r->name = realloc(r->name, strlen(s->int_display_name)+1);
strcpy(r->name, s->int_display_name);
}
fb_force_generic_impl(s->force_generic_fb);
if(s->anim_duration_coef == 0)
s->anim_duration_coef = 1.f;
return 0;
}
int multirom_save_status(struct multirom_status *s)
{
INFO("Saving multirom status\n");
char path[256];
char auto_boot_name[MAX_ROM_NAME_LEN+1];
char current_name[MAX_ROM_NAME_LEN+1];
snprintf(path, sizeof(path), "%s/multirom.ini", mrom_dir());
FILE *f = fopen(path, "we");
if(!f)
{
ERROR("Failed to open/create status file!\n");
return -1;