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