-
Notifications
You must be signed in to change notification settings - Fork 14
/
99-flash-bootloader
50 lines (37 loc) · 2.04 KB
/
99-flash-bootloader
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
#!/bin/sh
#
# Script used by hostapps updater to flash bootloader onto internal media
#
set -o errexit
# machine specific data
idbloader_file="idbloader.bin"
idbloader_block_size=512
idbloader_seek_blocks=64
trust_file="trust.img"
trust_block_size=512
trust_seek_blocks=24576
uboot_file="u-boot.img"
uboot_block_size=512
uboot_seek_blocks=16384
device="/dev/"$(findmnt --noheadings --canonicalize --output SOURCE /mnt/boot/ | xargs lsblk -no pkname)
# we sort the update files list by ascending memory addresses these binaries will be written to
update_files="idbloader uboot trust"
for i in $update_files; do
current_update_file=$(eval echo \$${i}_file)
block_size=$(eval echo \$${i}_block_size)
seek_blocks=$(eval echo \$${i}_seek_blocks)
# calculate size and md5sum of the binary to update from the update bundle
update_size=$(stat -c%s /resin-boot/$current_update_file)
update_md5sum=$(md5sum /resin-boot/$current_update_file | awk '{print $1'})
# calculate number of bytes to skip when computing the checksum of the data we want to update (i.e. the data already written to $device)
skip_bytes=$((block_size * seek_blocks))
# calculate md5sum of the data already written to $device, using $update_size bytes and skipping $skip_bytes from $device
existing_md5sum=$(dd if=$device skip=$skip_bytes bs=1 count=$update_size status=none | md5sum | awk '{print $1}')
if [ ! "$existing_md5sum" = "$update_md5sum" ] || [ -n "$force_write" ]; then
# we are flashing a binary and let's signal that we need to also flash the remaining binaries
# (if we do not write the remaining binaries when the previous one was written then the board may not boot anymore, due to the nature of the secure boot mechanism on the Rockchip hardware)
force_write=true
echo "Flashing $current_update_file to $device"
dd if=/resin-boot/$current_update_file of=$device conv=fdatasync seek=$seek_blocks bs=$block_size
fi
done