-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
remake
executable file
·1220 lines (1078 loc) · 51.1 KB
/
remake
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
#!/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 Remake OpenWrt
# https://github.com/ophub/amlogic-s9xxx-openwrt
#
# Description: Automatically Packaged OpenWrt
# Copyright (C) 2020~ https://github.com/openwrt/openwrt
# Copyright (C) 2020~ https://github.com/coolsnowwolf/lede
# Copyright (C) 2020~ https://github.com/immortalwrt/immortalwrt
# Copyright (C) 2020~ https://github.com/unifreq/openwrt_packit
# Copyright (C) 2021~ https://github.com/ophub/amlogic-s9xxx-armbian/blob/main/CONTRIBUTORS.md
# Copyright (C) 2020~ https://github.com/ophub/amlogic-s9xxx-openwrt
#
# Command: sudo ./remake
# Command optional parameters please refer to the source code repository
#
#======================================== Functions list ========================================
#
# error_msg : Output error message
# process_msg : Output process message
# mount_try : Mount the image file, fail again
# get_textoffset : Get kernel TEXT_OFFSET
#
# init_var : Initialize all variables
# check_data : Check the validity of the data
# find_openwrt : Find OpenWrt file (openwrt-armvirt/*rootfs.tar.gz)
# git_pull_dir : Download the files from the git repository
# download_depends : Download the dependency files
# query_kernel : Query the latest kernel version
# check_kernel : Check kernel files integrity
# download_kernel : Download the latest kernel
#
# confirm_version : Confirm version type
# make_image : Making OpenWrt file
# extract_openwrt : Extract OpenWrt files
# replace_kernel : Replace the kernel
# refactor_bootfs : Refactor bootfs files
# refactor_rootfs : Refactor rootfs files
# clean_tmp : Clear temporary files
#
# loop_make : Loop to make OpenWrt files
#
#================================ Set make environment variables ================================
#
# Related file storage path
current_path="${PWD}"
make_path="${current_path}/openwrt"
tmp_path="${make_path}/tmp"
out_path="${make_path}/out"
openwrt_path="${current_path}/openwrt-armvirt"
openwrt_rootfs_file="*rootfs.tar.gz"
resource_path="${current_path}/make-openwrt"
kernel_path="${resource_path}/kernel"
uboot_path="${resource_path}/u-boot"
common_files="${resource_path}/openwrt-files/common-files"
platform_files="${resource_path}/openwrt-files/platform-files"
different_files="${resource_path}/openwrt-files/different-files"
firmware_path="${common_files}/lib/firmware"
model_conf="${common_files}/etc/model_database.conf"
model_txt="${common_files}/etc/model_database.txt"
[[ -d "${make_path}" ]] || mkdir -p ${make_path}
# System operation environment
arch_info="$(uname -m)"
host_release="$(cat /etc/os-release | grep '^VERSION_CODENAME=.*' | cut -d'=' -f2)"
# Add custom OpenWrt firmware information
op_release="etc/flippy-openwrt-release"
ophub_release_file="etc/ophub-release"
# Dependency files download repository
depends_repo="https://github.com/ophub/amlogic-s9xxx-armbian"
# U-BOOT files download repository
uboot_repo="https://github.com/ophub/u-boot"
# Firmware files download repository
firmware_repo="https://github.com/ophub/firmware"
# Install/Update script files download repository
script_repo="https://github.com/ophub/luci-app-amlogic"
# Set the kernel download repository from github.com
kernel_repo="https://github.com/ophub/kernel"
# Set the tags(kernel_xxx) of the default kernel that can be replaced via the [ -u ] parameter
default_tags="stable"
kernel_usage=""
# Set the list of kernels used by default(Selectable version)
stable_kernel=("6.6.y" "6.1.y")
flippy_kernel=(${stable_kernel[@]})
dev_kernel=(${stable_kernel[@]})
beta_kernel=(${stable_kernel[@]})
rk3588_kernel=("5.10.y")
rk35xx_kernel=("5.10.y")
h6_kernel=("6.6.y")
specific_6xy=("6.6.y" "6.1.y")
specific_5xy=("5.15.y" "5.10.y" "5.4.y")
# Set to automatically use the latest kernel
auto_kernel="true"
# Initialize the kernel array
declare -A tags_list
# Set the Amlogic's u-boot series(u-boot-xxx.bin)
uboot_meson_gxl=("p201.bin" "p212.bin" "s905x-s912.bin" "n1.bin" "r3300l.bin")
uboot_meson_gxm=("p212.bin" "s905x-s912.bin" "zyxq.bin")
uboot_meson_g12a=("x96max.bin" "e900v22c.bin")
uboot_meson_g12b=("gtking.bin" "gtkingpro.bin" "gtkingpro-rev-a.bin" "s905x2-s922.bin")
uboot_meson_sm1=("x96maxplus.bin" "ugoos-x3.bin" "tx3-qz.bin" "tx3-bz.bin" "skyworth-lb2004.bin")
uboot_meson_gxbb=("p201.bin" "s905.bin")
# Initialize the build device
make_board="all"
# Set OpenWrt firmware size (Unit: MiB, boot_mb >= 256, root_mb >= 512)
boot_mb="256"
root_mb="1024"
# Set OpenWrt builder signature
builder_name=""
# Set font color
STEPS="[\033[95m STEPS \033[0m]"
INFO="[\033[94m INFO \033[0m]"
NOTE="[\033[93m NOTE \033[0m]"
WARNING="[\033[93m WARNING \033[0m]"
SUCCESS="[\033[92m SUCCESS \033[0m]"
ERROR="[\033[91m ERROR \033[0m]"
#
#================================================================================================
error_msg() {
echo -e " [💔] ${1}"
exit 1
}
process_msg() {
echo -e " [🌿] ${1}"
}
mount_try() {
# Check mount parameters
m_type="${1}"
m_dev="${2}"
m_target="${3}"
[[ -n "${m_type}" && -n "${m_dev}" && -n "${m_target}" ]] || {
error_msg "Mount parameter is missing: [ ${m_type}, ${m_dev}, ${m_target} ]"
}
t="1"
max_try="10"
while [[ "${t}" -le "${max_try}" ]]; do
# Mount according to the image partition format
if [[ "${m_type}" == "btrfs" ]]; then
mount -t ${m_type} -o discard,compress=zstd:6 ${m_dev} ${m_target}
else
mount -t ${m_type} -o discard ${m_dev} ${m_target}
fi
# Mount failed and continue trying
if [[ "${?}" -eq 0 ]]; then
break
else
sync && sleep 3
umount -f ${m_target} 2>/dev/null
t="$((t + 1))"
fi
done
[[ "${t}" -gt "${max_try}" ]] && error_msg "[ ${t} ] attempts to mount failed."
}
get_textoffset() {
vmlinuz_name="${1}"
need_overload="yes"
# With TEXT_OFFSET patch is [ 0108 ], without TEXT_OFFSET patch is [ 0000 ]
[[ "$(hexdump -n 15 -x "${vmlinuz_name}" 2>/dev/null | head -n 1 | awk '{print $7}')" == "0108" ]] && need_overload="no"
}
init_var() {
echo -e "${STEPS} Start Initializing Variables..."
# If it is followed by [ : ], it means that the option requires a parameter value
get_all_ver="$(getopt "b:r:u:k:a:s:n:" "${@}")"
while [[ -n "${1}" ]]; do
case "${1}" in
-b | --Board)
if [[ -n "${2}" ]]; then
make_board="${2// /}"
shift
else
error_msg "Invalid -b parameter [ ${2} ]!"
fi
;;
-r | --kernelRepository)
if [[ -n "${2}" ]]; then
kernel_repo="${2}"
shift
else
error_msg "Invalid -r parameter [ ${2} ]!"
fi
;;
-u | --kernelUsage)
if [[ -n "${2}" ]]; then
kernel_usage="${2//kernel_/}"
shift
else
error_msg "Invalid -u parameter [ ${2} ]!"
fi
;;
-k | --Kernel)
if [[ -n "${2}" ]]; then
oldIFS="${IFS}"
IFS="_"
flippy_kernel=(${2})
stable_kernel=(${2})
dev_kernel=(${2})
beta_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
;;
-s | --Size)
if [[ -n "${2}" ]]; then
img_mb="${2}"
shift
else
error_msg "Invalid -s parameter [ ${2} ]!"
fi
;;
-n | --BuilderName)
if [[ -n "${2}" ]]; then
builder_name="${2// /}"
shift
else
error_msg "Invalid -n parameter [ ${2} ]!"
fi
;;
*)
error_msg "Invalid option [ ${1} ]!"
;;
esac
shift
done
# Set the image size, such as [ -s 512/2560 ] or [ -s 2560 ]
[[ -n "${img_mb}" ]] && {
if [[ "${img_mb}" =~ / ]]; then
boot_mb="${img_mb%%/*}"
root_mb="${img_mb##*/}"
else
root_mb="${img_mb}"
fi
}
}
check_data() {
# Columns of ${model_conf}:
# 1.ID 2.MODEL 3.SOC 4.FDTFILE 5.UBOOT_OVERLOAD 6.MAINLINE_UBOOT 7.BOOTLOADER_IMG 8.DESCRIPTION
# 9.KERNEL_TAGS 10.PLATFORM 11.FAMILY 12.BOOT_CONF 13.CONTRIBUTORS 14.BOARD 15.BUILD
[[ -f "${model_conf}" ]] || error_msg "Missing model config file: [ ${model_conf} ]"
# Convert ${model_conf} to ${model_txt} for [ openwrt-install-amlogic ], Just the first 8 columns.
cat ${model_conf} |
sed -e 's/NULL/NA/g' -e 's/[ ][ ]*//g' |
grep -E "^[^#ar].*" |
awk -F':' '{if ($6 != "NA") $6 = "/lib/u-boot/"$6; if ($7 != "NA") $7 = "/lib/u-boot/"$7; NF = 12; print}' OFS=':' \
>${model_txt}
# Get a list of build devices
if [[ "${make_board}" == "all" ]]; then
board_list=":(yes)"
make_openwrt=($(
cat ${model_conf} |
sed -e 's/NA//g' -e 's/NULL//g' -e 's/[ ][ ]*//g' |
grep -E "^[^#].*:yes$" | awk -F':' '{print $14}' |
sort -u | xargs
))
else
board_list=":($(echo ${make_board} | sed -e 's/_/\|/g')):(yes|no)"
make_openwrt=($(echo ${make_board} | sed -e 's/_/ /g'))
fi
[[ "${#make_openwrt[@]}" -eq 0 ]] && error_msg "The board is missing, stop making."
# Get the kernel array
kernel_from=($(
cat ${model_conf} |
sed -e 's/NA//g' -e 's/NULL//g' -e 's/[ ][ ]*//g' |
grep -E "^[^#].*${board_list}$" | awk -F':' '{print $9}' |
sort -u | xargs
))
[[ "${#kernel_from[@]}" -eq 0 ]] && error_msg "Missing [ KERNEL_TAGS ] settings, stop building."
# Replace custom kernel tags
[[ -n "${kernel_usage}" ]] && {
for ((i = 0; i < ${#kernel_from[@]}; i++)); do
if [[ ${kernel_from[${i}]} == "${default_tags}/"* ]]; then
kernel_from[${i}]="${kernel_from[${i}]//${default_tags}/${kernel_usage}}"
fi
done
}
# Convert the kernel_from to the kernel array
for item in "${kernel_from[@]}"; do
# Split the key and value
IFS='/' read -r key value <<<"${item}"
# Check if the value is "all".
if [[ "${value}" == "all" ]]; then
# If the value is "all", assign the value of ${key}_kernel. such as [ stable_kernel, rk3588_kernel, etc. ]
eval "value=\"\${${key}_kernel[@]}\""
elif [[ "${value}" =~ ^[1-9]+ ]]; then
if [[ "${value}" == "5.x.y" ]]; then
value="${specific_5xy[@]}"
elif [[ "${value}" == "6.x.y" ]]; then
value="${specific_6xy[@]}"
else
IFS='_' read -ra value <<<"${value}"
value="${value[@]}"
fi
fi
# Merge the same key values
if [[ -n "${tags_list[${key}]}" ]]; then
tags_list[${key}]+=" ${value}"
else
tags_list[${key}]="${value}"
fi
done
# Convert the tags_list array to the kernel array (remove duplicates)
for key in "${!tags_list[@]}"; do
# Convert the space-separated string to an array and remove duplicates
read -ra unique_values <<<"$(echo "${tags_list[${key}]}" | tr ' ' '\n' | sort -u | tr '\n' ' ')"
# Assign the unique values back to the tags_list
tags_list[${key}]="${unique_values[@]}"
done
# Check the kernel tags list
[[ "${#tags_list[@]}" -eq 0 ]] && error_msg "The [ tags_list ] is missing, stop building."
echo -e "${INFO} The kernel tags list: [ ${!tags_list[@]} ]"
# Convert kernel repository address to api format
[[ "${kernel_repo}" =~ ^https: ]] && kernel_repo="$(echo ${kernel_repo} | awk -F'/' '{print $4"/"$5}')"
kernel_api="https://github.com/${kernel_repo}"
}
find_openwrt() {
cd ${current_path}
echo -e "${STEPS} Start searching for OpenWrt file..."
# Find whether the OpenWrt file exists
openwrt_default_file="$(ls ${openwrt_path}/${openwrt_rootfs_file} 2>/dev/null | head -n 1 | awk -F "/" '{print $NF}')"
if [[ -n "${openwrt_default_file}" ]]; then
echo -e "${INFO} OpenWrt file: [ ${openwrt_default_file} ]"
else
error_msg "There is no [ ${openwrt_rootfs_file} ] file in the [ ${openwrt_path} ] directory."
fi
# Extract the OpenWrt release information file
source_codename=""
source_release_file="etc/openwrt_release"
temp_dir="$(mktemp -d)"
(cd ${temp_dir} && tar -mxzf "${openwrt_path}/${openwrt_default_file}" "./${source_release_file}" 2>/dev/null)
# Find custom DISTRIB_SOURCECODE, such as [ official/lede ]
[[ -f "${temp_dir}/${source_release_file}" ]] && {
source_codename="$(cat ${temp_dir}/${source_release_file} 2>/dev/null | grep -oE "^DISTRIB_SOURCECODE=.*" | head -n 1 | cut -d"'" -f2)"
[[ -n "${source_codename}" ]] && {
# Record OpenWrt source codes repository
case "${source_codename}" in
official) OPENWRT_SOURCECODE="github.com/openwrt/openwrt" ;;
lede) OPENWRT_SOURCECODE="github.com/coolsnowwolf/lede" ;;
immortalwrt) OPENWRT_SOURCECODE="github.com/immortalwrt/immortalwrt" ;;
*) OPENWRT_SOURCECODE="unknown" ;;
esac
# Complete filename
[[ "${source_codename:0:1}" != "_" ]] && source_codename="_${source_codename}"
}
echo -e "${INFO} The source_codename: [ ${source_codename} ], OpenWrt source code repository: [ ${OPENWRT_SOURCECODE} ]"
}
# Remove temporary directory
rm -rf ${temp_dir}
}
git_pull_dir() {
cd ${current_path}
# Check git_pull_dir parameters
git_repo="${1}"
git_branch="${2}"
git_path="${3}"
[[ -n "${git_repo}" && -n "${git_branch}" && -n "${git_path}" ]] || {
error_msg "git_pull_dir parameter is missing: [ ${git_repo}, ${git_branch}, ${git_path} ]"
}
# Clone the repository to the temporary directory. If it fails, wait 1 minute and try again, try 10 times.
for i in {1..10}; do
git clone --quiet --single-branch --depth=1 --branch=${git_branch} ${git_repo} ${git_path}
[[ "${?}" -eq 0 ]] && break || sleep 60
done
[[ "${?}" -eq 0 ]] || error_msg "Failed to clone the [ ${git_repo} ] repository."
}
download_depends() {
cd ${current_path}
echo -e "${STEPS} Start downloading dependency files..."
# Download Armbian u-boot files
git_path="$(mktemp -d)"
git_pull_dir ${uboot_repo} main ${git_path}
# Move the files to the storage directory
mkdir -p ${uboot_path}
cp -af --no-preserve=ownership ${git_path}/u-boot/* ${uboot_path}
[[ "${?}" -eq 0 ]] || error_msg "Failed to move the [ u-boot ] files to the [ ${uboot_path} ] directory."
# Delete temporary files
rm -rf ${git_path}
# Download Armbian firmware files
git_path="$(mktemp -d)"
git_pull_dir ${firmware_repo} main ${git_path}
# Move the files to the storage directory
mkdir -p ${firmware_path}
cp -af --no-preserve=ownership ${git_path}/firmware/* ${firmware_path}
[[ "${?}" -eq 0 ]] && echo -e "${INFO} firmware download completed." || error_msg "firmware download failed."
# Delete temporary files
rm -rf ${git_path}
# Download platform files
git_path="$(mktemp -d)"
git_pull_dir ${depends_repo} main ${git_path}
# Move the files to the storage directory
cp -af --no-preserve=ownership ${git_path}/build-armbian/armbian-files/platform-files/* ${platform_files}
# Remove the special files in the [ sbin ] directory of the Armbian system
rm -rf $(find ${platform_files} -type d -name "sbin")
# Download different files
cp -af --no-preserve=ownership ${git_path}/build-armbian/armbian-files/different-files/* ${different_files}
[[ "${?}" -eq 0 ]] && echo -e "${INFO} different-files download completed." || error_msg "different-files download failed."
# Download balethirq related files
cp -f --no-preserve=ownership ${git_path}/build-armbian/armbian-files/common-files/usr/sbin/balethirq.pl ${common_files}/usr/sbin
cp -f --no-preserve=ownership ${git_path}/build-armbian/armbian-files/common-files/etc/balance_irq ${common_files}/etc
[[ "${?}" -eq 0 ]] && echo -e "${INFO} balethirq download completed." || error_msg "balethirq download failed."
# Delete temporary files
rm -rf ${git_path}
# Download install/update and other related files
git_path="$(mktemp -d)"
git_pull_dir ${script_repo} main ${git_path}
# Move the files to the storage directory
cp -af --no-preserve=ownership ${git_path}/luci-app-amlogic/root/usr/sbin/* ${common_files}/usr/sbin
[[ "${?}" -eq 0 ]] && echo -e "${INFO} app/sbin download completed." || error_msg "app/sbin download failed."
chmod +x ${common_files}/usr/sbin/*
# Move the files to the storage directory
mkdir -p ${common_files}/usr/share/amlogic
cp -af --no-preserve=ownership ${git_path}/luci-app-amlogic/root/usr/share/amlogic ${common_files}/usr/share/amlogic
[[ "${?}" -eq 0 ]] && echo -e "${INFO} app/share download completed." || error_msg "app/share download failed."
chmod +x ${common_files}/usr/share/amlogic/*
# Delete temporary files
rm -rf ${git_path}
}
query_kernel() {
echo -e "${STEPS} Start querying the latest kernel version..."
# Check the version on the kernel repository
x="1"
for key in "${!tags_list[@]}"; do
{
# Query the name of the latest kernel version
tmp_arr_kernels=()
down_kernel_list=(${tags_list[${key}]})
i=1
for kernel_var in "${down_kernel_list[@]}"; do
echo -e "${INFO} (${x}.${i}) Auto query the latest kernel version for [ ${key} - ${kernel_var} ]"
# Identify the kernel <VERSION> and <PATCHLEVEL>, such as [ 6.1 ]
kernel_verpatch="$(echo ${kernel_var} | awk -F '.' '{print $1"."$2}')"
# Query the latest kernel version
latest_version="$(
curl -fsSL \
${kernel_api}/releases/expanded_assets/kernel_${key} |
grep -oE "${kernel_verpatch}.[0-9]+.tar.gz" | sed 's/.tar.gz//' |
sort -urV | head -n 1
)"
if [[ "${?}" -eq 0 && -n "${latest_version}" ]]; then
tmp_arr_kernels[${i}]="${latest_version}"
else
tmp_arr_kernels[${i}]="${kernel_var}"
fi
echo -e "${INFO} (${x}.${i}) [ ${key} - ${tmp_arr_kernels[$i]} ] is latest kernel. \n"
let i++
done
# Assign the latest kernel version to the array
tags_list[${key}]="${tmp_arr_kernels[@]}"
let x++
}
done
}
check_kernel() {
[[ -n "${1}" ]] && check_path="${1}" || error_msg "Invalid kernel path to check."
check_files=($(cat "${check_path}/sha256sums" | awk '{print $2}'))
for cf in "${check_files[@]}"; do
{
# Check if file exists
[[ -s "${check_path}/${cf}" ]] || error_msg "The [ ${cf} ] file is missing."
# Check if the file sha256sum is correct
tmp_sha256sum="$(sha256sum "${check_path}/${cf}" | awk '{print $1}')"
tmp_checkcode="$(cat ${check_path}/sha256sums | grep ${cf} | awk '{print $1}')"
[[ "${tmp_sha256sum}" == "${tmp_checkcode}" ]] || error_msg "[ ${cf} ]: sha256sum verification failed."
}
done
echo -e "${INFO} All [ ${#check_files[@]} ] kernel files are sha256sum checked to be complete.\n"
}
download_kernel() {
cd ${current_path}
echo -e "${STEPS} Start downloading the kernel files..."
x="1"
for key in "${!tags_list[@]}"; do
{
down_kernel_list=(${tags_list[${key}]})
# Download the kernel to the storage directory
i="1"
for kernel_var in "${down_kernel_list[@]}"; do
if [[ ! -d "${kernel_path}/${key}/${kernel_var}" ]]; then
kernel_down_from="https://github.com/${kernel_repo}/releases/download/kernel_${key}/${kernel_var}.tar.gz"
echo -e "${INFO} (${x}.${i}) [ ${key} - ${kernel_var} ] Kernel download from [ ${kernel_down_from} ]"
# Download the kernel files. If the download fails, try again 10 times.
[[ -d "${kernel_path}/${key}" ]] || mkdir -p ${kernel_path}/${key}
for t in {1..10}; do
curl -fsSL "${kernel_down_from}" -o "${kernel_path}/${key}/${kernel_var}.tar.gz"
[[ "${?}" -eq 0 ]] && break || sleep 60
done
[[ "${?}" -eq 0 ]] || error_msg "Failed to download the kernel files from the server."
# Decompress the kernel files
tar -mxzf "${kernel_path}/${key}/${kernel_var}.tar.gz" -C "${kernel_path}/${key}"
[[ "${?}" -eq 0 ]] || error_msg "[ ${kernel_var} ] kernel decompression failed."
else
echo -e "${INFO} (${x}.${i}) [ ${key} - ${kernel_var} ] Kernel is in the local directory."
fi
# If the kernel contains the sha256sums file, check the files integrity
[[ -f "${kernel_path}/${key}/${kernel_var}/sha256sums" ]] && check_kernel "${kernel_path}/${key}/${kernel_var}"
let i++
done
# Delete downloaded kernel temporary files
rm -f ${kernel_path}/${key}/*.tar.gz
sync
let x++
}
done
}
confirm_version() {
cd ${current_path}
# Columns of ${model_conf}:
# 1.ID 2.MODEL 3.SOC 4.FDTFILE 5.UBOOT_OVERLOAD 6.MAINLINE_UBOOT 7.BOOTLOADER_IMG 8.DESCRIPTION
# 9.KERNEL_TAGS 10.PLATFORM 11.FAMILY 12.BOOT_CONF 13.CONTRIBUTORS 14.BOARD 15.BUILD
# Column 5, called <UBOOT_OVERLOAD> in Amlogic, <TRUST_IMG> in Rockchip, Not used in Allwinner.
# Find [ the first ] configuration information with [ the same BOARD name ] and [ BUILD as yes ] in the ${model_conf} file.
board_conf="$(
cat ${model_conf} |
sed -e 's/NA//g' -e 's/NULL//g' -e 's/[ ][ ]*//g' |
grep -E "^[^#].*:${board}:(yes|no)$" |
head -n 1
)"
[[ -n "${board_conf}" ]] || error_msg "[ ${board} ] config is missing!"
# Get device settings options
MODEL_ID="$(echo ${board_conf} | awk -F':' '{print $1}')"
MODEL_NAME="$(echo ${board_conf} | awk -F':' '{print $2}')"
SOC="$(echo ${board_conf} | awk -F':' '{print $3}')"
FDTFILE="$(echo ${board_conf} | awk -F':' '{print $4}')"
UBOOT_OVERLOAD="$(echo ${board_conf} | awk -F':' '{print $5}')"
TRUST_IMG="${UBOOT_OVERLOAD}"
MAINLINE_UBOOT="$(echo ${board_conf} | awk -F':' '{print $6}')"
BOOTLOADER_IMG="$(echo ${board_conf} | awk -F':' '{print $7}')"
KERNEL_TAGS="$(echo ${board_conf} | awk -F':' '{print $9}')"
PLATFORM="$(echo ${board_conf} | awk -F':' '{print $10}')"
FAMILY="$(echo ${board_conf} | awk -F':' '{print $11}')"
BOOT_CONF="$(echo ${board_conf} | awk -F':' '{print $12}')"
CONTRIBUTORS="$(echo ${board_conf} | awk -F':' '{print $13}')"
# Check whether the key parameters are correct
[[ -n "${PLATFORM}" ]] || error_msg "Invalid PLATFORM parameter: [ ${PLATFORM} ]"
# Set supported platform name
support_platform=("amlogic" "rockchip" "allwinner")
[[ -n "$(echo "${support_platform[@]}" | grep -w "${PLATFORM}")" ]] || error_msg "[ ${PLATFORM} ] not supported."
# Add u-boot files record information
[[ -n "${MAINLINE_UBOOT}" ]] && RECORD_MAINLINE_UBOOT="/lib/u-boot/${MAINLINE_UBOOT}" || RECORD_MAINLINE_UBOOT=""
[[ -n "${BOOTLOADER_IMG}" ]] && RECORD_BOOTLOADER_IMG="/lib/u-boot/${BOOTLOADER_IMG}" || RECORD_BOOTLOADER_IMG=""
[[ -n "${TRUST_IMG}" ]] && RECORD_TRUST_IMG="/lib/u-boot/${TRUST_IMG}" || RECORD_TRUST_IMG=""
# Set the Amlogic u-boot series
family_rename="${FAMILY//-/_}"
eval "amlogic_uboot=(\${uboot_${family_rename}[@]})"
# Get the kernel tags and version
conf_kernel_tags="${KERNEL_TAGS%%/*}"
conf_kernel_list="${KERNEL_TAGS##*/}"
# Replace the default kernel tags with the custom kernel tags
[[ -n "${kernel_usage}" && "${conf_kernel_tags}" == "${default_tags}" ]] && conf_kernel_tags="${kernel_usage}"
# Set the kernel version array
build_kernel=()
if [[ "${conf_kernel_list}" == "all" ]]; then
build_kernel=(${tags_list[${conf_kernel_tags}]})
else
conf_kernel_list="${conf_kernel_list//[a-z]/[0-9]+}"
# Convert the string into an array, using "_" as the delimiter.
IFS='_' read -ra conf_kernel_list <<<"${conf_kernel_list}"
model_kernel=(${conf_kernel_list[@]})
latest_kernel=(${tags_list[${conf_kernel_tags}]})
# Find the kernel version that matches the custom version
for ck in "${model_kernel[@]}"; do
for lk in "${latest_kernel[@]}"; do
[[ "${lk}" =~ ^${ck}$ ]] && build_kernel+=("${lk}")
done
done
fi
# Check the kernel tags and version
[[ -n "${conf_kernel_tags}" || "${#conf_kernel_list[@]}" -eq 0 ]] || error_msg "The [ KERNEL_TAGS ] is invalid: [ ${KERNEL_TAGS} ]."
[[ "${#build_kernel[@]}" -eq 0 ]] && error_msg "The [ KERNEL_TAGS ] is invalid: [ ${KERNEL_TAGS} ]."
}
make_image() {
process_msg "(1/6) Make OpenWrt image."
cd ${current_path}
# Set Armbian image file parameters
[[ "${PLATFORM}" == "amlogic" ]] && {
skip_mb="4"
partition_table_type="msdos"
bootfs_type="fat32"
}
[[ "${PLATFORM}" == "rockchip" ]] && {
skip_mb="16"
partition_table_type="gpt"
bootfs_type="ext4"
}
[[ "${PLATFORM}" == "allwinner" ]] && {
skip_mb="16"
partition_table_type="msdos"
bootfs_type="fat32"
}
# Set OpenWrt filename
[[ -d "${out_path}" ]] || mkdir -p ${out_path}
openwrt_filename="openwrt${source_codename}_${PLATFORM}_${board}_k${kernel}_$(date +"%Y.%m.%d").img"
build_image_file="${out_path}/${openwrt_filename}"
rm -f ${build_image_file}
IMG_SIZE="$((skip_mb + boot_mb + root_mb))"
truncate -s ${IMG_SIZE}M ${build_image_file} >/dev/null 2>&1
parted -s ${build_image_file} mklabel ${partition_table_type} 2>/dev/null
parted -s ${build_image_file} mkpart primary ${bootfs_type} $((skip_mb))MiB $((skip_mb + boot_mb - 1))MiB 2>/dev/null
parted -s ${build_image_file} mkpart primary btrfs $((skip_mb + boot_mb))MiB 100% 2>/dev/null
# Mount the OpenWrt image file
loop_new="$(losetup -P -f --show "${build_image_file}")"
[[ -n "${loop_new}" ]] || error_msg "losetup ${build_image_file} failed."
# Confirm BOOT_UUID
BOOT_UUID="$(cat /proc/sys/kernel/random/uuid)"
[[ -z "${BOOT_UUID}" ]] && BOOT_UUID="$(uuidgen)"
[[ -z "${BOOT_UUID}" ]] && error_msg "The uuidgen is invalid, cannot continue."
# Confirm ROOTFS_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."
# Format bootfs partition
if [[ "${bootfs_type}" == "fat32" ]]; then
mkfs.vfat -F 32 -n "BOOT" ${loop_new}p1 >/dev/null 2>&1
else
mkfs.ext4 -F -q -U ${BOOT_UUID} -L "BOOT" -b 4k -m 0 ${loop_new}p1 >/dev/null 2>&1
fi
# Format rootfs partition
mkfs.btrfs -f -U ${ROOTFS_UUID} -L "ROOTFS" -m single ${loop_new}p2 >/dev/null 2>&1
# Write the specific bootloader for [ Amlogic ] boxes
[[ "${PLATFORM}" == "amlogic" ]] && {
bootloader_path="${uboot_path}/${PLATFORM}/bootloader"
if [[ -n "${MAINLINE_UBOOT}" && -f "${bootloader_path}/${MAINLINE_UBOOT}" ]]; then
dd if="${bootloader_path}/${MAINLINE_UBOOT}" of="${loop_new}" conv=fsync bs=1 count=444 2>/dev/null
dd if="${bootloader_path}/${MAINLINE_UBOOT}" of="${loop_new}" conv=fsync bs=512 skip=1 seek=1 2>/dev/null
#echo -e "${INFO} 01. For [ ${board} ] write bootloader: ${MAINLINE_UBOOT}"
elif [[ -n "${BOOTLOADER_IMG}" && -f "${bootloader_path}/${BOOTLOADER_IMG}" ]]; then
dd if="${bootloader_path}/${BOOTLOADER_IMG}" of="${loop_new}" conv=fsync bs=1 count=444 2>/dev/null
dd if="${bootloader_path}/${BOOTLOADER_IMG}" of="${loop_new}" conv=fsync bs=512 skip=1 seek=1 2>/dev/null
#echo -e "${INFO} 02. For [ ${board} ] write bootloader: ${BOOTLOADER_IMG}"
fi
}
# Write the specific bootloader for [ Rockchip ] boxes
[[ "${PLATFORM}" == "rockchip" ]] && {
bootloader_path="${uboot_path}/${PLATFORM}/${board}"
if [[ -n "${BOOTLOADER_IMG}" && -f "${bootloader_path}/${BOOTLOADER_IMG}" ]] &&
[[ -n "${MAINLINE_UBOOT}" && -f "${bootloader_path}/${MAINLINE_UBOOT}" ]] &&
[[ -n "${TRUST_IMG}" && -f "${bootloader_path}/${TRUST_IMG}" ]]; then
dd if="${bootloader_path}/${BOOTLOADER_IMG}" of="${loop_new}" conv=fsync,notrunc bs=512 seek=64 2>/dev/null
dd if="${bootloader_path}/${MAINLINE_UBOOT}" of="${loop_new}" conv=fsync,notrunc bs=512 seek=16384 2>/dev/null
dd if="${bootloader_path}/${TRUST_IMG}" of="${loop_new}" conv=fsync,notrunc bs=512 seek=24576 2>/dev/null
#echo -e "${INFO} 01. For [ ${board} ] write bootloader: ${TRUST_IMG}"
elif [[ -n "${BOOTLOADER_IMG}" && -f "${bootloader_path}/${BOOTLOADER_IMG}" ]] &&
[[ -n "${MAINLINE_UBOOT}" && -f "${bootloader_path}/${MAINLINE_UBOOT}" ]]; then
dd if="${bootloader_path}/${BOOTLOADER_IMG}" of="${loop_new}" conv=fsync,notrunc bs=512 seek=64 2>/dev/null
dd if="${bootloader_path}/${MAINLINE_UBOOT}" of="${loop_new}" conv=fsync,notrunc bs=512 seek=16384 2>/dev/null
#echo -e "${INFO} 02. For [ ${board} ] write bootloader: ${MAINLINE_UBOOT}"
elif [[ -n "${BOOTLOADER_IMG}" && -f "${bootloader_path}/${BOOTLOADER_IMG}" ]]; then
dd if="${bootloader_path}/${BOOTLOADER_IMG}" of="${loop_new}" conv=fsync,notrunc bs=512 skip=64 seek=64 2>/dev/null
#echo -e "${INFO} 03. For [ ${board} ] write bootloader: ${BOOTLOADER_IMG}"
fi
}
# Write the specific bootloader for [ Allwinner ] boxes
[[ "${PLATFORM}" == "allwinner" ]] && {
bootloader_path="${uboot_path}/${PLATFORM}/${board}"
if [[ -n "${BOOTLOADER_IMG}" && -f "${bootloader_path}/${BOOTLOADER_IMG}" ]] &&
[[ -n "${MAINLINE_UBOOT}" && -f "${bootloader_path}/${MAINLINE_UBOOT}" ]]; then
dd if="${bootloader_path}/${BOOTLOADER_IMG}" of="${loop_new}" conv=fsync,notrunc bs=8k seek=1 2>/dev/null
dd if="${bootloader_path}/${MAINLINE_UBOOT}" of="${loop_new}" conv=fsync,notrunc bs=8k seek=5 2>/dev/null
#echo -e "${INFO} 01. For [ ${board} ] write bootloader: ${MAINLINE_UBOOT}"
elif [[ -n "${BOOTLOADER_IMG}" && -f "${bootloader_path}/${BOOTLOADER_IMG}" ]]; then
dd if="${bootloader_path}/${BOOTLOADER_IMG}" of="${loop_new}" conv=fsync,notrunc bs=8k seek=1 2>/dev/null
#echo -e "${INFO} 02. For [ ${board} ] write bootloader: ${BOOTLOADER_IMG}"
fi
}
}
extract_openwrt() {
process_msg "(2/6) Extract OpenWrt files."
cd ${current_path}
# Create a dual-partition general directory
tag_bootfs="${tmp_path}/${kernel}/${board}/bootfs"
tag_rootfs="${tmp_path}/${kernel}/${board}/rootfs"
mkdir -p ${tag_bootfs} ${tag_rootfs}
chown root:root ${tag_bootfs} ${tag_rootfs}
# Mount bootfs
if [[ "${bootfs_type}" == "fat32" ]]; then
mount_try vfat ${loop_new}p1 ${tag_bootfs}
else
mount_try ext4 ${loop_new}p1 ${tag_bootfs}
fi
# Mount rootfs
mount_try btrfs ${loop_new}p2 ${tag_rootfs}
# Create snapshot directory
btrfs subvolume create ${tag_rootfs}/etc >/dev/null 2>&1
# Unzip the OpenWrt rootfs file
tar -mxzf ${openwrt_path}/${openwrt_default_file} -C ${tag_rootfs}
rm -rf ${tag_rootfs}/lib/modules/*
rm -f ${tag_rootfs}/rom/sbin/firstboot
# Copy the common files
[[ -d "${common_files}" ]] && cp -af --no-preserve=ownership ${common_files}/* ${tag_rootfs}
# Copy the platform files
platform_bootfs="${platform_files}/${PLATFORM}/bootfs"
platform_rootfs="${platform_files}/${PLATFORM}/rootfs"
[[ -d "${platform_bootfs}" ]] && cp -rf ${platform_bootfs}/* ${tag_bootfs}
[[ -d "${platform_rootfs}" ]] && cp -af --no-preserve=ownership ${platform_rootfs}/* ${tag_rootfs}
# Copy the different files
different_bootfs="${different_files}/${board}/bootfs"
different_rootfs="${different_files}/${board}/rootfs"
[[ -d "${different_bootfs}" ]] && cp -rf ${different_bootfs}/* ${tag_bootfs}
[[ -d "${different_rootfs}" ]] && cp -af --no-preserve=ownership ${different_rootfs}/* ${tag_rootfs}
# Copy the bootloader files
[[ -d "${tag_rootfs}/lib/u-boot" ]] || mkdir -p "${tag_rootfs}/lib/u-boot"
rm -rf ${tag_rootfs}/lib/u-boot/*
[[ -d "${bootloader_path}" ]] && cp -af --no-preserve=ownership ${bootloader_path}/* ${tag_rootfs}/lib/u-boot
# Copy the Amlogic overload files
[[ "${PLATFORM}" == "amlogic" ]] && {
for au in "${amlogic_uboot[@]}"; do
if [[ -f "${uboot_path}/${PLATFORM}/overload/u-boot-${au}" ]]; then
cp -f ${uboot_path}/${PLATFORM}/overload/u-boot-${au} ${tag_bootfs}
else
error_msg "The [ u-boot-${au} ] file is missing in the [ ${uboot_path}/${PLATFORM}/overload ] directory."
fi
done
}
# Remove the .git directories
rm -rf $(find ${tmp_path} -type d -name '.git')
}
replace_kernel() {
process_msg "(3/6) Replace the kernel."
cd ${current_path}
# Determine custom kernel filename
kernel_boot="$(ls ${kernel_path}/${conf_kernel_tags}/${kernel}/boot-${kernel}*.tar.gz 2>/dev/null | head -n 1)"
kernel_name="${kernel_boot##*/}" && kernel_name="${kernel_name:5:-7}"
[[ -n "${kernel_name}" ]] || error_msg "Missing kernel files for [ ${kernel} ]"
kernel_dtb="${kernel_path}/${conf_kernel_tags}/${kernel}/dtb-${PLATFORM}-${kernel_name}.tar.gz"
kernel_modules="${kernel_path}/${conf_kernel_tags}/${kernel}/modules-${kernel_name}.tar.gz"
[[ -s "${kernel_boot}" && -s "${kernel_dtb}" && -s "${kernel_modules}" ]] || error_msg "The 3 kernel missing."
# 01. For /boot five files
tar -mxzf ${kernel_boot} -C ${tag_bootfs}
[[ "${PLATFORM}" == "allwinner" ]] && (cd ${tag_bootfs} && cp -f uInitrd-${kernel_name} uInitrd && cp -f vmlinuz-${kernel_name} Image)
[[ "${PLATFORM}" == "amlogic" ]] && (cd ${tag_bootfs} && cp -f uInitrd-${kernel_name} uInitrd && cp -f vmlinuz-${kernel_name} zImage)
[[ "${PLATFORM}" == "rockchip" ]] && (cd ${tag_bootfs} && ln -sf uInitrd-${kernel_name} uInitrd && ln -sf vmlinuz-${kernel_name} Image)
[[ "$(ls ${tag_bootfs}/*${kernel_name} -l 2>/dev/null | grep "^-" | wc -l)" -ge "2" ]] || error_msg "The /boot files is missing."
[[ "${PLATFORM}" == "amlogic" ]] && get_textoffset "${tag_bootfs}/zImage"
# 02. For /boot/dtb/${PLATFORM}/*
[[ -d "${tag_bootfs}/dtb/${PLATFORM}" ]] || mkdir -p ${tag_bootfs}/dtb/${PLATFORM}
tar -mxzf ${kernel_dtb} -C ${tag_bootfs}/dtb/${PLATFORM}
[[ "${PLATFORM}" == "rockchip" ]] && ln -sf dtb ${tag_bootfs}/dtb-${kernel_name}
[[ "$(ls ${tag_bootfs}/dtb/${PLATFORM} -l 2>/dev/null | grep "^-" | wc -l)" -ge "2" ]] || error_msg "/boot/dtb/${PLATFORM} files is missing."
# 03. For /lib/modules/${kernel_name}
tar -mxzf ${kernel_modules} -C ${tag_rootfs}/lib/modules
(cd ${tag_rootfs}/lib/modules/${kernel_name}/ && rm -f build source *.ko 2>/dev/null && find ./ -type f -name '*.ko' -exec ln -s {} ./ \;)
[[ "$(ls ${tag_rootfs}/lib/modules/${kernel_name} -l 2>/dev/null | grep "^d" | wc -l)" -eq "1" ]] || error_msg "/usr/lib/modules kernel folder is missing."
}
refactor_bootfs() {
process_msg "(4/6) Refactor bootfs files."
cd ${tag_bootfs}
# Process Amlogic series boot partition files
[[ "${PLATFORM}" == "amlogic" && "${need_overload}" == "yes" ]] && {
# Add u-boot.ext for Amlogic 5.10 kernel
if [[ -n "${UBOOT_OVERLOAD}" && -f "${UBOOT_OVERLOAD}" ]]; then
cp -f ${UBOOT_OVERLOAD} u-boot.ext
chmod +x u-boot.ext
elif [[ -z "${UBOOT_OVERLOAD}" || ! -f "${UBOOT_OVERLOAD}" ]]; then
error_msg "${board} Board does not support using ${kernel} kernel, missing u-boot."
fi
}
# Set uEnv.txt & extlinux.conf mount parameters
uenv_rootdev="UUID=${ROOTFS_UUID} rootflags=compress=zstd:6 rootfstype=btrfs"
# Set armbianEnv.txt mount parameters
armbianenv_rootdev="UUID=${ROOTFS_UUID}"
armbianenv_rootflags="compress=zstd:6"
# Edit the uEnv.txt
uenv_conf_file="uEnv.txt"
[[ -f "${uenv_conf_file}" ]] && {
sed -i "s|LABEL=ROOTFS|${uenv_rootdev}|g" ${uenv_conf_file}
sed -i "s|meson.*.dtb|${FDTFILE}|g" ${uenv_conf_file}
sed -i "s|sun.*.dtb|${FDTFILE}|g" ${uenv_conf_file}
sed -i "s|rk.*.dtb|${FDTFILE}|g" ${uenv_conf_file}
}
# Add an alternate file (/boot/extlinux/extlinux.conf)
boot_extlinux_file="extlinux/extlinux.conf.bak"
rename_extlinux_file="extlinux/extlinux.conf"
[[ -f "${boot_extlinux_file}" ]] && {
sed -i "s|LABEL=ROOTFS|${uenv_rootdev}|g" ${boot_extlinux_file}
sed -i "s|meson.*.dtb|${FDTFILE}|g" ${boot_extlinux_file}
sed -i "s|sun.*.dtb|${FDTFILE}|g" ${boot_extlinux_file}
sed -i "s|rk.*.dtb|${FDTFILE}|g" ${boot_extlinux_file}
# If needed, such as t95z(s905x), rename delete .bak
[[ "${BOOT_CONF}" == "extlinux.conf" ]] && mv -f ${boot_extlinux_file} ${rename_extlinux_file}
}
# Edit the armbianEnv.txt
armbianenv_conf_file="armbianEnv.txt"
[[ -f "${armbianenv_conf_file}" ]] && {
sed -i "s|\(fdtfile=.*\/\)[^/]*$|\1${FDTFILE}|g" ${armbianenv_conf_file}
sed -i "s|^rootdev=.*|rootdev=${armbianenv_rootdev}|g" ${armbianenv_conf_file}
sed -i "s|^rootfstype=.*|rootfstype=btrfs|g" ${armbianenv_conf_file}
sed -i "s|^rootflags=.*|rootflags=${armbianenv_rootflags}|g" ${armbianenv_conf_file}
sed -i "s|^overlay_prefix=.*|overlay_prefix=${FAMILY}|g" ${armbianenv_conf_file}
}
# Check device configuration files
[[ -f "${uenv_conf_file}" || -f "${rename_extlinux_file}" || -f "${armbianenv_conf_file}" ]] || error_msg "Missing [ /boot/*Env.txt ]"
}
refactor_rootfs() {
process_msg "(5/6) Refactor rootfs files."
cd ${tag_rootfs}
# Add directory
mkdir -p .reserved boot run
# Edit fstab
[[ -f "etc/fstab" && -f "etc/config/fstab" ]] || error_msg "The [ fstab ] files does not exist."
sed -i "s|LABEL=ROOTFS|UUID=${ROOTFS_UUID}|g" etc/fstab
sed -i "s|option label 'ROOTFS'|option uuid '${ROOTFS_UUID}'|g" etc/config/fstab
# Set the keyword for tags in Releases
[[ -n "${source_codename}" ]] && {
sed -i "s|option amlogic_firmware_tag.*|option amlogic_firmware_tag '${source_codename}'|g" etc/config/amlogic
}
# Modify the default script to [ bash ] for [ cpustat ]
[[ -x "bin/bash" ]] && {
sed -i "s/\/bin\/ash/\/bin\/bash/" etc/passwd
sed -i "s/\/bin\/ash/\/bin\/bash/" usr/libexec/login.sh
}
# Turn off hw_flow by default
[[ -f "etc/config/turboacc" ]] && {
sed -i "s|option hw_flow.*|option hw_flow '0'|g" etc/config/turboacc
sed -i "s|option sw_flow.*|option sw_flow '0'|g" etc/config/turboacc
}
# Add custom startup script
custom_startup_script="etc/custom_service/start_service.sh"
[[ -x "${custom_startup_script}" && -f "etc/rc.local" ]] && {
sed -i '/^exit 0/i\bash /etc/custom_service/start_service.sh' etc/rc.local
}
# Modify the cpu mode to schedutil
[[ -f "etc/config/cpufreq" ]] && sed -i "s/ondemand/schedutil/" etc/config/cpufreq
# Turn off speed limit by default
[[ -f "etc/config/nft-qos" ]] && sed -i "s|option limit_enable.*|option limit_enable '0'|g" etc/config/nft-qos
# Add USB and wireless network drivers
[[ -f "etc/modules.d/usb-net-rtl8150" ]] || echo "rtl8150" >etc/modules.d/usb-net-rtl8150
# USB RTL8152/8153/8156 network card Driver
[[ -f "etc/modules.d/usb-net-rtl8152" ]] || echo "r8152" >etc/modules.d/usb-net-rtl8152
# USB AX88179 network card Driver
[[ -f "etc/modules.d/usb-net-asix-ax88179" ]] || echo "ax88179_178a" >etc/modules.d/usb-net-asix-ax88179
# brcmfmac built-in wireless network card Driver
echo "brcmfmac" >etc/modules.d/brcmfmac
echo "brcmutil" >etc/modules.d/brcmutil
echo "bcmdhd" >etc/modules.d/bcmdhd
# USB Realtek RTL8188EU Wireless LAN Driver
echo "r8188eu" >etc/modules.d/rtl8188eu
# Realtek RTL8189FS Wireless LAN Driver
echo "8189fs" >etc/modules.d/8189fs
# Realtek RTL8188FU Wireless LAN Driver
echo "rtl8188fu" >etc/modules.d/rtl8188fu
# Realtek RTL8822CS Wireless LAN Driver
echo "88x2cs" >etc/modules.d/88x2cs
# USB Ralink Wireless LAN Driver
echo "rt2500usb" >etc/modules.d/rt2500-usb
echo "rt2800usb" >etc/modules.d/rt2800-usb
echo "rt2x00usb" >etc/modules.d/rt2x00-usb
# USB Mediatek Wireless LAN Driver
echo "mt7601u" >etc/modules.d/mt7601u
echo "mt7663u" >etc/modules.d/mt7663u
echo "mt76x0u" >etc/modules.d/mt76x0u
echo "mt76x2u" >etc/modules.d/mt76x2u
echo "mt76x2e" >etc/modules.d/mt76x2e
echo "mt7921e" >etc/modules.d/mt7921e
echo "mt7915e" >etc/modules.d/mt7915e
# GPU Driver
echo "panfrost" >etc/modules.d/panfrost
# PWM Driver
echo "pwm_meson" >etc/modules.d/pwm_meson
# Ath10k Driver
echo "ath10k_core" >etc/modules.d/ath10k_core
echo "ath10k_sdio" >etc/modules.d/ath10k_sdio
echo "ath10k_usb" >etc/modules.d/ath10k_usb
echo "ath10k_pci" >etc/modules.d/ath10k-pci
echo "ath10k_core frame_mode=2" >etc/modules.d/ath10k
# Enable watchdog driver
echo "meson_gxbb_wdt" >etc/modules.d/watchdog
# For rk3588
echo "bifrost_kbase" >etc/modules.d/rk_gpu
echo "rknpu" >etc/modules.d/rk_npu
# For rk3568
echo "rockchipdrm" >etc/modules.d/drm-rockchip
echo "rk_crypto2" >etc/modules.d/rk_crypto
echo -e "snd_soc_simple_card_utils\nsnd_soc_simple_card\nsnd_soc_rockchip_i2s" >etc/modules.d/snd-rk3568
echo "pwm_fan" >etc/modules.d/pwm-fan
echo "option" >etc/modules.d/usb-serial-option