2 # Copyright (C) 2015 OpenWrt.org
6 #Note: this code also uses some functions from nand.sh, but it is expected to be run by nand.sh, so we are not
7 #sourcing it explicitly here
9 UBNT_ERX_KERNEL_INDEX_OFFSET=160
11 ubnt_get_target_kernel() {
13 local current_kernel_index=$(hexdump -s $UBNT_ERX_KERNEL_INDEX_OFFSET -n 1 -e '/1 "%X "' ${factory_mtd})
15 if [ $current_kernel_index == "0" ]; then
17 elif [ $current_kernel_index == "1" ]; then
22 ubnt_update_target_kernel() {
26 local new_kernel_index
27 if [ $kernel_part == "kernel1" ]; then
28 new_kernel_index="\x00"
29 elif [ $kernel_part == "kernel2" ]; then
30 new_kernel_index="\x01"
32 echo 'Unknown kernel image index' >&2
36 if ! (echo -e $new_kernel_index | dd of=${factory_mtd} bs=1 count=1 seek=$UBNT_ERX_KERNEL_INDEX_OFFSET); then
37 echo 'Failed to update kernel bootup index' >&2
42 platform_upgrade_ubnt_erx() {
43 local factory_mtd=$(find_mtd_part factory)
44 if [ -z "$factory_mtd" ]; then
45 echo "cannot find factory partition" >&2
49 local kernel_part="$(ubnt_get_target_kernel ${factory_mtd})"
50 if [ -z "$kernel_part" ]; then
51 echo "cannot find factory partition" >&2
55 # This is a global defined in nand.sh, sets partition kernel will be flashed into
56 CI_KERNPART=${kernel_part}
58 #Remove volume possibly left over from stock firmware
59 local ubidev="$( nand_find_ubi "$CI_UBIPART" )"
60 if [ -z "$ubidev" ]; then
61 local mtdnum="$( find_mtd_index "$CI_UBIPART" )"
62 if [ -z "$mtdnum" ]; then
63 echo "cannot find ubi mtd partition $CI_UBIPART" >&2
66 ubiattach -m "$mtdnum"
68 ubidev="$( nand_find_ubi "$CI_UBIPART" )"
70 if [ -n "$ubidev" ]; then
71 local troot_ubivol="$( nand_find_volume $ubidev troot )"
72 [ -n "$troot_ubivol" ] && ubirmvol /dev/$ubidev -N troot || true
75 ubnt_update_target_kernel ${factory_mtd} ${kernel_part} || exit 1