summaryrefslogtreecommitdiff
path: root/board/cr50/wp.c
blob: 54dc17318caa2c233a7f3b064bb1e3135388aaa4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
/* Copyright 2016 The ChromiumOS Authors
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "ccd_config.h"
#include "console.h"
#include "crc8.h"
#include "ec_commands.h"
#include "extension.h"
#include "flash_log.h"
#include "gpio.h"
#include "hooks.h"
#include "registers.h"
#include "scratch_reg1.h"
#include "system.h"
#include "system_chip.h"
#include "tpm_nvmem.h"
#include "tpm_nvmem_ops.h"
#include "tpm_registers.h"
#include "util.h"

#define CPRINTS(format, args...) cprints(CC_RBOX, format, ## args)
#define CPRINTF(format, args...) cprintf(CC_RBOX, format, ## args)

enum fwmp_controlled_action_t {
	CCD_UNLOCK,
	BOOT_POLICY_UPDATE,
};

uint8_t bp_connect;
uint8_t bp_forced;
/**
 * Return non-zero if battery is present
 */
int board_battery_is_present(void)
{
	/* Invert because battery-present signal is active low */
	return bp_forced ? bp_connect : !gpio_get_level(GPIO_BATT_PRES_L);
}

/**
 * Return non-zero if the wp state is being overridden.
 */
static int board_forcing_wp(void)
{
	return GREG32(PMU, LONG_LIFE_SCRATCH1) & BOARD_FORCING_WP;
}

/**
 * Set the current write protect state in RBOX and long life scratch register.
 *
 * @param asserted: 0 to disable write protect, otherwise enable write protect.
 */
static void set_wp_state(int asserted)
{
	/* Enable writing to the long life register */
	GWRITE_FIELD(PMU, LONG_LIFE_SCRATCH_WR_EN, REG1, 1);

	if (asserted) {
		GREG32(PMU, LONG_LIFE_SCRATCH1) |= BOARD_WP_ASSERTED;
		GREG32(RBOX, EC_WP_L) = 0;
	} else {
		GREG32(PMU, LONG_LIFE_SCRATCH1) &= ~BOARD_WP_ASSERTED;
		GREG32(RBOX, EC_WP_L) = 1;
	}

	/* Disable writing to the long life register */
	GWRITE_FIELD(PMU, LONG_LIFE_SCRATCH_WR_EN, REG1, 0);
}

/**
 * Return the current WP state
 *
 * @return 0 if WP deasserted, 1 if WP asserted
 */
int wp_is_asserted(void)
{
	/* Signal is active low, so invert */
	return !GREG32(RBOX, EC_WP_L);
}

static void check_wp_battery_presence(void)
{
	int bp = board_battery_is_present();

	/* If we're forcing WP, ignore battery detect */
	if (board_forcing_wp())
		return;

	/* Otherwise, mirror battery */
	if (bp != wp_is_asserted()) {
		CPRINTS("WP %d", bp);
		set_wp_state(bp);
	}
}
DECLARE_HOOK(HOOK_SECOND, check_wp_battery_presence, HOOK_PRIO_DEFAULT);

/**
 * Force write protect state or follow battery presence.
 *
 * @param force: Force write protect to wp_en if non-zero, otherwise use battery
 *               presence as the source.
 * @param wp_en: 0: Deassert WP. 1: Assert WP.
 */
static void force_write_protect(int force, int wp_en)
{
	/* Enable writing to the long life register */
	GWRITE_FIELD(PMU, LONG_LIFE_SCRATCH_WR_EN, REG1, 1);

	if (force) {
		/* Force WP regardless of battery presence. */
		GREG32(PMU, LONG_LIFE_SCRATCH1) |= BOARD_FORCING_WP;
	} else {
		/* Stop forcing write protect. */
		GREG32(PMU, LONG_LIFE_SCRATCH1) &= ~BOARD_FORCING_WP;
		/* Use battery presence as the value for write protect. */
		wp_en = board_battery_is_present();
	}

	/* Disable writing to the long life register */
	GWRITE_FIELD(PMU, LONG_LIFE_SCRATCH_WR_EN, REG1, 0);

	/* Update the WP state. */
	set_wp_state(wp_en);
}

