blob: 6040ec89241f94fa58165e16d56313e923914c4e [file] [log] [blame]
Simon Glassdd7fb9b2019-12-06 21:41:34 -07001// 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 Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070013#include <malloc.h>
Simon Glassc4d9faf2020-07-07 21:32:04 -060014#include <mapmem.h>
Simon Glassdd7fb9b2019-12-06 21:41:34 -070015
Simon Glassc0ef7892020-07-07 21:32:02 -060016/**
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 Glassdd7fb9b2019-12-06 21:41:34 -070024struct binman_info {
25 ofnode image;
Simon Glassc0ef7892020-07-07 21:32:02 -060026 int rom_offset;
Simon Glassdd7fb9b2019-12-06 21:41:34 -070027};
28
Simon Glassc0ef7892020-07-07 21:32:02 -060029#define ROM_OFFSET_NONE (-1)
30
Simon Glassdd7fb9b2019-12-06 21:41:34 -070031static struct binman_info *binman;
32
Simon Glasscb31d762021-01-13 20:29:56 -070033/**
34 * find_image_node() - Find the top-level binman node
35 *
36 * Finds the binman node which can be used to load entries. The correct node
37 * depends on whether multiple-images is in use.
38 *
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
42 */
43static int find_image_node(ofnode *nodep)
44{
45 ofnode node;
46
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);
52
53 if (!ofnode_valid(node))
54 return log_msg_ret("first image", -ECHILD);
55 }
56 *nodep = node;
57
58 return 0;
59}
60
Simon Glassc7380c62020-07-07 21:32:03 -060061static int binman_entry_find_internal(ofnode node, const char *name,
62 struct binman_entry *entry)
Simon Glassdd7fb9b2019-12-06 21:41:34 -070063{
Simon Glassdd7fb9b2019-12-06 21:41:34 -070064 int ret;
65
Simon Glassdd7fb9b2019-12-06 21:41:34 -070066 if (!ofnode_valid(node))
Simon Glassc7380c62020-07-07 21:32:03 -060067 node = binman->image;
68 node = ofnode_find_subnode(node, name);
69 if (!ofnode_valid(node))
70 return log_msg_ret("node", -ENOENT);
Simon Glassdd7fb9b2019-12-06 21:41:34 -070071
72 ret = ofnode_read_u32(node, "image-pos", &entry->image_pos);
73 if (ret)
Simon Glassdff2ea12020-09-27 18:46:19 -060074 return log_msg_ret("image-pos", ret);
Simon Glassdd7fb9b2019-12-06 21:41:34 -070075 ret = ofnode_read_u32(node, "size", &entry->size);
76 if (ret)
Simon Glassc7380c62020-07-07 21:32:03 -060077 return log_msg_ret("size", ret);
Simon Glassdd7fb9b2019-12-06 21:41:34 -070078
79 return 0;
80}
81
Simon Glassc7380c62020-07-07 21:32:03 -060082int binman_entry_find(const char *name, struct binman_entry *entry)
83{
84 return binman_entry_find_internal(binman->image, name, entry);
85}
86
Simon Glassc4d9faf2020-07-07 21:32:04 -060087int binman_entry_map(ofnode parent, const char *name, void **bufp, int *sizep)
88{
89 struct binman_entry entry;
90 int ret;
91
92 if (binman->rom_offset == ROM_OFFSET_NONE)
93 return -EPERM;
94 ret = binman_entry_find_internal(parent, name, &entry);
95 if (ret)
96 return log_msg_ret("entry", ret);
97 if (sizep)
98 *sizep = entry.size;
99 *bufp = map_sysmem(entry.image_pos + binman->rom_offset, entry.size);
100
101 return 0;
102}
103
104ofnode binman_section_find_node(const char *name)
105{
106 return ofnode_find_subnode(binman->image, name);
107}
108
Simon Glassc0ef7892020-07-07 21:32:02 -0600109void binman_set_rom_offset(int rom_offset)
110{
111 binman->rom_offset = rom_offset;
112}
113
Simon Glassdff2ea12020-09-27 18:46:19 -0600114int binman_get_rom_offset(void)
115{
116 return binman->rom_offset;
117}
118
Simon Glass23c0ea52021-01-13 20:29:57 -0700119int binman_select_subnode(const char *name)
120{
121 ofnode node;
122 int ret;
123
124 ret = find_image_node(&node);
125 if (ret)
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_debug("binman: Selected image subnode '%s'\n",
132 ofnode_get_name(binman->image));
133
134 return 0;
135}
136
Simon Glassdd7fb9b2019-12-06 21:41:34 -0700137int binman_init(void)
138{
Simon Glasscb31d762021-01-13 20:29:56 -0700139 int ret;
140
Simon Glassdd7fb9b2019-12-06 21:41:34 -0700141 binman = malloc(sizeof(struct binman_info));
142 if (!binman)
143 return log_msg_ret("space for binman", -ENOMEM);
Simon Glasscb31d762021-01-13 20:29:56 -0700144 ret = find_image_node(&binman->image);
145 if (ret)
146 return log_msg_ret("node", -ENOENT);
Michal Simekf3d93c62020-08-26 15:13:14 +0200147 binman_set_rom_offset(ROM_OFFSET_NONE);
Simon Glassf91f23c2021-01-21 13:57:16 -0700148 log_debug("binman: Selected image node '%s'\n",
149 ofnode_get_name(binman->image));
Michal Simekb6f4b9f2020-11-23 09:08:19 +0100150
Simon Glassdd7fb9b2019-12-06 21:41:34 -0700151 return 0;
152}