diff options
Diffstat (limited to 'lib/efi_selftest')
-rw-r--r-- | lib/efi_selftest/Makefile | 8 | ||||
-rw-r--r-- | lib/efi_selftest/efi_selftest.c | 37 | ||||
-rw-r--r-- | lib/efi_selftest/efi_selftest_console.c | 41 | ||||
-rw-r--r-- | lib/efi_selftest/efi_selftest_events.c | 84 | ||||
-rw-r--r-- | lib/efi_selftest/efi_selftest_exitbootservices.c | 46 | ||||
-rw-r--r-- | lib/efi_selftest/efi_selftest_snp.c | 431 | ||||
-rw-r--r-- | lib/efi_selftest/efi_selftest_tpl.c | 90 | ||||
-rw-r--r-- | lib/efi_selftest/efi_selftest_util.c | 25 |
8 files changed, 644 insertions, 118 deletions
diff --git a/lib/efi_selftest/Makefile b/lib/efi_selftest/Makefile index 30f1960933..e446046e02 100644 --- a/lib/efi_selftest/Makefile +++ b/lib/efi_selftest/Makefile @@ -15,12 +15,18 @@ CFLAGS_efi_selftest_events.o := $(CFLAGS_EFI) CFLAGS_REMOVE_efi_selftest_events.o := $(CFLAGS_NON_EFI) CFLAGS_efi_selftest_exitbootservices.o := $(CFLAGS_EFI) CFLAGS_REMOVE_efi_selftest_exitbootservices.o := $(CFLAGS_NON_EFI) +CFLAGS_efi_selftest_snp.o := $(CFLAGS_EFI) +CFLAGS_REMOVE_efi_selftest_snp.o := $(CFLAGS_NON_EFI) CFLAGS_efi_selftest_tpl.o := $(CFLAGS_EFI) CFLAGS_REMOVE_efi_selftest_tpl.o := $(CFLAGS_NON_EFI) +CFLAGS_efi_selftest_util.o := $(CFLAGS_EFI) +CFLAGS_REMOVE_efi_selftest_util.o := $(CFLAGS_NON_EFI) obj-$(CONFIG_CMD_BOOTEFI_SELFTEST) += \ efi_selftest.o \ efi_selftest_console.o \ efi_selftest_events.o \ efi_selftest_exitbootservices.o \ -efi_selftest_tpl.o +efi_selftest_snp.o \ +efi_selftest_tpl.o \ +efi_selftest_util.o diff --git a/lib/efi_selftest/efi_selftest.c b/lib/efi_selftest/efi_selftest.c index efec832e98..45d8d3d384 100644 --- a/lib/efi_selftest/efi_selftest.c +++ b/lib/efi_selftest/efi_selftest.c @@ -35,8 +35,8 @@ void efi_st_exit_boot_services(void) ret = boottime->get_memory_map(&map_size, NULL, &map_key, &desc_size, &desc_version); if (ret != EFI_BUFFER_TOO_SMALL) { - efi_st_printf("ERROR: GetMemoryMap did not return " - "EFI_BUFFER_TOO_SMALL\n"); + efi_st_error( + "GetMemoryMap did not return EFI_BUFFER_TOO_SMALL\n"); return; } /* Allocate extra space for newly allocated memory */ @@ -44,21 +44,18 @@ void efi_st_exit_boot_services(void) ret = boottime->allocate_pool(EFI_BOOT_SERVICES_DATA, map_size, (void **)&memory_map); if (ret != EFI_SUCCESS) { - efi_st_printf("ERROR: AllocatePool did not return " - "EFI_SUCCESS\n"); + efi_st_error("AllocatePool did not return EFI_SUCCESS\n"); return; } ret = boottime->get_memory_map(&map_size, memory_map, &map_key, &desc_size, &desc_version); if (ret != EFI_SUCCESS) { - efi_st_printf("ERROR: GetMemoryMap did not return " - "EFI_SUCCESS\n"); + efi_st_error("GetMemoryMap did not return EFI_SUCCESS\n"); return; } ret = boottime->exit_boot_services(handle, map_key); if (ret != EFI_SUCCESS) { - efi_st_printf("ERROR: ExitBootServices did not return " - "EFI_SUCCESS\n"); + efi_st_error("ExitBootServices did not return EFI_SUCCESS\n"); return; } efi_st_printf("\nBoot services terminated\n"); @@ -69,17 +66,18 @@ void efi_st_exit_boot_services(void) * * @test the test to be executed * @failures counter that will be incremented if a failure occurs + * @return EFI_ST_SUCCESS for success */ static int setup(struct efi_unit_test *test, unsigned int *failures) { int ret; if (!test->setup) - return 0; + return EFI_ST_SUCCESS; efi_st_printf("\nSetting up '%s'\n", test->name); ret = test->setup(handle, systable); - if (ret) { - efi_st_printf("ERROR: Setting up '%s' failed\n", test->name); + if (ret != EFI_ST_SUCCESS) { + efi_st_error("Setting up '%s' failed\n", test->name); ++*failures; } else { efi_st_printf("Setting up '%s' succeeded\n", test->name); @@ -92,17 +90,18 @@ static int setup(struct efi_unit_test *test, unsigned int *failures) * * @test the test to be executed * @failures counter that will be incremented if a failure occurs + * @return EFI_ST_SUCCESS for success */ static int execute(struct efi_unit_test *test, unsigned int *failures) { int ret; if (!test->execute) - return 0; + return EFI_ST_SUCCESS; efi_st_printf("\nExecuting '%s'\n", test->name); ret = test->execute(); - if (ret) { - efi_st_printf("ERROR: Executing '%s' failed\n", test->name); + if (ret != EFI_ST_SUCCESS) { + efi_st_error("Executing '%s' failed\n", test->name); ++*failures; } else { efi_st_printf("Executing '%s' succeeded\n", test->name); @@ -115,17 +114,18 @@ static int execute(struct efi_unit_test *test, unsigned int *failures) * * @test the test to be torn down * @failures counter that will be incremented if a failure occurs + * @return EFI_ST_SUCCESS for success */ static int teardown(struct efi_unit_test *test, unsigned int *failures) { int ret; if (!test->teardown) - return 0; + return EFI_ST_SUCCESS; efi_st_printf("\nTearing down '%s'\n", test->name); ret = test->teardown(); - if (ret) { - efi_st_printf("ERROR: Tearing down '%s' failed\n", test->name); + if (ret != EFI_ST_SUCCESS) { + efi_st_error("Tearing down '%s' failed\n", test->name); ++*failures; } else { efi_st_printf("Tearing down '%s' succeeded\n", test->name); @@ -213,7 +213,8 @@ efi_status_t EFIAPI efi_selftest(efi_handle_t image_handle, efi_st_get_key(); runtime->reset_system(EFI_RESET_WARM, EFI_NOT_READY, sizeof(reset_message), reset_message); - efi_st_printf("\nERROR: reset failed.\n"); + efi_st_printf("\n"); + efi_st_error("Reset failed.\n"); return EFI_UNSUPPORTED; } diff --git a/lib/efi_selftest/efi_selftest_console.c b/lib/efi_selftest/efi_selftest_console.c index 7b5b724a61..840e2290c6 100644 --- a/lib/efi_selftest/efi_selftest_console.c +++ b/lib/efi_selftest/efi_selftest_console.c @@ -13,6 +13,37 @@ struct efi_simple_text_output_protocol *con_out; struct efi_simple_input_interface *con_in; /* + * Print a MAC address to an u16 string + * + * @pointer: mac address + * @buf: pointer to buffer address + * on return position of terminating zero word + */ +static void mac(void *pointer, u16 **buf) +{ + int i, j; + u16 c; + u8 *p = (u8 *)pointer; + u8 byte; + u16 *pos = *buf; + + for (i = 0; i < ARP_HLEN; ++i) { + if (i) + *pos++ = ':'; + byte = p[i]; + for (j = 4; j >= 0; j -= 4) { + c = (byte >> j) & 0x0f; + c += '0'; + if (c > '9') + c += 'a' - '9' - 1; + *pos++ = c; + } + } + *pos = 0; + *buf = pos; +} + +/* * Print a pointer to an u16 string * * @pointer: pointer @@ -146,7 +177,15 @@ void efi_st_printf(const char *fmt, ...) int2dec(va_arg(args, s32), &pos); break; case 'p': - pointer(va_arg(args, void*), &pos); + ++c; + switch (*c) { + case 'm': + mac(va_arg(args, void*), &pos); + break; + default: + --c; + pointer(va_arg(args, void*), &pos); + } break; case 's': s = va_arg(args, const char *); diff --git a/lib/efi_selftest/efi_selftest_events.c b/lib/efi_selftest/efi_selftest_events.c index c4f66952b9..081f31257f 100644 --- a/lib/efi_selftest/efi_selftest_events.c +++ b/lib/efi_selftest/efi_selftest_events.c @@ -14,20 +14,22 @@ static struct efi_event *event_notify; static struct efi_event *event_wait; -static unsigned int counter; +static unsigned int timer_ticks; static struct efi_boot_services *boottime; /* - * Notification function, increments a counter. + * Notification function, increments the notfication count if parameter + * context is provided. * * @event notified event - * @context pointer to the counter + * @context pointer to the notification count */ static void EFIAPI notify(struct efi_event *event, void *context) { - if (!context) - return; - ++*(unsigned int *)context; + unsigned int *count = context; + + if (count) + ++*count; } /* @@ -38,6 +40,7 @@ static void EFIAPI notify(struct efi_event *event, void *context) * * @handle: handle of the loaded image * @systable: system table + * @return: EFI_ST_SUCCESS for success */ static int setup(const efi_handle_t handle, const struct efi_system_table *systable) @@ -47,25 +50,27 @@ static int setup(const efi_handle_t handle, boottime = systable->boottime; ret = boottime->create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, - TPL_CALLBACK, notify, (void *)&counter, + TPL_CALLBACK, notify, (void *)&timer_ticks, &event_notify); if (ret != EFI_SUCCESS) { efi_st_error("could not create event\n"); - return 1; + return EFI_ST_FAILURE; } ret = boottime->create_event(EVT_TIMER | EVT_NOTIFY_WAIT, TPL_CALLBACK, notify, NULL, &event_wait); if (ret != EFI_SUCCESS) { efi_st_error("could not create event\n"); - return 1; + return EFI_ST_FAILURE; } - return 0; + return EFI_ST_SUCCESS; } /* * Tear down unit test. * * Close the events created in setup. + * + * @return: EFI_ST_SUCCESS for success */ static int teardown(void) { @@ -76,7 +81,7 @@ static int teardown(void) event_notify = NULL; if (ret != EFI_SUCCESS) { efi_st_error("could not close event\n"); - return 1; + return EFI_ST_FAILURE; } } if (event_wait) { @@ -84,10 +89,10 @@ static int teardown(void) event_wait = NULL; if (ret != EFI_SUCCESS) { efi_st_error("could not close event\n"); - return 1; + return EFI_ST_FAILURE; } } - return 0; + return EFI_ST_SUCCESS; } /* @@ -98,92 +103,95 @@ static int teardown(void) * * Run a 100 ms single shot timer and check that it is called once * while waiting for 100 ms periodic timer for two periods. + * + * @return: EFI_ST_SUCCESS for success */ static int execute(void) { - unsigned long index; + size_t index; efi_status_t ret; /* Set 10 ms timer */ - counter = 0; + timer_ticks = 0; ret = boottime->set_timer(event_notify, EFI_TIMER_PERIODIC, 100000); if (ret != EFI_SUCCESS) { efi_st_error("Could not set timer\n"); - return 1; + return EFI_ST_FAILURE; } /* Set 100 ms timer */ ret = boottime->set_timer(event_wait, EFI_TIMER_RELATIVE, 1000000); if (ret != EFI_SUCCESS) { efi_st_error("Could not set timer\n"); - return 1; + return EFI_ST_FAILURE; } + /* Set some arbitrary non-zero value to make change detectable. */ index = 5; ret = boottime->wait_for_event(1, &event_wait, &index); if (ret != EFI_SUCCESS) { efi_st_error("Could not wait for event\n"); - return 1; + return EFI_ST_FAILURE; } ret = boottime->check_event(event_wait); if (ret != EFI_NOT_READY) { efi_st_error("Signaled state was not cleared.\n"); efi_st_printf("ret = %u\n", (unsigned int)ret); - return 1; + return EFI_ST_FAILURE; } if (index != 0) { efi_st_error("WaitForEvent returned wrong index\n"); - return 1; + return EFI_ST_FAILURE; } - efi_st_printf("Counter periodic: %u\n", counter); - if (counter < 8 || counter > 12) { + efi_st_printf("Notification count periodic: %u\n", timer_ticks); + if (timer_ticks < 8 || timer_ticks > 12) { efi_st_error("Incorrect timing of events\n"); - return 1; + return EFI_ST_FAILURE; } ret = boottime->set_timer(event_notify, EFI_TIMER_STOP, 0); if (index != 0) { efi_st_error("Could not cancel timer\n"); - return 1; + return EFI_ST_FAILURE; } /* Set 10 ms timer */ - counter = 0; + timer_ticks = 0; ret = boottime->set_timer(event_notify, EFI_TIMER_RELATIVE, 100000); if (index != 0) { efi_st_error("Could not set timer\n"); - return 1; + return EFI_ST_FAILURE; } /* Set 100 ms timer */ ret = boottime->set_timer(event_wait, EFI_TIMER_PERIODIC, 1000000); if (index != 0) { efi_st_error("Could not set timer\n"); - return 1; + return EFI_ST_FAILURE; } ret = boottime->wait_for_event(1, &event_wait, &index); if (ret != EFI_SUCCESS) { efi_st_error("Could not wait for event\n"); - return 1; + return EFI_ST_FAILURE; } - efi_st_printf("Counter single shot: %u\n", counter); - if (counter != 1) { + efi_st_printf("Notification count single shot: %u\n", timer_ticks); + if (timer_ticks != 1) { efi_st_error("Single shot timer failed\n"); - return 1; + return EFI_ST_FAILURE; } ret = boottime->wait_for_event(1, &event_wait, &index); if (ret != EFI_SUCCESS) { efi_st_error("Could not wait for event\n"); - return 1; + return EFI_ST_FAILURE; } - efi_st_printf("Stopped counter: %u\n", counter); - if (counter != 1) { + efi_st_printf("Notification count stopped timer: %u\n", timer_ticks); + if (timer_ticks != 1) { efi_st_error("Stopped timer fired\n"); - return 1; + return EFI_ST_FAILURE; } ret = boottime->set_timer(event_wait, EFI_TIMER_STOP, 0); - if (index != 0) { + if (ret != EFI_SUCCESS) { efi_st_error("Could not cancel timer\n"); - return 1; + return EFI_ST_FAILURE; } - return 0; + return EFI_ST_SUCCESS; } EFI_UNIT_TEST(events) = { diff --git a/lib/efi_selftest/efi_selftest_exitbootservices.c b/lib/efi_selftest/efi_selftest_exitbootservices.c index 60271e6180..cddd11d52b 100644 --- a/lib/efi_selftest/efi_selftest_exitbootservices.c +++ b/lib/efi_selftest/efi_selftest_exitbootservices.c @@ -1,5 +1,5 @@ /* - * efi_selftest_events + * efi_selftest_exitbootservices * * Copyright (c) 2017 Heinrich Schuchardt <xypron.glpk@gmx.de> * @@ -13,19 +13,19 @@ static struct efi_boot_services *boottime; static struct efi_event *event_notify; -static unsigned int counter; +static unsigned int notification_count; /* - * Notification function, increments a counter. + * Notification function, increments the notification count. * * @event notified event - * @context pointer to the counter + * @context pointer to the notification count */ static void EFIAPI notify(struct efi_event *event, void *context) { - if (!context) - return; - ++*(unsigned int *)context; + unsigned int *count = context; + + ++*count; } /* @@ -35,6 +35,7 @@ static void EFIAPI notify(struct efi_event *event, void *context) * * @handle: handle of the loaded image * @systable: system table + * @return: EFI_ST_SUCCESS for success */ static int setup(const efi_handle_t handle, const struct efi_system_table *systable) @@ -43,21 +44,24 @@ static int setup(const efi_handle_t handle, boottime = systable->boottime; - counter = 0; + notification_count = 0; ret = boottime->create_event(EVT_SIGNAL_EXIT_BOOT_SERVICES, - TPL_CALLBACK, notify, (void *)&counter, + TPL_CALLBACK, notify, + (void *)¬ification_count, &event_notify); if (ret != EFI_SUCCESS) { efi_st_error("could not create event\n"); - return 1; + return EFI_ST_FAILURE; } - return 0; + return EFI_ST_SUCCESS; } /* * Tear down unit test. * * Close the event created in setup. + * + * @return: EFI_ST_SUCCESS for success */ static int teardown(void) { @@ -68,10 +72,10 @@ static int teardown(void) event_notify = NULL; if (ret != EFI_SUCCESS) { efi_st_error("could not close event\n"); - return 1; + return EFI_ST_FAILURE; } } - return 0; + return EFI_ST_SUCCESS; } /* @@ -82,19 +86,21 @@ static int teardown(void) * * Call ExitBootServices again and check that the notification function is * not called again. + * + * @return: EFI_ST_SUCCESS for success */ static int execute(void) { - if (counter != 1) { - efi_st_error("ExitBootServices was not notified"); - return 1; + if (notification_count != 1) { + efi_st_error("ExitBootServices was not notified\n"); + return EFI_ST_FAILURE; } efi_st_exit_boot_services(); - if (counter != 1) { - efi_st_error("ExitBootServices was notified twice"); - return 1; + if (notification_count != 1) { + efi_st_error("ExitBootServices was notified twice\n"); + return EFI_ST_FAILURE; } - return 0; + return EFI_ST_SUCCESS; } EFI_UNIT_TEST(exitbootservices) = { diff --git a/lib/efi_selftest/efi_selftest_snp.c b/lib/efi_selftest/efi_selftest_snp.c new file mode 100644 index 0000000000..bdd6ce20da --- /dev/null +++ b/lib/efi_selftest/efi_selftest_snp.c @@ -0,0 +1,431 @@ +/* + * efi_selftest_snp + * + * Copyright (c) 2017 Heinrich Schuchardt <xypron.glpk@gmx.de> + * + * SPDX-License-Identifier: GPL-2.0+ + * + * This unit test covers the Simple Network Protocol as well as + * the CopyMem and SetMem boottime services. + * + * A DHCP discover message is sent. The test is successful if a + * DHCP reply is received. + * + * TODO: Once ConnectController and DisconnectController are implemented + * we should connect our code as controller. + */ + +#include <efi_selftest.h> + +/* + * MAC address for broadcasts + */ +static const u8 BROADCAST_MAC[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; + +struct dhcp_hdr { + u8 op; +#define BOOTREQUEST 1 +#define BOOTREPLY 2 + u8 htype; +# define HWT_ETHER 1 + u8 hlen; +# define HWL_ETHER 6 + u8 hops; + u32 xid; + u16 secs; + u16 flags; +#define DHCP_FLAGS_UNICAST 0x0000 +#define DHCP_FLAGS_BROADCAST 0x0080 + u32 ciaddr; + u32 yiaddr; + u32 siaddr; + u32 giaddr; + u8 chaddr[16]; + u8 sname[64]; + u8 file[128]; +}; + +/* + * Message type option. + */ +#define DHCP_MESSAGE_TYPE 0x35 +#define DHCPDISCOVER 1 +#define DHCPOFFER 2 +#define DHCPREQUEST 3 +#define DHCPDECLINE 4 +#define DHCPACK 5 +#define DHCPNAK 6 +#define DHCPRELEASE 7 + +struct dhcp { + struct ethernet_hdr eth_hdr; + struct ip_udp_hdr ip_udp; + struct dhcp_hdr dhcp_hdr; + u8 opt[128]; +} __packed; + +static struct efi_boot_services *boottime; +static struct efi_simple_network *net; +static struct efi_event *timer; +static const efi_guid_t efi_net_guid = EFI_SIMPLE_NETWORK_GUID; +/* IP packet ID */ +static unsigned int net_ip_id; + +/* + * Compute the checksum of the IP header. We cover even values of length only. + * We cannot use net/checksum.c due to different CFLAGS values. + * + * @buf: IP header + * @len: length of header in bytes + * @return: checksum + */ +static unsigned int efi_ip_checksum(const void *buf, size_t len) +{ + size_t i; + u32 sum = 0; + const u16 *pos = buf; + + for (i = 0; i < len; i += 2) + sum += *pos++; + + sum = (sum >> 16) + (sum & 0xffff); + sum += sum >> 16; + sum = ~sum & 0xffff; + + return sum; +} + +/* + * Transmit a DHCPDISCOVER message. + */ +static efi_status_t send_dhcp_discover(void) +{ + efi_status_t ret; + struct dhcp p = {}; + + /* + * Fill ethernet header + */ + boottime->copy_mem(p.eth_hdr.et_dest, (void *)BROADCAST_MAC, ARP_HLEN); + boottime->copy_mem(p.eth_hdr.et_src, &net->mode->current_address, + ARP_HLEN); + p.eth_hdr.et_protlen = htons(PROT_IP); + /* + * Fill IP header + */ + p.ip_udp.ip_hl_v = 0x45; + p.ip_udp.ip_len = htons(sizeof(struct dhcp) - + sizeof(struct ethernet_hdr)); + p.ip_udp.ip_id = htons(++net_ip_id); + p.ip_udp.ip_off = htons(IP_FLAGS_DFRAG); + p.ip_udp.ip_ttl = 0xff; /* time to live */ + p.ip_udp.ip_p = IPPROTO_UDP; + boottime->set_mem(&p.ip_udp.ip_dst, 4, 0xff); + p.ip_udp.ip_sum = efi_ip_checksum(&p.ip_udp, IP_HDR_SIZE); + + /* + * Fill UDP header + */ + p.ip_udp.udp_src = htons(68); + p.ip_udp.udp_dst = htons(67); + p.ip_udp.udp_len = htons(sizeof(struct dhcp) - + sizeof(struct ethernet_hdr) - + sizeof(struct ip_hdr)); + /* + * Fill DHCP header + */ + p.dhcp_hdr.op = BOOTREQUEST; + p.dhcp_hdr.htype = HWT_ETHER; + p.dhcp_hdr.hlen = HWL_ETHER; + p.dhcp_hdr.flags = htons(DHCP_FLAGS_UNICAST); + boottime->copy_mem(&p.dhcp_hdr.chaddr, + &net->mode->current_address, ARP_HLEN); + /* + * Fill options + */ + p.opt[0] = 0x63; /* DHCP magic cookie */ + p.opt[1] = 0x82; + p.opt[2] = 0x53; + p.opt[3] = 0x63; + p.opt[4] = DHCP_MESSAGE_TYPE; + p.opt[5] = 0x01; /* length */ + p.opt[6] = DHCPDISCOVER; + p.opt[7] = 0x39; /* maximum message size */ + p.opt[8] = 0x02; /* length */ + p.opt[9] = 0x02; /* 576 bytes */ + p.opt[10] = 0x40; + p.opt[11] = 0xff; /* end of options */ + + /* + * Transmit DHCPDISCOVER message. + */ + ret = net->transmit(net, 0, sizeof(struct dhcp), &p, NULL, NULL, 0); + if (ret != EFI_SUCCESS) + efi_st_error("Sending a DHCP request failed\n"); + else + efi_st_printf("DHCP Discover\n"); + return ret; +} + +/* + * Setup unit test. + * + * Create a 1 s periodic timer. + * Start the network driver. + * + * @handle: handle of the loaded image + * @systable: system table + * @return: EFI_ST_SUCCESS for success + */ +static int setup(const efi_handle_t handle, + const struct efi_system_table *systable) +{ + efi_status_t ret; + + boottime = systable->boottime; + + /* + * Create a timer event. + */ + ret = boottime->create_event(EVT_TIMER, TPL_CALLBACK, NULL, NULL, + &timer); + if (ret != EFI_SUCCESS) { + efi_st_error("Failed to create event\n"); + return EFI_ST_FAILURE; + } + /* + * Set timer period to 1s. + */ + ret = boottime->set_timer(timer, EFI_TIMER_PERIODIC, 10000000); + if (ret != EFI_SUCCESS) { + efi_st_error("Failed to set timer\n"); + return EFI_ST_FAILURE; + } + /* + * Find an interface implementing the SNP protocol. + */ + ret = boottime->locate_protocol(&efi_net_guid, NULL, (void **)&net); + if (ret != EFI_SUCCESS) { + net = NULL; + efi_st_error("Failed to locate simple network protocol\n"); + return EFI_ST_FAILURE; + } + /* + * Check hardware address size. + */ + if (!net->mode) { + efi_st_error("Mode not provided\n"); + return EFI_ST_FAILURE; + } + if (net->mode->hwaddr_size != ARP_HLEN) { + efi_st_error("HwAddressSize = %u, expected %u\n", + net->mode->hwaddr_size, ARP_HLEN); + return EFI_ST_FAILURE; + } + /* + * Check that WaitForPacket event exists. + */ + if (!net->wait_for_packet) { + efi_st_error("WaitForPacket event missing\n"); + return EFI_ST_FAILURE; + } + /* + * Initialize network adapter. + */ + ret = net->initialize(net, 0, 0); + if (ret != EFI_SUCCESS) { + efi_st_error("Failed to initialize network adapter\n"); + return EFI_ST_FAILURE; + } + /* + * Start network adapter. + */ + ret = net->start(net); + if (ret != EFI_SUCCESS) { + efi_st_error("Failed to start network adapter\n"); + return EFI_ST_FAILURE; + } + return EFI_ST_SUCCESS; +} + +/* + * Execute unit test. + * + * A DHCP discover message is sent. The test is successful if a + * DHCP reply is received within 10 seconds. + * + * @return: EFI_ST_SUCCESS for success + */ +static int execute(void) +{ + efi_status_t ret; + struct efi_event *events[2]; + size_t index; + union { + struct dhcp p; + u8 b[PKTSIZE]; + } buffer; + struct efi_mac_address srcaddr; + struct efi_mac_address destaddr; + size_t buffer_size; + u8 *addr; + /* + * The timeout is to occur after 10 s. + */ + unsigned int timeout = 10; + + /* Setup may have failed */ + if (!net || !timer) { + efi_st_error("Cannot execute test after setup failure\n"); + return EFI_ST_FAILURE; + } + + /* + * Send DHCP discover message + */ + ret = send_dhcp_discover(); + if (ret != EFI_SUCCESS) + return EFI_ST_FAILURE; + + /* + * If we would call WaitForEvent only with the WaitForPacket event, + * our code would block until a packet is received which might never + * occur. By calling WaitFor event with both a timer event and the + * WaitForPacket event we can escape this blocking situation. + * + * If the timer event occurs before we have received a DHCP reply + * a further DHCP discover message is sent. + */ + events[0] = timer; + events[1] = net->wait_for_packet; + for (;;) { + /* + * Wait for packet to be received or timer event. + */ + boottime->wait_for_event(2, events, &index); + if (index == 0) { + /* + * The timer event occurred. Check for timeout. + */ + --timeout; + if (!timeout) { + efi_st_error("Timeout occurred\n"); + return EFI_ST_FAILURE; + } + /* + * Send further DHCP discover message + */ + ret = send_dhcp_discover(); + if (ret != EFI_SUCCESS) + return EFI_ST_FAILURE; + continue; + } + /* + * Receive packet + */ + buffer_size = sizeof(buffer); + net->receive(net, NULL, &buffer_size, &buffer, + &srcaddr, &destaddr, NULL); + if (ret != EFI_SUCCESS) { + efi_st_error("Failed to receive packet"); + return EFI_ST_FAILURE; + } + /* + * Check the packet is meant for this system. + * Unfortunately QEMU ignores the broadcast flag. + * So we have to check for broadcasts too. + */ + if (efi_st_memcmp(&destaddr, &net->mode->current_address, + ARP_HLEN) && + efi_st_memcmp(&destaddr, BROADCAST_MAC, ARP_HLEN)) + continue; + /* + * Check this is a DHCP reply + */ + if (buffer.p.eth_hdr.et_protlen != ntohs(PROT_IP) || + buffer.p.ip_udp.ip_hl_v != 0x45 || + buffer.p.ip_udp.ip_p != IPPROTO_UDP || + buffer.p.ip_udp.udp_src != ntohs(67) || + buffer.p.ip_udp.udp_dst != ntohs(68) || + buffer.p.dhcp_hdr.op != BOOTREPLY) + continue; + /* + * We successfully received a DHCP reply. + */ + break; + } + + /* + * Write a log message. + */ + addr = (u8 *)&buffer.p.ip_udp.ip_src; + efi_st_printf("DHCP reply received from %u.%u.%u.%u (%pm) ", + addr[0], addr[1], addr[2], addr[3], &srcaddr); + if (!efi_st_memcmp(&destaddr, BROADCAST_MAC, ARP_HLEN)) + efi_st_printf("as broadcast message.\n"); + else + efi_st_printf("as unicast message.\n"); + + return EFI_ST_SUCCESS; +} + +/* + * Tear down unit test. + * + * Close the timer event created in setup. + * Shut down the network adapter. + * + * @return: EFI_ST_SUCCESS for success + */ +static int teardown(void) +{ + efi_status_t ret; + int exit_status = EFI_ST_SUCCESS; + + if (timer) { + /* + * Stop timer. + */ + ret = boottime->set_timer(timer, EFI_TIMER_STOP, 0); + if (ret != EFI_SUCCESS) { + efi_st_error("Failed to stop timer"); + exit_status = EFI_ST_FAILURE; + } + /* + * Close timer event. + */ + ret = boottime->close_event(timer); + if (ret != EFI_SUCCESS) { + efi_st_error("Failed to close event"); + exit_status = EFI_ST_FAILURE; + } + } + if (net) { + /* + * Stop network adapter. + */ + ret = net->stop(net); + if (ret != EFI_SUCCESS) { + efi_st_error("Failed to stop network adapter\n"); + exit_status = EFI_ST_FAILURE; + } + /* + * Shut down network adapter. + */ + ret = net->shutdown(net); + if (ret != EFI_SUCCESS) { + efi_st_error("Failed to shut down network adapter\n"); + exit_status = EFI_ST_FAILURE; + } + } + + return exit_status; +} + +EFI_UNIT_TEST(snp) = { + .name = "simple network protocol", + .phase = EFI_EXECUTE_BEFORE_BOOTTIME_EXIT, + .setup = setup, + .execute = execute, + .teardown = teardown, +}; diff --git a/lib/efi_selftest/efi_selftest_tpl.c b/lib/efi_selftest/efi_selftest_tpl.c index 90ace0f51e..ddb67ed268 100644 --- a/lib/efi_selftest/efi_selftest_tpl.c +++ b/lib/efi_selftest/efi_selftest_tpl.c @@ -13,20 +13,21 @@ static struct efi_event *event_notify; static struct efi_event *event_wait; -static unsigned int counter; +static unsigned int notification_count; static struct efi_boot_services *boottime; /* - * Notification function, increments a counter. + * Notification function, increments the notification count. * * @event notified event - * @context pointer to the counter + * @context pointer to the notification count */ static void EFIAPI notify(struct efi_event *event, void *context) { - if (!context) - return; - ++*(unsigned int *)context; + unsigned int *count = context; + + if (count) + ++*count; } /* @@ -37,6 +38,7 @@ static void EFIAPI notify(struct efi_event *event, void *context) * * @handle: handle of the loaded image * @systable: system table + * @return: EFI_ST_SUCCESS for success */ static int setup(const efi_handle_t handle, const struct efi_system_table *systable) @@ -46,25 +48,28 @@ static int setup(const efi_handle_t handle, boottime = systable->boottime; ret = boottime->create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, - TPL_CALLBACK, notify, (void *)&counter, + TPL_CALLBACK, notify, + (void *)¬ification_count, &event_notify); if (ret != EFI_SUCCESS) { efi_st_error("could not create event\n"); - return 1; + return EFI_ST_FAILURE; } ret = boottime->create_event(EVT_TIMER | EVT_NOTIFY_WAIT, TPL_HIGH_LEVEL, notify, NULL, &event_wait); if (ret != EFI_SUCCESS) { efi_st_error("could not create event\n"); - return 1; + return EFI_ST_FAILURE; } - return 0; + return EFI_ST_SUCCESS; } /* * Tear down unit test. * * Close the events created in setup. + * + * @return: EFI_ST_SUCCESS for success */ static int teardown(void) { @@ -75,7 +80,7 @@ static int teardown(void) event_notify = NULL; if (ret != EFI_SUCCESS) { efi_st_error("could not close event\n"); - return 1; + return EFI_ST_FAILURE; } } if (event_wait) { @@ -83,11 +88,11 @@ static int teardown(void) event_wait = NULL; if (ret != EFI_SUCCESS) { efi_st_error("could not close event\n"); - return 1; + return EFI_ST_FAILURE; } } boottime->restore_tpl(TPL_APPLICATION); - return 0; + return EFI_ST_SUCCESS; } /* @@ -101,108 +106,113 @@ static int teardown(void) * * Lower the TPL level and check that the queued notification * function is called. + * + * @return: EFI_ST_SUCCESS for success */ static int execute(void) { - unsigned long index; + size_t index; efi_status_t ret; UINTN old_tpl; /* Set 10 ms timer */ - counter = 0; + notification_count = 0; ret = boottime->set_timer(event_notify, EFI_TIMER_PERIODIC, 100000); if (ret != EFI_SUCCESS) { efi_st_error("Could not set timer\n"); - return 1; + return EFI_ST_FAILURE; } /* Set 100 ms timer */ ret = boottime->set_timer(event_wait, EFI_TIMER_RELATIVE, 1000000); if (ret != EFI_SUCCESS) { efi_st_error("Could not set timer\n"); - return 1; + return EFI_ST_FAILURE; } index = 5; ret = boottime->wait_for_event(1, &event_wait, &index); if (ret != EFI_SUCCESS) { efi_st_error("Could not wait for event\n"); - return 1; + return EFI_ST_FAILURE; } ret = boottime->check_event(event_wait); if (ret != EFI_NOT_READY) { efi_st_error("Signaled state was not cleared.\n"); efi_st_printf("ret = %u\n", (unsigned int)ret); - return 1; + return EFI_ST_FAILURE; } if (index != 0) { efi_st_error("WaitForEvent returned wrong index\n"); - return 1; + return EFI_ST_FAILURE; } - efi_st_printf("Counter with TPL level TPL_APPLICATION: %u\n", counter); - if (counter < 8 || counter > 12) { + efi_st_printf("Notification count with TPL level TPL_APPLICATION: %u\n", + notification_count); + if (notification_count < 8 || notification_count > 12) { efi_st_error("Incorrect timing of events\n"); - return 1; + return EFI_ST_FAILURE; } ret = boottime->set_timer(event_notify, EFI_TIMER_STOP, 0); if (index != 0) { efi_st_error("Could not cancel timer\n"); - return 1; + return EFI_ST_FAILURE; } /* Raise TPL level */ old_tpl = boottime->raise_tpl(TPL_CALLBACK); if (old_tpl != TPL_APPLICATION) { efi_st_error("Initial TPL level was not TPL_APPLICATION"); - return 1; + return EFI_ST_FAILURE; } /* Set 10 ms timer */ - counter = 0; + notification_count = 0; ret = boottime->set_timer(event_notify, EFI_TIMER_PERIODIC, 100000); if (index != 0) { efi_st_error("Could not set timer\n"); - return 1; + return EFI_ST_FAILURE; } /* Set 100 ms timer */ ret = boottime->set_timer(event_wait, EFI_TIMER_RELATIVE, 1000000); if (ret != EFI_SUCCESS) { efi_st_error("Could not set timer\n"); - return 1; + return EFI_ST_FAILURE; } do { ret = boottime->check_event(event_wait); } while (ret == EFI_NOT_READY); if (ret != EFI_SUCCESS) { efi_st_error("Could not check event\n"); - return 1; + return EFI_ST_FAILURE; } - efi_st_printf("Counter with TPL level TPL_CALLBACK: %u\n", counter); - if (counter != 0) { + efi_st_printf("Notification count with TPL level TPL_CALLBACK: %u\n", + notification_count); + if (notification_count != 0) { efi_st_error("Suppressed timer fired\n"); - return 1; + return EFI_ST_FAILURE; } /* Set 1 ms timer */ ret = boottime->set_timer(event_wait, EFI_TIMER_RELATIVE, 1000); if (ret != EFI_SUCCESS) { efi_st_error("Could not set timer\n"); - return 1; + return EFI_ST_FAILURE; } /* Restore the old TPL level */ boottime->restore_tpl(TPL_APPLICATION); ret = boottime->wait_for_event(1, &event_wait, &index); if (ret != EFI_SUCCESS) { efi_st_error("Could not wait for event\n"); - return 1; + return EFI_ST_FAILURE; } - efi_st_printf("Counter with TPL level TPL_APPLICATION: %u\n", counter); - if (counter < 1) { + efi_st_printf("Notification count with TPL level TPL_APPLICATION: %u\n", + notification_count); + if (notification_count < 1) { efi_st_error("Queued timer event did not fire\n"); - return 1; + return EFI_ST_FAILURE; } ret = boottime->set_timer(event_wait, EFI_TIMER_STOP, 0); - if (index != 0) { + if (ret != EFI_SUCCESS) { efi_st_error("Could not cancel timer\n"); - return 1; + return EFI_ST_FAILURE; } - return 0; + return EFI_ST_SUCCESS; } EFI_UNIT_TEST(tpl) = { diff --git a/lib/efi_selftest/efi_selftest_util.c b/lib/efi_selftest/efi_selftest_util.c new file mode 100644 index 0000000000..5cffe383d8 --- /dev/null +++ b/lib/efi_selftest/efi_selftest_util.c @@ -0,0 +1,25 @@ +/* + * efi_selftest_util + * + * Copyright (c) 2017 Heinrich Schuchardt <xypron.glpk@gmx.de> + * + * SPDX-License-Identifier: GPL-2.0+ + * + * Utility functions + */ + +#include <efi_selftest.h> + +int efi_st_memcmp(const void *buf1, const void *buf2, size_t length) +{ + const u8 *pos1 = buf1; + const u8 *pos2 = buf2; + + for (; length; --length) { + if (*pos1 != *pos2) + return *pos1 - *pos2; + ++pos1; + ++pos2; + } + return EFI_ST_SUCCESS; +} |