Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Cooper Jr., Franklin | 3863f84 | 2017-06-16 17:25:05 -0500 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2016 Google, Inc |
| 4 | * Written by Simon Glass <sjg@chromium.org> |
Cooper Jr., Franklin | 3863f84 | 2017-06-16 17:25:05 -0500 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <errno.h> |
| 9 | #include <image.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 10 | #include <log.h> |
Masahiro Yamada | b08c8c4 | 2018-03-05 01:20:11 +0900 | [diff] [blame] | 11 | #include <linux/libfdt.h> |
Cooper Jr., Franklin | 3863f84 | 2017-06-16 17:25:05 -0500 | [diff] [blame] | 12 | |
| 13 | ulong fdt_getprop_u32(const void *fdt, int node, const char *prop) |
| 14 | { |
| 15 | const u32 *cell; |
| 16 | int len; |
| 17 | |
| 18 | cell = fdt_getprop(fdt, node, prop, &len); |
| 19 | if (!cell || len != sizeof(*cell)) |
| 20 | return FDT_ERROR; |
| 21 | |
| 22 | return fdt32_to_cpu(*cell); |
| 23 | } |
| 24 | |
Sean Anderson | d442f69 | 2021-03-31 14:32:27 -0400 | [diff] [blame] | 25 | __weak int board_fit_config_name_match(const char *name) |
| 26 | { |
| 27 | return -EINVAL; |
| 28 | } |
| 29 | |
Cooper Jr., Franklin | 3863f84 | 2017-06-16 17:25:05 -0500 | [diff] [blame] | 30 | /* |
| 31 | * Iterate over all /configurations subnodes and call a platform specific |
| 32 | * function to find the matching configuration. |
| 33 | * Returns the node offset or a negative error number. |
| 34 | */ |
| 35 | int fit_find_config_node(const void *fdt) |
| 36 | { |
| 37 | const char *name; |
| 38 | int conf, node, len; |
Jean-Jacques Hiblot | 02035d0 | 2017-09-15 12:57:27 +0200 | [diff] [blame] | 39 | const char *dflt_conf_name; |
| 40 | const char *dflt_conf_desc = NULL; |
| 41 | int dflt_conf_node = -ENOENT; |
Cooper Jr., Franklin | 3863f84 | 2017-06-16 17:25:05 -0500 | [diff] [blame] | 42 | |
| 43 | conf = fdt_path_offset(fdt, FIT_CONFS_PATH); |
| 44 | if (conf < 0) { |
| 45 | debug("%s: Cannot find /configurations node: %d\n", __func__, |
| 46 | conf); |
| 47 | return -EINVAL; |
| 48 | } |
Jean-Jacques Hiblot | 02035d0 | 2017-09-15 12:57:27 +0200 | [diff] [blame] | 49 | |
| 50 | dflt_conf_name = fdt_getprop(fdt, conf, "default", &len); |
| 51 | |
Cooper Jr., Franklin | 3863f84 | 2017-06-16 17:25:05 -0500 | [diff] [blame] | 52 | for (node = fdt_first_subnode(fdt, conf); |
| 53 | node >= 0; |
| 54 | node = fdt_next_subnode(fdt, node)) { |
| 55 | name = fdt_getprop(fdt, node, "description", &len); |
| 56 | if (!name) { |
| 57 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
| 58 | printf("%s: Missing FDT description in DTB\n", |
| 59 | __func__); |
| 60 | #endif |
| 61 | return -EINVAL; |
| 62 | } |
Jean-Jacques Hiblot | 02035d0 | 2017-09-15 12:57:27 +0200 | [diff] [blame] | 63 | |
| 64 | if (dflt_conf_name) { |
| 65 | const char *node_name = fdt_get_name(fdt, node, NULL); |
| 66 | if (strcmp(dflt_conf_name, node_name) == 0) { |
| 67 | dflt_conf_node = node; |
| 68 | dflt_conf_desc = name; |
| 69 | } |
| 70 | } |
| 71 | |
Cooper Jr., Franklin | 3863f84 | 2017-06-16 17:25:05 -0500 | [diff] [blame] | 72 | if (board_fit_config_name_match(name)) |
| 73 | continue; |
| 74 | |
Michael Walle | 6a457bb | 2020-11-16 22:01:31 +0100 | [diff] [blame] | 75 | debug("Selecting config '%s'\n", name); |
Cooper Jr., Franklin | 3863f84 | 2017-06-16 17:25:05 -0500 | [diff] [blame] | 76 | |
| 77 | return node; |
| 78 | } |
| 79 | |
Jean-Jacques Hiblot | 02035d0 | 2017-09-15 12:57:27 +0200 | [diff] [blame] | 80 | if (dflt_conf_node != -ENOENT) { |
Stefan Roese | 6514bfc | 2018-11-08 07:00:31 +0100 | [diff] [blame] | 81 | debug("Selecting default config '%s'\n", dflt_conf_desc); |
Jean-Jacques Hiblot | 02035d0 | 2017-09-15 12:57:27 +0200 | [diff] [blame] | 82 | return dflt_conf_node; |
| 83 | } |
| 84 | |
Cooper Jr., Franklin | 3863f84 | 2017-06-16 17:25:05 -0500 | [diff] [blame] | 85 | return -ENOENT; |
| 86 | } |