blob: 11e40f38868f8ec84cec9ad7adf2188592143ca2 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001#!/bin/sh
2# Copyright (C) 2006-2012 OpenWrt.org
3set -e -x
4if [ $# -ne 5 ] && [ $# -ne 6 ]; then
5 echo "SYNTAX: $0 <file> <kernel size> <kernel directory> <rootfs size> <rootfs image> [<align>]"
6 exit 1
7fi
8
9OUTPUT="$1"
10KERNELSIZE="$2"
11KERNELDIR="$3"
12KERNELPARTTYPE=${KERNELPARTTYPE:-83}
13ROOTFSSIZE="$4"
14ROOTFSIMAGE="$5"
15ROOTFSPARTTYPE=${ROOTFSPARTTYPE:-83}
16ALIGN="$6"
17
18rm -f "$OUTPUT"
19
20head=16
21sect=63
22
23# create partition table
24set $(ptgen -o "$OUTPUT" -h $head -s $sect ${GUID:+-g} -t "${KERNELPARTTYPE}" -p "${KERNELSIZE}m${PARTOFFSET:+@$PARTOFFSET}" -t "${ROOTFSPARTTYPE}" -p "${ROOTFSSIZE}m" ${ALIGN:+-l $ALIGN} ${SIGNATURE:+-S 0x$SIGNATURE} ${GUID:+-G $GUID})
25
26KERNELOFFSET="$(($1 / 512))"
27KERNELSIZE="$2"
28ROOTFSOFFSET="$(($3 / 512))"
29ROOTFSSIZE="$(($4 / 512))"
30
31# Using mcopy -s ... is using READDIR(3) to iterate through the directory
32# entries, hence they end up in the FAT filesystem in traversal order which
33# breaks reproducibility.
34# Implement recursive copy with reproducible order.
35dos_dircopy() {
36 local entry
37 local baseentry
38 for entry in "$1"/* ; do
39 if [ -f "$entry" ]; then
40 mcopy -i "$OUTPUT.kernel" "$entry" ::"$2"
41 elif [ -d "$entry" ]; then
42 baseentry="$(basename "$entry")"
43 mmd -i "$OUTPUT.kernel" ::"$2""$baseentry"
44 dos_dircopy "$entry" "$2""$baseentry"/
45 fi
46 done
47}
48
49[ -n "$PADDING" ] && dd if=/dev/zero of="$OUTPUT" bs=512 seek="$ROOTFSOFFSET" conv=notrunc count="$ROOTFSSIZE"
50dd if="$ROOTFSIMAGE" of="$OUTPUT" bs=512 seek="$ROOTFSOFFSET" conv=notrunc
51
52if [ -n "$GUID" ]; then
53 [ -n "$PADDING" ] && dd if=/dev/zero of="$OUTPUT" bs=512 seek="$((ROOTFSOFFSET + ROOTFSSIZE))" conv=notrunc count="$sect"
54 mkfs.fat --invariant -n kernel -C "$OUTPUT.kernel" -S 512 "$((KERNELSIZE / 1024))"
55 LC_ALL=C dos_dircopy "$KERNELDIR" /
56else
57 make_ext4fs -J -L kernel -l "$KERNELSIZE" ${SOURCE_DATE_EPOCH:+-T ${SOURCE_DATE_EPOCH}} "$OUTPUT.kernel" "$KERNELDIR"
58fi
59dd if="$OUTPUT.kernel" of="$OUTPUT" bs=512 seek="$KERNELOFFSET" conv=notrunc
60rm -f "$OUTPUT.kernel"