diff options
author | Christian Lamparter <chunkeey@gmail.com> | 2017-11-19 17:19:21 +0100 |
---|---|---|
committer | Felix Fietkau <nbd@nbd.name> | 2017-12-08 19:54:16 +0100 |
commit | 4e3f6dae04fb526a78e613db6c65aee584403d36 (patch) | |
tree | fbbfcb2fdeb8a7cee4957c0049173b4612961a9d /package/base-files | |
parent | 96b485ec7a292988cb8b44a2831b3a32d68c738f (diff) | |
download | mtk-20170518-4e3f6dae04fb526a78e613db6c65aee584403d36.zip mtk-20170518-4e3f6dae04fb526a78e613db6c65aee584403d36.tar.gz mtk-20170518-4e3f6dae04fb526a78e613db6c65aee584403d36.tar.bz2 |
base-files: upgrade: make get_partitions() endian agnostic
This patch fixes two issues with the current get_partitions()
function.
First: "Invalid partition table on $disk" will pop up on
legitimate images on big endian system.
This is because the little-endian representation of "55 AA" is
assumed in the context of little-endian architectures. On these
comparing it to the 16-bit word 0xAA55 does work as intented.
Whereas on big-endian systems, this would have to be 0x55AA.
This patch fixes the issue by replacing the integer conversion
and value match check with just a string comparision.
Second: The extraction of the type, start LBA and LBA num from
the partition table has the same endianness issue. This has been
fixed by using the new hex_le32_to_cpu() function. This function
will translate the stored little-endian data to the correct
byte-order if necessary.
Signed-off-by: Christian Lamparter <chunkeey@gmail.com>
Diffstat (limited to 'package/base-files')
-rw-r--r-- | package/base-files/files/lib/upgrade/common.sh | 18 |
1 files changed, 13 insertions, 5 deletions
diff --git a/package/base-files/files/lib/upgrade/common.sh b/package/base-files/files/lib/upgrade/common.sh index 428ec73..71cffc8 100644 --- a/package/base-files/files/lib/upgrade/common.sh +++ b/package/base-files/files/lib/upgrade/common.sh @@ -160,6 +160,14 @@ export_partdevice() { return 1 } +hex_le32_to_cpu() { + [ "$(echo 01 | hexdump -v -n 2 -e '/2 "%x"')" == "3031" ] && { + echo "${1:0:2}${1:8:2}${1:6:2}${1:4:2}${1:2:2}" + return + } + echo "$@" +} + get_partitions() { # <device> <filename> local disk="$1" local filename="$2" @@ -167,8 +175,8 @@ get_partitions() { # <device> <filename> if [ -b "$disk" -o -f "$disk" ]; then v "Reading partition table from $filename..." - local magic="$(hexdump -v -n 2 -s 0x1FE -e '1/2 "0x%04X"' "$disk")" - if [ "$magic" != 0xAA55 ]; then + local magic=$(dd if="$disk" bs=2 count=1 skip=255 2>/dev/null) + if [ "$magic" != $'\x55\xAA' ]; then v "Invalid partition table on $disk" exit fi @@ -179,9 +187,9 @@ get_partitions() { # <device> <filename> for part in 1 2 3 4; do set -- $(hexdump -v -n 12 -s "$((0x1B2 + $part * 16))" -e '3/4 "0x%08X "' "$disk") - local type="$(($1 % 256))" - local lba="$(($2))" - local num="$(($3))" + local type="$(( $(hex_le32_to_cpu $1) % 256))" + local lba="$(( $(hex_le32_to_cpu $2) ))" + local num="$(( $(hex_le32_to_cpu $3) ))" [ $type -gt 0 ] || continue |