| 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 |
| 11 #include "crossystem.h" | 11 #include "crossystem.h" |
| 12 #include "utility.h" | 12 #include "utility.h" |
| 13 #include "vboot_common.h" | 13 #include "vboot_common.h" |
| 14 #include "vboot_nvstorage.h" |
| 14 | 15 |
| 15 /* ACPI constants from Chrome OS Main Processor Firmware Spec */ | 16 /* ACPI constants from Chrome OS Main Processor Firmware Spec */ |
| 16 /* GPIO signal types */ | 17 /* GPIO signal types */ |
| 17 #define GPIO_SIGNAL_TYPE_RECOVERY 1 | 18 #define GPIO_SIGNAL_TYPE_RECOVERY 1 |
| 18 #define GPIO_SIGNAL_TYPE_DEV 2 | 19 #define GPIO_SIGNAL_TYPE_DEV 2 |
| 19 #define GPIO_SIGNAL_TYPE_WP 3 | 20 #define GPIO_SIGNAL_TYPE_WP 3 |
| 20 /* CHSW bitflags */ | 21 /* CHSW bitflags */ |
| 21 #define CHSW_RECOVERY_BOOT 0x00000002 | 22 #define CHSW_RECOVERY_BOOT 0x00000002 |
| 22 #define CHSW_RECOVERY_EC_BOOT 0x00000004 | 23 #define CHSW_RECOVERY_EC_BOOT 0x00000004 |
| 23 #define CHSW_DEV_BOOT 0x00000020 | 24 #define CHSW_DEV_BOOT 0x00000020 |
| 24 #define CHSW_WP_BOOT 0x00000200 | 25 #define CHSW_WP_BOOT 0x00000200 |
| 26 /* CMOS reboot field bitflags */ |
| 27 #define CMOSRF_RECOVERY 0x80 |
| 28 #define CMOSRF_DEBUG_RESET 0x40 |
| 29 #define CMOSRF_TRY_B 0x20 |
| 25 | 30 |
| 26 /* Base name for ACPI files */ | 31 /* Base name for ACPI files */ |
| 27 #define ACPI_BASE_PATH "/sys/devices/platform/chromeos_acpi" | 32 #define ACPI_BASE_PATH "/sys/devices/platform/chromeos_acpi" |
| 28 /* Paths for frequently used ACPI files */ | 33 /* Paths for frequently used ACPI files */ |
| 29 #define ACPI_CHNV_PATH ACPI_BASE_PATH "/CHNV" | 34 #define ACPI_CHNV_PATH ACPI_BASE_PATH "/CHNV" |
| 30 #define ACPI_CHSW_PATH ACPI_BASE_PATH "/CHSW" | 35 #define ACPI_CHSW_PATH ACPI_BASE_PATH "/CHSW" |
| 31 #define ACPI_GPIO_PATH ACPI_BASE_PATH "/GPIO" | 36 #define ACPI_GPIO_PATH ACPI_BASE_PATH "/GPIO" |
| 32 | 37 |
| 33 /* Base name for GPIO files */ | 38 /* Base name for GPIO files */ |
| 34 #define GPIO_BASE_PATH "/sys/class/gpio" | 39 #define GPIO_BASE_PATH "/sys/class/gpio" |
| 35 #define GPIO_EXPORT_PATH GPIO_BASE_PATH "/export" | 40 #define GPIO_EXPORT_PATH GPIO_BASE_PATH "/export" |
| 36 | 41 |
| 42 /* Base name for NVRAM file */ |
| 43 #define NVRAM_PATH "/dev/nvram" |
| 44 |
| 37 /* Read a string from a file. Passed the destination, dest size, and | 45 /* Read a string from a file. Passed the destination, dest size, and |
| 38 * filename to read. | 46 * filename to read. |
| 39 * | 47 * |
| 40 * Returns the destination, or NULL if error. */ | 48 * Returns the destination, or NULL if error. */ |
| 41 char* ReadFileString(char* dest, int size, const char* filename) { | 49 char* ReadFileString(char* dest, int size, const char* filename) { |
| 42 char* got; | 50 char* got; |
| 43 FILE* f; | 51 FILE* f; |
| 44 | 52 |
| 45 f = fopen(filename, "rt"); | 53 f = fopen(filename, "rt"); |
| 46 if (!f) | 54 if (!f) |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 154 } | 162 } |
| 155 | 163 |
| 156 if (value == -1) | 164 if (value == -1) |
| 157 return -1; | 165 return -1; |
| 158 | 166 |
| 159 /* Compare the GPIO value with the active value and return 1 if match. */ | 167 /* Compare the GPIO value with the active value and return 1 if match. */ |
| 160 return (value == active_high ? 1 : 0); | 168 return (value == active_high ? 1 : 0); |
| 161 } | 169 } |
| 162 | 170 |
| 163 | 171 |
| 172 /* Read the CMOS reboot field in NVRAM. |
| 173 * |
| 174 * Returns 0 if the mask is clear in the field, 1 if set, or -1 if error. */ |
| 175 int VbGetCmosRebootField(uint8_t mask) { |
| 176 FILE* f; |
| 177 int chnv, nvbyte; |
| 178 |
| 179 /* Get the byte offset from CHNV */ |
| 180 chnv = ReadFileInt(ACPI_CHNV_PATH); |
| 181 if (chnv == -1) |
| 182 return -1; |
| 183 |
| 184 f = fopen(NVRAM_PATH, "rb"); |
| 185 if (!f) |
| 186 return -1; |
| 187 |
| 188 if (0 != fseek(f, chnv, SEEK_SET) || EOF == (nvbyte = fgetc(f))) { |
| 189 fclose(f); |
| 190 return -1; |
| 191 } |
| 192 |
| 193 fclose(f); |
| 194 return (nvbyte & mask ? 1 : 0); |
| 195 } |
| 196 |
| 197 |
| 198 /* Write the CMOS reboot field in NVRAM. |
| 199 * |
| 200 * Sets (value=0) or clears (value!=0) the mask in the byte. |
| 201 * |
| 202 * Returns 0 if success, or -1 if error. */ |
| 203 int VbSetCmosRebootField(uint8_t mask, int value) { |
| 204 FILE* f; |
| 205 int chnv, nvbyte; |
| 206 |
| 207 /* Get the byte offset from CHNV */ |
| 208 chnv = ReadFileInt(ACPI_CHNV_PATH); |
| 209 if (chnv == -1) |
| 210 return -1; |
| 211 |
| 212 f = fopen(NVRAM_PATH, "w+b"); |
| 213 if (!f) |
| 214 return -1; |
| 215 |
| 216 /* Read the current value */ |
| 217 if (0 != fseek(f, chnv, SEEK_SET) || EOF == (nvbyte = fgetc(f))) { |
| 218 fclose(f); |
| 219 return -1; |
| 220 } |
| 221 |
| 222 /* Set/clear the mask */ |
| 223 if (value) |
| 224 nvbyte |= mask; |
| 225 else |
| 226 nvbyte &= ~mask; |
| 227 |
| 228 /* Write the byte back */ |
| 229 if (0 != fseek(f, chnv, SEEK_SET) || EOF == (fputc(nvbyte, f))) { |
| 230 fclose(f); |
| 231 return -1; |
| 232 } |
| 233 |
| 234 /* Success */ |
| 235 fclose(f); |
| 236 return 0; |
| 237 } |
| 238 |
| 164 /* Read a system property integer. | 239 /* Read a system property integer. |
| 165 * | 240 * |
| 166 * Returns the property value, or -1 if error. */ | 241 * Returns the property value, or -1 if error. */ |
| 167 int VbGetSystemPropertyInt(const char* name) { | 242 int VbGetSystemPropertyInt(const char* name) { |
| 168 int value = -1; | 243 int value = -1; |
| 169 | 244 |
| 245 /* Switch positions */ |
| 170 if (!strcasecmp(name,"devsw_cur")) { | 246 if (!strcasecmp(name,"devsw_cur")) { |
| 171 value = ReadGpio(GPIO_SIGNAL_TYPE_DEV); | 247 value = ReadGpio(GPIO_SIGNAL_TYPE_DEV); |
| 172 } else if (!strcasecmp(name,"devsw_boot")) { | 248 } else if (!strcasecmp(name,"devsw_boot")) { |
| 173 value = ReadFileBit(ACPI_CHSW_PATH, CHSW_DEV_BOOT); | 249 value = ReadFileBit(ACPI_CHSW_PATH, CHSW_DEV_BOOT); |
| 174 } else if (!strcasecmp(name,"recoverysw_cur")) { | 250 } else if (!strcasecmp(name,"recoverysw_cur")) { |
| 175 value = ReadGpio(GPIO_SIGNAL_TYPE_RECOVERY); | 251 value = ReadGpio(GPIO_SIGNAL_TYPE_RECOVERY); |
| 176 } else if (!strcasecmp(name,"recoverysw_boot")) { | 252 } else if (!strcasecmp(name,"recoverysw_boot")) { |
| 177 value = ReadFileBit(ACPI_CHSW_PATH, CHSW_RECOVERY_BOOT); | 253 value = ReadFileBit(ACPI_CHSW_PATH, CHSW_RECOVERY_BOOT); |
| 178 } else if (!strcasecmp(name,"recoverysw_ec_boot")) { | 254 } else if (!strcasecmp(name,"recoverysw_ec_boot")) { |
| 179 value = ReadFileBit(ACPI_CHSW_PATH, CHSW_RECOVERY_EC_BOOT); | 255 value = ReadFileBit(ACPI_CHSW_PATH, CHSW_RECOVERY_EC_BOOT); |
| 180 } else if (!strcasecmp(name,"wpsw_cur")) { | 256 } else if (!strcasecmp(name,"wpsw_cur")) { |
| 181 value = ReadGpio(GPIO_SIGNAL_TYPE_WP); | 257 value = ReadGpio(GPIO_SIGNAL_TYPE_WP); |
| 182 if (-1 != value && FwidStartsWith("Mario.")) | 258 if (-1 != value && FwidStartsWith("Mario.")) |
| 183 value = 1 - value; /* Mario reports this backwards */ | 259 value = 1 - value; /* Mario reports this backwards */ |
| 184 } else if (!strcasecmp(name,"wpsw_boot")) { | 260 } else if (!strcasecmp(name,"wpsw_boot")) { |
| 185 value = ReadFileBit(ACPI_CHSW_PATH, CHSW_WP_BOOT); | 261 value = ReadFileBit(ACPI_CHSW_PATH, CHSW_WP_BOOT); |
| 186 if (-1 != value && FwidStartsWith("Mario.")) | 262 if (-1 != value && FwidStartsWith("Mario.")) |
| 187 value = 1 - value; /* Mario reports this backwards */ | 263 value = 1 - value; /* Mario reports this backwards */ |
| 188 } | 264 } |
| 265 /* NV storage values for older H2C BIOS */ |
| 266 else if (!strcasecmp(name,"recovery_request")) { |
| 267 value = VbGetCmosRebootField(CMOSRF_RECOVERY); |
| 268 } else if (!strcasecmp(name,"dbg_reset")) { |
| 269 value = VbGetCmosRebootField(CMOSRF_DEBUG_RESET); |
| 270 } else if (!strcasecmp(name,"fwb_tries")) { |
| 271 value = VbGetCmosRebootField(CMOSRF_TRY_B); |
| 272 } |
| 189 | 273 |
| 190 /* TODO: remaining properties from spec */ | 274 /* TODO: remaining properties from spec */ |
| 191 | 275 |
| 192 return value; | 276 return value; |
| 193 } | 277 } |
| 194 | 278 |
| 195 | 279 |
| 196 /* Read a system property string into a destination buffer of the specified | 280 /* Read a system property string into a destination buffer of the specified |
| 197 * size. | 281 * size. |
| 198 * | 282 * |
| (...skipping 11 matching lines...) Expand all Loading... |
| 210 | 294 |
| 211 /* TODO: remaining properties from spec */ | 295 /* TODO: remaining properties from spec */ |
| 212 } | 296 } |
| 213 | 297 |
| 214 | 298 |
| 215 /* Set a system property integer. | 299 /* Set a system property integer. |
| 216 * | 300 * |
| 217 * Returns 0 if success, -1 if error. */ | 301 * Returns 0 if success, -1 if error. */ |
| 218 int VbSetSystemPropertyInt(const char* name, int value) { | 302 int VbSetSystemPropertyInt(const char* name, int value) { |
| 219 | 303 |
| 220 /* TODO: support setting */ | 304 /* NV storage values for older H2C BIOS */ |
| 305 if (!strcasecmp(name,"recovery_request")) { |
| 306 return VbSetCmosRebootField(CMOSRF_RECOVERY, value); |
| 307 } else if (!strcasecmp(name,"dbg_reset")) { |
| 308 return VbSetCmosRebootField(CMOSRF_DEBUG_RESET, value); |
| 309 } else if (!strcasecmp(name,"fwb_tries")) { |
| 310 return VbSetCmosRebootField(CMOSRF_TRY_B, value); |
| 311 } |
| 312 |
| 221 return -1; | 313 return -1; |
| 222 } | 314 } |
| 223 | 315 |
| 224 | 316 |
| 225 /* Set a system property string. | 317 /* Set a system property string. |
| 226 * | 318 * |
| 227 * Returns 0 if success, -1 if error. */ | 319 * Returns 0 if success, -1 if error. */ |
| 228 int VbSetSystemPropertyString(const char* name, const char* value) { | 320 int VbSetSystemPropertyString(const char* name, const char* value) { |
| 229 | 321 |
| 230 /* TODO: support setting */ | 322 /* TODO: support setting */ |
| 231 return -1; | 323 return -1; |
| 232 } | 324 } |
| OLD | NEW |