[coreboot] [commit] r6285 - trunk/util/nvramtool

repository service svn at coreboot.org
Fri Jan 21 08:24:13 CET 2011


Author: oxygene
Date: Fri Jan 21 08:24:08 2011
New Revision: 6285
URL: https://tracker.coreboot.org/trac/coreboot/changeset/6285

Log:
Add nvramtool -C option that takes a CBFS file as argument.
When using this option, nvramtool looks for a cmos_layout.bin
and cmos.default in the image and uses these for layout information
and CMOS data.

Signed-off-by: Patrick Georgi <patrick.georgi at secunet.com>
Acked-by: Stefan Reinauer <stepan at coreboot.org>

Added:
   trunk/util/nvramtool/cbfs.c
   trunk/util/nvramtool/cbfs.h
Modified:
   trunk/util/nvramtool/Makefile
   trunk/util/nvramtool/common.c
   trunk/util/nvramtool/lbtable.c
   trunk/util/nvramtool/lbtable.h
   trunk/util/nvramtool/nvramtool.c
   trunk/util/nvramtool/opts.c
   trunk/util/nvramtool/opts.h

Modified: trunk/util/nvramtool/Makefile
==============================================================================
--- trunk/util/nvramtool/Makefile	Fri Jan 21 08:19:59 2011	(r6284)
+++ trunk/util/nvramtool/Makefile	Fri Jan 21 08:24:08 2011	(r6285)
@@ -24,12 +24,12 @@
 STRIP	= strip
 INSTALL = /usr/bin/install
 PREFIX  = /usr/local
-CFLAGS  = -O2 -g -Wall -W
+CFLAGS  = -O2 -g -Wall -W -m32
 #CFLAGS  = -Os -Wall
 
 OBJS =  cmos_lowlevel.o cmos_ops.o common.o compute_ip_checksum.o \
 	hexdump.o input_file.o layout.o layout_file.o lbtable.o   \
-	nvramtool.o opts.o reg_expr.o
+	nvramtool.o opts.o reg_expr.o cbfs.o
 
 OS_ARCH        = $(shell uname)
 ifeq ($(OS_ARCH), Darwin)