#ifdef CONFIG_CMD_WP
static enum vendor_cmd_rc vc_set_wp(enum vendor_cmd_cc code,
				    void *buf,
				    size_t input_size,
				    size_t *response_size)
{
	uint8_t response = 0;

	*response_size = 0;
	/* There shouldn't be any args */
	if (input_size > 1)
		return VENDOR_RC_BOGUS_ARGS;

	if (input_size == 1) {
		uint8_t *cmd = buf;

		if (*cmd != WP_ENABLE)
			return VENDOR_RC_BOGUS_ARGS;

		set_wp_state(1);
	}

	/* Get current wp settings */
	if (board_forcing_wp())
		response |= WPV_FORCE;
	if (wp_is_asserted())
		response |= WPV_ENABLE;
	/* Get atboot wp settings */
	if (ccd_get_flag(CCD_FLAG_OVERRIDE_WP_AT_BOOT)) {
		response |= WPV_ATBOOT_SET;
		if (ccd_get_flag(CCD_FLAG_OVERRIDE_WP_STATE_ENABLED))
			response |= WPV_ATBOOT_ENABLE;
	}
	((uint8_t *)buf)[0] = response;
	*response_size = sizeof(response);
	return VENDOR_RC_SUCCESS;
}
DECLARE_VENDOR_COMMAND(VENDOR_CC_WP, vc_set_wp);

static int command_bpforce(int argc, char **argv)
{
	int val = 1;
	int forced = 1;

	if (argc > 1) {
		/* Make sure we're allowed to override battery presence */
		if (!ccd_is_cap_enabled(CCD_CAP_OVERRIDE_BATT_STATE))
			return EC_ERROR_ACCESS_DENIED;

		/* Update BP */
		if (!strncasecmp(argv[1], "follow", 6))
			forced = 0;
		else if (!strncasecmp(argv[1], "dis", 3))
			val = 0;
		else if (strncasecmp(argv[1], "con", 3))
			return EC_ERROR_PARAM2;

		bp_forced = forced;
		bp_connect = val;

		if (argc > 2 && !strcasecmp(argv[2], "atboot")) {
			/* Change override at boot to match */
			ccd_set_flag(CCD_FLAG_OVERRIDE_BATT_AT_BOOT, bp_forced);
			ccd_set_flag(CCD_FLAG_OVERRIDE_BATT_STATE_CONNECT,
				     bp_connect);
		}
		/* Update the WP state based on new battery presence setting */
		check_wp_battery_presence();
	}

	ccprintf("batt pres: %s%sconnect\n", bp_forced ? "forced " : "",
		 board_battery_is_present() ? "" : "dis");
	ccprintf("  at boot: ");
	if (ccd_get_flag(CCD_FLAG_OVERRIDE_BATT_AT_BOOT))
		ccprintf("forced %sconnect\n",
			 ccd_get_flag(CCD_FLAG_OVERRIDE_BATT_STATE_CONNECT) ? ""
			 : "dis");
	else
		ccprintf("follow_batt_pres\n");
	return EC_SUCCESS;
}
DECLARE_SAFE_CONSOLE_COMMAND(bpforce, command_bpforce,
			     "[connect|disconnect|follow_batt_pres [atboot]]",
			     "Get/set BATT_PRES_L signal override");

static int command_wp(int argc, char **argv)
{
	int val;
	int forced;

	if (argc > 1) {
		/* Make sure we're allowed to override WP settings */
		if (!ccd_is_cap_enabled(CCD_CAP_OVERRIDE_WP))
			return EC_ERROR_ACCESS_DENIED;

		/* Update WP */
		if (!strncasecmp(argv[1], "follow", 6))
			forced = 0;
		else if (parse_bool(argv[1], &val))
			forced = 1;
		else
			return EC_ERROR_PARAM1;

		force_write_protect(forced, val);

		if (argc > 2 && !strcasecmp(argv[2], "atboot")) {
			/* Change override at boot to match */
			ccd_set_flag(CCD_FLAG_OVERRIDE_WP_AT_BOOT, forced);
			ccd_set_flag(CCD_FLAG_OVERRIDE_WP_STATE_ENABLED, val);
		}
	}

	ccprintf("Flash WP: %s%sabled\n", board_forcing_wp() ? "forced " : "",
		 wp_is_asserted() ? "en" : "dis");
	ccprintf(" at boot: ");
	if (ccd_get_flag(CCD_FLAG_OVERRIDE_WP_AT_BOOT))
		ccprintf("forced %sabled\n",
			 ccd_get_flag(CCD_FLAG_OVERRIDE_WP_STATE_ENABLED)
			 ? "en" : "dis");
	else
		ccprintf("follow_batt_pres\n");

	return EC_SUCCESS;
}
DECLARE_SAFE_CONSOLE_COMMAND(wp, command_wp,
			     "[<BOOLEAN>/follow_batt_pres [atboot]]",
			     "Get/set the flash HW write-protect signal");
#endif /* CONFIG_CMD_WP */

void set_bp_follow_ccd_config(void)
{
	if (ccd_get_flag(CCD_FLAG_OVERRIDE_BATT_AT_BOOT)) {
		/* Reset to at-boot state specified by CCD */
		bp_forced = 1;
		bp_connect = ccd_get_flag(CCD_FLAG_OVERRIDE_BATT_STATE_CONNECT);
	} else {
		bp_forced = 0;
	}
}

