platform.sh 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. CI_BLKSZ=65536
  2. CI_LDADR=0x80041000
  3. platform_find_partitions() {
  4. local first dev size erasesize name
  5. while read dev size erasesize name; do
  6. name=${name#'"'}; name=${name%'"'}
  7. case "$name" in
  8. vmlinux.bin.l7|kernel|linux|rootfs)
  9. if [ -z "$first" ]; then
  10. first="$name"
  11. else
  12. echo "$erasesize:$first:$name"
  13. break
  14. fi
  15. ;;
  16. esac
  17. done < /proc/mtd
  18. }
  19. platform_find_kernelpart() {
  20. local part
  21. for part in "${1%:*}" "${1#*:}"; do
  22. case "$part" in
  23. vmlinux.bin.l7|kernel|linux)
  24. echo "$part"
  25. break
  26. ;;
  27. esac
  28. done
  29. }
  30. platform_check_image() {
  31. [ "$#" -gt 1 ] && return 1
  32. case "$(get_magic_word "$1")" in
  33. # Combined Image
  34. 4349)
  35. local md5_img=$(dd if="$1" bs=2 skip=9 count=16 2>/dev/null)
  36. local md5_chk=$(dd if="$1" bs=$CI_BLKSZ skip=1 2>/dev/null | md5sum -); md5_chk="${md5_chk%% *}"
  37. if [ -n "$md5_img" -a -n "$md5_chk" ] && [ "$md5_img" = "$md5_chk" ]; then
  38. return 0
  39. else
  40. echo "Invalid image. Contents do not match checksum (image:$md5_img calculated:$md5_chk)"
  41. return 1
  42. fi
  43. ;;
  44. *)
  45. echo "Invalid image. Use combined .img files on this platform"
  46. return 1
  47. ;;
  48. esac
  49. }
  50. platform_do_upgrade() {
  51. local partitions=$(platform_find_partitions)
  52. local kernelpart=$(platform_find_kernelpart "${partitions#*:}")
  53. local erase_size=$((0x${partitions%%:*})); partitions="${partitions#*:}"
  54. local kern_length=0x$(dd if="$1" bs=2 skip=1 count=4 2>/dev/null)
  55. local kern_blocks=$(($kern_length / $CI_BLKSZ))
  56. local root_blocks=$((0x$(dd if="$1" bs=2 skip=5 count=4 2>/dev/null) / $CI_BLKSZ))
  57. if [ -n "$partitions" ] && [ -n "$kernelpart" ] && \
  58. [ ${kern_blocks:-0} -gt 0 ] && \
  59. [ ${root_blocks:-0} -gt ${kern_blocks:-0} ] && \
  60. [ ${erase_size:-0} -gt 0 ];
  61. then
  62. local append=""
  63. [ -f "$CONF_TAR" -a "$SAVE_CONFIG" -eq 1 ] && append="-j $CONF_TAR"
  64. ( dd if="$1" bs=$CI_BLKSZ skip=1 count=$kern_blocks 2>/dev/null; \
  65. dd if="$1" bs=$CI_BLKSZ skip=$((1+$kern_blocks)) count=$root_blocks 2>/dev/null ) | \
  66. mtd -r $append -F$kernelpart:$kern_length:$CI_LDADR,rootfs write - $partitions
  67. fi
  68. }