summaryrefslogtreecommitdiffstats
path: root/tools/hibyos_nativepatcher/hibyos_nativepatcher.sh
blob: 3c6661863fe9e5278de08d95ed04c73fe29525d3 (plain)
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
#!/bin/bash
# hibyos_nativepatcher.sh
#
# NOTE: THIS SCRIPT IS NOT TOLERANT OF WHITESPACE IN FILENAMES OR PATHS

usage="hibyos_nativepatcher.sh
    
    USAGE: 
    
    hibyos_nativepatcher.sh <mkrbinstall/mkstockuboot> [arguments depend on mode, see below]

    hibyos_nativepatcher.sh mkrbinstall <OFVERNAME (erosq or eros_h2)>
        <path/to/output> <path/to/bootloader.erosq> <HWVER (hw1hw2 or hw3)> 
        Output file will be path/to/output/erosqnative_RBVER-HWVER-OFVERNAME.upt.
        Only the Hifiwalker H2 v1.3 uses "eros_h2", everything else uses "erosq".

    hibyos_nativepatcher.sh mkstockuboot <path/to/OFupdatefile.upt>
        Output file will be path/to/OFupdatefile-rbuninstall.upt.
    
    NOTE: THIS SCRIPT IS NOT TOLERANT OF WHITESPACE IN FILENAMES OR PATHS!"

# check OS type and for any needed tools
if [[ "$OSTYPE" == "darwin"* ]]; then
    echo "$OSTYPE DETECTED"
elif [[ "$OSTYPE" == "linux-gnu"* ]]; then
    echo "$OSTYPE DETECTED"
    if !(which 7z > /dev/null); then
        echo "PLEASE INSTALL 7z (usually part of p7zip-full package)"
        exit 1
    fi
    if !(which genisoimage > /dev/null); then
        echo "PLEASE INSTALL genisoimage"
        exit 1
    fi
else
    echo "SCRIPT NOT IMPLEMENTED ON $OSTYPE YET!"
    exit 1
fi

# make sure we can find patch_manifest
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
if !(which $SCRIPT_DIR/patch_manifest.pl > /dev/null); then
    echo "couldn't find patch_manifest.pl!"
    exit 1
fi

###########################################################################
# MKRBINSTALL
###########################################################################
if [[ "$1" == "mkrbinstall" ]]; then
    echo "Creating installation image from bootloader file..."

    # make sure all arguments are accounted for...
    if [[ -z "$5" ]]; then
        echo "not all parameters included, please see usage:"
        echo "$usage"
        exit 1
    fi

    # validate arguments
    outputdir=$(realpath --relative-base=$(pwd) $3)
    if !(ls $outputdir >& /dev/null); then
        echo "directory $outputdir doesn't seem to exist. Please make sure it exists, then re-run hibyos_nativepatcher.sh."
        exit 1
    fi

    # note, bootloaderfile might still be a valid path, but not a valid bootloader file... check to make sure tar can extract it okay.
    bootloaderfile=$(realpath --relative-base=$(pwd) $4)
    if !(ls $bootloaderfile >& /dev/null); then
        echo "bootloader file $bootloaderfile doesn't seem to exist. Please make sure it exists, then re-run hibyos_nativepatcher.sh."
        exit 1
    fi

    # make working directory...
    mkdir $outputdir/working_dir
    workingdir=$(realpath $outputdir/working_dir)
    mkdir $workingdir/bootloader

    # extract bootloader file
    if [[ "$OSTYPE" == "darwin"* ]]; then
        # macos
        tar -xvf $bootloaderfile --cd $workingdir/bootloader
    elif [[ "$OSTYPE" == "linux-gnu"* ]]; then
        # linux-gnu
        tar -xvf $bootloaderfile -C $workingdir/bootloader
    fi

    # make sure we got what we wanted
    if !(ls $workingdir/bootloader/bootloader.ucl >& /dev/null); then
        echo "can't find bootloader.ucl! help!"
        rm -rf $workingdir
        exit 1
    elif !(ls $workingdir/bootloader/spl.erosq >& /dev/null); then
        echo "can't find spl.erosq! help!"
        rm -rf $workingdir
        exit 1
    fi

    bootver=$(cat $workingdir/bootloader/bootloader-info.txt)
    if [ -z "$bootver" ]; then
        echo "COULDN'T FIND BOOTLOADER-INFO!"
        rm -rf $workingdir
        exit 1
    fi

    # if uboot.bin already exists, something is weird.
    if (ls $workingdir/image_contents/uboot.bin >& /dev/null); then
        echo "$workingdir/image_contents/uboot.bin already exists, something went weird."
        rm -rf $workingdir
        exit 1
    fi

    # everything exists, make the bin
    mkdir $workingdir/image_contents/
    touch $workingdir/image_contents/uboot.bin
    echo "PATCHING!"
    dd if=$workingdir/bootloader/spl.erosq of=$workingdir/image_contents/uboot.bin obs=1 seek=0 conv=notrunc
    dd if=$workingdir/bootloader/bootloader.ucl of=$workingdir/image_contents/uboot.bin obs=1 seek=26624 conv=notrunc

    # create update.txt
    md5=($(md5sum $workingdir/image_contents/uboot.bin))
    if [ -z "$md5" ]; then
        echo "COULDN'T MD5SUM UBOOT.BIN!"
        rm -rf $workingdir
        exit 1
    fi
    echo "Create update manifest with md5sum $md5"
    echo "" > $workingdir/image_contents/update.txt
    $SCRIPT_DIR/patch_manifest.pl $md5 $workingdir/image_contents/update.txt

    # create version.txt
    echo "version={
        name=$2
        ver=2024-09-10T14:42:18+08:00
}" > $workingdir/image_contents/version.txt

    outputfilename="erosqnative_$bootver-$5-$2"


