Stefan Reinauer | 95a6e1c | 2008-08-07 10:21:05 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the libpayload project. |
| 3 | * |
| 4 | * Copyright (C) 2008 coresystems GmbH |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions |
| 8 | * are met: |
| 9 | * 1. Redistributions of source code must retain the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer. |
| 11 | * 2. Redistributions in binary form must reproduce the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer in the |
| 13 | * documentation and/or other materials provided with the distribution. |
| 14 | * 3. The name of the author may not be used to endorse or promote products |
| 15 | * derived from this software without specific prior written permission. |
| 16 | * |
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 27 | * SUCH DAMAGE. |
| 28 | */ |
| 29 | |
| 30 | #include <libpayload.h> |
| 31 | #include <coreboot_tables.h> |
| 32 | |
Patrick Georgi | 5febb00 | 2012-02-02 15:51:29 +0100 | [diff] [blame] | 33 | u8 *mem_accessor_base; |
| 34 | |
| 35 | static u8 mem_read(u8 reg) |
| 36 | { |
| 37 | return mem_accessor_base[reg]; |
| 38 | } |
| 39 | |
| 40 | static void mem_write(u8 val, u8 reg) |
| 41 | { |
| 42 | mem_accessor_base[reg] = val; |
| 43 | } |
| 44 | |
Patrick Georgi | 317ca0d | 2012-01-16 10:14:24 +0100 | [diff] [blame] | 45 | struct nvram_accessor *use_nvram = &(struct nvram_accessor) { |
| 46 | nvram_read, |
| 47 | nvram_write |
| 48 | }; |
| 49 | |
Patrick Georgi | 5febb00 | 2012-02-02 15:51:29 +0100 | [diff] [blame] | 50 | struct nvram_accessor *use_mem = &(struct nvram_accessor) { |
| 51 | mem_read, |
| 52 | mem_write |
| 53 | }; |
| 54 | |
Patrick Georgi | 317ca0d | 2012-01-16 10:14:24 +0100 | [diff] [blame] | 55 | struct cb_cmos_option_table *get_system_option_table(void) |
| 56 | { |
Nico Huber | 425973c | 2012-11-13 17:11:01 +0100 | [diff] [blame] | 57 | return lib_sysinfo.option_table; |
Patrick Georgi | 317ca0d | 2012-01-16 10:14:24 +0100 | [diff] [blame] | 58 | } |
| 59 | |
Patrick Georgi | 56f468d | 2012-01-16 15:03:11 +0100 | [diff] [blame] | 60 | int options_checksum_valid(const struct nvram_accessor *nvram) |
Stefan Reinauer | 95a6e1c | 2008-08-07 10:21:05 +0000 | [diff] [blame] | 61 | { |
| 62 | int i; |
Stefan Reinauer | 71c006f | 2008-09-26 18:39:06 +0000 | [diff] [blame] | 63 | int range_start = lib_sysinfo.cmos_range_start / 8; |
| 64 | int range_end = lib_sysinfo.cmos_range_end / 8; |
| 65 | int checksum_location = lib_sysinfo.cmos_checksum_location / 8; |
Stefan Reinauer | 95a6e1c | 2008-08-07 10:21:05 +0000 | [diff] [blame] | 66 | u16 checksum = 0, checksum_old; |
| 67 | |
| 68 | for(i = range_start; i <= range_end; i++) { |
Patrick Georgi | 317ca0d | 2012-01-16 10:14:24 +0100 | [diff] [blame] | 69 | checksum += nvram->read(i); |
Stefan Reinauer | 95a6e1c | 2008-08-07 10:21:05 +0000 | [diff] [blame] | 70 | } |
Stefan Reinauer | 95a6e1c | 2008-08-07 10:21:05 +0000 | [diff] [blame] | 71 | |
Patrick Georgi | 317ca0d | 2012-01-16 10:14:24 +0100 | [diff] [blame] | 72 | checksum_old = ((nvram->read(checksum_location)<<8) | nvram->read(checksum_location+1)); |
Stefan Reinauer | 95a6e1c | 2008-08-07 10:21:05 +0000 | [diff] [blame] | 73 | |
| 74 | return (checksum_old == checksum); |
| 75 | } |
| 76 | |
Patrick Georgi | 317ca0d | 2012-01-16 10:14:24 +0100 | [diff] [blame] | 77 | void fix_options_checksum_with(const struct nvram_accessor *nvram) |
Stefan Reinauer | 59a1b7f | 2010-08-17 10:14:50 +0000 | [diff] [blame] | 78 | { |
| 79 | int i; |
| 80 | int range_start = lib_sysinfo.cmos_range_start / 8; |
| 81 | int range_end = lib_sysinfo.cmos_range_end / 8; |
| 82 | int checksum_location = lib_sysinfo.cmos_checksum_location / 8; |
| 83 | u16 checksum = 0; |
| 84 | |
| 85 | for(i = range_start; i <= range_end; i++) { |
Patrick Georgi | 317ca0d | 2012-01-16 10:14:24 +0100 | [diff] [blame] | 86 | checksum += nvram->read(i); |
Stefan Reinauer | 59a1b7f | 2010-08-17 10:14:50 +0000 | [diff] [blame] | 87 | } |
Stefan Reinauer | 59a1b7f | 2010-08-17 10:14:50 +0000 | [diff] [blame] | 88 | |
Patrick Georgi | 317ca0d | 2012-01-16 10:14:24 +0100 | [diff] [blame] | 89 | nvram->write((checksum >> 8), checksum_location); |
| 90 | nvram->write((checksum & 0xff), checksum_location + 1); |
Stefan Reinauer | 59a1b7f | 2010-08-17 10:14:50 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Patrick Georgi | 317ca0d | 2012-01-16 10:14:24 +0100 | [diff] [blame] | 93 | void fix_options_checksum(void) |
| 94 | { |
| 95 | fix_options_checksum_with(use_nvram); |
| 96 | } |
| 97 | |
| 98 | static int get_cmos_value(const struct nvram_accessor *nvram, u32 bitnum, u32 len, void *valptr) |
Stefan Reinauer | 95a6e1c | 2008-08-07 10:21:05 +0000 | [diff] [blame] | 99 | { |
Mathias Krause | d6a6eef | 2012-02-17 12:02:47 +0100 | [diff] [blame] | 100 | u8 *value = valptr; |
Stefan Reinauer | 95a6e1c | 2008-08-07 10:21:05 +0000 | [diff] [blame] | 101 | int offs = 0; |
| 102 | u32 addr, bit; |
| 103 | u8 reg8; |
| 104 | |
Stefan Reinauer | 95a6e1c | 2008-08-07 10:21:05 +0000 | [diff] [blame] | 105 | /* Convert to byte borders */ |
| 106 | addr=(bitnum / 8); |
| 107 | bit=(bitnum % 8); |
| 108 | |
| 109 | /* Handle single byte or less */ |
| 110 | if(len <= 8) { |
Patrick Georgi | 317ca0d | 2012-01-16 10:14:24 +0100 | [diff] [blame] | 111 | reg8 = nvram->read(addr); |
Stefan Reinauer | 95a6e1c | 2008-08-07 10:21:05 +0000 | [diff] [blame] | 112 | reg8 >>= bit; |
| 113 | value[0] = reg8 & ((1 << len) -1); |
| 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | /* When handling more than a byte, copy whole bytes */ |
| 118 | while (len > 0) { |
| 119 | len -= 8; |
Patrick Georgi | 317ca0d | 2012-01-16 10:14:24 +0100 | [diff] [blame] | 120 | value[offs++]=nvram->read(addr++); |
Stefan Reinauer | 95a6e1c | 2008-08-07 10:21:05 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | return 0; |
| 124 | } |
| 125 | |
Mathias Krause | d6a6eef | 2012-02-17 12:02:47 +0100 | [diff] [blame] | 126 | static int set_cmos_value(const struct nvram_accessor *nvram, u32 bitnum, u32 len, const void *valptr) |
Patrick Georgi | dbde809 | 2011-11-22 13:07:45 +0100 | [diff] [blame] | 127 | { |
Mathias Krause | d6a6eef | 2012-02-17 12:02:47 +0100 | [diff] [blame] | 128 | const u8 *value = valptr; |
Patrick Georgi | dbde809 | 2011-11-22 13:07:45 +0100 | [diff] [blame] | 129 | int offs = 0; |
| 130 | u32 addr, bit; |
| 131 | u8 reg8; |
| 132 | |
| 133 | /* Convert to byte borders */ |
| 134 | addr=(bitnum / 8); |
| 135 | bit=(bitnum % 8); |
| 136 | |
| 137 | /* Handle single byte or less */ |
| 138 | if (len <= 8) { |
Patrick Georgi | 317ca0d | 2012-01-16 10:14:24 +0100 | [diff] [blame] | 139 | reg8 = nvram->read(addr); |
Patrick Georgi | dbde809 | 2011-11-22 13:07:45 +0100 | [diff] [blame] | 140 | reg8 &= ~(((1 << len) - 1) << bit); |
| 141 | reg8 |= (value[0] & ((1 << len) - 1)) << bit; |
Patrick Georgi | 317ca0d | 2012-01-16 10:14:24 +0100 | [diff] [blame] | 142 | nvram->write(reg8, addr); |
Patrick Georgi | dbde809 | 2011-11-22 13:07:45 +0100 | [diff] [blame] | 143 | return 0; |
| 144 | } |
| 145 | |
| 146 | /* When handling more than a byte, copy whole bytes */ |
| 147 | while (len > 0) { |
| 148 | len -= 8; |
Patrick Georgi | 317ca0d | 2012-01-16 10:14:24 +0100 | [diff] [blame] | 149 | nvram->write(value[offs++], addr++); |
Patrick Georgi | dbde809 | 2011-11-22 13:07:45 +0100 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | return 0; |
| 153 | } |
| 154 | |
Mathias Krause | d6a6eef | 2012-02-17 12:02:47 +0100 | [diff] [blame] | 155 | static struct cb_cmos_entries *lookup_cmos_entry(struct cb_cmos_option_table *option_table, const char *name) |
Stefan Reinauer | 95a6e1c | 2008-08-07 10:21:05 +0000 | [diff] [blame] | 156 | { |
Stefan Reinauer | 95a6e1c | 2008-08-07 10:21:05 +0000 | [diff] [blame] | 157 | struct cb_cmos_entries *cmos_entry; |
Gabe Black | d94512e | 2012-10-01 18:05:50 -0700 | [diff] [blame] | 158 | int len = name ? strnlen(name, CB_CMOS_MAX_NAME_LENGTH) : 0; |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 159 | |
Stefan Reinauer | 95a6e1c | 2008-08-07 10:21:05 +0000 | [diff] [blame] | 160 | /* cmos entries are located right after the option table */ |
Patrick Georgi | d357e62 | 2012-09-24 20:47:03 +0200 | [diff] [blame] | 161 | cmos_entry = first_cmos_entry(option_table); |
| 162 | while (cmos_entry) { |
| 163 | if (memcmp((const char*)cmos_entry->name, name, len) == 0) |
| 164 | return cmos_entry; |
| 165 | cmos_entry = next_cmos_entry(cmos_entry); |
Patrick Georgi | dbde809 | 2011-11-22 13:07:45 +0100 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | printf("ERROR: No such CMOS option (%s)\n", name); |
| 169 | return NULL; |
| 170 | } |
| 171 | |
Patrick Georgi | 0a59511 | 2012-01-16 15:39:57 +0100 | [diff] [blame] | 172 | struct cb_cmos_entries *first_cmos_entry(struct cb_cmos_option_table *option_table) |
| 173 | { |
Patrick Georgi | d357e62 | 2012-09-24 20:47:03 +0200 | [diff] [blame] | 174 | return (struct cb_cmos_entries*)((unsigned char *)option_table + option_table->header_length); |
Patrick Georgi | 0a59511 | 2012-01-16 15:39:57 +0100 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | struct cb_cmos_entries *next_cmos_entry(struct cb_cmos_entries *cmos_entry) |
| 178 | { |
| 179 | struct cb_cmos_entries *next = (struct cb_cmos_entries*)((unsigned char *)cmos_entry + cmos_entry->size); |
| 180 | if (next->tag == CB_TAG_OPTION) |
| 181 | return next; |
| 182 | else |
| 183 | return NULL; |
| 184 | } |
| 185 | |
Patrick Georgi | 08ed5a8 | 2012-09-24 20:06:27 +0200 | [diff] [blame] | 186 | struct cb_cmos_enums *first_cmos_enum(struct cb_cmos_option_table *option_table) |
Patrick Georgi | da59f9a | 2012-01-16 13:47:33 +0100 | [diff] [blame] | 187 | { |
| 188 | struct cb_cmos_entries *cmos_entry; |
Patrick Georgi | da59f9a | 2012-01-16 13:47:33 +0100 | [diff] [blame] | 189 | /* cmos entries are located right after the option table. Skip them */ |
Patrick Georgi | 08ed5a8 | 2012-09-24 20:06:27 +0200 | [diff] [blame] | 190 | cmos_entry = (struct cb_cmos_entries *)((unsigned char *)option_table + option_table->header_length); |
Patrick Georgi | da59f9a | 2012-01-16 13:47:33 +0100 | [diff] [blame] | 191 | while (cmos_entry->tag == CB_TAG_OPTION) |
| 192 | cmos_entry = (struct cb_cmos_entries*)((unsigned char *)cmos_entry + cmos_entry->size); |
| 193 | |
| 194 | /* cmos enums are located after cmos entries. */ |
Patrick Georgi | 08ed5a8 | 2012-09-24 20:06:27 +0200 | [diff] [blame] | 195 | return (struct cb_cmos_enums *)cmos_entry; |
| 196 | } |
| 197 | |
| 198 | struct cb_cmos_enums *next_cmos_enum(struct cb_cmos_enums *cmos_enum) |
| 199 | { |
| 200 | if (!cmos_enum) { |
| 201 | return NULL; |
| 202 | } |
| 203 | |
| 204 | cmos_enum = (struct cb_cmos_enums*)((unsigned char *)cmos_enum + cmos_enum->size); |
| 205 | if (cmos_enum->tag == CB_TAG_OPTION_ENUM) { |
| 206 | return cmos_enum; |
| 207 | } else { |
| 208 | return NULL; |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | struct cb_cmos_enums *next_cmos_enum_of_id(struct cb_cmos_enums *cmos_enum, int id) |
| 213 | { |
| 214 | while ((cmos_enum = next_cmos_enum(cmos_enum))) { |
| 215 | if (cmos_enum->config_id == id) { |
| 216 | return cmos_enum; |
| 217 | } |
| 218 | } |
| 219 | return NULL; |
| 220 | } |
| 221 | |
| 222 | struct cb_cmos_enums *first_cmos_enum_of_id(struct cb_cmos_option_table *option_table, int id) |
| 223 | { |
| 224 | struct cb_cmos_enums *cmos_enum = first_cmos_enum(option_table); |
| 225 | if (!cmos_enum) { |
| 226 | return NULL; |
| 227 | } |
| 228 | if (cmos_enum->config_id == id) { |
| 229 | return cmos_enum; |
| 230 | } |
| 231 | |
| 232 | return next_cmos_enum_of_id(cmos_enum, id); |
| 233 | } |
| 234 | |
| 235 | /* Either value or text must be NULL. Returns the field that matches "the other" for a given config_id */ |
| 236 | static struct cb_cmos_enums *lookup_cmos_enum_core(struct cb_cmos_option_table *option_table, int config_id, const u8 *value, const char *text) |
| 237 | { |
Gabe Black | d94512e | 2012-10-01 18:05:50 -0700 | [diff] [blame] | 238 | int len = strnlen(text, CB_CMOS_MAX_TEXT_LENGTH); |
Patrick Georgi | 08ed5a8 | 2012-09-24 20:06:27 +0200 | [diff] [blame] | 239 | |
| 240 | /* cmos enums are located after cmos entries. */ |
Patrick Georgi | da59f9a | 2012-01-16 13:47:33 +0100 | [diff] [blame] | 241 | struct cb_cmos_enums *cmos_enum; |
Patrick Georgi | 08ed5a8 | 2012-09-24 20:06:27 +0200 | [diff] [blame] | 242 | for ( cmos_enum = first_cmos_enum_of_id(option_table, config_id); |
| 243 | cmos_enum; |
| 244 | cmos_enum = next_cmos_enum_of_id(cmos_enum, config_id)) { |
| 245 | if (((value == NULL) || (cmos_enum->value == *value)) && |
Nico Huber | 0227dec | 2012-11-09 18:12:21 +0100 | [diff] [blame] | 246 | ((text == NULL) || (memcmp((const char*)cmos_enum->text, text, len) == 0))) { |
Patrick Georgi | da59f9a | 2012-01-16 13:47:33 +0100 | [diff] [blame] | 247 | return cmos_enum; |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | return NULL; |
| 252 | } |
| 253 | |
Mathias Krause | d6a6eef | 2012-02-17 12:02:47 +0100 | [diff] [blame] | 254 | static struct cb_cmos_enums *lookup_cmos_enum_by_value(struct cb_cmos_option_table *option_table, int config_id, const u8 *value) |
Patrick Georgi | da59f9a | 2012-01-16 13:47:33 +0100 | [diff] [blame] | 255 | { |
| 256 | return lookup_cmos_enum_core(option_table, config_id, value, NULL); |
| 257 | } |
| 258 | |
Mathias Krause | d6a6eef | 2012-02-17 12:02:47 +0100 | [diff] [blame] | 259 | static struct cb_cmos_enums *lookup_cmos_enum_by_label(struct cb_cmos_option_table *option_table, int config_id, const char *label) |
Patrick Georgi | da59f9a | 2012-01-16 13:47:33 +0100 | [diff] [blame] | 260 | { |
| 261 | return lookup_cmos_enum_core(option_table, config_id, NULL, label); |
| 262 | } |
| 263 | |
Mathias Krause | d6a6eef | 2012-02-17 12:02:47 +0100 | [diff] [blame] | 264 | int get_option_with(const struct nvram_accessor *nvram, struct cb_cmos_option_table *option_table, void *dest, const char *name) |
Patrick Georgi | dbde809 | 2011-11-22 13:07:45 +0100 | [diff] [blame] | 265 | { |
| 266 | struct cb_cmos_entries *cmos_entry = lookup_cmos_entry(option_table, name); |
| 267 | if (cmos_entry) { |
Patrick Georgi | 317ca0d | 2012-01-16 10:14:24 +0100 | [diff] [blame] | 268 | if(get_cmos_value(nvram, cmos_entry->bit, cmos_entry->length, dest)) |
Stefan Reinauer | 95a6e1c | 2008-08-07 10:21:05 +0000 | [diff] [blame] | 269 | return 1; |
| 270 | |
Patrick Georgi | 317ca0d | 2012-01-16 10:14:24 +0100 | [diff] [blame] | 271 | if(!options_checksum_valid(nvram)) |
Stefan Reinauer | 95a6e1c | 2008-08-07 10:21:05 +0000 | [diff] [blame] | 272 | return 1; |
| 273 | |
| 274 | return 0; |
| 275 | } |
Stefan Reinauer | 95a6e1c | 2008-08-07 10:21:05 +0000 | [diff] [blame] | 276 | return 1; |
| 277 | } |
Patrick Georgi | c230058 | 2011-07-12 11:40:29 +0200 | [diff] [blame] | 278 | |
Mathias Krause | d6a6eef | 2012-02-17 12:02:47 +0100 | [diff] [blame] | 279 | int get_option_from(struct cb_cmos_option_table *option_table, void *dest, const char *name) |
Patrick Georgi | 317ca0d | 2012-01-16 10:14:24 +0100 | [diff] [blame] | 280 | { |
| 281 | return get_option_with(use_nvram, option_table, dest, name); |
| 282 | } |
| 283 | |
Mathias Krause | d6a6eef | 2012-02-17 12:02:47 +0100 | [diff] [blame] | 284 | int get_option(void *dest, const char *name) |
Patrick Georgi | c230058 | 2011-07-12 11:40:29 +0200 | [diff] [blame] | 285 | { |
Patrick Georgi | 317ca0d | 2012-01-16 10:14:24 +0100 | [diff] [blame] | 286 | return get_option_from(get_system_option_table(), dest, name); |
Patrick Georgi | c230058 | 2011-07-12 11:40:29 +0200 | [diff] [blame] | 287 | } |
Patrick Georgi | dbde809 | 2011-11-22 13:07:45 +0100 | [diff] [blame] | 288 | |
Mathias Krause | d6a6eef | 2012-02-17 12:02:47 +0100 | [diff] [blame] | 289 | int set_option_with(const struct nvram_accessor *nvram, struct cb_cmos_option_table *option_table, const void *value, const char *name) |
Patrick Georgi | dbde809 | 2011-11-22 13:07:45 +0100 | [diff] [blame] | 290 | { |
Patrick Georgi | dbde809 | 2011-11-22 13:07:45 +0100 | [diff] [blame] | 291 | struct cb_cmos_entries *cmos_entry = lookup_cmos_entry(option_table, name); |
| 292 | if (cmos_entry) { |
Patrick Georgi | 317ca0d | 2012-01-16 10:14:24 +0100 | [diff] [blame] | 293 | set_cmos_value(nvram, cmos_entry->bit, cmos_entry->length, value); |
| 294 | fix_options_checksum_with(nvram); |
Patrick Georgi | dbde809 | 2011-11-22 13:07:45 +0100 | [diff] [blame] | 295 | return 0; |
| 296 | } |
| 297 | return 1; |
| 298 | } |
| 299 | |
Mathias Krause | d6a6eef | 2012-02-17 12:02:47 +0100 | [diff] [blame] | 300 | int set_option(const void *value, const char *name) |
Patrick Georgi | 317ca0d | 2012-01-16 10:14:24 +0100 | [diff] [blame] | 301 | { |
| 302 | return set_option_with(use_nvram, get_system_option_table(), value, name); |
| 303 | } |
Patrick Georgi | da59f9a | 2012-01-16 13:47:33 +0100 | [diff] [blame] | 304 | |
Mathias Krause | d6a6eef | 2012-02-17 12:02:47 +0100 | [diff] [blame] | 305 | int get_option_as_string(const struct nvram_accessor *nvram, struct cb_cmos_option_table *option_table, char **dest, const char *name) |
Patrick Georgi | da59f9a | 2012-01-16 13:47:33 +0100 | [diff] [blame] | 306 | { |
| 307 | void *raw; |
| 308 | struct cb_cmos_entries *cmos_entry = lookup_cmos_entry(option_table, name); |
| 309 | if (!cmos_entry) |
| 310 | return 1; |
| 311 | int cmos_length = (cmos_entry->length+7)/8; |
| 312 | |
| 313 | /* extra byte to ensure 0-terminated strings */ |
| 314 | raw = malloc(cmos_length+1); |
| 315 | memset(raw, 0, cmos_length+1); |
| 316 | |
| 317 | int ret = get_option_with(nvram, option_table, raw, name); |
| 318 | |
| 319 | struct cb_cmos_enums *cmos_enum; |
| 320 | switch (cmos_entry->config) { |
| 321 | case 'h': |
| 322 | /* only works on little endian. |
| 323 | 26 bytes is enough for a 64bit value in decimal */ |
| 324 | *dest = malloc(26); |
Patrick Georgi | f78c700 | 2012-09-26 15:11:48 +0200 | [diff] [blame] | 325 | sprintf(*dest, "%llu", *(u64*)raw); |
Patrick Georgi | da59f9a | 2012-01-16 13:47:33 +0100 | [diff] [blame] | 326 | break; |
| 327 | case 's': |
| 328 | *dest = strdup(raw); |
| 329 | break; |
| 330 | case 'e': |
| 331 | cmos_enum = lookup_cmos_enum_by_value(option_table, cmos_entry->config_id, (u8*)raw); |
| 332 | *dest = strdup((const char*)cmos_enum->text); |
| 333 | break; |
| 334 | default: /* fail */ |
Mathias Krause | 9a4114a | 2012-02-08 10:31:42 +0100 | [diff] [blame] | 335 | ret = 1; |
Patrick Georgi | da59f9a | 2012-01-16 13:47:33 +0100 | [diff] [blame] | 336 | } |
| 337 | free(raw); |
| 338 | return ret; |
| 339 | } |
| 340 | |
Mathias Krause | d6a6eef | 2012-02-17 12:02:47 +0100 | [diff] [blame] | 341 | int set_option_from_string(const struct nvram_accessor *nvram, struct cb_cmos_option_table *option_table, const char *value, const char *name) |
Patrick Georgi | da59f9a | 2012-01-16 13:47:33 +0100 | [diff] [blame] | 342 | { |
| 343 | void *raw; |
| 344 | struct cb_cmos_entries *cmos_entry = lookup_cmos_entry(option_table, name); |
| 345 | if (!cmos_entry) |
| 346 | return 1; |
| 347 | |
| 348 | struct cb_cmos_enums *cmos_enum; |
| 349 | switch (cmos_entry->config) { |
| 350 | case 'h': |
| 351 | /* only works on little endian */ |
Mathias Krause | 50759ed | 2012-02-08 10:32:57 +0100 | [diff] [blame] | 352 | raw = malloc(sizeof(u64)); |
Patrick Georgi | da59f9a | 2012-01-16 13:47:33 +0100 | [diff] [blame] | 353 | *(u64*)raw = strtoull(value, NULL, 0); |
| 354 | break; |
| 355 | case 's': |
| 356 | raw = strdup(value); |
| 357 | break; |
| 358 | case 'e': |
| 359 | cmos_enum = lookup_cmos_enum_by_label(option_table, cmos_entry->config_id, value); |
| 360 | raw = malloc(sizeof(u32)); |
| 361 | *(u32*)raw = cmos_enum->value; |
| 362 | break; |
| 363 | default: /* fail */ |
| 364 | return 1; |
| 365 | } |
| 366 | |
| 367 | int ret = set_option_with(nvram, option_table, raw, name); |
| 368 | free(raw); |
| 369 | return ret; |
| 370 | } |