Daisuke Nojiri | 742fc8d | 2014-10-10 10:51:06 -0700 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright (C) 2014 The ChromiumOS Authors. All rights reserved. |
| 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. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | */ |
| 19 | |
| 20 | #include <stddef.h> |
| 21 | #include <string.h> |
| 22 | #include <boot/coreboot_tables.h> |
| 23 | #include <cbfs.h> |
| 24 | #include <cbmem.h> |
| 25 | #include <console/console.h> |
| 26 | #include "chromeos.h" |
| 27 | #include "vboot_common.h" |
Daisuke Nojiri | 24d4dae | 2015-02-03 14:44:55 -0800 | [diff] [blame] | 28 | #include "vboot_handoff.h" |
Daisuke Nojiri | 742fc8d | 2014-10-10 10:51:06 -0700 | [diff] [blame] | 29 | |
| 30 | void vboot_locate_region(const char *name, struct vboot_region *region) |
| 31 | { |
| 32 | region->size = find_fmap_entry(name, (void **)®ion->offset_addr); |
| 33 | } |
| 34 | |
| 35 | void *vboot_get_region(uintptr_t offset_addr, size_t size, void *dest) |
| 36 | { |
| 37 | if (IS_ENABLED(CONFIG_SPI_FLASH_MEMORY_MAPPED)) { |
| 38 | if (dest != NULL) |
| 39 | return memcpy(dest, (void *)offset_addr, size); |
| 40 | else |
| 41 | return (void *)offset_addr; |
| 42 | } else { |
| 43 | struct cbfs_media default_media, *media = &default_media; |
| 44 | void *cache; |
| 45 | |
| 46 | init_default_cbfs_media(media); |
| 47 | media->open(media); |
| 48 | if (dest != NULL) { |
| 49 | cache = dest; |
| 50 | if (media->read(media, dest, offset_addr, size) != size) |
| 51 | cache = NULL; |
| 52 | } else { |
| 53 | cache = media->map(media, offset_addr, size); |
| 54 | if (cache == CBFS_MEDIA_INVALID_MAP_ADDRESS) |
| 55 | cache = NULL; |
| 56 | } |
| 57 | media->close(media); |
| 58 | return cache; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | int vboot_get_handoff_info(void **addr, uint32_t *size) |
| 63 | { |
| 64 | struct vboot_handoff *vboot_handoff; |
| 65 | |
| 66 | vboot_handoff = cbmem_find(CBMEM_ID_VBOOT_HANDOFF); |
| 67 | |
| 68 | if (vboot_handoff == NULL) |
| 69 | return -1; |
| 70 | |
| 71 | *addr = vboot_handoff; |
| 72 | *size = sizeof(*vboot_handoff); |
| 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | /* This will leak a mapping of a fw region */ |
| 77 | struct vboot_components *vboot_locate_components(struct vboot_region *region) |
| 78 | { |
| 79 | size_t req_size; |
| 80 | struct vboot_components *vbc; |
| 81 | |
| 82 | req_size = sizeof(*vbc); |
| 83 | req_size += sizeof(struct vboot_component_entry) * |
| 84 | MAX_PARSED_FW_COMPONENTS; |
| 85 | |
| 86 | vbc = vboot_get_region(region->offset_addr, req_size, NULL); |
| 87 | if (vbc && vbc->num_components > MAX_PARSED_FW_COMPONENTS) |
| 88 | vbc = NULL; |
| 89 | |
| 90 | return vbc; |
| 91 | } |
| 92 | |
| 93 | void *vboot_get_payload(int *len) |
| 94 | { |
| 95 | struct vboot_handoff *vboot_handoff; |
| 96 | struct firmware_component *fwc; |
| 97 | |
Vadim Bendebury | 42001a7 | 2014-12-25 15:34:32 -0800 | [diff] [blame^] | 98 | if (IS_ENABLED(CONFIG_MULTIPLE_CBFS_INSTANCES)) |
| 99 | return NULL; /* Let CBFS figure it out. */ |
| 100 | |
Daisuke Nojiri | 742fc8d | 2014-10-10 10:51:06 -0700 | [diff] [blame] | 101 | vboot_handoff = cbmem_find(CBMEM_ID_VBOOT_HANDOFF); |
| 102 | |
| 103 | if (vboot_handoff == NULL) |
| 104 | return NULL; |
| 105 | |
| 106 | if (CONFIG_VBOOT_BOOT_LOADER_INDEX >= MAX_PARSED_FW_COMPONENTS) { |
| 107 | printk(BIOS_ERR, "Invalid boot loader index: %d\n", |
| 108 | CONFIG_VBOOT_BOOT_LOADER_INDEX); |
| 109 | return NULL; |
| 110 | } |
| 111 | |
| 112 | fwc = &vboot_handoff->components[CONFIG_VBOOT_BOOT_LOADER_INDEX]; |
| 113 | |
| 114 | /* If payload size is zero fall back to cbfs path. */ |
| 115 | if (fwc->size == 0) |
| 116 | return NULL; |
| 117 | |
| 118 | if (len != NULL) |
| 119 | *len = fwc->size; |
| 120 | |
| 121 | printk(BIOS_DEBUG, "Booting 0x%x byte verified payload at 0x%08x.\n", |
| 122 | fwc->size, fwc->address); |
| 123 | |
| 124 | /* This will leak a mapping. */ |
| 125 | return vboot_get_region(fwc->address, fwc->size, NULL); |
| 126 | } |