add get_option to libpayload, so coreboot cmos options can be queried.

Signed-off-by: Stefan Reinauer <stepan@coresystems.de>

Index: include/sysinfo.h
===================================================================
--- include/sysinfo.h	(revision 372)
+++ include/sysinfo.h	(working copy)
@@ -43,6 +43,11 @@
 		unsigned long long base;
 		unsigned long long size;
 	} memrange[SYSINFO_MAX_MEM_RANGES];
+
+	struct cb_cmos_option_table *option_table;
+	u32 cmos_range_start;
+	u32 cmos_range_end;
+	u32 cmos_checksum_location;
 };
 
 extern struct sysinfo_t lib_sysinfo;
Index: include/coreboot_tables.h
===================================================================
--- include/coreboot_tables.h	(revision 372)
+++ include/coreboot_tables.h	(working copy)
@@ -128,8 +128,58 @@
 #define CB_TAG_CONSOLE_SROM       4
 #define CB_TAG_CONSOLE_EHCI       5
 
-/* Still to come: CMOS information. */
+#define CB_TAG_CMOS_OPTION_TABLE 0x00c8
+struct cb_cmos_option_table {
+	u32 tag;
+	u32 size;
+	u32 header_length;
+};
 
+#define CB_TAG_OPTION         0x00c9
+#define CMOS_MAX_NAME_LENGTH    32
+struct cb_cmos_entries {
+	u32 tag;
+	u32 size;
+	u32 bit;
+	u32 length;
+	u32 config;
+	u32 config_id;
+	u8 name[CMOS_MAX_NAME_LENGTH];
+};
+
+
+#define CB_TAG_OPTION_ENUM    0x00ca
+#define CMOS_MAX_TEXT_LENGTH 32
+struct cb_cmos_enums {
+	u32 tag;
+	u32 size;
+	u32 config_id;
+	u32 value;
+	u8 text[CMOS_MAX_TEXT_LENGTH];
+};
+
+#define CB_TAG_OPTION_DEFAULTS 0x00cb
+#define CMOS_IMAGE_BUFFER_SIZE 128
+struct cb_cmos_defaults {
+	u32 tag;
+	u32 size;
+	u32 name_length;
+	u8 name[CMOS_MAX_NAME_LENGTH];
+	u8 default_set[CMOS_IMAGE_BUFFER_SIZE];
+};
+
+#define CB_TAG_OPTION_CHECKSUM 0x00cc
+#define CHECKSUM_NONE	0
+#define CHECKSUM_PCBIOS	1
+struct	cb_cmos_checksum {
+	u32 tag;
+	u32 size;
+	u32 range_start;
+	u32 range_end;
+	u32 location;
+	u32 type;
+};
+
 /* Helpful macros */
 
 #define MEM_RANGE_COUNT(_rec) \
Index: i386/coreboot.c
===================================================================
--- i386/coreboot.c	(revision 372)
+++ i386/coreboot.c	(working copy)
@@ -74,6 +74,21 @@
 	info->ser_ioport = ser->ioport;
 }
 
+#ifdef CONFIG_NVRAM
+static void cb_parse_optiontable(unsigned char *ptr, struct sysinfo_t *info)
+{
+	info->option_table = (struct cb_cmos_option_table *)ptr;
+}
+
+static void cb_parse_checksum(unsigned char *ptr, struct sysinfo_t *info)
+{
+	struct cb_cmos_checksum *cmos_cksum = (struct cb_cmos_checksum *)ptr;
+	info->cmos_range_start = cmos_cksum->range_start;
+	info->cmos_range_end = cmos_cksum->range_end;
+	info->cmos_checksum_location = cmos_cksum->location;
+}
+#endif
+
 static int cb_parse_header(void *addr, int len, struct sysinfo_t *info)
 {
 	struct cb_header *header;
@@ -115,6 +130,14 @@
 		case CB_TAG_SERIAL:
 			cb_parse_serial(ptr, info);
 			break;
+#ifdef CONFIG_NVRAM
+		case CB_TAG_CMOS_OPTION_TABLE:
+			cb_parse_optiontable(ptr, info);
+			break;
+		case CB_TAG_OPTION_CHECKSUM:
+			cb_parse_checksum(ptr, info);
+			break;
+#endif
 		}
 
 		ptr += rec->size;
