hc
2023-12-06 d38611ca164021d018c1b23eee65bbebc09c63e0
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
#!/bin/bash
 
set -e
 
if ! which fakeroot; then
    echo "fakeroot not found! (sudo apt-get install fakeroot)"
    exit -1
fi
 
SCRIPT_DIR=$(dirname $(realpath $BASH_SOURCE))
TOP_DIR=$(realpath $SCRIPT_DIR/../../..)
cd $TOP_DIR
 
DEV_DIR="$TOP_DIR/device/rockchip"
OUT_DIR="$TOP_DIR/buildroot/output"
IMG_DIR="$OUT_DIR/$RK_CFG_BUILDROOT/images"
 
function unset_board_config_all()
{
    local tmp_file=`mktemp`
    grep -o "^export.*RK_.*=" `find $DEV_DIR -name "Board*.mk" -type f` -h \
        | sort | uniq > $tmp_file
    source $tmp_file
    rm -f $tmp_file
}
unset_board_config_all
 
source $DEV_DIR/.BoardConfig.mk
ROCKDEV=$TOP_DIR/rockdev
PARAMETER=$DEV_DIR/$RK_TARGET_PRODUCT/$RK_PARAMETER
MISC_IMG=$DEV_DIR/rockimg/$RK_MISC
if [ "$RK_RAMDISK_SECURITY_BOOTUP" = "true" ];then
   ROOTFS_IMG=$IMG_DIR/security-system.img
else
   ROOTFS_IMG=$TOP_DIR/$RK_ROOTFS_IMG
fi
 
