blob: 17039330a6f7e93d2f89024fb2f4974817b0a38b [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 Wuaeb652a2019-11-14 15:42:25 +08005#include <fmap.h>
Aaron Durbinb5a20b22015-10-06 17:29:03 -05006#include <vb2_api.h>
Philipp Deppenwiesefea24292017-10-17 17:02:29 +02007#include <security/vboot/misc.h>
8#include <security/vboot/symbols.h>
9#include <security/vboot/vboot_common.h>
Daisuke Nojiri742fc8d2014-10-10 10:51:06 -070010
Arthur Heymans344e86b2019-11-20 19:47:10 +010011static struct vb2_context *vboot_ctx;
Joel Kitching2332c742019-10-23 15:01:37 +080012
Yu-Ping Wu63b97002019-11-26 13:31:32 +080013static void *vboot_get_workbuf(void)
Aaron Durbinb5933662015-10-07 16:03:41 -050014{
Yu-Ping Wua2962da2019-11-26 10:47:35 +080015 void *wb = NULL;
Aaron Durbinb5933662015-10-07 16:03:41 -050016
Joel Kitching0bcee882019-02-11 15:37:49 +080017 if (cbmem_possibly_online())
Yu-Ping Wua2962da2019-11-26 10:47:35 +080018 wb = cbmem_find(CBMEM_ID_VBOOT_WORKBUF);
Aaron Durbinb5933662015-10-07 16:03:41 -050019
Elyes Haouasaebccac2022-09-13 09:56:22 +020020 if (!wb && !CONFIG(VBOOT_STARTS_IN_ROMSTAGE) && preram_symbols_available())
Yu-Ping Wua2962da2019-11-26 10:47:35 +080021 wb = _vboot2_work;
Aaron Durbinb5933662015-10-07 16:03:41 -050022
Elyes Haouasaebccac2022-09-13 09:56:22 +020023 assert(wb);
Joel Kitching0bcee882019-02-11 15:37:49 +080024
Yu-Ping Wua2962da2019-11-26 10:47:35 +080025 return wb;
Joel Kitching2332c742019-10-23 15:01:37 +080026}
27
28struct vb2_context *vboot_get_context(void)
29{
Yu-Ping Wua2962da2019-11-26 10:47:35 +080030 void *wb;
Aaron Durbinb5a20b22015-10-06 17:29:03 -050031
Joel Kitching2332c742019-10-23 15:01:37 +080032 /* Return if context has already been initialized/restored. */
Arthur Heymans344e86b2019-11-20 19:47:10 +010033 if (vboot_ctx)
34 return vboot_ctx;
Joel Kitching2332c742019-10-23 15:01:37 +080035
Yu-Ping Wua2962da2019-11-26 10:47:35 +080036 wb = vboot_get_workbuf();
Joel Kitching2332c742019-10-23 15:01:37 +080037
38 /* Restore context from a previous stage. */
39 if (vboot_logic_executed()) {
Yu-Ping Wua2962da2019-11-26 10:47:35 +080040 assert(vb2api_reinit(wb, &vboot_ctx) == VB2_SUCCESS);
Arthur Heymans344e86b2019-11-20 19:47:10 +010041 return vboot_ctx;
Joel Kitching2332c742019-10-23 15:01:37 +080042 }
43
44 assert(verification_should_run());
Aaron Durbinb5a20b22015-10-06 17:29:03 -050045
Joel Kitching2332c742019-10-23 15:01:37 +080046 /* Initialize vb2_shared_data and friends. */
Yu-Ping Wua2962da2019-11-26 10:47:35 +080047 assert(vb2api_init(wb, VB2_FIRMWARE_WORKBUF_RECOMMENDED_SIZE,
Arthur Heymans344e86b2019-11-20 19:47:10 +010048 &vboot_ctx) == VB2_SUCCESS);
Aaron Durbinb5a20b22015-10-06 17:29:03 -050049
Arthur Heymans344e86b2019-11-20 19:47:10 +010050 return vboot_ctx;
Daisuke Nojirie5d13782014-12-18 11:59:06 -080051}
Aaron Durbinb5a20b22015-10-06 17:29:03 -050052
Julius Wernerf8e17642019-12-12 13:23:06 -080053int vboot_locate_firmware(struct vb2_context *ctx, struct region_device *fw)
Aaron Durbinb5a20b22015-10-06 17:29:03 -050054{
Yu-Ping Wuaeb652a2019-11-14 15:42:25 +080055 const char *name;
Aaron Durbin6d720f32015-12-08 17:00:23 -060056
Yu-Ping Wuaeb652a2019-11-14 15:42:25 +080057 if (vboot_is_firmware_slot_a(ctx))
58 name = "FW_MAIN_A";
59 else
60 name = "FW_MAIN_B";
Aaron Durbin6d720f32015-12-08 17:00:23 -060061
Julius Wernerf8e17642019-12-12 13:23:06 -080062 int ret = fmap_locate_area_as_rdev(name, fw);
63 if (ret)
64 return ret;
65
Jakub Czapiga967a76b2022-08-19 12:25:27 +020066 /*
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 Durbinb5a20b22015-10-06 17:29:03 -050076}
Aaron Durbinb5933662015-10-07 16:03:41 -050077
Joel Kitchingaf8471c2019-03-13 22:38:07 +080078static void vboot_setup_cbmem(int unused)
Joel Kitching0bcee882019-02-11 15:37:49 +080079{
Joel Kitching8a3bc3b2020-02-08 10:58:48 +080080 vb2_error_t rv;
Yu-Ping Wua2962da2019-11-26 10:47:35 +080081 const size_t cbmem_size = VB2_KERNEL_WORKBUF_RECOMMENDED_SIZE;
82 void *wb_cbmem = cbmem_add(CBMEM_ID_VBOOT_WORKBUF, cbmem_size);
Elyes Haouasaebccac2022-09-13 09:56:22 +020083 assert(wb_cbmem);
Yu-Ping Wua2962da2019-11-26 10:47:35 +080084 /*
Martin Roth8a3a3c82020-05-04 10:13:45 -060085 * 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 Wua2962da2019-11-26 10:47:35 +080090 * 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 Kitching8a3bc3b2020-02-08 10:58:48 +080094 *
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 Wua2962da2019-11-26 10:47:35 +080098 */
Martin Roth8a3a3c82020-05-04 10:13:45 -060099 if (!CONFIG(VBOOT_STARTS_IN_ROMSTAGE))
Joel Kitching8a3bc3b2020-02-08 10:58:48 +0800100 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 Kitching0bcee882019-02-11 15:37:49 +0800106}
Kyösti Mälkkifa3bc042022-03-31 07:40:10 +0300107CBMEM_CREATION_HOOK(vboot_setup_cbmem);