1 /* SPDX-License-Identifier: GPL-2.0-only */
6 #include <security/vboot/misc.h>
7 #include <security/vboot/vbnv.h>
8 #include <security/vboot/vbnv_layout.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
)
19 for (j
= len
; j
; j
--, data
++) {
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
);
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
);
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;
90 struct vb2_context
*ctx
;
92 /* NV data already initialized and read */
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;