MAKEDEV.awk revision 1.27 1 1.1 jdolecek #!/usr/bin/awk -
2 1.1 jdolecek #
3 1.27 ozaki # $NetBSD: MAKEDEV.awk,v 1.27 2019/10/28 02:53:29 ozaki-r Exp $
4 1.1 jdolecek #
5 1.1 jdolecek # Copyright (c) 2003 The NetBSD Foundation, Inc.
6 1.1 jdolecek # All rights reserved.
7 1.1 jdolecek #
8 1.1 jdolecek # This code is derived from software contributed to The NetBSD Foundation
9 1.1 jdolecek # by Jaromir Dolecek.
10 1.1 jdolecek #
11 1.1 jdolecek # Redistribution and use in source and binary forms, with or without
12 1.1 jdolecek # modification, are permitted provided that the following conditions
13 1.1 jdolecek # are met:
14 1.1 jdolecek # 1. Redistributions of source code must retain the above copyright
15 1.1 jdolecek # notice, this list of conditions and the following disclaimer.
16 1.1 jdolecek # 2. Redistributions in binary form must reproduce the above copyright
17 1.1 jdolecek # notice, this list of conditions and the following disclaimer in the
18 1.1 jdolecek # documentation and/or other materials provided with the distribution.
19 1.1 jdolecek #
20 1.1 jdolecek # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 1.1 jdolecek # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 1.1 jdolecek # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 1.1 jdolecek # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 1.1 jdolecek # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 1.1 jdolecek # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 1.1 jdolecek # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 1.1 jdolecek # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 1.1 jdolecek # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 1.1 jdolecek # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 1.1 jdolecek # POSSIBILITY OF SUCH DAMAGE.
31 1.1 jdolecek #
32 1.1 jdolecek
33 1.2 jdolecek # Script to generate platform MAKEDEV script from MI template, MD
34 1.2 jdolecek # MAKEDEV.conf and MD/MI major lists
35 1.2 jdolecek #
36 1.2 jdolecek # Uses environment variables MACHINE/MACHINE_ARCH to select
37 1.2 jdolecek # appropriate files, and NETBSDSRCDIR to get root of source tree.
38 1.2 jdolecek
39 1.1 jdolecek BEGIN {
40 1.1 jdolecek # top of source tree, used to find major number list in kernel
41 1.1 jdolecek # sources
42 1.11 lukem machine = ENVIRON["MACHINE"]
43 1.11 lukem maarch = ENVIRON["MACHINE_ARCH"]
44 1.11 lukem srcdir = ENVIRON["NETBSDSRCDIR"]
45 1.11 lukem if (!machine || !maarch || !srcdir) {
46 1.11 lukem print "ERROR: 'MACHINE', 'MACHINE_ARCH' and 'NETBSDSRCDIR' must be set in environment" > "/dev/stderr"
47 1.11 lukem exit 1
48 1.11 lukem }
49 1.11 lukem top = srcdir "/sys/"
50 1.1 jdolecek if (system("test -d '" top "'") != 0) {
51 1.4 jdolecek print "ERROR: can't find top of kernel source tree ('" top "' not a directory)" > "/dev/stderr"
52 1.1 jdolecek exit 1
53 1.1 jdolecek }
54 1.1 jdolecek
55 1.1 jdolecek
56 1.1 jdolecek # file with major definitions
57 1.1 jdolecek majors[0] = "conf/majors"
58 1.26 christos if ((index(maarch, "arm") != 0 || index(maarch, "aarch64")) && system("test -f '" top "arch/" machine "/conf/majors." machine "'") != 0)
59 1.1 jdolecek majors[1] = "arch/arm/conf/majors.arm32";
60 1.18 scw else if (machine == "sbmips")
61 1.6 jdolecek majors[1] = "arch/evbmips/conf/majors.evbmips";
62 1.21 mrg else if ((maarch == "powerpc" || maarch == "powerpc64") && system("test -f '" top "arch/" machine "/conf/majors." machine "'") != 0)
63 1.19 garbled majors[1] = "arch/powerpc/conf/majors.powerpc";
64 1.6 jdolecek else
65 1.1 jdolecek majors[1] = "arch/" machine "/conf/majors." machine;
66 1.25 matt nm = 2;
67 1.1 jdolecek
68 1.1 jdolecek # process all files with majors and fill the chr[] and blk[]
69 1.1 jdolecek # arrays, used in template processing
70 1.25 matt for (m = 0; m < nm; m++) {
71 1.1 jdolecek file = top majors[m]
72 1.4 jdolecek if (system("test -f '" file "'") != 0) {
73 1.4 jdolecek print "ERROR: can't find majors file '" file "'" > "/dev/stderr"
74 1.4 jdolecek exit 1
75 1.4 jdolecek }
76 1.1 jdolecek while (getline < file) {
77 1.25 matt if ($1 == "include") {
78 1.25 matt majors[nm++] = substr($2, 2, length($2)-2);
79 1.25 matt } else if ($1 == "device-major") {
80 1.1 jdolecek if ($3 == "char") {
81 1.1 jdolecek chr[$2] = $4
82 1.1 jdolecek if ($5 == "block")
83 1.1 jdolecek blk[$2] = $6
84 1.1 jdolecek } else if ($3 == "block")
85 1.1 jdolecek blk[$2] = $4
86 1.1 jdolecek }
87 1.1 jdolecek }
88 1.5 jdolecek close(file)
89 1.1 jdolecek }
90 1.17 dsl CONSOLE_CMAJOR = chr["cons"]
91 1.17 dsl if (CONSOLE_CMAJOR == "") {
92 1.17 dsl print "ERROR: no entry for 'cons' in majors file" > "/dev/stderr"
93 1.17 dsl exit 1
94 1.17 dsl }
95 1.1 jdolecek
96 1.5 jdolecek # read MD config file for MD device targets
97 1.11 lukem cfgfile = srcdir "/etc/etc." machine "/MAKEDEV.conf"
98 1.5 jdolecek if (system("test -f '" cfgfile "'") != 0) {
99 1.7 jdolecek print "ERROR: no platform MAKEDEV.conf - '" cfgfile "' doesn't exist" > "/dev/stderr"
100 1.5 jdolecek exit 1
101 1.5 jdolecek }
102 1.12 jdolecek # skip first two lines
103 1.12 jdolecek getline CONFRCSID < cfgfile # RCS Id
104 1.12 jdolecek getline < cfgfile # blank line
105 1.2 jdolecek MDDEV = 0 # MD device targets
106 1.2 jdolecek while (getline < cfgfile) {
107 1.5 jdolecek if (MDDEV)
108 1.5 jdolecek MDDEV = MDDEV "\n" $0
109 1.5 jdolecek else
110 1.5 jdolecek MDDEV = $0
111 1.5 jdolecek }
112 1.5 jdolecek close(cfgfile)
113 1.5 jdolecek
114 1.5 jdolecek # determine number of partitions used by platform
115 1.5 jdolecek # there are three variants in tree:
116 1.5 jdolecek # 1. MAXPARTITIONS = 8
117 1.5 jdolecek # 2. MAXPARTITIONS = 16 with no back compat mapping
118 1.5 jdolecek # 3. MAXPARTITIONS = 16 with back compat with old limit of 8
119 1.5 jdolecek # currently all archs, which moved from 8->16 use same
120 1.5 jdolecek # scheme for mapping disk minors, high minor offset
121 1.5 jdolecek # if this changes, the below needs to be adjusted and
122 1.5 jdolecek # additional makedisk_p16foo needs to be added
123 1.5 jdolecek incdir = machine
124 1.5 jdolecek diskpartitions = 0
125 1.5 jdolecek diskbackcompat = 0
126 1.5 jdolecek while (1) {
127 1.5 jdolecek inc = top "arch/" incdir "/include/disklabel.h"
128 1.5 jdolecek if (system("test -f '" inc "'") != 0) {
129 1.5 jdolecek print "ERROR: can't find kernel include file '" inc "'" > "/dev/stderr"
130 1.5 jdolecek exit 1
131 1.5 jdolecek }
132 1.5 jdolecek incdir = 0
133 1.5 jdolecek while (getline < inc) {
134 1.6 jdolecek if ($1 == "#define" && $2 == "MAXPARTITIONS")
135 1.5 jdolecek diskpartitions = $3
136 1.6 jdolecek else if ($1 == "#define" && $2 == "OLDMAXPARTITIONS")
137 1.6 jdolecek diskbackcompat = $3
138 1.6 jdolecek else if ($1 == "#define" && $2 == "RAW_PART")
139 1.6 jdolecek RAWDISK_OFF = $3
140 1.16 he else if ($1 == "#include" &&
141 1.16 he $2 ~ "<.*/disklabel.h>" &&
142 1.16 he $2 !~ ".*nbinclude.*")
143 1.16 he {
144 1.5 jdolecek # wrapper, switch to the right file
145 1.5 jdolecek incdir = substr($2, 2)
146 1.5 jdolecek sub("/.*", "", incdir)
147 1.5 jdolecek break;
148 1.5 jdolecek }
149 1.5 jdolecek }
150 1.13 enami close(inc)
151 1.5 jdolecek
152 1.5 jdolecek if (diskpartitions)
153 1.5 jdolecek break;
154 1.5 jdolecek
155 1.5 jdolecek if (!incdir) {
156 1.5 jdolecek print "ERROR: can't determine MAXPARTITIONS from include file '" inc "'" > "/dev/stderr"
157 1.5 jdolecek exit 1
158 1.5 jdolecek }
159 1.2 jdolecek }
160 1.5 jdolecek MKDISK = "makedisk_p" diskpartitions # routine to create disk devs
161 1.6 jdolecek DISKMINOROFFSET = diskpartitions
162 1.6 jdolecek if (diskbackcompat) {
163 1.5 jdolecek MKDISK = MKDISK "high"
164 1.6 jdolecek DISKMINOROFFSET = diskbackcompat
165 1.6 jdolecek }
166 1.6 jdolecek RAWDISK_NAME = sprintf("%c", 97 + RAWDISK_OFF) # a+offset
167 1.2 jdolecek
168 1.11 lukem # read etc/master.passwd for user name->UID mapping
169 1.11 lukem idfile = srcdir "/etc/master.passwd"
170 1.11 lukem if (system("test -f '" idfile "'") != 0) {
171 1.11 lukem print "ERROR: can't find password file '" idfile "'" > "/dev/stderr"
172 1.11 lukem exit 1
173 1.11 lukem }
174 1.11 lukem oldFS=FS
175 1.11 lukem FS=":"
176 1.11 lukem while (getline < idfile) {
177 1.11 lukem uid[$1] = $3
178 1.11 lukem }
179 1.11 lukem close(idfile)
180 1.11 lukem FS=oldFS
181 1.11 lukem
182 1.11 lukem # read etc/group for group name->GID mapping
183 1.11 lukem idfile = srcdir "/etc/group"
184 1.11 lukem if (system("test -f '" idfile "'") != 0) {
185 1.11 lukem print "ERROR: can't find group file '" idfile "'" > "/dev/stderr"
186 1.11 lukem exit 1
187 1.11 lukem }
188 1.11 lukem oldFS=FS
189 1.11 lukem FS=":"
190 1.11 lukem while (getline < idfile) {
191 1.11 lukem gid[$1] = $3
192 1.11 lukem }
193 1.11 lukem close(idfile)
194 1.11 lukem FS=oldFS
195 1.11 lukem
196 1.1 jdolecek # initially no substitutions
197 1.1 jdolecek devsubst = 0
198 1.1 jdolecek deventry = ""
199 1.1 jdolecek }
200 1.1 jdolecek
201 1.1 jdolecek /%MI_DEVICES_BEGIN%/ {
202 1.1 jdolecek devsubst = 1;
203 1.1 jdolecek next
204 1.1 jdolecek }
205 1.1 jdolecek
206 1.1 jdolecek /%MI_DEVICES_END%/ {
207 1.1 jdolecek devsubst = 0;
208 1.1 jdolecek next
209 1.1 jdolecek }
210 1.1 jdolecek
211 1.12 jdolecek # output 'Generated from' lines
212 1.12 jdolecek /\$[N]etBSD/ {
213 1.12 jdolecek print "#"
214 1.12 jdolecek print "# Generated from:"
215 1.12 jdolecek
216 1.12 jdolecek # MAKEDEV.awk (this script) RCS Id
217 1.27 ozaki ARCSID = "$NetBSD: MAKEDEV.awk,v 1.27 2019/10/28 02:53:29 ozaki-r Exp $"
218 1.12 jdolecek gsub(/\$/, "", ARCSID)
219 1.12 jdolecek print "# " ARCSID
220 1.12 jdolecek
221 1.12 jdolecek # MAKEDEV.tmpl RCS Id
222 1.12 jdolecek gsub(/\$/, "")
223 1.12 jdolecek print $0
224 1.12 jdolecek
225 1.12 jdolecek # MD MAKEDEV.conf RCS Id
226 1.12 jdolecek # strip leading hash and insert machine subdirectory name
227 1.12 jdolecek gsub(/\$/, "", CONFRCSID)
228 1.12 jdolecek sub(/^\# /, "", CONFRCSID)
229 1.12 jdolecek sub(/MAKEDEV.conf/, "etc." machine "/MAKEDEV.conf", CONFRCSID)
230 1.12 jdolecek print "# " CONFRCSID
231 1.12 jdolecek
232 1.12 jdolecek next # don't print the RCS Id line again
233 1.12 jdolecek }
234 1.12 jdolecek
235 1.12 jdolecek # filter the 'PLEASE RUN ...' paragraph
236 1.12 jdolecek /^\# PLEASE RUN/, /^\#\#\#\#\#\#/ {
237 1.12 jdolecek next
238 1.12 jdolecek }
239 1.12 jdolecek
240 1.12 jdolecek # filter the device list
241 1.12 jdolecek /^\# Tapes/,/^$/ {
242 1.12 jdolecek next
243 1.12 jdolecek }
244 1.12 jdolecek
245 1.4 jdolecek # filter the two unneeded makedisk_p* routines, leave only
246 1.4 jdolecek # the one used
247 1.10 dmcmahil /^makedisk_p8\(\) \{/, /^\}/ {
248 1.4 jdolecek if (MKDISK != "makedisk_p8")
249 1.4 jdolecek next;
250 1.4 jdolecek }
251 1.10 dmcmahil /^makedisk_p16\(\) \{/, /^\}/ {
252 1.4 jdolecek if (MKDISK != "makedisk_p16")
253 1.4 jdolecek next;
254 1.4 jdolecek }
255 1.10 dmcmahil /^makedisk_p16high\(\) \{/, /^\}/ {
256 1.4 jdolecek if (MKDISK != "makedisk_p16high")
257 1.4 jdolecek next;
258 1.4 jdolecek }
259 1.4 jdolecek
260 1.11 lukem # special cases aside, handle normal line
261 1.1 jdolecek {
262 1.8 jdolecek sub(/^%MD_DEVICES%/, MDDEV)
263 1.8 jdolecek sub(/%MKDISK%/, MKDISK)
264 1.8 jdolecek sub(/%DISKMINOROFFSET%/, DISKMINOROFFSET)
265 1.8 jdolecek sub(/%RAWDISK_OFF%/, RAWDISK_OFF)
266 1.8 jdolecek sub(/%RAWDISK_NAME%/, RAWDISK_NAME)
267 1.17 dsl sub(/%CONSOLE_CMAJOR%/, CONSOLE_CMAJOR)
268 1.14 enami parsed = ""
269 1.14 enami line = $0
270 1.24 mbalmer while (match(line, /%[gu]id_[_a-z]*%/)) {
271 1.14 enami typ = substr(line, RSTART + 1, 3);
272 1.14 enami nam = substr(line, RSTART + 5, RLENGTH - 6);
273 1.14 enami if (typ == "uid") {
274 1.14 enami if (!(nam in uid)) {
275 1.14 enami print "ERROR unmatched uid in `" $0 "'" > \
276 1.14 enami "/dev/stderr"
277 1.14 enami exit 1
278 1.14 enami } else
279 1.14 enami id = uid[nam];
280 1.14 enami } else {
281 1.14 enami if (!(nam in gid)) {
282 1.14 enami print "ERROR unmatched gid in `" $0 "'" > \
283 1.14 enami "/dev/stderr"
284 1.14 enami exit 1
285 1.14 enami } else
286 1.14 enami id = gid[nam];
287 1.14 enami }
288 1.15 enami parsed = parsed substr(line, 1, RSTART - 1) id
289 1.14 enami line = substr(line, RSTART + RLENGTH)
290 1.11 lukem }
291 1.14 enami $0 = parsed line
292 1.2 jdolecek
293 1.1 jdolecek # if device substitutions are not active, do nothing more
294 1.1 jdolecek if (!devsubst) {
295 1.1 jdolecek print
296 1.1 jdolecek next
297 1.1 jdolecek }
298 1.1 jdolecek }
299 1.1 jdolecek
300 1.1 jdolecek # first line of device entry
301 1.1 jdolecek /^[a-z].*\)$/ {
302 1.1 jdolecek if (length(deventry) > 0) {
303 1.1 jdolecek # We have a previous entry to print. Replace all known
304 1.1 jdolecek # character and block devices. If no unknown character
305 1.1 jdolecek # or block device definition remains within the entry,
306 1.1 jdolecek # print it to output, otherwise scrap it.
307 1.14 enami parsed = ""
308 1.27 ozaki while (match(deventry, /%[a-z0-9]*_(blk|chr)%/)) {
309 1.14 enami nam = substr(deventry, RSTART + 1, RLENGTH - 6);
310 1.14 enami typ = substr(deventry, RSTART + RLENGTH - 4, 3);
311 1.14 enami if (typ == "blk") {
312 1.14 enami if (!(nam in blk)) {
313 1.14 enami deventry = $0
314 1.14 enami next
315 1.14 enami } else
316 1.14 enami dev = blk[nam];
317 1.14 enami } else {
318 1.14 enami if (!(nam in chr)) {
319 1.14 enami deventry = $0
320 1.14 enami next
321 1.14 enami } else
322 1.14 enami dev = chr[nam];
323 1.14 enami }
324 1.15 enami parsed = parsed substr(deventry, 1, RSTART - 1) dev
325 1.14 enami deventry = substr(deventry, RSTART + RLENGTH)
326 1.14 enami }
327 1.1 jdolecek
328 1.14 enami print parsed deventry
329 1.1 jdolecek }
330 1.1 jdolecek deventry = $0
331 1.1 jdolecek next
332 1.1 jdolecek }
333 1.1 jdolecek
334 1.1 jdolecek # template line within device substitution section - just keep appending
335 1.1 jdolecek # to the current entry
336 1.1 jdolecek {
337 1.1 jdolecek deventry = deventry "\n" $0
338 1.1 jdolecek }
339