1 // SPDX-License-Identifier: Intel
3 * Access to binman information at runtime
5 * Copyright 2019 Google LLC
16 * struct binman_info - Information needed by the binman library
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
28 #define ROM_OFFSET_NONE (-1)
30 static struct binman_info *binman;
33 * find_image_node() - Find the top-level binman node
35 * Finds the binman node which can be used to load entries. The correct node
36 * depends on whether multiple-images is in use.
38 * @nodep: Returns the node found, on success
39 * Return: 0 if OK, , -EINVAL if there is no /binman node, -ECHILD if multiple
40 * images are being used but the first image is not available
42 static int find_image_node(ofnode *nodep)
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);
52 if (!ofnode_valid(node))
53 return log_msg_ret("first image", -ECHILD);
60 static int binman_entry_find_internal(ofnode node, const char *name,
61 struct binman_entry *entry)
65 if (!ofnode_valid(node))
67 node = ofnode_find_subnode(node, name);
68 if (!ofnode_valid(node))
69 return log_msg_ret("node", -ENOENT);
71 ret = ofnode_read_u32(node, "image-pos", &entry->image_pos);
73 return log_msg_ret("image-pos", ret);
74 ret = ofnode_read_u32(node, "size", &entry->size);
76 return log_msg_ret("size", ret);
81 int binman_entry_find(const char *name, struct binman_entry *entry)
83 return binman_entry_find_internal(binman->image, name, entry);
86 int binman_entry_map(ofnode parent, const char *name, void **bufp, int *sizep)
88 struct binman_entry entry;
91 if (binman->rom_offset == ROM_OFFSET_NONE)
93 ret = binman_entry_find_internal(parent, name, &entry);
95 return log_msg_ret("entry", ret);
98 *bufp = map_sysmem(entry.image_pos + binman->rom_offset, entry.size);
103 ofnode binman_section_find_node(const char *name)
105 return ofnode_find_subnode(binman->image, name);
108 void binman_set_rom_offset(int rom_offset)
110 binman->rom_offset = rom_offset;
113 int binman_get_rom_offset(void)
115 return binman->rom_offset;
118 int binman_select_subnode(const char *name)
123 ret = find_image_node(&node);
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;
130 log_info("binman: Selected image subnode '%s'\n",
131 ofnode_get_name(binman->image));
136 int binman_init(void)
140 binman = malloc(sizeof(struct binman_info));
142 return log_msg_ret("space for binman", -ENOMEM);
143 ret = find_image_node(&binman->image);
145 return log_msg_ret("node", -ENOENT);
146 binman_set_rom_offset(ROM_OFFSET_NONE);
147 log_debug("binman: Selected image node '%s'\n",
148 ofnode_get_name(binman->image));