openmesh.sh 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. # The U-Boot loader of the OpenMesh devices requires image sizes and
  2. # checksums to be provided in the U-Boot environment.
  3. # The OpenMesh devices come with 2 main partitions - while one is active
  4. # sysupgrade will flash the other. The boot order is changed to boot the
  5. # newly flashed partition. If the new partition can't be booted due to
  6. # upgrade failures the previously used partition is loaded.
  7. platform_do_upgrade_openmesh() {
  8. local tar_file="$1"
  9. local restore_backup
  10. local primary_kernel_mtd
  11. local setenv_script="/tmp/fw_env_upgrade"
  12. local kernel_mtd="$(find_mtd_index $PART_NAME)"
  13. local kernel_offset="$(cat /sys/class/mtd/mtd${kernel_mtd}/offset)"
  14. local total_size="$(cat /sys/class/mtd/mtd${kernel_mtd}/size)"
  15. # detect to which flash region the new image is written to.
  16. #
  17. # 1. check what is the mtd index for the first flash region on this
  18. # device
  19. # 2. check if the target partition ("inactive") has the mtd index of
  20. # the first flash region
  21. #
  22. # - when it is: the new bootseq will be 1,2 and the first region is
  23. # modified
  24. # - when it isnt: bootseq will be 2,1 and the second region is
  25. # modified
  26. #
  27. # The detection has to be done via the hardcoded mtd partition because
  28. # the current boot might be done with the fallback region. Let us
  29. # assume that the current bootseq is 1,2. The bootloader detected that
  30. # the image in flash region 1 is corrupt and thus switches to flash
  31. # region 2. The bootseq in the u-boot-env is now still the same and
  32. # the sysupgrade code can now only rely on the actual mtd indexes and
  33. # not the bootseq variable to detect the currently booted flash
  34. # region/image.
  35. #
  36. # In the above example, an implementation which uses bootseq ("1,2") to
  37. # detect the currently booted image would assume that region 1 is booted
  38. # and then overwrite the variables for the wrong flash region (aka the
  39. # one which isn't modified). This could result in a device which doesn't
  40. # boot anymore to Linux until it was reflashed with ap51-flash.
  41. local next_boot_part="1"
  42. case "$(board_name)" in
  43. openmesh,a42)
  44. primary_kernel_mtd=8
  45. ;;
  46. *)
  47. echo "failed to detect primary kernel mtd partition for board"
  48. return 1
  49. ;;
  50. esac
  51. [ "$kernel_mtd" = "$primary_kernel_mtd" ] || next_boot_part="2"
  52. local board_dir=$(tar tf $tar_file | grep -m 1 '^sysupgrade-.*/$')
  53. board_dir=${board_dir%/}
  54. local kernel_length=$(tar xf $tar_file ${board_dir}/kernel -O | wc -c)
  55. local rootfs_length=$(tar xf $tar_file ${board_dir}/root -O | wc -c)
  56. # rootfs without EOF marker
  57. rootfs_length=$((rootfs_length-4))
  58. local kernel_md5=$(tar xf $tar_file ${board_dir}/kernel -O | md5sum); kernel_md5="${kernel_md5%% *}"
  59. # md5 checksum of rootfs with EOF marker
  60. local rootfs_md5=$(tar xf $tar_file ${board_dir}/root -O | dd bs=1 count=$rootfs_length | md5sum); rootfs_md5="${rootfs_md5%% *}"
  61. #
  62. # add tar support to get_image() to use default_do_upgrade() instead?
  63. #
  64. # take care of restoring a saved config
  65. [ "$SAVE_CONFIG" -eq 1 ] && restore_backup="${MTD_CONFIG_ARGS} -j ${CONF_TAR}"
  66. # write concatinated kernel + rootfs to flash
  67. tar xf $tar_file ${board_dir}/kernel ${board_dir}/root -O | \
  68. mtd $restore_backup write - $PART_NAME
  69. # prepare new u-boot env
  70. if [ "$next_boot_part" = "1" ]; then
  71. echo "bootseq 1,2" > $setenv_script
  72. else
  73. echo "bootseq 2,1" > $setenv_script
  74. fi
  75. printf "kernel_size_%i 0x%08x\n" $next_boot_part $kernel_length >> $setenv_script
  76. printf "vmlinux_start_addr 0x%08x\n" ${kernel_offset} >> $setenv_script
  77. printf "vmlinux_size 0x%08x\n" ${kernel_length} >> $setenv_script
  78. printf "vmlinux_checksum %s\n" ${kernel_md5} >> $setenv_script
  79. printf "rootfs_size_%i 0x%08x\n" $next_boot_part $((total_size-kernel_length)) >> $setenv_script
  80. printf "rootfs_start_addr 0x%08x\n" $((kernel_offset+kernel_length)) >> $setenv_script
  81. printf "rootfs_size 0x%08x\n" ${rootfs_length} >> $setenv_script
  82. printf "rootfs_checksum %s\n" ${rootfs_md5} >> $setenv_script
  83. # store u-boot env changes
  84. fw_setenv -s $setenv_script || {
  85. echo "failed to update U-Boot environment"
  86. return 1
  87. }
  88. }
  89. # create /var/lock for the lock "fw_setenv.lock" of fw_setenv
  90. # the rest is copied using ipq806x's RAMFS_COPY_BIN and RAMFS_COPY_DATA
  91. platform_add_ramfs_ubootenv()
  92. {
  93. mkdir -p $RAM_ROOT/var/lock
  94. }
  95. append sysupgrade_pre_upgrade platform_add_ramfs_ubootenv