helloworld.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * EFI hello world
  3. *
  4. * Copyright (c) 2016 Google, Inc
  5. * Written by Simon Glass <sjg@chromium.org>
  6. *
  7. * SPDX-License-Identifier: GPL-2.0+
  8. *
  9. * This program demonstrates calling a boottime service.
  10. * It writes a greeting and the load options to the console.
  11. */
  12. #include <common.h>
  13. #include <efi_api.h>
  14. static const efi_guid_t loaded_image_guid = LOADED_IMAGE_GUID;
  15. static const efi_guid_t fdt_guid = EFI_FDT_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, &smbios_guid,
  75. sizeof(efi_guid_t)))
  76. con_out->output_string(con_out, L"Have SMBIOS table\n");
  77. }
  78. /* Output the load options */
  79. con_out->output_string(con_out, L"Load options: ");
  80. if (loaded_image->load_options_size && loaded_image->load_options)
  81. con_out->output_string(con_out,
  82. (u16 *)loaded_image->load_options);
  83. else
  84. con_out->output_string(con_out, L"<none>");
  85. con_out->output_string(con_out, L"\n");
  86. out:
  87. boottime->exit(handle, ret, 0, NULL);
  88. /* We should never arrive here */
  89. return ret;
  90. }