blob: d54634c595e2b8e4d7fbf9bee3465f26e0935554 [file] [log] [blame]
Gunnar Mills6a3e4e42017-10-23 11:05:03 -05001#!/bin/bash
2set -eo pipefail
3
4help=$'Generate PNOR UBI image from a PNOR SquashFS Tarball
5
6Generates a UBI, Unsorted Block Images, PNOR image from a PNOR SquashFS Tarball.
Lei YUdcb3fd72019-02-21 13:19:03 +08007The PNOR SquashFS Tarball is generated from the generate-tar script.
Gunnar Mills6a3e4e42017-10-23 11:05:03 -05008
9usage: generate-ubi [OPTION] <PNOR SquashFS Tarball>...
10
11Options:
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.)
Gunnar Mills8f618672017-10-25 11:24:39 -050016 -s, --size <MiB> Specify the size of the PNOR UBI image in MiBs.
17 Defaults to 128.
Gunnar Mills6a3e4e42017-10-23 11:05:03 -050018 -h, --help Display this help text and exit.
19'
Gunnar Mills8f618672017-10-25 11:24:39 -050020# 128MiB is the default image size
21image_size="128"
Gunnar Mills6a3e4e42017-10-23 11:05:03 -050022
23while [[ $# -gt 0 ]]; do
24 key="$1"
25 case $key in
26 -f|--file)
27 outfile="$2"
28 shift 2
29 ;;
Gunnar Mills8f618672017-10-25 11:24:39 -050030 -s|--size)
31 image_size="$2"
32 shift 2
33 ;;
Gunnar Mills6a3e4e42017-10-23 11:05:03 -050034 -h|--help)
35 echo "$help"
36 exit
37 ;;
38 *)
39 tarball="$1"
40 shift 1
41 ;;
42 esac
43done
44
45if [ ! -f "${tarball}" ]; then
46 echo "Please enter a PNOR SquashFS Tarball."
Lei YUdcb3fd72019-02-21 13:19:03 +080047 echo "To generate PNOR SquashFS Tarball see generate-tar"
Gunnar Mills6a3e4e42017-10-23 11:05:03 -050048 echo "$help"
49 exit 1
50fi
51
52if [[ -z $outfile ]]; then
53 # Remove .squashfs.tar from end if present and add .ubi.mtd
54 outfile=`pwd`/${tarball%".squashfs.tar"}.ubi.mtd
55else
56 if [[ $outfile != /* ]]; then
57 outfile=`pwd`/$outfile
58 fi
59fi
60
Gunnar Millsf8210842017-10-23 13:08:38 -050061echo "Generating PNOR UBI image."
62
63squashfs_file_name="pnor.xz.squashfs"
Gunnar Mills927ac0f2017-10-23 16:11:09 -050064manifest_file_name="MANIFEST"
Gunnar Millsf8210842017-10-23 13:08:38 -050065
66# Scratch directory for untarring and config file
67scratch_dir=`mktemp -d`
68
Gunnar Mills0e30f862017-10-25 11:35:18 -050069# Make sure scratch directory always gets cleaned up
70trap "{ rm -r ${scratch_dir}; }" EXIT
71
Gunnar Mills927ac0f2017-10-23 16:11:09 -050072squashfs_file=${scratch_dir}/${squashfs_file_name}
73manifest_file=${scratch_dir}/${manifest_file_name}
Gunnar Millsf8210842017-10-23 13:08:38 -050074# Untar tarball
Gunnar Mills927ac0f2017-10-23 16:11:09 -050075tar -xvf ${tarball} -C ${scratch_dir} ${squashfs_file_name} ${manifest_file_name}
Gunnar Millsf8210842017-10-23 13:08:38 -050076
77# All valid PNOR SquashFS Tarballs have a file named "pnor.xz.squashfs"
Gunnar Mills927ac0f2017-10-23 16:11:09 -050078if [ ! -f "${squashfs_file}" ]; then
Gunnar Millsf8210842017-10-23 13:08:38 -050079 echo "No \"${squashfs_file_name}\" file in the tarball!"
Gunnar Millsf8210842017-10-23 13:08:38 -050080 exit 1
81fi
82
Gunnar Mills927ac0f2017-10-23 16:11:09 -050083# Need the manifest file for calculating the version id
84if [ ! -f "${manifest_file}" ]; then
85 echo "No \"${manifest_file_name}\" file in the tarball!"
Gunnar Mills927ac0f2017-10-23 16:11:09 -050086 exit 1
87fi
88
89# Flash page size in bytes
90FLASH_PAGE_SIZE="1"
91# kibibyte(KiB)
92FLASH_PEB_SIZE="64"
Gunnar Mills8f618672017-10-25 11:24:39 -050093
94# Convert image size from MiB to KiB
95image_size=$((${image_size} * 1024))
Gunnar Mills927ac0f2017-10-23 16:11:09 -050096
97# Create UBI volume
98add_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
107 echo \[$vol_name\] >> $config_file
108 echo mode=ubi >> $config_file
109 if [ ! -z $image ]; then
110 echo image=$image >> $config_file
111 fi
112 echo vol_type=$vol_type >> $config_file
113 echo vol_name=$vol_name >> $config_file
114 echo vol_id=$vol_id >> $config_file
115 if [ ! -z $vol_size ]; then
116 echo vol_size=$vol_size >> $config_file
117 fi
118}
119
120# Create an image with all 1's
121mk_nor_image()
122{
123 image_dst=$1
124 image_size_kb=$2
125 dd if=/dev/zero bs=1k count=$image_size_kb | tr '\000' '\377' > $image_dst
126}
127
Gunnar Mills0e30f862017-10-25 11:35:18 -0500128# Used to temporary hold the UBI image
Gunnar Mills927ac0f2017-10-23 16:11:09 -0500129tmpfile=$(mktemp ${scratch_dir}/ubinized.XXXXXX)
130
131# Configuration file used to create UBI image
132config_file=${scratch_dir}/ubinize-PNOR.cfg
133
134# The version is listed in the MANIFEST file as "version=v1.99.10-19"
135# Use the version to calculate the version id, a unique 8 hexadecimal digit id
136version_id=$(sed -ne '/version=/ {s/version=//;p}' ${manifest_file} | head -n1 | \
137 tr -d '\n' | sha512sum | cut -b 1-8)
138
139add_volume $config_file 0 static pnor-ro-${version_id} ${squashfs_file}
140add_volume $config_file 1 dynamic pnor-prsv "" 2MiB
141add_volume $config_file 2 dynamic pnor-rw-${version_id} "" 16MiB
142
143# Build the UBI image
144ubinize -p ${FLASH_PEB_SIZE}KiB -m ${FLASH_PAGE_SIZE} -o ${tmpfile} $config_file
Gunnar Mills8f618672017-10-25 11:24:39 -0500145mk_nor_image ${outfile} ${image_size}
Gunnar Mills927ac0f2017-10-23 16:11:09 -0500146dd bs=1k conv=notrunc seek=0 if=${tmpfile} of=${outfile}
147
148echo "PNOR UBI image at ${outfile}"