Skip to content

Commit

Permalink
hostapp-update-hooks: Write new bootblobs to next active slot only
Browse files Browse the repository at this point in the history
We'll use tegra-boot-tools to mark the future
active slot on the Xavier and write the new signed
bootblobs, which include the dtb that contains the cmdline
with the new rootfs label. A slot designates any
nvidia partition, like for instance kernel-dtb
and kernel-dtb_b.

Signed-off-by: Alexandru Costache <[email protected]>
  • Loading branch information
acostach committed Dec 22, 2021
1 parent 5260f30 commit 4a2c3cb
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,30 @@ info_log()

info_log "New partition is ${new_part}"

get_label_suffix_by_slot()
{
if [ $1 -eq 0 ]; then
echo ''
else
echo '_b'
fi
}

if ! command -v tegra-boot-control &> /dev/null
then
info_log "Could not find tegra-boot-control!"
exit 1
fi

redundancy_state=$(/usr/bin/tegra-boot-control -s | awk -F 'Redundancy:' '{print $2}' | awk '{print $1}' | tr -d '\n')
info_log "Redundancy is currently ${redundancy_state}"

# Enable boot slot redundancy
tegra-boot-control -e
curr_slot=$(/usr/bin/tegra-boot-control -c)
info_log "New active slot is $((new_slot = ! curr_slot))"
new_label_suffix=$(get_label_suffix_by_slot ${new_slot})

rootstr=$(get_dev_label "${new_part}")
rootl=""

Expand Down Expand Up @@ -72,25 +96,22 @@ for n in ${partitions}; do
dst="$file_path"

if [ $(update_needed $src $dst) -eq 1 ]; then
info_log "Will update ${dst}..."
dd if=${src} of=${dst}
dd if=${src} of="${dst}_b"
info_log "Will update ${dst}${new_label_suffix} ..."
dd if=${src} of="${dst}${new_label_suffix}"
else
info_log "No need to update ${dst}"
info_log "No need to update ${dst}${new_label_suffix}"
fi
done

# root is contained in the dtb cmdline, needs to be updated to switch rootfs
info_log "Writing ${dtbfile} to specific partitions..."
dd if=/resin-boot/bootfiles/${dtbfile} of=$(get_state_path_from_label "bootloader-dtb")
dd if=/resin-boot/bootfiles/${dtbfile} of=$(get_state_path_from_label "bootloader-dtb_b")
dd if=/resin-boot/bootfiles/${dtbfile} of=$(get_state_path_from_label "kernel-dtb")
dd if=/resin-boot/bootfiles/${dtbfile} of=$(get_state_path_from_label "kernel-dtb_b")
# root is contained in the dtb cmdline, needs to be updated to switch rootfs, but only in the next active slot
info_log "Writing ${dtbfile} to specific partitions - suffix is ${new_label_suffix}."

dd if=/resin-boot/bootfiles/${dtbfile} of=$(get_state_path_from_label "bootloader-dtb${new_label_suffix}")
dd if=/resin-boot/bootfiles/${dtbfile} of=$(get_state_path_from_label "kernel-dtb${new_label_suffix}")

info_log "Writing kernel ${kernel} to specific partitions..."

dd if=/resin-boot/bootfiles/${kernel} of=$(get_state_path_from_label "kernel")
dd if=/resin-boot/bootfiles/${kernel} of=$(get_state_path_from_label "kernel_b")
dd if=/resin-boot/bootfiles/${kernel} of=$(get_state_path_from_label "kernel${new_label_suffix}")

info_log "Updating boot image on hw partition mmcblk0boot0..."

Expand All @@ -99,4 +120,10 @@ dd if=/resin-boot/bootfiles/boot0.img of=/dev/mmcblk0boot0
sync
echo 1 > /sys/block/mmcblk0boot0/force_ro

# Update slot selection after the boot memory was updated, otherwise
# scratch register contents will be lost.
info_log "Setting active slot to ${new_slot}"
/usr/bin/tegra-boot-control -e
/usr/bin/tegra-boot-control -a ${new_slot}

info_log "Done."
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,30 @@ info_log()

info_log "New partition is ${new_part}"

get_label_suffix_by_slot()
{
if [ $1 -eq 0 ]; then
echo ''
else
echo '_b'
fi
}

if ! command -v tegra-boot-control &> /dev/null
then
info_log "Could not find tegra-boot-control!"
exit 1
fi

redundancy_state=$(/usr/bin/tegra-boot-control -s | awk -F 'Redundancy:' '{print $2}' | awk '{print $1}' | tr -d '\n')
info_log "Redundancy is currently ${redundancy_state}"

# Enable boot slot redundancy
tegra-boot-control -e
curr_slot=$(/usr/bin/tegra-boot-control -c)
info_log "New active slot is $((new_slot = ! curr_slot))"
new_label_suffix=$(get_label_suffix_by_slot ${new_slot})

rootstr=$(get_dev_label "${new_part}")
rootl=""

Expand Down Expand Up @@ -74,22 +98,19 @@ for n in ${partitions}; do
dst="$file_path"

if [ $(update_needed $src $dst) -eq 1 ]; then
info_log "Will update ${dst} ..."
dd if=${src} of=${dst}
dd if=${src} of="${dst}_b"
info_log "Will update ${dst}${new_label_suffix} ..."
dd if=${src} of="${dst}${new_label_suffix}"
else
info_log "No need to update ${dst}"
info_log "No need to update ${dst}${new_label_suffix}"
fi
done

# DTB contains root partition, update is mandatory
# DTB contains root partition, update is mandatory on the new boot slot, before switching it to active
info_log "Writing ${dtbfile} to specific partitions..."
dd if=/opt/tegra-binaries/${dtbfile} of=$(get_state_path_from_label "kernel-dtb")
dd if=/opt/tegra-binaries/${dtbfile} of=$(get_state_path_from_label "kernel-dtb_b")
dd if=/opt/tegra-binaries/${dtbfile} of=$(get_state_path_from_label "kernel-dtb${new_label_suffix}")

info_log "Writing kernel ${kernel} to specific partitions..."
dd if=/opt/tegra-binaries/${kernel} of=$(get_state_path_from_label "kernel")
dd if=/opt/tegra-binaries/${kernel} of=$(get_state_path_from_label "kernel_b")
dd if=/opt/tegra-binaries/${kernel} of=$(get_state_path_from_label "kernel${new_label_suffix}")

existing_bootloader_md5sum=$(dd if=$bootloader_device bs=1M status=none | md5sum | awk '{print $1}')
update_bootloader_md5sum=$(md5sum $bootloader_blob | awk '{print $1}')
Expand All @@ -105,4 +126,10 @@ fi
# Sync internal memory
sync /dev/mmcblk0

# Update slot selection after the qspi was updated, otherwise
# scratch register contents will be lost
info_log "Setting active slot to ${new_slot}"
/usr/bin/tegra-boot-control -e
/usr/bin/tegra-boot-control -a ${new_slot}

info_log "Done."

0 comments on commit 4a2c3cb

Please sign in to comment.