Randall Spangler | 5421866 | 2011-02-07 11:20:20 -0800 | [diff] [blame] | 1 | /* 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 Spangler | e73302c | 2011-02-18 14:53:01 -0800 | [diff] [blame] | 14 | #include "vboot_nvstorage.h" |
Randall Spangler | 5421866 | 2011-02-07 11:20:20 -0800 | [diff] [blame] | 15 | |
| 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 Spangler | e73302c | 2011-02-18 14:53:01 -0800 | [diff] [blame] | 26 | /* CMOS reboot field bitflags */ |
| 27 | #define CMOSRF_RECOVERY 0x80 |
| 28 | #define CMOSRF_DEBUG_RESET 0x40 |
| 29 | #define CMOSRF_TRY_B 0x20 |
Randall Spangler | b47ed5a | 2011-02-23 13:05:40 -0800 | [diff] [blame] | 30 | /* 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 Spangler | 5421866 | 2011-02-07 11:20:20 -0800 | [diff] [blame] | 57 | |
| 58 | /* Base name for ACPI files */ |
| 59 | #define ACPI_BASE_PATH "/sys/devices/platform/chromeos_acpi" |
| 60 | /* Paths for frequently used ACPI files */ |
Randall Spangler | b47ed5a | 2011-02-23 13:05:40 -0800 | [diff] [blame] | 61 | #define ACPI_BINF_PATH ACPI_BASE_PATH "/BINF" |
Randall Spangler | 5421866 | 2011-02-07 11:20:20 -0800 | [diff] [blame] | 62 | #define ACPI_CHNV_PATH ACPI_BASE_PATH "/CHNV" |
| 63 | #define ACPI_CHSW_PATH ACPI_BASE_PATH "/CHSW" |
Randall Spangler | 2b59a07 | 2011-02-24 11:17:24 -0800 | [diff] [blame] | 64 | #define ACPI_FMAP_PATH ACPI_BASE_PATH "/FMAP" |
Randall Spangler | 5421866 | 2011-02-07 11:20:20 -0800 | [diff] [blame] | 65 | #define ACPI_GPIO_PATH ACPI_BASE_PATH "/GPIO" |
Randall Spangler | 0f8ffb1 | 2011-02-25 09:50:54 -0800 | [diff] [blame^] | 66 | #define ACPI_VBNV_PATH ACPI_BASE_PATH "/VBNV" |
Randall Spangler | 5421866 | 2011-02-07 11:20:20 -0800 | [diff] [blame] | 67 | |
| 68 | /* Base name for GPIO files */ |
| 69 | #define GPIO_BASE_PATH "/sys/class/gpio" |
| 70 | #define GPIO_EXPORT_PATH GPIO_BASE_PATH "/export" |
| 71 | |
Randall Spangler | e73302c | 2011-02-18 14:53:01 -0800 | [diff] [blame] | 72 | /* Base name for NVRAM file */ |
| 73 | #define NVRAM_PATH "/dev/nvram" |
| 74 | |
Randall Spangler | b47ed5a | 2011-02-23 13:05:40 -0800 | [diff] [blame] | 75 | |
| 76 | /* Copy up to dest_size-1 characters from src to dest, ensuring null |
| 77 | termination (which strncpy() doesn't do). Returns the destination |
| 78 | string. */ |
| 79 | char* StrCopy(char* dest, const char* src, int dest_size) { |
| 80 | strncpy(dest, src, dest_size); |
| 81 | dest[dest_size - 1] = '\0'; |
| 82 | return dest; |
| 83 | } |
| 84 | |
| 85 | |
Randall Spangler | 5421866 | 2011-02-07 11:20:20 -0800 | [diff] [blame] | 86 | /* Read a string from a file. Passed the destination, dest size, and |
| 87 | * filename to read. |
| 88 | * |
| 89 | * Returns the destination, or NULL if error. */ |
| 90 | char* ReadFileString(char* dest, int size, const char* filename) { |
| 91 | char* got; |
| 92 | FILE* f; |
| 93 | |
| 94 | f = fopen(filename, "rt"); |
| 95 | if (!f) |
| 96 | return NULL; |
| 97 | |
| 98 | got = fgets(dest, size, f); |
| 99 | fclose(f); |
| 100 | return got; |
| 101 | } |
| 102 | |
| 103 | |
| 104 | /* Read an integer from a file. |
| 105 | * |
| 106 | * Returns the parsed integer, or -1 if error. */ |
| 107 | int ReadFileInt(const char* filename) { |
| 108 | char buf[64]; |
| 109 | int value; |
| 110 | char* e = NULL; |
| 111 | |
| 112 | if (!ReadFileString(buf, sizeof(buf), filename)) |
| 113 | return -1; |
| 114 | |
| 115 | /* Convert to integer. Allow characters after the int ("123 blah"). */ |
| 116 | value = strtol(buf, &e, 0); |
| 117 | if (e == buf) |
| 118 | return -1; /* No characters consumed, so conversion failed */ |
| 119 | |
| 120 | return value; |
| 121 | } |
| 122 | |
| 123 | |
| 124 | /* Check if a bit is set in a file which contains an integer. |
| 125 | * |
| 126 | * Returns 1 if the bit is set, 0 if clear, or -1 if error. */ |
| 127 | int ReadFileBit(const char* filename, int bitmask) { |
| 128 | int value = ReadFileInt(filename); |
| 129 | if (value == -1) |
| 130 | return -1; |
| 131 | else return (value & bitmask ? 1 : 0); |
| 132 | } |
| 133 | |
| 134 | |
Randall Spangler | c80fe65 | 2011-02-17 11:06:47 -0800 | [diff] [blame] | 135 | /* Return true if the FWID starts with the specified string. */ |
| 136 | static int FwidStartsWith(const char *start) { |
| 137 | char fwid[128]; |
| 138 | if (!VbGetSystemPropertyString("fwid", fwid, sizeof(fwid))) |
| 139 | return 0; |
| 140 | |
| 141 | return 0 == strncmp(fwid, start, strlen(start)); |
| 142 | } |
| 143 | |
| 144 | |
Randall Spangler | 5421866 | 2011-02-07 11:20:20 -0800 | [diff] [blame] | 145 | /* Read a GPIO of the specified signal type (see ACPI GPIO SignalType). |
| 146 | * |
| 147 | * Returns 1 if the signal is asserted, 0 if not asserted, or -1 if error. */ |
| 148 | int ReadGpio(int signal_type) { |
| 149 | char name[128]; |
| 150 | int index = 0; |
| 151 | int gpio_type; |
| 152 | int active_high; |
| 153 | int controller_offset; |
| 154 | char controller_name[128]; |
| 155 | int value; |
| 156 | |
| 157 | /* Scan GPIO.* to find a matching signal type */ |
| 158 | for (index = 0; ; index++) { |
| 159 | snprintf(name, sizeof(name), "%s.%d/GPIO.0", ACPI_GPIO_PATH, index); |
| 160 | gpio_type = ReadFileInt(name); |
| 161 | if (gpio_type == signal_type) |
| 162 | break; |
| 163 | else if (gpio_type == -1) |
| 164 | return -1; /* Ran out of GPIOs before finding a match */ |
| 165 | } |
| 166 | |
| 167 | /* Read attributes and controller info for the GPIO */ |
| 168 | snprintf(name, sizeof(name), "%s.%d/GPIO.1", ACPI_GPIO_PATH, index); |
| 169 | active_high = ReadFileBit(name, 0x00000001); |
| 170 | snprintf(name, sizeof(name), "%s.%d/GPIO.2", ACPI_GPIO_PATH, index); |
| 171 | controller_offset = ReadFileInt(name); |
| 172 | if (active_high == -1 || controller_offset == -1) |
| 173 | return -1; /* Missing needed info */ |
| 174 | |
| 175 | /* We only support the NM10 for now */ |
| 176 | snprintf(name, sizeof(name), "%s.%d/GPIO.3", ACPI_GPIO_PATH, index); |
| 177 | if (!ReadFileString(controller_name, sizeof(controller_name), name)) |
| 178 | return -1; |
| 179 | if (0 != strcmp(controller_name, "NM10")) |
| 180 | return -1; |
| 181 | |
| 182 | /* Assume the NM10 has offset 192 */ |
| 183 | /* TODO: should really check gpiochipNNN/label to see if it's the |
| 184 | * address we expect for the NM10, and then read the offset from |
| 185 | * gpiochipNNN/base. */ |
| 186 | controller_offset += 192; |
| 187 | |
| 188 | /* Try reading the GPIO value */ |
| 189 | snprintf(name, sizeof(name), "%s/gpio%d/value", |
| 190 | GPIO_BASE_PATH, controller_offset); |
| 191 | value = ReadFileInt(name); |
| 192 | |
| 193 | if (value == -1) { |
| 194 | /* Try exporting the GPIO */ |
| 195 | FILE* f = fopen(GPIO_EXPORT_PATH, "wt"); |
| 196 | if (!f) |
| 197 | return -1; |
| 198 | fprintf(f, "%d", controller_offset); |
| 199 | fclose(f); |
| 200 | |
| 201 | /* Try re-reading the GPIO value */ |
| 202 | value = ReadFileInt(name); |
| 203 | } |
| 204 | |
| 205 | if (value == -1) |
| 206 | return -1; |
| 207 | |
| 208 | /* Compare the GPIO value with the active value and return 1 if match. */ |
| 209 | return (value == active_high ? 1 : 0); |
| 210 | } |
| 211 | |
| 212 | |
Randall Spangler | e73302c | 2011-02-18 14:53:01 -0800 | [diff] [blame] | 213 | /* Read the CMOS reboot field in NVRAM. |
| 214 | * |
| 215 | * Returns 0 if the mask is clear in the field, 1 if set, or -1 if error. */ |
| 216 | int VbGetCmosRebootField(uint8_t mask) { |
| 217 | FILE* f; |
| 218 | int chnv, nvbyte; |
| 219 | |
| 220 | /* Get the byte offset from CHNV */ |
| 221 | chnv = ReadFileInt(ACPI_CHNV_PATH); |
| 222 | if (chnv == -1) |
| 223 | return -1; |
| 224 | |
| 225 | f = fopen(NVRAM_PATH, "rb"); |
| 226 | if (!f) |
| 227 | return -1; |
| 228 | |
| 229 | if (0 != fseek(f, chnv, SEEK_SET) || EOF == (nvbyte = fgetc(f))) { |
| 230 | fclose(f); |
| 231 | return -1; |
| 232 | } |
| 233 | |
| 234 | fclose(f); |
| 235 | return (nvbyte & mask ? 1 : 0); |
| 236 | } |
| 237 | |
| 238 | |
| 239 | /* Write the CMOS reboot field in NVRAM. |
| 240 | * |
| 241 | * Sets (value=0) or clears (value!=0) the mask in the byte. |
| 242 | * |
| 243 | * Returns 0 if success, or -1 if error. */ |
| 244 | int VbSetCmosRebootField(uint8_t mask, int value) { |
| 245 | FILE* f; |
| 246 | int chnv, nvbyte; |
| 247 | |
| 248 | /* Get the byte offset from CHNV */ |
| 249 | chnv = ReadFileInt(ACPI_CHNV_PATH); |
| 250 | if (chnv == -1) |
| 251 | return -1; |
| 252 | |
| 253 | f = fopen(NVRAM_PATH, "w+b"); |
| 254 | if (!f) |
| 255 | return -1; |
| 256 | |
| 257 | /* Read the current value */ |
| 258 | if (0 != fseek(f, chnv, SEEK_SET) || EOF == (nvbyte = fgetc(f))) { |
| 259 | fclose(f); |
| 260 | return -1; |
| 261 | } |
| 262 | |
| 263 | /* Set/clear the mask */ |
| 264 | if (value) |
| 265 | nvbyte |= mask; |
| 266 | else |
| 267 | nvbyte &= ~mask; |
| 268 | |
| 269 | /* Write the byte back */ |
| 270 | if (0 != fseek(f, chnv, SEEK_SET) || EOF == (fputc(nvbyte, f))) { |
| 271 | fclose(f); |
| 272 | return -1; |
| 273 | } |
| 274 | |
| 275 | /* Success */ |
| 276 | fclose(f); |
| 277 | return 0; |
| 278 | } |
| 279 | |
Randall Spangler | 0f8ffb1 | 2011-02-25 09:50:54 -0800 | [diff] [blame^] | 280 | |
| 281 | /* Read an integer property from VbNvStorage. |
| 282 | * |
| 283 | * Returns the parameter value, or -1 if error. */ |
| 284 | int VbGetNvStorage(VbNvParam param) { |
| 285 | FILE* f; |
| 286 | VbNvContext vnc; |
| 287 | int offs; |
| 288 | uint32_t value; |
| 289 | int retval; |
| 290 | |
| 291 | /* Get the byte offset from VBNV */ |
| 292 | offs = ReadFileInt(ACPI_VBNV_PATH ".0"); |
| 293 | if (offs == -1) |
| 294 | return -1; |
| 295 | if (VBNV_BLOCK_SIZE > ReadFileInt(ACPI_VBNV_PATH ".1")) |
| 296 | return -1; /* NV storage block is too small */ |
| 297 | |
| 298 | /* TODO: locking around NV access */ |
| 299 | f = fopen(NVRAM_PATH, "rb"); |
| 300 | if (!f) |
| 301 | return -1; |
| 302 | |
| 303 | if (0 != fseek(f, offs, SEEK_SET) || |
| 304 | 1 != fread(vnc.raw, VBNV_BLOCK_SIZE, 1, f)) { |
| 305 | fclose(f); |
| 306 | return -1; |
| 307 | } |
| 308 | |
| 309 | fclose(f); |
| 310 | |
| 311 | if (0 != VbNvSetup(&vnc)) |
| 312 | return -1; |
| 313 | retval = VbNvGet(&vnc, param, &value); |
| 314 | if (0 != VbNvTeardown(&vnc)) |
| 315 | return -1; |
| 316 | if (0 != retval) |
| 317 | return -1; |
| 318 | |
| 319 | /* TODO: If vnc.raw_changed, attempt to reopen NVRAM for write and |
| 320 | * save the new defaults. If we're able to, log. */ |
| 321 | /* TODO: release lock */ |
| 322 | |
| 323 | return (int)value; |
| 324 | } |
| 325 | |
| 326 | |
| 327 | /* Write an integer property to VbNvStorage. |
| 328 | * |
| 329 | * Returns 0 if success, -1 if error. */ |
| 330 | int VbSetNvStorage(VbNvParam param, int value) { |
| 331 | FILE* f; |
| 332 | VbNvContext vnc; |
| 333 | int offs; |
| 334 | int retval = -1; |
| 335 | int i; |
| 336 | |
| 337 | /* Get the byte offset from VBNV */ |
| 338 | offs = ReadFileInt(ACPI_VBNV_PATH ".0"); |
| 339 | if (offs == -1) |
| 340 | return -1; |
| 341 | if (VBNV_BLOCK_SIZE > ReadFileInt(ACPI_VBNV_PATH ".1")) |
| 342 | return -1; /* NV storage block is too small */ |
| 343 | |
| 344 | /* TODO: locking around NV access */ |
| 345 | f = fopen(NVRAM_PATH, "w+b"); |
| 346 | if (!f) |
| 347 | return -1; |
| 348 | |
| 349 | if (0 != fseek(f, offs, SEEK_SET) || |
| 350 | 1 != fread(vnc.raw, VBNV_BLOCK_SIZE, 1, f)) { |
| 351 | goto VbSetNvCleanup; |
| 352 | } |
| 353 | |
| 354 | if (0 != VbNvSetup(&vnc)) |
| 355 | goto VbSetNvCleanup; |
| 356 | i = VbNvSet(&vnc, param, (uint32_t)value); |
| 357 | if (0 != VbNvTeardown(&vnc)) |
| 358 | goto VbSetNvCleanup; |
| 359 | if (0 != i) |
| 360 | goto VbSetNvCleanup; |
| 361 | |
| 362 | if (vnc.raw_changed) { |
| 363 | if (0 != fseek(f, offs, SEEK_SET) || |
| 364 | 1 != fwrite(vnc.raw, VBNV_BLOCK_SIZE, 1, f)) |
| 365 | goto VbSetNvCleanup; |
| 366 | } |
| 367 | |
| 368 | /* Success */ |
| 369 | retval = 0; |
| 370 | |
| 371 | VbSetNvCleanup: |
| 372 | fclose(f); |
| 373 | /* TODO: release lock */ |
| 374 | return retval; |
| 375 | } |
| 376 | |
| 377 | |
Randall Spangler | b47ed5a | 2011-02-23 13:05:40 -0800 | [diff] [blame] | 378 | /* Read the recovery reason. Returns the reason code or -1 if error. */ |
| 379 | int VbGetRecoveryReason(void) { |
| 380 | int value; |
| 381 | |
| 382 | /* Try reading type from BINF.4 */ |
| 383 | value = ReadFileInt(ACPI_BINF_PATH ".4"); |
| 384 | if (-1 != value) |
| 385 | return value; |
| 386 | |
| 387 | /* Fall back to BINF.0 for legacy systems like Mario. */ |
| 388 | switch(ReadFileInt(ACPI_BINF_PATH ".0")) { |
| 389 | case BINF0_NORMAL: |
| 390 | case BINF0_DEVELOPER: |
| 391 | return VBNV_RECOVERY_NOT_REQUESTED; |
| 392 | case BINF0_RECOVERY_BUTTON: |
| 393 | return VBNV_RECOVERY_RO_MANUAL; |
| 394 | case BINF0_RECOVERY_DEV_SCREEN_KEY: |
| 395 | return VBNV_RECOVERY_RW_DEV_SCREEN; |
| 396 | case BINF0_RECOVERY_RW_FW_BAD: |
| 397 | case BINF0_RECOVERY_NO_OS: |
| 398 | return VBNV_RECOVERY_RW_NO_OS; |
| 399 | case BINF0_RECOVERY_BAD_OS: |
| 400 | return VBNV_RECOVERY_RW_INVALID_OS; |
| 401 | case BINF0_RECOVERY_OS_INITIATED: |
| 402 | return VBNV_RECOVERY_LEGACY; |
| 403 | default: |
| 404 | /* Other values don't map cleanly to firmware type. */ |
| 405 | return -1; |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | |
| 410 | /* Read the active main firmware type into the destination buffer. |
| 411 | * Passed the destination and its size. Returns the destination, or |
| 412 | * NULL if error. */ |
| 413 | const char* VbReadMainFwType(char* dest, int size) { |
| 414 | |
| 415 | /* Try reading type from BINF.3 */ |
| 416 | switch(ReadFileInt(ACPI_BINF_PATH ".3")) { |
| 417 | case 0: |
| 418 | return StrCopy(dest, "recovery", size); |
| 419 | case 1: |
| 420 | return StrCopy(dest, "normal", size); |
| 421 | case 2: |
| 422 | return StrCopy(dest, "developer", size); |
| 423 | default: |
| 424 | break; /* Fall through to legacy handling */ |
| 425 | } |
| 426 | |
| 427 | /* Fall back to BINF.0 for legacy systems like Mario. */ |
| 428 | switch(ReadFileInt(ACPI_BINF_PATH ".0")) { |
| 429 | case -1: |
| 430 | /* Both BINF.0 and BINF.3 are missing, so this isn't Chrome OS |
| 431 | * firmware. */ |
| 432 | return StrCopy(dest, "nonchrome", size); |
| 433 | case BINF0_NORMAL: |
| 434 | return StrCopy(dest, "normal", size); |
| 435 | case BINF0_DEVELOPER: |
| 436 | return StrCopy(dest, "developer", size); |
| 437 | case BINF0_RECOVERY_BUTTON: |
| 438 | case BINF0_RECOVERY_DEV_SCREEN_KEY: |
| 439 | case BINF0_RECOVERY_RW_FW_BAD: |
| 440 | case BINF0_RECOVERY_NO_OS: |
| 441 | case BINF0_RECOVERY_BAD_OS: |
| 442 | case BINF0_RECOVERY_OS_INITIATED: |
| 443 | case BINF0_RECOVERY_TPM_ERROR: |
| 444 | /* Assorted flavors of recovery boot reason. */ |
| 445 | return StrCopy(dest, "recovery", size); |
| 446 | default: |
| 447 | /* Other values don't map cleanly to firmware type. */ |
| 448 | return NULL; |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | |
| 453 | |
Randall Spangler | 5421866 | 2011-02-07 11:20:20 -0800 | [diff] [blame] | 454 | /* Read a system property integer. |
| 455 | * |
| 456 | * Returns the property value, or -1 if error. */ |
| 457 | int VbGetSystemPropertyInt(const char* name) { |
Randall Spangler | c80fe65 | 2011-02-17 11:06:47 -0800 | [diff] [blame] | 458 | int value = -1; |
Randall Spangler | 5421866 | 2011-02-07 11:20:20 -0800 | [diff] [blame] | 459 | |
Randall Spangler | e73302c | 2011-02-18 14:53:01 -0800 | [diff] [blame] | 460 | /* Switch positions */ |
Randall Spangler | 5421866 | 2011-02-07 11:20:20 -0800 | [diff] [blame] | 461 | if (!strcasecmp(name,"devsw_cur")) { |
Randall Spangler | c80fe65 | 2011-02-17 11:06:47 -0800 | [diff] [blame] | 462 | value = ReadGpio(GPIO_SIGNAL_TYPE_DEV); |
Randall Spangler | 5421866 | 2011-02-07 11:20:20 -0800 | [diff] [blame] | 463 | } else if (!strcasecmp(name,"devsw_boot")) { |
Randall Spangler | c80fe65 | 2011-02-17 11:06:47 -0800 | [diff] [blame] | 464 | value = ReadFileBit(ACPI_CHSW_PATH, CHSW_DEV_BOOT); |
Randall Spangler | 5421866 | 2011-02-07 11:20:20 -0800 | [diff] [blame] | 465 | } else if (!strcasecmp(name,"recoverysw_cur")) { |
Randall Spangler | c80fe65 | 2011-02-17 11:06:47 -0800 | [diff] [blame] | 466 | value = ReadGpio(GPIO_SIGNAL_TYPE_RECOVERY); |
Randall Spangler | 5421866 | 2011-02-07 11:20:20 -0800 | [diff] [blame] | 467 | } else if (!strcasecmp(name,"recoverysw_boot")) { |
Randall Spangler | c80fe65 | 2011-02-17 11:06:47 -0800 | [diff] [blame] | 468 | value = ReadFileBit(ACPI_CHSW_PATH, CHSW_RECOVERY_BOOT); |
Randall Spangler | 5421866 | 2011-02-07 11:20:20 -0800 | [diff] [blame] | 469 | } else if (!strcasecmp(name,"recoverysw_ec_boot")) { |
Randall Spangler | c80fe65 | 2011-02-17 11:06:47 -0800 | [diff] [blame] | 470 | value = ReadFileBit(ACPI_CHSW_PATH, CHSW_RECOVERY_EC_BOOT); |
Randall Spangler | 5421866 | 2011-02-07 11:20:20 -0800 | [diff] [blame] | 471 | } else if (!strcasecmp(name,"wpsw_cur")) { |
Randall Spangler | c80fe65 | 2011-02-17 11:06:47 -0800 | [diff] [blame] | 472 | value = ReadGpio(GPIO_SIGNAL_TYPE_WP); |
| 473 | if (-1 != value && FwidStartsWith("Mario.")) |
| 474 | value = 1 - value; /* Mario reports this backwards */ |
Randall Spangler | 5421866 | 2011-02-07 11:20:20 -0800 | [diff] [blame] | 475 | } else if (!strcasecmp(name,"wpsw_boot")) { |
Randall Spangler | c80fe65 | 2011-02-17 11:06:47 -0800 | [diff] [blame] | 476 | value = ReadFileBit(ACPI_CHSW_PATH, CHSW_WP_BOOT); |
| 477 | if (-1 != value && FwidStartsWith("Mario.")) |
| 478 | value = 1 - value; /* Mario reports this backwards */ |
| 479 | } |
Randall Spangler | b47ed5a | 2011-02-23 13:05:40 -0800 | [diff] [blame] | 480 | /* Saved memory is at a fixed location for all H2C BIOS. If the CHSW |
| 481 | * path exists in sysfs, it's a H2C BIOS. */ |
| 482 | else if (!strcasecmp(name,"savedmem_base")) { |
| 483 | return (-1 == ReadFileInt(ACPI_CHSW_PATH) ? -1 : 0x00F00000); |
| 484 | } else if (!strcasecmp(name,"savedmem_size")) { |
| 485 | return (-1 == ReadFileInt(ACPI_CHSW_PATH) ? -1 : 0x00100000); |
| 486 | } |
Randall Spangler | 0f8ffb1 | 2011-02-25 09:50:54 -0800 | [diff] [blame^] | 487 | /* NV storage values. If unable to get from NV storage, fall back to the |
| 488 | * CMOS reboot field used by older BIOS. */ |
Randall Spangler | e73302c | 2011-02-18 14:53:01 -0800 | [diff] [blame] | 489 | else if (!strcasecmp(name,"recovery_request")) { |
Randall Spangler | 0f8ffb1 | 2011-02-25 09:50:54 -0800 | [diff] [blame^] | 490 | value = VbGetNvStorage(VBNV_RECOVERY_REQUEST); |
| 491 | if (-1 == value) |
| 492 | value = VbGetCmosRebootField(CMOSRF_RECOVERY); |
Randall Spangler | e73302c | 2011-02-18 14:53:01 -0800 | [diff] [blame] | 493 | } else if (!strcasecmp(name,"dbg_reset")) { |
Randall Spangler | 0f8ffb1 | 2011-02-25 09:50:54 -0800 | [diff] [blame^] | 494 | value = VbGetNvStorage(VBNV_DEBUG_RESET_MODE); |
| 495 | if (-1 == value) |
| 496 | value = VbGetCmosRebootField(CMOSRF_DEBUG_RESET); |
Randall Spangler | e73302c | 2011-02-18 14:53:01 -0800 | [diff] [blame] | 497 | } else if (!strcasecmp(name,"fwb_tries")) { |
Randall Spangler | 0f8ffb1 | 2011-02-25 09:50:54 -0800 | [diff] [blame^] | 498 | value = VbGetNvStorage(VBNV_TRY_B_COUNT); |
| 499 | if (-1 == value) |
| 500 | value = VbGetCmosRebootField(CMOSRF_TRY_B); |
Randall Spangler | e73302c | 2011-02-18 14:53:01 -0800 | [diff] [blame] | 501 | } |
Randall Spangler | b47ed5a | 2011-02-23 13:05:40 -0800 | [diff] [blame] | 502 | /* Other parameters */ |
| 503 | else if (!strcasecmp(name,"recovery_reason")) { |
| 504 | return VbGetRecoveryReason(); |
Randall Spangler | 2b59a07 | 2011-02-24 11:17:24 -0800 | [diff] [blame] | 505 | } else if (!strcasecmp(name,"fmap_base")) { |
| 506 | value = ReadFileInt(ACPI_FMAP_PATH); |
Randall Spangler | b47ed5a | 2011-02-23 13:05:40 -0800 | [diff] [blame] | 507 | } |
Randall Spangler | 5421866 | 2011-02-07 11:20:20 -0800 | [diff] [blame] | 508 | |
Randall Spangler | b47ed5a | 2011-02-23 13:05:40 -0800 | [diff] [blame] | 509 | /* TODO: implement the following properties: |
| 510 | * nvram_cleared |
| 511 | */ |
Randall Spangler | c80fe65 | 2011-02-17 11:06:47 -0800 | [diff] [blame] | 512 | |
| 513 | return value; |
Randall Spangler | 5421866 | 2011-02-07 11:20:20 -0800 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | |
| 517 | /* Read a system property string into a destination buffer of the specified |
| 518 | * size. |
| 519 | * |
| 520 | * Returns the passed buffer, or NULL if error. */ |
| 521 | const char* VbGetSystemPropertyString(const char* name, char* dest, int size) { |
| 522 | |
| 523 | if (!strcasecmp(name,"hwid")) { |
| 524 | return ReadFileString(dest, size, ACPI_BASE_PATH "/HWID"); |
| 525 | } else if (!strcasecmp(name,"fwid")) { |
| 526 | return ReadFileString(dest, size, ACPI_BASE_PATH "/FWID"); |
| 527 | } else if (!strcasecmp(name,"ro_fwid")) { |
| 528 | return ReadFileString(dest, size, ACPI_BASE_PATH "/FRID"); |
Randall Spangler | b47ed5a | 2011-02-23 13:05:40 -0800 | [diff] [blame] | 529 | } else if (!strcasecmp(name,"mainfw_act")) { |
| 530 | switch(ReadFileInt(ACPI_BINF_PATH ".1")) { |
| 531 | case 0: |
| 532 | return StrCopy(dest, "recovery", size); |
| 533 | case 1: |
| 534 | return StrCopy(dest, "A", size); |
| 535 | case 2: |
| 536 | return StrCopy(dest, "B", size); |
| 537 | default: |
| 538 | return NULL; |
| 539 | } |
| 540 | } else if (!strcasecmp(name,"mainfw_type")) { |
| 541 | return VbReadMainFwType(dest, size); |
| 542 | } else if (!strcasecmp(name,"ecfw_act")) { |
| 543 | switch(ReadFileInt(ACPI_BINF_PATH ".2")) { |
| 544 | case 0: |
| 545 | return StrCopy(dest, "RO", size); |
| 546 | case 1: |
| 547 | return StrCopy(dest, "RW", size); |
| 548 | default: |
| 549 | return NULL; |
| 550 | } |
Randall Spangler | 5421866 | 2011-02-07 11:20:20 -0800 | [diff] [blame] | 551 | } else |
| 552 | return NULL; |
Randall Spangler | 5421866 | 2011-02-07 11:20:20 -0800 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | |
| 556 | /* Set a system property integer. |
| 557 | * |
| 558 | * Returns 0 if success, -1 if error. */ |
| 559 | int VbSetSystemPropertyInt(const char* name, int value) { |
| 560 | |
Randall Spangler | 0f8ffb1 | 2011-02-25 09:50:54 -0800 | [diff] [blame^] | 561 | /* NV storage values. If unable to get from NV storage, fall back to the |
| 562 | * CMOS reboot field used by older BIOS. */ |
Randall Spangler | e73302c | 2011-02-18 14:53:01 -0800 | [diff] [blame] | 563 | if (!strcasecmp(name,"recovery_request")) { |
Randall Spangler | 0f8ffb1 | 2011-02-25 09:50:54 -0800 | [diff] [blame^] | 564 | if (0 == VbSetNvStorage(VBNV_RECOVERY_REQUEST, value)) |
| 565 | return 0; |
Randall Spangler | e73302c | 2011-02-18 14:53:01 -0800 | [diff] [blame] | 566 | return VbSetCmosRebootField(CMOSRF_RECOVERY, value); |
| 567 | } else if (!strcasecmp(name,"dbg_reset")) { |
Randall Spangler | 0f8ffb1 | 2011-02-25 09:50:54 -0800 | [diff] [blame^] | 568 | if (0 == VbSetNvStorage(VBNV_DEBUG_RESET_MODE, value)) |
| 569 | return 0; |
| 570 | return VbSetCmosRebootField(CMOSRF_DEBUG_RESET, value); |
Randall Spangler | e73302c | 2011-02-18 14:53:01 -0800 | [diff] [blame] | 571 | } else if (!strcasecmp(name,"fwb_tries")) { |
Randall Spangler | 0f8ffb1 | 2011-02-25 09:50:54 -0800 | [diff] [blame^] | 572 | if (0 == VbSetNvStorage(VBNV_TRY_B_COUNT, value)) |
| 573 | return 0; |
Randall Spangler | e73302c | 2011-02-18 14:53:01 -0800 | [diff] [blame] | 574 | return VbSetCmosRebootField(CMOSRF_TRY_B, value); |
| 575 | } |
| 576 | |
Randall Spangler | b47ed5a | 2011-02-23 13:05:40 -0800 | [diff] [blame] | 577 | /* TODO: implement the following: |
| 578 | * nvram_cleared |
| 579 | */ |
| 580 | |
Randall Spangler | 5421866 | 2011-02-07 11:20:20 -0800 | [diff] [blame] | 581 | return -1; |
| 582 | } |
| 583 | |
| 584 | |
| 585 | /* Set a system property string. |
| 586 | * |
| 587 | * Returns 0 if success, -1 if error. */ |
| 588 | int VbSetSystemPropertyString(const char* name, const char* value) { |
| 589 | |
| 590 | /* TODO: support setting */ |
| 591 | return -1; |
| 592 | } |