xref: /rk3399_rockchip-uboot/scripts/fit-resign.sh (revision e1aab64013804e120d3fc5d6940ed7245d8f9c2a)
1#!/bin/bash
2#
3# Copyright (c) 2020 Fuzhou Rockchip Electronics Co., Ltd
4# SPDX-License-Identifier: GPL-2.0
5#
6set -e
7
8FIT_IMG=$1
9FIT_SIG=$2
10
11if [ $# -ne 2 ]; then
12	echo "Usage: $0 [signed fit image] [new signature]"
13	exit
14elif [ ! -f $FIT_IMG ]; then
15	echo "ERROR: No $FIT_IMG"
16	exit
17elif [ ! -f $FIT_SIG ]; then
18	echo "ERROR: No $FIT_SIG"
19	exit
20fi
21
22SIG_SZ=`ls -l ${FIT_SIG} | awk '{ print $5 }'`
23LEN=`./tools/fit_info -f $FIT_IMG -n /configurations/conf@1/signature@1 -p value | sed -n "/LEN:/p" | awk '{ print $2 }'`
24OFF=`./tools/fit_info -f $FIT_IMG -n /configurations/conf@1/signature@1 -p value | sed -n "/OFF:/p" | awk '{ print $2 }'`
25END=`./tools/fit_info -f $FIT_IMG -n /configurations/conf@1/signature@1 -p value | sed -n "/END:/p" | awk '{ print $2 }'`
26
27if [ -z $LEN ]; then
28	echo "ERROR: No valid signature in $FIT_IMG"
29	exit
30elif [ "$SIG_SZ" -ne "$LEN" ]; then
31	echo "ERROR: $FIT_SIG size $SIG_SZ != $FIT_IMG Signature size $LEN"
32	exit
33fi
34
35dd if=$FIT_IMG of=$FIT_IMG.part1 count=1 bs=$OFF
36dd if=$FIT_IMG of=$FIT_IMG.part2 skip=1 ibs=$END
37
38cat $FIT_IMG.part1  >  $FIT_IMG.resig
39cat $FIT_SIG        >> $FIT_IMG.resig
40cat $FIT_IMG.part2  >> $FIT_IMG.resig
41
42rm $FIT_IMG.part1 && $FIT_IMG.part2
43
44echo
45echo "Re-signed fit image is OK: $FIT_IMG.resig"
46echo
47
48