blob: 9098a1dffa151c07b6671fcbef3813a055c58703 [file] [log] [blame]
Simon Glass3c10dc92019-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 Glassf7ae49f2020-05-10 11:40:05 -060012#include <log.h>
Simon Glass336d4612020-02-03 07:36:16 -070013#include <malloc.h>
Simon Glass3c10dc92019-12-06 21:41:34 -070014
Simon Glassdb6fb7d2020-07-07 21:32:02 -060015/**
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 Glass3c10dc92019-12-06 21:41:34 -070023struct binman_info {
24 ofnode image;
Simon Glassdb6fb7d2020-07-07 21:32:02 -060025 int rom_offset;
Simon Glass3c10dc92019-12-06 21:41:34 -070026};
27
Simon Glassdb6fb7d2020-07-07 21:32:02 -060028#define ROM_OFFSET_NONE (-1)
29
Simon Glass3c10dc92019-12-06 21:41:34 -070030static struct binman_info *binman;
31
Simon Glass956a9082020-07-07 21:32:03 -060032static int binman_entry_find_internal(ofnode node, const char *name,
33 struct binman_entry *entry)
Simon Glass3c10dc92019-12-06 21:41:34 -070034{
Simon Glass3c10dc92019-12-06 21:41:34 -070035 int ret;
36
Simon Glass3c10dc92019-12-06 21:41:34 -070037 if (!ofnode_valid(node))
Simon Glass956a9082020-07-07 21:32:03 -060038 node = binman->image;
39 node = ofnode_find_subnode(node, name);
40 if (!ofnode_valid(node))
41 return log_msg_ret("node", -ENOENT);
Simon Glass3c10dc92019-12-06 21:41:34 -070042
43 ret = ofnode_read_u32(node, "image-pos", &entry->image_pos);
44 if (ret)
Simon Glass956a9082020-07-07 21:32:03 -060045 return log_msg_ret("import-pos", ret);
Simon Glass3c10dc92019-12-06 21:41:34 -070046 ret = ofnode_read_u32(node, "size", &entry->size);
47 if (ret)
Simon Glass956a9082020-07-07 21:32:03 -060048 return log_msg_ret("size", ret);
Simon Glass3c10dc92019-12-06 21:41:34 -070049
50 return 0;
51}
52
Simon Glass956a9082020-07-07 21:32:03 -060053int binman_entry_find(const char *name, struct binman_entry *entry)
54{
55 return binman_entry_find_internal(binman->image, name, entry);
56}
57
Simon Glassdb6fb7d2020-07-07 21:32:02 -060058void binman_set_rom_offset(int rom_offset)
59{
60 binman->rom_offset = rom_offset;
61}
62
Simon Glass3c10dc92019-12-06 21:41:34 -070063int binman_init(void)
64{
65 binman = malloc(sizeof(struct binman_info));
66 if (!binman)
67 return log_msg_ret("space for binman", -ENOMEM);
68 binman->image = ofnode_path("/binman");
69 if (!ofnode_valid(binman->image))
70 return log_msg_ret("binman node", -EINVAL);
Simon Glassdb6fb7d2020-07-07 21:32:02 -060071 binman->rom_offset = ROM_OFFSET_NONE;
Simon Glass3c10dc92019-12-06 21:41:34 -070072
73 return 0;
74}