gen_image_generic.sh 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. OUTPUT="$1"
  9. KERNELSIZE="$2"
  10. KERNELDIR="$3"
  11. KERNELPARTTYPE=${KERNELPARTTYPE:-83}
  12. ROOTFSSIZE="$4"
  13. ROOTFSIMAGE="$5"
  14. ROOTFSPARTTYPE=${ROOTFSPARTTYPE:-83}
  15. ALIGN="$6"
  16. rm -f "$OUTPUT"
  17. head=16
  18. sect=63
  19. # create partition table
  20. 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})
  21. KERNELOFFSET="$(($1 / 512))"
  22. KERNELSIZE="$2"
  23. ROOTFSOFFSET="$(($3 / 512))"
  24. ROOTFSSIZE="$(($4 / 512))"
  25. # Using mcopy -s ... is using READDIR(3) to iterate through the directory
  26. # entries, hence they end up in the FAT filesystem in traversal order which
  27. # breaks reproducibility.
  28. # Implement recursive copy with reproducible order.
  29. dos_dircopy() {
  30. local entry
  31. local baseentry
  32. for entry in "$1"/* ; do
  33. if [ -f "$entry" ]; then
  34. mcopy -i "$OUTPUT.kernel" "$entry" ::"$2"
  35. elif [ -d "$entry" ]; then
  36. baseentry="$(basename "$entry")"
  37. mmd -i "$OUTPUT.kernel" ::"$2""$baseentry"
  38. dos_dircopy "$entry" "$2""$baseentry"/
  39. fi
  40. done
  41. }
  42. [ -n "$PADDING" ] && dd if=/dev/zero of="$OUTPUT" bs=512 seek="$ROOTFSOFFSET" conv=notrunc count="$ROOTFSSIZE"
  43. dd if="$ROOTFSIMAGE" of="$OUTPUT" bs=512 seek="$ROOTFSOFFSET" conv=notrunc
  44. if [ -n "$GUID" ]; then
  45. [ -n "$PADDING" ] && dd if=/dev/zero of="$OUTPUT" bs=512 seek="$((ROOTFSOFFSET + ROOTFSSIZE))" conv=notrunc count="$sect"
  46. mkfs.fat --invariant -n kernel -C "$OUTPUT.kernel" -S 512 "$((KERNELSIZE / 1024))"
  47. LC_ALL=C dos_dircopy "$KERNELDIR" /
  48. else
  49. make_ext4fs -J -L kernel -l "$KERNELSIZE" ${SOURCE_DATE_EPOCH:+-T ${SOURCE_DATE_EPOCH}} "$OUTPUT.kernel" "$KERNELDIR"
  50. fi
  51. dd if="$OUTPUT.kernel" of="$OUTPUT" bs=512 seek="$KERNELOFFSET" conv=notrunc
  52. rm -f "$OUTPUT.kernel"