b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | |
| 3 | . $TOPDIR/scripts/functions.sh |
| 4 | |
| 5 | part="" |
| 6 | ubootenv="" |
| 7 | ubinize_param="" |
| 8 | kernel="" |
| 9 | rootfs="" |
| 10 | outfile="" |
| 11 | err="" |
| 12 | ubinize_seq="" |
| 13 | |
| 14 | ubivol() { |
| 15 | local volid="$1" |
| 16 | local name="$2" |
| 17 | local image="$3" |
| 18 | local autoresize="$4" |
| 19 | local size="$5" |
| 20 | local voltype="${6:-dynamic}" |
| 21 | echo "[$name]" |
| 22 | echo "mode=ubi" |
| 23 | echo "vol_id=$volid" |
| 24 | echo "vol_type=$voltype" |
| 25 | echo "vol_name=$name" |
| 26 | if [ "$image" ]; then |
| 27 | echo "image=$image" |
| 28 | [ -n "$size" ] && echo "vol_size=${size}" |
| 29 | else |
| 30 | echo "vol_size=1MiB" |
| 31 | fi |
| 32 | if [ "$autoresize" ]; then |
| 33 | echo "vol_flags=autoresize" |
| 34 | fi |
| 35 | } |
| 36 | |
| 37 | ubilayout() { |
| 38 | local vol_id=0 |
| 39 | local rootsize |
| 40 | local autoresize |
| 41 | local rootfs_type |
| 42 | local voltype |
| 43 | |
| 44 | rootfs_type="$( get_fs_type "$2" )" |
| 45 | if [ "$1" = "ubootenv" ]; then |
| 46 | ubivol $vol_id ubootenv |
| 47 | vol_id=$(( vol_id + 1 )) |
| 48 | ubivol $vol_id ubootenv2 |
| 49 | vol_id=$(( vol_id + 1 )) |
| 50 | fi |
| 51 | for part in $parts; do |
| 52 | name="${part%%=*}" |
| 53 | prev="$part" |
| 54 | part="${part#*=}" |
| 55 | voltype=dynamic |
| 56 | [ "$prev" = "$part" ] && part= |
| 57 | |
| 58 | image="${part%%=*}" |
| 59 | if [ "${image#:}" != "$image" ]; then |
| 60 | voltype=static |
| 61 | image="${image#:}" |
| 62 | fi |
| 63 | prev="$part" |
| 64 | part="${part#*=}" |
| 65 | [ "$prev" = "$part" ] && part= |
| 66 | |
| 67 | size="$part" |
| 68 | if [ -z "$size" ]; then |
| 69 | size="$( round_up "$( stat -c%s "$image" )" 1024 )" |
| 70 | else |
| 71 | size="${size}MiB" |
| 72 | fi |
| 73 | |
| 74 | ubivol $vol_id "$name" "$image" "" "${size}" "$voltype" |
| 75 | vol_id=$(( vol_id + 1 )) |
| 76 | done |
| 77 | if [ "$3" ]; then |
| 78 | ubivol $vol_id kernel "$3" |
| 79 | vol_id=$(( vol_id + 1 )) |
| 80 | fi |
| 81 | |
| 82 | if [ "$2" ]; then |
| 83 | case "$rootfs_type" in |
| 84 | "ubifs") |
| 85 | autoresize=1 |
| 86 | ;; |
| 87 | "squashfs") |
| 88 | # squashfs uses 1k block size, ensure we do not |
| 89 | # violate that |
| 90 | rootsize="$( round_up "$( stat -c%s "$2" )" 1024 )" |
| 91 | ;; |
| 92 | esac |
| 93 | ubivol $vol_id rootfs "$2" "$autoresize" "$rootsize" |
| 94 | |
| 95 | vol_id=$(( vol_id + 1 )) |
| 96 | [ "$rootfs_type" = "ubifs" ] || ubivol $vol_id rootfs_data "" 1 |
| 97 | fi |
| 98 | } |
| 99 | |
| 100 | set_ubinize_seq() { |
| 101 | if [ -n "$SOURCE_DATE_EPOCH" ] ; then |
| 102 | ubinize_seq="-Q $SOURCE_DATE_EPOCH" |
| 103 | fi |
| 104 | } |
| 105 | |
| 106 | while [ "$1" ]; do |
| 107 | case "$1" in |
| 108 | "--uboot-env") |
| 109 | ubootenv="ubootenv" |
| 110 | shift |
| 111 | continue |
| 112 | ;; |
| 113 | "--kernel") |
| 114 | kernel="$2" |
| 115 | shift |
| 116 | shift |
| 117 | continue |
| 118 | ;; |
| 119 | "--rootfs") |
| 120 | rootfs="$2" |
| 121 | shift |
| 122 | shift |
| 123 | continue |
| 124 | ;; |
| 125 | "--part") |
| 126 | parts="$parts $2" |
| 127 | shift |
| 128 | shift |
| 129 | continue |
| 130 | ;; |
| 131 | "-"*) |
| 132 | ubinize_param="$*" |
| 133 | break |
| 134 | ;; |
| 135 | *) |
| 136 | if [ ! "$outfile" ]; then |
| 137 | outfile=$1 |
| 138 | shift |
| 139 | continue |
| 140 | fi |
| 141 | ;; |
| 142 | esac |
| 143 | done |
| 144 | |
| 145 | if [ ! -r "$rootfs" ] && [ ! -r "$kernel" ] && [ ! "$parts" ] && [ ! "$outfile" ]; then |
| 146 | echo "syntax: $0 [--uboot-env] [--part <name>=<file>] [--kernel kernelimage] [--rootfs rootfsimage] out [ubinize opts]" |
| 147 | exit 1 |
| 148 | fi |
| 149 | |
| 150 | ubinize="$( command -v ubinize )" |
| 151 | if [ ! -x "$ubinize" ]; then |
| 152 | echo "ubinize tool not found or not usable" |
| 153 | exit 1 |
| 154 | fi |
| 155 | |
| 156 | ubinizecfg="$( mktemp 2> /dev/null )" |
| 157 | if [ -z "$ubinizecfg" ]; then |
| 158 | # try OSX signature |
| 159 | ubinizecfg="$( mktemp -t 'ubitmp' )" |
| 160 | fi |
| 161 | ubilayout "$ubootenv" "$rootfs" "$kernel" > "$ubinizecfg" |
| 162 | |
| 163 | set_ubinize_seq |
| 164 | cat "$ubinizecfg" |
| 165 | ubinize $ubinize_seq -o "$outfile" $ubinize_param "$ubinizecfg" |
| 166 | err="$?" |
| 167 | [ ! -e "$outfile" ] && err=2 |
| 168 | rm "$ubinizecfg" |
| 169 | |
| 170 | exit $err |