Module Name:    src
Committed By:   rtr
Date:           Sat Jun 28 09:16:18 UTC 2014

Modified Files:
        src/distrib/sparc64/bootfs: boot.cfg
        src/sys/arch/i386/stand/boot: boot2.c
        src/sys/arch/i386/stand/lib: bootmenu.c bootmenu.h
        src/sys/arch/i386/stand/pxeboot: main.c
        src/sys/arch/sparc/stand/ofwboot: boot.c
        src/sys/arch/zaurus/stand/zboot: boot.c boot.h bootmenu.c bootmenu.h
            pathnames.h
        src/sys/lib/libsa: Makefile
Added Files:
        src/sys/lib/libsa: bootcfg.c bootcfg.h

Log Message:
patch posted to tech-kern@ 2014/06/25 for review with minor changes
resulting from feedback.

move multiple copies of code for parsing boot.cfg file from sparc, i386
and zaurus into libsa/bootcfg.{h,c}. largely retained i386 parsing logic
in addition to keeping sparc dispatch function while remaining consistent
with boot.cfg(5).

previous sparc64 file format has been obsoleted but only used by boot
CDs distrib/sparc64/bootfs/boot.cfg has been updated to compensate.

exported names have been prefixed with either BOOTCFG_ or bootcfg_ as per
feedback from christos@

tested on amd64 & sparc64 but not zaurus.


To generate a diff of this commit:
cvs rdiff -u -r1.1 -r1.2 src/distrib/sparc64/bootfs/boot.cfg
cvs rdiff -u -r1.62 -r1.63 src/sys/arch/i386/stand/boot/boot2.c
cvs rdiff -u -r1.12 -r1.13 src/sys/arch/i386/stand/lib/bootmenu.c
cvs rdiff -u -r1.3 -r1.4 src/sys/arch/i386/stand/lib/bootmenu.h
cvs rdiff -u -r1.30 -r1.31 src/sys/arch/i386/stand/pxeboot/main.c
cvs rdiff -u -r1.30 -r1.31 src/sys/arch/sparc/stand/ofwboot/boot.c
cvs rdiff -u -r1.5 -r1.6 src/sys/arch/zaurus/stand/zboot/boot.c
cvs rdiff -u -r1.2 -r1.3 src/sys/arch/zaurus/stand/zboot/boot.h \
    src/sys/arch/zaurus/stand/zboot/bootmenu.c
cvs rdiff -u -r1.1 -r1.2 src/sys/arch/zaurus/stand/zboot/bootmenu.h
cvs rdiff -u -r1.3 -r1.4 src/sys/arch/zaurus/stand/zboot/pathnames.h
cvs rdiff -u -r1.82 -r1.83 src/sys/lib/libsa/Makefile
cvs rdiff -u -r0 -r1.1 src/sys/lib/libsa/bootcfg.c \
    src/sys/lib/libsa/bootcfg.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/distrib/sparc64/bootfs/boot.cfg
diff -u src/distrib/sparc64/bootfs/boot.cfg:1.1 src/distrib/sparc64/bootfs/boot.cfg:1.2
--- src/distrib/sparc64/bootfs/boot.cfg:1.1	Fri Apr  2 21:29:30 2010
+++ src/distrib/sparc64/bootfs/boot.cfg	Sat Jun 28 09:16:18 2014
@@ -1 +1 @@
-bootpartition	:a
+bootpartition=:a

