Move option table (cmos.layout's binary representation)
to CBFS and adapt coreboot to use it.

Comments by Stefan and Mathias taken into account (except for
the build time failure if the table is missing when it should
exist and the "memory leak" in build_opt_tbl)

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


git-svn-id: svn://svn.coreboot.org/coreboot/trunk@6268 2b7e53f0-3cfb-0310-b3e9-8179ed1497e1
diff --git a/util/cbfstool/cbfs.h b/util/cbfstool/cbfs.h
index 6fb9edd..5c93838a 100644
--- a/util/cbfstool/cbfs.h
+++ b/util/cbfstool/cbfs.h
@@ -77,6 +77,7 @@
 #define CBFS_COMPONENT_MBI        0x52
 #define CBFS_COMPONENT_MICROCODE  0x53
 #define CBFS_COMPONENT_CMOS_DEFAULT 0xaa
+#define CBFS_COMPONENT_CMOS_LAYOUT 0x01aa
 
 /* The deleted type is chosen to be a value
  * that can be written in a FLASH from all other
diff --git a/util/options/build_opt_tbl.c b/util/options/build_opt_tbl.c
index 1d218df..0b25b0c 100644
--- a/util/options/build_opt_tbl.c
+++ b/util/options/build_opt_tbl.c
@@ -33,7 +33,7 @@
 #define INPUT_LINE_MAX 256
 #define MAX_VALUE_BYTE_LENGTH 64
 
-#define TMPFILE_LEN 256
+#define TMPFILE_LEN 25600
 #define TMPFILE_TEMPLATE "/build_opt_tbl_XXXXXX"
 
 static unsigned char cmos_table[4096];
@@ -142,8 +142,9 @@
 	printf("                       [--option filename]\n");
 	printf("                       [--header filename]\n\n");
 	printf("--config = Build the definitions table from the given file.\n");
+	printf("--binary = Output a binary file with the definitions.\n");
 	printf("--option = Output a C source file with the definitions.\n");
-	printf("--header = Ouput a C header file with the definitions.\n");
+	printf("--header = Output a C header file with the definitions.\n");
 	exit(1);
 }
 
@@ -253,6 +254,7 @@
 {
 	int i;
 	char *config=0;
+	char *binary=0;
 	char *option=0;
 	char *header=0;
 	FILE *fp;
@@ -288,6 +290,12 @@
                                                 }
                                                 config=argv[++i];
                                                 break;
+                                        case 'b':  /* Emit a binary file */
+                                                if(strcmp(&argv[i][2],"binary")) {
+                                                        display_usage(argv[0]);
+                                                }
+                                                binary=argv[++i];
+                                                break;
                                         case 'o':  /* use a cmos definitions table file */
                                                 if(strcmp(&argv[i][2],"option")) {
                                                         display_usage(argv[0]);
@@ -519,8 +527,7 @@
 	/* See if we want to output a C source file */
 	if(option) {
 		int err=0;
-		strncpy(tempfilename, dirname(strdup(option)), TMPFILE_LEN);
-	        strncat(tempfilename, TMPFILE_TEMPLATE, TMPFILE_LEN);
+		snprintf(tempfilename, TMPFILE_LEN, "%s%s", dirname(strdup(option)), TMPFILE_TEMPLATE);
 		tempfile = mkstemp(tempfilename);
 		if(tempfile == -1) {
                         perror("Error - Could not create temporary file");
@@ -566,13 +573,46 @@
 		}
 	}
 
+	/* See if we also want to output a binary file */
+	if(binary) {
+		int err=0;
+		snprintf(tempfilename, TMPFILE_LEN, "%s%s", dirname(strdup(binary)), TMPFILE_TEMPLATE);
+		tempfile = mkstemp(tempfilename);
+		if(tempfile == -1) {
+                        perror("Error - Could not create temporary file");
+                        exit(1);
+		}
+
+		if((fp=fdopen(tempfile,"wb"))==NULL){
+			perror("Error - Could not open temporary file");
+			unlink(tempfilename);
+			exit(1);
+		}
+
+		/* write the array values */
+		if(!fwrite(cmos_table, (int)(ct->size-1), 1, fp)) {
+        	        perror("Error - Could not write image file");
+        	        fclose(fp);
+			unlink(tempfilename);
+        	        exit(1);
+		}
+
+        	fclose(fp);
+		UNLINK_IF_NECESSARY(binary);
+		if (rename(tempfilename, binary)) {
+			fprintf(stderr, "Error - Could not write %s: ", binary);
+			perror(NULL);
+			unlink(tempfilename);
+			exit(1);
+		}
+	}
+
 	/* See if we also want to output a C header file */
 	if (header) {
 		struct cmos_option_table *hdr;
 		struct lb_record *ptr, *end;
 
-		strncpy(tempfilename, dirname(strdup(header)), TMPFILE_LEN);
-	        strncat(tempfilename, TMPFILE_TEMPLATE, TMPFILE_LEN);
+		snprintf(tempfilename, TMPFILE_LEN, "%s%s", dirname(strdup(header)), TMPFILE_TEMPLATE);
 		tempfile = mkstemp(tempfilename);
 		if(tempfile == -1) {
 			perror("Error - Could not create temporary file");