Lee Leahy | b5ad827 | 2015-04-20 15:29:16 -0700 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright 2015 Google Inc |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; version 2 of the License. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
Lee Leahy | b5ad827 | 2015-04-20 15:29:16 -0700 | [diff] [blame] | 14 | */ |
| 15 | |
| 16 | #include <console/console.h> |
| 17 | #include <cbmem.h> |
Furquan Shaikh | b0c2fe0 | 2016-05-09 12:23:01 -0700 | [diff] [blame^] | 18 | #include <commonlib/fsp.h> |
Aaron Durbin | 789f2b6 | 2015-09-09 17:05:06 -0500 | [diff] [blame] | 19 | #include <fsp/util.h> |
Lee Leahy | b5ad827 | 2015-04-20 15:29:16 -0700 | [diff] [blame] | 20 | |
Aaron Durbin | 22ea007 | 2015-08-05 10:17:33 -0500 | [diff] [blame] | 21 | int fsp_relocate(struct prog *fsp_relocd, const struct region_device *fsp_src) |
Lee Leahy | b5ad827 | 2015-04-20 15:29:16 -0700 | [diff] [blame] | 22 | { |
| 23 | void *new_loc; |
Aaron Durbin | 22ea007 | 2015-08-05 10:17:33 -0500 | [diff] [blame] | 24 | void *fih; |
Aaron Durbin | 8007c6b | 2015-09-10 21:19:25 -0500 | [diff] [blame] | 25 | ssize_t fih_offset; |
Aaron Durbin | 22ea007 | 2015-08-05 10:17:33 -0500 | [diff] [blame] | 26 | size_t size = region_device_sz(fsp_src); |
Lee Leahy | b5ad827 | 2015-04-20 15:29:16 -0700 | [diff] [blame] | 27 | |
| 28 | new_loc = cbmem_add(CBMEM_ID_REFCODE, size); |
Aaron Durbin | 22ea007 | 2015-08-05 10:17:33 -0500 | [diff] [blame] | 29 | |
Lee Leahy | b5ad827 | 2015-04-20 15:29:16 -0700 | [diff] [blame] | 30 | if (new_loc == NULL) { |
Aaron Durbin | 22ea007 | 2015-08-05 10:17:33 -0500 | [diff] [blame] | 31 | printk(BIOS_ERR, "ERROR: Unable to load FSP into memory.\n"); |
| 32 | return -1; |
Lee Leahy | b5ad827 | 2015-04-20 15:29:16 -0700 | [diff] [blame] | 33 | } |
Aaron Durbin | 22ea007 | 2015-08-05 10:17:33 -0500 | [diff] [blame] | 34 | |
| 35 | if (rdev_readat(fsp_src, new_loc, 0, size) != size) { |
| 36 | printk(BIOS_ERR, "ERROR: Can't read FSP's region device.\n"); |
| 37 | return -1; |
| 38 | } |
| 39 | |
Aaron Durbin | 8007c6b | 2015-09-10 21:19:25 -0500 | [diff] [blame] | 40 | fih_offset = fsp1_1_relocate((uintptr_t)new_loc, new_loc, size); |
Aaron Durbin | 22ea007 | 2015-08-05 10:17:33 -0500 | [diff] [blame] | 41 | |
Aaron Durbin | 8007c6b | 2015-09-10 21:19:25 -0500 | [diff] [blame] | 42 | if (fih_offset <= 0) { |
Aaron Durbin | 22ea007 | 2015-08-05 10:17:33 -0500 | [diff] [blame] | 43 | printk(BIOS_ERR, "ERROR: FSP relocation faiulre.\n"); |
| 44 | return -1; |
| 45 | } |
| 46 | |
Aaron Durbin | a5be7fa | 2015-09-10 22:52:27 -0500 | [diff] [blame] | 47 | fih = (void *)((uint8_t *)new_loc + fih_offset); |
Aaron Durbin | 8007c6b | 2015-09-10 21:19:25 -0500 | [diff] [blame] | 48 | |
Aaron Durbin | 22ea007 | 2015-08-05 10:17:33 -0500 | [diff] [blame] | 49 | prog_set_area(fsp_relocd, new_loc, size); |
| 50 | prog_set_entry(fsp_relocd, fih, NULL); |
| 51 | |
| 52 | return 0; |
Lee Leahy | b5ad827 | 2015-04-20 15:29:16 -0700 | [diff] [blame] | 53 | } |