forked from Tasssadar/Team-Win-Recovery-Project
/
multirom.cpp
1713 lines (1434 loc) · 38.8 KB
/
multirom.cpp
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 <unistd.h>
#include "multirom.h"
extern "C" {
#include "twcommon.h"
}
std::string MultiROM::m_path = "";
std::string MultiROM::m_mount_rom_paths[2] = { "", "" };
std::vector<MultiROM::file_backup> MultiROM::m_mount_bak;
std::string MultiROM::m_curr_roms_path = "";
MROMInstaller *MultiROM::m_installer = NULL;
MultiROM::baseFolders MultiROM::m_base_folders;
int MultiROM::m_base_folder_cnt = 0;
base_folder::base_folder(const std::string& name, int min_size, int size)
{
this->name = name;
this->min_size = min_size;
this->size = size;
}
base_folder::base_folder(const base_folder& other)
{
name = other.name;
min_size = other.min_size;
size = other.size;
}
base_folder::base_folder()
{
min_size = 1;
size = 1;
}
MultiROM::config::config()
{
current_rom = INTERNAL_NAME;
auto_boot_seconds = 5;
auto_boot_rom = INTERNAL_NAME;
colors = 0;
brightness = 40;
}
bool MultiROM::folderExists()
{
findPath();
return !m_path.empty();
}
std::string MultiROM::getRomsPath()
{
return m_curr_roms_path;
}
std::string MultiROM::getPath()
{
return m_path;
}
void MultiROM::findPath()
{
static const char *paths[] = {
"/data/media/multirom",
"/data/media/0/multirom",
NULL
};
struct stat info;
for(int i = 0; paths[i]; ++i)
{
if(stat(paths[i], &info) >= 0)
{
m_path = paths[i];
m_curr_roms_path = m_path + "/roms/";
return;
}
}
m_path.clear();
}
void MultiROM::setRomsPath(std::string loc)
{
umount("/mnt"); // umount last thing mounted there
if(loc.compare(INTERNAL_MEM_LOC_TXT) == 0)
{
m_curr_roms_path = m_path + "/roms/";
return;
}
size_t idx = loc.find(' ');
if(idx == std::string::npos)
{
m_curr_roms_path.clear();
return;
}
std::string dev = loc.substr(0, idx);
mkdir("/mnt", 0777); // in case it does not exist
char cmd[256];
if(loc.find("(ntfs") == std::string::npos)
sprintf(cmd, "mount %s /mnt", dev.c_str());
else
sprintf(cmd, "%s/ntfs-3g %s /mnt", m_path.c_str(), dev.c_str());
system(cmd);
m_curr_roms_path = "/mnt/multirom/";
mkdir("/mnt/multirom/", 0777);
}
std::string MultiROM::listInstallLocations()
{
std::string res = INTERNAL_MEM_LOC_TXT"\n";
system("blkid > /tmp/blkid.txt");
FILE *f = fopen("/tmp/blkid.txt", "r");
if(!f)
return res;
char line[1024];
std::string blk;
size_t idx1, idx2;
while((fgets(line, sizeof(line), f)))
{
if(!strstr(line, "/dev/block/sd"))
continue;
blk = line;
idx1 = blk.find(':');
if(idx1 == std::string::npos)
continue;
res += blk.substr(0, idx1);
blk = line;
idx1 = blk.find("TYPE=");
if(idx1 == std::string::npos)
continue;
idx1 += strlen("TYPE=\"");
idx2 = blk.find('"', idx1);
if(idx2 == std::string::npos)
continue;
res += " (" + blk.substr(idx1, idx2-idx1) + ")\n";
}
fclose(f);
return res;
}
bool MultiROM::move(std::string from, std::string to)
{
std::string roms = getRomsPath();
std::string cmd = "mv \"" + roms + "/" + from + "\" ";
cmd += "\"" + roms + "/" + to + "\"";
gui_print("Moving ROM \"%s\" to \"%s\"...\n", from.c_str(), to.c_str());
return system(cmd.c_str()) == 0;
}
bool MultiROM::erase(std::string name)
{
std::string path = getRomsPath() + "/" + name;
gui_print("Erasing ROM \"%s\"...\n", name.c_str());
std::string cmd = "rm -rf \"" + path + "\"";
return system(cmd.c_str()) == 0;
}
bool MultiROM::wipe(std::string name, std::string what)
{
gui_print("Changing mountpoints...\n");
if(!changeMounts(name))
{
gui_print("Failed to change mountpoints!\n");
return false;
}
char cmd[256];
bool res = true;
if(what == "dalvik")
{
static const char *dirs[] = {
"data/dalvik-cache",
"cache/dalvik-cache",
"cache/dc",
};
for(uint8_t i = 0; res && i < sizeof(dirs)/sizeof(dirs[0]); ++i)
{
sprintf(cmd, "rm -rf \"/%s\"", dirs[i]);
gui_print("Wiping dalvik: %s...\n", dirs[i]);
res = (system(cmd) == 0);
}
}
else
{
sprintf(cmd, "rm -rf \"/%s/\"*", what.c_str());
gui_print("Wiping ROM's /%s...\n", what.c_str());
res = (system(cmd) == 0);
}
sync();
if(!res)
gui_print("ERROR: Failed to erase %s!\n", what.c_str());
gui_print("Restoring mountpoints...\n");
restoreMounts();
return res;
}
int MultiROM::getType(std::string name)
{
std::string path = getRomsPath() + "/" + name + "/";
struct stat info;
if(getRomsPath().find("/mnt") != 0) // Internal memory
{
if (stat((path + "system").c_str(), &info) >= 0 &&
stat((path + "data").c_str(), &info) >= 0 &&
stat((path + "cache").c_str(), &info) >= 0)
return ROM_ANDROID_INTERNAL;
if(stat((path + "root").c_str(), &info) >= 0)
return ROM_UBUNTU_INTERNAL;
}
else // USB roms
{
if (stat((path + "system").c_str(), &info) >= 0 &&
stat((path + "data").c_str(), &info) >= 0 &&
stat((path + "cache").c_str(), &info) >= 0)
return ROM_ANDROID_USB_DIR;
if (stat((path + "system.img").c_str(), &info) >= 0 &&
stat((path + "data.img").c_str(), &info) >= 0 &&
stat((path + "cache.img").c_str(), &info) >= 0)
return ROM_ANDROID_USB_IMG;
if(stat((path + "root").c_str(), &info) >= 0)
return ROM_UBUNTU_USB_DIR;
if(stat((path + "root.img").c_str(), &info) >= 0)
return ROM_UBUNTU_USB_IMG;
}
return ROM_UNKNOWN;
}
static bool rom_sort(std::string a, std::string b)
{
if(a == INTERNAL_NAME)
return true;
if(b == INTERNAL_NAME)
return false;
return a.compare(b) < 0;
}
std::string MultiROM::listRoms()
{
DIR *d = opendir(getRomsPath().c_str());
if(!d)
return "";
std::vector<std::string> vec;
struct dirent *dr;
while((dr = readdir(d)) != NULL)
{
if(dr->d_type != DT_DIR)
continue;
if(dr->d_name[0] == '.')
continue;
vec.push_back(dr->d_name);
}
closedir(d);
std::sort(vec.begin(), vec.end(), rom_sort);
std::string res = "";
for(size_t i = 0; i < vec.size(); ++i)
res += vec[i] + "\n";
return res;
}
MultiROM::config MultiROM::loadConfig()
{
config cfg;
FILE *f = fopen((m_path + "/multirom.ini").c_str(), "r");
if(f)
{
char line[512];
char *p;
std::string name, val;
while(fgets(line, sizeof(line), f))
{
p = strtok(line, "=\n");
if(!p)
continue;
name = p;
p = strtok(NULL, "=\n");
if(!p)
continue;
val = p;
if(name == "current_rom")
cfg.current_rom = val;
else if(name == "auto_boot_seconds")
cfg.auto_boot_seconds = atoi(val.c_str());
else if(name == "auto_boot_rom")
cfg.auto_boot_rom = val;
else if(name == "colors")
cfg.colors = atoi(val.c_str());
else if(name == "brightness")
cfg.brightness = atoi(val.c_str());
else if(name == "enable_adb")
cfg.enable_adb = atoi(val.c_str());
}
fclose(f);
}
return cfg;
}
void MultiROM::saveConfig(const MultiROM::config& cfg)
{
FILE *f = fopen((m_path + "/multirom.ini").c_str(), "w");
if(!f)
return;
fprintf(f, "current_rom=%s\n", cfg.current_rom.c_str());
fprintf(f, "auto_boot_seconds=%d\n", cfg.auto_boot_seconds);
fprintf(f, "auto_boot_rom=%s\n", cfg.auto_boot_rom.c_str());
fprintf(f, "colors=%d\n", cfg.colors);
fprintf(f, "brightness=%d\n", cfg.brightness);
fprintf(f, "enable_adb=%d\n", cfg.enable_adb);
fclose(f);
}
bool MultiROM::changeMounts(std::string name)
{
int type = getType(name);
std::string base = getRomsPath() + name;
mkdir(REALDATA, 0777);
if(mount("/dev/block/platform/sdhci-tegra.3/by-name/UDA",
REALDATA, "ext4", MS_RELATIME | MS_NOATIME,
"user_xattr,acl,barrier=1,data=ordered,discard") < 0)
{
gui_print("Failed to mount realdata: %d (%s)", errno, strerror(errno));
return false;
}
if(M(type) & MASK_INTERNAL)
base.replace(0, 5, REALDATA);
static const char *files[] = {
"/etc/fstab",
"/etc/recovery.fstab",
NULL
};
for(size_t i = 0; i < m_mount_bak.size(); ++i)
delete[] m_mount_bak[i].content;
m_mount_bak.clear();
for(int i = 0; files[i]; ++i)
{
FILE *f = fopen(files[i], "r");
if(!f)
return false;
fseek(f, 0, SEEK_END);
int size = ftell(f);
rewind(f);
file_backup b;
b.name = files[i];
b.size = size;
b.content = new char[size]();
fread(b.content, 1, size, f);
fclose(f);
m_mount_bak.push_back(b);
}
system("umount /system /data /cache");
FILE *f_fstab = fopen("/etc/fstab", "w");
if(!f_fstab)
return false;
FILE *f_rec = fopen("/etc/recovery.fstab", "w");
if(!f_rec)
{
fclose(f_fstab);
return false;
}
// remove spaces from path
size_t idx = base.find(' ');
if(idx != std::string::npos)
m_mount_rom_paths[0] = base;
else
m_mount_rom_paths[0].clear();
while(idx != std::string::npos)
{
base.replace(idx, 1, "-");
idx = base.find(' ', idx);
}
struct stat info;
while(!m_mount_rom_paths[0].empty() && stat(base.c_str(), &info) >= 0)
base += "a";
if(!m_mount_rom_paths[0].empty())
{
m_mount_rom_paths[1] = base;
std::string cmd = "mv \"" + m_mount_rom_paths[0] + "\" \"" + base + "\"";
system(cmd.c_str());
}
fprintf(f_rec, "# mount point\tfstype\t\tdevice\n");
if(!(M(type) & MASK_IMAGES))
{
fprintf(f_rec, "/system\t\text4\t\t%s/system\n", base.c_str());
fprintf(f_rec, "/cache\t\text4\t\t%s/cache\n", base.c_str());
fprintf(f_rec, "/data\t\text4\t\t%s/data\n", base.c_str());
}
else
{
fprintf(f_rec, "/system\t\text4\t\t%s/system.img\n", base.c_str());
fprintf(f_rec, "/cache\t\text4\t\t%s/cache.img\n", base.c_str());
fprintf(f_rec, "/data\t\text4\t\t%s/data.img\n", base.c_str());
}
fprintf(f_rec, "/misc\t\temmc\t\t/dev/block/platform/sdhci-tegra.3/by-name/MSC\n");
fprintf(f_rec, "/boot\t\temmc\t\t/dev/block/platform/sdhci-tegra.3/by-name/LNX\n");
fprintf(f_rec, "/recovery\t\temmc\t\t/dev/block/platform/sdhci-tegra.3/by-name/SOS\n");
fprintf(f_rec, "/staging\t\temmc\t\t/dev/block/platform/sdhci-tegra.3/by-name/USP\n");
fprintf(f_rec, "/usb-otg\t\tvfat\t\t/dev/block/sda1\n");
fclose(f_rec);
if(!(M(type) & MASK_IMAGES))
{
fprintf(f_fstab, "%s/system /system ext4 rw,bind\n", base.c_str());
fprintf(f_fstab, "%s/cache /cache ext4 rw,bind\n", base.c_str());
fprintf(f_fstab, "%s/data /data ext4 rw,bind\n", base.c_str());
}
else
{
fprintf(f_fstab, "%s/system.img /system ext4 loop 0 0\n", base.c_str());
fprintf(f_fstab, "%s/cache.img /cache ext4 loop 0 0\n", base.c_str());
fprintf(f_fstab, "%s/data.img /data ext4 loop 0 0\n", base.c_str());
}
fprintf(f_fstab, "/usb-otg vfat rw\n");
fclose(f_fstab);
system("mount /system");
system("mount /data");
system("mount /cache");
system("mv /sbin/umount /sbin/umount.bak");
//load_volume_table();
return true;
}
void MultiROM::restoreMounts()
{
system("mv /sbin/umount.bak /sbin/umount");
system("umount /system /data /cache");
for(size_t i = 0; i < m_mount_bak.size(); ++i)
{
file_backup &b = m_mount_bak[i];
FILE *f = fopen(b.name.c_str(), "w");
if(f)
{
fwrite(b.content, 1, b.size, f);
fclose(f);
}
delete[] b.content;
}
m_mount_bak.clear();
if(!m_mount_rom_paths[0].empty())
{
std::string cmd = "mv \"" + m_mount_rom_paths[1] + "\" \"" + m_mount_rom_paths[0] + "\"";
system(cmd.c_str());
m_mount_rom_paths[0].clear();
}
system("umount "REALDATA);
//load_volume_table();
system("mount /data");
}
#define MR_UPDATE_SCRIPT_PATH "META-INF/com/google/android/"
#define MR_UPDATE_SCRIPT_NAME "META-INF/com/google/android/updater-script"
bool MultiROM::flashZip(std::string rom, std::string file)
{
gui_print("Flashing ZIP file %s\n", file.c_str());
gui_print("ROM: %s\n", rom.c_str());
gui_print("Preparing ZIP file...\n");
if(!prepareZIP(file))
return false;
gui_print("Changing mountpoints\n");
if(!changeMounts(rom))
{
gui_print("Failed to change mountpoints!\n");
return false;
}
if(file.find("/sdcard/") != std::string::npos)
{
struct stat info;
if(stat(REALDATA"/media/0", &info) >= 0)
file.replace(0, strlen("/sdcard/"), REALDATA"/media/0/");
else
file.replace(0, strlen("/sdcard/"), REALDATA"/media/");
}
else if(file.find("/data/media/") != std::string::npos)
file.replace(0, strlen("/data/"), REALDATA"/");
int wipe_cache = 0;
int status = TWinstall_zip(file.c_str(), &wipe_cache);
system("rm -r "MR_UPDATE_SCRIPT_PATH);
if(file == "/tmp/mr_update.zip")
system("rm /tmp/mr_update.zip");
if(status != INSTALL_SUCCESS)
gui_print("Failed to install ZIP!\n");
else
gui_print("ZIP successfully installed\n");
restoreMounts();
return (status == INSTALL_SUCCESS);
}
bool MultiROM::skipLine(const char *line)
{
if(strstr(line, "mount") && (!strstr(line, "bin/mount") || strstr(line, "run_program")))
return true;
if(strstr(line, "format"))
return true;
if(strstr(line, "/dev/block/platform/sdhci-tegra.3/"))
return true;
if (strstr(line, "boot.img") || strstr(line, "/dev/block/mmcblk0p2") ||
strstr(line, "/dev/block/platform/sdhci-tegra.3/by-name/LNX"))
return true;
return false;
}
bool MultiROM::prepareZIP(std::string& file)
{
bool res = false;
const ZipEntry *script_entry;
int script_len;
char* script_data;
int itr = 0;
char *token;
bool changed = false;
char cmd[512];
system("rm /tmp/mr_update.zip");
struct stat info;
if(stat(file.c_str(), &info) >= 0 && info.st_size < 450*1024*1024)
{
gui_print("Copying ZIP to /tmp...\n");
sprintf(cmd, "cp \"%s\" /tmp/mr_update.zip", file.c_str());
system(cmd);
file = "/tmp/mr_update.zip";
}
else
{
gui_print(" \n");
gui_print("=======================================================\n");
gui_print("WARN: Modifying the real ZIP, it is too big!\n");
gui_print("The ZIP file is now unusable for non-MultiROM flashing!\n");
gui_print("=======================================================\n");
gui_print(" \n");
}
sprintf(cmd, "mkdir -p /tmp/%s", MR_UPDATE_SCRIPT_PATH);
system(cmd);
sprintf(cmd, "/tmp/%s", MR_UPDATE_SCRIPT_NAME);
FILE *new_script = fopen(cmd, "w");
if(!new_script)
return false;
ZipArchive zip;
if (mzOpenZipArchive(file.c_str(), &zip) != 0)
goto exit;
script_entry = mzFindZipEntry(&zip, MR_UPDATE_SCRIPT_NAME);
if(!script_entry)
goto exit;
if (read_data(&zip, script_entry, &script_data, &script_len) < 0)
goto exit;
mzCloseZipArchive(&zip);
token = strtok(script_data, "\n");
while(token)
{
if(!skipLine(token))
{
fputs(token, new_script);
fputc('\n', new_script);
}
else
changed = true;
token = strtok(NULL, "\n");
}
free(script_data);
fclose(new_script);
if(changed)
{
sprintf(cmd, "cd /tmp && zip %s %s", file.c_str(), MR_UPDATE_SCRIPT_NAME);
if(system(cmd) < 0)
return false;
}
else
gui_print("No need to change ZIP.");
return true;
exit:
mzCloseZipArchive(&zip);
fclose(new_script);
return false;
}
bool MultiROM::injectBoot(std::string img_path)
{
char cmd[256];
std::string path_trampoline = m_path + "/trampoline";
struct stat info;
if (stat(path_trampoline.c_str(), &info) < 0)
{
gui_print("%s not found!\n", path_trampoline.c_str());
return false;
}
// EXTRACT BOOTIMG
gui_print("Extracting boot image...\n");
system("rm -r /tmp/boot; mkdir /tmp/boot");
sprintf(cmd, "unpackbootimg -i \"%s\" -o /tmp/boot/", img_path.c_str());
system(cmd);
std::string p = img_path.substr(img_path.find_last_of("/")+1);
sprintf(cmd, "/tmp/boot/%s-zImage", p.c_str());
if(stat(cmd, &info) < 0)
{
gui_print("Failed to unpack boot img!\n");
return false;
}
// DECOMPRESS RAMDISK
gui_print("Decompressing ramdisk...\n");
system("mkdir /tmp/boot/rd");
sprintf(cmd, "/tmp/boot/%s-ramdisk.gz", p.c_str());
int rd_cmpr = decompressRamdisk(cmd, "/tmp/boot/rd/");
if(rd_cmpr == -1 || stat("/tmp/boot/rd/init", &info) < 0)
{
gui_print("Failed to decompress ramdisk!\n");
return false;
}
// COPY TRAMPOLINE
gui_print("Copying trampoline...\n");
if(stat("/tmp/boot/rd/main_init", &info) < 0)
system("mv /tmp/boot/rd/init /tmp/boot/rd/main_init");
sprintf(cmd, "cp \"%s\" /tmp/boot/rd/init", path_trampoline.c_str());
system(cmd);
system("chmod 750 /tmp/boot/rd/init");
system("ln -sf ../main_init /tmp/boot/rd/sbin/ueventd");
// COMPRESS RAMDISK
gui_print("Compressing ramdisk...\n");
sprintf(cmd, "/tmp/boot/%s-ramdisk.gz", p.c_str());
if(!compressRamdisk("/tmp/boot/rd", cmd, rd_cmpr))
return false;
// PACK BOOT IMG
gui_print("Packing boot image\n");
FILE *script = fopen("/tmp/boot/create.sh", "w");
if(!script)
{
gui_print("Failed to open script file!\n");
return false;
}
std::string base_cmd = "mkbootimg --kernel /tmp/boot/%s-zImage --ramdisk /tmp/boot/%s-ramdisk.gz "
"--cmdline \"$(cat /tmp/boot/%s-cmdline)\" --base $(cat /tmp/boot/%s-base) --output /tmp/newboot.img\n";
for(size_t idx = base_cmd.find("%s", 0); idx != std::string::npos; idx = base_cmd.find("%s", idx))
base_cmd.replace(idx, 2, p);
fputs(base_cmd.c_str(), script);
fclose(script);
system("chmod 777 /tmp/boot/create.sh && /tmp/boot/create.sh");
if(stat("/tmp/newboot.img", &info) < 0)
{
gui_print("Failed to pack boot image!\n");
return false;
}
system("rm -r /tmp/boot");
if(img_path == "/dev/block/mmcblk0p2")
system("dd bs=4096 if=/tmp/newboot.img of=/dev/block/mmcblk0p2");
else
{
sprintf(cmd, "cp /tmp/newboot.img \"%s\"", img_path.c_str());;
system(cmd);
}
return true;
}
int MultiROM::decompressRamdisk(const char *src, const char* dest)
{
FILE *f = fopen(src, "r");
if(!f)
{
gui_print("Failed to open initrd\n");
return -1;
}
char m[4];
if(fread(m, 1, sizeof(m), f) != sizeof(m))
{
gui_print("Failed to read initrd magic\n");
fclose(f);
return -1;
}
fclose(f);
char cmd[256];
// gzip
if(*((uint16_t*)m) == 0x8B1F)
{
gui_print("Ramdisk uses GZIP compression\n");
sprintf(cmd, "cd \"%s\" && gzip -d -c \"%s\" | cpio -i", dest, src);
system(cmd);
return CMPR_GZIP;
}
// lz4
else if(*((uint32_t*)m) == 0x184C2102)
{
gui_print("Ramdisk uses LZ4 compression\n");
sprintf(cmd, "cd \"%s\" && lz4 -d \"%s\" stdout | cpio -i", dest, src);
system(cmd);
return CMPR_LZ4;
}
// lzma
else if(*((uint32_t*)m) == 0x0000005D || *((uint32_t*)m) == 0x8000005D)
{
gui_print("Ramdisk uses LZMA compression\n");
sprintf(cmd, "cd \"%s\" && lzma -d -c \"%s\" | cpio -i", dest, src);
system(cmd);
return CMPR_LZMA;
}
else
gui_print("Unknown ramdisk compression (%X %X %X %X)\n", m[0], m[1], m[2], m[3]);
return -1;
}
bool MultiROM::compressRamdisk(const char* src, const char* dst, int cmpr)
{
char cmd[256];
switch(cmpr)
{
case CMPR_GZIP:
sprintf(cmd, "cd \"%s\" && find . | cpio -o -H newc | gzip > \"%s\"", src, dst);
system(cmd);
return true;
case CMPR_LZ4:
sprintf(cmd, "cd \"%s\" && find . | cpio -o -H newc | lz4 stdin \"%s\"", src, dst);
system(cmd);
return true;
// FIXME: busybox can't compress with lzma
case CMPR_LZMA:
gui_print("Recovery can't compress ramdisk using LZMA!\n");
return false;
// sprintf(cmd, "cd \"%s\" && find . | cpio -o -H newc | lzma > \"%s\"", src, dst);
// system(cmd);
// return true;
default:
gui_print("Invalid compression type: %d", cmpr);
return false;
}
}
int MultiROM::copyBoot(std::string& orig, std::string rom)
{
std::string img_path = getRomsPath() + "/" + rom + "/boot.img";
char cmd[256];
sprintf(cmd, "cp \"%s\" \"%s\"", orig.c_str(), img_path.c_str());
if(system(cmd) != 0)
return 1;
orig.swap(img_path);
return 0;
}
std::string MultiROM::getNewRomName(std::string zip, std::string def)
{
std::string name = "ROM";
if(def.empty())
{
size_t idx = zip.find_last_of("/");
size_t idx_dot = zip.find_last_of(".");
if(zip.substr(idx) == "/rootfs.img")
name = "Ubuntu";
else if(idx != std::string::npos && idx_dot != std::string::npos && idx_dot > idx)
name = zip.substr(idx+1, idx_dot-idx-1);
}
else
name = def;
if(name.size() > MAX_ROM_NAME)
name.resize(MAX_ROM_NAME);
DIR *d = opendir(getRomsPath().c_str());
if(!d)
return "";
std::vector<std::string> roms;
struct dirent *dr;
while((dr = readdir(d)))
{
if(dr->d_name[0] == '.')
continue;
if(dr->d_type != DT_DIR && dr->d_type != DT_LNK)
continue;
roms.push_back(dr->d_name);
}
closedir(d);
std::string res = name;
char num[8] = { 0 };
int c = 1;
for(size_t i = 0; i < roms.size();)
{
if(roms[i] == res)
{
res = name;
sprintf(num, "%d", c++);
if(res.size() + strlen(num) > MAX_ROM_NAME)
res.replace(res.size()-strlen(num), strlen(num), num);
else
res += num;
i = 0;
}
else
++i;
}
return res;
}
bool MultiROM::createImage(const std::string& base, const char *img, int size)
{
gui_print("Creating %s.img...\n", img);
if(size <= 0)
{
gui_print("Failed to create %s image: invalid size (%d)\n", img, size);
return false;
}
char cmd[256];
sprintf(cmd, "dd if=/dev/zero of=\"%s/%s.img\" bs=1M count=%d", base.c_str(), img, size);
system(cmd);
struct stat info;
sprintf(cmd, "%s/%s.img", base.c_str(), img);
if(stat(cmd, &info) < 0)
{
gui_print("Failed to create %s image, probably not enough space.\n", img);
return false;
}
sprintf(cmd, "make_ext4fs -l %dM \"%s/%s.img\"", size, base.c_str(), img);
system(cmd);
return true;
}
bool MultiROM::createImagesFromBase(const std::string& base)
{
for(baseFolders::const_iterator itr = m_base_folders.begin(); itr != m_base_folders.end(); ++itr)
if(!createImage(base, itr->first.c_str(), itr->second.size))
return false;
return true;
}
bool MultiROM::createDirsFromBase(const string& base)
{
for(baseFolders::const_iterator itr = m_base_folders.begin(); itr != m_base_folders.end(); ++itr)
{
if (mkdir((base + "/" + itr->first).c_str(), 0777) < 0)
{
gui_print("Failed to create folder %s/%s!\n", base.c_str(), itr->first.c_str());
return false;
}
}
return true;
}
bool MultiROM::createDirs(std::string name, int type)
{
std::string base = getRomsPath() + "/" + name;
if(mkdir(base.c_str(), 0777) < 0)
{
gui_print("Failed to create ROM folder!\n");
return false;
}
gui_print("Creating folders and images for type %d\n", type);
switch(type)
{
case ROM_ANDROID_INTERNAL:
case ROM_ANDROID_USB_DIR:
if (mkdir((base + "/boot").c_str(), 0777) < 0 ||
mkdir((base + "/system").c_str(), 0755) < 0 ||
mkdir((base + "/data").c_str(), 0771) < 0 ||
mkdir((base + "/cache").c_str(), 0770) < 0)
{
gui_print("Failed to create android folders!\n");
return false;
}
break;
case ROM_ANDROID_USB_IMG:
if (mkdir((base + "/boot").c_str(), 0777) < 0)
{
gui_print("Failed to create android folders!\n");
return false;
}
if(!createImagesFromBase(base))
return false;
break;
case ROM_UBUNTU_INTERNAL:
case ROM_UBUNTU_USB_DIR:
case ROM_INSTALLER_INTERNAL:
case ROM_INSTALLER_USB_DIR:
if(!createDirsFromBase(base))
return false;
break;
case ROM_UBUNTU_USB_IMG:
case ROM_INSTALLER_USB_IMG:
if(!createImagesFromBase(base))
return false;
break;
default:
gui_print("Unknown ROM type %d!\n", type);
return false;
}
return true;
}
bool MultiROM::androidExportBoot(std::string name, std::string zip_path, int type)
{
gui_print("Processing boot.img of ROM %s...\n", name.c_str());
std::string base = getRomsPath() + "/" + name;
char cmd[256];