libpayload: Expose options_checksum_valid
[coreboot.git] / payloads / libpayload / drivers / options.c
index e3f084778cb00380c2b9a8c848365ca2023cd4b5..1b98cda7218baed968a4936db7bcc2e31ff645ea 100644 (file)
 #include <libpayload.h>
 #include <coreboot_tables.h>
 
-static int options_checksum_valid(void)
+u8 *mem_accessor_base;
+
+static u8 mem_read(u8 reg)
+{
+       return mem_accessor_base[reg];
+}
+
+static void mem_write(u8 val, u8 reg)
+{
+       mem_accessor_base[reg] = val;
+}
+
+struct nvram_accessor *use_nvram = &(struct nvram_accessor) {
+       nvram_read,
+       nvram_write
+};
+
+struct nvram_accessor *use_mem = &(struct nvram_accessor) {
+       mem_read,
+       mem_write
+};
+
+struct cb_cmos_option_table *get_system_option_table(void)
+{
+       return phys_to_virt(lib_sysinfo.option_table);
+}
+
+int options_checksum_valid(const struct nvram_accessor *nvram)
 {
        int i;
        int range_start = lib_sysinfo.cmos_range_start / 8;
@@ -39,15 +66,15 @@ static int options_checksum_valid(void)
        u16 checksum = 0, checksum_old;
 
        for(i = range_start; i <= range_end; i++) {
-               checksum += nvram_read(i);
+               checksum += nvram->read(i);
        }
 
-       checksum_old = ((nvram_read(checksum_location)<<8) | nvram_read(checksum_location+1));
+       checksum_old = ((nvram->read(checksum_location)<<8) | nvram->read(checksum_location+1));
 
        return (checksum_old == checksum);
 }
 
-void fix_options_checksum(void)
+void fix_options_checksum_with(const struct nvram_accessor *nvram)
 {
        int i;
        int range_start = lib_sysinfo.cmos_range_start / 8;
@@ -56,14 +83,19 @@ void fix_options_checksum(void)
        u16 checksum = 0;
 
        for(i = range_start; i <= range_end; i++) {
-               checksum += nvram_read(i);
+               checksum += nvram->read(i);
        }
 
-       nvram_write((checksum >> 8), checksum_location);
-       nvram_write((checksum & 0xff), checksum_location + 1);
+       nvram->write((checksum >> 8), checksum_location);
+       nvram->write((checksum & 0xff), checksum_location + 1);
 }
 
