efi_selftest_fdt.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 struct efi_boot_services *boottime;
  15. static const char *fdt;
  16. /* This should be sufficent for */
  17. #define BUFFERSIZE 0x100000
  18. static efi_guid_t fdt_guid = EFI_FDT_GUID;
  19. /*
  20. * Convert FDT value to host endianness.
  21. *
  22. * @val FDT value
  23. * @return converted value
  24. */
  25. static uint32_t f2h(fdt32_t val)
  26. {
  27. char *buf = (char *)&val;
  28. char i;
  29. #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
  30. /* Swap the bytes */
  31. i = buf[0]; buf[0] = buf[3]; buf[3] = i;
  32. i = buf[1]; buf[1] = buf[2]; buf[2] = i;
  33. #endif
  34. return *(uint32_t *)buf;
  35. }
  36. /*
  37. * Return the value of a property of the FDT root node.
  38. *
  39. * @name name of the property
  40. * @return value of the property
  41. */
  42. static char *get_property(const u16 *property)
  43. {
  44. struct fdt_header *header = (struct fdt_header *)fdt;
  45. const fdt32_t *pos;
  46. const char *strings;
  47. if (!header)
  48. return NULL;
  49. if (f2h(header->magic) != FDT_MAGIC) {
  50. printf("Wrong magic\n");
  51. return NULL;
  52. }
  53. pos = (fdt32_t *)(fdt + f2h(header->off_dt_struct));
  54. strings = fdt + f2h(header->off_dt_strings);
  55. for (;;) {
  56. switch (f2h(pos[0])) {
  57. case FDT_BEGIN_NODE: {
  58. char *c = (char *)&pos[1];
  59. size_t i;
  60. for (i = 0; c[i]; ++i)
  61. ;
  62. pos = &pos[2 + (i >> 2)];
  63. break;
  64. }
  65. case FDT_PROP: {
  66. struct fdt_property *prop = (struct fdt_property *)pos;
  67. const char *label = &strings[f2h(prop->nameoff)];
  68. efi_status_t ret;
  69. /* Check if this is the property to be returned */
  70. if (!efi_st_strcmp_16_8(property, label)) {
  71. char *str;
  72. efi_uintn_t len = f2h(prop->len);
  73. if (!len)
  74. return NULL;
  75. /*
  76. * The string might not be 0 terminated.
  77. * It is safer to make a copy.
  78. */
  79. ret = boottime->allocate_pool(
  80. EFI_LOADER_DATA, len + 1,
  81. (void **)&str);
  82. if (ret != EFI_SUCCESS) {
  83. efi_st_printf("AllocatePool failed\n");
  84. return NULL;
  85. }
  86. boottime->copy_mem(str, &pos[3], len);
  87. str[len] = 0;
  88. return str;
  89. }
  90. pos = &pos[3 + ((f2h(prop->len) + 3) >> 2)];
  91. break;
  92. }
  93. case FDT_NOP:
  94. pos = &pos[1];
  95. break;
  96. default:
  97. return NULL;
  98. }
  99. }
  100. }
  101. /*
  102. * Setup unit test.
  103. *
  104. * @handle: handle of the loaded image
  105. * @systable: system table
  106. * @return: EFI_ST_SUCCESS for success
  107. */
  108. static int setup(const efi_handle_t img_handle,
  109. const struct efi_system_table *systable)
  110. {
  111. efi_uintn_t i;
  112. boottime = systable->boottime;
  113. /* Find configuration tables */
  114. for (i = 0; i < systable->nr_tables; ++i) {
  115. if (!efi_st_memcmp(&systable->tables[i].guid, &fdt_guid,
  116. sizeof(efi_guid_t)))
  117. fdt = systable->tables[i].table;
  118. }
  119. if (!fdt) {
  120. efi_st_error("Missing device tree\n");
  121. return EFI_ST_FAILURE;
  122. }
  123. return EFI_ST_SUCCESS;
  124. }
  125. /*
  126. * Execute unit test.
  127. *
  128. * @return: EFI_ST_SUCCESS for success
  129. */
  130. static int execute(void)
  131. {
  132. char *str;
  133. efi_status_t ret;
  134. str = get_property(L"compatible");
  135. if (str) {
  136. efi_st_printf("compatible: %s\n", str);
  137. ret = boottime->free_pool(str);
  138. if (ret != EFI_SUCCESS) {
  139. efi_st_error("FreePool failed\n");
  140. return EFI_ST_FAILURE;
  141. }
  142. } else {
  143. efi_st_printf("Missing property 'compatible'\n");
  144. return EFI_ST_FAILURE;
  145. }
  146. str = get_property(L"serial-number");
  147. if (str) {
  148. efi_st_printf("serial-number: %s\n", str);
  149. ret = boottime->free_pool(str);
  150. if (ret != EFI_SUCCESS) {
  151. efi_st_error("FreePool failed\n");
  152. return EFI_ST_FAILURE;
  153. }
  154. }
  155. return EFI_ST_SUCCESS;
  156. }
  157. EFI_UNIT_TEST(fdt) = {
  158. .name = "device tree",
  159. .phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
  160. .setup = setup,
  161. .execute = execute,
  162. .on_request = true,
  163. };