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