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