MAKEDEV.awk revision 1.17 1 #!/usr/bin/awk -
2 #
3 # $NetBSD: MAKEDEV.awk,v 1.17 2007/03/09 13:57:54 dsl 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 == "evbsh5") {
68 majors[1] = "arch/evbsh5/conf/majors.evbsh5";
69 majors[2] = "arch/sh5/conf/majors.sh5";
70 } else if (machine == "sbmips")
71 majors[1] = "arch/evbmips/conf/majors.evbmips";
72 else
73 majors[1] = "arch/" machine "/conf/majors." machine;
74
75 # process all files with majors and fill the chr[] and blk[]
76 # arrays, used in template processing
77 for (m in majors) {
78 file = top majors[m]
79 if (system("test -f '" file "'") != 0) {
80 print "ERROR: can't find majors file '" file "'" > "/dev/stderr"
81 exit 1
82 }
83 while (getline < file) {
84 if ($1 == "device-major") {
85 if ($3 == "char") {
86 chr[$2] = $4
87 if ($5 == "block")
88 blk[$2] = $6
89 } else if ($3 == "block")
90 blk[$2] = $4
91 }
92 }
93 close(file)
94 }
95 CONSOLE_CMAJOR = chr["cons"]
96 if (CONSOLE_CMAJOR == "") {
97 print "ERROR: no entry for 'cons' in majors file" > "/dev/stderr"
98 exit 1
99 }
100
101 # read MD config file for MD device targets
102 cfgfile = srcdir "/etc/etc." machine "/MAKEDEV.conf"
103 if (system("test -f '" cfgfile "'") != 0) {
104 print "ERROR: no platform MAKEDEV.conf - '" cfgfile "' doesn't exist" > "/dev/stderr"
105 exit 1
106 }
107 # skip first two lines
108 getline CONFRCSID < cfgfile # RCS Id
109 getline < cfgfile # blank line
110 MDDEV = 0 # MD device targets
111 while (getline < cfgfile) {
112 if (MDDEV)
113 MDDEV = MDDEV "\n" $0
114 else
115 MDDEV = $0
116 }
117 close(cfgfile)
118
119 # determine number of partitions used by platform
120 # there are three variants in tree:
121 # 1. MAXPARTITIONS = 8
122 # 2. MAXPARTITIONS = 16 with no back compat mapping
123 # 3. MAXPARTITIONS = 16 with back compat with old limit of 8
124 # currently all archs, which moved from 8->16 use same
125 # scheme for mapping disk minors, high minor offset
126 # if this changes, the below needs to be adjusted and
127 # additional makedisk_p16foo needs to be added
128 incdir = machine
129 diskpartitions = 0
130 diskbackcompat = 0
131 while (1) {
132 inc = top "arch/" incdir "/include/disklabel.h"
133 if (system("test -f '" inc "'") != 0) {
134 print "ERROR: can't find kernel include file '" inc "'" > "/dev/stderr"
135 exit 1
136 }
137 incdir = 0
138 while (getline < inc) {
139 if ($1 == "#define" && $2 == "MAXPARTITIONS")
140 diskpartitions = $3
141 else if ($1 == "#define" && $2 == "OLDMAXPARTITIONS")
142 diskbackcompat = $3
143 else if ($1 == "#define" && $2 == "RAW_PART")
144 RAWDISK_OFF = $3
145 else if ($1 == "#include" &&
146 $2 ~ "<.*/disklabel.h>" &&
147 $2 !~ ".*nbinclude.*")
148 {
149 # wrapper, switch to the right file
150 incdir = substr($2, 2)
151 sub("/.*", "", incdir)
152 break;
153 }
154 }
155 close(inc)
156
157 if (diskpartitions)
158 break;
159
160 if (!incdir) {
161 print "ERROR: can't determine MAXPARTITIONS from include file '" inc "'" > "/dev/stderr"
162 exit 1
163 }
164 }
165 MKDISK = "makedisk_p" diskpartitions # routine to create disk devs
166 DISKMINOROFFSET = diskpartitions
167 if (diskbackcompat) {
168 MKDISK = MKDISK "high"
169 DISKMINOROFFSET = diskbackcompat
170 }
171 RAWDISK_NAME = sprintf("%c", 97 + RAWDISK_OFF) # a+offset
172
173 # read etc/master.passwd for user name->UID mapping
174 idfile = srcdir "/etc/master.passwd"
175 if (system("test -f '" idfile "'") != 0) {
176 print "ERROR: can't find password file '" idfile "'" > "/dev/stderr"
177 exit 1
178 }
179 oldFS=FS
180 FS=":"
181 while (getline < idfile) {
182 uid[$1] = $3
183 }
184 close(idfile)
185 FS=oldFS
186
187 # read etc/group for group name->GID mapping
188 idfile = srcdir "/etc/group"
189 if (system("test -f '" idfile "'") != 0) {
190 print "ERROR: can't find group file '" idfile "'" > "/dev/stderr"
191 exit 1
192 }
193 oldFS=FS
194 FS=":"
195 while (getline < idfile) {
196 gid[$1] = $3
197 }
198 close(idfile)
199 FS=oldFS
200
201 # initially no substitutions
202 devsubst = 0
203 deventry = ""
204 }
205
206 /%MI_DEVICES_BEGIN%/ {
207 devsubst = 1;
208 next
209 }
210
211 /%MI_DEVICES_END%/ {
212 devsubst = 0;
213 next
214 }
215
216 # output 'Generated from' lines
217 /\$[N]etBSD/ {
218 print "#"
219 print "# Generated from:"
220
221 # MAKEDEV.awk (this script) RCS Id
222 ARCSID = "$NetBSD: MAKEDEV.awk,v 1.17 2007/03/09 13:57:54 dsl Exp $"
223 gsub(/\$/, "", ARCSID)
224 print "# " ARCSID
225
226 # MAKEDEV.tmpl RCS Id
227 gsub(/\$/, "")
228 print $0
229
230 # MD MAKEDEV.conf RCS Id
231 # strip leading hash and insert machine subdirectory name
232 gsub(/\$/, "", CONFRCSID)
233 sub(/^\# /, "", CONFRCSID)
234 sub(/MAKEDEV.conf/, "etc." machine "/MAKEDEV.conf", CONFRCSID)
235 print "# " CONFRCSID
236
237 next # don't print the RCS Id line again
238 }
239
240 # filter the 'PLEASE RUN ...' paragraph
241 /^\# PLEASE RUN/, /^\#\#\#\#\#\#/ {
242 next
243 }
244
245 # filter the device list
246 /^\# Tapes/,/^$/ {
247 next
248 }
249
250 # filter the two unneeded makedisk_p* routines, leave only
251 # the one used
252 /^makedisk_p8\(\) \{/, /^\}/ {
253 if (MKDISK != "makedisk_p8")
254 next;
255 }
256 /^makedisk_p16\(\) \{/, /^\}/ {
257 if (MKDISK != "makedisk_p16")
258 next;
259 }
260 /^makedisk_p16high\(\) \{/, /^\}/ {
261 if (MKDISK != "makedisk_p16high")
262 next;
263 }
264
265 # special cases aside, handle normal line
266 {
267 sub(/^%MD_DEVICES%/, MDDEV)
268 sub(/%MKDISK%/, MKDISK)
269 sub(/%DISKMINOROFFSET%/, DISKMINOROFFSET)
270 sub(/%RAWDISK_OFF%/, RAWDISK_OFF)
271 sub(/%RAWDISK_NAME%/, RAWDISK_NAME)
272 sub(/%CONSOLE_CMAJOR%/, CONSOLE_CMAJOR)
273 parsed = ""
274 line = $0
275 while (match(line, /%[gu]id_[a-z]*%/)) {
276 typ = substr(line, RSTART + 1, 3);
277 nam = substr(line, RSTART + 5, RLENGTH - 6);
278 if (typ == "uid") {
279 if (!(nam in uid)) {
280 print "ERROR unmatched uid in `" $0 "'" > \
281 "/dev/stderr"
282 exit 1
283 } else
284 id = uid[nam];
285 } else {
286 if (!(nam in gid)) {
287 print "ERROR unmatched gid in `" $0 "'" > \
288 "/dev/stderr"
289 exit 1
290 } else
291 id = gid[nam];
292 }
293 parsed = parsed substr(line, 1, RSTART - 1) id
294 line = substr(line, RSTART + RLENGTH)
295 }
296 $0 = parsed line
297
298 # if device substitutions are not active, do nothing more
299 if (!devsubst) {
300 print
301 next
302 }
303 }
304
305 # first line of device entry
306 /^[a-z].*\)$/ {
307 if (length(deventry) > 0) {
308 # We have a previous entry to print. Replace all known
309 # character and block devices. If no unknown character
310 # or block device definition remains within the entry,
311 # print it to output, otherwise scrap it.
312 parsed = ""
313 while (match(deventry, /%[a-z]*_(blk|chr)%/)) {
314 nam = substr(deventry, RSTART + 1, RLENGTH - 6);
315 typ = substr(deventry, RSTART + RLENGTH - 4, 3);
316 if (typ == "blk") {
317 if (!(nam in blk)) {
318 deventry = $0
319 next
320 } else
321 dev = blk[nam];
322 } else {
323 if (!(nam in chr)) {
324 deventry = $0
325 next
326 } else
327 dev = chr[nam];
328 }
329 parsed = parsed substr(deventry, 1, RSTART - 1) dev
330 deventry = substr(deventry, RSTART + RLENGTH)
331 }
332
333 print parsed deventry
334 }
335 deventry = $0
336 next
337 }
338
339 # template line within device substitution section - just keep appending
340 # to the current entry
341 {
342 deventry = deventry "\n" $0
343 }
344