###########################################################################
# MKSTOCKUBOOT
###########################################################################
elif [[ "$1" == "mkstockuboot" ]]; then
    echo "Creating uninstallation image from stock update image..."

    # make sure all arguments are accounted for...
    if [[ -z "$2" ]]; then
        echo "not all parameters included, please see usage:"
        echo "$usage"
        exit 1
    fi

    updatefile=$(realpath --relative-base=$(pwd) $2)
    updatefile_path=$(echo "$updatefile" | perl -ne "s/\/[\w\.\_\-]*$// && print")
    updatefile_name=$(basename $updatefile)
    updatefile_name_noext=$(echo "$updatefile_name" | perl -ne "s/\.\w*$// && print")
    outputdir=$updatefile_path
    outputfilename="$updatefile_name_noext-rbuninstall"

    mkdir $updatefile_path/working_dir
    workingdir=$(realpath $updatefile_path/working_dir)

    # copy update.upt to update.iso
    cp $updatefile $workingdir/$updatefile_name_noext-cpy.iso

    mkdir $workingdir/image_contents

    # extract iso
    if [[ "$OSTYPE" == "darwin"* ]]; then
        # macos
        hdiutil attach $workingdir/$updatefile_name_noext-cpy.iso -mountpoint $workingdir/contentsiso

        # copy out iso contents
        cp $workingdir/contentsiso/* $workingdir/image_contents

        # unmount iso
        hdiutil detach $workingdir/contentsiso
    elif [[ "$OSTYPE" == "linux-gnu"* ]]; then
        # linux-gnu
        7z -o$workingdir/image_contents x $workingdir/$updatefile_name_noext-cpy.iso
    fi

    chmod 777 $workingdir/image_contents/*

    # modify update.txt
    md5=($(md5sum $workingdir/image_contents/uboot.bin))
    if [ -z "$md5" ]; then
        echo "COULDN'T MD5SUM UBOOT.BIN!"
        rm -rf $working_dir
        exit 1
    fi
    echo "add to update manifest with md5sum $md5"
    $SCRIPT_DIR/patch_manifest.pl $md5 $workingdir/image_contents/update.txt

######################################################################
# PRINT USAGE
######################################################################
else
    echo "$usage"
    exit 1
fi

######################################################################
# Common: make the image
######################################################################
# make the image
if [[ "$OSTYPE" == "darwin"* ]]; then
    # macos
    hdiutil makehybrid -iso -joliet -o $outputdir/output.iso $workingdir/image_contents/
elif [[ "$OSTYPE" == "linux-gnu"* ]]; then
    # linux-gnu
    genisoimage -o $outputdir/output.iso $workingdir/image_contents/
fi

# rename
mv $outputdir/output.iso $outputdir/$outputfilename.upt

# cleaning up
rm -rf $workingdir

exit 0