contexthub: util: update scripts to include support for new tools

convenience scripts used to help generate app image and OS image.
Keep thme in sync with updated verions of tools

Bug: 28265099
Change-Id: Ic9b372031fa064b9b44862eda426da85acde9117
Signed-off-by: Alexey Polyudov <[email protected]>
diff --git a/util/nanoapp_prepare.sh b/util/nanoapp_prepare.sh
index cb8a535..546b698 100755
--- a/util/nanoapp_prepare.sh
+++ b/util/nanoapp_prepare.sh
@@ -19,60 +19,20 @@
 # Exit in error if we use an undefined variable (i.e. commit a typo).
 set -u
 
-terminate() { #cleanup and exit
-	rm -rf $stage
-	exit $1
-}
-
 usage () { #show usage and bail out
 	echo "USAGE:" >&2
-	echo "    $1 [-e <ENCR_KEY_NUM> <ENCR_KEY_FILE>] [-s <PRIV_KEY_FILE> <PUB_KEY_FILE> [<SIG_TO_CHAIN_1> [<SIG_TO_CHAIN_2> [...]]]] < app.napp > app.final.napp" >&2
-	terminate -1
+	echo "    $1 <app.napp> [-e <ENCR_KEY_NUM> <ENCR_KEY_FILE>] [-s <PRIV_KEY_FILE> <PUB_KEY_FILE> [<SIG_TO_CHAIN_1> [<SIG_TO_CHAIN_2> [...]]]]" >&2
+	exit 1
 }
 
