blob: 2e29d04dd1a229d7b9dfa1c020c272ffd817565f [file] [log] [blame]
Randall Spangler54218662011-02-07 11:20:20 -08001/* Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
2 * Use of this source code is governed by a BSD-style license that can be
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 */
29 VDAT_STRING_LOAD_KERNEL_DEBUG /* LoadKernel() debug information */
Randall Spanglerf4ba19d2011-03-17 16:10:21 -070030} VdatStringField;
31
32
33/* Fields that GetVdatInt() can get */
34typedef enum VdatIntField {
Randall Spanglercabe6b32011-03-18 12:44:27 -070035 VDAT_INT_FLAGS = 0, /* Flags */
36 VDAT_INT_FW_VERSION_TPM, /* Current firmware version in TPM */
37 VDAT_INT_KERNEL_VERSION_TPM, /* Current kernel version in TPM */
38 VDAT_INT_TRIED_FIRMWARE_B, /* Tried firmware B due to fwb_tries */
39 VDAT_INT_KERNEL_KEY_VERIFIED /* Kernel key verified using
40 * signature, not just hash */
Randall Spanglerf4ba19d2011-03-17 16:10:21 -070041} VdatIntField;
42
43
Randall Spanglerc80fe652011-02-17 11:06:47 -080044/* Return true if the FWID starts with the specified string. */
Randall Spanglereb591952011-04-07 10:02:00 -070045int FwidStartsWith(const char *start) {
Randall Spanglerc80fe652011-02-17 11:06:47 -080046 char fwid[128];
47 if (!VbGetSystemPropertyString("fwid", fwid, sizeof(fwid)))
48 return 0;
49
50 return 0 == strncmp(fwid, start, strlen(start));
51}
52
53
Randall Spangler0f8ffb12011-02-25 09:50:54 -080054int VbGetNvStorage(VbNvParam param) {
Randall Spangler0f8ffb12011-02-25 09:50:54 -080055 VbNvContext vnc;
Randall Spangler0f8ffb12011-02-25 09:50:54 -080056 uint32_t value;
57 int retval;
58
Randall Spangler0f8ffb12011-02-25 09:50:54 -080059 /* TODO: locking around NV access */
Randall Spanglereb591952011-04-07 10:02:00 -070060
61 if (0 != VbReadNvStorage(&vnc))
Randall Spangler0f8ffb12011-02-25 09:50:54 -080062 return -1;
Randall Spangler0f8ffb12011-02-25 09:50:54 -080063 if (0 != VbNvSetup(&vnc))
64 return -1;
65 retval = VbNvGet(&vnc, param, &value);
66 if (0 != VbNvTeardown(&vnc))
67 return -1;
68 if (0 != retval)
69 return -1;
70
71 /* TODO: If vnc.raw_changed, attempt to reopen NVRAM for write and
72 * save the new defaults. If we're able to, log. */
73 /* TODO: release lock */
74
75 return (int)value;
76}
77
78
Randall Spangler0f8ffb12011-02-25 09:50:54 -080079int VbSetNvStorage(VbNvParam param, int value) {
Randall Spangler0f8ffb12011-02-25 09:50:54 -080080 VbNvContext vnc;
Randall Spangler0f8ffb12011-02-25 09:50:54 -080081 int retval = -1;
82 int i;
83
Randall Spanglereb591952011-04-07 10:02:00 -070084 if (0 != VbReadNvStorage(&vnc))
Randall Spangler0f8ffb12011-02-25 09:50:54 -080085 return -1;
Randall Spangler0f8ffb12011-02-25 09:50:54 -080086
87 if (0 != VbNvSetup(&vnc))
88 goto VbSetNvCleanup;
89 i = VbNvSet(&vnc, param, (uint32_t)value);
90 if (0 != VbNvTeardown(&vnc))
91 goto VbSetNvCleanup;
92 if (0 != i)
93 goto VbSetNvCleanup;
94
95 if (vnc.raw_changed) {
Randall Spanglereb591952011-04-07 10:02:00 -070096 if (0 != VbReadNvStorage(&vnc))
Randall Spangler0f8ffb12011-02-25 09:50:54 -080097 goto VbSetNvCleanup;
98 }
99
100 /* Success */
101 retval = 0;
102
103VbSetNvCleanup:
Randall Spangler0f8ffb12011-02-25 09:50:54 -0800104 /* TODO: release lock */
105 return retval;
106}
107
108
Randall Spangler196e1772011-03-10 11:31:06 -0800109/* Determine whether OS-level debugging should be allowed. Passed the
110 * destination and its size. Returns 1 if yes, 0 if no, -1 if error. */
111int VbGetCrosDebug(void) {
112 FILE* f = NULL;
113 char buf[4096] = "";
Randall Spangler196e1772011-03-10 11:31:06 -0800114 char *t, *saveptr;
115
Randall Spanglereb591952011-04-07 10:02:00 -0700116 /* Try reading firmware type. */
117 if (VbGetArchPropertyString("mainfw_type", buf, sizeof(buf))) {
118 if (0 == strcmp(buf, "recovery"))
119 return 0; /* Recovery mode never allows debug. */
120 else if (0 == strcmp(buf, "developer"))
121 return 1; /* Developer firmware always allows debug. */
122 }
Randall Spangler196e1772011-03-10 11:31:06 -0800123
124 /* Normal new firmware, older ChromeOS firmware, or non-Chrome firmware.
Randall Spangler227f7922011-03-11 13:34:56 -0800125 * For all these cases, check /proc/cmdline for cros_[no]debug. */
Randall Spangler196e1772011-03-10 11:31:06 -0800126 f = fopen(KERNEL_CMDLINE_PATH, "rt");
127 if (f) {
128 if (NULL == fgets(buf, sizeof(buf), f))
129 *buf = 0;
130 fclose(f);
131 }
132 for (t = strtok_r(buf, " ", &saveptr); t; t=strtok_r(NULL, " ", &saveptr)) {
133 if (0 == strcmp(t, "cros_debug"))
134 return 1;
Randall Spangler227f7922011-03-11 13:34:56 -0800135 else if (0 == strcmp(t, "cros_nodebug"))
136 return 0;
Randall Spangler196e1772011-03-10 11:31:06 -0800137 }
138
139 /* Normal new firmware or older Chrome OS firmware allows debug if the
140 * dev switch is on. */
Randall Spanglereb591952011-04-07 10:02:00 -0700141 if (1 == VbGetSystemPropertyInt("devsw_boot"))
Randall Spangler196e1772011-03-10 11:31:06 -0800142 return 1;
143
144 /* All other cases disallow debug. */
145 return 0;
146}
147
Randall Spanglerb47ed5a2011-02-23 13:05:40 -0800148
Randall Spangler71415712011-03-21 11:04:50 -0700149char* GetVdatLoadFirmwareDebug(char* dest, int size,
150 const VbSharedDataHeader* sh) {
151 snprintf(dest, size,
152 "Check A result=%d\n"
153 "Check B result=%d\n"
154 "Firmware index booted=0x%02x\n"
155 "TPM combined version at start=0x%08x\n"
156 "Lowest combined version from firmware=0x%08x\n",
157 sh->check_fw_a_result,
158 sh->check_fw_b_result,
159 sh->firmware_index,
160 sh->fw_version_tpm_start,
161 sh->fw_version_lowest);
162 return dest;
163}
164
165
166#define TRUNCATED "\n(truncated)\n"
167
168char* GetVdatLoadKernelDebug(char* dest, int size,
169 const VbSharedDataHeader* sh) {
170 int used = 0;
171 int first_call_tracked = 0;
172 int call;
173
174 /* Make sure we have space for truncation warning */
175 if (size < strlen(TRUNCATED) + 1)
176 return NULL;
177 size -= strlen(TRUNCATED) + 1;
178
179 used += snprintf(
180 dest + used, size - used,
181 "Calls to LoadKernel()=%d\n",
182 sh->lk_call_count);
183 if (used > size)
184 goto LoadKernelDebugExit;
185
186 /* Report on the last calls */
187 if (sh->lk_call_count > VBSD_MAX_KERNEL_CALLS)
188 first_call_tracked = sh->lk_call_count - VBSD_MAX_KERNEL_CALLS;
189 for (call = first_call_tracked; call < sh->lk_call_count; call++) {
190 const VbSharedDataKernelCall* shc =
191 sh->lk_calls + (call & (VBSD_MAX_KERNEL_CALLS - 1));
192 int first_part_tracked = 0;
193 int part;
194
195 used += snprintf(
196 dest + used, size - used,
197 "Call %d:\n"
198 " Boot flags=0x%02x\n"
199 " Boot mode=%d\n"
200 " Test error=%d\n"
201 " Return code=%d\n"
202 " Debug flags=0x%02x\n"
203 " Drive sectors=%" PRIu64 "\n"
204 " Sector size=%d\n"
205 " Check result=%d\n"
206 " Kernel partitions found=%d\n",
207 call + 1,
208 shc->boot_flags,
209 shc->boot_mode,
210 shc->test_error_num,
211 shc->return_code,
212 shc->flags,
213 shc->sector_count,
214 shc->sector_size,
215 shc->check_result,
216 shc->kernel_parts_found);
217 if (used > size)
218 goto LoadKernelDebugExit;
219
220 /* If we found too many partitions, only prints ones where the
221 * structure has info. */
222 if (shc->kernel_parts_found > VBSD_MAX_KERNEL_PARTS)
223 first_part_tracked = shc->kernel_parts_found - VBSD_MAX_KERNEL_PARTS;
224
225 /* Report on the partitions checked */
226 for (part = first_part_tracked; part < shc->kernel_parts_found; part++) {
227 const VbSharedDataKernelPart* shp =
228 shc->parts + (part & (VBSD_MAX_KERNEL_PARTS - 1));
229
230 used += snprintf(
231 dest + used, size - used,
232 " Kernel %d:\n"
233 " GPT index=%d\n"
234 " Start sector=%" PRIu64 "\n"
235 " Sector count=%" PRIu64 "\n"
236 " Combined version=0x%08x\n"
237 " Check result=%d\n"
238 " Debug flags=0x%02x\n",
239 part + 1,
240 shp->gpt_index,
241 shp->sector_start,
242 shp->sector_count,
243 shp->combined_version,
244 shp->check_result,
245 shp->flags);
246 if (used > size)
247 goto LoadKernelDebugExit;
248 }
249 }
250
251LoadKernelDebugExit:
252
253 /* Warn if data was truncated; we left space for this above. */
254 if (used > size)
255 strcat(dest, TRUNCATED);
256
257 return dest;
258}
259
260
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700261char* GetVdatString(char* dest, int size, VdatStringField field)
262{
Randall Spanglereb591952011-04-07 10:02:00 -0700263 VbSharedDataHeader* sh = VbSharedDataRead();
Randall Spangler71415712011-03-21 11:04:50 -0700264 char* value = dest;
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700265
Randall Spanglereb591952011-04-07 10:02:00 -0700266 if (!sh)
267 return NULL;
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700268
269 switch (field) {
270 case VDAT_STRING_TIMERS:
271 snprintf(dest, size,
272 "LFS=%" PRIu64 ",%" PRIu64
273 " LF=%" PRIu64 ",%" PRIu64
274 " LK=%" PRIu64 ",%" PRIu64,
275 sh->timer_load_firmware_start_enter,
276 sh->timer_load_firmware_start_exit,
277 sh->timer_load_firmware_enter,
278 sh->timer_load_firmware_exit,
279 sh->timer_load_kernel_enter,
280 sh->timer_load_kernel_exit);
281 break;
282
283 case VDAT_STRING_LOAD_FIRMWARE_DEBUG:
Randall Spangler71415712011-03-21 11:04:50 -0700284 value = GetVdatLoadFirmwareDebug(dest, size, sh);
285 break;
286
287 case VDAT_STRING_LOAD_KERNEL_DEBUG:
288 value = GetVdatLoadKernelDebug(dest, size, sh);
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700289 break;
290
291 default:
Randall Spanglereb591952011-04-07 10:02:00 -0700292 value = NULL;
293 break;
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700294 }
295
Randall Spanglereb591952011-04-07 10:02:00 -0700296 Free(sh);
Randall Spangler71415712011-03-21 11:04:50 -0700297 return value;
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700298}
299
300
301int GetVdatInt(VdatIntField field) {
Randall Spanglereb591952011-04-07 10:02:00 -0700302 VbSharedDataHeader* sh = VbSharedDataRead();
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700303 int value = -1;
304
Randall Spanglereb591952011-04-07 10:02:00 -0700305 if (!sh)
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700306 return -1;
307
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700308 switch (field) {
309 case VDAT_INT_FLAGS:
310 value = (int)sh->flags;
311 break;
Randall Spangler5ac39bf2011-03-17 17:58:56 -0700312 case VDAT_INT_FW_VERSION_TPM:
313 value = (int)sh->fw_version_tpm;
314 break;
315 case VDAT_INT_KERNEL_VERSION_TPM:
316 value = (int)sh->kernel_version_tpm;
317 break;
Randall Spanglercabe6b32011-03-18 12:44:27 -0700318 case VDAT_INT_TRIED_FIRMWARE_B:
319 value = (sh->flags & VBSD_FWB_TRIED ? 1 : 0);
320 break;
321 case VDAT_INT_KERNEL_KEY_VERIFIED:
322 value = (sh->flags & VBSD_KERNEL_KEY_VERIFIED ? 1 : 0);
323 break;
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700324 }
325
Randall Spanglereb591952011-04-07 10:02:00 -0700326 Free(sh);
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700327 return value;
328}
329
330
Randall Spangler54218662011-02-07 11:20:20 -0800331int VbGetSystemPropertyInt(const char* name) {
Randall Spanglerc80fe652011-02-17 11:06:47 -0800332 int value = -1;
Randall Spangler54218662011-02-07 11:20:20 -0800333
Randall Spanglereb591952011-04-07 10:02:00 -0700334 /* Check architecture-dependent properties first */
335 value = VbGetArchPropertyInt(name);
336 if (-1 != value)
337 return value;
338
339 /* NV storage values */
Randall Spanglercabe6b32011-03-18 12:44:27 -0700340 else if (!strcasecmp(name,"kern_nv")) {
Randall Spangler618d17d2011-03-01 10:33:11 -0800341 value = VbGetNvStorage(VBNV_KERNEL_FIELD);
Randall Spanglerb4167142011-03-01 13:04:22 -0800342 } else if (!strcasecmp(name,"nvram_cleared")) {
343 value = VbGetNvStorage(VBNV_KERNEL_SETTINGS_RESET);
Randall Spanglerb17e8d32011-03-15 09:50:38 -0700344 } else if (!strcasecmp(name,"vbtest_errfunc")) {
345 value = VbGetNvStorage(VBNV_TEST_ERROR_FUNC);
346 } else if (!strcasecmp(name,"vbtest_errno")) {
347 value = VbGetNvStorage(VBNV_TEST_ERROR_NUM);
Randall Spanglereb591952011-04-07 10:02:00 -0700348 } else if (!strcasecmp(name,"recovery_request")) {
Randall Spangler0f8ffb12011-02-25 09:50:54 -0800349 value = VbGetNvStorage(VBNV_RECOVERY_REQUEST);
Randall Spanglere73302c2011-02-18 14:53:01 -0800350 } else if (!strcasecmp(name,"dbg_reset")) {
Randall Spangler0f8ffb12011-02-25 09:50:54 -0800351 value = VbGetNvStorage(VBNV_DEBUG_RESET_MODE);
Randall Spanglere73302c2011-02-18 14:53:01 -0800352 } else if (!strcasecmp(name,"fwb_tries")) {
Randall Spangler0f8ffb12011-02-25 09:50:54 -0800353 value = VbGetNvStorage(VBNV_TRY_B_COUNT);
Randall Spanglere73302c2011-02-18 14:53:01 -0800354 }
Randall Spanglerb47ed5a2011-02-23 13:05:40 -0800355 /* Other parameters */
Randall Spanglereb591952011-04-07 10:02:00 -0700356 else if (!strcasecmp(name,"cros_debug")) {
Randall Spangler196e1772011-03-10 11:31:06 -0800357 value = VbGetCrosDebug();
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700358 } else if (!strcasecmp(name,"vdat_flags")) {
359 value = GetVdatInt(VDAT_INT_FLAGS);
Randall Spangler5ac39bf2011-03-17 17:58:56 -0700360 } else if (!strcasecmp(name,"tpm_fwver")) {
361 value = GetVdatInt(VDAT_INT_FW_VERSION_TPM);
362 } else if (!strcasecmp(name,"tpm_kernver")) {
363 value = GetVdatInt(VDAT_INT_KERNEL_VERSION_TPM);
Randall Spanglercabe6b32011-03-18 12:44:27 -0700364 } else if (!strcasecmp(name,"tried_fwb")) {
365 value = GetVdatInt(VDAT_INT_TRIED_FIRMWARE_B);
Randall Spanglerb47ed5a2011-02-23 13:05:40 -0800366 }
Randall Spangler54218662011-02-07 11:20:20 -0800367
Randall Spanglerc80fe652011-02-17 11:06:47 -0800368 return value;
Randall Spangler54218662011-02-07 11:20:20 -0800369}
370
Randall Spangler54218662011-02-07 11:20:20 -0800371
Randall Spanglereb591952011-04-07 10:02:00 -0700372const char* VbGetSystemPropertyString(const char* name, char* dest, int size) {
373 /* Check architecture-dependent properties first */
374 if (VbGetArchPropertyString(name, dest, size))
375 return dest;
376
377 if (!strcasecmp(name,"kernkey_vfy")) {
Randall Spanglercabe6b32011-03-18 12:44:27 -0700378 switch(GetVdatInt(VDAT_INT_KERNEL_KEY_VERIFIED)) {
Randall Spangler17260282011-02-25 12:06:26 -0800379 case 0:
380 return "hash";
381 case 1:
382 return "sig";
383 default:
384 return NULL;
385 }
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700386 } else if (!strcasecmp(name, "vdat_timers")) {
387 return GetVdatString(dest, size, VDAT_STRING_TIMERS);
388 } else if (!strcasecmp(name, "vdat_lfdebug")) {
389 return GetVdatString(dest, size, VDAT_STRING_LOAD_FIRMWARE_DEBUG);
Randall Spangler71415712011-03-21 11:04:50 -0700390 } else if (!strcasecmp(name, "vdat_lkdebug")) {
391 return GetVdatString(dest, size, VDAT_STRING_LOAD_KERNEL_DEBUG);
Randall Spanglereb591952011-04-07 10:02:00 -0700392 }
393
394 return NULL;
Randall Spangler54218662011-02-07 11:20:20 -0800395}
396
397
Randall Spangler54218662011-02-07 11:20:20 -0800398int VbSetSystemPropertyInt(const char* name, int value) {
Randall Spanglereb591952011-04-07 10:02:00 -0700399 /* Check architecture-dependent properties first */
400 if (0 == VbSetArchPropertyInt(name, value))
401 return 0;
Randall Spangler54218662011-02-07 11:20:20 -0800402
Randall Spanglereb591952011-04-07 10:02:00 -0700403 /* NV storage values */
Randall Spanglerb4167142011-03-01 13:04:22 -0800404 if (!strcasecmp(name,"nvram_cleared")) {
405 /* Can only clear this flag; it's set inside the NV storage library. */
406 return VbSetNvStorage(VBNV_KERNEL_SETTINGS_RESET, 0);
407 } else if (!strcasecmp(name,"kern_nv")) {
Randall Spangler618d17d2011-03-01 10:33:11 -0800408 return VbSetNvStorage(VBNV_KERNEL_FIELD, value);
Randall Spanglerb17e8d32011-03-15 09:50:38 -0700409 } else if (!strcasecmp(name,"vbtest_errfunc")) {
410 return VbSetNvStorage(VBNV_TEST_ERROR_FUNC, value);
411 } else if (!strcasecmp(name,"vbtest_errno")) {
412 return VbSetNvStorage(VBNV_TEST_ERROR_NUM, value);
Randall Spanglereb591952011-04-07 10:02:00 -0700413 } else if (!strcasecmp(name,"recovery_request")) {
414 return VbSetNvStorage(VBNV_RECOVERY_REQUEST, value);
Randall Spanglere73302c2011-02-18 14:53:01 -0800415 } else if (!strcasecmp(name,"dbg_reset")) {
Randall Spanglereb591952011-04-07 10:02:00 -0700416 return VbSetNvStorage(VBNV_DEBUG_RESET_MODE, value);
Randall Spanglere73302c2011-02-18 14:53:01 -0800417 } else if (!strcasecmp(name,"fwb_tries")) {
Randall Spanglereb591952011-04-07 10:02:00 -0700418 return VbSetNvStorage(VBNV_TRY_B_COUNT, value);
Randall Spanglere73302c2011-02-18 14:53:01 -0800419 }
420
Randall Spangler54218662011-02-07 11:20:20 -0800421 return -1;
422}
423
424
Randall Spangler54218662011-02-07 11:20:20 -0800425int VbSetSystemPropertyString(const char* name, const char* value) {
Randall Spanglereb591952011-04-07 10:02:00 -0700426 /* Chain to architecture-dependent properties */
427 return VbSetArchPropertyString(name, value);
Randall Spangler54218662011-02-07 11:20:20 -0800428}