Randall Spangler | c0e3742 | 2012-06-08 12:30:17 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Randall Spangler | 5421866 | 2011-02-07 11:20:20 -0800 | [diff] [blame] | 2 | * Use of this source code is governed by a BSD-style license that can be |
| 3 | * found in the LICENSE file. |
| 4 | */ |
| 5 | |
J. Richard Barnette | a3d70a3 | 2013-10-30 11:36:45 -0700 | [diff] [blame] | 6 | #include <stddef.h> |
Randall Spangler | 5421866 | 2011-02-07 11:20:20 -0800 | [diff] [blame] | 7 | #include <stdio.h> |
| 8 | #include <string.h> |
Vadim Bendebury | 2008423 | 2011-03-15 09:29:48 -0700 | [diff] [blame] | 9 | #include <sys/types.h> |
| 10 | #include <sys/stat.h> |
| 11 | #include <unistd.h> |
| 12 | #include <ctype.h> |
Duncan Laurie | e98431f | 2016-03-30 09:35:20 -0700 | [diff] [blame] | 13 | #include <sys/types.h> |
| 14 | #include <sys/stat.h> |
| 15 | #include <sys/wait.h> |
| 16 | #include <fcntl.h> |
| 17 | #include <unistd.h> |
Randall Spangler | 5421866 | 2011-02-07 11:20:20 -0800 | [diff] [blame] | 18 | |
Randall Spangler | dff5852 | 2017-11-27 15:37:13 -0800 | [diff] [blame] | 19 | #include "2api.h" |
Joel Kitching | 98b00ae | 2019-06-25 16:11:45 +0800 | [diff] [blame] | 20 | #include "2common.h" |
Randall Spangler | dff5852 | 2017-11-27 15:37:13 -0800 | [diff] [blame] | 21 | #include "2nvstorage.h" |
Joel Kitching | ffd42a8 | 2019-08-29 13:58:52 +0800 | [diff] [blame] | 22 | #include "2sysincludes.h" |
Randall Spangler | eb59195 | 2011-04-07 10:02:00 -0700 | [diff] [blame] | 23 | #include "crossystem_arch.h" |
Joel Kitching | ffd42a8 | 2019-08-29 13:58:52 +0800 | [diff] [blame] | 24 | #include "crossystem.h" |
Duncan Laurie | e98431f | 2016-03-30 09:35:20 -0700 | [diff] [blame] | 25 | #include "crossystem_vbnv.h" |
Joel Kitching | ffd42a8 | 2019-08-29 13:58:52 +0800 | [diff] [blame] | 26 | #include "host_common.h" |
Randall Spangler | 5421866 | 2011-02-07 11:20:20 -0800 | [diff] [blame] | 27 | #include "utility.h" |
Randall Spangler | f4ba19d | 2011-03-17 16:10:21 -0700 | [diff] [blame] | 28 | #include "vboot_struct.h" |
Randall Spangler | 5421866 | 2011-02-07 11:20:20 -0800 | [diff] [blame] | 29 | |
Randall Spangler | 196e177 | 2011-03-10 11:31:06 -0800 | [diff] [blame] | 30 | /* Filename for kernel command line */ |
| 31 | #define KERNEL_CMDLINE_PATH "/proc/cmdline" |
| 32 | |
Jack Rosenthal | fde6a34 | 2019-12-03 14:53:27 -0700 | [diff] [blame] | 33 | #define MOSYS_PATH "/usr/sbin/mosys" |
Duncan Laurie | e98431f | 2016-03-30 09:35:20 -0700 | [diff] [blame] | 34 | |
Randall Spangler | f4ba19d | 2011-03-17 16:10:21 -0700 | [diff] [blame] | 35 | /* Fields that GetVdatString() can get */ |
| 36 | typedef enum VdatStringField { |
Joel Kitching | 77dff64 | 2019-10-14 23:14:59 +0800 | [diff] [blame] | 37 | VDAT_STRING_DEPRECATED_TIMERS = 0, /* Timer values */ |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 38 | VDAT_STRING_LOAD_FIRMWARE_DEBUG, /* LoadFirmware() debug information */ |
| 39 | VDAT_STRING_LOAD_KERNEL_DEBUG, /* LoadKernel() debug information */ |
| 40 | VDAT_STRING_MAINFW_ACT /* Active main firmware */ |
Randall Spangler | f4ba19d | 2011-03-17 16:10:21 -0700 | [diff] [blame] | 41 | } VdatStringField; |
| 42 | |
| 43 | |
| 44 | /* Fields that GetVdatInt() can get */ |
| 45 | typedef enum VdatIntField { |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 46 | VDAT_INT_FLAGS = 0, /* Flags */ |
| 47 | VDAT_INT_HEADER_VERSION, /* Header version for VbSharedData */ |
| 48 | VDAT_INT_DEVSW_BOOT, /* Dev switch position at boot */ |
Joel Kitching | f9a2fa1 | 2019-03-18 16:46:39 +0800 | [diff] [blame] | 49 | VDAT_INT_DEPRECATED_DEVSW_VIRTUAL, /* Dev switch is virtual; |
| 50 | * deprecated: chromium:942901 */ |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 51 | VDAT_INT_RECSW_BOOT, /* Recovery switch position at boot */ |
Tudor Brindus | c239374 | 2018-07-31 16:25:26 -0700 | [diff] [blame] | 52 | VDAT_INT_RECSW_VIRTUAL, /* Recovery switch is virtual */ |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 53 | VDAT_INT_HW_WPSW_BOOT, /* Hardware WP switch position at boot */ |
Randall Spangler | da8d32d | 2012-08-03 12:48:24 -0700 | [diff] [blame] | 54 | |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 55 | VDAT_INT_FW_VERSION_TPM, /* Current firmware version in TPM */ |
| 56 | VDAT_INT_KERNEL_VERSION_TPM, /* Current kernel version in TPM */ |
| 57 | VDAT_INT_TRIED_FIRMWARE_B, /* Tried firmware B due to fwb_tries */ |
| 58 | VDAT_INT_KERNEL_KEY_VERIFIED, /* Kernel key verified using |
| 59 | * signature, not just hash */ |
| 60 | VDAT_INT_RECOVERY_REASON, /* Recovery reason for current boot */ |
| 61 | VDAT_INT_FW_BOOT2 /* Firmware selection by vboot2 */ |
Randall Spangler | f4ba19d | 2011-03-17 16:10:21 -0700 | [diff] [blame] | 62 | } VdatIntField; |
| 63 | |
| 64 | |
J. Richard Barnette | 92cbd5d | 2013-10-22 16:21:14 -0700 | [diff] [blame] | 65 | /* Description of build options that may be specified on the |
| 66 | * kernel command line. */ |
| 67 | typedef enum VbBuildOption { |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 68 | VB_BUILD_OPTION_UNKNOWN, |
| 69 | VB_BUILD_OPTION_DEBUG, |
| 70 | VB_BUILD_OPTION_NODEBUG |
J. Richard Barnette | 92cbd5d | 2013-10-22 16:21:14 -0700 | [diff] [blame] | 71 | } VbBuildOption; |
| 72 | |
Randall Spangler | 9e1da78 | 2014-06-24 15:31:04 -0700 | [diff] [blame] | 73 | static const char *fw_results[] = {"unknown", "trying", "success", "failure"}; |
Mary Ruthven | 12a55f2 | 2015-10-06 10:42:31 -0700 | [diff] [blame] | 74 | static const char *default_boot[] = {"disk", "usb", "legacy"}; |
J. Richard Barnette | 92cbd5d | 2013-10-22 16:21:14 -0700 | [diff] [blame] | 75 | |
Randall Spangler | ff3f000 | 2011-07-26 10:43:53 -0700 | [diff] [blame] | 76 | /* Masks for kern_nv usage by kernel. */ |
Randall Spangler | 4aaaeca | 2017-10-20 12:38:40 -0700 | [diff] [blame] | 77 | #define KERN_NV_FWUPDATE_TRIES_MASK 0x000F |
| 78 | #define KERN_NV_BLOCK_DEVMODE_FLAG 0x0010 |
| 79 | #define KERN_NV_TPM_ATTACK_FLAG 0x0020 |
Randall Spangler | ff3f000 | 2011-07-26 10:43:53 -0700 | [diff] [blame] | 80 | /* If you want to use the remaining currently-unused bits in kern_nv |
| 81 | * for something kernel-y, define a new field (the way we did for |
| 82 | * fwupdate_tries). Don't just modify kern_nv directly, because that |
| 83 | * makes it too easy to accidentally corrupt other sub-fields. */ |
Randall Spangler | 4aaaeca | 2017-10-20 12:38:40 -0700 | [diff] [blame] | 84 | #define KERN_NV_CURRENTLY_UNUSED 0xFFC0 |
Randall Spangler | d772823 | 2011-04-08 14:04:21 -0700 | [diff] [blame] | 85 | |
Randall Spangler | c80fe65 | 2011-02-17 11:06:47 -0800 | [diff] [blame] | 86 | /* Return true if the FWID starts with the specified string. */ |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 87 | int FwidStartsWith(const char *start) |
| 88 | { |
| 89 | char fwid[VB_MAX_STRING_PROPERTY]; |
| 90 | if (!VbGetSystemPropertyString("fwid", fwid, sizeof(fwid))) |
| 91 | return 0; |
Randall Spangler | c80fe65 | 2011-02-17 11:06:47 -0800 | [diff] [blame] | 92 | |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 93 | return 0 == strncmp(fwid, start, strlen(start)); |
Randall Spangler | c80fe65 | 2011-02-17 11:06:47 -0800 | [diff] [blame] | 94 | } |
| 95 | |
Julius Werner | 49428f4 | 2019-11-14 09:48:34 -0800 | [diff] [blame] | 96 | static struct vb2_context *get_fake_context(void) |
| 97 | { |
| 98 | static uint8_t fake_workbuf[sizeof(struct vb2_shared_data) + 16] |
| 99 | __attribute__((aligned(VB2_WORKBUF_ALIGN))); |
| 100 | static struct vb2_context *fake_ctx; |
| 101 | |
| 102 | if (fake_ctx) |
| 103 | return fake_ctx; |
| 104 | |
| 105 | vb2api_init(fake_workbuf, sizeof(fake_workbuf), &fake_ctx); |
| 106 | |
| 107 | return fake_ctx; |
| 108 | } |
| 109 | |
Vadim Bendebury | 38201fe | 2014-11-07 17:25:09 -0800 | [diff] [blame] | 110 | static int vnc_read; |
Randall Spangler | c80fe65 | 2011-02-17 11:06:47 -0800 | [diff] [blame] | 111 | |
Randall Spangler | dff5852 | 2017-11-27 15:37:13 -0800 | [diff] [blame] | 112 | int vb2_get_nv_storage(enum vb2_nv_param param) |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 113 | { |
Randall Spangler | a80a79f | 2018-02-26 17:01:24 -0800 | [diff] [blame] | 114 | VbSharedDataHeader* sh = VbSharedDataRead(); |
Julius Werner | 49428f4 | 2019-11-14 09:48:34 -0800 | [diff] [blame] | 115 | struct vb2_context *ctx = get_fake_context(); |
Randall Spangler | 0f8ffb1 | 2011-02-25 09:50:54 -0800 | [diff] [blame] | 116 | |
Patrick Georgi | d12e6da | 2019-08-27 17:18:03 +0200 | [diff] [blame] | 117 | if (!sh) |
| 118 | return -1; |
| 119 | |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 120 | /* TODO: locking around NV access */ |
| 121 | if (!vnc_read) { |
Randall Spangler | 0bdb871 | 2018-03-07 21:06:15 -0800 | [diff] [blame] | 122 | if (sh && sh->flags & VBSD_NVDATA_V2) |
Julius Werner | 49428f4 | 2019-11-14 09:48:34 -0800 | [diff] [blame] | 123 | ctx->flags |= VB2_CONTEXT_NVDATA_V2; |
| 124 | if (0 != vb2_read_nv_storage(ctx)) { |
Patrick Georgi | d12e6da | 2019-08-27 17:18:03 +0200 | [diff] [blame] | 125 | free(sh); |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 126 | return -1; |
Patrick Georgi | d12e6da | 2019-08-27 17:18:03 +0200 | [diff] [blame] | 127 | } |
Julius Werner | 49428f4 | 2019-11-14 09:48:34 -0800 | [diff] [blame] | 128 | vb2_nv_init(ctx); |
Randall Spangler | dff5852 | 2017-11-27 15:37:13 -0800 | [diff] [blame] | 129 | |
| 130 | /* TODO: If vnc.raw_changed, attempt to reopen NVRAM for write |
| 131 | * and save the new defaults. If we're able to, log. */ |
| 132 | |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 133 | vnc_read = 1; |
| 134 | } |
Randall Spangler | eb59195 | 2011-04-07 10:02:00 -0700 | [diff] [blame] | 135 | |
Patrick Georgi | d12e6da | 2019-08-27 17:18:03 +0200 | [diff] [blame] | 136 | free(sh); |
Julius Werner | 49428f4 | 2019-11-14 09:48:34 -0800 | [diff] [blame] | 137 | return (int)vb2_nv_get(ctx, param); |
Randall Spangler | 0f8ffb1 | 2011-02-25 09:50:54 -0800 | [diff] [blame] | 138 | } |
| 139 | |
Randall Spangler | dff5852 | 2017-11-27 15:37:13 -0800 | [diff] [blame] | 140 | int vb2_set_nv_storage(enum vb2_nv_param param, int value) |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 141 | { |
Randall Spangler | a80a79f | 2018-02-26 17:01:24 -0800 | [diff] [blame] | 142 | VbSharedDataHeader* sh = VbSharedDataRead(); |
Julius Werner | 49428f4 | 2019-11-14 09:48:34 -0800 | [diff] [blame] | 143 | struct vb2_context *ctx = get_fake_context(); |
Randall Spangler | 0f8ffb1 | 2011-02-25 09:50:54 -0800 | [diff] [blame] | 144 | |
Patrick Georgi | d12e6da | 2019-08-27 17:18:03 +0200 | [diff] [blame] | 145 | if (!sh) |
| 146 | return -1; |
| 147 | |
Randall Spangler | dff5852 | 2017-11-27 15:37:13 -0800 | [diff] [blame] | 148 | /* TODO: locking around NV access */ |
Randall Spangler | 0bdb871 | 2018-03-07 21:06:15 -0800 | [diff] [blame] | 149 | if (sh && sh->flags & VBSD_NVDATA_V2) |
Julius Werner | 49428f4 | 2019-11-14 09:48:34 -0800 | [diff] [blame] | 150 | ctx->flags |= VB2_CONTEXT_NVDATA_V2; |
| 151 | if (0 != vb2_read_nv_storage(ctx)) { |
Patrick Georgi | d12e6da | 2019-08-27 17:18:03 +0200 | [diff] [blame] | 152 | free(sh); |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 153 | return -1; |
Patrick Georgi | d12e6da | 2019-08-27 17:18:03 +0200 | [diff] [blame] | 154 | } |
Julius Werner | 49428f4 | 2019-11-14 09:48:34 -0800 | [diff] [blame] | 155 | vb2_nv_init(ctx); |
| 156 | vb2_nv_set(ctx, param, (uint32_t)value); |
Randall Spangler | 0f8ffb1 | 2011-02-25 09:50:54 -0800 | [diff] [blame] | 157 | |
Julius Werner | 49428f4 | 2019-11-14 09:48:34 -0800 | [diff] [blame] | 158 | if (ctx->flags & VB2_CONTEXT_NVDATA_CHANGED) { |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 159 | vnc_read = 0; |
Julius Werner | 49428f4 | 2019-11-14 09:48:34 -0800 | [diff] [blame] | 160 | if (0 != vb2_write_nv_storage(ctx)) { |
Patrick Georgi | d12e6da | 2019-08-27 17:18:03 +0200 | [diff] [blame] | 161 | free(sh); |
Randall Spangler | dff5852 | 2017-11-27 15:37:13 -0800 | [diff] [blame] | 162 | return -1; |
Patrick Georgi | d12e6da | 2019-08-27 17:18:03 +0200 | [diff] [blame] | 163 | } |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 164 | } |
Randall Spangler | 0f8ffb1 | 2011-02-25 09:50:54 -0800 | [diff] [blame] | 165 | |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 166 | /* Success */ |
Patrick Georgi | d12e6da | 2019-08-27 17:18:03 +0200 | [diff] [blame] | 167 | free(sh); |
Randall Spangler | dff5852 | 2017-11-27 15:37:13 -0800 | [diff] [blame] | 168 | return 0; |
Randall Spangler | 0f8ffb1 | 2011-02-25 09:50:54 -0800 | [diff] [blame] | 169 | } |
| 170 | |
Bill Richardson | b64f097 | 2014-05-28 15:49:23 -0700 | [diff] [blame] | 171 | /* |
Randall Spangler | dff5852 | 2017-11-27 15:37:13 -0800 | [diff] [blame] | 172 | * Set a param value, and try to flag it for persistent backup. It's okay if |
| 173 | * backup isn't supported (which it isn't, in current designs). It's |
| 174 | * best-effort only. |
Bill Richardson | b64f097 | 2014-05-28 15:49:23 -0700 | [diff] [blame] | 175 | */ |
Randall Spangler | dff5852 | 2017-11-27 15:37:13 -0800 | [diff] [blame] | 176 | static int vb2_set_nv_storage_with_backup(enum vb2_nv_param param, int value) |
Bill Richardson | b64f097 | 2014-05-28 15:49:23 -0700 | [diff] [blame] | 177 | { |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 178 | int retval; |
Randall Spangler | dff5852 | 2017-11-27 15:37:13 -0800 | [diff] [blame] | 179 | retval = vb2_set_nv_storage(param, value); |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 180 | if (!retval) |
Randall Spangler | dff5852 | 2017-11-27 15:37:13 -0800 | [diff] [blame] | 181 | vb2_set_nv_storage(VB2_NV_BACKUP_NVRAM_REQUEST, 1); |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 182 | return retval; |
Bill Richardson | b64f097 | 2014-05-28 15:49:23 -0700 | [diff] [blame] | 183 | } |
| 184 | |
J. Richard Barnette | 92cbd5d | 2013-10-22 16:21:14 -0700 | [diff] [blame] | 185 | /* Find what build/debug status is specified on the kernel command |
| 186 | * line, if any. */ |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 187 | static VbBuildOption VbScanBuildOption(void) |
| 188 | { |
| 189 | FILE* f = NULL; |
| 190 | char buf[4096] = ""; |
| 191 | char *t, *saveptr; |
| 192 | const char *delimiters = " \r\n"; |
Randall Spangler | 196e177 | 2011-03-10 11:31:06 -0800 | [diff] [blame] | 193 | |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 194 | f = fopen(KERNEL_CMDLINE_PATH, "r"); |
| 195 | if (NULL != f) { |
| 196 | if (NULL == fgets(buf, sizeof(buf), f)) |
| 197 | buf[0] = 0; |
| 198 | fclose(f); |
| 199 | } |
| 200 | for (t = strtok_r(buf, delimiters, &saveptr); t; |
| 201 | t = strtok_r(NULL, delimiters, &saveptr)) { |
| 202 | if (0 == strcmp(t, "cros_debug")) |
| 203 | return VB_BUILD_OPTION_DEBUG; |
| 204 | else if (0 == strcmp(t, "cros_nodebug")) |
| 205 | return VB_BUILD_OPTION_NODEBUG; |
| 206 | } |
J. Richard Barnette | 92cbd5d | 2013-10-22 16:21:14 -0700 | [diff] [blame] | 207 | |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 208 | return VB_BUILD_OPTION_UNKNOWN; |
J. Richard Barnette | 92cbd5d | 2013-10-22 16:21:14 -0700 | [diff] [blame] | 209 | } |
| 210 | |
J. Richard Barnette | 92cbd5d | 2013-10-22 16:21:14 -0700 | [diff] [blame] | 211 | /* Determine whether the running OS image was built for debugging. |
| 212 | * Returns 1 if yes, 0 if no or indeterminate. */ |
Joel Kitching | e6700f4 | 2019-07-31 14:12:30 +0800 | [diff] [blame] | 213 | static vb2_error_t VbGetDebugBuild(void) |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 214 | { |
| 215 | return VB_BUILD_OPTION_DEBUG == VbScanBuildOption(); |
J. Richard Barnette | 92cbd5d | 2013-10-22 16:21:14 -0700 | [diff] [blame] | 216 | } |
| 217 | |
J. Richard Barnette | 92cbd5d | 2013-10-22 16:21:14 -0700 | [diff] [blame] | 218 | /* Determine whether OS-level debugging should be allowed. |
| 219 | * Returns 1 if yes, 0 if no or indeterminate. */ |
Julius Werner | 52fa8c1 | 2019-05-07 12:59:47 -0700 | [diff] [blame] | 220 | static int VbGetCrosDebug(void) |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 221 | { |
| 222 | /* If the currently running system specifies its debug status, use |
| 223 | * that in preference to other indicators. */ |
| 224 | VbBuildOption option = VbScanBuildOption(); |
| 225 | if (VB_BUILD_OPTION_DEBUG == option) { |
| 226 | return 1; |
| 227 | } else if (VB_BUILD_OPTION_NODEBUG == option) { |
| 228 | return 0; |
| 229 | } |
Randall Spangler | 196e177 | 2011-03-10 11:31:06 -0800 | [diff] [blame] | 230 | |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 231 | /* Command line is silent; allow debug if the dev switch is on. */ |
| 232 | if (1 == VbGetSystemPropertyInt("devsw_boot")) |
| 233 | return 1; |
Randall Spangler | 196e177 | 2011-03-10 11:31:06 -0800 | [diff] [blame] | 234 | |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 235 | /* All other cases disallow debug. */ |
| 236 | return 0; |
Randall Spangler | 196e177 | 2011-03-10 11:31:06 -0800 | [diff] [blame] | 237 | } |
| 238 | |
Julius Werner | 52fa8c1 | 2019-05-07 12:59:47 -0700 | [diff] [blame] | 239 | static char *GetVdatLoadFirmwareDebug(char *dest, int size, |
| 240 | const VbSharedDataHeader *sh) |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 241 | { |
| 242 | snprintf(dest, size, |
| 243 | "Check A result=%d\n" |
| 244 | "Check B result=%d\n" |
| 245 | "Firmware index booted=0x%02x\n" |
| 246 | "TPM combined version at start=0x%08x\n" |
| 247 | "Lowest combined version from firmware=0x%08x\n", |
| 248 | sh->check_fw_a_result, |
| 249 | sh->check_fw_b_result, |
| 250 | sh->firmware_index, |
| 251 | sh->fw_version_tpm_start, |
| 252 | sh->fw_version_lowest); |
| 253 | return dest; |
Randall Spangler | 7141571 | 2011-03-21 11:04:50 -0700 | [diff] [blame] | 254 | } |
| 255 | |
Randall Spangler | 7141571 | 2011-03-21 11:04:50 -0700 | [diff] [blame] | 256 | #define TRUNCATED "\n(truncated)\n" |
| 257 | |
Julius Werner | 52fa8c1 | 2019-05-07 12:59:47 -0700 | [diff] [blame] | 258 | static char *GetVdatLoadKernelDebug(char *dest, int size, |
| 259 | const VbSharedDataHeader *sh) |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 260 | { |
| 261 | int used = 0; |
| 262 | int first_call_tracked = 0; |
| 263 | int call; |
Randall Spangler | 7141571 | 2011-03-21 11:04:50 -0700 | [diff] [blame] | 264 | |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 265 | /* Make sure we have space for truncation warning */ |
| 266 | if (size < strlen(TRUNCATED) + 1) |
| 267 | return NULL; |
| 268 | size -= strlen(TRUNCATED) + 1; |
Randall Spangler | 7141571 | 2011-03-21 11:04:50 -0700 | [diff] [blame] | 269 | |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 270 | used += snprintf( |
| 271 | dest + used, size - used, |
| 272 | "Calls to LoadKernel()=%d\n", |
| 273 | sh->lk_call_count); |
| 274 | if (used > size) |
| 275 | goto LoadKernelDebugExit; |
Randall Spangler | 7141571 | 2011-03-21 11:04:50 -0700 | [diff] [blame] | 276 | |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 277 | /* Report on the last calls */ |
| 278 | if (sh->lk_call_count > VBSD_MAX_KERNEL_CALLS) |
| 279 | first_call_tracked = sh->lk_call_count - VBSD_MAX_KERNEL_CALLS; |
| 280 | for (call = first_call_tracked; call < sh->lk_call_count; call++) { |
| 281 | const VbSharedDataKernelCall* shc = sh->lk_calls + |
| 282 | (call & (VBSD_MAX_KERNEL_CALLS - 1)); |
| 283 | int first_part_tracked = 0; |
| 284 | int part; |
Randall Spangler | 7141571 | 2011-03-21 11:04:50 -0700 | [diff] [blame] | 285 | |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 286 | used += snprintf(dest + used, size - used, |
| 287 | "Call %d:\n" |
| 288 | " Boot flags=0x%02x\n" |
| 289 | " Boot mode=%d\n" |
| 290 | " Test error=%d\n" |
| 291 | " Return code=%d\n" |
| 292 | " Debug flags=0x%02x\n" |
| 293 | " Drive sectors=%" PRIu64 "\n" |
| 294 | " Sector size=%d\n" |
| 295 | " Check result=%d\n" |
| 296 | " Kernel partitions found=%d\n", |
| 297 | call + 1, |
| 298 | shc->boot_flags, |
| 299 | shc->boot_mode, |
| 300 | shc->test_error_num, |
| 301 | shc->return_code, |
| 302 | shc->flags, |
| 303 | shc->sector_count, |
| 304 | shc->sector_size, |
| 305 | shc->check_result, |
| 306 | shc->kernel_parts_found); |
| 307 | if (used > size) |
| 308 | goto LoadKernelDebugExit; |
Randall Spangler | 7141571 | 2011-03-21 11:04:50 -0700 | [diff] [blame] | 309 | |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 310 | /* If we found too many partitions, only prints ones where the |
| 311 | * structure has info. */ |
| 312 | if (shc->kernel_parts_found > VBSD_MAX_KERNEL_PARTS) |
| 313 | first_part_tracked = shc->kernel_parts_found - |
| 314 | VBSD_MAX_KERNEL_PARTS; |
Randall Spangler | 7141571 | 2011-03-21 11:04:50 -0700 | [diff] [blame] | 315 | |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 316 | /* Report on the partitions checked */ |
| 317 | for (part = first_part_tracked; part < shc->kernel_parts_found; |
| 318 | part++) { |
| 319 | const VbSharedDataKernelPart* shp = shc->parts + |
| 320 | (part & (VBSD_MAX_KERNEL_PARTS - 1)); |
Randall Spangler | 7141571 | 2011-03-21 11:04:50 -0700 | [diff] [blame] | 321 | |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 322 | used += snprintf(dest + used, size - used, |
| 323 | " Kernel %d:\n" |
| 324 | " GPT index=%d\n" |
| 325 | " Start sector=%" PRIu64 "\n" |
| 326 | " Sector count=%" PRIu64 "\n" |
| 327 | " Combined version=0x%08x\n" |
| 328 | " Check result=%d\n" |
| 329 | " Debug flags=0x%02x\n", |
| 330 | part + 1, |
| 331 | shp->gpt_index, |
| 332 | shp->sector_start, |
| 333 | shp->sector_count, |
| 334 | shp->combined_version, |
| 335 | shp->check_result, |
| 336 | shp->flags); |
| 337 | if (used > size) |
| 338 | goto LoadKernelDebugExit; |
| 339 | } |
| 340 | } |
Randall Spangler | 7141571 | 2011-03-21 11:04:50 -0700 | [diff] [blame] | 341 | |
| 342 | LoadKernelDebugExit: |
| 343 | |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 344 | /* Warn if data was truncated; we left space for this above. */ |
| 345 | if (used > size) |
| 346 | strcat(dest, TRUNCATED); |
Randall Spangler | 7141571 | 2011-03-21 11:04:50 -0700 | [diff] [blame] | 347 | |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 348 | return dest; |
Randall Spangler | 7141571 | 2011-03-21 11:04:50 -0700 | [diff] [blame] | 349 | } |
| 350 | |
Julius Werner | 52fa8c1 | 2019-05-07 12:59:47 -0700 | [diff] [blame] | 351 | static char *GetVdatString(char *dest, int size, VdatStringField field) |
Randall Spangler | f4ba19d | 2011-03-17 16:10:21 -0700 | [diff] [blame] | 352 | { |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 353 | VbSharedDataHeader *sh = VbSharedDataRead(); |
| 354 | char *value = dest; |
Randall Spangler | f4ba19d | 2011-03-17 16:10:21 -0700 | [diff] [blame] | 355 | |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 356 | if (!sh) |
| 357 | return NULL; |
Randall Spangler | f4ba19d | 2011-03-17 16:10:21 -0700 | [diff] [blame] | 358 | |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 359 | switch (field) { |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 360 | case VDAT_STRING_LOAD_FIRMWARE_DEBUG: |
| 361 | value = GetVdatLoadFirmwareDebug(dest, size, sh); |
| 362 | break; |
Randall Spangler | 7141571 | 2011-03-21 11:04:50 -0700 | [diff] [blame] | 363 | |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 364 | case VDAT_STRING_LOAD_KERNEL_DEBUG: |
| 365 | value = GetVdatLoadKernelDebug(dest, size, sh); |
| 366 | break; |
Randall Spangler | f4ba19d | 2011-03-17 16:10:21 -0700 | [diff] [blame] | 367 | |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 368 | case VDAT_STRING_MAINFW_ACT: |
| 369 | switch(sh->firmware_index) { |
| 370 | case 0: |
| 371 | StrCopy(dest, "A", size); |
| 372 | break; |
| 373 | case 1: |
| 374 | StrCopy(dest, "B", size); |
| 375 | break; |
| 376 | case 0xFF: |
| 377 | StrCopy(dest, "recovery", size); |
| 378 | break; |
| 379 | default: |
| 380 | value = NULL; |
| 381 | } |
| 382 | break; |
Randall Spangler | a185b8d | 2011-07-15 16:28:38 -0700 | [diff] [blame] | 383 | |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 384 | default: |
| 385 | value = NULL; |
| 386 | break; |
| 387 | } |
Randall Spangler | f4ba19d | 2011-03-17 16:10:21 -0700 | [diff] [blame] | 388 | |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 389 | free(sh); |
| 390 | return value; |
Randall Spangler | f4ba19d | 2011-03-17 16:10:21 -0700 | [diff] [blame] | 391 | } |
| 392 | |
Julius Werner | 52fa8c1 | 2019-05-07 12:59:47 -0700 | [diff] [blame] | 393 | static int GetVdatInt(VdatIntField field) |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 394 | { |
| 395 | VbSharedDataHeader* sh = VbSharedDataRead(); |
| 396 | int value = -1; |
Randall Spangler | f4ba19d | 2011-03-17 16:10:21 -0700 | [diff] [blame] | 397 | |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 398 | if (!sh) |
| 399 | return -1; |
Randall Spangler | f4ba19d | 2011-03-17 16:10:21 -0700 | [diff] [blame] | 400 | |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 401 | /* Fields supported in version 1 */ |
| 402 | switch (field) { |
| 403 | case VDAT_INT_FLAGS: |
| 404 | value = (int)sh->flags; |
| 405 | break; |
| 406 | case VDAT_INT_HEADER_VERSION: |
| 407 | value = sh->struct_version; |
| 408 | break; |
| 409 | case VDAT_INT_TRIED_FIRMWARE_B: |
Joel Kitching | 3c00180 | 2019-10-02 12:53:21 +0800 | [diff] [blame] | 410 | value = (sh->flags & VBSD_DEPRECATED_FWB_TRIED ? 1 : 0); |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 411 | break; |
| 412 | case VDAT_INT_KERNEL_KEY_VERIFIED: |
| 413 | value = (sh->flags & VBSD_KERNEL_KEY_VERIFIED ? 1 : 0); |
| 414 | break; |
| 415 | case VDAT_INT_FW_VERSION_TPM: |
| 416 | value = (int)sh->fw_version_tpm; |
| 417 | break; |
| 418 | case VDAT_INT_KERNEL_VERSION_TPM: |
| 419 | value = (int)sh->kernel_version_tpm; |
| 420 | break; |
| 421 | case VDAT_INT_FW_BOOT2: |
| 422 | value = (sh->flags & VBSD_BOOT_FIRMWARE_VBOOT2 ? 1 : 0); |
Mathew King | ed8390e | 2019-11-05 22:21:33 +0000 | [diff] [blame] | 423 | VBOOT_FALLTHROUGH; |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 424 | default: |
| 425 | break; |
| 426 | } |
Randall Spangler | f4ba19d | 2011-03-17 16:10:21 -0700 | [diff] [blame] | 427 | |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 428 | /* Fields added in struct version 2 */ |
| 429 | if (sh->struct_version >= 2) { |
| 430 | switch(field) { |
| 431 | case VDAT_INT_DEVSW_BOOT: |
| 432 | value = (sh->flags & |
| 433 | VBSD_BOOT_DEV_SWITCH_ON ? 1 : 0); |
| 434 | break; |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 435 | case VDAT_INT_RECSW_BOOT: |
| 436 | value = (sh->flags & |
| 437 | VBSD_BOOT_REC_SWITCH_ON ? 1 : 0); |
| 438 | break; |
Tudor Brindus | c239374 | 2018-07-31 16:25:26 -0700 | [diff] [blame] | 439 | case VDAT_INT_RECSW_VIRTUAL: |
| 440 | value = (sh->flags & |
| 441 | VBSD_BOOT_REC_SWITCH_VIRTUAL ? 1 : 0); |
| 442 | break; |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 443 | case VDAT_INT_HW_WPSW_BOOT: |
| 444 | value = (sh->flags & |
| 445 | VBSD_BOOT_FIRMWARE_WP_ENABLED ? 1 : 0); |
| 446 | break; |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 447 | case VDAT_INT_RECOVERY_REASON: |
| 448 | value = sh->recovery_reason; |
| 449 | break; |
| 450 | default: |
| 451 | break; |
| 452 | } |
| 453 | } |
Randall Spangler | f4ba19d | 2011-03-17 16:10:21 -0700 | [diff] [blame] | 454 | |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 455 | free(sh); |
| 456 | return value; |
Randall Spangler | f4ba19d | 2011-03-17 16:10:21 -0700 | [diff] [blame] | 457 | } |
| 458 | |
Randall Spangler | da8d32d | 2012-08-03 12:48:24 -0700 | [diff] [blame] | 459 | /* Return version of VbSharedData struct or -1 if not found. */ |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 460 | int VbSharedDataVersion(void) |
| 461 | { |
| 462 | return GetVdatInt(VDAT_INT_HEADER_VERSION); |
Randall Spangler | da8d32d | 2012-08-03 12:48:24 -0700 | [diff] [blame] | 463 | } |
Randall Spangler | f4ba19d | 2011-03-17 16:10:21 -0700 | [diff] [blame] | 464 | |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 465 | int VbGetSystemPropertyInt(const char *name) |
| 466 | { |
| 467 | int value = -1; |
Randall Spangler | 5421866 | 2011-02-07 11:20:20 -0800 | [diff] [blame] | 468 | |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 469 | /* Check architecture-dependent properties first */ |
| 470 | value = VbGetArchPropertyInt(name); |
| 471 | if (-1 != value) |
| 472 | return value; |
Randall Spangler | eb59195 | 2011-04-07 10:02:00 -0700 | [diff] [blame] | 473 | |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 474 | /* NV storage values */ |
| 475 | else if (!strcasecmp(name,"kern_nv")) { |
Randall Spangler | dff5852 | 2017-11-27 15:37:13 -0800 | [diff] [blame] | 476 | value = vb2_get_nv_storage(VB2_NV_KERNEL_FIELD); |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 477 | } else if (!strcasecmp(name,"nvram_cleared")) { |
Randall Spangler | dff5852 | 2017-11-27 15:37:13 -0800 | [diff] [blame] | 478 | value = vb2_get_nv_storage(VB2_NV_KERNEL_SETTINGS_RESET); |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 479 | } else if (!strcasecmp(name,"recovery_request")) { |
Randall Spangler | dff5852 | 2017-11-27 15:37:13 -0800 | [diff] [blame] | 480 | value = vb2_get_nv_storage(VB2_NV_RECOVERY_REQUEST); |
Matt Delco | 90a95ea | 2019-03-05 16:54:23 -0800 | [diff] [blame] | 481 | } else if (!strcasecmp(name,"diagnostic_request")) { |
| 482 | value = vb2_get_nv_storage(VB2_NV_DIAG_REQUEST); |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 483 | } else if (!strcasecmp(name,"dbg_reset")) { |
Randall Spangler | dff5852 | 2017-11-27 15:37:13 -0800 | [diff] [blame] | 484 | value = vb2_get_nv_storage(VB2_NV_DEBUG_RESET_MODE); |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 485 | } else if (!strcasecmp(name,"disable_dev_request")) { |
Randall Spangler | dff5852 | 2017-11-27 15:37:13 -0800 | [diff] [blame] | 486 | value = vb2_get_nv_storage(VB2_NV_DISABLE_DEV_REQUEST); |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 487 | } else if (!strcasecmp(name,"clear_tpm_owner_request")) { |
Randall Spangler | dff5852 | 2017-11-27 15:37:13 -0800 | [diff] [blame] | 488 | value = vb2_get_nv_storage(VB2_NV_CLEAR_TPM_OWNER_REQUEST); |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 489 | } else if (!strcasecmp(name,"clear_tpm_owner_done")) { |
Randall Spangler | dff5852 | 2017-11-27 15:37:13 -0800 | [diff] [blame] | 490 | value = vb2_get_nv_storage(VB2_NV_CLEAR_TPM_OWNER_DONE); |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 491 | } else if (!strcasecmp(name,"tpm_rebooted")) { |
Randall Spangler | dff5852 | 2017-11-27 15:37:13 -0800 | [diff] [blame] | 492 | value = vb2_get_nv_storage(VB2_NV_TPM_REQUESTED_REBOOT); |
| 493 | } else if (!strcasecmp(name,"fwb_tries") || |
| 494 | !strcasecmp(name,"fw_try_count")) { |
| 495 | value = vb2_get_nv_storage(VB2_NV_TRY_COUNT); |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 496 | } else if (!strcasecmp(name,"fw_vboot2")) { |
| 497 | value = GetVdatInt(VDAT_INT_FW_BOOT2); |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 498 | } else if (!strcasecmp(name,"fwupdate_tries")) { |
Randall Spangler | dff5852 | 2017-11-27 15:37:13 -0800 | [diff] [blame] | 499 | value = vb2_get_nv_storage(VB2_NV_KERNEL_FIELD); |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 500 | if (value != -1) |
| 501 | value &= KERN_NV_FWUPDATE_TRIES_MASK; |
| 502 | } else if (!strcasecmp(name,"block_devmode")) { |
Randall Spangler | dff5852 | 2017-11-27 15:37:13 -0800 | [diff] [blame] | 503 | value = vb2_get_nv_storage(VB2_NV_KERNEL_FIELD); |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 504 | if (value != -1) { |
| 505 | value &= KERN_NV_BLOCK_DEVMODE_FLAG; |
| 506 | value = !!value; |
| 507 | } |
| 508 | } else if (!strcasecmp(name,"tpm_attack")) { |
Randall Spangler | dff5852 | 2017-11-27 15:37:13 -0800 | [diff] [blame] | 509 | value = vb2_get_nv_storage(VB2_NV_KERNEL_FIELD); |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 510 | if (value != -1) { |
| 511 | value &= KERN_NV_TPM_ATTACK_FLAG; |
| 512 | value = !!value; |
| 513 | } |
| 514 | } else if (!strcasecmp(name,"loc_idx")) { |
Randall Spangler | dff5852 | 2017-11-27 15:37:13 -0800 | [diff] [blame] | 515 | value = vb2_get_nv_storage(VB2_NV_LOCALIZATION_INDEX); |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 516 | } else if (!strcasecmp(name,"backup_nvram_request")) { |
Randall Spangler | dff5852 | 2017-11-27 15:37:13 -0800 | [diff] [blame] | 517 | value = vb2_get_nv_storage(VB2_NV_BACKUP_NVRAM_REQUEST); |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 518 | } else if (!strcasecmp(name,"dev_boot_usb")) { |
Randall Spangler | dff5852 | 2017-11-27 15:37:13 -0800 | [diff] [blame] | 519 | value = vb2_get_nv_storage(VB2_NV_DEV_BOOT_USB); |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 520 | } else if (!strcasecmp(name,"dev_boot_legacy")) { |
Randall Spangler | dff5852 | 2017-11-27 15:37:13 -0800 | [diff] [blame] | 521 | value = vb2_get_nv_storage(VB2_NV_DEV_BOOT_LEGACY); |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 522 | } else if (!strcasecmp(name,"dev_boot_signed_only")) { |
Randall Spangler | dff5852 | 2017-11-27 15:37:13 -0800 | [diff] [blame] | 523 | value = vb2_get_nv_storage(VB2_NV_DEV_BOOT_SIGNED_ONLY); |
Duncan Laurie | 534194e | 2018-04-12 10:20:16 -0700 | [diff] [blame] | 524 | } else if (!strcasecmp(name,"dev_enable_udc")) { |
| 525 | value = vb2_get_nv_storage(VB2_NV_DEV_ENABLE_UDC); |
Joel Kitching | 43f6cab | 2019-05-10 12:17:18 +0800 | [diff] [blame] | 526 | } else if (!strcasecmp(name,"display_request")) { |
| 527 | value = vb2_get_nv_storage(VB2_NV_DISPLAY_REQUEST); |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 528 | } else if (!strcasecmp(name,"recovery_subcode")) { |
Randall Spangler | dff5852 | 2017-11-27 15:37:13 -0800 | [diff] [blame] | 529 | value = vb2_get_nv_storage(VB2_NV_RECOVERY_SUBCODE); |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 530 | } else if (!strcasecmp(name,"wipeout_request")) { |
Randall Spangler | dff5852 | 2017-11-27 15:37:13 -0800 | [diff] [blame] | 531 | value = vb2_get_nv_storage(VB2_NV_REQ_WIPEOUT); |
Randall Spangler | 4aaaeca | 2017-10-20 12:38:40 -0700 | [diff] [blame] | 532 | } else if (!strcasecmp(name,"kernel_max_rollforward")) { |
Randall Spangler | dff5852 | 2017-11-27 15:37:13 -0800 | [diff] [blame] | 533 | value = vb2_get_nv_storage(VB2_NV_KERNEL_MAX_ROLLFORWARD); |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 534 | } |
| 535 | /* Other parameters */ |
| 536 | else if (!strcasecmp(name,"cros_debug")) { |
| 537 | value = VbGetCrosDebug(); |
| 538 | } else if (!strcasecmp(name,"debug_build")) { |
| 539 | value = VbGetDebugBuild(); |
| 540 | } else if (!strcasecmp(name,"devsw_boot")) { |
| 541 | value = GetVdatInt(VDAT_INT_DEVSW_BOOT); |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 542 | } else if (!strcasecmp(name, "recoverysw_boot")) { |
| 543 | value = GetVdatInt(VDAT_INT_RECSW_BOOT); |
Tudor Brindus | c239374 | 2018-07-31 16:25:26 -0700 | [diff] [blame] | 544 | } else if (!strcasecmp(name, "recoverysw_is_virtual")) { |
| 545 | value = GetVdatInt(VDAT_INT_RECSW_VIRTUAL); |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 546 | } else if (!strcasecmp(name, "wpsw_boot")) { |
| 547 | value = GetVdatInt(VDAT_INT_HW_WPSW_BOOT); |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 548 | } else if (!strcasecmp(name,"vdat_flags")) { |
| 549 | value = GetVdatInt(VDAT_INT_FLAGS); |
| 550 | } else if (!strcasecmp(name,"tpm_fwver")) { |
| 551 | value = GetVdatInt(VDAT_INT_FW_VERSION_TPM); |
| 552 | } else if (!strcasecmp(name,"tpm_kernver")) { |
| 553 | value = GetVdatInt(VDAT_INT_KERNEL_VERSION_TPM); |
| 554 | } else if (!strcasecmp(name,"tried_fwb")) { |
| 555 | value = GetVdatInt(VDAT_INT_TRIED_FIRMWARE_B); |
| 556 | } else if (!strcasecmp(name,"recovery_reason")) { |
| 557 | value = GetVdatInt(VDAT_INT_RECOVERY_REASON); |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 558 | } else if (!strcasecmp(name, "boot_on_ac_detect")) { |
Randall Spangler | dff5852 | 2017-11-27 15:37:13 -0800 | [diff] [blame] | 559 | value = vb2_get_nv_storage(VB2_NV_BOOT_ON_AC_DETECT); |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 560 | } else if (!strcasecmp(name, "try_ro_sync")) { |
Randall Spangler | dff5852 | 2017-11-27 15:37:13 -0800 | [diff] [blame] | 561 | value = vb2_get_nv_storage(VB2_NV_TRY_RO_SYNC); |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 562 | } else if (!strcasecmp(name, "battery_cutoff_request")) { |
Randall Spangler | dff5852 | 2017-11-27 15:37:13 -0800 | [diff] [blame] | 563 | value = vb2_get_nv_storage(VB2_NV_BATTERY_CUTOFF_REQUEST); |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 564 | } else if (!strcasecmp(name, "inside_vm")) { |
| 565 | /* Detect if the host is a VM. If there is no HWID and the |
| 566 | * firmware type is "nonchrome", then assume it is a VM. If |
| 567 | * HWID is present, it is a baremetal Chrome OS machine. Other |
| 568 | * cases are errors. */ |
| 569 | char hwid[VB_MAX_STRING_PROPERTY]; |
| 570 | if (!VbGetSystemPropertyString("hwid", hwid, sizeof(hwid))) { |
| 571 | char fwtype_buf[VB_MAX_STRING_PROPERTY]; |
| 572 | const char *fwtype = VbGetSystemPropertyString( |
Joel Kitching | 9ad8a41 | 2018-08-02 16:21:17 +0800 | [diff] [blame] | 573 | "mainfw_type", fwtype_buf, sizeof(fwtype_buf)); |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 574 | if (fwtype && !strcasecmp(fwtype, "nonchrome")) { |
| 575 | value = 1; |
| 576 | } |
| 577 | } else { |
| 578 | value = 0; |
| 579 | } |
Joel Kitching | 0f77798 | 2018-10-02 20:54:07 +0800 | [diff] [blame] | 580 | } else if (!strcasecmp(name, "post_ec_sync_delay")) { |
| 581 | value = vb2_get_nv_storage(VB2_NV_POST_EC_SYNC_DELAY); |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 582 | } |
Randall Spangler | 5421866 | 2011-02-07 11:20:20 -0800 | [diff] [blame] | 583 | |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 584 | return value; |
| 585 | } |
| 586 | |
| 587 | const char *VbGetSystemPropertyString(const char *name, char *dest, |
Joel Kitching | 9ad8a41 | 2018-08-02 16:21:17 +0800 | [diff] [blame] | 588 | size_t size) |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 589 | { |
| 590 | /* Check architecture-dependent properties first */ |
| 591 | if (VbGetArchPropertyString(name, dest, size)) |
| 592 | return dest; |
| 593 | |
| 594 | if (!strcasecmp(name,"kernkey_vfy")) { |
| 595 | switch(GetVdatInt(VDAT_INT_KERNEL_KEY_VERIFIED)) { |
| 596 | case 0: |
| 597 | return "hash"; |
| 598 | case 1: |
| 599 | return "sig"; |
| 600 | default: |
| 601 | return NULL; |
| 602 | } |
| 603 | } else if (!strcasecmp(name, "mainfw_act")) { |
| 604 | return GetVdatString(dest, size, VDAT_STRING_MAINFW_ACT); |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 605 | } else if (!strcasecmp(name, "vdat_lfdebug")) { |
| 606 | return GetVdatString(dest, size, |
| 607 | VDAT_STRING_LOAD_FIRMWARE_DEBUG); |
| 608 | } else if (!strcasecmp(name, "vdat_lkdebug")) { |
| 609 | return GetVdatString(dest, size, VDAT_STRING_LOAD_KERNEL_DEBUG); |
| 610 | } else if (!strcasecmp(name, "fw_try_next")) { |
Randall Spangler | dff5852 | 2017-11-27 15:37:13 -0800 | [diff] [blame] | 611 | return vb2_get_nv_storage(VB2_NV_TRY_NEXT) ? "B" : "A"; |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 612 | } else if (!strcasecmp(name, "fw_tried")) { |
Randall Spangler | dff5852 | 2017-11-27 15:37:13 -0800 | [diff] [blame] | 613 | return vb2_get_nv_storage(VB2_NV_FW_TRIED) ? "B" : "A"; |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 614 | } else if (!strcasecmp(name, "fw_result")) { |
Randall Spangler | dff5852 | 2017-11-27 15:37:13 -0800 | [diff] [blame] | 615 | int v = vb2_get_nv_storage(VB2_NV_FW_RESULT); |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 616 | if (v < ARRAY_SIZE(fw_results)) |
| 617 | return fw_results[v]; |
| 618 | else |
| 619 | return "unknown"; |
| 620 | } else if (!strcasecmp(name, "fw_prev_tried")) { |
Randall Spangler | dff5852 | 2017-11-27 15:37:13 -0800 | [diff] [blame] | 621 | return vb2_get_nv_storage(VB2_NV_FW_PREV_TRIED) ? "B" : "A"; |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 622 | } else if (!strcasecmp(name, "fw_prev_result")) { |
Randall Spangler | dff5852 | 2017-11-27 15:37:13 -0800 | [diff] [blame] | 623 | int v = vb2_get_nv_storage(VB2_NV_FW_PREV_RESULT); |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 624 | if (v < ARRAY_SIZE(fw_results)) |
| 625 | return fw_results[v]; |
| 626 | else |
| 627 | return "unknown"; |
| 628 | } else if (!strcasecmp(name,"dev_default_boot")) { |
Randall Spangler | dff5852 | 2017-11-27 15:37:13 -0800 | [diff] [blame] | 629 | int v = vb2_get_nv_storage(VB2_NV_DEV_DEFAULT_BOOT); |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 630 | if (v < ARRAY_SIZE(default_boot)) |
| 631 | return default_boot[v]; |
| 632 | else |
| 633 | return "unknown"; |
| 634 | } |
| 635 | |
| 636 | return NULL; |
Randall Spangler | 5421866 | 2011-02-07 11:20:20 -0800 | [diff] [blame] | 637 | } |
| 638 | |
Randall Spangler | 5421866 | 2011-02-07 11:20:20 -0800 | [diff] [blame] | 639 | |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 640 | int VbSetSystemPropertyInt(const char *name, int value) |
| 641 | { |
| 642 | /* Check architecture-dependent properties first */ |
Randall Spangler | eb59195 | 2011-04-07 10:02:00 -0700 | [diff] [blame] | 643 | |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 644 | if (0 == VbSetArchPropertyInt(name, value)) |
| 645 | return 0; |
Randall Spangler | eb59195 | 2011-04-07 10:02:00 -0700 | [diff] [blame] | 646 | |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 647 | /* NV storage values */ |
| 648 | if (!strcasecmp(name,"nvram_cleared")) { |
| 649 | /* Can only clear this flag; it's set inside the NV storage |
| 650 | * library. */ |
Randall Spangler | dff5852 | 2017-11-27 15:37:13 -0800 | [diff] [blame] | 651 | return vb2_set_nv_storage(VB2_NV_KERNEL_SETTINGS_RESET, 0); |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 652 | } else if (!strcasecmp(name,"recovery_request")) { |
Randall Spangler | dff5852 | 2017-11-27 15:37:13 -0800 | [diff] [blame] | 653 | return vb2_set_nv_storage(VB2_NV_RECOVERY_REQUEST, value); |
Matt Delco | 90a95ea | 2019-03-05 16:54:23 -0800 | [diff] [blame] | 654 | } else if (!strcasecmp(name,"diagnostic_request")) { |
| 655 | return vb2_set_nv_storage(VB2_NV_DIAG_REQUEST, value); |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 656 | } else if (!strcasecmp(name,"recovery_subcode")) { |
Randall Spangler | dff5852 | 2017-11-27 15:37:13 -0800 | [diff] [blame] | 657 | return vb2_set_nv_storage(VB2_NV_RECOVERY_SUBCODE, value); |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 658 | } else if (!strcasecmp(name,"dbg_reset")) { |
Randall Spangler | dff5852 | 2017-11-27 15:37:13 -0800 | [diff] [blame] | 659 | return vb2_set_nv_storage(VB2_NV_DEBUG_RESET_MODE, value); |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 660 | } else if (!strcasecmp(name,"disable_dev_request")) { |
Randall Spangler | dff5852 | 2017-11-27 15:37:13 -0800 | [diff] [blame] | 661 | return vb2_set_nv_storage(VB2_NV_DISABLE_DEV_REQUEST, value); |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 662 | } else if (!strcasecmp(name,"clear_tpm_owner_request")) { |
Randall Spangler | dff5852 | 2017-11-27 15:37:13 -0800 | [diff] [blame] | 663 | return vb2_set_nv_storage(VB2_NV_CLEAR_TPM_OWNER_REQUEST, value); |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 664 | } else if (!strcasecmp(name,"clear_tpm_owner_done")) { |
| 665 | /* Can only clear this flag; it's set by firmware. */ |
Randall Spangler | dff5852 | 2017-11-27 15:37:13 -0800 | [diff] [blame] | 666 | return vb2_set_nv_storage(VB2_NV_CLEAR_TPM_OWNER_DONE, 0); |
| 667 | } else if (!strcasecmp(name,"fwb_tries") || |
| 668 | !strcasecmp(name,"fw_try_count")) { |
| 669 | return vb2_set_nv_storage(VB2_NV_TRY_COUNT, value); |
Joel Kitching | 43f6cab | 2019-05-10 12:17:18 +0800 | [diff] [blame] | 670 | } else if (!strcasecmp(name,"display_request")) { |
| 671 | return vb2_set_nv_storage(VB2_NV_DISPLAY_REQUEST, value); |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 672 | } else if (!strcasecmp(name,"wipeout_request")) { |
| 673 | /* Can only clear this flag, set only by firmware. */ |
Randall Spangler | dff5852 | 2017-11-27 15:37:13 -0800 | [diff] [blame] | 674 | return vb2_set_nv_storage(VB2_NV_REQ_WIPEOUT, 0); |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 675 | } else if (!strcasecmp(name,"backup_nvram_request")) { |
| 676 | /* Best-effort only, since it requires firmware and TPM |
| 677 | * support. */ |
Randall Spangler | dff5852 | 2017-11-27 15:37:13 -0800 | [diff] [blame] | 678 | return vb2_set_nv_storage(VB2_NV_BACKUP_NVRAM_REQUEST, value); |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 679 | } else if (!strcasecmp(name,"fwupdate_tries")) { |
Randall Spangler | dff5852 | 2017-11-27 15:37:13 -0800 | [diff] [blame] | 680 | int kern_nv = vb2_get_nv_storage(VB2_NV_KERNEL_FIELD); |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 681 | if (kern_nv == -1) |
| 682 | return -1; |
| 683 | kern_nv &= ~KERN_NV_FWUPDATE_TRIES_MASK; |
| 684 | kern_nv |= (value & KERN_NV_FWUPDATE_TRIES_MASK); |
Joel Kitching | 9ad8a41 | 2018-08-02 16:21:17 +0800 | [diff] [blame] | 685 | return vb2_set_nv_storage_with_backup( |
| 686 | VB2_NV_KERNEL_FIELD, kern_nv); |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 687 | } else if (!strcasecmp(name,"block_devmode")) { |
Randall Spangler | dff5852 | 2017-11-27 15:37:13 -0800 | [diff] [blame] | 688 | int kern_nv = vb2_get_nv_storage(VB2_NV_KERNEL_FIELD); |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 689 | if (kern_nv == -1) |
| 690 | return -1; |
| 691 | kern_nv &= ~KERN_NV_BLOCK_DEVMODE_FLAG; |
| 692 | if (value) |
| 693 | kern_nv |= KERN_NV_BLOCK_DEVMODE_FLAG; |
Joel Kitching | 9ad8a41 | 2018-08-02 16:21:17 +0800 | [diff] [blame] | 694 | return vb2_set_nv_storage_with_backup( |
| 695 | VB2_NV_KERNEL_FIELD, kern_nv); |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 696 | } else if (!strcasecmp(name,"tpm_attack")) { |
| 697 | /* This value should only be read and cleared, but we allow |
| 698 | * setting it to 1 for testing. */ |
Randall Spangler | dff5852 | 2017-11-27 15:37:13 -0800 | [diff] [blame] | 699 | int kern_nv = vb2_get_nv_storage(VB2_NV_KERNEL_FIELD); |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 700 | if (kern_nv == -1) |
| 701 | return -1; |
| 702 | kern_nv &= ~KERN_NV_TPM_ATTACK_FLAG; |
| 703 | if (value) |
| 704 | kern_nv |= KERN_NV_TPM_ATTACK_FLAG; |
Randall Spangler | dff5852 | 2017-11-27 15:37:13 -0800 | [diff] [blame] | 705 | return vb2_set_nv_storage_with_backup( |
Joel Kitching | 9ad8a41 | 2018-08-02 16:21:17 +0800 | [diff] [blame] | 706 | VB2_NV_KERNEL_FIELD, kern_nv); |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 707 | } else if (!strcasecmp(name,"loc_idx")) { |
Randall Spangler | dff5852 | 2017-11-27 15:37:13 -0800 | [diff] [blame] | 708 | return vb2_set_nv_storage_with_backup( |
Joel Kitching | 9ad8a41 | 2018-08-02 16:21:17 +0800 | [diff] [blame] | 709 | VB2_NV_LOCALIZATION_INDEX, value); |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 710 | } else if (!strcasecmp(name,"dev_boot_usb")) { |
Randall Spangler | dff5852 | 2017-11-27 15:37:13 -0800 | [diff] [blame] | 711 | return vb2_set_nv_storage_with_backup( |
Joel Kitching | 9ad8a41 | 2018-08-02 16:21:17 +0800 | [diff] [blame] | 712 | VB2_NV_DEV_BOOT_USB, value); |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 713 | } else if (!strcasecmp(name,"dev_boot_legacy")) { |
Randall Spangler | dff5852 | 2017-11-27 15:37:13 -0800 | [diff] [blame] | 714 | return vb2_set_nv_storage_with_backup( |
Joel Kitching | 9ad8a41 | 2018-08-02 16:21:17 +0800 | [diff] [blame] | 715 | VB2_NV_DEV_BOOT_LEGACY, value); |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 716 | } else if (!strcasecmp(name,"dev_boot_signed_only")) { |
Randall Spangler | dff5852 | 2017-11-27 15:37:13 -0800 | [diff] [blame] | 717 | return vb2_set_nv_storage_with_backup( |
Joel Kitching | 9ad8a41 | 2018-08-02 16:21:17 +0800 | [diff] [blame] | 718 | VB2_NV_DEV_BOOT_SIGNED_ONLY, value); |
Duncan Laurie | 534194e | 2018-04-12 10:20:16 -0700 | [diff] [blame] | 719 | } else if (!strcasecmp(name, "dev_enable_udc")) { |
| 720 | return vb2_set_nv_storage_with_backup( |
Joel Kitching | 9ad8a41 | 2018-08-02 16:21:17 +0800 | [diff] [blame] | 721 | VB2_NV_DEV_ENABLE_UDC, value); |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 722 | } else if (!strcasecmp(name, "boot_on_ac_detect")) { |
Randall Spangler | dff5852 | 2017-11-27 15:37:13 -0800 | [diff] [blame] | 723 | return vb2_set_nv_storage_with_backup( |
Joel Kitching | 9ad8a41 | 2018-08-02 16:21:17 +0800 | [diff] [blame] | 724 | VB2_NV_BOOT_ON_AC_DETECT, value); |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 725 | } else if (!strcasecmp(name, "try_ro_sync")) { |
Randall Spangler | dff5852 | 2017-11-27 15:37:13 -0800 | [diff] [blame] | 726 | return vb2_set_nv_storage_with_backup( |
Joel Kitching | 9ad8a41 | 2018-08-02 16:21:17 +0800 | [diff] [blame] | 727 | VB2_NV_TRY_RO_SYNC, value); |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 728 | } else if (!strcasecmp(name, "battery_cutoff_request")) { |
Randall Spangler | dff5852 | 2017-11-27 15:37:13 -0800 | [diff] [blame] | 729 | return vb2_set_nv_storage(VB2_NV_BATTERY_CUTOFF_REQUEST, value); |
Randall Spangler | 4aaaeca | 2017-10-20 12:38:40 -0700 | [diff] [blame] | 730 | } else if (!strcasecmp(name,"kernel_max_rollforward")) { |
Randall Spangler | dff5852 | 2017-11-27 15:37:13 -0800 | [diff] [blame] | 731 | return vb2_set_nv_storage(VB2_NV_KERNEL_MAX_ROLLFORWARD, value); |
Joel Kitching | 0f77798 | 2018-10-02 20:54:07 +0800 | [diff] [blame] | 732 | } else if (!strcasecmp(name, "post_ec_sync_delay")) { |
| 733 | return vb2_set_nv_storage(VB2_NV_POST_EC_SYNC_DELAY, value); |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | return -1; |
Randall Spangler | 5421866 | 2011-02-07 11:20:20 -0800 | [diff] [blame] | 737 | } |
| 738 | |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 739 | int VbSetSystemPropertyString(const char* name, const char* value) |
| 740 | { |
| 741 | /* Chain to architecture-dependent properties */ |
| 742 | if (0 == VbSetArchPropertyString(name, value)) |
| 743 | return 0; |
Randall Spangler | 5421866 | 2011-02-07 11:20:20 -0800 | [diff] [blame] | 744 | |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 745 | if (!strcasecmp(name, "fw_try_next")) { |
| 746 | if (!strcasecmp(value, "A")) |
Randall Spangler | dff5852 | 2017-11-27 15:37:13 -0800 | [diff] [blame] | 747 | return vb2_set_nv_storage(VB2_NV_TRY_NEXT, 0); |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 748 | else if (!strcasecmp(value, "B")) |
Randall Spangler | dff5852 | 2017-11-27 15:37:13 -0800 | [diff] [blame] | 749 | return vb2_set_nv_storage(VB2_NV_TRY_NEXT, 1); |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 750 | else |
| 751 | return -1; |
Randall Spangler | d772823 | 2011-04-08 14:04:21 -0700 | [diff] [blame] | 752 | |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 753 | } else if (!strcasecmp(name, "fw_result")) { |
| 754 | int i; |
Randall Spangler | 5421866 | 2011-02-07 11:20:20 -0800 | [diff] [blame] | 755 | |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 756 | for (i = 0; i < ARRAY_SIZE(fw_results); i++) { |
| 757 | if (!strcasecmp(value, fw_results[i])) |
Randall Spangler | dff5852 | 2017-11-27 15:37:13 -0800 | [diff] [blame] | 758 | return vb2_set_nv_storage(VB2_NV_FW_RESULT, i); |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 759 | } |
| 760 | return -1; |
| 761 | } else if (!strcasecmp(name, "dev_default_boot")) { |
| 762 | int i; |
Randall Spangler | e73302c | 2011-02-18 14:53:01 -0800 | [diff] [blame] | 763 | |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 764 | for (i = 0; i < ARRAY_SIZE(default_boot); i++) { |
| 765 | if (!strcasecmp(value, default_boot[i])) |
Randall Spangler | dff5852 | 2017-11-27 15:37:13 -0800 | [diff] [blame] | 766 | return vb2_set_nv_storage( |
Joel Kitching | 9ad8a41 | 2018-08-02 16:21:17 +0800 | [diff] [blame] | 767 | VB2_NV_DEV_DEFAULT_BOOT, i); |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 768 | } |
| 769 | return -1; |
| 770 | } |
| 771 | |
| 772 | return -1; |
Randall Spangler | 5421866 | 2011-02-07 11:20:20 -0800 | [diff] [blame] | 773 | } |
| 774 | |
Julius Werner | 52fa8c1 | 2019-05-07 12:59:47 -0700 | [diff] [blame] | 775 | static int ExecuteMosys(const char * const argv[], char *buf, size_t bufsize) |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 776 | { |
| 777 | int status, mosys_to_crossystem[2]; |
| 778 | pid_t pid; |
| 779 | ssize_t n; |
Duncan Laurie | e98431f | 2016-03-30 09:35:20 -0700 | [diff] [blame] | 780 | |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 781 | if (pipe(mosys_to_crossystem) < 0) { |
Randall Spangler | 2101589 | 2017-01-03 13:49:18 -0800 | [diff] [blame] | 782 | fprintf(stderr, "pipe() error\n"); |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 783 | return -1; |
| 784 | } |
Duncan Laurie | e98431f | 2016-03-30 09:35:20 -0700 | [diff] [blame] | 785 | |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 786 | if ((pid = fork()) < 0) { |
Randall Spangler | 2101589 | 2017-01-03 13:49:18 -0800 | [diff] [blame] | 787 | fprintf(stderr, "fork() error\n"); |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 788 | close(mosys_to_crossystem[0]); |
| 789 | close(mosys_to_crossystem[1]); |
| 790 | return -1; |
| 791 | } else if (!pid) { /* Child */ |
| 792 | close(mosys_to_crossystem[0]); |
| 793 | /* Redirect pipe's write-end to mosys' stdout */ |
| 794 | if (STDOUT_FILENO != mosys_to_crossystem[1]) { |
| 795 | if (dup2(mosys_to_crossystem[1], STDOUT_FILENO) |
| 796 | != STDOUT_FILENO) { |
Randall Spangler | 2101589 | 2017-01-03 13:49:18 -0800 | [diff] [blame] | 797 | fprintf(stderr, "stdout dup2() failed (mosys)\n"); |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 798 | close(mosys_to_crossystem[1]); |
| 799 | exit(1); |
| 800 | } |
| 801 | } |
Julius Werner | 52fa8c1 | 2019-05-07 12:59:47 -0700 | [diff] [blame] | 802 | /* Execute mosys (needs cast because POSIX is stupid) */ |
Jack Rosenthal | fde6a34 | 2019-12-03 14:53:27 -0700 | [diff] [blame] | 803 | execv(MOSYS_PATH, (char * const *)argv); |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 804 | /* We shouldn't be here; exit now! */ |
Randall Spangler | 2101589 | 2017-01-03 13:49:18 -0800 | [diff] [blame] | 805 | fprintf(stderr, "execv() of mosys failed\n"); |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 806 | close(mosys_to_crossystem[1]); |
| 807 | exit(1); |
| 808 | } else { /* Parent */ |
| 809 | close(mosys_to_crossystem[1]); |
| 810 | if (bufsize) { |
| 811 | bufsize--; /* Reserve 1 byte for '\0' */ |
| 812 | while ((n = read(mosys_to_crossystem[0], |
| 813 | buf, bufsize)) > 0) { |
| 814 | buf += n; |
| 815 | bufsize -= n; |
| 816 | } |
| 817 | *buf = '\0'; |
| 818 | } else { |
| 819 | n = 0; |
| 820 | } |
| 821 | close(mosys_to_crossystem[0]); |
| 822 | if (n < 0) |
Randall Spangler | 2101589 | 2017-01-03 13:49:18 -0800 | [diff] [blame] | 823 | fprintf(stderr, "read() error on output from mosys\n"); |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 824 | if (waitpid(pid, &status, 0) < 0 || status) { |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 825 | fprintf(stderr, "waitpid() or mosys error\n"); |
| 826 | return -1; |
| 827 | } |
| 828 | if (n < 0) |
| 829 | return -1; |
| 830 | } |
| 831 | return 0; |
Duncan Laurie | e98431f | 2016-03-30 09:35:20 -0700 | [diff] [blame] | 832 | } |
| 833 | |
Randall Spangler | dff5852 | 2017-11-27 15:37:13 -0800 | [diff] [blame] | 834 | int vb2_read_nv_storage_mosys(struct vb2_context *ctx) |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 835 | { |
Randall Spangler | a80a79f | 2018-02-26 17:01:24 -0800 | [diff] [blame] | 836 | /* Reserve extra 32 bytes */ |
| 837 | char hexstring[VB2_NVDATA_SIZE_V2 * 2 + 32]; |
| 838 | /* |
| 839 | * TODO(rspangler): mosys doesn't know how to read anything but 16-byte |
| 840 | * records yet. When it grows a command line option to do that, call |
| 841 | * it here when needed. |
| 842 | * |
| 843 | * It's possible mosys won't need that. For example, if if examines |
| 844 | * the header byte to determine the records size, or if it calls back |
| 845 | * to crossystem to read the VBSD flag. |
| 846 | */ |
Julius Werner | 52fa8c1 | 2019-05-07 12:59:47 -0700 | [diff] [blame] | 847 | const char * const argv[] = { |
Jack Rosenthal | fde6a34 | 2019-12-03 14:53:27 -0700 | [diff] [blame] | 848 | MOSYS_PATH, "nvram", "vboot", "read", NULL |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 849 | }; |
| 850 | char hexdigit[3]; |
Randall Spangler | a80a79f | 2018-02-26 17:01:24 -0800 | [diff] [blame] | 851 | const int nvsize = vb2_nv_get_size(ctx); |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 852 | int i; |
Duncan Laurie | e98431f | 2016-03-30 09:35:20 -0700 | [diff] [blame] | 853 | |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 854 | if (ExecuteMosys(argv, hexstring, sizeof(hexstring))) |
| 855 | return -1; |
Randall Spangler | 4c84e07 | 2018-03-11 09:56:06 -0700 | [diff] [blame] | 856 | if (strlen(hexstring) < 2 * nvsize) { |
Randall Spangler | a80a79f | 2018-02-26 17:01:24 -0800 | [diff] [blame] | 857 | fprintf(stderr, "mosys returned hex nvdata size %d" |
Randall Spangler | 4c84e07 | 2018-03-11 09:56:06 -0700 | [diff] [blame] | 858 | " (need %d)\n", (int)strlen(hexstring), 2 * nvsize); |
Randall Spangler | a80a79f | 2018-02-26 17:01:24 -0800 | [diff] [blame] | 859 | return -1; |
| 860 | } |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 861 | hexdigit[2] = '\0'; |
Randall Spangler | a80a79f | 2018-02-26 17:01:24 -0800 | [diff] [blame] | 862 | for (i = 0; i < nvsize; i++) { |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 863 | hexdigit[0] = hexstring[i * 2]; |
| 864 | hexdigit[1] = hexstring[i * 2 + 1]; |
Randall Spangler | dff5852 | 2017-11-27 15:37:13 -0800 | [diff] [blame] | 865 | ctx->nvdata[i] = strtol(hexdigit, NULL, 16); |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 866 | } |
| 867 | return 0; |
Duncan Laurie | e98431f | 2016-03-30 09:35:20 -0700 | [diff] [blame] | 868 | } |
| 869 | |
Randall Spangler | dff5852 | 2017-11-27 15:37:13 -0800 | [diff] [blame] | 870 | int vb2_write_nv_storage_mosys(struct vb2_context *ctx) |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 871 | { |
Randall Spangler | a80a79f | 2018-02-26 17:01:24 -0800 | [diff] [blame] | 872 | char hexstring[VB2_NVDATA_SIZE_V2 * 2 + 1]; |
Julius Werner | 52fa8c1 | 2019-05-07 12:59:47 -0700 | [diff] [blame] | 873 | const char * const argv[] = { |
Jack Rosenthal | fde6a34 | 2019-12-03 14:53:27 -0700 | [diff] [blame] | 874 | MOSYS_PATH, "nvram", "vboot", "write", hexstring, NULL |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 875 | }; |
Randall Spangler | a80a79f | 2018-02-26 17:01:24 -0800 | [diff] [blame] | 876 | const int nvsize = vb2_nv_get_size(ctx); |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 877 | int i; |
Duncan Laurie | e98431f | 2016-03-30 09:35:20 -0700 | [diff] [blame] | 878 | |
Randall Spangler | a80a79f | 2018-02-26 17:01:24 -0800 | [diff] [blame] | 879 | for (i = 0; i < nvsize; i++) |
Randall Spangler | dff5852 | 2017-11-27 15:37:13 -0800 | [diff] [blame] | 880 | snprintf(hexstring + i * 2, 3, "%02x", ctx->nvdata[i]); |
Randall Spangler | fb26715 | 2016-10-11 15:28:16 -0700 | [diff] [blame] | 881 | hexstring[sizeof(hexstring) - 1] = '\0'; |
| 882 | if (ExecuteMosys(argv, NULL, 0)) |
| 883 | return -1; |
| 884 | return 0; |
Duncan Laurie | e98431f | 2016-03-30 09:35:20 -0700 | [diff] [blame] | 885 | } |