xref: /rk3399_rockchip-uboot/scripts/fit-unpack.sh (revision f8f8bbc62fc911ca724dd0a6184be5982d300b0e)
1#!/bin/bash
2#
3# Copyright (c) 2020 Fuzhou Rockchip Electronics Co., Ltd
4#
5# SPDX-License-Identifier: GPL-2.0
6#
7
8FIT_UBOOT_IMAGES=(
9	"/images/uboot@1      u-boot-nodtb.bin"
10	"/images/optee@1      tee.bin"
11	"/images/fdt@1        u-boot.dtb"
12)
13
14FIT_BOOT_IMAGES=(
15	"/images/kernel@1     kernel.img"
16	"/images/ramdisk@1    ramdisk.img"
17	"/images/resource@1   resource.img"
18	"/images/fdt@1        rk-kernel.dtb"
19)
20
21function usage()
22{
23	echo
24	echo "usage:"
25	echo "    $0 -f [fit/itb image] -o [output] -u    // unpack uboot.fit/itb"
26	echo "    $0 -f [fit/itb image] -o [output] -b    // unpack boot.fit/itb"
27	echo
28}
29
30function args_process()
31{
32	if [ $# -ne 5 ]; then
33		usage
34		exit 1
35	fi
36
37	while [ $# -gt 0 ]; do
38		case $1 in
39			-b|-u)
40				TYPE=$1
41				shift 1
42				;;
43			-f)
44				IMAGE=$2
45				shift 2
46				;;
47			-o)
48				OUTPUT=$2
49				shift 2
50				;;
51			*)
52				usage
53				exit 1
54				;;
55		esac
56	done
57
58	if [ ! -f $IMAGE ]; then
59		echo "ERROR: No $IMAGE"
60		exit 1
61	elif [ -z $OUTPUT ]; then
62		echo "ERROR: No output"
63		exit 1
64	elif [ -z $TYPE ]; then
65		echo "ERROR: No args -u or -b"
66		exit 1
67	fi
68
69	mkdir -p $OUTPUT
70}
71
72function copy_image()
73{
74	LIST=$1
75
76	NODE=`echo $LIST | awk '{ print $1 }'`
77	NAME=`echo $LIST | awk '{ print $2 }'`
78	OFFS=`fdtget -ti $IMAGE $NODE data-position`
79	SIZE=`fdtget -ti $IMAGE $NODE data-size`
80	if [ -z $OFFS ]; then
81		echo "ERROR: No find $NODE"
82		exit 1
83	fi
84
85	printf "    %-15s: %d bytes\n" $OUTPUT$NAME $SIZE
86	if [ $SIZE -ne 0 ]; then
87		dd if=$IMAGE         of=$OUTPUT/dd.tmp  bs=$OFFS skip=1  >/dev/null 2>&1
88		dd if=$OUTPUT/dd.tmp of=$OUTPUT/$NAME   bs=$SIZE count=1 >/dev/null 2>&1
89		rm $OUTPUT/dd.tmp
90	else
91		touch $OUTPUT/$NAME
92	fi
93}
94
95function gen_images()
96{
97	echo "Image:"
98	if [ $TYPE = "-u" ]; then
99		for LIST in "${FIT_UBOOT_IMAGES[@]}"
100		do
101			copy_image "$LIST"
102		done
103	elif [ $TYPE = "-k" ]; then
104		for LIST in "${FIT_BOOT_IMAGES[@]}"
105		do
106			copy_image "$LIST"
107		done
108	fi
109	echo
110}
111
112args_process $*
113gen_images