Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [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. |
| 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. |
| 18 | */ |
| 19 | |
| 20 | #include <boot_device.h> |
| 21 | #include <cbfs.h> |
| 22 | #include <console/console.h> |
| 23 | #include <endian.h> |
| 24 | #include <region.h> |
| 25 | |
| 26 | /* This function is marked as weak to allow a particular platform to |
| 27 | * override the logic. This implementation should work for most devices. */ |
| 28 | int __attribute__((weak)) cbfs_boot_region_properties(struct cbfs_props *props) |
| 29 | { |
| 30 | struct cbfs_header header; |
| 31 | const struct region_device *bdev; |
| 32 | int32_t rel_offset; |
| 33 | size_t offset; |
| 34 | |
| 35 | bdev = boot_device_ro(); |
| 36 | |
| 37 | if (bdev == NULL) |
| 38 | return -1; |
| 39 | |
| 40 | /* Find location of header using signed 32-bit offset from |
| 41 | * end of CBFS region. */ |
| 42 | offset = CONFIG_CBFS_SIZE - sizeof(int32_t); |
| 43 | if (rdev_readat(bdev, &rel_offset, offset, sizeof(int32_t)) < 0) |
| 44 | return -1; |
| 45 | |
| 46 | offset = CONFIG_CBFS_SIZE + rel_offset; |
| 47 | if (rdev_readat(bdev, &header, offset, sizeof(header)) < 0) |
| 48 | return -1; |
| 49 | |
| 50 | header.magic = ntohl(header.magic); |
| 51 | header.romsize = ntohl(header.romsize); |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 52 | header.offset = ntohl(header.offset); |
| 53 | |
| 54 | if (header.magic != CBFS_HEADER_MAGIC) |
| 55 | return -1; |
| 56 | |
Aaron Durbin | 899d13d | 2015-05-15 23:39:23 -0500 | [diff] [blame] | 57 | props->offset = header.offset; |
| 58 | props->size = header.romsize; |
| 59 | props->size -= props->offset; |
| 60 | |
| 61 | printk(BIOS_SPEW, "CBFS @ %zx size %zx\n", props->offset, props->size); |
| 62 | |
| 63 | return 0; |
| 64 | } |