Peter Stuge | 483b7bb | 2009-04-14 07:40:01 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 4 | * Copyright (C) 2011 secunet Security Networks AG |
| 5 | * Copyright 2015 Google Inc. |
Peter Stuge | 483b7bb | 2009-04-14 07:40:01 +0000 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; version 2 of the License. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
Peter Stuge | 483b7bb | 2009-04-14 07:40:01 +0000 | [diff] [blame] | 15 | */ |
| 16 | |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 17 | #include <assert.h> |
| 18 | #include <string.h> |
| 19 | #include <stdlib.h> |
| 20 | #include <boot_device.h> |
| 21 | #include <cbfs.h> |
Julius Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 22 | #include <commonlib/compression.h> |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 23 | #include <endian.h> |
| 24 | #include <lib.h> |
| 25 | #include <symbols.h> |
Julius Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 26 | #include <timestamp.h> |
Hung-Te Lin | 6fe0cab | 2013-01-22 18:57:56 +0800 | [diff] [blame] | 27 | |
Patrick Georgi | 58a150a | 2016-05-02 17:22:29 +0800 | [diff] [blame] | 28 | #include "fmap_config.h" |
| 29 | |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 30 | #define ERROR(x...) printk(BIOS_ERR, "CBFS: " x) |
| 31 | #define LOG(x...) printk(BIOS_INFO, "CBFS: " x) |
| 32 | #if IS_ENABLED(CONFIG_DEBUG_CBFS) |
| 33 | #define DEBUG(x...) printk(BIOS_SPEW, "CBFS: " x) |
| 34 | #else |
| 35 | #define DEBUG(x...) |
| 36 | #endif |
Peter Stuge | 483b7bb | 2009-04-14 07:40:01 +0000 | [diff] [blame] | 37 | |
Aaron Durbin | 37a5d15 | 2015-09-17 16:09:30 -0500 | [diff] [blame] | 38 | int cbfs_boot_locate(struct cbfsf *fh, const char *name, uint32_t *type) |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 39 | { |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 40 | struct region_device rdev; |
| 41 | const struct region_device *boot_dev; |
| 42 | struct cbfs_props props; |
| 43 | |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 44 | if (cbfs_boot_region_properties(&props)) |
| 45 | return -1; |
| 46 | |
| 47 | /* All boot CBFS operations are performed using the RO devie. */ |
| 48 | boot_dev = boot_device_ro(); |
| 49 | |
| 50 | if (boot_dev == NULL) |
| 51 | return -1; |
| 52 | |
| 53 | if (rdev_chain(&rdev, boot_dev, props.offset, props.size)) |
| 54 | return -1; |
| 55 | |
Aaron Durbin | 37a5d15 | 2015-09-17 16:09:30 -0500 | [diff] [blame] | 56 | return cbfs_locate(fh, &rdev, name, type); |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | void *cbfs_boot_map_with_leak(const char *name, uint32_t type, size_t *size) |
| 60 | { |
Aaron Durbin | 37a5d15 | 2015-09-17 16:09:30 -0500 | [diff] [blame] | 61 | struct cbfsf fh; |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 62 | size_t fsize; |
| 63 | |
| 64 | if (cbfs_boot_locate(&fh, name, &type)) |
| 65 | return NULL; |
| 66 | |
Aaron Durbin | 37a5d15 | 2015-09-17 16:09:30 -0500 | [diff] [blame] | 67 | fsize = region_device_sz(&fh.data); |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 68 | |
| 69 | if (size != NULL) |
| 70 | *size = fsize; |
| 71 | |
Aaron Durbin | 37a5d15 | 2015-09-17 16:09:30 -0500 | [diff] [blame] | 72 | return rdev_mmap(&fh.data, 0, fsize); |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 73 | } |
| 74 | |
Julius Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 75 | size_t cbfs_load_and_decompress(const struct region_device *rdev, size_t offset, |
| 76 | size_t in_size, void *buffer, size_t buffer_size, uint32_t compression) |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 77 | { |
Julius Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 78 | size_t out_size; |
| 79 | |
| 80 | switch (compression) { |
| 81 | case CBFS_COMPRESS_NONE: |
Julius Werner | f975e55 | 2016-08-19 15:43:06 -0700 | [diff] [blame] | 82 | if (buffer_size < in_size) |
| 83 | return 0; |
Julius Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 84 | if (rdev_readat(rdev, buffer, offset, in_size) != in_size) |
| 85 | return 0; |
| 86 | return in_size; |
| 87 | |
| 88 | case CBFS_COMPRESS_LZ4: |
| 89 | if ((ENV_BOOTBLOCK || ENV_VERSTAGE) && |
| 90 | !IS_ENABLED(CONFIG_COMPRESS_PRERAM_STAGES)) |
| 91 | return 0; |
| 92 | |
| 93 | /* Load the compressed image to the end of the available memory |
| 94 | * area for in-place decompression. It is the responsibility of |
| 95 | * the caller to ensure that buffer_size is large enough |
| 96 | * (see compression.h, guaranteed by cbfstool for stages). */ |
| 97 | void *compr_start = buffer + buffer_size - in_size; |
| 98 | if (rdev_readat(rdev, compr_start, offset, in_size) != in_size) |
| 99 | return 0; |
| 100 | |
| 101 | timestamp_add_now(TS_START_ULZ4F); |
| 102 | out_size = ulz4fn(compr_start, in_size, buffer, buffer_size); |
| 103 | timestamp_add_now(TS_END_ULZ4F); |
| 104 | return out_size; |
| 105 | |
| 106 | case CBFS_COMPRESS_LZMA: |
| 107 | if (ENV_BOOTBLOCK || ENV_VERSTAGE) |
| 108 | return 0; |
Lee Leahy | d950f51 | 2016-07-25 09:53:35 -0700 | [diff] [blame] | 109 | if ((ENV_ROMSTAGE || ENV_POSTCAR) |
| 110 | && !IS_ENABLED(CONFIG_COMPRESS_RAMSTAGE)) |
Julius Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 111 | return 0; |
Julius Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 112 | void *map = rdev_mmap(rdev, offset, in_size); |
| 113 | if (map == NULL) |
| 114 | return 0; |
| 115 | |
| 116 | /* Note: timestamp not useful for memory-mapped media (x86) */ |
| 117 | timestamp_add_now(TS_START_ULZMA); |
| 118 | out_size = ulzman(map, in_size, buffer, buffer_size); |
| 119 | timestamp_add_now(TS_END_ULZMA); |
| 120 | |
| 121 | rdev_munmap(rdev, map); |
| 122 | |
| 123 | return out_size; |
| 124 | |
| 125 | default: |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 126 | return 0; |
Julius Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 127 | } |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 128 | } |
| 129 | |
Stefan Reinauer | 800379f | 2010-03-01 08:34:19 +0000 | [diff] [blame] | 130 | static inline int tohex4(unsigned int c) |
Patrick Georgi | b203c2f | 2009-08-20 14:48:03 +0000 | [diff] [blame] | 131 | { |
Hung-Te Lin | 6fe0cab | 2013-01-22 18:57:56 +0800 | [diff] [blame] | 132 | return (c <= 9) ? (c + '0') : (c - 10 + 'a'); |
Patrick Georgi | b203c2f | 2009-08-20 14:48:03 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | static void tohex16(unsigned int val, char* dest) |
| 136 | { |
Hung-Te Lin | 6fe0cab | 2013-01-22 18:57:56 +0800 | [diff] [blame] | 137 | dest[0] = tohex4(val>>12); |
| 138 | dest[1] = tohex4((val>>8) & 0xf); |
| 139 | dest[2] = tohex4((val>>4) & 0xf); |
| 140 | dest[3] = tohex4(val & 0xf); |
Patrick Georgi | b203c2f | 2009-08-20 14:48:03 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 143 | void *cbfs_boot_map_optionrom(uint16_t vendor, uint16_t device) |
Peter Stuge | 483b7bb | 2009-04-14 07:40:01 +0000 | [diff] [blame] | 144 | { |
Hung-Te Lin | 6fe0cab | 2013-01-22 18:57:56 +0800 | [diff] [blame] | 145 | char name[17] = "pciXXXX,XXXX.rom"; |
Peter Stuge | 483b7bb | 2009-04-14 07:40:01 +0000 | [diff] [blame] | 146 | |
Patrick Georgi | b203c2f | 2009-08-20 14:48:03 +0000 | [diff] [blame] | 147 | tohex16(vendor, name+3); |
| 148 | tohex16(device, name+8); |
Peter Stuge | 483b7bb | 2009-04-14 07:40:01 +0000 | [diff] [blame] | 149 | |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 150 | return cbfs_boot_map_with_leak(name, CBFS_TYPE_OPTIONROM, NULL); |
Peter Stuge | 483b7bb | 2009-04-14 07:40:01 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 153 | void *cbfs_boot_load_stage_by_name(const char *name) |
Peter Stuge | 483b7bb | 2009-04-14 07:40:01 +0000 | [diff] [blame] | 154 | { |
Aaron Durbin | 37a5d15 | 2015-09-17 16:09:30 -0500 | [diff] [blame] | 155 | struct cbfsf fh; |
Aaron Durbin | 7e7a4df | 2015-12-08 14:34:35 -0600 | [diff] [blame] | 156 | struct prog stage = PROG_INIT(PROG_UNKNOWN, name); |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 157 | uint32_t type = CBFS_TYPE_STAGE; |
Aaron Durbin | 3948e53 | 2015-03-20 13:00:20 -0500 | [diff] [blame] | 158 | |
Aaron Durbin | 37a5d15 | 2015-09-17 16:09:30 -0500 | [diff] [blame] | 159 | if (cbfs_boot_locate(&fh, name, &type)) |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 160 | return NULL; |
Aaron Durbin | 3948e53 | 2015-03-20 13:00:20 -0500 | [diff] [blame] | 161 | |
Aaron Durbin | 37a5d15 | 2015-09-17 16:09:30 -0500 | [diff] [blame] | 162 | /* Chain data portion in the prog. */ |
| 163 | cbfs_file_data(prog_rdev(&stage), &fh); |
| 164 | |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 165 | if (cbfs_prog_stage_load(&stage)) |
| 166 | return NULL; |
| 167 | |
| 168 | return prog_entry(&stage); |
Peter Stuge | 483b7bb | 2009-04-14 07:40:01 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Julius Werner | f975e55 | 2016-08-19 15:43:06 -0700 | [diff] [blame] | 171 | size_t cbfs_boot_load_struct(const char *name, void *buf, size_t buf_size) |
| 172 | { |
| 173 | struct cbfsf fh; |
| 174 | uint32_t compression_algo; |
| 175 | size_t decompressed_size; |
| 176 | uint32_t type = CBFS_TYPE_STRUCT; |
| 177 | |
| 178 | if (cbfs_boot_locate(&fh, name, &type) < 0) |
| 179 | return 0; |
| 180 | |
| 181 | if (cbfsf_decompression_info(&fh, &compression_algo, |
| 182 | &decompressed_size) < 0 |
| 183 | || decompressed_size > buf_size) |
| 184 | return 0; |
| 185 | |
| 186 | return cbfs_load_and_decompress(&fh.data, 0, region_device_sz(&fh.data), |
| 187 | buf, buf_size, compression_algo); |
| 188 | } |
| 189 | |
Kyösti Mälkki | 9d6f365 | 2016-06-28 07:38:46 +0300 | [diff] [blame] | 190 | size_t cbfs_prog_stage_section(struct prog *pstage, uintptr_t *base) |
| 191 | { |
| 192 | struct cbfs_stage stage; |
| 193 | const struct region_device *fh = prog_rdev(pstage); |
| 194 | |
| 195 | if (rdev_readat(fh, &stage, 0, sizeof(stage)) != sizeof(stage)) |
| 196 | return 0; |
| 197 | |
| 198 | *base = (uintptr_t)stage.load; |
| 199 | return stage.memlen; |
| 200 | } |
| 201 | |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 202 | int cbfs_prog_stage_load(struct prog *pstage) |
| 203 | { |
| 204 | struct cbfs_stage stage; |
| 205 | uint8_t *load; |
| 206 | void *entry; |
| 207 | size_t fsize; |
| 208 | size_t foffset; |
Aaron Durbin | 37a5d15 | 2015-09-17 16:09:30 -0500 | [diff] [blame] | 209 | const struct region_device *fh = prog_rdev(pstage); |
Hung-Te Lin | 6fe0cab | 2013-01-22 18:57:56 +0800 | [diff] [blame] | 210 | |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 211 | if (rdev_readat(fh, &stage, 0, sizeof(stage)) != sizeof(stage)) |
Julius Werner | b29bd27b | 2015-12-03 11:29:12 -0800 | [diff] [blame] | 212 | return -1; |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 213 | |
| 214 | fsize = region_device_sz(fh); |
| 215 | fsize -= sizeof(stage); |
| 216 | foffset = 0; |
| 217 | foffset += sizeof(stage); |
| 218 | |
| 219 | assert(fsize == stage.len); |
| 220 | |
| 221 | /* Note: cbfs_stage fields are currently in the endianness of the |
| 222 | * running processor. */ |
| 223 | load = (void *)(uintptr_t)stage.load; |
| 224 | entry = (void *)(uintptr_t)stage.entry; |
| 225 | |
Aaron Durbin | ed253c8 | 2015-10-07 17:22:42 -0500 | [diff] [blame] | 226 | /* Hacky way to not load programs over read only media. The stages |
| 227 | * that would hit this path initialize themselves. */ |
Furquan Shaikh | d5583a5 | 2016-06-01 01:53:18 -0700 | [diff] [blame] | 228 | if (ENV_VERSTAGE && !IS_ENABLED(CONFIG_NO_XIP_EARLY_STAGES) && |
Aaron Durbin | 16c173f | 2016-08-11 14:04:10 -0500 | [diff] [blame] | 229 | IS_ENABLED(CONFIG_BOOT_DEVICE_MEMORY_MAPPED)) { |
Aaron Durbin | ed253c8 | 2015-10-07 17:22:42 -0500 | [diff] [blame] | 230 | void *mapping = rdev_mmap(fh, foffset, fsize); |
| 231 | rdev_munmap(fh, mapping); |
| 232 | if (mapping == load) |
| 233 | goto out; |
| 234 | } |
| 235 | |
Julius Werner | 09f2921 | 2015-09-29 13:51:35 -0700 | [diff] [blame] | 236 | fsize = cbfs_load_and_decompress(fh, foffset, fsize, load, |
| 237 | stage.memlen, stage.compression); |
| 238 | if (!fsize) |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 239 | return -1; |
| 240 | |
| 241 | /* Clear area not covered by file. */ |
| 242 | memset(&load[fsize], 0, stage.memlen - fsize); |
| 243 | |
Aaron Durbin | 096f457 | 2016-03-31 13:49:00 -0500 | [diff] [blame] | 244 | prog_segment_loaded((uintptr_t)load, stage.memlen, SEG_FINAL); |
Aaron Durbin | ed253c8 | 2015-10-07 17:22:42 -0500 | [diff] [blame] | 245 | |
| 246 | out: |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 247 | prog_set_area(pstage, load, stage.memlen); |
| 248 | prog_set_entry(pstage, entry, NULL); |
| 249 | |
| 250 | return 0; |
Hung-Te Lin | 6fe0cab | 2013-01-22 18:57:56 +0800 | [diff] [blame] | 251 | } |
Aaron Durbin | 6d720f3 | 2015-12-08 17:00:23 -0600 | [diff] [blame] | 252 | |
Patrick Georgi | 58a150a | 2016-05-02 17:22:29 +0800 | [diff] [blame] | 253 | /* This only supports the "COREBOOT" fmap region. */ |
Aaron Durbin | 6d720f3 | 2015-12-08 17:00:23 -0600 | [diff] [blame] | 254 | static int cbfs_master_header_props(struct cbfs_props *props) |
| 255 | { |
| 256 | struct cbfs_header header; |
| 257 | const struct region_device *bdev; |
| 258 | int32_t rel_offset; |
| 259 | size_t offset; |
| 260 | |
| 261 | bdev = boot_device_ro(); |
| 262 | |
| 263 | if (bdev == NULL) |
| 264 | return -1; |
| 265 | |
Patrick Georgi | 58a150a | 2016-05-02 17:22:29 +0800 | [diff] [blame] | 266 | size_t fmap_top = ___FMAP__COREBOOT_BASE + ___FMAP__COREBOOT_SIZE; |
| 267 | |
Aaron Durbin | 6d720f3 | 2015-12-08 17:00:23 -0600 | [diff] [blame] | 268 | /* Find location of header using signed 32-bit offset from |
| 269 | * end of CBFS region. */ |
Patrick Georgi | 58a150a | 2016-05-02 17:22:29 +0800 | [diff] [blame] | 270 | offset = fmap_top - sizeof(int32_t); |
Aaron Durbin | 6d720f3 | 2015-12-08 17:00:23 -0600 | [diff] [blame] | 271 | if (rdev_readat(bdev, &rel_offset, offset, sizeof(int32_t)) < 0) |
| 272 | return -1; |
| 273 | |
Patrick Georgi | 58a150a | 2016-05-02 17:22:29 +0800 | [diff] [blame] | 274 | offset = fmap_top + rel_offset; |
Aaron Durbin | 6d720f3 | 2015-12-08 17:00:23 -0600 | [diff] [blame] | 275 | if (rdev_readat(bdev, &header, offset, sizeof(header)) < 0) |
| 276 | return -1; |
| 277 | |
| 278 | header.magic = ntohl(header.magic); |
| 279 | header.romsize = ntohl(header.romsize); |
| 280 | header.offset = ntohl(header.offset); |
| 281 | |
| 282 | if (header.magic != CBFS_HEADER_MAGIC) |
| 283 | return -1; |
| 284 | |
| 285 | props->offset = header.offset; |
| 286 | props->size = header.romsize; |
| 287 | props->size -= props->offset; |
| 288 | |
| 289 | printk(BIOS_SPEW, "CBFS @ %zx size %zx\n", props->offset, props->size); |
| 290 | |
| 291 | return 0; |
| 292 | } |
| 293 | |
| 294 | /* This struct is marked as weak to allow a particular platform to |
| 295 | * override the master header logic. This implementation should work for most |
| 296 | * devices. */ |
| 297 | const struct cbfs_locator __attribute__((weak)) cbfs_master_header_locator = { |
| 298 | .name = "Master Header Locator", |
| 299 | .locate = cbfs_master_header_props, |
| 300 | }; |
| 301 | |
| 302 | extern const struct cbfs_locator vboot_locator; |
| 303 | |
| 304 | static const struct cbfs_locator *locators[] = { |
Furquan Shaikh | 2a12e2e | 2016-07-25 11:48:03 -0700 | [diff] [blame] | 305 | #if CONFIG_VBOOT |
Aaron Durbin | 6d720f3 | 2015-12-08 17:00:23 -0600 | [diff] [blame] | 306 | &vboot_locator, |
| 307 | #endif |
| 308 | &cbfs_master_header_locator, |
| 309 | }; |
| 310 | |
| 311 | int cbfs_boot_region_properties(struct cbfs_props *props) |
| 312 | { |
| 313 | int i; |
| 314 | |
| 315 | boot_device_init(); |
| 316 | |
| 317 | for (i = 0; i < ARRAY_SIZE(locators); i++) { |
| 318 | const struct cbfs_locator *ops; |
| 319 | |
| 320 | ops = locators[i]; |
| 321 | |
| 322 | if (ops->locate == NULL) |
| 323 | continue; |
| 324 | |
| 325 | if (ops->locate(props)) |
| 326 | continue; |
| 327 | |
| 328 | LOG("'%s' located CBFS at [%zx:%zx)\n", |
| 329 | ops->name, props->offset, props->offset + props->size); |
| 330 | |
| 331 | return 0; |
| 332 | } |
| 333 | |
| 334 | return -1; |
| 335 | } |
| 336 | |
| 337 | void cbfs_prepare_program_locate(void) |
| 338 | { |
| 339 | int i; |
| 340 | |
| 341 | boot_device_init(); |
| 342 | |
| 343 | for (i = 0; i < ARRAY_SIZE(locators); i++) { |
| 344 | if (locators[i]->prepare == NULL) |
| 345 | continue; |
| 346 | locators[i]->prepare(); |
| 347 | } |
| 348 | } |