helloworld.c 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * EFI hello world
  4. *
  5. * Copyright (c) 2016 Google, Inc
  6. * Written by Simon Glass <sjg@chromium.org>
  7. *
  8. * This program demonstrates calling a boottime service.
  9. * It writes a greeting and the load options to the console.
  10. */
  11. #include <common.h>
  12. #include <efi_api.h>
  13. static const efi_guid_t loaded_image_guid = EFI_LOADED_IMAGE_PROTOCOL_GUID;
  14. static const efi_guid_t fdt_guid = EFI_FDT_GUID;
  15. static const efi_guid_t acpi_guid = EFI_ACPI_TABLE_GUID;
  16. static const efi_guid_t smbios_guid = SMBIOS_TABLE_GUID;
  17. /**
  18. * efi_main() - entry point of the EFI application.
  19. *
  20. * @handle: handle of the loaded image
  21. * @systable: system table
  22. * @return: status code
  23. */
  24. efi_status_t EFIAPI efi_main(efi_handle_t handle,
  25. struct efi_system_table *systable)
  26. {
  27. struct efi_simple_text_output_protocol *con_out = systable->con_out;
  28. struct efi_boot_services *boottime = systable->boottime;
  29. struct efi_loaded_image *loaded_image;
  30. efi_status_t ret;
  31. efi_uintn_t i;
  32. u16 rev[] = L"0.0.0";
  33. /* UEFI requires CR LF */
  34. con_out->output_string(con_out, L"Hello, world!\r\n");
  35. /* Print the revision number */
  36. rev[0] = (systable->hdr.revision >> 16) + '0';
  37. rev[4] = systable->hdr.revision & 0xffff;
  38. for (; rev[4] >= 10;) {
  39. rev[4] -= 10;
  40. ++rev[2];
  41. }
  42. /* Third digit is only to be shown if non-zero */
  43. if (rev[4])
  44. rev[4] += '0';
  45. else
  46. rev[3] = 0;
  47. con_out->output_string(con_out, L"Running on UEFI ");
  48. con_out->output_string(con_out, rev);
  49. con_out->output_string(con_out, L"\r\n");
  50. /* Get the loaded image protocol */
  51. ret = boottime->handle_protocol(handle, &loaded_image_guid,
  52. (void **)&loaded_image);
  53. if (ret != EFI_SUCCESS) {
  54. con_out->output_string
  55. (con_out, L"Cannot open loaded image protocol\r\n");
  56. goto out;
  57. }
  58. /* Find configuration tables */
  59. for (i = 0; i < systable->nr_tables; ++i) {
  60. if (!memcmp(&systable->tables[i].guid, &fdt_guid,
  61. sizeof(efi_guid_t)))
  62. con_out->output_string
  63. (con_out, L"Have device tree\r\n");
  64. if (!memcmp(&systable->tables[i].guid, &acpi_guid,
  65. sizeof(efi_guid_t)))
  66. con_out->output_string
  67. (con_out, L"Have ACPI 2.0 table\r\n");
  68. if (!memcmp(&systable->tables[i].guid, &smbios_guid,
  69. sizeof(efi_guid_t)))
  70. con_out->output_string
  71. (con_out, L"Have SMBIOS table\r\n");
  72. }
  73. /* Output the load options */
  74. con_out->output_string(con_out, L"Load options: ");
  75. if (loaded_image->load_options_size && loaded_image->load_options)
  76. con_out->output_string(con_out,
  77. (u16 *)loaded_image->load_options);
  78. else
  79. con_out->output_string(con_out, L"<none>");
  80. con_out->output_string(con_out, L"\r\n");
  81. out:
  82. boottime->exit(handle, ret, 0, NULL);
  83. /* We should never arrive here */
  84. return ret;
  85. }