Subject: Little bootinfo cleanup
To: None <tech-kern@netbsd.org>
From: Julio M. Merino Vidal <jmmv84@gmail.com>
List: tech-kern
Date: 12/28/2005 21:11:37
------=_Part_3861_7471194.1135800697948
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

Hi,

the i386-code to manage the bootinfo structure in the kernel is
messy.  It treats the variable like a big byte array and access the
elements using offsets and casts within it; part of this is hard to avoid,
but another is quite easy.  What about defining a little structure that,
at the very least, divides the number of items and the actual raw data?

Also, the lookup_bootinfo function could be moved to the common x86
subdirectory, as it is shared between amd64, i386 and x86.

The attached patch does both things.  Comments?

Thanks,

--
Julio M. Merino Vidal <jmmv84@gmail.com>
The Julipedia - http://julipedia.blogspot.com/
The NetBSD Project - http://www.NetBSD.org/

------=_Part_3861_7471194.1135800697948
Content-Type: application/octet-stream; name=patch.diff
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="patch.diff"

Index: i386/i386/machdep.c
===================================================================
RCS file: /cvsroot/src/sys/arch/i386/i386/machdep.c,v
retrieving revision 1.568
diff -u -r1.568 machdep.c
--- i386/i386/machdep.c	26 Dec 2005 19:23:59 -0000	1.568
+++ i386/i386/machdep.c	28 Dec 2005 20:07:47 -0000
@@ -203,8 +203,6 @@
 char machine[] = "i386";		/* CPU "architecture" */
 char machine_arch[] = "i386";		/* machine == machine_arch */
 
-char bootinfo[BOOTINFO_MAXSIZE];
-
 extern struct bi_devmatch *x86_alldisks;
 extern int x86_ndisks;
 
@@ -2020,20 +2018,6 @@
 	return error;
 }
 
-void *
-lookup_bootinfo(int type)
-{
-	struct btinfo_common *help;
-	int n = *(int*)bootinfo;
-	help = (struct btinfo_common *)(bootinfo + sizeof(int));
-	while(n--) {
-		if(help->type == type)
-			return(help);
-		help = (struct btinfo_common *)((char*)help + help->len);
-	}
-	return(0);
-}
-
 #include <dev/ic/mc146818reg.h>		/* for NVRAM POST */
 #include <i386/isa/nvram.h>		/* for NVRAM POST */
 
Index: x86/conf/files.x86
===================================================================
RCS file: /cvsroot/src/sys/arch/x86/conf/files.x86,v
retrieving revision 1.14
diff -u -r1.14 files.x86
--- x86/conf/files.x86	11 Dec 2005 12:19:47 -0000	1.14
+++ x86/conf/files.x86	28 Dec 2005 20:07:47 -0000
@@ -25,6 +25,7 @@
 file	arch/x86/x86/mtrr_i686.c	mtrr
 file	arch/x86/x86/softintr.c
 file	arch/x86/x86/x86_autoconf.c
+file	arch/x86/x86/x86_machdep.c
 
 define lapic
 file	arch/x86/x86/lapic.c		lapic needs-flag
Index: x86/include/bootinfo.h
===================================================================
RCS file: /cvsroot/src/sys/arch/x86/include/bootinfo.h,v
retrieving revision 1.9
diff -u -r1.9 bootinfo.h
--- x86/include/bootinfo.h	6 Jul 2005 08:27:31 -0000	1.9
+++ x86/include/bootinfo.h	28 Dec 2005 20:07:47 -0000
@@ -157,11 +157,19 @@
 	struct bi_biosgeom_entry disk[1]; /* var len */
 };
 
-#ifdef _KERNEL
-void *lookup_bootinfo(int);
-#endif
 #endif /* _LOCORE */
 
 #ifdef _KERNEL
+
 #define BOOTINFO_MAXSIZE 4096
