blob: b4824332ebfd3d22cacf2bef07f8dba474091578 [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
J. Richard Barnette92cbd5d2013-10-22 16:21:14 -070053/* Description of build options that may be specified on the
54 * kernel command line. */
55typedef enum VbBuildOption {
56 VB_BUILD_OPTION_UNKNOWN,
57 VB_BUILD_OPTION_DEBUG,
58 VB_BUILD_OPTION_NODEBUG
59} VbBuildOption;
60
61
Randall Spanglerff3f0002011-07-26 10:43:53 -070062/* Masks for kern_nv usage by kernel. */
Randall Spanglerd7728232011-04-08 14:04:21 -070063#define KERN_NV_FWUPDATE_TRIES_MASK 0x0000000F
Randall Spanglerff3f0002011-07-26 10:43:53 -070064/* If you want to use the remaining currently-unused bits in kern_nv
65 * for something kernel-y, define a new field (the way we did for
66 * fwupdate_tries). Don't just modify kern_nv directly, because that
67 * makes it too easy to accidentally corrupt other sub-fields. */
68#define KERN_NV_CURRENTLY_UNUSED 0xFFFFFFF0
Randall Spanglerd7728232011-04-08 14:04:21 -070069
Randall Spanglerc80fe652011-02-17 11:06:47 -080070/* Return true if the FWID starts with the specified string. */
Randall Spanglereb591952011-04-07 10:02:00 -070071int FwidStartsWith(const char *start) {
Randall Spanglerc80fe652011-02-17 11:06:47 -080072 char fwid[128];
73 if (!VbGetSystemPropertyString("fwid", fwid, sizeof(fwid)))
74 return 0;
75
76 return 0 == strncmp(fwid, start, strlen(start));
77}
78
79
Randall Spangler0f8ffb12011-02-25 09:50:54 -080080int VbGetNvStorage(VbNvParam param) {
Randall Spangler0f8ffb12011-02-25 09:50:54 -080081 VbNvContext vnc;
Randall Spangler0f8ffb12011-02-25 09:50:54 -080082 uint32_t value;
83 int retval;
84
Randall Spangler0f8ffb12011-02-25 09:50:54 -080085 /* TODO: locking around NV access */
Randall Spanglereb591952011-04-07 10:02:00 -070086
87 if (0 != VbReadNvStorage(&vnc))
Randall Spangler0f8ffb12011-02-25 09:50:54 -080088 return -1;
Randall Spangler0f8ffb12011-02-25 09:50:54 -080089 if (0 != VbNvSetup(&vnc))
90 return -1;
91 retval = VbNvGet(&vnc, param, &value);
92 if (0 != VbNvTeardown(&vnc))
93 return -1;
94 if (0 != retval)
95 return -1;
96
97 /* TODO: If vnc.raw_changed, attempt to reopen NVRAM for write and
98 * save the new defaults. If we're able to, log. */
99 /* TODO: release lock */
100
101 return (int)value;
102}
103
104
Randall Spangler0f8ffb12011-02-25 09:50:54 -0800105int VbSetNvStorage(VbNvParam param, int value) {
Randall Spangler0f8ffb12011-02-25 09:50:54 -0800106 VbNvContext vnc;
Randall Spangler0f8ffb12011-02-25 09:50:54 -0800107 int retval = -1;
108 int i;
109
Randall Spanglereb591952011-04-07 10:02:00 -0700110 if (0 != VbReadNvStorage(&vnc))
Randall Spangler0f8ffb12011-02-25 09:50:54 -0800111 return -1;
Randall Spangler0f8ffb12011-02-25 09:50:54 -0800112
113 if (0 != VbNvSetup(&vnc))
114 goto VbSetNvCleanup;
115 i = VbNvSet(&vnc, param, (uint32_t)value);
116 if (0 != VbNvTeardown(&vnc))
117 goto VbSetNvCleanup;
118 if (0 != i)
119 goto VbSetNvCleanup;
120
121 if (vnc.raw_changed) {
Randall Spanglerd7728232011-04-08 14:04:21 -0700122 if (0 != VbWriteNvStorage(&vnc))
Randall Spangler0f8ffb12011-02-25 09:50:54 -0800123 goto VbSetNvCleanup;
124 }
125
126 /* Success */
127 retval = 0;
128
129VbSetNvCleanup:
Randall Spangler0f8ffb12011-02-25 09:50:54 -0800130 /* TODO: release lock */
131 return retval;
132}
133
J. Richard Barnette92cbd5d2013-10-22 16:21:14 -0700134/* Find what build/debug status is specified on the kernel command
135 * line, if any. */
136static VbBuildOption VbScanBuildOption(void) {
Randall Spangler196e1772011-03-10 11:31:06 -0800137 FILE* f = NULL;
138 char buf[4096] = "";
Randall Spangler196e1772011-03-10 11:31:06 -0800139 char *t, *saveptr;
Hung-Te Lin64ee1532013-03-21 13:02:46 +0800140 const char *delimiters = " \r\n";
Randall Spangler196e1772011-03-10 11:31:06 -0800141
J. Richard Barnettedbffddc2011-08-25 11:22:43 -0700142 f = fopen(KERNEL_CMDLINE_PATH, "r");
143 if (NULL != f) {
Randall Spangler196e1772011-03-10 11:31:06 -0800144 if (NULL == fgets(buf, sizeof(buf), f))
J. Richard Barnettedbffddc2011-08-25 11:22:43 -0700145 buf[0] = 0;
Randall Spangler196e1772011-03-10 11:31:06 -0800146 fclose(f);
147 }
Hung-Te Lin64ee1532013-03-21 13:02:46 +0800148 for (t = strtok_r(buf, delimiters, &saveptr); t;
149 t = strtok_r(NULL, delimiters, &saveptr)) {
Randall Spangler196e1772011-03-10 11:31:06 -0800150 if (0 == strcmp(t, "cros_debug"))
J. Richard Barnette92cbd5d2013-10-22 16:21:14 -0700151 return VB_BUILD_OPTION_DEBUG;
Randall Spangler227f7922011-03-11 13:34:56 -0800152 else if (0 == strcmp(t, "cros_nodebug"))
J. Richard Barnette92cbd5d2013-10-22 16:21:14 -0700153 return VB_BUILD_OPTION_NODEBUG;
154 }
155
156 return VB_BUILD_OPTION_UNKNOWN;
157}
158
159
160/* Determine whether the running OS image was built for debugging.
161 * Returns 1 if yes, 0 if no or indeterminate. */
162int VbGetDebugBuild(void) {
163 return VB_BUILD_OPTION_DEBUG == VbScanBuildOption();
164}
165
166
167/* Determine whether OS-level debugging should be allowed.
168 * Returns 1 if yes, 0 if no or indeterminate. */
169int VbGetCrosDebug(void) {
170 /* If the currently running system specifies its debug status, use
171 * that in preference to other indicators. */
172 VbBuildOption option = VbScanBuildOption();
173 if (VB_BUILD_OPTION_DEBUG == option) {
174 return 1;
175 } else if (VB_BUILD_OPTION_NODEBUG == option) {
Randall Spangler227f7922011-03-11 13:34:56 -0800176 return 0;
Randall Spangler196e1772011-03-10 11:31:06 -0800177 }
178
J. Richard Barnettedbffddc2011-08-25 11:22:43 -0700179 /* Command line is silent; allow debug if the dev switch is on. */
Randall Spanglereb591952011-04-07 10:02:00 -0700180 if (1 == VbGetSystemPropertyInt("devsw_boot"))
Randall Spangler196e1772011-03-10 11:31:06 -0800181 return 1;
182
183 /* All other cases disallow debug. */
184 return 0;
185}
186
Randall Spanglerb47ed5a2011-02-23 13:05:40 -0800187
Randall Spangler71415712011-03-21 11:04:50 -0700188char* GetVdatLoadFirmwareDebug(char* dest, int size,
189 const VbSharedDataHeader* sh) {
190 snprintf(dest, size,
191 "Check A result=%d\n"
192 "Check B result=%d\n"
193 "Firmware index booted=0x%02x\n"
194 "TPM combined version at start=0x%08x\n"
195 "Lowest combined version from firmware=0x%08x\n",
196 sh->check_fw_a_result,
197 sh->check_fw_b_result,
198 sh->firmware_index,
199 sh->fw_version_tpm_start,
200 sh->fw_version_lowest);
201 return dest;
202}
203
204
205#define TRUNCATED "\n(truncated)\n"
206
207char* GetVdatLoadKernelDebug(char* dest, int size,
208 const VbSharedDataHeader* sh) {
209 int used = 0;
210 int first_call_tracked = 0;
211 int call;
212
213 /* Make sure we have space for truncation warning */
214 if (size < strlen(TRUNCATED) + 1)
215 return NULL;
216 size -= strlen(TRUNCATED) + 1;
217
218 used += snprintf(
219 dest + used, size - used,
220 "Calls to LoadKernel()=%d\n",
221 sh->lk_call_count);
222 if (used > size)
223 goto LoadKernelDebugExit;
224
225 /* Report on the last calls */
226 if (sh->lk_call_count > VBSD_MAX_KERNEL_CALLS)
227 first_call_tracked = sh->lk_call_count - VBSD_MAX_KERNEL_CALLS;
228 for (call = first_call_tracked; call < sh->lk_call_count; call++) {
229 const VbSharedDataKernelCall* shc =
230 sh->lk_calls + (call & (VBSD_MAX_KERNEL_CALLS - 1));
231 int first_part_tracked = 0;
232 int part;
233
234 used += snprintf(
235 dest + used, size - used,
236 "Call %d:\n"
237 " Boot flags=0x%02x\n"
238 " Boot mode=%d\n"
239 " Test error=%d\n"
240 " Return code=%d\n"
241 " Debug flags=0x%02x\n"
242 " Drive sectors=%" PRIu64 "\n"
243 " Sector size=%d\n"
244 " Check result=%d\n"
245 " Kernel partitions found=%d\n",
246 call + 1,
247 shc->boot_flags,
248 shc->boot_mode,
249 shc->test_error_num,
250 shc->return_code,
251 shc->flags,
252 shc->sector_count,
253 shc->sector_size,
254 shc->check_result,
255 shc->kernel_parts_found);
256 if (used > size)
257 goto LoadKernelDebugExit;
258
259 /* If we found too many partitions, only prints ones where the
260 * structure has info. */
261 if (shc->kernel_parts_found > VBSD_MAX_KERNEL_PARTS)
262 first_part_tracked = shc->kernel_parts_found - VBSD_MAX_KERNEL_PARTS;
263
264 /* Report on the partitions checked */
265 for (part = first_part_tracked; part < shc->kernel_parts_found; part++) {
266 const VbSharedDataKernelPart* shp =
267 shc->parts + (part & (VBSD_MAX_KERNEL_PARTS - 1));
268
269 used += snprintf(
270 dest + used, size - used,
271 " Kernel %d:\n"
272 " GPT index=%d\n"
273 " Start sector=%" PRIu64 "\n"
274 " Sector count=%" PRIu64 "\n"
275 " Combined version=0x%08x\n"
276 " Check result=%d\n"
277 " Debug flags=0x%02x\n",
278 part + 1,
279 shp->gpt_index,
280 shp->sector_start,
281 shp->sector_count,
282 shp->combined_version,
283 shp->check_result,
284 shp->flags);
285 if (used > size)
286 goto LoadKernelDebugExit;
287 }
288 }
289
290LoadKernelDebugExit:
291
292 /* Warn if data was truncated; we left space for this above. */
293 if (used > size)
294 strcat(dest, TRUNCATED);
295
296 return dest;
297}
298
299
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700300char* GetVdatString(char* dest, int size, VdatStringField field)
301{
Randall Spanglereb591952011-04-07 10:02:00 -0700302 VbSharedDataHeader* sh = VbSharedDataRead();
Randall Spangler71415712011-03-21 11:04:50 -0700303 char* value = dest;
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700304
Randall Spanglereb591952011-04-07 10:02:00 -0700305 if (!sh)
306 return NULL;
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700307
308 switch (field) {
309 case VDAT_STRING_TIMERS:
310 snprintf(dest, size,
311 "LFS=%" PRIu64 ",%" PRIu64
312 " LF=%" PRIu64 ",%" PRIu64
313 " LK=%" PRIu64 ",%" PRIu64,
Randall Spangler96191122011-07-08 14:01:54 -0700314 sh->timer_vb_init_enter,
315 sh->timer_vb_init_exit,
316 sh->timer_vb_select_firmware_enter,
317 sh->timer_vb_select_firmware_exit,
318 sh->timer_vb_select_and_load_kernel_enter,
319 sh->timer_vb_select_and_load_kernel_exit);
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700320 break;
321
322 case VDAT_STRING_LOAD_FIRMWARE_DEBUG:
Randall Spangler71415712011-03-21 11:04:50 -0700323 value = GetVdatLoadFirmwareDebug(dest, size, sh);
324 break;
325
326 case VDAT_STRING_LOAD_KERNEL_DEBUG:
327 value = GetVdatLoadKernelDebug(dest, size, sh);
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700328 break;
329
Randall Spanglera185b8d2011-07-15 16:28:38 -0700330 case VDAT_STRING_MAINFW_ACT:
331 switch(sh->firmware_index) {
332 case 0:
333 StrCopy(dest, "A", size);
334 break;
335 case 1:
336 StrCopy(dest, "B", size);
337 break;
338 case 0xFF:
339 StrCopy(dest, "recovery", size);
340 break;
341 default:
342 value = NULL;
343 }
344 break;
345
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700346 default:
Randall Spanglereb591952011-04-07 10:02:00 -0700347 value = NULL;
348 break;
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700349 }
350
Randall Spangler32a65262011-06-27 10:49:11 -0700351 free(sh);
Randall Spangler71415712011-03-21 11:04:50 -0700352 return value;
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700353}
354
355
356int GetVdatInt(VdatIntField field) {
Randall Spanglereb591952011-04-07 10:02:00 -0700357 VbSharedDataHeader* sh = VbSharedDataRead();
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700358 int value = -1;
359
Randall Spanglereb591952011-04-07 10:02:00 -0700360 if (!sh)
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700361 return -1;
362
Randall Spanglerda8d32d2012-08-03 12:48:24 -0700363 /* Fields supported in version 1 */
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700364 switch (field) {
365 case VDAT_INT_FLAGS:
366 value = (int)sh->flags;
367 break;
Randall Spanglerda8d32d2012-08-03 12:48:24 -0700368 case VDAT_INT_HEADER_VERSION:
369 value = sh->struct_version;
Randall Spangler5ac39bf2011-03-17 17:58:56 -0700370 break;
Randall Spanglercabe6b32011-03-18 12:44:27 -0700371 case VDAT_INT_TRIED_FIRMWARE_B:
372 value = (sh->flags & VBSD_FWB_TRIED ? 1 : 0);
373 break;
374 case VDAT_INT_KERNEL_KEY_VERIFIED:
375 value = (sh->flags & VBSD_KERNEL_KEY_VERIFIED ? 1 : 0);
376 break;
Randall Spangler89286bc2012-08-20 13:39:35 -0700377 case VDAT_INT_FW_VERSION_TPM:
378 value = (int)sh->fw_version_tpm;
379 break;
380 case VDAT_INT_KERNEL_VERSION_TPM:
381 value = (int)sh->kernel_version_tpm;
382 break;
Randall Spanglerda8d32d2012-08-03 12:48:24 -0700383 default:
Randall Spangler7adcc602011-06-24 16:11:45 -0700384 break;
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700385 }
386
Randall Spanglerda8d32d2012-08-03 12:48:24 -0700387 /* Fields added in struct version 2 */
388 if (sh->struct_version >= 2) {
389 switch(field) {
390 case VDAT_INT_DEVSW_BOOT:
391 value = (sh->flags & VBSD_BOOT_DEV_SWITCH_ON ? 1 : 0);
392 break;
393 case VDAT_INT_DEVSW_VIRTUAL:
394 value = (sh->flags & VBSD_HONOR_VIRT_DEV_SWITCH ? 1 : 0);
395 break;
396 case VDAT_INT_RECSW_BOOT:
397 value = (sh->flags & VBSD_BOOT_REC_SWITCH_ON ? 1 : 0);
398 break;
Bill Richardson9dc62172012-08-28 15:00:51 -0700399 case VDAT_INT_HW_WPSW_BOOT:
Randall Spanglerda8d32d2012-08-03 12:48:24 -0700400 value = (sh->flags & VBSD_BOOT_FIRMWARE_WP_ENABLED ? 1 : 0);
401 break;
Bill Richardson9dc62172012-08-28 15:00:51 -0700402 case VDAT_INT_SW_WPSW_BOOT:
403 value = (sh->flags & VBSD_BOOT_FIRMWARE_SW_WP_ENABLED ? 1 : 0);
404 break;
Randall Spanglerda8d32d2012-08-03 12:48:24 -0700405 case VDAT_INT_RECOVERY_REASON:
406 value = sh->recovery_reason;
407 break;
408 default:
409 break;
410 }
411 }
412
Randall Spangler32a65262011-06-27 10:49:11 -0700413 free(sh);
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700414 return value;
415}
416
Randall Spanglerda8d32d2012-08-03 12:48:24 -0700417/* Return version of VbSharedData struct or -1 if not found. */
418int VbSharedDataVersion(void) {
419 return GetVdatInt(VDAT_INT_HEADER_VERSION);
420}
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700421
Randall Spangler54218662011-02-07 11:20:20 -0800422int VbGetSystemPropertyInt(const char* name) {
Randall Spanglerc80fe652011-02-17 11:06:47 -0800423 int value = -1;
Randall Spangler54218662011-02-07 11:20:20 -0800424
Randall Spanglereb591952011-04-07 10:02:00 -0700425 /* Check architecture-dependent properties first */
426 value = VbGetArchPropertyInt(name);
427 if (-1 != value)
428 return value;
429
430 /* NV storage values */
Randall Spanglercabe6b32011-03-18 12:44:27 -0700431 else if (!strcasecmp(name,"kern_nv")) {
Randall Spangler618d17d2011-03-01 10:33:11 -0800432 value = VbGetNvStorage(VBNV_KERNEL_FIELD);
Randall Spanglerb4167142011-03-01 13:04:22 -0800433 } else if (!strcasecmp(name,"nvram_cleared")) {
434 value = VbGetNvStorage(VBNV_KERNEL_SETTINGS_RESET);
Randall Spanglereb591952011-04-07 10:02:00 -0700435 } else if (!strcasecmp(name,"recovery_request")) {
Randall Spangler0f8ffb12011-02-25 09:50:54 -0800436 value = VbGetNvStorage(VBNV_RECOVERY_REQUEST);
Randall Spanglere73302c2011-02-18 14:53:01 -0800437 } else if (!strcasecmp(name,"dbg_reset")) {
Randall Spangler0f8ffb12011-02-25 09:50:54 -0800438 value = VbGetNvStorage(VBNV_DEBUG_RESET_MODE);
Bill Richardson35d07332012-05-25 12:55:58 -0700439 } else if (!strcasecmp(name,"disable_dev_request")) {
440 value = VbGetNvStorage(VBNV_DISABLE_DEV_REQUEST);
Randall Spangler29e88072012-06-19 10:03:53 -0700441 } else if (!strcasecmp(name,"clear_tpm_owner_request")) {
442 value = VbGetNvStorage(VBNV_CLEAR_TPM_OWNER_REQUEST);
443 } else if (!strcasecmp(name,"clear_tpm_owner_done")) {
444 value = VbGetNvStorage(VBNV_CLEAR_TPM_OWNER_DONE);
Randall Spanglere73302c2011-02-18 14:53:01 -0800445 } else if (!strcasecmp(name,"fwb_tries")) {
Randall Spangler0f8ffb12011-02-25 09:50:54 -0800446 value = VbGetNvStorage(VBNV_TRY_B_COUNT);
Randall Spanglerd7728232011-04-08 14:04:21 -0700447 } else if (!strcasecmp(name,"fwupdate_tries")) {
448 value = VbGetNvStorage(VBNV_KERNEL_FIELD);
449 if (value != -1)
450 value &= KERN_NV_FWUPDATE_TRIES_MASK;
Randall Spangler44a12762011-04-12 13:16:40 -0700451 } else if (!strcasecmp(name,"loc_idx")) {
452 value = VbGetNvStorage(VBNV_LOCALIZATION_INDEX);
Randall Spanglerdaa807c2011-07-11 10:55:18 -0700453 } else if (!strcasecmp(name,"dev_boot_usb")) {
454 value = VbGetNvStorage(VBNV_DEV_BOOT_USB);
Stefan Reinauera2326ee2012-08-23 15:06:45 -0700455 } else if (!strcasecmp(name,"dev_boot_legacy")) {
456 value = VbGetNvStorage(VBNV_DEV_BOOT_LEGACY);
Bill Richardson7272a692011-11-17 10:48:59 -0800457 } else if (!strcasecmp(name,"dev_boot_signed_only")) {
458 value = VbGetNvStorage(VBNV_DEV_BOOT_SIGNED_ONLY);
Bill Richardson17b82242012-06-26 16:33:56 -0700459 } else if (!strcasecmp(name,"oprom_needed")) {
460 value = VbGetNvStorage(VBNV_OPROM_NEEDED);
Bill Richardson699ebf32012-12-17 14:35:22 -0800461 } else if (!strcasecmp(name,"recovery_subcode")) {
462 value = VbGetNvStorage(VBNV_RECOVERY_SUBCODE);
Randall Spanglere73302c2011-02-18 14:53:01 -0800463 }
Randall Spanglerb47ed5a2011-02-23 13:05:40 -0800464 /* Other parameters */
Randall Spanglereb591952011-04-07 10:02:00 -0700465 else if (!strcasecmp(name,"cros_debug")) {
Randall Spangler196e1772011-03-10 11:31:06 -0800466 value = VbGetCrosDebug();
J. Richard Barnette92cbd5d2013-10-22 16:21:14 -0700467 } else if (!strcasecmp(name,"debug_build")) {
468 value = VbGetDebugBuild();
Randall Spanglerda8d32d2012-08-03 12:48:24 -0700469 } else if (!strcasecmp(name,"devsw_boot")) {
470 value = GetVdatInt(VDAT_INT_DEVSW_BOOT);
471 } else if (!strcasecmp(name,"devsw_virtual")) {
472 value = GetVdatInt(VDAT_INT_DEVSW_VIRTUAL);
473 } else if (!strcasecmp(name, "recoverysw_boot")) {
474 value = GetVdatInt(VDAT_INT_RECSW_BOOT);
475 } else if (!strcasecmp(name, "wpsw_boot")) {
Bill Richardson9dc62172012-08-28 15:00:51 -0700476 value = GetVdatInt(VDAT_INT_HW_WPSW_BOOT);
477 } else if (!strcasecmp(name, "sw_wpsw_boot")) {
478 value = GetVdatInt(VDAT_INT_SW_WPSW_BOOT);
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700479 } else if (!strcasecmp(name,"vdat_flags")) {
480 value = GetVdatInt(VDAT_INT_FLAGS);
Randall Spangler5ac39bf2011-03-17 17:58:56 -0700481 } else if (!strcasecmp(name,"tpm_fwver")) {
482 value = GetVdatInt(VDAT_INT_FW_VERSION_TPM);
483 } else if (!strcasecmp(name,"tpm_kernver")) {
484 value = GetVdatInt(VDAT_INT_KERNEL_VERSION_TPM);
Randall Spanglercabe6b32011-03-18 12:44:27 -0700485 } else if (!strcasecmp(name,"tried_fwb")) {
486 value = GetVdatInt(VDAT_INT_TRIED_FIRMWARE_B);
Randall Spangler7adcc602011-06-24 16:11:45 -0700487 } else if (!strcasecmp(name,"recovery_reason")) {
488 value = GetVdatInt(VDAT_INT_RECOVERY_REASON);
Randall Spanglerb47ed5a2011-02-23 13:05:40 -0800489 }
Randall Spangler54218662011-02-07 11:20:20 -0800490
Randall Spanglerc80fe652011-02-17 11:06:47 -0800491 return value;
Randall Spangler54218662011-02-07 11:20:20 -0800492}
493
Randall Spangler54218662011-02-07 11:20:20 -0800494
Randall Spanglereb591952011-04-07 10:02:00 -0700495const char* VbGetSystemPropertyString(const char* name, char* dest, int size) {
Tom Wai-Hong Tamd808a432012-06-29 17:24:27 +0800496 static const char unknown_string[] = "unknown";
497
Randall Spanglereb591952011-04-07 10:02:00 -0700498 /* Check architecture-dependent properties first */
499 if (VbGetArchPropertyString(name, dest, size))
500 return dest;
501
502 if (!strcasecmp(name,"kernkey_vfy")) {
Randall Spanglercabe6b32011-03-18 12:44:27 -0700503 switch(GetVdatInt(VDAT_INT_KERNEL_KEY_VERIFIED)) {
Randall Spangler17260282011-02-25 12:06:26 -0800504 case 0:
505 return "hash";
506 case 1:
507 return "sig";
508 default:
509 return NULL;
510 }
Randall Spanglera185b8d2011-07-15 16:28:38 -0700511 } else if (!strcasecmp(name, "mainfw_act")) {
512 return GetVdatString(dest, size, VDAT_STRING_MAINFW_ACT);
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700513 } else if (!strcasecmp(name, "vdat_timers")) {
514 return GetVdatString(dest, size, VDAT_STRING_TIMERS);
515 } else if (!strcasecmp(name, "vdat_lfdebug")) {
516 return GetVdatString(dest, size, VDAT_STRING_LOAD_FIRMWARE_DEBUG);
Randall Spangler71415712011-03-21 11:04:50 -0700517 } else if (!strcasecmp(name, "vdat_lkdebug")) {
518 return GetVdatString(dest, size, VDAT_STRING_LOAD_KERNEL_DEBUG);
Tom Wai-Hong Tamd808a432012-06-29 17:24:27 +0800519 } else if (!strcasecmp(name, "ddr_type")) {
520 return unknown_string;
Randall Spanglereb591952011-04-07 10:02:00 -0700521 }
522
523 return NULL;
Randall Spangler54218662011-02-07 11:20:20 -0800524}
525
526
Randall Spangler54218662011-02-07 11:20:20 -0800527int VbSetSystemPropertyInt(const char* name, int value) {
Randall Spanglereb591952011-04-07 10:02:00 -0700528 /* Check architecture-dependent properties first */
Randall Spanglerd7728232011-04-08 14:04:21 -0700529
Randall Spanglereb591952011-04-07 10:02:00 -0700530 if (0 == VbSetArchPropertyInt(name, value))
531 return 0;
Randall Spangler54218662011-02-07 11:20:20 -0800532
Randall Spanglereb591952011-04-07 10:02:00 -0700533 /* NV storage values */
Randall Spanglerb4167142011-03-01 13:04:22 -0800534 if (!strcasecmp(name,"nvram_cleared")) {
535 /* Can only clear this flag; it's set inside the NV storage library. */
536 return VbSetNvStorage(VBNV_KERNEL_SETTINGS_RESET, 0);
Randall Spanglereb591952011-04-07 10:02:00 -0700537 } else if (!strcasecmp(name,"recovery_request")) {
538 return VbSetNvStorage(VBNV_RECOVERY_REQUEST, value);
Bill Richardson699ebf32012-12-17 14:35:22 -0800539 } else if (!strcasecmp(name,"recovery_subcode")) {
540 return VbSetNvStorage(VBNV_RECOVERY_SUBCODE, value);
Randall Spanglere73302c2011-02-18 14:53:01 -0800541 } else if (!strcasecmp(name,"dbg_reset")) {
Randall Spanglereb591952011-04-07 10:02:00 -0700542 return VbSetNvStorage(VBNV_DEBUG_RESET_MODE, value);
Bill Richardson35d07332012-05-25 12:55:58 -0700543 } else if (!strcasecmp(name,"disable_dev_request")) {
544 return VbSetNvStorage(VBNV_DISABLE_DEV_REQUEST, value);
Randall Spangler29e88072012-06-19 10:03:53 -0700545 } else if (!strcasecmp(name,"clear_tpm_owner_request")) {
546 return VbSetNvStorage(VBNV_CLEAR_TPM_OWNER_REQUEST, value);
547 } else if (!strcasecmp(name,"clear_tpm_owner_done")) {
548 /* Can only clear this flag; it's set by firmware. */
549 return VbSetNvStorage(VBNV_CLEAR_TPM_OWNER_DONE, 0);
Randall Spanglere73302c2011-02-18 14:53:01 -0800550 } else if (!strcasecmp(name,"fwb_tries")) {
Randall Spanglereb591952011-04-07 10:02:00 -0700551 return VbSetNvStorage(VBNV_TRY_B_COUNT, value);
Randall Spanglerd7728232011-04-08 14:04:21 -0700552 } else if (!strcasecmp(name,"fwupdate_tries")) {
553 int kern_nv = VbGetNvStorage(VBNV_KERNEL_FIELD);
554 if (kern_nv == -1)
555 return -1;
556 kern_nv &= ~KERN_NV_FWUPDATE_TRIES_MASK;
557 kern_nv |= (value & KERN_NV_FWUPDATE_TRIES_MASK);
558 return VbSetNvStorage(VBNV_KERNEL_FIELD, kern_nv);
Randall Spangler44a12762011-04-12 13:16:40 -0700559 } else if (!strcasecmp(name,"loc_idx")) {
560 return VbSetNvStorage(VBNV_LOCALIZATION_INDEX, value);
Randall Spanglerdaa807c2011-07-11 10:55:18 -0700561 } else if (!strcasecmp(name,"dev_boot_usb")) {
562 return VbSetNvStorage(VBNV_DEV_BOOT_USB, value);
Stefan Reinauera2326ee2012-08-23 15:06:45 -0700563 } else if (!strcasecmp(name,"dev_boot_legacy")) {
564 return VbSetNvStorage(VBNV_DEV_BOOT_LEGACY, value);
Bill Richardson7272a692011-11-17 10:48:59 -0800565 } else if (!strcasecmp(name,"dev_boot_signed_only")) {
566 return VbSetNvStorage(VBNV_DEV_BOOT_SIGNED_ONLY, value);
Bill Richardson17b82242012-06-26 16:33:56 -0700567 } else if (!strcasecmp(name,"oprom_needed")) {
568 return VbSetNvStorage(VBNV_OPROM_NEEDED, value);
Randall Spanglere73302c2011-02-18 14:53:01 -0800569 }
570
Randall Spangler54218662011-02-07 11:20:20 -0800571 return -1;
572}
573
574
Randall Spangler54218662011-02-07 11:20:20 -0800575int VbSetSystemPropertyString(const char* name, const char* value) {
Randall Spanglereb591952011-04-07 10:02:00 -0700576 /* Chain to architecture-dependent properties */
577 return VbSetArchPropertyString(name, value);
Randall Spangler54218662011-02-07 11:20:20 -0800578}