Index: src/sys/arch/i386/stand/boot/boot2.c
diff -u src/sys/arch/i386/stand/boot/boot2.c:1.62 src/sys/arch/i386/stand/boot/boot2.c:1.63
--- src/sys/arch/i386/stand/boot/boot2.c:1.62	Wed Mar 26 17:58:57 2014
+++ src/sys/arch/i386/stand/boot/boot2.c	Sat Jun 28 09:16:18 2014
@@ -1,4 +1,4 @@
-/*	$NetBSD: boot2.c,v 1.62 2014/03/26 17:58:57 christos Exp $	*/
+/*	$NetBSD: boot2.c,v 1.63 2014/06/28 09:16:18 rtr Exp $	*/
 
 /*-
  * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
@@ -72,6 +72,7 @@
 #include <sys/bootblock.h>
 
 #include <lib/libsa/stand.h>
+#include <lib/libsa/bootcfg.h>
 #include <lib/libsa/ufs.h>
 #include <lib/libkern/libkern.h>
 
@@ -239,7 +240,7 @@ static void
 clearit(void)
 {
 
-	if (bootconf.clear)
+	if (bootcfg_info.clear)
 		clear_pc_screen();
 }
 
@@ -268,9 +269,10 @@ print_banner(void)
 	clearit();
 #ifndef SMALL
 	int n;
-	if (bootconf.banner[0]) {
-		for (n = 0; bootconf.banner[n] && n < MAXBANNER; n++) 
-			printf("%s\n", bootconf.banner[n]);
+	if (bootcfg_info.banner[0]) {
+		for (n = 0; bootcfg_info.banner[n]
+		    && n < BOOTCFG_MAXBANNER; n++) 
+			printf("%s\n", bootcfg_info.banner[n]);
 	} else {
 #endif /* !SMALL */
 		printf("\n"
@@ -326,9 +328,9 @@ boot2(int biosdev, uint64_t biossector)
 
 #ifndef SMALL
 	if (!(boot_params.bp_flags & X86_BP_FLAGS_NOBOOTCONF)) {
-		parsebootconf(BOOTCONF);
+		parsebootconf(BOOTCFG_FILENAME);
 	} else {
-		bootconf.timeout = boot_params.bp_timeout;
+		bootcfg_info.timeout = boot_params.bp_timeout;
 	}
 	
 
@@ -336,14 +338,14 @@ boot2(int biosdev, uint64_t biossector)
 	 * If console set in boot.cfg, switch to it.
 	 * This will print the banner, so we don't need to explicitly do it
 	 */
-	if (bootconf.consdev)
-		command_consdev(bootconf.consdev);
+	if (bootcfg_info.consdev)
+		command_consdev(bootcfg_info.consdev);
 	else 
 		print_banner();
 
 	/* Display the menu, if applicable */
 	twiddle_toggle = 0;
-	if (bootconf.nummenu > 0) {
+	if (bootcfg_info.nummenu > 0) {
 		/* Does not return */
 		doboottypemenu();
 	}
@@ -361,7 +363,8 @@ boot2(int biosdev, uint64_t biossector)
 #ifdef SMALL
 		c = awaitkey(boot_params.bp_timeout, 1);
 #else
-		c = awaitkey((bootconf.timeout < 0) ? 0 : bootconf.timeout, 1);
+		c = awaitkey((bootcfg_info.timeout < 0) ? 0
+		    : bootcfg_info.timeout, 1);
 #endif
 		if ((c != '\r') && (c != '\n') && (c != '\0')) {
 		    if ((boot_params.bp_flags & X86_BP_FLAGS_PASSWORD) == 0) {
@@ -533,7 +536,7 @@ void
 command_menu(char *arg)
 {
 
-	if (bootconf.nummenu > 0) {
+	if (bootcfg_info.nummenu > 0) {
 		/* Does not return */
 		doboottypemenu();
 	} else {

Index: src/sys/arch/i386/stand/lib/bootmenu.c
diff -u src/sys/arch/i386/stand/lib/bootmenu.c:1.12 src/sys/arch/i386/stand/lib/bootmenu.c:1.13
--- src/sys/arch/i386/stand/lib/bootmenu.c:1.12	Sun Apr  6 19:11:26 2014
+++ src/sys/arch/i386/stand/lib/bootmenu.c	Sat Jun 28 09:16:18 2014
@@ -1,4 +1,4 @@
-/*	$NetBSD: bootmenu.c,v 1.12 2014/04/06 19:11:26 jakllsch Exp $	*/
+/*	$NetBSD: bootmenu.c,v 1.13 2014/06/28 09:16:18 rtr Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -33,6 +33,7 @@
 #include <sys/bootblock.h>
 
 #include <lib/libsa/stand.h>
+#include <lib/libsa/bootcfg.h>
 #include <lib/libsa/ufs.h>
 #include <lib/libkern/libkern.h>
 
@@ -50,8 +51,6 @@ extern	const char bootprog_name[], bootp
 #define MENUFORMAT_NUMBER 1
 #define MENUFORMAT_LETTER 2
 
-struct bootconf_def bootconf;
-
 int
 atoi(const char *in)
 {
@@ -69,203 +68,23 @@ atoi(const char *in)
 }
 
 /*
- * This function parses a boot.cfg file in the root of the filesystem
- * (if present) and populates the global boot configuration.
- *
- * The file consists of a number of lines each terminated by \n
- * The lines are in the format keyword=value. There should not be spaces
- * around the = sign.
- *
- * The recognised keywords are:
- * banner: text displayed instead of the normal welcome text
- * menu: Descriptive text:command to use
- * timeout: Timeout in seconds (overrides that set by installboot)
- * default: the default menu option to use if Return is pressed
- * consdev: the console device to use
- * format: how menu choices are displayed: (a)utomatic, (n)umbers or (l)etters
- * clear: whether to clear the screen or not
- *
- * Example boot.cfg file:
- * banner=Welcome to NetBSD
- * banner=Please choose the boot type from the following menu
- * menu=Boot NetBSD:boot netbsd
- * menu=Boot into single user mode:boot netbsd -s
- * menu=:boot hd1a:netbsd -cs
- * menu=Goto boot comand line:prompt
- * timeout=10
- * consdev=com0
- * default=1
-*/
+ * XXX
+ * if module_add, userconf_add are strictly mi they can be folded back
+ * into sys/lib/libsa/bootcfg.c:perform_bootcfg().
+ */
+static void
+do_bootcfg_command(const char *cmd, char *arg)
+{
+	if (strcmp(cmd, BOOTCFG_CMD_LOAD) == 0)
+		module_add(arg);
+	else if (strcmp(cmd, BOOTCFG_CMD_USERCONF) == 0)
+		userconf_add(arg);
+}
+
 void
 parsebootconf(const char *conf)
 {
-	char *bc, *c;
-	int cmenu, cbanner, len;
-	int fd, err, off;
-	struct stat st;
-	char *next, *key, *value, *v2;
-
-	/* Clear bootconf structure */
-	memset((void *)&bootconf, 0, sizeof(bootconf));
-
-	/* Set timeout to configured */
-	bootconf.timeout = boot_params.bp_timeout;
-
-	/* automatically switch between letter and numbers on menu */
-	bootconf.menuformat = MENUFORMAT_AUTO;
-
-	fd = open(BOOTCONF, 0);
-	if (fd < 0)
-		return;
-
-	err = fstat(fd, &st);
-	if (err == -1) {
-		close(fd);
-		return;
-	}
-
-	/*
-	 * Check the size. A bootconf file is normally only a few
-	 * hundred bytes long. If it is much bigger than expected,
-	 * don't try to load it. We can't load something big into
-	 * an 8086 real mode segment anyway, and in pxeboot this is
-	 * probably a case of the loader getting a filename for the
-	 * kernel and thinking it is boot.cfg by accident. (The 32k
-	 * number is arbitrary but 8086 real mode data segments max
-	 * out at 64k.)
-	 */
-	if (st.st_size > 32768) {
-		close(fd);
-		return;
-	}
-
-	bc = alloc(st.st_size + 1);
-	if (bc == NULL) {
-		printf("Could not allocate memory for boot configuration\n");
-		return;
-	}
-
-	off = 0;
-	do {
-		len = read(fd, bc + off, 1024);
-		if (len <= 0)
-			break;
-		off += len;
-	} while (len > 0);
-	bc[off] = '\0';
-
-	close(fd);
-	/* bc now contains the whole boot.cfg file */
-
-	cmenu = 0;
-	cbanner = 0;
-	for (c = bc; *c; c = next) {
-		key = c;
-		/* find end of line */
-		for (; *c && *c != '\n'; c++)
-			/* zero terminate line on start of comment */
-			if (*c == '#')
-				*c = 0;
-		/* zero terminate line */
-		if (*(next = c))
-			*next++ = 0;
-		/* Look for = separator between key and value */
-		for (c = key; *c && *c != '='; c++)
-			continue;
-		/* Ignore lines with no key=value pair */
-		if (*c == '\0')
-			continue;
-
-		/* zero terminate key which points to keyword */
-		*c++ = 0;
-		value = c;
-		/* Look for end of line (or file) and zero terminate value */
-		for (; *c && *c != '\n'; c++)
-			continue;
-		*c = 0;
-
-		if (!strncmp(key, "menu", 4)) {
-			/*
-			 * Parse "menu=<description>:<command>".  If the
-			 * description is empty ("menu=:<command>)",
-			 * then re-use the command as the description.
-			 * Note that the command may contain embedded
-			 * colons.
-			 */
-			if (cmenu >= MAXMENU)
-				continue;
-			bootconf.desc[cmenu] = value;
-			for (v2 = value; *v2 && *v2 != ':'; v2++)
-				continue;
-			if (*v2) {
-				*v2++ = 0;
-				bootconf.command[cmenu] = v2;
-				if (! *value)
-					bootconf.desc[cmenu] = v2;
-				cmenu++;
-			} else {
-				/* No delimiter means invalid line */
-				bootconf.desc[cmenu] = NULL;
-			}
-		} else if (!strncmp(key, "banner", 6)) {
-			if (cbanner < MAXBANNER)
-				bootconf.banner[cbanner++] = value;
-		} else if (!strncmp(key, "timeout", 7)) {
-			if (!isnum(*value))
-				bootconf.timeout = -1;
-			else
-				bootconf.timeout = atoi(value);
-		} else if (!strncmp(key, "default", 7)) {
-			bootconf.def = atoi(value) - 1;
-		} else if (!strncmp(key, "consdev", 7)) {
-			bootconf.consdev = value;
-		} else if (!strncmp(key, "load", 4)) {
-			module_add(value);
-		} else if (!strncmp(key, "format", 6)) {
-			printf("value:%c\n", *value);
-			switch (*value) {
-			case 'a':
-			case 'A':
-				bootconf.menuformat = MENUFORMAT_AUTO;
-				break;
-
-			case 'n':
-			case 'N':
-			case 'd':
-			case 'D':
-				bootconf.menuformat = MENUFORMAT_NUMBER;
-				break;
-
-			case 'l':
-			case 'L':
-				bootconf.menuformat = MENUFORMAT_LETTER;
-				break;
-			}
-		} else if (!strncmp(key, "clear", 5)) {
-			bootconf.clear = !!atoi(value);
-		} else if (!strncmp(key, "userconf", 8)) {
-			userconf_add(value);
-		}
-	}
-	switch (bootconf.menuformat) {
-	case MENUFORMAT_AUTO:
-		if (cmenu > 9 && bootconf.timeout > 0)
-			bootconf.menuformat = MENUFORMAT_LETTER;
-		else
-			bootconf.menuformat = MENUFORMAT_NUMBER;
-		break;
-
-	case MENUFORMAT_NUMBER:
-		if (cmenu > 9 && bootconf.timeout > 0)
-			cmenu = 9;
-		break;
-	}
-
-	bootconf.nummenu = cmenu;
-	if (bootconf.def < 0)
-		bootconf.def = 0;
-	if (bootconf.def >= cmenu)
-		bootconf.def = cmenu - 1;
+	perform_bootcfg(conf, &do_bootcfg_command, 32768);
 }
 
 /*
@@ -282,17 +101,17 @@ getchoicefrominput(char *input, int def)
 	if (*input == '\0' || *input == '\r' || *input == '\n') {
 		choice = def;
 		usedef = 1;
-	} else if (*input >= 'A' && *input < bootconf.nummenu + 'A')
+	} else if (*input >= 'A' && *input < bootcfg_info.nummenu + 'A')
 		choice = (*input) - 'A';
-	else if (*input >= 'a' && *input < bootconf.nummenu + 'a')
+	else if (*input >= 'a' && *input < bootcfg_info.nummenu + 'a')
 		choice = (*input) - 'a';
 	else if (isnum(*input)) {
 		choice = atoi(input) - 1;
-		if (choice < 0 || choice >= bootconf.nummenu)
+		if (choice < 0 || choice >= bootcfg_info.nummenu)
 			choice = -1;
 	}
 
-	if (bootconf.menuformat != MENUFORMAT_LETTER &&
+	if (bootcfg_info.menuformat != MENUFORMAT_LETTER &&
 	    !isnum(*input) && !usedef)
 		choice = -1;
 
@@ -304,7 +123,7 @@ docommandchoice(int choice)
 {
 	char input[80], *ic, *oc;
 
-	ic = bootconf.command[choice];
+	ic = bootcfg_info.command[choice];
 	/* Split command string at ; into separate commands */
 	do {
 		oc = input;
@@ -333,14 +152,14 @@ bootdefault(void)
 	int choice;
 	static int entered;
 
-	if (bootconf.nummenu > 0) {
+	if (bootcfg_info.nummenu > 0) {
 		if (entered) {
 			printf("default boot twice, skipping...\n");
 			return;
 		}
 		entered = 1;
-		choice = bootconf.def;
-		printf("command(s): %s\n", bootconf.command[choice]);
+		choice = bootcfg_info.def;
+		printf("command(s): %s\n", bootcfg_info.command[choice]);
 		docommandchoice(choice);
 	}
 }
@@ -353,53 +172,53 @@ doboottypemenu(void)
 
 	printf("\n");
 	/* Display menu */
-	if (bootconf.menuformat == MENUFORMAT_LETTER) {
-		for (choice = 0; choice < bootconf.nummenu; choice++)
+	if (bootcfg_info.menuformat == MENUFORMAT_LETTER) {
+		for (choice = 0; choice < bootcfg_info.nummenu; choice++)
 			printf("    %c. %s\n", choice + 'A',
-			    bootconf.desc[choice]);
+			    bootcfg_info.desc[choice]);
 	} else {
 		/* Can't use %2d format string with libsa */
-		for (choice = 0; choice < bootconf.nummenu; choice++)
+		for (choice = 0; choice < bootcfg_info.nummenu; choice++)
 			printf("    %s%d. %s\n",
 			    (choice < 9) ?  " " : "",
 			    choice + 1,
-			    bootconf.desc[choice]);
+			    bootcfg_info.desc[choice]);
 	}
 	choice = -1;
 	for (;;) {
 		input[0] = '\0';
 
-		if (bootconf.timeout < 0) {
-			if (bootconf.menuformat == MENUFORMAT_LETTER)
+		if (bootcfg_info.timeout < 0) {
+			if (bootcfg_info.menuformat == MENUFORMAT_LETTER)
 				printf("\nOption: [%c]:",
-				    bootconf.def + 'A');
+				    bootcfg_info.def + 'A');
 			else
 				printf("\nOption: [%d]:",
-				    bootconf.def + 1);
+				    bootcfg_info.def + 1);
 
 			gets(input);
-			choice = getchoicefrominput(input, bootconf.def);
-		} else if (bootconf.timeout == 0)
-			choice = bootconf.def;
+			choice = getchoicefrominput(input, bootcfg_info.def);
+		} else if (bootcfg_info.timeout == 0)
+			choice = bootcfg_info.def;
 		else  {
 			printf("\nChoose an option; RETURN for default; "
 			       "SPACE to stop countdown.\n");
-			if (bootconf.menuformat == MENUFORMAT_LETTER)
+			if (bootcfg_info.menuformat == MENUFORMAT_LETTER)
 				printf("Option %c will be chosen in ",
-				    bootconf.def + 'A');
+				    bootcfg_info.def + 'A');
 			else
 				printf("Option %d will be chosen in ",
-				    bootconf.def + 1);
-			input[0] = awaitkey(bootconf.timeout, 1);
+				    bootcfg_info.def + 1);
+			input[0] = awaitkey(bootcfg_info.timeout, 1);
 			input[1] = '\0';
-			choice = getchoicefrominput(input, bootconf.def);
+			choice = getchoicefrominput(input, bootcfg_info.def);
 			/* If invalid key pressed, drop to menu */
 			if (choice == -1)
-				bootconf.timeout = -1;
+				bootcfg_info.timeout = -1;
 		}
 		if (choice < 0)
 			continue;
-		if (!strcmp(bootconf.command[choice], "prompt") &&
+		if (!strcmp(bootcfg_info.command[choice], "prompt") &&
 		    ((boot_params.bp_flags & X86_BP_FLAGS_PASSWORD) == 0 ||
 		    check_password((char *)boot_params.bp_password))) {
 			printf("type \"?\" or \"help\" for help.\n");

Index: src/sys/arch/i386/stand/lib/bootmenu.h
diff -u src/sys/arch/i386/stand/lib/bootmenu.h:1.3 src/sys/arch/i386/stand/lib/bootmenu.h:1.4
--- src/sys/arch/i386/stand/lib/bootmenu.h:1.3	Sun Jul 28 08:50:09 2013
+++ src/sys/arch/i386/stand/lib/bootmenu.h	Sat Jun 28 09:16:18 2014
@@ -1,4 +1,4 @@
-/*	$NetBSD: bootmenu.h,v 1.3 2013/07/28 08:50:09 he Exp $	*/
+/*	$NetBSD: bootmenu.h,v 1.4 2014/06/28 09:16:18 rtr Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -29,9 +29,6 @@
 #ifndef _BOOTMENU_H
 #define _BOOTMENU_H
 
-#define BOOTCONF "boot.cfg"
-#define MAXMENU 20
-#define MAXBANNER 12
 #define COMMAND_SEPARATOR ';'
 
 void parsebootconf(const char *);
@@ -39,16 +36,4 @@ void doboottypemenu(void);
 void bootdefault(void);
 int atoi(const char *);
 
-struct bootconf_def {
-	char *banner[MAXBANNER];	/* Banner text */
-	char *command[MAXMENU];		/* Menu commands per entry*/
-	char *consdev;			/* Console device */
-	int def;			/* Default menu option */
-	char *desc[MAXMENU];		/* Menu text per entry */
-	int nummenu;			/* Number of menu items */
-	int timeout;		 	/* Timeout in seconds */
-	int menuformat;			/* Print letters instead of numbers? */
-	int clear;			/* Clear the screen? */
-} extern bootconf;
-
 #endif /* !_BOOTMENU_H */

Index: src/sys/arch/i386/stand/pxeboot/main.c
diff -u src/sys/arch/i386/stand/pxeboot/main.c:1.30 src/sys/arch/i386/stand/pxeboot/main.c:1.31
--- src/sys/arch/i386/stand/pxeboot/main.c:1.30	Wed Mar  6 11:34:37 2013
+++ src/sys/arch/i386/stand/pxeboot/main.c	Sat Jun 28 09:16:18 2014
@@ -1,4 +1,4 @@
-/*	$NetBSD: main.c,v 1.30 2013/03/06 11:34:37 yamt Exp $	*/
+/*	$NetBSD: main.c,v 1.31 2014/06/28 09:16:18 rtr Exp $	*/
 
 /*
  * Copyright (c) 1996
@@ -43,6 +43,7 @@
 #include <lib/libkern/libkern.h>
 
 #include <lib/libsa/stand.h>
+#include <lib/libsa/bootcfg.h>
 
 #include <libi386.h>
 #include <bootmenu.h>
@@ -84,7 +85,7 @@ static void
 clearit(void)
 {
 
-	if (bootconf.clear)
+	if (bootcfg_info.clear)
 		clear_pc_screen();
 }
 
@@ -138,23 +139,23 @@ main(void)
 
 #ifndef SMALL
 	if (!(boot_params.bp_flags & X86_BP_FLAGS_NOBOOTCONF)) {
-		parsebootconf(BOOTCONF);
+		parsebootconf(BOOTCFG_FILENAME);
 	} else {
-		bootconf.timeout = boot_params.bp_timeout;
+		bootcfg_info.timeout = boot_params.bp_timeout;
 	}
 
 	/*
 	 * If console set in boot.cfg, switch to it.
 	 * This will print the banner, so we don't need to explicitly do it
 	 */
-	if (bootconf.consdev)
-		command_consdev(bootconf.consdev);
+	if (bootcfg_info.consdev)
+		command_consdev(bootcfg_info.consdev);
 	else 
 		print_banner();
 
 	/* Display the menu, if applicable */
 	twiddle_toggle = 0;
-	if (bootconf.nummenu > 0) {
+	if (bootcfg_info.nummenu > 0) {
 		/* Does not return */
 		doboottypemenu();
 	}
@@ -169,7 +170,7 @@ main(void)
 #ifdef SMALL
 	c = awaitkey(boot_params.bp_timeout, 1);
 #else
-	c = awaitkey((bootconf.timeout < 0) ? 0 : bootconf.timeout, 1);
+	c = awaitkey((bootcfg_info.timeout < 0) ? 0 : bootcfg_info.timeout, 1);
 #endif
 	if ((c != '\r') && (c != '\n') && (c != '\0') &&
 	    ((boot_params.bp_flags & X86_BP_FLAGS_PASSWORD) == 0

Index: src/sys/arch/sparc/stand/ofwboot/boot.c
diff -u src/sys/arch/sparc/stand/ofwboot/boot.c:1.30 src/sys/arch/sparc/stand/ofwboot/boot.c:1.31
--- src/sys/arch/sparc/stand/ofwboot/boot.c:1.30	Mon Dec 30 08:47:50 2013
+++ src/sys/arch/sparc/stand/ofwboot/boot.c	Sat Jun 28 09:16:18 2014
@@ -1,4 +1,4 @@
-/*	$NetBSD: boot.c,v 1.30 2013/12/30 08:47:50 martin Exp $	*/
+/*	$NetBSD: boot.c,v 1.31 2014/06/28 09:16:18 rtr Exp $	*/
 
 /*
  * Copyright (c) 1997, 1999 Eduardo E. Horvath.  All rights reserved.
@@ -44,6 +44,7 @@
  */
 
 #include <lib/libsa/stand.h>
+#include <lib/libsa/bootcfg.h>
 #include <lib/libsa/loadfile.h>
 #include <lib/libkern/libkern.h>
 
@@ -410,7 +411,7 @@ help(void)
 }
 
 static void
-do_config_command(const char *cmd, const char *arg)
+do_config_command(const char *cmd, char *arg)
 {
 	DPRINTF(("do_config_command: %s\n", cmd));
 	if (strcmp(cmd, "bootpartition") == 0) {
@@ -429,76 +430,12 @@ do_config_command(const char *cmd, const
 }
 
 static void
-parse_boot_config(char *cfg, size_t len)
-{
-	const char *cmd = NULL, *arg = NULL;
-
-	while (len) {
-		if (isspace(*cfg)) {
-			cfg++; len--; continue;
-		}
-		if (*cfg == ';' || *cfg == '#') {
-			while (len && *cfg != '\r' && *cfg != '\n') {
-				cfg++; len--;
-			}
-			continue;
-		}
-		cmd = cfg;
-		while (len && !isspace(*cfg)) {
-			cfg++; len--;
-		}
-		*cfg = 0;
-		if (len > 0) {
-			cfg++; len--;
-			while (isspace(*cfg) && len) {
-				cfg++; len--;
-			}
-			if (len > 0 ) {
-				arg = cfg;
-				while (len && !isspace(*cfg)) {
-					cfg++; len--;
-				}
-				*cfg = 0;
-			}
-		}
-		do_config_command(cmd, arg);
-		if (len > 0) {
-			cfg++; len--;
-		}
-	}
-}
-
-static void
 check_boot_config(void)
 {
-	int fd, off, len;
-	struct stat st;
-	char *bc;
-
-	if (!root_fs_quickseekable) return;
-	DPRINTF(("checking for /boot.cfg...\n"));
-	fd = open("/boot.cfg", 0);
-	if (fd < 0) {
-		DPRINTF(("no /boot.cfg found\n"));
+	if (!root_fs_quickseekable)
 		return;
-	}
-	DPRINTF(("found /boot.cfg\n"));
-	if (fstat(fd, &st) == -1 || st.st_size > 32*1024) {
-		close(fd);
-		return;
-	}
-	bc = alloc(st.st_size+1);
-	off = 0;
-	do {
-		len = read(fd, bc+off, 1024);
-		if (len <= 0)
-			break;
-		off += len;
-	} while (len > 0);
-	bc[off] = 0;
-	close(fd);
 
-	parse_boot_config(bc, off);
+	perform_bootcfg(BOOTCFG_FILENAME, &do_config_command, 32768);
 }
 
 void

Index: src/sys/arch/zaurus/stand/zboot/boot.c
diff -u src/sys/arch/zaurus/stand/zboot/boot.c:1.5 src/sys/arch/zaurus/stand/zboot/boot.c:1.6
--- src/sys/arch/zaurus/stand/zboot/boot.c:1.5	Wed Jan 18 23:12:21 2012
+++ src/sys/arch/zaurus/stand/zboot/boot.c	Sat Jun 28 09:16:18 2014
@@ -1,4 +1,4 @@
-/*	$NetBSD: boot.c,v 1.5 2012/01/18 23:12:21 nonaka Exp $	*/
+/*	$NetBSD: boot.c,v 1.6 2014/06/28 09:16:18 rtr Exp $	*/
 
 /*
  * Copyright (c) 2009 NONAKA Kimihiro <non...@netbsd.org>
@@ -199,7 +199,7 @@ boot(dev_t bootdev)
 
 	snprintf(bootconfpath, sizeof(bootconfpath), "%s%d%c:%s",
 	    default_devname, default_unit, 'a' + default_partition,
-	    _PATH_BOOTCONF);
+	    BOOTCFG_FILENAME);
 	parsebootconf(bootconfpath);
 
 #ifdef SUPPORT_CONSDEV
@@ -207,8 +207,8 @@ boot(dev_t bootdev)
 	 * If console set in boot.cfg, switch to it.
 	 * This will print the banner, so we don't need to explicitly do it
 	 */
-	if (bootconf.consdev)
-		bootcmd_consdev(bootconf.consdev);
+	if (bootcfg_info.consdev)
+		bootcmd_consdev(bootcfg_info.consdev);
 	else 
 #endif
 		print_banner();
@@ -217,7 +217,7 @@ boot(dev_t bootdev)
 
 	/* Display the menu, if applicable */
 	twiddle_toggle = 0;
-	if (bootconf.nummenu > 0) {
+	if (bootcfg_info.nummenu > 0) {
 		/* Does not return */
 		doboottypemenu();
 	}
@@ -228,7 +228,8 @@ boot(dev_t bootdev)
 		printf("booting %s - starting in ", 
 		    sprint_bootsel(names[currname][0]));
 
-		c = awaitkey((bootconf.timeout < 0) ? 0 : bootconf.timeout, 1);
+		c = awaitkey((bootcfg_info.timeout < 0) ? 0
+		    : bootcfg_info.timeout, 1);
 		if ((c != '\r') && (c != '\n') && (c != '\0')) {
 			printf("type \"?\" or \"help\" for help.\n");
 			bootmenu(); /* does not return */

Index: src/sys/arch/zaurus/stand/zboot/boot.h
diff -u src/sys/arch/zaurus/stand/zboot/boot.h:1.2 src/sys/arch/zaurus/stand/zboot/boot.h:1.3
--- src/sys/arch/zaurus/stand/zboot/boot.h:1.2	Wed Jan 18 23:12:21 2012
+++ src/sys/arch/zaurus/stand/zboot/boot.h	Sat Jun 28 09:16:18 2014
@@ -1,4 +1,4 @@
-/*	$NetBSD: boot.h,v 1.2 2012/01/18 23:12:21 nonaka Exp $	*/
+/*	$NetBSD: boot.h,v 1.3 2014/06/28 09:16:18 rtr Exp $	*/
 
 /*
  * Copyright (c) 2009 NONAKA Kimihiro
@@ -31,6 +31,7 @@
 #include <sys/param.h>
 
 #include <lib/libsa/stand.h>
+#include <lib/libsa/bootcfg.h>
 #include <lib/libkern/libkern.h>
 
 extern int debug;
Index: src/sys/arch/zaurus/stand/zboot/bootmenu.c
diff -u src/sys/arch/zaurus/stand/zboot/bootmenu.c:1.2 src/sys/arch/zaurus/stand/zboot/bootmenu.c:1.3
--- src/sys/arch/zaurus/stand/zboot/bootmenu.c:1.2	Wed Jan 18 23:12:21 2012
+++ src/sys/arch/zaurus/stand/zboot/bootmenu.c	Sat Jun 28 09:16:18 2014
@@ -1,4 +1,4 @@
-/*	$NetBSD: bootmenu.c,v 1.2 2012/01/18 23:12:21 nonaka Exp $	*/
+/*	$NetBSD: bootmenu.c,v 1.3 2014/06/28 09:16:18 rtr Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -41,8 +41,6 @@
 #define MENUFORMAT_NUMBER 1
 #define MENUFORMAT_LETTER 2
 
-struct bootconf_def bootconf;
-
 int
 atoi(const char *in)
 {
@@ -59,177 +57,10 @@ atoi(const char *in)
 	return (*in == '-') ? -ret : ret;
 }
 
-/*
- * This function parses a boot.cfg file in the root of the filesystem
- * (if present) and populates the global boot configuration.
- *
- * The file consists of a number of lines each terminated by \n
- * The lines are in the format keyword=value. There should not be spaces
- * around the = sign.
- *
- * The recognised keywords are:
- * banner: text displayed instead of the normal welcome text
- * menu: Descriptive text:command to use
- * timeout: Timeout in seconds (overrides that set by installboot)
- * default: the default menu option to use if Return is pressed
- * consdev: the console device to use
- * format: how menu choices are displayed: (a)utomatic, (n)umbers or (l)etters
- * clear: whether to clear the screen or not
- *
- * Example boot.cfg file:
- * banner=Welcome to NetBSD
- * banner=Please choose the boot type from the following menu
- * menu=Boot NetBSD:boot netbsd
- * menu=Boot into single user mode:boot netbsd -s
- * menu=:boot hd1a:netbsd -cs
- * menu=Goto boot comand line:prompt
- * timeout=10
- * consdev=com0
- * default=1
-*/
 void
 parsebootconf(const char *conf)
 {
-	char *bc, *c;
-	int cmenu, cbanner, len;
-	int fd, err, off;
-	struct stat st;
-	char *key, *value, *v2;
-
-	/* Clear bootconf structure */
-	memset(&bootconf, 0, sizeof(bootconf));
-
-	/* Set timeout to configured */
-	bootconf.timeout = default_timeout;
-
-	/* automatically switch between letter and numbers on menu */
-	bootconf.menuformat = MENUFORMAT_AUTO;
-
-	fd = open(conf, 0);
-	if (fd < 0)
-		return;
-
-	err = fstat(fd, &st);
-	if (err == -1) {
-		close(fd);
-		return;
-	}
-
-	bc = alloc(st.st_size + 1);
-	if (bc == NULL) {
-		printf("Could not allocate memory for boot configuration\n");
-		close(fd);
-		return;
-	}
-
-	off = 0;
-	do {
-		len = read(fd, bc + off, 1024);
-		if (len <= 0)
-			break;
-		off += len;
-	} while (len > 0);
-	bc[off] = '\0';
-
-	close(fd);
-	/* bc now contains the whole boot.cfg file */
-
-	cmenu = 0;
-	cbanner = 0;
-	for (c = bc; *c; c++) {
-		key = c;
-		/* Look for = separator between key and value */
-		for (; *c && *c != '='; c++)
-			continue;
-		if (*c == '\0')
-			break; /* break if at end of data */
-
-		/* zero terminate key which points to keyword */
-		*c++ = 0;
-		value = c;
-		/* Look for end of line (or file) and zero terminate value */
-		for (; *c && *c != '\n'; c++)
-			continue;
-		*c = 0;
-
-		if (!strncmp(key, "menu", 4)) {
-			/*
-			 * Parse "menu=<description>:<command>".  If the
-			 * description is empty ("menu=:<command>)",
-			 * then re-use the command as the description.
-			 * Note that the command may contain embedded
-			 * colons.
-			 */
-			if (cmenu >= MAXMENU)
-				continue;
-			bootconf.desc[cmenu] = value;
-			for (v2 = value; *v2 && *v2 != ':'; v2++)
-				continue;
-			if (*v2) {
-				*v2++ = 0;
-				bootconf.command[cmenu] = v2;
-				if (! *value)
-					bootconf.desc[cmenu] = v2;
-				cmenu++;
-			} else {
-				/* No delimiter means invalid line */
-				bootconf.desc[cmenu] = NULL;
-			}
-		} else if (!strncmp(key, "banner", 6)) {
-			if (cbanner < MAXBANNER)
-				bootconf.banner[cbanner++] = value;
-		} else if (!strncmp(key, "timeout", 7)) {
-			if (!isnum(*value))
-				bootconf.timeout = -1;
-			else
-				bootconf.timeout = atoi(value);
-		} else if (!strncmp(key, "default", 7)) {
-			bootconf.def = atoi(value) - 1;
-		} else if (!strncmp(key, "consdev", 7)) {
-			bootconf.consdev = value;
-		} else if (!strncmp(key, "format", 6)) {
-			printf("value:%c\n", *value);
-			switch (*value) {
-			case 'a':
-			case 'A':
-				bootconf.menuformat = MENUFORMAT_AUTO;
-				break;
-
-			case 'n':
-			case 'N':
-			case 'd':
-			case 'D':
-				bootconf.menuformat = MENUFORMAT_NUMBER;
-				break;
-
-			case 'l':
-			case 'L':
-				bootconf.menuformat = MENUFORMAT_LETTER;
-				break;
-			}
-		} else if (!strncmp(key, "clear", 5)) {
-			bootconf.clear = !!atoi(value);
-		}
-	}
-	switch (bootconf.menuformat) {
-	case MENUFORMAT_AUTO:
-		if (cmenu > 9 && bootconf.timeout > 0)
-			bootconf.menuformat = MENUFORMAT_LETTER;
-		else
-			bootconf.menuformat = MENUFORMAT_NUMBER;
-		break;
-
-	case MENUFORMAT_NUMBER:
-		if (cmenu > 9 && bootconf.timeout > 0)
-			cmenu = 9;
-		break;
-	}
-
-	bootconf.nummenu = cmenu;
-	if (bootconf.def < 0)
-		bootconf.def = 0;
-	if (bootconf.def >= cmenu)
-		bootconf.def = cmenu - 1;
+	perform_bootcfg(conf, &bootcfg_do_noop, 0);
 }
 
 /*
@@ -242,13 +73,13 @@ getchoicefrominput(char *input, int def)
 
 	if (*input == '\0' || *input == '\r' || *input == '\n')
 		choice = def;
-	else if (*input >= 'A' && *input < bootconf.nummenu + 'A')
+	else if (*input >= 'A' && *input < bootcfg_info.nummenu + 'A')
 		choice = (*input) - 'A';
-	else if (*input >= 'a' && *input < bootconf.nummenu + 'a')
+	else if (*input >= 'a' && *input < bootcfg_info.nummenu + 'a')
 		choice = (*input) - 'a';
 	else if (isnum(*input)) {
 		choice = atoi(input) - 1;
-		if (choice < 0 || choice >= bootconf.nummenu)
+		if (choice < 0 || choice >= bootcfg_info.nummenu)
 			choice = -1;
 	}
 	return choice;
@@ -262,57 +93,57 @@ doboottypemenu(void)
 
 	printf("\n");
 	/* Display menu */
-	if (bootconf.menuformat == MENUFORMAT_LETTER) {
-		for (choice = 0; choice < bootconf.nummenu; choice++)
+	if (bootcfg_info.menuformat == MENUFORMAT_LETTER) {
+		for (choice = 0; choice < bootcfg_info.nummenu; choice++)
 			printf("    %c. %s\n", choice + 'A',
-			    bootconf.desc[choice]);
+			    bootcfg_info.desc[choice]);
 	} else {
 		/* Can't use %2d format string with libsa */
-		for (choice = 0; choice < bootconf.nummenu; choice++)
+		for (choice = 0; choice < bootcfg_info.nummenu; choice++)
 			printf("    %s%d. %s\n",
 			    (choice < 9) ?  " " : "",
 			    choice + 1,
-			    bootconf.desc[choice]);
+			    bootcfg_info.desc[choice]);
 	}
 	choice = -1;
 	for (;;) {
 		input[0] = '\0';
 
-		if (bootconf.timeout < 0) {
-			if (bootconf.menuformat == MENUFORMAT_LETTER)
+		if (bootcfg_info.timeout < 0) {
+			if (bootcfg_info.menuformat == MENUFORMAT_LETTER)
 				printf("\nOption: [%c]:",
-				    bootconf.def + 'A');
+				    bootcfg_info.def + 'A');
 			else
 				printf("\nOption: [%d]:",
-				    bootconf.def + 1);
+				    bootcfg_info.def + 1);
 
 			gets(input);
-			choice = getchoicefrominput(input, bootconf.def);
-		} else if (bootconf.timeout == 0)
-			choice = bootconf.def;
+			choice = getchoicefrominput(input, bootcfg_info.def);
+		} else if (bootcfg_info.timeout == 0)
+			choice = bootcfg_info.def;
 		else  {
 			printf("\nChoose an option; RETURN for default; "
 			       "SPACE to stop countdown.\n");
-			if (bootconf.menuformat == MENUFORMAT_LETTER)
+			if (bootcfg_info.menuformat == MENUFORMAT_LETTER)
 				printf("Option %c will be chosen in ",
-				    bootconf.def + 'A');
+				    bootcfg_info.def + 'A');
 			else
 				printf("Option %d will be chosen in ",
-				    bootconf.def + 1);
-			input[0] = awaitkey(bootconf.timeout, 1);
+				    bootcfg_info.def + 1);
+			input[0] = awaitkey(bootcfg_info.timeout, 1);
 			input[1] = '\0';
-			choice = getchoicefrominput(input, bootconf.def);
+			choice = getchoicefrominput(input, bootcfg_info.def);
 			/* If invalid key pressed, drop to menu */
 			if (choice == -1)
-				bootconf.timeout = -1;
+				bootcfg_info.timeout = -1;
 		}
 		if (choice < 0)
 			continue;
-		if (!strcmp(bootconf.command[choice], "prompt")) {
+		if (!strcmp(bootcfg_info.command[choice], "prompt")) {
 			printf("type \"?\" or \"help\" for help.\n");
 			bootmenu(); /* does not return */
 		} else {
-			ic = bootconf.command[choice];
+			ic = bootcfg_info.command[choice];
 			/* Split command string at ; into separate commands */
 			do {
 				oc = input;

Index: src/sys/arch/zaurus/stand/zboot/bootmenu.h
diff -u src/sys/arch/zaurus/stand/zboot/bootmenu.h:1.1 src/sys/arch/zaurus/stand/zboot/bootmenu.h:1.2
--- src/sys/arch/zaurus/stand/zboot/bootmenu.h:1.1	Mon Mar  2 09:33:02 2009
+++ src/sys/arch/zaurus/stand/zboot/bootmenu.h	Sat Jun 28 09:16:18 2014
@@ -1,4 +1,4 @@
-/*	$NetBSD: bootmenu.h,v 1.1 2009/03/02 09:33:02 nonaka Exp $	*/
+/*	$NetBSD: bootmenu.h,v 1.2 2014/06/28 09:16:18 rtr Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -29,24 +29,10 @@
 #ifndef _BOOTMENU_H
 #define _BOOTMENU_H
 
-#define MAXMENU 20
-#define MAXBANNER 12
 #define COMMAND_SEPARATOR ';'
 
 void parsebootconf(const char *);
 void doboottypemenu(void);
 int atoi(const char *);
 
-struct bootconf_def {
-	char *banner[MAXBANNER];	/* Banner text */
-	char *command[MAXMENU];		/* Menu commands per entry*/
-	char *consdev;			/* Console device */
-	int def;			/* Default menu option */
-	char *desc[MAXMENU];		/* Menu text per entry */
-	int nummenu;			/* Number of menu items */
-	int timeout;		 	/* Timeout in seconds */
-	int menuformat;			/* Print letters instead of numbers? */
-	int clear;			/* Clear the screen? */
-} extern bootconf;
-
 #endif /* !_BOOTMENU_H */

Index: src/sys/arch/zaurus/stand/zboot/pathnames.h
diff -u src/sys/arch/zaurus/stand/zboot/pathnames.h:1.3 src/sys/arch/zaurus/stand/zboot/pathnames.h:1.4
--- src/sys/arch/zaurus/stand/zboot/pathnames.h:1.3	Wed Jan 18 23:12:21 2012
+++ src/sys/arch/zaurus/stand/zboot/pathnames.h	Sat Jun 28 09:16:18 2014
@@ -1,4 +1,4 @@
-/*	$NetBSD: pathnames.h,v 1.3 2012/01/18 23:12:21 nonaka Exp $	*/
+/*	$NetBSD: pathnames.h,v 1.4 2014/06/28 09:16:18 rtr Exp $	*/
 /*	$OpenBSD: pathnames.h,v 1.3 2005/01/14 08:10:16 uwe Exp $	*/
 
 /*
@@ -17,6 +17,5 @@
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
-#define _PATH_BOOTCONF		"boot.cfg"
 #define _PATH_ZBOOT		"/proc/zboot"
 #define _PATH_PARTITIONS	"/proc/partitions"

Index: src/sys/lib/libsa/Makefile
diff -u src/sys/lib/libsa/Makefile:1.82 src/sys/lib/libsa/Makefile:1.83
--- src/sys/lib/libsa/Makefile:1.82	Sat Jun 14 20:49:37 2014
+++ src/sys/lib/libsa/Makefile	Sat Jun 28 09:16:18 2014
@@ -1,4 +1,4 @@
-#	$NetBSD: Makefile,v 1.82 2014/06/14 20:49:37 mrg Exp $
+#	$NetBSD: Makefile,v 1.83 2014/06/28 09:16:18 rtr Exp $
 
 LIB=	sa
 LIBISPRIVATE?= yes
@@ -28,6 +28,8 @@ SRCS+=	alloc.c errno.c exit.c files.c \
 	panic.c printf.c qsort.c snprintf.c strerror.c \
 	subr_prf.c twiddle.c checkpasswd.c
 
+SRCS+=	bootcfg.c
+
 # string routines
 .if ${MACHINE_ARCH} != "i386" && ${MACHINE_ARCH} != "x86_64"
 SRCS+=	memcmp.c memcpy.c memmove.c memset.c strchr.c

Added files:

Index: src/sys/lib/libsa/bootcfg.c
diff -u /dev/null src/sys/lib/libsa/bootcfg.c:1.1
--- /dev/null	Sat Jun 28 09:16:19 2014
+++ src/sys/lib/libsa/bootcfg.c	Sat Jun 28 09:16:18 2014
@@ -0,0 +1,292 @@
+/*	$NetBSD: bootcfg.c,v 1.1 2014/06/28 09:16:18 rtr Exp $	*/
+
+/*-
+ * Copyright (c) 2008 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <sys/types.h>
+#include <sys/reboot.h>
+
+#include <lib/libsa/stand.h>
+#include <lib/libsa/bootcfg.h>
+#include <lib/libkern/libkern.h>
+
+static int atoi(const char *);
+
+#define isnum(c) ((c) >= '0' && (c) <= '9')
+
+#define MENUFORMAT_AUTO   0
+#define MENUFORMAT_NUMBER 1
+#define MENUFORMAT_LETTER 2
+
+#define DEFAULT_FORMAT  MENUFORMAT_AUTO
+#define DEFAULT_TIMEOUT 10
+
+struct bootcfg_def bootcfg_info;
+
+int
+atoi(const char *in)
+{
+	char *c;
+	int ret;
+
+	ret = 0;
+	c = (char *)in;
+	if (*c == '-')
+		c++;
+	for (; isnum(*c); c++)
+		ret = (ret * 10) + (*c - '0');
+
+	return (*in == '-') ? -ret : ret;
+}
+
+void
+bootcfg_do_noop(const char *cmd, char *arg)
+{
+	/* noop, do nothing */
+}
+
+/*
+ * This function parses a boot.cfg file in the root of the filesystem
+ * (if present) and populates the global boot configuration.
+ *
+ * The file consists of a number of lines each terminated by \n
+ * The lines are in the format keyword=value. There should not be spaces
+ * around the = sign.
+ *
+ * perform_bootcfg(conf, command, maxsz)
+ *
+ * conf		Path to boot.cfg to be passed verbatim to open()
+ *
+ * command	Pointer to a function that will be called when
+ * 		perform_bootcfg() encounters a key (command) it does not
+ *		recognize.
+ *		The command function is provided both the keyword and
+ *		value parsed as arguments to the function.
+ *
+ * maxsz	Limit the size of the boot.cfg perform_bootcfg() will parse.
+ * 		- If maxsz is < 0 boot.cfg will not be processed.
+ * 		- If maxsz is = 0 no limit will be imposed but parsing may
+ *		  fail due to platform or other constraints e.g. maximum
+ *		  segment size.
+ *		- If 0 < maxsz and boot.cfg exceeds maxsz it will not be
+ *		  parsed, otherwise it will be parsed.
+ *
+ * The recognised keywords are:
+ * banner: text displayed instead of the normal welcome text
+ * menu: Descriptive text:command to use
+ * timeout: Timeout in seconds (overrides that set by installboot)
+ * default: the default menu option to use if Return is pressed
+ * consdev: the console device to use
+ * format: how menu choices are displayed: (a)utomatic, (n)umbers or (l)etters
+ * clear: whether to clear the screen or not
+ *
+ * Example boot.cfg file:
+ * banner=Welcome to NetBSD
+ * banner=Please choose the boot type from the following menu
+ * menu=Boot NetBSD:boot netbsd
+ * menu=Boot into single user mode:boot netbsd -s
+ * menu=:boot hd1a:netbsd -cs
+ * menu=Goto boot comand line:prompt
+ * timeout=10
+ * consdev=com0
+ * default=1
+*/
+void
+perform_bootcfg(const char *conf, bootcfg_command command, const off_t maxsz)
+{
+	char *bc, *c;
+	int cmenu, cbanner, len;
+	int fd, err, off;
+	struct stat st;
+	char *next, *key, *value, *v2;
+
+	/* clear bootcfg structure */
+	memset(&bootcfg_info, 0, sizeof(bootcfg_info));
+
+	/* set default timeout */
+	bootcfg_info.timeout = DEFAULT_TIMEOUT;
+
+	/* automatically switch between letter and numbers on menu */
+	bootcfg_info.menuformat = DEFAULT_FORMAT;
+
+	fd = open(conf, 0);
+	if (fd < 0)
+		return;
+
+	err = fstat(fd, &st);
+	if (err == -1) {
+		close(fd);
+		return;
+	}
+
+	/* if a maximum size is being requested for the boot.cfg enforce it. */
+	if (0 < maxsz && st.st_size > maxsz) {
+		close(fd);
+		return;
+	}
+
+	bc = alloc(st.st_size + 1);
+	if (bc == NULL) {
+		printf("Could not allocate memory for boot configuration\n");
+		close(fd);
+		return;
+	}
+
+	/*
+	 * XXX original code, assumes error or eof return from read()
+	 *     results in the entire boot.cfg being buffered.
+	 *     - should bail out on read() failing.
+	 *     - assumption is made that the file size doesn't change between
+	 *       fstat() and read()ing.  probably safe in this context
+	 *       arguably should check that reading the file won't overflow
+	 *       the storage anyway.
+	 */
+	off = 0;
+	do {
+		len = read(fd, bc + off, 1024);
+		if (len <= 0)
+			break;
+		off += len;
+	} while (len > 0);
+	bc[off] = '\0';
+
+	close(fd);
+
+	/* bc is now assumed to contain the whole boot.cfg file (see above) */
+
+	cmenu = 0;
+	cbanner = 0;
+	for (c = bc; *c; c = next) {
+		key = c;
+		/* find end of line */
+		for (; *c && *c != '\n'; c++)
+			/* zero terminate line on start of comment */
+			if (*c == '#')
+				*c = 0;
+		/* zero terminate line */
+		if (*(next = c))
+			*next++ = 0;
+		/* Look for = separator between key and value */
+		for (c = key; *c && *c != '='; c++)
+			continue;
+		/* Ignore lines with no key=value pair */
+		if (*c == '\0')
+			continue;
+
+		/* zero terminate key which points to keyword */
+		*c++ = 0;
+		value = c;
+		/* Look for end of line (or file) and zero terminate value */
+		for (; *c && *c != '\n'; c++)
+			continue;
+		*c = 0;
+
+		if (!strncmp(key, "menu", 4)) {
+			/*
+			 * Parse "menu=<description>:<command>".  If the
+			 * description is empty ("menu=:<command>)",
+			 * then re-use the command as the description.
+			 * Note that the command may contain embedded
+			 * colons.
+			 */
+			if (cmenu >= BOOTCFG_MAXMENU)
+				continue;
+			bootcfg_info.desc[cmenu] = value;
+			for (v2 = value; *v2 && *v2 != ':'; v2++)
+				continue;
+			if (*v2) {
+				*v2++ = 0;
+				bootcfg_info.command[cmenu] = v2;
+				if (! *value)
+					bootcfg_info.desc[cmenu] = v2;
+				cmenu++;
+			} else {
+				/* No delimiter means invalid line */
+				bootcfg_info.desc[cmenu] = NULL;
+			}
+		} else if (!strncmp(key, "banner", 6)) {
+			if (cbanner < BOOTCFG_MAXBANNER)
+				bootcfg_info.banner[cbanner++] = value;
+		} else if (!strncmp(key, "timeout", 7)) {
+			if (!isnum(*value))
+				bootcfg_info.timeout = -1;
+			else
+				bootcfg_info.timeout = atoi(value);
+		} else if (!strncmp(key, "default", 7)) {
+			bootcfg_info.def = atoi(value) - 1;
+		} else if (!strncmp(key, "consdev", 7)) {
+			bootcfg_info.consdev = value;
+		} else if (!strncmp(key, BOOTCFG_CMD_LOAD, 4)) {
+			command(BOOTCFG_CMD_LOAD, value);
+		} else if (!strncmp(key, "format", 6)) {
+			printf("value:%c\n", *value);
+			switch (*value) {
+			case 'a':
+			case 'A':
+				bootcfg_info.menuformat = MENUFORMAT_AUTO;
+				break;
+
+			case 'n':
+			case 'N':
+			case 'd':
+			case 'D':
+				bootcfg_info.menuformat = MENUFORMAT_NUMBER;
+				break;
+
+			case 'l':
+			case 'L':
+				bootcfg_info.menuformat = MENUFORMAT_LETTER;
+				break;
+			}
+		} else if (!strncmp(key, "clear", 5)) {
+			bootcfg_info.clear = !!atoi(value);
+		} else if (!strncmp(key, BOOTCFG_CMD_USERCONF, 8)) {
+			command(BOOTCFG_CMD_USERCONF, value);
+		} else {
+			command(key, value);
+		}
+	}
+
+	switch (bootcfg_info.menuformat) {
+	case MENUFORMAT_AUTO:
+		if (cmenu > 9 && bootcfg_info.timeout > 0)
+			bootcfg_info.menuformat = MENUFORMAT_LETTER;
+		else
+			bootcfg_info.menuformat = MENUFORMAT_NUMBER;
+		break;
+
+	case MENUFORMAT_NUMBER:
+		if (cmenu > 9 && bootcfg_info.timeout > 0)
+			cmenu = 9;
+		break;
+	}
+
+	bootcfg_info.nummenu = cmenu;
+	if (bootcfg_info.def < 0)
+		bootcfg_info.def = 0;
+	if (bootcfg_info.def >= cmenu)
+		bootcfg_info.def = cmenu - 1;
+}
Index: src/sys/lib/libsa/bootcfg.h
diff -u /dev/null src/sys/lib/libsa/bootcfg.h:1.1
--- /dev/null	Sat Jun 28 09:16:19 2014
+++ src/sys/lib/libsa/bootcfg.h	Sat Jun 28 09:16:18 2014
@@ -0,0 +1,56 @@
+/*	$NetBSD: bootcfg.h,v 1.1 2014/06/28 09:16:18 rtr Exp $	*/
+
+/*-
+ * Copyright (c) 2008 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef _BOOTCFG_H
+#define _BOOTCFG_H
+
+#define BOOTCFG_FILENAME "boot.cfg"
+#define BOOTCFG_MAXMENU	 20
+#define BOOTCFG_MAXBANNER 12
+
+#define BOOTCFG_CMD_LOAD	  "load"
+#define BOOTCFG_CMD_USERCONF	  "userconf"
+
+typedef void (*bootcfg_command)(const char *cmd, char *arg);
+
+struct bootcfg_def {
+	char *banner[BOOTCFG_MAXBANNER];	/* Banner text */
+	char *command[BOOTCFG_MAXMENU];		/* Menu commands per entry*/
+	char *consdev;				/* Console device */
+	int def;				/* Default menu option */
+	char *desc[BOOTCFG_MAXMENU];		/* Menu text per entry */
+	int nummenu;				/* Number of menu items */
+	int timeout;		 		/* Timeout in seconds */
+	int menuformat;				/* Letters instead of numbers */
+	int clear;				/* Clear the screen? */
+} extern bootcfg_info;
+
+void perform_bootcfg(const char *, bootcfg_command, const off_t);
+void bootcfg_do_noop(const char *, char *);
+
+#endif /* !_BOOTCFG_H */

Reply via email to