helloworld.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 = LOADED_IMAGE_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. static int hw_memcmp(const void *buf1, const void *buf2, size_t length)
  18. {
  19. const u8 *pos1 = buf1;
  20. const u8 *pos2 = buf2;
  21. for (; length; --length) {
  22. if (*pos1 != *pos2)
  23. return *pos1 - *pos2;
  24. ++pos1;
  25. ++pos2;
  26. }
  27. return 0;
  28. }
  29. /*
  30. * Entry point of the EFI application.
  31. *
  32. * @handle handle of the loaded image
  33. * @systable system table
  34. * @return status code
  35. */
  36. efi_status_t EFIAPI efi_main(efi_handle_t handle,
  37. struct efi_system_table *systable)
  38. {
  39. struct efi_simple_text_output_protocol *con_out = systable->con_out;
  40. struct efi_boot_services *boottime = systable->boottime;
  41. struct efi_loaded_image *loaded_image;
  42. efi_status_t ret;
  43. efi_uintn_t i;
  44. u16 rev[] = L"0.0.0";
  45. con_out->output_string(con_out, L"Hello, world!\n");
  46. /* Print the revision number */
  47. rev[0] = (systable->hdr.revision >> 16) + '0';
  48. rev[4] = systable->hdr.revision & 0xffff;
  49. for (; rev[4] >= 10;) {
  50. rev[4] -= 10;
  51. ++rev[2];
  52. }
  53. /* Third digit is only to be shown if non-zero */
  54. if (rev[4])
  55. rev[4] += '0';
  56. else
  57. rev[3] = 0;
  58. con_out->output_string(con_out, L"Running on UEFI ");
  59. con_out->output_string(con_out, rev);
  60. con_out->output_string(con_out, L"\n");
  61. /* Get the loaded image protocol */
  62. ret = boottime->handle_protocol(handle, &loaded_image_guid,
  63. (void **)&loaded_image);
  64. if (ret != EFI_SUCCESS) {
  65. con_out->output_string(con_out,
  66. L"Cannot open loaded image protocol\n");
  67. goto out;
  68. }
  69. /* Find configuration tables */
  70. for (i = 0; i < systable->nr_tables; ++i) {
  71. if (!hw_memcmp(&systable->tables[i].guid, &fdt_guid,
  72. sizeof(efi_guid_t)))
  73. con_out->output_string(con_out, L"Have device tree\n");
  74. if (!hw_memcmp(&systable->tables[i].guid, &acpi_guid,
  75. sizeof(efi_guid_t)))
  76. con_out->output_string(con_out, L"Have ACPI 2.0 table\n");
  77. if (!hw_memcmp(&systable->tables[i].guid, &smbios_guid,
  78. sizeof(efi_guid_t)))
  79. con_out->output_string(con_out, L"Have SMBIOS table\n");
  80. }
  81. /* Output the load options */
  82. con_out->output_string(con_out, L"Load options: ");
  83. if (loaded_image->load_options_size && loaded_image->load_options)
  84. con_out->output_string(con_out,
  85. (u16 *)loaded_image->load_options);
  86. else
  87. con_out->output_string(con_out, L"<none>");
  88. con_out->output_string(con_out, L"\n");
  89. out:
  90. boottime->exit(handle, ret, 0, NULL);
  91. /* We should never arrive here */
  92. return ret;
  93. }