summaryrefslogtreecommitdiff
path: root/util/nvramtool/accessors
diff options
context:
space:
mode:
authorVikram Narayanan <vikram186@gmail.com>2012-04-14 15:25:13 +0530
committerPatrick Georgi <patrick@georgi-clan.de>2012-04-21 09:36:24 +0200
commita8111cf980e01e9f8706024f06c05840b5fc8bcf (patch)
treef08af47ce9c1efa72b08b4a44e64b2ddc866cc2d /util/nvramtool/accessors
parentc3fc4b933708a594d169e40a82d5f29f304d11c9 (diff)
downloadcoreboot-a8111cf980e01e9f8706024f06c05840b5fc8bcf.tar.xz
nvramtool: Unify nvramtool and build_opt_tbl
As cmos.layout parsing capabilities are already there in nvramtool, use those than using build_opt_tbl.c. Add binary and header file generation in nvramtool. Make appropriate changes to Makefile.inc. Change-Id: Iaf3f5d4f51451aeb33c92800a0c895045f2388cf Signed-off-by: Vikram Narayanan <vikram186@gmail.com> Reviewed-on: http://review.coreboot.org/898 Tested-by: build bot (Jenkins) Reviewed-by: Patrick Georgi <patrick@georgi-clan.de>
Diffstat (limited to 'util/nvramtool/accessors')
-rw-r--r--util/nvramtool/accessors/layout-bin.c133
-rw-r--r--util/nvramtool/accessors/layout-common.c75
-rw-r--r--util/nvramtool/accessors/layout-text.c57
-rw-r--r--util/nvramtool/accessors/layout-text.h3
4 files changed, 268 insertions, 0 deletions
diff --git a/util/nvramtool/accessors/layout-bin.c b/util/nvramtool/accessors/layout-bin.c
index cf79646453..b910e358b6 100644
--- a/util/nvramtool/accessors/layout-bin.c
+++ b/util/nvramtool/accessors/layout-bin.c
@@ -1,6 +1,12 @@
/*****************************************************************************\
* lbtable.c
*****************************************************************************
+ * Copyright (C) 2012, Vikram Narayanan
+ * Unified build_opt_tbl and nvramtool
+ * build_opt_tbl.c
+ * Copyright (C) 2003 Eric Biederman (ebiederm@xmission.com)
+ * Copyright (C) 2007-2010 coresystems GmbH
+ *
* Copyright (C) 2002-2005 The Regents of the University of California.
* Produced at the Lawrence Livermore National Laboratory.
* Written by Dave Peterson <dsp@llnl.gov> <dave_peterson@pobox.com>
@@ -40,6 +46,7 @@
#include "cmos_lowlevel.h"
#include "hexdump.h"
#include "cbfs.h"
+#include "layout-text.h"
static void process_cmos_table(void);
static void get_cmos_checksum_info(void);
@@ -61,6 +68,8 @@ static const struct lb_record *next_cmos_rec(const struct lb_record *last,
*/
static const struct cmos_option_table *cmos_table = NULL;
+#define ROUNDUP4(x) (x += (4 - (x % 4)))
+
void process_layout(void)
{
if ((cmos_table) == NULL) {
@@ -83,6 +92,130 @@ void get_layout_from_cbfs_file(void)
process_layout();
}
+int write_cmos_layout_bin(FILE *f)
+{
+ const cmos_entry_t *cmos_entry;
+ const cmos_enum_t *cmos_enum;
+ cmos_checksum_layout_t layout;
+ struct cmos_option_table table;
+ struct cmos_entries entry;
+ struct cmos_enums cenum;
+ struct cmos_checksum csum;
+ size_t sum = 0;
+ int len;
+
+ for (cmos_entry = first_cmos_entry(); cmos_entry != NULL;
+ cmos_entry = next_cmos_entry(cmos_entry)) {
+
+ if (cmos_entry == first_cmos_entry()) {
+ sum += sizeof(table);
+ table.header_length = sizeof(table);
+ table.tag = LB_TAG_CMOS_OPTION_TABLE;
+
+ if (fwrite((char *)&table, sizeof(table), 1, f) != 1) {
+ perror("Error writing image file");
+ goto err;
+ }
+ }
+
+ memset(&entry, 0, sizeof(entry));
+ entry.tag = LB_TAG_OPTION;
+ entry.config = cmos_entry->config;
+ entry.config_id = (uint32_t)cmos_entry->config_id;
+ entry.bit = cmos_entry->bit;
+ entry.length = cmos_entry->length;
+
+ if (!is_ident((char *)cmos_entry->name)) {
+ fprintf(stderr,
+ "Error - Name %s is an invalid identifier\n",
+ cmos_entry->name);
+ goto err;
+ }
+
+ memcpy(entry.name, cmos_entry->name, strlen(cmos_entry->name));
+ entry.name[strlen(cmos_entry->name)] = '\0';
+ len = strlen(cmos_entry->name) + 1;
+
+ if (len % 4)
+ ROUNDUP4(len);
+
+ entry.size = sizeof(entry) - CMOS_MAX_NAME_LENGTH + len;
+ sum += entry.size;
+ if (fwrite((char *)&entry, entry.size, 1, f) != 1) {
+ perror("Error writing image file");
+ goto err;
+ }
+ }
+
+ for (cmos_enum = first_cmos_enum();
+ cmos_enum != NULL; cmos_enum = next_cmos_enum(cmos_enum)) {
+ memset(&cenum, 0, sizeof(cenum));
+ cenum.tag = LB_TAG_OPTION_ENUM;
+ memcpy(cenum.text, cmos_enum->text, strlen(cmos_enum->text));
+ cenum.text[strlen(cmos_enum->text)] = '\0';
+ len = strlen((char *)cenum.text) + 1;
+
+ if (len % 4)
+ ROUNDUP4(len);
+
+ cenum.config_id = cmos_enum->config_id;
+ cenum.value = cmos_enum->value;
+ cenum.size = sizeof(cenum) - CMOS_MAX_TEXT_LENGTH + len;
+ sum += cenum.size;
+ if (fwrite((char *)&cenum, cenum.size, 1, f) != 1) {
+ perror("Error writing image file");
+ goto err;
+ }
+ }
+
+ layout.summed_area_start = cmos_checksum_start;
+ layout.summed_area_end = cmos_checksum_end;
+ layout.checksum_at = cmos_checksum_index;
+ checksum_layout_to_bits(&layout);
+
+ csum.tag = LB_TAG_OPTION_CHECKSUM;
+ csum.size = sizeof(csum);
+ csum.range_start = layout.summed_area_start;
+ csum.range_end = layout.summed_area_end;
+ csum.location = layout.checksum_at;
+ csum.type = CHECKSUM_PCBIOS;
+ sum += csum.size;
+
+ if (fwrite((char *)&csum, csum.size, 1, f) != 1) {
+ perror("Error writing image file");
+ goto err;
+ }
+
+ if (fseek(f, sizeof(table.tag), SEEK_SET) != 0) {
+ perror("Error while seeking");
+ goto err;
+ }
+
+ if (fwrite((char *)&sum, sizeof(table.tag), 1, f) != 1) {
+ perror("Error writing image file");
+ goto err;
+ }
+ return sum;
+
+err:
+ fclose(f);
+ exit(1);
+}
+
+void write_cmos_output_bin(const char *binary_filename)
+{
+ FILE *fp;
+
+ if ((fp = fopen(binary_filename, "wb")) == NULL) {
+ fprintf(stderr,
+ "%s: Can not open file %s for writing: "
+ "%s\n", prog_name, binary_filename, strerror(errno));
+ exit(1);
+ }
+ write_cmos_layout_bin(fp);
+ fclose(fp);
+}
+
/****************************************************************************
* get_layout_from_cmos_table
*
diff --git a/util/nvramtool/accessors/layout-common.c b/util/nvramtool/accessors/layout-common.c
new file mode 100644
index 0000000000..0a99d3b89c
--- /dev/null
+++ b/util/nvramtool/accessors/layout-common.c
@@ -0,0 +1,75 @@
+/*****************************************************************************\
+ * layout_common.c
+ *****************************************************************************
+ * Copyright (C) 2012, Vikram Narayanan
+ * Unified build_opt_tbl and nvramtool
+ * build_opt_tbl.c
+ * Copyright (C) 2003 Eric Biederman (ebiederm@xmission.com)
+ * Copyright (C) 2007-2010 coresystems GmbH
+ *
+ * This file is part of nvramtool, a utility for reading/writing coreboot
+ * parameters and displaying information from the coreboot table.
+ * For details, see http://coreboot.org/nvramtool.
+ *
+ * Please also read the file DISCLAIMER which is included in this software
+ * distribution.
+ *
+ * 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, dated June 1991.
+ *
+ * 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 terms and
+ * conditions of 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 <ctype.h>
+
+static int is_ident_nondigit(int c)
+{
+ int result;
+ switch(c) {
+ case 'A': case 'B': case 'C': case 'D':
+ case 'E': case 'F': case 'G': case 'H':
+ case 'I': case 'J': case 'K': case 'L':
+ case 'M': case 'N': case 'O': case 'P':
+ case 'Q': case 'R': case 'S': case 'T':
+ case 'U': case 'V': case 'W': case 'X':
+ case 'Y': case 'Z':
+ case 'a': case 'b': case 'c': case 'd':
+ case 'e': case 'f': case 'g': case 'h':
+ case 'i': case 'j': case 'k': case 'l':
+ case 'm': case 'n': case 'o': case 'p':
+ case 'q': case 'r': case 's': case 't':
+ case 'u': case 'v': case 'w': case 'x':
+ case 'y': case 'z':
+ case '_':
+ result = 1;
+ break;
+ default:
+ result = 0;
+ break;
+ }
+ return result;
+}
+
+int is_ident(char *str)
+{
+ int result;
+ int ch;
+ ch = *str;
+ result = 0;
+ if (is_ident_nondigit(ch)) {
+ do {
+ str++;
+ ch = *str;
+ } while(ch && (is_ident_nondigit(ch) || (isdigit(ch))));
+ result = (ch == '\0');
+ }
+ return result;
+}
diff --git a/util/nvramtool/accessors/layout-text.c b/util/nvramtool/accessors/layout-text.c
index cbe698c504..a06f560cee 100644
--- a/util/nvramtool/accessors/layout-text.c
+++ b/util/nvramtool/accessors/layout-text.c
@@ -1,6 +1,12 @@
/*****************************************************************************\
* layout-text.c
*****************************************************************************
+ * Copyright (C) 2012, Vikram Narayanan
+ * Unified build_opt_tbl and nvramtool
+ * build_opt_tbl.c
+ * Copyright (C) 2003 Eric Biederman (ebiederm@xmission.com)
+ * Copyright (C) 2007-2010 coresystems GmbH
+ *
* Copyright (C) 2002-2005 The Regents of the University of California.
* Produced at the Lawrence Livermore National Laboratory.
* Written by Dave Peterson <dsp@llnl.gov> <dave_peterson@pobox.com>.
@@ -218,6 +224,57 @@ void get_layout_from_file(void)
fclose(f);
}
+void write_cmos_layout_header(const char *header_filename)
+{
+ FILE *fp;
+ const cmos_entry_t *cmos_entry;
+ cmos_checksum_layout_t layout;
+
+ if ((fp = fopen(header_filename, "w+")) == NULL) {
+ fprintf(stderr,
+ "%s: Can't open file %s for writing: %s\n",
+ prog_name, header_filename, strerror(errno));
+ exit(1);
+ }
+
+ fprintf(fp, "/**\n * This is an autogenerated file. Do not EDIT.\n"
+ " * All changes made to this file will be lost.\n"
+ " * See mainboard's cmos.layout file.\n */\n"
+ "\n#ifndef __OPTION_TABLE_H\n"
+ "#define __OPTION_TABLE_H\n\n");
+
+ for (cmos_entry = first_cmos_entry(); cmos_entry != NULL;
+ cmos_entry = next_cmos_entry(cmos_entry)) {
+
+ if (!is_ident((char *)cmos_entry->name)) {
+ fprintf(stderr,
+ "Error - Name %s is an invalid identifier\n",
+ cmos_entry->name);
+ fclose(fp);
+ exit(1);
+ }
+
+ fprintf(fp, "#define CMOS_VSTART_%s\t%d\n",
+ cmos_entry->name, cmos_entry->bit);
+ fprintf(fp, "#define CMOS_VLEN_%s\t%d\n",
+ cmos_entry->name, cmos_entry->length);
+ }
+
+ layout.summed_area_start = cmos_checksum_start;
+ layout.summed_area_end = cmos_checksum_end;
+ layout.checksum_at = cmos_checksum_index;
+ checksum_layout_to_bits(&layout);
+
+ fprintf(fp, "\n#define LB_CKS_RANGE_START %d\n",
+ layout.summed_area_start / 8);
+ fprintf(fp, "#define LB_CKS_RANGE_END %d\n",
+ layout.summed_area_end / 8);
+ fprintf(fp, "#define LB_CKS_LOC %d\n",
+ layout.checksum_at / 8);
+ fprintf(fp, "\n#endif /* __OPTION_TABLE_H */\n");
+
+ fclose(fp);
+}
/****************************************************************************
* write_cmos_layout
*
diff --git a/util/nvramtool/accessors/layout-text.h b/util/nvramtool/accessors/layout-text.h
index 21997e9a46..85628f7e5e 100644
--- a/util/nvramtool/accessors/layout-text.h
+++ b/util/nvramtool/accessors/layout-text.h
@@ -37,5 +37,8 @@
void set_layout_filename(const char filename[]);
void get_layout_from_file(void);
void write_cmos_layout(FILE * f);
+void write_cmos_output_bin(const char *binary_filename);
+void write_cmos_layout_header(const char *header_filename);
+extern int is_ident(char *str);
#endif /* LAYOUT_FILE_H */