blob: 88b166ce6ac67136b04393f917982124d2a2e750 [file] [log] [blame]
Angel Pons986d50e2020-04-02 23:48:53 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Daisuke Nojiri742fc8d2014-10-10 10:51:06 -07002
Furquan Shaikha6c5ddd2016-07-22 06:59:40 -07003#include <assert.h>
Aaron Durbin0e571fd2015-05-08 17:14:15 -05004#include <cbmem.h>
Yu-Ping Wu4f1dda72023-10-30 16:45:32 +08005#include <console/console.h>
Yu-Ping Wuaeb652a2019-11-14 15:42:25 +08006#include <fmap.h>
Aaron Durbinb5a20b22015-10-06 17:29:03 -05007#include <vb2_api.h>
Philipp Deppenwiesefea24292017-10-17 17:02:29 +02008#include <security/vboot/misc.h>
9#include <security/vboot/symbols.h>
10#include <security/vboot/vboot_common.h>
Daisuke Nojiri742fc8d2014-10-10 10:51:06 -070011
Arthur Heymans344e86b2019-11-20 19:47:10 +010012static struct vb2_context *vboot_ctx;
Joel Kitching2332c742019-10-23 15:01:37 +080013
Yu-Ping Wu63b97002019-11-26 13:31:32 +080014static void *vboot_get_workbuf(void)
Aaron Durbinb5933662015-10-07 16:03:41 -050015{
Yu-Ping Wua2962da2019-11-26 10:47:35 +080016 void *wb = NULL;
Aaron Durbinb5933662015-10-07 16:03:41 -050017
Arthur Heymansb7cbb7c2023-08-11 11:31:05 +020018 if (ENV_HAS_CBMEM)
Yu-Ping Wua2962da2019-11-26 10:47:35 +080019 wb = cbmem_find(CBMEM_ID_VBOOT_WORKBUF);
Aaron Durbinb5933662015-10-07 16:03:41 -050020
Elyes Haouasaebccac2022-09-13 09:56:22 +020021 if (!wb && !CONFIG(VBOOT_STARTS_IN_ROMSTAGE) && preram_symbols_available())
Yu-Ping Wua2962da2019-11-26 10:47:35 +080022 wb = _vboot2_work;
Aaron Durbinb5933662015-10-07 16:03:41 -050023
Elyes Haouasaebccac2022-09-13 09:56:22 +020024 assert(wb);
Joel Kitching0bcee882019-02-11 15:37:49 +080025
Yu-Ping Wua2962da2019-11-26 10:47:35 +080026 return wb;
Joel Kitching2332c742019-10-23 15:01:37 +080027}
28
29struct vb2_context *vboot_get_context(void)
30{
Yu-Ping Wua2962da2019-11-26 10:47:35 +080031 void *wb;
Yu-Ping Wu4f1dda72023-10-30 16:45:32 +080032 vb2_error_t rv;
Aaron Durbinb5a20b22015-10-06 17:29:03 -050033
Joel Kitching2332c742019-10-23 15:01:37 +080034 /* Return if context has already been initialized/restored. */
Arthur Heymans344e86b2019-11-20 19:47:10 +010035 if (vboot_ctx)
36 return vboot_ctx;
Joel Kitching2332c742019-10-23 15:01:37 +080037
Yu-Ping Wua2962da2019-11-26 10:47:35 +080038 wb = vboot_get_workbuf();
Joel Kitching2332c742019-10-23 15:01:37 +080039
40 /* Restore context from a previous stage. */
41 if (vboot_logic_executed()) {
Yu-Ping Wu4f1dda72023-10-30 16:45:32 +080042 rv = vb2api_reinit(wb, &vboot_ctx);
43 if (rv != VB2_SUCCESS)
44 die("%s: vb2api_reinit returned %#x\n", __func__, rv);
Arthur Heymans344e86b2019-11-20 19:47:10 +010045 return vboot_ctx;
Joel Kitching2332c742019-10-23 15:01:37 +080046 }
47
48 assert(verification_should_run());
Aaron Durbinb5a20b22015-10-06 17:29:03 -050049
Joel Kitching2332c742019-10-23 15:01:37 +080050 /* Initialize vb2_shared_data and friends. */
Yu-Ping Wu4f1dda72023-10-30 16:45:32 +080051 rv = vb2api_init(wb, VB2_FIRMWARE_WORKBUF_RECOMMENDED_SIZE, &vboot_ctx);
52 assert(rv == VB2_SUCCESS);
Aaron Durbinb5a20b22015-10-06 17:29:03 -050053
Arthur Heymans344e86b2019-11-20 19:47:10 +010054 return vboot_ctx;
Daisuke Nojirie5d13782014-12-18 11:59:06 -080055}
Aaron Durbinb5a20b22015-10-06 17:29:03 -050056
Julius Wernerf8e17642019-12-12 13:23:06 -080057int vboot_locate_firmware(struct vb2_context *ctx, struct region_device *fw)
Aaron Durbinb5a20b22015-10-06 17:29:03 -050058{
Yu-Ping Wuaeb652a2019-11-14 15:42:25 +080059 const char *name;
Aaron Durbin6d720f32015-12-08 17:00:23 -060060
Yu-Ping Wuaeb652a2019-11-14 15:42:25 +080061 if (vboot_is_firmware_slot_a(ctx))
62 name = "FW_MAIN_A";
63 else
64 name = "FW_MAIN_B";
Aaron Durbin6d720f32015-12-08 17:00:23 -060065
Julius Wernerf8e17642019-12-12 13:23:06 -080066 int ret = fmap_locate_area_as_rdev(name, fw);
67 if (ret)
68 return ret;
69
Jakub Czapiga967a76b2022-08-19 12:25:27 +020070 /*
71 * Truncate area to the size that was actually signed by vboot.
72 * It is only required for old verification mechanism calculating full body hash.
73 * New verification mechanism uses signature with zero data size, so truncation
74 * is not possible.
75 */
76 if (!CONFIG(VBOOT_CBFS_INTEGRATION))
77 return rdev_chain(fw, fw, 0, vb2api_get_firmware_size(ctx));
78
79 return 0;
Aaron Durbinb5a20b22015-10-06 17:29:03 -050080}
Aaron Durbinb5933662015-10-07 16:03:41 -050081
Joel Kitchingaf8471c2019-03-13 22:38:07 +080082static void vboot_setup_cbmem(int unused)
Joel Kitching0bcee882019-02-11 15:37:49 +080083{
Joel Kitching8a3bc3b2020-02-08 10:58:48 +080084 vb2_error_t rv;
Yu-Ping Wua2962da2019-11-26 10:47:35 +080085 const size_t cbmem_size = VB2_KERNEL_WORKBUF_RECOMMENDED_SIZE;
86 void *wb_cbmem = cbmem_add(CBMEM_ID_VBOOT_WORKBUF, cbmem_size);
Elyes Haouasaebccac2022-09-13 09:56:22 +020087 assert(wb_cbmem);
Yu-Ping Wua2962da2019-11-26 10:47:35 +080088 /*
Martin Roth8a3a3c82020-05-04 10:13:45 -060089 * On platforms where VBOOT_STARTS_BEFORE_BOOTBLOCK, the verification
90 * occurs before the main processor starts running. The vboot data-
91 * structure is available in the _vboot2_work memory area as soon
92 * as the main processor is released.
93 *
Yu-Ping Wua2962da2019-11-26 10:47:35 +080094 * For platforms where VBOOT_STARTS_IN_BOOTBLOCK, vboot verification
95 * occurs before CBMEM is brought online, using pre-RAM. In order to
96 * make vboot data structures available downstream, copy vboot workbuf
97 * from SRAM/CAR into CBMEM.
Joel Kitching8a3bc3b2020-02-08 10:58:48 +080098 *
99 * For platforms where VBOOT_STARTS_IN_ROMSTAGE, verification occurs
100 * after CBMEM is brought online. Directly initialize vboot data
101 * structures in CBMEM, which will also be available downstream.
Yu-Ping Wua2962da2019-11-26 10:47:35 +0800102 */
Martin Roth8a3a3c82020-05-04 10:13:45 -0600103 if (!CONFIG(VBOOT_STARTS_IN_ROMSTAGE))
Joel Kitching8a3bc3b2020-02-08 10:58:48 +0800104 rv = vb2api_relocate(wb_cbmem, _vboot2_work, cbmem_size,
105 &vboot_ctx);
106 else
107 rv = vb2api_init(wb_cbmem, cbmem_size, &vboot_ctx);
108
109 assert(rv == VB2_SUCCESS);
Joel Kitching0bcee882019-02-11 15:37:49 +0800110}
Kyösti Mälkkifa3bc042022-03-31 07:40:10 +0300111CBMEM_CREATION_HOOK(vboot_setup_cbmem);