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