blob: 66d0b6caaab8dd54e73eb3af44fb481ea6572d67 [file] [log] [blame]
Angel Pons8a3453f2020-04-02 23:48:19 +02001/* SPDX-License-Identifier: GPL-2.0-only */
2/* This file is part of the coreboot project. */
Lee Leahyb5ad8272015-04-20 15:29:16 -07003
4#include <console/console.h>
5#include <cbmem.h>
Furquan Shaikhb0c2fe02016-05-09 12:23:01 -07006#include <commonlib/fsp.h>
Aaron Durbin789f2b62015-09-09 17:05:06 -05007#include <fsp/util.h>
Lee Leahyb5ad8272015-04-20 15:29:16 -07008
Aaron Durbin22ea0072015-08-05 10:17:33 -05009int fsp_relocate(struct prog *fsp_relocd, const struct region_device *fsp_src)
Lee Leahyb5ad8272015-04-20 15:29:16 -070010{
11 void *new_loc;
Aaron Durbin22ea0072015-08-05 10:17:33 -050012 void *fih;
Aaron Durbin8007c6b2015-09-10 21:19:25 -050013 ssize_t fih_offset;
Aaron Durbin22ea0072015-08-05 10:17:33 -050014 size_t size = region_device_sz(fsp_src);
Lee Leahyb5ad8272015-04-20 15:29:16 -070015
16 new_loc = cbmem_add(CBMEM_ID_REFCODE, size);
Aaron Durbin22ea0072015-08-05 10:17:33 -050017
Lee Leahyb5ad8272015-04-20 15:29:16 -070018 if (new_loc == NULL) {
Aaron Durbin22ea0072015-08-05 10:17:33 -050019 printk(BIOS_ERR, "ERROR: Unable to load FSP into memory.\n");
20 return -1;
Lee Leahyb5ad8272015-04-20 15:29:16 -070021 }
Aaron Durbin22ea0072015-08-05 10:17:33 -050022
23 if (rdev_readat(fsp_src, new_loc, 0, size) != size) {
24 printk(BIOS_ERR, "ERROR: Can't read FSP's region device.\n");
25 return -1;
26 }
27
Aaron Durbin8007c6b2015-09-10 21:19:25 -050028 fih_offset = fsp1_1_relocate((uintptr_t)new_loc, new_loc, size);
Aaron Durbin22ea0072015-08-05 10:17:33 -050029
Aaron Durbin8007c6b2015-09-10 21:19:25 -050030 if (fih_offset <= 0) {
Wimfb758d42018-08-28 11:59:38 +020031 printk(BIOS_ERR, "ERROR: FSP relocation failure.\n");
Aaron Durbin22ea0072015-08-05 10:17:33 -050032 return -1;
33 }
34
Aaron Durbina5be7fa2015-09-10 22:52:27 -050035 fih = (void *)((uint8_t *)new_loc + fih_offset);
Aaron Durbin8007c6b2015-09-10 21:19:25 -050036
Aaron Durbin22ea0072015-08-05 10:17:33 -050037 prog_set_area(fsp_relocd, new_loc, size);
38 prog_set_entry(fsp_relocd, fih, NULL);
39
40 return 0;
Lee Leahyb5ad8272015-04-20 15:29:16 -070041}