static void set_wp_follow_ccd_config(void)
{
	if (ccd_get_flag(CCD_FLAG_OVERRIDE_WP_AT_BOOT)) {
		/* Reset to at-boot state specified by CCD */
		force_write_protect(1, ccd_get_flag
				    (CCD_FLAG_OVERRIDE_WP_STATE_ENABLED));
	} else {
		/* Reset to WP based on battery-present (val is ignored) */
		force_write_protect(0, 1);
	}
}

void board_wp_follow_ccd_config(void)
{
	/*
	 * Battery presence can be overidden using CCD. Get that setting before
	 * configuring write protect.
	 */
	set_bp_follow_ccd_config();

	/* Update write protect setting based on ccd config */
	set_wp_follow_ccd_config();
}

void init_wp_state(void)
{
	/*
	 * Battery presence can be overidden using CCD. Get that setting before
	 * configuring write protect.
	 */
	set_bp_follow_ccd_config();

	/* Check system reset flags after CCD config is initially loaded */
	if ((system_get_reset_flags() & EC_RESET_FLAG_HIBERNATE) &&
	    !system_rollback_detected()) {
		/*
		 * Deep sleep resume without rollback, so reload the WP state
		 * that was saved to the long-life registers before the deep
		 * sleep instead of going back to the at-boot default.
		 */
		if (board_forcing_wp()) {
			/* Temporarily forcing WP */
			set_wp_state(GREG32(PMU, LONG_LIFE_SCRATCH1) &
				     BOARD_WP_ASSERTED);
		} else {
			/* Write protected if battery is present */
			set_wp_state(board_battery_is_present());
		}
	} else {
		set_wp_follow_ccd_config();
	}
}

/**
 * Wipe the TPM
 *
 * @param reset_required: reset the system after wiping the TPM.
 *
 * @return EC_SUCCESS, or non-zero if error.
 */
int board_wipe_tpm(int reset_required)
{
	int rc;

	/* Wipe the TPM's memory and reset the TPM task. */
	rc = tpm_reset_request(1, 1);
	if (rc != EC_SUCCESS) {
		flash_log_add_event(FE_LOG_TPM_WIPE_ERROR, 0, NULL);
		/*
		 * If anything goes wrong (which is unlikely), we REALLY don't
		 * want to unlock the console. It's possible to fail without
		 * the TPM task ever running, so rebooting is probably our best
		 * bet for fixing the problem.
		 */
		CPRINTS("%s: Couldn't wipe nvmem! (rc %d)", __func__, rc);
		cflush();
		system_reset(SYSTEM_RESET_MANUALLY_TRIGGERED |
			     SYSTEM_RESET_HARD);

		/*
		 * That should never return, but if it did, reset the EC and
		 * through the error we got.
		 */
		board_reboot_ec();
		return rc;
	}

	/*
	 * TPM was wiped out successfully, let's prevent further communications
	 * from the AP until next reboot. The reboot will be triggered below if
	 * a reset is requested. If we aren't resetting the system now, the TPM
	 * will stay disabled until the user resets the system.
	 * This should be done as soon as possible after tpm_reset_request
	 * completes.
	 */
	tpm_stop();

	CPRINTS("TPM is erased");

	/* Tell the TPM task to re-enable NvMem commits. */
	tpm_reinstate_nvmem_commits();

	/*
	 * Use board_reboot_ec to ensure the system resets instead of
	 * deassert_ec_reset. Some boards don't reset immediately when EC_RST_L
	 * is asserted. board_reboot_ec will ensure the system has actually
	 * reset before releasing it. If the system has a normal reset scheme,
	 * EC reset will be released immediately.
	 */
	if (reset_required) {
		CPRINTS("%s: reset EC", __func__);
		board_reboot_ec();
	}
	return EC_SUCCESS;
}

/****************************************************************************/
/* Verified boot TPM NVRAM space support */

/*
 * These definitions and the structure layout were manually copied from
 * src/platform/vboot_reference/firmware/2lib/include/2secdata.h. at
 * git sha 38d7d1c.
 */
#define FWMP_HASH_SIZE		    32
#define FWMP_DEV_DISABLE_CCD_UNLOCK BIT(6)
#define FWMP_DEV_DISABLE_BOOT       BIT(0)
#define FIRMWARE_FLAG_DEV_MODE      0x02

struct RollbackSpaceFirmware {
	/* Struct version, for backwards compatibility */
	uint8_t struct_version;
	/* Flags (see FIRMWARE_FLAG_* above) */
	uint8_t flags;
	/* Firmware versions */
	uint32_t fw_versions;
	/* Reserved for future expansion */
	uint8_t reserved[3];
	/* Checksum (v2 and later only) */
	uint8_t crc8;
} __packed;

