1 #!/bin/sh 2 3 # Emit a partition summary for a device employing parameters such as the start, 4 # size, and type. 5 # 6 # Copyright (C) 2019 Paul Boddie <paul@boddie.org.uk> 7 # 8 # This program is free software; you can redistribute it and/or modify it under 9 # the terms of the GNU General Public License as published by the Free Software 10 # Foundation; either version 3 of the License, or (at your option) any later 11 # version. 12 # 13 # This program is distributed in the hope that it will be useful, but WITHOUT 14 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 16 # details. 17 # 18 # You should have received a copy of the GNU General Public License along with 19 # this program. If not, see <http://www.gnu.org/licenses/>. 20 21 PROGNAME=`basename "$0"` 22 THISDIR=`dirname "$0"` 23 24 25 26 # Emit any partition details as a complete record. 27 28 emit_partition() 29 { 30 echo "${START:--}\t${SIZE:--}\t${TYPE:--}\t${BOOTLOADER:--}\t" \ 31 "${CONFIG:--}\t${DEVICETREE:--}\t${KERNEL:--}\t${MODULES:--}\t" \ 32 "${ROOTFS:--}" 33 } 34 35 # Reset the current partition details. 36 37 reset_partition() 38 { 39 START= 40 SIZE= 41 TYPE= 42 43 BOOTLOADER= 44 CONFIG= 45 DEVICETREE= 46 KERNEL= 47 MODULES= 48 ROOTFS= 49 } 50 51 # Emit the current partition details and proceed to the next partition. 52 53 next_partition() 54 { 55 if [ "$START" ] || [ "$SIZE" ] || [ "$TYPE" ] ; then 56 emit_partition 57 reset_partition 58 fi 59 } 60 61 # test_partition <value> 62 # 63 # Test for the presence of the given value, starting a new partition if already 64 # defined. 65 66 test_partition() 67 { 68 if [ "$1" ] ; then 69 next_partition 70 fi 71 } 72 73 # replace_in_partition <name> <value> 74 # 75 # Set in the partition the parameter of the given name to value. 76 77 replace_in_partition() 78 { 79 eval "$1=\"\$2\"" 80 } 81 82 # set_in_partition <name> <value> 83 # 84 # Set the partition parameter of the given name to value, starting a new 85 # partition if necessary. 86 87 set_in_partition() 88 { 89 eval "test_partition \$$1" 90 replace_in_partition "$1" "$2" 91 } 92 93 94 95 # Emit the help message if requested. 96 97 if [ "$1" = '--help' ] ; then 98 cat 1>&2 <<EOF 99 Usage: $PROGNAME <option>... 100 101 Produce partition descriptions, indicating partition type, start position and 102 size for each partition. Each new occurrence of an active partitioning option 103 starts a new partition description. 104 105 Partitioning options are as follows: 106 107 -f <type> - partition type (ext2, ext3, ext4, fat, swap...) 108 -p <start position> - start position/sector 109 -s <size> - partition size as a percentage of device capacity 110 111 Alongside partitioning options, the following population options can be given: 112 113 -b <bootloader> - bootloader payload 114 -c <config> - configuration archive 115 -d <device tree> - device tree binary archive 116 -k <kernel> - kernel payload 117 -m <modules> - kernel modules archive 118 -r <root filesystem> - root filesystem archive 119 120 Each line of the produced description is tab-separated with '-' indicating an 121 empty field. 122 EOF 123 exit 0 124 fi 125 126 # Process the arguments, building a partition description. 127 128 reset_partition 129 130 while [ "$1" ] ; do 131 case "$1" in 132 133 # Partition type, position, size. 134 135 -f ) 136 set_in_partition "TYPE" "$2" 137 shift 2 138 ;; 139 -p ) 140 set_in_partition "START" "$2" 141 shift 2 142 ;; 143 -s ) 144 set_in_partition "SIZE" "$2" 145 shift 2 146 ;; 147 148 # Bootloader, configuration, device tree, kernel, modules, root 149 # filesystem. 150 151 -b ) 152 replace_in_partition "BOOTLOADER" "$2" 153 shift 2 154 ;; 155 -c ) 156 replace_in_partition "CONFIG" "$2" 157 shift 2 158 ;; 159 -dd ) 160 # NOTE: To be supported. 161 shift 1 162 ;; 163 -d ) 164 replace_in_partition "DEVICETREE" "$2" 165 shift 2 166 ;; 167 -k ) 168 replace_in_partition "KERNEL" "$2" 169 shift 2 170 ;; 171 -m ) 172 replace_in_partition "MODULES" "$2" 173 shift 2 174 ;; 175 -r ) 176 replace_in_partition "ROOTFS" "$2" 177 shift 2 178 ;; 179 * ) 180 shift 1 181 ;; 182 esac 183 done 184 185 # Terminate any unfinished partition. 186 187 next_partition