Index: drivers/Makefile.inc
===================================================================
--- drivers/Makefile.inc	(revision 380)
+++ drivers/Makefile.inc	(working copy)
@@ -35,6 +35,7 @@
 TARGETS-$(CONFIG_SERIAL_CONSOLE) += drivers/serial.o
 TARGETS-$(CONFIG_PC_KEYBOARD) += drivers/keyboard.o
 TARGETS-$(CONFIG_NVRAM) += drivers/nvram.o
+TARGETS-$(CONFIG_NVRAM) += drivers/options.o
 
 # Video console drivers
 TARGETS-$(CONFIG_VIDEO_CONSOLE) += drivers/video/video.o
Index: drivers/options.c
===================================================================
--- drivers/options.c	(revision 0)
+++ drivers/options.c	(revision 0)
@@ -0,0 +1,106 @@
+/*
+ * This file is part of the libpayload project.
+ *
+ * Copyright (C) 2008 coresystems GmbH
+ *
+ * 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. The name of the author may not be used to endorse or promote products
+ *    derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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 <libpayload.h>
+#include <coreboot_tables.h>
+
+static int options_checksum_valid(void)
+{
+	int i;
+	int range_start = lib_sysinfo.cmos_range_start;
+	int range_end = lib_sysinfo.cmos_range_end;
+	int checksum_location = lib_sysinfo.cmos_checksum_location;
+	u16 checksum = 0, checksum_old;
+
+	for(i = range_start; i <= range_end; i++) {
+		checksum += nvram_read(i);
+	}
+	checksum = (~checksum)&0xffff;
+
+	checksum_old = ((nvram_read(checksum_location)<<8) | nvram_read(checksum_location+1));
+
+	return (checksum_old == checksum);
+}
+
+static int get_cmos_value(u32 bitnum, u32 len, void *valptr)
+{
+	u8 *value = (u8 *)valptr;
+	int offs = 0;
+	u32 addr, bit;
+	u8 reg8;
+
+	value = valptr;
+
+	/* Convert to byte borders */
+	addr=(bitnum / 8);
+	bit=(bitnum % 8);
+
+	/* Handle single byte or less */
+	if(len <= 8) {
+		reg8 = nvram_read(addr);
+		reg8 >>= bit;
+		value[0] = reg8 & ((1 << len) -1);
+		return 0;
+	}
+
+	/* When handling more than a byte, copy whole bytes */
+	while (len > 0) {
+		len -= 8;
+		value[offs++]=nvram_read(addr++);
+	}
+
+	return 0;
+}
+
+int get_option(void *dest, char *name)
+{
+	struct cb_cmos_option_table *option_table = lib_sysinfo.option_table;
+	struct cb_cmos_entries *cmos_entry;
+	int len = strnlen(name, CMOS_MAX_NAME_LENGTH);
+	
+	/* cmos entries are located right after the option table */
+	cmos_entry=(struct cb_cmos_entries*)((unsigned char *)option_table + option_table->header_length);
+
+	for (   cmos_entry = (struct cb_cmos_entries*)((unsigned char *)option_table + option_table->header_length);
+		cmos_entry->tag == CB_TAG_OPTION;
+		cmos_entry = (struct cb_cmos_entries*)((unsigned char *)cmos_entry + cmos_entry->size)) {
+		if (memcmp(cmos_entry->name, name, len))
+			continue;
+		if(get_cmos_value(cmos_entry->bit, cmos_entry->length, dest))
+			return 1;
+
+		if(!options_checksum_valid())
+			return 1;
+
+		return 0;
+	}
+
+	printf("ERROR: No such CMOS option (%s)\n", name);
+	return 1;
+}