Added: trunk/util/nvramtool/cbfs.c
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ trunk/util/nvramtool/cbfs.c	Fri Jan 21 08:24:08 2011	(r6285)
@@ -0,0 +1,146 @@
+/*
+ * This file is part of the coreboot project.
+ *
+ * Copyright (C) 2008 Jordan Crouse <jordan at cosmicpenguin.net>
+ * Copyright (C) 2011 secunet Security Networks AG
+ *     (Written by Patrick Georgi <patrick.georgi at secunet.com>)
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA
+ */
+
+#include <arpa/inet.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/mman.h>
+#include <stdlib.h>
+#include <fcntl.h>
+#include <string.h>
+#include <stdio.h>
+#include "cbfs.h"
+
+#define ALIGN(x,a)              __ALIGN_MASK(x,(typeof(x))(a)-1)
+#define __ALIGN_MASK(x,mask)    (((x)+(mask))&~(mask))
+
+static void *cbfs_mapped;
+static u32 cbfs_offset;
+static void* virt_to_phys(u32 virt)
+{
+	return (void*)(virt+cbfs_offset);
+}
+
+#ifdef DEBUG
+#define debug(x...) printf(x)
+#else
+#define debug(x...) while(0) {}
+#endif
+
+static int cbfs_check_magic(struct cbfs_file *file)
+{
+	return !strcmp(file->magic, CBFS_FILE_MAGIC) ? 1 : 0;
+}
+
+static struct cbfs_header *cbfs_master_header(void)
+{
+	struct cbfs_header *header;
+
+	void *ptr = virt_to_phys(*((u32*)virt_to_phys(CBFS_HEADPTR_ADDR)));
+	debug("Check CBFS header at %p\n", ptr);
+	header = (struct cbfs_header *) ptr;
+
+	debug("magic is %08x\n", ntohl(header->magic));
+	if (ntohl(header->magic) != CBFS_HEADER_MAGIC) {
+		printf("ERROR: No valid CBFS header found!\n");
+		return NULL;
+	}
+
+	debug("Found CBFS header at %p\n", ptr);
+	return header;
+}
+
+struct cbfs_file *cbfs_find(const char *name)
+{
+	struct cbfs_header *header = cbfs_master_header();
+	unsigned long offset;
+
+	if (header == NULL)
+		return NULL;
+	offset = (u32)virt_to_phys(0 - ntohl(header->romsize) + ntohl(header->offset));
+
+	int align= ntohl(header->align);
+
+	while(1) {
+		struct cbfs_file *file = (struct cbfs_file *) offset;
+		if (!cbfs_check_magic(file)) return NULL;
+		debug("Check %s\n", CBFS_NAME(file));
+		if (!strcmp(CBFS_NAME(file), name))
+			return file;
+
+		int flen = ntohl(file->len);
+		int foffset = ntohl(file->offset);
+		debug("CBFS: follow chain: %p + %x + %x + align -> ", (void *)offset, foffset, flen);
+
+		unsigned long oldoffset = offset;
+		offset = ALIGN(offset + foffset + flen, align);
+		debug("%p\n", (void *)offset);
+		if (offset <= oldoffset) return NULL;
+
+		if (offset < (u32)virt_to_phys(0xFFFFFFFF - ntohl(header->romsize)))
+			return NULL;
+	}
+}
+
+void *cbfs_find_file(const char *name, unsigned int type, unsigned int *len)
+{
+	struct cbfs_file *file = cbfs_find(name);
+
+	if (file == NULL) {
+		printf("CBFS:  Could not find file %s\n",
+		       name);
+		return NULL;
+	}
+
+	if (ntohl(file->type) != type) {
+		printf("CBFS:  File %s is of type %x instead of"
+		       "type %x\n", name, file->type, type);
+
+		return NULL;
+	}
+	if (len != NULL) *len = file->len;
+
+	return (void *) CBFS_SUBHEADER(file);
+}
+
+void open_cbfs(const char *filename)
+{
+	struct stat cbfs_stat;
+	int cbfs_fd;
+
+	cbfs_fd = open(filename, O_RDWR);
+	if (cbfs_fd == -1) {
+		printf("Couldn't open '%s'\n", filename);
+		exit(-1);
+	}
+	if (fstat(cbfs_fd, &cbfs_stat) == -1) {
+		printf("Couldn't stat '%s'\n", filename);
+		exit(-1);
+	}
+	cbfs_mapped = mmap(NULL, cbfs_stat.st_size, PROT_READ | PROT_WRITE,
+			MAP_SHARED, cbfs_fd, 0);
+	if (cbfs_mapped == MAP_FAILED) {
+		printf("Couldn't map '%s'\n", filename);
+		exit(-1);
+	}
+	cbfs_offset = (uint32_t)cbfs_mapped-(0xffffffff-cbfs_stat.st_size+1);
+}
+

