Simon Glass | 3c10dc9 | 2019-12-06 21:41:34 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: Intel |
| 2 | /* |
| 3 | * Access to binman information at runtime |
| 4 | * |
| 5 | * Copyright 2019 Google LLC |
| 6 | * Written by Simon Glass <sjg@chromium.org> |
| 7 | */ |
| 8 | |
| 9 | #include <common.h> |
| 10 | #include <binman.h> |
| 11 | #include <dm.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 12 | #include <log.h> |
Simon Glass | 336d461 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 13 | #include <malloc.h> |
Simon Glass | 8f9877d | 2020-07-07 21:32:04 -0600 | [diff] [blame] | 14 | #include <mapmem.h> |
Simon Glass | 3c10dc9 | 2019-12-06 21:41:34 -0700 | [diff] [blame] | 15 | |
Simon Glass | db6fb7d | 2020-07-07 21:32:02 -0600 | [diff] [blame] | 16 | /** |
| 17 | * struct binman_info - Information needed by the binman library |
| 18 | * |
| 19 | * @image: Node describing the image we are running from |
| 20 | * @rom_offset: Offset from an image_pos to the memory-mapped address, or |
| 21 | * ROM_OFFSET_NONE if the ROM is not memory-mapped. Can be positive or |
| 22 | * negative |
| 23 | */ |
Simon Glass | 3c10dc9 | 2019-12-06 21:41:34 -0700 | [diff] [blame] | 24 | struct binman_info { |
| 25 | ofnode image; |
Simon Glass | db6fb7d | 2020-07-07 21:32:02 -0600 | [diff] [blame] | 26 | int rom_offset; |
Simon Glass | 3c10dc9 | 2019-12-06 21:41:34 -0700 | [diff] [blame] | 27 | }; |
| 28 | |
Simon Glass | db6fb7d | 2020-07-07 21:32:02 -0600 | [diff] [blame] | 29 | #define ROM_OFFSET_NONE (-1) |
| 30 | |
Simon Glass | 3c10dc9 | 2019-12-06 21:41:34 -0700 | [diff] [blame] | 31 | static struct binman_info *binman; |
| 32 | |
Simon Glass | 956a908 | 2020-07-07 21:32:03 -0600 | [diff] [blame] | 33 | static int binman_entry_find_internal(ofnode node, const char *name, |
| 34 | struct binman_entry *entry) |
Simon Glass | 3c10dc9 | 2019-12-06 21:41:34 -0700 | [diff] [blame] | 35 | { |
Simon Glass | 3c10dc9 | 2019-12-06 21:41:34 -0700 | [diff] [blame] | 36 | int ret; |
| 37 | |
Simon Glass | 3c10dc9 | 2019-12-06 21:41:34 -0700 | [diff] [blame] | 38 | if (!ofnode_valid(node)) |
Simon Glass | 956a908 | 2020-07-07 21:32:03 -0600 | [diff] [blame] | 39 | node = binman->image; |
| 40 | node = ofnode_find_subnode(node, name); |
| 41 | if (!ofnode_valid(node)) |
| 42 | return log_msg_ret("node", -ENOENT); |
Simon Glass | 3c10dc9 | 2019-12-06 21:41:34 -0700 | [diff] [blame] | 43 | |
| 44 | ret = ofnode_read_u32(node, "image-pos", &entry->image_pos); |
| 45 | if (ret) |
Simon Glass | f4f4123 | 2020-09-27 18:46:19 -0600 | [diff] [blame] | 46 | return log_msg_ret("image-pos", ret); |
Simon Glass | 3c10dc9 | 2019-12-06 21:41:34 -0700 | [diff] [blame] | 47 | ret = ofnode_read_u32(node, "size", &entry->size); |
| 48 | if (ret) |
Simon Glass | 956a908 | 2020-07-07 21:32:03 -0600 | [diff] [blame] | 49 | return log_msg_ret("size", ret); |
Simon Glass | 3c10dc9 | 2019-12-06 21:41:34 -0700 | [diff] [blame] | 50 | |
| 51 | return 0; |
| 52 | } |
| 53 | |
Simon Glass | 956a908 | 2020-07-07 21:32:03 -0600 | [diff] [blame] | 54 | int binman_entry_find(const char *name, struct binman_entry *entry) |
| 55 | { |
| 56 | return binman_entry_find_internal(binman->image, name, entry); |
| 57 | } |
| 58 | |
Simon Glass | 8f9877d | 2020-07-07 21:32:04 -0600 | [diff] [blame] | 59 | int binman_entry_map(ofnode parent, const char *name, void **bufp, int *sizep) |
| 60 | { |
| 61 | struct binman_entry entry; |
| 62 | int ret; |
| 63 | |
| 64 | if (binman->rom_offset == ROM_OFFSET_NONE) |
| 65 | return -EPERM; |
| 66 | ret = binman_entry_find_internal(parent, name, &entry); |
| 67 | if (ret) |
| 68 | return log_msg_ret("entry", ret); |
| 69 | if (sizep) |
| 70 | *sizep = entry.size; |
| 71 | *bufp = map_sysmem(entry.image_pos + binman->rom_offset, entry.size); |
| 72 | |
| 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | ofnode binman_section_find_node(const char *name) |
| 77 | { |
| 78 | return ofnode_find_subnode(binman->image, name); |
| 79 | } |
| 80 | |
Simon Glass | db6fb7d | 2020-07-07 21:32:02 -0600 | [diff] [blame] | 81 | void binman_set_rom_offset(int rom_offset) |
| 82 | { |
| 83 | binman->rom_offset = rom_offset; |
| 84 | } |
| 85 | |
Simon Glass | f4f4123 | 2020-09-27 18:46:19 -0600 | [diff] [blame] | 86 | int binman_get_rom_offset(void) |
| 87 | { |
| 88 | return binman->rom_offset; |
| 89 | } |
| 90 | |
Simon Glass | 3c10dc9 | 2019-12-06 21:41:34 -0700 | [diff] [blame] | 91 | int binman_init(void) |
| 92 | { |
| 93 | binman = malloc(sizeof(struct binman_info)); |
| 94 | if (!binman) |
| 95 | return log_msg_ret("space for binman", -ENOMEM); |
| 96 | binman->image = ofnode_path("/binman"); |
| 97 | if (!ofnode_valid(binman->image)) |
| 98 | return log_msg_ret("binman node", -EINVAL); |
Simon Glass | 8318754 | 2020-09-27 18:46:20 -0600 | [diff] [blame] | 99 | if (ofnode_read_bool(binman->image, "multiple-images")) { |
| 100 | ofnode node = ofnode_first_subnode(binman->image); |
| 101 | |
| 102 | if (!ofnode_valid(node)) |
| 103 | return log_msg_ret("first image", -ENOENT); |
| 104 | binman->image = node; |
| 105 | } |
Michal Simek | 6723b4c | 2020-08-26 15:13:14 +0200 | [diff] [blame] | 106 | binman_set_rom_offset(ROM_OFFSET_NONE); |
Michal Simek | a9e73d2 | 2020-11-23 09:08:19 +0100 | [diff] [blame] | 107 | |
Simon Glass | 3c10dc9 | 2019-12-06 21:41:34 -0700 | [diff] [blame] | 108 | return 0; |
| 109 | } |