2019-12-07 04:41:34 +00:00
|
|
|
// SPDX-License-Identifier: Intel
|
|
|
|
/*
|
|
|
|
* Access to binman information at runtime
|
|
|
|
*
|
|
|
|
* Copyright 2019 Google LLC
|
|
|
|
* Written by Simon Glass <sjg@chromium.org>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
|
|
|
#include <binman.h>
|
|
|
|
#include <dm.h>
|
2020-05-10 17:40:05 +00:00
|
|
|
#include <log.h>
|
2020-02-03 14:36:16 +00:00
|
|
|
#include <malloc.h>
|
2020-07-08 03:32:04 +00:00
|
|
|
#include <mapmem.h>
|
2019-12-07 04:41:34 +00:00
|
|
|
|
2020-07-08 03:32:02 +00:00
|
|
|
/**
|
|
|
|
* struct binman_info - Information needed by the binman library
|
|
|
|
*
|
|
|
|
* @image: Node describing the image we are running from
|
|
|
|
* @rom_offset: Offset from an image_pos to the memory-mapped address, or
|
|
|
|
* ROM_OFFSET_NONE if the ROM is not memory-mapped. Can be positive or
|
|
|
|
* negative
|
|
|
|
*/
|
2019-12-07 04:41:34 +00:00
|
|
|
struct binman_info {
|
|
|
|
ofnode image;
|
2020-07-08 03:32:02 +00:00
|
|
|
int rom_offset;
|
2019-12-07 04:41:34 +00:00
|
|
|
};
|
|
|
|
|
2020-07-08 03:32:02 +00:00
|
|
|
#define ROM_OFFSET_NONE (-1)
|
|
|
|
|
2019-12-07 04:41:34 +00:00
|
|
|
static struct binman_info *binman;
|
|
|
|
|
2021-01-14 03:29:56 +00:00
|
|
|
/**
|
|
|
|
* find_image_node() - Find the top-level binman node
|
|
|
|
*
|
|
|
|
* Finds the binman node which can be used to load entries. The correct node
|
|
|
|
* depends on whether multiple-images is in use.
|
|
|
|
*
|
|
|
|
* @nodep: Returns the node found, on success
|
|
|
|
* @return 0 if OK, , -EINVAL if there is no /binman node, -ECHILD if multiple
|
|
|
|
* images are being used but the first image is not available
|
|
|
|
*/
|
|
|
|
static int find_image_node(ofnode *nodep)
|
|
|
|
{
|
|
|
|
ofnode node;
|
|
|
|
|
|
|
|
node = ofnode_path("/binman");
|
|
|
|
if (!ofnode_valid(node))
|
|
|
|
return log_msg_ret("binman node", -EINVAL);
|
|
|
|
if (ofnode_read_bool(node, "multiple-images")) {
|
|
|
|
node = ofnode_first_subnode(node);
|
|
|
|
|
|
|
|
if (!ofnode_valid(node))
|
|
|
|
return log_msg_ret("first image", -ECHILD);
|
|
|
|
}
|
|
|
|
*nodep = node;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-07-08 03:32:03 +00:00
|
|
|
static int binman_entry_find_internal(ofnode node, const char *name,
|
|
|
|
struct binman_entry *entry)
|
2019-12-07 04:41:34 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!ofnode_valid(node))
|
2020-07-08 03:32:03 +00:00
|
|
|
node = binman->image;
|
|
|
|
node = ofnode_find_subnode(node, name);
|
|
|
|
if (!ofnode_valid(node))
|
|
|
|
return log_msg_ret("node", -ENOENT);
|
2019-12-07 04:41:34 +00:00
|
|
|
|
|
|
|
ret = ofnode_read_u32(node, "image-pos", &entry->image_pos);
|
|
|
|
if (ret)
|
2020-09-28 00:46:19 +00:00
|
|
|
return log_msg_ret("image-pos", ret);
|
2019-12-07 04:41:34 +00:00
|
|
|
ret = ofnode_read_u32(node, "size", &entry->size);
|
|
|
|
if (ret)
|
2020-07-08 03:32:03 +00:00
|
|
|
return log_msg_ret("size", ret);
|
2019-12-07 04:41:34 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-07-08 03:32:03 +00:00
|
|
|
int binman_entry_find(const char *name, struct binman_entry *entry)
|
|
|
|
{
|
|
|
|
return binman_entry_find_internal(binman->image, name, entry);
|
|
|
|
}
|
|
|
|
|
2020-07-08 03:32:04 +00:00
|
|
|
int binman_entry_map(ofnode parent, const char *name, void **bufp, int *sizep)
|
|
|
|
{
|
|
|
|
struct binman_entry entry;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (binman->rom_offset == ROM_OFFSET_NONE)
|
|
|
|
return -EPERM;
|
|
|
|
ret = binman_entry_find_internal(parent, name, &entry);
|
|
|
|
if (ret)
|
|
|
|
return log_msg_ret("entry", ret);
|
|
|
|
if (sizep)
|
|
|
|
*sizep = entry.size;
|
|
|
|
*bufp = map_sysmem(entry.image_pos + binman->rom_offset, entry.size);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ofnode binman_section_find_node(const char *name)
|
|
|
|
{
|
|
|
|
return ofnode_find_subnode(binman->image, name);
|
|
|
|
}
|
|
|
|
|
2020-07-08 03:32:02 +00:00
|
|
|
void binman_set_rom_offset(int rom_offset)
|
|
|
|
{
|
|
|
|
binman->rom_offset = rom_offset;
|
|
|
|
}
|
|
|
|
|
2020-09-28 00:46:19 +00:00
|
|
|
int binman_get_rom_offset(void)
|
|
|
|
{
|
|
|
|
return binman->rom_offset;
|
|
|
|
}
|
|
|
|
|
2021-01-14 03:29:57 +00:00
|
|
|
int binman_select_subnode(const char *name)
|
|
|
|
{
|
|
|
|
ofnode node;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = find_image_node(&node);
|
|
|
|
if (ret)
|
|
|
|
return log_msg_ret("main", -ENOENT);
|
|
|
|
node = ofnode_find_subnode(node, name);
|
|
|
|
if (!ofnode_valid(node))
|
|
|
|
return log_msg_ret("node", -ENOENT);
|
|
|
|
binman->image = node;
|
2021-03-15 05:11:14 +00:00
|
|
|
log_info("binman: Selected image subnode '%s'\n",
|
|
|
|
ofnode_get_name(binman->image));
|
2021-01-14 03:29:57 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-12-07 04:41:34 +00:00
|
|
|
int binman_init(void)
|
|
|
|
{
|
2021-01-14 03:29:56 +00:00
|
|
|
int ret;
|
|
|
|
|
2019-12-07 04:41:34 +00:00
|
|
|
binman = malloc(sizeof(struct binman_info));
|
|
|
|
if (!binman)
|
|
|
|
return log_msg_ret("space for binman", -ENOMEM);
|
2021-01-14 03:29:56 +00:00
|
|
|
ret = find_image_node(&binman->image);
|
|
|
|
if (ret)
|
|
|
|
return log_msg_ret("node", -ENOENT);
|
2020-08-26 13:13:14 +00:00
|
|
|
binman_set_rom_offset(ROM_OFFSET_NONE);
|
2021-01-21 20:57:16 +00:00
|
|
|
log_debug("binman: Selected image node '%s'\n",
|
|
|
|
ofnode_get_name(binman->image));
|
2020-11-23 08:08:19 +00:00
|
|
|
|
2019-12-07 04:41:34 +00:00
|
|
|
return 0;
|
|
|
|
}
|