efi_selftest: allow to select a single test for execution
authorHeinrich Schuchardt <xypron.glpk@gmx.de>
Wed, 18 Oct 2017 16:13:13 +0000 (18:13 +0200)
committerAlexander Graf <agraf@suse.de>
Fri, 1 Dec 2017 12:22:55 +0000 (13:22 +0100)
Environment variable efi_selftest is passed as load options
to the selftest application. It is used to select a single
test to be executed.

The load options are an UTF8 string. Yet I decided to keep
the name propertiy of the tests as char[] to reduce code
size.

Special value 'list' displays a list of all available tests.

Tests get an on_request property. If this property is set
the tests are only executed if explicitly requested.

The invocation of efi_selftest is changed to reflect that
bootefi selftest with efi_selftest = 'list' will call the
Exit bootservice.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>
cmd/bootefi.c
include/efi_selftest.h
lib/efi_selftest/efi_selftest.c
lib/efi_selftest/efi_selftest_console.c
lib/efi_selftest/efi_selftest_util.c

index 4935173ce07784cc85e0f60cef766c324802c4c4..b894403ff6777132327bcb3230cece53c0093fc8 100644 (file)
@@ -6,10 +6,12 @@
  *  SPDX-License-Identifier:     GPL-2.0+
  */
 
+#include <charset.h>
 #include <common.h>
 #include <command.h>
 #include <dm.h>
 #include <efi_loader.h>
+#include <efi_selftest.h>
 #include <errno.h>
 #include <libfdt.h>
 #include <libfdt_env.h>
@@ -50,6 +52,32 @@ static void efi_init_obj_list(void)
        efi_get_time_init();
 }
 
+/*
+ * Set the load options of an image from an environment variable.
+ *
+ * @loaded_image_info: the image
+ * @env_var:           name of the environment variable
+ */
+static void set_load_options(struct efi_loaded_image *loaded_image_info,
+                            const char *env_var)
+{
+       size_t size;
+       const char *env = env_get(env_var);
+
+       loaded_image_info->load_options = NULL;
+       loaded_image_info->load_options_size = 0;
+       if (!env)
+               return;
+       size = strlen(env) + 1;
+       loaded_image_info->load_options = calloc(size, sizeof(u16));
+       if (!loaded_image_info->load_options) {
+               printf("ERROR: Out of memory\n");
+               return;
+       }
+       utf8_to_utf16(loaded_image_info->load_options, (u8 *)env, size);
+       loaded_image_info->load_options_size = size * 2;
+}
+
 static void *copy_fdt(void *fdt)
 {
        u64 fdt_size = fdt_totalsize(fdt);
@@ -317,7 +345,12 @@ static int do_bootefi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
                /* Initialize and populate EFI object list */
                if (!efi_obj_list_initalized)
                        efi_init_obj_list();
-               return efi_selftest(&loaded_image_info, &systab);
+               /* Transfer environment variable efi_selftest as load options */
+               set_load_options(&loaded_image_info, "efi_selftest");
+               /* Execute the test */
+               r = efi_selftest(&loaded_image_info, &systab);
+               free(loaded_image_info.load_options);
+               return r;
        } else
 #endif
        if (!strcmp(argv[1], "bootmgr")) {
@@ -363,6 +396,8 @@ static char bootefi_help_text[] =
 #ifdef CONFIG_CMD_BOOTEFI_SELFTEST
        "bootefi selftest\n"
        "  - boot an EFI selftest application stored within U-Boot\n"
+       "    Use environment variable efi_selftest to select a single test.\n"
+       "    Use 'setenv efi_selftest list' to enumerate all tests.\n"
 #endif
        "bootmgr [fdt addr]\n"
        "  - load and boot EFI payload based on BootOrder/BootXXXX variables.\n"
index 7ec42a0406bca52da1add4ddcc224c38ffa2b7c5..5cc8d4f6006f084cece25933a1512df42e91620e 100644 (file)
@@ -12,6 +12,7 @@
 #include <common.h>
 #include <efi.h>
 #include <efi_api.h>
+#include <efi_loader.h>
 #include <linker_lists.h>
 
 #define EFI_ST_SUCCESS 0
@@ -71,6 +72,15 @@ void efi_st_printf(const char *fmt, ...)
  */
 int efi_st_memcmp(const void *buf1, const void *buf2, size_t length);
 
+/*
+ * Compare an u16 string to a char string.
+ *
+ * @buf1:      u16 string
+ * @buf2:      char string
+ * @return:    0 if both buffers contain the same bytes
+ */
+int efi_st_strcmp_16_8(const u16 *buf1, const char *buf2);
+
 /*
  * Reads an Unicode character from the input device.
  *
@@ -88,6 +98,7 @@ u16 efi_st_get_key(void);
  * @setup:     set up the unit test
  * @teardown:  tear down the unit test
  * @execute:   execute the unit test
+ * @on_request:        test is only executed on request
  */
 struct efi_unit_test {
        const char *name;
@@ -96,6 +107,7 @@ struct efi_unit_test {
                     const struct efi_system_table *systable);
        int (*execute)(void);
        int (*teardown)(void);
+       bool on_request;
 };
 
 /* Declare a new EFI unit test */
index 73f074d9e15ce835a7c0c2878818ac2801dc8a72..b6bc4dd5c073067cca50be2070f0fdd9403eaf45 100644 (file)
@@ -140,20 +140,59 @@ static int teardown(struct efi_unit_test *test, unsigned int *failures)
        return ret;
 }
 
