Patrick Georgi | 89f73dc | 2015-07-09 13:57:00 +0200 | [diff] [blame^] | 1 | /* |
| 2 | * This file is part of the libpayload project. |
| 3 | * |
| 4 | * Copyright (C) 2015 Google Inc. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions |
| 8 | * are met: |
| 9 | * 1. Redistributions of source code must retain the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer. |
| 11 | * 2. Redistributions in binary form must reproduce the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer in the |
| 13 | * documentation and/or other materials provided with the distribution. |
| 14 | * 3. The name of the author may not be used to endorse or promote products |
| 15 | * derived from this software without specific prior written permission. |
| 16 | * |
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 27 | * SUCH DAMAGE. |
| 28 | */ |
| 29 | |
| 30 | #include <libpayload-config.h> |
| 31 | #include <libpayload.h> |
| 32 | #include <coreboot_tables.h> |
| 33 | #include <cbfs.h> |
| 34 | #include <fmap_serialized.h> |
| 35 | #include <stdint.h> |
| 36 | |
| 37 | int fmap_region_by_name(const uint32_t fmap_offset, const char * const name, |
| 38 | uint32_t * const offset, uint32_t * const size) |
| 39 | { |
| 40 | int i; |
| 41 | |
| 42 | struct fmap *fmap; |
| 43 | struct fmap fmap_head; |
| 44 | struct cbfs_media default_media; |
| 45 | struct cbfs_media *media = &default_media; |
| 46 | |
| 47 | if (init_default_cbfs_media(media) != 0) |
| 48 | return -1; |
| 49 | |
| 50 | media->open(media); |
| 51 | |
| 52 | if (!media->read(media, &fmap_head, fmap_offset, sizeof(fmap_head))) |
| 53 | return -1; |
| 54 | |
| 55 | if (memcmp(fmap_head.signature, FMAP_SIGNATURE, sizeof(fmap_head.signature))) { |
| 56 | return -1; |
| 57 | } |
| 58 | |
| 59 | int fmap_size = sizeof(*fmap) + |
| 60 | fmap_head.nareas * sizeof(struct fmap_area); |
| 61 | |
| 62 | fmap = malloc(fmap_size); |
| 63 | if (!fmap) |
| 64 | return -1; |
| 65 | |
| 66 | if (!media->read(media, fmap, fmap_offset, fmap_size)) |
| 67 | goto err; |
| 68 | |
| 69 | media->close(media); |
| 70 | |
| 71 | for (i = 0; i < fmap->nareas; i++) { |
| 72 | if (strcmp((const char *)fmap->areas[i].name, name) != 0) |
| 73 | continue; |
| 74 | if (offset) |
| 75 | *offset = fmap->areas[i].offset; |
| 76 | if (size) |
| 77 | *size = fmap->areas[i].size; |
| 78 | free(fmap); |
| 79 | return 0; |
| 80 | } |
| 81 | err: |
| 82 | free(fmap); |
| 83 | return -1; |
| 84 | } |