-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate_gxl_gxm_bootloader
executable file
·289 lines (255 loc) · 8.39 KB
/
generate_gxl_gxm_bootloader
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
#!/bin/sh
# Usage function
function usage() {
cat >&2 <<EOF
${SH_FILE} is used to convert official U-Boot generated binary into a usable bootloader for Amlogic GXL/GXM SoC
Usage: ${SH_FILE} [a-] -b board [-c] [-h] [-n] [-o] [-s] -u dir[:branch]
Where:
-a use official ARM ATF fimrware
-b name of the board to configure (for example: kvim)
-c copy the binary blob files in the current directory
-g use gxlimg to generate image instead of aml_encrypt_gxl
-h this help page
-n don't compile Amlogic U-Boot (use pre-compiled binary blob)
-o don't copy the bl33.bin from official U-Boot
-s "burn" the generated U-Boot file on a sdcard
-u U-Boot Amlogic version source directory / pre-compiled binary blob directory
EOF
exit 0
}
# Error function
function error() {
echo -e "$@" >&2
exit 1
}
# Copy/Check function
function copy() {
typeset -r FILE=$1
typeset -r DEST=$2
cp ${FILE} ${DEST} >/dev/null 2>&1 \
|| error "Error while copying file ${FILE} to ${DEST}!"
}
# Variable(s)
typeset -r SH_FILE=${0##*/}
typeset -r U_BOOT_DIR=/dev/shm/u-boot-tmp
typeset -r TMPFS=/dev/shm/u-boot-amlogic-tmp
typeset -r FIPDIR=${U_BOOT_DIR}/fip
typeset -r BLOB_FIPDIR=${TMPFS}/fip
typeset -r AML_ENCRYPT_BIN=${TMPFS}/fip/gxl/aml_encrypt_gxl
typeset -r GXLIMG_BIN=gxlimg
typeset -i ATF_FW=0
typeset -i BURN=0
typeset -i COPY_UBOOT_BL33=1
typeset -i COPY_FW=0
typeset -i USE_GXLIMG=0
typeset -i COMPILE_AMLOGIC=1
typeset -l BURN_FILE=u-boot.bin.sd.bin
typeset -r BL31_NAME=bl31.img
typeset AMLOGIC_U_BOOT_DIR BRANCH BOARD DEV FW_DIR
# Get options
while getopts "ab:cghnosu:" ARG; do
case ${ARG} in
a) ATF_FW=1
;;
b) BOARD=${OPTARG}
;;
c) COPY_FW=1
;;
g) USE_GXLIMG=1
BURN_FILE=u-boot.bin
;;
n) COMPILE_AMLOGIC=0
;;
o) COPY_UBOOT_BL33=0
;;
s) BURN=1
DEV=${MMC_DEV:=/dev/mmcblk0}
;;
u) AMLOGIC_U_BOOT_DIR=${OPTARG%:*}
BRANCH=${OPTARG#*:}
;;
h|*) usage
;;
esac
done
shift $((OPTIND-1))
# Board model must be filled!
[[ -z "${BOARD}" ]] \
&& error "Board model must be filled!"
# Amlogic U-Boot directory must be filled!
[[ -z "${AMLOGIC_U_BOOT_DIR}" ]] \
&& error "Amlogic U-Boot directory must be filled!"
# Informative message
(( USE_GXLIMG )) \
&& echo "- Using gxlimg!" \
|| echo "- Using aml_encrypt_gxl :-("
# Export needed variables
export ARCH=arm
export CROSS_COMPILE=aarch64-elf-
export CROSS_COMPILE_T32=arm-eabi-
export PATH=~/cross-compile/aarch64/gcc-linaro-4.9.4-2017.01-x86_64_aarch64-elf/bin:~/cross-compile/aarch64/gcc-linaro-4.9.4-2017.01-x86_64_arm-eabi/bin:~/cross-compile/aarch64/gcc-linaro-aarch64-none-elf-4.8-2013.11_linux/bin:$PATH
#export PATH=~/cross-compile/aarch64/gcc-linaro-7.5.0-2019.12-x86_64_aarch64-elf/bin:~/cross-compile/aarch64/gcc-linaro-7.5.0-2019.12-x86_64_arm-eabi/bin:${PATH}
# Configure board name for some files
FW_DIR=amlogic/${BOARD}
case ${BOARD} in
kvim|kvim2)
CODE_NAME=khadas-${BOARD#k}
FW_DIR=khadas/${BOARD}
;;
libretech_ac)
CODE_NAME=lafrite
;;
libretech_cc)
CODE_NAME=lepotato
;;
esac
# Remove old TMPFS and re-create it
echo "- Cleaning/Creating ${TMPFS} directory..."
rm -rf ${TMPFS} \
|| error "Cannot clean temporary directory ${TMPFS}!"
mkdir -p ${TMPFS} \
|| error "Cannot create temporary directory ${TMPFS}!"
# Copy u-boot source in the tmpfs directory
rsync -aq ${AMLOGIC_U_BOOT_DIR}/ ${TMPFS} \
|| error "Cannot copy ${AMLOGIC_U_BOOT_DIR}/* in ${TMPFS}!"
if (( COMPILE_AMLOGIC )); then
# Go to temp U-Boot dir
cd ${TMPFS}
# Select the correct branch if needed
if [[ -n "${BRANCH}" ]]; then
echo "- Select branch '${BRANCH}' for Amlogic U-Boot..."
git checkout ${BRANCH} || error "Failed to select branch!"
fi
echo "- Cleaning/Creating Amlogic U-Boot build directory..."
make clean && make mrproper && make distclean
# Configure Amlogic U-Boot
echo "- Configure Amlogic U-Boot for ${BOARD} board..."
make ${BOARD}_defconfig
# Compile Amlogic U-Boot
echo "- Compiling Amlogic U-Boot..."
make || error "Failed to compile Amlogic U-Boot!"
# We need to go back into original directory
cd ${OLDPWD}
fi
# Remove old FIPDIR and re-create it
echo "- Cleaning/Creating ${FIPDIR} directory..."
rm -rf ${FIPDIR} \
|| error "Cannot clean FIP directory ${FIPDIR}!"
mkdir -p ${FIPDIR} \
|| error "Cannot create FIP directory ${FIPDIR}!"
# Copy compiled u-boot.bin as BL3-3
echo "- Copying BL3-3 file..."
(( COPY_UBOOT_BL33 )) \
&& copy ${U_BOOT_DIR}/u-boot.bin ${FIPDIR}/bl33.bin \
|| copy ${BLOB_FIPDIR}/gxl/bl33.bin ${FIPDIR}/bl33.bin
# Copy needed binary blob files
echo "- Copying binary blob files..."
for FW in bl2.bin bl30.bin; do
if [[ "${BOARD}" =~ libretech_ac|libretech_cc ]]; then
curl -sfL https://raw.githubusercontent.com/LibreELEC/amlogic-boot-fip/master/${CODE_NAME}/${FW} \
-o ${FIPDIR}/${FW}
elif [[ "${BOARD}" =~ kvim|kvim2 ]]; then
copy ~/firmware/amlogic-binary-blob/${FW} ${FIPDIR}
else
copy ${BLOB_FIPDIR}/gxl/${FW}.bin ${FIPDIR}
fi
done
if (( COMPILE_AMLOGIC )); then
copy ${TMPFS}/build/board/${FW_DIR}/firmware/acs.bin ${FIPDIR}
copy ${TMPFS}/build/board/${FW_DIR}/firmware/bl21.bin ${FIPDIR}
copy ${TMPFS}/build/scp_task/bl301.bin ${FIPDIR}
else
copy ${TMPFS}/fip/gxl/acs.bin ${FIPDIR}
copy ${TMPFS}/fip/gxl/bl21.bin ${FIPDIR}
copy ${TMPFS}/fip/gxl/bl301.bin ${FIPDIR}
fi
if (( ATF_FW )); then
echo "- Using ATF BL31..."
copy atf-bl31.bin ${FIPDIR}/${BL31_NAME} \
|| error "Error while copying ATF firmware!"
else
echo "- Using Amlogic BL31..."
FW=bl31.img
curl -sfL https://raw.githubusercontent.com/LibreELEC/amlogic-boot-fip/master/${CODE_NAME}/${BL31_NAME} \
-o ${FIPDIR}/${BL31_NAME}
fi
echo "- Copying scripts..."
for SCRIPT in acs_tool.py blx_fix.sh; do
curl -sfL https://raw.githubusercontent.com/LibreELEC/amlogic-boot-fip/master/${CODE_NAME}/${SCRIPT} \
-o ${FIPDIR}/${SCRIPT}
done
# Create BL3-0
echo "- Creating BL3-0..."
sh ${FIPDIR}/blx_fix.sh \
${FIPDIR}/bl30.bin \
${FIPDIR}/zero_tmp \
${FIPDIR}/bl30_zero.bin \
${FIPDIR}/bl301.bin \
${FIPDIR}/bl301_zero.bin \
${FIPDIR}/bl30_new.bin \
bl30
# Create BL2-ACS file
echo "- Creating BL2-ACS file..."
python ${FIPDIR}/acs_tool.py ${FIPDIR}/bl2.bin ${FIPDIR}/bl2_acs.bin ${FIPDIR}/acs.bin 0
# Create BL2 file
echo "- Creating BL2 file..."
sh ${FIPDIR}/blx_fix.sh \
${FIPDIR}/bl2_acs.bin \
${FIPDIR}/zero_tmp \
${FIPDIR}/bl2_zero.bin \
${FIPDIR}/bl21.bin \
${FIPDIR}/bl21_zero.bin \
${FIPDIR}/bl2_new.bin \
bl2
# Encode BL files
echo "- Encrypting BL* files..."
if (( USE_GXLIMG )); then
${GXLIMG_BIN} -t bl2 -s ${FIPDIR}/bl2_new.bin ${FIPDIR}/bl2_new.bin.sig
${GXLIMG_BIN} -t bl3x -c ${FIPDIR}/bl30_new.bin ${FIPDIR}/bl30_new.bin.enc
${GXLIMG_BIN} -t bl3x -c ${FIPDIR}/bl31.img ${FIPDIR}/bl31.img.enc
${GXLIMG_BIN} -t bl3x -c ${FIPDIR}/bl33.bin ${FIPDIR}/bl33.bin.enc
else
${AML_ENCRYPT_BIN} --bl2sig --input ${FIPDIR}/bl2_new.bin
${AML_ENCRYPT_BIN} --bl3enc --input ${FIPDIR}/bl30_new.bin
${AML_ENCRYPT_BIN} --bl3enc --input ${FIPDIR}/bl31.img
${AML_ENCRYPT_BIN} --bl3enc --input ${FIPDIR}/bl33.bin
fi
# Create u-boot.* files
echo "- Creating final u-boot.bin files..."
if (( USE_GXLIMG )); then
${GXLIMG_BIN} -t fip \
--bl2 ${FIPDIR}/bl2_new.bin.sig \
--bl30 ${FIPDIR}/bl30_new.bin.enc \
--bl31 ${FIPDIR}/bl31.img.enc \
--bl33 ${FIPDIR}/bl33.bin.enc \
${FIPDIR}/u-boot.bin
else
${AML_ENCRYPT_BIN} \
--bootmk \
--output ${FIPDIR}/u-boot.bin \
--bl2 ${FIPDIR}/bl2_new.bin.sig \
--bl30 ${FIPDIR}/bl30_new.bin.enc \
--bl31 ${FIPDIR}/bl31.img.enc \
--bl33 ${FIPDIR}/bl33.bin.enc
fi
# Copy U-Boot files in the current directory
if (( COPY_FW )); then
for FILE in ${FIPDIR}/bl33.bin ${FIPDIR}/u-boot.*; do
echo "- Copy firmware file ${FILE##*/}..."
copy ${FILE} ${PWD}
done
fi
# Write U-Boot to sd-card
if (( BURN )); then
echo "- Burning ${BURN_FILE}..."
if (( USE_GXLIMG )); then
dd if=${FIPDIR}/${BURN_FILE} of=${DEV} conv=fsync,notrunc bs=512 seek=1
dd if=/dev/zero of=${DEV} conv=fsync,notrunc bs=1 count=444
else
dd if=${FIPDIR}/${BURN_FILE} of=${DEV} conv=fsync,notrunc bs=512 skip=1 seek=1
dd if=${FIPDIR}/${BURN_FILE} of=${DEV} conv=fsync,notrunc bs=1 count=444
fi
sync ${DEV}
fi
# Clean exit
exit 0