| OLD | NEW |
| 1 /* Copyright (c) 2011 The Chromium OS Authors. All rights reserved. | 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 | 2 * Use of this source code is governed by a BSD-style license that can be |
| 3 * found in the LICENSE file. | 3 * found in the LICENSE file. |
| 4 */ | 4 */ |
| 5 | 5 |
| 6 #include <stdio.h> | 6 #include <stdio.h> |
| 7 #include <string.h> | 7 #include <string.h> |
| 8 | 8 |
| 9 #include "host_common.h" | 9 #include "host_common.h" |
| 10 | 10 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 47 /* Recovery caused by OS kernel failed signature check */ | 47 /* Recovery caused by OS kernel failed signature check */ |
| 48 #define BINF0_RECOVERY_BAD_OS 7 | 48 #define BINF0_RECOVERY_BAD_OS 7 |
| 49 /* Recovery initiated by OS */ | 49 /* Recovery initiated by OS */ |
| 50 #define BINF0_RECOVERY_OS_INITIATED 8 | 50 #define BINF0_RECOVERY_OS_INITIATED 8 |
| 51 /* OS-initiated S3 diagnostic path (debug mode boot) */ | 51 /* OS-initiated S3 diagnostic path (debug mode boot) */ |
| 52 #define BINF0_S3_DIAGNOSTIC_PATH 9 | 52 #define BINF0_S3_DIAGNOSTIC_PATH 9 |
| 53 /* S3 resume failed */ | 53 /* S3 resume failed */ |
| 54 #define BINF0_S3_RESUME_FAILED 10 | 54 #define BINF0_S3_RESUME_FAILED 10 |
| 55 /* Recovery caused by TPM error */ | 55 /* Recovery caused by TPM error */ |
| 56 #define BINF0_RECOVERY_TPM_ERROR 11 | 56 #define BINF0_RECOVERY_TPM_ERROR 11 |
| 57 /* Firmware types from BINF.3 */ |
| 58 #define BINF3_RECOVERY 0 |
| 59 #define BINF3_NORMAL 1 |
| 60 #define BINF3_DEVELOPER 2 |
| 57 | 61 |
| 58 /* Base name for ACPI files */ | 62 /* Base name for ACPI files */ |
| 59 #define ACPI_BASE_PATH "/sys/devices/platform/chromeos_acpi" | 63 #define ACPI_BASE_PATH "/sys/devices/platform/chromeos_acpi" |
| 60 /* Paths for frequently used ACPI files */ | 64 /* Paths for frequently used ACPI files */ |
| 61 #define ACPI_BINF_PATH ACPI_BASE_PATH "/BINF" | 65 #define ACPI_BINF_PATH ACPI_BASE_PATH "/BINF" |
| 62 #define ACPI_CHNV_PATH ACPI_BASE_PATH "/CHNV" | 66 #define ACPI_CHNV_PATH ACPI_BASE_PATH "/CHNV" |
| 63 #define ACPI_CHSW_PATH ACPI_BASE_PATH "/CHSW" | 67 #define ACPI_CHSW_PATH ACPI_BASE_PATH "/CHSW" |
| 64 #define ACPI_FMAP_PATH ACPI_BASE_PATH "/FMAP" | 68 #define ACPI_FMAP_PATH ACPI_BASE_PATH "/FMAP" |
| 65 #define ACPI_GPIO_PATH ACPI_BASE_PATH "/GPIO" | 69 #define ACPI_GPIO_PATH ACPI_BASE_PATH "/GPIO" |
| 66 #define ACPI_VBNV_PATH ACPI_BASE_PATH "/VBNV" | 70 #define ACPI_VBNV_PATH ACPI_BASE_PATH "/VBNV" |
| 67 | 71 |
| 68 /* Base name for GPIO files */ | 72 /* Base name for GPIO files */ |
| 69 #define GPIO_BASE_PATH "/sys/class/gpio" | 73 #define GPIO_BASE_PATH "/sys/class/gpio" |
| 70 #define GPIO_EXPORT_PATH GPIO_BASE_PATH "/export" | 74 #define GPIO_EXPORT_PATH GPIO_BASE_PATH "/export" |
| 71 | 75 |
| 72 /* Base name for NVRAM file */ | 76 /* Filename for NVRAM file */ |
| 73 #define NVRAM_PATH "/dev/nvram" | 77 #define NVRAM_PATH "/dev/nvram" |
| 74 | 78 |
| 79 /* Filename for kernel command line */ |
| 80 #define KERNEL_CMDLINE_PATH "/proc/cmdline" |
| 81 |
| 75 | 82 |
| 76 /* Copy up to dest_size-1 characters from src to dest, ensuring null | 83 /* Copy up to dest_size-1 characters from src to dest, ensuring null |
| 77 termination (which strncpy() doesn't do). Returns the destination | 84 termination (which strncpy() doesn't do). Returns the destination |
| 78 string. */ | 85 string. */ |
| 79 char* StrCopy(char* dest, const char* src, int dest_size) { | 86 char* StrCopy(char* dest, const char* src, int dest_size) { |
| 80 strncpy(dest, src, dest_size); | 87 strncpy(dest, src, dest_size); |
| 81 dest[dest_size - 1] = '\0'; | 88 dest[dest_size - 1] = '\0'; |
| 82 return dest; | 89 return dest; |
| 83 } | 90 } |
| 84 | 91 |
| (...skipping 322 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 407 } | 414 } |
| 408 | 415 |
| 409 | 416 |
| 410 /* Read the active main firmware type into the destination buffer. | 417 /* Read the active main firmware type into the destination buffer. |
| 411 * Passed the destination and its size. Returns the destination, or | 418 * Passed the destination and its size. Returns the destination, or |
| 412 * NULL if error. */ | 419 * NULL if error. */ |
| 413 const char* VbReadMainFwType(char* dest, int size) { | 420 const char* VbReadMainFwType(char* dest, int size) { |
| 414 | 421 |
| 415 /* Try reading type from BINF.3 */ | 422 /* Try reading type from BINF.3 */ |
| 416 switch(ReadFileInt(ACPI_BINF_PATH ".3")) { | 423 switch(ReadFileInt(ACPI_BINF_PATH ".3")) { |
| 417 case 0: | 424 case BINF3_RECOVERY: |
| 418 return StrCopy(dest, "recovery", size); | 425 return StrCopy(dest, "recovery", size); |
| 419 case 1: | 426 case BINF3_NORMAL: |
| 420 return StrCopy(dest, "normal", size); | 427 return StrCopy(dest, "normal", size); |
| 421 case 2: | 428 case BINF3_DEVELOPER: |
| 422 return StrCopy(dest, "developer", size); | 429 return StrCopy(dest, "developer", size); |
| 423 default: | 430 default: |
| 424 break; /* Fall through to legacy handling */ | 431 break; /* Fall through to legacy handling */ |
| 425 } | 432 } |
| 426 | 433 |
| 427 /* Fall back to BINF.0 for legacy systems like Mario. */ | 434 /* Fall back to BINF.0 for legacy systems like Mario. */ |
| 428 switch(ReadFileInt(ACPI_BINF_PATH ".0")) { | 435 switch(ReadFileInt(ACPI_BINF_PATH ".0")) { |
| 429 case -1: | 436 case -1: |
| 430 /* Both BINF.0 and BINF.3 are missing, so this isn't Chrome OS | 437 /* Both BINF.0 and BINF.3 are missing, so this isn't Chrome OS |
| 431 * firmware. */ | 438 * firmware. */ |
| (...skipping 11 matching lines...) Expand all Loading... |
| 443 case BINF0_RECOVERY_TPM_ERROR: | 450 case BINF0_RECOVERY_TPM_ERROR: |
| 444 /* Assorted flavors of recovery boot reason. */ | 451 /* Assorted flavors of recovery boot reason. */ |
| 445 return StrCopy(dest, "recovery", size); | 452 return StrCopy(dest, "recovery", size); |
| 446 default: | 453 default: |
| 447 /* Other values don't map cleanly to firmware type. */ | 454 /* Other values don't map cleanly to firmware type. */ |
| 448 return NULL; | 455 return NULL; |
| 449 } | 456 } |
| 450 } | 457 } |
| 451 | 458 |
| 452 | 459 |
| 460 /* 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. */ |
| 462 int 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. |
| 476 * For all these cases, check /proc/cmdline for cros_[no]debug. */ |
| 477 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; |
| 486 else if (0 == strcmp(t, "cros_nodebug")) |
| 487 return 0; |
| 488 } |
| 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 |
| 453 | 499 |
| 454 /* Read a system property integer. | 500 /* Read a system property integer. |
| 455 * | 501 * |
| 456 * Returns the property value, or -1 if error. */ | 502 * Returns the property value, or -1 if error. */ |
| 457 int VbGetSystemPropertyInt(const char* name) { | 503 int VbGetSystemPropertyInt(const char* name) { |
| 458 int value = -1; | 504 int value = -1; |
| 459 | 505 |
| 460 /* Switch positions */ | 506 /* Switch positions */ |
| 461 if (!strcasecmp(name,"devsw_cur")) { | 507 if (!strcasecmp(name,"devsw_cur")) { |
| 462 value = ReadGpio(GPIO_SIGNAL_TYPE_DEV); | 508 value = ReadGpio(GPIO_SIGNAL_TYPE_DEV); |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 505 } else if (!strcasecmp(name,"fwb_tries")) { | 551 } else if (!strcasecmp(name,"fwb_tries")) { |
| 506 value = VbGetNvStorage(VBNV_TRY_B_COUNT); | 552 value = VbGetNvStorage(VBNV_TRY_B_COUNT); |
| 507 if (-1 == value) | 553 if (-1 == value) |
| 508 value = VbGetCmosRebootField(CMOSRF_TRY_B); | 554 value = VbGetCmosRebootField(CMOSRF_TRY_B); |
| 509 } | 555 } |
| 510 /* Other parameters */ | 556 /* Other parameters */ |
| 511 else if (!strcasecmp(name,"recovery_reason")) { | 557 else if (!strcasecmp(name,"recovery_reason")) { |
| 512 return VbGetRecoveryReason(); | 558 return VbGetRecoveryReason(); |
| 513 } else if (!strcasecmp(name,"fmap_base")) { | 559 } else if (!strcasecmp(name,"fmap_base")) { |
| 514 value = ReadFileInt(ACPI_FMAP_PATH); | 560 value = ReadFileInt(ACPI_FMAP_PATH); |
| 561 } else if (!strcasecmp(name,"cros_debug")) { |
| 562 value = VbGetCrosDebug(); |
| 515 } | 563 } |
| 516 | 564 |
| 517 return value; | 565 return value; |
| 518 } | 566 } |
| 519 | 567 |
| 520 | 568 |
| 521 /* Read a system property string into a destination buffer of the specified | 569 /* Read a system property string into a destination buffer of the specified |
| 522 * size. | 570 * size. |
| 523 * | 571 * |
| 524 * Returns the passed buffer, or NULL if error. */ | 572 * Returns the passed buffer, or NULL if error. */ |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 599 | 647 |
| 600 | 648 |
| 601 /* Set a system property string. | 649 /* Set a system property string. |
| 602 * | 650 * |
| 603 * Returns 0 if success, -1 if error. */ | 651 * Returns 0 if success, -1 if error. */ |
| 604 int VbSetSystemPropertyString(const char* name, const char* value) { | 652 int VbSetSystemPropertyString(const char* name, const char* value) { |
| 605 | 653 |
| 606 /* TODO: support setting */ | 654 /* TODO: support setting */ |
| 607 return -1; | 655 return -1; |
| 608 } | 656 } |
| OLD | NEW |