| 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 #include <sys/types.h> | 8 #include <sys/types.h> |
| 9 #include <sys/stat.h> | 9 #include <sys/stat.h> |
| 10 #include <unistd.h> | 10 #include <unistd.h> |
| 11 #include <ctype.h> | 11 #include <ctype.h> |
| 12 | 12 |
| 13 #include "host_common.h" | 13 #include "host_common.h" |
| 14 | 14 |
| 15 #include "crossystem.h" | 15 #include "crossystem.h" |
| 16 #include "utility.h" | 16 #include "utility.h" |
| 17 #include "vboot_common.h" | 17 #include "vboot_common.h" |
| 18 #include "vboot_nvstorage.h" | 18 #include "vboot_nvstorage.h" |
| 19 #include "vboot_struct.h" |
| 19 | 20 |
| 20 /* ACPI constants from Chrome OS Main Processor Firmware Spec */ | 21 /* ACPI constants from Chrome OS Main Processor Firmware Spec */ |
| 21 /* GPIO signal types */ | 22 /* GPIO signal types */ |
| 22 #define GPIO_SIGNAL_TYPE_RECOVERY 1 | 23 #define GPIO_SIGNAL_TYPE_RECOVERY 1 |
| 23 #define GPIO_SIGNAL_TYPE_DEV 2 | 24 #define GPIO_SIGNAL_TYPE_DEV 2 |
| 24 #define GPIO_SIGNAL_TYPE_WP 3 | 25 #define GPIO_SIGNAL_TYPE_WP 3 |
| 25 /* CHSW bitflags */ | 26 /* CHSW bitflags */ |
| 26 #define CHSW_RECOVERY_BOOT 0x00000002 | 27 #define CHSW_RECOVERY_BOOT 0x00000002 |
| 27 #define CHSW_RECOVERY_EC_BOOT 0x00000004 | 28 #define CHSW_RECOVERY_EC_BOOT 0x00000004 |
| 28 #define CHSW_DEV_BOOT 0x00000020 | 29 #define CHSW_DEV_BOOT 0x00000020 |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 80 | 81 |
| 81 /* Filename for NVRAM file */ | 82 /* Filename for NVRAM file */ |
| 82 #define NVRAM_PATH "/dev/nvram" | 83 #define NVRAM_PATH "/dev/nvram" |
| 83 | 84 |
| 84 /* Filename for kernel command line */ | 85 /* Filename for kernel command line */ |
| 85 #define KERNEL_CMDLINE_PATH "/proc/cmdline" | 86 #define KERNEL_CMDLINE_PATH "/proc/cmdline" |
| 86 | 87 |
| 87 /* A structure to contain buffer data retrieved from the ACPI. */ | 88 /* A structure to contain buffer data retrieved from the ACPI. */ |
| 88 typedef struct { | 89 typedef struct { |
| 89 int buffer_size; | 90 int buffer_size; |
| 90 void* buffer; | 91 uint8_t* buffer; |
| 91 } AcpiBuffer; | 92 } AcpiBuffer; |
| 92 | 93 |
| 93 | 94 |
| 95 /* Fields that GetVdatString() can get */ |
| 96 typedef enum VdatStringField { |
| 97 VDAT_STRING_TIMERS = 0, /* Timer values */ |
| 98 VDAT_STRING_LOAD_FIRMWARE_DEBUG /* LoadFirmware() debug information */ |
| 99 } VdatStringField; |
| 100 |
| 101 |
| 102 /* Fields that GetVdatInt() can get */ |
| 103 typedef enum VdatIntField { |
| 104 VDAT_INT_FLAGS = 0 /* Flags */ |
| 105 } VdatIntField; |
| 106 |
| 107 |
| 94 /* Copy up to dest_size-1 characters from src to dest, ensuring null | 108 /* Copy up to dest_size-1 characters from src to dest, ensuring null |
| 95 termination (which strncpy() doesn't do). Returns the destination | 109 termination (which strncpy() doesn't do). Returns the destination |
| 96 string. */ | 110 string. */ |
| 97 char* StrCopy(char* dest, const char* src, int dest_size) { | 111 char* StrCopy(char* dest, const char* src, int dest_size) { |
| 98 strncpy(dest, src, dest_size); | 112 strncpy(dest, src, dest_size); |
| 99 dest[dest_size - 1] = '\0'; | 113 dest[dest_size - 1] = '\0'; |
| 100 return dest; | 114 return dest; |
| 101 } | 115 } |
| 102 | 116 |
| 103 | 117 |
| (...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 312 | 326 |
| 313 AcpiBuffer* VbGetBuffer(const char* filename) | 327 AcpiBuffer* VbGetBuffer(const char* filename) |
| 314 { | 328 { |
| 315 FILE* f = NULL; | 329 FILE* f = NULL; |
| 316 char* file_buffer = NULL; | 330 char* file_buffer = NULL; |
| 317 AcpiBuffer* acpi_buffer = NULL; | 331 AcpiBuffer* acpi_buffer = NULL; |
| 318 AcpiBuffer* return_value = NULL; | 332 AcpiBuffer* return_value = NULL; |
| 319 | 333 |
| 320 do { | 334 do { |
| 321 struct stat fs; | 335 struct stat fs; |
| 322 unsigned char* output_ptr; | 336 uint8_t* output_ptr; |
| 323 int rv, i, real_size; | 337 int rv, i, real_size; |
| 324 | 338 |
| 325 rv = stat(filename, &fs); | 339 rv = stat(filename, &fs); |
| 326 if (rv || !S_ISREG(fs.st_mode)) | 340 if (rv || !S_ISREG(fs.st_mode)) |
| 327 break; | 341 break; |
| 328 | 342 |
| 329 f = fopen(filename, "r"); | 343 f = fopen(filename, "r"); |
| 330 if (!f) | 344 if (!f) |
| 331 break; | 345 break; |
| 332 | 346 |
| 333 file_buffer = Malloc(fs.st_size + 1); | 347 file_buffer = Malloc(fs.st_size + 1); |
| 334 if (!file_buffer) | 348 if (!file_buffer) |
| 335 break; | 349 break; |
| 336 | 350 |
| 337 | |
| 338 real_size = fread(file_buffer, 1, fs.st_size, f); | 351 real_size = fread(file_buffer, 1, fs.st_size, f); |
| 339 if (!real_size) | 352 if (!real_size) |
| 340 break; | 353 break; |
| 354 file_buffer[real_size] = '\0'; |
| 341 | 355 |
| 342 /* each byte in the output will replace two characters and a space in the | 356 /* Each byte in the output will replace two characters and a space |
| 343 * input, so the output size does not exceed input side/3 (a little less | 357 * in the input, so the output size does not exceed input side/3 |
| 344 * if account for newline characters). | 358 * (a little less if account for newline characters). */ |
| 345 */ | |
| 346 acpi_buffer = Malloc(sizeof(AcpiBuffer) + real_size/3); | 359 acpi_buffer = Malloc(sizeof(AcpiBuffer) + real_size/3); |
| 347 | |
| 348 if (!acpi_buffer) | 360 if (!acpi_buffer) |
| 349 break; | 361 break; |
| 350 | 362 acpi_buffer->buffer = (uint8_t*)(acpi_buffer + 1); |
| 351 file_buffer[real_size] = '\0'; | |
| 352 | |
| 353 acpi_buffer->buffer = acpi_buffer + 1; | |
| 354 acpi_buffer->buffer_size = 0; | 363 acpi_buffer->buffer_size = 0; |
| 355 output_ptr = acpi_buffer->buffer; | 364 output_ptr = acpi_buffer->buffer; |
| 356 | 365 |
| 357 /* process the file contents */ | 366 /* process the file contents */ |
| 358 for (i = 0; i < real_size; i++) { | 367 for (i = 0; i < real_size; i++) { |
| 359 char* base, *end; | 368 char* base, *end; |
| 360 | 369 |
| 361 base = file_buffer + i; | 370 base = file_buffer + i; |
| 362 | 371 |
| 363 if (!isxdigit(*base)) | 372 if (!isxdigit(*base)) |
| (...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 597 /* Normal new firmware or older Chrome OS firmware allows debug if the | 606 /* Normal new firmware or older Chrome OS firmware allows debug if the |
| 598 * dev switch is on. */ | 607 * dev switch is on. */ |
| 599 if (1 == ReadFileBit(ACPI_CHSW_PATH, CHSW_DEV_BOOT)) | 608 if (1 == ReadFileBit(ACPI_CHSW_PATH, CHSW_DEV_BOOT)) |
| 600 return 1; | 609 return 1; |
| 601 | 610 |
| 602 /* All other cases disallow debug. */ | 611 /* All other cases disallow debug. */ |
| 603 return 0; | 612 return 0; |
| 604 } | 613 } |
| 605 | 614 |
| 606 | 615 |
| 616 char* GetVdatString(char* dest, int size, VdatStringField field) |
| 617 { |
| 618 VbSharedDataHeader* sh; |
| 619 AcpiBuffer* ab = VbGetBuffer(ACPI_VDAT_PATH); |
| 620 if (!ab) |
| 621 return NULL; |
| 622 |
| 623 sh = (VbSharedDataHeader*)ab->buffer; |
| 624 |
| 625 switch (field) { |
| 626 case VDAT_STRING_TIMERS: |
| 627 snprintf(dest, size, |
| 628 "LFS=%" PRIu64 ",%" PRIu64 |
| 629 " LF=%" PRIu64 ",%" PRIu64 |
| 630 " LK=%" PRIu64 ",%" PRIu64, |
| 631 sh->timer_load_firmware_start_enter, |
| 632 sh->timer_load_firmware_start_exit, |
| 633 sh->timer_load_firmware_enter, |
| 634 sh->timer_load_firmware_exit, |
| 635 sh->timer_load_kernel_enter, |
| 636 sh->timer_load_kernel_exit); |
| 637 break; |
| 638 |
| 639 case VDAT_STRING_LOAD_FIRMWARE_DEBUG: |
| 640 snprintf(dest, size, |
| 641 "check=%d,%d index=0x%02x tpmver=0x%x lowestver=0x%x", |
| 642 sh->check_fw_a_result, |
| 643 sh->check_fw_b_result, |
| 644 sh->firmware_index, |
| 645 sh->fw_version_tpm_start, |
| 646 sh->fw_version_lowest); |
| 647 break; |
| 648 |
| 649 default: |
| 650 Free(ab); |
| 651 return NULL; |
| 652 } |
| 653 |
| 654 Free(ab); |
| 655 return dest; |
| 656 } |
| 657 |
| 658 |
| 659 int GetVdatInt(VdatIntField field) { |
| 660 VbSharedDataHeader* sh; |
| 661 AcpiBuffer* ab = VbGetBuffer(ACPI_VDAT_PATH); |
| 662 int value = -1; |
| 663 |
| 664 if (!ab) |
| 665 return -1; |
| 666 |
| 667 sh = (VbSharedDataHeader*)ab->buffer; |
| 668 |
| 669 switch (field) { |
| 670 case VDAT_INT_FLAGS: |
| 671 value = (int)sh->flags; |
| 672 break; |
| 673 |
| 674 } |
| 675 |
| 676 Free(ab); |
| 677 return value; |
| 678 } |
| 679 |
| 680 |
| 607 /* Read a system property integer. | 681 /* Read a system property integer. |
| 608 * | 682 * |
| 609 * Returns the property value, or -1 if error. */ | 683 * Returns the property value, or -1 if error. */ |
| 610 int VbGetSystemPropertyInt(const char* name) { | 684 int VbGetSystemPropertyInt(const char* name) { |
| 611 int value = -1; | 685 int value = -1; |
| 612 | 686 |
| 613 /* Switch positions */ | 687 /* Switch positions */ |
| 614 if (!strcasecmp(name,"devsw_cur")) { | 688 if (!strcasecmp(name,"devsw_cur")) { |
| 615 value = ReadGpio(GPIO_SIGNAL_TYPE_DEV); | 689 value = ReadGpio(GPIO_SIGNAL_TYPE_DEV); |
| 616 } else if (!strcasecmp(name,"devsw_boot")) { | 690 } else if (!strcasecmp(name,"devsw_boot")) { |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 664 if (-1 == value) | 738 if (-1 == value) |
| 665 value = VbGetCmosRebootField(CMOSRF_TRY_B); | 739 value = VbGetCmosRebootField(CMOSRF_TRY_B); |
| 666 } | 740 } |
| 667 /* Other parameters */ | 741 /* Other parameters */ |
| 668 else if (!strcasecmp(name,"recovery_reason")) { | 742 else if (!strcasecmp(name,"recovery_reason")) { |
| 669 return VbGetRecoveryReason(); | 743 return VbGetRecoveryReason(); |
| 670 } else if (!strcasecmp(name,"fmap_base")) { | 744 } else if (!strcasecmp(name,"fmap_base")) { |
| 671 value = ReadFileInt(ACPI_FMAP_PATH); | 745 value = ReadFileInt(ACPI_FMAP_PATH); |
| 672 } else if (!strcasecmp(name,"cros_debug")) { | 746 } else if (!strcasecmp(name,"cros_debug")) { |
| 673 value = VbGetCrosDebug(); | 747 value = VbGetCrosDebug(); |
| 748 } else if (!strcasecmp(name,"vdat_flags")) { |
| 749 value = GetVdatInt(VDAT_INT_FLAGS); |
| 674 } | 750 } |
| 675 | 751 |
| 676 return value; | 752 return value; |
| 677 } | 753 } |
| 678 | 754 |
| 679 /* This function is just an example illustrating the use of VbGetBuffer(). It | |
| 680 * converts the binary contents of the buffer into a space delimetered hex | |
| 681 * string. It is expected to be replaced with a function which has knowledge | |
| 682 * of the buffer data structure. | |
| 683 */ | |
| 684 char* GetVdatBuffer(void) | |
| 685 { | |
| 686 char* buffer, *src, *p; | |
| 687 int i; | |
| 688 | |
| 689 AcpiBuffer* ab = VbGetBuffer(ACPI_VDAT_PATH); | |
| 690 if (!ab) | |
| 691 return NULL; | |
| 692 | |
| 693 buffer = Malloc(ab->buffer_size * 3 + 2); | |
| 694 p = buffer; | |
| 695 src = ab->buffer; | |
| 696 for (i = 0; i < ab->buffer_size; i++) { | |
| 697 snprintf(p, 4, " %2.2x", *src++); | |
| 698 p += 3; | |
| 699 } | |
| 700 *p = '\0'; | |
| 701 Free(ab); | |
| 702 return buffer; | |
| 703 } | |
| 704 | |
| 705 /* Read a system property string into a destination buffer of the specified | 755 /* Read a system property string into a destination buffer of the specified |
| 706 * size. | 756 * size. |
| 707 * | 757 * |
| 708 * Returns the passed buffer, or NULL if error. */ | 758 * Returns the passed buffer, or NULL if error. */ |
| 709 const char* VbGetSystemPropertyString(const char* name, char* dest, int size) { | 759 const char* VbGetSystemPropertyString(const char* name, char* dest, int size) { |
| 710 | 760 |
| 711 if (!strcasecmp(name,"hwid")) { | 761 if (!strcasecmp(name,"hwid")) { |
| 712 return ReadFileString(dest, size, ACPI_BASE_PATH "/HWID"); | 762 return ReadFileString(dest, size, ACPI_BASE_PATH "/HWID"); |
| 713 } else if (!strcasecmp(name,"fwid")) { | 763 } else if (!strcasecmp(name,"fwid")) { |
| 714 return ReadFileString(dest, size, ACPI_BASE_PATH "/FWID"); | 764 return ReadFileString(dest, size, ACPI_BASE_PATH "/FWID"); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 738 } | 788 } |
| 739 } else if (!strcasecmp(name,"kernkey_vfy")) { | 789 } else if (!strcasecmp(name,"kernkey_vfy")) { |
| 740 switch(VbGetNvStorage(VBNV_FW_VERIFIED_KERNEL_KEY)) { | 790 switch(VbGetNvStorage(VBNV_FW_VERIFIED_KERNEL_KEY)) { |
| 741 case 0: | 791 case 0: |
| 742 return "hash"; | 792 return "hash"; |
| 743 case 1: | 793 case 1: |
| 744 return "sig"; | 794 return "sig"; |
| 745 default: | 795 default: |
| 746 return NULL; | 796 return NULL; |
| 747 } | 797 } |
| 748 } else if (!strcasecmp(name, "vdat")) { | 798 } else if (!strcasecmp(name, "vdat_timers")) { |
| 749 return GetVdatBuffer(); | 799 return GetVdatString(dest, size, VDAT_STRING_TIMERS); |
| 800 } else if (!strcasecmp(name, "vdat_lfdebug")) { |
| 801 return GetVdatString(dest, size, VDAT_STRING_LOAD_FIRMWARE_DEBUG); |
| 750 } else | 802 } else |
| 751 return NULL; | 803 return NULL; |
| 752 } | 804 } |
| 753 | 805 |
| 754 | 806 |
| 755 /* Set a system property integer. | 807 /* Set a system property integer. |
| 756 * | 808 * |
| 757 * Returns 0 if success, -1 if error. */ | 809 * Returns 0 if success, -1 if error. */ |
| 758 int VbSetSystemPropertyInt(const char* name, int value) { | 810 int VbSetSystemPropertyInt(const char* name, int value) { |
| 759 | 811 |
| (...skipping 29 matching lines...) Expand all Loading... |
| 789 | 841 |
| 790 | 842 |
| 791 /* Set a system property string. | 843 /* Set a system property string. |
| 792 * | 844 * |
| 793 * Returns 0 if success, -1 if error. */ | 845 * Returns 0 if success, -1 if error. */ |
| 794 int VbSetSystemPropertyString(const char* name, const char* value) { | 846 int VbSetSystemPropertyString(const char* name, const char* value) { |
| 795 | 847 |
| 796 /* TODO: support setting */ | 848 /* TODO: support setting */ |
| 797 return -1; | 849 return -1; |
| 798 } | 850 } |
| OLD | NEW |