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. 7*dcb3fd79SLei 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 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.) 168f618671SGunnar Mills -s, --size <MiB> Specify the size of the PNOR UBI image in MiBs. 178f618671SGunnar Mills Defaults 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." 47*dcb3fd79SLei 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 546a3e4e45SGunnar Mills outfile=`pwd`/${tarball%".squashfs.tar"}.ubi.mtd 556a3e4e45SGunnar Millselse 566a3e4e45SGunnar Mills if [[ $outfile != /* ]]; then 576a3e4e45SGunnar Mills 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 67f8210842SGunnar Millsscratch_dir=`mktemp -d` 68f8210842SGunnar Mills 690e30f86cSGunnar Mills# Make sure scratch directory always gets cleaned up 700e30f86cSGunnar Millstrap "{ 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 75927ac0faSGunnar Millstar -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 958f618671SGunnar Millsimage_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 107927ac0faSGunnar Mills echo \[$vol_name\] >> $config_file 108927ac0faSGunnar Mills echo mode=ubi >> $config_file 109927ac0faSGunnar Mills if [ ! -z $image ]; then 110927ac0faSGunnar Mills echo image=$image >> $config_file 111927ac0faSGunnar Mills fi 112927ac0faSGunnar Mills echo vol_type=$vol_type >> $config_file 113927ac0faSGunnar Mills echo vol_name=$vol_name >> $config_file 114927ac0faSGunnar Mills echo vol_id=$vol_id >> $config_file 115927ac0faSGunnar Mills if [ ! -z $vol_size ]; then 116927ac0faSGunnar Mills echo vol_size=$vol_size >> $config_file 117927ac0faSGunnar Mills fi 118927ac0faSGunnar Mills} 119927ac0faSGunnar Mills 120927ac0faSGunnar Mills# Create an image with all 1's 121927ac0faSGunnar Millsmk_nor_image() 122927ac0faSGunnar Mills{ 123927ac0faSGunnar Mills image_dst=$1 124927ac0faSGunnar Mills image_size_kb=$2 125927ac0faSGunnar Mills dd if=/dev/zero bs=1k count=$image_size_kb | tr '\000' '\377' > $image_dst 126927ac0faSGunnar Mills} 127927ac0faSGunnar Mills 1280e30f86cSGunnar Mills# Used to temporary hold the UBI image 129927ac0faSGunnar Millstmpfile=$(mktemp ${scratch_dir}/ubinized.XXXXXX) 130927ac0faSGunnar Mills 131927ac0faSGunnar Mills# Configuration file used to create UBI image 132927ac0faSGunnar Millsconfig_file=${scratch_dir}/ubinize-PNOR.cfg 133927ac0faSGunnar Mills 134927ac0faSGunnar Mills# The version is listed in the MANIFEST file as "version=v1.99.10-19" 135927ac0faSGunnar Mills# Use the version to calculate the version id, a unique 8 hexadecimal digit id 136927ac0faSGunnar Millsversion_id=$(sed -ne '/version=/ {s/version=//;p}' ${manifest_file} | head -n1 | \ 137927ac0faSGunnar Mills tr -d '\n' | sha512sum | cut -b 1-8) 138927ac0faSGunnar Mills 139927ac0faSGunnar Millsadd_volume $config_file 0 static pnor-ro-${version_id} ${squashfs_file} 140927ac0faSGunnar Millsadd_volume $config_file 1 dynamic pnor-prsv "" 2MiB 141927ac0faSGunnar Millsadd_volume $config_file 2 dynamic pnor-rw-${version_id} "" 16MiB 142927ac0faSGunnar Mills 143927ac0faSGunnar Mills# Build the UBI image 144927ac0faSGunnar Millsubinize -p ${FLASH_PEB_SIZE}KiB -m ${FLASH_PAGE_SIZE} -o ${tmpfile} $config_file 1458f618671SGunnar Millsmk_nor_image ${outfile} ${image_size} 146927ac0faSGunnar Millsdd bs=1k conv=notrunc seek=0 if=${tmpfile} of=${outfile} 147927ac0faSGunnar Mills 148927ac0faSGunnar Millsecho "PNOR UBI image at ${outfile}" 149