Guest User

Untitled

a guest
Jun 24th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.29 KB | None | 0 0
  1. #!/bin/sh -x
  2.  
  3. source /lib/upgrade/platform.sh
  4.  
  5. # determine size of the main firmware partition
  6. platform_get_firmware_size() {
  7. local dev size erasesize name
  8. while read dev size erasesize name; do
  9. name=${name#'"'}; name=${name%'"'}
  10. case "$name" in
  11. firmware)
  12. printf "%d" "0x$size"
  13. break
  14. ;;
  15. esac
  16. done < /proc/mtd
  17. }
  18.  
  19. # get the first 4 bytes (magic) of a given file starting at offset in hex format
  20. get_magic_long_at() {
  21. dd if="$1" skip=$(( $CI_BLKSZ / 4 * $2 )) bs=4 count=1 2>/dev/null | hexdump -v -n 4 -e '1/1 "%02x"'
  22. }
  23.  
  24. get_filesize() {
  25. wc -c "$1" | while read image_size _n ; do echo $image_size ; break; done
  26. }
  27.  
  28. # scan through the update image pages until matching a magic
  29. platform_get_offset() {
  30. offsetcount=0
  31. magiclong="x"
  32. if [ -n "$3" ]; then
  33. offsetcount=$3
  34. fi
  35. while magiclong=$( get_magic_long_at "$1" "$offsetcount" ) && [ -n "$magiclong" ]; do
  36. case "$magiclong" in
  37. "2705"*)
  38. # U-Boot image magic
  39. if [ "$2" = "uImage" ]; then
  40. echo $offsetcount
  41. return
  42. fi
  43. ;;
  44. "68737173"|"73717368")
  45. # SquashFS
  46. if [ "$2" = "rootfs" ]; then
  47. echo $offsetcount
  48. return
  49. fi
  50. ;;
  51. "deadc0de"|"19852003")
  52. # JFFS2 empty page
  53. if [ "$2" = "rootfs-data" ]; then
  54. echo $offsetcount
  55. return
  56. fi
  57. ;;
  58. esac
  59. offsetcount=$(( $offsetcount + 1 ))
  60. done
  61. }
  62.  
  63. platform_check_image_allnet() {
  64. local fw_printenv=/usr/sbin/fw_printenv
  65. [ ! -n "$fw_printenv" -o ! -x "$fw_printenv" ] && {
  66. echo "Please install uboot-envtools!"
  67. return 1
  68. }
  69.  
  70. [ ! -r "/etc/fw_env.config" ] && {
  71. echo "/etc/fw_env.config is missing"
  72. return 1
  73. }
  74.  
  75. local image_size=$( get_filesize "$1" )
  76. local firmware_size=$( platform_get_firmware_size )
  77. [ $image_size -ge $firmware_size ] &&
  78. {
  79. echo "upgrade image is too big (${image_size}b > ${firmware_size}b)"
  80. }
  81.  
  82. local vmlinux_blockoffset=$( platform_get_offset "$1" uImage )
  83. [ -z $vmlinux_blockoffset ] && {
  84. echo "vmlinux-uImage not found"
  85. return 1
  86. }
  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.  
  94. local data_blockoffset=$( platform_get_offset "$1" rootfs-data "$rootfs_blockoffset" )
  95. [ -z $data_blockoffset ] && {
  96. echo "rootfs doesn't have JFFS2 end marker"
  97. return 1
  98. }
  99.  
  100. return 0
  101. }
  102.  
  103. platform_do_upgrade_allnet() {
  104. local firmware_base_addr=$( printf "%d" "$1" )
  105. local vmlinux_blockoffset=$( platform_get_offset "$2" uImage )
  106. if [ ! -n "$vmlinux_blockoffset" ]; then
  107. echo "can't determine uImage offset"
  108. return 1
  109. fi
  110. local rootfs_blockoffset=$( platform_get_offset "$2" rootfs $(( $vmlinux_blockoffset + 1 )) )
  111. local vmlinux_offset=$(( $vmlinux_blockoffset * $CI_BLKSZ ))
  112. local vmlinux_addr=$(( $firmware_base_addr + $vmlinux_offset ))
  113. local vmlinux_hexaddr=0x$( printf "%08x" "$vmlinux_addr" )
  114. if [ ! -n "$rootfs_blockoffset" ]; then
  115. echo "can't determine rootfs offset"
  116. return 1
  117. fi
  118. local rootfs_offset=$(( $rootfs_blockoffset * $CI_BLKSZ ))
  119. local rootfs_addr=$(( $firmware_base_addr + $rootfs_offset ))
  120. local rootfs_hexaddr=0x$( printf "%08x" "$rootfs_addr" )
  121. local vmlinux_blockcount=$(( $rootfs_blockoffset - $vmlinux_blockoffset ))
  122. local vmlinux_size=$(( $rootfs_offset - $vmlinux_offset ))
  123. local vmlinux_hexsize=0x$( printf "%08x" "$vmlinux_size" )
  124. local data_blockoffset=$( platform_get_offset "$2" rootfs-data $(( $rootfs_blockoffset + 1 )) )
  125. if [ ! -n "$data_blockoffset" ]; then
  126. echo "can't determine rootfs size"
  127. return 1
  128. fi
  129. local data_offset=$(( $data_blockoffset * $CI_BLKSZ ))
  130. local rootfs_blockcount=$(( $data_blockoffset - $rootfs_blockoffset ))
  131. local rootfs_size=$(( $data_offset - $rootfs_offset ))
  132. local rootfs_hexsize=0x$( printf "%08x" "$rootfs_size" )
  133.  
  134. local rootfs_md5=$( dd if="$2" bs=$CI_BLKSZ skip=$rootfs_blockoffset count=$rootfs_blockcount 2>/dev/null | md5sum -); rootfs_md5="${rootfs_md5%% *}"
  135. local vmlinux_md5=$( dd if="$2" bs=$CI_BLKSZ skip=$vmlinux_blockoffset count=$vmlinux_blockcount 2>/dev/null | md5sum -); vmlinux_md5="${vmlinux_md5%% *}"
  136. # this needs a recent version of uboot-envtools!
  137. cat >/tmp/fw_env_upgrade <<EOF
  138. vmlinux_start_addr $vmlinux_hexaddr
  139. vmlinux_size $vmlinux_hexsize
  140. vmlinux_checksum $vmlinux_md5
  141. rootfs_start_addr $rootfs_hexaddr
  142. rootfs_size $rootfs_hexsize
  143. rootfs_checksum $rootfs_md5
  144. bootcmd bootm $vmlinux_hexaddr
  145. EOF
  146. }
  147.  
  148. platform_do_upgrade_allnet "$1" "$2"
Add Comment
Please, Sign In to add comment