1 platform_export_partdevice
() {
2 local var
="$1" offset
="$2"
3 local uevent MAJOR MINOR DEVNAME DEVTYPE
5 for uevent
in /sys
/class
/block
/*/uevent
; do
7 if [ $BOOTDEV_MAJOR = $MAJOR -a $
(($BOOTDEV_MINOR + $offset)) = $MINOR -a -b "/dev/$DEVNAME" ]; then
16 platform_export_bootdevice
() {
17 local cmdline uuid disk uevent
18 local MAJOR MINOR DEVNAME DEVTYPE
20 if read cmdline
< /proc
/cmdline
; then
23 disk
="${cmdline##*block2mtd=}"
27 disk
="${cmdline##*root=}"
33 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)
34 uuid
="${disk#PARTUUID=}"
36 for disk
in $
(find /dev
-type b
); do
37 set -- $
(dd if=$disk bs
=1 skip
=440 count
=4 2>/dev
/null |
hexdump -v -e '4/1 "%02x "')
38 if [ "$4$3$2$1" = "$uuid" ]; then
39 uevent
="/sys/class/block/${disk##*/}/uevent"
45 uevent
="/sys/class/block/${disk##*/}/uevent"
49 if [ -e "$uevent" ]; then
52 export BOOTDEV_MAJOR
=$MAJOR
53 export BOOTDEV_MINOR
=$MINOR
62 platform_check_image
() {
63 [ "$#" -gt 1 ] && return 1
65 case "$(get_magic_word "$1")" in
68 echo "Invalid image type"
74 platform_copy_config
() {
77 if platform_export_partdevice partdev
1; then
78 mount
-t ext4
-o rw
,noatime
"/dev/$partdev" /mnt
79 cp -af "$CONF_TAR" /mnt
/
84 get_partitions
() { # <device> <filename>
88 if [ -b "$disk" -o -f "$disk" ]; then
89 echo "Reading partition table from $filename..."
91 local magic
="$(hexdump -v -n 2 -s 0x1FE -e '1/2 "0x
%04X
"' "$disk")"
92 if [ "$magic" != 0xAA55 ]; then
93 echo "Invalid partition table on $disk"
97 rm -f "/tmp/partmap.$filename"
100 for part
in 1 2 3 4; do
101 set -- $
(hexdump -v -n 12 -s "$((0x1B2 + $part * 16))" -e '3/4 "0x%08X "' "$disk")
103 local type="$(($1 % 256))"
107 [ $type -gt 0 ] ||
continue
109 printf "%2d %5d %7d\n" $part $lba $num >> "/tmp/partmap.$filename"
114 platform_do_upgrade
() {
115 local diskdev partdev ibs
diff
117 if platform_export_bootdevice
&& platform_export_partdevice diskdev
0; then
119 if [ "$SAVE_PARTITIONS" = "1" ]; then
120 get_partitions
"/dev/$diskdev" bootdisk
123 if [ -f "/sys/block/$diskdev/queue/physical_block_size" ]; then
124 ibs
="$(cat "/sys
/block
/$diskdev/queue
/physical_block_size
")"
129 #extract the boot sector from the image
130 get_image
"$@" |
dd of
=/tmp
/image.bs count
=1 bs
=512b
132 get_partitions
/tmp
/image.bs image
135 diff="$(grep -F -x -v -f /tmp/partmap.bootdisk /tmp/partmap.image)"
136 if [ -n "$diff" ]; then
137 echo "Partition layout is changed. Full image will be written."
138 ask_bool
0 "Abort" && exit
140 get_image
"$@" |
dd of
="/dev/$diskdev" bs
=4096 conv
=fsync
144 #iterate over each partition from the image and write it to the boot disk
145 while read part start size
; do
146 if platform_export_partdevice partdev
$part; then
147 echo "Writing image to /dev/$partdev..."
148 get_image
"$@" |
dd of
="/dev/$partdev" ibs
="$ibs" obs
=1M skip
="$start" count
="$size" conv
=fsync
150 echo "Unable to find partition $part device, skipped."
152 done < /tmp
/partmap.image
155 echo "Writing new UUID to /dev/$diskdev..."
156 get_image
"$@" |
dd of
="/dev/$diskdev" bs
=1 skip
=440 count
=4 seek
=440 conv
=fsync
158 get_image
"$@" |
dd of
="/dev/$diskdev" bs
=4096 conv
=fsync