-static int get_cmos_value(u32 bitnum, u32 len, void *valptr)
+void fix_options_checksum(void)
+{
+       fix_options_checksum_with(use_nvram);
+}
+
+static int get_cmos_value(const struct nvram_accessor *nvram, u32 bitnum, u32 len, void *valptr)
 {
        u8 *value = (u8 *)valptr;
        int offs = 0;
@@ -76,7 +108,7 @@ static int get_cmos_value(u32 bitnum, u32 len, void *valptr)
 
        /* Handle single byte or less */
        if(len <= 8) {
-               reg8 = nvram_read(addr);
+               reg8 = nvram->read(addr);
                reg8 >>= bit;
                value[0] = reg8 & ((1 << len) -1);
                return 0;
@@ -85,13 +117,13 @@ static int get_cmos_value(u32 bitnum, u32 len, void *valptr)
        /* When handling more than a byte, copy whole bytes */
        while (len > 0) {
                len -= 8;
-               value[offs++]=nvram_read(addr++);
+               value[offs++]=nvram->read(addr++);
        }
 
        return 0;
 }
 
-static int set_cmos_value(u32 bitnum, u32 len, void *valptr)
+static int set_cmos_value(const struct nvram_accessor *nvram, u32 bitnum, u32 len, void *valptr)
 {
        u8 *value = (u8 *)valptr;
        int offs = 0;
@@ -104,17 +136,17 @@ static int set_cmos_value(u32 bitnum, u32 len, void *valptr)
 
        /* Handle single byte or less */
        if (len <= 8) {
-               reg8 = nvram_read(addr);
+               reg8 = nvram->read(addr);
                reg8 &= ~(((1 << len) - 1) << bit);
                reg8 |= (value[0] & ((1 << len) - 1)) << bit;
-               nvram_write(reg8, addr);
+               nvram->write(reg8, addr);
                return 0;
        }
 
        /* When handling more than a byte, copy whole bytes */
        while (len > 0) {
                len -= 8;
-               nvram_write(value[offs++], addr++);
+               nvram->write(value[offs++], addr++);
        }
 
        return 0;
@@ -139,14 +171,50 @@ static struct cb_cmos_entries *lookup_cmos_entry(struct cb_cmos_option_table *op
        return NULL;
 }
 
-int get_option_from(struct cb_cmos_option_table *option_table, void *dest, char *name)
+/* Either value or text must be NULL. Returns the field that matches "the other" for a given config_id */
+static struct cb_cmos_enums *lookup_cmos_enum_core(struct cb_cmos_option_table *option_table, int config_id, u8 *value, char *text)
+{
+       struct cb_cmos_entries *cmos_entry;
+       int len = strnlen(text, CMOS_MAX_TEXT_LENGTH);
+
+       /* cmos entries are located right after the option table. Skip them */
+       cmos_entry = (struct cb_cmos_entries*)((unsigned char *)option_table + option_table->header_length);
+       while (cmos_entry->tag == CB_TAG_OPTION)
+               cmos_entry = (struct cb_cmos_entries*)((unsigned char *)cmos_entry + cmos_entry->size);
+
+       /* cmos enums are located after cmos entries. */
+       struct cb_cmos_enums *cmos_enum;
+       for (   cmos_enum = (struct cb_cmos_enums*)cmos_entry;
+               cmos_enum->tag == CB_TAG_OPTION_ENUM;
+               cmos_enum = (struct cb_cmos_enums*)((unsigned char *)cmos_enum + cmos_enum->size)) {
+               if ((cmos_enum->config_id == config_id)
+                  && ((value == NULL) || (cmos_enum->value == *value))
+                  && ((text == NULL) || (memcmp((const char*)cmos_enum->text, text, len)))) {
+                       return cmos_enum;
+               }
+       }
+
+       return NULL;
+}
+
+static struct cb_cmos_enums *lookup_cmos_enum_by_value(struct cb_cmos_option_table *option_table, int config_id, u8 *value)
+{
+       return lookup_cmos_enum_core(option_table, config_id, value, NULL);
+}
+
+static struct cb_cmos_enums *lookup_cmos_enum_by_label(struct cb_cmos_option_table *option_table, int config_id, char *label)
+{
+       return lookup_cmos_enum_core(option_table, config_id, NULL, label);
+}
+
+int get_option_with(const struct nvram_accessor *nvram, struct cb_cmos_option_table *option_table, void *dest, char *name)
 {
        struct cb_cmos_entries *cmos_entry = lookup_cmos_entry(option_table, name);
        if (cmos_entry) {
-               if(get_cmos_value(cmos_entry->bit, cmos_entry->length, dest))
+               if(get_cmos_value(nvram, cmos_entry->bit, cmos_entry->length, dest))
                        return 1;
 
-               if(!options_checksum_valid())
+               if(!options_checksum_valid(nvram))
                        return 1;
 
                return 0;
@@ -154,21 +222,95 @@ int get_option_from(struct cb_cmos_option_table *option_table, void *dest, char
        return 1;
 }
 
+int get_option_from(struct cb_cmos_option_table *option_table, void *dest, char *name)
+{
+       return get_option_with(use_nvram, option_table, dest, name);
+}
+
 int get_option(void *dest, char *name)
 {
-       struct cb_cmos_option_table *option_table = phys_to_virt(lib_sysinfo.option_table);
-       return get_option_from(option_table, dest, name);
+       return get_option_from(get_system_option_table(), dest, name);
 }
 
-int set_option(void *value, char *name)
+int set_option_with(const struct nvram_accessor *nvram, struct cb_cmos_option_table *option_table, void *value, char *name)
 {
-       struct cb_cmos_option_table *option_table = phys_to_virt(lib_sysinfo.option_table);
        struct cb_cmos_entries *cmos_entry = lookup_cmos_entry(option_table, name);
        if (cmos_entry) {
-               set_cmos_value(cmos_entry->bit, cmos_entry->length, value);
-               fix_options_checksum();
+               set_cmos_value(nvram, cmos_entry->bit, cmos_entry->length, value);
+               fix_options_checksum_with(nvram);
                return 0;
        }
        return 1;
 }
 
+int set_option(void *value, char *name)
+{
+       return set_option_with(use_nvram, get_system_option_table(), value, name);
+}
+
+int get_option_as_string(const struct nvram_accessor *nvram, struct cb_cmos_option_table *option_table, char **dest, char *name)
+{
+       void *raw;
+       struct cb_cmos_entries *cmos_entry = lookup_cmos_entry(option_table, name);
+       if (!cmos_entry)
+               return 1;
+       int cmos_length = (cmos_entry->length+7)/8;
+
+       /* extra byte to ensure 0-terminated strings */
+       raw = malloc(cmos_length+1);
+       memset(raw, 0, cmos_length+1);
+
+       int ret = get_option_with(nvram, option_table, raw, name);
+
+       struct cb_cmos_enums *cmos_enum;
+       switch (cmos_entry->config) {
+               case 'h':
+                       /* only works on little endian.
+                          26 bytes is enough for a 64bit value in decimal */
+                       *dest = malloc(26);
+                       sprintf(*dest, "%ull", *(u64*)raw);
+                       break;
+               case 's':
+                       *dest = strdup(raw);
+                       break;
+               case 'e':
+                       cmos_enum = lookup_cmos_enum_by_value(option_table, cmos_entry->config_id, (u8*)raw);
+                       *dest = strdup((const char*)cmos_enum->text);
+                       break;
+               default: /* fail */
+                       return 1;
+       }
+       free(raw);
+       return ret;
+}
+
+int set_option_from_string(const struct nvram_accessor *nvram, struct cb_cmos_option_table *option_table, char *value, char *name)
+{
+       void *raw;
+       struct cb_cmos_entries *cmos_entry = lookup_cmos_entry(option_table, name);
+       if (!cmos_entry)
+               return 1;
+
+       struct cb_cmos_enums *cmos_enum;
+       switch (cmos_entry->config) {
+               case 'h':
+                       /* only works on little endian */
+                       raw = malloc(8);
+                       *(u64*)raw = strtoull(value, NULL, 0);
+                       break;
+               case 's':
+                       raw = strdup(value);
+                       break;
+               case 'e':
+                       cmos_enum = lookup_cmos_enum_by_label(option_table, cmos_entry->config_id, value);
+                       raw = malloc(sizeof(u32));
+                       *(u32*)raw = cmos_enum->value;
+                       break;
+               default: /* fail */
+                       return 1;
+       }
+
+       int ret = set_option_with(nvram, option_table, raw, name);
+       free(raw);
+       return ret;
+}