forked from ophub/amlogic-s9xxx-armbian
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rebuild
executable file
·686 lines (607 loc) · 25.6 KB
/
rebuild
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
#!/bin/bash
#==================================================================================
#
# This file is licensed under the terms of the GNU General Public
# License version 2. This program is licensed "as is" without any
# warranty of any kind, whether express or implied.
#
# This file is a part of the Armbian Rebuild and kernel Recompile script
# https://github.com/ophub/amlogic-s9xxx-armbian
#
# Description: Run on Ubuntu-20.04-x86_64, Build armbian for Amlogic s9xxx tv box
# Copyright (C) 2021- https://github.com/unifreq
# Copyright (C) 2021- https://github.com/ophub/amlogic-s9xxx-armbian
#
#========= Install the basic packages of rebuild armbian for Ubuntu 20.04 =========
#
# sudo apt-get update -y
# sudo apt-get full-upgrade -y
# sudo apt-get install -y $(curl -fsSL git.io/ubuntu-2004-server)
#
# Command: sudo ./rebuild -d
# Command optional parameters please refer to the source code repository
#
#================================= Functions list =================================
#
# error_msg : Output error message
# process_msg : Output process message
#
# init_var : Initialize all variables
# find_armbian : Find Armbian file (build/output/images/*.img)
# download_kernel : Download the latest kernel
#
# confirm_version : Confirm version type
# extract_armbian : Extract Armbian files
# refactor_files : Refactor related files
# make_image : Making Armbian file
# copy_files : Copy the Armbian files
# clean_tmp : Clear temporary files
#
# loop_rebuild : Loop to rebuild Armbian files
#
#========================= Set make environment variables =========================
#
# Related file storage path
make_path="${PWD}"
armbian_outputpath="${make_path}/build/output/images"
armbian_rebuild_file="${armbian_outputpath}/*.img"
build_path="${make_path}/build-armbian"
armbian_path="${build_path}/amlogic-armbian"
dtb_path="${build_path}/amlogic-dtb"
kernel_path="${build_path}/amlogic-kernel"
uboot_path="${build_path}/amlogic-u-boot"
configfiles_path="${build_path}/common-files"
tmp_dir="${make_path}/tmp_dir"
tmp_outpath="${tmp_dir}/tmp_out"
tmp_armbian="${tmp_dir}/tmp_armbian"
tmp_build="${tmp_dir}/tmp_build"
tmp_aml_image="${tmp_dir}/tmp_aml_image"
os_release_file="etc/os-release" # Get armbian ${VERSION_CODENAME}: such as [ focal ]
armbian_release_file="etc/armbian-release" # Set banner's ${BOARD_NAME}: such as [ s905x3 ]
ophub_release_file="etc/ophub-release" # Add custom armbian firmware information
build_armbian=("a311d" "s922x" "s922x-n2" "s922x-reva" "s905x3" "s905x2" "s905x2-km3" "s912" "s912-t95z" "s905d" "s905d-ki" "s905x" "s905w" "s905")
#
# Latest kernel download repository
kernel_repo="https://github.com/ophub/kernel/tree/main/pub"
#kernel_repo="https://github.com/ophub/kernel/trunk/pub"
version_branch="stable"
build_kernel=("5.10.100" "5.4.180")
auto_kernel="true"
#
# Set Armbian firmware size (BOOT_MB >= 256, ROOT_MB >= 2000)
SKIP_MB="68"
BOOT_MB="256"
ROOT_MB="2748"
#
#==================================================================================
error_msg() {
echo -e " [\033[1;91m Error \033[0m] ${1}"
exit 1
}
process_msg() {
echo -e " [\033[1;92m ${soc} - ${kernel} \033[0m] ${1}"
}
init_var() {
cd ${make_path}
# If it is followed by [ : ], it means that the option requires a parameter value
get_all_ver=$(getopt "db:k:a:v:s:" "${@}")
while [ -n "${1}" ]; do
case "${1}" in
-d | --default)
: ${version_branch:="${version_branch}"}
: ${build_armbian:="${build_armbian}"}
: ${build_kernel:="${build_kernel}"}
: ${ROOT_MB:="${ROOT_MB}"}
;;
-b | --build)
if [ -n "${2}" ]; then
unset build_armbian
oldIFS=$IFS
IFS=_
build_armbian=(${2})
IFS=$oldIFS
shift
else
error_msg "Invalid -b parameter [ ${2} ]!"
fi
;;
-k | --kernel)
if [ -n "${2}" ]; then
oldIFS=$IFS
IFS=_
build_kernel=(${2})
IFS=$oldIFS
shift
else
error_msg "Invalid -k parameter [ ${2} ]!"
fi
;;
-a | --autokernel)
if [ -n "${2}" ]; then
auto_kernel="${2}"
shift
else
error_msg "Invalid -a parameter [ ${2} ]!"
fi
;;
-v | --versionbranch)
if [ -n "${2}" ]; then
version_branch="${2}"
shift
else
error_msg "Invalid -v parameter [ ${2} ]!"
fi
;;
-s | --size)
if [[ -n "${2}" && "${2}" -ge "2000" ]]; then
ROOT_MB="${2}"
shift
else
error_msg "Invalid -s parameter [ ${2} ]!"
fi
;;
*)
error_msg "Invalid option [ ${1} ]!"
;;
esac
shift
done
}
find_armbian() {
cd ${make_path}
# Get armbian release and version
armbian_rebuild_name=$(ls ${armbian_rebuild_file} 2>/dev/null | head -n 1 | awk -F "/" '{print $NF}')
[[ -n "${armbian_rebuild_name}" ]] || error_msg "The armbian original file does not exist: [ ${armbian_rebuild_file} ]"
# Find armbian version info: such as [ 22.02.0 ]
armbian_rebuild_version="$(echo ${armbian_rebuild_name} | grep -oE '[2-9][0-9].[0-9]{1,2}.[0-9]{1,2}' | head -n 1)"
[[ -n "${armbian_rebuild_version}" ]] || {
armbian_rebuild_version="22.02.02"
echo -e "Missing armbian version info!"
}
}
download_kernel() {
cd ${make_path}
# Convert kernel library address to svn format
if [[ ${kernel_repo} == http* && $(echo ${kernel_repo} | grep "tree/main") != "" ]]; then
kernel_repo="${kernel_repo//tree\/main/trunk}"
fi
kernel_repo="${kernel_repo}/${version_branch}"
# Set empty array
tmp_arr_kernels=()
# Convert kernel library address to API format
server_kernel_url=${kernel_repo#*com\/}
server_kernel_url=${server_kernel_url//trunk/contents}
server_kernel_url="https://api.github.com/repos/${server_kernel_url}"
# Query the latest kernel in a loop
i=1
for KERNEL_VAR in ${build_kernel[*]}; do
echo -e "(${i}) Auto query the latest kernel version of the same series for [ ${KERNEL_VAR} ]"
MAIN_LINE_M=$(echo "${KERNEL_VAR}" | cut -d '.' -f1)
MAIN_LINE_V=$(echo "${KERNEL_VAR}" | cut -d '.' -f2)
MAIN_LINE_S=$(echo "${KERNEL_VAR}" | cut -d '.' -f3)
MAIN_LINE="${MAIN_LINE_M}.${MAIN_LINE_V}"
# Check the version on the server (e.g LATEST_VERSION="124")
LATEST_VERSION=$(curl -s "${server_kernel_url}" | grep "name" | grep -oE "${MAIN_LINE}.[0-9]+" | sed -e "s/${MAIN_LINE}.//g" | sort -n | sed -n '$p')
if [[ "$?" -eq "0" && ! -z "${LATEST_VERSION}" ]]; then
tmp_arr_kernels[${i}]="${MAIN_LINE}.${LATEST_VERSION}"
else
tmp_arr_kernels[${i}]="${KERNEL_VAR}"
fi
echo -e "(${i}) [ ${tmp_arr_kernels[$i]} ] is latest kernel. \n"
let i++
done
# Reset the kernel array to the latest kernel version
unset build_kernel
build_kernel=${tmp_arr_kernels[*]}
# Synchronization related kernel
i=1
for KERNEL_VAR in ${build_kernel[*]}; do
if [ ! -d "${kernel_path}/${KERNEL_VAR}" ]; then
echo -e "(${i}) [ ${KERNEL_VAR} ] Kernel loading from [ ${kernel_repo}/${KERNEL_VAR} ]"
svn export ${kernel_repo}/${KERNEL_VAR} ${kernel_path}/${KERNEL_VAR} --force
else
echo -e "(${i}) [ ${KERNEL_VAR} ] Kernel is in the local directory."
fi
let i++
done
sync
}
confirm_version() {
process_msg " (1/6) Confirm version type."
cd ${make_path}
# Confirm kernel branch
k510_ver=$(echo "${kernel}" | cut -d '.' -f1)
k510_maj=$(echo "${kernel}" | cut -d '.' -f2)
if [ "${k510_ver}" -eq "5" ]; then
if [ "${k510_maj}" -ge "10" ]; then
K510="1"
else
K510="0"
fi
elif [ "${k510_ver}" -gt "5" ]; then
K510="1"
else
K510="0"
fi
# Confirm soc branch
case "${soc}" in
s905x3 | x96 | hk1 | h96 | ugoosx3)
FDTFILE="meson-sm1-x96-max-plus-100m.dtb"
UBOOT_OVERLOAD="u-boot-x96maxplus.bin"
MAINLINE_UBOOT="/lib/u-boot/x96maxplus-u-boot.bin.sd.bin"
ANDROID_UBOOT="/lib/u-boot/hk1box-bootloader.img"
;;
s905x2 | x96max4g | x96max2g)
FDTFILE="meson-g12a-x96-max.dtb"
UBOOT_OVERLOAD="u-boot-x96max.bin"
MAINLINE_UBOOT="/lib/u-boot/x96max-u-boot.bin.sd.bin"
ANDROID_UBOOT=""
;;
s905x2-km3)
FDTFILE="meson-g12a-sei510.dtb"
UBOOT_OVERLOAD="u-boot-x96max.bin"
MAINLINE_UBOOT="/lib/u-boot/x96max-u-boot.bin.sd.bin"
ANDROID_UBOOT=""
;;
s905x | hg680p | b860h)
FDTFILE="meson-gxl-s905x-p212.dtb"
UBOOT_OVERLOAD="u-boot-p212.bin"
MAINLINE_UBOOT=""
ANDROID_UBOOT=""
;;
s905w | x96mini | tx3mini)
FDTFILE="meson-gxl-s905w-tx3-mini.dtb"
UBOOT_OVERLOAD="u-boot-s905x-s912.bin"
MAINLINE_UBOOT=""
ANDROID_UBOOT=""
;;
s905d | n1)
FDTFILE="meson-gxl-s905d-phicomm-n1.dtb"
UBOOT_OVERLOAD="u-boot-n1.bin"
MAINLINE_UBOOT=""
ANDROID_UBOOT="/lib/u-boot/u-boot-2015-phicomm-n1.bin"
;;
s905d-ki)
FDTFILE="meson-gxl-s905d-mecool-ki-pro.dtb"
UBOOT_OVERLOAD="u-boot-p201.bin"
MAINLINE_UBOOT=""
ANDROID_UBOOT=""
;;
s905 | beelinkminimx | mxqpro+)
FDTFILE="meson-gxbb-vega-s95-telos.dtb"
#FDTFILE="meson-gxbb-mxq-pro-plus.dtb"
UBOOT_OVERLOAD="u-boot-s905.bin"
#UBOOT_OVERLOAD="u-boot-p201.bin"
MAINLINE_UBOOT=""
ANDROID_UBOOT=""
;;
s912 | h96proplus | octopus)
FDTFILE="meson-gxm-octopus-planet.dtb"
UBOOT_OVERLOAD="u-boot-zyxq.bin"
MAINLINE_UBOOT=""
ANDROID_UBOOT=""
;;
s912-t95z | s912-t95z-plus)
FDTFILE="meson-gxm-t95z-plus.dtb"
UBOOT_OVERLOAD="u-boot-s905x-s912.bin"
MAINLINE_UBOOT=""
ANDROID_UBOOT=""
;;
s922x | belink | belinkpro | ugoos)
FDTFILE="meson-g12b-gtking-pro.dtb"
UBOOT_OVERLOAD="u-boot-gtkingpro.bin"
MAINLINE_UBOOT="/lib/u-boot/gtkingpro-u-boot.bin.sd.bin"
ANDROID_UBOOT=""
;;
s922x-n2 | odroid-n2 | n2)
FDTFILE="meson-g12b-odroid-n2.dtb"
UBOOT_OVERLOAD="u-boot-gtkingpro.bin"
MAINLINE_UBOOT="/lib/u-boot/odroid-n2-u-boot.bin.sd.bin"
ANDROID_UBOOT=""
;;
s922x-reva)
FDTFILE="meson-g12b-gtking-pro.dtb"
UBOOT_OVERLOAD="u-boot-gtkingpro-rev-a.bin"
MAINLINE_UBOOT=""
ANDROID_UBOOT=""
;;
a311d | khadas-vim3 | vim3)
FDTFILE="meson-g12b-a311d-khadas-vim3.dtb"
UBOOT_OVERLOAD="u-boot-gtkingpro.bin"
MAINLINE_UBOOT="/lib/u-boot/khadas-vim3-u-boot.sd.bin"
ANDROID_UBOOT=""
;;
*)
error_msg "Have no this soc: [ ${soc} ]"
;;
esac
# Confirm UUID
ROOTFS_UUID="$(cat /proc/sys/kernel/random/uuid)"
[ -z "${ROOTFS_UUID}" ] && ROOTFS_UUID="$(uuidgen)"
[ -z "${ROOTFS_UUID}" ] && error_msg "The uuidgen is invalid, cannot continue."
}
extract_armbian() {
process_msg " (2/6) Extract old armbian files."
cd ${make_path}
rm -rf ${tmp_dir} 2>/dev/null && sync
mkdir -p ${tmp_outpath} ${tmp_armbian} ${tmp_build} ${tmp_aml_image} && sync
armbian_image_file="${tmp_aml_image}/armbian_${soc}_${kernel}.img"
rm -f ${armbian_image_file} 2>/dev/null && sync
cp -f "${armbian_outputpath}/${armbian_rebuild_name}" "${armbian_image_file}"
sync && sleep 3
loop_old=$(losetup -P -f --show "${armbian_image_file}")
[ ${loop_old} ] || error_msg "losetup ${armbian_image_file} failed."
if ! mount ${loop_old}p1 ${tmp_armbian}; then
error_msg "mount ${loop_old}p1 failed!"
fi
cd ${tmp_armbian}
rm -rf boot/* 2>/dev/null
rm -rf bin lib sbin tmp usr/lib/modules/* usr/local/include/* var/sbin 2>/dev/null
sync && sleep 3
}
refactor_files() {
process_msg " (3/6) Refactor related files."
cd ${make_path}
# Create a dual-partition general directory
tag_bootfs="${tmp_build}/bootfs"
tag_rootfs="${tmp_build}/rootfs"
mkdir -p ${tag_bootfs} ${tag_rootfs} && sync
# Copy the full Armbian image
cp -rf ${tmp_armbian}/* ${tag_rootfs}
sync && sleep 3
# Unzip the relevant files
tar -xzf "${armbian_path}/boot-common.tar.gz" -C ${tag_bootfs}
tar -xzf "${armbian_path}/root-common.tar.gz" -C ${tag_rootfs}
# Copy the same files of the release for ROOTFS
[[ "$(ls ${configfiles_path}/files 2>/dev/null | wc -w)" -ne "0" ]] && cp -rf ${configfiles_path}/files/* ${tag_rootfs}
# Copy the patches files
[ -f "${tag_rootfs}/usr/bin/uuidgen" ] || cp -f ${configfiles_path}/patches/rootfs/usr/bin/uuidgen ${tag_rootfs}/usr/bin
# Find ID in ${os_release_file}: such as [ubuntu/debian]
release_codeid="$(cat ${tag_rootfs}/${os_release_file} | grep -oE "^ID=.*" | cut -d"=" -f2)"
[ -z "${release_codeid}" ] && error_msg "The [ ${os_release_file}: ID ] is invalid."
# Find VERSION_CODENAME in ${os_release_file}: such as [focal/bullseye/buster]
release_codename="$(cat ${tag_rootfs}/${os_release_file} | grep -oE "^VERSION_CODENAME=.*" | cut -d"=" -f2)"
[ -z "${release_codename}" ] && error_msg "The [ ${os_release_file}: VERSION_CODENAME ] is invalid."
# Copy the different files of the release for ROOTFS
diff_release_files="${configfiles_path}/release/${release_codename}"
if [[ -n "${release_codename}" && -d "${diff_release_files}" ]]; then
# Backup source
mv -f ${tag_rootfs}/etc/apt/sources.list ${tag_rootfs}/etc/apt/sources.list.bak
mv -f ${tag_rootfs}/etc/apt/sources.list.d/armbian.list ${tag_rootfs}/etc/apt/sources.list.d/armbian.list.bak
# Copy custom source
cp -rf ${diff_release_files}/* ${tag_rootfs}
fi
sync
# Replace the kernel
build_boot=$(ls ${kernel_path}/${kernel}/boot-${kernel}-*.tar.gz 2>/dev/null | head -n 1)
build_dtb=$(ls ${kernel_path}/${kernel}/dtb-amlogic-${kernel}-*.tar.gz 2>/dev/null | head -n 1)
build_modules=$(ls ${kernel_path}/${kernel}/modules-${kernel}-*.tar.gz 2>/dev/null | head -n 1)
build_header=$(ls ${kernel_path}/${kernel}/header-${kernel}-*.tar.gz 2>/dev/null | head -n 1)
[[ -n "${build_boot}" && -n "${build_dtb}" && -n "${build_modules}" && -n "${build_header}" ]] || error_msg "The 4 kernel missing."
# 01 For /boot five files
tar -xzf ${build_boot} -C ${tag_bootfs} && sync
[[ "$(ls ${tag_bootfs}/*-${kernel}-* -l 2>/dev/null | grep "^-" | wc -l)" -ge "4" ]] || error_msg "The /boot files is missing."
(cd ${tag_bootfs} && cp -f uInitrd-* uInitrd && cp -f vmlinuz-* zImage && sync)
# 02 For dtb files
cp -rf ${dtb_path}/* ${tag_bootfs}/dtb/amlogic && sync
tar -xzf ${build_dtb} -C ${tag_bootfs}/dtb/amlogic && sync
# 03 For usr/lib/modules/*
tar -xzf ${build_modules} -C ${tag_rootfs}/usr/lib/modules && sync
(cd ${tag_rootfs}/usr/lib/modules/${kernel}-*/ && rm -f build source 2>/dev/null && sync)
[[ "$(ls ${tag_rootfs}/usr/lib/modules/${kernel}-* -l 2>/dev/null | grep "^d" | wc -l)" -eq "1" ]] || error_msg "Missing kernel."
# 04 For header files, usr/local/include
tar -xzf ${build_header} -C ${tag_rootfs}/usr/local && sync
# Processing partition files: bootfs
cd ${tag_bootfs}
# Add u-boot.ext for 5.10 kernel
cp -f ${uboot_path}/* . && sync
if [[ "${K510}" -eq "1" ]]; then
if [[ -f "${UBOOT_OVERLOAD}" && -n "${UBOOT_OVERLOAD}" ]]; then
cp -f ${UBOOT_OVERLOAD} u-boot.ext
chmod +x u-boot.ext
else
error_msg "${soc} have no the 5.10 kernel u-boot file: [ ${UBOOT_OVERLOAD} ]"
fi
fi
# Edit the uEnv.txt
[ -f "uEnv.txt" ] || error_msg "The uEnv.txt File does not exist."
sed -i "s|LABEL=ROOTFS|UUID=${ROOTFS_UUID}|g" uEnv.txt
sed -i "s|meson.*.dtb|${FDTFILE}|g" uEnv.txt
# For s912-t95z-plus /boot/extlinux/extlinux.conf
[ "${FDTFILE}" == "meson-gxm-t95z-plus.dtb" ] && cp -rf ${configfiles_path}/patches/boot/s912-t95z-plus/* .
sync
# Processing partition files: rootfs
cd ${tag_rootfs}
# Delete related files
rm -f etc/apt/sources.list.save 2>/dev/null
rm -f etc/apt/*.gpg~ 2>/dev/null
rm -f etc/systemd/system/basic.target.wants/armbian-resize-filesystem.service 2>/dev/null
rm -rf usr/share/doc/linux-image-current-meson64 2>/dev/null
rm -rf usr/lib/linux-image-current-meson64 2>/dev/null
rm -f usr/sbin/ddbr 2>/dev/null
rm -f var/lib/dpkg/info/linux-image* 2>/dev/null
# Renaming/disabling related files
mv -f etc/udev/rules.d/hdmi.rules etc/udev/rules.d/hdmi.rules.bak 2>/dev/null
# Fix permission
chmod 777 var/tmp
chown man:root var/cache/man -R
chmod g+s var/cache/man -R
chown root:root usr/bin/sudo
chmod 4755 usr/bin/sudo
chown 0 usr/lib/sudo/sudoers.so
chmod 644 usr/lib/sudo/sudoers.so
chmod 4755 usr/lib/policykit-1/polkit-agent-helper-1
chmod 755 etc/update-motd.d/*
# Add soft connection
ln -sf usr/bin bin
ln -sf usr/lib lib
ln -sf usr/sbin sbin
ln -sf var/tmp tmp
ln -sf /usr/share/zoneinfo/Asia/Shanghai etc/localtime
ln -sf armbian-ddbr usr/sbin/ddbr
# Add custom firmware information
echo "VERSION_CODEID='${release_codeid}'" >>${ophub_release_file} 2>/dev/null
echo "VERSION_CODENAME='${release_codename}'" >>${ophub_release_file} 2>/dev/null
echo "FDTFILE='${FDTFILE}'" >>${ophub_release_file} 2>/dev/null
echo "UBOOT_OVERLOAD='${UBOOT_OVERLOAD}'" >>${ophub_release_file} 2>/dev/null
echo "MAINLINE_UBOOT='${MAINLINE_UBOOT}'" >>${ophub_release_file} 2>/dev/null
echo "ANDROID_UBOOT='${ANDROID_UBOOT}'" >>${ophub_release_file} 2>/dev/null
echo "KERNEL_VERSION='${kernel}'" >>${ophub_release_file} 2>/dev/null
echo "SOC='${soc}'" >>${ophub_release_file} 2>/dev/null
echo "K510='${K510}'" >>${ophub_release_file} 2>/dev/null
echo "PACKAGED_DATE='$(date +%Y-%m-%d)'" >>${ophub_release_file} 2>/dev/null
# Edit the etc/fstab
[ -f "etc/fstab" ] || error_msg "The etc/fstab File does not exist."
sed -i "s|LABEL=ROOTFS|UUID=${ROOTFS_UUID}|g" etc/fstab
# Custom banner name
sed -i "s|BOARD_NAME=.*|BOARD_NAME=\"Aml ${soc}\"|g" ${armbian_release_file} 2>/dev/null
# Add tasks that need to be executed on initial startup
armbian_firstrun="usr/lib/armbian/armbian-firstrun"
sed -i '/armbian-release/i\sudo apt-get -qq purge -y linux-image-current-meson64' ${armbian_firstrun} 2>/dev/null
#armbian_firstlogin="usr/lib/armbian/armbian-firstlogin"
#sed -i 's/warranty.*implied/#&/g' ${armbian_firstlogin} 2>/dev/null
# Get random macaddr
mac_hexchars="0123456789ABCDEF"
mac_end=$(for i in {1..6}; do echo -n ${mac_hexchars:$((${RANDOM} % 16)):1}; done | sed -e 's/\(..\)/:\1/g')
random_macaddr="9E:61${mac_end}"
#
# Set interfaces macaddr
interfaces_file="etc/network/interfaces"
[ -f "${interfaces_file}" ] && sed -i "s|hwaddress ether.*|hwaddress ether ${random_macaddr}:AA|g" ${interfaces_file}
#
# Optimize wifi/bluetooth module
[ -d "usr/lib/firmware/brcm" ] && (
cd usr/lib/firmware/brcm/ && mv -f ../*.hcd . 2>/dev/null
# gtking/gtking pro is bcm4356 wifi/bluetooth, wifi5 module AP6356S
sed -e "s/macaddr=.*/macaddr=${random_macaddr}:00/" "brcmfmac4356-sdio.txt" >"brcmfmac4356-sdio.azw,gtking.txt"
# gtking/gtking pro is bcm4356 wifi/bluetooth, wifi6 module AP6275S
sed -e "s/macaddr=.*/macaddr=${random_macaddr}:01/" "brcmfmac4375-sdio.txt" >"brcmfmac4375-sdio.azw,gtking.txt"
# Phicomm N1 is bcm43455 wifi/bluetooth
sed -e "s/macaddr=.*/macaddr=${random_macaddr}:02/" "brcmfmac43455-sdio.txt" >"brcmfmac43455-sdio.phicomm,n1.txt"
# MXQ Pro+ is AP6330(bcm4330) wifi/bluetooth
sed -e "s/macaddr=.*/macaddr=${random_macaddr}:03/" "brcmfmac4330-sdio.txt" >"brcmfmac4330-sdio.crocon,mxq-pro-plus.txt"
# HK1 Box & H96 Max X3 is bcm54339 wifi/bluetooth
sed -e "s/macaddr=.*/macaddr=${random_macaddr}:04/" "brcmfmac4339-sdio.ZP.txt" >"brcmfmac4339-sdio.amlogic,sm1.txt"
# old ugoos x3 is bcm43455 wifi/bluetooth
sed -e "s/macaddr=.*/macaddr=${random_macaddr}:05/" "brcmfmac43455-sdio.txt" >"brcmfmac43455-sdio.amlogic,sm1.txt"
# new ugoos x3 is brm43456
sed -e "s/macaddr=.*/macaddr=${random_macaddr}:06/" "brcmfmac43456-sdio.txt" >"brcmfmac43456-sdio.amlogic,sm1.txt"
)
sync
}
make_image() {
process_msg " (4/6) Make new armbian image."
cd ${make_path}
# Make Amlogic s9xxx armbian
build_image_file="${tmp_outpath}/Armbian_${armbian_rebuild_version}_Aml_${soc}_focal_${kernel}_$(date +"%Y.%m.%d.%H%M").img"
rm -f ${build_image_file} 2>/dev/null && sync
IMG_SIZE=$((SKIP_MB + BOOT_MB + ROOT_MB))
dd if=/dev/zero of=${build_image_file} bs=1M count=${IMG_SIZE} conv=fsync >/dev/null 2>&1
sync
parted -s ${build_image_file} mklabel msdos 2>/dev/null
parted -s ${build_image_file} mkpart primary fat32 $((SKIP_MB))M $((SKIP_MB + BOOT_MB - 1))M 2>/dev/null
parted -s ${build_image_file} mkpart primary ext4 $((SKIP_MB + BOOT_MB))M 100% 2>/dev/null
sync
loop_new=$(losetup -P -f --show "${build_image_file}")
[ ${loop_new} ] || error_msg "losetup ${build_image_file} failed."
mkfs.vfat -n "BOOT" ${loop_new}p1 >/dev/null 2>&1
mkfs.ext4 -F -q -U ${ROOTFS_UUID} -L "ROOTFS" -m 0 ${loop_new}p2 >/dev/null 2>&1
sync
# Write the bootloader
if [[ -n "${MAINLINE_UBOOT}" && -f "${tag_rootfs}/usr${MAINLINE_UBOOT}" ]]; then
dd if=${tag_rootfs}/usr${MAINLINE_UBOOT} of=${loop_new} bs=1 count=444 conv=fsync 2>/dev/null
dd if=${tag_rootfs}/usr${MAINLINE_UBOOT} of=${loop_new} bs=512 skip=1 seek=1 conv=fsync 2>/dev/null
#echo -e "For [ ${soc} ] write Mainline bootloader: ${MAINLINE_UBOOT}"
elif [[ -n "${ANDROID_UBOOT}" && -f "${tag_rootfs}/usr${ANDROID_UBOOT}" ]]; then
dd if=${tag_rootfs}/usr${ANDROID_UBOOT} of=${loop_new} bs=1 count=444 conv=fsync 2>/dev/null
dd if=${tag_rootfs}/usr${ANDROID_UBOOT} of=${loop_new} bs=512 skip=1 seek=1 conv=fsync 2>/dev/null
#echo -e "For [ ${soc} ] write Android bootloader: ${ANDROID_UBOOT}"
fi
sync && sleep 3
}
copy_files() {
process_msg " (5/6) Copy the Armbian files."
cd ${make_path}
build_image_bootfs="${tmp_outpath}/bootfs"
build_image_rootfs="${tmp_outpath}/rootfs"
mkdir -p ${build_image_bootfs} ${build_image_rootfs} && sync
if ! mount ${loop_new}p2 ${build_image_rootfs}; then
error_msg "mount ${loop_new}p2 failed!"
fi
if ! mount ${loop_new}p1 ${build_image_bootfs}; then
error_msg "mount ${loop_new}p1 failed!"
fi
cp -rf ${tag_bootfs}/* ${build_image_bootfs}
cp -rf ${tag_rootfs}/* ${build_image_rootfs}
sync && sleep 3
}
clean_tmp() {
process_msg " (6/6) Clear temp files."
cd ${make_path}
umount -f ${tmp_armbian} 2>/dev/null
losetup -d ${loop_old} 2>/dev/null
umount -f ${build_image_bootfs} 2>/dev/null
umount -f ${build_image_rootfs} 2>/dev/null
losetup -d ${loop_new} 2>/dev/null
cd ${tmp_outpath}
# Rename version
[[ "${release_codename}" != "focal" ]] && rename "s/focal/${release_codename}/g" *.img 2>/dev/null && sync
gzip *.img && sync && mv -f *.img.gz ${armbian_outputpath}
sync
cd ${make_path}
rm -rf ${tmp_dir} 2>/dev/null
sync
}
loop_rebuild() {
cd ${make_path}
j=1
for b in ${build_armbian[*]}; do
i=1
for k in ${build_kernel[*]}; do
{
echo -n "(${j}.${i}) Start building Armbian [ ${b} - ${k} ]. "
now_remaining_space=$(df -hT ${PWD} | grep '/dev/' | awk '{print $5}' | sed 's/.$//' | awk -F "." '{print $1}')
if [[ "${now_remaining_space}" -le "2" ]]; then
echo "Remaining space is less than 2G, exit this build. \n"
break
else
echo "Remaining space is ${now_remaining_space}G."
fi
# The loop variable assignment
soc="${b}"
kernel="${k}"
# Execute the following functions in sequence
confirm_version
extract_armbian
refactor_files
make_image
copy_files
clean_tmp
echo -e "(${j}.${i}) Armbian build successfully. \n"
let i++
}
done
let j++
done
}
# Show welcome message
echo -e "Welcome to Rebuild Armbian for Amlogic s9xxx tv box!"
[ $(id -u) = 0 ] || error_msg "please run this script as root: [ sudo ./$0 ]"
# Show server start information
echo -e "Server CPU configuration information: \n$(cat /proc/cpuinfo | grep name | cut -f2 -d: | uniq -c) \n"
echo -e "Server memory usage: \n$(free -h) \n"
echo -e "Server space usage before starting to compile: \n$(df -hT ${PWD}) \n"
#
# Initialize variables and download the kernel
init_var "${@}"
find_armbian && echo -e "Armbian rebuild file: [ ${armbian_rebuild_name} ], Version: [ ${armbian_rebuild_version} ]"
[ "${auto_kernel}" == "true" ] && download_kernel
echo -e "Armbian SoC List: [ $(echo ${build_armbian[*]} | tr "\n" " ") ]"
echo -e "Kernel List: [ $(echo ${build_kernel[*]} | tr "\n" " ") ] \n"
# Loop to rebuild armbian firmware
loop_rebuild
#
# Show server end information
echo -e "Server space usage after compilation: \n$(df -hT ${PWD}) \n"
# All process completed
wait