-putchar() {
-	hexch="0123456789abcdef"
-	h=$[$1/16]
-	l=$[$1%16]
-	h=${hexch:$h:1}
-	l=${hexch:$l:1}
-	e="\x"$h$l
-	echo -ne $e
-}
-
-printhex() {
-	w3=$[$1/16777216]
-	t=$[$w3*16777216]
-	a=$[$1-$t]
-
-	w2=$[$a/65536]
-	t=$[$w2*65536]
-	a=$[$a-$t]
-
-	w1=$[$a/256]
-	w0=$[$a%256]
-
-	putchar $w0
-	putchar $w1
-	putchar $w2
-	putchar $w3
-}
-
-#save args and create temp dir
-stage=$(mktemp -dt "$(basename $0).XXXXXXXXXX")
-args=( "$@" )
-
-#sanity checks (on the user)
-if [ -t 1 ]
-then
-	usage $0
+if [ $# -ge 1 ] ; then
+app=${1%.napp}
+shift
+else
+usage $0
 fi
 
-if [ -t 0 ]
-then
-	usage $0
-fi
-
+args=( $@ )
 
 #get encryption key if it exists & encrypt app
 encr_key_num=""
@@ -92,16 +52,11 @@
 			usage $0
 		fi
 
-		nanoapp_encr encr "$encr_key_num" "$encr_key_file" > "$stage/postencr"
+		nanoapp_encr -e -i "$encr_key_num" -k "$encr_key_file" "${app}.napp" "${app}.encr.napp"
+		app="${app}.encr"
 	fi
 fi
 
-
-#if app is not encrypted, just copy it to staging area
-if [ ! -f "$stage/postencr" ]; then
-	cat > "$stage/postencr"
-fi
-
 #handle signing
 if [ ${#args[@]} -ge 1 ]
 then
@@ -124,50 +79,16 @@
 			i=$[$i+1]
 		done
 
-		#get and save file size
-		signed_sz=$(du -b "$stage/postencr" | cut -f1)
-
-		nanoapp_sign sign "$priv1" "$pub1" < "$stage/postencr" > "$stage/sig"
-
-		#pad data to 16 bytes
-		t=$signed_sz
-		while [ $[$t%16] -ne 0 ]
-		do
-			echo -ne "\0" >> "$stage/postencr"
-			t=$(du -b "$stage/postencr" | cut -f1)
-		done
-
-		#produce signed output
-		cat "$stage/postencr" "$stage/sig" "$pub1" > "$stage/signed"
+		nanoapp_sign -s -e "$priv1" -m "$pub1" "${app}.napp" "${app}.sign.napp"
 
 		#append remaining chunks
 		i=3
 		while [ $i -lt ${#args[@]} ]
 		do
-			cat "${args[$i]}" >> "$stage/signed"
+			cat "${args[$i]}" >> "${app}.sign.napp"
 			i=$[$i+1]
 		done
-
-		#create header
-		num_sigs=$[${#args[@]}-2]
-
-		echo -n SigndApp > "$stage/finished"
-		printhex $signed_sz >> "$stage/finished"
-		printhex $num_sigs >> "$stage/finished"
-		echo -ne "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" >> "$stage/finished"
-		cat "$stage/signed" >> "$stage/finished"
 	else
 		usage $0
 	fi
 fi
-
-#if app is not signed, just copy it to staging area
-if [ ! -f "$stage/finished" ]; then
-	mv "$stage/postencr" "$stage/finished"
-fi
-
-#produce output
-cat "$stage/finished"
-
-terminate 0
-
diff --git a/util/nanohub_os_update_prepare.sh b/util/nanohub_os_update_prepare.sh
index fa802fa..0227e43 100755
--- a/util/nanohub_os_update_prepare.sh
+++ b/util/nanohub_os_update_prepare.sh
@@ -19,95 +19,25 @@
 # Exit in error if we use an undefined variable (i.e. commit a typo).
 set -u
 
-terminate() { #cleanup and exit
-	rm -rf $stage
-	exit $1
-}
-
 usage () { #show usage and bail out
 	echo "USAGE:" >&2
-	echo "    $1 <PRIV_KEY_FILE> <PUB_KEY_FILE> < nanohub.update.bin > nanohub.update.signed.bin" >&2
-	terminate -1
+	echo "    $1 <PRIV_KEY_FILE> <PUB_KEY_FILE> nanohub.update.bin" >&2
+	exit 1
 }
 
-putchar() {
-	hexch="0123456789abcdef"
-	h=$[$1/16]
-	l=$[$1%16]
-	h=${hexch:$h:1}
-	l=${hexch:$l:1}
-	e="\x"$h$l
-	echo -ne $e
-}
-
-printhex() {
-	w3=$[$1/16777216]
-	t=$[$w3*16777216]
-	a=$[$1-$t]
-
-	w2=$[$a/65536]
-	t=$[$w2*65536]
-	a=$[$a-$t]
-
-	w1=$[$a/256]
-	w0=$[$a%256]
-
-	putchar $w0
-	putchar $w1
-	putchar $w2
-	putchar $w3
-}
-
-#create temp dir
-stage=$(mktemp -dt "$(basename $0).XXXXXXXXXX")
-
-
-#sanity checks (on the user)
-if [ -t 1 ]
-then
-	usage $0
+if [ $# != 3 ] ; then
+usage $0
 fi
 
-if [ -t 0 ]
-then
-	usage $0
-fi
+priv=$1
+pub=$2
+raw_image=$3
 
-#handle signing
-if [ $# -ne 2 ]
-then
-	usage $0
-fi
-priv1="$1"
-pub1="$2"
+# make signed image with header; suitable for BL
+# to be consumed by BL it has to be named nanohub.kernel.signed
+nanoapp_postprocess -n os -r ${raw_image} ${raw_image}.oshdr
+nanoapp_sign -s -e ${priv} -m ${pub} -r ${raw_image}.oshdr nanohub.kernel.signed
 
+# embed this image inside nanoapp container
 
-#save update to file in dir
-cat > "$stage/raw"
-
-#pad update to 4 byte boundary
-t=$(du -b "$stage/raw" | cut -f1)
-while [ $[$t%4] -ne 0 ]
-do
-	echo -ne "\0" >> "$stage/raw"
-	t=$(du -b "$stage/raw" | cut -f1)
-done
-
-#get and save the file size
-signed_sz=$(du -b "$stage/raw" | cut -f1)
-
-#create the header (with the marker set for signing
-echo -ne "Nanohub OS\x00\xFF" > "$stage/hdr"
-printhex $signed_sz >> "$stage/hdr"
-
-#concat the data to header
-cat "$stage/hdr" "$stage/raw" > "$stage/with_hdr"
-
-#create the signature
-nanoapp_sign sign "$priv1" "$pub1" < "$stage/with_hdr" > "$stage/sig"
-
-#produce signed output
-cat "$stage/with_hdr" "$stage/sig" "$pub1"
-
-terminate 0
-
+nanoapp_postprocess -n os nanohub.kernel.signed ${raw_image}.napp