xref: /openbmc/openpower-pnor-code-mgmt/generate-ubi (revision 927ac0fa3a08c9f4a89e9b466cebe5495fc89160)
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
53f8210842SGunnar Millsecho "Generating PNOR UBI image."
54f8210842SGunnar Mills
55f8210842SGunnar Millssquashfs_file_name="pnor.xz.squashfs"
56*927ac0faSGunnar Millsmanifest_file_name="MANIFEST"
57f8210842SGunnar Mills
58f8210842SGunnar Mills# Scratch directory for untarring and config file
59f8210842SGunnar Millsscratch_dir=`mktemp -d`
60f8210842SGunnar Mills
61*927ac0faSGunnar Millssquashfs_file=${scratch_dir}/${squashfs_file_name}
62*927ac0faSGunnar Millsmanifest_file=${scratch_dir}/${manifest_file_name}
63f8210842SGunnar Mills# Untar tarball
64*927ac0faSGunnar Millstar -xvf ${tarball} -C ${scratch_dir} ${squashfs_file_name} ${manifest_file_name}
65f8210842SGunnar Mills
66f8210842SGunnar Mills# All valid PNOR SquashFS Tarballs have a file named "pnor.xz.squashfs"
67*927ac0faSGunnar Millsif [ ! -f "${squashfs_file}" ]; then
68f8210842SGunnar Mills  echo "No \"${squashfs_file_name}\" file in the tarball!"
69f8210842SGunnar Mills  rm -r "${scratch_dir}"
70f8210842SGunnar Mills  exit 1
71f8210842SGunnar Millsfi
72f8210842SGunnar Mills
73*927ac0faSGunnar Mills# Need the manifest file for calculating the version id
74*927ac0faSGunnar Millsif [ ! -f "${manifest_file}" ]; then
75*927ac0faSGunnar Mills  echo "No \"${manifest_file_name}\" file in the tarball!"
76*927ac0faSGunnar Mills  rm -r "${scratch_dir}"
77*927ac0faSGunnar Mills  exit 1
78*927ac0faSGunnar Millsfi
79*927ac0faSGunnar Mills
80*927ac0faSGunnar Mills# Flash page size in bytes
81*927ac0faSGunnar MillsFLASH_PAGE_SIZE="1"
82*927ac0faSGunnar Mills# kibibyte(KiB)
83*927ac0faSGunnar MillsFLASH_PEB_SIZE="64"
84*927ac0faSGunnar Mills# Future enhancement would be to allow this to be passed in
85*927ac0faSGunnar Mills# 128MiB (128 * 1024)
86*927ac0faSGunnar MillsFLASH_SIZE="131072"
87*927ac0faSGunnar Mills
88*927ac0faSGunnar Mills# Create UBI volume
89*927ac0faSGunnar Millsadd_volume()
90*927ac0faSGunnar Mills{
91*927ac0faSGunnar Mills  config_file=$1
92*927ac0faSGunnar Mills  vol_id=$2
93*927ac0faSGunnar Mills  vol_type=$3
94*927ac0faSGunnar Mills  vol_name=$4
95*927ac0faSGunnar Mills  image=$5
96*927ac0faSGunnar Mills  vol_size=$6
97*927ac0faSGunnar Mills
98*927ac0faSGunnar Mills  echo \[$vol_name\] >> $config_file
99*927ac0faSGunnar Mills  echo mode=ubi >> $config_file
100*927ac0faSGunnar Mills  if [ ! -z $image ]; then
101*927ac0faSGunnar Mills    echo image=$image >> $config_file
102*927ac0faSGunnar Mills  fi
103*927ac0faSGunnar Mills  echo vol_type=$vol_type >> $config_file
104*927ac0faSGunnar Mills  echo vol_name=$vol_name >> $config_file
105*927ac0faSGunnar Mills  echo vol_id=$vol_id >> $config_file
106*927ac0faSGunnar Mills  if [ ! -z $vol_size ]; then
107*927ac0faSGunnar Mills    echo vol_size=$vol_size >> $config_file
108*927ac0faSGunnar Mills  fi
109*927ac0faSGunnar Mills}
110*927ac0faSGunnar Mills
111*927ac0faSGunnar Mills# Create an image with all 1's
112*927ac0faSGunnar Millsmk_nor_image()
113*927ac0faSGunnar Mills{
114*927ac0faSGunnar Mills  image_dst=$1
115*927ac0faSGunnar Mills  image_size_kb=$2
116*927ac0faSGunnar Mills  dd if=/dev/zero bs=1k count=$image_size_kb | tr '\000' '\377' > $image_dst
117*927ac0faSGunnar Mills}
118*927ac0faSGunnar Mills
119*927ac0faSGunnar Mills# Used to temporary hold the UBI volume
120*927ac0faSGunnar Millstmpfile=$(mktemp ${scratch_dir}/ubinized.XXXXXX)
121*927ac0faSGunnar Mills
122*927ac0faSGunnar Mills# Configuration file used to create UBI image
123*927ac0faSGunnar Millsconfig_file=${scratch_dir}/ubinize-PNOR.cfg
124*927ac0faSGunnar Mills
125*927ac0faSGunnar Mills# The version is listed in the MANIFEST file as "version=v1.99.10-19"
126*927ac0faSGunnar Mills# Use the version to calculate the version id, a unique 8 hexadecimal digit id
127*927ac0faSGunnar Millsversion_id=$(sed -ne '/version=/ {s/version=//;p}' ${manifest_file} | head -n1 | \
128*927ac0faSGunnar Mills  tr -d '\n' | sha512sum | cut -b 1-8)
129*927ac0faSGunnar Mills
130*927ac0faSGunnar Millsadd_volume $config_file 0 static pnor-ro-${version_id} ${squashfs_file}
131*927ac0faSGunnar Millsadd_volume $config_file 1 dynamic pnor-prsv "" 2MiB
132*927ac0faSGunnar Millsadd_volume $config_file 2 dynamic pnor-rw-${version_id} "" 16MiB
133*927ac0faSGunnar Mills
134*927ac0faSGunnar Mills# Build the UBI image
135*927ac0faSGunnar Millsubinize -p ${FLASH_PEB_SIZE}KiB -m ${FLASH_PAGE_SIZE} -o ${tmpfile} $config_file
136*927ac0faSGunnar Millsmk_nor_image ${outfile} ${FLASH_SIZE}
137*927ac0faSGunnar Millsdd bs=1k conv=notrunc seek=0 if=${tmpfile} of=${outfile}
138*927ac0faSGunnar Mills
139*927ac0faSGunnar Millsecho "PNOR UBI image at ${outfile}"
140f8210842SGunnar Millsrm -r "${scratch_dir}"
141