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