+/*
+ * Check that a test exists.
+ *
+ * @testname:  name of the test
+ * @return:    test
+ */
+static struct efi_unit_test *find_test(const u16 *testname)
+{
+       struct efi_unit_test *test;
+
+       for (test = ll_entry_start(struct efi_unit_test, efi_unit_test);
+            test < ll_entry_end(struct efi_unit_test, efi_unit_test); ++test) {
+               if (!efi_st_strcmp_16_8(testname, test->name))
+                       return test;
+       }
+       efi_st_printf("\nTest '%ps' not found\n", testname);
+       return NULL;
+}
+
+/*
+ * List all available tests.
+ */
+static void list_all_tests(void)
+{
+       struct efi_unit_test *test;
+
+       /* List all tests */
+       efi_st_printf("\nAvailable tests:\n");
+       for (test = ll_entry_start(struct efi_unit_test, efi_unit_test);
+            test < ll_entry_end(struct efi_unit_test, efi_unit_test); ++test) {
+               efi_st_printf("'%s'%s\n", test->name,
+                             test->on_request ? " - on request" : "");
+       }
+}
+
 /*
  * Execute test steps of one phase.
  *
+ * @testname   name of a single selected test or NULL
  * @phase      test phase
  * @steps      steps to execute
  * failures    returns EFI_ST_SUCCESS if all test steps succeeded
  */
