platform.sh 11 KB

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