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