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