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 ( -f <type> | -p <start position> | -s <size> )... 100 101 Produce partition descriptions, indicating partition type, start position and 102 size for each partition. Each new occurrence of an active option starts a new 103 partition description. 104 105 Each line of the produced description is tab-separated with '-' indicating an 106 empty field. 107 EOF 108 exit 0 109 fi 110 111 # Process the arguments, building a partition description. 112 113 reset_partition 114 115 while [ "$1" ] ; do 116 case "$1" in 117 118 # Partition type, position, size. 119 120 -f ) 121 set_in_partition "TYPE" "$2" 122 shift 2 123 ;; 124 -p ) 125 set_in_partition "START" "$2" 126 shift 2 127 ;; 128 -s ) 129 set_in_partition "SIZE" "$2" 130 shift 2 131 ;; 132 133 # Bootloader, configuration, device tree, kernel, modules, root 134 # filesystem. 135 136 -b ) 137 replace_in_partition "BOOTLOADER" "$2" 138 shift 2 139 ;; 140 -c ) 141 replace_in_partition "CONFIG" "$2" 142 shift 2 143 ;; 144 -dd ) 145 # NOTE: To be supported. 146 shift 1 147 ;; 148 -d ) 149 replace_in_partition "DEVICETREE" "$2" 150 shift 2 151 ;; 152 -k ) 153 replace_in_partition "KERNEL" "$2" 154 shift 2 155 ;; 156 -m ) 157 replace_in_partition "MODULES" "$2" 158 shift 2 159 ;; 160 -r ) 161 replace_in_partition "ROOTFS" "$2" 162 shift 2 163 ;; 164 * ) 165 shift 1 166 ;; 167 esac 168 done 169 170 # Terminate any unfinished partition. 171 172 next_partition