platform.sh 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. PART_NAME=firmware
  2. # $(1): file to read magic from
  3. # $(2): offset in bytes
  4. get_magic_long_at() {
  5. dd if="$1" skip=$2 bs=1 count=4 2>/dev/null | hexdump -v -e '1/1 "%02x"'
  6. }
  7. platform_machine() {
  8. cat /proc/device-tree/compatible | tr '\0' '\t' | cut -f 1
  9. }
  10. platform_flash_type() {
  11. # On NAND devices "rootfs" is UBI volume, so won't be find in /proc/mtd
  12. grep -q "\"rootfs\"" /proc/mtd && {
  13. echo "serial"
  14. return
  15. }
  16. echo "nand"
  17. }
  18. platform_expected_image() {
  19. local machine=$(platform_machine)
  20. case "$machine" in
  21. "dlink,dir-885l") echo "seama wrgac42_dlink.2015_dir885l"; return;;
  22. "netgear,r6250v1") echo "chk U12H245T00_NETGEAR"; return;;
  23. "netgear,r6300v2") echo "chk U12H240T00_NETGEAR"; return;;
  24. "netgear,r7000") echo "chk U12H270T00_NETGEAR"; return;;
  25. "netgear,r7900") echo "chk U12H315T30_NETGEAR"; return;;
  26. "netgear,r8000") echo "chk U12H315T00_NETGEAR"; return;;
  27. "netgear,r8500") echo "chk U12H334T00_NETGEAR"; return;;
  28. esac
  29. }
  30. platform_identify() {
  31. local magic
  32. magic=$(get_magic_long "$1")
  33. case "$magic" in
  34. "48445230")
  35. echo "trx"
  36. return
  37. ;;
  38. "2a23245e")
  39. echo "chk"
  40. return
  41. ;;
  42. "5ea3a417")
  43. echo "seama"
  44. return
  45. ;;
  46. esac
  47. magic=$(get_magic_long_at "$1" 14)
  48. [ "$magic" = "55324e44" ] && {
  49. echo "cybertan"
  50. return
  51. }
  52. echo "unknown"
  53. }
  54. platform_check_image() {
  55. [ "$#" -gt 1 ] && return 1
  56. local file_type=$(platform_identify "$1")
  57. local magic
  58. local error=0
  59. case "$file_type" in
  60. "chk")
  61. local header_len=$((0x$(get_magic_long_at "$1" 4)))
  62. local board_id_len=$(($header_len - 40))
  63. local board_id=$(dd if="$1" skip=40 bs=1 count=$board_id_len 2>/dev/null | hexdump -v -e '1/1 "%c"')
  64. local dev_board_id=$(platform_expected_image)
  65. echo "Found CHK image with device board_id $board_id"
  66. [ -n "$dev_board_id" -a "chk $board_id" != "$dev_board_id" ] && {
  67. echo "Firmware board_id doesn't match device board_id ($dev_board_id)"
  68. error=1
  69. }
  70. if ! otrx check "$1" -o "$header_len"; then
  71. echo "No valid TRX firmware in the CHK image"
  72. error=1
  73. fi
  74. ;;
  75. "cybertan")
  76. local pattern=$(dd if="$1" bs=1 count=4 2>/dev/null | hexdump -v -e '1/1 "%c"')
  77. local dev_pattern=$(platform_expected_image)
  78. echo "Found CyberTAN image with device pattern: $pattern"
  79. [ -n "$dev_pattern" -a "cybertan $pattern" != "$dev_pattern" ] && {
  80. echo "Firmware pattern doesn't match device pattern ($dev_pattern)"
  81. error=1
  82. }
  83. if ! otrx check "$1" -o 32; then
  84. echo "No valid TRX firmware in the CyberTAN image"
  85. error=1
  86. fi
  87. ;;
  88. "seama")
  89. local img_signature=$(oseama info "$1" | grep "Meta entry:.*signature=" | sed "s/.*=//")
  90. local dev_signature=$(platform_expected_image)
  91. echo "Found Seama image with device signature: $img_signature"
  92. [ -n "$dev_signature" -a "seama $img_signature" != "$dev_signature" ] && {
  93. echo "Firmware signature doesn't match device signature ($dev_signature)"
  94. error=1
  95. }
  96. $(oseama info "$1" -e 0 | grep -q "Meta entry:.*type=firmware") || {
  97. echo "Seama container doesn't have firmware entity"
  98. error=1
  99. }
  100. ;;
  101. "trx")
  102. if ! otrx check "$1"; then
  103. echo "Invalid (corrupted?) TRX firmware"
  104. error=1
  105. fi
  106. ;;
  107. *)
  108. echo "Invalid image type. Please use only .trx files"
  109. error=1
  110. ;;
  111. esac
  112. return $error
  113. }
  114. # $(1): image for upgrade (with possible extra header)
  115. # $(2): offset of trx in image
  116. platform_pre_upgrade_trx() {
  117. local dir="/tmp/sysupgrade-bcm53xx"
  118. local trx="$1"
  119. local offset="$2"
  120. # Extract partitions from trx
  121. rm -fR $dir
  122. mkdir -p $dir
  123. otrx extract "$trx" \
  124. ${offset:+-o $offset} \
  125. -1 $dir/kernel \
  126. -2 $dir/root
  127. [ $? -ne 0 ] && {
  128. echo "Failed to extract TRX partitions."
  129. return
  130. }
  131. # Firmwares without UBI image should be flashed "normally"
  132. local root_type=$(identify $dir/root)
  133. [ "$root_type" != "ubi" ] && {
  134. echo "Provided firmware doesn't use UBI for rootfs."
  135. return
  136. }
  137. # Prepare TRX file with just a kernel that will replace current one
  138. local linux_length=$(grep "\"linux\"" /proc/mtd | sed "s/mtd[0-9]*:[ \t]*\([^ \t]*\).*/\1/")
  139. [ -z "$linux_length" ] && {
  140. echo "Unable to find \"linux\" partition size"
  141. exit 1
  142. }
  143. linux_length=$((0x$linux_length))
  144. local kernel_length=$(wc -c $dir/kernel | cut -d ' ' -f 1)
  145. [ $kernel_length -gt $linux_length ] && {
  146. echo "New kernel doesn't fit \"linux\" partition."
  147. return
  148. }
  149. rm -f /tmp/null.bin
  150. rm -f /tmp/kernel.trx
  151. touch /tmp/null.bin
  152. otrx create /tmp/kernel.trx \
  153. -f $dir/kernel -b $(($linux_length + 28)) \
  154. -f /tmp/null.bin
  155. [ $? -ne 0 ] && {
  156. echo "Failed to create simple TRX with new kernel."
  157. return
  158. }
  159. # Prepare UBI image (drop unwanted extra blocks)
  160. local ubi_length=0
  161. while [ "$(dd if=$dir/root skip=$ubi_length bs=1 count=4 2>/dev/null)" = "UBI#" ]; do
  162. ubi_length=$(($ubi_length + 131072))
  163. done
  164. dd if=$dir/root of=/tmp/root.ubi bs=131072 count=$((ubi_length / 131072)) 2>/dev/null
  165. [ $? -ne 0 ] && {
  166. echo "Failed to prepare new UBI image."
  167. return
  168. }
  169. # Flash
  170. mtd write /tmp/kernel.trx firmware
  171. nand_do_upgrade /tmp/root.ubi
  172. }
  173. platform_pre_upgrade_seama() {
  174. local dir="/tmp/sysupgrade-bcm53xx"
  175. local seama="$1"
  176. local tmp
  177. # Extract Seama entity from Seama seal
  178. rm -fR $dir
  179. mkdir -p $dir
  180. oseama extract "$seama" \
  181. -e 0 \
  182. -o $dir/seama.entity
  183. [ $? -ne 0 ] && {
  184. echo "Failed to extract Seama entity."
  185. return
  186. }
  187. local entity_size=$(wc -c $dir/seama.entity | cut -d ' ' -f 1)
  188. local ubi_offset=0
  189. tmp=0
  190. while [ 1 ]; do
  191. [ $tmp -ge $entity_size ] && break
  192. [ "$(dd if=$dir/seama.entity skip=$tmp bs=1 count=4 2>/dev/null)" = "UBI#" ] && {
  193. ubi_offset=$tmp
  194. break
  195. }
  196. tmp=$(($tmp + 131072))
  197. done
  198. [ $ubi_offset -eq 0 ] && {
  199. echo "Failed to find UBI in Seama entity."
  200. return
  201. }
  202. local ubi_length=0
  203. while [ "$(dd if=$dir/seama.entity skip=$(($ubi_offset + $ubi_length)) bs=1 count=4 2>/dev/null)" = "UBI#" ]; do
  204. ubi_length=$(($ubi_length + 131072))
  205. done
  206. dd if=$dir/seama.entity of=$dir/kernel.seama bs=131072 count=$(($ubi_offset / 131072)) 2>/dev/null
  207. dd if=$dir/seama.entity of=$dir/root.ubi bs=131072 skip=$(($ubi_offset / 131072)) count=$(($ubi_length / 131072)) 2>/dev/null
  208. # Flash
  209. local kernel_size=$(sed -n 's/mtd[0-9]*: \([0-9a-f]*\).*"\(kernel\|linux\)".*/\1/p' /proc/mtd)
  210. mtd write $dir/kernel.seama firmware
  211. mtd ${kernel_size:+-c 0x$kernel_size} fixseama firmware
  212. nand_do_upgrade $dir/root.ubi
  213. }
  214. platform_pre_upgrade() {
  215. export RAMFS_COPY_BIN="${RAMFS_COPY_BIN} /usr/bin/oseama /bin/sed"
  216. local file_type=$(platform_identify "$1")
  217. [ "$(platform_flash_type)" != "nand" ] && return
  218. # Find trx offset
  219. case "$file_type" in
  220. "chk") platform_pre_upgrade_trx "$1" $((0x$(get_magic_long_at "$1" 4)));;
  221. "cybertan") platform_pre_upgrade_trx "$1" 32;;
  222. "seama") platform_pre_upgrade_seama "$1";;
  223. "trx") platform_pre_upgrade_trx "$1";;
  224. esac
  225. }
  226. platform_trx_from_chk_cmd() {
  227. local header_len=$((0x$(get_magic_long_at "$1" 4)))
  228. echo -n dd bs=$header_len skip=1
  229. }
  230. platform_trx_from_cybertan_cmd() {
  231. echo -n dd bs=32 skip=1
  232. }
  233. platform_img_from_seama() {
  234. local dir="/tmp/sysupgrade-bcm53xx"
  235. local offset=$(oseama info "$1" -e 0 | grep "Entity offset:" | sed "s/.*:\s*//")
  236. local size=$(oseama info "$1" -e 0 | grep "Entity size:" | sed "s/.*:\s*//")
  237. # Busybox doesn't support required iflag-s
  238. # echo -n dd iflag=skip_bytes,count_bytes skip=$offset count=$size
  239. rm -fR $dir
  240. mkdir -p $dir
  241. dd if="$1" of=$dir/image-noheader.bin bs=$offset skip=1
  242. dd if=$dir/image-noheader.bin of=$dir/image-entity.bin bs=$size count=1
  243. echo -n $dir/image-entity.bin
  244. }
  245. platform_do_upgrade() {
  246. local file_type=$(platform_identify "$1")
  247. local trx="$1"
  248. local cmd=
  249. [ "$(platform_flash_type)" == "nand" ] && {
  250. echo "Writing whole image to NAND flash. All erase counters will be lost."
  251. }
  252. case "$file_type" in
  253. "chk") cmd=$(platform_trx_from_chk_cmd "$trx");;
  254. "cybertan") cmd=$(platform_trx_from_cybertan_cmd "$trx");;
  255. "seama") trx=$(platform_img_from_seama "$trx");;
  256. esac
  257. default_do_upgrade "$trx" "$cmd"
  258. }