Added: trunk/util/nvramtool/cbfs.h
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ trunk/util/nvramtool/cbfs.h	Fri Jan 21 08:24:08 2011	(r6285)
@@ -0,0 +1,181 @@
+/*
+ * This file is part of the coreboot project.
+ *
+ * Copyright (C) 2008 Jordan Crouse <jordan at cosmicpenguin.net>
+ *
+ * This file is dual-licensed. You can choose between:
+ *   - The GNU GPL, version 2, as published by the Free Software Foundation
+ *   - The revised BSD license (without advertising clause)
+ *
+ * ---------------------------------------------------------------------------
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA
+ * ---------------------------------------------------------------------------
+ * 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.
+ * ---------------------------------------------------------------------------
+ */
+
+#ifndef _CBFS_H_
+#define _CBFS_H_
+
+#include "coreboot_tables.h"
+
+typedef uint64_t u64;
+typedef uint32_t u32;
+typedef uint16_t u16;
+typedef uint8_t u8;
+
+/** These are standard values for the known compression
+    alogrithms that coreboot knows about for stages and
+    payloads.  Of course, other CBFS users can use whatever
+    values they want, as long as they understand them. */
+
+#define CBFS_COMPRESS_NONE  0
+#define CBFS_COMPRESS_LZMA  1
+
+/** These are standard component types for well known
+    components (i.e - those that coreboot needs to consume.
+    Users are welcome to use any other value for their
+    components */
+
+#define CBFS_TYPE_STAGE      0x10
+#define CBFS_TYPE_PAYLOAD    0x20
+#define CBFS_TYPE_OPTIONROM  0x30
+#define CBFS_TYPE_BOOTSPLASH 0x40
+#define CBFS_TYPE_RAW        0x50
+#define CBFS_TYPE_VSA        0x51
+#define CBFS_TYPE_MBI        0x52
+#define CBFS_TYPE_MICROCODE  0x53
+#define CBFS_COMPONENT_CMOS_DEFAULT 0xaa
+#define CBFS_COMPONENT_CMOS_LAYOUT 0x01aa
+
+
+/** this is the master cbfs header - it need to be
+    located somewhere in the bootblock.  Where it
+    actually lives is up to coreboot. A pointer to
+    this header will live at 0xFFFFFFFc, so we can
+    easily find it. */
+
+#define CBFS_HEADER_MAGIC  0x4F524243
+#define CBFS_HEADPTR_ADDR 0xFFFFFFFc
+#define VERSION1 0x31313131
+
+struct cbfs_header {
+	u32 magic;
+        u32 version;
+        u32 romsize;
+        u32 bootblocksize;
+	u32 align;
+	u32 offset;
+	u32 pad[2];
+} __attribute__((packed));
+
+/** This is a component header - every entry in the CBFS
+    will have this header.
+
+    This is how the component is arranged in the ROM:
+
+    --------------   <- 0
+    component header
+    --------------   <- sizeof(struct component)
+    component name
+    --------------   <- offset
+    data
+    ...
+    --------------   <- offset + len
+*/
+
+#define CBFS_FILE_MAGIC "LARCHIVE"
+
+struct cbfs_file {
+	char magic[8];
+	u32 len;
+	u32 type;
+	u32 checksum;
+	u32 offset;
+} __attribute__((packed));
+
+/*** Component sub-headers ***/
+
+/* Following are component sub-headers for the "standard"
+   component types */
+
+/** This is the sub-header for stage components.  Stages are
+    loaded by coreboot during the normal boot process */
+
+struct cbfs_stage {
+	u32 compression;  /** Compression type */
+	u64 entry;  /** entry point */
+	u64 load;   /** Where to load in memory */
+	u32 len;          /** length of data to load */
+	u32 memlen;	   /** total length of object in memory */
+} __attribute__((packed));
+
+/** this is the sub-header for payload components.  Payloads
+    are loaded by coreboot at the end of the boot process */
+
+struct cbfs_payload_segment {
+	u32 type;
+	u32 compression;
+	u32 offset;
+	u64 load_addr;
+	u32 len;
+	u32 mem_len;
+} __attribute__((packed));
+
+struct cbfs_payload {
+	struct cbfs_payload_segment segments;
+};
+
+#define PAYLOAD_SEGMENT_CODE   0x45444F43
+#define PAYLOAD_SEGMENT_DATA   0x41544144
+#define PAYLOAD_SEGMENT_BSS    0x20535342
+#define PAYLOAD_SEGMENT_PARAMS 0x41524150
+#define PAYLOAD_SEGMENT_ENTRY  0x52544E45
+
+struct cbfs_optionrom {
+	u32 compression;
+	u32 len;
+} __attribute__((packed));
+
+#define CBFS_NAME(_c) (((char *) (_c)) + sizeof(struct cbfs_file))
+#define CBFS_SUBHEADER(_p) ( (void *) ((((u8 *) (_p)) + ntohl((_p)->offset))) )
+
+void * cbfs_get_file(const char *name);
+struct cbfs_file *cbfs_find(const char *name);
+void *cbfs_find_file(const char *name, unsigned int type, unsigned int *len);
+
+void open_cbfs(const char *filename);
+#endif
+

