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