-#endif
+
+#ifndef _LOCORE
+struct bootinfo {
+	uint32_t	bi_nentries;
+	uint8_t		bi_data[BOOTINFO_MAXSIZE - sizeof(uint32_t)];
+};
+
+void *lookup_bootinfo(int);
+#endif /* _LOCORE */
+
+#endif /* _KERNEL */
Index: x86/x86/x86_machdep.c
===================================================================
RCS file: x86/x86/x86_machdep.c
diff -N x86/x86/x86_machdep.c
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ x86/x86/x86_machdep.c	28 Dec 2005 20:07:47 -0000
@@ -0,0 +1,81 @@
+/*	$NetBSD$	*/
+
+/*-
+ * Copyright (c) 2005 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Julio M. Merino Vidal.
+ *
+ * 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.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *	This product includes software developed by the NetBSD
+ *	Foundation, Inc. and its contributors.
+ * 4. Neither the name of The NetBSD Foundation nor the names of its
+ *    contributors may be used to endorse or promote products derived
+ *    from this software without specific prior written permission.
+ *
+ * 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/cdefs.h>
+__KERNEL_RCSID(0, "$NetBSD$");
+
+#include <sys/types.h>
+#include <sys/systm.h>
+
+#include <machine/bootinfo.h>
+
+/* --------------------------------------------------------------------- */
+
+/*
+ * Main bootinfo structure.  This is filled in by the bootstrap process
+ * done in locore.S based on the information passed by the boot loader.
+ */
+struct bootinfo bootinfo;
+
+/* --------------------------------------------------------------------- */
+
+/*
+ * Given the type of a bootinfo entry, looks for a matching item inside
+ * the bootinfo structure.  If found, returns a pointer to it (which must
+ * then be casted to the appropriate bootinfo_* type); otherwise, returns
+ * NULL.
+ */
+void *
+lookup_bootinfo(int type)
+{
+	boolean_t found;
+	int i;
+	struct btinfo_common *bic;
+
+	bic = (struct btinfo_common *)(bootinfo.bi_data);
+	found = FALSE;
+	for (i = 0; i < bootinfo.bi_nentries && !found; i++) {
+		if (bic->type == type)
+			found = TRUE;
+		else
+			bic = (struct btinfo_common *)
+			    ((uint8_t *)bic + bic->len);
+	}
+
+	return found ? bic : NULL;
+}
Index: xen/conf/files.xen
===================================================================
RCS file: /cvsroot/src/sys/arch/xen/conf/files.xen,v
retrieving revision 1.31
diff -u -r1.31 files.xen
--- xen/conf/files.xen	21 Nov 2005 22:15:13 -0000	1.31
+++ xen/conf/files.xen	28 Dec 2005 20:07:47 -0000
@@ -93,6 +93,7 @@
 file	arch/x86/x86/ipi.c		multiprocessor
 file	arch/x86/x86/lock_machdep.c	lockdebug
 file	arch/x86/x86/softintr.c
+file	arch/x86/x86/x86_machdep.c
 
 include	"arch/xen/conf/files.compat"
 
Index: xen/i386/machdep.c
===================================================================
RCS file: /cvsroot/src/sys/arch/xen/i386/machdep.c,v
retrieving revision 1.22
diff -u -r1.22 machdep.c
--- xen/i386/machdep.c	24 Dec 2005 20:07:48 -0000	1.22
+++ xen/i386/machdep.c	28 Dec 2005 20:07:48 -0000
@@ -235,8 +235,6 @@
 char machine[] = "i386";		/* CPU "architecture" */
 char machine_arch[] = "i386";		/* machine == machine_arch */
 
-char bootinfo[BOOTINFO_MAXSIZE];
-
 extern struct bi_devmatch *x86_alldisks;
 extern int x86_ndisks;
 
@@ -2226,20 +2224,6 @@
 	return error;
 }
 
-void *
-lookup_bootinfo(int type)
-{
-	struct btinfo_common *help;
-	int n = *(int*)bootinfo;
-	help = (struct btinfo_common *)(bootinfo + sizeof(int));
-	while(n--) {
-		if(help->type == type)
-			return(help);
-		help = (struct btinfo_common *)((char*)help + help->len);
-	}
-	return(0);
-}
-
 #include <dev/ic/mc146818reg.h>		/* for NVRAM POST */
 #include <i386/isa/nvram.h>		/* for NVRAM POST */
 

------=_Part_3861_7471194.1135800697948--