kernel: armsr: Renesas: RZ: Ethernet module and ttySC0
[openwrt/openwrt.git] / target / linux / mediatek / mt7622 / base-files / lib / upgrade / buffalo.sh
1 # ======== dev note ========
2 # for following buffalo MT7622 devices:
3 #
4 # - WSR-2533DHP2 (trx magic: "DHP2")
5 # - WSR-2533DHP3 (trx magic: "DHP3")
6 # - WSR-3200AX4S (trx magic: "DHP3")
7 #
8 # sysupgrade-tar image:
9 #
10 # This is for normal upgrading for OpenWrt.
11 # use nand_do_upgrade with CI_KERNPART="firmware"
12 #
13 # - if the size of new kernel is not equal with the current kernel's
14 # -> block upgrade and print a message about using TRX + UBI
15 # formatted image
16 # (should be flashed the new ubi contains rootfs + rootfs_data
17 # with the offset (=new padded kernel's end) if this case? But
18 # it maybe too hard for writing scripts...)
19 #
20 # TRX + UBI formatted image:
21 #
22 # This is for upgrading if the new kernel is larger than the
23 # current kernel.
24 #
25 # ex:
26 # - stock firmware is installed in the flash and booted with
27 # OpenWrt initramfs image
28 # - kernel partition is increased from 4MiB in OpenWrt in the
29 # future
30 #
31 # packing TRX + UBI formatted image by tar is needed for image validation
32 # with the metadata in the future?
33 # ====== dev note end ======
34 #
35 # The mtd partitions "firmware" and "Kernel2" on NAND flash are os-image
36 # partitions. These partitions are called as "Image1/Image2" in U-Boot
37 # on WSR-2533DHP2, and they are checked conditions when booting.
38 # "Image1" is always used for booting.
39 #
40 # == U-Boot Behaviors ==
41 # - "Image1"/"Image2" images are good, images are different or
42 # "Image2" image is broken
43 # -> writes os-image to "Image2" from "Image1"
44 #
45 # - "Image1" image is broken
46 # -> writes os-image to "Image1" from "Image2"
47 #
48 # - "Image1"/"Image2" images are broken
49 # -> fall to U-Boot command line
50
51 buffalo_check_image() {
52 local board="$1"
53 local boardname="$(echo $board | tr ',' '_')"
54 local magic="$2"
55 local fw_image="$3"
56
57 # return error state if TRX + UBI formatted image specified
58 # to notify about configurations
59 if [ "$magic" = "44485032" -o "$magic" = "44485033" ]; then
60 echo "Your configurations won't be saved if factory-uboot.bin image specified."
61 echo "But if you want to upgrade, please execute sysupgrade with \"-F\" option."
62 return 1
63 fi
64
65 # check if valid tar file specifed
66 if ! tar tf "$fw_image" &>/dev/null; then
67 echo "Specified file is not a tar archive: $fw_image"
68 return 1
69 fi
70
71 local control_len=$( (tar xf $fw_image sysupgrade-$boardname/CONTROL -O | wc -c) 2> /dev/null)
72
73 # check if valid sysupgrade tar archive
74 if [ "$control_len" = "0" ]; then
75 echo "Invalid sysupgrade file: $fw_image"
76 return 1
77 fi
78
79 local kern_part_len=$(grep "\"linux\"" /proc/mtd | sed "s/mtd[0-9]*:[ \t]*\([^ \t]*\).*/\1/")
80 [ -z "$kern_part_len" ] && {
81 echo "Unable to get \"linux\" partition size"
82 return 1
83 }
84 kern_part_len=$((0x$kern_part_len))
85
86 # this also checks if the sysupgrade image is for correct models
87 local kern_bin_len=$( (tar xf $fw_image sysupgrade-${boardname}/kernel -O | wc -c) 2> /dev/null)
88 if [ -z "$kern_bin_len" ]; then
89 echo "Failed to get new kernel size, is valid sysupgrade image specified for the device?"
90 return 1
91 fi
92
93 # kernel binary has a trx header (len: 28 (0x1c))
94 kern_bin_len=$((kern_bin_len - 28))
95
96 if [ "$kern_bin_len" != "$kern_part_len" ]; then
97 echo -n "The length of new kernel is invalid for current "
98 echo "\"linux\" partition, please use factory-uboot.bin image."
99 echo "\"linux\" partition: $kern_part_len, new kernel: $kern_bin_len"
100 return 1
101 fi
102 }
103
104 # for TRX + UBI formatted image
105 buffalo_upgrade_ubinized() {
106 sync
107 echo 3 > /proc/sys/vm/drop_caches
108
109 local mtdnum="$( find_mtd_index "ubi" )"
110 # if no "ubi", don't return error for the purpose of recovery
111 # ex: recovery after accidental erasing "firmware" partition
112 if [ ! "$mtdnum" ]; then
113 echo "cannot find ubi mtd partition \"ubi\", skip detachment"
114 else
115 ubidetach -m "$mtdnum"
116 fi
117
118 # erase all data in "firmware"
119 mtd erase "${PART_NAME}"
120 # write TRX + UBI formatted image to "firmware"
121 get_image "$1" | mtd $MTD_ARGS write - "${PART_NAME:-firmware}"
122 if [ $? -ne 0 ]; then
123 echo "Failed to write the specified image."
124 exit 1
125 fi
126 }