blob: 6ba67e6460800ea0e84f30ae4a56a05e7664f91a [file] [log] [blame]
Randall Spangler54218662011-02-07 11:20:20 -08001/* 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>
Vadim Bendebury20084232011-03-15 09:29:48 -07008#include <sys/types.h>
9#include <sys/stat.h>
10#include <unistd.h>
11#include <ctype.h>
Randall Spangler54218662011-02-07 11:20:20 -080012
13#include "host_common.h"
14
15#include "crossystem.h"
16#include "utility.h"
17#include "vboot_common.h"
Randall Spanglere73302c2011-02-18 14:53:01 -080018#include "vboot_nvstorage.h"
Randall Spanglerf4ba19d2011-03-17 16:10:21 -070019#include "vboot_struct.h"
Randall Spangler54218662011-02-07 11:20:20 -080020
21/* ACPI constants from Chrome OS Main Processor Firmware Spec */
22/* GPIO signal types */
23#define GPIO_SIGNAL_TYPE_RECOVERY 1
24#define GPIO_SIGNAL_TYPE_DEV 2
25#define GPIO_SIGNAL_TYPE_WP 3
26/* CHSW bitflags */
27#define CHSW_RECOVERY_BOOT 0x00000002
28#define CHSW_RECOVERY_EC_BOOT 0x00000004
29#define CHSW_DEV_BOOT 0x00000020
30#define CHSW_WP_BOOT 0x00000200
Randall Spanglere73302c2011-02-18 14:53:01 -080031/* CMOS reboot field bitflags */
32#define CMOSRF_RECOVERY 0x80
33#define CMOSRF_DEBUG_RESET 0x40
34#define CMOSRF_TRY_B 0x20
Randall Spanglerb47ed5a2011-02-23 13:05:40 -080035/* Boot reasons from BINF.0, from early H2C firmware */
36/* Unknown */
37#define BINF0_UNKNOWN 0
38/* Normal boot to Chrome OS */
39#define BINF0_NORMAL 1
40/* Developer mode boot (developer mode warning displayed) */
41#define BINF0_DEVELOPER 2
42/* Recovery initiated by user, using recovery button */
43#define BINF0_RECOVERY_BUTTON 3
44/* Recovery initiated by user pressing a key at developer mode warning
45 * screen */
46#define BINF0_RECOVERY_DEV_SCREEN_KEY 4
47/* Recovery caused by BIOS failed signature check (neither rewritable
48 * firmware was valid) */
49#define BINF0_RECOVERY_RW_FW_BAD 5
50/* Recovery caused by no OS kernel detected */
51#define BINF0_RECOVERY_NO_OS 6
52/* Recovery caused by OS kernel failed signature check */
53#define BINF0_RECOVERY_BAD_OS 7
54/* Recovery initiated by OS */
55#define BINF0_RECOVERY_OS_INITIATED 8
56/* OS-initiated S3 diagnostic path (debug mode boot) */
57#define BINF0_S3_DIAGNOSTIC_PATH 9
58/* S3 resume failed */
59#define BINF0_S3_RESUME_FAILED 10
60/* Recovery caused by TPM error */
61#define BINF0_RECOVERY_TPM_ERROR 11
Randall Spangler196e1772011-03-10 11:31:06 -080062/* Firmware types from BINF.3 */
63#define BINF3_RECOVERY 0
64#define BINF3_NORMAL 1
65#define BINF3_DEVELOPER 2
Randall Spangler54218662011-02-07 11:20:20 -080066
67/* Base name for ACPI files */
68#define ACPI_BASE_PATH "/sys/devices/platform/chromeos_acpi"
69/* Paths for frequently used ACPI files */
Randall Spanglerb47ed5a2011-02-23 13:05:40 -080070#define ACPI_BINF_PATH ACPI_BASE_PATH "/BINF"
Randall Spangler54218662011-02-07 11:20:20 -080071#define ACPI_CHNV_PATH ACPI_BASE_PATH "/CHNV"
72#define ACPI_CHSW_PATH ACPI_BASE_PATH "/CHSW"
Randall Spangler2b59a072011-02-24 11:17:24 -080073#define ACPI_FMAP_PATH ACPI_BASE_PATH "/FMAP"
Randall Spangler54218662011-02-07 11:20:20 -080074#define ACPI_GPIO_PATH ACPI_BASE_PATH "/GPIO"
Randall Spangler0f8ffb12011-02-25 09:50:54 -080075#define ACPI_VBNV_PATH ACPI_BASE_PATH "/VBNV"
Vadim Bendebury20084232011-03-15 09:29:48 -070076#define ACPI_VDAT_PATH ACPI_BASE_PATH "/VDAT"
Randall Spangler54218662011-02-07 11:20:20 -080077
78/* Base name for GPIO files */
79#define GPIO_BASE_PATH "/sys/class/gpio"
80#define GPIO_EXPORT_PATH GPIO_BASE_PATH "/export"
81
Randall Spangler196e1772011-03-10 11:31:06 -080082/* Filename for NVRAM file */
Randall Spanglere73302c2011-02-18 14:53:01 -080083#define NVRAM_PATH "/dev/nvram"
84
Randall Spangler196e1772011-03-10 11:31:06 -080085/* Filename for kernel command line */
86#define KERNEL_CMDLINE_PATH "/proc/cmdline"
87
Vadim Bendebury20084232011-03-15 09:29:48 -070088/* A structure to contain buffer data retrieved from the ACPI. */
89typedef struct {
90 int buffer_size;
Randall Spanglerf4ba19d2011-03-17 16:10:21 -070091 uint8_t* buffer;
Vadim Bendebury20084232011-03-15 09:29:48 -070092} AcpiBuffer;
93
Randall Spanglerb47ed5a2011-02-23 13:05:40 -080094
Randall Spanglerf4ba19d2011-03-17 16:10:21 -070095/* Fields that GetVdatString() can get */
96typedef enum VdatStringField {
97 VDAT_STRING_TIMERS = 0, /* Timer values */
98 VDAT_STRING_LOAD_FIRMWARE_DEBUG /* LoadFirmware() debug information */
99} VdatStringField;
100
101
102/* Fields that GetVdatInt() can get */
103typedef enum VdatIntField {
104 VDAT_INT_FLAGS = 0 /* Flags */
105} VdatIntField;
106
107
Randall Spanglerb47ed5a2011-02-23 13:05:40 -0800108/* Copy up to dest_size-1 characters from src to dest, ensuring null
109 termination (which strncpy() doesn't do). Returns the destination
110 string. */
111char* StrCopy(char* dest, const char* src, int dest_size) {
112 strncpy(dest, src, dest_size);
113 dest[dest_size - 1] = '\0';
114 return dest;
115}
116
117
Randall Spangler54218662011-02-07 11:20:20 -0800118/* Read a string from a file. Passed the destination, dest size, and
119 * filename to read.
120 *
121 * Returns the destination, or NULL if error. */
122char* ReadFileString(char* dest, int size, const char* filename) {
123 char* got;
124 FILE* f;
125
126 f = fopen(filename, "rt");
127 if (!f)
128 return NULL;
129
130 got = fgets(dest, size, f);
131 fclose(f);
132 return got;
133}
134
135
136/* Read an integer from a file.
137 *
138 * Returns the parsed integer, or -1 if error. */
139int ReadFileInt(const char* filename) {
140 char buf[64];
141 int value;
142 char* e = NULL;
143
144 if (!ReadFileString(buf, sizeof(buf), filename))
145 return -1;
146
147 /* Convert to integer. Allow characters after the int ("123 blah"). */
148 value = strtol(buf, &e, 0);
149 if (e == buf)
150 return -1; /* No characters consumed, so conversion failed */
151
152 return value;
153}
154
155
156/* Check if a bit is set in a file which contains an integer.
157 *
158 * Returns 1 if the bit is set, 0 if clear, or -1 if error. */
159int ReadFileBit(const char* filename, int bitmask) {
160 int value = ReadFileInt(filename);
161 if (value == -1)
162 return -1;
163 else return (value & bitmask ? 1 : 0);
164}
165
166
Randall Spanglerc80fe652011-02-17 11:06:47 -0800167/* Return true if the FWID starts with the specified string. */
168static int FwidStartsWith(const char *start) {
169 char fwid[128];
170 if (!VbGetSystemPropertyString("fwid", fwid, sizeof(fwid)))
171 return 0;
172
173 return 0 == strncmp(fwid, start, strlen(start));
174}
175
176
Randall Spangler54218662011-02-07 11:20:20 -0800177/* Read a GPIO of the specified signal type (see ACPI GPIO SignalType).
178 *
179 * Returns 1 if the signal is asserted, 0 if not asserted, or -1 if error. */
180int ReadGpio(int signal_type) {
181 char name[128];
182 int index = 0;
183 int gpio_type;
184 int active_high;
185 int controller_offset;
186 char controller_name[128];
187 int value;
188
189 /* Scan GPIO.* to find a matching signal type */
190 for (index = 0; ; index++) {
191 snprintf(name, sizeof(name), "%s.%d/GPIO.0", ACPI_GPIO_PATH, index);
192 gpio_type = ReadFileInt(name);
193 if (gpio_type == signal_type)
194 break;
195 else if (gpio_type == -1)
196 return -1; /* Ran out of GPIOs before finding a match */
197 }
198
199 /* Read attributes and controller info for the GPIO */
200 snprintf(name, sizeof(name), "%s.%d/GPIO.1", ACPI_GPIO_PATH, index);
201 active_high = ReadFileBit(name, 0x00000001);
202 snprintf(name, sizeof(name), "%s.%d/GPIO.2", ACPI_GPIO_PATH, index);
203 controller_offset = ReadFileInt(name);
204 if (active_high == -1 || controller_offset == -1)
205 return -1; /* Missing needed info */
206
207 /* We only support the NM10 for now */
208 snprintf(name, sizeof(name), "%s.%d/GPIO.3", ACPI_GPIO_PATH, index);
209 if (!ReadFileString(controller_name, sizeof(controller_name), name))
210 return -1;
211 if (0 != strcmp(controller_name, "NM10"))
212 return -1;
213
214 /* Assume the NM10 has offset 192 */
215 /* TODO: should really check gpiochipNNN/label to see if it's the
216 * address we expect for the NM10, and then read the offset from
217 * gpiochipNNN/base. */
218 controller_offset += 192;
219
220 /* Try reading the GPIO value */
221 snprintf(name, sizeof(name), "%s/gpio%d/value",
222 GPIO_BASE_PATH, controller_offset);
223 value = ReadFileInt(name);
224
225 if (value == -1) {
226 /* Try exporting the GPIO */
227 FILE* f = fopen(GPIO_EXPORT_PATH, "wt");
228 if (!f)
229 return -1;
230 fprintf(f, "%d", controller_offset);
231 fclose(f);
232
233 /* Try re-reading the GPIO value */
234 value = ReadFileInt(name);
235 }
236
237 if (value == -1)
238 return -1;
239
240 /* Compare the GPIO value with the active value and return 1 if match. */
241 return (value == active_high ? 1 : 0);
242}
243
244
Randall Spanglere73302c2011-02-18 14:53:01 -0800245/* Read the CMOS reboot field in NVRAM.
246 *
247 * Returns 0 if the mask is clear in the field, 1 if set, or -1 if error. */
248int VbGetCmosRebootField(uint8_t mask) {
249 FILE* f;
250 int chnv, nvbyte;
251
252 /* Get the byte offset from CHNV */
253 chnv = ReadFileInt(ACPI_CHNV_PATH);
254 if (chnv == -1)
255 return -1;
256
257 f = fopen(NVRAM_PATH, "rb");
258 if (!f)
259 return -1;
260
261 if (0 != fseek(f, chnv, SEEK_SET) || EOF == (nvbyte = fgetc(f))) {
262 fclose(f);
263 return -1;
264 }
265
266 fclose(f);
267 return (nvbyte & mask ? 1 : 0);
268}
269
270
271/* Write the CMOS reboot field in NVRAM.
272 *
273 * Sets (value=0) or clears (value!=0) the mask in the byte.
274 *
275 * Returns 0 if success, or -1 if error. */
276int VbSetCmosRebootField(uint8_t mask, int value) {
277 FILE* f;
278 int chnv, nvbyte;
279
280 /* Get the byte offset from CHNV */
281 chnv = ReadFileInt(ACPI_CHNV_PATH);
282 if (chnv == -1)
283 return -1;
284
285 f = fopen(NVRAM_PATH, "w+b");
286 if (!f)
287 return -1;
288
289 /* Read the current value */
290 if (0 != fseek(f, chnv, SEEK_SET) || EOF == (nvbyte = fgetc(f))) {
291 fclose(f);
292 return -1;
293 }
294
295 /* Set/clear the mask */
296 if (value)
297 nvbyte |= mask;
298 else
299 nvbyte &= ~mask;
300
301 /* Write the byte back */
302 if (0 != fseek(f, chnv, SEEK_SET) || EOF == (fputc(nvbyte, f))) {
303 fclose(f);
304 return -1;
305 }
306
307 /* Success */
308 fclose(f);
309 return 0;
310}
311
Vadim Bendebury20084232011-03-15 09:29:48 -0700312/*
313 * Get buffer data from ACPI.
314 *
315 * Buffer data is expected to be represented by a file which is a text dump of
316 * the buffer, representing each byte by two hex numbers, space and newline
317 * separated.
318 *
319 * Input - ACPI file name to get data from.
320 *
321 * Output: a pointer to AcpiBuffer structure containing the binary
322 * representation of the data. The caller is responsible for
323 * deallocating the pointer, this will take care of both the structure
324 * and the buffer. Null in case of error.
325 */
326
327AcpiBuffer* VbGetBuffer(const char* filename)
328{
329 FILE* f = NULL;
330 char* file_buffer = NULL;
331 AcpiBuffer* acpi_buffer = NULL;
332 AcpiBuffer* return_value = NULL;
333
334 do {
335 struct stat fs;
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700336 uint8_t* output_ptr;
Vadim Bendebury20084232011-03-15 09:29:48 -0700337 int rv, i, real_size;
338
339 rv = stat(filename, &fs);
340 if (rv || !S_ISREG(fs.st_mode))
341 break;
342
343 f = fopen(filename, "r");
344 if (!f)
345 break;
346
347 file_buffer = Malloc(fs.st_size + 1);
348 if (!file_buffer)
349 break;
350
Vadim Bendebury20084232011-03-15 09:29:48 -0700351 real_size = fread(file_buffer, 1, fs.st_size, f);
352 if (!real_size)
353 break;
Vadim Bendebury20084232011-03-15 09:29:48 -0700354 file_buffer[real_size] = '\0';
355
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700356 /* Each byte in the output will replace two characters and a space
357 * in the input, so the output size does not exceed input side/3
358 * (a little less if account for newline characters). */
359 acpi_buffer = Malloc(sizeof(AcpiBuffer) + real_size/3);
360 if (!acpi_buffer)
361 break;
362 acpi_buffer->buffer = (uint8_t*)(acpi_buffer + 1);
Vadim Bendebury20084232011-03-15 09:29:48 -0700363 acpi_buffer->buffer_size = 0;
364 output_ptr = acpi_buffer->buffer;
365
366 /* process the file contents */
367 for (i = 0; i < real_size; i++) {
368 char* base, *end;
369
370 base = file_buffer + i;
371
372 if (!isxdigit(*base))
373 continue;
374
375 output_ptr[acpi_buffer->buffer_size++] = strtol(base, &end, 16) & 0xff;
376
377 if ((end - base) != 2)
378 /* Input file format error */
379 break;
380
381 i += 2; /* skip the second character and the following space */
382 }
383
384 if (i == real_size) {
385 /* all is well */
386 return_value = acpi_buffer;
387 acpi_buffer = NULL; /* prevent it from deallocating */
388 }
389 } while(0);
390
391 /* wrap up */
392 if (f)
393 fclose(f);
394
395 if (file_buffer)
396 Free(file_buffer);
397
398 if (acpi_buffer)
399 Free(acpi_buffer);
400
401 return return_value;
402}
Randall Spangler0f8ffb12011-02-25 09:50:54 -0800403
404/* Read an integer property from VbNvStorage.
405 *
406 * Returns the parameter value, or -1 if error. */
407int VbGetNvStorage(VbNvParam param) {
408 FILE* f;
409 VbNvContext vnc;
410 int offs;
411 uint32_t value;
412 int retval;
413
414 /* Get the byte offset from VBNV */
415 offs = ReadFileInt(ACPI_VBNV_PATH ".0");
416 if (offs == -1)
417 return -1;
418 if (VBNV_BLOCK_SIZE > ReadFileInt(ACPI_VBNV_PATH ".1"))
419 return -1; /* NV storage block is too small */
420
421 /* TODO: locking around NV access */
422 f = fopen(NVRAM_PATH, "rb");
423 if (!f)
424 return -1;
425
426 if (0 != fseek(f, offs, SEEK_SET) ||
427 1 != fread(vnc.raw, VBNV_BLOCK_SIZE, 1, f)) {
428 fclose(f);
429 return -1;
430 }
431
432 fclose(f);
433
434 if (0 != VbNvSetup(&vnc))
435 return -1;
436 retval = VbNvGet(&vnc, param, &value);
437 if (0 != VbNvTeardown(&vnc))
438 return -1;
439 if (0 != retval)
440 return -1;
441
442 /* TODO: If vnc.raw_changed, attempt to reopen NVRAM for write and
443 * save the new defaults. If we're able to, log. */
444 /* TODO: release lock */
445
446 return (int)value;
447}
448
449
450/* Write an integer property to VbNvStorage.
451 *
452 * Returns 0 if success, -1 if error. */
453int VbSetNvStorage(VbNvParam param, int value) {
454 FILE* f;
455 VbNvContext vnc;
456 int offs;
457 int retval = -1;
458 int i;
459
460 /* Get the byte offset from VBNV */
461 offs = ReadFileInt(ACPI_VBNV_PATH ".0");
462 if (offs == -1)
463 return -1;
464 if (VBNV_BLOCK_SIZE > ReadFileInt(ACPI_VBNV_PATH ".1"))
465 return -1; /* NV storage block is too small */
466
467 /* TODO: locking around NV access */
468 f = fopen(NVRAM_PATH, "w+b");
469 if (!f)
470 return -1;
471
472 if (0 != fseek(f, offs, SEEK_SET) ||
473 1 != fread(vnc.raw, VBNV_BLOCK_SIZE, 1, f)) {
474 goto VbSetNvCleanup;
475 }
476
477 if (0 != VbNvSetup(&vnc))
478 goto VbSetNvCleanup;
479 i = VbNvSet(&vnc, param, (uint32_t)value);
480 if (0 != VbNvTeardown(&vnc))
481 goto VbSetNvCleanup;
482 if (0 != i)
483 goto VbSetNvCleanup;
484
485 if (vnc.raw_changed) {
486 if (0 != fseek(f, offs, SEEK_SET) ||
487 1 != fwrite(vnc.raw, VBNV_BLOCK_SIZE, 1, f))
488 goto VbSetNvCleanup;
489 }
490
491 /* Success */
492 retval = 0;
493
494VbSetNvCleanup:
495 fclose(f);
496 /* TODO: release lock */
497 return retval;
498}
499
500
Randall Spanglerb47ed5a2011-02-23 13:05:40 -0800501/* Read the recovery reason. Returns the reason code or -1 if error. */
502int VbGetRecoveryReason(void) {
503 int value;
504
505 /* Try reading type from BINF.4 */
506 value = ReadFileInt(ACPI_BINF_PATH ".4");
507 if (-1 != value)
508 return value;
509
510 /* Fall back to BINF.0 for legacy systems like Mario. */
511 switch(ReadFileInt(ACPI_BINF_PATH ".0")) {
512 case BINF0_NORMAL:
513 case BINF0_DEVELOPER:
514 return VBNV_RECOVERY_NOT_REQUESTED;
515 case BINF0_RECOVERY_BUTTON:
516 return VBNV_RECOVERY_RO_MANUAL;
517 case BINF0_RECOVERY_DEV_SCREEN_KEY:
518 return VBNV_RECOVERY_RW_DEV_SCREEN;
519 case BINF0_RECOVERY_RW_FW_BAD:
520 case BINF0_RECOVERY_NO_OS:
521 return VBNV_RECOVERY_RW_NO_OS;
522 case BINF0_RECOVERY_BAD_OS:
523 return VBNV_RECOVERY_RW_INVALID_OS;
524 case BINF0_RECOVERY_OS_INITIATED:
525 return VBNV_RECOVERY_LEGACY;
526 default:
527 /* Other values don't map cleanly to firmware type. */
528 return -1;
529 }
530}
531
532
533/* Read the active main firmware type into the destination buffer.
534 * Passed the destination and its size. Returns the destination, or
535 * NULL if error. */
536const char* VbReadMainFwType(char* dest, int size) {
537
538 /* Try reading type from BINF.3 */
539 switch(ReadFileInt(ACPI_BINF_PATH ".3")) {
Randall Spangler196e1772011-03-10 11:31:06 -0800540 case BINF3_RECOVERY:
Randall Spanglerb47ed5a2011-02-23 13:05:40 -0800541 return StrCopy(dest, "recovery", size);
Randall Spangler196e1772011-03-10 11:31:06 -0800542 case BINF3_NORMAL:
Randall Spanglerb47ed5a2011-02-23 13:05:40 -0800543 return StrCopy(dest, "normal", size);
Randall Spangler196e1772011-03-10 11:31:06 -0800544 case BINF3_DEVELOPER:
Randall Spanglerb47ed5a2011-02-23 13:05:40 -0800545 return StrCopy(dest, "developer", size);
546 default:
547 break; /* Fall through to legacy handling */
548 }
549
550 /* Fall back to BINF.0 for legacy systems like Mario. */
551 switch(ReadFileInt(ACPI_BINF_PATH ".0")) {
552 case -1:
553 /* Both BINF.0 and BINF.3 are missing, so this isn't Chrome OS
554 * firmware. */
555 return StrCopy(dest, "nonchrome", size);
556 case BINF0_NORMAL:
557 return StrCopy(dest, "normal", size);
558 case BINF0_DEVELOPER:
559 return StrCopy(dest, "developer", size);
560 case BINF0_RECOVERY_BUTTON:
561 case BINF0_RECOVERY_DEV_SCREEN_KEY:
562 case BINF0_RECOVERY_RW_FW_BAD:
563 case BINF0_RECOVERY_NO_OS:
564 case BINF0_RECOVERY_BAD_OS:
565 case BINF0_RECOVERY_OS_INITIATED:
566 case BINF0_RECOVERY_TPM_ERROR:
567 /* Assorted flavors of recovery boot reason. */
568 return StrCopy(dest, "recovery", size);
569 default:
570 /* Other values don't map cleanly to firmware type. */
571 return NULL;
572 }
573}
574
575
Randall Spangler196e1772011-03-10 11:31:06 -0800576/* Determine whether OS-level debugging should be allowed. Passed the
577 * destination and its size. Returns 1 if yes, 0 if no, -1 if error. */
578int VbGetCrosDebug(void) {
579 FILE* f = NULL;
580 char buf[4096] = "";
581 int binf3;
582 char *t, *saveptr;
583
584 /* Try reading firmware type from BINF.3. */
585 binf3 = ReadFileInt(ACPI_BINF_PATH ".3");
586 if (BINF3_RECOVERY == binf3)
587 return 0; /* Recovery mode never allows debug. */
588 else if (BINF3_DEVELOPER == binf3)
589 return 1; /* Developer firmware always allows debug. */
590
591 /* Normal new firmware, older ChromeOS firmware, or non-Chrome firmware.
Randall Spangler227f7922011-03-11 13:34:56 -0800592 * For all these cases, check /proc/cmdline for cros_[no]debug. */
Randall Spangler196e1772011-03-10 11:31:06 -0800593 f = fopen(KERNEL_CMDLINE_PATH, "rt");
594 if (f) {
595 if (NULL == fgets(buf, sizeof(buf), f))
596 *buf = 0;
597 fclose(f);
598 }
599 for (t = strtok_r(buf, " ", &saveptr); t; t=strtok_r(NULL, " ", &saveptr)) {
600 if (0 == strcmp(t, "cros_debug"))
601 return 1;
Randall Spangler227f7922011-03-11 13:34:56 -0800602 else if (0 == strcmp(t, "cros_nodebug"))
603 return 0;
Randall Spangler196e1772011-03-10 11:31:06 -0800604 }
605
606 /* Normal new firmware or older Chrome OS firmware allows debug if the
607 * dev switch is on. */
608 if (1 == ReadFileBit(ACPI_CHSW_PATH, CHSW_DEV_BOOT))
609 return 1;
610
611 /* All other cases disallow debug. */
612 return 0;
613}
614
Randall Spanglerb47ed5a2011-02-23 13:05:40 -0800615
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700616char* GetVdatString(char* dest, int size, VdatStringField field)
617{
618 VbSharedDataHeader* sh;
619 AcpiBuffer* ab = VbGetBuffer(ACPI_VDAT_PATH);
620 if (!ab)
621 return NULL;
622
623 sh = (VbSharedDataHeader*)ab->buffer;
624
625 switch (field) {
626 case VDAT_STRING_TIMERS:
627 snprintf(dest, size,
628 "LFS=%" PRIu64 ",%" PRIu64
629 " LF=%" PRIu64 ",%" PRIu64
630 " LK=%" PRIu64 ",%" PRIu64,
631 sh->timer_load_firmware_start_enter,
632 sh->timer_load_firmware_start_exit,
633 sh->timer_load_firmware_enter,
634 sh->timer_load_firmware_exit,
635 sh->timer_load_kernel_enter,
636 sh->timer_load_kernel_exit);
637 break;
638
639 case VDAT_STRING_LOAD_FIRMWARE_DEBUG:
640 snprintf(dest, size,
641 "check=%d,%d index=0x%02x tpmver=0x%x lowestver=0x%x",
642 sh->check_fw_a_result,
643 sh->check_fw_b_result,
644 sh->firmware_index,
645 sh->fw_version_tpm_start,
646 sh->fw_version_lowest);
647 break;
648
649 default:
650 Free(ab);
651 return NULL;
652 }
653
654 Free(ab);
655 return dest;
656}
657
658
659int GetVdatInt(VdatIntField field) {
660 VbSharedDataHeader* sh;
661 AcpiBuffer* ab = VbGetBuffer(ACPI_VDAT_PATH);
662 int value = -1;
663
664 if (!ab)
665 return -1;
666
667 sh = (VbSharedDataHeader*)ab->buffer;
668
669 switch (field) {
670 case VDAT_INT_FLAGS:
671 value = (int)sh->flags;
672 break;
673
674 }
675
676 Free(ab);
677 return value;
678}
679
680
Randall Spangler54218662011-02-07 11:20:20 -0800681/* Read a system property integer.
682 *
683 * Returns the property value, or -1 if error. */
684int VbGetSystemPropertyInt(const char* name) {
Randall Spanglerc80fe652011-02-17 11:06:47 -0800685 int value = -1;
Randall Spangler54218662011-02-07 11:20:20 -0800686
Randall Spanglere73302c2011-02-18 14:53:01 -0800687 /* Switch positions */
Randall Spangler54218662011-02-07 11:20:20 -0800688 if (!strcasecmp(name,"devsw_cur")) {
Randall Spanglerc80fe652011-02-17 11:06:47 -0800689 value = ReadGpio(GPIO_SIGNAL_TYPE_DEV);
Randall Spangler54218662011-02-07 11:20:20 -0800690 } else if (!strcasecmp(name,"devsw_boot")) {
Randall Spanglerc80fe652011-02-17 11:06:47 -0800691 value = ReadFileBit(ACPI_CHSW_PATH, CHSW_DEV_BOOT);
Randall Spangler54218662011-02-07 11:20:20 -0800692 } else if (!strcasecmp(name,"recoverysw_cur")) {
Randall Spanglerc80fe652011-02-17 11:06:47 -0800693 value = ReadGpio(GPIO_SIGNAL_TYPE_RECOVERY);
Randall Spangler54218662011-02-07 11:20:20 -0800694 } else if (!strcasecmp(name,"recoverysw_boot")) {
Randall Spanglerc80fe652011-02-17 11:06:47 -0800695 value = ReadFileBit(ACPI_CHSW_PATH, CHSW_RECOVERY_BOOT);
Randall Spangler54218662011-02-07 11:20:20 -0800696 } else if (!strcasecmp(name,"recoverysw_ec_boot")) {
Randall Spanglerc80fe652011-02-17 11:06:47 -0800697 value = ReadFileBit(ACPI_CHSW_PATH, CHSW_RECOVERY_EC_BOOT);
Randall Spangler54218662011-02-07 11:20:20 -0800698 } else if (!strcasecmp(name,"wpsw_cur")) {
Randall Spanglerc80fe652011-02-17 11:06:47 -0800699 value = ReadGpio(GPIO_SIGNAL_TYPE_WP);
700 if (-1 != value && FwidStartsWith("Mario."))
701 value = 1 - value; /* Mario reports this backwards */
Randall Spangler54218662011-02-07 11:20:20 -0800702 } else if (!strcasecmp(name,"wpsw_boot")) {
Randall Spanglerc80fe652011-02-17 11:06:47 -0800703 value = ReadFileBit(ACPI_CHSW_PATH, CHSW_WP_BOOT);
704 if (-1 != value && FwidStartsWith("Mario."))
705 value = 1 - value; /* Mario reports this backwards */
706 }
Randall Spanglerb47ed5a2011-02-23 13:05:40 -0800707 /* Saved memory is at a fixed location for all H2C BIOS. If the CHSW
708 * path exists in sysfs, it's a H2C BIOS. */
709 else if (!strcasecmp(name,"savedmem_base")) {
710 return (-1 == ReadFileInt(ACPI_CHSW_PATH) ? -1 : 0x00F00000);
711 } else if (!strcasecmp(name,"savedmem_size")) {
712 return (-1 == ReadFileInt(ACPI_CHSW_PATH) ? -1 : 0x00100000);
713 }
Randall Spangler17260282011-02-25 12:06:26 -0800714 /* NV storage values with no defaults for older BIOS. */
715 else if (!strcasecmp(name,"tried_fwb")) {
Randall Spangler92e378e2011-02-25 13:56:53 -0800716 value = VbGetNvStorage(VBNV_TRIED_FIRMWARE_B);
Randall Spangler618d17d2011-03-01 10:33:11 -0800717 } else if (!strcasecmp(name,"kern_nv")) {
718 value = VbGetNvStorage(VBNV_KERNEL_FIELD);
Randall Spanglerb4167142011-03-01 13:04:22 -0800719 } else if (!strcasecmp(name,"nvram_cleared")) {
720 value = VbGetNvStorage(VBNV_KERNEL_SETTINGS_RESET);
Randall Spanglerb17e8d32011-03-15 09:50:38 -0700721 } else if (!strcasecmp(name,"vbtest_errfunc")) {
722 value = VbGetNvStorage(VBNV_TEST_ERROR_FUNC);
723 } else if (!strcasecmp(name,"vbtest_errno")) {
724 value = VbGetNvStorage(VBNV_TEST_ERROR_NUM);
Randall Spangler17260282011-02-25 12:06:26 -0800725 }
Randall Spangler0f8ffb12011-02-25 09:50:54 -0800726 /* NV storage values. If unable to get from NV storage, fall back to the
727 * CMOS reboot field used by older BIOS. */
Randall Spanglere73302c2011-02-18 14:53:01 -0800728 else if (!strcasecmp(name,"recovery_request")) {
Randall Spangler0f8ffb12011-02-25 09:50:54 -0800729 value = VbGetNvStorage(VBNV_RECOVERY_REQUEST);
730 if (-1 == value)
731 value = VbGetCmosRebootField(CMOSRF_RECOVERY);
Randall Spanglere73302c2011-02-18 14:53:01 -0800732 } else if (!strcasecmp(name,"dbg_reset")) {
Randall Spangler0f8ffb12011-02-25 09:50:54 -0800733 value = VbGetNvStorage(VBNV_DEBUG_RESET_MODE);
734 if (-1 == value)
735 value = VbGetCmosRebootField(CMOSRF_DEBUG_RESET);
Randall Spanglere73302c2011-02-18 14:53:01 -0800736 } else if (!strcasecmp(name,"fwb_tries")) {
Randall Spangler0f8ffb12011-02-25 09:50:54 -0800737 value = VbGetNvStorage(VBNV_TRY_B_COUNT);
738 if (-1 == value)
739 value = VbGetCmosRebootField(CMOSRF_TRY_B);
Randall Spanglere73302c2011-02-18 14:53:01 -0800740 }
Randall Spanglerb47ed5a2011-02-23 13:05:40 -0800741 /* Other parameters */
742 else if (!strcasecmp(name,"recovery_reason")) {
743 return VbGetRecoveryReason();
Randall Spangler2b59a072011-02-24 11:17:24 -0800744 } else if (!strcasecmp(name,"fmap_base")) {
745 value = ReadFileInt(ACPI_FMAP_PATH);
Randall Spangler196e1772011-03-10 11:31:06 -0800746 } else if (!strcasecmp(name,"cros_debug")) {
747 value = VbGetCrosDebug();
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700748 } else if (!strcasecmp(name,"vdat_flags")) {
749 value = GetVdatInt(VDAT_INT_FLAGS);
Randall Spanglerb47ed5a2011-02-23 13:05:40 -0800750 }
Randall Spangler54218662011-02-07 11:20:20 -0800751
Randall Spanglerc80fe652011-02-17 11:06:47 -0800752 return value;
Randall Spangler54218662011-02-07 11:20:20 -0800753}
754
Randall Spangler54218662011-02-07 11:20:20 -0800755/* Read a system property string into a destination buffer of the specified
756 * size.
757 *
758 * Returns the passed buffer, or NULL if error. */
759const char* VbGetSystemPropertyString(const char* name, char* dest, int size) {
760
761 if (!strcasecmp(name,"hwid")) {
762 return ReadFileString(dest, size, ACPI_BASE_PATH "/HWID");
763 } else if (!strcasecmp(name,"fwid")) {
764 return ReadFileString(dest, size, ACPI_BASE_PATH "/FWID");
765 } else if (!strcasecmp(name,"ro_fwid")) {
766 return ReadFileString(dest, size, ACPI_BASE_PATH "/FRID");
Randall Spanglerb47ed5a2011-02-23 13:05:40 -0800767 } else if (!strcasecmp(name,"mainfw_act")) {
768 switch(ReadFileInt(ACPI_BINF_PATH ".1")) {
769 case 0:
770 return StrCopy(dest, "recovery", size);
771 case 1:
772 return StrCopy(dest, "A", size);
773 case 2:
774 return StrCopy(dest, "B", size);
775 default:
776 return NULL;
777 }
778 } else if (!strcasecmp(name,"mainfw_type")) {
779 return VbReadMainFwType(dest, size);
780 } else if (!strcasecmp(name,"ecfw_act")) {
781 switch(ReadFileInt(ACPI_BINF_PATH ".2")) {
782 case 0:
783 return StrCopy(dest, "RO", size);
784 case 1:
785 return StrCopy(dest, "RW", size);
786 default:
787 return NULL;
788 }
Randall Spangler17260282011-02-25 12:06:26 -0800789 } else if (!strcasecmp(name,"kernkey_vfy")) {
790 switch(VbGetNvStorage(VBNV_FW_VERIFIED_KERNEL_KEY)) {
791 case 0:
792 return "hash";
793 case 1:
794 return "sig";
795 default:
796 return NULL;
797 }
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700798 } else if (!strcasecmp(name, "vdat_timers")) {
799 return GetVdatString(dest, size, VDAT_STRING_TIMERS);
800 } else if (!strcasecmp(name, "vdat_lfdebug")) {
801 return GetVdatString(dest, size, VDAT_STRING_LOAD_FIRMWARE_DEBUG);
Randall Spangler54218662011-02-07 11:20:20 -0800802 } else
803 return NULL;
Randall Spangler54218662011-02-07 11:20:20 -0800804}
805
806
807/* Set a system property integer.
808 *
809 * Returns 0 if success, -1 if error. */
810int VbSetSystemPropertyInt(const char* name, int value) {
811
Randall Spangler618d17d2011-03-01 10:33:11 -0800812 /* NV storage values with no defaults for older BIOS. */
Randall Spanglerb4167142011-03-01 13:04:22 -0800813 if (!strcasecmp(name,"nvram_cleared")) {
814 /* Can only clear this flag; it's set inside the NV storage library. */
815 return VbSetNvStorage(VBNV_KERNEL_SETTINGS_RESET, 0);
816 } else if (!strcasecmp(name,"kern_nv")) {
Randall Spangler618d17d2011-03-01 10:33:11 -0800817 return VbSetNvStorage(VBNV_KERNEL_FIELD, value);
Randall Spanglerb17e8d32011-03-15 09:50:38 -0700818 } else if (!strcasecmp(name,"vbtest_errfunc")) {
819 return VbSetNvStorage(VBNV_TEST_ERROR_FUNC, value);
820 } else if (!strcasecmp(name,"vbtest_errno")) {
821 return VbSetNvStorage(VBNV_TEST_ERROR_NUM, value);
Randall Spangler618d17d2011-03-01 10:33:11 -0800822 }
Randall Spangler0f8ffb12011-02-25 09:50:54 -0800823 /* NV storage values. If unable to get from NV storage, fall back to the
824 * CMOS reboot field used by older BIOS. */
Randall Spangler618d17d2011-03-01 10:33:11 -0800825 else if (!strcasecmp(name,"recovery_request")) {
Randall Spangler0f8ffb12011-02-25 09:50:54 -0800826 if (0 == VbSetNvStorage(VBNV_RECOVERY_REQUEST, value))
827 return 0;
Randall Spanglere73302c2011-02-18 14:53:01 -0800828 return VbSetCmosRebootField(CMOSRF_RECOVERY, value);
829 } else if (!strcasecmp(name,"dbg_reset")) {
Randall Spangler0f8ffb12011-02-25 09:50:54 -0800830 if (0 == VbSetNvStorage(VBNV_DEBUG_RESET_MODE, value))
831 return 0;
832 return VbSetCmosRebootField(CMOSRF_DEBUG_RESET, value);
Randall Spanglere73302c2011-02-18 14:53:01 -0800833 } else if (!strcasecmp(name,"fwb_tries")) {
Randall Spangler0f8ffb12011-02-25 09:50:54 -0800834 if (0 == VbSetNvStorage(VBNV_TRY_B_COUNT, value))
835 return 0;
Randall Spanglere73302c2011-02-18 14:53:01 -0800836 return VbSetCmosRebootField(CMOSRF_TRY_B, value);
837 }
838
Randall Spangler54218662011-02-07 11:20:20 -0800839 return -1;
840}
841
842
843/* Set a system property string.
844 *
845 * Returns 0 if success, -1 if error. */
846int VbSetSystemPropertyString(const char* name, const char* value) {
847
848 /* TODO: support setting */
849 return -1;
850}