common.sh 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. RAM_ROOT=/tmp/root
  2. export BACKUP_FILE=sysupgrade.tgz # file extracted by preinit
  3. [ -x /usr/bin/ldd ] || ldd() { LD_TRACE_LOADED_OBJECTS=1 $*; }
  4. libs() { ldd $* 2>/dev/null | sed -E 's/(.* => )?(.*) .*/\2/'; }
  5. install_file() { # <file> [ <file> ... ]
  6. local target dest dir
  7. for file in "$@"; do
  8. if [ -L "$file" ]; then
  9. target="$(readlink -f "$file")"
  10. dest="$RAM_ROOT/$file"
  11. [ ! -f "$dest" ] && {
  12. dir="$(dirname "$dest")"
  13. mkdir -p "$dir"
  14. ln -s "$target" "$dest"
  15. }
  16. file="$target"
  17. fi
  18. dest="$RAM_ROOT/$file"
  19. [ -f "$file" -a ! -f "$dest" ] && {
  20. dir="$(dirname "$dest")"
  21. mkdir -p "$dir"
  22. cp "$file" "$dest"
  23. }
  24. done
  25. }
  26. install_bin() {
  27. local src files
  28. src=$1
  29. files=$1
  30. [ -x "$src" ] && files="$src $(libs $src)"
  31. install_file $files
  32. }
  33. run_hooks() {
  34. local arg="$1"; shift
  35. for func in "$@"; do
  36. eval "$func $arg"
  37. done
  38. }
  39. ask_bool() {
  40. local default="$1"; shift;
  41. local answer="$default"
  42. [ "$INTERACTIVE" -eq 1 ] && {
  43. case "$default" in
  44. 0) echo -n "$* (y/N): ";;
  45. *) echo -n "$* (Y/n): ";;
  46. esac
  47. read answer
  48. case "$answer" in
  49. y*) answer=1;;
  50. n*) answer=0;;
  51. *) answer="$default";;
  52. esac
  53. }
  54. [ "$answer" -gt 0 ]
  55. }
  56. _v() {
  57. [ -n "$VERBOSE" ] && [ "$VERBOSE" -ge 1 ] && echo "$*" >&2
  58. }
  59. v() {
  60. _v "$(date) upgrade: $@"
  61. logger -p info -t upgrade "$@"
  62. }
  63. json_string() {
  64. local v="$1"
  65. v="${v//\\/\\\\}"
  66. v="${v//\"/\\\"}"
  67. echo "\"$v\""
  68. }
  69. rootfs_type() {
  70. /bin/mount | awk '($3 ~ /^\/$/) && ($5 !~ /rootfs/) { print $5 }'
  71. }
  72. get_image() { # <source> [ <command> ]
  73. local from="$1"
  74. local cmd="$2"
  75. if [ -z "$cmd" ]; then
  76. local magic="$(dd if="$from" bs=2 count=1 2>/dev/null | hexdump -n 2 -e '1/1 "%02x"')"
  77. case "$magic" in
  78. 1f8b) cmd="busybox zcat";;
  79. *) cmd="cat";;
  80. esac
  81. fi
  82. $cmd <"$from"
  83. }
  84. get_image_dd() {
  85. local from="$1"; shift
  86. (
  87. exec 3>&2
  88. ( exec 3>&2; get_image "$from" 2>&1 1>&3 | grep -v -F ' Broken pipe' ) 2>&1 1>&3 \
  89. | ( exec 3>&2; dd "$@" 2>&1 1>&3 | grep -v -E ' records (in|out)') 2>&1 1>&3
  90. exec 3>&-
  91. )
  92. }
  93. get_magic_word() {
  94. (get_image "$@" | dd bs=2 count=1 | hexdump -v -n 2 -e '1/1 "%02x"') 2>/dev/null
  95. }
  96. get_magic_long() {
  97. (get_image "$@" | dd bs=4 count=1 | hexdump -v -n 4 -e '1/1 "%02x"') 2>/dev/null
  98. }
  99. get_magic_gpt() {
  100. (get_image "$@" | dd bs=8 count=1 skip=64) 2>/dev/null
  101. }
  102. get_magic_vfat() {
  103. (get_image "$@" | dd bs=3 count=1 skip=18) 2>/dev/null
  104. }
  105. get_magic_fat32() {
  106. (get_image "$@" | dd bs=1 count=5 skip=82) 2>/dev/null
  107. }
  108. identify_magic_long() {
  109. local magic=$1
  110. case "$magic" in
  111. "55424923")
  112. echo "ubi"
  113. ;;
  114. "31181006")
  115. echo "ubifs"
  116. ;;
  117. "68737173")
  118. echo "squashfs"
  119. ;;
  120. "d00dfeed")
  121. echo "fit"
  122. ;;
  123. "4349"*)
  124. echo "combined"
  125. ;;
  126. "1f8b"*)
  127. echo "gzip"
  128. ;;
  129. *)
  130. echo "unknown $magic"
  131. ;;
  132. esac
  133. }
  134. part_magic_efi() {
  135. local magic=$(get_magic_gpt "$@")
  136. [ "$magic" = "EFI PART" ]
  137. }
  138. part_magic_fat() {
  139. local magic=$(get_magic_vfat "$@")
  140. local magic_fat32=$(get_magic_fat32 "$@")
  141. [ "$magic" = "FAT" ] || [ "$magic_fat32" = "FAT32" ]
  142. }
  143. fitblk_get_bootdev() {
  144. [ -e /sys/firmware/devicetree/base/chosen/rootdisk ] || return
  145. local rootdisk="$(cat /sys/firmware/devicetree/base/chosen/rootdisk)"
  146. local handle bootdev
  147. for handle in /sys/class/block/*/of_node/phandle /sys/class/block/*/device/of_node/phandle; do
  148. [ ! -e "$handle" ] && continue
  149. if [ "$rootdisk" = "$(cat $handle)" ]; then
  150. bootdev="${handle%/of_node/phandle}"
  151. bootdev="${bootdev%/device}"
  152. bootdev="${bootdev#/sys/class/block/}"
  153. echo "$bootdev"
  154. break
  155. fi
  156. done
  157. }
  158. export_bootdevice() {
  159. local cmdline uuid blockdev uevent line class
  160. local MAJOR MINOR DEVNAME DEVTYPE
  161. local rootpart="$(cmdline_get_var root)"
  162. case "$rootpart" in
  163. PARTUUID=[a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9]-[a-f0-9][a-f0-9])
  164. uuid="${rootpart#PARTUUID=}"
  165. uuid="${uuid%-[a-f0-9][a-f0-9]}"
  166. for blockdev in $(find /dev -type b); do
  167. set -- $(dd if=$blockdev bs=1 skip=440 count=4 2>/dev/null | hexdump -v -e '4/1 "%02x "')
  168. if [ "$4$3$2$1" = "$uuid" ]; then
  169. uevent="/sys/class/block/${blockdev##*/}/uevent"
  170. break
  171. fi
  172. done
  173. ;;
  174. PARTUUID=????????-????-????-????-??????????0?/PARTNROFF=1 | \
  175. PARTUUID=????????-????-????-????-??????????02)
  176. uuid="${rootpart#PARTUUID=}"
  177. uuid="${uuid%/PARTNROFF=1}"
  178. uuid="${uuid%0?}00"
  179. for disk in $(find /dev -type b); do
  180. set -- $(dd if=$disk bs=1 skip=568 count=16 2>/dev/null | hexdump -v -e '8/1 "%02x "" "2/1 "%02x""-"6/1 "%02x"')
  181. if [ "$4$3$2$1-$6$5-$8$7-$9" = "$uuid" ]; then
  182. uevent="/sys/class/block/${disk##*/}/uevent"
  183. break
  184. fi
  185. done
  186. ;;
  187. /dev/*)
  188. [ "$rootpart" = "/dev/fit0" ] && rootpart="$(fitblk_get_bootdev)"
  189. uevent="/sys/class/block/${rootpart##*/}/../uevent"
  190. ;;
  191. 0x[a-f0-9][a-f0-9][a-f0-9] | 0x[a-f0-9][a-f0-9][a-f0-9][a-f0-9] | \
  192. [a-f0-9][a-f0-9][a-f0-9] | [a-f0-9][a-f0-9][a-f0-9][a-f0-9])
  193. rootpart=0x${rootpart#0x}
  194. for class in /sys/class/block/*; do
  195. while read line; do
  196. export -n "$line"
  197. done < "$class/uevent"
  198. if [ $((rootpart/256)) = $MAJOR -a $((rootpart%256)) = $MINOR ]; then
  199. uevent="$class/../uevent"
  200. fi
  201. done
  202. ;;
  203. esac
  204. if [ -e "$uevent" ]; then
  205. while read line; do
  206. export -n "$line"
  207. done < "$uevent"
  208. export BOOTDEV_MAJOR=$MAJOR
  209. export BOOTDEV_MINOR=$MINOR
  210. return 0
  211. fi
  212. return 1
  213. }
  214. export_partdevice() {
  215. local var="$1" offset="$2"
  216. local uevent line MAJOR MINOR DEVNAME DEVTYPE
  217. for uevent in /sys/class/block/*/uevent; do
  218. while read line; do
  219. export -n "$line"
  220. done < "$uevent"
  221. if [ "$BOOTDEV_MAJOR" = "$MAJOR" -a $(($BOOTDEV_MINOR + $offset)) = "$MINOR" -a -b "/dev/$DEVNAME" ]; then
  222. export "$var=$DEVNAME"
  223. return 0
  224. fi
  225. done
  226. return 1
  227. }
  228. hex_le32_to_cpu() {
  229. [ "$(echo 01 | hexdump -v -n 2 -e '/2 "%x"')" = "3031" ] && {
  230. echo "${1:0:2}${1:8:2}${1:6:2}${1:4:2}${1:2:2}"
  231. return
  232. }
  233. echo "$@"
  234. }
  235. get_partitions() { # <device> <filename>
  236. local disk="$1"
  237. local filename="$2"
  238. if [ -b "$disk" -o -f "$disk" ]; then
  239. v "Reading partition table from $filename..."
  240. local magic=$(dd if="$disk" bs=2 count=1 skip=255 2>/dev/null)
  241. if [ "$magic" != $'\x55\xAA' ]; then
  242. v "Invalid partition table on $disk"
  243. exit
  244. fi
  245. rm -f "/tmp/partmap.$filename"
  246. local part
  247. part_magic_efi "$disk" && {
  248. #export_partdevice will fail when partition number is greater than 15, as
  249. #the partition major device number is not equal to the disk major device number
  250. for part in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15; do
  251. set -- $(hexdump -v -n 48 -s "$((0x380 + $part * 0x80))" -e '4/4 "%08x"" "4/4 "%08x"" "4/4 "0x%08X "' "$disk")
  252. local type="$1"
  253. local lba="$(( $(hex_le32_to_cpu $4) * 0x100000000 + $(hex_le32_to_cpu $3) ))"
  254. local end="$(( $(hex_le32_to_cpu $6) * 0x100000000 + $(hex_le32_to_cpu $5) ))"
  255. local num="$(( $end - $lba + 1 ))"
  256. [ "$type" = "00000000000000000000000000000000" ] && continue
  257. printf "%2d %5d %7d\n" $part $lba $num >> "/tmp/partmap.$filename"
  258. done
  259. } || {
  260. for part in 1 2 3 4; do
  261. set -- $(hexdump -v -n 12 -s "$((0x1B2 + $part * 16))" -e '3/4 "0x%08X "' "$disk")
  262. local type="$(( $(hex_le32_to_cpu $1) % 256))"
  263. local lba="$(( $(hex_le32_to_cpu $2) ))"
  264. local num="$(( $(hex_le32_to_cpu $3) ))"
  265. [ $type -gt 0 ] || continue
  266. printf "%2d %5d %7d\n" $part $lba $num >> "/tmp/partmap.$filename"
  267. done
  268. }
  269. fi
  270. }
  271. indicate_upgrade() {
  272. . /etc/diag.sh
  273. set_state upgrade
  274. }
  275. # Flash firmware to MTD partition
  276. #
  277. # $(1): path to image
  278. # $(2): (optional) pipe command to extract firmware, e.g. dd bs=n skip=m
  279. default_do_upgrade() {
  280. sync
  281. echo 3 > /proc/sys/vm/drop_caches
  282. if [ -n "$UPGRADE_BACKUP" ]; then
  283. get_image "$1" "$2" | mtd $MTD_ARGS $MTD_CONFIG_ARGS -j "$UPGRADE_BACKUP" write - "${PART_NAME:-image}"
  284. else
  285. get_image "$1" "$2" | mtd $MTD_ARGS write - "${PART_NAME:-image}"
  286. fi
  287. [ $? -ne 0 ] && exit 1
  288. }