platform.sh 12 KB

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