Home | History | Annotate | Line # | Download | only in etc
MAKEDEV.awk revision 1.26
      1 #!/usr/bin/awk -
      2 #
      3 #	$NetBSD: MAKEDEV.awk,v 1.26 2019/06/13 20:54:04 christos 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 
     39 BEGIN {
     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 == "#define" && $2 == "RAW_PART")
    139 				RAWDISK_OFF = $3
    140 			else if ($1 == "#include" && 
    141 				 $2 ~ "<.*/disklabel.h>" &&
    142 				 $2 !~ ".*nbinclude.*")
    143 			{
    144 				# wrapper, switch to the right file
    145 				incdir = substr($2, 2)
    146 				sub("/.*", "", incdir)
    147 				break;
    148 			}
    149 		}
    150 		close(inc)
    151 
    152 		if (diskpartitions)
    153 			break;
    154 
    155 		if (!incdir) {
    156 			print "ERROR: can't determine MAXPARTITIONS from include file '" inc "'" > "/dev/stderr"
    157 			exit 1
    158 		}
    159 	}
    160 	MKDISK = "makedisk_p" diskpartitions	# routine to create disk devs
    161 	DISKMINOROFFSET = diskpartitions
    162 	if (diskbackcompat) {
    163 		MKDISK = MKDISK "high"
    164 		DISKMINOROFFSET = diskbackcompat
    165 	}
    166 	RAWDISK_NAME = sprintf("%c", 97 + RAWDISK_OFF)		# a+offset
    167 
    168 	# read etc/master.passwd for user name->UID mapping
    169 	idfile = srcdir "/etc/master.passwd"
    170 	if (system("test -f '" idfile "'") != 0) {
    171 		print "ERROR: can't find password file '" idfile "'" > "/dev/stderr"
    172 		exit 1
    173 	}
    174 	oldFS=FS
    175 	FS=":"
    176 	while (getline < idfile) {
    177 		uid[$1] = $3
    178 	}
    179 	close(idfile)
    180 	FS=oldFS
    181 
    182 	# read etc/group for group name->GID mapping
    183 	idfile = srcdir "/etc/group"
    184 	if (system("test -f '" idfile "'") != 0) {
    185 		print "ERROR: can't find group file '" idfile "'" > "/dev/stderr"
    186 		exit 1
    187 	}
    188 	oldFS=FS
    189 	FS=":"
    190 	while (getline < idfile) {
    191 		gid[$1] = $3
    192 	}
    193 	close(idfile)
    194 	FS=oldFS
    195 
    196 	# initially no substitutions
    197 	devsubst = 0
    198 	deventry = ""
    199 }
    200 
    201 /%MI_DEVICES_BEGIN%/ {
    202 	devsubst = 1;
    203 	next
    204 }
    205 
    206 /%MI_DEVICES_END%/ {
    207 	devsubst = 0;
    208 	next
    209 }
    210 
    211 # output 'Generated from' lines
    212 /\$[N]etBSD/ {
    213 	print "#"
    214 	print "# Generated from:"
    215 
    216 	# MAKEDEV.awk (this script) RCS Id
    217 	ARCSID = "$NetBSD: MAKEDEV.awk,v 1.26 2019/06/13 20:54:04 christos Exp $"
    218 	gsub(/\$/, "", ARCSID)
    219 	print "#	" ARCSID
    220 	
    221 	# MAKEDEV.tmpl RCS Id
    222 	gsub(/\$/, "")
    223 	print $0
    224 
    225 	# MD MAKEDEV.conf RCS Id
    226 	# strip leading hash and insert machine subdirectory name
    227 	gsub(/\$/, "", CONFRCSID)
    228 	sub(/^\# /, "", CONFRCSID)
    229 	sub(/MAKEDEV.conf/, "etc." machine "/MAKEDEV.conf", CONFRCSID)
    230 	print "#	" CONFRCSID
    231 
    232 	next # don't print the RCS Id line again
    233 }
    234 
    235 # filter the 'PLEASE RUN ...' paragraph
    236 /^\#   PLEASE RUN/, /^\#\#\#\#\#\#/ {
    237 	next
    238 }
    239  
    240 # filter the device list
    241 /^\# Tapes/,/^$/ {
    242 	next
    243 }
    244 
    245 # filter the two unneeded makedisk_p* routines, leave only
    246 # the one used
    247 /^makedisk_p8\(\) \{/, /^\}/ {
    248 	if (MKDISK != "makedisk_p8")
    249 		next;
    250 }
    251 /^makedisk_p16\(\) \{/, /^\}/ {
    252 	if (MKDISK != "makedisk_p16")
    253 		next;
    254 }
    255 /^makedisk_p16high\(\) \{/, /^\}/ {
    256 	if (MKDISK != "makedisk_p16high")
    257 		next;
    258 }
    259 
    260 # special cases aside, handle normal line
    261 {
    262 	sub(/^%MD_DEVICES%/, MDDEV)
    263 	sub(/%MKDISK%/, MKDISK)
    264 	sub(/%DISKMINOROFFSET%/, DISKMINOROFFSET)
    265 	sub(/%RAWDISK_OFF%/, RAWDISK_OFF)
    266 	sub(/%RAWDISK_NAME%/, RAWDISK_NAME)
    267 	sub(/%CONSOLE_CMAJOR%/, CONSOLE_CMAJOR)
    268 	parsed = ""
    269 	line = $0
    270 	while (match(line, /%[gu]id_[_a-z]*%/)) {
    271 		typ = substr(line, RSTART + 1, 3);
    272 		nam = substr(line, RSTART + 5, RLENGTH - 6);
    273 		if (typ == "uid") {
    274 			if (!(nam in uid)) {
    275 				print "ERROR unmatched uid in `" $0 "'" > \
    276 				    "/dev/stderr"
    277 				exit 1
    278 			} else
    279 				id = uid[nam];
    280 		} else {
    281 			if (!(nam in gid)) {
    282 				print "ERROR unmatched gid in `" $0 "'" > \
    283 				    "/dev/stderr"
    284 				exit 1
    285 			} else
    286 				id = gid[nam];
    287 		}
    288 		parsed = parsed substr(line, 1, RSTART - 1) id
    289 		line = substr(line, RSTART + RLENGTH)
    290 	}
    291 	$0 = parsed line
    292 
    293 	# if device substitutions are not active, do nothing more
    294 	if (!devsubst) {
    295 		print
    296 		next
    297 	}
    298 }
    299 
    300 # first line of device entry
    301 /^[a-z].*\)$/ {
    302 	if (length(deventry) > 0) {
    303 		# We have a previous entry to print. Replace all known
    304 		# character and block devices. If no unknown character
    305 		# or block device definition remains within the entry,
    306 		# print it to output, otherwise scrap it.
    307 		parsed = ""
    308 		while (match(deventry, /%[a-z]*_(blk|chr)%/)) {
    309 			nam = substr(deventry, RSTART + 1, RLENGTH - 6);
    310 			typ = substr(deventry, RSTART + RLENGTH - 4, 3);
    311 			if (typ == "blk") {
    312 				if (!(nam in blk)) {
    313 					deventry = $0
    314 					next
    315 				} else
    316 					dev = blk[nam];
    317 			} else {
    318 				if (!(nam in chr)) {
    319 					deventry = $0
    320 					next
    321 				} else
    322 					dev = chr[nam];
    323 			}
    324 			parsed = parsed substr(deventry, 1, RSTART - 1) dev
    325 			deventry = substr(deventry, RSTART + RLENGTH)
    326 		}
    327 
    328 		print parsed deventry
    329 	}
    330 	deventry = $0
    331 	next
    332 }
    333 
    334 # template line within device substitution section - just keep appending
    335 # to the current entry
    336 {
    337 	deventry = deventry "\n" $0
    338 }
    339