platform.sh 12 KB

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