blob: 6c98954ff295c6bd6e0b2bcfa02d313029294dbf [file] [log] [blame]
Gabe Black396b0722013-09-26 16:22:09 -07001/*
2 * This file is part of the coreboot project.
3 *
Aaron Durbinc6588c52015-05-15 13:15:34 -05004 * Copyright 2015 Google Inc.
Gabe Black396b0722013-09-26 16:22:09 -07005 *
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
Patrick Georgib890a122015-03-26 15:17:45 +010017 * Foundation, Inc.
Gabe Black396b0722013-09-26 16:22:09 -070018 */
19
Aaron Durbinc6588c52015-05-15 13:15:34 -050020#include <boot_device.h>
Aaron Durbin899d13d2015-05-15 23:39:23 -050021#include <console/console.h>
22#include <cbfs.h>
23#include <endian.h>
24#include <stdlib.h>
Gabe Black396b0722013-09-26 16:22:09 -070025
Aaron Durbinc6588c52015-05-15 13:15:34 -050026/* The ROM is memory mapped just below 4GiB. Form a pointer for the base. */
27#define rom_base ((void *)(uintptr_t)(-(int32_t)CONFIG_ROM_SIZE))
Gabe Black396b0722013-09-26 16:22:09 -070028
Aaron Durbinc6588c52015-05-15 13:15:34 -050029static const struct mem_region_device boot_dev =
30 MEM_REGION_DEV_INIT(rom_base, CONFIG_ROM_SIZE);
31
32const struct region_device *boot_device_ro(void)
Gabe Black396b0722013-09-26 16:22:09 -070033{
Aaron Durbinc6588c52015-05-15 13:15:34 -050034 return &boot_dev.rdev;
Gabe Black396b0722013-09-26 16:22:09 -070035}
Aaron Durbin899d13d2015-05-15 23:39:23 -050036
37int cbfs_boot_region_properties(struct cbfs_props *props)
38{
39 struct cbfs_header header;
40 int32_t offset;
41 const struct region_device *bdev;
42
43 bdev = boot_device_ro();
44
45 rdev_readat(bdev, &offset, CONFIG_ROM_SIZE - sizeof(offset),
46 sizeof(offset));
47
48 /* The offset is relative to the end of the media. */
49 offset += CONFIG_ROM_SIZE;
50
51 rdev_readat(bdev, &header , offset, sizeof(header));
52
53 header.magic = ntohl(header.magic);
54 header.romsize = ntohl(header.romsize);
55 header.bootblocksize = ntohl(header.bootblocksize);
Aaron Durbin899d13d2015-05-15 23:39:23 -050056 header.offset = ntohl(header.offset);
57
58 if (header.magic != CBFS_HEADER_MAGIC)
59 return -1;
60
Aaron Durbin899d13d2015-05-15 23:39:23 -050061 props->offset = header.offset;
62 if (CONFIG_ROM_SIZE != header.romsize)
63 props->size = CONFIG_ROM_SIZE;
64 else
65 props->size = header.romsize;
66 props->size -= props->offset;
67 props->size -= header.bootblocksize;
Patrick Georgi4d3e4c42015-07-14 22:28:27 +020068 props->size = ALIGN_DOWN(props->size, 64);
Aaron Durbin899d13d2015-05-15 23:39:23 -050069
70 printk(BIOS_DEBUG, "CBFS @ %zx size %zx\n", props->offset, props->size);
71
72 return 0;
73}