| 1 |
|
| 2 |
|
| 3 |
#include "common.h" |
| 4 |
#include "cmos_ops.h" |
| 5 |
#include "cmos_lowlevel.h" |
| 6 |
|
| 7 |
static int prepare_cmos_op_common(const cmos_entry_t * e); |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
static int prepare_cmos_op_common(const cmos_entry_t * e) |
| 15 |
{ |
| 16 |
int result; |
| 17 |
|
| 18 |
if (e->config == CMOS_ENTRY_RESERVED) |
| 19 |
|
| 20 |
return CMOS_OP_RESERVED; |
| 21 |
|
| 22 |
if ((result = verify_cmos_op(e->bit, e->length, e->config)) != OK) |
| 23 |
return result; |
| 24 |
|
| 25 |
assert(e->length > 0); |
| 26 |
return OK; |
| 27 |
} |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
int prepare_cmos_read(const cmos_entry_t * e) |
| 37 |
{ |
| 38 |
int result; |
| 39 |
|
| 40 |
if ((result = prepare_cmos_op_common(e)) != OK) |
| 41 |
return result; |
| 42 |
|
| 43 |
switch (e->config) { |
| 44 |
case CMOS_ENTRY_ENUM: |
| 45 |
case CMOS_ENTRY_HEX: |
| 46 |
case CMOS_ENTRY_STRING: |
| 47 |
break; |
| 48 |
|
| 49 |
default: |
| 50 |
BUG(); |
| 51 |
} |
| 52 |
|
| 53 |
return OK; |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
int prepare_cmos_write(const cmos_entry_t * e, const char value_str[], |
| 65 |
unsigned long long *value) |
| 66 |
{ |
| 67 |
const cmos_enum_t *q; |
| 68 |
unsigned long long out; |
| 69 |
const char *p; |
| 70 |
char *memory = NULL; |
| 71 |
int negative, result, found_one; |
| 72 |
|
| 73 |
if ((result = prepare_cmos_op_common(e)) != OK) |
| 74 |
return result; |
| 75 |
|
| 76 |
switch (e->config) { |
| 77 |
case CMOS_ENTRY_ENUM: |
| 78 |
|
| 79 |
for (q = first_cmos_enum_id(e->config_id), found_one = 0; |
| 80 |
q != NULL; q = next_cmos_enum_id(q)) { |
| 81 |
found_one = 1; |
| 82 |
|
| 83 |
if (!strncmp(q->text, value_str, CMOS_MAX_TEXT_LENGTH)) |
| 84 |
break; |
| 85 |
} |
| 86 |
|
| 87 |
if (!found_one) |
| 88 |
return CMOS_OP_NO_MATCHING_ENUM; |
| 89 |
|
| 90 |
if (q == NULL) |
| 91 |
return CMOS_OP_BAD_ENUM_VALUE; |
| 92 |
|
| 93 |
out = q->value; |
| 94 |
break; |
| 95 |
|
| 96 |
case CMOS_ENTRY_HEX: |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
for (p = value_str; isspace((int)(unsigned char)*p); p++) ; |
| 101 |
negative = (*p == '-'); |
| 102 |
|
| 103 |
out = strtoull(value_str, (char **)&p, 0); |
| 104 |
|
| 105 |
if (*p) |
| 106 |
return CMOS_OP_INVALID_INT; |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
if (negative) |
| 113 |
return CMOS_OP_NEGATIVE_INT; |
| 114 |
|
| 115 |
break; |
| 116 |
|
| 117 |
case CMOS_ENTRY_STRING: |
| 118 |
if (e->length < (8 * strlen(value_str))) |
| 119 |
return CMOS_OP_VALUE_TOO_WIDE; |
| 120 |
memory = malloc(e->length / 8); |
| 121 |
memset(memory, 0, e->length / 8); |
| 122 |
strcpy(memory, value_str); |
| 123 |
out = (unsigned long)memory; |
| 124 |
break; |
| 125 |
|
| 126 |
default: |
| 127 |
BUG(); |
| 128 |
} |
| 129 |
|
| 130 |
if ((e->length < (8 * sizeof(*value))) && (out >= (1ull << e->length))) { |
| 131 |
if (memory) free(memory); |
| 132 |
return CMOS_OP_VALUE_TOO_WIDE; |
| 133 |
} |
| 134 |
|
| 135 |
*value = out; |
| 136 |
return OK; |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
uint16_t cmos_checksum_read(void) |
| 146 |
{ |
| 147 |
uint16_t lo, hi; |
| 148 |
|
| 149 |
|
| 150 |
hi = cmos_read_byte(cmos_checksum_index); |
| 151 |
lo = cmos_read_byte(cmos_checksum_index + 1); |
| 152 |
return (hi << 8) + lo; |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
void cmos_checksum_write(uint16_t checksum) |
| 162 |
{ |
| 163 |
unsigned char lo, hi; |
| 164 |
|
| 165 |
|
| 166 |
hi = (unsigned char)(checksum >> 8); |
| 167 |
lo = (unsigned char)(checksum & 0x00ff); |
| 168 |
cmos_write_byte(cmos_checksum_index, hi); |
| 169 |
cmos_write_byte(cmos_checksum_index + 1, lo); |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
uint16_t cmos_checksum_compute(void) |
| 179 |
{ |
| 180 |
unsigned i, sum; |
| 181 |
|
| 182 |
sum = 0; |
| 183 |
|
| 184 |
for (i = cmos_checksum_start; i <= cmos_checksum_end; i++) |
| 185 |
sum += cmos_read_byte(i); |
| 186 |
|
| 187 |
return (uint16_t)(sum & 0xffff); |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
void cmos_checksum_verify(void) |
| 197 |
{ |
| 198 |
uint16_t computed, actual; |
| 199 |
|
| 200 |
set_iopl(3); |
| 201 |
computed = cmos_checksum_compute(); |
| 202 |
actual = cmos_checksum_read(); |
| 203 |
set_iopl(0); |
| 204 |
|
| 205 |
if (computed != actual) { |
| 206 |
fprintf(stderr, "%s: Warning: coreboot CMOS checksum is bad.\n", |
| 207 |
prog_name); |
| 208 |
fprintf(stderr, "Computed checksum: 0x%x. Stored checksum: 0x%x\n", |
| 209 |
computed, actual); |
| 210 |
exit(1); |
| 211 |
} |
| 212 |
} |