blob: c4d0a607b9e8244071b5b180d0ac205525722ae4 [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;
132
J. Richard Barnettedbffddc2011-08-25 11:22:43 -0700133 /* If the currently running system specifies its debug status, use
134 * that in preference to other indicators. */
135 f = fopen(KERNEL_CMDLINE_PATH, "r");
136 if (NULL != f) {
Randall Spangler196e1772011-03-10 11:31:06 -0800137 if (NULL == fgets(buf, sizeof(buf), f))
J. Richard Barnettedbffddc2011-08-25 11:22:43 -0700138 buf[0] = 0;
Randall Spangler196e1772011-03-10 11:31:06 -0800139 fclose(f);
140 }
141 for (t = strtok_r(buf, " ", &saveptr); t; t=strtok_r(NULL, " ", &saveptr)) {
142 if (0 == strcmp(t, "cros_debug"))
143 return 1;
Randall Spangler227f7922011-03-11 13:34:56 -0800144 else if (0 == strcmp(t, "cros_nodebug"))
145 return 0;
Randall Spangler196e1772011-03-10 11:31:06 -0800146 }
147
J. Richard Barnettedbffddc2011-08-25 11:22:43 -0700148 /* Command line is silent; allow debug if the dev switch is on. */
Randall Spanglereb591952011-04-07 10:02:00 -0700149 if (1 == VbGetSystemPropertyInt("devsw_boot"))
Randall Spangler196e1772011-03-10 11:31:06 -0800150 return 1;
151
152 /* All other cases disallow debug. */
153 return 0;
154}
155
Randall Spanglerb47ed5a2011-02-23 13:05:40 -0800156
Randall Spangler71415712011-03-21 11:04:50 -0700157char* GetVdatLoadFirmwareDebug(char* dest, int size,
158 const VbSharedDataHeader* sh) {
159 snprintf(dest, size,
160 "Check A result=%d\n"
161 "Check B result=%d\n"
162 "Firmware index booted=0x%02x\n"
163 "TPM combined version at start=0x%08x\n"
164 "Lowest combined version from firmware=0x%08x\n",
165 sh->check_fw_a_result,
166 sh->check_fw_b_result,
167 sh->firmware_index,
168 sh->fw_version_tpm_start,
169 sh->fw_version_lowest);
170 return dest;
171}
172
173
174#define TRUNCATED "\n(truncated)\n"
175
176char* GetVdatLoadKernelDebug(char* dest, int size,
177 const VbSharedDataHeader* sh) {
178 int used = 0;
179 int first_call_tracked = 0;
180 int call;
181
182 /* Make sure we have space for truncation warning */
183 if (size < strlen(TRUNCATED) + 1)
184 return NULL;
185 size -= strlen(TRUNCATED) + 1;
186
187 used += snprintf(
188 dest + used, size - used,
189 "Calls to LoadKernel()=%d\n",
190 sh->lk_call_count);
191 if (used > size)
192 goto LoadKernelDebugExit;
193
194 /* Report on the last calls */
195 if (sh->lk_call_count > VBSD_MAX_KERNEL_CALLS)
196 first_call_tracked = sh->lk_call_count - VBSD_MAX_KERNEL_CALLS;
197 for (call = first_call_tracked; call < sh->lk_call_count; call++) {
198 const VbSharedDataKernelCall* shc =
199 sh->lk_calls + (call & (VBSD_MAX_KERNEL_CALLS - 1));
200 int first_part_tracked = 0;
201 int part;
202
203 used += snprintf(
204 dest + used, size - used,
205 "Call %d:\n"
206 " Boot flags=0x%02x\n"
207 " Boot mode=%d\n"
208 " Test error=%d\n"
209 " Return code=%d\n"
210 " Debug flags=0x%02x\n"
211 " Drive sectors=%" PRIu64 "\n"
212 " Sector size=%d\n"
213 " Check result=%d\n"
214 " Kernel partitions found=%d\n",
215 call + 1,
216 shc->boot_flags,
217 shc->boot_mode,
218 shc->test_error_num,
219 shc->return_code,
220 shc->flags,
221 shc->sector_count,
222 shc->sector_size,
223 shc->check_result,
224 shc->kernel_parts_found);
225 if (used > size)
226 goto LoadKernelDebugExit;
227
228 /* If we found too many partitions, only prints ones where the
229 * structure has info. */
230 if (shc->kernel_parts_found > VBSD_MAX_KERNEL_PARTS)
231 first_part_tracked = shc->kernel_parts_found - VBSD_MAX_KERNEL_PARTS;
232
233 /* Report on the partitions checked */
234 for (part = first_part_tracked; part < shc->kernel_parts_found; part++) {
235 const VbSharedDataKernelPart* shp =
236 shc->parts + (part & (VBSD_MAX_KERNEL_PARTS - 1));
237
238 used += snprintf(
239 dest + used, size - used,
240 " Kernel %d:\n"
241 " GPT index=%d\n"
242 " Start sector=%" PRIu64 "\n"
243 " Sector count=%" PRIu64 "\n"
244 " Combined version=0x%08x\n"
245 " Check result=%d\n"
246 " Debug flags=0x%02x\n",
247 part + 1,
248 shp->gpt_index,
249 shp->sector_start,
250 shp->sector_count,
251 shp->combined_version,
252 shp->check_result,
253 shp->flags);
254 if (used > size)
255 goto LoadKernelDebugExit;
256 }
257 }
258
259LoadKernelDebugExit:
260
261 /* Warn if data was truncated; we left space for this above. */
262 if (used > size)
263 strcat(dest, TRUNCATED);
264
265 return dest;
266}
267
268
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700269char* GetVdatString(char* dest, int size, VdatStringField field)
270{
Randall Spanglereb591952011-04-07 10:02:00 -0700271 VbSharedDataHeader* sh = VbSharedDataRead();
Randall Spangler71415712011-03-21 11:04:50 -0700272 char* value = dest;
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700273
Randall Spanglereb591952011-04-07 10:02:00 -0700274 if (!sh)
275 return NULL;
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700276
277 switch (field) {
278 case VDAT_STRING_TIMERS:
279 snprintf(dest, size,
280 "LFS=%" PRIu64 ",%" PRIu64
281 " LF=%" PRIu64 ",%" PRIu64
282 " LK=%" PRIu64 ",%" PRIu64,
Randall Spangler96191122011-07-08 14:01:54 -0700283 sh->timer_vb_init_enter,
284 sh->timer_vb_init_exit,
285 sh->timer_vb_select_firmware_enter,
286 sh->timer_vb_select_firmware_exit,
287 sh->timer_vb_select_and_load_kernel_enter,
288 sh->timer_vb_select_and_load_kernel_exit);
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700289 break;
290
291 case VDAT_STRING_LOAD_FIRMWARE_DEBUG:
Randall Spangler71415712011-03-21 11:04:50 -0700292 value = GetVdatLoadFirmwareDebug(dest, size, sh);
293 break;
294
295 case VDAT_STRING_LOAD_KERNEL_DEBUG:
296 value = GetVdatLoadKernelDebug(dest, size, sh);
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700297 break;
298
Randall Spanglera185b8d2011-07-15 16:28:38 -0700299 case VDAT_STRING_MAINFW_ACT:
300 switch(sh->firmware_index) {
301 case 0:
302 StrCopy(dest, "A", size);
303 break;
304 case 1:
305 StrCopy(dest, "B", size);
306 break;
307 case 0xFF:
308 StrCopy(dest, "recovery", size);
309 break;
310 default:
311 value = NULL;
312 }
313 break;
314
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700315 default:
Randall Spanglereb591952011-04-07 10:02:00 -0700316 value = NULL;
317 break;
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700318 }
319
Randall Spangler32a65262011-06-27 10:49:11 -0700320 free(sh);
Randall Spangler71415712011-03-21 11:04:50 -0700321 return value;
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700322}
323
324
325int GetVdatInt(VdatIntField field) {
Randall Spanglereb591952011-04-07 10:02:00 -0700326 VbSharedDataHeader* sh = VbSharedDataRead();
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700327 int value = -1;
328
Randall Spanglereb591952011-04-07 10:02:00 -0700329 if (!sh)
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700330 return -1;
331
Randall Spanglerda8d32d2012-08-03 12:48:24 -0700332 /* Fields supported in version 1 */
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700333 switch (field) {
334 case VDAT_INT_FLAGS:
335 value = (int)sh->flags;
336 break;
Randall Spanglerda8d32d2012-08-03 12:48:24 -0700337 case VDAT_INT_HEADER_VERSION:
338 value = sh->struct_version;
Randall Spangler5ac39bf2011-03-17 17:58:56 -0700339 break;
Randall Spanglercabe6b32011-03-18 12:44:27 -0700340 case VDAT_INT_TRIED_FIRMWARE_B:
341 value = (sh->flags & VBSD_FWB_TRIED ? 1 : 0);
342 break;
343 case VDAT_INT_KERNEL_KEY_VERIFIED:
344 value = (sh->flags & VBSD_KERNEL_KEY_VERIFIED ? 1 : 0);
345 break;
Randall Spangler89286bc2012-08-20 13:39:35 -0700346 case VDAT_INT_FW_VERSION_TPM:
347 value = (int)sh->fw_version_tpm;
348 break;
349 case VDAT_INT_KERNEL_VERSION_TPM:
350 value = (int)sh->kernel_version_tpm;
351 break;
Randall Spanglerda8d32d2012-08-03 12:48:24 -0700352 default:
Randall Spangler7adcc602011-06-24 16:11:45 -0700353 break;
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700354 }
355
Randall Spanglerda8d32d2012-08-03 12:48:24 -0700356 /* Fields added in struct version 2 */
357 if (sh->struct_version >= 2) {
358 switch(field) {
359 case VDAT_INT_DEVSW_BOOT:
360 value = (sh->flags & VBSD_BOOT_DEV_SWITCH_ON ? 1 : 0);
361 break;
362 case VDAT_INT_DEVSW_VIRTUAL:
363 value = (sh->flags & VBSD_HONOR_VIRT_DEV_SWITCH ? 1 : 0);
364 break;
365 case VDAT_INT_RECSW_BOOT:
366 value = (sh->flags & VBSD_BOOT_REC_SWITCH_ON ? 1 : 0);
367 break;
Bill Richardson9dc62172012-08-28 15:00:51 -0700368 case VDAT_INT_HW_WPSW_BOOT:
Randall Spanglerda8d32d2012-08-03 12:48:24 -0700369 value = (sh->flags & VBSD_BOOT_FIRMWARE_WP_ENABLED ? 1 : 0);
370 break;
Bill Richardson9dc62172012-08-28 15:00:51 -0700371 case VDAT_INT_SW_WPSW_BOOT:
372 value = (sh->flags & VBSD_BOOT_FIRMWARE_SW_WP_ENABLED ? 1 : 0);
373 break;
Randall Spanglerda8d32d2012-08-03 12:48:24 -0700374 case VDAT_INT_RECOVERY_REASON:
375 value = sh->recovery_reason;
376 break;
377 default:
378 break;
379 }
380 }
381
Randall Spangler32a65262011-06-27 10:49:11 -0700382 free(sh);
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700383 return value;
384}
385
Randall Spanglerda8d32d2012-08-03 12:48:24 -0700386/* Return version of VbSharedData struct or -1 if not found. */
387int VbSharedDataVersion(void) {
388 return GetVdatInt(VDAT_INT_HEADER_VERSION);
389}
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700390
Randall Spangler54218662011-02-07 11:20:20 -0800391int VbGetSystemPropertyInt(const char* name) {
Randall Spanglerc80fe652011-02-17 11:06:47 -0800392 int value = -1;
Randall Spangler54218662011-02-07 11:20:20 -0800393
Randall Spanglereb591952011-04-07 10:02:00 -0700394 /* Check architecture-dependent properties first */
395 value = VbGetArchPropertyInt(name);
396 if (-1 != value)
397 return value;
398
399 /* NV storage values */
Randall Spanglercabe6b32011-03-18 12:44:27 -0700400 else if (!strcasecmp(name,"kern_nv")) {
Randall Spangler618d17d2011-03-01 10:33:11 -0800401 value = VbGetNvStorage(VBNV_KERNEL_FIELD);
Randall Spanglerb4167142011-03-01 13:04:22 -0800402 } else if (!strcasecmp(name,"nvram_cleared")) {
403 value = VbGetNvStorage(VBNV_KERNEL_SETTINGS_RESET);
Randall Spanglereb591952011-04-07 10:02:00 -0700404 } else if (!strcasecmp(name,"recovery_request")) {
Randall Spangler0f8ffb12011-02-25 09:50:54 -0800405 value = VbGetNvStorage(VBNV_RECOVERY_REQUEST);
Randall Spanglere73302c2011-02-18 14:53:01 -0800406 } else if (!strcasecmp(name,"dbg_reset")) {
Randall Spangler0f8ffb12011-02-25 09:50:54 -0800407 value = VbGetNvStorage(VBNV_DEBUG_RESET_MODE);
Bill Richardson35d07332012-05-25 12:55:58 -0700408 } else if (!strcasecmp(name,"disable_dev_request")) {
409 value = VbGetNvStorage(VBNV_DISABLE_DEV_REQUEST);
Randall Spangler29e88072012-06-19 10:03:53 -0700410 } else if (!strcasecmp(name,"clear_tpm_owner_request")) {
411 value = VbGetNvStorage(VBNV_CLEAR_TPM_OWNER_REQUEST);
412 } else if (!strcasecmp(name,"clear_tpm_owner_done")) {
413 value = VbGetNvStorage(VBNV_CLEAR_TPM_OWNER_DONE);
Randall Spanglere73302c2011-02-18 14:53:01 -0800414 } else if (!strcasecmp(name,"fwb_tries")) {
Randall Spangler0f8ffb12011-02-25 09:50:54 -0800415 value = VbGetNvStorage(VBNV_TRY_B_COUNT);
Randall Spanglerd7728232011-04-08 14:04:21 -0700416 } else if (!strcasecmp(name,"fwupdate_tries")) {
417 value = VbGetNvStorage(VBNV_KERNEL_FIELD);
418 if (value != -1)
419 value &= KERN_NV_FWUPDATE_TRIES_MASK;
Randall Spangler44a12762011-04-12 13:16:40 -0700420 } else if (!strcasecmp(name,"loc_idx")) {
421 value = VbGetNvStorage(VBNV_LOCALIZATION_INDEX);
Randall Spanglerdaa807c2011-07-11 10:55:18 -0700422 } else if (!strcasecmp(name,"dev_boot_usb")) {
423 value = VbGetNvStorage(VBNV_DEV_BOOT_USB);
Stefan Reinauera2326ee2012-08-23 15:06:45 -0700424 } else if (!strcasecmp(name,"dev_boot_legacy")) {
425 value = VbGetNvStorage(VBNV_DEV_BOOT_LEGACY);
Bill Richardson7272a692011-11-17 10:48:59 -0800426 } else if (!strcasecmp(name,"dev_boot_signed_only")) {
427 value = VbGetNvStorage(VBNV_DEV_BOOT_SIGNED_ONLY);
Bill Richardson17b82242012-06-26 16:33:56 -0700428 } else if (!strcasecmp(name,"oprom_needed")) {
429 value = VbGetNvStorage(VBNV_OPROM_NEEDED);
Bill Richardson699ebf32012-12-17 14:35:22 -0800430 } else if (!strcasecmp(name,"recovery_subcode")) {
431 value = VbGetNvStorage(VBNV_RECOVERY_SUBCODE);
Randall Spanglere73302c2011-02-18 14:53:01 -0800432 }
Randall Spanglerb47ed5a2011-02-23 13:05:40 -0800433 /* Other parameters */
Randall Spanglereb591952011-04-07 10:02:00 -0700434 else if (!strcasecmp(name,"cros_debug")) {
Randall Spangler196e1772011-03-10 11:31:06 -0800435 value = VbGetCrosDebug();
Randall Spanglerda8d32d2012-08-03 12:48:24 -0700436 } else if (!strcasecmp(name,"devsw_boot")) {
437 value = GetVdatInt(VDAT_INT_DEVSW_BOOT);
438 } else if (!strcasecmp(name,"devsw_virtual")) {
439 value = GetVdatInt(VDAT_INT_DEVSW_VIRTUAL);
440 } else if (!strcasecmp(name, "recoverysw_boot")) {
441 value = GetVdatInt(VDAT_INT_RECSW_BOOT);
442 } else if (!strcasecmp(name, "wpsw_boot")) {
Bill Richardson9dc62172012-08-28 15:00:51 -0700443 value = GetVdatInt(VDAT_INT_HW_WPSW_BOOT);
444 } else if (!strcasecmp(name, "sw_wpsw_boot")) {
445 value = GetVdatInt(VDAT_INT_SW_WPSW_BOOT);
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700446 } else if (!strcasecmp(name,"vdat_flags")) {
447 value = GetVdatInt(VDAT_INT_FLAGS);
Randall Spangler5ac39bf2011-03-17 17:58:56 -0700448 } else if (!strcasecmp(name,"tpm_fwver")) {
449 value = GetVdatInt(VDAT_INT_FW_VERSION_TPM);
450 } else if (!strcasecmp(name,"tpm_kernver")) {
451 value = GetVdatInt(VDAT_INT_KERNEL_VERSION_TPM);
Randall Spanglercabe6b32011-03-18 12:44:27 -0700452 } else if (!strcasecmp(name,"tried_fwb")) {
453 value = GetVdatInt(VDAT_INT_TRIED_FIRMWARE_B);
Randall Spangler7adcc602011-06-24 16:11:45 -0700454 } else if (!strcasecmp(name,"recovery_reason")) {
455 value = GetVdatInt(VDAT_INT_RECOVERY_REASON);
Randall Spanglerb47ed5a2011-02-23 13:05:40 -0800456 }
Randall Spangler54218662011-02-07 11:20:20 -0800457
Randall Spanglerc80fe652011-02-17 11:06:47 -0800458 return value;
Randall Spangler54218662011-02-07 11:20:20 -0800459}
460
Randall Spangler54218662011-02-07 11:20:20 -0800461
Randall Spanglereb591952011-04-07 10:02:00 -0700462const char* VbGetSystemPropertyString(const char* name, char* dest, int size) {
Tom Wai-Hong Tamd808a432012-06-29 17:24:27 +0800463 static const char unknown_string[] = "unknown";
464
Randall Spanglereb591952011-04-07 10:02:00 -0700465 /* Check architecture-dependent properties first */
466 if (VbGetArchPropertyString(name, dest, size))
467 return dest;
468
469 if (!strcasecmp(name,"kernkey_vfy")) {
Randall Spanglercabe6b32011-03-18 12:44:27 -0700470 switch(GetVdatInt(VDAT_INT_KERNEL_KEY_VERIFIED)) {
Randall Spangler17260282011-02-25 12:06:26 -0800471 case 0:
472 return "hash";
473 case 1:
474 return "sig";
475 default:
476 return NULL;
477 }
Randall Spanglera185b8d2011-07-15 16:28:38 -0700478 } else if (!strcasecmp(name, "mainfw_act")) {
479 return GetVdatString(dest, size, VDAT_STRING_MAINFW_ACT);
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700480 } else if (!strcasecmp(name, "vdat_timers")) {
481 return GetVdatString(dest, size, VDAT_STRING_TIMERS);
482 } else if (!strcasecmp(name, "vdat_lfdebug")) {
483 return GetVdatString(dest, size, VDAT_STRING_LOAD_FIRMWARE_DEBUG);
Randall Spangler71415712011-03-21 11:04:50 -0700484 } else if (!strcasecmp(name, "vdat_lkdebug")) {
485 return GetVdatString(dest, size, VDAT_STRING_LOAD_KERNEL_DEBUG);
Tom Wai-Hong Tamd808a432012-06-29 17:24:27 +0800486 } else if (!strcasecmp(name, "ddr_type")) {
487 return unknown_string;
Randall Spanglereb591952011-04-07 10:02:00 -0700488 }
489
490 return NULL;
Randall Spangler54218662011-02-07 11:20:20 -0800491}
492
493
Randall Spangler54218662011-02-07 11:20:20 -0800494int VbSetSystemPropertyInt(const char* name, int value) {
Randall Spanglereb591952011-04-07 10:02:00 -0700495 /* Check architecture-dependent properties first */
Randall Spanglerd7728232011-04-08 14:04:21 -0700496
Randall Spanglereb591952011-04-07 10:02:00 -0700497 if (0 == VbSetArchPropertyInt(name, value))
498 return 0;
Randall Spangler54218662011-02-07 11:20:20 -0800499
Randall Spanglereb591952011-04-07 10:02:00 -0700500 /* NV storage values */
Randall Spanglerb4167142011-03-01 13:04:22 -0800501 if (!strcasecmp(name,"nvram_cleared")) {
502 /* Can only clear this flag; it's set inside the NV storage library. */
503 return VbSetNvStorage(VBNV_KERNEL_SETTINGS_RESET, 0);
Randall Spanglereb591952011-04-07 10:02:00 -0700504 } else if (!strcasecmp(name,"recovery_request")) {
505 return VbSetNvStorage(VBNV_RECOVERY_REQUEST, value);
Bill Richardson699ebf32012-12-17 14:35:22 -0800506 } else if (!strcasecmp(name,"recovery_subcode")) {
507 return VbSetNvStorage(VBNV_RECOVERY_SUBCODE, value);
Randall Spanglere73302c2011-02-18 14:53:01 -0800508 } else if (!strcasecmp(name,"dbg_reset")) {
Randall Spanglereb591952011-04-07 10:02:00 -0700509 return VbSetNvStorage(VBNV_DEBUG_RESET_MODE, value);
Bill Richardson35d07332012-05-25 12:55:58 -0700510 } else if (!strcasecmp(name,"disable_dev_request")) {
511 return VbSetNvStorage(VBNV_DISABLE_DEV_REQUEST, value);
Randall Spangler29e88072012-06-19 10:03:53 -0700512 } else if (!strcasecmp(name,"clear_tpm_owner_request")) {
513 return VbSetNvStorage(VBNV_CLEAR_TPM_OWNER_REQUEST, value);
514 } else if (!strcasecmp(name,"clear_tpm_owner_done")) {
515 /* Can only clear this flag; it's set by firmware. */
516 return VbSetNvStorage(VBNV_CLEAR_TPM_OWNER_DONE, 0);
Randall Spanglere73302c2011-02-18 14:53:01 -0800517 } else if (!strcasecmp(name,"fwb_tries")) {
Randall Spanglereb591952011-04-07 10:02:00 -0700518 return VbSetNvStorage(VBNV_TRY_B_COUNT, value);
Randall Spanglerd7728232011-04-08 14:04:21 -0700519 } else if (!strcasecmp(name,"fwupdate_tries")) {
520 int kern_nv = VbGetNvStorage(VBNV_KERNEL_FIELD);
521 if (kern_nv == -1)
522 return -1;
523 kern_nv &= ~KERN_NV_FWUPDATE_TRIES_MASK;
524 kern_nv |= (value & KERN_NV_FWUPDATE_TRIES_MASK);
525 return VbSetNvStorage(VBNV_KERNEL_FIELD, kern_nv);
Randall Spangler44a12762011-04-12 13:16:40 -0700526 } else if (!strcasecmp(name,"loc_idx")) {
527 return VbSetNvStorage(VBNV_LOCALIZATION_INDEX, value);
Randall Spanglerdaa807c2011-07-11 10:55:18 -0700528 } else if (!strcasecmp(name,"dev_boot_usb")) {
529 return VbSetNvStorage(VBNV_DEV_BOOT_USB, value);
Stefan Reinauera2326ee2012-08-23 15:06:45 -0700530 } else if (!strcasecmp(name,"dev_boot_legacy")) {
531 return VbSetNvStorage(VBNV_DEV_BOOT_LEGACY, value);
Bill Richardson7272a692011-11-17 10:48:59 -0800532 } else if (!strcasecmp(name,"dev_boot_signed_only")) {
533 return VbSetNvStorage(VBNV_DEV_BOOT_SIGNED_ONLY, value);
Bill Richardson17b82242012-06-26 16:33:56 -0700534 } else if (!strcasecmp(name,"oprom_needed")) {
535 return VbSetNvStorage(VBNV_OPROM_NEEDED, value);
Randall Spanglere73302c2011-02-18 14:53:01 -0800536 }
537
Randall Spangler54218662011-02-07 11:20:20 -0800538 return -1;
539}
540
541
Randall Spangler54218662011-02-07 11:20:20 -0800542int VbSetSystemPropertyString(const char* name, const char* value) {
Randall Spanglereb591952011-04-07 10:02:00 -0700543 /* Chain to architecture-dependent properties */
544 return VbSetArchPropertyString(name, value);
Randall Spangler54218662011-02-07 11:20:20 -0800545}