OLD | NEW |
(Empty) | |
| 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" |
| 14 |
| 15 /* ACPI constants from Chrome OS Main Processor Firmware Spec */ |
| 16 /* GPIO signal types */ |
| 17 #define GPIO_SIGNAL_TYPE_RECOVERY 1 |
| 18 #define GPIO_SIGNAL_TYPE_DEV 2 |
| 19 #define GPIO_SIGNAL_TYPE_WP 3 |
| 20 /* CHSW bitflags */ |
| 21 #define CHSW_RECOVERY_BOOT 0x00000002 |
| 22 #define CHSW_RECOVERY_EC_BOOT 0x00000004 |
| 23 #define CHSW_DEV_BOOT 0x00000020 |
| 24 #define CHSW_WP_BOOT 0x00000200 |
| 25 |
| 26 /* Base name for ACPI files */ |
| 27 #define ACPI_BASE_PATH "/sys/devices/platform/chromeos_acpi" |
| 28 /* Paths for frequently used ACPI files */ |
| 29 #define ACPI_CHNV_PATH ACPI_BASE_PATH "/CHNV" |
| 30 #define ACPI_CHSW_PATH ACPI_BASE_PATH "/CHSW" |
| 31 #define ACPI_GPIO_PATH ACPI_BASE_PATH "/GPIO" |
| 32 |
| 33 /* Base name for GPIO files */ |
| 34 #define GPIO_BASE_PATH "/sys/class/gpio" |
| 35 #define GPIO_EXPORT_PATH GPIO_BASE_PATH "/export" |
| 36 |
| 37 /* Read a string from a file. Passed the destination, dest size, and |
| 38 * filename to read. |
| 39 * |
| 40 * Returns the destination, or NULL if error. */ |
| 41 char* ReadFileString(char* dest, int size, const char* filename) { |
| 42 char* got; |
| 43 FILE* f; |
| 44 |
| 45 f = fopen(filename, "rt"); |
| 46 if (!f) |
| 47 return NULL; |
| 48 |
| 49 got = fgets(dest, size, f); |
| 50 fclose(f); |
| 51 return got; |
| 52 } |
| 53 |
| 54 |
| 55 /* Read an integer from a file. |
| 56 * |
| 57 * Returns the parsed integer, or -1 if error. */ |
| 58 int ReadFileInt(const char* filename) { |
| 59 char buf[64]; |
| 60 int value; |
| 61 char* e = NULL; |
| 62 |
| 63 if (!ReadFileString(buf, sizeof(buf), filename)) |
| 64 return -1; |
| 65 |
| 66 /* Convert to integer. Allow characters after the int ("123 blah"). */ |
| 67 value = strtol(buf, &e, 0); |
| 68 if (e == buf) |
| 69 return -1; /* No characters consumed, so conversion failed */ |
| 70 |
| 71 return value; |
| 72 } |
| 73 |
| 74 |
| 75 /* Check if a bit is set in a file which contains an integer. |
| 76 * |
| 77 * Returns 1 if the bit is set, 0 if clear, or -1 if error. */ |
| 78 int ReadFileBit(const char* filename, int bitmask) { |
| 79 int value = ReadFileInt(filename); |
| 80 if (value == -1) |
| 81 return -1; |
| 82 else return (value & bitmask ? 1 : 0); |
| 83 } |
| 84 |
| 85 |
| 86 /* Read a GPIO of the specified signal type (see ACPI GPIO SignalType). |
| 87 * |
| 88 * Returns 1 if the signal is asserted, 0 if not asserted, or -1 if error. */ |
| 89 int ReadGpio(int signal_type) { |
| 90 char name[128]; |
| 91 int index = 0; |
| 92 int gpio_type; |
| 93 int active_high; |
| 94 int controller_offset; |
| 95 char controller_name[128]; |
| 96 int value; |
| 97 |
| 98 /* Scan GPIO.* to find a matching signal type */ |
| 99 for (index = 0; ; index++) { |
| 100 snprintf(name, sizeof(name), "%s.%d/GPIO.0", ACPI_GPIO_PATH, index); |
| 101 gpio_type = ReadFileInt(name); |
| 102 if (gpio_type == signal_type) |
| 103 break; |
| 104 else if (gpio_type == -1) |
| 105 return -1; /* Ran out of GPIOs before finding a match */ |
| 106 } |
| 107 |
| 108 /* Read attributes and controller info for the GPIO */ |
| 109 snprintf(name, sizeof(name), "%s.%d/GPIO.1", ACPI_GPIO_PATH, index); |
| 110 active_high = ReadFileBit(name, 0x00000001); |
| 111 snprintf(name, sizeof(name), "%s.%d/GPIO.2", ACPI_GPIO_PATH, index); |
| 112 controller_offset = ReadFileInt(name); |
| 113 if (active_high == -1 || controller_offset == -1) |
| 114 return -1; /* Missing needed info */ |
| 115 |
| 116 /* We only support the NM10 for now */ |
| 117 snprintf(name, sizeof(name), "%s.%d/GPIO.3", ACPI_GPIO_PATH, index); |
| 118 if (!ReadFileString(controller_name, sizeof(controller_name), name)) |
| 119 return -1; |
| 120 if (0 != strcmp(controller_name, "NM10")) |
| 121 return -1; |
| 122 |
| 123 /* Assume the NM10 has offset 192 */ |
| 124 /* TODO: should really check gpiochipNNN/label to see if it's the |
| 125 * address we expect for the NM10, and then read the offset from |
| 126 * gpiochipNNN/base. */ |
| 127 controller_offset += 192; |
| 128 |
| 129 /* Try reading the GPIO value */ |
| 130 snprintf(name, sizeof(name), "%s/gpio%d/value", |
| 131 GPIO_BASE_PATH, controller_offset); |
| 132 value = ReadFileInt(name); |
| 133 |
| 134 if (value == -1) { |
| 135 /* Try exporting the GPIO */ |
| 136 FILE* f = fopen(GPIO_EXPORT_PATH, "wt"); |
| 137 if (!f) |
| 138 return -1; |
| 139 fprintf(f, "%d", controller_offset); |
| 140 fclose(f); |
| 141 |
| 142 /* Try re-reading the GPIO value */ |
| 143 value = ReadFileInt(name); |
| 144 } |
| 145 |
| 146 if (value == -1) |
| 147 return -1; |
| 148 |
| 149 /* Compare the GPIO value with the active value and return 1 if match. */ |
| 150 return (value == active_high ? 1 : 0); |
| 151 } |
| 152 |
| 153 |
| 154 /* Read a system property integer. |
| 155 * |
| 156 * Returns the property value, or -1 if error. */ |
| 157 int VbGetSystemPropertyInt(const char* name) { |
| 158 |
| 159 if (!strcasecmp(name,"devsw_cur")) { |
| 160 return ReadGpio(GPIO_SIGNAL_TYPE_DEV); |
| 161 } else if (!strcasecmp(name,"devsw_boot")) { |
| 162 return ReadFileBit(ACPI_CHSW_PATH, CHSW_DEV_BOOT); |
| 163 } else if (!strcasecmp(name,"recoverysw_cur")) { |
| 164 return ReadGpio(GPIO_SIGNAL_TYPE_RECOVERY); |
| 165 } else if (!strcasecmp(name,"recoverysw_boot")) { |
| 166 return ReadFileBit(ACPI_CHSW_PATH, CHSW_RECOVERY_BOOT); |
| 167 } else if (!strcasecmp(name,"recoverysw_ec_boot")) { |
| 168 return ReadFileBit(ACPI_CHSW_PATH, CHSW_RECOVERY_EC_BOOT); |
| 169 } else if (!strcasecmp(name,"wpsw_cur")) { |
| 170 return ReadGpio(GPIO_SIGNAL_TYPE_WP); |
| 171 } else if (!strcasecmp(name,"wpsw_boot")) { |
| 172 return ReadFileBit(ACPI_CHSW_PATH, CHSW_WP_BOOT); |
| 173 } else |
| 174 return -1; |
| 175 |
| 176 /* TODO: remaining properties from spec */ |
| 177 } |
| 178 |
| 179 |
| 180 /* Read a system property string into a destination buffer of the specified |
| 181 * size. |
| 182 * |
| 183 * Returns the passed buffer, or NULL if error. */ |
| 184 const char* VbGetSystemPropertyString(const char* name, char* dest, int size) { |
| 185 |
| 186 if (!strcasecmp(name,"hwid")) { |
| 187 return ReadFileString(dest, size, ACPI_BASE_PATH "/HWID"); |
| 188 } else if (!strcasecmp(name,"fwid")) { |
| 189 return ReadFileString(dest, size, ACPI_BASE_PATH "/FWID"); |
| 190 } else if (!strcasecmp(name,"ro_fwid")) { |
| 191 return ReadFileString(dest, size, ACPI_BASE_PATH "/FRID"); |
| 192 } else |
| 193 return NULL; |
| 194 |
| 195 /* TODO: remaining properties from spec */ |
| 196 } |
| 197 |
| 198 |
| 199 /* Set a system property integer. |
| 200 * |
| 201 * Returns 0 if success, -1 if error. */ |
| 202 int VbSetSystemPropertyInt(const char* name, int value) { |
| 203 |
| 204 /* TODO: support setting */ |
| 205 return -1; |
| 206 } |
| 207 |
| 208 |
| 209 /* Set a system property string. |
| 210 * |
| 211 * Returns 0 if success, -1 if error. */ |
| 212 int VbSetSystemPropertyString(const char* name, const char* value) { |
| 213 |
| 214 /* TODO: support setting */ |
| 215 return -1; |
| 216 } |
OLD | NEW |