helloworld.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Hello world EFI application
  4. *
  5. * Copyright 2020, Heinrich Schuchardt <xypron.glpk@gmx.de>
  6. *
  7. * This test program is used to test the invocation of an EFI application.
  8. * It writes
  9. *
  10. * * a greeting
  11. * * the firmware's UEFI version
  12. * * the installed configuration tables
  13. * * the boot device's device path and the file path
  14. *
  15. * to the console.
  16. */
  17. #include <efi_api.h>
  18. static const efi_guid_t loaded_image_guid = EFI_LOADED_IMAGE_PROTOCOL_GUID;
  19. static const efi_guid_t device_path_to_text_protocol_guid =
  20. EFI_DEVICE_PATH_TO_TEXT_PROTOCOL_GUID;
  21. static const efi_guid_t device_path_guid = EFI_DEVICE_PATH_PROTOCOL_GUID;
  22. static const efi_guid_t fdt_guid = EFI_FDT_GUID;
  23. static const efi_guid_t acpi_guid = EFI_ACPI_TABLE_GUID;
  24. static const efi_guid_t smbios_guid = SMBIOS_TABLE_GUID;
  25. static struct efi_system_table *systable;
  26. static struct efi_boot_services *boottime;
  27. static struct efi_simple_text_output_protocol *con_out;
  28. /*
  29. * Print an unsigned 32bit value as decimal number to an u16 string
  30. *
  31. * @value: value to be printed
  32. * @buf: pointer to buffer address
  33. * on return position of terminating zero word
  34. */
  35. static void uint2dec(u32 value, u16 **buf)
  36. {
  37. u16 *pos = *buf;
  38. int i;
  39. u16 c;
  40. u64 f;
  41. /*
  42. * Increment by .5 and multiply with
  43. * (2 << 60) / 1,000,000,000 = 0x44B82FA0.9B5A52CC
  44. * to move the first digit to bit 60-63.
  45. */
  46. f = 0x225C17D0;
  47. f += (0x9B5A52DULL * value) >> 28;
  48. f += 0x44B82FA0ULL * value;
  49. for (i = 0; i < 10; ++i) {
  50. /* Write current digit */
  51. c = f >> 60;
  52. if (c || pos != *buf)
  53. *pos++ = c + '0';
  54. /* Eliminate current digit */
  55. f &= 0xfffffffffffffff;
  56. /* Get next digit */
  57. f *= 0xaULL;
  58. }
  59. if (pos == *buf)
  60. *pos++ = '0';
  61. *pos = 0;
  62. *buf = pos;
  63. }
  64. /**
  65. * print_uefi_revision() - print UEFI revision number
  66. */
  67. static void print_uefi_revision(void)
  68. {
  69. u16 rev[13] = {0};
  70. u16 *buf = rev;
  71. u16 digit;
  72. uint2dec(systable->hdr.revision >> 16, &buf);
  73. *buf++ = '.';
  74. uint2dec(systable->hdr.revision & 0xffff, &buf);
  75. /* Minor revision is only to be shown if non-zero */
  76. digit = *--buf;
  77. if (digit == '0') {
  78. *buf = 0;
  79. } else {
  80. *buf++ = '.';
  81. *buf = digit;
  82. }
  83. con_out->output_string(con_out, u"Running on UEFI ");
  84. con_out->output_string(con_out, rev);
  85. con_out->output_string(con_out, u"\r\n");
  86. }
  87. /**
  88. * print_config_tables() - print configuration tables
  89. */
  90. static void print_config_tables(void)
  91. {
  92. efi_uintn_t i;
  93. /* Find configuration tables */
  94. for (i = 0; i < systable->nr_tables; ++i) {
  95. if (!memcmp(&systable->tables[i].guid, &fdt_guid,
  96. sizeof(efi_guid_t)))
  97. con_out->output_string
  98. (con_out, u"Have device tree\r\n");
  99. if (!memcmp(&systable->tables[i].guid, &acpi_guid,
  100. sizeof(efi_guid_t)))
  101. con_out->output_string
  102. (con_out, u"Have ACPI 2.0 table\r\n");
  103. if (!memcmp(&systable->tables[i].guid, &smbios_guid,
  104. sizeof(efi_guid_t)))
  105. con_out->output_string
  106. (con_out, u"Have SMBIOS table\r\n");
  107. }
  108. }
  109. /**
  110. * print_load_options() - print load options
  111. *
  112. * @systable: system table
  113. * @con_out: simple text output protocol
  114. */
  115. static void print_load_options(struct efi_loaded_image *loaded_image)
  116. {
  117. /* Output the load options */
  118. con_out->output_string(con_out, u"Load options: ");
  119. if (loaded_image->load_options_size && loaded_image->load_options)
  120. con_out->output_string(con_out,
  121. (u16 *)loaded_image->load_options);
  122. else
  123. con_out->output_string(con_out, u"<none>");
  124. con_out->output_string(con_out, u"\r\n");
  125. }
  126. /**
  127. * print_device_path() - print device path
  128. *
  129. * @device_path: device path to print
  130. * @dp2txt: device path to text protocol
  131. */
  132. static
  133. efi_status_t print_device_path(struct efi_device_path *device_path,
  134. struct efi_device_path_to_text_protocol *dp2txt)
  135. {
  136. u16 *string;
  137. efi_status_t ret;
  138. if (!device_path) {
  139. con_out->output_string(con_out, u"<none>\r\n");
  140. return EFI_SUCCESS;
  141. }
  142. string = dp2txt->convert_device_path_to_text(device_path, true, false);
  143. if (!string) {
  144. con_out->output_string
  145. (con_out, u"Cannot convert device path to text\r\n");
  146. return EFI_OUT_OF_RESOURCES;
  147. }
  148. con_out->output_string(con_out, string);
  149. con_out->output_string(con_out, u"\r\n");
  150. ret = boottime->free_pool(string);
  151. if (ret != EFI_SUCCESS) {
  152. con_out->output_string(con_out, u"Cannot free pool memory\r\n");
  153. return ret;
  154. }
  155. return EFI_SUCCESS;
  156. }
  157. /**
  158. * efi_main() - entry point of the EFI application.
  159. *
  160. * @handle: handle of the loaded image
  161. * @systab: system table
  162. * Return: status code
  163. */
  164. efi_status_t EFIAPI efi_main(efi_handle_t handle,
  165. struct efi_system_table *systab)
  166. {
  167. struct efi_loaded_image *loaded_image;
  168. struct efi_device_path_to_text_protocol *device_path_to_text;
  169. struct efi_device_path *device_path;
  170. efi_status_t ret;
  171. systable = systab;
  172. boottime = systable->boottime;
  173. con_out = systable->con_out;
  174. /* UEFI requires CR LF */
  175. con_out->output_string(con_out, u"Hello, world!\r\n");
  176. print_uefi_revision();
  177. print_config_tables();
  178. /* Get the loaded image protocol */
  179. ret = boottime->open_protocol(handle, &loaded_image_guid,
  180. (void **)&loaded_image, NULL, NULL,
  181. EFI_OPEN_PROTOCOL_GET_PROTOCOL);
  182. if (ret != EFI_SUCCESS) {
  183. con_out->output_string
  184. (con_out, u"Cannot open loaded image protocol\r\n");
  185. goto out;
  186. }
  187. print_load_options(loaded_image);
  188. /* Get the device path to text protocol */
  189. ret = boottime->locate_protocol(&device_path_to_text_protocol_guid,
  190. NULL, (void **)&device_path_to_text);
  191. if (ret != EFI_SUCCESS) {
  192. con_out->output_string
  193. (con_out, u"Cannot open device path to text protocol\r\n");
  194. goto out;
  195. }
  196. con_out->output_string(con_out, u"File path: ");
  197. ret = print_device_path(loaded_image->file_path, device_path_to_text);
  198. if (ret != EFI_SUCCESS)
  199. goto out;
  200. if (!loaded_image->device_handle) {
  201. con_out->output_string
  202. (con_out, u"Missing device handle\r\n");
  203. goto out;
  204. }
  205. ret = boottime->open_protocol(loaded_image->device_handle,
  206. &device_path_guid,
  207. (void **)&device_path, NULL, NULL,
  208. EFI_OPEN_PROTOCOL_GET_PROTOCOL);
  209. if (ret != EFI_SUCCESS) {
  210. con_out->output_string
  211. (con_out, u"Missing device path for device handle\r\n");
  212. goto out;
  213. }
  214. con_out->output_string(con_out, u"Boot device: ");
  215. ret = print_device_path(device_path, device_path_to_text);
  216. if (ret != EFI_SUCCESS)
  217. goto out;
  218. out:
  219. boottime->exit(handle, ret, 0, NULL);
  220. /* We should never arrive here */
  221. return ret;
  222. }