-
Notifications
You must be signed in to change notification settings - Fork 270
/
multirom.c
2555 lines (2150 loc) · 66.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 <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;
}
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);
struct multirom_rom *to_boot = NULL;
int exit = (EXIT_REBOOT | EXIT_UMOUNT);
if(rom_to_boot != NULL)
{
struct multirom_rom *rom = multirom_get_rom(&s, rom_to_boot, NULL);
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);
}
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);
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:
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;
s->colors = 0;
s->brightness = MULTIROM_DEFAULT_BRIGHTNESS;
s->enable_adb = 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);
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, "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)
{
struct usb_partition *p = NULL;
int tries = 0;
while(!p && tries < 10)
{
multirom_update_partitions(s);
p = multirom_get_partition(s, s->curr_rom_part);
if(p)
{
multirom_scan_partition_for_roms(s, p);
break;
}
++tries;
ERROR("part %s not found, waiting 1s (%d)\n", s->curr_rom_part, tries);
sleep(1);
}
}
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);
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;
}
multirom_fixup_rom_name(s->auto_boot_rom, auto_boot_name, "");
multirom_fixup_rom_name(s->current_rom, current_name, INTERNAL_ROM_NAME);
fprintf(f, "current_rom=%s\n", current_name);
fprintf(f, "auto_boot_seconds=%d\n", s->auto_boot_seconds);
fprintf(f, "auto_boot_rom=%s\n", auto_boot_name);
fprintf(f, "auto_boot_type=%d\n", s->auto_boot_type);
fprintf(f, "curr_rom_part=%s\n", s->curr_rom_part ? s->curr_rom_part : "");
fprintf(f, "colors_v2=%d\n", s->colors);
fprintf(f, "brightness=%d\n", s->brightness);
fprintf(f, "enable_adb=%d\n", s->enable_adb);
fprintf(f, "hide_internal=%d\n", s->hide_internal);
fprintf(f, "int_display_name=%s\n", s->int_display_name ? s->int_display_name : "");
fprintf(f, "rotation=%d\n", s->rotation);
fprintf(f, "force_generic_fb=%d\n", s->force_generic_fb);
fprintf(f, "anim_duration_coef_pct=%d\n", (int)(s->anim_duration_coef*100));
fclose(f);
return 0;
}
void multirom_fixup_rom_name(struct multirom_rom *rom, char *name, const char *def)
{
if(rom)
{
if(rom->type == ROM_DEFAULT)
strcpy(name, INTERNAL_ROM_NAME);
else
strcpy(name, rom->name);
}
else
{
strcpy(name, def);
}
}
void multirom_dump_status(struct multirom_status *s)
{
INFO("Dumping multirom status:\n");
INFO(" is_second_boot=%d\n", s->is_second_boot);
INFO(" is_running_in_primary_rom=%d\n", s->is_running_in_primary_rom);
INFO(" current_rom=%s\n", s->current_rom ? s->current_rom->name : "NULL");
INFO(" colors_v2=%d\n", s->colors);
INFO(" brightness=%d\n", s->brightness);
INFO(" enable_adb=%d\n", s->enable_adb);
INFO(" rotation=%d\n", s->rotation);
INFO(" force_generic_fb=%d\n", s->force_generic_fb);
INFO(" anim_duration_coef=%f\n", s->anim_duration_coef);
INFO(" hide_internal=%d\n", s->hide_internal);
INFO(" int_display_name=%s\n", s->int_display_name ? s->int_display_name : "NULL");
INFO(" auto_boot_seconds=%d\n", s->auto_boot_seconds);
INFO(" auto_boot_rom=%s\n", s->auto_boot_rom ? s->auto_boot_rom->name : "NULL");
INFO(" auto_boot_type=%d\n", s->auto_boot_type);
INFO(" curr_rom_part=%s\n", s->curr_rom_part ? s->curr_rom_part : "NULL");
INFO("\n");
int i;
for(i = 0; s->roms && s->roms[i]; ++i)
{
INFO(" ROM: %s\n", s->roms[i]->name);
INFO(" base_path: %s\n", s->roms[i]->base_path);
INFO(" icon_path: %s\n", s->roms[i]->icon_path);
INFO(" type: %d\n", s->roms[i]->type);
INFO(" has_bootimg: %d\n", s->roms[i]->has_bootimg);
if(s->roms[i]->partition)
INFO(" partition: %s (%s)\n", s->roms[i]->partition->name, s->roms[i]->partition->fs);
}
}
void multirom_free_status(struct multirom_status *s)
{
list_clear(&s->partitions, &multirom_destroy_partition);
list_clear(&s->roms, &multirom_free_rom);
free(s->curr_rom_part);
free(s->int_display_name);
fstab_destroy(s->fstab);
}
void multirom_free_rom(void *rom)
{
free(((struct multirom_rom*)rom)->name);
free(((struct multirom_rom*)rom)->base_path);
free(((struct multirom_rom*)rom)->icon_path);
free(rom);
}
void multirom_find_usb_roms(struct multirom_status *s)
{
// remove USB roms
int i;
for(i = 0; s->roms && s->roms[i];)
{
if(s->roms[i]->partition)
{
list_rm_at(&s->roms, i, &multirom_free_rom);
i = 0;
}
else ++i;
}
char path[256];
struct usb_partition *p;
pthread_mutex_lock(&parts_mutex);
for(i = 0; s->partitions && s->partitions[i]; ++i)
multirom_scan_partition_for_roms(s, s->partitions[i]);
pthread_mutex_unlock(&parts_mutex);
multirom_dump_status(s);
}
int multirom_scan_partition_for_roms(struct multirom_status *s, struct usb_partition *p)
{
char path[256];
int i;
struct dirent *dr;
struct multirom_rom **add_roms = NULL;
#ifdef MR_MOVE_USB_DIR
// groupers will have old "multirom" folder on USB drive instead of "multirom-grouper".
// We have to move it.
sprintf(path, "%s/multirom", p->mount_path);
if(access(path, F_OK) >= 0)
{
char dest[256];
sprintf(dest, "%s/multirom-"TARGET_DEVICE, p->mount_path);
INFO("Moving usb dir %s to %s!\n", path, dest);
mkdir(dest, 0777);
char *cmd[] = { busybox_path, "sh", "-c", malloc(1024), NULL };
sprintf(cmd[3], "%s mv \"%s\"/* \"%s\"/", busybox_path, path, dest);
run_cmd(cmd);
rmdir(path);
free(cmd[3]);
sync();
}
#endif
sprintf(path, "%s/multirom-"TARGET_DEVICE, p->mount_path);
if(access(path, F_OK) < 0)
return -1;
DIR *d = opendir(path);
if(!d)
return -1;
while((dr = readdir(d)) != NULL)
{
if(dr->d_name[0] == '.')
continue;
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);
sprintf(path, "%s/multirom-"TARGET_DEVICE"/%s", p->mount_path, rom->name);
rom->base_path = strdup(path);
rom->partition = p;
rom->type = multirom_get_rom_type(rom);
sprintf(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);
list_add_from_list(&s->roms, add_roms);
list_clear(&add_roms, NULL);
}
return 0;
}
int multirom_path_exists(char *base, char *filename)
{
char path[256];
sprintf(path, "%s/%s", base, filename);
if(access(path, R_OK) < 0)
return -1;
return 0;
}
int multirom_get_rom_type(struct multirom_rom *rom)
{
if(!rom->partition && strcmp(rom->name, INTERNAL_ROM_NAME) == 0)
return ROM_DEFAULT;
char *b = rom->base_path;
// Handle android ROMs
if(!multirom_path_exists(b, "boot"))
{
if (!multirom_path_exists(b, "system") && !multirom_path_exists(b, "data") &&
!multirom_path_exists(b, "cache"))
{
if(!rom->partition) return ROM_ANDROID_INTERNAL;
else return ROM_ANDROID_USB_DIR;
}
else if(!multirom_path_exists(b, "system.img") && !multirom_path_exists(b, "data.img") &&
!multirom_path_exists(b, "cache.img"))
{
return ROM_ANDROID_USB_IMG;
}
}
// handle linux ROMs
if(!multirom_path_exists(b, "rom_info.txt"))
{
if(!rom->partition)
return ROM_LINUX_INTERNAL;
else
return ROM_LINUX_USB;
}
// Handle Ubuntu 13.04 - deprecated
if ((!multirom_path_exists(b, "root") && multirom_path_exists(b, "boot.img")) ||
(!multirom_path_exists(b, "root.img") && rom->partition))
{
// try to copy rom_info.txt in there, ubuntu is deprecated
ERROR("Found deprecated Ubuntu 13.04, trying to copy rom_info.txt...\n");
char *cmd[] = { busybox_path, "cp", malloc(256), malloc(256), NULL };
sprintf(cmd[2], "%s/infos/ubuntu.txt", mrom_dir());
sprintf(cmd[3], "%s/rom_info.txt", b);
int res = run_cmd(cmd);
free(cmd[2]);
free(cmd[3]);
if(res != 0)
{
ERROR("Failed to copy rom_info for Ubuntu!\n");
if(!rom->partition) return ROM_UNSUPPORTED_INT;
else return ROM_UNSUPPORTED_USB;
}
else
{
if(!rom->partition) return ROM_LINUX_INTERNAL;
else return ROM_LINUX_USB;
}
}
// Handle ubuntu 12.10
if(!multirom_path_exists(b, "root") && !multirom_path_exists(b, "boot.img"))
{
if(!rom->partition) return ROM_UNSUPPORTED_INT;
else return ROM_UNSUPPORTED_USB;
}
return ROM_UNKNOWN;
}
void multirom_import_internal(void)
{
char path[256];
// multirom
mkdir(mrom_dir(), 0777);
// roms
snprintf(path, sizeof(path), "%s/roms", mrom_dir());
mkdir(path, 0777);
// internal rom
snprintf(path, sizeof(path), "%s/roms/%s", mrom_dir(), INTERNAL_ROM_NAME);
mkdir(path, 0777);
// set default icon if it doesn't exist yet
snprintf(path, sizeof(path), "%s/roms/%s/.icon_data", mrom_dir(), INTERNAL_ROM_NAME);
if(access(path, F_OK) < 0)
{
FILE *f = fopen(path, "we");
if(f)
{
fputs("predef_set\ncom.tassadar.multirommgr:drawable/romic_android\n", f);
fclose(f);
}
}
}
struct multirom_rom *multirom_get_internal(struct multirom_status *s)
{
int i;
for(i = 0; s->roms && s->roms[i]; ++i)
{
if(s->roms[i]->type == ROM_DEFAULT)
return s->roms[i];
}
ERROR(" Something is wrong, multirom_get_internal returns NULL!\n");
return NULL;
}
struct multirom_rom *multirom_get_rom(struct multirom_status *s, const char *name, const char *part_uuid)
{
if(part_uuid == NULL && strcmp(name, INTERNAL_ROM_NAME) == 0)
return multirom_get_internal(s);
int i = 0;
struct multirom_rom *r;
for(; s->roms && s->roms[i]; ++i)
{
r = s->roms[i];
if (r->type != ROM_DEFAULT && strcmp(r->name, name) == 0 &&
(!part_uuid || (r->partition && strcmp(r->partition->uuid, part_uuid) == 0)))
{
return r;
}
}
return NULL;
}
int multirom_generate_rom_id(void)
{
static int id = 0;
return id++;
}
struct multirom_rom *multirom_get_rom_by_id(struct multirom_status *s, int id)
{
int i = 0;
for(; s->roms && s->roms[i]; ++i)
if(s->roms[i]->id == id)
return s->roms[i];
return NULL;
}
int multirom_prepare_for_boot(struct multirom_status *s, struct multirom_rom *to_boot)
{
int exit = EXIT_UMOUNT;
int type = to_boot->type;
if(((M(type) & MASK_KEXEC) || to_boot->has_bootimg) && type != ROM_DEFAULT && s->is_second_boot == 0)
{
if(multirom_load_kexec(s, to_boot) != 0)
return -1;
exit |= EXIT_KEXEC;
}
switch(type)
{
case ROM_DEFAULT:
{
rom_quirks_on_initrd_finalized();
break;
}
case ROM_LINUX_INTERNAL:
case ROM_LINUX_USB:
break;
case ROM_ANDROID_USB_IMG:
case ROM_ANDROID_USB_DIR:
case ROM_ANDROID_INTERNAL:
{
if(!(exit & (EXIT_REBOOT | EXIT_KEXEC)))
{
exit &= ~(EXIT_UMOUNT);
if(multirom_prep_android_mounts(s, to_boot) == -1)
return -1;
if(multirom_create_media_link(s) == -1)
return -1;
rom_quirks_on_initrd_finalized();
rcadditions_write_to_files(&s->rc);