[OpenWrt-Devel] [PATCH] procd: reorder the arguments of command invocations

Gianluca Anzolin gianluca at sottospazio.it
Sat Jan 10 12:54:51 EST 2015


Some commands in the script /lib/upgrade/nand.sh (provided by
procd) use option arguments after non-option arguments.

This unfortunately doesn't work with musl because it doesn't yet support
permutations of the arguments: when getopt finds a non-option argument
it returns -1 and ignores any option which may follow.

One visible side effect is that sysupgrade doesn't work with musl/NAND
tarballs.

The following patch reorders the command invocations by placing the
options before any non-option argument.

Tested on a Netgear WNDR4300 built with ubifs support and musl libc.

Signed-off-by: Gianluca Anzolin <gianluca at sottospazio.it>
---
 package/system/procd/files/nand.sh | 40 +++++++++++++++++++-------------------
 1 file changed, 20 insertions(+), 20 deletions(-)

diff --git a/package/system/procd/files/nand.sh b/package/system/procd/files/nand.sh
index 7fb9343..a22381d 100644
--- a/package/system/procd/files/nand.sh
+++ b/package/system/procd/files/nand.sh
@@ -58,7 +58,7 @@ nand_get_magic_long() {
 }
 
 get_magic_long_tar() {
-	( tar xf $1 $2 -O | dd bs=4 count=1 | hexdump -v -n 4 -e '1/1 "%02x"') 2> /dev/null
+	( tar -O -xf $1 $2 | dd bs=4 count=1 | hexdump -v -n 4 -e '1/1 "%02x"') 2> /dev/null
 }
 
 identify_magic() {
@@ -132,13 +132,13 @@ nand_upgrade_prepare_ubi() {
 	fi
 
 	if [ ! "$ubidev" ]; then
-		ubiformat /dev/mtd$mtdnum -y
+		ubiformat -y /dev/mtd$mtdnum
 		ubiattach -m "$mtdnum"
 		sync
 		ubidev="$( nand_find_ubi "$CI_UBIPART" )"
 		[ "$has_env" -gt 0 ] && {
-			ubimkvol /dev/$ubidev -n 0 -N ubootenv -s 1MiB
-			ubimkvol /dev/$ubidev -n 1 -N ubootenv2 -s 1MiB
+			ubimkvol -n 0 -N ubootenv -s 1MiB /dev/$ubidev
+			ubimkvol -n 1 -N ubootenv2 -s 1MiB /dev/$ubidev
 		}
 	fi
 
@@ -157,13 +157,13 @@ nand_upgrade_prepare_ubi() {
 	fi
 
 	# kill volumes
-	[ "$kern_ubivol" ] && ubirmvol /dev/$ubidev -N kernel || true
-	[ "$root_ubivol" ] && ubirmvol /dev/$ubidev -N rootfs || true
-	[ "$data_ubivol" ] && ubirmvol /dev/$ubidev -N rootfs_data || true
+	[ "$kern_ubivol" ] && ubirmvol -N kernel /dev/$ubidev || true
+	[ "$root_ubivol" ] && ubirmvol -N rootfs /dev/$ubidev || true
+	[ "$data_ubivol" ] && ubirmvol -N rootfs_data /dev/$ubidev || true
 
 	# update kernel
 	if [ "$has_kernel" = "1" ]; then
-		if ! ubimkvol /dev/$ubidev -N kernel -s $kernel_length; then
+		if ! ubimkvol -N kernel -s $kernel_length /dev/$ubidev ; then
 			echo "cannot create kernel volume"
 			return 1;
 		fi
@@ -176,14 +176,14 @@ nand_upgrade_prepare_ubi() {
 	else
 		root_size_param="-s $rootfs_length"
 	fi
-	if ! ubimkvol /dev/$ubidev -N rootfs $root_size_param; then
+	if ! ubimkvol -N rootfs $root_size_param /dev/$ubidev ; then
 		echo "cannot create rootfs volume"
 		return 1;
 	fi
 
 	# create rootfs_data for non-ubifs rootfs
 	if [ "$rootfs_type" != "ubifs" ]; then
-		if ! ubimkvol /dev/$ubidev -N rootfs_data -m; then
+		if ! ubimkvol -N rootfs_data -m /dev/$ubidev ; then
 			echo "cannot initialize rootfs_data volume"
 			return 1
 		fi
@@ -219,7 +219,7 @@ nand_upgrade_ubinized() {
 	local mtddev="/dev/mtd${mtdnum}"
 	ubidetach -p "${mtddev}" || true
 	sync
-	ubiformat "${mtddev}" -y -f "${ubi_file}"
+	ubiformat -y -f "${mtddev}" "${ubi_file}"
 	ubiattach -p "${mtddev}"
 	nand_do_upgrade_success
 }
@@ -232,7 +232,7 @@ nand_upgrade_ubifs() {
 	
 	local ubidev="$( nand_find_ubi "$CI_UBIPART" )"
 	local root_ubivol="$(nand_find_volume $ubidev rootfs)"
-	ubiupdatevol /dev/$root_ubivol -s $rootfs_length $1
+	ubiupdatevol -s $rootfs_length /dev/$root_ubivol $1
 
 	nand_do_upgrade_success
 }
@@ -242,8 +242,8 @@ nand_upgrade_tar() {
 	local board_name="$(cat /tmp/sysinfo/board_name)"
 	local kernel_mtd="$(find_mtd_index $CI_KERNPART)"
 
-	local kernel_length=`(tar xf $tar_file sysupgrade-$board_name/kernel -O | wc -c) 2> /dev/null`
-	local rootfs_length=`(tar xf $tar_file sysupgrade-$board_name/root -O | wc -c) 2> /dev/null`
+	local kernel_length=`(tar -O -xf $tar_file sysupgrade-$board_name/kernel | wc -c) 2> /dev/null`
+	local rootfs_length=`(tar -O -xf $tar_file sysupgrade-$board_name/root | wc -c) 2> /dev/null`
 
 	local rootfs_type="$(identify_tar "$tar_file" sysupgrade-$board_name/root)"
 
@@ -251,7 +251,7 @@ nand_upgrade_tar() {
 	local has_env=0
 
 	[ "$kernel_length" != 0 -a -n "$kernel_mtd" ] && {
-		tar xf $tar_file sysupgrade-$board_name/kernel -O | mtd write - $CI_KERNPART
+		tar -O -xf $tar_file sysupgrade-$board_name/kernel | mtd write - $CI_KERNPART
 	}
 	[ "$kernel_length" = 0 -o ! -z "$kernel_mtd" ] && has_kernel=0
 
@@ -260,13 +260,13 @@ nand_upgrade_tar() {
 	local ubidev="$( nand_find_ubi "$CI_UBIPART" )"
 	[ "$has_kernel" = "1" ] && {
 		local kern_ubivol="$(nand_find_volume $ubidev kernel)"
-	 	tar xf $tar_file sysupgrade-$board_name/kernel -O | \
-			ubiupdatevol /dev/$kern_ubivol -s $kernel_length -
+		tar -O -xf $tar_file sysupgrade-$board_name/kernel | \
+			ubiupdatevol -s $kernel_length /dev/$kern_ubivol -
 	}
 
 	local root_ubivol="$(nand_find_volume $ubidev rootfs)"
-	tar xf $tar_file sysupgrade-$board_name/root -O | \
-		ubiupdatevol /dev/$root_ubivol -s $rootfs_length -
+	tar -O -xf $tar_file sysupgrade-$board_name/root | \
+		ubiupdatevol -s $rootfs_length /dev/$root_ubivol -
 
 	nand_do_upgrade_success
 }
@@ -340,7 +340,7 @@ append sysupgrade_pre_upgrade nand_upgrade_stage1
 nand_do_platform_check() {
 	local board_name="$1"
 	local tar_file="$2"
-	local control_length=`(tar xf $tar_file sysupgrade-$board_name/CONTROL -O | wc -c) 2> /dev/null`
+	local control_length=`(tar -O -xf $tar_file sysupgrade-$board_name/CONTROL | wc -c) 2> /dev/null`
 	local file_type="$(identify $2)"
 
 	[ "$control_length" = 0 -a "$file_type" != "ubi" -a "$file_type" != "ubifs" ] && {
-- 
2.2.1
_______________________________________________
openwrt-devel mailing list
openwrt-devel at lists.openwrt.org
https://lists.openwrt.org/cgi-bin/mailman/listinfo/openwrt-devel



More information about the openwrt-devel mailing list