platform.sh 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  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-lite | \
  242. unifiac-pro | \
  243. unifi-outdoor | \
  244. carambola2 | \
  245. weio | \
  246. wrtnode2q)
  247. [ "$magic" != "2705" ] && {
  248. echo "Invalid image type."
  249. return 1
  250. }
  251. return 0
  252. ;;
  253. cpe210|\
  254. cpe510)
  255. tplink_pharos_check_image "$1" && return 0
  256. return 1
  257. ;;
  258. bsb | \
  259. dir-825-b1 | \
  260. tew-673gru)
  261. dir825b_check_image "$1" && return 0
  262. ;;
  263. mynet-rext|\
  264. wrt160nl)
  265. cybertan_check_image "$1" && return 0
  266. return 1
  267. ;;
  268. qihoo-c301 | \
  269. mynet-n600 | \
  270. mynet-n750)
  271. [ "$magic_long" != "5ea3a417" ] && {
  272. echo "Invalid image, bad magic: $magic_long"
  273. return 1
  274. }
  275. local typemagic=$(seama_get_type_magic "$1")
  276. [ "$typemagic" != "6669726d" ] && {
  277. echo "Invalid image, bad type: $typemagic"
  278. return 1
  279. }
  280. return 0;
  281. ;;
  282. mr1750 | \
  283. mr1750v2 | \
  284. mr600 | \
  285. mr600v2 | \
  286. mr900 | \
  287. mr900v2 | \
  288. om2p | \
  289. om2pv2 | \
  290. om2p-hs | \
  291. om2p-hsv2 | \
  292. om2p-hsv3 | \
  293. om2p-lc | \
  294. om5p | \
  295. om5p-an | \
  296. om5p-ac | \
  297. om5p-acv2)
  298. platform_check_image_openmesh "$magic_long" "$1" && return 0
  299. return 1
  300. ;;
  301. antminer-s1 | \
  302. antminer-s3 | \
  303. antrouter-r1 | \
  304. archer-c5 | \
  305. archer-c7 | \
  306. el-m150 | \
  307. el-mini | \
  308. gl-inet | \
  309. mc-mac1200r | \
  310. minibox-v1 |\
  311. omy-g1 |\
  312. omy-x1 |\
  313. onion-omega | \
  314. oolite | \
  315. smart-300 | \
  316. som9331 | \
  317. tellstick-znet-lite | \
  318. tl-mr10u | \
  319. tl-mr11u | \
  320. tl-mr12u | \
  321. tl-mr13u | \
  322. tl-mr3020 | \
  323. tl-mr3040 | \
  324. tl-mr3040-v2 | \
  325. tl-mr3220 | \
  326. tl-mr3220-v2 | \
  327. tl-mr3420 | \
  328. tl-mr3420-v2 | \
  329. tl-wa701nd-v2 | \
  330. tl-wa7210n-v2 | \
  331. tl-wa7510n | \
  332. tl-wa750re | \
  333. tl-wa850re | \
  334. tl-wa860re | \
  335. tl-wa801nd-v2 | \
  336. tl-wa901nd | \
  337. tl-wa901nd-v2 | \
  338. tl-wa901nd-v3 | \
  339. tl-wa901nd-v4 | \
  340. tl-wdr3320-v2 | \
  341. tl-wdr3500 | \
  342. tl-wdr4300 | \
  343. tl-wdr4900-v2 | \
  344. tl-wdr6500-v2 | \
  345. tl-wr703n | \
  346. tl-wr710n | \
  347. tl-wr720n-v3 | \
  348. tl-wr741nd | \
  349. tl-wr741nd-v4 | \
  350. tl-wr810n | \
  351. tl-wr841n-v1 | \
  352. tl-wa830re-v2 | \
  353. tl-wr841n-v7 | \
  354. tl-wr841n-v8 | \
  355. tl-wr841n-v9 | \
  356. tl-wr841n-v11 | \
  357. tl-wr842n-v2 | \
  358. tl-wr842n-v3 | \
  359. tl-wr941nd | \
  360. tl-wr941nd-v5 | \
  361. tl-wr941nd-v6 | \
  362. tl-wr1041n-v2 | \
  363. tl-wr1043nd | \
  364. tl-wr1043nd-v2 | \
  365. tl-wr2543n)
  366. local magic_ver="0100"
  367. case "$board" in
  368. tl-wdr6500-v2)
  369. magic_ver="0200"
  370. ;;
  371. esac
  372. [ "$magic" != "$magic_ver" ] && {
  373. echo "Invalid image type."
  374. return 1
  375. }
  376. local hwid
  377. local mid
  378. local imagehwid
  379. local imagemid
  380. hwid=$(tplink_get_hwid)
  381. mid=$(tplink_get_mid)
  382. imagehwid=$(tplink_get_image_hwid "$1")
  383. imagemid=$(tplink_get_image_mid "$1")
  384. [ "$hwid" != "$imagehwid" -o "$mid" != "$imagemid" ] && {
  385. echo "Invalid image, hardware ID mismatch, hw:$hwid $mid image:$imagehwid $imagemid."
  386. return 1
  387. }
  388. local boot_size
  389. boot_size=$(tplink_get_image_boot_size "$1")
  390. [ "$boot_size" != "00000000" ] && {
  391. echo "Invalid image, it contains a bootloader."
  392. return 1
  393. }
  394. return 0
  395. ;;
  396. tube2h)
  397. alfa_check_image "$1" && return 0
  398. return 1
  399. ;;
  400. nbg6616 | \
  401. unifi-outdoor-plus | \
  402. uap-pro)
  403. [ "$magic_long" != "19852003" ] && {
  404. echo "Invalid image type."
  405. return 1
  406. }
  407. return 0
  408. ;;
  409. wndr3700 | \
  410. wnr2000-v3 | \
  411. wnr612-v2 | \
  412. wnr1000-v2 | \
  413. wpn824n)
  414. local hw_magic
  415. hw_magic="$(ar71xx_get_mtd_part_magic firmware)"
  416. [ "$magic_long" != "$hw_magic" ] && {
  417. echo "Invalid image, hardware ID mismatch, hw:$hw_magic image:$magic_long."
  418. return 1
  419. }
  420. return 0
  421. ;;
  422. mr18)
  423. merakinand_do_platform_check $board $1
  424. return $?;
  425. ;;
  426. nbg6716 | \
  427. r6100 | \
  428. wndr3700v4 | \
  429. wndr4300 )
  430. nand_do_platform_check $board $1
  431. return $?;
  432. ;;
  433. routerstation | \
  434. routerstation-pro | \
  435. ls-sr71 | \
  436. pb42 | \
  437. pb44 | \
  438. all0305 | \
  439. eap300v2 | \
  440. eap7660d | \
  441. ja76pf | \
  442. ja76pf2 | \
  443. jwap003 | \
  444. wp543 | \
  445. wpe72)
  446. [ "$magic" != "4349" ] && {
  447. echo "Invalid image. Use *-sysupgrade.bin files on this board"
  448. return 1
  449. }
  450. local md5_img=$(dd if="$1" bs=2 skip=9 count=16 2>/dev/null)
  451. local md5_chk=$(dd if="$1" bs=$CI_BLKSZ skip=1 2>/dev/null | md5sum -); md5_chk="${md5_chk%% *}"
  452. if [ -n "$md5_img" -a -n "$md5_chk" ] && [ "$md5_img" = "$md5_chk" ]; then
  453. return 0
  454. else
  455. echo "Invalid image. Contents do not match checksum (image:$md5_img calculated:$md5_chk)"
  456. return 1
  457. fi
  458. return 0
  459. ;;
  460. wnr2000-v4)
  461. [ "$magic_long" != "32303034" ] && {
  462. echo "Invalid image type."
  463. return 1
  464. }
  465. return 0
  466. ;;
  467. wnr2200)
  468. [ "$magic_long" != "32323030" ] && {
  469. echo "Invalid image type."
  470. return 1
  471. }
  472. return 0
  473. ;;
  474. esac
  475. echo "Sysupgrade is not yet supported on $board."
  476. return 1
  477. }
  478. platform_pre_upgrade() {
  479. local board=$(ar71xx_board_name)
  480. case "$board" in
  481. nbg6716 | \
  482. r6100 | \
  483. wndr3700v4 | \
  484. wndr4300 )
  485. nand_do_upgrade "$1"
  486. ;;
  487. mr18)
  488. merakinand_do_upgrade "$1"
  489. ;;
  490. esac
  491. }
  492. platform_do_upgrade() {
  493. local board=$(ar71xx_board_name)
  494. case "$board" in
  495. routerstation | \
  496. routerstation-pro | \
  497. ls-sr71 | \
  498. all0305 | \
  499. eap7660d | \
  500. pb42 | \
  501. pb44 | \
  502. ja76pf | \
  503. ja76pf2 | \
  504. jwap003)
  505. platform_do_upgrade_combined "$ARGV"
  506. ;;
  507. wp543|\
  508. wpe72)
  509. platform_do_upgrade_compex "$ARGV"
  510. ;;
  511. all0258n )
  512. platform_do_upgrade_allnet "0x9f050000" "$ARGV"
  513. ;;
  514. all0315n )
  515. platform_do_upgrade_allnet "0x9f080000" "$ARGV"
  516. ;;
  517. eap300v2 |\
  518. cap4200ag)
  519. platform_do_upgrade_allnet "0xbf0a0000" "$ARGV"
  520. ;;
  521. dir-825-b1 |\
  522. tew-673gru)
  523. platform_do_upgrade_dir825b "$ARGV"
  524. ;;
  525. mr1750 | \
  526. mr1750v2 | \
  527. mr600 | \
  528. mr600v2 | \
  529. mr900 | \
  530. mr900v2 | \
  531. om2p | \
  532. om2pv2 | \
  533. om2p-hs | \
  534. om2p-hsv2 | \
  535. om2p-hsv3 | \
  536. om2p-lc | \
  537. om5p | \
  538. om5p-an | \
  539. om5p-ac | \
  540. om5p-acv2)
  541. platform_do_upgrade_openmesh "$ARGV"
  542. ;;
  543. unifi-outdoor-plus | \
  544. uap-pro)
  545. MTD_CONFIG_ARGS="-s 0x180000"
  546. default_do_upgrade "$ARGV"
  547. ;;
  548. *)
  549. default_do_upgrade "$ARGV"
  550. ;;
  551. esac
  552. }
  553. disable_watchdog() {
  554. killall watchdog
  555. ( ps | grep -v 'grep' | grep '/dev/watchdog' ) && {
  556. echo 'Could not disable watchdog'
  557. return 1
  558. }
  559. }
  560. append sysupgrade_pre_upgrade disable_watchdog