merakinand.sh 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #!/bin/sh
  2. #
  3. # Copyright (C) 2015 Chris Blake <chrisrblake93@gmail.com>
  4. #
  5. # Custom upgrade script for Meraki NAND devices (ex. MR18)
  6. # Based on dir825.sh and stock nand functions
  7. #
  8. . /lib/ar71xx.sh
  9. . /lib/functions.sh
  10. get_magic_at() {
  11. local mtddev=$1
  12. local pos=$2
  13. dd bs=1 count=2 skip=$pos if=$mtddev 2>/dev/null | hexdump -v -n 4 -e '1/1 "%02x"'
  14. }
  15. mr18_is_caldata_valid() {
  16. local mtddev=$1
  17. local magic
  18. magic=$(get_magic_at $mtddev 4096)
  19. [ "$magic" != "0202" ] && return 0
  20. magic=$(get_magic_at $mtddev 20480)
  21. [ "$magic" != "0202" ] && return 0
  22. magic=$(get_magic_at $mtddev 36864)
  23. [ "$magic" != "0202" ] && return 0
  24. return 1
  25. }
  26. merakinand_copy_caldata() {
  27. local cal_src=$1
  28. local cal_dst=$2
  29. local ubidev=$( nand_find_ubi $CI_UBIPART )
  30. local board_name="$(cat /tmp/sysinfo/board_name)"
  31. local rootfs_size="$(ubinfo /dev/ubi0 -N rootfs_data | grep "Size" | awk '{ print $6 }')"
  32. # Setup partitions using board name, in case of future platforms
  33. case "$board_name" in
  34. "mr18")
  35. # Src is MTD
  36. mtd_src=$(find_mtd_chardev $cal_src)
  37. [ -n "$mtd_src" ] || {
  38. echo "no mtd device found for partition $cal_src"
  39. exit 1
  40. }
  41. # Dest is UBI
  42. # TODO: possibly add create (hard to do when rootfs_data is expanded & mounted)
  43. # Would need to be done from ramdisk
  44. mtd_dst="$(nand_find_volume $ubidev $cal_dst)"
  45. [ -n "$mtd_dst" ] || {
  46. echo "no ubi device found for partition $cal_dst"
  47. exit 1
  48. }
  49. mr18_is_caldata_valid "$mtd_src" && {
  50. echo "no valid calibration data found in $cal_src"
  51. exit 1
  52. }
  53. mr18_is_caldata_valid "/dev/$mtd_dst" && {
  54. echo "Copying calibration data from $cal_src to $cal_dst..."
  55. dd if="$mtd_src" of=/tmp/caldata.tmp 2>/dev/null
  56. ubiupdatevol "/dev/$mtd_dst" /tmp/caldata.tmp
  57. rm /tmp/caldata.tmp
  58. sync
  59. }
  60. return 0
  61. ;;
  62. *)
  63. echo "Unsupported device $board_name";
  64. return 1
  65. ;;
  66. esac
  67. }
  68. merakinand_do_kernel_check() {
  69. local board_name="$1"
  70. local tar_file="$2"
  71. local image_magic_word=`(tar xf $tar_file sysupgrade-$board_name/kernel -O 2>/dev/null | dd bs=1 count=4 skip=0 2>/dev/null | hexdump -v -n 4 -e '1/1 "%02x"')`
  72. # What is our kernel magic string?
  73. case "$board_name" in
  74. "mr18")
  75. [ "$image_magic_word" == "8e73ed8a" ] && {
  76. echo "pass" && return 0
  77. }
  78. ;;
  79. esac
  80. exit 1
  81. }
  82. merakinand_do_platform_check() {
  83. local board_name="$1"
  84. local tar_file="$2"
  85. local control_length=`(tar xf $tar_file sysupgrade-$board_name/CONTROL -O | wc -c) 2> /dev/null`
  86. local file_type="$(identify_tar $2 sysupgrade-$board_name/root)"
  87. local kernel_magic="$(merakinand_do_kernel_check $1 $2)"
  88. case "$board_name" in
  89. "mr18")
  90. [ "$control_length" = 0 -o "$file_type" != "squashfs" -o "$kernel_magic" != "pass" ] && {
  91. echo "Invalid sysupgrade file for $board_name"
  92. return 1
  93. }
  94. ;;
  95. *)
  96. echo "Unsupported device $board_name";
  97. return 1
  98. ;;
  99. esac
  100. return 0
  101. }
  102. merakinand_do_upgrade() {
  103. local tar_file="$1"
  104. local board_name="$(cat /tmp/sysinfo/board_name)"
  105. # Do we need to do any platform tweaks?
  106. case "$board_name" in
  107. "mr18")
  108. # Check and create UBI caldata if it's invalid
  109. merakinand_copy_caldata "odm-caldata" "caldata"
  110. nand_do_upgrade $1
  111. ;;
  112. *)
  113. echo "Unsupported device $board_name";
  114. exit 1
  115. ;;
  116. esac
  117. }