diff options
Diffstat (limited to 'tools')
-rwxr-xr-x | tools/hiby_patcher.pl | 165 |
1 files changed, 165 insertions, 0 deletions
diff --git a/tools/hiby_patcher.pl b/tools/hiby_patcher.pl new file mode 100755 index 0000000000..e1aa30733e --- /dev/null +++ b/tools/hiby_patcher.pl | |||
@@ -0,0 +1,165 @@ | |||
1 | #!/usr/bin/perl -w | ||
2 | ################## | ||
3 | # __________ __ ___. | ||
4 | # Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
5 | # Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
6 | # Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
7 | # Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
8 | # \/ \/ \/ \/ \/ | ||
9 | # | ||
10 | # Hiby patcher Copyright (C) 2020 Solomon Peachy <pizza@shaftnet.org> | ||
11 | # | ||
12 | # Licensed under the GNU GPLv2 (or newer) | ||
13 | # | ||
14 | ################## | ||
15 | |||
16 | # usage: hiby_patcher.pl model imagefile bootloaderfile | ||
17 | |||
18 | # need '7z', 'mkisofs', 'md5sum', 'mkfs.ubifs' | ||
19 | # and https://github.com/jrspruitt/ubi_reader | ||
20 | |||
21 | # Rocker: PEB size 128KiB, I/O 2048, LEBs size: 124KiB, max LEBs 1024 (?) | ||
22 | |||
23 | use strict; | ||
24 | |||
25 | use File::Basename; | ||
26 | |||
27 | my $model = $ARGV[0]; | ||
28 | my $inname = $ARGV[1]; | ||
29 | my $rbbname = $ARGV[2]; | ||
30 | |||
31 | my $prefix = "/tmp"; # XXX mktmp | ||
32 | my $debug = 0; # Set to 1 to prevent cleaning up work dirs | ||
33 | |||
34 | #### Let's get to work | ||
35 | |||
36 | my @ubiopts; | ||
37 | if ($model eq 'rocker') { | ||
38 | @ubiopts = ("-e", "124KiB", "-c", "1024", "-m", "2048", "-j", "8192KiB", "-U"); | ||
39 | } elsif ($model eq 'x3ii') { | ||
40 | @ubiopts = ("-e", "124KiB", "-c", "1024", "-m", "2048", "-j", "8192KiB", "-U"); | ||
41 | } elsif ($model eq 'x20') { | ||
42 | @ubiopts = ("-e", "124KiB", "-c", "1024", "-m", "2048", "-j", "8192KiB", "-U"); | ||
43 | } else { | ||
44 | die ("Unknown hiby model: $model\n"); | ||
45 | } | ||
46 | |||
47 | my $uptname = basename($inname); | ||
48 | my $uptnamenew = "rb-$uptname"; | ||
49 | my $isowork = "$prefix/iso.$uptname"; | ||
50 | my $rootfsdir = "$prefix/rootfs.$uptname"; | ||
51 | my $ubiname; | ||
52 | my $ubinamenew; | ||
53 | |||
54 | |||
55 | my @sysargs; | ||
56 | |||
57 | |||
58 | ### Extract outer ISO9660 image | ||
59 | system("rm -Rf $isowork"); | ||
60 | mkdir($isowork) || die ("Can't create '$isowork'"); | ||
61 | @sysargs = ("7z", "x", "-aoa", "-o$isowork", $inname); | ||
62 | system(@sysargs); | ||
63 | |||
64 | ### figure out the rootfs image filename | ||
65 | my $updatename; | ||
66 | if ( -e "$isowork/UPDATE.TXT") { | ||
67 | $updatename = "$isowork/UPDATE.TXT"; | ||
68 | } elsif ( -e "$isowork/update.txt") { | ||
69 | $updatename = "$isowork/update.txt"; | ||
70 | } | ||
71 | open UPDATE, $updatename || die ("Can't open update.txt!");; | ||
72 | |||
73 | my $rootfs_found = 0; | ||
74 | while (<UPDATE>) { | ||
75 | chomp; | ||
76 | if ($rootfs_found) { | ||
77 | if (/file_path=(.*)/) { | ||
78 | $ubiname = basename($1); | ||
79 | $ubiname =~ tr/[a-z]/[A-Z]/; | ||
80 | last; | ||
81 | } | ||
82 | } else { | ||
83 | if (/rootfs/) { | ||
84 | $rootfs_found = 1; | ||
85 | } | ||
86 | } | ||
87 | } | ||
88 | close UPDATE; | ||
89 | |||
90 | die("can't locate rootfs image") if (! -e "$isowork/$ubiname"); | ||
91 | $ubiname = "$isowork/$ubiname"; | ||
92 | |||
93 | ### Extract RootFS | ||
94 | system("rm -Rf $rootfsdir"); | ||
95 | mkdir($rootfsdir) || die ("Can't create '$rootfsdir'"); | ||
96 | |||
97 | @sysargs = ("ubireader_extract_files", "-k", "-o", $rootfsdir, $ubiname); | ||
98 | system(@sysargs); | ||
99 | |||
100 | ### Mangle RootFS | ||
101 | |||
102 | # Generate rb_bootloader.sh | ||
103 | my $rbbasename = basename($rbbname); | ||
104 | my $bootloader_sh = | ||
105 | "#!/bin/sh | ||
106 | |||
107 | mount /dev/mmcblk0 /mnt/sd_0 &>/dev/null || \ | ||
108 | mount /dev/mmcblk0p1 /mnt/sd_0 &>/dev/null | ||
109 | |||
110 | killall $rbbasename | ||
111 | killall -9 $rbbasename | ||
112 | |||
113 | /usr/bin/$rbbasename | ||
114 | sleep 1 | ||
115 | reboot | ||
116 | "; | ||
117 | open FILE, ">$rootfsdir/usr/bin/hiby_player.sh" || die ("can't write bootloader script!"); | ||
118 | print FILE $bootloader_sh; | ||
119 | close FILE; | ||
120 | |||
121 | # Copy bootloader over | ||
122 | @sysargs=("cp", "$rbbname", "$rootfsdir/usr/bin/$rbbasename"); | ||
123 | system(@sysargs); | ||
124 | |||
125 | ### Generate new RootFS UBI image | ||
126 | $ubinamenew = "$ubiname.new"; | ||
127 | |||
128 | @sysargs = ("mkfs.ubifs", @ubiopts, "-v", "-o", $ubinamenew, "-r", "$rootfsdir"); | ||
129 | system(@sysargs); | ||
130 | |||
131 | system("rm -Rf $rootfsdir") if (!$debug); | ||
132 | |||
133 | # md5sum | ||
134 | my $md5 = `md5sum $ubinamenew | cut -d ' ' -f 1`; | ||
135 | |||
136 | system("mv $ubinamenew $ubiname"); | ||
137 | |||
138 | ### Generate new ISO9660 update image | ||
139 | |||
140 | # Update version string as needed XXX | ||
141 | |||
142 | open UPDATE, "<$updatename" || die ("Can't open update.txt!");; | ||
143 | open UPDATEO, ">$updatename.new" || die ("Can't open update.txt!");; | ||
144 | |||
145 | $rootfs_found = 0; | ||
146 | while (<UPDATE>) { | ||
147 | if ($rootfs_found) { | ||
148 | if (s/md5=.*/md5=$md5/) { | ||
149 | print "#### $_ ####\n"; | ||
150 | $rootfs_found=0; | ||
151 | } | ||
152 | } else { | ||
153 | if (/rootfs/) { | ||
154 | $rootfs_found = 1; | ||
155 | } | ||
156 | } | ||
157 | print UPDATEO; | ||
158 | } | ||
159 | close UPDATE; | ||
160 | close UPDATEO; | ||
161 | system("mv $updatename.new $updatename"); | ||
162 | @sysargs = ("mkisofs", "-volid", "CDROM", "-o", $uptnamenew, $isowork); | ||
163 | system(@sysargs); | ||
164 | |||
165 | system("rm -Rf $isowork") if (!$debug); | ||