Add overwrite capability to ARM host image script

If the user specifies an image that already exists
this script will now overwrite the specified
partitions with newly generated data.

BUG: 141692755
Test: local build and run on rock pi 4b
Change-Id: I85506925a33cb05dfcb7458b17f25b12b55f420d
diff --git a/tools/create_base_image_arm.sh b/tools/create_base_image_arm.sh
index bae7a04..e2e2c89 100755
--- a/tools/create_base_image_arm.sh
+++ b/tools/create_base_image_arm.sh
@@ -56,14 +56,11 @@
 done
 
 USE_IMAGE=`[ -z "${IMAGE}" ] && echo "0" || echo "1"`
-if [ -z $KERNEL_DIR ] || [ -z $IMAGE ]; then
+OVERWRITE=`[ -e "${IMAGE}" ] && echo "1" || echo "0"`
+if [ -z $KERNEL_DIR ]; then
 	flags_help
 	exit 1
 fi
-if [ -e "${IMAGE}" ]; then
-	echo "error: ${IMAGE} already exists"
-	exit 1
-fi
 if [ ! -e "${KERNEL_DIR}" ]; then
 	echo "error: can't find '${KERNEL_DIR}'. aborting..."
 	exit 1
@@ -79,6 +76,11 @@
 	exec sudo -E "${0}" ${@}
 fi
 
+if [ $OVERWRITE -eq 1 ]; then
+	OVERWRITE_IMAGE=${IMAGE}
+	IMAGE=`mktemp`
+fi
+
 if [ $USE_IMAGE -eq 0 ]; then
 	init_devs=`lsblk --nodeps -oNAME -n`
 	echo "Reinsert device (to write to) into PC"
@@ -497,7 +499,11 @@
 	fi
 
 	# Create final image
-	tmpimg=`mktemp`
+	if [ $OVERWRITE -eq 1 ]; then
+		tmpimg=${OVERWRITE_IMAGE}
+	else
+		tmpimg=`mktemp`
+	fi
 	truncate -s ${fs_end} ${tmpimg}
 
 	# Create GPT
@@ -526,7 +532,9 @@
 fi
 if [ ${USE_IMAGE} -eq 1 ]; then
 	chown $SUDO_USER:`id -ng $SUDO_USER` ${tmpimg}
-	mv ${tmpimg} ${IMAGE}
+	if [ $OVERWRITE -eq 0 ]; then
+		mv ${tmpimg} ${IMAGE}
+	fi
 	partx -v --delete ${device}
 	losetup -d ${device}
 fi