Home | History | Annotate | Line # | Download | only in etc
MAKEDEV.awk revision 1.11
      1 #!/usr/bin/awk -
      2 #
      3 #	$NetBSD: MAKEDEV.awk,v 1.11 2003/12/19 06:04:16 lukem 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 
     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 - RCS Id and blank line
    103 	getline < cfgfile
    104 	getline < cfgfile
    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 == "#define" && $2 == "RAW_PART")
    139 				RAWDISK_OFF = $3
    140 			else if ($1 == "#include" && $2 ~ "<.*/disklabel.h>") {
    141 				# wrapper, switch to the right file
    142 				incdir = substr($2, 2)
    143 				sub("/.*", "", incdir)
    144 				break;
    145 			}
    146 		}
    147 
    148 		if (diskpartitions)
    149 			break;
    150 
    151 		if (!incdir) {
    152 			print "ERROR: can't determine MAXPARTITIONS from include file '" inc "'" > "/dev/stderr"
    153 			exit 1
    154 		}
    155 	}
    156 	MKDISK = "makedisk_p" diskpartitions	# routine to create disk devs
    157 	DISKMINOROFFSET = diskpartitions
    158 	if (diskbackcompat) {
    159 		MKDISK = MKDISK "high"
    160 		DISKMINOROFFSET = diskbackcompat
    161 	}
    162 	RAWDISK_NAME = sprintf("%c", 97 + RAWDISK_OFF)		# a+offset
    163 
    164 	# read etc/master.passwd for user name->UID mapping
    165 	idfile = srcdir "/etc/master.passwd"
    166 	if (system("test -f '" idfile "'") != 0) {
    167 		print "ERROR: can't find password file '" idfile "'" > "/dev/stderr"
    168 		exit 1
    169 	}
    170 	oldFS=FS
    171 	FS=":"
    172 	while (getline < idfile) {
    173 		uid[$1] = $3
    174 	}
    175 	close(idfile)
    176 	FS=oldFS
    177 
    178 	# read etc/group for group name->GID mapping
    179 	idfile = srcdir "/etc/group"
    180 	if (system("test -f '" idfile "'") != 0) {
    181 		print "ERROR: can't find group file '" idfile "'" > "/dev/stderr"
    182 		exit 1
    183 	}
    184 	oldFS=FS
    185 	FS=":"
    186 	while (getline < idfile) {
    187 		gid[$1] = $3
    188 	}
    189 	close(idfile)
    190 	FS=oldFS
    191 
    192 	# initially no substitutions
    193 	devsubst = 0
    194 	deventry = ""
    195 }
    196 
    197 /%MI_DEVICES_BEGIN%/ {
    198 	devsubst = 1;
    199 	next
    200 }
    201 
    202 /%MI_DEVICES_END%/ {
    203 	devsubst = 0;
    204 	next
    205 }
    206 
    207 # filter the two unneeded makedisk_p* routines, leave only
    208 # the one used
    209 /^makedisk_p8\(\) \{/, /^\}/ {
    210 	if (MKDISK != "makedisk_p8")
    211 		next;
    212 }
    213 /^makedisk_p16\(\) \{/, /^\}/ {
    214 	if (MKDISK != "makedisk_p16")
    215 		next;
    216 }
    217 /^makedisk_p16high\(\) \{/, /^\}/ {
    218 	if (MKDISK != "makedisk_p16high")
    219 		next;
    220 }
    221 
    222 # special cases aside, handle normal line
    223 {
    224 	sub(/^%MD_DEVICES%/, MDDEV)
    225 	sub(/%MKDISK%/, MKDISK)
    226 	sub(/%DISKMINOROFFSET%/, DISKMINOROFFSET)
    227 	sub(/%RAWDISK_OFF%/, RAWDISK_OFF)
    228 	sub(/%RAWDISK_NAME%/, RAWDISK_NAME)
    229 	for (u in uid)
    230 		gsub("%uid_" u "%", uid[u])
    231 	for (g in gid)
    232 		gsub("%gid_" g "%", gid[g])
    233 	if ($0 ~ /%[gu]id_[a-z]*%/) {
    234 		print "ERROR unmatched gid or uid in `" $0 "'"> "/dev/stderr"
    235 		exit 1
    236 	}
    237 
    238 	# if device substitutions are not active, do nothing more
    239 	if (!devsubst) {
    240 		print
    241 		next
    242 	}
    243 }
    244 
    245 # first line of device entry
    246 /^[a-z].*\)$/ {
    247 	if (length(deventry) > 0) {
    248 		# We have a previous entry to print. Replace all known
    249 		# character and block devices. If no unknown character
    250 		# or block device definition remains within the entry,
    251 		# print it to output, otherwise scrap it.
    252 		for (c in chr)
    253 			gsub("%" c "_chr%", chr[c], deventry)
    254 		for (b in blk)
    255 			gsub("%" b "_blk%", blk[b], deventry)
    256 
    257 		if (deventry !~ /%[a-z]*_(chr|blk)%/)
    258 			print deventry
    259 	}
    260 	deventry = $0
    261 	next
    262 }
    263 
    264 # template line within device substitution section - just keep appending
    265 # to the current entry
    266 {
    267 	deventry = deventry "\n" $0
    268 }
    269