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.
7dcb3fd79SLei YUThe PNOR SquashFS Tarball is generated from the generate-tar 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
13*c1a97bf8SAdriana Kobylak$(pwd)/<PNOR Tarball FILE, removing .squashfs.tar>.ubi.mtd
146a3e4e45SGunnar Mills(For example, "generate-ubi my.pnor.squashfs.tar"
15*c1a97bf8SAdriana Kobylakwould generate $(pwd)/my.pnor.ubi.mtd output.)
168f618671SGunnar Mills-s, --size <MiB>       Specify the size of the PNOR UBI image in MiBs.
178f618671SGunnar MillsDefaults to 128.
186a3e4e45SGunnar Mills-h, --help             Display this help text and exit.
196a3e4e45SGunnar Mills'
208f618671SGunnar Mills# 128MiB is the default image size
218f618671SGunnar Millsimage_size="128"
226a3e4e45SGunnar Mills
236a3e4e45SGunnar Millswhile [[ $# -gt 0 ]]; do
246a3e4e45SGunnar Mills  key="$1"
256a3e4e45SGunnar Mills  case $key in
266a3e4e45SGunnar Mills    -f|--file)
276a3e4e45SGunnar Mills      outfile="$2"
286a3e4e45SGunnar Mills      shift 2
296a3e4e45SGunnar Mills      ;;
308f618671SGunnar Mills    -s|--size)
318f618671SGunnar Mills      image_size="$2"
328f618671SGunnar Mills      shift 2
338f618671SGunnar Mills      ;;
346a3e4e45SGunnar Mills    -h|--help)
356a3e4e45SGunnar Mills      echo "$help"
366a3e4e45SGunnar Mills      exit
376a3e4e45SGunnar Mills      ;;
386a3e4e45SGunnar Mills    *)
396a3e4e45SGunnar Mills      tarball="$1"
406a3e4e45SGunnar Mills      shift 1
416a3e4e45SGunnar Mills      ;;
426a3e4e45SGunnar Mills  esac
436a3e4e45SGunnar Millsdone
446a3e4e45SGunnar Mills
456a3e4e45SGunnar Millsif [ ! -f "${tarball}" ]; then
466a3e4e45SGunnar Mills  echo "Please enter a PNOR SquashFS Tarball."
47dcb3fd79SLei YU  echo "To generate PNOR SquashFS Tarball see generate-tar"
486a3e4e45SGunnar Mills  echo "$help"
496a3e4e45SGunnar Mills  exit 1
506a3e4e45SGunnar Millsfi
516a3e4e45SGunnar Mills
526a3e4e45SGunnar Millsif [[ -z $outfile ]]; then
536a3e4e45SGunnar Mills    # Remove .squashfs.tar from end if present and add .ubi.mtd
54*c1a97bf8SAdriana Kobylak    outfile=$(pwd)/${tarball%".squashfs.tar"}.ubi.mtd
556a3e4e45SGunnar Millselse
566a3e4e45SGunnar Mills  if [[ $outfile != /* ]]; then
57*c1a97bf8SAdriana Kobylak    outfile=$(pwd)/$outfile
586a3e4e45SGunnar Mills  fi
596a3e4e45SGunnar Millsfi
606a3e4e45SGunnar Mills
61f8210842SGunnar Millsecho "Generating PNOR UBI image."
62f8210842SGunnar Mills
63f8210842SGunnar Millssquashfs_file_name="pnor.xz.squashfs"
64927ac0faSGunnar Millsmanifest_file_name="MANIFEST"
65f8210842SGunnar Mills
66f8210842SGunnar Mills# Scratch directory for untarring and config file
67*c1a97bf8SAdriana Kobylakscratch_dir=$(mktemp -d)
68f8210842SGunnar Mills
690e30f86cSGunnar Mills# Make sure scratch directory always gets cleaned up
70*c1a97bf8SAdriana Kobylaktrap '{ rm -r ${scratch_dir}; }' EXIT
710e30f86cSGunnar Mills
72927ac0faSGunnar Millssquashfs_file=${scratch_dir}/${squashfs_file_name}
73927ac0faSGunnar Millsmanifest_file=${scratch_dir}/${manifest_file_name}
74f8210842SGunnar Mills# Untar tarball
75*c1a97bf8SAdriana Kobylaktar -xvf "${tarball}" -C "${scratch_dir}" ${squashfs_file_name} ${manifest_file_name}
76f8210842SGunnar Mills
77f8210842SGunnar Mills# All valid PNOR SquashFS Tarballs have a file named "pnor.xz.squashfs"
78927ac0faSGunnar Millsif [ ! -f "${squashfs_file}" ]; then
79f8210842SGunnar Mills  echo "No \"${squashfs_file_name}\" file in the tarball!"
80f8210842SGunnar Mills  exit 1
81f8210842SGunnar Millsfi
82f8210842SGunnar Mills
83927ac0faSGunnar Mills# Need the manifest file for calculating the version id
84927ac0faSGunnar Millsif [ ! -f "${manifest_file}" ]; then
85927ac0faSGunnar Mills  echo "No \"${manifest_file_name}\" file in the tarball!"
86927ac0faSGunnar Mills  exit 1
87927ac0faSGunnar Millsfi
88927ac0faSGunnar Mills
89927ac0faSGunnar Mills# Flash page size in bytes
90927ac0faSGunnar MillsFLASH_PAGE_SIZE="1"
91927ac0faSGunnar Mills# kibibyte(KiB)
92927ac0faSGunnar MillsFLASH_PEB_SIZE="64"
938f618671SGunnar Mills
948f618671SGunnar Mills# Convert image size from MiB to KiB
95*c1a97bf8SAdriana Kobylakimage_size=$((image_size * 1024))
96927ac0faSGunnar Mills
97927ac0faSGunnar Mills# Create UBI volume
98927ac0faSGunnar Millsadd_volume()
99927ac0faSGunnar Mills{
100927ac0faSGunnar Mills  config_file=$1
101927ac0faSGunnar Mills  vol_id=$2
102927ac0faSGunnar Mills  vol_type=$3
103927ac0faSGunnar Mills  vol_name=$4
104927ac0faSGunnar Mills  image=$5
105927ac0faSGunnar Mills  vol_size=$6
106927ac0faSGunnar Mills
107*c1a97bf8SAdriana Kobylak{
108*c1a97bf8SAdriana Kobylak  echo \["$vol_name"\]
109*c1a97bf8SAdriana Kobylak  echo mode=ubi
110*c1a97bf8SAdriana Kobylak} >> "$config_file"
111*c1a97bf8SAdriana Kobylak  if [ -n "$image" ]; then
112*c1a97bf8SAdriana Kobylak    echo image="$image" >> "$config_file"
113927ac0faSGunnar Mills  fi
114*c1a97bf8SAdriana Kobylak{
115*c1a97bf8SAdriana Kobylak  echo vol_type="$vol_type"
116*c1a97bf8SAdriana Kobylak  echo vol_name="$vol_name"
117*c1a97bf8SAdriana Kobylak  echo vol_id="$vol_id"
118*c1a97bf8SAdriana Kobylak} >> "$config_file"
119*c1a97bf8SAdriana Kobylak  if [ -n "$vol_size" ]; then
120*c1a97bf8SAdriana Kobylak    echo vol_size="$vol_size" >> "$config_file"
121927ac0faSGunnar Mills  fi
122927ac0faSGunnar Mills}
123927ac0faSGunnar Mills
124927ac0faSGunnar Mills# Create an image with all 1's
125927ac0faSGunnar Millsmk_nor_image()
126927ac0faSGunnar Mills{
127927ac0faSGunnar Mills  image_dst=$1
128927ac0faSGunnar Mills  image_size_kb=$2
129*c1a97bf8SAdriana Kobylak  dd if=/dev/zero bs=1k count="$image_size_kb" | tr '\000' '\377' > "$image_dst"
130927ac0faSGunnar Mills}
131927ac0faSGunnar Mills
1320e30f86cSGunnar Mills# Used to temporary hold the UBI image
133*c1a97bf8SAdriana Kobylaktmpfile=$(mktemp "${scratch_dir}"/ubinized.XXXXXX)
134927ac0faSGunnar Mills
135927ac0faSGunnar Mills# Configuration file used to create UBI image
136927ac0faSGunnar Millsconfig_file=${scratch_dir}/ubinize-PNOR.cfg
137927ac0faSGunnar Mills
138927ac0faSGunnar Mills# The version is listed in the MANIFEST file as "version=v1.99.10-19"
139927ac0faSGunnar Mills# Use the version to calculate the version id, a unique 8 hexadecimal digit id
140*c1a97bf8SAdriana Kobylakversion_id=$(sed -ne '/version=/ {s/version=//;p}' "${manifest_file}" | head -n1 | \
141927ac0faSGunnar Mills  tr -d '\n' | sha512sum | cut -b 1-8)
142927ac0faSGunnar Mills
143*c1a97bf8SAdriana Kobylakadd_volume "$config_file" 0 static pnor-ro-"${version_id}" "${squashfs_file}"
144*c1a97bf8SAdriana Kobylakadd_volume "$config_file" 1 dynamic pnor-prsv "" 2MiB
145*c1a97bf8SAdriana Kobylakadd_volume "$config_file" 2 dynamic pnor-rw-"${version_id}" "" 16MiB
146927ac0faSGunnar Mills
147927ac0faSGunnar Mills# Build the UBI image
148*c1a97bf8SAdriana Kobylakubinize -p ${FLASH_PEB_SIZE}KiB -m ${FLASH_PAGE_SIZE} -o "${tmpfile}" "$config_file"
149*c1a97bf8SAdriana Kobylakmk_nor_image "${outfile}" "${image_size}"
150*c1a97bf8SAdriana Kobylakdd bs=1k conv=notrunc seek=0 if="${tmpfile}" of="${outfile}"
151927ac0faSGunnar Mills
152927ac0faSGunnar Millsecho "PNOR UBI image at ${outfile}"
153