Modified: trunk/util/nvramtool/common.c
==============================================================================
--- trunk/util/nvramtool/common.c	Fri Jan 21 08:19:59 2011	(r6284)
+++ trunk/util/nvramtool/common.c	Fri Jan 21 08:24:08 2011	(r6285)
@@ -82,6 +82,7 @@
 		"LAYOUT_FILE.\n"
 		"       -t:             Use CMOS layout specified by CMOS option "
 		"table.\n"
+		"       -C CBFS_FILE:   Use CBFS file for layout and CMOS defaults.\n"
 		"       [-n] -r NAME:   Show parameter NAME.  If -n is given, "
 		"show value only.\n"
 		"       -e NAME:        Show all possible values for parameter "

Modified: trunk/util/nvramtool/lbtable.c
==============================================================================
--- trunk/util/nvramtool/lbtable.c	Fri Jan 21 08:19:59 2011	(r6284)
+++ trunk/util/nvramtool/lbtable.c	Fri Jan 21 08:24:08 2011	(r6285)
@@ -29,6 +29,7 @@
  *  51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 \*****************************************************************************/
 
+#include <arpa/inet.h>
 #include <string.h>
 #include <sys/mman.h>
 #include "common.h"
@@ -38,6 +39,7 @@
 #include "layout.h"
 #include "cmos_lowlevel.h"
 #include "hexdump.h"
+#include "cbfs.h"
 
 typedef void (*lbtable_print_fn_t) (const struct lb_record * rec);
 
@@ -315,6 +317,21 @@
 	exit(1);
 }
 
+static void process_layout(void)
+{
+	if ((cmos_table) == NULL) {
+		fprintf(stderr,
+			"%s: CMOS option table not found in coreboot table.  "
+			"Apparently, the coreboot installed on this system was "
+			"built without specifying CONFIG_HAVE_OPTION_TABLE.\n",
+			prog_name);
+		exit(1);
+	}
+
+	process_cmos_table();
+	get_cmos_checksum_info();
+}
+
 /****************************************************************************
  * get_layout_from_cmos_table
  *
@@ -327,18 +344,18 @@
 	get_lbtable();
 	cmos_table = (const struct cmos_option_table *)
 	    find_lbrec(LB_TAG_CMOS_OPTION_TABLE);
+	process_layout();
+}
 
-	if ((cmos_table) == NULL) {
-		fprintf(stderr,
-			"%s: CMOS option table not found in coreboot table.  "
-			"Apparently, the coreboot installed on this system was "
-			"built without specifying CONFIG_HAVE_OPTION_TABLE.\n",
-			prog_name);
-		exit(1);
-	}
-
-	process_cmos_table();
-	get_cmos_checksum_info();
+void get_layout_from_cbfs_file(void)
+{
+	static struct lb_header header;
+	u32 len;
+	cmos_table = cbfs_find_file("cmos_layout.bin", CBFS_COMPONENT_CMOS_LAYOUT, &len);
+	lbtable = &header;
+	header.header_bytes = (u32)cmos_table-(u32)lbtable;
+	header.table_bytes = ntohl(len);
+	process_layout();
 }
 
 /****************************************************************************

Modified: trunk/util/nvramtool/lbtable.h
==============================================================================
--- trunk/util/nvramtool/lbtable.h	Fri Jan 21 08:19:59 2011	(r6284)
+++ trunk/util/nvramtool/lbtable.h	Fri Jan 21 08:24:08 2011	(r6285)
@@ -35,6 +35,7 @@
 
 void get_lbtable(void);
 void get_layout_from_cmos_table(void);
+void get_layout_from_cbfs_file(void);
 void dump_lbtable(void);
 void list_lbtable_choices(void);
 void list_lbtable_item(const char item[]);

Modified: trunk/util/nvramtool/nvramtool.c
==============================================================================
--- trunk/util/nvramtool/nvramtool.c	Fri Jan 21 08:19:59 2011	(r6284)
+++ trunk/util/nvramtool/nvramtool.c	Fri Jan 21 08:24:08 2011	(r6285)
@@ -38,6 +38,7 @@
 #include "cmos_lowlevel.h"
 #include "reg_expr.h"
 #include "hexdump.h"
+#include "cbfs.h"
 
 typedef void (*op_fn_t) (void);
 
@@ -92,7 +93,7 @@
  ****************************************************************************/
 int main(int argc, char *argv[])
 {
-	cmos_layout_get_fn_t fn;
+	cmos_layout_get_fn_t fn = get_layout_from_cmos_table;
 
 	parse_nvramtool_args(argc, argv);
 
@@ -100,8 +101,18 @@
 		set_layout_filename(nvramtool_op_modifiers
 				    [NVRAMTOOL_MOD_USE_CMOS_LAYOUT_FILE].param);
 		fn = get_layout_from_file;
-	} else
+	} else if (nvramtool_op_modifiers[NVRAMTOOL_MOD_USE_CMOS_OPT_TABLE].found) {
 		fn = get_layout_from_cmos_table;
+	} else if (nvramtool_op_modifiers[NVRAMTOOL_MOD_USE_CBFS_FILE].found) {
+		open_cbfs(nvramtool_op_modifiers[NVRAMTOOL_MOD_USE_CBFS_FILE].param);
+		void *cmosdefault = cbfs_find_file("cmos.default", CBFS_COMPONENT_CMOS_DEFAULT, NULL);
+		if (cmosdefault == NULL) {
+			printf("Need a cmos.default in the CBFS image for now.\n");
+			exit(1);
+		}
+		select_hal(HAL_MEMORY, cmosdefault);
+		fn = get_layout_from_cbfs_file;
+	}
 
 	register_cmos_layout_get_fn(fn);
 	op_fns[nvramtool_op.op] ();

