Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(200)

Side by Side Diff: host/arch/x86/lib/crossystem_arch.c

Issue 6780008: Refactor crossystem to move x86-specific implementation to its own file. (Closed) Base URL: ssh://gitrw.chromium.org:9222/vboot_reference.git@master
Patch Set: post-pull Created 9 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « host/arch/arm/lib/crossystem_arch.c ('k') | host/include/crossystem_arch.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /* Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
2 * Use of this source code is governed by a BSD-style license that can be
3 * found in the LICENSE file.
4 */
5
6 #include <stdio.h>
7 #include <string.h>
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <unistd.h>
11 #include <ctype.h>
12
13 #include "host_common.h"
14
15 #include "crossystem.h"
16 #include "crossystem_arch.h"
17 #include "utility.h"
18 #include "vboot_common.h"
19 #include "vboot_nvstorage.h"
20 #include "vboot_struct.h"
21
22
23 /* ACPI constants from Chrome OS Main Processor Firmware Spec */
24 /* Boot reasons from BINF.0, from early H2C firmware */
25 /* Unknown */
26 #define BINF0_UNKNOWN 0
27 /* Normal boot to Chrome OS */
28 #define BINF0_NORMAL 1
29 /* Developer mode boot (developer mode warning displayed) */
30 #define BINF0_DEVELOPER 2
31 /* Recovery initiated by user, using recovery button */
32 #define BINF0_RECOVERY_BUTTON 3
33 /* Recovery initiated by user pressing a key at developer mode warning
34 * screen */
35 #define BINF0_RECOVERY_DEV_SCREEN_KEY 4
36 /* Recovery caused by BIOS failed signature check (neither rewritable
37 * firmware was valid) */
38 #define BINF0_RECOVERY_RW_FW_BAD 5
39 /* Recovery caused by no OS kernel detected */
40 #define BINF0_RECOVERY_NO_OS 6
41 /* Recovery caused by OS kernel failed signature check */
42 #define BINF0_RECOVERY_BAD_OS 7
43 /* Recovery initiated by OS */
44 #define BINF0_RECOVERY_OS_INITIATED 8
45 /* OS-initiated S3 diagnostic path (debug mode boot) */
46 #define BINF0_S3_DIAGNOSTIC_PATH 9
47 /* S3 resume failed */
48 #define BINF0_S3_RESUME_FAILED 10
49 /* Recovery caused by TPM error */
50 #define BINF0_RECOVERY_TPM_ERROR 11
51 /* Firmware types from BINF.3 */
52 #define BINF3_RECOVERY 0
53 #define BINF3_NORMAL 1
54 #define BINF3_DEVELOPER 2
55 /* CHSW bitflags */
56 #define CHSW_RECOVERY_BOOT 0x00000002
57 #define CHSW_RECOVERY_EC_BOOT 0x00000004
58 #define CHSW_DEV_BOOT 0x00000020
59 #define CHSW_WP_BOOT 0x00000200
60 /* CMOS reboot field bitflags */
61 #define CMOSRF_RECOVERY 0x80
62 #define CMOSRF_DEBUG_RESET 0x40
63 #define CMOSRF_TRY_B 0x20
64 /* GPIO signal types */
65 #define GPIO_SIGNAL_TYPE_RECOVERY 1
66 #define GPIO_SIGNAL_TYPE_DEV 2
67 #define GPIO_SIGNAL_TYPE_WP 3
68
69 /* Base name for ACPI files */
70 #define ACPI_BASE_PATH "/sys/devices/platform/chromeos_acpi"
71 /* Paths for frequently used ACPI files */
72 #define ACPI_BINF_PATH ACPI_BASE_PATH "/BINF"
73 #define ACPI_CHNV_PATH ACPI_BASE_PATH "/CHNV"
74 #define ACPI_CHSW_PATH ACPI_BASE_PATH "/CHSW"
75 #define ACPI_FMAP_PATH ACPI_BASE_PATH "/FMAP"
76 #define ACPI_GPIO_PATH ACPI_BASE_PATH "/GPIO"
77 #define ACPI_VBNV_PATH ACPI_BASE_PATH "/VBNV"
78 #define ACPI_VDAT_PATH ACPI_BASE_PATH "/VDAT"
79
80 /* Base name for GPIO files */
81 #define GPIO_BASE_PATH "/sys/class/gpio"
82 #define GPIO_EXPORT_PATH GPIO_BASE_PATH "/export"
83
84 /* Filename for NVRAM file */
85 #define NVRAM_PATH "/dev/nvram"
86
87
88 int VbReadNvStorage(VbNvContext* vnc) {
89 FILE* f;
90 int offs;
91
92 /* Get the byte offset from VBNV */
93 offs = ReadFileInt(ACPI_VBNV_PATH ".0");
94 if (offs == -1)
95 return -1;
96 if (VBNV_BLOCK_SIZE > ReadFileInt(ACPI_VBNV_PATH ".1"))
97 return -1; /* NV storage block is too small */
98
99 f = fopen(NVRAM_PATH, "rb");
100 if (!f)
101 return -1;
102
103 if (0 != fseek(f, offs, SEEK_SET) ||
104 1 != fread(vnc->raw, VBNV_BLOCK_SIZE, 1, f)) {
105 fclose(f);
106 return -1;
107 }
108
109 fclose(f);
110 return 0;
111 }
112
113
114 int VbWriteNvStorage(VbNvContext* vnc) {
115 FILE* f;
116 int offs;
117
118 if (!vnc->raw_changed)
119 return 0; /* Nothing changed, so no need to write */
120
121 /* Get the byte offset from VBNV */
122 offs = ReadFileInt(ACPI_VBNV_PATH ".0");
123 if (offs == -1)
124 return -1;
125 if (VBNV_BLOCK_SIZE > ReadFileInt(ACPI_VBNV_PATH ".1"))
126 return -1; /* NV storage block is too small */
127
128 f = fopen(NVRAM_PATH, "w+b");
129 if (!f)
130 return -1;
131
132 if (0 != fseek(f, offs, SEEK_SET) ||
133 1 != fwrite(vnc->raw, VBNV_BLOCK_SIZE, 1, f)) {
134 fclose(f);
135 return -1;
136 }
137
138 fclose(f);
139 return 0;
140 }
141
142
143 /*
144 * Get buffer data from ACPI.
145 *
146 * Buffer data is expected to be represented by a file which is a text dump of
147 * the buffer, representing each byte by two hex numbers, space and newline
148 * separated.
149 *
150 * On success, stores the amount of data read in bytes to *buffer_size; on
151 * erros, sets *buffer_size=0.
152 *
153 * Input - ACPI file name to get data from.
154 *
155 * Output: a pointer to AcpiBuffer structure containing the binary
156 * representation of the data. The caller is responsible for
157 * deallocating the pointer, this will take care of both the structure
158 * and the buffer. Null in case of error.
159 */
160 static uint8_t* VbGetBuffer(const char* filename, int* buffer_size)
161 {
162 FILE* f = NULL;
163 char* file_buffer = NULL;
164 uint8_t* output_buffer = NULL;
165 uint8_t* return_value = NULL;
166
167 /* Assume error until proven otherwise */
168 if (buffer_size)
169 *buffer_size = 0;
170
171 do {
172 struct stat fs;
173 uint8_t* output_ptr;
174 int rv, i, real_size;
175 int parsed_size = 0;
176
177 rv = stat(filename, &fs);
178 if (rv || !S_ISREG(fs.st_mode))
179 break;
180
181 f = fopen(filename, "r");
182 if (!f)
183 break;
184
185 file_buffer = Malloc(fs.st_size + 1);
186 if (!file_buffer)
187 break;
188
189 real_size = fread(file_buffer, 1, fs.st_size, f);
190 if (!real_size)
191 break;
192 file_buffer[real_size] = '\0';
193
194 /* Each byte in the output will replace two characters and a space
195 * in the input, so the output size does not exceed input side/3
196 * (a little less if account for newline characters). */
197 output_buffer = Malloc(real_size/3);
198 if (!output_buffer)
199 break;
200 output_ptr = output_buffer;
201
202 /* process the file contents */
203 for (i = 0; i < real_size; i++) {
204 char* base, *end;
205
206 base = file_buffer + i;
207
208 if (!isxdigit(*base))
209 continue;
210
211 output_ptr[parsed_size++] = strtol(base, &end, 16) & 0xff;
212
213 if ((end - base) != 2)
214 /* Input file format error */
215 break;
216
217 i += 2; /* skip the second character and the following space */
218 }
219
220 if (i == real_size) {
221 /* all is well */
222 return_value = output_buffer;
223 output_buffer = NULL; /* prevent it from deallocating */
224 if (buffer_size)
225 *buffer_size = parsed_size;
226 }
227 } while(0);
228
229 /* wrap up */
230 if (f)
231 fclose(f);
232
233 if (file_buffer)
234 Free(file_buffer);
235
236 if (output_buffer)
237 Free(output_buffer);
238
239 return return_value;
240 }
241
242
243 VbSharedDataHeader* VbSharedDataRead(void) {
244
245 VbSharedDataHeader* sh;
246 int got_size = 0;
247
248 sh = (VbSharedDataHeader*)VbGetBuffer(ACPI_VDAT_PATH, &got_size);
249 if (!sh)
250 return NULL;
251 if (got_size < sizeof(VbSharedDataHeader)) {
252 Free(sh);
253 return NULL;
254 }
255 if (sh->data_size > got_size)
256 sh->data_size = got_size; /* Truncated read */
257
258 return sh;
259 }
260
261
262 /* Read the CMOS reboot field in NVRAM.
263 *
264 * Returns 0 if the mask is clear in the field, 1 if set, or -1 if error. */
265 static int VbGetCmosRebootField(uint8_t mask) {
266 FILE* f;
267 int chnv, nvbyte;
268
269 /* Get the byte offset from CHNV */
270 chnv = ReadFileInt(ACPI_CHNV_PATH);
271 if (chnv == -1)
272 return -1;
273
274 f = fopen(NVRAM_PATH, "rb");
275 if (!f)
276 return -1;
277
278 if (0 != fseek(f, chnv, SEEK_SET) || EOF == (nvbyte = fgetc(f))) {
279 fclose(f);
280 return -1;
281 }
282
283 fclose(f);
284 return (nvbyte & mask ? 1 : 0);
285 }
286
287
288 /* Write the CMOS reboot field in NVRAM.
289 *
290 * Sets (value=0) or clears (value!=0) the mask in the byte.
291 *
292 * Returns 0 if success, or -1 if error. */
293 static int VbSetCmosRebootField(uint8_t mask, int value) {
294 FILE* f;
295 int chnv, nvbyte;
296
297 /* Get the byte offset from CHNV */
298 chnv = ReadFileInt(ACPI_CHNV_PATH);
299 if (chnv == -1)
300 return -1;
301
302 f = fopen(NVRAM_PATH, "w+b");
303 if (!f)
304 return -1;
305
306 /* Read the current value */
307 if (0 != fseek(f, chnv, SEEK_SET) || EOF == (nvbyte = fgetc(f))) {
308 fclose(f);
309 return -1;
310 }
311
312 /* Set/clear the mask */
313 if (value)
314 nvbyte |= mask;
315 else
316 nvbyte &= ~mask;
317
318 /* Write the byte back */
319 if (0 != fseek(f, chnv, SEEK_SET) || EOF == (fputc(nvbyte, f))) {
320 fclose(f);
321 return -1;
322 }
323
324 /* Success */
325 fclose(f);
326 return 0;
327 }
328
329
330 /* Read the active main firmware type into the destination buffer.
331 * Passed the destination and its size. Returns the destination, or
332 * NULL if error. */
333 static const char* VbReadMainFwType(char* dest, int size) {
334
335 /* Try reading type from BINF.3 */
336 switch(ReadFileInt(ACPI_BINF_PATH ".3")) {
337 case BINF3_RECOVERY:
338 return StrCopy(dest, "recovery", size);
339 case BINF3_NORMAL:
340 return StrCopy(dest, "normal", size);
341 case BINF3_DEVELOPER:
342 return StrCopy(dest, "developer", size);
343 default:
344 break; /* Fall through to legacy handling */
345 }
346
347 /* Fall back to BINF.0 for legacy systems like Mario. */
348 switch(ReadFileInt(ACPI_BINF_PATH ".0")) {
349 case -1:
350 /* Both BINF.0 and BINF.3 are missing, so this isn't Chrome OS
351 * firmware. */
352 return StrCopy(dest, "nonchrome", size);
353 case BINF0_NORMAL:
354 return StrCopy(dest, "normal", size);
355 case BINF0_DEVELOPER:
356 return StrCopy(dest, "developer", size);
357 case BINF0_RECOVERY_BUTTON:
358 case BINF0_RECOVERY_DEV_SCREEN_KEY:
359 case BINF0_RECOVERY_RW_FW_BAD:
360 case BINF0_RECOVERY_NO_OS:
361 case BINF0_RECOVERY_BAD_OS:
362 case BINF0_RECOVERY_OS_INITIATED:
363 case BINF0_RECOVERY_TPM_ERROR:
364 /* Assorted flavors of recovery boot reason. */
365 return StrCopy(dest, "recovery", size);
366 default:
367 /* Other values don't map cleanly to firmware type. */
368 return NULL;
369 }
370 }
371
372
373 /* Read the recovery reason. Returns the reason code or -1 if error. */
374 static int VbGetRecoveryReason(void) {
375 int value;
376
377 /* Try reading type from BINF.4 */
378 value = ReadFileInt(ACPI_BINF_PATH ".4");
379 if (-1 != value)
380 return value;
381
382 /* Fall back to BINF.0 for legacy systems like Mario. */
383 switch(ReadFileInt(ACPI_BINF_PATH ".0")) {
384 case BINF0_NORMAL:
385 case BINF0_DEVELOPER:
386 return VBNV_RECOVERY_NOT_REQUESTED;
387 case BINF0_RECOVERY_BUTTON:
388 return VBNV_RECOVERY_RO_MANUAL;
389 case BINF0_RECOVERY_DEV_SCREEN_KEY:
390 return VBNV_RECOVERY_RW_DEV_SCREEN;
391 case BINF0_RECOVERY_RW_FW_BAD:
392 case BINF0_RECOVERY_NO_OS:
393 return VBNV_RECOVERY_RW_NO_OS;
394 case BINF0_RECOVERY_BAD_OS:
395 return VBNV_RECOVERY_RW_INVALID_OS;
396 case BINF0_RECOVERY_OS_INITIATED:
397 return VBNV_RECOVERY_LEGACY;
398 default:
399 /* Other values don't map cleanly to firmware type. */
400 return -1;
401 }
402 }
403
404
405 /* Read a GPIO of the specified signal type (see ACPI GPIO SignalType).
406 *
407 * Returns 1 if the signal is asserted, 0 if not asserted, or -1 if error. */
408 static int ReadGpio(int signal_type) {
409 char name[128];
410 int index = 0;
411 int gpio_type;
412 int active_high;
413 int controller_offset;
414 char controller_name[128];
415 int value;
416
417 /* Scan GPIO.* to find a matching signal type */
418 for (index = 0; ; index++) {
419 snprintf(name, sizeof(name), "%s.%d/GPIO.0", ACPI_GPIO_PATH, index);
420 gpio_type = ReadFileInt(name);
421 if (gpio_type == signal_type)
422 break;
423 else if (gpio_type == -1)
424 return -1; /* Ran out of GPIOs before finding a match */
425 }
426
427 /* Read attributes and controller info for the GPIO */
428 snprintf(name, sizeof(name), "%s.%d/GPIO.1", ACPI_GPIO_PATH, index);
429 active_high = ReadFileBit(name, 0x00000001);
430 snprintf(name, sizeof(name), "%s.%d/GPIO.2", ACPI_GPIO_PATH, index);
431 controller_offset = ReadFileInt(name);
432 if (active_high == -1 || controller_offset == -1)
433 return -1; /* Missing needed info */
434
435 /* We only support the NM10 for now */
436 snprintf(name, sizeof(name), "%s.%d/GPIO.3", ACPI_GPIO_PATH, index);
437 if (!ReadFileString(controller_name, sizeof(controller_name), name))
438 return -1;
439 if (0 != strcmp(controller_name, "NM10"))
440 return -1;
441
442 /* Assume the NM10 has offset 192 */
443 /* TODO: should really check gpiochipNNN/label to see if it's the
444 * address we expect for the NM10, and then read the offset from
445 * gpiochipNNN/base. */
446 controller_offset += 192;
447
448 /* Try reading the GPIO value */
449 snprintf(name, sizeof(name), "%s/gpio%d/value",
450 GPIO_BASE_PATH, controller_offset);
451 value = ReadFileInt(name);
452
453 if (value == -1) {
454 /* Try exporting the GPIO */
455 FILE* f = fopen(GPIO_EXPORT_PATH, "wt");
456 if (!f)
457 return -1;
458 fprintf(f, "%d", controller_offset);
459 fclose(f);
460
461 /* Try re-reading the GPIO value */
462 value = ReadFileInt(name);
463 }
464
465 if (value == -1)
466 return -1;
467
468 /* Compare the GPIO value with the active value and return 1 if match. */
469 return (value == active_high ? 1 : 0);
470 }
471
472
473 int VbGetArchPropertyInt(const char* name) {
474 int value = -1;
475
476 /* Values from ACPI */
477 if (!strcasecmp(name,"recovery_reason")) {
478 value = VbGetRecoveryReason();
479 } else if (!strcasecmp(name,"fmap_base")) {
480 value = ReadFileInt(ACPI_FMAP_PATH);
481 }
482 /* Switch positions */
483 else if (!strcasecmp(name,"devsw_cur")) {
484 value = ReadGpio(GPIO_SIGNAL_TYPE_DEV);
485 } else if (!strcasecmp(name,"recoverysw_cur")) {
486 value = ReadGpio(GPIO_SIGNAL_TYPE_RECOVERY);
487 } else if (!strcasecmp(name,"wpsw_cur")) {
488 value = ReadGpio(GPIO_SIGNAL_TYPE_WP);
489 if (-1 != value && FwidStartsWith("Mario."))
490 value = 1 - value; /* Mario reports this backwards */
491 } else if (!strcasecmp(name,"devsw_boot")) {
492 value = ReadFileBit(ACPI_CHSW_PATH, CHSW_DEV_BOOT);
493 } else if (!strcasecmp(name,"recoverysw_boot")) {
494 value = ReadFileBit(ACPI_CHSW_PATH, CHSW_RECOVERY_BOOT);
495 } else if (!strcasecmp(name,"recoverysw_ec_boot")) {
496 value = ReadFileBit(ACPI_CHSW_PATH, CHSW_RECOVERY_EC_BOOT);
497 } else if (!strcasecmp(name,"wpsw_boot")) {
498 value = ReadFileBit(ACPI_CHSW_PATH, CHSW_WP_BOOT);
499 if (-1 != value && FwidStartsWith("Mario."))
500 value = 1 - value; /* Mario reports this backwards */
501 }
502
503 /* Saved memory is at a fixed location for all H2C BIOS. If the CHSW
504 * path exists in sysfs, it's a H2C BIOS. */
505 else if (!strcasecmp(name,"savedmem_base")) {
506 return (-1 == ReadFileInt(ACPI_CHSW_PATH) ? -1 : 0x00F00000);
507 } else if (!strcasecmp(name,"savedmem_size")) {
508 return (-1 == ReadFileInt(ACPI_CHSW_PATH) ? -1 : 0x00100000);
509 }
510 /* NV storage values. If unable to get from NV storage, fall back to the
511 * CMOS reboot field used by older BIOS. */
512 else if (!strcasecmp(name,"recovery_request")) {
513 value = VbGetNvStorage(VBNV_RECOVERY_REQUEST);
514 if (-1 == value)
515 value = VbGetCmosRebootField(CMOSRF_RECOVERY);
516 } else if (!strcasecmp(name,"dbg_reset")) {
517 value = VbGetNvStorage(VBNV_DEBUG_RESET_MODE);
518 if (-1 == value)
519 value = VbGetCmosRebootField(CMOSRF_DEBUG_RESET);
520 } else if (!strcasecmp(name,"fwb_tries")) {
521 value = VbGetNvStorage(VBNV_TRY_B_COUNT);
522 if (-1 == value)
523 value = VbGetCmosRebootField(CMOSRF_TRY_B);
524 }
525
526 return value;
527 }
528
529
530 const char* VbGetArchPropertyString(const char* name, char* dest, int size) {
531
532 if (!strcasecmp(name,"hwid")) {
533 return ReadFileString(dest, size, ACPI_BASE_PATH "/HWID");
534 } else if (!strcasecmp(name,"fwid")) {
535 return ReadFileString(dest, size, ACPI_BASE_PATH "/FWID");
536 } else if (!strcasecmp(name,"ro_fwid")) {
537 return ReadFileString(dest, size, ACPI_BASE_PATH "/FRID");
538 } else if (!strcasecmp(name,"mainfw_act")) {
539 switch(ReadFileInt(ACPI_BINF_PATH ".1")) {
540 case 0:
541 return StrCopy(dest, "recovery", size);
542 case 1:
543 return StrCopy(dest, "A", size);
544 case 2:
545 return StrCopy(dest, "B", size);
546 default:
547 return NULL;
548 }
549 } else if (!strcasecmp(name,"mainfw_type")) {
550 return VbReadMainFwType(dest, size);
551 } else if (!strcasecmp(name,"ecfw_act")) {
552 switch(ReadFileInt(ACPI_BINF_PATH ".2")) {
553 case 0:
554 return StrCopy(dest, "RO", size);
555 case 1:
556 return StrCopy(dest, "RW", size);
557 default:
558 return NULL;
559 }
560 }
561
562 return NULL;
563 }
564
565
566 int VbSetArchPropertyInt(const char* name, int value) {
567 /* NV storage values. If unable to get from NV storage, fall back to the
568 * CMOS reboot field used by older BIOS. */
569 if (!strcasecmp(name,"recovery_request")) {
570 if (0 == VbSetNvStorage(VBNV_RECOVERY_REQUEST, value))
571 return 0;
572 return VbSetCmosRebootField(CMOSRF_RECOVERY, value);
573 } else if (!strcasecmp(name,"dbg_reset")) {
574 if (0 == VbSetNvStorage(VBNV_DEBUG_RESET_MODE, value))
575 return 0;
576 return VbSetCmosRebootField(CMOSRF_DEBUG_RESET, value);
577 } else if (!strcasecmp(name,"fwb_tries")) {
578 if (0 == VbSetNvStorage(VBNV_TRY_B_COUNT, value))
579 return 0;
580 return VbSetCmosRebootField(CMOSRF_TRY_B, value);
581 }
582
583 return -1;
584 }
585
586
587 int VbSetArchPropertyString(const char* name, const char* value) {
588 /* If there were settable architecture-dependent string properties,
589 * they'd be here. */
590 return -1;
591 }
OLDNEW
« no previous file with comments | « host/arch/arm/lib/crossystem_arch.c ('k') | host/include/crossystem_arch.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698