hc
2024-11-15 a46a1ad097419aeea7350987dd95230f50d90392
commit | author | age
a07526 1 #!/bin/bash
H 2
3 set -e
4
5 if ! which fakeroot; then
6     echo "fakeroot not found! (sudo apt-get install fakeroot)"
7     exit -1
8 fi
9
10 SCRIPT_DIR=$(dirname $(realpath $BASH_SOURCE))
11 TOP_DIR=$(realpath $SCRIPT_DIR/../../..)
12 cd $TOP_DIR
13
14 DEV_DIR="$TOP_DIR/device/rockchip"
15 OUT_DIR="$TOP_DIR/buildroot/output"
16 IMG_DIR="$OUT_DIR/$RK_CFG_BUILDROOT/images"
17
18 function unset_board_config_all()
19 {
20     local tmp_file=`mktemp`
21     grep -o "^export.*RK_.*=" `find $DEV_DIR -name "Board*.mk" -type f` -h \
22         | sort | uniq > $tmp_file
23     source $tmp_file
24     rm -f $tmp_file
25 }
26 unset_board_config_all
27
28 source $DEV_DIR/.BoardConfig.mk
29 ROCKDEV=$TOP_DIR/rockdev
30 PARAMETER=$DEV_DIR/$RK_TARGET_PRODUCT/$RK_PARAMETER
31 MISC_IMG=$DEV_DIR/rockimg/$RK_MISC
32 if [ "$RK_RAMDISK_SECURITY_BOOTUP" = "true" ];then
33    ROOTFS_IMG=$IMG_DIR/security-system.img
34 else
35    ROOTFS_IMG=$TOP_DIR/$RK_ROOTFS_IMG
36 fi
37
38 ROOTFS_IMG_SOURCE=$IMG_DIR/rootfs.$RK_ROOTFS_TYPE
39 RAMBOOT_IMG=$OUT_DIR/$RK_CFG_RAMBOOT/images/ramboot.img
40 RECOVERY_IMG=$OUT_DIR/$RK_CFG_RECOVERY/images/recovery.img
41 TRUST_IMG=$TOP_DIR/u-boot/trust.img
42 UBOOT_IMG=$TOP_DIR/u-boot/uboot.img
43 BOOT_IMG=$TOP_DIR/kernel/$RK_BOOT_IMG
44 LOADER=$(echo $TOP_DIR/u-boot/*_loader_*v*.bin | head -1)
45 SPL=$(echo $TOP_DIR/u-boot/*_loader_spl.bin | head -1)
46 MKIMAGE=$SCRIPT_DIR/mk-image.sh
47
48 message() {
49     echo -e "\e[36m $@ \e[0m"
50 }
51
52 fatal() {
53     echo -e "\e[31m $@ \e[0m"
54     exit -1
55 }
56
57 mkdir -p $ROCKDEV
58
59 # Require buildroot host tools to do image packing.
60 if [ ! -d "$TARGET_OUTPUT_DIR" ]; then
61     message "Source buildroot/build/envsetup.sh"
62     if [ "${RK_CFG_RAMBOOT}" ];then
63         source $TOP_DIR/buildroot/build/envsetup.sh $RK_CFG_RAMBOOT
64     fi
65     if [ "${RK_CFG_BUILDROOT}" ];then
66         source $TOP_DIR/buildroot/build/envsetup.sh $RK_CFG_BUILDROOT
67     fi
68 fi
69
70 # Parse size limit from parameter.txt, 0 means unlimited or not exists.
71 partition_size_kb() {
72     PART_NAME=$1
73     PART_STR=$(grep -oE "[^,^:^\(]*\(${PART_NAME}[\)_:][^\)]*\)" $PARAMETER)
74     PART_SIZE=$(echo $PART_STR | grep -oE "^[^@^-]*")
75     echo $(( ${PART_SIZE:-0} / 2 ))
76 }
77
78 # Assert the image's size smaller than parameter.txt's limit
79 assert_size() {
80     PART_NAME="$1"
81     IMG="$2"
82
83     PART_SIZE=$(partition_size_kb $PART_NAME)
84     [ "$PART_SIZE" -gt 0 ] || return 0
85
86     IMG_SIZE=$(stat -c "%s" "$IMG")
87
88     if [ $PART_SIZE -lt $(( "$IMG_SIZE" / 1024 )) ]; then
89         fatal "error: $IMG's size exceed parameter.txt's limit!"
90     fi
91 }
92
93 link_image() {
94     SRC="$1"
95     DST="$2"
96     FALLBACK="$3"
97
98     message "Linking $DST from $SRC..."
99
100     if [ ! -e "$SRC" ]; then
101         if [ -e "$FALLBACK" ]; then
102             SRC="$FALLBACK"
103             message "Fallback to $SRC"
104         else
105             message "warning: $SRC not found!"
106         fi
107     fi
108
109     ln -rsf "$SRC" "$ROCKDEV/$DST"
110     assert_size "${DST%.img}" "$SRC"
111
112     message "Done linking $DST"
113 }
114
115 link_image_optional() {
116     link_image "$@" || true
117 }
118
119 pack_image() {
120     SRC="$1"
121     DST="$2"
122     FS_TYPE="$3"
123     SIZE="${4:-$(partition_size_kb "${DST%.img}")}"
124     LABEL="$5"
125     EXTRA_CMD="$6"
126
127     FAKEROOT_SCRIPT="$ROCKDEV/${DST%.img}.fs"
128
129     message "Packing $DST from $SRC..."
130
131     if [ ! -d "$SRC" ]; then
132         message "warning: $SRC not found!"
133         return 0
134     fi
135
136     cat << EOF > $FAKEROOT_SCRIPT
137 #!/bin/sh -e
138 $EXTRA_CMD
139 $MKIMAGE "$SRC" "$ROCKDEV/$DST" "$FS_TYPE" "$SIZE" "$LABEL"
140 EOF
141
142     chmod a+x "$FAKEROOT_SCRIPT"
143     fakeroot -- "$FAKEROOT_SCRIPT"
144     rm -f "$FAKEROOT_SCRIPT"
145
146     assert_size "${DST%.img}" "$ROCKDEV/$DST"
147
148     message "Done packing $DST"
149 }
150
151 # Convert legacy partition variables to new style
152 legacy_partion() {
153     PART_NAME="$1"
154     SRC="$2"
155     FS_TYPE="$3"
156     SIZE="${4:-0}"
157     MOUNT="/$PART_NAME"
158     OPT=""
159
160     [ "$FS_TYPE" ] || return 0
161     [ "$SRC" ] || return 0
162
163     # Fixed size for ubi
164     if [ "$FS_TYPE" = ubi ]; then
165         OPT="fixed"
166     fi
167
168     case $SIZE in
169         *k|*K)
170             SIZE=${SIZE//k/K}
171             ;;
172         *m|*M)
173             SIZE=${SIZE//m/M}
174             ;;
175         *)
176             SIZE=$(( ${SIZE} / 1024 ))K # default is bytes
177             ;;
178     esac
179
180     echo "$PART_NAME:$MOUNT:$FS_TYPE:defaults:$SRC:${SIZE}:$OPT"
181 }
182
183 RK_LEGACY_PARTITIONS=" \
184     $(legacy_partion oem "$RK_OEM_DIR" "$RK_OEM_FS_TYPE" "$RK_OEM_PARTITION_SIZE")
185     $(legacy_partion userdata "$RK_USERDATA_DIR" "$RK_USERDATA_FS_TYPE" "$RK_USERDATA_PARTITION_SIZE")
186 "
187
188 # <dev>:<mount point>:<fs type>:<mount flags>:<source dir>:<image size(M|K|auto)>:[options]
189 # for example:
190 # RK_EXTRA_PARTITIONS="oem:/oem:ext2:defaults:oem_normal:256M:fixed
191 # userdata:/userdata:vfat:errors=remount-ro:userdata_empty:auto"
192 RK_EXTRA_PARTITIONS="${RK_EXTRA_PARTITIONS:-${RK_LEGACY_PARTITIONS}}"
193
194 partition_arg() {
195     PART="$1"
196     I="$2"
197     DEFAULT="$3"
198
199     ARG=$(echo $PART | cut -d':' -f"$I")
200     echo ${ARG:-$DEFAULT}
201 }
202
203 pack_extra_partitions() {
204     for part in ${RK_EXTRA_PARTITIONS//@/ }; do
205         DEV="$(partition_arg "$part" 1)"
206
207         # Dev is either <name> or /dev/.../<name>
208         [ "$DEV" ] || continue
209         PART_NAME="${DEV##*/}"
210
211         MOUNT="$(partition_arg "$part" 2 "/$PART_NAME")"
212         FS_TYPE="$(partition_arg "$part" 3)"
213
214         SRC="$(partition_arg "$part" 5)"
215
216         # Src is either none or relative path to device/rockchip/<name>/
217         # or absolute path
218         case "$SRC" in
219             "")
220                 continue
221                 ;;
222             /*)
223                 ;;
224             *)
225                 SRC="$DEV_DIR/$PART_NAME/$SRC"
226                 ;;
227         esac
228
229         SIZE="$(partition_arg "$part" 6 auto)"
230         OPTS="$(partition_arg "$part" 7)"
231         LABEL=
232         EXTRA_CMD=
233
234         # Special handling for oem
235         if [ "$PART_NAME" = oem ]; then
236             # Skip packing oem when builtin
237             [ "${RK_OEM_BUILDIN_BUILDROOT}" != "YES" ] || continue
238
239             if [ -d "$SRC/www" ]; then
240                 EXTRA_CMD="chown -R www-data:www-data $SRC/www"
241             fi
242         fi
243
244         # Skip boot time resize by adding a label
245         if echo $OPTS | grep -wq fixed; then
246             LABEL="$PART_NAME"
247         fi
248
249         pack_image "$SRC" "${PART_NAME}.img" "$FS_TYPE" "$SIZE" "$LABEL" \
250             "$EXTRA_CMD"
251     done
252 }
253
254 link_image_optional "$PARAMETER" parameter.txt
255
256 link_image_optional "$UBOOT_IMG" uboot.img
257
258 [ "$RK_UBOOT_FORMAT_TYPE" != "fit" ] && \
259     link_image_optional "$TRUST_IMG" trust.img
260
261 link_image_optional "$LOADER" MiniLoaderAll.bin "$SPL"
262
263 [ "$RK_BOOT_IMG" ] && link_image_optional "$BOOT_IMG" boot.img
264
265 [ $RK_CFG_RAMBOOT ] && link_image_optional "$RAMBOOT_IMG" boot.img
266
267 [ "$RK_CFG_RECOVERY" ] && link_image_optional "$RECOVERY_IMG" recovery.img
268
269 [ "$RK_MISC" ] && \
270     link_image_optional "$DEV_DIR/rockimg/misc.img" misc.img "$MISC_IMG"
271
272 [ "$RK_ROOTFS_IMG" ] && \
273     link_image_optional "$ROOTFS_IMG" rootfs.img "$ROOTFS_IMG_SOURCE"
274
275 [ "${RK_OEM_BUILDIN_BUILDROOT}" = "YES" ] && \
276     link_image_optional "$IMG_DIR/oem.img" oem.img
277
278 if [ "$RK_RAMDISK_SECURITY_BOOTUP" = "true" ]; then
279     for part in boot recovery rootfs;do
280         test -e $TOP_DIR/u-boot/${part}.img &&
281             link_image "$TOP_DIR/u-boot/${part}.img" ${part}.img && \
282                 message "Enabled ramdisk security $part..." || true
283     done
284 fi
285
286 pack_extra_partitions
287
288 message "Images in $ROCKDEV are ready!"