ROOTFS_IMG_SOURCE=$IMG_DIR/rootfs.$RK_ROOTFS_TYPE
RAMBOOT_IMG=$OUT_DIR/$RK_CFG_RAMBOOT/images/ramboot.img
RECOVERY_IMG=$OUT_DIR/$RK_CFG_RECOVERY/images/recovery.img
TRUST_IMG=$TOP_DIR/u-boot/trust.img
UBOOT_IMG=$TOP_DIR/u-boot/uboot.img
BOOT_IMG=$TOP_DIR/kernel/$RK_BOOT_IMG
LOADER=$(echo $TOP_DIR/u-boot/*_loader_*v*.bin | head -1)
SPL=$(echo $TOP_DIR/u-boot/*_loader_spl.bin | head -1)
MKIMAGE=$SCRIPT_DIR/mk-image.sh
 
message() {
    echo -e "\e[36m $@ \e[0m"
}
 
fatal() {
    echo -e "\e[31m $@ \e[0m"
    exit -1
}
 
mkdir -p $ROCKDEV
 
# Require buildroot host tools to do image packing.
if [ ! -d "$TARGET_OUTPUT_DIR" ]; then
    message "Source buildroot/build/envsetup.sh"
    if [ "${RK_CFG_RAMBOOT}" ];then
        source $TOP_DIR/buildroot/build/envsetup.sh $RK_CFG_RAMBOOT
    fi
    if [ "${RK_CFG_BUILDROOT}" ];then
        source $TOP_DIR/buildroot/build/envsetup.sh $RK_CFG_BUILDROOT
    fi
fi
 
# Parse size limit from parameter.txt, 0 means unlimited or not exists.
partition_size_kb() {
    PART_NAME=$1
    PART_STR=$(grep -oE "[^,^:^\(]*\(${PART_NAME}[\)_:][^\)]*\)" $PARAMETER)
    PART_SIZE=$(echo $PART_STR | grep -oE "^[^@^-]*")
    echo $(( ${PART_SIZE:-0} / 2 ))
}
 
# Assert the image's size smaller than parameter.txt's limit
assert_size() {
    PART_NAME="$1"
    IMG="$2"
 
    PART_SIZE=$(partition_size_kb $PART_NAME)
    [ "$PART_SIZE" -gt 0 ] || return 0
 
    IMG_SIZE=$(stat -c "%s" "$IMG")
 
    if [ $PART_SIZE -lt $(( $IMG_SIZE / 1024 )) ]; then
        fatal "error: $IMG's size exceed parameter.txt's limit!"
    fi
}
 
link_image() {
    SRC="$1"
    DST="$2"
    FALLBACK="$3"
 
    message "Linking $DST from $SRC..."
 
    if [ ! -e "$SRC" ]; then
        if [ -e "$FALLBACK" ]; then
            SRC="$FALLBACK"
            message "Fallback to $SRC"
        else
            message "warning: $SRC not found!"
        fi
    fi
 
    ln -rsf "$SRC" "$ROCKDEV/$DST"
    assert_size "${DST%.img}" "$SRC"
 
    message "Done linking $DST"
}
 
link_image_optional() {
    link_image "$@" || true
}
 
pack_image() {
    SRC="$1"
    DST="$2"
    FS_TYPE="$3"
    SIZE="${4:-$(partition_size_kb "${DST%.img}")}"
    LABEL="$5"
    EXTRA_CMD="$6"
 
    FAKEROOT_SCRIPT="$ROCKDEV/${DST%.img}.fs"
 
    message "Packing $DST from $SRC..."
 
    if [ ! -d "$SRC" ]; then
        message "warning: $SRC not found!"
        return 0
    fi
 
    cat << EOF > $FAKEROOT_SCRIPT
#!/bin/sh -e
$EXTRA_CMD
$MKIMAGE "$SRC" "$ROCKDEV/$DST" "$FS_TYPE" "$SIZE" "$LABEL"
EOF
 
    chmod a+x "$FAKEROOT_SCRIPT"
    fakeroot -- "$FAKEROOT_SCRIPT"
    rm -f "$FAKEROOT_SCRIPT"
 
    assert_size "${DST%.img}" "$ROCKDEV/$DST"
 
    message "Done packing $DST"
}
 
# Convert legacy partition variables to new style
legacy_partion() {
    PART_NAME="$1"
    SRC="$2"
    FS_TYPE="$3"
    SIZE="${4:-0}"
    MOUNT="/$PART_NAME"
    OPT=""
 
    [ "$FS_TYPE" ] || return 0
    [ "$SRC" ] || return 0
 
    # Fixed size for ubi
    if [ "$FS_TYPE" = ubi ]; then
        OPT="fixed"
    fi
 
    case $SIZE in
        *k|*K)
            SIZE=${SIZE//k/K}
            ;;
        *m|*M)
            SIZE=${SIZE//m/M}
            ;;
        *)
            SIZE=$(( ${SIZE} / 1024 ))K # default is bytes
            ;;
    esac
 
    echo "$PART_NAME:$MOUNT:$FS_TYPE:defaults:$SRC:${SIZE}:$OPT"
}
 
RK_LEGACY_PARTITIONS=" \
    $(legacy_partion oem "$RK_OEM_DIR" "$RK_OEM_FS_TYPE" "$RK_OEM_PARTITION_SIZE")
    $(legacy_partion userdata "$RK_USERDATA_DIR" "$RK_USERDATA_FS_TYPE" "$RK_USERDATA_PARTITION_SIZE")
"
 
# <dev>:<mount point>:<fs type>:<mount flags>:<source dir>:<image size(M|K|auto)>:[options]
# for example:
# RK_EXTRA_PARTITIONS="oem:/oem:ext2:defaults:oem_normal:256M:fixed
# userdata:/userdata:vfat:errors=remount-ro:userdata_empty:auto"
RK_EXTRA_PARTITIONS="${RK_EXTRA_PARTITIONS:-${RK_LEGACY_PARTITIONS}}"
 
partition_arg() {
    PART="$1"
    I="$2"
    DEFAULT="$3"
 
    ARG=$(echo $PART | cut -d':' -f"$I")
    echo ${ARG:-$DEFAULT}
}
 
pack_extra_partitions() {
    for part in ${RK_EXTRA_PARTITIONS//@/ }; do
        DEV="$(partition_arg "$part" 1)"
 
        # Dev is either <name> or /dev/.../<name>
        [ "$DEV" ] || continue
        PART_NAME="${DEV##*/}"
 
        MOUNT="$(partition_arg "$part" 2 "/$PART_NAME")"
        FS_TYPE="$(partition_arg "$part" 3)"
 
        SRC="$(partition_arg "$part" 5)"
 
        # Src is either none or relative path to device/rockchip/<name>/
        # or absolute path
        case "$SRC" in
            "")
                continue
                ;;
            /*)
                ;;
            *)
                SRC="$DEV_DIR/$PART_NAME/$SRC"
                ;;
        esac
 
        SIZE="$(partition_arg "$part" 6 auto)"
        OPTS="$(partition_arg "$part" 7)"
        LABEL="$PART_NAME"
        EXTRA_CMD=
 
        # Special handling for oem
        if [ "$PART_NAME" = oem ]; then
            # Skip packing oem when builtin
            [ "${RK_OEM_BUILDIN_BUILDROOT}" != "YES" ] || continue
 
            if [ -d "$SRC/www" ]; then
                EXTRA_CMD="chown -R www-data:www-data $SRC/www"
            fi
        fi
 
        # Skip boot time resize by adding a tag file
        echo $OPTS | grep -wq fixed || touch "$SRC/.fixed"
 
        pack_image "$SRC" "${PART_NAME}.img" "$FS_TYPE" "$SIZE" "$LABEL" \
            "$EXTRA_CMD"
 
        rm -rf "$SRC/.fixed"
    done
}
 
link_image_optional "$PARAMETER" parameter.txt
 
link_image_optional "$UBOOT_IMG" uboot.img
 
[ "$RK_UBOOT_FORMAT_TYPE" != "fit" ] && \
    link_image_optional "$TRUST_IMG" trust.img
 
link_image_optional "$LOADER" MiniLoaderAll.bin "$SPL"
 
[ "$RK_BOOT_IMG" ] && link_image_optional "$BOOT_IMG" boot.img
 
[ $RK_CFG_RAMBOOT ] && link_image_optional "$RAMBOOT_IMG" boot.img
 
[ "$RK_CFG_RECOVERY" ] && link_image_optional "$RECOVERY_IMG" recovery.img
 
[ "$RK_MISC" ] && \
    link_image_optional "$DEV_DIR/rockimg/misc.img" misc.img "$MISC_IMG"
 
[ "$RK_ROOTFS_IMG" ] && \
    link_image_optional "$ROOTFS_IMG" rootfs.img "$ROOTFS_IMG_SOURCE"
 
[ "${RK_OEM_BUILDIN_BUILDROOT}" = "YES" ] && \
    link_image_optional "$IMG_DIR/oem.img" oem.img
 
if [ "$RK_RAMDISK_SECURITY_BOOTUP" = "true" ]; then
    for part in boot recovery rootfs;do
        test -e $TOP_DIR/u-boot/${part}.img &&
            link_image "$TOP_DIR/u-boot/${part}.img" ${part}.img && \
                message "Enabled ramdisk security $part..." || true
    done
fi
 
pack_extra_partitions
 
message "Images in $ROCKDEV are ready!"