xref: /openbmc/openpower-pnor-code-mgmt/generate-ubi (revision f8210842dc0cb13ddd8cec27f7ce23aa7f24f320)
16a3e4e45SGunnar Mills#!/bin/bash
26a3e4e45SGunnar Millsset -eo pipefail
36a3e4e45SGunnar Mills
46a3e4e45SGunnar Millshelp=$'Generate PNOR UBI image from a PNOR SquashFS Tarball
56a3e4e45SGunnar Mills
66a3e4e45SGunnar MillsGenerates a UBI, Unsorted Block Images, PNOR image from a PNOR SquashFS Tarball.
76a3e4e45SGunnar MillsThe PNOR SquashFS Tarball is generated from the generate-squashfs script.
86a3e4e45SGunnar Mills
96a3e4e45SGunnar Millsusage: generate-ubi [OPTION] <PNOR SquashFS Tarball>...
106a3e4e45SGunnar Mills
116a3e4e45SGunnar MillsOptions:
126a3e4e45SGunnar Mills   -f, --file <file>      Specify destination file. Defaults to
136a3e4e45SGunnar Mills                          `pwd`/<PNOR Tarball FILE, removing .squashfs.tar>.ubi.mtd
146a3e4e45SGunnar Mills                          (For example, "generate-ubi my.pnor.squashfs.tar"
156a3e4e45SGunnar Mills                          would generate `pwd`/my.pnor.ubi.mtd output.)
166a3e4e45SGunnar Mills   -h, --help             Display this help text and exit.
176a3e4e45SGunnar Mills'
186a3e4e45SGunnar Mills
196a3e4e45SGunnar Millswhile [[ $# -gt 0 ]]; do
206a3e4e45SGunnar Mills  key="$1"
216a3e4e45SGunnar Mills  case $key in
226a3e4e45SGunnar Mills    -f|--file)
236a3e4e45SGunnar Mills      outfile="$2"
246a3e4e45SGunnar Mills      shift 2
256a3e4e45SGunnar Mills      ;;
266a3e4e45SGunnar Mills    -h|--help)
276a3e4e45SGunnar Mills      echo "$help"
286a3e4e45SGunnar Mills      exit
296a3e4e45SGunnar Mills      ;;
306a3e4e45SGunnar Mills    *)
316a3e4e45SGunnar Mills      tarball="$1"
326a3e4e45SGunnar Mills      shift 1
336a3e4e45SGunnar Mills      ;;
346a3e4e45SGunnar Mills  esac
356a3e4e45SGunnar Millsdone
366a3e4e45SGunnar Mills
376a3e4e45SGunnar Millsif [ ! -f "${tarball}" ]; then
386a3e4e45SGunnar Mills  echo "Please enter a PNOR SquashFS Tarball."
396a3e4e45SGunnar Mills  echo "To generate PNOR SquashFS Tarball see generate-squashfs"
406a3e4e45SGunnar Mills  echo "$help"
416a3e4e45SGunnar Mills  exit 1
426a3e4e45SGunnar Millsfi
436a3e4e45SGunnar Mills
446a3e4e45SGunnar Millsif [[ -z $outfile ]]; then
456a3e4e45SGunnar Mills    # Remove .squashfs.tar from end if present and add .ubi.mtd
466a3e4e45SGunnar Mills    outfile=`pwd`/${tarball%".squashfs.tar"}.ubi.mtd
476a3e4e45SGunnar Millselse
486a3e4e45SGunnar Mills  if [[ $outfile != /* ]]; then
496a3e4e45SGunnar Mills    outfile=`pwd`/$outfile
506a3e4e45SGunnar Mills  fi
516a3e4e45SGunnar Millsfi
526a3e4e45SGunnar Mills
53*f8210842SGunnar Millsecho "Generating PNOR UBI image."
54*f8210842SGunnar Mills
55*f8210842SGunnar Millssquashfs_file_name="pnor.xz.squashfs"
56*f8210842SGunnar Mills
57*f8210842SGunnar Mills# Scratch directory for untarring and config file
58*f8210842SGunnar Millsscratch_dir=`mktemp -d`
59*f8210842SGunnar Mills
60*f8210842SGunnar Mills# Untar tarball
61*f8210842SGunnar Millstar -xvf ${tarball} -C ${scratch_dir} ${squashfs_file_name}
62*f8210842SGunnar Mills
63*f8210842SGunnar Mills# All valid PNOR SquashFS Tarballs have a file named "pnor.xz.squashfs"
64*f8210842SGunnar Millsif [ ! -f "${scratch_dir}/${squashfs_file_name}" ]; then
65*f8210842SGunnar Mills  echo "No \"${squashfs_file_name}\" file in the tarball!"
66*f8210842SGunnar Mills  rm -r "${scratch_dir}"
67*f8210842SGunnar Mills  exit 1
68*f8210842SGunnar Millsfi
69*f8210842SGunnar Mills
70*f8210842SGunnar Millsrm -r "${scratch_dir}"
71