generate-tar: Extract PNOR contents to a subdir

Extract the PNOR contents to a subdir to support creating an ext4 image
that takes a directory as a parameter instead of individual files.

Tested: Generated a squashfs image and verified it contained the PNOR
        partitions.

Change-Id: I4aef816b55833b5e4df8e93c29d9484c2c9b3d3d
Signed-off-by: Adriana Kobylak <anoo@us.ibm.com>
diff --git a/generate-tar b/generate-tar
index f0b94a9..536b11c 100755
--- a/generate-tar
+++ b/generate-tar
@@ -147,22 +147,25 @@
 
 echo "Parsing PNOR TOC..."
 
-pflash --partition=part --read=${scratch_dir}/part -F ${pnorfile}
-pflash --partition=VERSION --read=${scratch_dir}/VERSION -F ${pnorfile}
-version_size=$(du -k ${scratch_dir}/VERSION | head -1 | cut -f 1)
-magic_number=$(xxd -p -l 4 ${scratch_dir}/VERSION)
+pnor_dir="${scratch_dir}/pnor"
+mkdir ${pnor_dir}
+
+pflash --partition=part --read=${pnor_dir}/part -F ${pnorfile}
+pflash --partition=VERSION --read=${pnor_dir}/VERSION -F ${pnorfile}
+version_size=$(du -k ${pnor_dir}/VERSION | head -1 | cut -f 1)
+magic_number=$(xxd -p -l 4 ${pnor_dir}/VERSION)
 # Check if VERSION is signed. A signed version partition will have an extra
 # 4K header starting with the magic number 0x17082011, see:
 # https://github.com/open-power/skiboot/blob/master/libstb/container.h#L47
 if [ "$version_size" == "8" -a "$magic_number" == "17082011" ]; then
   # Advance past the STB header (4K, indexed from 1)
-  cp ${scratch_dir}/VERSION ${scratch_dir}/VERSION_FULL
-  tail --bytes=+4097 ${scratch_dir}/VERSION_FULL > ${scratch_dir}/VERSION
+  cp ${pnor_dir}/VERSION ${pnor_dir}/VERSION_FULL
+  tail --bytes=+4097 ${pnor_dir}/VERSION_FULL > ${pnor_dir}/VERSION
 fi
 {
-  version=$(head -n 1 ${scratch_dir}/VERSION)
+  version=$(head -n 1 ${pnor_dir}/VERSION)
   echo "version=$version"
-  extended_version=$(echo $(tail -n +2 ${scratch_dir}/VERSION)|tr ' ' ',')
+  extended_version=$(echo $(tail -n +2 ${pnor_dir}/VERSION)|tr ' ' ',')
   echo "extended_version=$extended_version"
   while read line; do
     if [[ $line == "ID="* ]]; then
@@ -172,7 +175,7 @@
 
         id=${fields[0]##*=}
         offset=$((${ffs_entry_size} * 10#${id} + ${vercheck_offset}))
-        vercheck=$(xxd -p -l  0x1 -seek ${offset} ${scratch_dir}/part)
+        vercheck=$(xxd -p -l  0x1 -seek ${offset} ${pnor_dir}/part)
         export flags=$(pflash --detail=$((10#$id)) -F ${pnorfile} | grep "\[" |
                 sed 's/....$//' | tr '\n' ',' | sed 's/.$//')
         if [[ $flags != "" ]]; then
@@ -192,12 +195,12 @@
     fi
   # Don't need the BACKUP_PART partition
   done < <(pflash --info -F ${pnorfile} | grep -v "BACKUP")
-} > ${scratch_dir}/${tocfile}
+} > ${pnor_dir}/${tocfile}
 
 for partition in "${partitions[@]}"; do
   echo "Reading ${partition}..."
   pflash --partition=${partition} \
-    --read=${scratch_dir}/${partition} \
+    --read=${pnor_dir}/${partition} \
     -F ${pnorfile}
 done
 
@@ -208,9 +211,10 @@
 
 if [[ "${image_type}" == "squashfs" ]]; then
   echo "Creating SquashFS image..."
-  # Prepare pnor file in scratch_dir
+  # Prepare pnor file in ${pnor_dir}
+  cd "${pnor_dir}"
+  mksquashfs ${tocfile} ${partitions[*]} ${scratch_dir}/pnor.xz.squashfs
   cd "${scratch_dir}"
-  mksquashfs ${tocfile} ${partitions[*]} pnor.xz.squashfs
   files_to_sign+=" pnor.xz.squashfs"
 else
   cp ${pnorfile} ${scratch_dir}