common_fit.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * Copyright (C) 2016 Google, Inc
  3. * Written by Simon Glass <sjg@chromium.org>
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <errno.h>
  9. #include <image.h>
  10. #include <libfdt.h>
  11. #include <spl.h>
  12. ulong fdt_getprop_u32(const void *fdt, int node, const char *prop)
  13. {
  14. const u32 *cell;
  15. int len;
  16. cell = fdt_getprop(fdt, node, prop, &len);
  17. if (len != sizeof(*cell))
  18. return -1U;
  19. return fdt32_to_cpu(*cell);
  20. }
  21. int fit_select_fdt(const void *fdt, int images, int *fdt_offsetp)
  22. {
  23. const char *name, *fdt_name;
  24. int conf, node, fdt_node;
  25. int len;
  26. *fdt_offsetp = 0;
  27. conf = fdt_path_offset(fdt, FIT_CONFS_PATH);
  28. if (conf < 0) {
  29. debug("%s: Cannot find /configurations node: %d\n", __func__,
  30. conf);
  31. return -EINVAL;
  32. }
  33. for (node = fdt_first_subnode(fdt, conf);
  34. node >= 0;
  35. node = fdt_next_subnode(fdt, node)) {
  36. name = fdt_getprop(fdt, node, "description", &len);
  37. if (!name) {
  38. #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
  39. printf("%s: Missing FDT description in DTB\n",
  40. __func__);
  41. #endif
  42. return -EINVAL;
  43. }
  44. if (board_fit_config_name_match(name))
  45. continue;
  46. debug("Selecting config '%s'", name);
  47. fdt_name = fdt_getprop(fdt, node, FIT_FDT_PROP, &len);
  48. if (!fdt_name) {
  49. debug("%s: Cannot find fdt name property: %d\n",
  50. __func__, len);
  51. return -EINVAL;
  52. }
  53. debug(", fdt '%s'\n", fdt_name);
  54. fdt_node = fdt_subnode_offset(fdt, images, fdt_name);
  55. if (fdt_node < 0) {
  56. debug("%s: Cannot find fdt node '%s': %d\n",
  57. __func__, fdt_name, fdt_node);
  58. return -EINVAL;
  59. }
  60. *fdt_offsetp = fdt_getprop_u32(fdt, fdt_node, "data-offset");
  61. len = fdt_getprop_u32(fdt, fdt_node, "data-size");
  62. debug("FIT: Selected '%s'\n", name);
  63. return len;
  64. }
  65. #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
  66. printf("No matching DT out of these options:\n");
  67. for (node = fdt_first_subnode(fdt, conf);
  68. node >= 0;
  69. node = fdt_next_subnode(fdt, node)) {
  70. name = fdt_getprop(fdt, node, "description", &len);
  71. printf(" %s\n", name);
  72. }
  73. #endif
  74. return -ENOENT;
  75. }