blob: 1c3fcc256a115827a375fa1bb1ccbcb368e9628e [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 Spanglerd7728232011-04-08 14:04:21 -070044/* Masks for kern_nv usage by kernel */
45#define KERN_NV_FWUPDATE_TRIES_MASK 0x0000000F
46
47
Randall Spanglerc80fe652011-02-17 11:06:47 -080048/* Return true if the FWID starts with the specified string. */
Randall Spanglereb591952011-04-07 10:02:00 -070049int FwidStartsWith(const char *start) {
Randall Spanglerc80fe652011-02-17 11:06:47 -080050 char fwid[128];
51 if (!VbGetSystemPropertyString("fwid", fwid, sizeof(fwid)))
52 return 0;
53
54 return 0 == strncmp(fwid, start, strlen(start));
55}
56
57
Randall Spangler0f8ffb12011-02-25 09:50:54 -080058int VbGetNvStorage(VbNvParam param) {
Randall Spangler0f8ffb12011-02-25 09:50:54 -080059 VbNvContext vnc;
Randall Spangler0f8ffb12011-02-25 09:50:54 -080060 uint32_t value;
61 int retval;
62
Randall Spangler0f8ffb12011-02-25 09:50:54 -080063 /* TODO: locking around NV access */
Randall Spanglereb591952011-04-07 10:02:00 -070064
65 if (0 != VbReadNvStorage(&vnc))
Randall Spangler0f8ffb12011-02-25 09:50:54 -080066 return -1;
Randall Spangler0f8ffb12011-02-25 09:50:54 -080067 if (0 != VbNvSetup(&vnc))
68 return -1;
69 retval = VbNvGet(&vnc, param, &value);
70 if (0 != VbNvTeardown(&vnc))
71 return -1;
72 if (0 != retval)
73 return -1;
74
75 /* TODO: If vnc.raw_changed, attempt to reopen NVRAM for write and
76 * save the new defaults. If we're able to, log. */
77 /* TODO: release lock */
78
79 return (int)value;
80}
81
82
Randall Spangler0f8ffb12011-02-25 09:50:54 -080083int VbSetNvStorage(VbNvParam param, int value) {
Randall Spangler0f8ffb12011-02-25 09:50:54 -080084 VbNvContext vnc;
Randall Spangler0f8ffb12011-02-25 09:50:54 -080085 int retval = -1;
86 int i;
87
Randall Spanglereb591952011-04-07 10:02:00 -070088 if (0 != VbReadNvStorage(&vnc))
Randall Spangler0f8ffb12011-02-25 09:50:54 -080089 return -1;
Randall Spangler0f8ffb12011-02-25 09:50:54 -080090
91 if (0 != VbNvSetup(&vnc))
92 goto VbSetNvCleanup;
93 i = VbNvSet(&vnc, param, (uint32_t)value);
94 if (0 != VbNvTeardown(&vnc))
95 goto VbSetNvCleanup;
96 if (0 != i)
97 goto VbSetNvCleanup;
98
99 if (vnc.raw_changed) {
Randall Spanglerd7728232011-04-08 14:04:21 -0700100 if (0 != VbWriteNvStorage(&vnc))
Randall Spangler0f8ffb12011-02-25 09:50:54 -0800101 goto VbSetNvCleanup;
102 }
103
104 /* Success */
105 retval = 0;
106
107VbSetNvCleanup:
Randall Spangler0f8ffb12011-02-25 09:50:54 -0800108 /* TODO: release lock */
109 return retval;
110}
111
112
Randall Spangler196e1772011-03-10 11:31:06 -0800113/* Determine whether OS-level debugging should be allowed. Passed the
114 * destination and its size. Returns 1 if yes, 0 if no, -1 if error. */
115int VbGetCrosDebug(void) {
116 FILE* f = NULL;
117 char buf[4096] = "";
Randall Spangler196e1772011-03-10 11:31:06 -0800118 char *t, *saveptr;
119
Randall Spanglereb591952011-04-07 10:02:00 -0700120 /* Try reading firmware type. */
121 if (VbGetArchPropertyString("mainfw_type", buf, sizeof(buf))) {
122 if (0 == strcmp(buf, "recovery"))
123 return 0; /* Recovery mode never allows debug. */
124 else if (0 == strcmp(buf, "developer"))
125 return 1; /* Developer firmware always allows debug. */
126 }
Randall Spangler196e1772011-03-10 11:31:06 -0800127
128 /* Normal new firmware, older ChromeOS firmware, or non-Chrome firmware.
Randall Spangler227f7922011-03-11 13:34:56 -0800129 * For all these cases, check /proc/cmdline for cros_[no]debug. */
Randall Spangler196e1772011-03-10 11:31:06 -0800130 f = fopen(KERNEL_CMDLINE_PATH, "rt");
131 if (f) {
132 if (NULL == fgets(buf, sizeof(buf), f))
133 *buf = 0;
134 fclose(f);
135 }
136 for (t = strtok_r(buf, " ", &saveptr); t; t=strtok_r(NULL, " ", &saveptr)) {
137 if (0 == strcmp(t, "cros_debug"))
138 return 1;
Randall Spangler227f7922011-03-11 13:34:56 -0800139 else if (0 == strcmp(t, "cros_nodebug"))
140 return 0;
Randall Spangler196e1772011-03-10 11:31:06 -0800141 }
142
143 /* Normal new firmware or older Chrome OS firmware allows debug if the
144 * dev switch is on. */
Randall Spanglereb591952011-04-07 10:02:00 -0700145 if (1 == VbGetSystemPropertyInt("devsw_boot"))
Randall Spangler196e1772011-03-10 11:31:06 -0800146 return 1;
147
148 /* All other cases disallow debug. */
149 return 0;
150}
151
Randall Spanglerb47ed5a2011-02-23 13:05:40 -0800152
Randall Spangler71415712011-03-21 11:04:50 -0700153char* GetVdatLoadFirmwareDebug(char* dest, int size,
154 const VbSharedDataHeader* sh) {
155 snprintf(dest, size,
156 "Check A result=%d\n"
157 "Check B result=%d\n"
158 "Firmware index booted=0x%02x\n"
159 "TPM combined version at start=0x%08x\n"
160 "Lowest combined version from firmware=0x%08x\n",
161 sh->check_fw_a_result,
162 sh->check_fw_b_result,
163 sh->firmware_index,
164 sh->fw_version_tpm_start,
165 sh->fw_version_lowest);
166 return dest;
167}
168
169
170#define TRUNCATED "\n(truncated)\n"
171
172char* GetVdatLoadKernelDebug(char* dest, int size,
173 const VbSharedDataHeader* sh) {
174 int used = 0;
175 int first_call_tracked = 0;
176 int call;
177
178 /* Make sure we have space for truncation warning */
179 if (size < strlen(TRUNCATED) + 1)
180 return NULL;
181 size -= strlen(TRUNCATED) + 1;
182
183 used += snprintf(
184 dest + used, size - used,
185 "Calls to LoadKernel()=%d\n",
186 sh->lk_call_count);
187 if (used > size)
188 goto LoadKernelDebugExit;
189
190 /* Report on the last calls */
191 if (sh->lk_call_count > VBSD_MAX_KERNEL_CALLS)
192 first_call_tracked = sh->lk_call_count - VBSD_MAX_KERNEL_CALLS;
193 for (call = first_call_tracked; call < sh->lk_call_count; call++) {
194 const VbSharedDataKernelCall* shc =
195 sh->lk_calls + (call & (VBSD_MAX_KERNEL_CALLS - 1));
196 int first_part_tracked = 0;
197 int part;
198
199 used += snprintf(
200 dest + used, size - used,
201 "Call %d:\n"
202 " Boot flags=0x%02x\n"
203 " Boot mode=%d\n"
204 " Test error=%d\n"
205 " Return code=%d\n"
206 " Debug flags=0x%02x\n"
207 " Drive sectors=%" PRIu64 "\n"
208 " Sector size=%d\n"
209 " Check result=%d\n"
210 " Kernel partitions found=%d\n",
211 call + 1,
212 shc->boot_flags,
213 shc->boot_mode,
214 shc->test_error_num,
215 shc->return_code,
216 shc->flags,
217 shc->sector_count,
218 shc->sector_size,
219 shc->check_result,
220 shc->kernel_parts_found);
221 if (used > size)
222 goto LoadKernelDebugExit;
223
224 /* If we found too many partitions, only prints ones where the
225 * structure has info. */
226 if (shc->kernel_parts_found > VBSD_MAX_KERNEL_PARTS)
227 first_part_tracked = shc->kernel_parts_found - VBSD_MAX_KERNEL_PARTS;
228
229 /* Report on the partitions checked */
230 for (part = first_part_tracked; part < shc->kernel_parts_found; part++) {
231 const VbSharedDataKernelPart* shp =
232 shc->parts + (part & (VBSD_MAX_KERNEL_PARTS - 1));
233
234 used += snprintf(
235 dest + used, size - used,
236 " Kernel %d:\n"
237 " GPT index=%d\n"
238 " Start sector=%" PRIu64 "\n"
239 " Sector count=%" PRIu64 "\n"
240 " Combined version=0x%08x\n"
241 " Check result=%d\n"
242 " Debug flags=0x%02x\n",
243 part + 1,
244 shp->gpt_index,
245 shp->sector_start,
246 shp->sector_count,
247 shp->combined_version,
248 shp->check_result,
249 shp->flags);
250 if (used > size)
251 goto LoadKernelDebugExit;
252 }
253 }
254
255LoadKernelDebugExit:
256
257 /* Warn if data was truncated; we left space for this above. */
258 if (used > size)
259 strcat(dest, TRUNCATED);
260
261 return dest;
262}
263
264
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700265char* GetVdatString(char* dest, int size, VdatStringField field)
266{
Randall Spanglereb591952011-04-07 10:02:00 -0700267 VbSharedDataHeader* sh = VbSharedDataRead();
Randall Spangler71415712011-03-21 11:04:50 -0700268 char* value = dest;
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700269
Randall Spanglereb591952011-04-07 10:02:00 -0700270 if (!sh)
271 return NULL;
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700272
273 switch (field) {
274 case VDAT_STRING_TIMERS:
275 snprintf(dest, size,
276 "LFS=%" PRIu64 ",%" PRIu64
277 " LF=%" PRIu64 ",%" PRIu64
278 " LK=%" PRIu64 ",%" PRIu64,
279 sh->timer_load_firmware_start_enter,
280 sh->timer_load_firmware_start_exit,
281 sh->timer_load_firmware_enter,
282 sh->timer_load_firmware_exit,
283 sh->timer_load_kernel_enter,
284 sh->timer_load_kernel_exit);
285 break;
286
287 case VDAT_STRING_LOAD_FIRMWARE_DEBUG:
Randall Spangler71415712011-03-21 11:04:50 -0700288 value = GetVdatLoadFirmwareDebug(dest, size, sh);
289 break;
290
291 case VDAT_STRING_LOAD_KERNEL_DEBUG:
292 value = GetVdatLoadKernelDebug(dest, size, sh);
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700293 break;
294
295 default:
Randall Spanglereb591952011-04-07 10:02:00 -0700296 value = NULL;
297 break;
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700298 }
299
Randall Spanglereb591952011-04-07 10:02:00 -0700300 Free(sh);
Randall Spangler71415712011-03-21 11:04:50 -0700301 return value;
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700302}
303
304
305int GetVdatInt(VdatIntField field) {
Randall Spanglereb591952011-04-07 10:02:00 -0700306 VbSharedDataHeader* sh = VbSharedDataRead();
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700307 int value = -1;
308
Randall Spanglereb591952011-04-07 10:02:00 -0700309 if (!sh)
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700310 return -1;
311
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700312 switch (field) {
313 case VDAT_INT_FLAGS:
314 value = (int)sh->flags;
315 break;
Randall Spangler5ac39bf2011-03-17 17:58:56 -0700316 case VDAT_INT_FW_VERSION_TPM:
317 value = (int)sh->fw_version_tpm;
318 break;
319 case VDAT_INT_KERNEL_VERSION_TPM:
320 value = (int)sh->kernel_version_tpm;
321 break;
Randall Spanglercabe6b32011-03-18 12:44:27 -0700322 case VDAT_INT_TRIED_FIRMWARE_B:
323 value = (sh->flags & VBSD_FWB_TRIED ? 1 : 0);
324 break;
325 case VDAT_INT_KERNEL_KEY_VERIFIED:
326 value = (sh->flags & VBSD_KERNEL_KEY_VERIFIED ? 1 : 0);
327 break;
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700328 }
329
Randall Spanglereb591952011-04-07 10:02:00 -0700330 Free(sh);
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700331 return value;
332}
333
334
Randall Spangler54218662011-02-07 11:20:20 -0800335int VbGetSystemPropertyInt(const char* name) {
Randall Spanglerc80fe652011-02-17 11:06:47 -0800336 int value = -1;
Randall Spangler54218662011-02-07 11:20:20 -0800337
Randall Spanglereb591952011-04-07 10:02:00 -0700338 /* Check architecture-dependent properties first */
339 value = VbGetArchPropertyInt(name);
340 if (-1 != value)
341 return value;
342
343 /* NV storage values */
Randall Spanglercabe6b32011-03-18 12:44:27 -0700344 else if (!strcasecmp(name,"kern_nv")) {
Randall Spangler618d17d2011-03-01 10:33:11 -0800345 value = VbGetNvStorage(VBNV_KERNEL_FIELD);
Randall Spanglerb4167142011-03-01 13:04:22 -0800346 } else if (!strcasecmp(name,"nvram_cleared")) {
347 value = VbGetNvStorage(VBNV_KERNEL_SETTINGS_RESET);
Randall Spanglerb17e8d32011-03-15 09:50:38 -0700348 } else if (!strcasecmp(name,"vbtest_errfunc")) {
349 value = VbGetNvStorage(VBNV_TEST_ERROR_FUNC);
350 } else if (!strcasecmp(name,"vbtest_errno")) {
351 value = VbGetNvStorage(VBNV_TEST_ERROR_NUM);
Randall Spanglereb591952011-04-07 10:02:00 -0700352 } else if (!strcasecmp(name,"recovery_request")) {
Randall Spangler0f8ffb12011-02-25 09:50:54 -0800353 value = VbGetNvStorage(VBNV_RECOVERY_REQUEST);
Randall Spanglere73302c2011-02-18 14:53:01 -0800354 } else if (!strcasecmp(name,"dbg_reset")) {
Randall Spangler0f8ffb12011-02-25 09:50:54 -0800355 value = VbGetNvStorage(VBNV_DEBUG_RESET_MODE);
Randall Spanglere73302c2011-02-18 14:53:01 -0800356 } else if (!strcasecmp(name,"fwb_tries")) {
Randall Spangler0f8ffb12011-02-25 09:50:54 -0800357 value = VbGetNvStorage(VBNV_TRY_B_COUNT);
Randall Spanglerd7728232011-04-08 14:04:21 -0700358 } else if (!strcasecmp(name,"fwupdate_tries")) {
359 value = VbGetNvStorage(VBNV_KERNEL_FIELD);
360 if (value != -1)
361 value &= KERN_NV_FWUPDATE_TRIES_MASK;
Randall Spanglere73302c2011-02-18 14:53:01 -0800362 }
Randall Spanglerb47ed5a2011-02-23 13:05:40 -0800363 /* Other parameters */
Randall Spanglereb591952011-04-07 10:02:00 -0700364 else if (!strcasecmp(name,"cros_debug")) {
Randall Spangler196e1772011-03-10 11:31:06 -0800365 value = VbGetCrosDebug();
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700366 } else if (!strcasecmp(name,"vdat_flags")) {
367 value = GetVdatInt(VDAT_INT_FLAGS);
Randall Spangler5ac39bf2011-03-17 17:58:56 -0700368 } else if (!strcasecmp(name,"tpm_fwver")) {
369 value = GetVdatInt(VDAT_INT_FW_VERSION_TPM);
370 } else if (!strcasecmp(name,"tpm_kernver")) {
371 value = GetVdatInt(VDAT_INT_KERNEL_VERSION_TPM);
Randall Spanglercabe6b32011-03-18 12:44:27 -0700372 } else if (!strcasecmp(name,"tried_fwb")) {
373 value = GetVdatInt(VDAT_INT_TRIED_FIRMWARE_B);
Randall Spanglerb47ed5a2011-02-23 13:05:40 -0800374 }
Randall Spangler54218662011-02-07 11:20:20 -0800375
Randall Spanglerc80fe652011-02-17 11:06:47 -0800376 return value;
Randall Spangler54218662011-02-07 11:20:20 -0800377}
378
Randall Spangler54218662011-02-07 11:20:20 -0800379
Randall Spanglereb591952011-04-07 10:02:00 -0700380const char* VbGetSystemPropertyString(const char* name, char* dest, int size) {
381 /* Check architecture-dependent properties first */
382 if (VbGetArchPropertyString(name, dest, size))
383 return dest;
384
385 if (!strcasecmp(name,"kernkey_vfy")) {
Randall Spanglercabe6b32011-03-18 12:44:27 -0700386 switch(GetVdatInt(VDAT_INT_KERNEL_KEY_VERIFIED)) {
Randall Spangler17260282011-02-25 12:06:26 -0800387 case 0:
388 return "hash";
389 case 1:
390 return "sig";
391 default:
392 return NULL;
393 }
Randall Spanglerf4ba19d2011-03-17 16:10:21 -0700394 } else if (!strcasecmp(name, "vdat_timers")) {
395 return GetVdatString(dest, size, VDAT_STRING_TIMERS);
396 } else if (!strcasecmp(name, "vdat_lfdebug")) {
397 return GetVdatString(dest, size, VDAT_STRING_LOAD_FIRMWARE_DEBUG);
Randall Spangler71415712011-03-21 11:04:50 -0700398 } else if (!strcasecmp(name, "vdat_lkdebug")) {
399 return GetVdatString(dest, size, VDAT_STRING_LOAD_KERNEL_DEBUG);
Randall Spanglereb591952011-04-07 10:02:00 -0700400 }
401
402 return NULL;
Randall Spangler54218662011-02-07 11:20:20 -0800403}
404
405
Randall Spangler54218662011-02-07 11:20:20 -0800406int VbSetSystemPropertyInt(const char* name, int value) {
Randall Spanglereb591952011-04-07 10:02:00 -0700407 /* Check architecture-dependent properties first */
Randall Spanglerd7728232011-04-08 14:04:21 -0700408
Randall Spanglereb591952011-04-07 10:02:00 -0700409 if (0 == VbSetArchPropertyInt(name, value))
410 return 0;
Randall Spangler54218662011-02-07 11:20:20 -0800411
Randall Spanglereb591952011-04-07 10:02:00 -0700412 /* NV storage values */
Randall Spanglerb4167142011-03-01 13:04:22 -0800413 if (!strcasecmp(name,"nvram_cleared")) {
414 /* Can only clear this flag; it's set inside the NV storage library. */
415 return VbSetNvStorage(VBNV_KERNEL_SETTINGS_RESET, 0);
416 } else if (!strcasecmp(name,"kern_nv")) {
Randall Spangler618d17d2011-03-01 10:33:11 -0800417 return VbSetNvStorage(VBNV_KERNEL_FIELD, value);
Randall Spanglerb17e8d32011-03-15 09:50:38 -0700418 } else if (!strcasecmp(name,"vbtest_errfunc")) {
419 return VbSetNvStorage(VBNV_TEST_ERROR_FUNC, value);
420 } else if (!strcasecmp(name,"vbtest_errno")) {
421 return VbSetNvStorage(VBNV_TEST_ERROR_NUM, value);
Randall Spanglereb591952011-04-07 10:02:00 -0700422 } else if (!strcasecmp(name,"recovery_request")) {
423 return VbSetNvStorage(VBNV_RECOVERY_REQUEST, value);
Randall Spanglere73302c2011-02-18 14:53:01 -0800424 } else if (!strcasecmp(name,"dbg_reset")) {
Randall Spanglereb591952011-04-07 10:02:00 -0700425 return VbSetNvStorage(VBNV_DEBUG_RESET_MODE, value);
Randall Spanglere73302c2011-02-18 14:53:01 -0800426 } else if (!strcasecmp(name,"fwb_tries")) {
Randall Spanglereb591952011-04-07 10:02:00 -0700427 return VbSetNvStorage(VBNV_TRY_B_COUNT, value);
Randall Spanglerd7728232011-04-08 14:04:21 -0700428 } else if (!strcasecmp(name,"fwupdate_tries")) {
429 int kern_nv = VbGetNvStorage(VBNV_KERNEL_FIELD);
430 if (kern_nv == -1)
431 return -1;
432 kern_nv &= ~KERN_NV_FWUPDATE_TRIES_MASK;
433 kern_nv |= (value & KERN_NV_FWUPDATE_TRIES_MASK);
434 return VbSetNvStorage(VBNV_KERNEL_FIELD, kern_nv);
Randall Spanglere73302c2011-02-18 14:53:01 -0800435 }
436
Randall Spangler54218662011-02-07 11:20:20 -0800437 return -1;
438}
439
440
Randall Spangler54218662011-02-07 11:20:20 -0800441int VbSetSystemPropertyString(const char* name, const char* value) {
Randall Spanglereb591952011-04-07 10:02:00 -0700442 /* Chain to architecture-dependent properties */
443 return VbSetArchPropertyString(name, value);
Randall Spangler54218662011-02-07 11:20:20 -0800444}