bcm4908.sh 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. # SPDX-License-Identifier: GPL-2.0-or-later OR BSD-2-Clause
  2. FS_STATE_READY=2
  3. # $(1): file to read from
  4. # $(2): offset in bytes
  5. get_hex_u32_le() {
  6. dd if="$1" skip=$2 bs=1 count=4 2>/dev/null | hexdump -v -e '1/4 "%02x"'
  7. }
  8. # Setup /tmp/env.config to provide "metadata" UBI volume access
  9. #
  10. # It can be used with "fw_printenv -c /tmp/env.config"
  11. bcm4908_pkgtb_setup_env_config() {
  12. local size=$((0x$(get_hex_u32_le /dev/ubi0_1 4)))
  13. dd if=/dev/ubi0_1 of=/tmp/env.head count=8 iflag=count_bytes
  14. dd if=/dev/ubi0_1 of=/tmp/env.body skip=8 iflag=skip_bytes
  15. printf "%s\t0x%x\t0x%x\t0x%x" "/tmp/env.body" 0x0 $size $size > /tmp/env.config
  16. }
  17. bcm4908_committed_image_seq() {
  18. bcm4908_pkgtb_setup_env_config
  19. commited="$(fw_printenv -n -c /tmp/env.config COMMITTED)"
  20. [ -n "$commited" ] && {
  21. seq=$(fw_printenv -n -c /tmp/env.config SEQ | cut -d ',' -f $commited)
  22. [ -n "$seq" ] && {
  23. echo $seq
  24. return
  25. }
  26. }
  27. echo "Failed to read COMMITED and SEQ from metadata1" >&2
  28. }
  29. # Make sure "rootfs_data" UBI volume matches currently flashed image
  30. #
  31. # On mismatch "rootfs_data" will be wiped and assigned
  32. #
  33. # $1: UBI volume of "rootfs_data" (e.g. ubi0_123)
  34. bcm4908_verify_rootfs_data() {
  35. local ubivol="$1"
  36. local dir=/tmp/rootfs_data
  37. local seq="$(bcm4908_committed_image_seq)"
  38. [ -z "$seq" ] && return
  39. mkdir $dir
  40. if ! mount -t ubifs /dev/$ubivol $dir; then
  41. echo "Failed to mount $ubivol UBI volume" >&2
  42. rmdir $dir
  43. return
  44. fi
  45. # Wipe rootfs_data if it doesn't belong to us
  46. [ "$(readlink $dir/.openwrt-image-seq)" != "$seq" ] && {
  47. echo "Removing \"rootfs_data\" content"
  48. rm -rf $dir/..?* $dir/.[!.]* $dir/*
  49. }
  50. # If rootfs_data is clean (or was just wiped) claim it
  51. [ -z "$(ls -A $dir)" ] && {
  52. echo "Assigning \"rootfs_data\" to the current firmware"
  53. # Claim this "rootfs_data"
  54. ln -s $seq $dir/.openwrt-image-seq
  55. # Mark it ready to avoid "mount_root" wiping it again
  56. ln -s $FS_STATE_READY $dir/.fs_state
  57. }
  58. umount $dir
  59. rmdir $dir
  60. }