layerscape: fix platform_copy_config function
[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 echo "Writing image to /dev/$diskdev..."
64 dd if="$1" of="/dev/$diskdev" bs=1024 skip=15360 seek=15360 > /dev/null 2>&1
65 sync
66
67 # Separate removal and addtion is necessary; otherwise, partition 1
68 # will be missing if it overlaps with the old partition 2
69 partx -d - "/dev/$diskdev"
70 partx -a - "/dev/$diskdev"
71
72 return 0
73 fi
74
75 # write dtb
76 echo "Writing dtb to /dev/$diskdev..."
77 dd if="$1" of="/dev/$diskdev" bs=1024 skip=15360 seek=15360 count=1024 > /dev/null 2>&1
78 # write kernel image
79 echo "Writing kernel to /dev/$diskdev..."
80 dd if="$1" of="/dev/$diskdev" bs=1024 skip=16384 seek=16384 count=16384 > /dev/null 2>&1
81 sync
82
83 # iterate over each partition from the image and write it to the boot disk
84 while read part start size; do
85 if export_partdevice partdev $part; then
86 echo "Writing image to /dev/$partdev..."
87 dd if="$1" of="/dev/$partdev" bs=512 skip="$start" count="$size" > /dev/null 2>&1
88 sync
89 else
90 echo "Unable to find partition $part device, skipped."
91 fi
92 done < /tmp/partmap.image
93
94 }
95 platform_do_upgrade_traverse_nandubi() {
96 bootsys=$(fw_printenv bootsys | awk -F= '{{print $2}}')
97 newbootsys=2
98 if [ "$bootsys" -eq "2" ]; then
99 newbootsys=1
100 fi
101
102 # If nand_do_upgrade succeeds, we don't have an opportunity to add any actions of
103 # our own, so do it here and set back on failure
104 echo "Setting bootsys to #${newbootsys}"
105 fw_setenv bootsys $newbootsys
106 CI_UBIPART="nandubi"
107 CI_KERNPART="kernel${newbootsys}"
108 CI_ROOTPART="rootfs${newbootsys}"
109 nand_do_upgrade "$1" || (echo "Upgrade failed, setting bootsys ${bootsys}" && fw_setenv bootsys $bootsys)
110
111 }
112 platform_copy_config_sdboot() {
113 local diskdev partdev parttype=ext4
114
115 export_bootdevice && export_partdevice diskdev 0 || {
116 echo "Unable to determine upgrade device"
117 return 1
118 }
119
120 if export_partdevice partdev 1; then
121 mount -t $parttype -o rw,noatime "/dev/$partdev" /mnt 2>&1
122 cp -af "$UPGRADE_BACKUP" "/mnt/$BACKUP_FILE"
123 umount /mnt
124 fi
125 }
126 platform_copy_config() {
127 local board=$(board_name)
128
129 case "$board" in
130 fsl,ls1012a-frwy-sdboot | \
131 fsl,ls1021a-twr-sdboot | \
132 fsl,ls1043a-rdb-sdboot | \
133 fsl,ls1046a-rdb-sdboot | \
134 fsl,ls1088a-rdb-sdboot)
135 platform_copy_config_sdboot
136 ;;
137 esac
138 }
139 platform_check_image() {
140 local board=$(board_name)
141
142 case "$board" in
143 traverse,ls1043v | \
144 traverse,ls1043s)
145 nand_do_platform_check "traverse-ls1043" $1
146 return $?
147 ;;
148 fsl,ls1012a-frdm | \
149 fsl,ls1012a-rdb | \
150 fsl,ls1021a-twr | \
151 fsl,ls1043a-rdb | \
152 fsl,ls1046a-rdb | \
153 fsl,ls1088a-rdb | \
154 fsl,ls2088a-rdb)
155 return 0
156 ;;
157 fsl,ls1012a-frwy-sdboot | \
158 fsl,ls1021a-twr-sdboot | \
159 fsl,ls1043a-rdb-sdboot | \
160 fsl,ls1046a-rdb-sdboot | \
161 fsl,ls1088a-rdb-sdboot)
162 platform_check_image_sdboot "$1"
163 return 0
164 ;;
165 *)
166 echo "Sysupgrade is not currently supported on $board"
167 ;;
168 esac
169
170 return 1
171 }
172 platform_do_upgrade() {
173 local board=$(board_name)
174
175 # Force the creation of fw_printenv.lock
176 mkdir -p /var/lock
177 touch /var/lock/fw_printenv.lock
178
179 case "$board" in
180 traverse,ls1043v | \
181 traverse,ls1043s)
182 platform_do_upgrade_traverse_nandubi "$1"
183 ;;
184 fsl,ls1012a-frdm | \
185 fsl,ls1012a-rdb | \
186 fsl,ls1021a-twr | \
187 fsl,ls1043a-rdb | \
188 fsl,ls1046a-rdb | \
189 fsl,ls1088a-rdb | \
190 fsl,ls2088a-rdb)
191 PART_NAME=firmware
192 default_do_upgrade "$1"
193 ;;
194 fsl,ls1012a-frwy-sdboot | \
195 fsl,ls1021a-twr-sdboot | \
196 fsl,ls1043a-rdb-sdboot | \
197 fsl,ls1046a-rdb-sdboot | \
198 fsl,ls1088a-rdb-sdboot)
199 platform_do_upgrade_sdboot "$1"
200 return 0
201 ;;
202 *)
203 echo "Sysupgrade is not currently supported on $board"
204 ;;
205 esac
206 }