1
0

allnet.sh 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. # The U-Boot loader of the some Allnet devices requires image sizes and
  2. # checksums to be provided in the U-Boot environment.
  3. # In case the check fails during boot, a failsafe-system is started to provide
  4. # a minimal web-interface for flashing a new firmware.
  5. # make sure we got uboot-envtools and fw_env.config copied over to the ramfs
  6. # create /var/lock for the lock "fw_setenv.lock" of fw_setenv
  7. platform_add_ramfs_ubootenv() {
  8. [ -e /usr/sbin/fw_printenv ] && install_bin /usr/sbin/fw_printenv /usr/sbin/fw_setenv
  9. [ -e /etc/fw_env.config ] && install_file /etc/fw_env.config
  10. mkdir -p $RAM_ROOT/var/lock
  11. }
  12. append sysupgrade_pre_upgrade platform_add_ramfs_ubootenv
  13. # determine size of the main firmware partition
  14. platform_get_firmware_size() {
  15. local dev size erasesize name
  16. while read dev size erasesize name; do
  17. name=${name#'"'}; name=${name%'"'}
  18. case "$name" in
  19. firmware)
  20. printf "%d" "0x$size"
  21. break
  22. ;;
  23. esac
  24. done < /proc/mtd
  25. }
  26. # get the first 4 bytes (magic) of a given file starting at offset in hex format
  27. get_magic_long_at() {
  28. dd if="$1" skip=$(( $CI_BLKSZ / 4 * $2 )) bs=4 count=1 2>/dev/null | hexdump -v -n 4 -e '1/1 "%02x"'
  29. }
  30. get_filesize() {
  31. wc -c "$1" | while read image_size _n ; do echo $image_size ; break; done
  32. }
  33. # scan through the update image pages until matching a magic
  34. platform_get_offset() {
  35. offsetcount=0
  36. magiclong="x"
  37. if [ -n "$3" ]; then
  38. offsetcount=$3
  39. fi
  40. while magiclong=$( get_magic_long_at "$1" "$offsetcount" ) && [ -n "$magiclong" ]; do
  41. case "$magiclong" in
  42. "2705"*)
  43. # U-Boot image magic
  44. if [ "$2" = "uImage" ]; then
  45. echo $offsetcount
  46. return
  47. fi
  48. ;;
  49. "68737173"|"73717368")
  50. # SquashFS
  51. if [ "$2" = "rootfs" ]; then
  52. echo $offsetcount
  53. return
  54. fi
  55. ;;
  56. "deadc0de"|"19852003")
  57. # JFFS2 empty page
  58. if [ "$2" = "rootfs-data" ]; then
  59. echo $offsetcount
  60. return
  61. fi
  62. ;;
  63. esac
  64. offsetcount=$(( $offsetcount + 1 ))
  65. done
  66. }
  67. platform_check_image_allnet() {
  68. local fw_printenv=/usr/sbin/fw_printenv
  69. [ ! -n "$fw_printenv" -o ! -x "$fw_printenv" ] && {
  70. echo "Please install uboot-envtools!"
  71. return 1
  72. }
  73. [ ! -r "/etc/fw_env.config" ] && {
  74. echo "/etc/fw_env.config is missing"
  75. return 1
  76. }
  77. local image_size=$( get_filesize "$1" )
  78. local firmware_size=$( platform_get_firmware_size )
  79. [ $image_size -ge $firmware_size ] &&
  80. {
  81. echo "upgrade image is too big (${image_size}b > ${firmware_size}b)"
  82. }
  83. local vmlinux_blockoffset=$( platform_get_offset "$1" uImage )
  84. [ -z $vmlinux_blockoffset ] && {
  85. echo "vmlinux-uImage not found"
  86. return 1
  87. }
  88. local rootfs_blockoffset=$( platform_get_offset "$1" rootfs "$vmlinux_blockoffset" )
  89. [ -z $rootfs_blockoffset ] && {
  90. echo "missing rootfs"
  91. return 1
  92. }
  93. local data_blockoffset=$( platform_get_offset "$1" rootfs-data "$rootfs_blockoffset" )
  94. [ -z $data_blockoffset ] && {
  95. echo "rootfs doesn't have JFFS2 end marker"
  96. return 1
  97. }
  98. return 0
  99. }
  100. platform_do_upgrade_allnet() {
  101. local firmware_base_addr=$( printf "%d" "$1" )
  102. local vmlinux_blockoffset=$( platform_get_offset "$2" uImage )
  103. if [ ! -n "$vmlinux_blockoffset" ]; then
  104. echo "can't determine uImage offset"
  105. return 1
  106. fi
  107. local rootfs_blockoffset=$( platform_get_offset "$2" rootfs $(( $vmlinux_blockoffset + 1 )) )
  108. local vmlinux_offset=$(( $vmlinux_blockoffset * $CI_BLKSZ ))
  109. local vmlinux_addr=$(( $firmware_base_addr + $vmlinux_offset ))
  110. local vmlinux_hexaddr=0x$( printf "%08x" "$vmlinux_addr" )
  111. if [ ! -n "$rootfs_blockoffset" ]; then
  112. echo "can't determine rootfs offset"
  113. return 1
  114. fi
  115. local rootfs_offset=$(( $rootfs_blockoffset * $CI_BLKSZ ))
  116. local rootfs_addr=$(( $firmware_base_addr + $rootfs_offset ))
  117. local rootfs_hexaddr=0x$( printf "%08x" "$rootfs_addr" )
  118. local vmlinux_blockcount=$(( $rootfs_blockoffset - $vmlinux_blockoffset ))
  119. local vmlinux_size=$(( $rootfs_offset - $vmlinux_offset ))
  120. local vmlinux_hexsize=0x$( printf "%08x" "$vmlinux_size" )
  121. local data_blockoffset=$( platform_get_offset "$2" rootfs-data $(( $rootfs_blockoffset + 1 )) )
  122. if [ ! -n "$data_blockoffset" ]; then
  123. echo "can't determine rootfs size"
  124. return 1
  125. fi
  126. local data_offset=$(( $data_blockoffset * $CI_BLKSZ ))
  127. local rootfs_blockcount=$(( $data_blockoffset - $rootfs_blockoffset ))
  128. local rootfs_size=$(( $data_offset - $rootfs_offset ))
  129. local rootfs_hexsize=0x$( printf "%08x" "$rootfs_size" )
  130. local rootfs_md5=$( dd if="$2" bs=$CI_BLKSZ skip=$rootfs_blockoffset count=$rootfs_blockcount 2>/dev/null | md5sum -); rootfs_md5="${rootfs_md5%% *}"
  131. local vmlinux_md5=$( dd if="$2" bs=$CI_BLKSZ skip=$vmlinux_blockoffset count=$vmlinux_blockcount 2>/dev/null | md5sum -); vmlinux_md5="${vmlinux_md5%% *}"
  132. # this needs a recent version of uboot-envtools!
  133. cat >/tmp/fw_env_upgrade <<EOF
  134. vmlinux_start_addr $vmlinux_hexaddr
  135. vmlinux_size $vmlinux_hexsize
  136. vmlinux_checksum $vmlinux_md5
  137. rootfs_start_addr $rootfs_hexaddr
  138. rootfs_size $rootfs_hexsize
  139. rootfs_checksum $rootfs_md5
  140. bootcmd bootm $vmlinux_hexaddr
  141. EOF
  142. fw_setenv -s /tmp/fw_env_upgrade || {
  143. echo "failed to update U-Boot environment"
  144. return 1
  145. }
  146. shift
  147. default_do_upgrade "$@"
  148. }