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_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 } |
| 487 |
| 488 /* Normal new firmware or older Chrome OS firmware allows debug if the |
| 489 * dev switch is on. */ |
| 490 if (1 == ReadFileBit(ACPI_CHSW_PATH, CHSW_DEV_BOOT)) |
| 491 return 1; |
| 492 |
| 493 /* All other cases disallow debug. */ |
| 494 return 0; |
| 495 } |
| 496 |
453 | 497 |
454 /* Read a system property integer. | 498 /* Read a system property integer. |
455 * | 499 * |
456 * Returns the property value, or -1 if error. */ | 500 * Returns the property value, or -1 if error. */ |
457 int VbGetSystemPropertyInt(const char* name) { | 501 int VbGetSystemPropertyInt(const char* name) { |
458 int value = -1; | 502 int value = -1; |
459 | 503 |
460 /* Switch positions */ | 504 /* Switch positions */ |
461 if (!strcasecmp(name,"devsw_cur")) { | 505 if (!strcasecmp(name,"devsw_cur")) { |
462 value = ReadGpio(GPIO_SIGNAL_TYPE_DEV); | 506 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")) { | 549 } else if (!strcasecmp(name,"fwb_tries")) { |
506 value = VbGetNvStorage(VBNV_TRY_B_COUNT); | 550 value = VbGetNvStorage(VBNV_TRY_B_COUNT); |
507 if (-1 == value) | 551 if (-1 == value) |
508 value = VbGetCmosRebootField(CMOSRF_TRY_B); | 552 value = VbGetCmosRebootField(CMOSRF_TRY_B); |
509 } | 553 } |
510 /* Other parameters */ | 554 /* Other parameters */ |
511 else if (!strcasecmp(name,"recovery_reason")) { | 555 else if (!strcasecmp(name,"recovery_reason")) { |
512 return VbGetRecoveryReason(); | 556 return VbGetRecoveryReason(); |
513 } else if (!strcasecmp(name,"fmap_base")) { | 557 } else if (!strcasecmp(name,"fmap_base")) { |
514 value = ReadFileInt(ACPI_FMAP_PATH); | 558 value = ReadFileInt(ACPI_FMAP_PATH); |
| 559 } else if (!strcasecmp(name,"cros_debug")) { |
| 560 value = VbGetCrosDebug(); |
515 } | 561 } |
516 | 562 |
517 return value; | 563 return value; |
518 } | 564 } |
519 | 565 |
520 | 566 |
521 /* Read a system property string into a destination buffer of the specified | 567 /* Read a system property string into a destination buffer of the specified |
522 * size. | 568 * size. |
523 * | 569 * |
524 * Returns the passed buffer, or NULL if error. */ | 570 * Returns the passed buffer, or NULL if error. */ |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
599 | 645 |
600 | 646 |
601 /* Set a system property string. | 647 /* Set a system property string. |
602 * | 648 * |
603 * Returns 0 if success, -1 if error. */ | 649 * Returns 0 if success, -1 if error. */ |
604 int VbSetSystemPropertyString(const char* name, const char* value) { | 650 int VbSetSystemPropertyString(const char* name, const char* value) { |
605 | 651 |
606 /* TODO: support setting */ | 652 /* TODO: support setting */ |
607 return -1; | 653 return -1; |
608 } | 654 } |
OLD | NEW |