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