layerscape: support sysupgrade for SD card ext4 rootfs
[openwrt/openwrt.git] / target / linux / layerscape / base-files / lib / upgrade / platform.sh
1 #
2 # Copyright 2015-2019 Traverse Technologies
3 # Copyright 2020 NXP
4 #
5
6 RAMFS_COPY_BIN="/usr/sbin/fw_printenv /usr/sbin/fw_setenv /usr/sbin/ubinfo /bin/echo"
7 RAMFS_COPY_DATA="/etc/fw_env.config /var/lock/fw_printenv.lock"
8
9 REQUIRE_IMAGE_METADATA=1
10
11 platform_check_image_sdboot() {
12 local diskdev partdev diff
13
14 export_bootdevice && export_partdevice diskdev 0 || {
15 echo "Unable to determine upgrade device"
16 return 1
17 }
18
19 # get partitions table from boot device
20 get_partitions "/dev/$diskdev" bootdisk
21
22 # get partitions table from sysupgrade.bin
23 dd if="$1" of=/tmp/image.bs bs=512b count=1 > /dev/null 2>&1
24 sync
25 get_partitions /tmp/image.bs image
26
27 # compare tables
28 diff="$(grep -F -x -v -f /tmp/partmap.bootdisk /tmp/partmap.image)"
29
30 rm -f /tmp/image.bs /tmp/partmap.bootdisk /tmp/partmap.image
31
32 if [ -n "$diff" ]; then
33 echo "Partition layout has changed. Full image will be written."
34 ask_bool 0 "Abort" && exit 1
35 return 0
36 fi
37 }
38 platform_do_upgrade_sdboot() {
39 local diskdev partdev diff
40
41 export_bootdevice && export_partdevice diskdev 0 || {
42 echo "Unable to determine upgrade device"
43 return 1
44 }
45
46 if [ "$UPGRADE_OPT_SAVE_PARTITIONS" = "1" ]; then
47 # get partitions table from boot device
48 get_partitions "/dev/$diskdev" bootdisk
49
50 # get partitions table from sysupgrade.bin
51 dd if="$1" of=/tmp/image.bs bs=512b count=1 > /dev/null 2>&1
52 sync
53 get_partitions /tmp/image.bs image
54
55 # compare tables
56 diff="$(grep -F -x -v -f /tmp/partmap.bootdisk /tmp/partmap.image)"
57 else
58 diff=1
59 fi
60
61 if [ -n "$diff" ]; then
62 dd if="$1" of="/dev/$diskdev" bs=1024 count=4 > /dev/null 2>&1
63 dd if="$1" of="$diskdev" bs=1024 skip=4 seek=16384 > /dev/null 2>&1
64 sync
65
66 # Separate removal and addtion is necessary; otherwise, partition 1
67 # will be missing if it overlaps with the old partition 2
68 partx -d - "/dev/$diskdev"
69 partx -a - "/dev/$diskdev"
70
71 return 0
72 fi
73
74 # write kernel image
75 dd if="$1" of="$diskdev" bs=1024 skip=4 seek=16384 count=16384 > /dev/null 2>&1
76 sync
77
78 # iterate over each partition from the image and write it to the boot disk
79 while read part start size; do
80 if export_partdevice partdev $part; then
81 echo "Writing image to /dev/$partdev..."
82 dd if="$1" of="/dev/$partdev" bs=512 skip="$start" count="$size" > /dev/null 2>&1
83 sync
84 else
85 echo "Unable to find partition $part device, skipped."
86 fi
87 done < /tmp/partmap.image
88
89 }
90 platform_do_upgrade_traverse_nandubi() {
91 bootsys=$(fw_printenv bootsys | awk -F= '{{print $2}}')
92 newbootsys=2
93 if [ "$bootsys" -eq "2" ]; then
94 newbootsys=1
95 fi
96
97 # If nand_do_upgrade succeeds, we don't have an opportunity to add any actions of
98 # our own, so do it here and set back on failure
99 echo "Setting bootsys to #${newbootsys}"
100 fw_setenv bootsys $newbootsys
101 CI_UBIPART="nandubi"
102 CI_KERNPART="kernel${newbootsys}"
103 CI_ROOTPART="rootfs${newbootsys}"
104 nand_do_upgrade "$1" || (echo "Upgrade failed, setting bootsys ${bootsys}" && fw_setenv bootsys $bootsys)
105
106 }
107 platform_copy_config() {
108 local partdev parttype=ext4
109
110 if export_partdevice partdev 1; then
111 mount -t $parttype -o rw,noatime "/dev/$partdev" /mnt
112 cp -af "$UPGRADE_BACKUP" "/mnt/$BACKUP_FILE"
113 umount /mnt
114 fi
115 }
116 platform_check_image() {
117 local board=$(board_name)
118
119 case "$board" in
120 traverse,ls1043v | \
121 traverse,ls1043s)
122 nand_do_platform_check "traverse-ls1043" $1
123 return $?
124 ;;
125 fsl,ls1012a-frdm | \
126 fsl,ls1012a-rdb | \
127 fsl,ls1021a-twr | \
128 fsl,ls1043a-rdb | \
129 fsl,ls1046a-rdb | \
130 fsl,ls1088a-rdb | \
131 fsl,ls2088a-rdb)
132 return 0
133 ;;
134 fsl,ls1012a-frwy-sdboot | \
135 fsl,ls1021a-twr-sdboot | \
136 fsl,ls1043a-rdb-sdboot | \
137 fsl,ls1046a-rdb-sdboot | \
138 fsl,ls1088a-rdb-sdboot)
139 platform_check_image_sdboot "$1"
140 return 0
141 ;;
142 *)
143 echo "Sysupgrade is not currently supported on $board"
144 ;;
145 esac
146
147 return 1
148 }
149 platform_do_upgrade() {
150 local board=$(board_name)
151
152 # Force the creation of fw_printenv.lock
153 mkdir -p /var/lock
154 touch /var/lock/fw_printenv.lock
155
156 case "$board" in
157 traverse,ls1043v | \
158 traverse,ls1043s)
159 platform_do_upgrade_traverse_nandubi "$1"
160 ;;
161 fsl,ls1012a-frdm | \
162 fsl,ls1012a-rdb | \
163 fsl,ls1021a-twr | \
164 fsl,ls1043a-rdb | \
165 fsl,ls1046a-rdb | \
166 fsl,ls1088a-rdb | \
167 fsl,ls2088a-rdb)
168 PART_NAME=firmware
169 default_do_upgrade "$1"
170 ;;
171 fsl,ls1012a-frwy-sdboot | \
172 fsl,ls1021a-twr-sdboot | \
173 fsl,ls1043a-rdb-sdboot | \
174 fsl,ls1046a-rdb-sdboot | \
175 fsl,ls1088a-rdb-sdboot)
176 platform_do_upgrade_sdboot "$1"
177 return 0
178 ;;
179 *)
180 echo "Sysupgrade is not currently supported on $board"
181 ;;
182 esac
183 }