b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame] | 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 | |
| 9 | # Write image header followed by all specified files |
| 10 | # The header is padded to 64k, format is: |
| 11 | # CE magic word ("Combined Extended Image") (2 bytes) |
| 12 | # <CE_VERSION> file format version field (2 bytes) |
| 13 | # <TYPE> short description of the target device (32 bytes) |
| 14 | # <NUM FILES> number of files following the header (2 byte) |
| 15 | # <file1_name> name of the first file (32 bytes) |
| 16 | # <file1_length> length of the first file encoded as zero padded 8 digit hex (8 bytes) |
| 17 | # <file1_md5> md5 checksum of the first file (32 bytes) |
| 18 | # <fileN_name> name of the Nth file (32 bytes) |
| 19 | # <fileN_length> length of the Nth file encoded as zero padded 8 digit hex (8 bytes) |
| 20 | # <fileN_md5> md5 checksum of the Nth file (32 bytes) |
| 21 | |
| 22 | ## version history |
| 23 | # * version 1: initial file format with num files / name / length / md5 checksum |
| 24 | |
| 25 | set -e |
| 26 | |
| 27 | ME="${0##*/}" |
| 28 | |
| 29 | usage() { |
| 30 | echo "Usage: $ME <type> <ext filename> <file1> <filename1> [<file2> <filename2> <fileN> <filenameN>]" |
| 31 | [ "$IMG_OUT" ] && rm -f "$IMG_OUT" |
| 32 | exit 1 |
| 33 | } |
| 34 | |
| 35 | [ "$#" -lt 4 ] && usage |
| 36 | |
| 37 | CE_VERSION=1 |
| 38 | IMG_TYPE=$1; shift |
| 39 | IMG_OUT=$1; shift |
| 40 | FILE_NUM=$(($# / 2)) |
| 41 | FILES="" |
| 42 | |
| 43 | tmpdir="$( mktemp -d 2> /dev/null )" |
| 44 | if [ -z "$tmpdir" ]; then |
| 45 | # try OSX signature |
| 46 | tmpdir="$( mktemp -t 'ubitmp' -d )" |
| 47 | fi |
| 48 | |
| 49 | if [ -z "$tmpdir" ]; then |
| 50 | exit 1 |
| 51 | fi |
| 52 | |
| 53 | trap "rm -rf $tmpdir" EXIT |
| 54 | |
| 55 | IMG_TMP_OUT="${tmpdir}/out" |
| 56 | |
| 57 | printf "CE%02x%-32s%02x" $CE_VERSION "$IMG_TYPE" $FILE_NUM > "${IMG_TMP_OUT}" |
| 58 | |
| 59 | while [ "$#" -gt 1 ] |
| 60 | do |
| 61 | file=$1 |
| 62 | filename=$2 |
| 63 | |
| 64 | [ ! -f "$file" ] && echo "$ME: Not a valid file: $file" && usage |
| 65 | FILES="$FILES $file" |
| 66 | md5=$($MKHASH md5 "$file") |
| 67 | printf "%-32s%08x%32s" "$filename" $(stat -c "%s" "$file") "${md5%% *}" >> "${IMG_TMP_OUT}" |
| 68 | shift 2 |
| 69 | done |
| 70 | |
| 71 | [ "$#" -eq 1 ] && echo "$ME: Filename not specified: $1" && usage |
| 72 | |
| 73 | mv "${IMG_TMP_OUT}" "${IMG_TMP_OUT}".tmp |
| 74 | dd if="${IMG_TMP_OUT}.tmp" of="${IMG_TMP_OUT}" bs=65536 conv=sync 2>/dev/null |
| 75 | rm "${IMG_TMP_OUT}".tmp |
| 76 | |
| 77 | cat $FILES >> "${IMG_TMP_OUT}" |
| 78 | cp "${IMG_TMP_OUT}" "${IMG_OUT}" |