helloworld.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. con_out->output_string(con_out, L"Hello, world!\n");
  45. /* Get the loaded image protocol */
  46. ret = boottime->handle_protocol(handle, &loaded_image_guid,
  47. (void **)&loaded_image);
  48. if (ret != EFI_SUCCESS) {
  49. con_out->output_string(con_out,
  50. L"Cannot open loaded image protocol\n");
  51. goto out;
  52. }
  53. /* Find configuration tables */
  54. for (i = 0; i < systable->nr_tables; ++i) {
  55. if (!hw_memcmp(&systable->tables[i].guid, &fdt_guid,
  56. sizeof(efi_guid_t)))
  57. con_out->output_string(con_out, L"Have device tree\n");
  58. if (!hw_memcmp(&systable->tables[i].guid, &smbios_guid,
  59. sizeof(efi_guid_t)))
  60. con_out->output_string(con_out, L"Have SMBIOS table\n");
  61. }
  62. /* Output the load options */
  63. con_out->output_string(con_out, L"Load options: ");
  64. if (loaded_image->load_options_size && loaded_image->load_options)
  65. con_out->output_string(con_out,
  66. (u16 *)loaded_image->load_options);
  67. else
  68. con_out->output_string(con_out, L"<none>");
  69. con_out->output_string(con_out, L"\n");
  70. out:
  71. boottime->exit(handle, ret, 0, NULL);
  72. /* We should never arrive here */
  73. return ret;
  74. }