MAKEDEV.awk revision 1.6
1#!/usr/bin/awk -
2#
3#	$NetBSD: MAKEDEV.awk,v 1.6 2003/10/24 08:27:26 jdolecek 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	top = ENVIRON["NETBSDSRCDIR"]
50	if (!top)
51		top = ".."
52	top = top "/sys/"
53	if (system("test -d '" top "'") != 0) {
54		print "ERROR: can't find top of kernel source tree ('" top "' not a directory)" > "/dev/stderr"
55		exit 1
56	}
57
58	machine = ENVIRON["MACHINE"]
59	maarch = ENVIRON["MACHINE_ARCH"]
60	if (!machine || !maarch) {
61		print "ERROR: 'MACHINE' and 'MACHINE_ARCH' must be set in environment" > "/dev/stderr"
62		exit 1
63	}
64
65	# file with major definitions
66	majors[0] = "conf/majors"
67	if (maarch == "arm" && system("test -f '" top "arch/" machine "/conf/majors." machine "'") != 0)
68		majors[1] = "arch/arm/conf/majors.arm32";
69	else if (machine == "evbsh5") {
70		majors[1] = "arch/evbsh5/conf/majors.evbsh5";
71		majors[2] = "arch/sh5/conf/majors.sh5";
72	} else if (machine == "sbmips")
73		majors[1] = "arch/evbmips/conf/majors.evbmips";
74	else
75		majors[1] = "arch/" machine "/conf/majors." machine;
76
77	# process all files with majors and fill the chr[] and blk[]
78	# arrays, used in template processing
79	for (m in majors) {
80		file = top majors[m]
81		if (system("test -f '" file "'") != 0) {
82			print "ERROR: can't find majors file '" file "'" > "/dev/stderr"
83			exit 1
84		}
85			
86		while (getline < file) {
87			if ($1 == "device-major") {
88				if ($3 == "char") {
89					chr[$2] = $4
90					if ($5 == "block")
91						blk[$2] = $6
92				} else if ($3 == "block")
93					blk[$2] = $4
94			}
95		}
96		close(file)
97	}
98
99	# read MD config file for MD device targets
100	cfgfile = "etc." machine "/MAKEDEV.conf"
101	if (system("test -f '" cfgfile "'") != 0) {
102		print "ERROR: no platform MAKEDEV.conf - '" file "' doesn't exist" > "/dev/stderr"
103		exit 1
104	}
105	# skip first two lines - RCS Id and blank line
106	getline < cfgfile
107	getline < cfgfile
108	MDDEV = 0		# MD device targets
109	while (getline < cfgfile) {
110		if (MDDEV)
111			MDDEV = MDDEV "\n" $0
112		else
113			MDDEV = $0
114	}
115	close(cfgfile)
116
117	# determine number of partitions used by platform
118	# there are three variants in tree:
119	# 1. MAXPARTITIONS = 8
120	# 2. MAXPARTITIONS = 16 with no back compat mapping
121	# 3. MAXPARTITIONS = 16 with back compat with old limit of 8
122	# currently all archs, which moved from 8->16 use same
123	# scheme for mapping disk minors, high minor offset
124	# if this changes, the below needs to be adjusted and
125	# additional makedisk_p16foo needs to be added
126	incdir = machine
127	diskpartitions = 0
128	diskbackcompat = 0
129	while (1) {
130		inc = top "arch/" incdir "/include/disklabel.h"
131		if (system("test -f '" inc "'") != 0) {
132			print "ERROR: can't find kernel include file '" inc "'" > "/dev/stderr"
133			exit 1
134		}
135		incdir = 0
136		while (getline < inc) {
137			if ($1 == "#define" && $2 == "MAXPARTITIONS")
138				diskpartitions = $3
139			else if ($1 == "#define" && $2 == "OLDMAXPARTITIONS")
140				diskbackcompat = $3
141			else if ($1 == "#define" && $2 == "RAW_PART")
142				RAWDISK_OFF = $3
143			else if ($1 == "#include" && $2 ~ "<.*/disklabel.h>") {
144				# wrapper, switch to the right file
145				incdir = substr($2, 2)
146				sub("/.*", "", incdir)
147				break;
148			}
149		}
150
151		if (diskpartitions)
152			break;
153
154		if (!incdir) {
155			print "ERROR: can't determine MAXPARTITIONS from include file '" inc "'" > "/dev/stderr"
156			exit 1
157		}
158	}
159	MKDISK = "makedisk_p" diskpartitions	# routine to create disk devs
160	DISKMINOROFFSET = diskpartitions
161	if (diskbackcompat) {
162		MKDISK = MKDISK "high"
163		DISKMINOROFFSET = diskbackcompat
164	}
165	RAWDISK_NAME = sprintf("%c", 97 + RAWDISK_OFF)		# a+offset
166
167	# initially no substitutions
168	devsubst = 0
169	deventry = ""
170}
171
172/%MI_DEVICES_BEGIN%/ {
173	devsubst = 1;
174	next
175}
176
177/%MI_DEVICES_END%/ {
178	devsubst = 0;
179	next
180}
181
182# filter the two unneeded makedisk_p* routines, leave only
183# the one used
184/^makedisk_p8\(\) {/, /^}/ {
185	if (MKDISK != "makedisk_p8")
186		next;
187}
188/^makedisk_p16\(\) {/, /^}/ {
189	if (MKDISK != "makedisk_p16")
190		next;
191}
192/^makedisk_p16high\(\) {/, /^}/ {
193	if (MKDISK != "makedisk_p16high")
194		next;
195}
196
197# special cases aside, handle normal line 
198{
199	sub("^%MD_DEVICES%", MDDEV)
200	sub("%MKDISK%", MKDISK)
201	sub("%DISKMINOROFFSET%", DISKMINOROFFSET)
202	sub("%RAWDISK_OFF%", RAWDISK_OFF)
203	sub("%RAWDISK_NAME%", RAWDISK_NAME)
204
205	# if device substitutions are not active, do nothing more
206	if (!devsubst) {
207		print
208		next
209	}
210}
211
212# first line of device entry
213/^[a-z].*\)$/ {
214	if (length(deventry) > 0) {
215		# We have a previous entry to print. Replace all known
216		# character and block devices. If no unknown character
217		# or block device definition remains within the entry,
218		# print it to output, otherwise scrap it.
219		for (c in chr)
220			gsub("%" c "_chr%", chr[c], deventry)
221		for (b in blk)
222			gsub("%" b "_blk%", blk[b], deventry)
223
224		if (deventry !~ "%[a-z]*_chr%" && deventry !~ "%[a-z]*_blk%")
225			print deventry
226	}
227	deventry = $0
228	next
229}
230
231# template line within device substitution section - just keep appending
232# to the current entry
233{
234	deventry = deventry "\n" $0
235}
236