mb/google/nissa/var/rull: add ssd timing and modify ssd GPIO pins of rtd3
[coreboot2.git] / src / security / vboot / vbnv.c
blob2ddb89803de920f419085a4837bc1a1e79d0b927
1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #include <assert.h>
4 #include <string.h>
5 #include <types.h>
6 #include <security/vboot/misc.h>
7 #include <security/vboot/vbnv.h>
8 #include <security/vboot/vbnv_layout.h>
9 #include <vb2_api.h>
11 static bool vbnv_initialized;
13 /* Return CRC-8 of the data, using x^8 + x^2 + x + 1 polynomial. */
14 static uint8_t crc8_vbnv(const uint8_t *data, int len)
16 unsigned int crc = 0;
17 int i, j;
19 for (j = len; j; j--, data++) {
20 crc ^= (*data << 8);
21 for (i = 8; i; i--) {
22 if (crc & 0x8000)
23 crc ^= (0x1070 << 3);
24 crc <<= 1;
28 return (uint8_t)(crc >> 8);
31 void vbnv_reset(uint8_t *vbnv_copy)
33 memset(vbnv_copy, 0, VBOOT_VBNV_BLOCK_SIZE);
36 /* Verify VBNV header and checksum. */
37 int verify_vbnv(uint8_t *vbnv_copy)
39 return (HEADER_SIGNATURE == (vbnv_copy[HEADER_OFFSET] & HEADER_MASK)) &&
40 (crc8_vbnv(vbnv_copy, CRC_OFFSET) == vbnv_copy[CRC_OFFSET]);
43 /* Re-generate VBNV checksum. */
44 void regen_vbnv_crc(uint8_t *vbnv_copy)
46 vbnv_copy[CRC_OFFSET] = crc8_vbnv(vbnv_copy, CRC_OFFSET);
50 * Read VBNV data from configured storage backend.
51 * If VBNV verification fails, reset the vbnv copy.
53 void read_vbnv(uint8_t *vbnv_copy)
55 if (CONFIG(VBOOT_VBNV_CMOS))
56 read_vbnv_cmos(vbnv_copy);
57 else if (CONFIG(VBOOT_VBNV_FLASH))
58 read_vbnv_flash(vbnv_copy);
59 else
60 dead_code();
62 /* Check data for consistency */
63 if (!verify_vbnv(vbnv_copy))
64 vbnv_reset(vbnv_copy);
68 * Write VBNV data to configured storage backend.
69 * This assumes that the caller has updated the CRC already.
71 void save_vbnv(const uint8_t *vbnv_copy)
73 if (CONFIG(VBOOT_VBNV_CMOS))
74 save_vbnv_cmos(vbnv_copy);
75 else if (CONFIG(VBOOT_VBNV_FLASH))
76 save_vbnv_flash(vbnv_copy);
77 else
78 dead_code();
81 /* Read the USB Device Controller(UDC) enable flag from VBNV. */
82 int vbnv_udc_enable_flag(void)
84 struct vb2_context *ctx = vboot_get_context();
85 return (ctx->nvdata[DEV_FLAGS_OFFSET] & DEV_ENABLE_UDC) ? 1 : 0;
88 void vbnv_init(void)
90 struct vb2_context *ctx;
92 /* NV data already initialized and read */
93 if (vbnv_initialized)
94 return;
96 ctx = vboot_get_context();
97 if (CONFIG(VBOOT_VBNV_CMOS))
98 vbnv_init_cmos(ctx->nvdata);
99 read_vbnv(ctx->nvdata);
100 vbnv_initialized = true;