forked from Tasssadar/multirom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
multirom.c
2005 lines (1681 loc) · 49.2 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
#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 "multirom.h"
#include "multirom_ui.h"
#include "framebuffer.h"
#include "input.h"
#include "log.h"
#include "util.h"
#include "version.h"
#include "adb.h"
#define REALDATA "/realdata"
#define BUSYBOX_BIN "busybox"
#define KEXEC_BIN "kexec"
#define NTFS_BIN "ntfs-3g"
#define ADBD_BIN "adbd"
#define INTERNAL_ROM_NAME "Internal"
#define BOOT_BLK "/dev/block/mmcblk0p2"
#define MAX_ROM_NAME_LEN 26
#define LAYOUT_VERSION "/data/.layout_version"
#define SECOND_BOOT_KMESG "MultiromSaysNextBootShouldBeSecondMagic108"
#define BATTERY_CAP "/sys/class/power_supply/battery/capacity"
#define BRIGHTNESS_FILE "/sys/devices/platform/pwm-backlight/backlight/pwm-backlight/brightness"
#define T_FOLDER 4
char busybox_path[64] = { 0 };
char adbd_path[64] = { 0 };
static char multirom_dir[64] = { 0 };
static char kexec_path[64] = { 0 };
static char ntfs_path[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",
NULL,
};
for(i = 0; paths[i]; ++i)
{
if(stat(paths[i], &info) < 0)
continue;
strcpy(multirom_dir, 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(adbd_path, "%s/%s", paths[i], ADBD_BIN);
chmod(kexec_path, 0755);
chmod(ntfs_path, 0755);
chmod(adbd_path, 0755);
return 0;
}
return -1;
}
int multirom(void)
{
if(multirom_find_base_dir() == -1)
{
ERROR("Could not find multirom dir");
return -1;
}
struct multirom_status s;
memset(&s, 0, sizeof(struct multirom_status));
multirom_load_status(&s);
multirom_dump_status(&s);
if(s.enable_adb)
adb_init();
struct multirom_rom *to_boot = NULL;
int exit = (EXIT_REBOOT | EXIT_UMOUNT);
if(s.is_second_boot == 0)
{
// 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;
}
}
else
{
ERROR("Skipping ROM selection beacause of is_second_boot==1");
to_boot = s.current_rom;
}
if(to_boot)
{
exit = multirom_prepare_for_boot(&s, to_boot);
// Something went wrong, reboot
if(exit == -1)
{
multirom_emergency_reboot();
return EXIT_REBOOT;
}
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;
else
s.is_second_boot = 0;
}
adb_quit();
multirom_save_status(&s);
multirom_free_status(&s);
sync();
return exit;
}
int multirom_init_fb(void)
{
vt_set_mode(1);
if(fb_open() < 0)
{
ERROR("Failed to open framebuffer!");
return -1;
}
fb_fill(BLACK);
return 0;
}
void multirom_emergency_reboot(void)
{
if(multirom_init_fb() < 0)
{
ERROR("Failed to init framebuffer in emergency reboot");
return;
}
char *klog = multirom_get_klog();
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/error.txt\n\n"
"Press POWER button to reboot.");
fb_add_text(0, 370, GRAYISH, SIZE_SMALL, "Last lines from klog:");
fb_add_rect(0, 390, fb_width, 1, GRAYISH);
char *tail = klog+strlen(klog);
int count = 0;
while(tail > klog && count < 50)
{
--tail;
if(*tail == '\n')
++count;
}
fb_add_text_long(0, 395, GRAYISH, SIZE_SMALL, ++tail);
fb_draw();
fb_clear();
fb_close();
multirom_copy_log(klog);
free(klog);
// Wait for power key
start_input_thread();
while(wait_for_key() != KEY_POWER);
stop_input_thread();
}
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 -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(strcmp(rom_a->name, INTERNAL_ROM_NAME) == 0)
return -1;
else if(strcmp(rom_b->name, INTERNAL_ROM_NAME) == 0)
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 = 40;
s->enable_adb = 0;
char roms_path[256];
sprintf(roms_path, "%s/roms/"INTERNAL_ROM_NAME, multirom_dir);
DIR *d = opendir(roms_path);
if(!d)
{
ERROR("Failed to open Internal ROM's folder, creating one with ROM from internal memory...\n");
if(multirom_import_internal() == -1)
return -1;
}
else
closedir(d);
sprintf(roms_path, "%s/roms", multirom_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 != T_FOLDER)
continue;
if(strlen(dr->d_name) > MAX_ROM_NAME_LEN)
{
ERROR("Skipping ROM %s, name is too long (max %d chars allowed)", dr->d_name, MAX_ROM_NAME_LEN);
continue;
}
fb_debug("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);
sprintf(path, "%s/%s", roms_path, rom->name);
rom->base_path = strdup(path);
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;
list_add(rom, &add_roms);
}
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
int i;
for(i = 0; add_roms[i]; ++i)
list_add(add_roms[i], &s->roms);
list_clear(&add_roms, NULL);
}
s->current_rom = multirom_get_rom(s, INTERNAL_ROM_NAME, NULL);
if(!s->current_rom)
{
fb_debug("No internal rom found!\n");
return -1;
}
return 0;
}
int multirom_load_status(struct multirom_status *s)
{
fb_debug("Loading MultiROM status...\n");
multirom_default_status(s);
char arg[256];
sprintf(arg, "%s/multirom.ini", multirom_dir);
FILE *f = fopen(arg, "r");
if(!f)
{
fb_debug("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;
if(multirom_get_cmdline(line, sizeof(line)) != 0)
{
ERROR("Failed to get cmdline!\n");
return -1;
}
if(multirom_search_last_kmsg(SECOND_BOOT_KMESG) == 0)
s->is_second_boot = 1;
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, "curr_rom_part"))
s->curr_rom_part = strdup(arg);
else if(strstr(name, "colors"))
s->colors = atoi(arg);
else if(strstr(name, "brightness"))
s->brightness = atoi(arg);
else if(strstr(name, "enable_adb"))
s->enable_adb = atoi(arg);
}
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)
{
fb_debug("Failed to select current rom (%s, part %s), using Internal!\n", current_rom, s->curr_rom_part);
s->current_rom = multirom_get_rom(s, INTERNAL_ROM_NAME, NULL);
if(!s->current_rom)
{
fb_debug("No internal rom found!\n");
return -1;
}
}
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", auto_boot_rom);
return 0;
}
int multirom_save_status(struct multirom_status *s)
{
fb_debug("Saving multirom status\n");
char path[256];
sprintf(path, "%s/multirom.ini", multirom_dir);
FILE *f = fopen(path, "w");
if(!f)
{
fb_debug("Failed to open/create status file!\n");
return -1;
}
fprintf(f, "current_rom=%s\n", s->current_rom ? s->current_rom->name : INTERNAL_ROM_NAME);
fprintf(f, "auto_boot_seconds=%d\n", s->auto_boot_seconds);
fprintf(f, "auto_boot_rom=%s\n", s->auto_boot_rom ? s->auto_boot_rom->name : "");
fprintf(f, "curr_rom_part=%s\n", s->curr_rom_part ? s->curr_rom_part : "");
fprintf(f, "colors=%d\n", s->colors);
fprintf(f, "brightness=%d\n", s->brightness);
fprintf(f, "enable_adb=%d\n", s->enable_adb);
fclose(f);
return 0;
}
void multirom_find_usb_roms(struct multirom_status *s)
{
// remove USB roms
int i;
for(i = 0; s->roms && s->roms[i];)
{
if(M(s->roms[i]->type) & MASK_USB_ROMS)
{
list_rm(s->roms[i], &s->roms, &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)
if(!strstr(s->partitions[i]->name, "mmcblk"))
multirom_scan_partition_for_roms(s, s->partitions[i]);
pthread_mutex_unlock(&parts_mutex);
}
int multirom_scan_partition_for_roms(struct multirom_status *s, struct usb_partition *p)
{
char path[256];
int i;
struct stat info;
struct dirent *dr;
struct multirom_rom **add_roms = NULL;
sprintf(path, "%s/multirom", p->mount_path);
if(stat(path, &info) < 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/%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;
list_add(rom, &add_roms);
}
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
for(i = 0; add_roms[i]; ++i)
list_add(add_roms[i], &s->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", multirom_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;
}
int multirom_import_internal(void)
{
char path[256];
// multirom
mkdir(multirom_dir, 0777);
// roms
sprintf(path, "%s/roms", multirom_dir);
mkdir(path, 0777);
// internal rom
sprintf(path, "%s/roms/%s", multirom_dir, INTERNAL_ROM_NAME);
mkdir(path, 0777);
// boot image
sprintf(path, "%s/roms/%s/boot.img", multirom_dir, INTERNAL_ROM_NAME);
return multirom_dump_boot(path);
}
int multirom_dump_boot(const char *dest)
{
fb_debug("Dumping boot image...");
// 0 1 2 3
char *cmd[] = { busybox_path, "dd", "if="BOOT_BLK, NULL, NULL };
cmd[3] = malloc(256);
sprintf(cmd[3], "of=%s", dest);
int res = run_cmd(cmd);
free(cmd[3]);
fb_debug("done, result: %d\n", res);
return res;
}
struct multirom_rom *multirom_get_rom(struct multirom_status *s, const char *name, const char *part_uuid)
{
int i = 0;
struct multirom_rom *r;
for(; s->roms && s->roms[i]; ++i)
{
r = s->roms[i];
if (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;
}
void multirom_dump_status(struct multirom_status *s)
{
fb_debug("Dumping multirom status:\n");
fb_debug(" is_second_boot=%d\n", s->is_second_boot);
fb_debug(" current_rom=%s\n", s->current_rom ? s->current_rom->name : "NULL");
fb_debug(" colors=%d\n", s->colors);
fb_debug(" brightness=%d\n", s->brightness);
fb_debug(" enable_adb=%d\n", s->enable_adb);
fb_debug(" auto_boot_seconds=%d\n", s->auto_boot_seconds);
fb_debug(" auto_boot_rom=%s\n", s->auto_boot_rom ? s->auto_boot_rom->name : "NULL");
fb_debug(" curr_rom_part=%s\n", s->curr_rom_part ? s->curr_rom_part : "NULL");
fb_debug("\n");
int i, y;
char buff[256];
for(i = 0; s->roms && s->roms[i]; ++i)
{
fb_debug(" ROM: %s\n", s->roms[i]->name);
fb_debug(" base_path: %s\n", s->roms[i]->base_path);
fb_debug(" type: %d\n", s->roms[i]->type);
fb_debug(" has_bootimg: %d\n", s->roms[i]->has_bootimg);
}
}
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:
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(to_boot) == -1)
return -1;
if(multirom_create_media_link() == -1)
return -1;
if(to_boot->partition)
to_boot->partition->keep_mounted = 1;
break;
}
default:
ERROR("Unknown ROM type\n");
return -1;
}
return exit;
}
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);
}
void multirom_free_rom(void *rom)
{
free(((struct multirom_rom*)rom)->name);
free(((struct multirom_rom*)rom)->base_path);
free(rom);
}
#define EXEC_MASK (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP)
int multirom_prep_android_mounts(struct multirom_rom *rom)
{
char in[128];
char out[128];
char folder[256];
sprintf(folder, "%s/boot", rom->base_path);
DIR *d = opendir(folder);
if(!d)
{
ERROR("Failed to open rom folder %s", folder);
return -1;
}
struct dirent *dp = NULL;
char line[1024];
FILE *f_in = NULL;
FILE *f_out = NULL;
int add_dummy = 0;
while((dp = readdir(d)))
{
sprintf(in, "%s/%s", folder, dp->d_name);
sprintf(out, "/%s", dp->d_name);
// just copy the file if not rc
if(!strstr(dp->d_name, ".rc"))
{
copy_file(in, out);
continue;
}
f_in = fopen(in, "r");
if(!f_in)
continue;
f_out = fopen(out, "w");
if(!f_out)
{
fclose(f_in);
continue;
}
while((fgets(line, sizeof(line), f_in)))
{
if (strstr(line, "on "))
add_dummy = 1;
// Remove mounts from RCs
else if (strstr(line, "mount_all") ||
(strstr(line, "mount ") && (strstr(line, "/data") || strstr(line, "/system"))))
{
if(add_dummy == 1)
{
add_dummy = 0;
fputs(" export DUMMY_LINE_INGORE_IT 1\n", f_out);
}
// This is needed to start sdcard and gps daemon
// (class late_start). Calling "on unencrypted"
// is in mount_all cmd, which is just not cool.
if(strstr(line, "mount_all"))
{
fputs(" setprop ro.crypto.state unencrypted\n", f_out);
fputs(" trigger nonencrypted\n", f_out);
}
fputc((int)'#', f_out);
}
fputs(line, f_out);
}
fclose(f_out);
fclose(f_in);
chmod(out, EXEC_MASK);
}
closedir(d);
mkdir_with_perms("/system", 0755, NULL, NULL);
mkdir_with_perms("/data", 0771, "system", "system");
mkdir_with_perms("/cache", 0770, "system", "cache");
static const char *folders[2][3] =
{
{ "system", "data", "cache" },
{ "system.img", "data.img", "cache.img" },
};
unsigned long flags[2][3] = {
{ MS_BIND | MS_RDONLY, MS_BIND, MS_BIND },
{ MS_RDONLY | MS_NOATIME, MS_NOATIME, MS_NOATIME },
};
uint32_t i;
char from[256];
char to[256];
int img = (int)(rom->type == ROM_ANDROID_USB_IMG);
for(i = 0; i < ARRAY_SIZE(folders[0]); ++i)
{
sprintf(from, "%s/%s", rom->base_path, folders[img][i]);
sprintf(to, "/%s", folders[0][i]);
if(img == 0)
{
if(mount(from, to, "ext4", flags[img][i], "discard,nomblk_io_submit") < 0)
{
ERROR("Failed to mount %s to %s (%d: %s)", from, to, errno, strerror(errno));
return -1;
}
}
else
{
if(multirom_mount_loop(from, to, "ext4", flags[img][i]) < 0)
return -1;
}
}
return 0;
}
int multirom_create_media_link(void)
{
int media_new = 0;
int api_level = multirom_get_api_level("/system/build.prop");
if(api_level <= 0)
return -1;
struct stat info;
if(stat(REALDATA"/media/0", &info) >= 0)
media_new = 1;
static const char *paths[] = {
REALDATA"/media", // 0
REALDATA"/media/0", // 1
"/data/media", // 2
"/data/media/0", // 3
};
int from, to;
if(api_level <= 16)
{
to = 2;
if(!media_new) from = 0;
else from = 1;
}
else if(api_level >= 17)
{
from = 0;
if(!media_new) to = 3;
else to = 2;
}
ERROR("Making media dir: api %d, media_new %d, %s to %s", api_level, media_new, paths[from], paths[to]);
if(mkdir_recursive(paths[to], 0775) == -1)
{
ERROR("Failed to make media dir");
return -1;
}
if(mount(paths[from], paths[to], "ext4", MS_BIND, "") < 0)
{
ERROR("Failed to bind media folder %d (%s)", errno, strerror(errno));
return -1;
}
if(api_level >= 17)
{
FILE *f = fopen(LAYOUT_VERSION, "w");
if(!f)
{
ERROR("Failed to create .layout_version!\n");
return -1;
}
fputs("2", f);
fclose(f);
chmod(LAYOUT_VERSION, 0600);
}
return 0;
}
int multirom_get_api_level(const char *path)
{
FILE *f = fopen(path, "r");
if(!f)
{
ERROR("Could not open %s to read api level!", path);
return -1;
}
int res = -1;
char line[256];
while(res == -1 && (fgets(line, sizeof(line), f)))
{
if(strstr(line, "ro.build.version.sdk=") == line)
res = atoi(strchr(line, '=')+1);
}
fclose(f);
if(res == 0)
ERROR("Invalid ro.build.version.sdk line in build.prop");
return res;