-
Notifications
You must be signed in to change notification settings - Fork 11
/
common_functions.sh
452 lines (388 loc) · 12.9 KB
/
common_functions.sh
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
#!/bin/bash
#
# Usage:
# The common functions for envsetup_soc.sh
#
# Partition generation :
# pack_boot -> boot.emmc / boot.spinand (kernel+dtb)
# pack_rootfs -> rootfs.emmc / rootfs.spinand (squashFS rootfs)
# pack_system -> system.emmc / system.spinand (3rd/sdk shared libraries, spinand-ubifs/ emmc-ext4)
# pack_gpt -> gpt.img (emmc only)
# pack_cfg -> cfg.emmc / cfg.spinand (cofigs partition for saving configs and isp pq bin)
#
function print_error()
{
printf "\e[1;31;47m\tERROR\t %s \e[0m\n" "$1"
}
function print_notice()
{
printf "\e[1;34;47m %s \e[0m\n" "$1"
}
# $1 : The path for removing files
function remove_unused_files()
{
delFiles=("*.a" "kdump" "*.la" "*DS_Store*" "include")
for file in "${delFiles[@]}";do
find "$1" -name "$file" -printf 'removing %p from rootfs\n' -prune -exec rm -rf {} \;
done
}
function create_ramdisk_folder
{(
CHIP_ARCH_LOWER=$(echo "${CHIP_ARCH}" | tr A-Z a-z)
CUST_FOLDER_NAME="$PROJECT_FULLNAME"
CHIP_FOLDER_PATH="$RAMDISK_PATH/rootfs/overlay/$CHIP"
CUST_FOLDER_PATH="$RAMDISK_PATH/rootfs/overlay/$CUST_FOLDER_NAME"
SDK_VER_FOLDER_PATH="$RAMDISK_PATH/rootfs/overlay/${CHIP_ARCH_LOWER}_${SDK_VER}"
pushd "$BUILD_PATH"
export RAMDISK_OUTPUT_BASE CHIP_FOLDER_PATH CUST_FOLDER_PATH SDK_VER_FOLDER_PATH
make ramdisk || return "$?"
popd
# copy overlay/public folder to overlay/$CHIP
local tmp_dir
tmp_dir=$(mktemp -d)
local ROOTFS_SCRIPT=
if [ -e rootfs_"$PROJECT_FULLNAME".json ]; then
ROOTFS_SCRIPT=rootfs_"$PROJECT_FULLNAME".json
else
ROOTFS_SCRIPT=rootfs_"${CHIP}_${STORAGE_TYPE}".json
fi
# create config folder with priority: project->chip
if [ -d "$RAMDISK_PATH"/configs/"$CUST_FOLDER_NAME" ] ; then
command cp "$RAMDISK_PATH"/configs/"$CUST_FOLDER_NAME"/* "$RAMDISK_PATH"/"$RAMDISK_OUTPUT_BASE"/configs
fi
if [ -d "$RAMDISK_PATH"/configs/"$CHIP"/"$SDK_VER" ]; then
command cp "$RAMDISK_PATH"/configs/"$CHIP"/"$SDK_VER"/* "$RAMDISK_PATH"/"$RAMDISK_OUTPUT_BASE"/configs
fi
if [ -d "$RAMDISK_PATH"/configs/"$CHIP" ]; then
command cp "$RAMDISK_PATH"/configs/"$CHIP"/* "$RAMDISK_PATH"/"$RAMDISK_OUTPUT_BASE"/configs
fi
if [ -d "$RAMDISK_PATH"/configs/"$CHIP_ARCH_LOWER" ]; then
command cp "$RAMDISK_PATH"/configs/"$CHIP_ARCH_LOWER"/* "$RAMDISK_PATH"/"$RAMDISK_OUTPUT_BASE"/configs
fi
command cp -rf "$RAMDISK_PATH"/"$RAMDISK_OUTPUT_BASE"/target/system/* "$SYSTEM_OUT_DIR"
command rm -rf "$RAMDISK_PATH"/"$RAMDISK_OUTPUT_BASE"/target/system
)}
function build_ramboot
{(
print_notice "Run ${FUNCNAME[0]}() function"
create_ramdisk_folder || return "$?"
_build_kernel_env
cd "$BUILD_PATH" || return
make ramboot || return "$?"
)}
function pack_boot
{(
print_notice "Run ${FUNCNAME[0]}() function"
create_ramdisk_folder
pushd "$RAMDISK_PATH"/"$RAMDISK_OUTPUT_FOLDER"
_build_kernel_env
pushd "$BUILD_PATH"
make boot || return "$?"
popd
mkdir -p "$OUTPUT_DIR"/rawimages
# Only pack header when SUBTYPE is asic to avoid storage_type is null
if [[ ${BOARD} != "fpga" && ${BOARD} != "palladium" ]]; then
command cp ./boot.itb "$OUTPUT_DIR"/rawimages/boot."$STORAGE_TYPE"
python3 "$IMGTOOL_PATH"/raw2cimg.py "$OUTPUT_DIR"/rawimages/boot."$STORAGE_TYPE" "$OUTPUT_DIR" "$FLASH_PARTITION_XML"
if [[ "${AB_SYSTEM}" == "y" ]]; then
command cp ./boot.itb "$OUTPUT_DIR"/rawimages/boot_b."$STORAGE_TYPE"
python3 "$IMGTOOL_PATH"/raw2cimg.py "$OUTPUT_DIR"/rawimages/boot_b."$STORAGE_TYPE" "$OUTPUT_DIR" "$FLASH_PARTITION_XML"
fi
else
command cp ./boot.itb "$OUTPUT_DIR"/boot.itb
fi
popd
)}
# $1 : The path for rootfs partition
function pack_rootfs
{(
echo "pack_rootfs"
print_notice "Run ${FUNCNAME[0]}_${STORAGE_TYPE}() function"
CHIP_ARCH_LOWER=$(echo "${CHIP_ARCH}" | tr A-Z a-z)
CUST_FOLDER_NAME="$PROJECT_FULLNAME"
CHIP_FOLDER_PATH="$RAMDISK_PATH"/rootfs/overlay/"$CHIP"
SDK_VER_FOLDER_PATH="$RAMDISK_PATH"/rootfs/overlay/"${CHIP_ARCH_LOWER}_${SDK_VER}"
CUST_FOLDER_PATH="$RAMDISK_PATH"/rootfs/overlay/"$CUST_FOLDER_NAME"
echo "CUST_FOLDER_NAME = ${CUST_FOLDER_NAME}"
echo "CHIP_FOLDER_PATH = ${CHIP_FOLDER_PATH}"
echo "SDK_VER_FOLDER_PATH = ${SDK_VER_FOLDER_PATH}"
echo "CUST_FOLDER_PATH = ${CUST_FOLDER_PATH}"
export ROOTFS_DIR COMMON_TOOLS_PATH FLASH_PARTITION_XML STORAGE_TYPE
export CHIP_FOLDER_PATH SDK_VER_FOLDER_PATH CUST_FOLDER_PATH
cd "$BUILD_PATH" || return
make rootfs || return "$?"
)}
function pack_data
{(
print_notice "Run ${FUNCNAME[0]}_${STORAGE_TYPE}() function"
export ROOTFS_DIR COMMON_TOOLS_PATH FLASH_PARTITION_XML STORAGE_TYPE
export CHIP_FOLDER_PATH SDK_VER_FOLDER_PATH CUST_FOLDER_PATH
mkdir -p "$OUTPUT_DIR"/data
pushd "$OUTPUT_DIR"/data;echo "If you can dream it, you can do it." > sample;popd
cd "$BUILD_PATH" || return
make jffs2 || return "$?"
)}
function clean_rootfs
{(
export ROOTFS_DIR STORAGE_TYPE
cd "$BUILD_PATH" || return
make rootfs-clean
)}
# $1 : The path for system partition
function pack_system
{(
print_notice "Run ${FUNCNAME[0]}_${STORAGE_TYPE}() function"
export TOOLS_PATH COMMON_TOOLS_PATH STORAGE_TYPE FLASH_PARTITION_XML
cd "$BUILD_PATH" || return
if [ "$STORAGE_TYPE" == "emmc" ] || [ "$STORAGE_TYPE" == "spinor" ] || [ "$STORAGE_TYPE" == "spinand" ]; then
make system || return "$?"
fi
)}
function pack_gpt
{(
if [[ $STORAGE_TYPE != "emmc" ]]; then
return 0
fi
print_notice "Run ${FUNCNAME[0]}() function"
pushd "$EMMCTOOL_PATH"
mkdir -p "$OUTPUT_DIR"/rawimages
make gpt.img PARTITION_XML="$FLASH_PARTITION_XML" INSTALL_DIR="$OUTPUT_DIR"/rawimages
test "$?" -eq 0 || return 1
python3 "$IMGTOOL_PATH"/raw2cimg.py "$OUTPUT_DIR"/rawimages/gpt.img "$OUTPUT_DIR" "$FLASH_PARTITION_XML"
popd
)}
function pack_cfg
{(
print_notice "Run ${FUNCNAME[0]}_${STORAGE_TYPE}() function"
pushd "$ISP_TUNING_PATH"
if [ $STORAGE_TYPE == "spinor" ]; then
./copyBin.sh "$OUTPUT_DIR"/rootfs/mnt/cfg/param/ "$SENSOR_TUNING_PARAM"
else
./copyBin.sh "$OUTPUT_DIR"/rootfs/mnt/cfg/tmp_secure/ "$SENSOR_TUNING_PARAM"
fi
popd
export TOOLS_PATH COMMON_TOOLS_PATH STORAGE_TYPE FLASH_PARTITION_XML ROOTFS_DIR
cd "$BUILD_PATH" || return
if [ $STORAGE_TYPE != "sd" ]; then
make cfg || return "$?"
fi
)}
function copy_tools
{(
# Copy USB_DL, partition.xml and bootlogo
if [[ "${chip_cv[*]}" =~ "$CHIP" ]] && [[ ${BOARD} != "fpga" && ${BOARD} != "palladium" ]]; then
command rm -rf "$OUTPUT_DIR"/tools
command mkdir -p "$OUTPUT_DIR"/tools/
command cp -rf "$TOOLS_PATH"/common/usb_dl/ "$OUTPUT_DIR"/tools/
if [ "$ENABLE_BOOTLOGO" -eq 1 ];then
python3 "$IMGTOOL_PATH"/raw2cimg.py "$BOOTLOGO_PATH" "$OUTPUT_DIR" "$FLASH_PARTITION_XML"
fi
command cp --remove-destination "$FLASH_PARTITION_XML" "$OUTPUT_DIR"/
fi
)}
function pack_upgrade
{(
local TMPDIR
TMPDIR=$(mktemp -d)
local extra_files_args="-f utils $TOOLS_PATH/common/ota_tool/utils/cimg2raw"
if [[ $STORAGE_TYPE == "spinand" ]]; then
extra_files_args="$extra_files_args -f utils $TOOLS_PATH/common/ota_tool/utils/nandwrite"
fi
# Customer can customize
{
echo "Version=1"
echo "STORAGE_TYPE=$STORAGE_TYPE"
echo "CHIP=$CHIP"
echo "BOARD=$BOARD"
} >> "$TMPDIR"/misc_info.txt
extra_files_args="$extra_files_args -f META $TMPDIR/misc_info.txt"
# Generating default ota script.
# Customer can customize thils script. Beware the script should be utils/update-script.sh
if [[ $STORAGE_TYPE == "emmc" || $STORAGE_TYPE == "spinand" ]]; then
python3 "$TOOLS_PATH"/common/ota_tool/gen_ota_script "$FLASH_PARTITION_XML" "$TMPDIR"
extra_files_args="$extra_files_args -f utils $TMPDIR/update-script.sh"
# Customize example
# Rename original update-script.sh so we can invoke it later by our own wrapped one
mv "$TMPDIR"/update-script.sh "$TMPDIR"/update-script.sh_
extra_files_args=$extra_files_args"_"
fi
# Below is an example application to support panel display during updating
for each in "$TOOLS_PATH"/common/ota_tool/utils/example/*
do
extra_files_args="$extra_files_args -f utils $each"
done
python3 "$IMGTOOL_PATH"/mk_package.py "$FLASH_PARTITION_XML" "$OUTPUT_DIR" -o "$OUTPUT_DIR"/upgrade.zip $extra_files_args
command rm -rf "$TMPDIR"
)}
function pack_burn_image
{(
pushd "$OUTPUT_DIR"
[ -d tmp ] && rm -rf tmp
[ -d br-rootfs ] && rm -rf br-rootfs
mkdir br-rootfs
tar xf $BR_DIR/output/$BR_BOARD/images/rootfs.tar.xz -C br-rootfs
# genimage
export PATH=${TOP_DIR}/build/tools/common/sd_tools:${PATH}
export LD_LIBRARY_PATH=$TOP_DIR/build/tools/common/sd_tools/libconfuse/lib:${LD_LIBRARY_PATH}
image=sophpi-duo-`date +%Y%m%d-%H%M`.img
cp $COMMON_TOOLS_PATH/sd_tools/genimage.cfg $COMMON_TOOLS_PATH/sd_tools/genimage.cfg.tmp
sed -i 's/sophpi-duo.img/'"$image"'/' $COMMON_TOOLS_PATH/sd_tools/genimage.cfg.tmp
genimage --config $COMMON_TOOLS_PATH/sd_tools/genimage.cfg.tmp --rootpath $OUTPUT_DIR/br-rootfs --inputpath $OUTPUT_DIR --outputpath $OUTPUT_DIR
popd
)}
function pack_prog_img
{(
local tmp_dir
tmp_dir="$OUTPUT_DIR"/temp
mkdir -p "$tmp_dir"
rm -rf "${tmp_dir:?}/"*
if [[ "$STORAGE_TYPE" = "spinand" ]]; then
pushd "$SPINANDTOOL_PATH"/sv_tool
make || return "$?"
./create_sv -c 5 -o "$tmp_dir"/sv.bin
popd
cp "$OUTPUT_DIR"/rawimages/*."$STORAGE_TYPE" "$tmp_dir"
cp "$OUTPUT_DIR"/*.xml "$tmp_dir"
cp "$OUTPUT_DIR"/fip.bin "$tmp_dir"
# Tar images
tar -caf "$OUTPUT_DIR"/prog_img.tar.gz -C "$tmp_dir" .
# List images in tar
tar -tzvf "$OUTPUT_DIR"/prog_img.tar.gz
elif [[ "$STORAGE_TYPE" = "emmc" ]]; then
cp "$OUTPUT_DIR"/fip.bin "$OUTPUT_DIR"/rawimages/
python3 "$IMGTOOL_PATH"/pack_emmc_bin.py "$FLASH_PARTITION_XML" "$OUTPUT_DIR"/rawimages/ "$tmp_dir" -v
fi
# Tar images
tar -caf "$OUTPUT_DIR"/prog_img.tar.gz -C "$tmp_dir" .
# List images in tar
tar -tzvf "$OUTPUT_DIR"/prog_img.tar.gz
rm -rf "$tmp_dir"
)}
pathremove()
{
local IFS=':'
local NEWPATH
local DIR
local PATHVARIABLE=${2:-PATH}
for DIR in ${!PATHVARIABLE} ; do
if [ "$DIR" != "$1" ] ; then
NEWPATH=${NEWPATH:+$NEWPATH:}$DIR
fi
done
export $PATHVARIABLE="$NEWPATH"
}
pathprepend()
{
pathremove $1 $2
local PATHVARIABLE=${2:-PATH}
export $PATHVARIABLE="$1${!PATHVARIABLE:+:${!PATHVARIABLE}}"
}
pathappend()
{
pathremove $1 $2
local PATHVARIABLE=${2:-PATH}
export $PATHVARIABLE="${!PATHVARIABLE:+${!PATHVARIABLE}:}$1"
}
function _setconfig_complete()
{
local cur prev
COMPREPLY=()
prev="${COMP_WORDS[COMP_CWORD-1]}"
cur="${COMP_WORDS[COMP_CWORD]}"
if [[ -z "${cur}" ]]; then
COMPREPLY=("${!_BUILD_KCONFIG_MAP[@]}")
elif [[ "${cur}" =~ ^[nym]$ ]] && [[ "${prev}" = "=" ]]; then
COMPREPLY=("${cur} ")
else
if [[ "${cur}" = "=" ]]; then
case "${_BUILD_KCONFIG_MAP[${prev}=]}" in
"<bool>")
COMPREPLY=("y" "n")
;;
"<tristate>")
COMPREPLY=("y" "n" "m")
;;
*)
COMPREPLY=()
esac
else
mapfile -t COMPREPLY < <(compgen -W "${_BUILD_KCONFIG_MAP_STR}" -- "${cur}")
fi
fi
return 0
}
function _call_kconfig_script()
{
local ret script
script=$1
shift
print_notice "Run ${script} function"
(
set -e
cd "$BUILD_PATH" || return
"${BUILD_PATH}/scripts/${script}.py" "$@"
"${BUILD_PATH}/scripts/savedefconfig.py" --out "${BUILD_PATH}/.defconfig"
)
ret=$?; if [ $ret -ne 0 ]; then return $ret; fi
cvi_setup_env || return $?
cvi_print_env
(
set -e
cd "$BUILD_PATH" || return
"${BUILD_PATH}/scripts/gencmakeconfig.py" "${BUILD_PATH}/.config"
)
ret=$?; if [ $ret -ne 0 ]; then return $ret; fi
source "$BUILD_PATH/output/config_map.sh"
_BUILD_KCONFIG_MAP_STR="${!_BUILD_KCONFIG_MAP[*]}"
complete -o nospace -F _setconfig_complete setconfig
}
function oldconfig()
{
_call_kconfig_script "${FUNCNAME[0]}"
}
function olddefconfig()
{
_call_kconfig_script "${FUNCNAME[0]}"
}
function defconfig()
{
local chip_arch
local board
board=$1
chip_arch=`"${BUILD_PATH}/scripts/boards_scan.py" --get-chip-arch --board_name ${board}`
# if input is chip series, then list boards by chip series
if [ "${chip_arch}" != "" ]; then
_call_kconfig_script "${FUNCNAME[0]}" "${BUILD_PATH}/boards/${chip_arch}/${board}/${board}_defconfig"
else
"${BUILD_PATH}/scripts/boards_scan.py" --list-boards ${board}
fi
}
function menuconfig()
{
_call_kconfig_script "${FUNCNAME[0]}"
}
function guiconfig()
{
_call_kconfig_script "${FUNCNAME[0]}"
}
function setconfig()
{
_call_kconfig_script "${FUNCNAME[0]}" "$@"
}
function _build_add_bash_completion()
{
_boards=$(find "${BUILD_PATH}/boards" -mindepth 2 -maxdepth 2 -type d -not -path '*/default/*' -printf '%f ')
complete -W "$_boards" defconfig
complete -r setconfig 2> /dev/null || return 0
}
function _gen_build_env()
{
local _tmp ret
_tmp=$(python3 "${TOP_DIR}/build/scripts/boards_scan.py" --scan-boards-config)
ret=$?
[[ "$ret" == 0 ]] || return $ret
# shellcheck disable=SC1090
source <(echo "${_tmp}")
}
_gen_build_env || return $?
_build_add_bash_completion