1
0

merakinand.sh 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #!/bin/sh
  2. #
  3. # Copyright (C) 2015-2016 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. meraki_is_caldata_valid() {
  16. local board=$1
  17. local mtddev=$2
  18. local magic
  19. case "$board" in
  20. "mr18")
  21. magic=$(get_magic_at $mtddev 4096)
  22. [ "$magic" != "0202" ] && return 0
  23. magic=$(get_magic_at $mtddev 20480)
  24. [ "$magic" != "0202" ] && return 0
  25. magic=$(get_magic_at $mtddev 36864)
  26. [ "$magic" != "0202" ] && return 0
  27. return 1
  28. ;;
  29. "z1")
  30. magic=$(get_magic_at $mtddev 4096)
  31. [ "$magic" != "0202" ] && return 0
  32. magic=$(get_magic_at $mtddev 86016)
  33. [ "$magic" != "a55a" ] && return 0
  34. return 1
  35. ;;
  36. *)
  37. return 1
  38. ;;
  39. esac
  40. }
  41. merakinand_copy_caldata() {
  42. local cal_src=$1
  43. local cal_dst=$2
  44. local ubidev="$(nand_find_ubi $CI_UBIPART)"
  45. local board_name="$(cat /tmp/sysinfo/board_name)"
  46. local rootfs_size="$(ubinfo /dev/ubi0 -N rootfs_data | grep "Size" | awk '{ print $6 }')"
  47. # Setup partitions using board name, in case of future platforms
  48. case "$board_name" in
  49. "mr18"|\
  50. "z1")
  51. # Src is MTD
  52. mtd_src="$(find_mtd_chardev $cal_src)"
  53. [ -n "$mtd_src" ] || {
  54. echo "no mtd device found for partition $cal_src"
  55. exit 1
  56. }
  57. # Dest is UBI
  58. # TODO: possibly add create (hard to do when rootfs_data is expanded & mounted)
  59. # Would need to be done from ramdisk
  60. mtd_dst="$(nand_find_volume $ubidev $cal_dst)"
  61. [ -n "$mtd_dst" ] || {
  62. echo "no ubi device found for partition $cal_dst"
  63. exit 1
  64. }
  65. meraki_is_caldata_valid "$board_name" "$mtd_src" && {
  66. echo "no valid calibration data found in $cal_src"
  67. exit 1
  68. }
  69. meraki_is_caldata_valid "$board_name" "/dev/$mtd_dst" && {
  70. echo "Copying calibration data from $cal_src to $cal_dst..."
  71. dd if="$mtd_src" of=/tmp/caldata.tmp 2>/dev/null
  72. ubiupdatevol "/dev/$mtd_dst" /tmp/caldata.tmp
  73. rm /tmp/caldata.tmp
  74. sync
  75. }
  76. return 0
  77. ;;
  78. *)
  79. echo "Unsupported device $board_name";
  80. return 1
  81. ;;
  82. esac
  83. }
  84. merakinand_do_kernel_check() {
  85. local board_name="$1"
  86. local tar_file="$2"
  87. 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"')`
  88. # What is our kernel magic string?
  89. case "$board_name" in
  90. "mr18")
  91. [ "$image_magic_word" == "8e73ed8a" ] && {
  92. echo "pass" && return 0
  93. }
  94. ;;
  95. "z1")
  96. [ "$image_magic_word" == "4d495053" ] && {
  97. echo "pass" && return 0
  98. }
  99. ;;
  100. esac
  101. exit 1
  102. }
  103. merakinand_do_platform_check() {
  104. local board_name="$1"
  105. local tar_file="$2"
  106. local control_length=`(tar xf $tar_file sysupgrade-$board_name/CONTROL -O | wc -c) 2> /dev/null`
  107. local file_type="$(identify_tar $2 sysupgrade-$board_name/root)"
  108. local kernel_magic="$(merakinand_do_kernel_check $1 $2)"
  109. case "$board_name" in
  110. "mr18"|\
  111. "z1")
  112. [ "$control_length" = 0 -o "$file_type" != "squashfs" -o "$kernel_magic" != "pass" ] && {
  113. echo "Invalid sysupgrade file for $board_name"
  114. return 1
  115. }
  116. ;;
  117. *)
  118. echo "Unsupported device $board_name";
  119. return 1
  120. ;;
  121. esac
  122. return 0
  123. }
  124. merakinand_do_upgrade() {
  125. local tar_file="$1"
  126. local board_name="$(cat /tmp/sysinfo/board_name)"
  127. # Do we need to do any platform tweaks?
  128. case "$board_name" in
  129. "mr18")
  130. # Check and create UBI caldata if it's invalid
  131. merakinand_copy_caldata "odm-caldata" "caldata"
  132. nand_do_upgrade $1
  133. ;;
  134. "z1")
  135. # Check and create UBI caldata if it's invalid
  136. merakinand_copy_caldata "origcaldata" "caldata"
  137. nand_do_upgrade $1
  138. ;;
  139. *)
  140. echo "Unsupported device $board_name";
  141. exit 1
  142. ;;
  143. esac
  144. }