blob: 70f39ddb2f098e70438372d59d3852ca6cd5c8e3 [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 Spangler54218662011-02-07 11:20:20 -080057
58/* Base name for ACPI files */
59#define ACPI_BASE_PATH "/sys/devices/platform/chromeos_acpi"
60/* Paths for frequently used ACPI files */
Randall Spanglerb47ed5a2011-02-23 13:05:40 -080061#define ACPI_BINF_PATH ACPI_BASE_PATH "/BINF"
Randall Spangler54218662011-02-07 11:20:20 -080062#define ACPI_CHNV_PATH ACPI_BASE_PATH "/CHNV"
63#define ACPI_CHSW_PATH ACPI_BASE_PATH "/CHSW"
Randall Spangler2b59a072011-02-24 11:17:24 -080064#define ACPI_FMAP_PATH ACPI_BASE_PATH "/FMAP"
Randall Spangler54218662011-02-07 11:20:20 -080065#define ACPI_GPIO_PATH ACPI_BASE_PATH "/GPIO"
66
67/* Base name for GPIO files */
68#define GPIO_BASE_PATH "/sys/class/gpio"
69#define GPIO_EXPORT_PATH GPIO_BASE_PATH "/export"
70
Randall Spanglere73302c2011-02-18 14:53:01 -080071/* Base name for NVRAM file */
72#define NVRAM_PATH "/dev/nvram"
73
Randall Spanglerb47ed5a2011-02-23 13:05:40 -080074
75/* Copy up to dest_size-1 characters from src to dest, ensuring null
76 termination (which strncpy() doesn't do). Returns the destination
77 string. */
78char* StrCopy(char* dest, const char* src, int dest_size) {
79 strncpy(dest, src, dest_size);
80 dest[dest_size - 1] = '\0';
81 return dest;
82}
83
84
Randall Spangler54218662011-02-07 11:20:20 -080085/* Read a string from a file. Passed the destination, dest size, and
86 * filename to read.
87 *
88 * Returns the destination, or NULL if error. */
89char* ReadFileString(char* dest, int size, const char* filename) {
90 char* got;
91 FILE* f;
92
93 f = fopen(filename, "rt");
94 if (!f)
95 return NULL;
96
97 got = fgets(dest, size, f);
98 fclose(f);
99 return got;
100}
101
102
103/* Read an integer from a file.
104 *
105 * Returns the parsed integer, or -1 if error. */
106int ReadFileInt(const char* filename) {
107 char buf[64];
108 int value;
109 char* e = NULL;
110
111 if (!ReadFileString(buf, sizeof(buf), filename))
112 return -1;
113
114 /* Convert to integer. Allow characters after the int ("123 blah"). */
115 value = strtol(buf, &e, 0);
116 if (e == buf)
117 return -1; /* No characters consumed, so conversion failed */
118
119 return value;
120}
121
122
123/* Check if a bit is set in a file which contains an integer.
124 *
125 * Returns 1 if the bit is set, 0 if clear, or -1 if error. */
126int ReadFileBit(const char* filename, int bitmask) {
127 int value = ReadFileInt(filename);
128 if (value == -1)
129 return -1;
130 else return (value & bitmask ? 1 : 0);
131}
132
133
Randall Spanglerc80fe652011-02-17 11:06:47 -0800134/* Return true if the FWID starts with the specified string. */
135static int FwidStartsWith(const char *start) {
136 char fwid[128];
137 if (!VbGetSystemPropertyString("fwid", fwid, sizeof(fwid)))
138 return 0;
139
140 return 0 == strncmp(fwid, start, strlen(start));
141}
142
143
Randall Spangler54218662011-02-07 11:20:20 -0800144/* Read a GPIO of the specified signal type (see ACPI GPIO SignalType).
145 *
146 * Returns 1 if the signal is asserted, 0 if not asserted, or -1 if error. */
147int ReadGpio(int signal_type) {
148 char name[128];
149 int index = 0;
150 int gpio_type;
151 int active_high;
152 int controller_offset;
153 char controller_name[128];
154 int value;
155
156 /* Scan GPIO.* to find a matching signal type */
157 for (index = 0; ; index++) {
158 snprintf(name, sizeof(name), "%s.%d/GPIO.0", ACPI_GPIO_PATH, index);
159 gpio_type = ReadFileInt(name);
160 if (gpio_type == signal_type)
161 break;
162 else if (gpio_type == -1)
163 return -1; /* Ran out of GPIOs before finding a match */
164 }
165
166 /* Read attributes and controller info for the GPIO */
167 snprintf(name, sizeof(name), "%s.%d/GPIO.1", ACPI_GPIO_PATH, index);
168 active_high = ReadFileBit(name, 0x00000001);
169 snprintf(name, sizeof(name), "%s.%d/GPIO.2", ACPI_GPIO_PATH, index);
170 controller_offset = ReadFileInt(name);
171 if (active_high == -1 || controller_offset == -1)
172 return -1; /* Missing needed info */
173
174 /* We only support the NM10 for now */
175 snprintf(name, sizeof(name), "%s.%d/GPIO.3", ACPI_GPIO_PATH, index);
176 if (!ReadFileString(controller_name, sizeof(controller_name), name))
177 return -1;
178 if (0 != strcmp(controller_name, "NM10"))
179 return -1;
180
181 /* Assume the NM10 has offset 192 */
182 /* TODO: should really check gpiochipNNN/label to see if it's the
183 * address we expect for the NM10, and then read the offset from
184 * gpiochipNNN/base. */
185 controller_offset += 192;
186
187 /* Try reading the GPIO value */
188 snprintf(name, sizeof(name), "%s/gpio%d/value",
189 GPIO_BASE_PATH, controller_offset);
190 value = ReadFileInt(name);
191
192 if (value == -1) {
193 /* Try exporting the GPIO */
194 FILE* f = fopen(GPIO_EXPORT_PATH, "wt");
195 if (!f)
196 return -1;
197 fprintf(f, "%d", controller_offset);
198 fclose(f);
199
200 /* Try re-reading the GPIO value */
201 value = ReadFileInt(name);
202 }
203
204 if (value == -1)
205 return -1;
206
207 /* Compare the GPIO value with the active value and return 1 if match. */
208 return (value == active_high ? 1 : 0);
209}
210
211
Randall Spanglere73302c2011-02-18 14:53:01 -0800212/* Read the CMOS reboot field in NVRAM.
213 *
214 * Returns 0 if the mask is clear in the field, 1 if set, or -1 if error. */
215int VbGetCmosRebootField(uint8_t mask) {
216 FILE* f;
217 int chnv, nvbyte;
218
219 /* Get the byte offset from CHNV */
220 chnv = ReadFileInt(ACPI_CHNV_PATH);
221 if (chnv == -1)
222 return -1;
223
224 f = fopen(NVRAM_PATH, "rb");
225 if (!f)
226 return -1;
227
228 if (0 != fseek(f, chnv, SEEK_SET) || EOF == (nvbyte = fgetc(f))) {
229 fclose(f);
230 return -1;
231 }
232
233 fclose(f);
234 return (nvbyte & mask ? 1 : 0);
235}
236
237
238/* Write the CMOS reboot field in NVRAM.
239 *
240 * Sets (value=0) or clears (value!=0) the mask in the byte.
241 *
242 * Returns 0 if success, or -1 if error. */
243int VbSetCmosRebootField(uint8_t mask, int value) {
244 FILE* f;
245 int chnv, nvbyte;
246
247 /* Get the byte offset from CHNV */
248 chnv = ReadFileInt(ACPI_CHNV_PATH);
249 if (chnv == -1)
250 return -1;
251
252 f = fopen(NVRAM_PATH, "w+b");
253 if (!f)
254 return -1;
255
256 /* Read the current value */
257 if (0 != fseek(f, chnv, SEEK_SET) || EOF == (nvbyte = fgetc(f))) {
258 fclose(f);
259 return -1;
260 }
261
262 /* Set/clear the mask */
263 if (value)
264 nvbyte |= mask;
265 else
266 nvbyte &= ~mask;
267
268 /* Write the byte back */
269 if (0 != fseek(f, chnv, SEEK_SET) || EOF == (fputc(nvbyte, f))) {
270 fclose(f);
271 return -1;
272 }
273
274 /* Success */
275 fclose(f);
276 return 0;
277}
278
Randall Spanglerb47ed5a2011-02-23 13:05:40 -0800279/* Read the recovery reason. Returns the reason code or -1 if error. */
280int VbGetRecoveryReason(void) {
281 int value;
282
283 /* Try reading type from BINF.4 */
284 value = ReadFileInt(ACPI_BINF_PATH ".4");
285 if (-1 != value)
286 return value;
287
288 /* Fall back to BINF.0 for legacy systems like Mario. */
289 switch(ReadFileInt(ACPI_BINF_PATH ".0")) {
290 case BINF0_NORMAL:
291 case BINF0_DEVELOPER:
292 return VBNV_RECOVERY_NOT_REQUESTED;
293 case BINF0_RECOVERY_BUTTON:
294 return VBNV_RECOVERY_RO_MANUAL;
295 case BINF0_RECOVERY_DEV_SCREEN_KEY:
296 return VBNV_RECOVERY_RW_DEV_SCREEN;
297 case BINF0_RECOVERY_RW_FW_BAD:
298 case BINF0_RECOVERY_NO_OS:
299 return VBNV_RECOVERY_RW_NO_OS;
300 case BINF0_RECOVERY_BAD_OS:
301 return VBNV_RECOVERY_RW_INVALID_OS;
302 case BINF0_RECOVERY_OS_INITIATED:
303 return VBNV_RECOVERY_LEGACY;
304 default:
305 /* Other values don't map cleanly to firmware type. */
306 return -1;
307 }
308}
309
310
311/* Read the active main firmware type into the destination buffer.
312 * Passed the destination and its size. Returns the destination, or
313 * NULL if error. */
314const char* VbReadMainFwType(char* dest, int size) {
315
316 /* Try reading type from BINF.3 */
317 switch(ReadFileInt(ACPI_BINF_PATH ".3")) {
318 case 0:
319 return StrCopy(dest, "recovery", size);
320 case 1:
321 return StrCopy(dest, "normal", size);
322 case 2:
323 return StrCopy(dest, "developer", size);
324 default:
325 break; /* Fall through to legacy handling */
326 }
327
328 /* Fall back to BINF.0 for legacy systems like Mario. */
329 switch(ReadFileInt(ACPI_BINF_PATH ".0")) {
330 case -1:
331 /* Both BINF.0 and BINF.3 are missing, so this isn't Chrome OS
332 * firmware. */
333 return StrCopy(dest, "nonchrome", size);
334 case BINF0_NORMAL:
335 return StrCopy(dest, "normal", size);
336 case BINF0_DEVELOPER:
337 return StrCopy(dest, "developer", size);
338 case BINF0_RECOVERY_BUTTON:
339 case BINF0_RECOVERY_DEV_SCREEN_KEY:
340 case BINF0_RECOVERY_RW_FW_BAD:
341 case BINF0_RECOVERY_NO_OS:
342 case BINF0_RECOVERY_BAD_OS:
343 case BINF0_RECOVERY_OS_INITIATED:
344 case BINF0_RECOVERY_TPM_ERROR:
345 /* Assorted flavors of recovery boot reason. */
346 return StrCopy(dest, "recovery", size);
347 default:
348 /* Other values don't map cleanly to firmware type. */
349 return NULL;
350 }
351}
352
353
354
Randall Spangler54218662011-02-07 11:20:20 -0800355/* Read a system property integer.
356 *
357 * Returns the property value, or -1 if error. */
358int VbGetSystemPropertyInt(const char* name) {
Randall Spanglerc80fe652011-02-17 11:06:47 -0800359 int value = -1;
Randall Spangler54218662011-02-07 11:20:20 -0800360
Randall Spanglere73302c2011-02-18 14:53:01 -0800361 /* Switch positions */
Randall Spangler54218662011-02-07 11:20:20 -0800362 if (!strcasecmp(name,"devsw_cur")) {
Randall Spanglerc80fe652011-02-17 11:06:47 -0800363 value = ReadGpio(GPIO_SIGNAL_TYPE_DEV);
Randall Spangler54218662011-02-07 11:20:20 -0800364 } else if (!strcasecmp(name,"devsw_boot")) {
Randall Spanglerc80fe652011-02-17 11:06:47 -0800365 value = ReadFileBit(ACPI_CHSW_PATH, CHSW_DEV_BOOT);
Randall Spangler54218662011-02-07 11:20:20 -0800366 } else if (!strcasecmp(name,"recoverysw_cur")) {
Randall Spanglerc80fe652011-02-17 11:06:47 -0800367 value = ReadGpio(GPIO_SIGNAL_TYPE_RECOVERY);
Randall Spangler54218662011-02-07 11:20:20 -0800368 } else if (!strcasecmp(name,"recoverysw_boot")) {
Randall Spanglerc80fe652011-02-17 11:06:47 -0800369 value = ReadFileBit(ACPI_CHSW_PATH, CHSW_RECOVERY_BOOT);
Randall Spangler54218662011-02-07 11:20:20 -0800370 } else if (!strcasecmp(name,"recoverysw_ec_boot")) {
Randall Spanglerc80fe652011-02-17 11:06:47 -0800371 value = ReadFileBit(ACPI_CHSW_PATH, CHSW_RECOVERY_EC_BOOT);
Randall Spangler54218662011-02-07 11:20:20 -0800372 } else if (!strcasecmp(name,"wpsw_cur")) {
Randall Spanglerc80fe652011-02-17 11:06:47 -0800373 value = ReadGpio(GPIO_SIGNAL_TYPE_WP);
374 if (-1 != value && FwidStartsWith("Mario."))
375 value = 1 - value; /* Mario reports this backwards */
Randall Spangler54218662011-02-07 11:20:20 -0800376 } else if (!strcasecmp(name,"wpsw_boot")) {
Randall Spanglerc80fe652011-02-17 11:06:47 -0800377 value = ReadFileBit(ACPI_CHSW_PATH, CHSW_WP_BOOT);
378 if (-1 != value && FwidStartsWith("Mario."))
379 value = 1 - value; /* Mario reports this backwards */
380 }
Randall Spanglerb47ed5a2011-02-23 13:05:40 -0800381 /* Saved memory is at a fixed location for all H2C BIOS. If the CHSW
382 * path exists in sysfs, it's a H2C BIOS. */
383 else if (!strcasecmp(name,"savedmem_base")) {
384 return (-1 == ReadFileInt(ACPI_CHSW_PATH) ? -1 : 0x00F00000);
385 } else if (!strcasecmp(name,"savedmem_size")) {
386 return (-1 == ReadFileInt(ACPI_CHSW_PATH) ? -1 : 0x00100000);
387 }
Randall Spanglere73302c2011-02-18 14:53:01 -0800388 /* NV storage values for older H2C BIOS */
389 else if (!strcasecmp(name,"recovery_request")) {
390 value = VbGetCmosRebootField(CMOSRF_RECOVERY);
391 } else if (!strcasecmp(name,"dbg_reset")) {
392 value = VbGetCmosRebootField(CMOSRF_DEBUG_RESET);
393 } else if (!strcasecmp(name,"fwb_tries")) {
394 value = VbGetCmosRebootField(CMOSRF_TRY_B);
395 }
Randall Spanglerb47ed5a2011-02-23 13:05:40 -0800396 /* Other parameters */
397 else if (!strcasecmp(name,"recovery_reason")) {
398 return VbGetRecoveryReason();
Randall Spangler2b59a072011-02-24 11:17:24 -0800399 } else if (!strcasecmp(name,"fmap_base")) {
400 value = ReadFileInt(ACPI_FMAP_PATH);
Randall Spanglerb47ed5a2011-02-23 13:05:40 -0800401 }
Randall Spangler54218662011-02-07 11:20:20 -0800402
Randall Spanglerb47ed5a2011-02-23 13:05:40 -0800403 /* TODO: implement the following properties:
404 * nvram_cleared
405 */
Randall Spanglerc80fe652011-02-17 11:06:47 -0800406
407 return value;
Randall Spangler54218662011-02-07 11:20:20 -0800408}
409
410
411/* Read a system property string into a destination buffer of the specified
412 * size.
413 *
414 * Returns the passed buffer, or NULL if error. */
415const char* VbGetSystemPropertyString(const char* name, char* dest, int size) {
416
417 if (!strcasecmp(name,"hwid")) {
418 return ReadFileString(dest, size, ACPI_BASE_PATH "/HWID");
419 } else if (!strcasecmp(name,"fwid")) {
420 return ReadFileString(dest, size, ACPI_BASE_PATH "/FWID");
421 } else if (!strcasecmp(name,"ro_fwid")) {
422 return ReadFileString(dest, size, ACPI_BASE_PATH "/FRID");
Randall Spanglerb47ed5a2011-02-23 13:05:40 -0800423 } else if (!strcasecmp(name,"mainfw_act")) {
424 switch(ReadFileInt(ACPI_BINF_PATH ".1")) {
425 case 0:
426 return StrCopy(dest, "recovery", size);
427 case 1:
428 return StrCopy(dest, "A", size);
429 case 2:
430 return StrCopy(dest, "B", size);
431 default:
432 return NULL;
433 }
434 } else if (!strcasecmp(name,"mainfw_type")) {
435 return VbReadMainFwType(dest, size);
436 } else if (!strcasecmp(name,"ecfw_act")) {
437 switch(ReadFileInt(ACPI_BINF_PATH ".2")) {
438 case 0:
439 return StrCopy(dest, "RO", size);
440 case 1:
441 return StrCopy(dest, "RW", size);
442 default:
443 return NULL;
444 }
Randall Spangler54218662011-02-07 11:20:20 -0800445 } else
446 return NULL;
Randall Spangler54218662011-02-07 11:20:20 -0800447}
448
449
450/* Set a system property integer.
451 *
452 * Returns 0 if success, -1 if error. */
453int VbSetSystemPropertyInt(const char* name, int value) {
454
Randall Spanglere73302c2011-02-18 14:53:01 -0800455 /* NV storage values for older H2C BIOS */
456 if (!strcasecmp(name,"recovery_request")) {
457 return VbSetCmosRebootField(CMOSRF_RECOVERY, value);
458 } else if (!strcasecmp(name,"dbg_reset")) {
459 return VbSetCmosRebootField(CMOSRF_DEBUG_RESET, value);
460 } else if (!strcasecmp(name,"fwb_tries")) {
461 return VbSetCmosRebootField(CMOSRF_TRY_B, value);
462 }
463
Randall Spanglerb47ed5a2011-02-23 13:05:40 -0800464 /* TODO: implement the following:
465 * nvram_cleared
466 */
467
Randall Spangler54218662011-02-07 11:20:20 -0800468 return -1;
469}
470
471
472/* Set a system property string.
473 *
474 * Returns 0 if success, -1 if error. */
475int VbSetSystemPropertyString(const char* name, const char* value) {
476
477 /* TODO: support setting */
478 return -1;
479}