platform.sh 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  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. anonabox-pro | \
  155. ap113 | \
  156. ap121 | \
  157. ap121-mini | \
  158. ap136-010 | \
  159. ap136-020 | \
  160. ap135-020 | \
  161. ap147-010 | \
  162. ap152 | \
  163. ap96 | \
  164. bxu2000n-2-a1 | \
  165. db120 | \
  166. dr344 | \
  167. dw33d |\
  168. f9k1115v2 |\
  169. hornet-ub | \
  170. mr12 | \
  171. mr16 | \
  172. wpj558 | \
  173. zcn-1523h-2 | \
  174. zcn-1523h-5)
  175. [ "$magic_long" != "68737173" -a "$magic_long" != "19852003" ] && {
  176. echo "Invalid image type."
  177. return 1
  178. }
  179. return 0
  180. ;;
  181. ap81 | \
  182. ap83 | \
  183. ap132 | \
  184. c-55 | \
  185. cf-e316n-v2 | \
  186. dgl-5500-a1 |\
  187. dhp-1565-a1 |\
  188. dir-505-a1 | \
  189. dir-600-a1 | \
  190. dir-615-c1 | \
  191. dir-615-e1 | \
  192. dir-615-e4 | \
  193. dir-615-i1 | \
  194. dir-825-c1 | \
  195. dir-835-a1 | \
  196. dlan-hotspot | \
  197. dlan-pro-500-wp | \
  198. dlan-pro-1200-ac | \
  199. dragino2 | \
  200. epg5000 | \
  201. esr1750 | \
  202. esr900 | \
  203. ew-dorin | \
  204. ew-dorin-router | \
  205. gl-ar150 | \
  206. gl-ar300 | \
  207. gl-domino | \
  208. hiwifi-hc6361 | \
  209. hornet-ub-x2 | \
  210. mzk-w04nu | \
  211. mzk-w300nh | \
  212. tew-632brp | \
  213. tew-712br | \
  214. tew-732br | \
  215. tew-823dru | \
  216. wrt400n | \
  217. airgateway | \
  218. airgatewaypro | \
  219. airrouter | \
  220. bullet-m | \
  221. loco-m-xw | \
  222. nanostation-m | \
  223. rocket-m | \
  224. rocket-m-xw | \
  225. rocket-m-ti | \
  226. nanostation-m-xw | \
  227. rw2458n | \
  228. wpj531 | \
  229. wndap360 | \
  230. wpj342 | \
  231. wpj344 | \
  232. wzr-hp-g300nh2 | \
  233. wzr-hp-g300nh | \
  234. wzr-hp-g450h | \
  235. wzr-hp-ag300h | \
  236. wzr-450hp2 | \
  237. whr-g301n | \
  238. whr-hp-g300n | \
  239. whr-hp-gn | \
  240. wlae-ag300n | \
  241. nbg460n_550n_550nh | \
  242. unifi | \
  243. unifiac-lite | \
  244. unifiac-pro | \
  245. unifi-outdoor | \
  246. carambola2 | \
  247. weio | \
  248. wrtnode2q)
  249. [ "$magic" != "2705" ] && {
  250. echo "Invalid image type."
  251. return 1
  252. }
  253. return 0
  254. ;;
  255. cpe210|\
  256. cpe510)
  257. tplink_pharos_check_image "$1" && return 0
  258. return 1
  259. ;;
  260. bsb | \
  261. dir-825-b1 | \
  262. tew-673gru)
  263. dir825b_check_image "$1" && return 0
  264. ;;
  265. mynet-rext|\
  266. e2100l|\
  267. wrt160nl)
  268. cybertan_check_image "$1" && return 0
  269. return 1
  270. ;;
  271. qihoo-c301 | \
  272. mynet-n600 | \
  273. mynet-n750)
  274. [ "$magic_long" != "5ea3a417" ] && {
  275. echo "Invalid image, bad magic: $magic_long"
  276. return 1
  277. }
  278. local typemagic=$(seama_get_type_magic "$1")
  279. [ "$typemagic" != "6669726d" ] && {
  280. echo "Invalid image, bad type: $typemagic"
  281. return 1
  282. }
  283. return 0;
  284. ;;
  285. mr1750 | \
  286. mr1750v2 | \
  287. mr600 | \
  288. mr600v2 | \
  289. mr900 | \
  290. mr900v2 | \
  291. om2p | \
  292. om2pv2 | \
  293. om2p-hs | \
  294. om2p-hsv2 | \
  295. om2p-hsv3 | \
  296. om2p-lc | \
  297. om5p | \
  298. om5p-an | \
  299. om5p-ac | \
  300. om5p-acv2)
  301. platform_check_image_openmesh "$magic_long" "$1" && return 0
  302. return 1
  303. ;;
  304. antminer-s1 | \
  305. antminer-s3 | \
  306. antrouter-r1 | \
  307. archer-c5 | \
  308. archer-c7 | \
  309. el-m150 | \
  310. el-mini | \
  311. gl-inet | \
  312. mc-mac1200r | \
  313. minibox-v1 |\
  314. omy-g1 |\
  315. omy-x1 |\
  316. onion-omega | \
  317. oolite | \
  318. smart-300 | \
  319. som9331 | \
  320. tellstick-znet-lite | \
  321. tl-mr10u | \
  322. tl-mr11u | \
  323. tl-mr12u | \
  324. tl-mr13u | \
  325. tl-mr3020 | \
  326. tl-mr3040 | \
  327. tl-mr3040-v2 | \
  328. tl-mr3220 | \
  329. tl-mr3220-v2 | \
  330. tl-mr3420 | \
  331. tl-mr3420-v2 | \
  332. tl-wa701nd-v2 | \
  333. tl-wa7210n-v2 | \
  334. tl-wa7510n | \
  335. tl-wa750re | \
  336. tl-wa850re | \
  337. tl-wa860re | \
  338. tl-wa801nd-v2 | \
  339. tl-wa901nd | \
  340. tl-wa901nd-v2 | \
  341. tl-wa901nd-v3 | \
  342. tl-wa901nd-v4 | \
  343. tl-wdr3320-v2 | \
  344. tl-wdr3500 | \
  345. tl-wdr4300 | \
  346. tl-wdr4900-v2 | \
  347. tl-wdr6500-v2 | \
  348. tl-wr703n | \
  349. tl-wr710n | \
  350. tl-wr720n-v3 | \
  351. tl-wr741nd | \
  352. tl-wr741nd-v4 | \
  353. tl-wr810n | \
  354. tl-wr841n-v1 | \
  355. tl-wa830re-v2 | \
  356. tl-wr841n-v7 | \
  357. tl-wr841n-v8 | \
  358. tl-wr841n-v9 | \
  359. tl-wr841n-v11 | \
  360. tl-wr842n-v2 | \
  361. tl-wr842n-v3 | \
  362. tl-wr941nd | \
  363. tl-wr941nd-v5 | \
  364. tl-wr941nd-v6 | \
  365. tl-wr1041n-v2 | \
  366. tl-wr1043nd | \
  367. tl-wr1043nd-v2 | \
  368. tl-wr1043nd-v4| \
  369. tl-wr2543n)
  370. local magic_ver="0100"
  371. case "$board" in
  372. tl-wdr6500-v2)
  373. magic_ver="0200"
  374. ;;
  375. esac
  376. [ "$magic" != "$magic_ver" ] && {
  377. echo "Invalid image type."
  378. return 1
  379. }
  380. local hwid
  381. local mid
  382. local imagehwid
  383. local imagemid
  384. hwid=$(tplink_get_hwid)
  385. mid=$(tplink_get_mid)
  386. imagehwid=$(tplink_get_image_hwid "$1")
  387. imagemid=$(tplink_get_image_mid "$1")
  388. [ "$hwid" != "$imagehwid" -o "$mid" != "$imagemid" ] && {
  389. echo "Invalid image, hardware ID mismatch, hw:$hwid $mid image:$imagehwid $imagemid."
  390. return 1
  391. }
  392. local boot_size
  393. boot_size=$(tplink_get_image_boot_size "$1")
  394. [ "$boot_size" != "00000000" ] && {
  395. echo "Invalid image, it contains a bootloader."
  396. return 1
  397. }
  398. return 0
  399. ;;
  400. tube2h)
  401. alfa_check_image "$1" && return 0
  402. return 1
  403. ;;
  404. nbg6616 | \
  405. unifi-outdoor-plus | \
  406. uap-pro)
  407. [ "$magic_long" != "19852003" ] && {
  408. echo "Invalid image type."
  409. return 1
  410. }
  411. return 0
  412. ;;
  413. wndr3700 | \
  414. wnr2000-v3 | \
  415. wnr612-v2 | \
  416. wnr1000-v2 | \
  417. wpn824n)
  418. local hw_magic
  419. hw_magic="$(ar71xx_get_mtd_part_magic firmware)"
  420. [ "$magic_long" != "$hw_magic" ] && {
  421. echo "Invalid image, hardware ID mismatch, hw:$hw_magic image:$magic_long."
  422. return 1
  423. }
  424. return 0
  425. ;;
  426. mr18)
  427. merakinand_do_platform_check $board $1
  428. return $?;
  429. ;;
  430. nbg6716 | \
  431. r6100 | \
  432. wndr3700v4 | \
  433. wndr4300 )
  434. nand_do_platform_check $board $1
  435. return $?;
  436. ;;
  437. routerstation | \
  438. routerstation-pro | \
  439. ls-sr71 | \
  440. pb42 | \
  441. pb44 | \
  442. all0305 | \
  443. eap300v2 | \
  444. eap7660d | \
  445. ja76pf | \
  446. ja76pf2 | \
  447. jwap003 | \
  448. wp543 | \
  449. wpe72)
  450. [ "$magic" != "4349" ] && {
  451. echo "Invalid image. Use *-sysupgrade.bin files on this board"
  452. return 1
  453. }
  454. local md5_img=$(dd if="$1" bs=2 skip=9 count=16 2>/dev/null)
  455. local md5_chk=$(dd if="$1" bs=$CI_BLKSZ skip=1 2>/dev/null | md5sum -); md5_chk="${md5_chk%% *}"
  456. if [ -n "$md5_img" -a -n "$md5_chk" ] && [ "$md5_img" = "$md5_chk" ]; then
  457. return 0
  458. else
  459. echo "Invalid image. Contents do not match checksum (image:$md5_img calculated:$md5_chk)"
  460. return 1
  461. fi
  462. return 0
  463. ;;
  464. wnr2000-v4)
  465. [ "$magic_long" != "32303034" ] && {
  466. echo "Invalid image type."
  467. return 1
  468. }
  469. return 0
  470. ;;
  471. wnr2200)
  472. [ "$magic_long" != "32323030" ] && {
  473. echo "Invalid image type."
  474. return 1
  475. }
  476. return 0
  477. ;;
  478. esac
  479. echo "Sysupgrade is not yet supported on $board."
  480. return 1
  481. }
  482. platform_pre_upgrade() {
  483. local board=$(ar71xx_board_name)
  484. case "$board" in
  485. nbg6716 | \
  486. r6100 | \
  487. wndr3700v4 | \
  488. wndr4300 )
  489. nand_do_upgrade "$1"
  490. ;;
  491. mr18)
  492. merakinand_do_upgrade "$1"
  493. ;;
  494. esac
  495. }
  496. platform_do_upgrade() {
  497. local board=$(ar71xx_board_name)
  498. case "$board" in
  499. routerstation | \
  500. routerstation-pro | \
  501. ls-sr71 | \
  502. all0305 | \
  503. eap7660d | \
  504. pb42 | \
  505. pb44 | \
  506. ja76pf | \
  507. ja76pf2 | \
  508. jwap003)
  509. platform_do_upgrade_combined "$ARGV"
  510. ;;
  511. wp543|\
  512. wpe72)
  513. platform_do_upgrade_compex "$ARGV"
  514. ;;
  515. all0258n )
  516. platform_do_upgrade_allnet "0x9f050000" "$ARGV"
  517. ;;
  518. all0315n )
  519. platform_do_upgrade_allnet "0x9f080000" "$ARGV"
  520. ;;
  521. eap300v2 |\
  522. cap4200ag)
  523. platform_do_upgrade_allnet "0xbf0a0000" "$ARGV"
  524. ;;
  525. dir-825-b1 |\
  526. tew-673gru)
  527. platform_do_upgrade_dir825b "$ARGV"
  528. ;;
  529. mr1750 | \
  530. mr1750v2 | \
  531. mr600 | \
  532. mr600v2 | \
  533. mr900 | \
  534. mr900v2 | \
  535. om2p | \
  536. om2pv2 | \
  537. om2p-hs | \
  538. om2p-hsv2 | \
  539. om2p-hsv3 | \
  540. om2p-lc | \
  541. om5p | \
  542. om5p-an | \
  543. om5p-ac | \
  544. om5p-acv2)
  545. platform_do_upgrade_openmesh "$ARGV"
  546. ;;
  547. unifi-outdoor-plus | \
  548. uap-pro)
  549. MTD_CONFIG_ARGS="-s 0x180000"
  550. default_do_upgrade "$ARGV"
  551. ;;
  552. *)
  553. default_do_upgrade "$ARGV"
  554. ;;
  555. esac
  556. }
  557. disable_watchdog() {
  558. killall watchdog
  559. ( ps | grep -v 'grep' | grep '/dev/watchdog' ) && {
  560. echo 'Could not disable watchdog'
  561. return 1
  562. }
  563. }
  564. append sysupgrade_pre_upgrade disable_watchdog