blob: e841bad6f302c2c153ce7484c6c98050ed36288a [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 {
Randall Spanglercabe6b32011-03-18 12:44:27 -0700104 VDAT_INT_FLAGS = 0, /* Flags */
105 VDAT_INT_FW_VERSION_TPM, /* Current firmware version in TPM */
106 VDAT_INT_KERNEL_VERSION_TPM, /* Current kernel version in TPM */
107 VDAT_INT_TRIED_FIRMWARE_B, /* Tried firmware B due to fwb_tries */
108 VDAT_INT_KERNEL_KEY_VERIFIED /* Kernel key verified using
109 * signature, not just hash */
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700110} VdatIntField;
111
112
Randall Spanglerb47ed5a2011-02-23 13:05:40 -0800113/* Copy up to dest_size-1 characters from src to dest, ensuring null
114 termination (which strncpy() doesn't do). Returns the destination
115 string. */
116char* StrCopy(char* dest, const char* src, int dest_size) {
117 strncpy(dest, src, dest_size);
118 dest[dest_size - 1] = '\0';
119 return dest;
120}
121
122
Randall Spangler54218662011-02-07 11:20:20 -0800123/* Read a string from a file. Passed the destination, dest size, and
124 * filename to read.
125 *
126 * Returns the destination, or NULL if error. */
127char* ReadFileString(char* dest, int size, const char* filename) {
128 char* got;
129 FILE* f;
130
131 f = fopen(filename, "rt");
132 if (!f)
133 return NULL;
134
135 got = fgets(dest, size, f);
136 fclose(f);
137 return got;
138}
139
140
141/* Read an integer from a file.
142 *
143 * Returns the parsed integer, or -1 if error. */
144int ReadFileInt(const char* filename) {
145 char buf[64];
146 int value;
147 char* e = NULL;
148
149 if (!ReadFileString(buf, sizeof(buf), filename))
150 return -1;
151
152 /* Convert to integer. Allow characters after the int ("123 blah"). */
153 value = strtol(buf, &e, 0);
154 if (e == buf)
155 return -1; /* No characters consumed, so conversion failed */
156
157 return value;
158}
159
160
161/* Check if a bit is set in a file which contains an integer.
162 *
163 * Returns 1 if the bit is set, 0 if clear, or -1 if error. */
164int ReadFileBit(const char* filename, int bitmask) {
165 int value = ReadFileInt(filename);
166 if (value == -1)
167 return -1;
168 else return (value & bitmask ? 1 : 0);
169}
170
171
Randall Spanglerc80fe652011-02-17 11:06:47 -0800172/* Return true if the FWID starts with the specified string. */
173static int FwidStartsWith(const char *start) {
174 char fwid[128];
175 if (!VbGetSystemPropertyString("fwid", fwid, sizeof(fwid)))
176 return 0;
177
178 return 0 == strncmp(fwid, start, strlen(start));
179}
180
181
Randall Spangler54218662011-02-07 11:20:20 -0800182/* Read a GPIO of the specified signal type (see ACPI GPIO SignalType).
183 *
184 * Returns 1 if the signal is asserted, 0 if not asserted, or -1 if error. */
185int ReadGpio(int signal_type) {
186 char name[128];
187 int index = 0;
188 int gpio_type;
189 int active_high;
190 int controller_offset;
191 char controller_name[128];
192 int value;
193
194 /* Scan GPIO.* to find a matching signal type */
195 for (index = 0; ; index++) {
196 snprintf(name, sizeof(name), "%s.%d/GPIO.0", ACPI_GPIO_PATH, index);
197 gpio_type = ReadFileInt(name);
198 if (gpio_type == signal_type)
199 break;
200 else if (gpio_type == -1)
201 return -1; /* Ran out of GPIOs before finding a match */
202 }
203
204 /* Read attributes and controller info for the GPIO */
205 snprintf(name, sizeof(name), "%s.%d/GPIO.1", ACPI_GPIO_PATH, index);
206 active_high = ReadFileBit(name, 0x00000001);
207 snprintf(name, sizeof(name), "%s.%d/GPIO.2", ACPI_GPIO_PATH, index);
208 controller_offset = ReadFileInt(name);
209 if (active_high == -1 || controller_offset == -1)
210 return -1; /* Missing needed info */
211
212 /* We only support the NM10 for now */
213 snprintf(name, sizeof(name), "%s.%d/GPIO.3", ACPI_GPIO_PATH, index);
214 if (!ReadFileString(controller_name, sizeof(controller_name), name))
215 return -1;
216 if (0 != strcmp(controller_name, "NM10"))
217 return -1;
218
219 /* Assume the NM10 has offset 192 */
220 /* TODO: should really check gpiochipNNN/label to see if it's the
221 * address we expect for the NM10, and then read the offset from
222 * gpiochipNNN/base. */
223 controller_offset += 192;
224
225 /* Try reading the GPIO value */
226 snprintf(name, sizeof(name), "%s/gpio%d/value",
227 GPIO_BASE_PATH, controller_offset);
228 value = ReadFileInt(name);
229
230 if (value == -1) {
231 /* Try exporting the GPIO */
232 FILE* f = fopen(GPIO_EXPORT_PATH, "wt");
233 if (!f)
234 return -1;
235 fprintf(f, "%d", controller_offset);
236 fclose(f);
237
238 /* Try re-reading the GPIO value */
239 value = ReadFileInt(name);
240 }
241
242 if (value == -1)
243 return -1;
244
245 /* Compare the GPIO value with the active value and return 1 if match. */
246 return (value == active_high ? 1 : 0);
247}
248
249
Randall Spanglere73302c2011-02-18 14:53:01 -0800250/* Read the CMOS reboot field in NVRAM.
251 *
252 * Returns 0 if the mask is clear in the field, 1 if set, or -1 if error. */
253int VbGetCmosRebootField(uint8_t mask) {
254 FILE* f;
255 int chnv, nvbyte;
256
257 /* Get the byte offset from CHNV */
258 chnv = ReadFileInt(ACPI_CHNV_PATH);
259 if (chnv == -1)
260 return -1;
261
262 f = fopen(NVRAM_PATH, "rb");
263 if (!f)
264 return -1;
265
266 if (0 != fseek(f, chnv, SEEK_SET) || EOF == (nvbyte = fgetc(f))) {
267 fclose(f);
268 return -1;
269 }
270
271 fclose(f);
272 return (nvbyte & mask ? 1 : 0);
273}
274
275
276/* Write the CMOS reboot field in NVRAM.
277 *
278 * Sets (value=0) or clears (value!=0) the mask in the byte.
279 *
280 * Returns 0 if success, or -1 if error. */
281int VbSetCmosRebootField(uint8_t mask, int value) {
282 FILE* f;
283 int chnv, nvbyte;
284
285 /* Get the byte offset from CHNV */
286 chnv = ReadFileInt(ACPI_CHNV_PATH);
287 if (chnv == -1)
288 return -1;
289
290 f = fopen(NVRAM_PATH, "w+b");
291 if (!f)
292 return -1;
293
294 /* Read the current value */
295 if (0 != fseek(f, chnv, SEEK_SET) || EOF == (nvbyte = fgetc(f))) {
296 fclose(f);
297 return -1;
298 }
299
300 /* Set/clear the mask */
301 if (value)
302 nvbyte |= mask;
303 else
304 nvbyte &= ~mask;
305
306 /* Write the byte back */
307 if (0 != fseek(f, chnv, SEEK_SET) || EOF == (fputc(nvbyte, f))) {
308 fclose(f);
309 return -1;
310 }
311
312 /* Success */
313 fclose(f);
314 return 0;
315}
316
Vadim Bendebury20084232011-03-15 09:29:48 -0700317/*
318 * Get buffer data from ACPI.
319 *
320 * Buffer data is expected to be represented by a file which is a text dump of
321 * the buffer, representing each byte by two hex numbers, space and newline
322 * separated.
323 *
324 * Input - ACPI file name to get data from.
325 *
326 * Output: a pointer to AcpiBuffer structure containing the binary
327 * representation of the data. The caller is responsible for
328 * deallocating the pointer, this will take care of both the structure
329 * and the buffer. Null in case of error.
330 */
331
332AcpiBuffer* VbGetBuffer(const char* filename)
333{
334 FILE* f = NULL;
335 char* file_buffer = NULL;
336 AcpiBuffer* acpi_buffer = NULL;
337 AcpiBuffer* return_value = NULL;
338
339 do {
340 struct stat fs;
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700341 uint8_t* output_ptr;
Vadim Bendebury20084232011-03-15 09:29:48 -0700342 int rv, i, real_size;
343
344 rv = stat(filename, &fs);
345 if (rv || !S_ISREG(fs.st_mode))
346 break;
347
348 f = fopen(filename, "r");
349 if (!f)
350 break;
351
352 file_buffer = Malloc(fs.st_size + 1);
353 if (!file_buffer)
354 break;
355
Vadim Bendebury20084232011-03-15 09:29:48 -0700356 real_size = fread(file_buffer, 1, fs.st_size, f);
357 if (!real_size)
358 break;
Vadim Bendebury20084232011-03-15 09:29:48 -0700359 file_buffer[real_size] = '\0';
360
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700361 /* Each byte in the output will replace two characters and a space
362 * in the input, so the output size does not exceed input side/3
363 * (a little less if account for newline characters). */
364 acpi_buffer = Malloc(sizeof(AcpiBuffer) + real_size/3);
365 if (!acpi_buffer)
366 break;
367 acpi_buffer->buffer = (uint8_t*)(acpi_buffer + 1);
Vadim Bendebury20084232011-03-15 09:29:48 -0700368 acpi_buffer->buffer_size = 0;
369 output_ptr = acpi_buffer->buffer;
370
371 /* process the file contents */
372 for (i = 0; i < real_size; i++) {
373 char* base, *end;
374
375 base = file_buffer + i;
376
377 if (!isxdigit(*base))
378 continue;
379
380 output_ptr[acpi_buffer->buffer_size++] = strtol(base, &end, 16) & 0xff;
381
382 if ((end - base) != 2)
383 /* Input file format error */
384 break;
385
386 i += 2; /* skip the second character and the following space */
387 }
388
389 if (i == real_size) {
390 /* all is well */
391 return_value = acpi_buffer;
392 acpi_buffer = NULL; /* prevent it from deallocating */
393 }
394 } while(0);
395
396 /* wrap up */
397 if (f)
398 fclose(f);
399
400 if (file_buffer)
401 Free(file_buffer);
402
403 if (acpi_buffer)
404 Free(acpi_buffer);
405
406 return return_value;
407}
Randall Spangler0f8ffb12011-02-25 09:50:54 -0800408
409/* Read an integer property from VbNvStorage.
410 *
411 * Returns the parameter value, or -1 if error. */
412int VbGetNvStorage(VbNvParam param) {
413 FILE* f;
414 VbNvContext vnc;
415 int offs;
416 uint32_t value;
417 int retval;
418
419 /* Get the byte offset from VBNV */
420 offs = ReadFileInt(ACPI_VBNV_PATH ".0");
421 if (offs == -1)
422 return -1;
423 if (VBNV_BLOCK_SIZE > ReadFileInt(ACPI_VBNV_PATH ".1"))
424 return -1; /* NV storage block is too small */
425
426 /* TODO: locking around NV access */
427 f = fopen(NVRAM_PATH, "rb");
428 if (!f)
429 return -1;
430
431 if (0 != fseek(f, offs, SEEK_SET) ||
432 1 != fread(vnc.raw, VBNV_BLOCK_SIZE, 1, f)) {
433 fclose(f);
434 return -1;
435 }
436
437 fclose(f);
438
439 if (0 != VbNvSetup(&vnc))
440 return -1;
441 retval = VbNvGet(&vnc, param, &value);
442 if (0 != VbNvTeardown(&vnc))
443 return -1;
444 if (0 != retval)
445 return -1;
446
447 /* TODO: If vnc.raw_changed, attempt to reopen NVRAM for write and
448 * save the new defaults. If we're able to, log. */
449 /* TODO: release lock */
450
451 return (int)value;
452}
453
454
455/* Write an integer property to VbNvStorage.
456 *
457 * Returns 0 if success, -1 if error. */
458int VbSetNvStorage(VbNvParam param, int value) {
459 FILE* f;
460 VbNvContext vnc;
461 int offs;
462 int retval = -1;
463 int i;
464
465 /* Get the byte offset from VBNV */
466 offs = ReadFileInt(ACPI_VBNV_PATH ".0");
467 if (offs == -1)
468 return -1;
469 if (VBNV_BLOCK_SIZE > ReadFileInt(ACPI_VBNV_PATH ".1"))
470 return -1; /* NV storage block is too small */
471
472 /* TODO: locking around NV access */
473 f = fopen(NVRAM_PATH, "w+b");
474 if (!f)
475 return -1;
476
477 if (0 != fseek(f, offs, SEEK_SET) ||
478 1 != fread(vnc.raw, VBNV_BLOCK_SIZE, 1, f)) {
479 goto VbSetNvCleanup;
480 }
481
482 if (0 != VbNvSetup(&vnc))
483 goto VbSetNvCleanup;
484 i = VbNvSet(&vnc, param, (uint32_t)value);
485 if (0 != VbNvTeardown(&vnc))
486 goto VbSetNvCleanup;
487 if (0 != i)
488 goto VbSetNvCleanup;
489
490 if (vnc.raw_changed) {
491 if (0 != fseek(f, offs, SEEK_SET) ||
492 1 != fwrite(vnc.raw, VBNV_BLOCK_SIZE, 1, f))
493 goto VbSetNvCleanup;
494 }
495
496 /* Success */
497 retval = 0;
498
499VbSetNvCleanup:
500 fclose(f);
501 /* TODO: release lock */
502 return retval;
503}
504
505
Randall Spanglerb47ed5a2011-02-23 13:05:40 -0800506/* Read the recovery reason. Returns the reason code or -1 if error. */
507int VbGetRecoveryReason(void) {
508 int value;
509
510 /* Try reading type from BINF.4 */
511 value = ReadFileInt(ACPI_BINF_PATH ".4");
512 if (-1 != value)
513 return value;
514
515 /* Fall back to BINF.0 for legacy systems like Mario. */
516 switch(ReadFileInt(ACPI_BINF_PATH ".0")) {
517 case BINF0_NORMAL:
518 case BINF0_DEVELOPER:
519 return VBNV_RECOVERY_NOT_REQUESTED;
520 case BINF0_RECOVERY_BUTTON:
521 return VBNV_RECOVERY_RO_MANUAL;
522 case BINF0_RECOVERY_DEV_SCREEN_KEY:
523 return VBNV_RECOVERY_RW_DEV_SCREEN;
524 case BINF0_RECOVERY_RW_FW_BAD:
525 case BINF0_RECOVERY_NO_OS:
526 return VBNV_RECOVERY_RW_NO_OS;
527 case BINF0_RECOVERY_BAD_OS:
528 return VBNV_RECOVERY_RW_INVALID_OS;
529 case BINF0_RECOVERY_OS_INITIATED:
530 return VBNV_RECOVERY_LEGACY;
531 default:
532 /* Other values don't map cleanly to firmware type. */
533 return -1;
534 }
535}
536
537
538/* Read the active main firmware type into the destination buffer.
539 * Passed the destination and its size. Returns the destination, or
540 * NULL if error. */
541const char* VbReadMainFwType(char* dest, int size) {
542
543 /* Try reading type from BINF.3 */
544 switch(ReadFileInt(ACPI_BINF_PATH ".3")) {
Randall Spangler196e1772011-03-10 11:31:06 -0800545 case BINF3_RECOVERY:
Randall Spanglerb47ed5a2011-02-23 13:05:40 -0800546 return StrCopy(dest, "recovery", size);
Randall Spangler196e1772011-03-10 11:31:06 -0800547 case BINF3_NORMAL:
Randall Spanglerb47ed5a2011-02-23 13:05:40 -0800548 return StrCopy(dest, "normal", size);
Randall Spangler196e1772011-03-10 11:31:06 -0800549 case BINF3_DEVELOPER:
Randall Spanglerb47ed5a2011-02-23 13:05:40 -0800550 return StrCopy(dest, "developer", size);
551 default:
552 break; /* Fall through to legacy handling */
553 }
554
555 /* Fall back to BINF.0 for legacy systems like Mario. */
556 switch(ReadFileInt(ACPI_BINF_PATH ".0")) {
557 case -1:
558 /* Both BINF.0 and BINF.3 are missing, so this isn't Chrome OS
559 * firmware. */
560 return StrCopy(dest, "nonchrome", size);
561 case BINF0_NORMAL:
562 return StrCopy(dest, "normal", size);
563 case BINF0_DEVELOPER:
564 return StrCopy(dest, "developer", size);
565 case BINF0_RECOVERY_BUTTON:
566 case BINF0_RECOVERY_DEV_SCREEN_KEY:
567 case BINF0_RECOVERY_RW_FW_BAD:
568 case BINF0_RECOVERY_NO_OS:
569 case BINF0_RECOVERY_BAD_OS:
570 case BINF0_RECOVERY_OS_INITIATED:
571 case BINF0_RECOVERY_TPM_ERROR:
572 /* Assorted flavors of recovery boot reason. */
573 return StrCopy(dest, "recovery", size);
574 default:
575 /* Other values don't map cleanly to firmware type. */
576 return NULL;
577 }
578}
579
580
Randall Spangler196e1772011-03-10 11:31:06 -0800581/* Determine whether OS-level debugging should be allowed. Passed the
582 * destination and its size. Returns 1 if yes, 0 if no, -1 if error. */
583int VbGetCrosDebug(void) {
584 FILE* f = NULL;
585 char buf[4096] = "";
586 int binf3;
587 char *t, *saveptr;
588
589 /* Try reading firmware type from BINF.3. */
590 binf3 = ReadFileInt(ACPI_BINF_PATH ".3");
591 if (BINF3_RECOVERY == binf3)
592 return 0; /* Recovery mode never allows debug. */
593 else if (BINF3_DEVELOPER == binf3)
594 return 1; /* Developer firmware always allows debug. */
595
596 /* Normal new firmware, older ChromeOS firmware, or non-Chrome firmware.
Randall Spangler227f7922011-03-11 13:34:56 -0800597 * For all these cases, check /proc/cmdline for cros_[no]debug. */
Randall Spangler196e1772011-03-10 11:31:06 -0800598 f = fopen(KERNEL_CMDLINE_PATH, "rt");
599 if (f) {
600 if (NULL == fgets(buf, sizeof(buf), f))
601 *buf = 0;
602 fclose(f);
603 }
604 for (t = strtok_r(buf, " ", &saveptr); t; t=strtok_r(NULL, " ", &saveptr)) {
605 if (0 == strcmp(t, "cros_debug"))
606 return 1;
Randall Spangler227f7922011-03-11 13:34:56 -0800607 else if (0 == strcmp(t, "cros_nodebug"))
608 return 0;
Randall Spangler196e1772011-03-10 11:31:06 -0800609 }
610
611 /* Normal new firmware or older Chrome OS firmware allows debug if the
612 * dev switch is on. */
613 if (1 == ReadFileBit(ACPI_CHSW_PATH, CHSW_DEV_BOOT))
614 return 1;
615
616 /* All other cases disallow debug. */
617 return 0;
618}
619
Randall Spanglerb47ed5a2011-02-23 13:05:40 -0800620
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700621char* GetVdatString(char* dest, int size, VdatStringField field)
622{
623 VbSharedDataHeader* sh;
624 AcpiBuffer* ab = VbGetBuffer(ACPI_VDAT_PATH);
625 if (!ab)
626 return NULL;
627
628 sh = (VbSharedDataHeader*)ab->buffer;
629
630 switch (field) {
631 case VDAT_STRING_TIMERS:
632 snprintf(dest, size,
633 "LFS=%" PRIu64 ",%" PRIu64
634 " LF=%" PRIu64 ",%" PRIu64
635 " LK=%" PRIu64 ",%" PRIu64,
636 sh->timer_load_firmware_start_enter,
637 sh->timer_load_firmware_start_exit,
638 sh->timer_load_firmware_enter,
639 sh->timer_load_firmware_exit,
640 sh->timer_load_kernel_enter,
641 sh->timer_load_kernel_exit);
642 break;
643
644 case VDAT_STRING_LOAD_FIRMWARE_DEBUG:
645 snprintf(dest, size,
646 "check=%d,%d index=0x%02x tpmver=0x%x lowestver=0x%x",
647 sh->check_fw_a_result,
648 sh->check_fw_b_result,
649 sh->firmware_index,
650 sh->fw_version_tpm_start,
651 sh->fw_version_lowest);
652 break;
653
654 default:
655 Free(ab);
656 return NULL;
657 }
658
659 Free(ab);
660 return dest;
661}
662
663
664int GetVdatInt(VdatIntField field) {
665 VbSharedDataHeader* sh;
666 AcpiBuffer* ab = VbGetBuffer(ACPI_VDAT_PATH);
667 int value = -1;
668
669 if (!ab)
670 return -1;
671
672 sh = (VbSharedDataHeader*)ab->buffer;
673
674 switch (field) {
675 case VDAT_INT_FLAGS:
676 value = (int)sh->flags;
677 break;
Randall Spangler5ac39bf2011-03-17 17:58:56 -0700678 case VDAT_INT_FW_VERSION_TPM:
679 value = (int)sh->fw_version_tpm;
680 break;
681 case VDAT_INT_KERNEL_VERSION_TPM:
682 value = (int)sh->kernel_version_tpm;
683 break;
Randall Spanglercabe6b32011-03-18 12:44:27 -0700684 case VDAT_INT_TRIED_FIRMWARE_B:
685 value = (sh->flags & VBSD_FWB_TRIED ? 1 : 0);
686 break;
687 case VDAT_INT_KERNEL_KEY_VERIFIED:
688 value = (sh->flags & VBSD_KERNEL_KEY_VERIFIED ? 1 : 0);
689 break;
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700690 }
691
692 Free(ab);
693 return value;
694}
695
696
Randall Spangler54218662011-02-07 11:20:20 -0800697/* Read a system property integer.
698 *
699 * Returns the property value, or -1 if error. */
700int VbGetSystemPropertyInt(const char* name) {
Randall Spanglerc80fe652011-02-17 11:06:47 -0800701 int value = -1;
Randall Spangler54218662011-02-07 11:20:20 -0800702
Randall Spanglere73302c2011-02-18 14:53:01 -0800703 /* Switch positions */
Randall Spangler54218662011-02-07 11:20:20 -0800704 if (!strcasecmp(name,"devsw_cur")) {
Randall Spanglerc80fe652011-02-17 11:06:47 -0800705 value = ReadGpio(GPIO_SIGNAL_TYPE_DEV);
Randall Spangler54218662011-02-07 11:20:20 -0800706 } else if (!strcasecmp(name,"devsw_boot")) {
Randall Spanglerc80fe652011-02-17 11:06:47 -0800707 value = ReadFileBit(ACPI_CHSW_PATH, CHSW_DEV_BOOT);
Randall Spangler54218662011-02-07 11:20:20 -0800708 } else if (!strcasecmp(name,"recoverysw_cur")) {
Randall Spanglerc80fe652011-02-17 11:06:47 -0800709 value = ReadGpio(GPIO_SIGNAL_TYPE_RECOVERY);
Randall Spangler54218662011-02-07 11:20:20 -0800710 } else if (!strcasecmp(name,"recoverysw_boot")) {
Randall Spanglerc80fe652011-02-17 11:06:47 -0800711 value = ReadFileBit(ACPI_CHSW_PATH, CHSW_RECOVERY_BOOT);
Randall Spangler54218662011-02-07 11:20:20 -0800712 } else if (!strcasecmp(name,"recoverysw_ec_boot")) {
Randall Spanglerc80fe652011-02-17 11:06:47 -0800713 value = ReadFileBit(ACPI_CHSW_PATH, CHSW_RECOVERY_EC_BOOT);
Randall Spangler54218662011-02-07 11:20:20 -0800714 } else if (!strcasecmp(name,"wpsw_cur")) {
Randall Spanglerc80fe652011-02-17 11:06:47 -0800715 value = ReadGpio(GPIO_SIGNAL_TYPE_WP);
716 if (-1 != value && FwidStartsWith("Mario."))
717 value = 1 - value; /* Mario reports this backwards */
Randall Spangler54218662011-02-07 11:20:20 -0800718 } else if (!strcasecmp(name,"wpsw_boot")) {
Randall Spanglerc80fe652011-02-17 11:06:47 -0800719 value = ReadFileBit(ACPI_CHSW_PATH, CHSW_WP_BOOT);
720 if (-1 != value && FwidStartsWith("Mario."))
721 value = 1 - value; /* Mario reports this backwards */
722 }
Randall Spanglerb47ed5a2011-02-23 13:05:40 -0800723 /* Saved memory is at a fixed location for all H2C BIOS. If the CHSW
724 * path exists in sysfs, it's a H2C BIOS. */
725 else if (!strcasecmp(name,"savedmem_base")) {
726 return (-1 == ReadFileInt(ACPI_CHSW_PATH) ? -1 : 0x00F00000);
727 } else if (!strcasecmp(name,"savedmem_size")) {
728 return (-1 == ReadFileInt(ACPI_CHSW_PATH) ? -1 : 0x00100000);
729 }
Randall Spangler17260282011-02-25 12:06:26 -0800730 /* NV storage values with no defaults for older BIOS. */
Randall Spanglercabe6b32011-03-18 12:44:27 -0700731 else if (!strcasecmp(name,"kern_nv")) {
Randall Spangler618d17d2011-03-01 10:33:11 -0800732 value = VbGetNvStorage(VBNV_KERNEL_FIELD);
Randall Spanglerb4167142011-03-01 13:04:22 -0800733 } else if (!strcasecmp(name,"nvram_cleared")) {
734 value = VbGetNvStorage(VBNV_KERNEL_SETTINGS_RESET);
Randall Spanglerb17e8d32011-03-15 09:50:38 -0700735 } else if (!strcasecmp(name,"vbtest_errfunc")) {
736 value = VbGetNvStorage(VBNV_TEST_ERROR_FUNC);
737 } else if (!strcasecmp(name,"vbtest_errno")) {
738 value = VbGetNvStorage(VBNV_TEST_ERROR_NUM);
Randall Spangler17260282011-02-25 12:06:26 -0800739 }
Randall Spangler0f8ffb12011-02-25 09:50:54 -0800740 /* NV storage values. If unable to get from NV storage, fall back to the
741 * CMOS reboot field used by older BIOS. */
Randall Spanglere73302c2011-02-18 14:53:01 -0800742 else if (!strcasecmp(name,"recovery_request")) {
Randall Spangler0f8ffb12011-02-25 09:50:54 -0800743 value = VbGetNvStorage(VBNV_RECOVERY_REQUEST);
744 if (-1 == value)
745 value = VbGetCmosRebootField(CMOSRF_RECOVERY);
Randall Spanglere73302c2011-02-18 14:53:01 -0800746 } else if (!strcasecmp(name,"dbg_reset")) {
Randall Spangler0f8ffb12011-02-25 09:50:54 -0800747 value = VbGetNvStorage(VBNV_DEBUG_RESET_MODE);
748 if (-1 == value)
749 value = VbGetCmosRebootField(CMOSRF_DEBUG_RESET);
Randall Spanglere73302c2011-02-18 14:53:01 -0800750 } else if (!strcasecmp(name,"fwb_tries")) {
Randall Spangler0f8ffb12011-02-25 09:50:54 -0800751 value = VbGetNvStorage(VBNV_TRY_B_COUNT);
752 if (-1 == value)
753 value = VbGetCmosRebootField(CMOSRF_TRY_B);
Randall Spanglere73302c2011-02-18 14:53:01 -0800754 }
Randall Spanglerb47ed5a2011-02-23 13:05:40 -0800755 /* Other parameters */
756 else if (!strcasecmp(name,"recovery_reason")) {
757 return VbGetRecoveryReason();
Randall Spangler2b59a072011-02-24 11:17:24 -0800758 } else if (!strcasecmp(name,"fmap_base")) {
759 value = ReadFileInt(ACPI_FMAP_PATH);
Randall Spangler196e1772011-03-10 11:31:06 -0800760 } else if (!strcasecmp(name,"cros_debug")) {
761 value = VbGetCrosDebug();
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700762 } else if (!strcasecmp(name,"vdat_flags")) {
763 value = GetVdatInt(VDAT_INT_FLAGS);
Randall Spangler5ac39bf2011-03-17 17:58:56 -0700764 } else if (!strcasecmp(name,"tpm_fwver")) {
765 value = GetVdatInt(VDAT_INT_FW_VERSION_TPM);
766 } else if (!strcasecmp(name,"tpm_kernver")) {
767 value = GetVdatInt(VDAT_INT_KERNEL_VERSION_TPM);
Randall Spanglercabe6b32011-03-18 12:44:27 -0700768 } else if (!strcasecmp(name,"tried_fwb")) {
769 value = GetVdatInt(VDAT_INT_TRIED_FIRMWARE_B);
Randall Spanglerb47ed5a2011-02-23 13:05:40 -0800770 }
Randall Spangler54218662011-02-07 11:20:20 -0800771
Randall Spanglerc80fe652011-02-17 11:06:47 -0800772 return value;
Randall Spangler54218662011-02-07 11:20:20 -0800773}
774
Randall Spangler54218662011-02-07 11:20:20 -0800775/* Read a system property string into a destination buffer of the specified
776 * size.
777 *
778 * Returns the passed buffer, or NULL if error. */
779const char* VbGetSystemPropertyString(const char* name, char* dest, int size) {
780
781 if (!strcasecmp(name,"hwid")) {
782 return ReadFileString(dest, size, ACPI_BASE_PATH "/HWID");
783 } else if (!strcasecmp(name,"fwid")) {
784 return ReadFileString(dest, size, ACPI_BASE_PATH "/FWID");
785 } else if (!strcasecmp(name,"ro_fwid")) {
786 return ReadFileString(dest, size, ACPI_BASE_PATH "/FRID");
Randall Spanglerb47ed5a2011-02-23 13:05:40 -0800787 } else if (!strcasecmp(name,"mainfw_act")) {
788 switch(ReadFileInt(ACPI_BINF_PATH ".1")) {
789 case 0:
790 return StrCopy(dest, "recovery", size);
791 case 1:
792 return StrCopy(dest, "A", size);
793 case 2:
794 return StrCopy(dest, "B", size);
795 default:
796 return NULL;
797 }
798 } else if (!strcasecmp(name,"mainfw_type")) {
799 return VbReadMainFwType(dest, size);
800 } else if (!strcasecmp(name,"ecfw_act")) {
801 switch(ReadFileInt(ACPI_BINF_PATH ".2")) {
802 case 0:
803 return StrCopy(dest, "RO", size);
804 case 1:
805 return StrCopy(dest, "RW", size);
806 default:
807 return NULL;
808 }
Randall Spangler17260282011-02-25 12:06:26 -0800809 } else if (!strcasecmp(name,"kernkey_vfy")) {
Randall Spanglercabe6b32011-03-18 12:44:27 -0700810 switch(GetVdatInt(VDAT_INT_KERNEL_KEY_VERIFIED)) {
Randall Spangler17260282011-02-25 12:06:26 -0800811 case 0:
812 return "hash";
813 case 1:
814 return "sig";
815 default:
816 return NULL;
817 }
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700818 } else if (!strcasecmp(name, "vdat_timers")) {
819 return GetVdatString(dest, size, VDAT_STRING_TIMERS);
820 } else if (!strcasecmp(name, "vdat_lfdebug")) {
821 return GetVdatString(dest, size, VDAT_STRING_LOAD_FIRMWARE_DEBUG);
Randall Spangler54218662011-02-07 11:20:20 -0800822 } else
823 return NULL;
Randall Spangler54218662011-02-07 11:20:20 -0800824}
825
826
827/* Set a system property integer.
828 *
829 * Returns 0 if success, -1 if error. */
830int VbSetSystemPropertyInt(const char* name, int value) {
831
Randall Spangler618d17d2011-03-01 10:33:11 -0800832 /* NV storage values with no defaults for older BIOS. */
Randall Spanglerb4167142011-03-01 13:04:22 -0800833 if (!strcasecmp(name,"nvram_cleared")) {
834 /* Can only clear this flag; it's set inside the NV storage library. */
835 return VbSetNvStorage(VBNV_KERNEL_SETTINGS_RESET, 0);
836 } else if (!strcasecmp(name,"kern_nv")) {
Randall Spangler618d17d2011-03-01 10:33:11 -0800837 return VbSetNvStorage(VBNV_KERNEL_FIELD, value);
Randall Spanglerb17e8d32011-03-15 09:50:38 -0700838 } else if (!strcasecmp(name,"vbtest_errfunc")) {
839 return VbSetNvStorage(VBNV_TEST_ERROR_FUNC, value);
840 } else if (!strcasecmp(name,"vbtest_errno")) {
841 return VbSetNvStorage(VBNV_TEST_ERROR_NUM, value);
Randall Spangler618d17d2011-03-01 10:33:11 -0800842 }
Randall Spangler0f8ffb12011-02-25 09:50:54 -0800843 /* NV storage values. If unable to get from NV storage, fall back to the
844 * CMOS reboot field used by older BIOS. */
Randall Spangler618d17d2011-03-01 10:33:11 -0800845 else if (!strcasecmp(name,"recovery_request")) {
Randall Spangler0f8ffb12011-02-25 09:50:54 -0800846 if (0 == VbSetNvStorage(VBNV_RECOVERY_REQUEST, value))
847 return 0;
Randall Spanglere73302c2011-02-18 14:53:01 -0800848 return VbSetCmosRebootField(CMOSRF_RECOVERY, value);
849 } else if (!strcasecmp(name,"dbg_reset")) {
Randall Spangler0f8ffb12011-02-25 09:50:54 -0800850 if (0 == VbSetNvStorage(VBNV_DEBUG_RESET_MODE, value))
851 return 0;
852 return VbSetCmosRebootField(CMOSRF_DEBUG_RESET, value);
Randall Spanglere73302c2011-02-18 14:53:01 -0800853 } else if (!strcasecmp(name,"fwb_tries")) {
Randall Spangler0f8ffb12011-02-25 09:50:54 -0800854 if (0 == VbSetNvStorage(VBNV_TRY_B_COUNT, value))
855 return 0;
Randall Spanglere73302c2011-02-18 14:53:01 -0800856 return VbSetCmosRebootField(CMOSRF_TRY_B, value);
857 }
858
Randall Spangler54218662011-02-07 11:20:20 -0800859 return -1;
860}
861
862
863/* Set a system property string.
864 *
865 * Returns 0 if success, -1 if error. */
866int VbSetSystemPropertyString(const char* name, const char* value) {
867
868 /* TODO: support setting */
869 return -1;
870}