xref: /rk3399_rockchip-uboot/scripts/fit-unpack.sh (revision 5c8c82be5c8b87d33f2dd62feebfa5c3690c5dcb)
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		node="/images/${line}"
60		name=`fdtget -ts $file $node image`
61		offs=`fdtget -ti $file $node data-position`
62		size=`fdtget -ti $file $node data-size`
63		if [ -z $offs ]; then
64			continue;
65		fi
66
67		printf "    %-15s: %d bytes\n" ${name} $size
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	done
76
77	rm $output/unpack.txt
78	echo
79}
80
81args_process $*
82gen_images