blob: 41305d8aa69315e2fd16f18158a01c43f7a08413 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Cooper Jr., Frankline6792402017-06-16 17:25:05 -05002/*
3 * Copyright (C) 2016 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
Cooper Jr., Frankline6792402017-06-16 17:25:05 -05005 */
6
7#include <common.h>
8#include <errno.h>
9#include <image.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090010#include <linux/libfdt.h>
Cooper Jr., Frankline6792402017-06-16 17:25:05 -050011
12ulong fdt_getprop_u32(const void *fdt, int node, const char *prop)
13{
14 const u32 *cell;
15 int len;
16
17 cell = fdt_getprop(fdt, node, prop, &len);
18 if (!cell || len != sizeof(*cell))
19 return FDT_ERROR;
20
21 return fdt32_to_cpu(*cell);
22}
23
24/*
25 * Iterate over all /configurations subnodes and call a platform specific
26 * function to find the matching configuration.
27 * Returns the node offset or a negative error number.
28 */
29int fit_find_config_node(const void *fdt)
30{
31 const char *name;
32 int conf, node, len;
Jean-Jacques Hiblot5e7c12e2017-09-15 12:57:27 +020033 const char *dflt_conf_name;
34 const char *dflt_conf_desc = NULL;
35 int dflt_conf_node = -ENOENT;
Cooper Jr., Frankline6792402017-06-16 17:25:05 -050036
37 conf = fdt_path_offset(fdt, FIT_CONFS_PATH);
38 if (conf < 0) {
39 debug("%s: Cannot find /configurations node: %d\n", __func__,
40 conf);
41 return -EINVAL;
42 }
Jean-Jacques Hiblot5e7c12e2017-09-15 12:57:27 +020043
44 dflt_conf_name = fdt_getprop(fdt, conf, "default", &len);
45
Cooper Jr., Frankline6792402017-06-16 17:25:05 -050046 for (node = fdt_first_subnode(fdt, conf);
47 node >= 0;
48 node = fdt_next_subnode(fdt, node)) {
49 name = fdt_getprop(fdt, node, "description", &len);
50 if (!name) {
51#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
52 printf("%s: Missing FDT description in DTB\n",
53 __func__);
54#endif
55 return -EINVAL;
56 }
Jean-Jacques Hiblot5e7c12e2017-09-15 12:57:27 +020057
58 if (dflt_conf_name) {
59 const char *node_name = fdt_get_name(fdt, node, NULL);
60 if (strcmp(dflt_conf_name, node_name) == 0) {
61 dflt_conf_node = node;
62 dflt_conf_desc = name;
63 }
64 }
65
Cooper Jr., Frankline6792402017-06-16 17:25:05 -050066 if (board_fit_config_name_match(name))
67 continue;
68
69 debug("Selecting config '%s'", name);
70
71 return node;
72 }
73
Jean-Jacques Hiblot5e7c12e2017-09-15 12:57:27 +020074 if (dflt_conf_node != -ENOENT) {
Stefan Roeseda0af042018-11-08 07:00:31 +010075 debug("Selecting default config '%s'\n", dflt_conf_desc);
Jean-Jacques Hiblot5e7c12e2017-09-15 12:57:27 +020076 return dflt_conf_node;
77 }
78
Cooper Jr., Frankline6792402017-06-16 17:25:05 -050079 return -ENOENT;
80}