1
0

combined-ext-image.sh 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #!/bin/sh
  2. #
  3. # Copyright (C) 2011 OpenWrt.org
  4. #
  5. # This is free software, licensed under the GNU General Public License v2.
  6. # See /LICENSE for more information.
  7. #
  8. # Write image header followed by all specified files
  9. # The header is padded to 64k, format is:
  10. # CE magic word ("Combined Extended Image") (2 bytes)
  11. # <CE_VERSION> file format version field (2 bytes)
  12. # <TYPE> short description of the target device (32 bytes)
  13. # <NUM FILES> number of files following the header (2 byte)
  14. # <file1_name> name of the first file (32 bytes)
  15. # <file1_length> length of the first file encoded as zero padded 8 digit hex (8 bytes)
  16. # <file1_md5> md5 checksum of the first file (32 bytes)
  17. # <fileN_name> name of the Nth file (32 bytes)
  18. # <fileN_length> length of the Nth file encoded as zero padded 8 digit hex (8 bytes)
  19. # <fileN_md5> md5 checksum of the Nth file (32 bytes)
  20. ## version history
  21. # * version 1: initial file format with num files / name / length / md5 checksum
  22. set -e
  23. ME="${0##*/}"
  24. usage() {
  25. echo "Usage: $ME <type> <ext filename> <file1> <filename1> [<file2> <filename2> <fileN> <filenameN>]"
  26. [ "$IMG_OUT" ] && rm -f "$IMG_OUT"
  27. exit 1
  28. }
  29. [ "$#" -lt 4 ] && usage
  30. CE_VERSION=1
  31. IMG_TYPE=$1; shift
  32. IMG_OUT=$1; shift
  33. FILE_NUM=$(($# / 2))
  34. FILES=""
  35. tmpdir="$( mktemp -d 2> /dev/null )"
  36. if [ -z "$tmpdir" ]; then
  37. # try OSX signature
  38. tmpdir="$( mktemp -t 'ubitmp' -d )"
  39. fi
  40. if [ -z "$tmpdir" ]; then
  41. exit 1
  42. fi
  43. trap "rm -rf $tmpdir" EXIT
  44. IMG_TMP_OUT="${tmpdir}/out"
  45. printf "CE%02x%-32s%02x" $CE_VERSION "$IMG_TYPE" $FILE_NUM > "${IMG_TMP_OUT}"
  46. while [ "$#" -gt 1 ]
  47. do
  48. file=$1
  49. filename=$2
  50. [ ! -f "$file" ] && echo "$ME: Not a valid file: $file" && usage
  51. FILES="$FILES $file"
  52. md5=$($MKHASH md5 "$file")
  53. printf "%-32s%08x%32s" "$filename" $(stat -c "%s" "$file") "${md5%% *}" >> "${IMG_TMP_OUT}"
  54. shift 2
  55. done
  56. [ "$#" -eq 1 ] && echo "$ME: Filename not specified: $1" && usage
  57. mv "${IMG_TMP_OUT}" "${IMG_TMP_OUT}".tmp
  58. dd if="${IMG_TMP_OUT}.tmp" of="${IMG_TMP_OUT}" bs=65536 conv=sync 2>/dev/null
  59. rm "${IMG_TMP_OUT}".tmp
  60. cat $FILES >> "${IMG_TMP_OUT}"
  61. cp "${IMG_TMP_OUT}" "${IMG_OUT}"