-void efi_st_do_tests(unsigned int phase, unsigned int steps,
-                    unsigned int *failures)
+void efi_st_do_tests(const u16 *testname, unsigned int phase,
+                    unsigned int steps, unsigned int *failures)
 {
        struct efi_unit_test *test;
 
        for (test = ll_entry_start(struct efi_unit_test, efi_unit_test);
             test < ll_entry_end(struct efi_unit_test, efi_unit_test); ++test) {
+               if (testname ?
+                   efi_st_strcmp_16_8(testname, test->name) : test->on_request)
+                       continue;
                if (test->phase != phase)
                        continue;
                if (steps & EFI_ST_SETUP)
@@ -186,6 +225,9 @@ efi_status_t EFIAPI efi_selftest(efi_handle_t image_handle,
                                 struct efi_system_table *systab)
 {
        unsigned int failures = 0;
+       const u16 *testname = NULL;
+       struct efi_loaded_image *loaded_image;
+       efi_status_t ret;
 
        systable = systab;
        boottime = systable->boottime;
@@ -194,27 +236,57 @@ efi_status_t EFIAPI efi_selftest(efi_handle_t image_handle,
        con_out = systable->con_out;
        con_in = systable->con_in;
 
+       ret = boottime->handle_protocol(image_handle, &efi_guid_loaded_image,
+                                       (void **)&loaded_image);
+       if (ret != EFI_SUCCESS) {
+               efi_st_error("Cannot open loaded image protocol");
+               return ret;
+       }
+
+       if (loaded_image->load_options)
+               testname = (u16 *)loaded_image->load_options;
+
+       if (testname) {
+               if (!efi_st_strcmp_16_8(testname, "list") ||
+                   !find_test(testname)) {
+                       list_all_tests();
+                       /*
+                        * TODO:
+                        * Once the Exit boottime service is correctly
+                        * implemented we should call
+                        *   boottime->exit(image_handle, EFI_SUCCESS, 0, NULL);
+                        * here, cf.
+                        * https://lists.denx.de/pipermail/u-boot/2017-October/308720.html
+                        */
+                       return EFI_SUCCESS;
+               }
+       }
+
        efi_st_printf("\nTesting EFI API implementation\n");
 
-       efi_st_printf("\nNumber of tests to execute: %u\n",
-                     ll_entry_count(struct efi_unit_test, efi_unit_test));
+       if (testname)
+               efi_st_printf("\nSelected test: '%ps'\n", testname);
+       else
+               efi_st_printf("\nNumber of tests to execute: %u\n",
+                             ll_entry_count(struct efi_unit_test,
+                                            efi_unit_test));
 
        /* Execute boottime tests */
-       efi_st_do_tests(EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
+       efi_st_do_tests(testname, EFI_EXECUTE_BEFORE_BOOTTIME_EXIT,
                        EFI_ST_SETUP | EFI_ST_EXECUTE | EFI_ST_TEARDOWN,
                        &failures);
 
        /* Execute mixed tests */
-       efi_st_do_tests(EFI_SETUP_BEFORE_BOOTTIME_EXIT,
+       efi_st_do_tests(testname, EFI_SETUP_BEFORE_BOOTTIME_EXIT,
                        EFI_ST_SETUP, &failures);
 
        efi_st_exit_boot_services();
 
-       efi_st_do_tests(EFI_SETUP_BEFORE_BOOTTIME_EXIT,
+       efi_st_do_tests(testname, EFI_SETUP_BEFORE_BOOTTIME_EXIT,
                        EFI_ST_EXECUTE | EFI_ST_TEARDOWN, &failures);
 
        /* Execute runtime tests */
-       efi_st_do_tests(EFI_SETUP_AFTER_BOOTTIME_EXIT,
+       efi_st_do_tests(testname, EFI_SETUP_AFTER_BOOTTIME_EXIT,
                        EFI_ST_SETUP | EFI_ST_EXECUTE | EFI_ST_TEARDOWN,
                        &failures);
 
index 840e2290c60c05684c6afb898e4c66db42c374da..6a7fd20da5c51b008fd0794b07c96345c50225cc 100644 (file)
@@ -142,6 +142,7 @@ void efi_st_printf(const char *fmt, ...)
        const char *c;
        u16 *pos = buf;
        const char *s;
+       const u16 *u;
 
        va_start(args, fmt);
 
@@ -179,9 +180,18 @@ void efi_st_printf(const char *fmt, ...)
                        case 'p':
                                ++c;
                                switch (*c) {
+                               /* MAC address */
                                case 'm':
                                        mac(va_arg(args, void*), &pos);
                                        break;
+
+                               /* u16 string */
+                               case 's':
+                                       u = va_arg(args, u16*);
+                                       /* Ensure string fits into buffer */
+                                       for (; *u && pos < buf + 120; ++u)
+                                               *pos++ = *u;
+                                       break;
                                default:
                                        --c;
                                        pointer(va_arg(args, void*), &pos);
index 5f81f251c441fa30969c0172f8acc3006c29a753..1b17bf4d4bc4c290a539f41f275cd6832c6627a9 100644 (file)
@@ -23,3 +23,12 @@ int efi_st_memcmp(const void *buf1, const void *buf2, size_t length)
        }
        return 0;
 }
+
+int efi_st_strcmp_16_8(const u16 *buf1, const char *buf2)
+{
+       for (; *buf1 || *buf2; ++buf1, ++buf2) {
+               if (*buf1 != *buf2)
+                       return *buf1 - *buf2;
+       }
+       return 0;
+}