Home | History | Annotate | Line # | Download | only in etc
MAKEDEV.awk revision 1.10
      1   1.1  jdolecek #!/usr/bin/awk -
      2   1.1  jdolecek #
      3  1.10  dmcmahil #	$NetBSD: MAKEDEV.awk,v 1.10 2003/12/08 23:49:25 dmcmahill Exp $
      4   1.1  jdolecek #
      5   1.1  jdolecek # Copyright (c) 2003 The NetBSD Foundation, Inc.
      6   1.1  jdolecek # All rights reserved.
      7   1.1  jdolecek #
      8   1.1  jdolecek # This code is derived from software contributed to The NetBSD Foundation
      9   1.1  jdolecek # by Jaromir Dolecek.
     10   1.1  jdolecek #
     11   1.1  jdolecek # Redistribution and use in source and binary forms, with or without
     12   1.1  jdolecek # modification, are permitted provided that the following conditions
     13   1.1  jdolecek # are met:
     14   1.1  jdolecek # 1. Redistributions of source code must retain the above copyright
     15   1.1  jdolecek #    notice, this list of conditions and the following disclaimer.
     16   1.1  jdolecek # 2. Redistributions in binary form must reproduce the above copyright
     17   1.1  jdolecek #    notice, this list of conditions and the following disclaimer in the
     18   1.1  jdolecek #    documentation and/or other materials provided with the distribution.
     19   1.1  jdolecek # 3. All advertising materials mentioning features or use of this software
     20   1.1  jdolecek #    must display the following acknowledgement:
     21   1.1  jdolecek #        This product includes software developed by the NetBSD
     22   1.1  jdolecek #        Foundation, Inc. and its contributors.
     23   1.1  jdolecek # 4. Neither the name of The NetBSD Foundation nor the names of its
     24   1.1  jdolecek #    contributors may be used to endorse or promote products derived
     25   1.1  jdolecek #    from this software without specific prior written permission.
     26   1.1  jdolecek #
     27   1.1  jdolecek # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28   1.1  jdolecek # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29   1.1  jdolecek # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30   1.1  jdolecek # PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31   1.1  jdolecek # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32   1.1  jdolecek # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33   1.1  jdolecek # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34   1.1  jdolecek # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35   1.1  jdolecek # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36   1.1  jdolecek # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37   1.1  jdolecek # POSSIBILITY OF SUCH DAMAGE.
     38   1.1  jdolecek #
     39   1.1  jdolecek 
     40   1.2  jdolecek # Script to generate platform MAKEDEV script from MI template, MD
     41   1.2  jdolecek # MAKEDEV.conf and MD/MI major lists
     42   1.2  jdolecek #
     43   1.2  jdolecek # Uses environment variables MACHINE/MACHINE_ARCH to select
     44   1.2  jdolecek # appropriate files, and NETBSDSRCDIR to get root of source tree.
     45   1.2  jdolecek 
     46   1.1  jdolecek BEGIN {
     47   1.1  jdolecek 	# top of source tree, used to find major number list in kernel
     48   1.1  jdolecek 	# sources
     49   1.7  jdolecek 	top = ETCDIR "/../sys/"
     50   1.1  jdolecek 	if (system("test -d '" top "'") != 0) {
     51   1.4  jdolecek 		print "ERROR: can't find top of kernel source tree ('" top "' not a directory)" > "/dev/stderr"
     52   1.1  jdolecek 		exit 1
     53   1.1  jdolecek 	}
     54   1.1  jdolecek 
     55   1.1  jdolecek 	machine = ENVIRON["MACHINE"]
     56   1.1  jdolecek 	maarch = ENVIRON["MACHINE_ARCH"]
     57   1.4  jdolecek 	if (!machine || !maarch) {
     58   1.4  jdolecek 		print "ERROR: 'MACHINE' and 'MACHINE_ARCH' must be set in environment" > "/dev/stderr"
     59   1.4  jdolecek 		exit 1
     60   1.4  jdolecek 	}
     61   1.1  jdolecek 
     62   1.1  jdolecek 	# file with major definitions
     63   1.1  jdolecek 	majors[0] = "conf/majors"
     64   1.9  jdolecek 	if ((maarch == "arm" || maarch == "armeb") && system("test -f '" top "arch/" machine "/conf/majors." machine "'") != 0)
     65   1.1  jdolecek 		majors[1] = "arch/arm/conf/majors.arm32";
     66   1.1  jdolecek 	else if (machine == "evbsh5") {
     67   1.1  jdolecek 		majors[1] = "arch/evbsh5/conf/majors.evbsh5";
     68   1.1  jdolecek 		majors[2] = "arch/sh5/conf/majors.sh5";
     69   1.6  jdolecek 	} else if (machine == "sbmips")
     70   1.6  jdolecek 		majors[1] = "arch/evbmips/conf/majors.evbmips";
     71   1.6  jdolecek 	else
     72   1.1  jdolecek 		majors[1] = "arch/" machine "/conf/majors." machine;
     73   1.1  jdolecek 
     74   1.1  jdolecek 	# process all files with majors and fill the chr[] and blk[]
     75   1.1  jdolecek 	# arrays, used in template processing
     76   1.3    itojun 	for (m in majors) {
     77   1.1  jdolecek 		file = top majors[m]
     78   1.4  jdolecek 		if (system("test -f '" file "'") != 0) {
     79   1.4  jdolecek 			print "ERROR: can't find majors file '" file "'" > "/dev/stderr"
     80   1.4  jdolecek 			exit 1
     81   1.4  jdolecek 		}
     82   1.4  jdolecek 			
     83   1.1  jdolecek 		while (getline < file) {
     84   1.1  jdolecek 			if ($1 == "device-major") {
     85   1.1  jdolecek 				if ($3 == "char") {
     86   1.1  jdolecek 					chr[$2] = $4
     87   1.1  jdolecek 					if ($5 == "block")
     88   1.1  jdolecek 						blk[$2] = $6
     89   1.1  jdolecek 				} else if ($3 == "block")
     90   1.1  jdolecek 					blk[$2] = $4
     91   1.1  jdolecek 			}
     92   1.1  jdolecek 		}
     93   1.5  jdolecek 		close(file)
     94   1.1  jdolecek 	}
     95   1.1  jdolecek 
     96   1.5  jdolecek 	# read MD config file for MD device targets
     97   1.7  jdolecek 	cfgfile = ETCDIR "/etc." machine "/MAKEDEV.conf"
     98   1.5  jdolecek 	if (system("test -f '" cfgfile "'") != 0) {
     99   1.7  jdolecek 		print "ERROR: no platform MAKEDEV.conf - '" cfgfile "' doesn't exist" > "/dev/stderr"
    100   1.5  jdolecek 		exit 1
    101   1.5  jdolecek 	}
    102   1.5  jdolecek 	# skip first two lines - RCS Id and blank line
    103   1.5  jdolecek 	getline < cfgfile
    104   1.5  jdolecek 	getline < cfgfile
    105   1.2  jdolecek 	MDDEV = 0		# MD device targets
    106   1.2  jdolecek 	while (getline < cfgfile) {
    107   1.5  jdolecek 		if (MDDEV)
    108   1.5  jdolecek 			MDDEV = MDDEV "\n" $0
    109   1.5  jdolecek 		else
    110   1.5  jdolecek 			MDDEV = $0
    111   1.5  jdolecek 	}
    112   1.5  jdolecek 	close(cfgfile)
    113   1.5  jdolecek 
    114   1.5  jdolecek 	# determine number of partitions used by platform
    115   1.5  jdolecek 	# there are three variants in tree:
    116   1.5  jdolecek 	# 1. MAXPARTITIONS = 8
    117   1.5  jdolecek 	# 2. MAXPARTITIONS = 16 with no back compat mapping
    118   1.5  jdolecek 	# 3. MAXPARTITIONS = 16 with back compat with old limit of 8
    119   1.5  jdolecek 	# currently all archs, which moved from 8->16 use same
    120   1.5  jdolecek 	# scheme for mapping disk minors, high minor offset
    121   1.5  jdolecek 	# if this changes, the below needs to be adjusted and
    122   1.5  jdolecek 	# additional makedisk_p16foo needs to be added
    123   1.5  jdolecek 	incdir = machine
    124   1.5  jdolecek 	diskpartitions = 0
    125   1.5  jdolecek 	diskbackcompat = 0
    126   1.5  jdolecek 	while (1) {
    127   1.5  jdolecek 		inc = top "arch/" incdir "/include/disklabel.h"
    128   1.5  jdolecek 		if (system("test -f '" inc "'") != 0) {
    129   1.5  jdolecek 			print "ERROR: can't find kernel include file '" inc "'" > "/dev/stderr"
    130   1.5  jdolecek 			exit 1
    131   1.5  jdolecek 		}
    132   1.5  jdolecek 		incdir = 0
    133   1.5  jdolecek 		while (getline < inc) {
    134   1.6  jdolecek 			if ($1 == "#define" && $2 == "MAXPARTITIONS")
    135   1.5  jdolecek 				diskpartitions = $3
    136   1.6  jdolecek 			else if ($1 == "#define" && $2 == "OLDMAXPARTITIONS")
    137   1.6  jdolecek 				diskbackcompat = $3
    138   1.6  jdolecek 			else if ($1 == "#define" && $2 == "RAW_PART")
    139   1.6  jdolecek 				RAWDISK_OFF = $3
    140   1.6  jdolecek 			else if ($1 == "#include" && $2 ~ "<.*/disklabel.h>") {
    141   1.5  jdolecek 				# wrapper, switch to the right file
    142   1.5  jdolecek 				incdir = substr($2, 2)
    143   1.5  jdolecek 				sub("/.*", "", incdir)
    144   1.5  jdolecek 				break;
    145   1.5  jdolecek 			}
    146   1.5  jdolecek 		}
    147   1.5  jdolecek 
    148   1.5  jdolecek 		if (diskpartitions)
    149   1.5  jdolecek 			break;
    150   1.5  jdolecek 
    151   1.5  jdolecek 		if (!incdir) {
    152   1.5  jdolecek 			print "ERROR: can't determine MAXPARTITIONS from include file '" inc "'" > "/dev/stderr"
    153   1.5  jdolecek 			exit 1
    154   1.5  jdolecek 		}
    155   1.2  jdolecek 	}
    156   1.5  jdolecek 	MKDISK = "makedisk_p" diskpartitions	# routine to create disk devs
    157   1.6  jdolecek 	DISKMINOROFFSET = diskpartitions
    158   1.6  jdolecek 	if (diskbackcompat) {
    159   1.5  jdolecek 		MKDISK = MKDISK "high"
    160   1.6  jdolecek 		DISKMINOROFFSET = diskbackcompat
    161   1.6  jdolecek 	}
    162   1.6  jdolecek 	RAWDISK_NAME = sprintf("%c", 97 + RAWDISK_OFF)		# a+offset
    163   1.2  jdolecek 
    164   1.1  jdolecek 	# initially no substitutions
    165   1.1  jdolecek 	devsubst = 0
    166   1.1  jdolecek 	deventry = ""
    167   1.1  jdolecek }
    168   1.1  jdolecek 
    169   1.1  jdolecek /%MI_DEVICES_BEGIN%/ {
    170   1.1  jdolecek 	devsubst = 1;
    171   1.1  jdolecek 	next
    172   1.1  jdolecek }
    173   1.1  jdolecek 
    174   1.1  jdolecek /%MI_DEVICES_END%/ {
    175   1.1  jdolecek 	devsubst = 0;
    176   1.1  jdolecek 	next
    177   1.1  jdolecek }
    178   1.1  jdolecek 
    179   1.4  jdolecek # filter the two unneeded makedisk_p* routines, leave only
    180   1.4  jdolecek # the one used
    181  1.10  dmcmahil /^makedisk_p8\(\) \{/, /^\}/ {
    182   1.4  jdolecek 	if (MKDISK != "makedisk_p8")
    183   1.4  jdolecek 		next;
    184   1.4  jdolecek }
    185  1.10  dmcmahil /^makedisk_p16\(\) \{/, /^\}/ {
    186   1.4  jdolecek 	if (MKDISK != "makedisk_p16")
    187   1.4  jdolecek 		next;
    188   1.4  jdolecek }
    189  1.10  dmcmahil /^makedisk_p16high\(\) \{/, /^\}/ {
    190   1.4  jdolecek 	if (MKDISK != "makedisk_p16high")
    191   1.4  jdolecek 		next;
    192   1.4  jdolecek }
    193   1.4  jdolecek 
    194   1.4  jdolecek # special cases aside, handle normal line 
    195   1.1  jdolecek {
    196   1.8  jdolecek 	sub(/^%MD_DEVICES%/, MDDEV)
    197   1.8  jdolecek 	sub(/%MKDISK%/, MKDISK)
    198   1.8  jdolecek 	sub(/%DISKMINOROFFSET%/, DISKMINOROFFSET)
    199   1.8  jdolecek 	sub(/%RAWDISK_OFF%/, RAWDISK_OFF)
    200   1.8  jdolecek 	sub(/%RAWDISK_NAME%/, RAWDISK_NAME)
    201   1.2  jdolecek 
    202   1.1  jdolecek 	# if device substitutions are not active, do nothing more
    203   1.1  jdolecek 	if (!devsubst) {
    204   1.1  jdolecek 		print
    205   1.1  jdolecek 		next
    206   1.1  jdolecek 	}
    207   1.1  jdolecek }
    208   1.1  jdolecek 
    209   1.1  jdolecek # first line of device entry
    210   1.1  jdolecek /^[a-z].*\)$/ {
    211   1.1  jdolecek 	if (length(deventry) > 0) {
    212   1.1  jdolecek 		# We have a previous entry to print. Replace all known
    213   1.1  jdolecek 		# character and block devices. If no unknown character
    214   1.1  jdolecek 		# or block device definition remains within the entry,
    215   1.1  jdolecek 		# print it to output, otherwise scrap it.
    216   1.3    itojun 		for (c in chr)
    217   1.1  jdolecek 			gsub("%" c "_chr%", chr[c], deventry)
    218   1.3    itojun 		for (b in blk)
    219   1.1  jdolecek 			gsub("%" b "_blk%", blk[b], deventry)
    220   1.1  jdolecek 
    221   1.8  jdolecek 		if (deventry !~ /%[a-z]*_chr%/ && deventry !~ /%[a-z]*_blk%/)
    222   1.1  jdolecek 			print deventry
    223   1.1  jdolecek 	}
    224   1.1  jdolecek 	deventry = $0
    225   1.1  jdolecek 	next
    226   1.1  jdolecek }
    227   1.1  jdolecek 
    228   1.1  jdolecek # template line within device substitution section - just keep appending
    229   1.1  jdolecek # to the current entry
    230   1.1  jdolecek {
    231   1.1  jdolecek 	deventry = deventry "\n" $0
    232   1.1  jdolecek }
    233