Simon Glass | dd7fb9b | 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 | |
Simon Glass | dd7fb9b | 2019-12-06 21:41:34 -0700 | [diff] [blame] | 9 | #include <binman.h> |
| 10 | #include <dm.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 11 | #include <log.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 12 | #include <malloc.h> |
Simon Glass | c4d9faf | 2020-07-07 21:32:04 -0600 | [diff] [blame] | 13 | #include <mapmem.h> |
Simon Glass | dd7fb9b | 2019-12-06 21:41:34 -0700 | [diff] [blame] | 14 | |
Simon Glass | c0ef789 | 2020-07-07 21:32:02 -0600 | [diff] [blame] | 15 | /** |
| 16 | * struct binman_info - Information needed by the binman library |
| 17 | * |
| 18 | * @image: Node describing the image we are running from |
| 19 | * @rom_offset: Offset from an image_pos to the memory-mapped address, or |
| 20 | * ROM_OFFSET_NONE if the ROM is not memory-mapped. Can be positive or |
| 21 | * negative |
| 22 | */ |
Simon Glass | dd7fb9b | 2019-12-06 21:41:34 -0700 | [diff] [blame] | 23 | struct binman_info { |
| 24 | ofnode image; |
Simon Glass | c0ef789 | 2020-07-07 21:32:02 -0600 | [diff] [blame] | 25 | int rom_offset; |
Simon Glass | dd7fb9b | 2019-12-06 21:41:34 -0700 | [diff] [blame] | 26 | }; |
| 27 | |
Simon Glass | c0ef789 | 2020-07-07 21:32:02 -0600 | [diff] [blame] | 28 | #define ROM_OFFSET_NONE (-1) |
| 29 | |
Simon Glass | dd7fb9b | 2019-12-06 21:41:34 -0700 | [diff] [blame] | 30 | static struct binman_info *binman; |
| 31 | |
Simon Glass | cb31d76 | 2021-01-13 20:29:56 -0700 | [diff] [blame] | 32 | /** |
| 33 | * find_image_node() - Find the top-level binman node |
| 34 | * |
| 35 | * Finds the binman node which can be used to load entries. The correct node |
| 36 | * depends on whether multiple-images is in use. |
| 37 | * |
| 38 | * @nodep: Returns the node found, on success |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 39 | * Return: 0 if OK, , -EINVAL if there is no /binman node, -ECHILD if multiple |
Simon Glass | cb31d76 | 2021-01-13 20:29:56 -0700 | [diff] [blame] | 40 | * images are being used but the first image is not available |
| 41 | */ |
| 42 | static int find_image_node(ofnode *nodep) |
| 43 | { |
| 44 | ofnode node; |
| 45 | |
| 46 | node = ofnode_path("/binman"); |
| 47 | if (!ofnode_valid(node)) |
| 48 | return log_msg_ret("binman node", -EINVAL); |
| 49 | if (ofnode_read_bool(node, "multiple-images")) { |
| 50 | node = ofnode_first_subnode(node); |
| 51 | |
| 52 | if (!ofnode_valid(node)) |
| 53 | return log_msg_ret("first image", -ECHILD); |
| 54 | } |
| 55 | *nodep = node; |
| 56 | |
| 57 | return 0; |
| 58 | } |
| 59 | |
Simon Glass | c7380c6 | 2020-07-07 21:32:03 -0600 | [diff] [blame] | 60 | static int binman_entry_find_internal(ofnode node, const char *name, |
| 61 | struct binman_entry *entry) |
Simon Glass | dd7fb9b | 2019-12-06 21:41:34 -0700 | [diff] [blame] | 62 | { |
Simon Glass | dd7fb9b | 2019-12-06 21:41:34 -0700 | [diff] [blame] | 63 | int ret; |
| 64 | |
Simon Glass | dd7fb9b | 2019-12-06 21:41:34 -0700 | [diff] [blame] | 65 | if (!ofnode_valid(node)) |
Simon Glass | c7380c6 | 2020-07-07 21:32:03 -0600 | [diff] [blame] | 66 | node = binman->image; |
| 67 | node = ofnode_find_subnode(node, name); |
| 68 | if (!ofnode_valid(node)) |
| 69 | return log_msg_ret("node", -ENOENT); |
Simon Glass | dd7fb9b | 2019-12-06 21:41:34 -0700 | [diff] [blame] | 70 | |
| 71 | ret = ofnode_read_u32(node, "image-pos", &entry->image_pos); |
| 72 | if (ret) |
Simon Glass | dff2ea1 | 2020-09-27 18:46:19 -0600 | [diff] [blame] | 73 | return log_msg_ret("image-pos", ret); |
Simon Glass | dd7fb9b | 2019-12-06 21:41:34 -0700 | [diff] [blame] | 74 | ret = ofnode_read_u32(node, "size", &entry->size); |
| 75 | if (ret) |
Simon Glass | c7380c6 | 2020-07-07 21:32:03 -0600 | [diff] [blame] | 76 | return log_msg_ret("size", ret); |
Simon Glass | dd7fb9b | 2019-12-06 21:41:34 -0700 | [diff] [blame] | 77 | |
| 78 | return 0; |
| 79 | } |
| 80 | |
Simon Glass | c7380c6 | 2020-07-07 21:32:03 -0600 | [diff] [blame] | 81 | int binman_entry_find(const char *name, struct binman_entry *entry) |
| 82 | { |
| 83 | return binman_entry_find_internal(binman->image, name, entry); |
| 84 | } |
| 85 | |
Simon Glass | c4d9faf | 2020-07-07 21:32:04 -0600 | [diff] [blame] | 86 | int binman_entry_map(ofnode parent, const char *name, void **bufp, int *sizep) |
| 87 | { |
| 88 | struct binman_entry entry; |
| 89 | int ret; |
| 90 | |
| 91 | if (binman->rom_offset == ROM_OFFSET_NONE) |
| 92 | return -EPERM; |
| 93 | ret = binman_entry_find_internal(parent, name, &entry); |
| 94 | if (ret) |
| 95 | return log_msg_ret("entry", ret); |
| 96 | if (sizep) |
| 97 | *sizep = entry.size; |
| 98 | *bufp = map_sysmem(entry.image_pos + binman->rom_offset, entry.size); |
| 99 | |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | ofnode binman_section_find_node(const char *name) |
| 104 | { |
| 105 | return ofnode_find_subnode(binman->image, name); |
| 106 | } |
| 107 | |
Simon Glass | c0ef789 | 2020-07-07 21:32:02 -0600 | [diff] [blame] | 108 | void binman_set_rom_offset(int rom_offset) |
| 109 | { |
| 110 | binman->rom_offset = rom_offset; |
| 111 | } |
| 112 | |
Simon Glass | dff2ea1 | 2020-09-27 18:46:19 -0600 | [diff] [blame] | 113 | int binman_get_rom_offset(void) |
| 114 | { |
| 115 | return binman->rom_offset; |
| 116 | } |
| 117 | |
Simon Glass | 23c0ea5 | 2021-01-13 20:29:57 -0700 | [diff] [blame] | 118 | int binman_select_subnode(const char *name) |
| 119 | { |
| 120 | ofnode node; |
| 121 | int ret; |
| 122 | |
| 123 | ret = find_image_node(&node); |
| 124 | if (ret) |
| 125 | return log_msg_ret("main", -ENOENT); |
| 126 | node = ofnode_find_subnode(node, name); |
| 127 | if (!ofnode_valid(node)) |
| 128 | return log_msg_ret("node", -ENOENT); |
| 129 | binman->image = node; |
Simon Glass | 5dc338f | 2021-03-15 18:11:14 +1300 | [diff] [blame] | 130 | log_info("binman: Selected image subnode '%s'\n", |
| 131 | ofnode_get_name(binman->image)); |
Simon Glass | 23c0ea5 | 2021-01-13 20:29:57 -0700 | [diff] [blame] | 132 | |
| 133 | return 0; |
| 134 | } |
| 135 | |
Simon Glass | dd7fb9b | 2019-12-06 21:41:34 -0700 | [diff] [blame] | 136 | int binman_init(void) |
| 137 | { |
Simon Glass | cb31d76 | 2021-01-13 20:29:56 -0700 | [diff] [blame] | 138 | int ret; |
| 139 | |
Jerome Forissier | 6a78e96 | 2024-10-16 12:04:05 +0200 | [diff] [blame] | 140 | return 0; |
Simon Glass | dd7fb9b | 2019-12-06 21:41:34 -0700 | [diff] [blame] | 141 | binman = malloc(sizeof(struct binman_info)); |
| 142 | if (!binman) |
| 143 | return log_msg_ret("space for binman", -ENOMEM); |
Simon Glass | cb31d76 | 2021-01-13 20:29:56 -0700 | [diff] [blame] | 144 | ret = find_image_node(&binman->image); |
| 145 | if (ret) |
| 146 | return log_msg_ret("node", -ENOENT); |
Michal Simek | f3d93c6 | 2020-08-26 15:13:14 +0200 | [diff] [blame] | 147 | binman_set_rom_offset(ROM_OFFSET_NONE); |
Simon Glass | f91f23c | 2021-01-21 13:57:16 -0700 | [diff] [blame] | 148 | log_debug("binman: Selected image node '%s'\n", |
| 149 | ofnode_get_name(binman->image)); |
Michal Simek | b6f4b9f | 2020-11-23 09:08:19 +0100 | [diff] [blame] | 150 | |
Simon Glass | dd7fb9b | 2019-12-06 21:41:34 -0700 | [diff] [blame] | 151 | return 0; |
| 152 | } |