Angel Pons | 986d50e | 2020-04-02 23:48:53 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Daisuke Nojiri | 742fc8d | 2014-10-10 10:51:06 -0700 | [diff] [blame] | 2 | |
Furquan Shaikh | a6c5ddd | 2016-07-22 06:59:40 -0700 | [diff] [blame] | 3 | #include <assert.h> |
Aaron Durbin | 0e571fd | 2015-05-08 17:14:15 -0500 | [diff] [blame] | 4 | #include <cbmem.h> |
Yu-Ping Wu | aeb652a | 2019-11-14 15:42:25 +0800 | [diff] [blame] | 5 | #include <fmap.h> |
Aaron Durbin | b5a20b2 | 2015-10-06 17:29:03 -0500 | [diff] [blame] | 6 | #include <vb2_api.h> |
Philipp Deppenwiese | fea2429 | 2017-10-17 17:02:29 +0200 | [diff] [blame] | 7 | #include <security/vboot/misc.h> |
| 8 | #include <security/vboot/symbols.h> |
| 9 | #include <security/vboot/vboot_common.h> |
Daisuke Nojiri | 742fc8d | 2014-10-10 10:51:06 -0700 | [diff] [blame] | 10 | |
Arthur Heymans | 344e86b | 2019-11-20 19:47:10 +0100 | [diff] [blame] | 11 | static struct vb2_context *vboot_ctx; |
Joel Kitching | 2332c74 | 2019-10-23 15:01:37 +0800 | [diff] [blame] | 12 | |
Yu-Ping Wu | 63b9700 | 2019-11-26 13:31:32 +0800 | [diff] [blame] | 13 | static void *vboot_get_workbuf(void) |
Aaron Durbin | b593366 | 2015-10-07 16:03:41 -0500 | [diff] [blame] | 14 | { |
Yu-Ping Wu | a2962da | 2019-11-26 10:47:35 +0800 | [diff] [blame] | 15 | void *wb = NULL; |
Aaron Durbin | b593366 | 2015-10-07 16:03:41 -0500 | [diff] [blame] | 16 | |
Joel Kitching | 0bcee88 | 2019-02-11 15:37:49 +0800 | [diff] [blame] | 17 | if (cbmem_possibly_online()) |
Yu-Ping Wu | a2962da | 2019-11-26 10:47:35 +0800 | [diff] [blame] | 18 | wb = cbmem_find(CBMEM_ID_VBOOT_WORKBUF); |
Aaron Durbin | b593366 | 2015-10-07 16:03:41 -0500 | [diff] [blame] | 19 | |
Elyes Haouas | aebccac | 2022-09-13 09:56:22 +0200 | [diff] [blame] | 20 | if (!wb && !CONFIG(VBOOT_STARTS_IN_ROMSTAGE) && preram_symbols_available()) |
Yu-Ping Wu | a2962da | 2019-11-26 10:47:35 +0800 | [diff] [blame] | 21 | wb = _vboot2_work; |
Aaron Durbin | b593366 | 2015-10-07 16:03:41 -0500 | [diff] [blame] | 22 | |
Elyes Haouas | aebccac | 2022-09-13 09:56:22 +0200 | [diff] [blame] | 23 | assert(wb); |
Joel Kitching | 0bcee88 | 2019-02-11 15:37:49 +0800 | [diff] [blame] | 24 | |
Yu-Ping Wu | a2962da | 2019-11-26 10:47:35 +0800 | [diff] [blame] | 25 | return wb; |
Joel Kitching | 2332c74 | 2019-10-23 15:01:37 +0800 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | struct vb2_context *vboot_get_context(void) |
| 29 | { |
Yu-Ping Wu | a2962da | 2019-11-26 10:47:35 +0800 | [diff] [blame] | 30 | void *wb; |
Aaron Durbin | b5a20b2 | 2015-10-06 17:29:03 -0500 | [diff] [blame] | 31 | |
Joel Kitching | 2332c74 | 2019-10-23 15:01:37 +0800 | [diff] [blame] | 32 | /* Return if context has already been initialized/restored. */ |
Arthur Heymans | 344e86b | 2019-11-20 19:47:10 +0100 | [diff] [blame] | 33 | if (vboot_ctx) |
| 34 | return vboot_ctx; |
Joel Kitching | 2332c74 | 2019-10-23 15:01:37 +0800 | [diff] [blame] | 35 | |
Yu-Ping Wu | a2962da | 2019-11-26 10:47:35 +0800 | [diff] [blame] | 36 | wb = vboot_get_workbuf(); |
Joel Kitching | 2332c74 | 2019-10-23 15:01:37 +0800 | [diff] [blame] | 37 | |
| 38 | /* Restore context from a previous stage. */ |
| 39 | if (vboot_logic_executed()) { |
Yu-Ping Wu | a2962da | 2019-11-26 10:47:35 +0800 | [diff] [blame] | 40 | assert(vb2api_reinit(wb, &vboot_ctx) == VB2_SUCCESS); |
Arthur Heymans | 344e86b | 2019-11-20 19:47:10 +0100 | [diff] [blame] | 41 | return vboot_ctx; |
Joel Kitching | 2332c74 | 2019-10-23 15:01:37 +0800 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | assert(verification_should_run()); |
Aaron Durbin | b5a20b2 | 2015-10-06 17:29:03 -0500 | [diff] [blame] | 45 | |
Joel Kitching | 2332c74 | 2019-10-23 15:01:37 +0800 | [diff] [blame] | 46 | /* Initialize vb2_shared_data and friends. */ |
Yu-Ping Wu | a2962da | 2019-11-26 10:47:35 +0800 | [diff] [blame] | 47 | assert(vb2api_init(wb, VB2_FIRMWARE_WORKBUF_RECOMMENDED_SIZE, |
Arthur Heymans | 344e86b | 2019-11-20 19:47:10 +0100 | [diff] [blame] | 48 | &vboot_ctx) == VB2_SUCCESS); |
Aaron Durbin | b5a20b2 | 2015-10-06 17:29:03 -0500 | [diff] [blame] | 49 | |
Arthur Heymans | 344e86b | 2019-11-20 19:47:10 +0100 | [diff] [blame] | 50 | return vboot_ctx; |
Daisuke Nojiri | e5d1378 | 2014-12-18 11:59:06 -0800 | [diff] [blame] | 51 | } |
Aaron Durbin | b5a20b2 | 2015-10-06 17:29:03 -0500 | [diff] [blame] | 52 | |
Julius Werner | f8e1764 | 2019-12-12 13:23:06 -0800 | [diff] [blame] | 53 | int vboot_locate_firmware(struct vb2_context *ctx, struct region_device *fw) |
Aaron Durbin | b5a20b2 | 2015-10-06 17:29:03 -0500 | [diff] [blame] | 54 | { |
Yu-Ping Wu | aeb652a | 2019-11-14 15:42:25 +0800 | [diff] [blame] | 55 | const char *name; |
Aaron Durbin | 6d720f3 | 2015-12-08 17:00:23 -0600 | [diff] [blame] | 56 | |
Yu-Ping Wu | aeb652a | 2019-11-14 15:42:25 +0800 | [diff] [blame] | 57 | if (vboot_is_firmware_slot_a(ctx)) |
| 58 | name = "FW_MAIN_A"; |
| 59 | else |
| 60 | name = "FW_MAIN_B"; |
Aaron Durbin | 6d720f3 | 2015-12-08 17:00:23 -0600 | [diff] [blame] | 61 | |
Julius Werner | f8e1764 | 2019-12-12 13:23:06 -0800 | [diff] [blame] | 62 | int ret = fmap_locate_area_as_rdev(name, fw); |
| 63 | if (ret) |
| 64 | return ret; |
| 65 | |
Jakub Czapiga | 967a76b | 2022-08-19 12:25:27 +0200 | [diff] [blame] | 66 | /* |
| 67 | * Truncate area to the size that was actually signed by vboot. |
| 68 | * It is only required for old verification mechanism calculating full body hash. |
| 69 | * New verification mechanism uses signature with zero data size, so truncation |
| 70 | * is not possible. |
| 71 | */ |
| 72 | if (!CONFIG(VBOOT_CBFS_INTEGRATION)) |
| 73 | return rdev_chain(fw, fw, 0, vb2api_get_firmware_size(ctx)); |
| 74 | |
| 75 | return 0; |
Aaron Durbin | b5a20b2 | 2015-10-06 17:29:03 -0500 | [diff] [blame] | 76 | } |
Aaron Durbin | b593366 | 2015-10-07 16:03:41 -0500 | [diff] [blame] | 77 | |
Joel Kitching | af8471c | 2019-03-13 22:38:07 +0800 | [diff] [blame] | 78 | static void vboot_setup_cbmem(int unused) |
Joel Kitching | 0bcee88 | 2019-02-11 15:37:49 +0800 | [diff] [blame] | 79 | { |
Joel Kitching | 8a3bc3b | 2020-02-08 10:58:48 +0800 | [diff] [blame] | 80 | vb2_error_t rv; |
Yu-Ping Wu | a2962da | 2019-11-26 10:47:35 +0800 | [diff] [blame] | 81 | const size_t cbmem_size = VB2_KERNEL_WORKBUF_RECOMMENDED_SIZE; |
| 82 | void *wb_cbmem = cbmem_add(CBMEM_ID_VBOOT_WORKBUF, cbmem_size); |
Elyes Haouas | aebccac | 2022-09-13 09:56:22 +0200 | [diff] [blame] | 83 | assert(wb_cbmem); |
Yu-Ping Wu | a2962da | 2019-11-26 10:47:35 +0800 | [diff] [blame] | 84 | /* |
Martin Roth | 8a3a3c8 | 2020-05-04 10:13:45 -0600 | [diff] [blame] | 85 | * On platforms where VBOOT_STARTS_BEFORE_BOOTBLOCK, the verification |
| 86 | * occurs before the main processor starts running. The vboot data- |
| 87 | * structure is available in the _vboot2_work memory area as soon |
| 88 | * as the main processor is released. |
| 89 | * |
Yu-Ping Wu | a2962da | 2019-11-26 10:47:35 +0800 | [diff] [blame] | 90 | * For platforms where VBOOT_STARTS_IN_BOOTBLOCK, vboot verification |
| 91 | * occurs before CBMEM is brought online, using pre-RAM. In order to |
| 92 | * make vboot data structures available downstream, copy vboot workbuf |
| 93 | * from SRAM/CAR into CBMEM. |
Joel Kitching | 8a3bc3b | 2020-02-08 10:58:48 +0800 | [diff] [blame] | 94 | * |
| 95 | * For platforms where VBOOT_STARTS_IN_ROMSTAGE, verification occurs |
| 96 | * after CBMEM is brought online. Directly initialize vboot data |
| 97 | * structures in CBMEM, which will also be available downstream. |
Yu-Ping Wu | a2962da | 2019-11-26 10:47:35 +0800 | [diff] [blame] | 98 | */ |
Martin Roth | 8a3a3c8 | 2020-05-04 10:13:45 -0600 | [diff] [blame] | 99 | if (!CONFIG(VBOOT_STARTS_IN_ROMSTAGE)) |
Joel Kitching | 8a3bc3b | 2020-02-08 10:58:48 +0800 | [diff] [blame] | 100 | rv = vb2api_relocate(wb_cbmem, _vboot2_work, cbmem_size, |
| 101 | &vboot_ctx); |
| 102 | else |
| 103 | rv = vb2api_init(wb_cbmem, cbmem_size, &vboot_ctx); |
| 104 | |
| 105 | assert(rv == VB2_SUCCESS); |
Joel Kitching | 0bcee88 | 2019-02-11 15:37:49 +0800 | [diff] [blame] | 106 | } |
Kyösti Mälkki | fa3bc04 | 2022-03-31 07:40:10 +0300 | [diff] [blame] | 107 | CBMEM_CREATION_HOOK(vboot_setup_cbmem); |