Skip to content

Instantly share code, notes, and snippets.

@trapier
Last active April 15, 2016 19:51
Show Gist options
  • Save trapier/0ee553929a3f3f3edfb0 to your computer and use it in GitHub Desktop.
Save trapier/0ee553929a3f3f3edfb0 to your computer and use it in GitHub Desktop.
extract xzipped initramfs from uboot new FIT image format (itb)
#!/bin/bash
XZMAGIC=fd377a58
STRUCT_POINTER_OFFSET=8
file=$1
declare -A word
map_word () {
word[addr]=$1
word[value]=$2
}
map_hex_word_at_cursor() {
map_word $(hexdump -s$cursor -n4 -e '/4 "%_ad"' -e '/4 " %x"' $file)
}
map_uint_word_at_cursor() {
map_word $(hexdump -s$cursor -n4 -e '/4 "%_ad"' -e '/4 " %u"' $file)
}
cursor_forward() {
cursor=$(($cursor + $1))
}
# cursor = 0
cursor=0
# set cursor to location of struct pointer offset
cursor_forward $STRUCT_POINTER_OFFSET
# read structure offset (4 bytes), and move cursor to structure
cursor=$(hexdump -s$cursor -n4 -e '/4 "%u"' $file)
map_uint_word_at_cursor # read value at cursor
# while true
while true; do
# while 4-byte uint at cursor not 3
# cursor forward four bytes
# read 4-byte uint at cursor
while [[ ${word[value]} -ne 3 ]]; do
cursor_forward 4
map_uint_word_at_cursor
done
cursor_forward 4 # cursor forward 4 bytes
# read uint size (4 bytes)
map_uint_word_at_cursor
size=${word[value]}
cursor_forward 8 # cursor forward 8 bytes
# if 4-bytes at cursor match XZ magic
# dd from cursor to (cursor + size)
# exit
# else
# cursor forward size bytes
map_hex_word_at_cursor
if [[ "${word[value]}" = "$XZMAGIC" ]]; then
dd if=$file of=initramfs bs=1 skip=${word[addr]} count=$size &>/dev/null
exit
else
cursor_forward $(($size + 4 - $size % 4))
map_uint_word_at_cursor
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment