blob: 2ef222b0f7579e7f7a607b164547cb4c0e1a64fd [file] [log] [blame]
Randall Spanglerc0e37422012-06-08 12:30:17 -07001/* Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Randall Spangler54218662011-02-07 11:20:20 -08002 * 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>
Vadim Bendebury20084232011-03-15 09:29:48 -07008#include <sys/types.h>
9#include <sys/stat.h>
10#include <unistd.h>
11#include <ctype.h>
Randall Spangler54218662011-02-07 11:20:20 -080012
13#include "host_common.h"
14
15#include "crossystem.h"
Randall Spanglereb591952011-04-07 10:02:00 -070016#include "crossystem_arch.h"
Randall Spangler54218662011-02-07 11:20:20 -080017#include "utility.h"
18#include "vboot_common.h"
Randall Spanglere73302c2011-02-18 14:53:01 -080019#include "vboot_nvstorage.h"
Randall Spanglerf4ba19d2011-03-17 16:10:21 -070020#include "vboot_struct.h"
Randall Spangler54218662011-02-07 11:20:20 -080021
Randall Spangler196e1772011-03-10 11:31:06 -080022/* Filename for kernel command line */
23#define KERNEL_CMDLINE_PATH "/proc/cmdline"
24
Randall Spanglerf4ba19d2011-03-17 16:10:21 -070025/* Fields that GetVdatString() can get */
26typedef enum VdatStringField {
Randall Spangler71415712011-03-21 11:04:50 -070027 VDAT_STRING_TIMERS = 0, /* Timer values */
28 VDAT_STRING_LOAD_FIRMWARE_DEBUG, /* LoadFirmware() debug information */
Randall Spanglera185b8d2011-07-15 16:28:38 -070029 VDAT_STRING_LOAD_KERNEL_DEBUG, /* LoadKernel() debug information */
30 VDAT_STRING_MAINFW_ACT /* Active main firmware */
Randall Spanglerf4ba19d2011-03-17 16:10:21 -070031} VdatStringField;
32
33
34/* Fields that GetVdatInt() can get */
35typedef enum VdatIntField {
Randall Spanglercabe6b32011-03-18 12:44:27 -070036 VDAT_INT_FLAGS = 0, /* Flags */
Randall Spanglerda8d32d2012-08-03 12:48:24 -070037 VDAT_INT_HEADER_VERSION, /* Header version for VbSharedData */
38 VDAT_INT_DEVSW_BOOT, /* Dev switch position at boot */
39 VDAT_INT_DEVSW_VIRTUAL, /* Dev switch is virtual */
40 VDAT_INT_RECSW_BOOT, /* Recovery switch position at boot */
Bill Richardson9dc62172012-08-28 15:00:51 -070041 VDAT_INT_HW_WPSW_BOOT, /* Hardware WP switch position at boot */
42 VDAT_INT_SW_WPSW_BOOT, /* Flash chip's WP setting at boot */
Randall Spanglerda8d32d2012-08-03 12:48:24 -070043
Randall Spanglercabe6b32011-03-18 12:44:27 -070044 VDAT_INT_FW_VERSION_TPM, /* Current firmware version in TPM */
45 VDAT_INT_KERNEL_VERSION_TPM, /* Current kernel version in TPM */
46 VDAT_INT_TRIED_FIRMWARE_B, /* Tried firmware B due to fwb_tries */
Randall Spangler7adcc602011-06-24 16:11:45 -070047 VDAT_INT_KERNEL_KEY_VERIFIED, /* Kernel key verified using
Randall Spanglercabe6b32011-03-18 12:44:27 -070048 * signature, not just hash */
Randall Spangler7adcc602011-06-24 16:11:45 -070049 VDAT_INT_RECOVERY_REASON /* Recovery reason for current boot */
Randall Spanglerf4ba19d2011-03-17 16:10:21 -070050} VdatIntField;
51
52
Randall Spanglerff3f0002011-07-26 10:43:53 -070053/* Masks for kern_nv usage by kernel. */
Randall Spanglerd7728232011-04-08 14:04:21 -070054#define KERN_NV_FWUPDATE_TRIES_MASK 0x0000000F
Randall Spanglerff3f0002011-07-26 10:43:53 -070055/* If you want to use the remaining currently-unused bits in kern_nv
56 * for something kernel-y, define a new field (the way we did for
57 * fwupdate_tries). Don't just modify kern_nv directly, because that
58 * makes it too easy to accidentally corrupt other sub-fields. */
59#define KERN_NV_CURRENTLY_UNUSED 0xFFFFFFF0
Randall Spanglerd7728232011-04-08 14:04:21 -070060
Randall Spanglerc80fe652011-02-17 11:06:47 -080061/* Return true if the FWID starts with the specified string. */
Randall Spanglereb591952011-04-07 10:02:00 -070062int FwidStartsWith(const char *start) {
Randall Spanglerc80fe652011-02-17 11:06:47 -080063 char fwid[128];
64 if (!VbGetSystemPropertyString("fwid", fwid, sizeof(fwid)))
65 return 0;
66
67 return 0 == strncmp(fwid, start, strlen(start));
68}
69
70
Randall Spangler0f8ffb12011-02-25 09:50:54 -080071int VbGetNvStorage(VbNvParam param) {
Randall Spangler0f8ffb12011-02-25 09:50:54 -080072 VbNvContext vnc;
Randall Spangler0f8ffb12011-02-25 09:50:54 -080073 uint32_t value;
74 int retval;
75
Randall Spangler0f8ffb12011-02-25 09:50:54 -080076 /* TODO: locking around NV access */
Randall Spanglereb591952011-04-07 10:02:00 -070077
78 if (0 != VbReadNvStorage(&vnc))
Randall Spangler0f8ffb12011-02-25 09:50:54 -080079 return -1;
Randall Spangler0f8ffb12011-02-25 09:50:54 -080080 if (0 != VbNvSetup(&vnc))
81 return -1;
82 retval = VbNvGet(&vnc, param, &value);
83 if (0 != VbNvTeardown(&vnc))
84 return -1;
85 if (0 != retval)
86 return -1;
87
88 /* TODO: If vnc.raw_changed, attempt to reopen NVRAM for write and
89 * save the new defaults. If we're able to, log. */
90 /* TODO: release lock */
91
92 return (int)value;
93}
94
95
Randall Spangler0f8ffb12011-02-25 09:50:54 -080096int VbSetNvStorage(VbNvParam param, int value) {
Randall Spangler0f8ffb12011-02-25 09:50:54 -080097 VbNvContext vnc;
Randall Spangler0f8ffb12011-02-25 09:50:54 -080098 int retval = -1;
99 int i;
100
Randall Spanglereb591952011-04-07 10:02:00 -0700101 if (0 != VbReadNvStorage(&vnc))
Randall Spangler0f8ffb12011-02-25 09:50:54 -0800102 return -1;
Randall Spangler0f8ffb12011-02-25 09:50:54 -0800103
104 if (0 != VbNvSetup(&vnc))
105 goto VbSetNvCleanup;
106 i = VbNvSet(&vnc, param, (uint32_t)value);
107 if (0 != VbNvTeardown(&vnc))
108 goto VbSetNvCleanup;
109 if (0 != i)
110 goto VbSetNvCleanup;
111
112 if (vnc.raw_changed) {
Randall Spanglerd7728232011-04-08 14:04:21 -0700113 if (0 != VbWriteNvStorage(&vnc))
Randall Spangler0f8ffb12011-02-25 09:50:54 -0800114 goto VbSetNvCleanup;
115 }
116
117 /* Success */
118 retval = 0;
119
120VbSetNvCleanup:
Randall Spangler0f8ffb12011-02-25 09:50:54 -0800121 /* TODO: release lock */
122 return retval;
123}
124
125
Randall Spangler196e1772011-03-10 11:31:06 -0800126/* Determine whether OS-level debugging should be allowed. Passed the
127 * destination and its size. Returns 1 if yes, 0 if no, -1 if error. */
128int VbGetCrosDebug(void) {
129 FILE* f = NULL;
130 char buf[4096] = "";
Randall Spangler196e1772011-03-10 11:31:06 -0800131 char *t, *saveptr;
Hung-Te Lin64ee1532013-03-21 13:02:46 +0800132 const char *delimiters = " \r\n";
Randall Spangler196e1772011-03-10 11:31:06 -0800133
J. Richard Barnettedbffddc2011-08-25 11:22:43 -0700134 /* If the currently running system specifies its debug status, use
135 * that in preference to other indicators. */
136 f = fopen(KERNEL_CMDLINE_PATH, "r");
137 if (NULL != f) {
Randall Spangler196e1772011-03-10 11:31:06 -0800138 if (NULL == fgets(buf, sizeof(buf), f))
J. Richard Barnettedbffddc2011-08-25 11:22:43 -0700139 buf[0] = 0;
Randall Spangler196e1772011-03-10 11:31:06 -0800140 fclose(f);
141 }
Hung-Te Lin64ee1532013-03-21 13:02:46 +0800142 for (t = strtok_r(buf, delimiters, &saveptr); t;
143 t = strtok_r(NULL, delimiters, &saveptr)) {
Randall Spangler196e1772011-03-10 11:31:06 -0800144 if (0 == strcmp(t, "cros_debug"))
145 return 1;
Randall Spangler227f7922011-03-11 13:34:56 -0800146 else if (0 == strcmp(t, "cros_nodebug"))
147 return 0;
Randall Spangler196e1772011-03-10 11:31:06 -0800148 }
149
J. Richard Barnettedbffddc2011-08-25 11:22:43 -0700150 /* Command line is silent; allow debug if the dev switch is on. */
Randall Spanglereb591952011-04-07 10:02:00 -0700151 if (1 == VbGetSystemPropertyInt("devsw_boot"))
Randall Spangler196e1772011-03-10 11:31:06 -0800152 return 1;
153
154 /* All other cases disallow debug. */
155 return 0;
156}
157
Randall Spanglerb47ed5a2011-02-23 13:05:40 -0800158
Randall Spangler71415712011-03-21 11:04:50 -0700159char* GetVdatLoadFirmwareDebug(char* dest, int size,
160 const VbSharedDataHeader* sh) {
161 snprintf(dest, size,
162 "Check A result=%d\n"
163 "Check B result=%d\n"
164 "Firmware index booted=0x%02x\n"
165 "TPM combined version at start=0x%08x\n"
166 "Lowest combined version from firmware=0x%08x\n",
167 sh->check_fw_a_result,
168 sh->check_fw_b_result,
169 sh->firmware_index,
170 sh->fw_version_tpm_start,
171 sh->fw_version_lowest);
172 return dest;
173}
174
175
176#define TRUNCATED "\n(truncated)\n"
177
178char* GetVdatLoadKernelDebug(char* dest, int size,
179 const VbSharedDataHeader* sh) {
180 int used = 0;
181 int first_call_tracked = 0;
182 int call;
183
184 /* Make sure we have space for truncation warning */
185 if (size < strlen(TRUNCATED) + 1)
186 return NULL;
187 size -= strlen(TRUNCATED) + 1;
188
189 used += snprintf(
190 dest + used, size - used,
191 "Calls to LoadKernel()=%d\n",
192 sh->lk_call_count);
193 if (used > size)
194 goto LoadKernelDebugExit;
195
196 /* Report on the last calls */
197 if (sh->lk_call_count > VBSD_MAX_KERNEL_CALLS)
198 first_call_tracked = sh->lk_call_count - VBSD_MAX_KERNEL_CALLS;
199 for (call = first_call_tracked; call < sh->lk_call_count; call++) {
200 const VbSharedDataKernelCall* shc =
201 sh->lk_calls + (call & (VBSD_MAX_KERNEL_CALLS - 1));
202 int first_part_tracked = 0;
203 int part;
204
205 used += snprintf(
206 dest + used, size - used,
207 "Call %d:\n"
208 " Boot flags=0x%02x\n"
209 " Boot mode=%d\n"
210 " Test error=%d\n"
211 " Return code=%d\n"
212 " Debug flags=0x%02x\n"
213 " Drive sectors=%" PRIu64 "\n"
214 " Sector size=%d\n"
215 " Check result=%d\n"
216 " Kernel partitions found=%d\n",
217 call + 1,
218 shc->boot_flags,
219 shc->boot_mode,
220 shc->test_error_num,
221 shc->return_code,
222 shc->flags,
223 shc->sector_count,
224 shc->sector_size,
225 shc->check_result,
226 shc->kernel_parts_found);
227 if (used > size)
228 goto LoadKernelDebugExit;
229
230 /* If we found too many partitions, only prints ones where the
231 * structure has info. */
232 if (shc->kernel_parts_found > VBSD_MAX_KERNEL_PARTS)
233 first_part_tracked = shc->kernel_parts_found - VBSD_MAX_KERNEL_PARTS;
234
235 /* Report on the partitions checked */
236 for (part = first_part_tracked; part < shc->kernel_parts_found; part++) {
237 const VbSharedDataKernelPart* shp =
238 shc->parts + (part & (VBSD_MAX_KERNEL_PARTS - 1));
239
240 used += snprintf(
241 dest + used, size - used,
242 " Kernel %d:\n"
243 " GPT index=%d\n"
244 " Start sector=%" PRIu64 "\n"
245 " Sector count=%" PRIu64 "\n"
246 " Combined version=0x%08x\n"
247 " Check result=%d\n"
248 " Debug flags=0x%02x\n",
249 part + 1,
250 shp->gpt_index,
251 shp->sector_start,
252 shp->sector_count,
253 shp->combined_version,
254 shp->check_result,
255 shp->flags);
256 if (used > size)
257 goto LoadKernelDebugExit;
258 }
259 }
260
261LoadKernelDebugExit:
262
263 /* Warn if data was truncated; we left space for this above. */
264 if (used > size)
265 strcat(dest, TRUNCATED);
266
267 return dest;
268}
269
270
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700271char* GetVdatString(char* dest, int size, VdatStringField field)
272{
Randall Spanglereb591952011-04-07 10:02:00 -0700273 VbSharedDataHeader* sh = VbSharedDataRead();
Randall Spangler71415712011-03-21 11:04:50 -0700274 char* value = dest;
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700275
Randall Spanglereb591952011-04-07 10:02:00 -0700276 if (!sh)
277 return NULL;
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700278
279 switch (field) {
280 case VDAT_STRING_TIMERS:
281 snprintf(dest, size,
282 "LFS=%" PRIu64 ",%" PRIu64
283 " LF=%" PRIu64 ",%" PRIu64
284 " LK=%" PRIu64 ",%" PRIu64,
Randall Spangler96191122011-07-08 14:01:54 -0700285 sh->timer_vb_init_enter,
286 sh->timer_vb_init_exit,
287 sh->timer_vb_select_firmware_enter,
288 sh->timer_vb_select_firmware_exit,
289 sh->timer_vb_select_and_load_kernel_enter,
290 sh->timer_vb_select_and_load_kernel_exit);
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700291 break;
292
293 case VDAT_STRING_LOAD_FIRMWARE_DEBUG:
Randall Spangler71415712011-03-21 11:04:50 -0700294 value = GetVdatLoadFirmwareDebug(dest, size, sh);
295 break;
296
297 case VDAT_STRING_LOAD_KERNEL_DEBUG:
298 value = GetVdatLoadKernelDebug(dest, size, sh);
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700299 break;
300
Randall Spanglera185b8d2011-07-15 16:28:38 -0700301 case VDAT_STRING_MAINFW_ACT:
302 switch(sh->firmware_index) {
303 case 0:
304 StrCopy(dest, "A", size);
305 break;
306 case 1:
307 StrCopy(dest, "B", size);
308 break;
309 case 0xFF:
310 StrCopy(dest, "recovery", size);
311 break;
312 default:
313 value = NULL;
314 }
315 break;
316
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700317 default:
Randall Spanglereb591952011-04-07 10:02:00 -0700318 value = NULL;
319 break;
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700320 }
321
Randall Spangler32a65262011-06-27 10:49:11 -0700322 free(sh);
Randall Spangler71415712011-03-21 11:04:50 -0700323 return value;
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700324}
325
326
327int GetVdatInt(VdatIntField field) {
Randall Spanglereb591952011-04-07 10:02:00 -0700328 VbSharedDataHeader* sh = VbSharedDataRead();
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700329 int value = -1;
330
Randall Spanglereb591952011-04-07 10:02:00 -0700331 if (!sh)
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700332 return -1;
333
Randall Spanglerda8d32d2012-08-03 12:48:24 -0700334 /* Fields supported in version 1 */
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700335 switch (field) {
336 case VDAT_INT_FLAGS:
337 value = (int)sh->flags;
338 break;
Randall Spanglerda8d32d2012-08-03 12:48:24 -0700339 case VDAT_INT_HEADER_VERSION:
340 value = sh->struct_version;
Randall Spangler5ac39bf2011-03-17 17:58:56 -0700341 break;
Randall Spanglercabe6b32011-03-18 12:44:27 -0700342 case VDAT_INT_TRIED_FIRMWARE_B:
343 value = (sh->flags & VBSD_FWB_TRIED ? 1 : 0);
344 break;
345 case VDAT_INT_KERNEL_KEY_VERIFIED:
346 value = (sh->flags & VBSD_KERNEL_KEY_VERIFIED ? 1 : 0);
347 break;
Randall Spangler89286bc2012-08-20 13:39:35 -0700348 case VDAT_INT_FW_VERSION_TPM:
349 value = (int)sh->fw_version_tpm;
350 break;
351 case VDAT_INT_KERNEL_VERSION_TPM:
352 value = (int)sh->kernel_version_tpm;
353 break;
Randall Spanglerda8d32d2012-08-03 12:48:24 -0700354 default:
Randall Spangler7adcc602011-06-24 16:11:45 -0700355 break;
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700356 }
357
Randall Spanglerda8d32d2012-08-03 12:48:24 -0700358 /* Fields added in struct version 2 */
359 if (sh->struct_version >= 2) {
360 switch(field) {
361 case VDAT_INT_DEVSW_BOOT:
362 value = (sh->flags & VBSD_BOOT_DEV_SWITCH_ON ? 1 : 0);
363 break;
364 case VDAT_INT_DEVSW_VIRTUAL:
365 value = (sh->flags & VBSD_HONOR_VIRT_DEV_SWITCH ? 1 : 0);
366 break;
367 case VDAT_INT_RECSW_BOOT:
368 value = (sh->flags & VBSD_BOOT_REC_SWITCH_ON ? 1 : 0);
369 break;
Bill Richardson9dc62172012-08-28 15:00:51 -0700370 case VDAT_INT_HW_WPSW_BOOT:
Randall Spanglerda8d32d2012-08-03 12:48:24 -0700371 value = (sh->flags & VBSD_BOOT_FIRMWARE_WP_ENABLED ? 1 : 0);
372 break;
Bill Richardson9dc62172012-08-28 15:00:51 -0700373 case VDAT_INT_SW_WPSW_BOOT:
374 value = (sh->flags & VBSD_BOOT_FIRMWARE_SW_WP_ENABLED ? 1 : 0);
375 break;
Randall Spanglerda8d32d2012-08-03 12:48:24 -0700376 case VDAT_INT_RECOVERY_REASON:
377 value = sh->recovery_reason;
378 break;
379 default:
380 break;
381 }
382 }
383
Randall Spangler32a65262011-06-27 10:49:11 -0700384 free(sh);
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700385 return value;
386}
387
Randall Spanglerda8d32d2012-08-03 12:48:24 -0700388/* Return version of VbSharedData struct or -1 if not found. */
389int VbSharedDataVersion(void) {
390 return GetVdatInt(VDAT_INT_HEADER_VERSION);
391}
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700392
Randall Spangler54218662011-02-07 11:20:20 -0800393int VbGetSystemPropertyInt(const char* name) {
Randall Spanglerc80fe652011-02-17 11:06:47 -0800394 int value = -1;
Randall Spangler54218662011-02-07 11:20:20 -0800395
Randall Spanglereb591952011-04-07 10:02:00 -0700396 /* Check architecture-dependent properties first */
397 value = VbGetArchPropertyInt(name);
398 if (-1 != value)
399 return value;
400
401 /* NV storage values */
Randall Spanglercabe6b32011-03-18 12:44:27 -0700402 else if (!strcasecmp(name,"kern_nv")) {
Randall Spangler618d17d2011-03-01 10:33:11 -0800403 value = VbGetNvStorage(VBNV_KERNEL_FIELD);
Randall Spanglerb4167142011-03-01 13:04:22 -0800404 } else if (!strcasecmp(name,"nvram_cleared")) {
405 value = VbGetNvStorage(VBNV_KERNEL_SETTINGS_RESET);
Randall Spanglereb591952011-04-07 10:02:00 -0700406 } else if (!strcasecmp(name,"recovery_request")) {
Randall Spangler0f8ffb12011-02-25 09:50:54 -0800407 value = VbGetNvStorage(VBNV_RECOVERY_REQUEST);
Randall Spanglere73302c2011-02-18 14:53:01 -0800408 } else if (!strcasecmp(name,"dbg_reset")) {
Randall Spangler0f8ffb12011-02-25 09:50:54 -0800409 value = VbGetNvStorage(VBNV_DEBUG_RESET_MODE);
Bill Richardson35d07332012-05-25 12:55:58 -0700410 } else if (!strcasecmp(name,"disable_dev_request")) {
411 value = VbGetNvStorage(VBNV_DISABLE_DEV_REQUEST);
Randall Spangler29e88072012-06-19 10:03:53 -0700412 } else if (!strcasecmp(name,"clear_tpm_owner_request")) {
413 value = VbGetNvStorage(VBNV_CLEAR_TPM_OWNER_REQUEST);
414 } else if (!strcasecmp(name,"clear_tpm_owner_done")) {
415 value = VbGetNvStorage(VBNV_CLEAR_TPM_OWNER_DONE);
Randall Spanglere73302c2011-02-18 14:53:01 -0800416 } else if (!strcasecmp(name,"fwb_tries")) {
Randall Spangler0f8ffb12011-02-25 09:50:54 -0800417 value = VbGetNvStorage(VBNV_TRY_B_COUNT);
Randall Spanglerd7728232011-04-08 14:04:21 -0700418 } else if (!strcasecmp(name,"fwupdate_tries")) {
419 value = VbGetNvStorage(VBNV_KERNEL_FIELD);
420 if (value != -1)
421 value &= KERN_NV_FWUPDATE_TRIES_MASK;
Randall Spangler44a12762011-04-12 13:16:40 -0700422 } else if (!strcasecmp(name,"loc_idx")) {
423 value = VbGetNvStorage(VBNV_LOCALIZATION_INDEX);
Randall Spanglerdaa807c2011-07-11 10:55:18 -0700424 } else if (!strcasecmp(name,"dev_boot_usb")) {
425 value = VbGetNvStorage(VBNV_DEV_BOOT_USB);
Stefan Reinauera2326ee2012-08-23 15:06:45 -0700426 } else if (!strcasecmp(name,"dev_boot_legacy")) {
427 value = VbGetNvStorage(VBNV_DEV_BOOT_LEGACY);
Bill Richardson7272a692011-11-17 10:48:59 -0800428 } else if (!strcasecmp(name,"dev_boot_signed_only")) {
429 value = VbGetNvStorage(VBNV_DEV_BOOT_SIGNED_ONLY);
Bill Richardson17b82242012-06-26 16:33:56 -0700430 } else if (!strcasecmp(name,"oprom_needed")) {
431 value = VbGetNvStorage(VBNV_OPROM_NEEDED);
Bill Richardson699ebf32012-12-17 14:35:22 -0800432 } else if (!strcasecmp(name,"recovery_subcode")) {
433 value = VbGetNvStorage(VBNV_RECOVERY_SUBCODE);
Randall Spanglere73302c2011-02-18 14:53:01 -0800434 }
Randall Spanglerb47ed5a2011-02-23 13:05:40 -0800435 /* Other parameters */
Randall Spanglereb591952011-04-07 10:02:00 -0700436 else if (!strcasecmp(name,"cros_debug")) {
Randall Spangler196e1772011-03-10 11:31:06 -0800437 value = VbGetCrosDebug();
Randall Spanglerda8d32d2012-08-03 12:48:24 -0700438 } else if (!strcasecmp(name,"devsw_boot")) {
439 value = GetVdatInt(VDAT_INT_DEVSW_BOOT);
440 } else if (!strcasecmp(name,"devsw_virtual")) {
441 value = GetVdatInt(VDAT_INT_DEVSW_VIRTUAL);
442 } else if (!strcasecmp(name, "recoverysw_boot")) {
443 value = GetVdatInt(VDAT_INT_RECSW_BOOT);
444 } else if (!strcasecmp(name, "wpsw_boot")) {
Bill Richardson9dc62172012-08-28 15:00:51 -0700445 value = GetVdatInt(VDAT_INT_HW_WPSW_BOOT);
446 } else if (!strcasecmp(name, "sw_wpsw_boot")) {
447 value = GetVdatInt(VDAT_INT_SW_WPSW_BOOT);
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700448 } else if (!strcasecmp(name,"vdat_flags")) {
449 value = GetVdatInt(VDAT_INT_FLAGS);
Randall Spangler5ac39bf2011-03-17 17:58:56 -0700450 } else if (!strcasecmp(name,"tpm_fwver")) {
451 value = GetVdatInt(VDAT_INT_FW_VERSION_TPM);
452 } else if (!strcasecmp(name,"tpm_kernver")) {
453 value = GetVdatInt(VDAT_INT_KERNEL_VERSION_TPM);
Randall Spanglercabe6b32011-03-18 12:44:27 -0700454 } else if (!strcasecmp(name,"tried_fwb")) {
455 value = GetVdatInt(VDAT_INT_TRIED_FIRMWARE_B);
Randall Spangler7adcc602011-06-24 16:11:45 -0700456 } else if (!strcasecmp(name,"recovery_reason")) {
457 value = GetVdatInt(VDAT_INT_RECOVERY_REASON);
Randall Spanglerb47ed5a2011-02-23 13:05:40 -0800458 }
Randall Spangler54218662011-02-07 11:20:20 -0800459
Randall Spanglerc80fe652011-02-17 11:06:47 -0800460 return value;
Randall Spangler54218662011-02-07 11:20:20 -0800461}
462
Randall Spangler54218662011-02-07 11:20:20 -0800463
Randall Spanglereb591952011-04-07 10:02:00 -0700464const char* VbGetSystemPropertyString(const char* name, char* dest, int size) {
Tom Wai-Hong Tamd808a432012-06-29 17:24:27 +0800465 static const char unknown_string[] = "unknown";
466
Randall Spanglereb591952011-04-07 10:02:00 -0700467 /* Check architecture-dependent properties first */
468 if (VbGetArchPropertyString(name, dest, size))
469 return dest;
470
471 if (!strcasecmp(name,"kernkey_vfy")) {
Randall Spanglercabe6b32011-03-18 12:44:27 -0700472 switch(GetVdatInt(VDAT_INT_KERNEL_KEY_VERIFIED)) {
Randall Spangler17260282011-02-25 12:06:26 -0800473 case 0:
474 return "hash";
475 case 1:
476 return "sig";
477 default:
478 return NULL;
479 }
Randall Spanglera185b8d2011-07-15 16:28:38 -0700480 } else if (!strcasecmp(name, "mainfw_act")) {
481 return GetVdatString(dest, size, VDAT_STRING_MAINFW_ACT);
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700482 } else if (!strcasecmp(name, "vdat_timers")) {
483 return GetVdatString(dest, size, VDAT_STRING_TIMERS);
484 } else if (!strcasecmp(name, "vdat_lfdebug")) {
485 return GetVdatString(dest, size, VDAT_STRING_LOAD_FIRMWARE_DEBUG);
Randall Spangler71415712011-03-21 11:04:50 -0700486 } else if (!strcasecmp(name, "vdat_lkdebug")) {
487 return GetVdatString(dest, size, VDAT_STRING_LOAD_KERNEL_DEBUG);
Tom Wai-Hong Tamd808a432012-06-29 17:24:27 +0800488 } else if (!strcasecmp(name, "ddr_type")) {
489 return unknown_string;
Randall Spanglereb591952011-04-07 10:02:00 -0700490 }
491
492 return NULL;
Randall Spangler54218662011-02-07 11:20:20 -0800493}
494
495
Randall Spangler54218662011-02-07 11:20:20 -0800496int VbSetSystemPropertyInt(const char* name, int value) {
Randall Spanglereb591952011-04-07 10:02:00 -0700497 /* Check architecture-dependent properties first */
Randall Spanglerd7728232011-04-08 14:04:21 -0700498
Randall Spanglereb591952011-04-07 10:02:00 -0700499 if (0 == VbSetArchPropertyInt(name, value))
500 return 0;
Randall Spangler54218662011-02-07 11:20:20 -0800501
Randall Spanglereb591952011-04-07 10:02:00 -0700502 /* NV storage values */
Randall Spanglerb4167142011-03-01 13:04:22 -0800503 if (!strcasecmp(name,"nvram_cleared")) {
504 /* Can only clear this flag; it's set inside the NV storage library. */
505 return VbSetNvStorage(VBNV_KERNEL_SETTINGS_RESET, 0);
Randall Spanglereb591952011-04-07 10:02:00 -0700506 } else if (!strcasecmp(name,"recovery_request")) {
507 return VbSetNvStorage(VBNV_RECOVERY_REQUEST, value);
Bill Richardson699ebf32012-12-17 14:35:22 -0800508 } else if (!strcasecmp(name,"recovery_subcode")) {
509 return VbSetNvStorage(VBNV_RECOVERY_SUBCODE, value);
Randall Spanglere73302c2011-02-18 14:53:01 -0800510 } else if (!strcasecmp(name,"dbg_reset")) {
Randall Spanglereb591952011-04-07 10:02:00 -0700511 return VbSetNvStorage(VBNV_DEBUG_RESET_MODE, value);
Bill Richardson35d07332012-05-25 12:55:58 -0700512 } else if (!strcasecmp(name,"disable_dev_request")) {
513 return VbSetNvStorage(VBNV_DISABLE_DEV_REQUEST, value);
Randall Spangler29e88072012-06-19 10:03:53 -0700514 } else if (!strcasecmp(name,"clear_tpm_owner_request")) {
515 return VbSetNvStorage(VBNV_CLEAR_TPM_OWNER_REQUEST, value);
516 } else if (!strcasecmp(name,"clear_tpm_owner_done")) {
517 /* Can only clear this flag; it's set by firmware. */
518 return VbSetNvStorage(VBNV_CLEAR_TPM_OWNER_DONE, 0);
Randall Spanglere73302c2011-02-18 14:53:01 -0800519 } else if (!strcasecmp(name,"fwb_tries")) {
Randall Spanglereb591952011-04-07 10:02:00 -0700520 return VbSetNvStorage(VBNV_TRY_B_COUNT, value);
Randall Spanglerd7728232011-04-08 14:04:21 -0700521 } else if (!strcasecmp(name,"fwupdate_tries")) {
522 int kern_nv = VbGetNvStorage(VBNV_KERNEL_FIELD);
523 if (kern_nv == -1)
524 return -1;
525 kern_nv &= ~KERN_NV_FWUPDATE_TRIES_MASK;
526 kern_nv |= (value & KERN_NV_FWUPDATE_TRIES_MASK);
527 return VbSetNvStorage(VBNV_KERNEL_FIELD, kern_nv);
Randall Spangler44a12762011-04-12 13:16:40 -0700528 } else if (!strcasecmp(name,"loc_idx")) {
529 return VbSetNvStorage(VBNV_LOCALIZATION_INDEX, value);
Randall Spanglerdaa807c2011-07-11 10:55:18 -0700530 } else if (!strcasecmp(name,"dev_boot_usb")) {
531 return VbSetNvStorage(VBNV_DEV_BOOT_USB, value);
Stefan Reinauera2326ee2012-08-23 15:06:45 -0700532 } else if (!strcasecmp(name,"dev_boot_legacy")) {
533 return VbSetNvStorage(VBNV_DEV_BOOT_LEGACY, value);
Bill Richardson7272a692011-11-17 10:48:59 -0800534 } else if (!strcasecmp(name,"dev_boot_signed_only")) {
535 return VbSetNvStorage(VBNV_DEV_BOOT_SIGNED_ONLY, value);
Bill Richardson17b82242012-06-26 16:33:56 -0700536 } else if (!strcasecmp(name,"oprom_needed")) {
537 return VbSetNvStorage(VBNV_OPROM_NEEDED, value);
Randall Spanglere73302c2011-02-18 14:53:01 -0800538 }
539
Randall Spangler54218662011-02-07 11:20:20 -0800540 return -1;
541}
542
543
Randall Spangler54218662011-02-07 11:20:20 -0800544int VbSetSystemPropertyString(const char* name, const char* value) {
Randall Spanglereb591952011-04-07 10:02:00 -0700545 /* Chain to architecture-dependent properties */
546 return VbSetArchPropertyString(name, value);
Randall Spangler54218662011-02-07 11:20:20 -0800547}