openmesh.sh 7.5 KB

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