blob: 9bea492a3ec49de26652db0a8ff6437a2d68bfd6 [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 Spangler54218662011-02-07 11:20:20 -080019
20/* ACPI constants from Chrome OS Main Processor Firmware Spec */
21/* GPIO signal types */
22#define GPIO_SIGNAL_TYPE_RECOVERY 1
23#define GPIO_SIGNAL_TYPE_DEV 2
24#define GPIO_SIGNAL_TYPE_WP 3
25/* CHSW bitflags */
26#define CHSW_RECOVERY_BOOT 0x00000002
27#define CHSW_RECOVERY_EC_BOOT 0x00000004
28#define CHSW_DEV_BOOT 0x00000020
29#define CHSW_WP_BOOT 0x00000200
Randall Spanglere73302c2011-02-18 14:53:01 -080030/* CMOS reboot field bitflags */
31#define CMOSRF_RECOVERY 0x80
32#define CMOSRF_DEBUG_RESET 0x40
33#define CMOSRF_TRY_B 0x20
Randall Spanglerb47ed5a2011-02-23 13:05:40 -080034/* Boot reasons from BINF.0, from early H2C firmware */
35/* Unknown */
36#define BINF0_UNKNOWN 0
37/* Normal boot to Chrome OS */
38#define BINF0_NORMAL 1
39/* Developer mode boot (developer mode warning displayed) */
40#define BINF0_DEVELOPER 2
41/* Recovery initiated by user, using recovery button */
42#define BINF0_RECOVERY_BUTTON 3
43/* Recovery initiated by user pressing a key at developer mode warning
44 * screen */
45#define BINF0_RECOVERY_DEV_SCREEN_KEY 4
46/* Recovery caused by BIOS failed signature check (neither rewritable
47 * firmware was valid) */
48#define BINF0_RECOVERY_RW_FW_BAD 5
49/* Recovery caused by no OS kernel detected */
50#define BINF0_RECOVERY_NO_OS 6
51/* Recovery caused by OS kernel failed signature check */
52#define BINF0_RECOVERY_BAD_OS 7
53/* Recovery initiated by OS */
54#define BINF0_RECOVERY_OS_INITIATED 8
55/* OS-initiated S3 diagnostic path (debug mode boot) */
56#define BINF0_S3_DIAGNOSTIC_PATH 9
57/* S3 resume failed */
58#define BINF0_S3_RESUME_FAILED 10
59/* Recovery caused by TPM error */
60#define BINF0_RECOVERY_TPM_ERROR 11
Randall Spangler196e1772011-03-10 11:31:06 -080061/* Firmware types from BINF.3 */
62#define BINF3_RECOVERY 0
63#define BINF3_NORMAL 1
64#define BINF3_DEVELOPER 2
Randall Spangler54218662011-02-07 11:20:20 -080065
66/* Base name for ACPI files */
67#define ACPI_BASE_PATH "/sys/devices/platform/chromeos_acpi"
68/* Paths for frequently used ACPI files */
Randall Spanglerb47ed5a2011-02-23 13:05:40 -080069#define ACPI_BINF_PATH ACPI_BASE_PATH "/BINF"
Randall Spangler54218662011-02-07 11:20:20 -080070#define ACPI_CHNV_PATH ACPI_BASE_PATH "/CHNV"
71#define ACPI_CHSW_PATH ACPI_BASE_PATH "/CHSW"
Randall Spangler2b59a072011-02-24 11:17:24 -080072#define ACPI_FMAP_PATH ACPI_BASE_PATH "/FMAP"
Randall Spangler54218662011-02-07 11:20:20 -080073#define ACPI_GPIO_PATH ACPI_BASE_PATH "/GPIO"
Randall Spangler0f8ffb12011-02-25 09:50:54 -080074#define ACPI_VBNV_PATH ACPI_BASE_PATH "/VBNV"
Vadim Bendebury20084232011-03-15 09:29:48 -070075#define ACPI_VDAT_PATH ACPI_BASE_PATH "/VDAT"
Randall Spangler54218662011-02-07 11:20:20 -080076
77/* Base name for GPIO files */
78#define GPIO_BASE_PATH "/sys/class/gpio"
79#define GPIO_EXPORT_PATH GPIO_BASE_PATH "/export"
80
Randall Spangler196e1772011-03-10 11:31:06 -080081/* Filename for NVRAM file */
Randall Spanglere73302c2011-02-18 14:53:01 -080082#define NVRAM_PATH "/dev/nvram"
83
Randall Spangler196e1772011-03-10 11:31:06 -080084/* Filename for kernel command line */
85#define KERNEL_CMDLINE_PATH "/proc/cmdline"
86
Vadim Bendebury20084232011-03-15 09:29:48 -070087/* A structure to contain buffer data retrieved from the ACPI. */
88typedef struct {
89 int buffer_size;
90 void* buffer;
91} AcpiBuffer;
92
Randall Spanglerb47ed5a2011-02-23 13:05:40 -080093
94/* Copy up to dest_size-1 characters from src to dest, ensuring null
95 termination (which strncpy() doesn't do). Returns the destination
96 string. */
97char* StrCopy(char* dest, const char* src, int dest_size) {
98 strncpy(dest, src, dest_size);
99 dest[dest_size - 1] = '\0';
100 return dest;
101}
102
103
Randall Spangler54218662011-02-07 11:20:20 -0800104/* Read a string from a file. Passed the destination, dest size, and
105 * filename to read.
106 *
107 * Returns the destination, or NULL if error. */
108char* ReadFileString(char* dest, int size, const char* filename) {
109 char* got;
110 FILE* f;
111
112 f = fopen(filename, "rt");
113 if (!f)
114 return NULL;
115
116 got = fgets(dest, size, f);
117 fclose(f);
118 return got;
119}
120
121
122/* Read an integer from a file.
123 *
124 * Returns the parsed integer, or -1 if error. */
125int ReadFileInt(const char* filename) {
126 char buf[64];
127 int value;
128 char* e = NULL;
129
130 if (!ReadFileString(buf, sizeof(buf), filename))
131 return -1;
132
133 /* Convert to integer. Allow characters after the int ("123 blah"). */
134 value = strtol(buf, &e, 0);
135 if (e == buf)
136 return -1; /* No characters consumed, so conversion failed */
137
138 return value;
139}
140
141
142/* Check if a bit is set in a file which contains an integer.
143 *
144 * Returns 1 if the bit is set, 0 if clear, or -1 if error. */
145int ReadFileBit(const char* filename, int bitmask) {
146 int value = ReadFileInt(filename);
147 if (value == -1)
148 return -1;
149 else return (value & bitmask ? 1 : 0);
150}
151
152
Randall Spanglerc80fe652011-02-17 11:06:47 -0800153/* Return true if the FWID starts with the specified string. */
154static int FwidStartsWith(const char *start) {
155 char fwid[128];
156 if (!VbGetSystemPropertyString("fwid", fwid, sizeof(fwid)))
157 return 0;
158
159 return 0 == strncmp(fwid, start, strlen(start));
160}
161
162
Randall Spangler54218662011-02-07 11:20:20 -0800163/* Read a GPIO of the specified signal type (see ACPI GPIO SignalType).
164 *
165 * Returns 1 if the signal is asserted, 0 if not asserted, or -1 if error. */
166int ReadGpio(int signal_type) {
167 char name[128];
168 int index = 0;
169 int gpio_type;
170 int active_high;
171 int controller_offset;
172 char controller_name[128];
173 int value;
174
175 /* Scan GPIO.* to find a matching signal type */
176 for (index = 0; ; index++) {
177 snprintf(name, sizeof(name), "%s.%d/GPIO.0", ACPI_GPIO_PATH, index);
178 gpio_type = ReadFileInt(name);
179 if (gpio_type == signal_type)
180 break;
181 else if (gpio_type == -1)
182 return -1; /* Ran out of GPIOs before finding a match */
183 }
184
185 /* Read attributes and controller info for the GPIO */
186 snprintf(name, sizeof(name), "%s.%d/GPIO.1", ACPI_GPIO_PATH, index);
187 active_high = ReadFileBit(name, 0x00000001);
188 snprintf(name, sizeof(name), "%s.%d/GPIO.2", ACPI_GPIO_PATH, index);
189 controller_offset = ReadFileInt(name);
190 if (active_high == -1 || controller_offset == -1)
191 return -1; /* Missing needed info */
192
193 /* We only support the NM10 for now */
194 snprintf(name, sizeof(name), "%s.%d/GPIO.3", ACPI_GPIO_PATH, index);
195 if (!ReadFileString(controller_name, sizeof(controller_name), name))
196 return -1;
197 if (0 != strcmp(controller_name, "NM10"))
198 return -1;
199
200 /* Assume the NM10 has offset 192 */
201 /* TODO: should really check gpiochipNNN/label to see if it's the
202 * address we expect for the NM10, and then read the offset from
203 * gpiochipNNN/base. */
204 controller_offset += 192;
205
206 /* Try reading the GPIO value */
207 snprintf(name, sizeof(name), "%s/gpio%d/value",
208 GPIO_BASE_PATH, controller_offset);
209 value = ReadFileInt(name);
210
211 if (value == -1) {
212 /* Try exporting the GPIO */
213 FILE* f = fopen(GPIO_EXPORT_PATH, "wt");
214 if (!f)
215 return -1;
216 fprintf(f, "%d", controller_offset);
217 fclose(f);
218
219 /* Try re-reading the GPIO value */
220 value = ReadFileInt(name);
221 }
222
223 if (value == -1)
224 return -1;
225
226 /* Compare the GPIO value with the active value and return 1 if match. */
227 return (value == active_high ? 1 : 0);
228}
229
230
Randall Spanglere73302c2011-02-18 14:53:01 -0800231/* Read the CMOS reboot field in NVRAM.
232 *
233 * Returns 0 if the mask is clear in the field, 1 if set, or -1 if error. */
234int VbGetCmosRebootField(uint8_t mask) {
235 FILE* f;
236 int chnv, nvbyte;
237
238 /* Get the byte offset from CHNV */
239 chnv = ReadFileInt(ACPI_CHNV_PATH);
240 if (chnv == -1)
241 return -1;
242
243 f = fopen(NVRAM_PATH, "rb");
244 if (!f)
245 return -1;
246
247 if (0 != fseek(f, chnv, SEEK_SET) || EOF == (nvbyte = fgetc(f))) {
248 fclose(f);
249 return -1;
250 }
251
252 fclose(f);
253 return (nvbyte & mask ? 1 : 0);
254}
255
256
257/* Write the CMOS reboot field in NVRAM.
258 *
259 * Sets (value=0) or clears (value!=0) the mask in the byte.
260 *
261 * Returns 0 if success, or -1 if error. */
262int VbSetCmosRebootField(uint8_t mask, int value) {
263 FILE* f;
264 int chnv, nvbyte;
265
266 /* Get the byte offset from CHNV */
267 chnv = ReadFileInt(ACPI_CHNV_PATH);
268 if (chnv == -1)
269 return -1;
270
271 f = fopen(NVRAM_PATH, "w+b");
272 if (!f)
273 return -1;
274
275 /* Read the current value */
276 if (0 != fseek(f, chnv, SEEK_SET) || EOF == (nvbyte = fgetc(f))) {
277 fclose(f);
278 return -1;
279 }
280
281 /* Set/clear the mask */
282 if (value)
283 nvbyte |= mask;
284 else
285 nvbyte &= ~mask;
286
287 /* Write the byte back */
288 if (0 != fseek(f, chnv, SEEK_SET) || EOF == (fputc(nvbyte, f))) {
289 fclose(f);
290 return -1;
291 }
292
293 /* Success */
294 fclose(f);
295 return 0;
296}
297
Vadim Bendebury20084232011-03-15 09:29:48 -0700298/*
299 * Get buffer data from ACPI.
300 *
301 * Buffer data is expected to be represented by a file which is a text dump of
302 * the buffer, representing each byte by two hex numbers, space and newline
303 * separated.
304 *
305 * Input - ACPI file name to get data from.
306 *
307 * Output: a pointer to AcpiBuffer structure containing the binary
308 * representation of the data. The caller is responsible for
309 * deallocating the pointer, this will take care of both the structure
310 * and the buffer. Null in case of error.
311 */
312
313AcpiBuffer* VbGetBuffer(const char* filename)
314{
315 FILE* f = NULL;
316 char* file_buffer = NULL;
317 AcpiBuffer* acpi_buffer = NULL;
318 AcpiBuffer* return_value = NULL;
319
320 do {
321 struct stat fs;
322 unsigned char* output_ptr;
323 int rv, i, real_size;
324
325 rv = stat(filename, &fs);
326 if (rv || !S_ISREG(fs.st_mode))
327 break;
328
329 f = fopen(filename, "r");
330 if (!f)
331 break;
332
333 file_buffer = Malloc(fs.st_size + 1);
334 if (!file_buffer)
335 break;
336
337
338 real_size = fread(file_buffer, 1, fs.st_size, f);
339 if (!real_size)
340 break;
341
342 /* each byte in the output will replace two characters and a space in the
343 * input, so the output size does not exceed input side/3 (a little less
344 * if account for newline characters).
345 */
346 acpi_buffer = Malloc(sizeof(AcpiBuffer) + real_size/3);
347
348 if (!acpi_buffer)
349 break;
350
351 file_buffer[real_size] = '\0';
352
353 acpi_buffer->buffer = acpi_buffer + 1;
354 acpi_buffer->buffer_size = 0;
355 output_ptr = acpi_buffer->buffer;
356
357 /* process the file contents */
358 for (i = 0; i < real_size; i++) {
359 char* base, *end;
360
361 base = file_buffer + i;
362
363 if (!isxdigit(*base))
364 continue;
365
366 output_ptr[acpi_buffer->buffer_size++] = strtol(base, &end, 16) & 0xff;
367
368 if ((end - base) != 2)
369 /* Input file format error */
370 break;
371
372 i += 2; /* skip the second character and the following space */
373 }
374
375 if (i == real_size) {
376 /* all is well */
377 return_value = acpi_buffer;
378 acpi_buffer = NULL; /* prevent it from deallocating */
379 }
380 } while(0);
381
382 /* wrap up */
383 if (f)
384 fclose(f);
385
386 if (file_buffer)
387 Free(file_buffer);
388
389 if (acpi_buffer)
390 Free(acpi_buffer);
391
392 return return_value;
393}
Randall Spangler0f8ffb12011-02-25 09:50:54 -0800394
395/* Read an integer property from VbNvStorage.
396 *
397 * Returns the parameter value, or -1 if error. */
398int VbGetNvStorage(VbNvParam param) {
399 FILE* f;
400 VbNvContext vnc;
401 int offs;
402 uint32_t value;
403 int retval;
404
405 /* Get the byte offset from VBNV */
406 offs = ReadFileInt(ACPI_VBNV_PATH ".0");
407 if (offs == -1)
408 return -1;
409 if (VBNV_BLOCK_SIZE > ReadFileInt(ACPI_VBNV_PATH ".1"))
410 return -1; /* NV storage block is too small */
411
412 /* TODO: locking around NV access */
413 f = fopen(NVRAM_PATH, "rb");
414 if (!f)
415 return -1;
416
417 if (0 != fseek(f, offs, SEEK_SET) ||
418 1 != fread(vnc.raw, VBNV_BLOCK_SIZE, 1, f)) {
419 fclose(f);
420 return -1;
421 }
422
423 fclose(f);
424
425 if (0 != VbNvSetup(&vnc))
426 return -1;
427 retval = VbNvGet(&vnc, param, &value);
428 if (0 != VbNvTeardown(&vnc))
429 return -1;
430 if (0 != retval)
431 return -1;
432
433 /* TODO: If vnc.raw_changed, attempt to reopen NVRAM for write and
434 * save the new defaults. If we're able to, log. */
435 /* TODO: release lock */
436
437 return (int)value;
438}
439
440
441/* Write an integer property to VbNvStorage.
442 *
443 * Returns 0 if success, -1 if error. */
444int VbSetNvStorage(VbNvParam param, int value) {
445 FILE* f;
446 VbNvContext vnc;
447 int offs;
448 int retval = -1;
449 int i;
450
451 /* Get the byte offset from VBNV */
452 offs = ReadFileInt(ACPI_VBNV_PATH ".0");
453 if (offs == -1)
454 return -1;
455 if (VBNV_BLOCK_SIZE > ReadFileInt(ACPI_VBNV_PATH ".1"))
456 return -1; /* NV storage block is too small */
457
458 /* TODO: locking around NV access */
459 f = fopen(NVRAM_PATH, "w+b");
460 if (!f)
461 return -1;
462
463 if (0 != fseek(f, offs, SEEK_SET) ||
464 1 != fread(vnc.raw, VBNV_BLOCK_SIZE, 1, f)) {
465 goto VbSetNvCleanup;
466 }
467
468 if (0 != VbNvSetup(&vnc))
469 goto VbSetNvCleanup;
470 i = VbNvSet(&vnc, param, (uint32_t)value);
471 if (0 != VbNvTeardown(&vnc))
472 goto VbSetNvCleanup;
473 if (0 != i)
474 goto VbSetNvCleanup;
475
476 if (vnc.raw_changed) {
477 if (0 != fseek(f, offs, SEEK_SET) ||
478 1 != fwrite(vnc.raw, VBNV_BLOCK_SIZE, 1, f))
479 goto VbSetNvCleanup;
480 }
481
482 /* Success */
483 retval = 0;
484
485VbSetNvCleanup:
486 fclose(f);
487 /* TODO: release lock */
488 return retval;
489}
490
491
Randall Spanglerb47ed5a2011-02-23 13:05:40 -0800492/* Read the recovery reason. Returns the reason code or -1 if error. */
493int VbGetRecoveryReason(void) {
494 int value;
495
496 /* Try reading type from BINF.4 */
497 value = ReadFileInt(ACPI_BINF_PATH ".4");
498 if (-1 != value)
499 return value;
500
501 /* Fall back to BINF.0 for legacy systems like Mario. */
502 switch(ReadFileInt(ACPI_BINF_PATH ".0")) {
503 case BINF0_NORMAL:
504 case BINF0_DEVELOPER:
505 return VBNV_RECOVERY_NOT_REQUESTED;
506 case BINF0_RECOVERY_BUTTON:
507 return VBNV_RECOVERY_RO_MANUAL;
508 case BINF0_RECOVERY_DEV_SCREEN_KEY:
509 return VBNV_RECOVERY_RW_DEV_SCREEN;
510 case BINF0_RECOVERY_RW_FW_BAD:
511 case BINF0_RECOVERY_NO_OS:
512 return VBNV_RECOVERY_RW_NO_OS;
513 case BINF0_RECOVERY_BAD_OS:
514 return VBNV_RECOVERY_RW_INVALID_OS;
515 case BINF0_RECOVERY_OS_INITIATED:
516 return VBNV_RECOVERY_LEGACY;
517 default:
518 /* Other values don't map cleanly to firmware type. */
519 return -1;
520 }
521}
522
523
524/* Read the active main firmware type into the destination buffer.
525 * Passed the destination and its size. Returns the destination, or
526 * NULL if error. */
527const char* VbReadMainFwType(char* dest, int size) {
528
529 /* Try reading type from BINF.3 */
530 switch(ReadFileInt(ACPI_BINF_PATH ".3")) {
Randall Spangler196e1772011-03-10 11:31:06 -0800531 case BINF3_RECOVERY:
Randall Spanglerb47ed5a2011-02-23 13:05:40 -0800532 return StrCopy(dest, "recovery", size);
Randall Spangler196e1772011-03-10 11:31:06 -0800533 case BINF3_NORMAL:
Randall Spanglerb47ed5a2011-02-23 13:05:40 -0800534 return StrCopy(dest, "normal", size);
Randall Spangler196e1772011-03-10 11:31:06 -0800535 case BINF3_DEVELOPER:
Randall Spanglerb47ed5a2011-02-23 13:05:40 -0800536 return StrCopy(dest, "developer", size);
537 default:
538 break; /* Fall through to legacy handling */
539 }
540
541 /* Fall back to BINF.0 for legacy systems like Mario. */
542 switch(ReadFileInt(ACPI_BINF_PATH ".0")) {
543 case -1:
544 /* Both BINF.0 and BINF.3 are missing, so this isn't Chrome OS
545 * firmware. */
546 return StrCopy(dest, "nonchrome", size);
547 case BINF0_NORMAL:
548 return StrCopy(dest, "normal", size);
549 case BINF0_DEVELOPER:
550 return StrCopy(dest, "developer", size);
551 case BINF0_RECOVERY_BUTTON:
552 case BINF0_RECOVERY_DEV_SCREEN_KEY:
553 case BINF0_RECOVERY_RW_FW_BAD:
554 case BINF0_RECOVERY_NO_OS:
555 case BINF0_RECOVERY_BAD_OS:
556 case BINF0_RECOVERY_OS_INITIATED:
557 case BINF0_RECOVERY_TPM_ERROR:
558 /* Assorted flavors of recovery boot reason. */
559 return StrCopy(dest, "recovery", size);
560 default:
561 /* Other values don't map cleanly to firmware type. */
562 return NULL;
563 }
564}
565
566
Randall Spangler196e1772011-03-10 11:31:06 -0800567/* Determine whether OS-level debugging should be allowed. Passed the
568 * destination and its size. Returns 1 if yes, 0 if no, -1 if error. */
569int VbGetCrosDebug(void) {
570 FILE* f = NULL;
571 char buf[4096] = "";
572 int binf3;
573 char *t, *saveptr;
574
575 /* Try reading firmware type from BINF.3. */
576 binf3 = ReadFileInt(ACPI_BINF_PATH ".3");
577 if (BINF3_RECOVERY == binf3)
578 return 0; /* Recovery mode never allows debug. */
579 else if (BINF3_DEVELOPER == binf3)
580 return 1; /* Developer firmware always allows debug. */
581
582 /* Normal new firmware, older ChromeOS firmware, or non-Chrome firmware.
Randall Spangler227f7922011-03-11 13:34:56 -0800583 * For all these cases, check /proc/cmdline for cros_[no]debug. */
Randall Spangler196e1772011-03-10 11:31:06 -0800584 f = fopen(KERNEL_CMDLINE_PATH, "rt");
585 if (f) {
586 if (NULL == fgets(buf, sizeof(buf), f))
587 *buf = 0;
588 fclose(f);
589 }
590 for (t = strtok_r(buf, " ", &saveptr); t; t=strtok_r(NULL, " ", &saveptr)) {
591 if (0 == strcmp(t, "cros_debug"))
592 return 1;
Randall Spangler227f7922011-03-11 13:34:56 -0800593 else if (0 == strcmp(t, "cros_nodebug"))
594 return 0;
Randall Spangler196e1772011-03-10 11:31:06 -0800595 }
596
597 /* Normal new firmware or older Chrome OS firmware allows debug if the
598 * dev switch is on. */
599 if (1 == ReadFileBit(ACPI_CHSW_PATH, CHSW_DEV_BOOT))
600 return 1;
601
602 /* All other cases disallow debug. */
603 return 0;
604}
605
Randall Spanglerb47ed5a2011-02-23 13:05:40 -0800606
Randall Spangler54218662011-02-07 11:20:20 -0800607/* Read a system property integer.
608 *
609 * Returns the property value, or -1 if error. */
610int VbGetSystemPropertyInt(const char* name) {
Randall Spanglerc80fe652011-02-17 11:06:47 -0800611 int value = -1;
Randall Spangler54218662011-02-07 11:20:20 -0800612
Randall Spanglere73302c2011-02-18 14:53:01 -0800613 /* Switch positions */
Randall Spangler54218662011-02-07 11:20:20 -0800614 if (!strcasecmp(name,"devsw_cur")) {
Randall Spanglerc80fe652011-02-17 11:06:47 -0800615 value = ReadGpio(GPIO_SIGNAL_TYPE_DEV);
Randall Spangler54218662011-02-07 11:20:20 -0800616 } else if (!strcasecmp(name,"devsw_boot")) {
Randall Spanglerc80fe652011-02-17 11:06:47 -0800617 value = ReadFileBit(ACPI_CHSW_PATH, CHSW_DEV_BOOT);
Randall Spangler54218662011-02-07 11:20:20 -0800618 } else if (!strcasecmp(name,"recoverysw_cur")) {
Randall Spanglerc80fe652011-02-17 11:06:47 -0800619 value = ReadGpio(GPIO_SIGNAL_TYPE_RECOVERY);
Randall Spangler54218662011-02-07 11:20:20 -0800620 } else if (!strcasecmp(name,"recoverysw_boot")) {
Randall Spanglerc80fe652011-02-17 11:06:47 -0800621 value = ReadFileBit(ACPI_CHSW_PATH, CHSW_RECOVERY_BOOT);
Randall Spangler54218662011-02-07 11:20:20 -0800622 } else if (!strcasecmp(name,"recoverysw_ec_boot")) {
Randall Spanglerc80fe652011-02-17 11:06:47 -0800623 value = ReadFileBit(ACPI_CHSW_PATH, CHSW_RECOVERY_EC_BOOT);
Randall Spangler54218662011-02-07 11:20:20 -0800624 } else if (!strcasecmp(name,"wpsw_cur")) {
Randall Spanglerc80fe652011-02-17 11:06:47 -0800625 value = ReadGpio(GPIO_SIGNAL_TYPE_WP);
626 if (-1 != value && FwidStartsWith("Mario."))
627 value = 1 - value; /* Mario reports this backwards */
Randall Spangler54218662011-02-07 11:20:20 -0800628 } else if (!strcasecmp(name,"wpsw_boot")) {
Randall Spanglerc80fe652011-02-17 11:06:47 -0800629 value = ReadFileBit(ACPI_CHSW_PATH, CHSW_WP_BOOT);
630 if (-1 != value && FwidStartsWith("Mario."))
631 value = 1 - value; /* Mario reports this backwards */
632 }
Randall Spanglerb47ed5a2011-02-23 13:05:40 -0800633 /* Saved memory is at a fixed location for all H2C BIOS. If the CHSW
634 * path exists in sysfs, it's a H2C BIOS. */
635 else if (!strcasecmp(name,"savedmem_base")) {
636 return (-1 == ReadFileInt(ACPI_CHSW_PATH) ? -1 : 0x00F00000);
637 } else if (!strcasecmp(name,"savedmem_size")) {
638 return (-1 == ReadFileInt(ACPI_CHSW_PATH) ? -1 : 0x00100000);
639 }
Randall Spangler17260282011-02-25 12:06:26 -0800640 /* NV storage values with no defaults for older BIOS. */
641 else if (!strcasecmp(name,"tried_fwb")) {
Randall Spangler92e378e2011-02-25 13:56:53 -0800642 value = VbGetNvStorage(VBNV_TRIED_FIRMWARE_B);
Randall Spangler618d17d2011-03-01 10:33:11 -0800643 } else if (!strcasecmp(name,"kern_nv")) {
644 value = VbGetNvStorage(VBNV_KERNEL_FIELD);
Randall Spanglerb4167142011-03-01 13:04:22 -0800645 } else if (!strcasecmp(name,"nvram_cleared")) {
646 value = VbGetNvStorage(VBNV_KERNEL_SETTINGS_RESET);
Randall Spangler17260282011-02-25 12:06:26 -0800647 }
Randall Spangler0f8ffb12011-02-25 09:50:54 -0800648 /* NV storage values. If unable to get from NV storage, fall back to the
649 * CMOS reboot field used by older BIOS. */
Randall Spanglere73302c2011-02-18 14:53:01 -0800650 else if (!strcasecmp(name,"recovery_request")) {
Randall Spangler0f8ffb12011-02-25 09:50:54 -0800651 value = VbGetNvStorage(VBNV_RECOVERY_REQUEST);
652 if (-1 == value)
653 value = VbGetCmosRebootField(CMOSRF_RECOVERY);
Randall Spanglere73302c2011-02-18 14:53:01 -0800654 } else if (!strcasecmp(name,"dbg_reset")) {
Randall Spangler0f8ffb12011-02-25 09:50:54 -0800655 value = VbGetNvStorage(VBNV_DEBUG_RESET_MODE);
656 if (-1 == value)
657 value = VbGetCmosRebootField(CMOSRF_DEBUG_RESET);
Randall Spanglere73302c2011-02-18 14:53:01 -0800658 } else if (!strcasecmp(name,"fwb_tries")) {
Randall Spangler0f8ffb12011-02-25 09:50:54 -0800659 value = VbGetNvStorage(VBNV_TRY_B_COUNT);
660 if (-1 == value)
661 value = VbGetCmosRebootField(CMOSRF_TRY_B);
Randall Spanglere73302c2011-02-18 14:53:01 -0800662 }
Randall Spanglerb47ed5a2011-02-23 13:05:40 -0800663 /* Other parameters */
664 else if (!strcasecmp(name,"recovery_reason")) {
665 return VbGetRecoveryReason();
Randall Spangler2b59a072011-02-24 11:17:24 -0800666 } else if (!strcasecmp(name,"fmap_base")) {
667 value = ReadFileInt(ACPI_FMAP_PATH);
Randall Spangler196e1772011-03-10 11:31:06 -0800668 } else if (!strcasecmp(name,"cros_debug")) {
669 value = VbGetCrosDebug();
Randall Spanglerb47ed5a2011-02-23 13:05:40 -0800670 }
Randall Spangler54218662011-02-07 11:20:20 -0800671
Randall Spanglerc80fe652011-02-17 11:06:47 -0800672 return value;
Randall Spangler54218662011-02-07 11:20:20 -0800673}
674
Vadim Bendebury20084232011-03-15 09:29:48 -0700675/* This function is just an example illustrating the use of VbGetBuffer(). It
676 * converts the binary contents of the buffer into a space delimetered hex
677 * string. It is expected to be replaced with a function which has knowledge
678 * of the buffer data structure.
679 */
680char* GetVdatBuffer(void)
681{
682 char* buffer, *src, *p;
683 int i;
684
685 AcpiBuffer* ab = VbGetBuffer(ACPI_VDAT_PATH);
686 if (!ab)
687 return NULL;
688
689 buffer = Malloc(ab->buffer_size * 3 + 2);
690 p = buffer;
691 src = ab->buffer;
692 for (i = 0; i < ab->buffer_size; i++) {
693 snprintf(p, 4, " %2.2x", *src++);
694 p += 3;
695 }
696 *p = '\0';
697 Free(ab);
698 return buffer;
699}
Randall Spangler54218662011-02-07 11:20:20 -0800700
701/* Read a system property string into a destination buffer of the specified
702 * size.
703 *
704 * Returns the passed buffer, or NULL if error. */
705const char* VbGetSystemPropertyString(const char* name, char* dest, int size) {
706
707 if (!strcasecmp(name,"hwid")) {
708 return ReadFileString(dest, size, ACPI_BASE_PATH "/HWID");
709 } else if (!strcasecmp(name,"fwid")) {
710 return ReadFileString(dest, size, ACPI_BASE_PATH "/FWID");
711 } else if (!strcasecmp(name,"ro_fwid")) {
712 return ReadFileString(dest, size, ACPI_BASE_PATH "/FRID");
Randall Spanglerb47ed5a2011-02-23 13:05:40 -0800713 } else if (!strcasecmp(name,"mainfw_act")) {
714 switch(ReadFileInt(ACPI_BINF_PATH ".1")) {
715 case 0:
716 return StrCopy(dest, "recovery", size);
717 case 1:
718 return StrCopy(dest, "A", size);
719 case 2:
720 return StrCopy(dest, "B", size);
721 default:
722 return NULL;
723 }
724 } else if (!strcasecmp(name,"mainfw_type")) {
725 return VbReadMainFwType(dest, size);
726 } else if (!strcasecmp(name,"ecfw_act")) {
727 switch(ReadFileInt(ACPI_BINF_PATH ".2")) {
728 case 0:
729 return StrCopy(dest, "RO", size);
730 case 1:
731 return StrCopy(dest, "RW", size);
732 default:
733 return NULL;
734 }
Randall Spangler17260282011-02-25 12:06:26 -0800735 } else if (!strcasecmp(name,"kernkey_vfy")) {
736 switch(VbGetNvStorage(VBNV_FW_VERIFIED_KERNEL_KEY)) {
737 case 0:
738 return "hash";
739 case 1:
740 return "sig";
741 default:
742 return NULL;
743 }
Vadim Bendebury20084232011-03-15 09:29:48 -0700744 } else if (!strcasecmp(name, "vdat")) {
745 return GetVdatBuffer();
Randall Spangler54218662011-02-07 11:20:20 -0800746 } else
747 return NULL;
Randall Spangler54218662011-02-07 11:20:20 -0800748}
749
750
751/* Set a system property integer.
752 *
753 * Returns 0 if success, -1 if error. */
754int VbSetSystemPropertyInt(const char* name, int value) {
755
Randall Spangler618d17d2011-03-01 10:33:11 -0800756 /* NV storage values with no defaults for older BIOS. */
Randall Spanglerb4167142011-03-01 13:04:22 -0800757 if (!strcasecmp(name,"nvram_cleared")) {
758 /* Can only clear this flag; it's set inside the NV storage library. */
759 return VbSetNvStorage(VBNV_KERNEL_SETTINGS_RESET, 0);
760 } else if (!strcasecmp(name,"kern_nv")) {
Randall Spangler618d17d2011-03-01 10:33:11 -0800761 return VbSetNvStorage(VBNV_KERNEL_FIELD, value);
762 }
Randall Spangler0f8ffb12011-02-25 09:50:54 -0800763 /* NV storage values. If unable to get from NV storage, fall back to the
764 * CMOS reboot field used by older BIOS. */
Randall Spangler618d17d2011-03-01 10:33:11 -0800765 else if (!strcasecmp(name,"recovery_request")) {
Randall Spangler0f8ffb12011-02-25 09:50:54 -0800766 if (0 == VbSetNvStorage(VBNV_RECOVERY_REQUEST, value))
767 return 0;
Randall Spanglere73302c2011-02-18 14:53:01 -0800768 return VbSetCmosRebootField(CMOSRF_RECOVERY, value);
769 } else if (!strcasecmp(name,"dbg_reset")) {
Randall Spangler0f8ffb12011-02-25 09:50:54 -0800770 if (0 == VbSetNvStorage(VBNV_DEBUG_RESET_MODE, value))
771 return 0;
772 return VbSetCmosRebootField(CMOSRF_DEBUG_RESET, value);
Randall Spanglere73302c2011-02-18 14:53:01 -0800773 } else if (!strcasecmp(name,"fwb_tries")) {
Randall Spangler0f8ffb12011-02-25 09:50:54 -0800774 if (0 == VbSetNvStorage(VBNV_TRY_B_COUNT, value))
775 return 0;
Randall Spanglere73302c2011-02-18 14:53:01 -0800776 return VbSetCmosRebootField(CMOSRF_TRY_B, value);
777 }
778
Randall Spangler54218662011-02-07 11:20:20 -0800779 return -1;
780}
781
782
783/* Set a system property string.
784 *
785 * Returns 0 if success, -1 if error. */
786int VbSetSystemPropertyString(const char* name, const char* value) {
787
788 /* TODO: support setting */
789 return -1;
790}