platform.sh 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. PART_NAME=firmware
  2. # $(1): file to read magic from
  3. # $(2): offset in bytes
  4. get_magic_long_at() {
  5. dd if="$1" skip=$2 bs=1 count=4 2>/dev/null | hexdump -v -e '1/1 "%02x"'
  6. }
  7. platform_machine() {
  8. cat /proc/device-tree/compatible | tr '\0' '\t' | cut -f 1
  9. }
  10. platform_flash_type() {
  11. # On NAND devices "rootfs" is UBI volume, so won't be find in /proc/mtd
  12. grep -q "\"rootfs\"" /proc/mtd && {
  13. echo "serial"
  14. return
  15. }
  16. echo "nand"
  17. }
  18. platform_expected_image() {
  19. local machine=$(platform_machine)
  20. case "$machine" in
  21. "dlink,dir-885l") echo "seama wrgac42_dlink.2015_dir885l"; return;;
  22. "netgear,r6250v1") echo "chk U12H245T00_NETGEAR"; return;;
  23. "netgear,r6300v2") echo "chk U12H240T00_NETGEAR"; return;;
  24. "netgear,r7000") echo "chk U12H270T00_NETGEAR"; return;;
  25. "netgear,r7900") echo "chk U12H315T30_NETGEAR"; return;;
  26. "netgear,r8000") echo "chk U12H315T00_NETGEAR"; return;;
  27. "netgear,r8500") echo "chk U12H334T00_NETGEAR"; return;;
  28. "tplink,archer-c9-v1") echo "safeloader"; return;;
  29. esac
  30. }
  31. platform_identify() {
  32. local magic
  33. magic=$(get_magic_long "$1")
  34. case "$magic" in
  35. "48445230")
  36. echo "trx"
  37. return
  38. ;;
  39. "2a23245e")
  40. echo "chk"
  41. return
  42. ;;
  43. "5ea3a417")
  44. echo "seama"
  45. return
  46. ;;
  47. esac
  48. magic=$(get_magic_long_at "$1" 14)
  49. [ "$magic" = "55324e44" ] && {
  50. echo "cybertan"
  51. return
  52. }
  53. if osafeloader info "$1" > /dev/null 2>&1; then
  54. echo "safeloader"
  55. return
  56. fi
  57. echo "unknown"
  58. }
  59. platform_check_image() {
  60. [ "$#" -gt 1 ] && return 1
  61. local file_type=$(platform_identify "$1")
  62. local magic
  63. local error=0
  64. case "$file_type" in
  65. "chk")
  66. local header_len=$((0x$(get_magic_long_at "$1" 4)))
  67. local board_id_len=$(($header_len - 40))
  68. local board_id=$(dd if="$1" skip=40 bs=1 count=$board_id_len 2>/dev/null | hexdump -v -e '1/1 "%c"')
  69. local dev_board_id=$(platform_expected_image)
  70. echo "Found CHK image with device board_id $board_id"
  71. [ -n "$dev_board_id" -a "chk $board_id" != "$dev_board_id" ] && {
  72. echo "Firmware board_id doesn't match device board_id ($dev_board_id)"
  73. error=1
  74. }
  75. if ! otrx check "$1" -o "$header_len"; then
  76. echo "No valid TRX firmware in the CHK image"
  77. error=1
  78. fi
  79. ;;
  80. "cybertan")
  81. local pattern=$(dd if="$1" bs=1 count=4 2>/dev/null | hexdump -v -e '1/1 "%c"')
  82. local dev_pattern=$(platform_expected_image)
  83. echo "Found CyberTAN image with device pattern: $pattern"
  84. [ -n "$dev_pattern" -a "cybertan $pattern" != "$dev_pattern" ] && {
  85. echo "Firmware pattern doesn't match device pattern ($dev_pattern)"
  86. error=1
  87. }
  88. if ! otrx check "$1" -o 32; then
  89. echo "No valid TRX firmware in the CyberTAN image"
  90. error=1
  91. fi
  92. ;;
  93. "safeloader")
  94. ;;
  95. "seama")
  96. local img_signature=$(oseama info "$1" | grep "Meta entry:.*signature=" | sed "s/.*=//")
  97. local dev_signature=$(platform_expected_image)
  98. echo "Found Seama image with device signature: $img_signature"
  99. [ -n "$dev_signature" -a "seama $img_signature" != "$dev_signature" ] && {
  100. echo "Firmware signature doesn't match device signature ($dev_signature)"
  101. error=1
  102. }
  103. $(oseama info "$1" -e 0 | grep -q "Meta entry:.*type=firmware") || {
  104. echo "Seama container doesn't have firmware entity"
  105. error=1
  106. }
  107. ;;
  108. "trx")
  109. local expected=$(platform_expected_image)
  110. [ "$expected" == "safeloader" ] && {
  111. echo "This device expects SafeLoader format and may not work with TRX"
  112. error=1
  113. }
  114. if ! otrx check "$1"; then
  115. echo "Invalid (corrupted?) TRX firmware"
  116. error=1
  117. fi
  118. ;;
  119. *)
  120. echo "Invalid image type. Please use only .trx files"
  121. error=1
  122. ;;
  123. esac
  124. return $error
  125. }
  126. # $(1): image for upgrade (with possible extra header)
  127. # $(2): offset of trx in image
  128. platform_pre_upgrade_trx() {
  129. local dir="/tmp/sysupgrade-bcm53xx"
  130. local trx="$1"
  131. local offset="$2"
  132. # Extract partitions from trx
  133. rm -fR $dir
  134. mkdir -p $dir
  135. otrx extract "$trx" \
  136. ${offset:+-o $offset} \
  137. -1 $dir/kernel \
  138. -2 $dir/root
  139. [ $? -ne 0 ] && {
  140. echo "Failed to extract TRX partitions."
  141. return
  142. }
  143. # Firmwares without UBI image should be flashed "normally"
  144. local root_type=$(identify $dir/root)
  145. [ "$root_type" != "ubi" ] && {
  146. echo "Provided firmware doesn't use UBI for rootfs."
  147. return
  148. }
  149. # Prepare TRX file with just a kernel that will replace current one
  150. local linux_length=$(grep "\"linux\"" /proc/mtd | sed "s/mtd[0-9]*:[ \t]*\([^ \t]*\).*/\1/")
  151. [ -z "$linux_length" ] && {
  152. echo "Unable to find \"linux\" partition size"
  153. exit 1
  154. }
  155. linux_length=$((0x$linux_length))
  156. local kernel_length=$(wc -c $dir/kernel | cut -d ' ' -f 1)
  157. [ $kernel_length -gt $linux_length ] && {
  158. echo "New kernel doesn't fit \"linux\" partition."
  159. return
  160. }
  161. rm -f /tmp/null.bin
  162. rm -f /tmp/kernel.trx
  163. touch /tmp/null.bin
  164. otrx create /tmp/kernel.trx \
  165. -f $dir/kernel -b $(($linux_length + 28)) \
  166. -f /tmp/null.bin
  167. [ $? -ne 0 ] && {
  168. echo "Failed to create simple TRX with new kernel."
  169. return
  170. }
  171. # Prepare UBI image (drop unwanted extra blocks)
  172. local ubi_length=0
  173. while [ "$(dd if=$dir/root skip=$ubi_length bs=1 count=4 2>/dev/null)" = "UBI#" ]; do
  174. ubi_length=$(($ubi_length + 131072))
  175. done
  176. dd if=$dir/root of=/tmp/root.ubi bs=131072 count=$((ubi_length / 131072)) 2>/dev/null
  177. [ $? -ne 0 ] && {
  178. echo "Failed to prepare new UBI image."
  179. return
  180. }
  181. # Flash
  182. mtd write /tmp/kernel.trx firmware
  183. nand_do_upgrade /tmp/root.ubi
  184. }
  185. platform_pre_upgrade_seama() {
  186. local dir="/tmp/sysupgrade-bcm53xx"
  187. local seama="$1"
  188. local tmp
  189. # Extract Seama entity from Seama seal
  190. rm -fR $dir
  191. mkdir -p $dir
  192. oseama extract "$seama" \
  193. -e 0 \
  194. -o $dir/seama.entity
  195. [ $? -ne 0 ] && {
  196. echo "Failed to extract Seama entity."
  197. return
  198. }
  199. local entity_size=$(wc -c $dir/seama.entity | cut -d ' ' -f 1)
  200. local ubi_offset=0
  201. tmp=0
  202. while [ 1 ]; do
  203. [ $tmp -ge $entity_size ] && break
  204. [ "$(dd if=$dir/seama.entity skip=$tmp bs=1 count=4 2>/dev/null)" = "UBI#" ] && {
  205. ubi_offset=$tmp
  206. break
  207. }
  208. tmp=$(($tmp + 131072))
  209. done
  210. [ $ubi_offset -eq 0 ] && {
  211. echo "Failed to find UBI in Seama entity."
  212. return
  213. }
  214. local ubi_length=0
  215. while [ "$(dd if=$dir/seama.entity skip=$(($ubi_offset + $ubi_length)) bs=1 count=4 2>/dev/null)" = "UBI#" ]; do
  216. ubi_length=$(($ubi_length + 131072))
  217. done
  218. dd if=$dir/seama.entity of=$dir/kernel.seama bs=131072 count=$(($ubi_offset / 131072)) 2>/dev/null
  219. dd if=$dir/seama.entity of=$dir/root.ubi bs=131072 skip=$(($ubi_offset / 131072)) count=$(($ubi_length / 131072)) 2>/dev/null
  220. # Flash
  221. local kernel_size=$(sed -n 's/mtd[0-9]*: \([0-9a-f]*\).*"\(kernel\|linux\)".*/\1/p' /proc/mtd)
  222. mtd write $dir/kernel.seama firmware
  223. mtd ${kernel_size:+-c 0x$kernel_size} fixseama firmware
  224. nand_do_upgrade $dir/root.ubi
  225. }
  226. platform_pre_upgrade() {
  227. export RAMFS_COPY_BIN="${RAMFS_COPY_BIN} /usr/bin/osafeloader /usr/bin/oseama /bin/sed"
  228. local file_type=$(platform_identify "$1")
  229. [ "$(platform_flash_type)" != "nand" ] && return
  230. # Find trx offset
  231. case "$file_type" in
  232. "chk") platform_pre_upgrade_trx "$1" $((0x$(get_magic_long_at "$1" 4)));;
  233. "cybertan") platform_pre_upgrade_trx "$1" 32;;
  234. "seama") platform_pre_upgrade_seama "$1";;
  235. "trx") platform_pre_upgrade_trx "$1";;
  236. esac
  237. }
  238. platform_trx_from_chk_cmd() {
  239. local header_len=$((0x$(get_magic_long_at "$1" 4)))
  240. echo -n dd bs=$header_len skip=1
  241. }
  242. platform_trx_from_cybertan_cmd() {
  243. echo -n dd bs=32 skip=1
  244. }
  245. platform_img_from_safeloader() {
  246. local dir="/tmp/sysupgrade-bcm53xx"
  247. # Extract partitions from SafeLoader
  248. rm -fR $dir
  249. mkdir -p $dir
  250. osafeloader extract "$1" \
  251. -p "os-image" \
  252. -o $dir/os-image
  253. osafeloader extract "$1" \
  254. -p "file-system" \
  255. -o $dir/file-system
  256. mtd write $dir/file-system rootfs
  257. echo -n $dir/os-image
  258. }
  259. platform_img_from_seama() {
  260. local dir="/tmp/sysupgrade-bcm53xx"
  261. local offset=$(oseama info "$1" -e 0 | grep "Entity offset:" | sed "s/.*:\s*//")
  262. local size=$(oseama info "$1" -e 0 | grep "Entity size:" | sed "s/.*:\s*//")
  263. # Busybox doesn't support required iflag-s
  264. # echo -n dd iflag=skip_bytes,count_bytes skip=$offset count=$size
  265. rm -fR $dir
  266. mkdir -p $dir
  267. dd if="$1" of=$dir/image-noheader.bin bs=$offset skip=1
  268. dd if=$dir/image-noheader.bin of=$dir/image-entity.bin bs=$size count=1
  269. echo -n $dir/image-entity.bin
  270. }
  271. platform_do_upgrade() {
  272. local file_type=$(platform_identify "$1")
  273. local trx="$1"
  274. local cmd=
  275. [ "$(platform_flash_type)" == "nand" ] && {
  276. echo "Writing whole image to NAND flash. All erase counters will be lost."
  277. }
  278. case "$file_type" in
  279. "chk") cmd=$(platform_trx_from_chk_cmd "$trx");;
  280. "cybertan") cmd=$(platform_trx_from_cybertan_cmd "$trx");;
  281. "safeloader") trx=$(platform_img_from_safeloader "$trx");;
  282. "seama") trx=$(platform_img_from_seama "$trx");;
  283. esac
  284. default_do_upgrade "$trx" "$cmd"
  285. }