1# copied from allnet.sh
2
3# make sure we got uboot-envtools and fw_env.config copied over to the ramfs
4platform_add_ramfs_ubootenv() {
5	[ -e /usr/sbin/fw_printenv ] && install_bin /usr/sbin/fw_printenv /usr/sbin/fw_setenv
6	[ -e /etc/fw_env.config ] && install_file /etc/fw_env.config
7}
8append sysupgrade_pre_upgrade platform_add_ramfs_ubootenv
9
10# determine size of the main firmware partition
11platform_get_firmware_size() {
12	local dev size erasesize name
13	while read dev size erasesize name; do
14		name=${name#'"'}; name=${name%'"'}
15		case "$name" in
16			firmware)
17				printf "%d" "0x$size"
18				break
19			;;
20		esac
21	done < /proc/mtd
22}
23
24# get the first 4 bytes (magic) of a given file starting at offset in hex format
25get_magic_long_at() {
26	dd if="$1" skip=$(( $CI_BLKSZ / 4 * $2 )) bs=4 count=1 2>/dev/null | hexdump -v -n 4 -e '1/1 "%02x"'
27}
28
29get_filesize() {
30	wc -c "$1" | while read image_size _n ; do echo $image_size ; break; done
31}
32
33# scan through the update image pages until matching a magic
34platform_get_offset() {
35	offsetcount=0
36	magiclong="x"
37	if [ -n "$3" ]; then
38		offsetcount=$3
39	fi
40	while magiclong=$( get_magic_long_at "$1" "$offsetcount" ) && [ -n "$magiclong" ]; do
41		case "$magiclong" in
42			"2705"*)
43				# U-Boot image magic
44				if [ "$2" = "uImage" ]; then
45					echo $offsetcount
46					return
47				fi
48			;;
49			"68737173"|"73717368")
50				# SquashFS
51				if [ "$2" = "rootfs" ]; then
52					echo $offsetcount
53					return
54				fi
55			;;
56			"deadc0de"|"19852003")
57				# JFFS2 empty page
58				if [ "$2" = "rootfs-data" ]; then
59					echo $offsetcount
60					return
61				fi
62			;;
63		esac
64		offsetcount=$(( $offsetcount + 1 ))
65	done
66}
67
68platform_check_image_ap135() {
69
70
71	local image_size=$( get_filesize "$1" )
72	local firmware_size=$( platform_get_firmware_size )
73	[ $image_size -ge $firmware_size ] &&
74	{
75		echo "upgrade image is too big (${image_size}b > ${firmware_size}b)"
76	}
77
78	local rootfs_blockoffset=$( platform_get_offset "$1" rootfs )
79	[ -z $rootfs_blockoffset ] && {
80		echo "missing rootfs"
81		return 1
82	}
83
84	local data_blockoffset=$( platform_get_offset "$1" rootfs-data "$rootfs_blockoffset" )
85	[ -z $data_blockoffset ] && {
86		echo "rootfs doesn't have JFFS2 end marker"
87		return 1
88	}
89
90	local vmlinux_blockoffset=$( platform_get_offset "$1" uImage "$data_blockoffset" )
91	[ -z $vmlinux_blockoffset ] && {
92		echo "vmlinux-uImage not found"
93		return 1
94	}
95
96	return 0
97}
98
99platform_do_upgrade_ap135() {
100	local firmware_base_addr=$( printf "%d" "$1" )
101	local vmlinux_blockoffset=$( platform_get_offset "$2" uImage )
102	if [ ! -n "$vmlinux_blockoffset" ]; then
103		echo "can't determine new kernel address"
104		return 1
105	fi
106	local vmlinux_offset=$(( $vmlinux_blockoffset * $CI_BLKSZ ))
107	local vmlinux_addr=$(( $firmware_base_addr + $vmlinux_offset ))
108	local vmlinux_hexaddr=0x$( printf "%08x" "$vmlinux_addr" )
109
110	local curr_linux_addr=$(fw_printenv bootcmd | sed 's/.*0x/0x/g')
111	[ $(printf "0x%x\n" $curr_linux_addr) != $vmlinux_hexaddr ] && {
112		local fw_printenv=/usr/sbin/fw_printenv
113		[ ! -x "$fw_printenv" ] && { 
114			echo "Please install uboot-envtools, aborting!"
115			return 1
116		}
117
118		[ ! -r "/etc/fw_env.config" ] && {
119			echo "/etc/fw_env.config is missing, aborting!"
120			return 1
121		}
122
123		echo "Updating boot command to: bootm $vmlinux_hexaddr"
124		fw_setenv bootcmd "bootm $vmlinux_hexaddr" || {
125			echo "failed to	update U-Boot environment, aborting!"
126			return 1
127		}
128	}
129	shift
130	default_do_upgrade "$@"
131}
132