Static layout: Generate tar.gz for static layout pnor

Add --image_type option to specify the image type of generated PNOR in
tarball.
Support "squashfs" and "static" image types.
Rename `generate-squashfs` to `generate-tar` to reflect that this util
is to generate a tarball instead of squashfs.

Example of usage:

    # Generate unsigned tarball for Witherspoon
    ./generate-tar -i squashfs witherspoon.pnor

    # Generate signed tarball with default key for Witherspoon
    ./generate-tar -i squashfs -s -- witherspoon.pnor

    # Generate unsigned tarball for Romulus
    ./generate-tar -i static romulus.pnor

    # Generate signed tarball with default key for Romulus
    ./generate-tar -i static -s -- romulus.pnor

Tested: Verify the generated tarball contains correct files.

Change-Id: Ibb66605663b28888bd643baf92d6e4bdbf60c206
Signed-off-by: Lei YU <mine260309@gmail.com>
diff --git a/generate-squashfs b/generate-tar
similarity index 72%
rename from generate-squashfs
rename to generate-tar
index 71f4e5e..d90aefa 100755
--- a/generate-squashfs
+++ b/generate-tar
@@ -1,19 +1,26 @@
 #!/bin/bash
 set -eo pipefail
 
-help=$'Generate Tarball with PNOR SquashFS image and MANIFEST Script
+help=$'Generate Tarball with PNOR image and MANIFEST Script
 
-Generates a PNOR SquashFS image from the PNOR image
+Generates a PNOR SquashFS image from the PNOR image for VPNOR,
+Or use a static layout raw PNOR image,
 Creates a MANIFEST for image verification and recreation
-Packages the SquashFS image and MANIFEST together in a tarball
+Packages the image and MANIFEST together in a tarball
 
-usage: generate-squashfs [OPTION] <PNOR FILE>...
+usage: generate-tar [OPTION] <PNOR FILE>...
 
 Options:
+   -i, --image <squashfs|static>
+                          Generate SquashFS image or use static PNOR
    -f, --file <file>      Specify destination file. Defaults to
-                          `pwd`/<PNOR FILE>.pnor.squashfs.tar if unspecified.
-                          (For example, "generate-squashfs my.pnor" would
-                          generate `pwd`/my.pnor.squashfs.tar output.)
+                          `pwd`/<PNOR FILE>.pnor.<image_type>.tar[.gz] if
+                          unspecified.
+                          (For example,
+                          * "generate-tar -i squashfs my.pnor" would generate
+                          `pwd`/my.pnor.squashfs.tar
+                          * "generate-tar -i static my.pnor" would generate
+                          `pwd`/my.pnor.static.tar.gz)
    -s, --sign <path>      Sign the image. The optional path argument specifies
                           the private key file. Defaults to the bash variable
                           PRIVATE_KEY_PATH if available, or else uses the
@@ -46,6 +53,7 @@
 let vercheck_offset=112
 do_sign=false
 private_key_path="${PRIVATE_KEY_PATH}"
+image_type=""
 outfile=""
 declare -a partitions=()
 tocfile="pnor.toc"
@@ -53,6 +61,10 @@
 while [[ $# -gt 0 ]]; do
   key="$1"
   case $key in
+    -i|--image)
+      image_type="$2"
+      shift 2
+      ;;
     -f|--file)
       outfile="$2"
       shift 2
@@ -83,11 +95,26 @@
   exit 1
 fi
 
