mainboard: Clean up boot_option/reboot_bits in cmos.layout
[coreboot.git] / src / lib / cbfs.c
blob7a0f1870926a6bb795dec1f4485741ba4cf4667c
1 /*
2 * This file is part of the coreboot project.
4 * Copyright (C) 2011 secunet Security Networks AG
5 * Copyright 2015 Google Inc.
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.
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.
17 #include <assert.h>
18 #include <string.h>
19 #include <stdlib.h>
20 #include <boot_device.h>
21 #include <cbfs.h>
22 #include <commonlib/compression.h>
23 #include <endian.h>
24 #include <lib.h>
25 #include <symbols.h>
26 #include <timestamp.h>
28 #include "fmap_config.h"
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
38 int cbfs_boot_locate(struct cbfsf *fh, const char *name, uint32_t *type)
40 struct region_device rdev;
41 const struct region_device *boot_dev;
42 struct cbfs_props props;
44 if (cbfs_boot_region_properties(&props))
45 return -1;
47 /* All boot CBFS operations are performed using the RO devie. */
48 boot_dev = boot_device_ro();
50 if (boot_dev == NULL)
51 return -1;
53 if (rdev_chain(&rdev, boot_dev, props.offset, props.size))
54 return -1;
56 return cbfs_locate(fh, &rdev, name, type);
59 void *cbfs_boot_map_with_leak(const char *name, uint32_t type, size_t *size)
61 struct cbfsf fh;
62 size_t fsize;
64 if (cbfs_boot_locate(&fh, name, &type))
65 return NULL;
67 fsize = region_device_sz(&fh.data);
69 if (size != NULL)
70 *size = fsize;
72 return rdev_mmap(&fh.data, 0, fsize);
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)
78 size_t out_size;
80 switch (compression) {
81 case CBFS_COMPRESS_NONE:
82 if (rdev_readat(rdev, buffer, offset, in_size) != in_size)
83 return 0;
84 return in_size;
86 case CBFS_COMPRESS_LZ4:
87 if ((ENV_BOOTBLOCK || ENV_VERSTAGE) &&
88 !IS_ENABLED(CONFIG_COMPRESS_PRERAM_STAGES))
89 return 0;
91 /* Load the compressed image to the end of the available memory
92 * area for in-place decompression. It is the responsibility of
93 * the caller to ensure that buffer_size is large enough
94 * (see compression.h, guaranteed by cbfstool for stages). */
95 void *compr_start = buffer + buffer_size - in_size;
96 if (rdev_readat(rdev, compr_start, offset, in_size) != in_size)
97 return 0;
99 timestamp_add_now(TS_START_ULZ4F);
100 out_size = ulz4fn(compr_start, in_size, buffer, buffer_size);
101 timestamp_add_now(TS_END_ULZ4F);
102 return out_size;
104 case CBFS_COMPRESS_LZMA:
105 if (ENV_BOOTBLOCK || ENV_VERSTAGE)
106 return 0;
107 if ((ENV_ROMSTAGE || ENV_POSTCAR)
108 && !IS_ENABLED(CONFIG_COMPRESS_RAMSTAGE))
109 return 0;
110 void *map = rdev_mmap(rdev, offset, in_size);
111 if (map == NULL)
112 return 0;
114 /* Note: timestamp not useful for memory-mapped media (x86) */
115 timestamp_add_now(TS_START_ULZMA);
116 out_size = ulzman(map, in_size, buffer, buffer_size);
117 timestamp_add_now(TS_END_ULZMA);
119 rdev_munmap(rdev, map);
121 return out_size;
123 default:
124 return 0;
128 static inline int tohex4(unsigned int c)
130 return (c <= 9) ? (c + '0') : (c - 10 + 'a');
133 static void tohex16(unsigned int val, char* dest)
135 dest[0] = tohex4(val>>12);
136 dest[1] = tohex4((val>>8) & 0xf);
137 dest[2] = tohex4((val>>4) & 0xf);
138 dest[3] = tohex4(val & 0xf);
141 void *cbfs_boot_map_optionrom(uint16_t vendor, uint16_t device)
143 char name[17] = "pciXXXX,XXXX.rom";
145 tohex16(vendor, name+3);
146 tohex16(device, name+8);
148 return cbfs_boot_map_with_leak(name, CBFS_TYPE_OPTIONROM, NULL);
151 void *cbfs_boot_load_stage_by_name(const char *name)
153 struct cbfsf fh;
154 struct prog stage = PROG_INIT(PROG_UNKNOWN, name);
155 uint32_t type = CBFS_TYPE_STAGE;
157 if (cbfs_boot_locate(&fh, name, &type))
158 return NULL;
160 /* Chain data portion in the prog. */
161 cbfs_file_data(prog_rdev(&stage), &fh);
163 if (cbfs_prog_stage_load(&stage))
164 return NULL;
166 return prog_entry(&stage);
169 int cbfs_prog_stage_load(struct prog *pstage)
171 struct cbfs_stage stage;
172 uint8_t *load;
173 void *entry;
174 size_t fsize;
175 size_t foffset;
176 const struct region_device *fh = prog_rdev(pstage);
178 if (rdev_readat(fh, &stage, 0, sizeof(stage)) != sizeof(stage))
179 return -1;
181 fsize = region_device_sz(fh);
182 fsize -= sizeof(stage);
183 foffset = 0;
184 foffset += sizeof(stage);
186 assert(fsize == stage.len);
188 /* Note: cbfs_stage fields are currently in the endianness of the
189 * running processor. */
190 load = (void *)(uintptr_t)stage.load;
191 entry = (void *)(uintptr_t)stage.entry;
193 /* Hacky way to not load programs over read only media. The stages
194 * that would hit this path initialize themselves. */
195 if (ENV_VERSTAGE && !IS_ENABLED(CONFIG_NO_XIP_EARLY_STAGES) &&
196 IS_ENABLED(CONFIG_SPI_FLASH_MEMORY_MAPPED)) {
197 void *mapping = rdev_mmap(fh, foffset, fsize);
198 rdev_munmap(fh, mapping);
199 if (mapping == load)
200 goto out;
203 fsize = cbfs_load_and_decompress(fh, foffset, fsize, load,
204 stage.memlen, stage.compression);
205 if (!fsize)
206 return -1;
208 /* Clear area not covered by file. */
209 memset(&load[fsize], 0, stage.memlen - fsize);
211 prog_segment_loaded((uintptr_t)load, stage.memlen, SEG_FINAL);
213 out:
214 prog_set_area(pstage, load, stage.memlen);
215 prog_set_entry(pstage, entry, NULL);
217 return 0;
220 /* This only supports the "COREBOOT" fmap region. */
221 static int cbfs_master_header_props(struct cbfs_props *props)
223 struct cbfs_header header;
224 const struct region_device *bdev;
225 int32_t rel_offset;
226 size_t offset;
228 bdev = boot_device_ro();
230 if (bdev == NULL)
231 return -1;
233 size_t fmap_top = ___FMAP__COREBOOT_BASE + ___FMAP__COREBOOT_SIZE;
235 /* Find location of header using signed 32-bit offset from
236 * end of CBFS region. */
237 offset = fmap_top - sizeof(int32_t);
238 if (rdev_readat(bdev, &rel_offset, offset, sizeof(int32_t)) < 0)
239 return -1;
241 offset = fmap_top + rel_offset;
242 if (rdev_readat(bdev, &header, offset, sizeof(header)) < 0)
243 return -1;
245 header.magic = ntohl(header.magic);
246 header.romsize = ntohl(header.romsize);
247 header.offset = ntohl(header.offset);
249 if (header.magic != CBFS_HEADER_MAGIC)
250 return -1;
252 props->offset = header.offset;
253 props->size = header.romsize;
254 props->size -= props->offset;
256 printk(BIOS_SPEW, "CBFS @ %zx size %zx\n", props->offset, props->size);
258 return 0;
261 /* This struct is marked as weak to allow a particular platform to
262 * override the master header logic. This implementation should work for most
263 * devices. */
264 const struct cbfs_locator __attribute__((weak)) cbfs_master_header_locator = {
265 .name = "Master Header Locator",
266 .locate = cbfs_master_header_props,
269 extern const struct cbfs_locator vboot_locator;
271 static const struct cbfs_locator *locators[] = {
272 #if CONFIG_VBOOT
273 &vboot_locator,
274 #endif
275 &cbfs_master_header_locator,
278 int cbfs_boot_region_properties(struct cbfs_props *props)
280 int i;
282 boot_device_init();
284 for (i = 0; i < ARRAY_SIZE(locators); i++) {
285 const struct cbfs_locator *ops;
287 ops = locators[i];
289 if (ops->locate == NULL)
290 continue;
292 if (ops->locate(props))
293 continue;
295 LOG("'%s' located CBFS at [%zx:%zx)\n",
296 ops->name, props->offset, props->offset + props->size);
298 return 0;
301 return -1;
304 void cbfs_prepare_program_locate(void)
306 int i;
308 boot_device_init();
310 for (i = 0; i < ARRAY_SIZE(locators); i++) {
311 if (locators[i]->prepare == NULL)
312 continue;
313 locators[i]->prepare();