Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | fac4ced | 2016-11-07 08:47:08 -0700 | [diff] [blame] | 2 | /* |
| 3 | * EFI hello world |
| 4 | * |
| 5 | * Copyright (c) 2016 Google, Inc |
| 6 | * Written by Simon Glass <sjg@chromium.org> |
| 7 | * |
Heinrich Schuchardt | 0864580 | 2017-11-26 14:05:20 +0100 | [diff] [blame] | 8 | * This program demonstrates calling a boottime service. |
| 9 | * It writes a greeting and the load options to the console. |
Simon Glass | fac4ced | 2016-11-07 08:47:08 -0700 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <common.h> |
Simon Glass | fac4ced | 2016-11-07 08:47:08 -0700 | [diff] [blame] | 13 | #include <efi_api.h> |
| 14 | |
Alexander Graf | c28a947 | 2017-12-11 09:40:47 +0100 | [diff] [blame] | 15 | static const efi_guid_t loaded_image_guid = LOADED_IMAGE_GUID; |
Heinrich Schuchardt | 5a77152 | 2018-01-19 20:24:42 +0100 | [diff] [blame] | 16 | static const efi_guid_t fdt_guid = EFI_FDT_GUID; |
| 17 | static const efi_guid_t smbios_guid = SMBIOS_TABLE_GUID; |
| 18 | |
| 19 | static int hw_memcmp(const void *buf1, const void *buf2, size_t length) |
| 20 | { |
| 21 | const u8 *pos1 = buf1; |
| 22 | const u8 *pos2 = buf2; |
| 23 | |
| 24 | for (; length; --length) { |
| 25 | if (*pos1 != *pos2) |
| 26 | return *pos1 - *pos2; |
| 27 | ++pos1; |
| 28 | ++pos2; |
| 29 | } |
| 30 | return 0; |
| 31 | } |
Alexander Graf | c28a947 | 2017-12-11 09:40:47 +0100 | [diff] [blame] | 32 | |
Heinrich Schuchardt | 0864580 | 2017-11-26 14:05:20 +0100 | [diff] [blame] | 33 | /* |
| 34 | * Entry point of the EFI application. |
| 35 | * |
| 36 | * @handle handle of the loaded image |
| 37 | * @systable system table |
| 38 | * @return status code |
| 39 | */ |
Simon Glass | fac4ced | 2016-11-07 08:47:08 -0700 | [diff] [blame] | 40 | efi_status_t EFIAPI efi_main(efi_handle_t handle, |
| 41 | struct efi_system_table *systable) |
| 42 | { |
| 43 | struct efi_simple_text_output_protocol *con_out = systable->con_out; |
| 44 | struct efi_boot_services *boottime = systable->boottime; |
Heinrich Schuchardt | 0864580 | 2017-11-26 14:05:20 +0100 | [diff] [blame] | 45 | struct efi_loaded_image *loaded_image; |
Heinrich Schuchardt | 0864580 | 2017-11-26 14:05:20 +0100 | [diff] [blame] | 46 | efi_status_t ret; |
Heinrich Schuchardt | 5a77152 | 2018-01-19 20:24:42 +0100 | [diff] [blame] | 47 | efi_uintn_t i; |
Heinrich Schuchardt | a0a61dd | 2018-02-05 18:24:26 +0100 | [diff] [blame] | 48 | u16 rev[] = L"0.0.0"; |
Simon Glass | fac4ced | 2016-11-07 08:47:08 -0700 | [diff] [blame] | 49 | |
| 50 | con_out->output_string(con_out, L"Hello, world!\n"); |
Heinrich Schuchardt | 0864580 | 2017-11-26 14:05:20 +0100 | [diff] [blame] | 51 | |
Heinrich Schuchardt | a0a61dd | 2018-02-05 18:24:26 +0100 | [diff] [blame] | 52 | /* Print the revision number */ |
| 53 | rev[0] = (systable->hdr.revision >> 16) + '0'; |
| 54 | rev[4] = systable->hdr.revision & 0xffff; |
| 55 | for (; rev[4] >= 10;) { |
| 56 | rev[4] -= 10; |
| 57 | ++rev[2]; |
| 58 | } |
| 59 | /* Third digit is only to be shown if non-zero */ |
| 60 | if (rev[4]) |
| 61 | rev[4] += '0'; |
| 62 | else |
| 63 | rev[3] = 0; |
| 64 | |
| 65 | con_out->output_string(con_out, L"Running on UEFI "); |
| 66 | con_out->output_string(con_out, rev); |
| 67 | con_out->output_string(con_out, L"\n"); |
| 68 | |
Heinrich Schuchardt | 0864580 | 2017-11-26 14:05:20 +0100 | [diff] [blame] | 69 | /* Get the loaded image protocol */ |
| 70 | ret = boottime->handle_protocol(handle, &loaded_image_guid, |
| 71 | (void **)&loaded_image); |
| 72 | if (ret != EFI_SUCCESS) { |
| 73 | con_out->output_string(con_out, |
| 74 | L"Cannot open loaded image protocol\n"); |
| 75 | goto out; |
| 76 | } |
Heinrich Schuchardt | 5a77152 | 2018-01-19 20:24:42 +0100 | [diff] [blame] | 77 | /* Find configuration tables */ |
| 78 | for (i = 0; i < systable->nr_tables; ++i) { |
| 79 | if (!hw_memcmp(&systable->tables[i].guid, &fdt_guid, |
| 80 | sizeof(efi_guid_t))) |
| 81 | con_out->output_string(con_out, L"Have device tree\n"); |
| 82 | if (!hw_memcmp(&systable->tables[i].guid, &smbios_guid, |
| 83 | sizeof(efi_guid_t))) |
| 84 | con_out->output_string(con_out, L"Have SMBIOS table\n"); |
| 85 | } |
Heinrich Schuchardt | 0864580 | 2017-11-26 14:05:20 +0100 | [diff] [blame] | 86 | /* Output the load options */ |
| 87 | con_out->output_string(con_out, L"Load options: "); |
| 88 | if (loaded_image->load_options_size && loaded_image->load_options) |
| 89 | con_out->output_string(con_out, |
| 90 | (u16 *)loaded_image->load_options); |
| 91 | else |
| 92 | con_out->output_string(con_out, L"<none>"); |
| 93 | con_out->output_string(con_out, L"\n"); |
| 94 | |
| 95 | out: |
| 96 | boottime->exit(handle, ret, 0, NULL); |
Simon Glass | fac4ced | 2016-11-07 08:47:08 -0700 | [diff] [blame] | 97 | |
Heinrich Schuchardt | 0864580 | 2017-11-26 14:05:20 +0100 | [diff] [blame] | 98 | /* We should never arrive here */ |
| 99 | return ret; |
Simon Glass | fac4ced | 2016-11-07 08:47:08 -0700 | [diff] [blame] | 100 | } |