blob: 2633500c3295e7f585d6d86e4e3af847bbd01531 [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.
7The PNOR SquashFS Tarball is generated from the generate-squashfs script.
8
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.)
16 -h, --help Display this help text and exit.
17'
18
19while [[ $# -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
35done
36
37if [ ! -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
42fi
43
44if [[ -z $outfile ]]; then
45 # Remove .squashfs.tar from end if present and add .ubi.mtd
46 outfile=`pwd`/${tarball%".squashfs.tar"}.ubi.mtd
47else
48 if [[ $outfile != /* ]]; then
49 outfile=`pwd`/$outfile
50 fi
51fi
52
Gunnar Millsf8210842017-10-23 13:08:38 -050053echo "Generating PNOR UBI image."
54
55squashfs_file_name="pnor.xz.squashfs"
Gunnar Mills927ac0f2017-10-23 16:11:09 -050056manifest_file_name="MANIFEST"
Gunnar Millsf8210842017-10-23 13:08:38 -050057
58# Scratch directory for untarring and config file
59scratch_dir=`mktemp -d`
60
Gunnar Mills927ac0f2017-10-23 16:11:09 -050061squashfs_file=${scratch_dir}/${squashfs_file_name}
62manifest_file=${scratch_dir}/${manifest_file_name}
Gunnar Millsf8210842017-10-23 13:08:38 -050063# Untar tarball
Gunnar Mills927ac0f2017-10-23 16:11:09 -050064tar -xvf ${tarball} -C ${scratch_dir} ${squashfs_file_name} ${manifest_file_name}
Gunnar Millsf8210842017-10-23 13:08:38 -050065
66# All valid PNOR SquashFS Tarballs have a file named "pnor.xz.squashfs"
Gunnar Mills927ac0f2017-10-23 16:11:09 -050067if [ ! -f "${squashfs_file}" ]; then
Gunnar Millsf8210842017-10-23 13:08:38 -050068 echo "No \"${squashfs_file_name}\" file in the tarball!"
69 rm -r "${scratch_dir}"
70 exit 1
71fi
72
Gunnar Mills927ac0f2017-10-23 16:11:09 -050073# Need the manifest file for calculating the version id
74if [ ! -f "${manifest_file}" ]; then
75 echo "No \"${manifest_file_name}\" file in the tarball!"
76 rm -r "${scratch_dir}"
77 exit 1
78fi
79
80# Flash page size in bytes
81FLASH_PAGE_SIZE="1"
82# kibibyte(KiB)
83FLASH_PEB_SIZE="64"
84# Future enhancement would be to allow this to be passed in
85# 128MiB (128 * 1024)
86FLASH_SIZE="131072"
87
88# Create UBI volume
89add_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
112mk_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
120tmpfile=$(mktemp ${scratch_dir}/ubinized.XXXXXX)
121
122# Configuration file used to create UBI image
123config_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
127version_id=$(sed -ne '/version=/ {s/version=//;p}' ${manifest_file} | head -n1 | \
128 tr -d '\n' | sha512sum | cut -b 1-8)
129
130add_volume $config_file 0 static pnor-ro-${version_id} ${squashfs_file}
131add_volume $config_file 1 dynamic pnor-prsv "" 2MiB
132add_volume $config_file 2 dynamic pnor-rw-${version_id} "" 16MiB
133
134# Build the UBI image
135ubinize -p ${FLASH_PEB_SIZE}KiB -m ${FLASH_PAGE_SIZE} -o ${tmpfile} $config_file
136mk_nor_image ${outfile} ${FLASH_SIZE}
137dd bs=1k conv=notrunc seek=0 if=${tmpfile} of=${outfile}
138
139echo "PNOR UBI image at ${outfile}"
Gunnar Millsf8210842017-10-23 13:08:38 -0500140rm -r "${scratch_dir}"