binman.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // SPDX-License-Identifier: Intel
  2. /*
  3. * Access to binman information at runtime
  4. *
  5. * Copyright 2019 Google LLC
  6. * Written by Simon Glass <sjg@chromium.org>
  7. */
  8. #include <common.h>
  9. #include <binman.h>
  10. #include <dm.h>
  11. #include <log.h>
  12. #include <malloc.h>
  13. #include <mapmem.h>
  14. /**
  15. * struct binman_info - Information needed by the binman library
  16. *
  17. * @image: Node describing the image we are running from
  18. * @rom_offset: Offset from an image_pos to the memory-mapped address, or
  19. * ROM_OFFSET_NONE if the ROM is not memory-mapped. Can be positive or
  20. * negative
  21. */
  22. struct binman_info {
  23. ofnode image;
  24. int rom_offset;
  25. };
  26. #define ROM_OFFSET_NONE (-1)
  27. static struct binman_info *binman;
  28. /**
  29. * find_image_node() - Find the top-level binman node
  30. *
  31. * Finds the binman node which can be used to load entries. The correct node
  32. * depends on whether multiple-images is in use.
  33. *
  34. * @nodep: Returns the node found, on success
  35. * @return 0 if OK, , -EINVAL if there is no /binman node, -ECHILD if multiple
  36. * images are being used but the first image is not available
  37. */
  38. static int find_image_node(ofnode *nodep)
  39. {
  40. ofnode node;
  41. node = ofnode_path("/binman");
  42. if (!ofnode_valid(node))
  43. return log_msg_ret("binman node", -EINVAL);
  44. if (ofnode_read_bool(node, "multiple-images")) {
  45. node = ofnode_first_subnode(node);
  46. if (!ofnode_valid(node))
  47. return log_msg_ret("first image", -ECHILD);
  48. }
  49. *nodep = node;
  50. return 0;
  51. }
  52. static int binman_entry_find_internal(ofnode node, const char *name,
  53. struct binman_entry *entry)
  54. {
  55. int ret;
  56. if (!ofnode_valid(node))
  57. node = binman->image;
  58. node = ofnode_find_subnode(node, name);
  59. if (!ofnode_valid(node))
  60. return log_msg_ret("node", -ENOENT);
  61. ret = ofnode_read_u32(node, "image-pos", &entry->image_pos);
  62. if (ret)
  63. return log_msg_ret("image-pos", ret);
  64. ret = ofnode_read_u32(node, "size", &entry->size);
  65. if (ret)
  66. return log_msg_ret("size", ret);
  67. return 0;
  68. }
  69. int binman_entry_find(const char *name, struct binman_entry *entry)
  70. {
  71. return binman_entry_find_internal(binman->image, name, entry);
  72. }
  73. int binman_entry_map(ofnode parent, const char *name, void **bufp, int *sizep)
  74. {
  75. struct binman_entry entry;
  76. int ret;
  77. if (binman->rom_offset == ROM_OFFSET_NONE)
  78. return -EPERM;
  79. ret = binman_entry_find_internal(parent, name, &entry);
  80. if (ret)
  81. return log_msg_ret("entry", ret);
  82. if (sizep)
  83. *sizep = entry.size;
  84. *bufp = map_sysmem(entry.image_pos + binman->rom_offset, entry.size);
  85. return 0;
  86. }
  87. ofnode binman_section_find_node(const char *name)
  88. {
  89. return ofnode_find_subnode(binman->image, name);
  90. }
  91. void binman_set_rom_offset(int rom_offset)
  92. {
  93. binman->rom_offset = rom_offset;
  94. }
  95. int binman_get_rom_offset(void)
  96. {
  97. return binman->rom_offset;
  98. }
  99. int binman_select_subnode(const char *name)
  100. {
  101. ofnode node;
  102. int ret;
  103. ret = find_image_node(&node);
  104. if (ret)
  105. return log_msg_ret("main", -ENOENT);
  106. node = ofnode_find_subnode(node, name);
  107. if (!ofnode_valid(node))
  108. return log_msg_ret("node", -ENOENT);
  109. binman->image = node;
  110. log_info("binman: Selected image subnode '%s'\n",
  111. ofnode_get_name(binman->image));
  112. return 0;
  113. }
  114. int binman_init(void)
  115. {
  116. int ret;
  117. binman = malloc(sizeof(struct binman_info));
  118. if (!binman)
  119. return log_msg_ret("space for binman", -ENOMEM);
  120. ret = find_image_node(&binman->image);
  121. if (ret)
  122. return log_msg_ret("node", -ENOENT);
  123. binman_set_rom_offset(ROM_OFFSET_NONE);
  124. log_debug("binman: Selected image node '%s'\n",
  125. ofnode_get_name(binman->image));
  126. return 0;
  127. }