Angel Pons | 986d50e | 2020-04-02 23:48:53 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Duncan Laurie | 88b28ad | 2016-01-25 17:13:27 -0800 | [diff] [blame] | 2 | |
Yu-Ping Wu | 6b0d085 | 2022-06-08 10:23:32 +0800 | [diff] [blame] | 3 | #include <assert.h> |
Duncan Laurie | 88b28ad | 2016-01-25 17:13:27 -0800 | [diff] [blame] | 4 | #include <string.h> |
| 5 | #include <types.h> |
Philipp Deppenwiese | fea2429 | 2017-10-17 17:02:29 +0200 | [diff] [blame] | 6 | #include <security/vboot/vbnv.h> |
| 7 | #include <security/vboot/vbnv_layout.h> |
Duncan Laurie | 88b28ad | 2016-01-25 17:13:27 -0800 | [diff] [blame] | 8 | |
Arthur Heymans | 344e86b | 2019-11-20 19:47:10 +0100 | [diff] [blame] | 9 | static int vbnv_initialized; |
| 10 | static uint8_t vbnv[VBOOT_VBNV_BLOCK_SIZE]; |
Duncan Laurie | 88b28ad | 2016-01-25 17:13:27 -0800 | [diff] [blame] | 11 | |
| 12 | /* Return CRC-8 of the data, using x^8 + x^2 + x + 1 polynomial. */ |
| 13 | static uint8_t crc8_vbnv(const uint8_t *data, int len) |
| 14 | { |
Martin Roth | 468d02c | 2019-10-23 21:44:42 -0600 | [diff] [blame] | 15 | unsigned int crc = 0; |
Duncan Laurie | 88b28ad | 2016-01-25 17:13:27 -0800 | [diff] [blame] | 16 | int i, j; |
| 17 | |
| 18 | for (j = len; j; j--, data++) { |
| 19 | crc ^= (*data << 8); |
| 20 | for (i = 8; i; i--) { |
| 21 | if (crc & 0x8000) |
| 22 | crc ^= (0x1070 << 3); |
| 23 | crc <<= 1; |
| 24 | } |
| 25 | } |
| 26 | |
Elyes Haouas | b538d71 | 2022-11-18 15:03:07 +0100 | [diff] [blame^] | 27 | return (uint8_t)(crc >> 8); |
Duncan Laurie | 88b28ad | 2016-01-25 17:13:27 -0800 | [diff] [blame] | 28 | } |
| 29 | |
Aaron Durbin | fe265a1 | 2017-09-15 11:17:38 -0600 | [diff] [blame] | 30 | void vbnv_reset(uint8_t *vbnv_copy) |
Duncan Laurie | 88b28ad | 2016-01-25 17:13:27 -0800 | [diff] [blame] | 31 | { |
Furquan Shaikh | 2a12e2e | 2016-07-25 11:48:03 -0700 | [diff] [blame] | 32 | memset(vbnv_copy, 0, VBOOT_VBNV_BLOCK_SIZE); |
Duncan Laurie | 88b28ad | 2016-01-25 17:13:27 -0800 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | /* Read VBNV data into cache. */ |
| 36 | static void vbnv_setup(void) |
| 37 | { |
Arthur Heymans | 344e86b | 2019-11-20 19:47:10 +0100 | [diff] [blame] | 38 | if (!vbnv_initialized) { |
| 39 | read_vbnv(vbnv); |
| 40 | vbnv_initialized = 1; |
Duncan Laurie | 88b28ad | 2016-01-25 17:13:27 -0800 | [diff] [blame] | 41 | } |
| 42 | } |
| 43 | |
| 44 | /* Verify VBNV header and checksum. */ |
| 45 | int verify_vbnv(uint8_t *vbnv_copy) |
| 46 | { |
| 47 | return (HEADER_SIGNATURE == (vbnv_copy[HEADER_OFFSET] & HEADER_MASK)) && |
| 48 | (crc8_vbnv(vbnv_copy, CRC_OFFSET) == vbnv_copy[CRC_OFFSET]); |
| 49 | } |
| 50 | |
Hung-Te Lin | fe2fc83 | 2016-12-29 20:59:37 +0800 | [diff] [blame] | 51 | /* Re-generate VBNV checksum. */ |
| 52 | void regen_vbnv_crc(uint8_t *vbnv_copy) |
| 53 | { |
| 54 | vbnv_copy[CRC_OFFSET] = crc8_vbnv(vbnv_copy, CRC_OFFSET); |
| 55 | } |
| 56 | |
Furquan Shaikh | 165b6cf | 2016-06-27 16:19:09 -0700 | [diff] [blame] | 57 | /* |
| 58 | * Read VBNV data from configured storage backend. |
| 59 | * If VBNV verification fails, reset the vbnv copy. |
Furquan Shaikh | 165b6cf | 2016-06-27 16:19:09 -0700 | [diff] [blame] | 60 | */ |
Furquan Shaikh | be87f73 | 2016-06-29 11:26:27 -0700 | [diff] [blame] | 61 | void read_vbnv(uint8_t *vbnv_copy) |
Duncan Laurie | 88b28ad | 2016-01-25 17:13:27 -0800 | [diff] [blame] | 62 | { |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 63 | if (CONFIG(VBOOT_VBNV_CMOS)) |
Duncan Laurie | 88b28ad | 2016-01-25 17:13:27 -0800 | [diff] [blame] | 64 | read_vbnv_cmos(vbnv_copy); |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 65 | else if (CONFIG(VBOOT_VBNV_FLASH)) |
Duncan Laurie | 88b28ad | 2016-01-25 17:13:27 -0800 | [diff] [blame] | 66 | read_vbnv_flash(vbnv_copy); |
Yu-Ping Wu | 6b0d085 | 2022-06-08 10:23:32 +0800 | [diff] [blame] | 67 | else |
| 68 | dead_code(); |
Duncan Laurie | 88b28ad | 2016-01-25 17:13:27 -0800 | [diff] [blame] | 69 | |
| 70 | /* Check data for consistency */ |
Furquan Shaikh | be87f73 | 2016-06-29 11:26:27 -0700 | [diff] [blame] | 71 | if (!verify_vbnv(vbnv_copy)) |
Aaron Durbin | fe265a1 | 2017-09-15 11:17:38 -0600 | [diff] [blame] | 72 | vbnv_reset(vbnv_copy); |
Duncan Laurie | 88b28ad | 2016-01-25 17:13:27 -0800 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | /* |
| 76 | * Write VBNV data to configured storage backend. |
| 77 | * This assumes that the caller has updated the CRC already. |
| 78 | */ |
| 79 | void save_vbnv(const uint8_t *vbnv_copy) |
| 80 | { |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 81 | if (CONFIG(VBOOT_VBNV_CMOS)) |
Duncan Laurie | 88b28ad | 2016-01-25 17:13:27 -0800 | [diff] [blame] | 82 | save_vbnv_cmos(vbnv_copy); |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 83 | else if (CONFIG(VBOOT_VBNV_FLASH)) |
Duncan Laurie | 88b28ad | 2016-01-25 17:13:27 -0800 | [diff] [blame] | 84 | save_vbnv_flash(vbnv_copy); |
Yu-Ping Wu | 6b0d085 | 2022-06-08 10:23:32 +0800 | [diff] [blame] | 85 | else |
| 86 | dead_code(); |
Duncan Laurie | 88b28ad | 2016-01-25 17:13:27 -0800 | [diff] [blame] | 87 | |
| 88 | /* Clear initialized flag to force cached data to be updated */ |
Arthur Heymans | 344e86b | 2019-11-20 19:47:10 +0100 | [diff] [blame] | 89 | vbnv_initialized = 0; |
Duncan Laurie | 88b28ad | 2016-01-25 17:13:27 -0800 | [diff] [blame] | 90 | } |
| 91 | |
Furquan Shaikh | 37c721d | 2018-04-25 18:13:03 -0700 | [diff] [blame] | 92 | /* Read the USB Device Controller(UDC) enable flag from VBNV. */ |
| 93 | int vbnv_udc_enable_flag(void) |
| 94 | { |
| 95 | vbnv_setup(); |
Arthur Heymans | 344e86b | 2019-11-20 19:47:10 +0100 | [diff] [blame] | 96 | return (vbnv[DEV_FLAGS_OFFSET] & DEV_ENABLE_UDC) ? 1 : 0; |
Furquan Shaikh | 37c721d | 2018-04-25 18:13:03 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Aaron Durbin | 118a84f | 2017-09-15 11:15:07 -0600 | [diff] [blame] | 99 | void vbnv_init(uint8_t *vbnv_copy) |
| 100 | { |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 101 | if (CONFIG(VBOOT_VBNV_CMOS)) |
Aaron Durbin | 0990fbf | 2017-09-15 15:23:04 -0600 | [diff] [blame] | 102 | vbnv_init_cmos(vbnv_copy); |
Aaron Durbin | 118a84f | 2017-09-15 11:15:07 -0600 | [diff] [blame] | 103 | read_vbnv(vbnv_copy); |
| 104 | } |