-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadd_xhdr.sh
executable file
·110 lines (92 loc) · 2.36 KB
/
add_xhdr.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/bin/bash
source ../../build/tools/secure_hsm/secure/sb_info.sh_inc
# $1: val
# $2: 1=no reverse
# $3: 1=have secure boot signature
# $4: header name
function gen_bin_tmp
{
#echo $1
if [ "$2" = "1" ];then
printf "0: %.8x" $1 | xxd -r -g0 >tmp
else
printf "0: %.8x" $1 | sed -E 's/0: (..)(..)(..)(..)/0: \4\3\2\1/' | xxd -r -g0 >tmp
fi
#hexdump -C tmp
}
if [ $# -lt 3 ];then
echo "Error: $0 missed arguments"
exit 1
fi
input=$1
output=$2
img_flag=$3
header_name=$4
rel_id=$5
sz=`stat -c%s $1`
finalsz=$((64 + sz))
#chksum=`md5sum $input |cut -c 1-8`
#chksum=`./tools/tcpsum $input |cut -c 1-4`
curdir=`dirname $0`
chksum=`${curdir}/tools/tcpsum $input |cut -c 1-4`
chksum="0000$chksum"
echo "chksum=$chksum"
# header size is 32 (0x20)
dd if=/dev/zero of=$output bs=1 count=32 2>/dev/null
##############
# 4-byte magic
if [ "$4" = "im1d" ];then
val=$((0x64316d69)) # im1d (i=69h)
elif [ "$4" = "dm1d" ];then
val=$((0x64316d64)) # dm1d (d=64h)
elif [ "$4" = "im2d" ];then
val=$((0x64326d69)) # im2d (i=69h)
elif [ "$4" = "dm2d" ];then
val=$((0x64326d64)) # dm2d (d=64h)
elif [ "$4" = "imda" ];then
val=$((0x6a646d69)) # imda (i=69h)
elif [ "$4" = "dmda" ];then
val=$((0x6a646d64)) # dmda (d=64h)
else
val=$((0x54554258)) # XBUT (X=58h)
fi
gen_bin_tmp $val
dd if=tmp of=$output conv=notrunc bs=1 count=4 seek=0 2>/dev/null
##############
# 4-byte version=0
##############
# 4-byte length of bin (exhclude header)
val=$sz
gen_bin_tmp $val
dd if=tmp of=$output conv=notrunc bs=1 count=4 seek=8 2>/dev/null
##############
# 4-byte checksum
val=$((0x$chksum))
gen_bin_tmp $val 0
dd if=tmp of=$output conv=notrunc bs=1 count=4 seek=12 2>/dev/null
##############
# 4-byte img_flag
val=$img_flag
gen_bin_tmp $val 0
dd if=tmp of=$output conv=notrunc bs=1 count=4 seek=16 2>/dev/null
##############
# 12-byte reserved => modification
##############
# 4-byte build_epoch
val=$((`date +%s`))
gen_bin_tmp $val 0
dd if=tmp of=$output conv=notrunc bs=1 count=4 seek=20 2>/dev/null
##############
# 4-byte offs_sb
val=$((sz - SB_INFO_SIZE)) #xboot.bin offset to sb_info
gen_bin_tmp $val 0
dd if=tmp of=$output conv=notrunc bs=1 count=4 seek=24 2>/dev/null
##############
# 4-byte project release ID
val=$((rel_id))
gen_bin_tmp $val 0
dd if=tmp of=$output conv=notrunc bs=1 count=4 seek=28 2>/dev/null
##############
# bin content
dd if=$input of=$2 conv=notrunc bs=1 seek=32 2>/dev/null
rm tmp