/* Firmware management parameters */
struct RollbackSpaceFwmp {
	/* CRC-8 of fields following struct_size */
	uint8_t crc;
	/* Structure size in bytes */
	uint8_t struct_size;
	/* Structure version */
	uint8_t struct_version;
	/* Reserved; ignored by current reader */
	uint8_t reserved0;
	/* Flags; see enum fwmp_flags */
	uint32_t flags;
	/* Hash of developer kernel key */
	uint8_t dev_key_hash[FWMP_HASH_SIZE];
} __packed;

#ifndef CR50_DEV
static int lock_enforced(const struct RollbackSpaceFwmp *fwmp,
			 enum fwmp_controlled_action_t action)
{
	uint8_t crc;

	/* Let's verify that the FWMP structure makes sense. */
	if (fwmp->struct_size != sizeof(*fwmp)) {
		CPRINTS("%s: fwmp size mismatch (%d)", __func__,
			fwmp->struct_size);
		return 1;
	}

	crc = crc8(&fwmp->struct_version, sizeof(struct RollbackSpaceFwmp) -
		   offsetof(struct RollbackSpaceFwmp, struct_version));
	if (fwmp->crc != crc) {
		CPRINTS("%s: fwmp crc mismatch", __func__);
		return 1;
	}

	switch (action) {
	case CCD_UNLOCK:
		return !!(fwmp->flags & FWMP_DEV_DISABLE_CCD_UNLOCK);
	case BOOT_POLICY_UPDATE:
		return !!(fwmp->flags & FWMP_DEV_DISABLE_BOOT);
	}
	return 0;
}
#endif

static int fwmp_allows(enum fwmp_controlled_action_t action)
{
#ifdef CR50_DEV
	return 1;
#else
	/* Let's see if FWMP allows the requested action. */
	struct RollbackSpaceFwmp fwmp;
	int allows;

	switch (read_tpm_nvmem(FWMP_NV_INDEX,
			       sizeof(struct RollbackSpaceFwmp), &fwmp)) {
	default:
		/* Something is messed up, let's not allow. */
		allows = 0;
		break;

	case TPM_READ_NOT_FOUND:
		allows = 1;
		break;

	case TPM_READ_SUCCESS:
		allows = !lock_enforced(&fwmp, action);
		break;
	}

	return allows;
#endif
}

int board_fwmp_allows_unlock(void)
{
	int allows_unlock = fwmp_allows(CCD_UNLOCK);
#ifndef CR50_DEV
	CPRINTS("Console unlock %sallowed", allows_unlock ? "" : "not ");
#endif
	return allows_unlock;
}

int board_fwmp_allows_boot_policy_update(void)
{
	return fwmp_allows(BOOT_POLICY_UPDATE);
}

void board_fwmp_update_policies(void)
{
#ifdef CR50_DEV
	CPRINTS("Update fwmp policies.");
#endif
}

int board_vboot_dev_mode_enabled(void)
{
	struct RollbackSpaceFirmware fw;

	if (TPM_READ_SUCCESS ==
	    read_tpm_nvmem(FIRMWARE_NV_INDEX, sizeof(fw), &fw)) {
		return !!(fw.flags & FIRMWARE_FLAG_DEV_MODE);
	}

	/* If not found or other error, assume dev mode is disabled */
	return 0;
}

/****************************************************************************/
/* TPM vendor-specific commands */

static enum vendor_cmd_rc vc_lock(enum vendor_cmd_cc code,
				  void *buf,
				  size_t input_size,
				  size_t *response_size)
{
	uint8_t *buffer = buf;

	if (code == VENDOR_CC_GET_LOCK) {
		/*
		 * Get the state of the console lock.
		 *
		 *   Args: none
		 *   Returns: one byte; true (locked) or false (unlocked)
		 */
		if (input_size != 0) {
			*response_size = 0;
			return VENDOR_RC_BOGUS_ARGS;
		}

		buffer[0] = console_is_restricted() ? 0x01 : 0x00;
		*response_size = 1;
		return VENDOR_RC_SUCCESS;
	}

	/* I have no idea what you're talking about */
	*response_size = 0;
	return VENDOR_RC_NO_SUCH_COMMAND;
}
DECLARE_VENDOR_COMMAND(VENDOR_CC_GET_LOCK, vc_lock);

/*
 * TODO(rspangler): The old concept of 'lock the console' really meant
 * something closer to 'reset CCD config', not the CCD V1 meaning of 'ccdlock'.
 * This command is no longer supported, so will fail.  It was defined this
 * way:
 *
 * DECLARE_VENDOR_COMMAND(VENDOR_CC_SET_LOCK, vc_lock);
 */