Gunnar Mills | 6a3e4e4 | 2017-10-23 11:05:03 -0500 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | set -eo pipefail |
| 3 | |
| 4 | help=$'Generate PNOR UBI image from a PNOR SquashFS Tarball |
| 5 | |
| 6 | Generates a UBI, Unsorted Block Images, PNOR image from a PNOR SquashFS Tarball. |
| 7 | The PNOR SquashFS Tarball is generated from the generate-squashfs script. |
| 8 | |
| 9 | usage: generate-ubi [OPTION] <PNOR SquashFS Tarball>... |
| 10 | |
| 11 | Options: |
| 12 | -f, --file <file> Specify destination file. Defaults to |
| 13 | `pwd`/<PNOR Tarball FILE, removing .squashfs.tar>.ubi.mtd |
| 14 | (For example, "generate-ubi my.pnor.squashfs.tar" |
| 15 | would generate `pwd`/my.pnor.ubi.mtd output.) |
| 16 | -h, --help Display this help text and exit. |
| 17 | ' |
| 18 | |
| 19 | while [[ $# -gt 0 ]]; do |
| 20 | key="$1" |
| 21 | case $key in |
| 22 | -f|--file) |
| 23 | outfile="$2" |
| 24 | shift 2 |
| 25 | ;; |
| 26 | -h|--help) |
| 27 | echo "$help" |
| 28 | exit |
| 29 | ;; |
| 30 | *) |
| 31 | tarball="$1" |
| 32 | shift 1 |
| 33 | ;; |
| 34 | esac |
| 35 | done |
| 36 | |
| 37 | if [ ! -f "${tarball}" ]; then |
| 38 | echo "Please enter a PNOR SquashFS Tarball." |
| 39 | echo "To generate PNOR SquashFS Tarball see generate-squashfs" |
| 40 | echo "$help" |
| 41 | exit 1 |
| 42 | fi |
| 43 | |
| 44 | if [[ -z $outfile ]]; then |
| 45 | # Remove .squashfs.tar from end if present and add .ubi.mtd |
| 46 | outfile=`pwd`/${tarball%".squashfs.tar"}.ubi.mtd |
| 47 | else |
| 48 | if [[ $outfile != /* ]]; then |
| 49 | outfile=`pwd`/$outfile |
| 50 | fi |
| 51 | fi |
| 52 | |
Gunnar Mills | f821084 | 2017-10-23 13:08:38 -0500 | [diff] [blame] | 53 | echo "Generating PNOR UBI image." |
| 54 | |
| 55 | squashfs_file_name="pnor.xz.squashfs" |
Gunnar Mills | 927ac0f | 2017-10-23 16:11:09 -0500 | [diff] [blame^] | 56 | manifest_file_name="MANIFEST" |
Gunnar Mills | f821084 | 2017-10-23 13:08:38 -0500 | [diff] [blame] | 57 | |
| 58 | # Scratch directory for untarring and config file |
| 59 | scratch_dir=`mktemp -d` |
| 60 | |
Gunnar Mills | 927ac0f | 2017-10-23 16:11:09 -0500 | [diff] [blame^] | 61 | squashfs_file=${scratch_dir}/${squashfs_file_name} |
| 62 | manifest_file=${scratch_dir}/${manifest_file_name} |
Gunnar Mills | f821084 | 2017-10-23 13:08:38 -0500 | [diff] [blame] | 63 | # Untar tarball |
Gunnar Mills | 927ac0f | 2017-10-23 16:11:09 -0500 | [diff] [blame^] | 64 | tar -xvf ${tarball} -C ${scratch_dir} ${squashfs_file_name} ${manifest_file_name} |
Gunnar Mills | f821084 | 2017-10-23 13:08:38 -0500 | [diff] [blame] | 65 | |
| 66 | # All valid PNOR SquashFS Tarballs have a file named "pnor.xz.squashfs" |
Gunnar Mills | 927ac0f | 2017-10-23 16:11:09 -0500 | [diff] [blame^] | 67 | if [ ! -f "${squashfs_file}" ]; then |
Gunnar Mills | f821084 | 2017-10-23 13:08:38 -0500 | [diff] [blame] | 68 | echo "No \"${squashfs_file_name}\" file in the tarball!" |
| 69 | rm -r "${scratch_dir}" |
| 70 | exit 1 |
| 71 | fi |
| 72 | |
Gunnar Mills | 927ac0f | 2017-10-23 16:11:09 -0500 | [diff] [blame^] | 73 | # Need the manifest file for calculating the version id |
| 74 | if [ ! -f "${manifest_file}" ]; then |
| 75 | echo "No \"${manifest_file_name}\" file in the tarball!" |
| 76 | rm -r "${scratch_dir}" |
| 77 | exit 1 |
| 78 | fi |
| 79 | |
| 80 | # Flash page size in bytes |
| 81 | FLASH_PAGE_SIZE="1" |
| 82 | # kibibyte(KiB) |
| 83 | FLASH_PEB_SIZE="64" |
| 84 | # Future enhancement would be to allow this to be passed in |
| 85 | # 128MiB (128 * 1024) |
| 86 | FLASH_SIZE="131072" |
| 87 | |
| 88 | # Create UBI volume |
| 89 | add_volume() |
| 90 | { |
| 91 | config_file=$1 |
| 92 | vol_id=$2 |
| 93 | vol_type=$3 |
| 94 | vol_name=$4 |
| 95 | image=$5 |
| 96 | vol_size=$6 |
| 97 | |
| 98 | echo \[$vol_name\] >> $config_file |
| 99 | echo mode=ubi >> $config_file |
| 100 | if [ ! -z $image ]; then |
| 101 | echo image=$image >> $config_file |
| 102 | fi |
| 103 | echo vol_type=$vol_type >> $config_file |
| 104 | echo vol_name=$vol_name >> $config_file |
| 105 | echo vol_id=$vol_id >> $config_file |
| 106 | if [ ! -z $vol_size ]; then |
| 107 | echo vol_size=$vol_size >> $config_file |
| 108 | fi |
| 109 | } |
| 110 | |
| 111 | # Create an image with all 1's |
| 112 | mk_nor_image() |
| 113 | { |
| 114 | image_dst=$1 |
| 115 | image_size_kb=$2 |
| 116 | dd if=/dev/zero bs=1k count=$image_size_kb | tr '\000' '\377' > $image_dst |
| 117 | } |
| 118 | |
| 119 | # Used to temporary hold the UBI volume |
| 120 | tmpfile=$(mktemp ${scratch_dir}/ubinized.XXXXXX) |
| 121 | |
| 122 | # Configuration file used to create UBI image |
| 123 | config_file=${scratch_dir}/ubinize-PNOR.cfg |
| 124 | |
| 125 | # The version is listed in the MANIFEST file as "version=v1.99.10-19" |
| 126 | # Use the version to calculate the version id, a unique 8 hexadecimal digit id |
| 127 | version_id=$(sed -ne '/version=/ {s/version=//;p}' ${manifest_file} | head -n1 | \ |
| 128 | tr -d '\n' | sha512sum | cut -b 1-8) |
| 129 | |
| 130 | add_volume $config_file 0 static pnor-ro-${version_id} ${squashfs_file} |
| 131 | add_volume $config_file 1 dynamic pnor-prsv "" 2MiB |
| 132 | add_volume $config_file 2 dynamic pnor-rw-${version_id} "" 16MiB |
| 133 | |
| 134 | # Build the UBI image |
| 135 | ubinize -p ${FLASH_PEB_SIZE}KiB -m ${FLASH_PAGE_SIZE} -o ${tmpfile} $config_file |
| 136 | mk_nor_image ${outfile} ${FLASH_SIZE} |
| 137 | dd bs=1k conv=notrunc seek=0 if=${tmpfile} of=${outfile} |
| 138 | |
| 139 | echo "PNOR UBI image at ${outfile}" |
Gunnar Mills | f821084 | 2017-10-23 13:08:38 -0500 | [diff] [blame] | 140 | rm -r "${scratch_dir}" |