1 // SPDX-License-Identifier: Intel
3 * Access to binman information at runtime
5 * Copyright 2019 Google LLC
6 * Written by Simon Glass <sjg@chromium.org>
17 * struct binman_info - Information needed by the binman library
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
29 #define ROM_OFFSET_NONE (-1)
31 static struct binman_info *binman;
34 * find_image_node() - Find the top-level binman node
36 * Finds the binman node which can be used to load entries. The correct node
37 * depends on whether multiple-images is in use.
39 * @nodep: Returns the node found, on success
40 * @return 0 if OK, , -EINVAL if there is no /binman node, -ECHILD if multiple
41 * images are being used but the first image is not available
43 static int find_image_node(ofnode *nodep)
47 node = ofnode_path("/binman");
48 if (!ofnode_valid(node))
49 return log_msg_ret("binman node", -EINVAL);
50 if (ofnode_read_bool(node, "multiple-images")) {
51 node = ofnode_first_subnode(node);
53 if (!ofnode_valid(node))
54 return log_msg_ret("first image", -ECHILD);
61 static int binman_entry_find_internal(ofnode node, const char *name,
62 struct binman_entry *entry)
66 if (!ofnode_valid(node))
68 node = ofnode_find_subnode(node, name);
69 if (!ofnode_valid(node))
70 return log_msg_ret("node", -ENOENT);
72 ret = ofnode_read_u32(node, "image-pos", &entry->image_pos);
74 return log_msg_ret("image-pos", ret);
75 ret = ofnode_read_u32(node, "size", &entry->size);
77 return log_msg_ret("size", ret);
82 int binman_entry_find(const char *name, struct binman_entry *entry)
84 return binman_entry_find_internal(binman->image, name, entry);
87 int binman_entry_map(ofnode parent, const char *name, void **bufp, int *sizep)
89 struct binman_entry entry;
92 if (binman->rom_offset == ROM_OFFSET_NONE)
94 ret = binman_entry_find_internal(parent, name, &entry);
96 return log_msg_ret("entry", ret);
99 *bufp = map_sysmem(entry.image_pos + binman->rom_offset, entry.size);
104 ofnode binman_section_find_node(const char *name)
106 return ofnode_find_subnode(binman->image, name);
109 void binman_set_rom_offset(int rom_offset)
111 binman->rom_offset = rom_offset;
114 int binman_get_rom_offset(void)
116 return binman->rom_offset;
119 int binman_select_subnode(const char *name)
124 ret = find_image_node(&node);
126 return log_msg_ret("main", -ENOENT);
127 node = ofnode_find_subnode(node, name);
128 if (!ofnode_valid(node))
129 return log_msg_ret("node", -ENOENT);
130 binman->image = node;
131 log_info("binman: Selected image subnode '%s'\n",
132 ofnode_get_name(binman->image));
137 int binman_init(void)
141 binman = malloc(sizeof(struct binman_info));
143 return log_msg_ret("space for binman", -ENOMEM);
144 ret = find_image_node(&binman->image);
146 return log_msg_ret("node", -ENOENT);
147 binman_set_rom_offset(ROM_OFFSET_NONE);
148 log_debug("binman: Selected image node '%s'\n",
149 ofnode_get_name(binman->image));