xref: /rk3399_rockchip-uboot/scripts/fit-unpack.sh (revision 0fb435fa051047f1ecd4293f97f5ebcb139ebd9a)
1#!/bin/bash
2#
3# Copyright (c) 2020 Fuzhou Rockchip Electronics Co., Ltd
4#
5# SPDX-License-Identifier: GPL-2.0
6#
7set -e
8
9function usage()
10{
11	echo
12	echo "usage:"
13	echo "    $0 -f [fit/itb] -o [output]"
14	echo
15}
16
17function args_process()
18{
19	if [ $# -ne 4 -a $# -ne 2 ]; then
20		usage
21		exit 1
22	fi
23
24	while [ $# -gt 0 ]; do
25		case $1 in
26			-f)
27				file=$2
28				shift 2
29				;;
30			-o)
31				output=$2
32				shift 2
33				;;
34			*)
35				usage
36				exit 1
37				;;
38		esac
39	done
40
41	if [ ! -f $file ]; then
42		echo "ERROR: No $file"
43		exit 1
44	fi
45
46	if [ -z $output ]; then
47		output="out"
48	fi
49
50	mkdir -p $output
51}
52
53function gen_images()
54{
55	printf "\n# Unpack $file to directory $output/\n"
56	fdtget -l $file /images > $output/unpack.txt
57	cat $output/unpack.txt | while read line
58	do
59		# generate image
60		node="/images/${line}"
61		name=`fdtget -ts $file $node image`
62		offs=`fdtget -ti $file $node data-position`
63		size=`fdtget -ti $file $node data-size`
64		if [ -z $offs ]; then
65			continue;
66		fi
67
68		if [ $size -ne 0 ]; then
69			dd if=$file of=$output/dd.tmp  bs=$offs skip=1  >/dev/null 2>&1
70			dd if=$output/dd.tmp of=$output/$name bs=$size count=1 >/dev/null 2>&1
71			rm $output/dd.tmp
72		else
73			touch $output/$name
74		fi
75
76		# hash verify
77		algo=`fdtget -ts $file $node/hash@1 algo`
78		if [ -z $algo ]; then
79			printf "    %-20s: %d bytes" $name $size
80			continue;
81		fi
82
83		data=`fdtget -tx $file $node/hash@1 value`
84		data=`echo " "$data | sed "s/ / 0x/g"`
85		csum=`"$algo"sum $output/$name | awk '{ print $1}'`
86
87		hash=""
88		for((i=1;;i++));
89		do
90			hex=`echo $data | awk -v idx=$i '{ print $idx }'`
91			if [ -z $hex ]; then
92				break;
93			fi
94
95			hex=`printf "%08x" $hex` # align !!
96			hash="$hash$hex"
97		done
98
99		printf "  %-20s: %d bytes... %s" $name $size $algo
100		if [ "$csum" = "$hash" -o $size -eq 0 ]; then
101			echo "+"
102		else
103			echo "-"
104		fi
105	done
106
107	rm $output/unpack.txt
108	echo
109}
110
111args_process $*
112gen_images
113
114