1# Copyright (c) 2013 The Linux Foundation. All rights reserved.
2USE_REFRESH=
3
4# make sure we got the tools we need during the fw upgrade process
5platform_add_ramfs_cus227_tools()
6{
7	install_bin /usr/sbin/fw_printenv /usr/sbin/fw_setenv
8	install_bin /bin/busybox /usr/bin/cut
9	install_bin /usr/bin/md5sum
10	install_file /etc/fw_env.config
11}
12append sysupgrade_pre_upgrade platform_add_ramfs_cus227_tools
13
14get_mtdpart_size() {
15	local varname=$1
16	local partname=$2
17
18	local size
19	size=$(grep ${partname} /proc/mtd | cut -f2 -d' ')
20	eval "${varname}=${size}"
21}
22
23# This function read the MTD mapping in /proc/mtd and output
24# its offset in flash
25# @1) (OUTPUT) variable to store the offset to
26# @2) partition name
27get_mtdpart_offset() {
28	local varname=$1
29	local partname=$2
30	local offset=0
31	local mtdsize=0
32
33	# Iterate over every line in mtd until the partition name matches
34	local line
35	while read line && echo ${line} | grep -vq ".*\"${partname}\""; do
36		# If the line doesn't start with mtd* (like first line)
37		# we just skip it
38		echo ${line} | grep -q "^mtd.*" || continue
39		# Here we extract the size info and add it to the previous offset
40		mtdsize=$(echo ${line}|cut -f2 -d' ')
41		offset=$(printf '%x' $((0x${offset} + 0x${mtdsize})))
42	done < /proc/mtd
43	eval "${varname}=${offset}"
44}
45
46cus227_update_u_boot_env() {
47	local file="$1"
48
49	local kernel_addr
50	local devtree_addr devtree_size
51	local bootargs bootcmd
52
53	# Perform sanity checks to make sure we've got all the tools we need
54	[ -x /usr/sbin/fw_printenv -a -x /usr/sbin/fw_setenv -a \
55	  -x /bin/grep -a -x /usr/bin/cut -a -x /usr/bin/md5sum ] || {
56		v "Error: missing tools to perform FW upgrade"
57		return 1
58	}
59	[ -f /etc/fw_env.config ] || {
60		v "/etc/fw_env.config does not exist"
61		return 1
62	}
63
64	get_mtdpart_offset kernel_addr "k-2"
65	get_mtdpart_offset devtree_addr "devtree"
66	get_mtdpart_size   devtree_size "devtree"
67
68	local  bootcmd_old=$(fw_printenv -n bootcmd)
69	local bootargs_old=$(fw_printenv -n bootargs)
70
71	v "Updating cus-227 u-boot-env..."
72	bootargs=$(fw_printenv -n bootargs | sed \
73		-e 's/(kernel)/(tmp-k-1)/' \
74		-e 's/(rootfs)/(tmp-r-1)/' \
75		-e 's/(vendor)/(tmp-v-1)/' \
76		-e 's/(firmware)/(tmp-fw-1)/' \
77		\
78		-e 's/(k-2)/(kernel)/' \
79		-e 's/(r-2)/(rootfs)/' \
80		-e 's/(v-2)/(vendor)/' \
81		-e 's/(fw-2)/(firmware)/' \
82		\
83		-e 's/(tmp-k-1)/(k-2)/' \
84		-e 's/(tmp-r-1)/(r-2)/' \
85		-e 's/(tmp-v-1)/(v-2)/' \
86		-e 's/(tmp-fw-1)/(fw-2)/' \
87	)
88	bootcmd="nboot 0x81000000 0 0x${kernel_addr}"
89
90	if [ ! -z "$devtree_addr" ]; then
91		bootcmd="nand read \${devaddr} ${devtree_addr} ${devtree_size}; ${bootcmd}; run fail"
92	fi
93
94	fw_setenv -s - << EOF
95	swap_root set bootargs_tmp \${bootargs}; set bootargs \${bootargs_old}; set bootargs_old \${bootargs_tmp}
96	swap_kernel set bootcmd_tmp \${bootcmd}; set bootcmd \${bootcmd_old}; set bootcmd_old \${bootcmd_tmp}
97	fail run swap_root; run swap_kernel; save; reset
98	bootargs ${bootargs}
99	bootcmd ${bootcmd}
100	bootargs_old ${bootargs_old}
101	bootcmd_old ${bootcmd_old}
102EOF
103	v "done"
104}
105
106platform_do_upgrade_cus227() {
107	local file=$1
108	sync
109	dd bs=512 skip=1 if="${file}" | mtd write - fw-2
110	if [ "$SAVE_CONFIG" -eq 1 -a -z "$USE_REFRESH" ]; then
111		mtd -e rootfs_data jffs2write "$CONF_TAR" rootfs_data
112	fi
113	cus227_update_u_boot_env "${file}"
114}
115
116