Modified: trunk/util/nvramtool/opts.c
==============================================================================
--- trunk/util/nvramtool/opts.c	Fri Jan 21 08:19:59 2011	(r6284)
+++ trunk/util/nvramtool/opts.c	Fri Jan 21 08:24:08 2011	(r6285)
@@ -41,7 +41,7 @@
 static void resolve_op_modifiers(void);
 static void sanity_check_args(void);
 
-static const char getopt_string[] = "-ab:B:c::de:hil::np:r:tvw:xX:y:Y";
+static const char getopt_string[] = "-ab:B:c::C:de:hil::np:r:tvw:xX:y:Y";
 
 /****************************************************************************
  * parse_nvramtool_args
@@ -82,6 +82,10 @@
 			register_op(&op_found, NVRAMTOOL_OP_CMOS_CHECKSUM,
 				    handle_optional_arg(argc, argv));
 			break;
+		case 'C':
+			register_op_modifier(NVRAMTOOL_MOD_USE_CBFS_FILE,
+					     optarg);
+			break;
 		case 'd':
 			register_op(&op_found, NVRAMTOOL_OP_LBTABLE_DUMP, NULL);
 			break;

Modified: trunk/util/nvramtool/opts.h
==============================================================================
--- trunk/util/nvramtool/opts.h	Fri Jan 21 08:19:59 2011	(r6284)
+++ trunk/util/nvramtool/opts.h	Fri Jan 21 08:24:08 2011	(r6285)
@@ -58,6 +58,7 @@
 
 typedef enum { NVRAMTOOL_MOD_SHOW_VALUE_ONLY = 0,
 	NVRAMTOOL_MOD_USE_CMOS_LAYOUT_FILE,
+	NVRAMTOOL_MOD_USE_CBFS_FILE,
 	NVRAMTOOL_MOD_USE_CMOS_OPT_TABLE,
 	NVRAMTOOL_NUM_OP_MODIFIERS	/* must always be last */
 } nvramtool_op_modifier_t;




More information about the coreboot mailing list