platform.sh 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. #
  2. # Copyright (C) 2011 OpenWrt.org
  3. #
  4. . /lib/functions/system.sh
  5. . /lib/ar71xx.sh
  6. PART_NAME=firmware
  7. RAMFS_COPY_DATA=/lib/ar71xx.sh
  8. CI_BLKSZ=65536
  9. CI_LDADR=0x80060000
  10. platform_find_partitions() {
  11. local first dev size erasesize name
  12. while read dev size erasesize name; do
  13. name=${name#'"'}; name=${name%'"'}
  14. case "$name" in
  15. vmlinux.bin.l7|vmlinux|kernel|linux|linux.bin|rootfs|filesystem)
  16. if [ -z "$first" ]; then
  17. first="$name"
  18. else
  19. echo "$erasesize:$first:$name"
  20. break
  21. fi
  22. ;;
  23. esac
  24. done < /proc/mtd
  25. }
  26. platform_find_kernelpart() {
  27. local part
  28. for part in "${1%:*}" "${1#*:}"; do
  29. case "$part" in
  30. vmlinux.bin.l7|vmlinux|kernel|linux|linux.bin)
  31. echo "$part"
  32. break
  33. ;;
  34. esac
  35. done
  36. }
  37. platform_do_upgrade_combined() {
  38. local partitions=$(platform_find_partitions)
  39. local kernelpart=$(platform_find_kernelpart "${partitions#*:}")
  40. local erase_size=$((0x${partitions%%:*})); partitions="${partitions#*:}"
  41. local kern_length=0x$(dd if="$1" bs=2 skip=1 count=4 2>/dev/null)
  42. local kern_blocks=$(($kern_length / $CI_BLKSZ))
  43. local root_blocks=$((0x$(dd if="$1" bs=2 skip=5 count=4 2>/dev/null) / $CI_BLKSZ))
  44. if [ -n "$partitions" ] && [ -n "$kernelpart" ] && \
  45. [ ${kern_blocks:-0} -gt 0 ] && \
  46. [ ${root_blocks:-0} -gt 0 ] && \
  47. [ ${erase_size:-0} -gt 0 ];
  48. then
  49. local append=""
  50. [ -f "$CONF_TAR" -a "$SAVE_CONFIG" -eq 1 ] && append="-j $CONF_TAR"
  51. ( dd if="$1" bs=$CI_BLKSZ skip=1 count=$kern_blocks 2>/dev/null; \
  52. dd if="$1" bs=$CI_BLKSZ skip=$((1+$kern_blocks)) count=$root_blocks 2>/dev/null ) | \
  53. mtd -r $append -F$kernelpart:$kern_length:$CI_LDADR,rootfs write - $partitions
  54. fi
  55. }
  56. tplink_get_image_hwid() {
  57. get_image "$@" | dd bs=4 count=1 skip=16 2>/dev/null | hexdump -v -n 4 -e '1/1 "%02x"'
  58. }
  59. tplink_get_image_boot_size() {
  60. get_image "$@" | dd bs=4 count=1 skip=37 2>/dev/null | hexdump -v -n 4 -e '1/1 "%02x"'
  61. }
  62. tplink_pharos_check_image() {
  63. local magic_long="$(get_magic_long "$1")"
  64. [ "$magic_long" != "7f454c46" ] && {
  65. echo "Invalid image magic '$magic_long'"
  66. return 1
  67. }
  68. local model_string="$(tplink_pharos_get_model_string)"
  69. local line
  70. # Here $1 is given to dd directly instead of get_image as otherwise the skip
  71. # will take almost a second (as dd can't seek then)
  72. #
  73. # This will fail if the image isn't local, but that's fine: as the
  74. # read loop won't be executed at all, it will return true, so the image
  75. # is accepted (loading the first 1.5M of a remote image for this check seems
  76. # a bit extreme)
  77. dd if="$1" bs=1 skip=1511432 count=1024 2>/dev/null | while read line; do
  78. [ "$line" == "$model_string" ] && break
  79. done || {
  80. echo "Unsupported image (model not in support-list)"
  81. return 1
  82. }
  83. return 0
  84. }
  85. seama_get_type_magic() {
  86. get_image "$@" | dd bs=1 count=4 skip=53 2>/dev/null | hexdump -v -n 4 -e '1/1 "%02x"'
  87. }
  88. cybertan_get_image_magic() {
  89. get_image "$@" | dd bs=8 count=1 skip=0 2>/dev/null | hexdump -v -n 8 -e '1/1 "%02x"'
  90. }
  91. cybertan_check_image() {
  92. local magic="$(cybertan_get_image_magic "$1")"
  93. local fw_magic="$(cybertan_get_hw_magic)"
  94. [ "$fw_magic" != "$magic" ] && {
  95. echo "Invalid image, ID mismatch, got:$magic, but need:$fw_magic"
  96. return 1
  97. }
  98. return 0
  99. }
  100. platform_do_upgrade_compex() {
  101. local fw_file=$1
  102. local fw_part=$PART_NAME
  103. local fw_mtd=$(find_mtd_part $fw_part)
  104. local fw_length=0x$(dd if="$fw_file" bs=2 skip=1 count=4 2>/dev/null)
  105. local fw_blocks=$(($fw_length / 65536))
  106. if [ -n "$fw_mtd" ] && [ ${fw_blocks:-0} -gt 0 ]; then
  107. local append=""
  108. [ -f "$CONF_TAR" -a "$SAVE_CONFIG" -eq 1 ] && append="-j $CONF_TAR"
  109. sync
  110. dd if="$fw_file" bs=64k skip=1 count=$fw_blocks 2>/dev/null | \
  111. mtd $append write - "$fw_part"
  112. fi
  113. }
  114. alfa_check_image() {
  115. local magic_long="$(get_magic_long "$1")"
  116. local fw_part_size=$(mtd_get_part_size firmware)
  117. case "$magic_long" in
  118. "27051956")
  119. [ "$fw_part_size" != "16318464" ] && {
  120. echo "Invalid image magic \"$magic_long\" for $fw_part_size bytes"
  121. return 1
  122. }
  123. ;;
  124. "68737173")
  125. [ "$fw_part_size" != "7929856" ] && {
  126. echo "Invalid image magic \"$magic_long\" for $fw_part_size bytes"
  127. return 1
  128. }
  129. ;;
  130. esac
  131. return 0
  132. }
  133. platform_check_image() {
  134. local board=$(ar71xx_board_name)
  135. local magic="$(get_magic_word "$1")"
  136. local magic_long="$(get_magic_long "$1")"
  137. [ "$#" -gt 1 ] && return 1
  138. case "$board" in
  139. all0315n | \
  140. all0258n | \
  141. cap324 | \
  142. cap4200ag | \
  143. cr3000 |\
  144. cr5000)
  145. platform_check_image_allnet "$1" && return 0
  146. return 1
  147. ;;
  148. alfa-ap96 | \
  149. alfa-nx | \
  150. arduino-yun | \
  151. ap113 | \
  152. ap121 | \
  153. ap121-mini | \
  154. ap136-010 | \
  155. ap136-020 | \
  156. ap135-020 | \
  157. ap147-010 | \
  158. ap96 | \
  159. bxu2000n-2-a1 | \
  160. db120 | \
  161. dr344 | \
  162. f9k1115v2 |\
  163. hornet-ub | \
  164. mr12 | \
  165. mr16 | \
  166. wpj558 | \
  167. zcn-1523h-2 | \
  168. zcn-1523h-5)
  169. [ "$magic_long" != "68737173" -a "$magic_long" != "19852003" ] && {
  170. echo "Invalid image type."
  171. return 1
  172. }
  173. return 0
  174. ;;
  175. ap81 | \
  176. ap83 | \
  177. ap132 | \
  178. c-55 | \
  179. cf-e316n-v2 | \
  180. dgl-5500-a1 |\
  181. dhp-1565-a1 |\
  182. dir-505-a1 | \
  183. dir-600-a1 | \
  184. dir-615-c1 | \
  185. dir-615-e1 | \
  186. dir-615-e4 | \
  187. dir-615-i1 | \
  188. dir-825-c1 | \
  189. dir-835-a1 | \
  190. dlan-hotspot | \
  191. dlan-pro-500-wp | \
  192. dlan-pro-1200-ac | \
  193. dragino2 | \
  194. epg5000 | \
  195. esr1750 | \
  196. esr900 | \
  197. ew-dorin | \
  198. ew-dorin-router | \
  199. gl-ar150 | \
  200. gl-ar300 | \
  201. gl-domino | \
  202. hiwifi-hc6361 | \
  203. hornet-ub-x2 | \
  204. mzk-w04nu | \
  205. mzk-w300nh | \
  206. tew-632brp | \
  207. tew-712br | \
  208. tew-732br | \
  209. tew-823dru | \
  210. wrt400n | \
  211. airgateway | \
  212. airgatewaypro | \
  213. airrouter | \
  214. bullet-m | \
  215. loco-m-xw | \
  216. nanostation-m | \
  217. rocket-m | \
  218. rocket-m-xw | \
  219. rocket-m-ti | \
  220. nanostation-m-xw | \
  221. rw2458n | \
  222. wpj531 | \
  223. wndap360 | \
  224. wpj344 | \
  225. wzr-hp-g300nh2 | \
  226. wzr-hp-g300nh | \
  227. wzr-hp-g450h | \
  228. wzr-hp-ag300h | \
  229. wzr-450hp2 | \
  230. whr-g301n | \
  231. whr-hp-g300n | \
  232. whr-hp-gn | \
  233. wlae-ag300n | \
  234. nbg460n_550n_550nh | \
  235. unifi | \
  236. unifi-outdoor | \
  237. carambola2 | \
  238. weio )
  239. [ "$magic" != "2705" ] && {
  240. echo "Invalid image type."
  241. return 1
  242. }
  243. return 0
  244. ;;
  245. cpe510)
  246. tplink_pharos_check_image "$1" && return 0
  247. return 1
  248. ;;
  249. bsb | \
  250. dir-825-b1 | \
  251. tew-673gru)
  252. dir825b_check_image "$1" && return 0
  253. ;;
  254. mynet-rext|\
  255. wrt160nl)
  256. cybertan_check_image "$1" && return 0
  257. return 1
  258. ;;
  259. qihoo-c301 | \
  260. mynet-n600 | \
  261. mynet-n750)
  262. [ "$magic_long" != "5ea3a417" ] && {
  263. echo "Invalid image, bad magic: $magic_long"
  264. return 1
  265. }
  266. local typemagic=$(seama_get_type_magic "$1")
  267. [ "$typemagic" != "6669726d" ] && {
  268. echo "Invalid image, bad type: $typemagic"
  269. return 1
  270. }
  271. return 0;
  272. ;;
  273. mr1750 | \
  274. mr600 | \
  275. mr600v2 | \
  276. mr900 | \
  277. mr900v2 | \
  278. om2p | \
  279. om2pv2 | \
  280. om2p-hs | \
  281. om2p-hsv2 | \
  282. om2p-lc | \
  283. om5p | \
  284. om5p-an)
  285. platform_check_image_openmesh "$magic_long" "$1" && return 0
  286. return 1
  287. ;;
  288. antminer-s1 | \
  289. antminer-s3 | \
  290. archer-c5 | \
  291. archer-c7 | \
  292. el-m150 | \
  293. el-mini | \
  294. gl-inet | \
  295. mc-mac1200r | \
  296. minibox-v1 |\
  297. onion-omega | \
  298. oolite | \
  299. smart-300 | \
  300. tl-mr10u | \
  301. tl-mr11u | \
  302. tl-mr12u | \
  303. tl-mr13u | \
  304. tl-mr3020 | \
  305. tl-mr3040 | \
  306. tl-mr3040-v2 | \
  307. tl-mr3220 | \
  308. tl-mr3220-v2 | \
  309. tl-mr3420 | \
  310. tl-mr3420-v2 | \
  311. tl-wa701nd-v2 | \
  312. tl-wa7210n-v2 | \
  313. tl-wa7510n | \
  314. tl-wa750re | \
  315. tl-wa850re | \
  316. tl-wa860re | \
  317. tl-wa801nd-v2 | \
  318. tl-wa901nd | \
  319. tl-wa901nd-v2 | \
  320. tl-wa901nd-v3 | \
  321. tl-wdr3320-v2 | \
  322. tl-wdr3500 | \
  323. tl-wdr4300 | \
  324. tl-wdr4900-v2 | \
  325. tl-wdr6500-v2 | \
  326. tl-wr703n | \
  327. tl-wr710n | \
  328. tl-wr720n-v3 | \
  329. tl-wr741nd | \
  330. tl-wr741nd-v4 | \
  331. tl-wr841n-v1 | \
  332. tl-wa830re-v2 | \
  333. tl-wr841n-v7 | \
  334. tl-wr841n-v8 | \
  335. tl-wr841n-v9 | \
  336. tl-wr842n-v2 | \
  337. tl-wr941nd | \
  338. tl-wr941nd-v5 | \
  339. tl-wr941nd-v6 | \
  340. tl-wr1041n-v2 | \
  341. tl-wr1043nd | \
  342. tl-wr1043nd-v2 | \
  343. tl-wr2543n)
  344. local magic_ver="0100"
  345. case "$board" in
  346. tl-wdr6500-v2)
  347. magic_ver="0200"
  348. ;;
  349. esac
  350. [ "$magic" != "$magic_ver" ] && {
  351. echo "Invalid image type."
  352. return 1
  353. }
  354. local hwid
  355. local imageid
  356. hwid=$(tplink_get_hwid)
  357. imageid=$(tplink_get_image_hwid "$1")
  358. [ "$hwid" != "$imageid" ] && {
  359. echo "Invalid image, hardware ID mismatch, hw:$hwid image:$imageid."
  360. return 1
  361. }
  362. local boot_size
  363. boot_size=$(tplink_get_image_boot_size "$1")
  364. [ "$boot_size" != "00000000" ] && {
  365. echo "Invalid image, it contains a bootloader."
  366. return 1
  367. }
  368. return 0
  369. ;;
  370. tube2h)
  371. alfa_check_image "$1" && return 0
  372. return 1
  373. ;;
  374. nbg6616 | \
  375. unifi-outdoor-plus | \
  376. uap-pro)
  377. [ "$magic_long" != "19852003" ] && {
  378. echo "Invalid image type."
  379. return 1
  380. }
  381. return 0
  382. ;;
  383. wndr3700 | \
  384. wnr2000-v3 | \
  385. wnr612-v2 | \
  386. wnr1000-v2)
  387. local hw_magic
  388. hw_magic="$(ar71xx_get_mtd_part_magic firmware)"
  389. [ "$magic_long" != "$hw_magic" ] && {
  390. echo "Invalid image, hardware ID mismatch, hw:$hw_magic image:$magic_long."
  391. return 1
  392. }
  393. return 0
  394. ;;
  395. mr18)
  396. merakinand_do_platform_check $board $1
  397. return $?;
  398. ;;
  399. nbg6716 | \
  400. r6100 | \
  401. wndr3700v4 | \
  402. wndr4300 )
  403. nand_do_platform_check $board $1
  404. return $?;
  405. ;;
  406. routerstation | \
  407. routerstation-pro | \
  408. ls-sr71 | \
  409. pb42 | \
  410. pb44 | \
  411. all0305 | \
  412. eap300v2 | \
  413. eap7660d | \
  414. ja76pf | \
  415. ja76pf2 | \
  416. jwap003 | \
  417. wp543 | \
  418. wpe72)
  419. [ "$magic" != "4349" ] && {
  420. echo "Invalid image. Use *-sysupgrade.bin files on this board"
  421. return 1
  422. }
  423. local md5_img=$(dd if="$1" bs=2 skip=9 count=16 2>/dev/null)
  424. local md5_chk=$(dd if="$1" bs=$CI_BLKSZ skip=1 2>/dev/null | md5sum -); md5_chk="${md5_chk%% *}"
  425. if [ -n "$md5_img" -a -n "$md5_chk" ] && [ "$md5_img" = "$md5_chk" ]; then
  426. return 0
  427. else
  428. echo "Invalid image. Contents do not match checksum (image:$md5_img calculated:$md5_chk)"
  429. return 1
  430. fi
  431. return 0
  432. ;;
  433. wnr2000-v4)
  434. [ "$magic_long" != "32303034" ] && {
  435. echo "Invalid image type."
  436. return 1
  437. }
  438. return 0
  439. ;;
  440. esac
  441. echo "Sysupgrade is not yet supported on $board."
  442. return 1
  443. }
  444. platform_pre_upgrade() {
  445. local board=$(ar71xx_board_name)
  446. case "$board" in
  447. nbg6716 | \
  448. r6100 | \
  449. wndr3700v4 | \
  450. wndr4300 )
  451. nand_do_upgrade "$1"
  452. ;;
  453. mr18)
  454. merakinand_do_upgrade "$1"
  455. ;;
  456. esac
  457. }
  458. platform_do_upgrade() {
  459. local board=$(ar71xx_board_name)
  460. case "$board" in
  461. routerstation | \
  462. routerstation-pro | \
  463. ls-sr71 | \
  464. all0305 | \
  465. eap7660d | \
  466. pb42 | \
  467. pb44 | \
  468. ja76pf | \
  469. ja76pf2 | \
  470. jwap003)
  471. platform_do_upgrade_combined "$ARGV"
  472. ;;
  473. wp543|\
  474. wpe72)
  475. platform_do_upgrade_compex "$ARGV"
  476. ;;
  477. all0258n )
  478. platform_do_upgrade_allnet "0x9f050000" "$ARGV"
  479. ;;
  480. all0315n )
  481. platform_do_upgrade_allnet "0x9f080000" "$ARGV"
  482. ;;
  483. eap300v2 |\
  484. cap4200ag)
  485. platform_do_upgrade_allnet "0xbf0a0000" "$ARGV"
  486. ;;
  487. dir-825-b1 |\
  488. tew-673gru)
  489. platform_do_upgrade_dir825b "$ARGV"
  490. ;;
  491. mr1750 | \
  492. mr600 | \
  493. mr600v2 | \
  494. mr900 | \
  495. mr900v2 | \
  496. om2p | \
  497. om2pv2 | \
  498. om2p-hs | \
  499. om2p-hsv2 | \
  500. om2p-lc | \
  501. om5p | \
  502. om5p-an)
  503. platform_do_upgrade_openmesh "$ARGV"
  504. ;;
  505. unifi-outdoor-plus | \
  506. uap-pro)
  507. MTD_CONFIG_ARGS="-s 0x180000"
  508. default_do_upgrade "$ARGV"
  509. ;;
  510. *)
  511. default_do_upgrade "$ARGV"
  512. ;;
  513. esac
  514. }
  515. disable_watchdog() {
  516. killall watchdog
  517. ( ps | grep -v 'grep' | grep '/dev/watchdog' ) && {
  518. echo 'Could not disable watchdog'
  519. return 1
  520. }
  521. }
  522. append sysupgrade_pre_upgrade disable_watchdog