blob: 04dc9756446878651dc0226ffa226e5c716fce38 [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>
8
9#include "host_common.h"
10
11#include "crossystem.h"
12#include "utility.h"
13#include "vboot_common.h"
Randall Spanglere73302c2011-02-18 14:53:01 -080014#include "vboot_nvstorage.h"
Randall Spangler54218662011-02-07 11:20:20 -080015
16/* ACPI constants from Chrome OS Main Processor Firmware Spec */
17/* GPIO signal types */
18#define GPIO_SIGNAL_TYPE_RECOVERY 1
19#define GPIO_SIGNAL_TYPE_DEV 2
20#define GPIO_SIGNAL_TYPE_WP 3
21/* CHSW bitflags */
22#define CHSW_RECOVERY_BOOT 0x00000002
23#define CHSW_RECOVERY_EC_BOOT 0x00000004
24#define CHSW_DEV_BOOT 0x00000020
25#define CHSW_WP_BOOT 0x00000200
Randall Spanglere73302c2011-02-18 14:53:01 -080026/* CMOS reboot field bitflags */
27#define CMOSRF_RECOVERY 0x80
28#define CMOSRF_DEBUG_RESET 0x40
29#define CMOSRF_TRY_B 0x20
Randall Spanglerb47ed5a2011-02-23 13:05:40 -080030/* Boot reasons from BINF.0, from early H2C firmware */
31/* Unknown */
32#define BINF0_UNKNOWN 0
33/* Normal boot to Chrome OS */
34#define BINF0_NORMAL 1
35/* Developer mode boot (developer mode warning displayed) */
36#define BINF0_DEVELOPER 2
37/* Recovery initiated by user, using recovery button */
38#define BINF0_RECOVERY_BUTTON 3
39/* Recovery initiated by user pressing a key at developer mode warning
40 * screen */
41#define BINF0_RECOVERY_DEV_SCREEN_KEY 4
42/* Recovery caused by BIOS failed signature check (neither rewritable
43 * firmware was valid) */
44#define BINF0_RECOVERY_RW_FW_BAD 5
45/* Recovery caused by no OS kernel detected */
46#define BINF0_RECOVERY_NO_OS 6
47/* Recovery caused by OS kernel failed signature check */
48#define BINF0_RECOVERY_BAD_OS 7
49/* Recovery initiated by OS */
50#define BINF0_RECOVERY_OS_INITIATED 8
51/* OS-initiated S3 diagnostic path (debug mode boot) */
52#define BINF0_S3_DIAGNOSTIC_PATH 9
53/* S3 resume failed */
54#define BINF0_S3_RESUME_FAILED 10
55/* Recovery caused by TPM error */
56#define BINF0_RECOVERY_TPM_ERROR 11
Randall Spangler196e1772011-03-10 11:31:06 -080057/* Firmware types from BINF.3 */
58#define BINF3_RECOVERY 0
59#define BINF3_NORMAL 1
60#define BINF3_DEVELOPER 2
Randall Spangler54218662011-02-07 11:20:20 -080061
62/* Base name for ACPI files */
63#define ACPI_BASE_PATH "/sys/devices/platform/chromeos_acpi"
64/* Paths for frequently used ACPI files */
Randall Spanglerb47ed5a2011-02-23 13:05:40 -080065#define ACPI_BINF_PATH ACPI_BASE_PATH "/BINF"
Randall Spangler54218662011-02-07 11:20:20 -080066#define ACPI_CHNV_PATH ACPI_BASE_PATH "/CHNV"
67#define ACPI_CHSW_PATH ACPI_BASE_PATH "/CHSW"
Randall Spangler2b59a072011-02-24 11:17:24 -080068#define ACPI_FMAP_PATH ACPI_BASE_PATH "/FMAP"
Randall Spangler54218662011-02-07 11:20:20 -080069#define ACPI_GPIO_PATH ACPI_BASE_PATH "/GPIO"
Randall Spangler0f8ffb12011-02-25 09:50:54 -080070#define ACPI_VBNV_PATH ACPI_BASE_PATH "/VBNV"
Randall Spangler54218662011-02-07 11:20:20 -080071
72/* Base name for GPIO files */
73#define GPIO_BASE_PATH "/sys/class/gpio"
74#define GPIO_EXPORT_PATH GPIO_BASE_PATH "/export"
75
Randall Spangler196e1772011-03-10 11:31:06 -080076/* Filename for NVRAM file */
Randall Spanglere73302c2011-02-18 14:53:01 -080077#define NVRAM_PATH "/dev/nvram"
78
Randall Spangler196e1772011-03-10 11:31:06 -080079/* Filename for kernel command line */
80#define KERNEL_CMDLINE_PATH "/proc/cmdline"
81
Randall Spanglerb47ed5a2011-02-23 13:05:40 -080082
83/* Copy up to dest_size-1 characters from src to dest, ensuring null
84 termination (which strncpy() doesn't do). Returns the destination
85 string. */
86char* StrCopy(char* dest, const char* src, int dest_size) {
87 strncpy(dest, src, dest_size);
88 dest[dest_size - 1] = '\0';
89 return dest;
90}
91
92
Randall Spangler54218662011-02-07 11:20:20 -080093/* Read a string from a file. Passed the destination, dest size, and
94 * filename to read.
95 *
96 * Returns the destination, or NULL if error. */
97char* ReadFileString(char* dest, int size, const char* filename) {
98 char* got;
99 FILE* f;
100
101 f = fopen(filename, "rt");
102 if (!f)
103 return NULL;
104
105 got = fgets(dest, size, f);
106 fclose(f);
107 return got;
108}
109
110
111/* Read an integer from a file.
112 *
113 * Returns the parsed integer, or -1 if error. */
114int ReadFileInt(const char* filename) {
115 char buf[64];
116 int value;
117 char* e = NULL;
118
119 if (!ReadFileString(buf, sizeof(buf), filename))
120 return -1;
121
122 /* Convert to integer. Allow characters after the int ("123 blah"). */
123 value = strtol(buf, &e, 0);
124 if (e == buf)
125 return -1; /* No characters consumed, so conversion failed */
126
127 return value;
128}
129
130
131/* Check if a bit is set in a file which contains an integer.
132 *
133 * Returns 1 if the bit is set, 0 if clear, or -1 if error. */
134int ReadFileBit(const char* filename, int bitmask) {
135 int value = ReadFileInt(filename);
136 if (value == -1)
137 return -1;
138 else return (value & bitmask ? 1 : 0);
139}
140
141
Randall Spanglerc80fe652011-02-17 11:06:47 -0800142/* Return true if the FWID starts with the specified string. */
143static int FwidStartsWith(const char *start) {
144 char fwid[128];
145 if (!VbGetSystemPropertyString("fwid", fwid, sizeof(fwid)))
146 return 0;
147
148 return 0 == strncmp(fwid, start, strlen(start));
149}
150
151
Randall Spangler54218662011-02-07 11:20:20 -0800152/* Read a GPIO of the specified signal type (see ACPI GPIO SignalType).
153 *
154 * Returns 1 if the signal is asserted, 0 if not asserted, or -1 if error. */
155int ReadGpio(int signal_type) {
156 char name[128];
157 int index = 0;
158 int gpio_type;
159 int active_high;
160 int controller_offset;
161 char controller_name[128];
162 int value;
163
164 /* Scan GPIO.* to find a matching signal type */
165 for (index = 0; ; index++) {
166 snprintf(name, sizeof(name), "%s.%d/GPIO.0", ACPI_GPIO_PATH, index);
167 gpio_type = ReadFileInt(name);
168 if (gpio_type == signal_type)
169 break;
170 else if (gpio_type == -1)
171 return -1; /* Ran out of GPIOs before finding a match */
172 }
173
174 /* Read attributes and controller info for the GPIO */
175 snprintf(name, sizeof(name), "%s.%d/GPIO.1", ACPI_GPIO_PATH, index);
176 active_high = ReadFileBit(name, 0x00000001);
177 snprintf(name, sizeof(name), "%s.%d/GPIO.2", ACPI_GPIO_PATH, index);
178 controller_offset = ReadFileInt(name);
179 if (active_high == -1 || controller_offset == -1)
180 return -1; /* Missing needed info */
181
182 /* We only support the NM10 for now */
183 snprintf(name, sizeof(name), "%s.%d/GPIO.3", ACPI_GPIO_PATH, index);
184 if (!ReadFileString(controller_name, sizeof(controller_name), name))
185 return -1;
186 if (0 != strcmp(controller_name, "NM10"))
187 return -1;
188
189 /* Assume the NM10 has offset 192 */
190 /* TODO: should really check gpiochipNNN/label to see if it's the
191 * address we expect for the NM10, and then read the offset from
192 * gpiochipNNN/base. */
193 controller_offset += 192;
194
195 /* Try reading the GPIO value */
196 snprintf(name, sizeof(name), "%s/gpio%d/value",
197 GPIO_BASE_PATH, controller_offset);
198 value = ReadFileInt(name);
199
200 if (value == -1) {
201 /* Try exporting the GPIO */
202 FILE* f = fopen(GPIO_EXPORT_PATH, "wt");
203 if (!f)
204 return -1;
205 fprintf(f, "%d", controller_offset);
206 fclose(f);
207
208 /* Try re-reading the GPIO value */
209 value = ReadFileInt(name);
210 }
211
212 if (value == -1)
213 return -1;
214
215 /* Compare the GPIO value with the active value and return 1 if match. */
216 return (value == active_high ? 1 : 0);
217}
218
219
Randall Spanglere73302c2011-02-18 14:53:01 -0800220/* Read the CMOS reboot field in NVRAM.
221 *
222 * Returns 0 if the mask is clear in the field, 1 if set, or -1 if error. */
223int VbGetCmosRebootField(uint8_t mask) {
224 FILE* f;
225 int chnv, nvbyte;
226
227 /* Get the byte offset from CHNV */
228 chnv = ReadFileInt(ACPI_CHNV_PATH);
229 if (chnv == -1)
230 return -1;
231
232 f = fopen(NVRAM_PATH, "rb");
233 if (!f)
234 return -1;
235
236 if (0 != fseek(f, chnv, SEEK_SET) || EOF == (nvbyte = fgetc(f))) {
237 fclose(f);
238 return -1;
239 }
240
241 fclose(f);
242 return (nvbyte & mask ? 1 : 0);
243}
244
245
246/* Write the CMOS reboot field in NVRAM.
247 *
248 * Sets (value=0) or clears (value!=0) the mask in the byte.
249 *
250 * Returns 0 if success, or -1 if error. */
251int VbSetCmosRebootField(uint8_t mask, int value) {
252 FILE* f;
253 int chnv, nvbyte;
254
255 /* Get the byte offset from CHNV */
256 chnv = ReadFileInt(ACPI_CHNV_PATH);
257 if (chnv == -1)
258 return -1;
259
260 f = fopen(NVRAM_PATH, "w+b");
261 if (!f)
262 return -1;
263
264 /* Read the current value */
265 if (0 != fseek(f, chnv, SEEK_SET) || EOF == (nvbyte = fgetc(f))) {
266 fclose(f);
267 return -1;
268 }
269
270 /* Set/clear the mask */
271 if (value)
272 nvbyte |= mask;
273 else
274 nvbyte &= ~mask;
275
276 /* Write the byte back */
277 if (0 != fseek(f, chnv, SEEK_SET) || EOF == (fputc(nvbyte, f))) {
278 fclose(f);
279 return -1;
280 }
281
282 /* Success */
283 fclose(f);
284 return 0;
285}
286
Randall Spangler0f8ffb12011-02-25 09:50:54 -0800287
288/* Read an integer property from VbNvStorage.
289 *
290 * Returns the parameter value, or -1 if error. */
291int VbGetNvStorage(VbNvParam param) {
292 FILE* f;
293 VbNvContext vnc;
294 int offs;
295 uint32_t value;
296 int retval;
297
298 /* Get the byte offset from VBNV */
299 offs = ReadFileInt(ACPI_VBNV_PATH ".0");
300 if (offs == -1)
301 return -1;
302 if (VBNV_BLOCK_SIZE > ReadFileInt(ACPI_VBNV_PATH ".1"))
303 return -1; /* NV storage block is too small */
304
305 /* TODO: locking around NV access */
306 f = fopen(NVRAM_PATH, "rb");
307 if (!f)
308 return -1;
309
310 if (0 != fseek(f, offs, SEEK_SET) ||
311 1 != fread(vnc.raw, VBNV_BLOCK_SIZE, 1, f)) {
312 fclose(f);
313 return -1;
314 }
315
316 fclose(f);
317
318 if (0 != VbNvSetup(&vnc))
319 return -1;
320 retval = VbNvGet(&vnc, param, &value);
321 if (0 != VbNvTeardown(&vnc))
322 return -1;
323 if (0 != retval)
324 return -1;
325
326 /* TODO: If vnc.raw_changed, attempt to reopen NVRAM for write and
327 * save the new defaults. If we're able to, log. */
328 /* TODO: release lock */
329
330 return (int)value;
331}
332
333
334/* Write an integer property to VbNvStorage.
335 *
336 * Returns 0 if success, -1 if error. */
337int VbSetNvStorage(VbNvParam param, int value) {
338 FILE* f;
339 VbNvContext vnc;
340 int offs;
341 int retval = -1;
342 int i;
343
344 /* Get the byte offset from VBNV */
345 offs = ReadFileInt(ACPI_VBNV_PATH ".0");
346 if (offs == -1)
347 return -1;
348 if (VBNV_BLOCK_SIZE > ReadFileInt(ACPI_VBNV_PATH ".1"))
349 return -1; /* NV storage block is too small */
350
351 /* TODO: locking around NV access */
352 f = fopen(NVRAM_PATH, "w+b");
353 if (!f)
354 return -1;
355
356 if (0 != fseek(f, offs, SEEK_SET) ||
357 1 != fread(vnc.raw, VBNV_BLOCK_SIZE, 1, f)) {
358 goto VbSetNvCleanup;
359 }
360
361 if (0 != VbNvSetup(&vnc))
362 goto VbSetNvCleanup;
363 i = VbNvSet(&vnc, param, (uint32_t)value);
364 if (0 != VbNvTeardown(&vnc))
365 goto VbSetNvCleanup;
366 if (0 != i)
367 goto VbSetNvCleanup;
368
369 if (vnc.raw_changed) {
370 if (0 != fseek(f, offs, SEEK_SET) ||
371 1 != fwrite(vnc.raw, VBNV_BLOCK_SIZE, 1, f))
372 goto VbSetNvCleanup;
373 }
374
375 /* Success */
376 retval = 0;
377
378VbSetNvCleanup:
379 fclose(f);
380 /* TODO: release lock */
381 return retval;
382}
383
384
Randall Spanglerb47ed5a2011-02-23 13:05:40 -0800385/* Read the recovery reason. Returns the reason code or -1 if error. */
386int VbGetRecoveryReason(void) {
387 int value;
388
389 /* Try reading type from BINF.4 */
390 value = ReadFileInt(ACPI_BINF_PATH ".4");
391 if (-1 != value)
392 return value;
393
394 /* Fall back to BINF.0 for legacy systems like Mario. */
395 switch(ReadFileInt(ACPI_BINF_PATH ".0")) {
396 case BINF0_NORMAL:
397 case BINF0_DEVELOPER:
398 return VBNV_RECOVERY_NOT_REQUESTED;
399 case BINF0_RECOVERY_BUTTON:
400 return VBNV_RECOVERY_RO_MANUAL;
401 case BINF0_RECOVERY_DEV_SCREEN_KEY:
402 return VBNV_RECOVERY_RW_DEV_SCREEN;
403 case BINF0_RECOVERY_RW_FW_BAD:
404 case BINF0_RECOVERY_NO_OS:
405 return VBNV_RECOVERY_RW_NO_OS;
406 case BINF0_RECOVERY_BAD_OS:
407 return VBNV_RECOVERY_RW_INVALID_OS;
408 case BINF0_RECOVERY_OS_INITIATED:
409 return VBNV_RECOVERY_LEGACY;
410 default:
411 /* Other values don't map cleanly to firmware type. */
412 return -1;
413 }
414}
415
416
417/* Read the active main firmware type into the destination buffer.
418 * Passed the destination and its size. Returns the destination, or
419 * NULL if error. */
420const char* VbReadMainFwType(char* dest, int size) {
421
422 /* Try reading type from BINF.3 */
423 switch(ReadFileInt(ACPI_BINF_PATH ".3")) {
Randall Spangler196e1772011-03-10 11:31:06 -0800424 case BINF3_RECOVERY:
Randall Spanglerb47ed5a2011-02-23 13:05:40 -0800425 return StrCopy(dest, "recovery", size);
Randall Spangler196e1772011-03-10 11:31:06 -0800426 case BINF3_NORMAL:
Randall Spanglerb47ed5a2011-02-23 13:05:40 -0800427 return StrCopy(dest, "normal", size);
Randall Spangler196e1772011-03-10 11:31:06 -0800428 case BINF3_DEVELOPER:
Randall Spanglerb47ed5a2011-02-23 13:05:40 -0800429 return StrCopy(dest, "developer", size);
430 default:
431 break; /* Fall through to legacy handling */
432 }
433
434 /* Fall back to BINF.0 for legacy systems like Mario. */
435 switch(ReadFileInt(ACPI_BINF_PATH ".0")) {
436 case -1:
437 /* Both BINF.0 and BINF.3 are missing, so this isn't Chrome OS
438 * firmware. */
439 return StrCopy(dest, "nonchrome", size);
440 case BINF0_NORMAL:
441 return StrCopy(dest, "normal", size);
442 case BINF0_DEVELOPER:
443 return StrCopy(dest, "developer", size);
444 case BINF0_RECOVERY_BUTTON:
445 case BINF0_RECOVERY_DEV_SCREEN_KEY:
446 case BINF0_RECOVERY_RW_FW_BAD:
447 case BINF0_RECOVERY_NO_OS:
448 case BINF0_RECOVERY_BAD_OS:
449 case BINF0_RECOVERY_OS_INITIATED:
450 case BINF0_RECOVERY_TPM_ERROR:
451 /* Assorted flavors of recovery boot reason. */
452 return StrCopy(dest, "recovery", size);
453 default:
454 /* Other values don't map cleanly to firmware type. */
455 return NULL;
456 }
457}
458
459
Randall Spangler196e1772011-03-10 11:31:06 -0800460/* Determine whether OS-level debugging should be allowed. Passed the
461 * destination and its size. Returns 1 if yes, 0 if no, -1 if error. */
462int VbGetCrosDebug(void) {
463 FILE* f = NULL;
464 char buf[4096] = "";
465 int binf3;
466 char *t, *saveptr;
467
468 /* Try reading firmware type from BINF.3. */
469 binf3 = ReadFileInt(ACPI_BINF_PATH ".3");
470 if (BINF3_RECOVERY == binf3)
471 return 0; /* Recovery mode never allows debug. */
472 else if (BINF3_DEVELOPER == binf3)
473 return 1; /* Developer firmware always allows debug. */
474
475 /* Normal new firmware, older ChromeOS firmware, or non-Chrome firmware.
Randall Spangler227f7922011-03-11 13:34:56 -0800476 * For all these cases, check /proc/cmdline for cros_[no]debug. */
Randall Spangler196e1772011-03-10 11:31:06 -0800477 f = fopen(KERNEL_CMDLINE_PATH, "rt");
478 if (f) {
479 if (NULL == fgets(buf, sizeof(buf), f))
480 *buf = 0;
481 fclose(f);
482 }
483 for (t = strtok_r(buf, " ", &saveptr); t; t=strtok_r(NULL, " ", &saveptr)) {
484 if (0 == strcmp(t, "cros_debug"))
485 return 1;
Randall Spangler227f7922011-03-11 13:34:56 -0800486 else if (0 == strcmp(t, "cros_nodebug"))
487 return 0;
Randall Spangler196e1772011-03-10 11:31:06 -0800488 }
489
490 /* Normal new firmware or older Chrome OS firmware allows debug if the
491 * dev switch is on. */
492 if (1 == ReadFileBit(ACPI_CHSW_PATH, CHSW_DEV_BOOT))
493 return 1;
494
495 /* All other cases disallow debug. */
496 return 0;
497}
498
Randall Spanglerb47ed5a2011-02-23 13:05:40 -0800499
Randall Spangler54218662011-02-07 11:20:20 -0800500/* Read a system property integer.
501 *
502 * Returns the property value, or -1 if error. */
503int VbGetSystemPropertyInt(const char* name) {
Randall Spanglerc80fe652011-02-17 11:06:47 -0800504 int value = -1;
Randall Spangler54218662011-02-07 11:20:20 -0800505
Randall Spanglere73302c2011-02-18 14:53:01 -0800506 /* Switch positions */
Randall Spangler54218662011-02-07 11:20:20 -0800507 if (!strcasecmp(name,"devsw_cur")) {
Randall Spanglerc80fe652011-02-17 11:06:47 -0800508 value = ReadGpio(GPIO_SIGNAL_TYPE_DEV);
Randall Spangler54218662011-02-07 11:20:20 -0800509 } else if (!strcasecmp(name,"devsw_boot")) {
Randall Spanglerc80fe652011-02-17 11:06:47 -0800510 value = ReadFileBit(ACPI_CHSW_PATH, CHSW_DEV_BOOT);
Randall Spangler54218662011-02-07 11:20:20 -0800511 } else if (!strcasecmp(name,"recoverysw_cur")) {
Randall Spanglerc80fe652011-02-17 11:06:47 -0800512 value = ReadGpio(GPIO_SIGNAL_TYPE_RECOVERY);
Randall Spangler54218662011-02-07 11:20:20 -0800513 } else if (!strcasecmp(name,"recoverysw_boot")) {
Randall Spanglerc80fe652011-02-17 11:06:47 -0800514 value = ReadFileBit(ACPI_CHSW_PATH, CHSW_RECOVERY_BOOT);
Randall Spangler54218662011-02-07 11:20:20 -0800515 } else if (!strcasecmp(name,"recoverysw_ec_boot")) {
Randall Spanglerc80fe652011-02-17 11:06:47 -0800516 value = ReadFileBit(ACPI_CHSW_PATH, CHSW_RECOVERY_EC_BOOT);
Randall Spangler54218662011-02-07 11:20:20 -0800517 } else if (!strcasecmp(name,"wpsw_cur")) {
Randall Spanglerc80fe652011-02-17 11:06:47 -0800518 value = ReadGpio(GPIO_SIGNAL_TYPE_WP);
519 if (-1 != value && FwidStartsWith("Mario."))
520 value = 1 - value; /* Mario reports this backwards */
Randall Spangler54218662011-02-07 11:20:20 -0800521 } else if (!strcasecmp(name,"wpsw_boot")) {
Randall Spanglerc80fe652011-02-17 11:06:47 -0800522 value = ReadFileBit(ACPI_CHSW_PATH, CHSW_WP_BOOT);
523 if (-1 != value && FwidStartsWith("Mario."))
524 value = 1 - value; /* Mario reports this backwards */
525 }
Randall Spanglerb47ed5a2011-02-23 13:05:40 -0800526 /* Saved memory is at a fixed location for all H2C BIOS. If the CHSW
527 * path exists in sysfs, it's a H2C BIOS. */
528 else if (!strcasecmp(name,"savedmem_base")) {
529 return (-1 == ReadFileInt(ACPI_CHSW_PATH) ? -1 : 0x00F00000);
530 } else if (!strcasecmp(name,"savedmem_size")) {
531 return (-1 == ReadFileInt(ACPI_CHSW_PATH) ? -1 : 0x00100000);
532 }
Randall Spangler17260282011-02-25 12:06:26 -0800533 /* NV storage values with no defaults for older BIOS. */
534 else if (!strcasecmp(name,"tried_fwb")) {
Randall Spangler92e378e2011-02-25 13:56:53 -0800535 value = VbGetNvStorage(VBNV_TRIED_FIRMWARE_B);
Randall Spangler618d17d2011-03-01 10:33:11 -0800536 } else if (!strcasecmp(name,"kern_nv")) {
537 value = VbGetNvStorage(VBNV_KERNEL_FIELD);
Randall Spanglerb4167142011-03-01 13:04:22 -0800538 } else if (!strcasecmp(name,"nvram_cleared")) {
539 value = VbGetNvStorage(VBNV_KERNEL_SETTINGS_RESET);
Randall Spangler17260282011-02-25 12:06:26 -0800540 }
Randall Spangler0f8ffb12011-02-25 09:50:54 -0800541 /* NV storage values. If unable to get from NV storage, fall back to the
542 * CMOS reboot field used by older BIOS. */
Randall Spanglere73302c2011-02-18 14:53:01 -0800543 else if (!strcasecmp(name,"recovery_request")) {
Randall Spangler0f8ffb12011-02-25 09:50:54 -0800544 value = VbGetNvStorage(VBNV_RECOVERY_REQUEST);
545 if (-1 == value)
546 value = VbGetCmosRebootField(CMOSRF_RECOVERY);
Randall Spanglere73302c2011-02-18 14:53:01 -0800547 } else if (!strcasecmp(name,"dbg_reset")) {
Randall Spangler0f8ffb12011-02-25 09:50:54 -0800548 value = VbGetNvStorage(VBNV_DEBUG_RESET_MODE);
549 if (-1 == value)
550 value = VbGetCmosRebootField(CMOSRF_DEBUG_RESET);
Randall Spanglere73302c2011-02-18 14:53:01 -0800551 } else if (!strcasecmp(name,"fwb_tries")) {
Randall Spangler0f8ffb12011-02-25 09:50:54 -0800552 value = VbGetNvStorage(VBNV_TRY_B_COUNT);
553 if (-1 == value)
554 value = VbGetCmosRebootField(CMOSRF_TRY_B);
Randall Spanglere73302c2011-02-18 14:53:01 -0800555 }
Randall Spanglerb47ed5a2011-02-23 13:05:40 -0800556 /* Other parameters */
557 else if (!strcasecmp(name,"recovery_reason")) {
558 return VbGetRecoveryReason();
Randall Spangler2b59a072011-02-24 11:17:24 -0800559 } else if (!strcasecmp(name,"fmap_base")) {
560 value = ReadFileInt(ACPI_FMAP_PATH);
Randall Spangler196e1772011-03-10 11:31:06 -0800561 } else if (!strcasecmp(name,"cros_debug")) {
562 value = VbGetCrosDebug();
Randall Spanglerb47ed5a2011-02-23 13:05:40 -0800563 }
Randall Spangler54218662011-02-07 11:20:20 -0800564
Randall Spanglerc80fe652011-02-17 11:06:47 -0800565 return value;
Randall Spangler54218662011-02-07 11:20:20 -0800566}
567
568
569/* Read a system property string into a destination buffer of the specified
570 * size.
571 *
572 * Returns the passed buffer, or NULL if error. */
573const char* VbGetSystemPropertyString(const char* name, char* dest, int size) {
574
575 if (!strcasecmp(name,"hwid")) {
576 return ReadFileString(dest, size, ACPI_BASE_PATH "/HWID");
577 } else if (!strcasecmp(name,"fwid")) {
578 return ReadFileString(dest, size, ACPI_BASE_PATH "/FWID");
579 } else if (!strcasecmp(name,"ro_fwid")) {
580 return ReadFileString(dest, size, ACPI_BASE_PATH "/FRID");
Randall Spanglerb47ed5a2011-02-23 13:05:40 -0800581 } else if (!strcasecmp(name,"mainfw_act")) {
582 switch(ReadFileInt(ACPI_BINF_PATH ".1")) {
583 case 0:
584 return StrCopy(dest, "recovery", size);
585 case 1:
586 return StrCopy(dest, "A", size);
587 case 2:
588 return StrCopy(dest, "B", size);
589 default:
590 return NULL;
591 }
592 } else if (!strcasecmp(name,"mainfw_type")) {
593 return VbReadMainFwType(dest, size);
594 } else if (!strcasecmp(name,"ecfw_act")) {
595 switch(ReadFileInt(ACPI_BINF_PATH ".2")) {
596 case 0:
597 return StrCopy(dest, "RO", size);
598 case 1:
599 return StrCopy(dest, "RW", size);
600 default:
601 return NULL;
602 }
Randall Spangler17260282011-02-25 12:06:26 -0800603 } else if (!strcasecmp(name,"kernkey_vfy")) {
604 switch(VbGetNvStorage(VBNV_FW_VERIFIED_KERNEL_KEY)) {
605 case 0:
606 return "hash";
607 case 1:
608 return "sig";
609 default:
610 return NULL;
611 }
Randall Spangler54218662011-02-07 11:20:20 -0800612 } else
613 return NULL;
Randall Spangler54218662011-02-07 11:20:20 -0800614}
615
616
617/* Set a system property integer.
618 *
619 * Returns 0 if success, -1 if error. */
620int VbSetSystemPropertyInt(const char* name, int value) {
621
Randall Spangler618d17d2011-03-01 10:33:11 -0800622 /* NV storage values with no defaults for older BIOS. */
Randall Spanglerb4167142011-03-01 13:04:22 -0800623 if (!strcasecmp(name,"nvram_cleared")) {
624 /* Can only clear this flag; it's set inside the NV storage library. */
625 return VbSetNvStorage(VBNV_KERNEL_SETTINGS_RESET, 0);
626 } else if (!strcasecmp(name,"kern_nv")) {
Randall Spangler618d17d2011-03-01 10:33:11 -0800627 return VbSetNvStorage(VBNV_KERNEL_FIELD, value);
628 }
Randall Spangler0f8ffb12011-02-25 09:50:54 -0800629 /* NV storage values. If unable to get from NV storage, fall back to the
630 * CMOS reboot field used by older BIOS. */
Randall Spangler618d17d2011-03-01 10:33:11 -0800631 else if (!strcasecmp(name,"recovery_request")) {
Randall Spangler0f8ffb12011-02-25 09:50:54 -0800632 if (0 == VbSetNvStorage(VBNV_RECOVERY_REQUEST, value))
633 return 0;
Randall Spanglere73302c2011-02-18 14:53:01 -0800634 return VbSetCmosRebootField(CMOSRF_RECOVERY, value);
635 } else if (!strcasecmp(name,"dbg_reset")) {
Randall Spangler0f8ffb12011-02-25 09:50:54 -0800636 if (0 == VbSetNvStorage(VBNV_DEBUG_RESET_MODE, value))
637 return 0;
638 return VbSetCmosRebootField(CMOSRF_DEBUG_RESET, value);
Randall Spanglere73302c2011-02-18 14:53:01 -0800639 } else if (!strcasecmp(name,"fwb_tries")) {
Randall Spangler0f8ffb12011-02-25 09:50:54 -0800640 if (0 == VbSetNvStorage(VBNV_TRY_B_COUNT, value))
641 return 0;
Randall Spanglere73302c2011-02-18 14:53:01 -0800642 return VbSetCmosRebootField(CMOSRF_TRY_B, value);
643 }
644
Randall Spangler54218662011-02-07 11:20:20 -0800645 return -1;
646}
647
648
649/* Set a system property string.
650 *
651 * Returns 0 if success, -1 if error. */
652int VbSetSystemPropertyString(const char* name, const char* value) {
653
654 /* TODO: support setting */
655 return -1;
656}