helloworld.c 2.6 KB

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