0094-base-files-export-x86-platform-upgrade-functions-to-common.sh.patch 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. From: Yousong Zhou <yszhou4tech@gmail.com>
  2. Date: Sun, 1 Jan 2017 01:06:37 +0800
  3. Subject: base-files: export x86 platform upgrade functions to common.sh
  4. Signed-off-by: Yousong Zhou <yszhou4tech@gmail.com>
  5. Backport of LEDE 6f61d8511eccf2736fd7f430aff2c98c595fa4b9
  6. diff --git a/package/base-files/files/lib/upgrade/common.sh b/package/base-files/files/lib/upgrade/common.sh
  7. index 14684959dd46d04b1c4b72213dbe06943ae64686..2064adf775c491725dbff5826b94bd6132d8d060 100644
  8. --- a/package/base-files/files/lib/upgrade/common.sh
  9. +++ b/package/base-files/files/lib/upgrade/common.sh
  10. @@ -203,6 +203,96 @@ get_magic_long() {
  11. (get_image "$@" | dd bs=4 count=1 | hexdump -v -n 4 -e '1/1 "%02x"') 2>/dev/null
  12. }
  13. +export_bootdevice() {
  14. + local cmdline uuid disk uevent
  15. + local MAJOR MINOR DEVNAME DEVTYPE
  16. +
  17. + if read cmdline < /proc/cmdline; then
  18. + case "$cmdline" in
  19. + *block2mtd=*)
  20. + disk="${cmdline##*block2mtd=}"
  21. + disk="${disk%%,*}"
  22. + ;;
  23. + *root=*)
  24. + disk="${cmdline##*root=}"
  25. + disk="${disk%% *}"
  26. + ;;
  27. + esac
  28. +
  29. + case "$disk" in
  30. + 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]-02)
  31. + uuid="${disk#PARTUUID=}"
  32. + uuid="${uuid%-02}"
  33. + for disk in $(find /dev -type b); do
  34. + set -- $(dd if=$disk bs=1 skip=440 count=4 2>/dev/null | hexdump -v -e '4/1 "%02x "')
  35. + if [ "$4$3$2$1" = "$uuid" ]; then
  36. + uevent="/sys/class/block/${disk##*/}/uevent"
  37. + break
  38. + fi
  39. + done
  40. + ;;
  41. + /dev/*)
  42. + uevent="/sys/class/block/${disk##*/}/uevent"
  43. + ;;
  44. + esac
  45. +
  46. + if [ -e "$uevent" ]; then
  47. + . "$uevent"
  48. +
  49. + export BOOTDEV_MAJOR=$MAJOR
  50. + export BOOTDEV_MINOR=$MINOR
  51. + return 0
  52. + fi
  53. + fi
  54. +
  55. + return 1
  56. +}
  57. +
  58. +export_partdevice() {
  59. + local var="$1" offset="$2"
  60. + local uevent MAJOR MINOR DEVNAME DEVTYPE
  61. +
  62. + for uevent in /sys/class/block/*/uevent; do
  63. + . "$uevent"
  64. + if [ $BOOTDEV_MAJOR = $MAJOR -a $(($BOOTDEV_MINOR + $offset)) = $MINOR -a -b "/dev/$DEVNAME" ]; then
  65. + export "$var=$DEVNAME"
  66. + return 0
  67. + fi
  68. + done
  69. +
  70. + return 1
  71. +}
  72. +
  73. +get_partitions() { # <device> <filename>
  74. + local disk="$1"
  75. + local filename="$2"
  76. +
  77. + if [ -b "$disk" -o -f "$disk" ]; then
  78. + v "Reading partition table from $filename..."
  79. +
  80. + local magic="$(hexdump -v -n 2 -s 0x1FE -e '1/2 "0x%04X"' "$disk")"
  81. + if [ "$magic" != 0xAA55 ]; then
  82. + v "Invalid partition table on $disk"
  83. + exit
  84. + fi
  85. +
  86. + rm -f "/tmp/partmap.$filename"
  87. +
  88. + local part
  89. + for part in 1 2 3 4; do
  90. + set -- $(hexdump -v -n 12 -s "$((0x1B2 + $part * 16))" -e '3/4 "0x%08X "' "$disk")
  91. +
  92. + local type="$(($1 % 256))"
  93. + local lba="$(($2))"
  94. + local num="$(($3))"
  95. +
  96. + [ $type -gt 0 ] || continue
  97. +
  98. + printf "%2d %5d %7d\n" $part $lba $num >> "/tmp/partmap.$filename"
  99. + done
  100. + fi
  101. +}
  102. +
  103. jffs2_copy_config() {
  104. if grep rootfs_data /proc/mtd >/dev/null; then
  105. # squashfs+jffs2
  106. diff --git a/target/linux/x86/base-files/lib/preinit/79_move_config b/target/linux/x86/base-files/lib/preinit/79_move_config
  107. index 37954f0236faafca657a05a3c1efc7df2e162bfc..143ca5147b5693a5b8068dc10eb71b5066777160 100644
  108. --- a/target/linux/x86/base-files/lib/preinit/79_move_config
  109. +++ b/target/linux/x86/base-files/lib/preinit/79_move_config
  110. @@ -4,9 +4,9 @@
  111. move_config() {
  112. local partdev
  113. - . /lib/upgrade/platform.sh
  114. + . /lib/upgrade/common.sh
  115. - if platform_export_bootdevice && platform_export_partdevice partdev 1; then
  116. + if export_bootdevice && export_partdevice partdev 1; then
  117. if mount -t ext4 -o rw,noatime "/dev/$partdev" /mnt; then
  118. if [ -f /mnt/sysupgrade.tgz ]; then
  119. mv -f /mnt/sysupgrade.tgz /
  120. diff --git a/target/linux/x86/base-files/lib/upgrade/platform.sh b/target/linux/x86/base-files/lib/upgrade/platform.sh
  121. index c8bc3f7f608fc82ee3afc049b64af3a740fd2c37..8850917062618985126b5086dccfe11506ed1fbb 100644
  122. --- a/target/linux/x86/base-files/lib/upgrade/platform.sh
  123. +++ b/target/linux/x86/base-files/lib/upgrade/platform.sh
  124. @@ -1,64 +1,3 @@
  125. -platform_export_partdevice() {
  126. - local var="$1" offset="$2"
  127. - local uevent MAJOR MINOR DEVNAME DEVTYPE
  128. -
  129. - for uevent in /sys/class/block/*/uevent; do
  130. - . "$uevent"
  131. - if [ $BOOTDEV_MAJOR = $MAJOR -a $(($BOOTDEV_MINOR + $offset)) = $MINOR -a -b "/dev/$DEVNAME" ]; then
  132. - export "$var=$DEVNAME"
  133. - return 0
  134. - fi
  135. - done
  136. -
  137. - return 1
  138. -}
  139. -
  140. -platform_export_bootdevice() {
  141. - local cmdline uuid disk uevent
  142. - local MAJOR MINOR DEVNAME DEVTYPE
  143. -
  144. - if read cmdline < /proc/cmdline; then
  145. - case "$cmdline" in
  146. - *block2mtd=*)
  147. - disk="${cmdline##*block2mtd=}"
  148. - disk="${disk%%,*}"
  149. - ;;
  150. - *root=*)
  151. - disk="${cmdline##*root=}"
  152. - disk="${disk%% *}"
  153. - ;;
  154. - esac
  155. -
  156. - case "$disk" in
  157. - 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]-02)
  158. - uuid="${disk#PARTUUID=}"
  159. - uuid="${uuid%-02}"
  160. - for disk in $(find /dev -type b); do
  161. - set -- $(dd if=$disk bs=1 skip=440 count=4 2>/dev/null | hexdump -v -e '4/1 "%02x "')
  162. - if [ "$4$3$2$1" = "$uuid" ]; then
  163. - uevent="/sys/class/block/${disk##*/}/uevent"
  164. - break
  165. - fi
  166. - done
  167. - ;;
  168. - /dev/*)
  169. - uevent="/sys/class/block/${disk##*/}/uevent"
  170. - ;;
  171. - esac
  172. -
  173. - if [ -e "$uevent" ]; then
  174. - . "$uevent"
  175. -
  176. - export BOOTDEV_MAJOR=$MAJOR
  177. - export BOOTDEV_MINOR=$MINOR
  178. -
  179. - return 0
  180. - fi
  181. - fi
  182. -
  183. - return 1
  184. -}
  185. -
  186. platform_check_image() {
  187. [ "$#" -gt 1 ] && return 1
  188. @@ -74,47 +13,17 @@ platform_check_image() {
  189. platform_copy_config() {
  190. local partdev
  191. - if platform_export_partdevice partdev 1; then
  192. + if export_partdevice partdev 1; then
  193. mount -t ext4 -o rw,noatime "/dev/$partdev" /mnt
  194. cp -af "$CONF_TAR" /mnt/
  195. umount /mnt
  196. fi
  197. }
  198. -get_partitions() { # <device> <filename>
  199. - local disk="$1"
  200. - local filename="$2"
  201. -
  202. - if [ -b "$disk" -o -f "$disk" ]; then
  203. - echo "Reading partition table from $filename..."
  204. -
  205. - local magic="$(hexdump -v -n 2 -s 0x1FE -e '1/2 "0x%04X"' "$disk")"
  206. - if [ "$magic" != 0xAA55 ]; then
  207. - echo "Invalid partition table on $disk"
  208. - exit
  209. - fi
  210. -
  211. - rm -f "/tmp/partmap.$filename"
  212. -
  213. - local part
  214. - for part in 1 2 3 4; do
  215. - set -- $(hexdump -v -n 12 -s "$((0x1B2 + $part * 16))" -e '3/4 "0x%08X "' "$disk")
  216. -
  217. - local type="$(($1 % 256))"
  218. - local lba="$(($2))"
  219. - local num="$(($3))"
  220. -
  221. - [ $type -gt 0 ] || continue
  222. -
  223. - printf "%2d %5d %7d\n" $part $lba $num >> "/tmp/partmap.$filename"
  224. - done
  225. - fi
  226. -}
  227. -
  228. platform_do_upgrade() {
  229. local diskdev partdev ibs diff
  230. - if platform_export_bootdevice && platform_export_partdevice diskdev 0; then
  231. + if export_bootdevice && export_partdevice diskdev 0; then
  232. sync
  233. if [ "$SAVE_PARTITIONS" = "1" ]; then
  234. get_partitions "/dev/$diskdev" bootdisk
  235. @@ -143,7 +52,7 @@ platform_do_upgrade() {
  236. #iterate over each partition from the image and write it to the boot disk
  237. while read part start size; do
  238. - if platform_export_partdevice partdev $part; then
  239. + if export_partdevice partdev $part; then
  240. echo "Writing image to /dev/$partdev..."
  241. get_image "$@" | dd of="/dev/$partdev" ibs="$ibs" obs=1M skip="$start" count="$size" conv=fsync
  242. else