openmesh.sh 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. trim()
  8. {
  9. echo $1
  10. }
  11. cfg_value_get()
  12. {
  13. local cfg=$1 cfg_opt
  14. local section=$2 our_section=0
  15. local param=$3 our_param=
  16. for cfg_opt in $cfg
  17. do
  18. [ "$cfg_opt" = "[$section]" ] && our_section=1 && continue
  19. [ "$our_section" = "1" ] || continue
  20. our_param=$(echo ${cfg_opt%%=*})
  21. [ "$param" = "$our_param" ] && echo ${cfg_opt##*=} && break
  22. done
  23. }
  24. # make sure we got uboot-envtools and fw_env.config copied over to the ramfs
  25. # create /var/lock for the lock "fw_setenv.lock" of fw_setenv
  26. platform_add_ramfs_ubootenv()
  27. {
  28. [ -e /usr/sbin/fw_printenv ] && install_bin /usr/sbin/fw_printenv /usr/sbin/fw_setenv
  29. [ -e /etc/fw_env.config ] && install_file /etc/fw_env.config
  30. mkdir -p $RAM_ROOT/var/lock
  31. }
  32. append sysupgrade_pre_upgrade platform_add_ramfs_ubootenv
  33. platform_check_image_target_openmesh()
  34. {
  35. img_board_target="$1"
  36. case "$img_board_target" in
  37. A60)
  38. [ "$board" = "a40" ] && return 0
  39. [ "$board" = "a60" ] && return 0
  40. echo "Invalid image board target ($img_board_target) for this platform: $board. Use the correct image for this platform"
  41. return 1
  42. ;;
  43. OM2P)
  44. [ "$board" = "om2p" ] && return 0
  45. [ "$board" = "om2pv2" ] && return 0
  46. [ "$board" = "om2pv4" ] && return 0
  47. [ "$board" = "om2p-lc" ] && return 0
  48. [ "$board" = "om2p-hs" ] && return 0
  49. [ "$board" = "om2p-hsv2" ] && return 0
  50. [ "$board" = "om2p-hsv3" ] && return 0
  51. [ "$board" = "om2p-hsv4" ] && return 0
  52. echo "Invalid image board target ($img_board_target) for this platform: $board. Use the correct image for this platform"
  53. return 1
  54. ;;
  55. OM5P)
  56. [ "$board" = "om5p" ] && return 0
  57. [ "$board" = "om5p-an" ] && return 0
  58. echo "Invalid image board target ($img_board_target) for this platform: $board. Use the correct image for this platform"
  59. return 1
  60. ;;
  61. OM5PAC)
  62. [ "$board" = "om5p-ac" ] && return 0
  63. [ "$board" = "om5p-acv2" ] && return 0
  64. echo "Invalid image board target ($img_board_target) for this platform: $board. Use the correct image for this platform"
  65. return 1
  66. ;;
  67. MR1750)
  68. [ "$board" = "mr1750" ] && return 0
  69. [ "$board" = "mr1750v2" ] && return 0
  70. echo "Invalid image board target ($img_board_target) for this platform: $board. Use the correct image for this platform"
  71. return 1
  72. ;;
  73. MR600)
  74. [ "$board" = "mr600" ] && return 0
  75. [ "$board" = "mr600v2" ] && return 0
  76. echo "Invalid image board target ($img_board_target) for this platform: $board. Use the correct image for this platform"
  77. return 1
  78. ;;
  79. MR900)
  80. [ "$board" = "mr900" ] && return 0
  81. [ "$board" = "mr900v2" ] && return 0
  82. echo "Invalid image board target ($img_board_target) for this platform: $board. Use the correct image for this platform"
  83. return 1
  84. ;;
  85. *)
  86. echo "Invalid board target ($img_board_target). Use the correct image for this platform"
  87. return 1
  88. ;;
  89. esac
  90. }
  91. platform_check_image_openmesh()
  92. {
  93. local img_magic=$1
  94. local img_path=$2
  95. local fw_printenv=/usr/sbin/fw_printenv
  96. local img_board_target= img_num_files= i=0
  97. local cfg_name= kernel_name= rootfs_name=
  98. case "$img_magic" in
  99. # Combined Extended Image v1
  100. 43453031)
  101. img_board_target=$(trim $(dd if="$img_path" bs=4 skip=1 count=8 2>/dev/null))
  102. img_num_files=$(trim $(dd if="$img_path" bs=2 skip=18 count=1 2>/dev/null))
  103. ;;
  104. *)
  105. echo "Invalid image ($img_magic). Use combined extended images on this platform"
  106. return 1
  107. ;;
  108. esac
  109. platform_check_image_target_openmesh "$img_board_target" || return 1
  110. [ $img_num_files -lt 3 ] && {
  111. echo "Invalid number of embedded images ($img_num_files). Use the correct image for this platform"
  112. return 1
  113. }
  114. cfg_name=$(trim $(dd if="$img_path" bs=2 skip=19 count=16 2>/dev/null))
  115. [ "$cfg_name" != "fwupgrade.cfg" ] && {
  116. echo "Invalid embedded config file ($cfg_name). Use the correct image for this platform"
  117. return 1
  118. }
  119. kernel_name=$(trim $(dd if="$img_path" bs=2 skip=55 count=16 2>/dev/null))
  120. [ "$kernel_name" != "kernel" ] && {
  121. echo "Invalid embedded kernel file ($kernel_name). Use the correct image for this platform"
  122. return 1
  123. }
  124. rootfs_name=$(trim $(dd if="$img_path" bs=2 skip=91 count=16 2>/dev/null))
  125. [ "$rootfs_name" != "rootfs" ] && {
  126. echo "Invalid embedded kernel file ($rootfs_name). Use the correct image for this platform"
  127. return 1
  128. }
  129. [ ! -x "$fw_printenv" ] && {
  130. echo "Please install uboot-envtools!"
  131. return 1
  132. }
  133. [ ! -r "/etc/fw_env.config" ] && {
  134. echo "/etc/fw_env.config is missing"
  135. return 1
  136. }
  137. return 0
  138. }
  139. platform_do_upgrade_openmesh()
  140. {
  141. local img_path=$1 img_board_target=
  142. local kernel_start_addr= kernel_start_addr1= kernel_start_addr2=
  143. local kernel_size= kernel_md5=
  144. local rootfs_size= rootfs_checksize= rootfs_md5=
  145. local kernel_bsize= total_size=
  146. local data_offset=$((64 * 1024)) block_size= offset=
  147. local uboot_env_upgrade="/tmp/fw_env_upgrade"
  148. local cfg_size= kernel_size= rootfs_size=
  149. local append=""
  150. [ -f "$CONF_TAR" -a "$SAVE_CONFIG" -eq 1 ] && append="-j $CONF_TAR"
  151. cfg_size=$(dd if="$img_path" bs=2 skip=35 count=4 2>/dev/null)
  152. kernel_size=$(dd if="$img_path" bs=2 skip=71 count=4 2>/dev/null)
  153. rootfs_size=$(dd if="$img_path" bs=2 skip=107 count=4 2>/dev/null)
  154. img_board_target=$(trim $(dd if="$img_path" bs=4 skip=1 count=8 2>/dev/null))
  155. cfg_content=$(dd if="$img_path" bs=1 skip=$data_offset count=$(echo $((0x$cfg_size))) 2>/dev/null)
  156. case $img_board_target in
  157. OM2P)
  158. block_size=$((256 * 1024))
  159. total_size=7340032
  160. kernel_start_addr1=0x9f1c0000
  161. kernel_start_addr2=0x9f8c0000
  162. ;;
  163. OM5P|OM5PAC|MR600|MR900|MR1750|A60)
  164. block_size=$((64 * 1024))
  165. total_size=7995392
  166. kernel_start_addr1=0x9f0b0000
  167. kernel_start_addr2=0x9f850000
  168. ;;
  169. esac
  170. kernel_md5=$(cfg_value_get "$cfg_content" "vmlinux" "md5sum")
  171. rootfs_md5=$(cfg_value_get "$cfg_content" "rootfs" "md5sum")
  172. rootfs_checksize=$(cfg_value_get "$cfg_content" "rootfs" "checksize")
  173. if [ "$((0x$kernel_size % $block_size))" = "0" ]
  174. then
  175. kernel_bsize=$(echo $((0x$kernel_size)))
  176. else
  177. kernel_bsize=$((0x$kernel_size + ($block_size - (0x$kernel_size % $block_size))))
  178. fi
  179. mtd -q erase inactive
  180. offset=$(echo $(($data_offset + 0x$cfg_size + 0x$kernel_size)))
  181. dd if="$img_path" bs=1 skip=$offset count=$(echo $((0x$rootfs_size))) 2>&- | mtd -n -p $kernel_bsize $append write - "inactive"
  182. offset=$(echo $(($data_offset + 0x$cfg_size)))
  183. dd if="$img_path" bs=1 skip=$offset count=$(echo $((0x$kernel_size))) 2>&- | mtd -n write - "inactive"
  184. rm $uboot_env_upgrade 2>&-
  185. if [ "$(grep 'mtd3:.*inactive' /proc/mtd)" ]
  186. then
  187. printf "kernel_size_1 %u\n" $(($kernel_bsize / 1024)) >> $uboot_env_upgrade
  188. printf "rootfs_size_1 %u\n" $((($total_size - $kernel_bsize) / 1024)) >> $uboot_env_upgrade
  189. printf "bootseq 1,2\n" >> $uboot_env_upgrade
  190. kernel_start_addr=$kernel_start_addr1
  191. else
  192. printf "kernel_size_2 %u\n" $(($kernel_bsize / 1024)) >> $uboot_env_upgrade
  193. printf "rootfs_size_2 %u\n" $((($total_size - $kernel_bsize) / 1024)) >> $uboot_env_upgrade
  194. printf "bootseq 2,1\n" >> $uboot_env_upgrade
  195. kernel_start_addr=$kernel_start_addr2
  196. fi
  197. printf "vmlinux_start_addr %s\n" $kernel_start_addr >> $uboot_env_upgrade
  198. printf "vmlinux_size 0x%s\n" $kernel_size >> $uboot_env_upgrade
  199. printf "vmlinux_checksum %s\n" $kernel_md5 >> $uboot_env_upgrade
  200. printf "rootfs_start_addr 0x%x\n" $(($kernel_start_addr + $kernel_bsize)) >> $uboot_env_upgrade
  201. printf "rootfs_size %s\n" $rootfs_checksize >> $uboot_env_upgrade
  202. printf "rootfs_checksum %s\n" $rootfs_md5 >> $uboot_env_upgrade
  203. fw_setenv -s $uboot_env_upgrade || {
  204. echo "failed to update U-Boot environment"
  205. return 1
  206. }
  207. }