b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | # Copyright (C) 2006-2012 OpenWrt.org |
| 3 | set -e -x |
| 4 | if [ $# -ne 5 ] && [ $# -ne 6 ]; then |
| 5 | echo "SYNTAX: $0 <file> <kernel size> <kernel directory> <rootfs size> <rootfs image> [<align>]" |
| 6 | exit 1 |
| 7 | fi |
| 8 | |
| 9 | OUTPUT="$1" |
| 10 | KERNELSIZE="$2" |
| 11 | KERNELDIR="$3" |
| 12 | KERNELPARTTYPE=${KERNELPARTTYPE:-83} |
| 13 | ROOTFSSIZE="$4" |
| 14 | ROOTFSIMAGE="$5" |
| 15 | ROOTFSPARTTYPE=${ROOTFSPARTTYPE:-83} |
| 16 | ALIGN="$6" |
| 17 | |
| 18 | rm -f "$OUTPUT" |
| 19 | |
| 20 | head=16 |
| 21 | sect=63 |
| 22 | |
| 23 | # create partition table |
| 24 | set $(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 | |
| 26 | KERNELOFFSET="$(($1 / 512))" |
| 27 | KERNELSIZE="$2" |
| 28 | ROOTFSOFFSET="$(($3 / 512))" |
| 29 | ROOTFSSIZE="$(($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. |
| 35 | dos_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" |
| 50 | dd if="$ROOTFSIMAGE" of="$OUTPUT" bs=512 seek="$ROOTFSOFFSET" conv=notrunc |
| 51 | |
| 52 | if [ -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" / |
| 56 | else |
| 57 | make_ext4fs -J -L kernel -l "$KERNELSIZE" ${SOURCE_DATE_EPOCH:+-T ${SOURCE_DATE_EPOCH}} "$OUTPUT.kernel" "$KERNELDIR" |
| 58 | fi |
| 59 | dd if="$OUTPUT.kernel" of="$OUTPUT" bs=512 seek="$KERNELOFFSET" conv=notrunc |
| 60 | rm -f "$OUTPUT.kernel" |