Patchwork nvramtool: fixed working with files only on a non-coreboot system

login
register
about
Submitter Mathias Krause
Date 2011-03-07 14:33:22
Message ID <4D74ECB2.6050700@secunet.com>
Download mbox | patch
Permalink /patch/2752/
State Accepted
Headers show

Comments

Mathias Krause - 2011-03-07 14:33:22
nvramtool tried to get the CMOS layout from the coreboot option table
even when told to take a layout file. This patch fixes that case by
slightly changing the order of evaluating the specific options. Now
almost all combinations are valid (CMOS content from NVRAM/CBFS/file
with CMOS layout from CBFS/option table/layout file).

Additionally I fixed some white space issues (spaces to tabs) and throw
in some newlines to aid readability of the code.

Signed-off-by: Mathias Krause <mathias.krause@secunet.com>
Patrick Georgi - 2011-03-08 13:01:21
Am Montag, den 07.03.2011, 15:33 +0100 schrieb Mathias Krause:
> Signed-off-by: Mathias Krause <mathias.krause@secunet.com>
Acked-by: Patrick Georgi <patrick.georgi@secunet.com>

and committed as r6437


Thanks,
Patrick

Patch

Index: nvramtool/cli/nvramtool.c
===================================================================
--- nvramtool/cli/nvramtool.c	(Revision 6435)
+++ nvramtool/cli/nvramtool.c	(Arbeitskopie)
@@ -102,13 +102,9 @@ 
 
 	parse_nvramtool_args(argc, argv);
 
-	if (nvramtool_op_modifiers[NVRAMTOOL_MOD_USE_CMOS_LAYOUT_FILE].found) {
-		set_layout_filename(nvramtool_op_modifiers
-				    [NVRAMTOOL_MOD_USE_CMOS_LAYOUT_FILE].param);
-		fn = get_layout_from_file;
-	} 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) {
+	/* If we should operate on a CBFS file default to reading the layout
+	 * and CMOS contents from it. */
+	if (nvramtool_op_modifiers[NVRAMTOOL_MOD_USE_CBFS_FILE].found) {
 		open_cbfs(nvramtool_op_modifiers[NVRAMTOOL_MOD_USE_CBFS_FILE].param);
 		if (!nvramtool_op_modifiers[NVRAMTOOL_MOD_USE_CMOS_FILE].found) {
 			cmos_default = cbfs_find_file("cmos.default", CBFS_COMPONENT_CMOS_DEFAULT, NULL);
@@ -117,36 +113,55 @@ 
 				exit(1);
 			}
 		}
+		fn = get_layout_from_cbfs_file;
 	}
+
+	/* If the user wants to use a specific layout file or explicitly use
+	 * the coreboot option table allow him to override previous settings. */
+	if (nvramtool_op_modifiers[NVRAMTOOL_MOD_USE_CMOS_LAYOUT_FILE].found) {
+		set_layout_filename(nvramtool_op_modifiers[NVRAMTOOL_MOD_USE_CMOS_LAYOUT_FILE].param);
+		fn = get_layout_from_file;
+	} else if (nvramtool_op_modifiers[NVRAMTOOL_MOD_USE_CMOS_OPT_TABLE].found) {
+		fn = get_layout_from_cmos_table;
+	}
+
+	/* Allow the user to use a file for the CMOS contents, possibly
+	 * overriding a previously opened "cmos.default" file from the CBFS. */
 	if (nvramtool_op_modifiers[NVRAMTOOL_MOD_USE_CMOS_FILE].found) {
+		struct stat fd_stat;
 		int fd;
-		struct stat fd_stat;
-	       	if ((fd = open(nvramtool_op_modifiers[NVRAMTOOL_MOD_USE_CMOS_FILE].param, O_RDWR | O_CREAT, 0666)) < 0) {
+
+		if ((fd = open(nvramtool_op_modifiers[NVRAMTOOL_MOD_USE_CMOS_FILE].param, O_RDWR | O_CREAT, 0666)) < 0) {
 			fprintf(stderr, "Couldn't open '%s'\n", nvramtool_op_modifiers[NVRAMTOOL_MOD_USE_CMOS_FILE].param);
 			exit(1);
 		}
+
 		if (fstat(fd, &fd_stat) == -1) {
 			fprintf(stderr, "Couldn't stat '%s'\n", nvramtool_op_modifiers[NVRAMTOOL_MOD_USE_CMOS_FILE].param);
 			exit(1);
 		}
+		
 		if (fd_stat.st_size < 128) {
 			lseek(fd, 127, SEEK_SET);
 			write(fd, "\0", 1);
 			fsync(fd);
 		}
+		
 		cmos_default = mmap(NULL, 128, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
 		if (cmos_default == MAP_FAILED) {
 			fprintf(stderr, "Couldn't map '%s'\n", nvramtool_op_modifiers[NVRAMTOOL_MOD_USE_CMOS_FILE].param);
 			exit(1);
 		}
 	}
+
+	/* Switch to memory based CMOS access. */
 	if (cmos_default) {
 		select_hal(HAL_MEMORY, cmos_default);
-		fn = get_layout_from_cbfs_file;
 	}
 
 	register_cmos_layout_get_fn(fn);
-	op_fns[nvramtool_op.op] ();
+	op_fns[nvramtool_op.op]();
+
 	return 0;
 }