+if [[ "${image_type}" == "squashfs" ]]; then
+  echo "Will generate squashfs image for VPNOR"
+elif [[ "${image_type}" == "static" ]]; then
+  echo "Will use static image for PNOR"
+else
+  echo "Please specify the image type, \"squashfs\" or \"static\""
+  echo
+  echo "$help"
+  exit 1
+fi
+
 if [[ -z $outfile ]]; then
     if [[ ${pnorfile##*.} == "pnor" ]]; then
-        outfile=`pwd`/${pnorfile##*/}.squashfs.tar
+        outfile=`pwd`/${pnorfile##*/}.$image_type.tar
     else
-        outfile=`pwd`/${pnorfile##*/}.pnor.squashfs.tar
+        outfile=`pwd`/${pnorfile##*/}.pnor.$image_type.tar
+    fi
+    if [[ "${image_type}" == "static" ]]; then
+        # Append .gz so the tarball is compressed
+        outfile=$outfile.gz
     fi
 else
     if [[ $outfile != /* ]]; then
@@ -95,6 +122,7 @@
     fi
 fi
 
+
 scratch_dir=`mktemp -d`
 
 if [[ "${do_sign}" == true ]]; then
@@ -172,12 +200,24 @@
     -F ${pnorfile}
 done
 
-echo "Creating SquashFS image..."
-cd "${scratch_dir}"
-mksquashfs ${tocfile} ${partitions[*]} pnor.xz.squashfs
+manifest_location="MANIFEST"
+files_to_sign="$manifest_location $public_key_file"
+
+# Go to scratch_dir
+
+if [[ "${image_type}" == "squashfs" ]]; then
+  echo "Creating SquashFS image..."
+  # Prepare pnor file in scratch_dir
+  cd "${scratch_dir}"
+  mksquashfs ${tocfile} ${partitions[*]} pnor.xz.squashfs
+  files_to_sign+=" pnor.xz.squashfs"
+else
+  cp ${pnorfile} ${scratch_dir}
+  cd "${scratch_dir}"
+  files_to_sign+=" $(basename ${pnorfile})"
+fi
 
 echo "Creating MANIFEST for the image"
-manifest_location="MANIFEST"
 echo -e "purpose=xyz.openbmc_project.Software.Version.VersionPurpose.Host\nversion=$version\n\
 extended_version=$extended_version" >> $manifest_location
 
@@ -187,15 +227,20 @@
   echo KeyType="${key_type}" >> $manifest_location
   echo HashType="RSA-SHA256" >> $manifest_location
 
-  for file in pnor.xz.squashfs $manifest_location $public_key_file; do
+  for file in $files_to_sign; do
     openssl dgst -sha256 -sign ${private_key_path} -out "${file}.sig" $file
   done
 
-  additional_files="${public_key_file} *.sig"
+  additional_files="*.sig"
 fi
 
-echo "Generating tarball to contain the SquashFS image and its MANIFEST"
-tar -cvf $outfile $manifest_location pnor.xz.squashfs $additional_files
+if [[ "${image_type}" == "squashfs" ]]; then
+  echo "Generating tarball to contain the SquashFS image and its MANIFEST"
+  tar -cvf $outfile $files_to_sign $additional_files
+  echo "SquashFSTarball at ${outfile}"
+else
+  tar -czvf $outfile $files_to_sign $additional_files
+  echo "Static layout tarball at $outfile"
+fi
 
-echo "SquashFSTarball at ${outfile}"
 rm -r "${scratch_dir}"
diff --git a/generate-ubi b/generate-ubi
index 5c2b1b8..d54634c 100755
--- a/generate-ubi
+++ b/generate-ubi
@@ -4,7 +4,7 @@
 help=$'Generate PNOR UBI image from a PNOR SquashFS Tarball
 
 Generates a UBI, Unsorted Block Images, PNOR image from a PNOR SquashFS Tarball.
-The PNOR SquashFS Tarball is generated from the generate-squashfs script.
+The PNOR SquashFS Tarball is generated from the generate-tar script.
 
 usage: generate-ubi [OPTION] <PNOR SquashFS Tarball>...
 
@@ -44,7 +44,7 @@
 
 if [ ! -f "${tarball}" ]; then
   echo "Please enter a PNOR SquashFS Tarball."
-  echo "To generate PNOR SquashFS Tarball see generate-squashfs"
+  echo "To generate PNOR SquashFS Tarball see generate-tar"
   echo "$help"
   exit 1
 fi