2011-08-24 19:03:05 +00:00
|
|
|
#include <common.h>
|
|
|
|
#include <environment.h>
|
|
|
|
#include <fdt.h>
|
|
|
|
#include <of.h>
|
|
|
|
#include <command.h>
|
|
|
|
#include <fs.h>
|
|
|
|
#include <malloc.h>
|
|
|
|
#include <linux/ctype.h>
|
|
|
|
#include <asm/byteorder.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <getopt.h>
|
|
|
|
#include <init.h>
|
2012-04-29 18:13:53 +00:00
|
|
|
#include <boot.h>
|
2011-08-24 19:03:05 +00:00
|
|
|
|
|
|
|
#define MAX_LEVEL 32 /* how deeply nested we will go */
|
|
|
|
|
|
|
|
static int is_printable_string(const void *data, int len)
|
|
|
|
{
|
|
|
|
const char *s = data;
|
|
|
|
|
|
|
|
/* zero length is not */
|
|
|
|
if (len == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* must terminate with zero */
|
|
|
|
if (s[len - 1] != '\0')
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* printable or a null byte (concatenated strings) */
|
|
|
|
while (((*s == '\0') || isprint(*s)) && (len > 0)) {
|
|
|
|
/*
|
|
|
|
* If we see a null, there are three possibilities:
|
|
|
|
* 1) If len == 1, it is the end of the string, printable
|
|
|
|
* 2) Next character also a null, not printable.
|
|
|
|
* 3) Next character not a null, continue to check.
|
|
|
|
*/
|
|
|
|
if (s[0] == '\0') {
|
|
|
|
if (len == 1)
|
|
|
|
return 1;
|
|
|
|
if (s[1] == '\0')
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
s++;
|
|
|
|
len--;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Not the null termination, or not done yet: not printable */
|
|
|
|
if (*s != '\0' || (len != 0))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Print the property in the best format, a heuristic guess. Print as
|
|
|
|
* a string, concatenated strings, a byte, word, double word, or (if all
|
|
|
|
* else fails) it is printed as a stream of bytes.
|
|
|
|
*/
|
2011-08-09 17:12:34 +00:00
|
|
|
void of_print_property(const void *data, int len)
|
2011-08-24 19:03:05 +00:00
|
|
|
{
|
|
|
|
int j;
|
|
|
|
|
|
|
|
/* no data, don't print */
|
|
|
|
if (len == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* It is a string, but it may have multiple strings (embedded '\0's).
|
|
|
|
*/
|
|
|
|
if (is_printable_string(data, len)) {
|
|
|
|
puts("\"");
|
|
|
|
j = 0;
|
|
|
|
while (j < len) {
|
|
|
|
if (j > 0)
|
|
|
|
puts("\", \"");
|
|
|
|
puts(data);
|
|
|
|
j += strlen(data) + 1;
|
|
|
|
data += strlen(data) + 1;
|
|
|
|
}
|
|
|
|
puts("\"");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((len % 4) == 0) {
|
|
|
|
const u32 *p;
|
|
|
|
|
|
|
|
printf("<");
|
|
|
|
for (j = 0, p = data; j < len/4; j ++)
|
|
|
|
printf("0x%x%s", fdt32_to_cpu(p[j]), j < (len/4 - 1) ? " " : "");
|
|
|
|
printf(">");
|
|
|
|
} else { /* anything else... hexdump */
|
|
|
|
const u8 *s;
|
|
|
|
|
|
|
|
printf("[");
|
|
|
|
for (j = 0, s = data; j < len; j++)
|
|
|
|
printf("%02x%s", s[j], j < len - 1 ? " " : "");
|
|
|
|
printf("]");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-28 12:57:29 +00:00
|
|
|
void of_print_cmdline(struct device_node *root)
|
|
|
|
{
|
2013-07-27 05:38:10 +00:00
|
|
|
struct device_node *node = of_find_node_by_path_from(root, "/chosen");
|
2013-05-28 12:57:29 +00:00
|
|
|
const char *cmdline;
|
|
|
|
|
|
|
|
if (!node) {
|
|
|
|
printf("commandline: no /chosen node\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
cmdline = of_get_property(node, "bootargs", NULL);
|
|
|
|
|
|
|
|
printf("commandline: %s\n", cmdline);
|
|
|
|
}
|
|
|
|
|
2013-07-16 09:16:24 +00:00
|
|
|
static int of_fixup_bootargs(struct device_node *root, void *unused)
|
2011-08-24 19:03:05 +00:00
|
|
|
{
|
2013-02-20 23:21:29 +00:00
|
|
|
struct device_node *node;
|
2011-08-24 19:03:05 +00:00
|
|
|
const char *str;
|
|
|
|
int err;
|
|
|
|
|
2012-04-29 18:13:53 +00:00
|
|
|
str = linux_bootargs_get();
|
2013-02-20 23:21:29 +00:00
|
|
|
if (!str)
|
|
|
|
return 0;
|
2011-08-24 19:03:05 +00:00
|
|
|
|
2013-02-20 23:21:29 +00:00
|
|
|
node = of_create_node(root, "/chosen");
|
|
|
|
if (!node)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
|
|
|
|
err = of_set_property(node, "bootargs", str, strlen(str) + 1, 1);
|
|
|
|
|
|
|
|
return err;
|
2011-08-24 19:03:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int of_register_bootargs_fixup(void)
|
|
|
|
{
|
2013-07-16 09:16:24 +00:00
|
|
|
return of_register_fixup(of_fixup_bootargs, NULL);
|
2011-08-24 19:03:05 +00:00
|
|
|
}
|
|
|
|
late_initcall(of_register_bootargs_fixup);
|
|
|
|
|
2016-02-25 07:36:47 +00:00
|
|
|
struct of_fixup_status_data {
|
|
|
|
const char *path;
|
|
|
|
bool status;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int of_fixup_status(struct device_node *root, void *context)
|
|
|
|
{
|
|
|
|
const struct of_fixup_status_data *data = context;
|
|
|
|
struct device_node *node;
|
|
|
|
|
|
|
|
node = of_find_node_by_path_or_alias(root, data->path);
|
|
|
|
if (!node)
|
|
|
|
return -ENODEV;
|
|
|
|
|
|
|
|
if (data->status)
|
|
|
|
return of_device_enable(node);
|
|
|
|
else
|
|
|
|
return of_device_disable(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* of_register_set_status_fixup - register fix up to set status of nodes
|
|
|
|
* Register a fixup to enable or disable a node in the devicet tree by
|
|
|
|
* passing the path or alias.
|
|
|
|
*/
|
|
|
|
int of_register_set_status_fixup(const char *path, bool status)
|
|
|
|
{
|
|
|
|
struct of_fixup_status_data *data;
|
|
|
|
|
|
|
|
data = xzalloc(sizeof(*data));
|
|
|
|
data->path = path;
|
|
|
|
data->status = status;
|
|
|
|
|
|
|
|
return of_register_fixup(of_fixup_status, (void *)data);
|
|
|
|
}
|
|
|
|
|
2011-08-24 19:03:05 +00:00
|
|
|
struct of_fixup {
|
2013-07-16 09:16:24 +00:00
|
|
|
int (*fixup)(struct device_node *, void *);
|
|
|
|
void *context;
|
2011-08-24 19:03:05 +00:00
|
|
|
struct list_head list;
|
|
|
|
};
|
|
|
|
|
|
|
|
static LIST_HEAD(of_fixup_list);
|
|
|
|
|
2013-07-16 09:16:24 +00:00
|
|
|
int of_register_fixup(int (*fixup)(struct device_node *, void *), void *context)
|
2011-08-24 19:03:05 +00:00
|
|
|
{
|
|
|
|
struct of_fixup *of_fixup = xzalloc(sizeof(*of_fixup));
|
|
|
|
|
|
|
|
of_fixup->fixup = fixup;
|
2013-07-16 09:16:24 +00:00
|
|
|
of_fixup->context = context;
|
2011-08-24 19:03:05 +00:00
|
|
|
|
|
|
|
list_add_tail(&of_fixup->list, &of_fixup_list);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-05-16 10:12:29 +00:00
|
|
|
/*
|
|
|
|
* Remove a previously registered fixup. Only the first (if any) is removed.
|
|
|
|
* Returns 0 if a match was found (and removed), -ENOENT otherwise.
|
|
|
|
*/
|
|
|
|
int of_unregister_fixup(int (*fixup)(struct device_node *, void *),
|
|
|
|
void *context)
|
|
|
|
{
|
|
|
|
struct of_fixup *of_fixup;
|
|
|
|
|
|
|
|
list_for_each_entry(of_fixup, &of_fixup_list, list) {
|
|
|
|
if (of_fixup->fixup == fixup && of_fixup->context == context) {
|
|
|
|
list_del(&of_fixup->list);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
|
2013-01-10 13:37:17 +00:00
|
|
|
/*
|
|
|
|
* Apply registered fixups for the given fdt. The fdt must have
|
|
|
|
* enough free space to apply the fixups.
|
|
|
|
*/
|
2013-02-20 23:21:29 +00:00
|
|
|
int of_fix_tree(struct device_node *node)
|
2011-08-24 19:03:05 +00:00
|
|
|
{
|
|
|
|
struct of_fixup *of_fixup;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
list_for_each_entry(of_fixup, &of_fixup_list, list) {
|
2013-07-16 09:16:24 +00:00
|
|
|
ret = of_fixup->fixup(node, of_fixup->context);
|
2015-09-10 13:11:28 +00:00
|
|
|
if (ret)
|
|
|
|
pr_warn("Failed to fixup node in %pS: %s\n",
|
2015-09-08 13:48:41 +00:00
|
|
|
of_fixup->fixup, strerror(-ret));
|
2011-08-24 19:03:05 +00:00
|
|
|
}
|
|
|
|
|
2011-12-14 19:19:57 +00:00
|
|
|
return 0;
|
2011-08-24 19:03:05 +00:00
|
|
|
}
|
|
|
|
|
2013-01-10 13:37:17 +00:00
|
|
|
/*
|
|
|
|
* Get the fixed fdt. This function uses the fdt input pointer
|
|
|
|
* if provided or the barebox internal devicetree if not.
|
|
|
|
* It increases the size of the tree and applies the registered
|
|
|
|
* fixups.
|
|
|
|
*/
|
2013-02-19 23:10:44 +00:00
|
|
|
struct fdt_header *of_get_fixed_tree(struct device_node *node)
|
2011-12-14 19:19:57 +00:00
|
|
|
{
|
|
|
|
int ret;
|
2013-02-19 23:10:44 +00:00
|
|
|
struct fdt_header *fdt;
|
2011-12-14 19:19:57 +00:00
|
|
|
|
2013-02-19 23:10:44 +00:00
|
|
|
if (!node) {
|
|
|
|
node = of_get_root_node();
|
|
|
|
if (!node)
|
2013-01-09 12:14:45 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2013-01-10 13:37:17 +00:00
|
|
|
|
2013-02-20 23:21:29 +00:00
|
|
|
ret = of_fix_tree(node);
|
2011-12-14 19:19:57 +00:00
|
|
|
if (ret)
|
2013-02-20 23:21:29 +00:00
|
|
|
return NULL;
|
2013-01-10 13:37:17 +00:00
|
|
|
|
2013-02-20 23:21:29 +00:00
|
|
|
fdt = of_flatten_dtb(node);
|
|
|
|
if (!fdt)
|
|
|
|
return NULL;
|
2013-01-10 13:37:17 +00:00
|
|
|
|
2013-02-20 23:21:29 +00:00
|
|
|
return fdt;
|
2011-12-14 19:19:57 +00:00
|
|
|
}
|