efi_selftest_fdt.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * efi_selftest_pos
  4. *
  5. * Copyright (c) 2018 Heinrich Schuchardt <xypron.glpk@gmx.de>
  6. *
  7. * Test the EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.
  8. *
  9. * The following services are tested:
  10. * OutputString, TestString, SetAttribute.
  11. */
  12. #include <efi_selftest.h>
  13. #include <linux/libfdt.h>
  14. static const struct efi_system_table *systemtab;
  15. static const struct efi_boot_services *boottime;
  16. static const char *fdt;
  17. /* This should be sufficient for */
  18. #define BUFFERSIZE 0x100000
  19. static const efi_guid_t fdt_guid = EFI_FDT_GUID;
  20. static const efi_guid_t acpi_guid = EFI_ACPI_TABLE_GUID;
  21. /*
  22. * Convert FDT value to host endianness.
  23. *
  24. * @val FDT value
  25. * @return converted value
  26. */
  27. static uint32_t f2h(fdt32_t val)
  28. {
  29. char *buf = (char *)&val;
  30. char i;
  31. #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
  32. /* Swap the bytes */
  33. i = buf[0]; buf[0] = buf[3]; buf[3] = i;
  34. i = buf[1]; buf[1] = buf[2]; buf[2] = i;
  35. #endif
  36. return *(uint32_t *)buf;
  37. }
  38. /**
  39. * get_property() - return value of a property of an FDT node
  40. *
  41. * A property of the root node or one of its direct children can be
  42. * retrieved.
  43. *
  44. * @property name of the property
  45. * @node name of the node or NULL for root node
  46. * @return value of the property
  47. */
  48. static char *get_property(const u16 *property, const u16 *node)
  49. {
  50. struct fdt_header *header = (struct fdt_header *)fdt;
  51. const fdt32_t *end;
  52. const fdt32_t *pos;
  53. const char *strings;
  54. size_t level = 0;
  55. const char *nodelabel = NULL;
  56. if (!header) {
  57. efi_st_error("Missing device tree\n");
  58. return NULL;
  59. }
  60. if (f2h(header->magic) != FDT_MAGIC) {
  61. efi_st_error("Wrong device tree magic\n");
  62. return NULL;
  63. }
  64. pos = (fdt32_t *)(fdt + f2h(header->off_dt_struct));
  65. end = &pos[f2h(header->totalsize) >> 2];
  66. strings = fdt + f2h(header->off_dt_strings);
  67. for (; pos < end;) {
  68. switch (f2h(pos[0])) {
  69. case FDT_BEGIN_NODE: {
  70. const char *c = (char *)&pos[1];
  71. size_t i;
  72. if (level == 1)
  73. nodelabel = c;
  74. ++level;
  75. for (i = 0; c[i]; ++i)
  76. ;
  77. pos = &pos[2 + (i >> 2)];
  78. break;
  79. }
  80. case FDT_PROP: {
  81. struct fdt_property *prop = (struct fdt_property *)pos;
  82. const char *label = &strings[f2h(prop->nameoff)];
  83. efi_status_t ret;
  84. /* Check if this is the property to be returned */
  85. if (!efi_st_strcmp_16_8(property, label) &&
  86. ((level == 1 && !node) ||
  87. (level == 2 && node &&
  88. !efi_st_strcmp_16_8(node, nodelabel)))) {
  89. char *str;
  90. efi_uintn_t len = f2h(prop->len);
  91. if (!len)
  92. return NULL;
  93. /*
  94. * The string might not be 0 terminated.
  95. * It is safer to make a copy.
  96. */
  97. ret = boottime->allocate_pool(
  98. EFI_LOADER_DATA, len + 1,
  99. (void **)&str);
  100. if (ret != EFI_SUCCESS) {
  101. efi_st_error("AllocatePool failed\n");
  102. return NULL;
  103. }
  104. boottime->copy_mem(str, &pos[3], len);
  105. str[len] = 0;
  106. return str;
  107. }
  108. pos = &pos[3 + ((f2h(prop->len) + 3) >> 2)];
  109. break;
  110. }
  111. case FDT_NOP:
  112. ++pos;
  113. break;
  114. case FDT_END_NODE:
  115. --level;
  116. ++pos;
  117. break;
  118. case FDT_END:
  119. return NULL;
  120. default:
  121. efi_st_error("Invalid device tree token\n");
  122. return NULL;
  123. }
  124. }
  125. efi_st_error("Missing FDT_END token\n");
  126. return NULL;
  127. }
  128. /**
  129. * efi_st_get_config_table() - get configuration table
  130. *
  131. * @guid: GUID of the configuration table
  132. * Return: pointer to configuration table or NULL
  133. */
  134. static void *efi_st_get_config_table(const efi_guid_t *guid)
  135. {
  136. size_t i;
  137. for (i = 0; i < systab.nr_tables; i++) {
  138. if (!guidcmp(guid, &systemtab->tables[i].guid))
  139. return systemtab->tables[i].table;
  140. }
  141. return NULL;
  142. }
  143. /*
  144. * Setup unit test.
  145. *
  146. * @handle: handle of the loaded image
  147. * @systable: system table
  148. * @return: EFI_ST_SUCCESS for success
  149. */
  150. static int setup(const efi_handle_t img_handle,
  151. const struct efi_system_table *systable)
  152. {
  153. void *acpi;
  154. systemtab = systable;
  155. boottime = systable->boottime;
  156. acpi = efi_st_get_config_table(&acpi_guid);
  157. fdt = efi_st_get_config_table(&fdt_guid);
  158. if (!fdt) {
  159. efi_st_error("Missing device tree\n");
  160. return EFI_ST_FAILURE;
  161. }
  162. if (acpi) {
  163. efi_st_error("Found ACPI table and device tree\n");
  164. return EFI_ST_FAILURE;
  165. }
  166. return EFI_ST_SUCCESS;
  167. }
  168. /*
  169. * Execute unit test.
  170. *
  171. * @return: EFI_ST_SUCCESS for success
  172. */
  173. static int execute(void)
  174. {
  175. char *str;
  176. efi_status_t ret;
  177. str = get_property(L"compatible", NULL);
  178. if (str) {
  179. efi_st_printf("compatible: %s\n", str);
  180. ret = boottime->free_pool(str);
  181. if (ret != EFI_SUCCESS) {
  182. efi_st_error("FreePool failed\n");
  183. return EFI_ST_FAILURE;
  184. }
  185. } else {
  186. efi_st_error("Missing property 'compatible'\n");
  187. return EFI_ST_FAILURE;
  188. }
  189. str = get_property(L"serial-number", NULL);
  190. if (str) {
  191. efi_st_printf("serial-number: %s\n", str);
  192. ret = boottime->free_pool(str);
  193. if (ret != EFI_SUCCESS) {
  194. efi_st_error("FreePool failed\n");
  195. return EFI_ST_FAILURE;
  196. }
  197. }
  198. str = get_property(L"boot-hartid", L"chosen");
  199. if (IS_ENABLED(CONFIG_RISCV)) {
  200. if (str) {
  201. efi_st_printf("boot-hartid: %u\n",
  202. f2h(*(fdt32_t *)str));
  203. ret = boottime->free_pool(str);
  204. if (ret != EFI_SUCCESS) {
  205. efi_st_error("FreePool failed\n");
  206. return EFI_ST_FAILURE;
  207. }
  208. } else {
  209. efi_st_error("boot-hartid not found\n");
  210. return EFI_ST_FAILURE;
  211. }
  212. }
  213. return EFI_ST_SUCCESS;
  214. }
  215. EFI_UNIT_TEST(fdt) = {
  216. .name = "device tree",
  217. .phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
  218. .setup = setup,
  219. .execute = execute,
  220. };