diff options
Diffstat (limited to 'test')
35 files changed, 1284 insertions, 363 deletions
diff --git a/test/Kconfig b/test/Kconfig index 6e859fb..830245b 100644 --- a/test/Kconfig +++ b/test/Kconfig @@ -1,9 +1,4 @@ -config POST - bool "Power On Self Test support" - help - See doc/README.POST for more details - -menu "Unit tests" +menu "Testing" config UNIT_TEST bool "Unit tests" @@ -110,4 +105,9 @@ source "test/lib/Kconfig" source "test/optee/Kconfig" source "test/overlay/Kconfig" +config POST + bool "Power On Self Test support" + help + See doc/README.POST for more details + endmenu diff --git a/test/boot/Makefile b/test/boot/Makefile index 22ed61c..5294758 100644 --- a/test/boot/Makefile +++ b/test/boot/Makefile @@ -6,6 +6,7 @@ obj-$(CONFIG_BOOTSTD) += bootdev.o bootstd_common.o bootflow.o bootmeth.o obj-$(CONFIG_FIT) += image.o obj-$(CONFIG_EXPO) += expo.o +obj-$(CONFIG_CEDIT) += cedit.o ifdef CONFIG_OF_LIVE obj-$(CONFIG_BOOTMETH_VBE_SIMPLE) += vbe_simple.o diff --git a/test/boot/bootflow.c b/test/boot/bootflow.c index 1c91a94..f5b2059 100644 --- a/test/boot/bootflow.c +++ b/test/boot/bootflow.c @@ -27,6 +27,7 @@ DECLARE_GLOBAL_DATA_PTR; +extern U_BOOT_DRIVER(bootmeth_cros); extern U_BOOT_DRIVER(bootmeth_2script); static int inject_response(struct unit_test_state *uts) @@ -167,21 +168,22 @@ static int bootflow_cmd_scan_e(struct unit_test_state *uts) ut_assert_nextline("Seq Method State Uclass Part Name Filename"); ut_assert_nextlinen("---"); ut_assert_nextline("Scanning bootdev 'mmc2.bootdev':"); - ut_assert_nextline(" 0 extlinux media mmc 0 mmc2.bootdev.whole <NULL>"); + ut_assert_nextline(" 0 extlinux media mmc 0 mmc2.bootdev.whole "); ut_assert_nextline(" ** No partition found, err=-93: Protocol not supported"); - ut_assert_nextline(" 1 efi media mmc 0 mmc2.bootdev.whole <NULL>"); + ut_assert_nextline(" 1 efi media mmc 0 mmc2.bootdev.whole "); ut_assert_nextline(" ** No partition found, err=-93: Protocol not supported"); ut_assert_nextline("Scanning bootdev 'mmc1.bootdev':"); - ut_assert_nextline(" 2 extlinux media mmc 0 mmc1.bootdev.whole <NULL>"); + ut_assert_nextline(" 2 extlinux media mmc 0 mmc1.bootdev.whole "); ut_assert_nextline(" ** No partition found, err=-2: No such file or directory"); - ut_assert_nextline(" 3 efi media mmc 0 mmc1.bootdev.whole <NULL>"); + ut_assert_nextline(" 3 efi media mmc 0 mmc1.bootdev.whole "); ut_assert_nextline(" ** No partition found, err=-2: No such file or directory"); ut_assert_nextline(" 4 extlinux ready mmc 1 mmc1.bootdev.part_1 /extlinux/extlinux.conf"); ut_assert_nextline(" 5 efi fs mmc 1 mmc1.bootdev.part_1 efi/boot/bootsbox.efi"); ut_assert_skip_to_line("Scanning bootdev 'mmc0.bootdev':"); - ut_assert_skip_to_line(" 3f efi media mmc 0 mmc0.bootdev.whole <NULL>"); + ut_assert_skip_to_line( + " 3f efi media mmc 0 mmc0.bootdev.whole "); ut_assert_nextline(" ** No partition found, err=-93: Protocol not supported"); ut_assert_nextline("No more bootdevs"); ut_assert_nextlinen("---"); @@ -192,10 +194,11 @@ static int bootflow_cmd_scan_e(struct unit_test_state *uts) ut_assert_nextline("Showing all bootflows"); ut_assert_nextline("Seq Method State Uclass Part Name Filename"); ut_assert_nextlinen("---"); - ut_assert_nextline(" 0 extlinux media mmc 0 mmc2.bootdev.whole <NULL>"); - ut_assert_nextline(" 1 efi media mmc 0 mmc2.bootdev.whole <NULL>"); - ut_assert_skip_to_line(" 4 extlinux ready mmc 1 mmc1.bootdev.part_1 /extlinux/extlinux.conf"); - ut_assert_skip_to_line(" 3f efi media mmc 0 mmc0.bootdev.whole <NULL>"); + ut_assert_nextline(" 0 extlinux media mmc 0 mmc2.bootdev.whole "); + ut_assert_nextline(" 1 efi media mmc 0 mmc2.bootdev.whole "); + ut_assert_skip_to_line( + " 4 extlinux ready mmc 1 mmc1.bootdev.part_1 /extlinux/extlinux.conf"); + ut_assert_skip_to_line(" 3f efi media mmc 0 mmc0.bootdev.whole "); ut_assert_nextlinen("---"); ut_assert_nextline("(64 bootflows, 1 valid)"); ut_assert_console_end(); @@ -384,7 +387,7 @@ static int bootflow_system(struct unit_test_state *uts) console_record_reset_enable(); ut_assertok(run_command("bootflow scan -lH", 0)); ut_assert_skip_to_line( - " 0 efi_mgr ready (none) 0 <NULL> <NULL>"); + " 0 efi_mgr ready (none) 0 <NULL> "); ut_assert_skip_to_line("No more bootdevs"); ut_assert_skip_to_line("(2 bootflows, 2 valid)"); ut_assert_console_end(); @@ -506,21 +509,25 @@ static int bootflow_cmd_boot(struct unit_test_state *uts) BOOTSTD_TEST(bootflow_cmd_boot, UT_TESTF_DM | UT_TESTF_SCAN_FDT); /** - * prep_mmc4_bootdev() - Set up the mmc4 bootdev so we can access a fake Armbian + * prep_mmc_bootdev() - Set up an mmc bootdev so we can access other distros * * @uts: Unit test state + * @mmc_dev: MMC device to use, e.g. "mmc4" * Returns 0 on success, -ve on failure */ -static int prep_mmc4_bootdev(struct unit_test_state *uts) +static int prep_mmc_bootdev(struct unit_test_state *uts, const char *mmc_dev, + bool bind_cros) { - static const char *order[] = {"mmc2", "mmc1", "mmc4", NULL}; + const char *order[] = {"mmc2", "mmc1", mmc_dev, NULL}; struct udevice *dev, *bootstd; struct bootstd_priv *std; const char **old_order; - ofnode node; + ofnode root, node; /* Enable the mmc4 node since we need a second bootflow */ - node = ofnode_path("/mmc4"); + root = oftree_root(oftree_default()); + node = ofnode_find_subnode(root, mmc_dev); + ut_assert(ofnode_valid(node)); ut_assertok(lists_bind_fdt(gd->dm_root, node, &dev, NULL, false)); /* Enable the script bootmeth too */ @@ -528,7 +535,14 @@ static int prep_mmc4_bootdev(struct unit_test_state *uts) ut_assertok(device_bind(bootstd, DM_DRIVER_REF(bootmeth_2script), "bootmeth_script", 0, ofnode_null(), &dev)); - /* Change the order to include mmc4 */ + /* Enable the cros bootmeth if needed */ + if (bind_cros) { + ut_assertok(uclass_first_device_err(UCLASS_BOOTSTD, &bootstd)); + ut_assertok(device_bind(bootstd, DM_DRIVER_REF(bootmeth_cros), + "cros", 0, ofnode_null(), &dev)); + } + + /* Change the order to include the device */ std = dev_get_priv(bootstd); old_order = std->bootdev_order; std->bootdev_order = order; @@ -543,6 +557,19 @@ static int prep_mmc4_bootdev(struct unit_test_state *uts) return 0; } +/** + * prep_mmc4_bootdev() - Set up the mmc4 bootdev so we can access a fake Armbian + * + * @uts: Unit test state + * Returns 0 on success, -ve on failure + */ +static int prep_mmc4_bootdev(struct unit_test_state *uts) +{ + ut_assertok(prep_mmc_bootdev(uts, "mmc4", false)); + + return 0; +} + /* Check 'bootflow menu' to select a bootflow */ static int bootflow_cmd_menu(struct unit_test_state *uts) { @@ -945,3 +972,24 @@ static int bootflow_cmdline(struct unit_test_state *uts) return 0; } BOOTSTD_TEST(bootflow_cmdline, 0); + +/* Test ChromiumOS bootmeth */ +static int bootflow_cros(struct unit_test_state *uts) +{ + ut_assertok(prep_mmc_bootdev(uts, "mmc5", true)); + ut_assertok(run_command("bootflow list", 0)); + + ut_assert_nextlinen("Showing all"); + ut_assert_nextlinen("Seq"); + ut_assert_nextlinen("---"); + ut_assert_nextlinen(" 0 extlinux"); + ut_assert_nextlinen(" 1 cros ready mmc 2 mmc5.bootdev.part_2 "); + ut_assert_nextlinen(" 2 cros ready mmc 4 mmc5.bootdev.part_4 "); + ut_assert_nextlinen("---"); + ut_assert_skip_to_line("(3 bootflows, 3 valid)"); + + ut_assert_console_end(); + + return 0; +} +BOOTSTD_TEST(bootflow_cros, 0); diff --git a/test/boot/cedit.c b/test/boot/cedit.c new file mode 100644 index 0000000..ab2b8a1 --- /dev/null +++ b/test/boot/cedit.c @@ -0,0 +1,198 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Copyright 2023 Google LLC + * Written by Simon Glass <sjg@chromium.org> + */ + +#include <common.h> +#include <cedit.h> +#include <env.h> +#include <expo.h> +#include <mapmem.h> +#include <dm/ofnode.h> +#include <test/ut.h> +#include "bootstd_common.h" +#include <test/cedit-test.h> +#include "../../boot/scene_internal.h" + +/* Check the cedit command */ +static int cedit_base(struct unit_test_state *uts) +{ + extern struct expo *cur_exp; + struct scene_obj_menu *menu; + struct scene_obj_txt *txt; + struct expo *exp; + struct scene *scn; + + ut_assertok(run_command("cedit load hostfs - cedit.dtb", 0)); + + console_record_reset_enable(); + + /* + * ^N Move down to second menu + * ^M Open menu + * ^N Move down to second item + * ^M Select item + * \e Quit + */ + console_in_puts("\x0e\x0d\x0e\x0d\e"); + ut_assertok(run_command("cedit run", 0)); + + exp = cur_exp; + scn = expo_lookup_scene_id(exp, exp->scene_id); + ut_assertnonnull(scn); + + menu = scene_obj_find(scn, scn->highlight_id, SCENEOBJT_NONE); + ut_assertnonnull(menu); + + txt = scene_obj_find(scn, menu->title_id, SCENEOBJT_NONE); + ut_assertnonnull(txt); + ut_asserteq_str("AC Power", expo_get_str(exp, txt->str_id)); + + ut_asserteq(ID_AC_ON, menu->cur_item_id); + + return 0; +} +BOOTSTD_TEST(cedit_base, 0); + +/* Check the cedit write_fdt and read_fdt commands */ +static int cedit_fdt(struct unit_test_state *uts) +{ + struct video_priv *vid_priv; + extern struct expo *cur_exp; + struct scene_obj_menu *menu; + ulong addr = 0x1000; + struct ofprop prop; + struct scene *scn; + oftree tree; + ofnode node; + void *fdt; + int i; + + console_record_reset_enable(); + ut_assertok(run_command("cedit load hostfs - cedit.dtb", 0)); + + ut_asserteq(ID_SCENE1, cedit_prepare(cur_exp, &vid_priv, &scn)); + + /* get a menu to fiddle with */ + menu = scene_obj_find(scn, ID_CPU_SPEED, SCENEOBJT_MENU); + ut_assertnonnull(menu); + menu->cur_item_id = ID_CPU_SPEED_2; + + ut_assertok(run_command("cedit write_fdt hostfs - settings.dtb", 0)); + ut_assertok(run_commandf("load hostfs - %lx settings.dtb", addr)); + ut_assert_nextlinen("1024 bytes read"); + + fdt = map_sysmem(addr, 1024); + tree = oftree_from_fdt(fdt); + node = ofnode_find_subnode(oftree_root(tree), CEDIT_NODE_NAME); + + ut_asserteq(ID_CPU_SPEED_2, + ofnode_read_u32_default(node, "cpu-speed", 0)); + ut_asserteq_str("2.5 GHz", ofnode_read_string(node, "cpu-speed-str")); + ut_assert(ofnode_valid(node)); + + /* There should only be 4 properties */ + for (i = 0, ofnode_first_property(node, &prop); ofprop_valid(&prop); + i++, ofnode_next_property(&prop)) + ; + ut_asserteq(4, i); + + ut_assert_console_end(); + + /* reset the expo */ + menu->cur_item_id = ID_CPU_SPEED_1; + + /* load in the settings and make sure they update */ + ut_assertok(run_command("cedit read_fdt hostfs - settings.dtb", 0)); + ut_asserteq(ID_CPU_SPEED_2, menu->cur_item_id); + + ut_assertnonnull(menu); + ut_assert_console_end(); + + return 0; +} +BOOTSTD_TEST(cedit_fdt, 0); + +/* Check the cedit write_env and read_env commands */ +static int cedit_env(struct unit_test_state *uts) +{ + struct video_priv *vid_priv; + extern struct expo *cur_exp; + struct scene_obj_menu *menu; + struct scene *scn; + + console_record_reset_enable(); + ut_assertok(run_command("cedit load hostfs - cedit.dtb", 0)); + + ut_asserteq(ID_SCENE1, cedit_prepare(cur_exp, &vid_priv, &scn)); + + /* get a menu to fiddle with */ + menu = scene_obj_find(scn, ID_CPU_SPEED, SCENEOBJT_MENU); + ut_assertnonnull(menu); + menu->cur_item_id = ID_CPU_SPEED_2; + + ut_assertok(run_command("cedit write_env -v", 0)); + ut_assert_nextlinen("c.cpu-speed=7"); + ut_assert_nextlinen("c.cpu-speed-str=2.5 GHz"); + ut_assert_nextlinen("c.power-loss=10"); + ut_assert_nextlinen("c.power-loss-str=Always Off"); + ut_assert_console_end(); + + ut_asserteq(7, env_get_ulong("c.cpu-speed", 10, 0)); + ut_asserteq_str("2.5 GHz", env_get("c.cpu-speed-str")); + + /* reset the expo */ + menu->cur_item_id = ID_CPU_SPEED_1; + + ut_assertok(run_command("cedit read_env -v", 0)); + ut_assert_nextlinen("c.cpu-speed=7"); + ut_assert_nextlinen("c.power-loss=10"); + ut_assert_console_end(); + + ut_asserteq(ID_CPU_SPEED_2, menu->cur_item_id); + + return 0; +} +BOOTSTD_TEST(cedit_env, 0); + +/* Check the cedit write_cmos and read_cmos commands */ +static int cedit_cmos(struct unit_test_state *uts) +{ + struct scene_obj_menu *menu, *menu2; + struct video_priv *vid_priv; + extern struct expo *cur_exp; + struct scene *scn; + + console_record_reset_enable(); + ut_assertok(run_command("cedit load hostfs - cedit.dtb", 0)); + + ut_asserteq(ID_SCENE1, cedit_prepare(cur_exp, &vid_priv, &scn)); + + /* get the menus to fiddle with */ + menu = scene_obj_find(scn, ID_CPU_SPEED, SCENEOBJT_MENU); + ut_assertnonnull(menu); + menu->cur_item_id = ID_CPU_SPEED_2; + + menu2 = scene_obj_find(scn, ID_POWER_LOSS, SCENEOBJT_MENU); + ut_assertnonnull(menu2); + menu2->cur_item_id = ID_AC_MEMORY; + + ut_assertok(run_command("cedit write_cmos -v", 0)); + ut_assert_nextlinen("Write 2 bytes from offset 80 to 84"); + ut_assert_console_end(); + + /* reset the expo */ + menu->cur_item_id = ID_CPU_SPEED_1; + menu2->cur_item_id = ID_AC_OFF; + + ut_assertok(run_command("cedit read_cmos -v", 0)); + ut_assert_nextlinen("Read 2 bytes from offset 80 to 84"); + ut_assert_console_end(); + + ut_asserteq(ID_CPU_SPEED_2, menu->cur_item_id); + ut_asserteq(ID_AC_MEMORY, menu2->cur_item_id); + + return 0; +} +BOOTSTD_TEST(cedit_cmos, 0); diff --git a/test/boot/expo.c b/test/boot/expo.c index 3898f85..9002740 100644 --- a/test/boot/expo.c +++ b/test/boot/expo.c @@ -289,6 +289,33 @@ static int expo_object_attr(struct unit_test_state *uts) } BOOTSTD_TEST(expo_object_attr, UT_TESTF_DM | UT_TESTF_SCAN_FDT); +/** + * struct test_iter_priv - private data for expo-iterator test + * + * @count: number of scene objects + * @menu_count: number of menus + * @fail_at: item ID at which to return an error + */ +struct test_iter_priv { + int count; + int menu_count; + int fail_at; +}; + +int h_test_iter(struct scene_obj *obj, void *vpriv) +{ + struct test_iter_priv *priv = vpriv; + + if (priv->fail_at == obj->id) + return -EINVAL; + + priv->count++; + if (obj->type == SCENEOBJT_MENU) + priv->menu_count++; + + return 0; +} + /* Check creating a scene with a menu */ static int expo_object_menu(struct unit_test_state *uts) { @@ -296,6 +323,7 @@ static int expo_object_menu(struct unit_test_state *uts) struct scene_menitem *item; int id, label_id, desc_id, key_id, pointer_id, preview_id; struct scene_obj_txt *ptr, *name1, *desc1, *key1, *tit, *prev1; + struct test_iter_priv priv; struct scene *scn; struct expo *exp; ulong start_mem; @@ -382,6 +410,23 @@ static int expo_object_menu(struct unit_test_state *uts) ut_asserteq(menu->obj.dim.y + 32, prev1->obj.dim.y); ut_asserteq(true, prev1->obj.flags & SCENEOF_HIDE); + /* check iterating through scene items */ + memset(&priv, '\0', sizeof(priv)); + ut_assertok(expo_iter_scene_objs(exp, h_test_iter, &priv)); + ut_asserteq(7, priv.count); + ut_asserteq(1, priv.menu_count); + + /* check the iterator failing part way through iteration */ + memset(&priv, '\0', sizeof(priv)); + priv.fail_at = key_id; + ut_asserteq(-EINVAL, expo_iter_scene_objs(exp, h_test_iter, &priv)); + + /* 2 items (preview_id and the menuitem) are after key_id, 7 - 2 = 5 */ + ut_asserteq(5, priv.count); + + /* menu is first, so is still processed */ + ut_asserteq(1, priv.menu_count); + expo_destroy(exp); ut_assertok(ut_check_delta(start_mem)); @@ -669,46 +714,3 @@ static int expo_test_build(struct unit_test_state *uts) return 0; } BOOTSTD_TEST(expo_test_build, UT_TESTF_DM); - -/* Check the cedit command */ -static int expo_cedit(struct unit_test_state *uts) -{ - extern struct expo *cur_exp; - struct scene_obj_menu *menu; - struct scene_obj_txt *txt; - struct expo *exp; - struct scene *scn; - - if (!IS_ENABLED(CONFIG_CMD_CEDIT)) - return -EAGAIN; - - ut_assertok(run_command("cedit load hostfs - cedit.dtb", 0)); - - console_record_reset_enable(); - - /* - * ^N Move down to second menu - * ^M Open menu - * ^N Move down to second item - * ^M Select item - * \e Quit - */ - console_in_puts("\x0e\x0d\x0e\x0d\e"); - ut_assertok(run_command("cedit run", 0)); - - exp = cur_exp; - scn = expo_lookup_scene_id(exp, exp->scene_id); - ut_assertnonnull(scn); - - menu = scene_obj_find(scn, scn->highlight_id, SCENEOBJT_NONE); - ut_assertnonnull(menu); - - txt = scene_obj_find(scn, menu->title_id, SCENEOBJT_NONE); - ut_assertnonnull(txt); - ut_asserteq_str("AC Power", expo_get_str(exp, txt->str_id)); - - ut_asserteq(ID_AC_ON, menu->cur_item_id); - - return 0; -} -BOOTSTD_TEST(expo_cedit, UT_TESTF_DM | UT_TESTF_SCAN_FDT); diff --git a/test/boot/files/expo_ids.h b/test/boot/files/expo_ids.h new file mode 100644 index 0000000..027d44b --- /dev/null +++ b/test/boot/files/expo_ids.h @@ -0,0 +1,25 @@ +/* SPDX-License-Identifier: GPL-2.0+ */ +/* + * Sample expo screen layout (ID numbers) + */ + +enum { + ZERO, + ID_PROMPT, + + ID_SCENE1, + ID_SCENE1_TITLE, + + ID_CPU_SPEED, + ID_CPU_SPEED_TITLE, + ID_CPU_SPEED_1, + ID_CPU_SPEED_2, + ID_CPU_SPEED_3, + + ID_POWER_LOSS, + ID_AC_OFF, + ID_AC_ON, + ID_AC_MEMORY, + + ID_DYNAMIC_START, +}; diff --git a/test/boot/files/expo_layout.dts b/test/boot/files/expo_layout.dts index 55d5c91..cb2a674 100644 --- a/test/boot/files/expo_layout.dts +++ b/test/boot/files/expo_layout.dts @@ -5,28 +5,7 @@ /dts-v1/; -/* -enum { - ZERO, - ID_PROMPT, - - ID_SCENE1, - ID_SCENE1_TITLE, - - ID_CPU_SPEED, - ID_CPU_SPEED_TITLE, - ID_CPU_SPEED_1, - ID_CPU_SPEED_2, - ID_CPU_SPEED_3, - - ID_POWER_LOSS, - ID_AC_OFF, - ID_AC_ON, - ID_AC_MEMORY, - - ID_DYNAMIC_START, -}; -*/ +/* see expo_ids.h for the IDs */ / { dynamic-start = <ID_DYNAMIC_START>; @@ -59,6 +38,9 @@ enum { /* IDs for the menu items */ item-id = <ID_CPU_SPEED_1 ID_CPU_SPEED_2 ID_CPU_SPEED_3>; + + start-bit = <0x400>; + bit-length = <2>; }; power-loss { @@ -70,6 +52,8 @@ enum { "Memory"; item-id = <ID_AC_OFF ID_AC_ON ID_AC_MEMORY>; + start-bit = <0x422>; + bit-length = <2>; }; }; }; diff --git a/test/cmd_ut.c b/test/cmd_ut.c index 0cb5144..0f56409 100644 --- a/test/cmd_ut.c +++ b/test/cmd_ut.c @@ -201,7 +201,7 @@ static char ut_help_text[] = "\nfdt - fdt command" #endif #ifdef CONFIG_CONSOLE_TRUETYPE - "\nut font - font command" + "\nfont - font command" #endif #ifdef CONFIG_CMD_LOADM "\nloadm - loadm command parameters and loading memory blob" diff --git a/test/common/event.c b/test/common/event.c index 6037ae2..c0912a3 100644 --- a/test/common/event.c +++ b/test/common/event.c @@ -18,6 +18,8 @@ struct test_state { int val; }; +static bool called; + static int h_adder(void *ctx, struct event *event) { struct event_data_test *data = &event->data.test; @@ -28,6 +30,14 @@ static int h_adder(void *ctx, struct event *event) return 0; } +static int h_adder_simple(void) +{ + called = true; + + return 0; +} +EVENT_SPY_SIMPLE(EVT_TEST, h_adder_simple); + static int test_event_base(struct unit_test_state *uts) { struct test_state state; @@ -46,6 +56,18 @@ static int test_event_base(struct unit_test_state *uts) } COMMON_TEST(test_event_base, 0); +static int test_event_simple(struct unit_test_state *uts) +{ + called = false; + + /* Check that the handler is called */ + ut_assertok(event_notify_null(EVT_TEST)); + ut_assert(called); + + return 0; +} +COMMON_TEST(test_event_simple, 0); + static int h_probe(void *ctx, struct event *event) { struct test_state *test_state = ctx; diff --git a/test/dm/acpi.c b/test/dm/acpi.c index 77eb524..5997bda 100644 --- a/test/dm/acpi.c +++ b/test/dm/acpi.c @@ -221,7 +221,8 @@ static int dm_test_acpi_create_dmar(struct unit_test_state *uts) ut_assertnonnull(cpu); ut_assertok(acpi_create_dmar(&dmar, DMAR_INTR_REMAP)); ut_asserteq(DMAR_INTR_REMAP, dmar.flags); - ut_asserteq(32 - 1, dmar.host_address_width); + ut_asserteq((IS_ENABLED(CONFIG_PHYS_64BIT) ? 64 : 32) - 1, + dmar.host_address_width); return 0; } @@ -277,13 +278,16 @@ static int dm_test_acpi_write_tables(struct unit_test_state *uts) */ ut_asserteq_ptr(dmar + 3, ctx.current); ut_asserteq(DMAR_INTR_REMAP, dmar->flags); - ut_asserteq(32 - 1, dmar->host_address_width); + ut_asserteq((IS_ENABLED(CONFIG_PHYS_64BIT) ? 64 : 32) - 1, + dmar->host_address_width); ut_asserteq(DMAR_INTR_REMAP, dmar[1].flags); - ut_asserteq(32 - 1, dmar[1].host_address_width); + ut_asserteq((IS_ENABLED(CONFIG_PHYS_64BIT) ? 64 : 32) - 1, + dmar[1].host_address_width); ut_asserteq(DMAR_INTR_REMAP, dmar[2].flags); - ut_asserteq(32 - 1, dmar[2].host_address_width); + ut_asserteq((IS_ENABLED(CONFIG_PHYS_64BIT) ? 64 : 32) - 1, + dmar[2].host_address_width); /* Check that the pointers were added correctly */ for (i = 0; i < 3; i++) { diff --git a/test/dm/clk.c b/test/dm/clk.c index f48de05..01417fb 100644 --- a/test/dm/clk.c +++ b/test/dm/clk.c @@ -36,6 +36,15 @@ static int dm_test_clk_base(struct unit_test_state *uts) ut_asserteq(clk_is_match(&clk_method1, &clk_method2), true); ut_asserteq(clk_method1.id, clk_method2.id); + ut_assertok(uclass_get_device_by_name(UCLASS_MISC, "clk-test2", &dev)); + ut_assertok(clk_set_defaults(dev, CLK_DEFAULTS_PRE)); + + ut_assertok(uclass_get_device_by_name(UCLASS_MISC, "clk-test3", &dev)); + ut_assertok(clk_set_defaults(dev, CLK_DEFAULTS_PRE)); + + ut_assertok(uclass_get_device_by_name(UCLASS_MISC, "clk-test4", &dev)); + ut_assertok(clk_set_defaults(dev, CLK_DEFAULTS_PRE)); + return 0; } diff --git a/test/dm/fwu_mdata.c b/test/dm/fwu_mdata.c index 8b5c83e..52018f6 100644 --- a/test/dm/fwu_mdata.c +++ b/test/dm/fwu_mdata.c @@ -93,6 +93,12 @@ static int dm_test_fwu_mdata_read(struct unit_test_state *uts) struct udevice *dev; struct fwu_mdata mdata = { 0 }; + /* + * Trigger lib/fwu_updates/fwu.c fwu_boottime_checks() + * to populate g_dev global pointer in that library. + */ + event_notify_null(EVT_MAIN_LOOP); + ut_assertok(uclass_first_device_err(UCLASS_FWU_MDATA, &dev)); ut_assertok(setup_blk_device(uts)); ut_assertok(populate_mmc_disk_image(uts)); @@ -112,6 +118,12 @@ static int dm_test_fwu_mdata_write(struct unit_test_state *uts) struct udevice *dev; struct fwu_mdata mdata = { 0 }; + /* + * Trigger lib/fwu_updates/fwu.c fwu_boottime_checks() + * to populate g_dev global pointer in that library. + */ + event_notify_null(EVT_MAIN_LOOP); + ut_assertok(setup_blk_device(uts)); ut_assertok(populate_mmc_disk_image(uts)); ut_assertok(write_mmc_blk_device(uts)); diff --git a/test/dm/host.c b/test/dm/host.c index 355ba77..85f21f9 100644 --- a/test/dm/host.c +++ b/test/dm/host.c @@ -8,6 +8,7 @@ #include <blk.h> #include <dm.h> #include <fs.h> +#include <os.h> #include <sandbox_host.h> #include <asm/test.h> #include <dm/device-internal.h> @@ -15,9 +16,6 @@ #include <test/test.h> #include <test/ut.h> -static const char filename[] = "2MB.ext2.img"; -static const char filename2[] = "1MB.fat32.img"; - /* Basic test of host interface */ static int dm_test_host(struct unit_test_state *uts) { @@ -25,6 +23,7 @@ static int dm_test_host(struct unit_test_state *uts) struct udevice *dev, *part, *chk, *blk; struct host_sb_plat *plat; struct blk_desc *desc; + char fname[256]; ulong mem_start; loff_t actwrite; @@ -40,13 +39,15 @@ static int dm_test_host(struct unit_test_state *uts) ut_assert(label != plat->label); ut_asserteq(0, plat->fd); - /* Attach a file created in test_host.py */ - ut_assertok(host_attach_file(dev, filename)); + /* Attach a file created in test_ut_dm_init */ + ut_assertok(os_persistent_file(fname, sizeof(fname), "2MB.ext2.img")); + + ut_assertok(host_attach_file(dev, fname)); ut_assertok(uclass_first_device_err(UCLASS_HOST, &chk)); ut_asserteq_ptr(chk, dev); - ut_asserteq_str(filename, plat->filename); - ut_assert(filename != plat->filename); + ut_asserteq_str(fname, plat->filename); + ut_assert(fname != plat->filename); ut_assert(plat->fd != 0); /* Get the block device */ @@ -79,12 +80,14 @@ static int dm_test_host_dup(struct unit_test_state *uts) { static char label[] = "test"; struct udevice *dev, *chk; + char fname[256]; ut_asserteq(0, uclass_id_count(UCLASS_HOST)); ut_assertok(host_create_device(label, true, &dev)); - /* Attach a file created in test_host.py */ - ut_assertok(host_attach_file(dev, filename)); + /* Attach a file created in test_ut_dm_init */ + ut_assertok(os_persistent_file(fname, sizeof(fname), "2MB.ext2.img")); + ut_assertok(host_attach_file(dev, fname)); ut_assertok(uclass_first_device_err(UCLASS_HOST, &chk)); ut_asserteq_ptr(chk, dev); ut_asserteq(1, uclass_id_count(UCLASS_HOST)); @@ -92,8 +95,10 @@ static int dm_test_host_dup(struct unit_test_state *uts) /* Create another device with the same label (should remove old one) */ ut_assertok(host_create_device(label, true, &dev)); - /* Attach a different file created in test_host.py */ - ut_assertok(host_attach_file(dev, filename2)); + /* Attach a different file created in test_ut_dm_init */ + ut_assertok(os_persistent_file(fname, sizeof(fname), "1MB.fat32.img")); + ut_assertok(host_attach_file(dev, fname)); + ut_assertok(uclass_first_device_err(UCLASS_HOST, &chk)); ut_asserteq_ptr(chk, dev); @@ -109,6 +114,7 @@ static int dm_test_cmd_host(struct unit_test_state *uts) { struct udevice *dev, *blk; struct blk_desc *desc; + char fname[256]; console_record_reset(); @@ -117,7 +123,8 @@ static int dm_test_cmd_host(struct unit_test_state *uts) ut_assert_nextline("dev blocks label path"); ut_assert_console_end(); - ut_assertok(run_commandf("host bind -r test2 %s", filename)); + ut_assertok(os_persistent_file(fname, sizeof(fname), "2MB.ext2.img")); + ut_assertok(run_commandf("host bind -r test2 %s", fname)); /* Check the -r flag worked */ ut_assertok(uclass_first_device_err(UCLASS_HOST, &dev)); @@ -127,10 +134,11 @@ static int dm_test_cmd_host(struct unit_test_state *uts) ut_assertok(run_command("host info", 0)); ut_assert_nextline("dev blocks label path"); - ut_assert_nextline(" 0 4096 test2 2MB.ext2.img"); + ut_assert_nextlinen(" 0 4096 test2"); ut_assert_console_end(); - ut_assertok(run_commandf("host bind fat %s", filename2)); + ut_assertok(os_persistent_file(fname, sizeof(fname), "1MB.fat32.img")); + ut_assertok(run_commandf("host bind fat %s", fname)); /* Check it is not removable (no '-r') */ ut_assertok(uclass_next_device_err(&dev)); @@ -140,8 +148,8 @@ static int dm_test_cmd_host(struct unit_test_state *uts) ut_assertok(run_command("host info", 0)); ut_assert_nextline("dev blocks label path"); - ut_assert_nextline(" 0 4096 test2 2MB.ext2.img"); - ut_assert_nextline(" 1 2048 fat 1MB.fat32.img"); + ut_assert_nextlinen(" 0 4096 test2"); + ut_assert_nextlinen(" 1 2048 fat"); ut_assert_console_end(); ut_asserteq(1, run_command("host info test", 0)); @@ -150,7 +158,7 @@ static int dm_test_cmd_host(struct unit_test_state *uts) ut_assertok(run_command("host info fat", 0)); ut_assert_nextline("dev blocks label path"); - ut_assert_nextline(" 1 2048 fat 1MB.fat32.img"); + ut_assert_nextlinen(" 1 2048 fat"); ut_assert_console_end(); /* check 'host dev' */ @@ -187,7 +195,7 @@ static int dm_test_cmd_host(struct unit_test_state *uts) ut_assertok(run_command("host info", 0)); ut_assert_nextline("dev blocks label path"); - ut_assert_nextline(" 1 2048 fat 1MB.fat32.img"); + ut_assert_nextlinen(" 1 2048 fat"); ut_assert_console_end(); return 0; diff --git a/test/dm/nvmxip.c b/test/dm/nvmxip.c index 89bf481..f0ad47d 100644 --- a/test/dm/nvmxip.c +++ b/test/dm/nvmxip.c @@ -103,6 +103,8 @@ static int dm_test_nvmxip(struct unit_test_state *uts) void *buffer = NULL; unsigned long flashsz; + sandbox_set_enable_memio(true); + /* set the flash content first for both devices */ dm_nvmxip_flash_sanity(uts, 0, NULL); dm_nvmxip_flash_sanity(uts, 1, NULL); diff --git a/test/dm/ofnode.c b/test/dm/ofnode.c index 6fbebc7..d71faac 100644 --- a/test/dm/ofnode.c +++ b/test/dm/ofnode.c @@ -583,6 +583,25 @@ static int dm_test_ofnode_conf(struct unit_test_state *uts) } DM_TEST(dm_test_ofnode_conf, 0); +static int dm_test_ofnode_options(struct unit_test_state *uts) +{ + u64 bootscr_address, bootscr_offset; + u64 bootscr_flash_offset, bootscr_flash_size; + + ut_assertok(ofnode_read_bootscript_address(&bootscr_address, + &bootscr_offset)); + ut_asserteq_64(0, bootscr_address); + ut_asserteq_64(0x12345678, bootscr_offset); + + ut_assertok(ofnode_read_bootscript_flash(&bootscr_flash_offset, + &bootscr_flash_size)); + ut_asserteq_64(0, bootscr_flash_offset); + ut_asserteq_64(0x2000, bootscr_flash_size); + + return 0; +} +DM_TEST(dm_test_ofnode_options, 0); + static int dm_test_ofnode_for_each_compatible_node(struct unit_test_state *uts) { const char compatible[] = "denx,u-boot-fdt-test"; @@ -967,6 +986,14 @@ static int dm_test_ofnode_u64(struct unit_test_state *uts) ut_asserteq_64(0x1111222233334444, val); ut_asserteq(-EINVAL, ofnode_read_u64(node, "missing", &val)); + ut_assertok(ofnode_read_u64_index(node, "int64-array", 0, &val)); + ut_asserteq_64(0x1111222233334444, val); + ut_assertok(ofnode_read_u64_index(node, "int64-array", 1, &val)); + ut_asserteq_64(0x4444333322221111, val); + ut_asserteq(-EOVERFLOW, + ofnode_read_u64_index(node, "int64-array", 2, &val)); + ut_asserteq(-EINVAL, ofnode_read_u64_index(node, "missing", 0, &val)); + return 0; } DM_TEST(dm_test_ofnode_u64, UT_TESTF_SCAN_FDT); diff --git a/test/dm/phy.c b/test/dm/phy.c index 4d4a083..4f91abc 100644 --- a/test/dm/phy.c +++ b/test/dm/phy.c @@ -29,7 +29,9 @@ static int dm_test_phy_base(struct unit_test_state *uts) * Get the same phy port in 2 different ways and compare. */ ut_assertok(generic_phy_get_by_name(parent, "phy1", &phy1_method1)); + ut_assert(generic_phy_valid(&phy1_method1)); ut_assertok(generic_phy_get_by_index(parent, 0, &phy1_method2)); + ut_assert(generic_phy_valid(&phy1_method2)); ut_asserteq(phy1_method1.id, phy1_method2.id); /* @@ -47,9 +49,23 @@ static int dm_test_phy_base(struct unit_test_state *uts) ut_assert(phy2.dev != phy3.dev); /* Try to get a non-existing phy */ - ut_asserteq(-ENODEV, uclass_get_device(UCLASS_PHY, 4, &dev)); + ut_asserteq(-ENODEV, uclass_get_device(UCLASS_PHY, 5, &dev)); ut_asserteq(-ENODATA, generic_phy_get_by_name(parent, "phy_not_existing", &phy1_method1)); + ut_assert(!generic_phy_valid(&phy1_method1)); + ut_asserteq(-ENOENT, generic_phy_get_by_index(parent, 3, + &phy1_method2)); + ut_assert(!generic_phy_valid(&phy1_method2)); + + /* Try to get a phy where of_xlate fail */ + ut_assertok(uclass_get_device_by_name(UCLASS_SIMPLE_BUS, + "gen_phy_user2", &parent)); + ut_asserteq(-EINVAL, generic_phy_get_by_name(parent, "phy1", + &phy1_method1)); + ut_assert(!generic_phy_valid(&phy1_method1)); + ut_asserteq(-EINVAL, generic_phy_get_by_index(parent, 0, + &phy1_method2)); + ut_assert(!generic_phy_valid(&phy1_method2)); return 0; } @@ -218,3 +234,32 @@ static int dm_test_phy_multi_exit(struct unit_test_state *uts) return 0; } DM_TEST(dm_test_phy_multi_exit, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); + +static int dm_test_phy_setup(struct unit_test_state *uts) +{ + struct phy phy; + struct udevice *parent; + + ut_assertok(uclass_get_device_by_name(UCLASS_SIMPLE_BUS, + "gen_phy_user", &parent)); + + /* normal */ + ut_assertok(generic_setup_phy(parent, &phy, 0)); + ut_assertok(generic_shutdown_phy(&phy)); + + /* power_off fail with -EIO */ + ut_assertok(generic_setup_phy(parent, &phy, 1)); + ut_asserteq(-EIO, generic_shutdown_phy(&phy)); + + /* power_on fail with -EIO */ + ut_asserteq(-EIO, generic_setup_phy(parent, &phy, 2)); + ut_assertok(generic_shutdown_phy(&phy)); + + /* generic_phy_get_by_index fail with -ENOENT */ + ut_asserteq(-ENOENT, generic_phy_get_by_index(parent, 3, &phy)); + ut_assertok(generic_setup_phy(parent, &phy, 3)); + ut_assertok(generic_shutdown_phy(&phy)); + + return 0; +} +DM_TEST(dm_test_phy_setup, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); diff --git a/test/dm/test-fdt.c b/test/dm/test-fdt.c index eeecd1d..72d0eb5 100644 --- a/test/dm/test-fdt.c +++ b/test/dm/test-fdt.c @@ -135,8 +135,8 @@ int dm_check_devices(struct unit_test_state *uts, int num_devices) * want to test the code that sets that up * (testfdt_drv_probe()). */ - base = fdtdec_get_addr(gd->fdt_blob, dev_of_offset(dev), - "ping-expect"); + base = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev), + "ping-expect", -1); debug("dev=%d, base=%d: %s\n", i, base, fdt_get_name(gd->fdt_blob, dev_of_offset(dev), NULL)); diff --git a/test/dm/tpm.c b/test/dm/tpm.c index 3defb3c..cde933a 100644 --- a/test/dm/tpm.c +++ b/test/dm/tpm.c @@ -98,10 +98,11 @@ static int test_tpm_autostart(struct unit_test_state *uts, if (reinit) ut_assertok(tpm_init(dev)); - /* - * tpm_auto_start will rerun tpm_init() if reinit, but handles the - * -EBUSY return code internally. - */ + + /* + * tpm_auto_start will rerun tpm_init() if reinit, but handles the + * -EBUSY return code internally. + */ ut_assertok(tpm_auto_start(dev)); return 0; diff --git a/test/lib/abuf.c b/test/lib/abuf.c index 42ee4c1..42803b2 100644 --- a/test/lib/abuf.c +++ b/test/lib/abuf.c @@ -155,6 +155,31 @@ static int lib_test_abuf_realloc_size(struct unit_test_state *uts) } LIB_TEST(lib_test_abuf_realloc_size, 0); +/* Test abuf_realloc_inc() */ +static int lib_test_abuf_realloc_inc(struct unit_test_state *uts) +{ + struct abuf buf; + ulong start; + + start = ut_check_free(); + + abuf_init(&buf); + ut_asserteq(0, buf.size); + ut_asserteq(false, buf.alloced); + + abuf_realloc_inc(&buf, 20); + ut_asserteq(20, buf.size); + ut_asserteq(true, buf.alloced); + + abuf_uninit(&buf); + + /* Check for memory leaks */ + ut_assertok(ut_check_delta(start)); + + return 0; +} +LIB_TEST(lib_test_abuf_realloc_inc, 0); + /* Test handling of buffers that are too large */ static int lib_test_abuf_large(struct unit_test_state *uts) { diff --git a/test/lib/asn1.c b/test/lib/asn1.c index 8661fdd..a66cdd7 100644 --- a/test/lib/asn1.c +++ b/test/lib/asn1.c @@ -120,7 +120,7 @@ static int lib_asn1_x509(struct unit_test_state *uts) cert = x509_cert_parse(cert_data, cert_data_len); - ut_assertf(cert != NULL, "decoding failed\n"); + ut_assertf(!IS_ERR(cert), "decoding failed\n"); ut_assertf(!strcmp(cert->subject, "Linaro: Tester"), "subject doesn't match\n"); ut_assertf(!strcmp(cert->issuer, "Linaro: Tester"), @@ -313,7 +313,7 @@ static int lib_asn1_pkcs7(struct unit_test_state *uts) pkcs7 = pkcs7_parse_message(image_pk7, image_pk7_len); - ut_assertf(pkcs7 != NULL, "decoding failed\n"); + ut_assertf(!IS_ERR(pkcs7), "decoding failed\n"); ut_assertf(pkcs7->data_len == 104, "signature size doesn't match\n"); ut_assertf(pkcs7->signed_infos != NULL, "sign-info doesn't exist\n"); ut_assertf(pkcs7->signed_infos->msgdigest_len == 32, diff --git a/test/lib/strlcat.c b/test/lib/strlcat.c index a0ec037..d8453fe 100644 --- a/test/lib/strlcat.c +++ b/test/lib/strlcat.c @@ -43,11 +43,11 @@ static int do_test_strlcat(struct unit_test_state *uts, int line, size_t align1, s2[i] = 32 + 23 * i % (127 - 32); s2[len2 - 1] = '\0'; - expected = len2 < n ? min(len1 + len2 - 1, n) : n; + expected = min(strlen(s2), n) + strlen(s1); actual = strlcat(s2, s1, n); if (expected != actual) { ut_failf(uts, __FILE__, line, __func__, - "strlcat(s2, s1, 2) == len2 < n ? min(len1 + len2, n) : n", + "strlcat(s2, s1, n) == min(len2, n) + len1", "Expected %#zx (%zd), got %#zx (%zd)", expected, expected, actual, actual); return CMD_RET_FAILURE; diff --git a/test/print_ut.c b/test/print_ut.c index 47a6ce5..b26f628 100644 --- a/test/print_ut.c +++ b/test/print_ut.c @@ -283,16 +283,21 @@ static int print_do_hex_dump(struct unit_test_state *uts) /* bytes */ console_record_reset(); print_hex_dump_bytes("", DUMP_PREFIX_ADDRESS, buf, 0x12); - ut_assert_nextline("00000000: 00 11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff ..\"3DUfw........"); - ut_assert_nextline("00000010: 10 00 .."); + ut_assert_nextline("%0*lx: 00 11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff ..\"3DUfw........", + IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, 0x0UL); + ut_assert_nextline("%0*lx: 10 00 ..", + IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, 0x10UL); ut_assert_console_end(); /* line length */ console_record_reset(); print_hex_dump("", DUMP_PREFIX_ADDRESS, 8, 1, buf, 0x12, true); - ut_assert_nextline("00000000: 00 11 22 33 44 55 66 77 ..\"3DUfw"); - ut_assert_nextline("00000008: 88 99 aa bb cc dd ee ff ........"); - ut_assert_nextline("00000010: 10 00 .."); + ut_assert_nextline("%0*lx: 00 11 22 33 44 55 66 77 ..\"3DUfw", + IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, 0x0UL); + ut_assert_nextline("%0*lx: 88 99 aa bb cc dd ee ff ........", + IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, 0x8UL); + ut_assert_nextline("%0*lx: 10 00 ..", + IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, 0x10UL); ut_assert_console_end(); unmap_sysmem(buf); @@ -300,31 +305,39 @@ static int print_do_hex_dump(struct unit_test_state *uts) console_record_reset(); buf[0x41] = 0x41; print_hex_dump("", DUMP_PREFIX_ADDRESS, 0x40, 1, buf, 0x42, true); - ut_assert_nextline("00000000: 00 11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff 10 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ..\"3DUfw........................................................"); - ut_assert_nextline("00000040: 00 41 .A"); + ut_assert_nextline("%0*lx: 00 11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff 10 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ..\"3DUfw........................................................", + IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, 0x0UL); + ut_assert_nextline("%0*lx: 00 41 .A", + IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, 0x40UL); ut_assert_console_end(); /* 16-bit */ console_record_reset(); print_hex_dump("", DUMP_PREFIX_ADDRESS, 0, 2, buf, 0x12, true); - ut_assert_nextline("00000000: 1100 3322 5544 7766 9988 bbaa ddcc ffee ..\"3DUfw........"); - ut_assert_nextline("00000010: 0010 .."); + ut_assert_nextline("%0*lx: 1100 3322 5544 7766 9988 bbaa ddcc ffee ..\"3DUfw........", + IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, 0x0UL); + ut_assert_nextline("%0*lx: 0010 ..", + IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, 0x10UL); ut_assert_console_end(); unmap_sysmem(buf); /* 32-bit */ console_record_reset(); print_hex_dump("", DUMP_PREFIX_ADDRESS, 0, 4, buf, 0x14, true); - ut_assert_nextline("00000000: 33221100 77665544 bbaa9988 ffeeddcc ..\"3DUfw........"); - ut_assert_nextline("00000010: 00000010 ...."); + ut_assert_nextline("%0*lx: 33221100 77665544 bbaa9988 ffeeddcc ..\"3DUfw........", + IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, 0x0UL); + ut_assert_nextline("%0*lx: 00000010 ....", + IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, 0x10UL); ut_assert_console_end(); unmap_sysmem(buf); /* 64-bit */ console_record_reset(); print_hex_dump("", DUMP_PREFIX_ADDRESS, 16, 8, buf, 0x18, true); - ut_assert_nextline("00000000: 7766554433221100 ffeeddccbbaa9988 ..\"3DUfw........"); - ut_assert_nextline("00000010: 0000000000000010 ........"); + ut_assert_nextline("%0*lx: 7766554433221100 ffeeddccbbaa9988 ..\"3DUfw........", + IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, 0x0UL); + ut_assert_nextline("%0*lx: 0000000000000010 ........", + IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, 0x10UL); ut_assert_console_end(); unmap_sysmem(buf); @@ -337,7 +350,8 @@ static int print_do_hex_dump(struct unit_test_state *uts) buf[4 + i] = 126 + i; buf[8] = 255; print_hex_dump("", DUMP_PREFIX_ADDRESS, 0, 1, buf, 10, true); - ut_assert_nextline("00000000: 00 1f 20 21 7e 7f 80 81 ff 99 .. !~....."); + ut_assert_nextline("%0*lx: 00 1f 20 21 7e 7f 80 81 ff 99 .. !~.....", + IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, 0x0UL); ut_assert_console_end(); unmap_sysmem(buf); diff --git a/test/py/tests/fs_helper.py b/test/py/tests/fs_helper.py index 17151bc..9882ddb 100644 --- a/test/py/tests/fs_helper.py +++ b/test/py/tests/fs_helper.py @@ -9,7 +9,7 @@ import re import os from subprocess import call, check_call, check_output, CalledProcessError -def mk_fs(config, fs_type, size, prefix, use_src_dir=False): +def mk_fs(config, fs_type, size, prefix): """Create a file system volume Args: @@ -17,14 +17,12 @@ def mk_fs(config, fs_type, size, prefix, use_src_dir=False): fs_type (str): File system type, e.g. 'ext4' size (int): Size of file system in bytes prefix (str): Prefix string of volume's file name - use_src_dir (bool): true to put the file in the source directory Raises: CalledProcessError: if any error occurs when creating the filesystem """ fs_img = f'{prefix}.{fs_type}.img' - fs_img = os.path.join(config.source_dir if use_src_dir - else config.persistent_data_dir, fs_img) + fs_img = os.path.join(config.persistent_data_dir, fs_img) if fs_type == 'fat16': mkfs_opt = '-F 16' diff --git a/test/py/tests/test_efi_capsule/capsule_gen_binman.dts b/test/py/tests/test_efi_capsule/capsule_gen_binman.dts new file mode 100644 index 0000000..e8a1858 --- /dev/null +++ b/test/py/tests/test_efi_capsule/capsule_gen_binman.dts @@ -0,0 +1,321 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Devicetree for capsule generation through binman + */ + +/dts-v1/; + +#include <sandbox_efi_capsule.h> + +/ { + binman: binman { + multiple-images; + }; +}; + +&binman { + itb { + filename = UBOOT_FIT_IMAGE; + + fit { + description = "Automatic U-Boot environment update"; + #address-cells = <2>; + + images { + u-boot-bin { + description = "U-Boot binary on SPI Flash"; + compression = "none"; + type = "firmware"; + arch = "sandbox"; + load = <0>; + text { + text = "u-boot:New"; + }; + + hash-1 { + algo = "sha1"; + }; + }; + u-boot-env { + description = "U-Boot environment on SPI Flash"; + compression = "none"; + type = "firmware"; + arch = "sandbox"; + load = <0>; + text { + text = "u-boot-env:New"; + }; + + hash-1 { + algo = "sha1"; + }; + }; + }; + }; + }; + + capsule1 { + filename = "Test01"; + efi-capsule { + image-index = <0x1>; + image-guid = SANDBOX_UBOOT_IMAGE_GUID; + + text { + text = "u-boot:New"; + }; + }; + }; + + capsule2 { + filename = "Test02"; + efi-capsule { + image-index = <0x2>; + image-guid = SANDBOX_UBOOT_ENV_IMAGE_GUID; + + text { + text = "u-boot-env:New"; + }; + }; + }; + + capsule3 { + filename = "Test03"; + efi-capsule { + image-index = <0x1>; + image-guid = SANDBOX_INCORRECT_GUID; + + text { + text = "u-boot:New"; + }; + }; + }; + + capsule4 { + filename = "Test04"; + efi-capsule { + image-index = <0x1>; + image-guid = SANDBOX_FIT_IMAGE_GUID; + + blob { + filename = UBOOT_FIT_IMAGE; + }; + }; + }; + + capsule5 { + filename = "Test05"; + efi-capsule { + image-index = <0x1>; + image-guid = SANDBOX_INCORRECT_GUID; + + blob { + filename = UBOOT_FIT_IMAGE; + }; + }; + }; + + capsule6 { + filename = "Test101"; + efi-capsule { + image-index = <0x1>; + fw-version = <0x5>; + image-guid = SANDBOX_UBOOT_IMAGE_GUID; + + text { + text = "u-boot:New"; + }; + }; + }; + + capsule7 { + filename = "Test102"; + efi-capsule { + image-index = <0x2>; + fw-version = <0xa>; + image-guid = SANDBOX_UBOOT_ENV_IMAGE_GUID; + + text { + text = "u-boot-env:New"; + }; + }; + }; + + capsule8 { + filename = "Test103"; + efi-capsule { + image-index = <0x1>; + fw-version = <0x2>; + image-guid = SANDBOX_UBOOT_IMAGE_GUID; + + text { + text = "u-boot:New"; + }; + }; + }; + + capsule9 { + filename = "Test104"; + efi-capsule { + image-index = <0x1>; + fw-version = <0x5>; + image-guid = SANDBOX_FIT_IMAGE_GUID; + + blob { + filename = UBOOT_FIT_IMAGE; + }; + }; + }; + + capsule10 { + filename = "Test105"; + efi-capsule { + image-index = <0x1>; + fw-version = <0x2>; + image-guid = SANDBOX_FIT_IMAGE_GUID; + + blob { + filename = UBOOT_FIT_IMAGE; + }; + }; + }; + + capsule11 { + filename = "Test11"; + efi-capsule { + image-index = <0x1>; + image-guid = SANDBOX_UBOOT_IMAGE_GUID; + private-key = CAPSULE_PRIV_KEY; + public-key-cert = CAPSULE_PUB_KEY; + monotonic-count = <0x1>; + + text { + text = "u-boot:New"; + }; + }; + }; + + capsule12 { + filename = "Test12"; + efi-capsule { + image-index = <0x1>; + image-guid = SANDBOX_UBOOT_IMAGE_GUID; + private-key = CAPSULE_INVAL_KEY; + public-key-cert = CAPSULE_INVAL_PUB_KEY; + monotonic-count = <0x1>; + + text { + text = "u-boot:New"; + }; + }; + }; + + capsule13 { + filename = "Test13"; + efi-capsule { + image-index = <0x1>; + image-guid = SANDBOX_FIT_IMAGE_GUID; + private-key = CAPSULE_PRIV_KEY; + public-key-cert = CAPSULE_PUB_KEY; + monotonic-count = <0x1>; + + blob { + filename = UBOOT_FIT_IMAGE; + }; + }; + }; + + capsule14 { + filename = "Test14"; + efi-capsule { + image-index = <0x1>; + image-guid = SANDBOX_FIT_IMAGE_GUID; + private-key = CAPSULE_INVAL_KEY; + public-key-cert = CAPSULE_INVAL_PUB_KEY; + monotonic-count = <0x1>; + + blob { + filename = UBOOT_FIT_IMAGE; + }; + }; + }; + + capsule15 { + filename = "Test111"; + efi-capsule { + image-index = <0x1>; + fw-version = <0x5>; + image-guid = SANDBOX_UBOOT_IMAGE_GUID; + private-key = CAPSULE_PRIV_KEY; + public-key-cert = CAPSULE_PUB_KEY; + monotonic-count = <0x1>; + + text { + text = "u-boot:New"; + }; + }; + }; + + capsule16 { + filename = "Test112"; + efi-capsule { + image-index = <0x2>; + fw-version = <0xa>; + image-guid = SANDBOX_UBOOT_ENV_IMAGE_GUID; + private-key = CAPSULE_PRIV_KEY; + public-key-cert = CAPSULE_PUB_KEY; + monotonic-count = <0x1>; + + text { + text = "u-boot-env:New"; + }; + }; + }; + + capsule17 { + filename = "Test113"; + efi-capsule { + image-index = <0x1>; + fw-version = <0x2>; + image-guid = SANDBOX_UBOOT_IMAGE_GUID; + private-key = CAPSULE_PRIV_KEY; + public-key-cert = CAPSULE_PUB_KEY; + monotonic-count = <0x1>; + + text { + text = "u-boot:New"; + }; + }; + }; + + capsule18 { + filename = "Test114"; + efi-capsule { + image-index = <0x1>; + fw-version = <0x5>; + image-guid = SANDBOX_FIT_IMAGE_GUID; + private-key = CAPSULE_PRIV_KEY; + public-key-cert = CAPSULE_PUB_KEY; + monotonic-count = <0x1>; + + blob { + filename = UBOOT_FIT_IMAGE; + }; + }; + }; + + capsule19 { + filename = "Test115"; + efi-capsule { + image-index = <0x1>; + fw-version = <0x2>; + image-guid = SANDBOX_FIT_IMAGE_GUID; + private-key = CAPSULE_PRIV_KEY; + public-key-cert = CAPSULE_PUB_KEY; + monotonic-count = <0x1>; + + blob { + filename = UBOOT_FIT_IMAGE; + }; + }; + }; +}; diff --git a/test/py/tests/test_efi_capsule/conftest.py b/test/py/tests/test_efi_capsule/conftest.py index 054be1e..dd41da9 100644 --- a/test/py/tests/test_efi_capsule/conftest.py +++ b/test/py/tests/test_efi_capsule/conftest.py @@ -4,6 +4,8 @@ """Fixture for UEFI capsule test.""" +import os + from subprocess import call, check_call, CalledProcessError import pytest from capsule_defs import CAPSULE_DATA_DIR, CAPSULE_INSTALL_DIR, EFITOOLS_PATH @@ -34,40 +36,31 @@ def efi_capsule_data(request, u_boot_config): capsule_auth_enabled = u_boot_config.buildconfig.get( 'config_efi_capsule_authenticate') + key_dir = u_boot_config.source_dir + '/board/sandbox' if capsule_auth_enabled: - # Create private key (SIGNER.key) and certificate (SIGNER.crt) - check_call('cd %s; ' - 'openssl req -x509 -sha256 -newkey rsa:2048 ' - '-subj /CN=TEST_SIGNER/ -keyout SIGNER.key ' - '-out SIGNER.crt -nodes -days 365' - % data_dir, shell=True) - check_call('cd %s; %scert-to-efi-sig-list SIGNER.crt SIGNER.esl' - % (data_dir, EFITOOLS_PATH), shell=True) - - # Update dtb adding capsule certificate - check_call('cd %s; ' - 'cp %s/test/py/tests/test_efi_capsule/signature.dts .' - % (data_dir, u_boot_config.source_dir), shell=True) - check_call('cd %s; ' - 'dtc -@ -I dts -O dtb -o signature.dtbo signature.dts; ' - 'fdtoverlay -i %s/arch/sandbox/dts/test.dtb ' - '-o test_sig.dtb signature.dtbo' - % (data_dir, u_boot_config.build_dir), shell=True) + # Get the keys from the board directory + check_call('cp %s/capsule_priv_key_good.key %s/SIGNER.key' + % (key_dir, data_dir), shell=True) + check_call('cp %s/capsule_pub_key_good.crt %s/SIGNER.crt' + % (key_dir, data_dir), shell=True) + check_call('cp %s/capsule_pub_esl_good.esl %s/SIGNER.esl' + % (key_dir, data_dir), shell=True) - # Create *malicious* private key (SIGNER2.key) and certificate - # (SIGNER2.crt) - check_call('cd %s; ' - 'openssl req -x509 -sha256 -newkey rsa:2048 ' - '-subj /CN=TEST_SIGNER/ -keyout SIGNER2.key ' - '-out SIGNER2.crt -nodes -days 365' - % data_dir, shell=True) + check_call('cp %s/capsule_priv_key_bad.key %s/SIGNER2.key' + % (key_dir, data_dir), shell=True) + check_call('cp %s/capsule_pub_key_bad.crt %s/SIGNER2.crt' + % (key_dir, data_dir), shell=True) # Update dtb to add the version information check_call('cd %s; ' 'cp %s/test/py/tests/test_efi_capsule/version.dts .' % (data_dir, u_boot_config.source_dir), shell=True) + if capsule_auth_enabled: check_call('cd %s; ' + 'cp %s/arch/sandbox/dts/test.dtb test_sig.dtb' + % (data_dir, u_boot_config.build_dir), shell=True) + check_call('cd %s; ' 'dtc -@ -I dts -O dtb -o version.dtbo version.dts; ' 'fdtoverlay -i test_sig.dtb ' '-o test_ver.dtb version.dtbo' @@ -79,132 +72,20 @@ def efi_capsule_data(request, u_boot_config): '-o test_ver.dtb version.dtbo' % (data_dir, u_boot_config.build_dir), shell=True) - # Create capsule files # two regions: one for u-boot.bin and the other for u-boot.env check_call('cd %s; echo -n u-boot:Old > u-boot.bin.old; echo -n u-boot:New > u-boot.bin.new; echo -n u-boot-env:Old > u-boot.env.old; echo -n u-boot-env:New > u-boot.env.new' % data_dir, shell=True) - check_call('sed -e \"s?BINFILE1?u-boot.bin.new?\" -e \"s?BINFILE2?u-boot.env.new?\" %s/test/py/tests/test_efi_capsule/uboot_bin_env.its > %s/uboot_bin_env.its' % - (u_boot_config.source_dir, data_dir), - shell=True) - check_call('cd %s; %s/tools/mkimage -f uboot_bin_env.its uboot_bin_env.itb' % - (data_dir, u_boot_config.build_dir), - shell=True) - check_call('cd %s; %s/tools/mkeficapsule --index 1 --guid 09D7CF52-0720-4710-91D1-08469B7FE9C8 u-boot.bin.new Test01' % - (data_dir, u_boot_config.build_dir), - shell=True) - check_call('cd %s; %s/tools/mkeficapsule --index 2 --guid 5A7021F5-FEF2-48B4-AABA-832E777418C0 u-boot.env.new Test02' % - (data_dir, u_boot_config.build_dir), - shell=True) - check_call('cd %s; %s/tools/mkeficapsule --index 1 --guid 058B7D83-50D5-4C47-A195-60D86AD341C4 u-boot.bin.new Test03' % - (data_dir, u_boot_config.build_dir), - shell=True) - check_call('cd %s; %s/tools/mkeficapsule --index 1 --guid 3673B45D-6A7C-46F3-9E60-ADABB03F7937 uboot_bin_env.itb Test04' % - (data_dir, u_boot_config.build_dir), - shell=True) - check_call('cd %s; %s/tools/mkeficapsule --index 1 --guid 058B7D83-50D5-4C47-A195-60D86AD341C4 uboot_bin_env.itb Test05' % - (data_dir, u_boot_config.build_dir), - shell=True) - check_call('cd %s; %s/tools/mkeficapsule --index 1 --fw-version 5 ' - '--guid 09D7CF52-0720-4710-91D1-08469B7FE9C8 u-boot.bin.new Test101' % - (data_dir, u_boot_config.build_dir), - shell=True) - check_call('cd %s; %s/tools/mkeficapsule --index 2 --fw-version 10 ' - '--guid 5A7021F5-FEF2-48B4-AABA-832E777418C0 u-boot.env.new Test102' % - (data_dir, u_boot_config.build_dir), - shell=True) - check_call('cd %s; %s/tools/mkeficapsule --index 1 --fw-version 2 ' - '--guid 09D7CF52-0720-4710-91D1-08469B7FE9C8 u-boot.bin.new Test103' % - (data_dir, u_boot_config.build_dir), - shell=True) - check_call('cd %s; %s/tools/mkeficapsule --index 1 --fw-version 5 ' - '--guid 3673B45D-6A7C-46F3-9E60-ADABB03F7937 uboot_bin_env.itb Test104' % - (data_dir, u_boot_config.build_dir), - shell=True) - check_call('cd %s; %s/tools/mkeficapsule --index 1 --fw-version 2 ' - '--guid 3673B45D-6A7C-46F3-9E60-ADABB03F7937 uboot_bin_env.itb Test105' % - (data_dir, u_boot_config.build_dir), - shell=True) - if capsule_auth_enabled: - # raw firmware signed with proper key - check_call('cd %s; ' - '%s/tools/mkeficapsule --index 1 --monotonic-count 1 ' - '--private-key SIGNER.key --certificate SIGNER.crt ' - '--guid 09D7CF52-0720-4710-91D1-08469B7FE9C8 ' - 'u-boot.bin.new Test11' - % (data_dir, u_boot_config.build_dir), - shell=True) - # raw firmware signed with *mal* key - check_call('cd %s; ' - '%s/tools/mkeficapsule --index 1 --monotonic-count 1 ' - '--private-key SIGNER2.key ' - '--certificate SIGNER2.crt ' - '--guid 09D7CF52-0720-4710-91D1-08469B7FE9C8 ' - 'u-boot.bin.new Test12' - % (data_dir, u_boot_config.build_dir), - shell=True) - # FIT firmware signed with proper key - check_call('cd %s; ' - '%s/tools/mkeficapsule --index 1 --monotonic-count 1 ' - '--private-key SIGNER.key --certificate SIGNER.crt ' - '--guid 3673B45D-6A7C-46F3-9E60-ADABB03F7937 ' - 'uboot_bin_env.itb Test13' - % (data_dir, u_boot_config.build_dir), - shell=True) - # FIT firmware signed with *mal* key - check_call('cd %s; ' - '%s/tools/mkeficapsule --index 1 --monotonic-count 1 ' - '--private-key SIGNER2.key ' - '--certificate SIGNER2.crt ' - '--guid 3673B45D-6A7C-46F3-9E60-ADABB03F7937 ' - 'uboot_bin_env.itb Test14' - % (data_dir, u_boot_config.build_dir), - shell=True) - # raw firmware signed with proper key with version information - check_call('cd %s; ' - '%s/tools/mkeficapsule --index 1 --monotonic-count 1 ' - '--fw-version 5 ' - '--private-key SIGNER.key --certificate SIGNER.crt ' - '--guid 09D7CF52-0720-4710-91D1-08469B7FE9C8 ' - 'u-boot.bin.new Test111' - % (data_dir, u_boot_config.build_dir), - shell=True) - # raw firmware signed with proper key with version information - check_call('cd %s; ' - '%s/tools/mkeficapsule --index 2 --monotonic-count 1 ' - '--fw-version 10 ' - '--private-key SIGNER.key --certificate SIGNER.crt ' - '--guid 5A7021F5-FEF2-48B4-AABA-832E777418C0 ' - 'u-boot.env.new Test112' - % (data_dir, u_boot_config.build_dir), - shell=True) - # raw firmware signed with proper key with lower version information - check_call('cd %s; ' - '%s/tools/mkeficapsule --index 1 --monotonic-count 1 ' - '--fw-version 2 ' - '--private-key SIGNER.key --certificate SIGNER.crt ' - '--guid 09D7CF52-0720-4710-91D1-08469B7FE9C8 ' - 'u-boot.bin.new Test113' - % (data_dir, u_boot_config.build_dir), - shell=True) - # FIT firmware signed with proper key with version information - check_call('cd %s; ' - '%s/tools/mkeficapsule --index 1 --monotonic-count 1 ' - '--fw-version 5 ' - '--private-key SIGNER.key --certificate SIGNER.crt ' - '--guid 3673B45D-6A7C-46F3-9E60-ADABB03F7937 ' - 'uboot_bin_env.itb Test114' - % (data_dir, u_boot_config.build_dir), - shell=True) - # FIT firmware signed with proper key with lower version information - check_call('cd %s; ' - '%s/tools/mkeficapsule --index 1 --monotonic-count 1 ' - '--fw-version 2 ' - '--private-key SIGNER.key --certificate SIGNER.crt ' - '--guid 3673B45D-6A7C-46F3-9E60-ADABB03F7937 ' - 'uboot_bin_env.itb Test115' - % (data_dir, u_boot_config.build_dir), - shell=True) + pythonpath = os.environ.get('PYTHONPATH', '') + os.environ['PYTHONPATH'] = pythonpath + ':' + '%s/scripts/dtc/pylibfdt' % u_boot_config.build_dir + check_call('cd %s; ' + 'cc -E -I %s/include -x assembler-with-cpp -o capsule_gen_tmp.dts %s/test/py/tests/test_efi_capsule/capsule_gen_binman.dts; ' + 'dtc -I dts -O dtb capsule_gen_tmp.dts -o capsule_binman.dtb;' + % (data_dir, u_boot_config.source_dir, u_boot_config.source_dir), shell=True) + check_call('cd %s; ' + './tools/binman/binman --toolpath %s/tools build -u -d %s/capsule_binman.dtb -O %s -m --allow-missing -I %s -I ./board/sandbox -I ./arch/sandbox/dts' + % (u_boot_config.source_dir, u_boot_config.build_dir, data_dir, data_dir, data_dir), shell=True) + os.environ['PYTHONPATH'] = pythonpath # Create a disk image with EFI system partition check_call('virt-make-fs --partition=gpt --size=+1M --type=vfat %s %s' % diff --git a/test/py/tests/test_efi_capsule/signature.dts b/test/py/tests/test_efi_capsule/signature.dts deleted file mode 100644 index 078cfc7..0000000 --- a/test/py/tests/test_efi_capsule/signature.dts +++ /dev/null @@ -1,10 +0,0 @@ -// SPDX-License-Identifier: GPL-2.0+ - -/dts-v1/; -/plugin/; - -&{/} { - signature { - capsule-key = /incbin/("SIGNER.esl"); - }; -}; diff --git a/test/py/tests/test_efi_capsule/uboot_bin_env.its b/test/py/tests/test_efi_capsule/uboot_bin_env.its deleted file mode 100644 index fc65907..0000000 --- a/test/py/tests/test_efi_capsule/uboot_bin_env.its +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Automatic software update for U-Boot - * Make sure the flashing addresses ('load' prop) is correct for your board! - */ - -/dts-v1/; - -/ { - description = "Automatic U-Boot environment update"; - #address-cells = <2>; - - images { - u-boot-bin { - description = "U-Boot binary on SPI Flash"; - data = /incbin/("BINFILE1"); - compression = "none"; - type = "firmware"; - arch = "sandbox"; - load = <0>; - hash-1 { - algo = "sha1"; - }; - }; - u-boot-env { - description = "U-Boot environment on SPI Flash"; - data = /incbin/("BINFILE2"); - compression = "none"; - type = "firmware"; - arch = "sandbox"; - load = <0>; - hash-1 { - algo = "sha1"; - }; - }; - }; -}; diff --git a/test/py/tests/test_event_dump.py b/test/py/tests/test_event_dump.py index da196df..e282c67 100644 --- a/test/py/tests/test_event_dump.py +++ b/test/py/tests/test_event_dump.py @@ -18,5 +18,7 @@ def test_event_dump(u_boot_console): -------------------- ------------------------------ ------------------------------ EVT_FT_FIXUP bootmeth_vbe_ft_fixup .*boot/vbe_request.c:.* EVT_FT_FIXUP bootmeth_vbe_simple_ft_fixup .*boot/vbe_simple_os.c:.* -EVT_MISC_INIT_F sandbox_misc_init_f .*arch/sandbox/cpu/start.c:''' +EVT_LAST_STAGE_INIT install_smbios_table .*lib/efi_loader/efi_smbios.c:.* +EVT_MISC_INIT_F sandbox_early_getopt_check .*arch/sandbox/cpu/start.c:.* +EVT_TEST h_adder_simple .*test/common/event.c:''' assert re.match(expect, out, re.MULTILINE) is not None diff --git a/test/py/tests/test_gpt.py b/test/py/tests/test_gpt.py index 73bfbf7..6e135b6 100644 --- a/test/py/tests/test_gpt.py +++ b/test/py/tests/test_gpt.py @@ -16,6 +16,35 @@ the test. # Mark all tests here as slow pytestmark = pytest.mark.slow +def parse_gpt_parts(disk_str): + """Parser a partition string into a list of partitions. + + Args: + disk_str: The disk description string, as returned by `gpt read` + + Returns: + A list of parsed partitions. Each partition is a dictionary with the + string value from each specified key in the partition description, or a + key with with the value True for a boolean flag + """ + parts = [] + for part_str in disk_str.split(';'): + part = {} + for option in part_str.split(","): + if not option: + continue + + if "=" in option: + key, value = option.split("=") + part[key] = value + else: + part[option] = True + + if part: + parts.append(part) + + return parts + class GptTestDiskImage(object): """Disk Image used by the GPT tests.""" @@ -49,10 +78,13 @@ class GptTestDiskImage(object): u_boot_utils.run_and_log(u_boot_console, cmd) # part1 offset 1MB size 1MB cmd = ('sgdisk', '--new=1:2048:4095', '--change-name=1:part1', + '--partition-guid=1:33194895-67f6-4561-8457-6fdeed4f50a3', + '-A 1:set:2', persistent) # part2 offset 2MB size 1.5MB u_boot_utils.run_and_log(u_boot_console, cmd) cmd = ('sgdisk', '--new=2:4096:7167', '--change-name=2:part2', + '--partition-guid=2:cc9c6e4a-6551-4cb5-87be-3210f96c86fb', persistent) u_boot_utils.run_and_log(u_boot_console, cmd) cmd = ('sgdisk', '--load-backup=' + persistent) @@ -61,18 +93,14 @@ class GptTestDiskImage(object): cmd = ('cp', persistent, self.path) u_boot_utils.run_and_log(u_boot_console, cmd) -gtdi = None @pytest.fixture(scope='function') def state_disk_image(u_boot_console): """pytest fixture to provide a GptTestDiskImage object to tests. This is function-scoped because it uses u_boot_console, which is also - function-scoped. However, we don't need to actually do any function-scope - work, so this simply returns the same object over and over each time.""" + function-scoped. A new disk is returned each time to prevent tests from + interfering with each other.""" - global gtdi - if not gtdi: - gtdi = GptTestDiskImage(u_boot_console) - return gtdi + return GptTestDiskImage(u_boot_console) @pytest.mark.boardspec('sandbox') @pytest.mark.buildconfigspec('cmd_gpt') @@ -93,6 +121,41 @@ def test_gpt_read(state_disk_image, u_boot_console): @pytest.mark.boardspec('sandbox') @pytest.mark.buildconfigspec('cmd_gpt') +@pytest.mark.buildconfigspec('partition_type_guid') +@pytest.mark.requiredtool('sgdisk') +def test_gpt_read_var(state_disk_image, u_boot_console): + """Test the gpt read command.""" + + u_boot_console.run_command('host bind 0 ' + state_disk_image.path) + output = u_boot_console.run_command('gpt read host 0 gpt_parts') + assert 'success!' in output + + output = u_boot_console.run_command('echo ${gpt_parts}') + parts = parse_gpt_parts(output.rstrip()) + + assert parts == [ + { + "uuid_disk": "375a56f7-d6c9-4e81-b5f0-09d41ca89efe", + }, + { + "name": "part1", + "start": "0x100000", + "size": "0x100000", + "type": "0fc63daf-8483-4772-8e79-3d69d8477de4", + "uuid": "33194895-67f6-4561-8457-6fdeed4f50a3", + "bootable": True, + }, + { + "name": "part2", + "start": "0x200000", + "size": "0x180000", + "type": "0fc63daf-8483-4772-8e79-3d69d8477de4", + "uuid": "cc9c6e4a-6551-4cb5-87be-3210f96c86fb", + }, + ] + +@pytest.mark.boardspec('sandbox') +@pytest.mark.buildconfigspec('cmd_gpt') @pytest.mark.requiredtool('sgdisk') def test_gpt_verify(state_disk_image, u_boot_console): """Test the gpt verify command.""" @@ -124,6 +187,38 @@ def test_gpt_guid(state_disk_image, u_boot_console): @pytest.mark.boardspec('sandbox') @pytest.mark.buildconfigspec('cmd_gpt') @pytest.mark.requiredtool('sgdisk') +def test_gpt_setenv(state_disk_image, u_boot_console): + """Test the gpt setenv command.""" + u_boot_console.run_command('host bind 0 ' + state_disk_image.path) + output = u_boot_console.run_command('gpt setenv host 0 part1') + assert 'success!' in output + output = u_boot_console.run_command('echo ${gpt_partition_addr}') + assert output.rstrip() == '800' + output = u_boot_console.run_command('echo ${gpt_partition_size}') + assert output.rstrip() == '800' + output = u_boot_console.run_command('echo ${gpt_partition_name}') + assert output.rstrip() == 'part1' + output = u_boot_console.run_command('echo ${gpt_partition_entry}') + assert output.rstrip() == '1' + output = u_boot_console.run_command('echo ${gpt_partition_bootable}') + assert output.rstrip() == '1' + + output = u_boot_console.run_command('gpt setenv host 0 part2') + assert 'success!' in output + output = u_boot_console.run_command('echo ${gpt_partition_addr}') + assert output.rstrip() == '1000' + output = u_boot_console.run_command('echo ${gpt_partition_size}') + assert output.rstrip() == 'c00' + output = u_boot_console.run_command('echo ${gpt_partition_name}') + assert output.rstrip() == 'part2' + output = u_boot_console.run_command('echo ${gpt_partition_entry}') + assert output.rstrip() == '2' + output = u_boot_console.run_command('echo ${gpt_partition_bootable}') + assert output.rstrip() == '0' + +@pytest.mark.boardspec('sandbox') +@pytest.mark.buildconfigspec('cmd_gpt') +@pytest.mark.requiredtool('sgdisk') def test_gpt_save_guid(state_disk_image, u_boot_console): """Test the gpt guid command to save GUID into a string.""" @@ -186,12 +281,34 @@ def test_gpt_swap_partitions(state_disk_image, u_boot_console): u_boot_console.run_command('host bind 0 ' + state_disk_image.path) output = u_boot_console.run_command('part list host 0') - assert '0x00000800 0x00000fff "first"' in output - assert '0x00001000 0x00001bff "second"' in output - u_boot_console.run_command('gpt swap host 0 first second') + assert '0x00000800 0x00000fff "part1"' in output + assert '0x00001000 0x00001bff "part2"' in output + u_boot_console.run_command('gpt swap host 0 part1 part2') output = u_boot_console.run_command('part list host 0') - assert '0x00000800 0x00000fff "second"' in output - assert '0x00001000 0x00001bff "first"' in output + assert '0x00000800 0x00000fff "part2"' in output + assert '0x00001000 0x00001bff "part1"' in output + +@pytest.mark.buildconfigspec('cmd_gpt') +@pytest.mark.buildconfigspec('cmd_gpt_rename') +@pytest.mark.buildconfigspec('cmd_part') +@pytest.mark.requiredtool('sgdisk') +def test_gpt_set_bootable(state_disk_image, u_boot_console): + """Test the gpt set-bootable command.""" + + u_boot_console.run_command('host bind 0 ' + state_disk_image.path) + parts = ('part2', 'part1') + for bootable in parts: + output = u_boot_console.run_command(f'gpt set-bootable host 0 {bootable}') + assert 'success!' in output + + for p in parts: + output = u_boot_console.run_command(f'gpt setenv host 0 {p}') + assert 'success!' in output + output = u_boot_console.run_command('echo ${gpt_partition_bootable}') + if p == bootable: + assert output.rstrip() == '1' + else: + assert output.rstrip() == '0' @pytest.mark.boardspec('sandbox') @pytest.mark.buildconfigspec('cmd_gpt') @@ -212,3 +329,22 @@ def test_gpt_write(state_disk_image, u_boot_console): assert '0x00001000 0x00001bff "second"' in output output = u_boot_console.run_command('gpt guid host 0') assert '375a56f7-d6c9-4e81-b5f0-09d41ca89efe' in output + +@pytest.mark.buildconfigspec('cmd_gpt') +@pytest.mark.buildconfigspec('cmd_gpt_rename') +@pytest.mark.buildconfigspec('cmd_part') +@pytest.mark.requiredtool('sgdisk') +def test_gpt_transpose(state_disk_image, u_boot_console): + """Test the gpt transpose command.""" + + u_boot_console.run_command('host bind 0 ' + state_disk_image.path) + output = u_boot_console.run_command('part list host 0') + assert '1\t0x00000800\t0x00000fff\t"part1"' in output + assert '2\t0x00001000\t0x00001bff\t"part2"' in output + + output = u_boot_console.run_command('gpt transpose host 0 1 2') + assert 'success!' in output + + output = u_boot_console.run_command('part list host 0') + assert '2\t0x00000800\t0x00000fff\t"part1"' in output + assert '1\t0x00001000\t0x00001bff\t"part2"' in output diff --git a/test/py/tests/test_semihosting/conftest.py b/test/py/tests/test_semihosting/conftest.py new file mode 100644 index 0000000..b00d8f4 --- /dev/null +++ b/test/py/tests/test_semihosting/conftest.py @@ -0,0 +1,23 @@ +# SPDX-License-Identifier: GPL-2.0-or-later + +"""Fixture for semihosting command test +""" + +import os +import pytest + +@pytest.fixture(scope='session') +def semihosting_data(u_boot_config): + """Set up a file system to be used in semihosting tests + + Args: + u_boot_config -- U-Boot configuration. + """ + image_path = u_boot_config.persistent_data_dir + '/semihosting.txt' + + with open(image_path, 'w', encoding = 'utf-8') as file: + file.write('Das U-Boot\n') + + yield image_path + + os.remove(image_path) diff --git a/test/py/tests/test_semihosting/test_hostfs.py b/test/py/tests/test_semihosting/test_hostfs.py new file mode 100644 index 0000000..51f6fa7 --- /dev/null +++ b/test/py/tests/test_semihosting/test_hostfs.py @@ -0,0 +1,33 @@ +# SPDX-License-Identifier: GPL-2.0+ + +""" Unit test for semihosting +""" + +import pytest + +@pytest.mark.buildconfigspec('semihosting') +def test_semihosting_hostfs(u_boot_console, semihosting_data): + """ Unit test for semihosting + + Args: + u_boot_console -- U-Boot console + semihosting_data -- Path to the disk image used for testing. + """ + response = u_boot_console.run_command( + f'load hostfs - $loadaddr {semihosting_data}') + assert '11 bytes read' in response + + response = u_boot_console.run_command( + 'crc32 $loadaddr $filesize') + assert '==> 60cfccfc' in response + + u_boot_console.run_command( + f'save hostfs - $loadaddr {semihosting_data} 11 11') + + response = u_boot_console.run_command( + f'load hostfs - $loadaddr {semihosting_data} 4 13') + assert '4 bytes read' in response + + response = u_boot_console.run_command( + 'crc32 $loadaddr $filesize') + assert '==> e29063ea' in response diff --git a/test/py/tests/test_tpm2.py b/test/py/tests/test_tpm2.py index fce689c..c2579fa 100644 --- a/test/py/tests/test_tpm2.py +++ b/test/py/tests/test_tpm2.py @@ -71,6 +71,9 @@ def test_tpm2_startup(u_boot_console): Initiate the TPM internal state machine. """ + skip_test = u_boot_console.config.env.get('env__tpm_device_test_skip', False) + if skip_test: + pytest.skip('skip TPM device test') u_boot_console.run_command('tpm2 startup TPM2_SU_CLEAR') output = u_boot_console.run_command('echo $?') assert output.endswith('0') diff --git a/test/py/tests/test_trace.py b/test/py/tests/test_trace.py index ac3e959..28a6e72 100644 --- a/test/py/tests/test_trace.py +++ b/test/py/tests/test_trace.py @@ -61,7 +61,7 @@ def collect_trace(cons): # Read out the trace data addr = 0x02000000 - size = 0x01000000 + size = 0x02000000 out = cons.run_command(f'trace calls {addr:x} {size:x}') print(out) fname = os.path.join(TMPDIR, 'trace') @@ -175,7 +175,7 @@ def check_funcgraph(cons, fname, proftool, map_fname, trace_dat): # Then look for this: # u-boot-1 [000] 282.101375: funcgraph_exit: 0.006 us | } # Then check for this: - # u-boot-1 [000] 282.101375: funcgraph_entry: 0.000 us | event_init(); + # u-boot-1 [000] 282.101375: funcgraph_entry: 0.000 us | initcall_is_event(); expected_indent = None found_start = False @@ -197,8 +197,9 @@ def check_funcgraph(cons, fname, proftool, map_fname, trace_dat): elif found_start and indent == expected_indent and brace == '}': found_end = True - # The next function after initf_bootstage() exits should be event_init() - assert upto == 'event_init()' + # The next function after initf_bootstage() exits should be + # initcall_is_event() + assert upto == 'initcall_is_event()' # Now look for initf_dm() and dm_timer_init() so we can check the bootstage # time @@ -247,7 +248,7 @@ def check_flamegraph(cons, fname, proftool, map_fname, trace_fg): # We expect dm_timer_init() to be called twice: once before relocation and # once after look1 = 'initf_dm;dm_timer_init 1' - look2 = 'board_init_r;initr_dm_devices;dm_timer_init 1' + look2 = 'board_init_r;initcall_run_list;initr_dm_devices;dm_timer_init 1' found = 0 with open(trace_fg, 'r') as fd: for line in fd: @@ -272,7 +273,7 @@ def check_flamegraph(cons, fname, proftool, map_fname, trace_fg): total += count return total - +check_flamegraph @pytest.mark.slow @pytest.mark.boardspec('sandbox') @pytest.mark.buildconfigspec('trace') diff --git a/test/py/tests/test_ut.py b/test/py/tests/test_ut.py index aa1d477..82932a6 100644 --- a/test/py/tests/test_ut.py +++ b/test/py/tests/test_ut.py @@ -1,6 +1,7 @@ # SPDX-License-Identifier: GPL-2.0 # Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved. +import collections import getpass import gzip import os @@ -282,13 +283,155 @@ label Fedora-Workstation-armhfp-31-1.9 (5.3.7-301.fc31.armv7hl) copy_prepared_image(cons, mmc_dev, fname) +def setup_cros_image(cons): + """Create a 20MB disk image with ChromiumOS partitions""" + Partition = collections.namedtuple('part', 'start,size,name') + parts = {} + disk_data = None + + def pack_kernel(cons, arch, kern, dummy): + """Pack a kernel containing some fake data + + Args: + cons (ConsoleBase): Console to use + arch (str): Architecture to use ('x86' or 'arm') + kern (str): Filename containing kernel + dummy (str): Dummy filename to use for config and bootloader + + Return: + bytes: Packed-kernel data + """ + kern_part = os.path.join(cons.config.result_dir, 'kern-part-{arch}.bin') + u_boot_utils.run_and_log( + cons, + f'futility vbutil_kernel --pack {kern_part} ' + '--keyblock doc/chromium/files/devkeys/kernel.keyblock ' + '--signprivate doc/chromium/files/devkeys/kernel_data_key.vbprivk ' + f'--version 1 --config {dummy} --bootloader {dummy} ' + f'--vmlinuz {kern}') + + with open(kern_part, 'rb') as inf: + kern_part_data = inf.read() + return kern_part_data + + def set_part_data(partnum, data): + """Set the contents of a disk partition + + This updates disk_data by putting data in the right place + + Args: + partnum (int): Partition number to set + data (bytes): Data for that partition + """ + nonlocal disk_data + + start = parts[partnum].start * sect_size + disk_data = disk_data[:start] + data + disk_data[start + len(data):] + + mmc_dev = 5 + fname = os.path.join(cons.config.source_dir, f'mmc{mmc_dev}.img') + u_boot_utils.run_and_log(cons, 'qemu-img create %s 20M' % fname) + #mnt = os.path.join(cons.config.persistent_data_dir, 'mnt') + #mkdir_cond(mnt) + u_boot_utils.run_and_log(cons, f'cgpt create {fname}') + + uuid_state = 'ebd0a0a2-b9e5-4433-87c0-68b6b72699c7' + uuid_kern = 'fe3a2a5d-4f32-41a7-b725-accc3285a309' + uuid_root = '3cb8e202-3b7e-47dd-8a3c-7ff2a13cfcec' + uuid_rwfw = 'cab6e88e-abf3-4102-a07a-d4bb9be3c1d3' + uuid_reserved = '2e0a753d-9e48-43b0-8337-b15192cb1b5e' + uuid_efi = 'c12a7328-f81f-11d2-ba4b-00a0c93ec93b' + + ptr = 40 + + # Number of sectors in 1MB + sect_size = 512 + sect_1mb = (1 << 20) // sect_size + + required_parts = [ + {'num': 0xb, 'label':'RWFW', 'type': uuid_rwfw, 'size': '1'}, + {'num': 6, 'label':'KERN_C', 'type': uuid_kern, 'size': '1'}, + {'num': 7, 'label':'ROOT_C', 'type': uuid_root, 'size': '1'}, + {'num': 9, 'label':'reserved', 'type': uuid_reserved, 'size': '1'}, + {'num': 0xa, 'label':'reserved', 'type': uuid_reserved, 'size': '1'}, + + {'num': 2, 'label':'KERN_A', 'type': uuid_kern, 'size': '1M'}, + {'num': 4, 'label':'KERN_B', 'type': uuid_kern, 'size': '1M'}, + + {'num': 8, 'label':'OEM', 'type': uuid_state, 'size': '1M'}, + {'num': 0xc, 'label':'EFI-SYSTEM', 'type': uuid_efi, 'size': '1M'}, + + {'num': 5, 'label':'ROOT_B', 'type': uuid_root, 'size': '1'}, + {'num': 3, 'label':'ROOT_A', 'type': uuid_root, 'size': '1'}, + {'num': 1, 'label':'STATE', 'type': uuid_state, 'size': '1M'}, + ] + + for part in required_parts: + size_str = part['size'] + if 'M' in size_str: + size = int(size_str[:-1]) * sect_1mb + else: + size = int(size_str) + u_boot_utils.run_and_log( + cons, + f"cgpt add -i {part['num']} -b {ptr} -s {size} -t {part['type']} {fname}") + ptr += size + + u_boot_utils.run_and_log(cons, f'cgpt boot -p {fname}') + out = u_boot_utils.run_and_log(cons, f'cgpt show -q {fname}') + '''We expect something like this: + 8239 2048 1 Basic data + 45 2048 2 ChromeOS kernel + 8238 1 3 ChromeOS rootfs + 2093 2048 4 ChromeOS kernel + 8237 1 5 ChromeOS rootfs + 41 1 6 ChromeOS kernel + 42 1 7 ChromeOS rootfs + 4141 2048 8 Basic data + 43 1 9 ChromeOS reserved + 44 1 10 ChromeOS reserved + 40 1 11 ChromeOS firmware + 6189 2048 12 EFI System Partition + ''' + + # Create a dict (indexed by partition number) containing the above info + for line in out.splitlines(): + start, size, num, name = line.split(maxsplit=3) + parts[int(num)] = Partition(int(start), int(size), name) + + dummy = os.path.join(cons.config.result_dir, 'dummy.txt') + with open(dummy, 'wb') as outf: + outf.write(b'dummy\n') + + # For now we just use dummy kernels. This limits testing to just detecting + # a signed kernel. We could add support for the x86 data structures so that + # testing could cover getting the cmdline, setup.bin and other pieces. + kern = os.path.join(cons.config.result_dir, 'kern.bin') + with open(kern, 'wb') as outf: + outf.write(b'kernel\n') + + with open(fname, 'rb') as inf: + disk_data = inf.read() + + # put x86 kernel in partition 2 and arm one in partition 4 + set_part_data(2, pack_kernel(cons, 'x86', kern, dummy)) + set_part_data(4, pack_kernel(cons, 'arm', kern, dummy)) + + with open(fname, 'wb') as outf: + outf.write(disk_data) + + return fname + + def setup_cedit_file(cons): infname = os.path.join(cons.config.source_dir, 'test/boot/files/expo_layout.dts') + inhname = os.path.join(cons.config.source_dir, + 'test/boot/files/expo_ids.h') expo_tool = os.path.join(cons.config.source_dir, 'tools/expo.py') outfname = 'cedit.dtb' u_boot_utils.run_and_log( - cons, f'{expo_tool} -e {infname} -l {infname} -o {outfname}') + cons, f'{expo_tool} -e {inhname} -l {infname} -o {outfname}') @pytest.mark.buildconfigspec('ut_dm') @@ -317,10 +460,8 @@ def test_ut_dm_init(u_boot_console): u_boot_utils.run_and_log( u_boot_console, f'sfdisk {fn}', stdin=b'type=83') - fs_helper.mk_fs(u_boot_console.config, 'ext2', 0x200000, '2MB', - use_src_dir=True) - fs_helper.mk_fs(u_boot_console.config, 'fat32', 0x100000, '1MB', - use_src_dir=True) + fs_helper.mk_fs(u_boot_console.config, 'ext2', 0x200000, '2MB') + fs_helper.mk_fs(u_boot_console.config, 'fat32', 0x100000, '1MB') @pytest.mark.buildconfigspec('cmd_bootflow') def test_ut_dm_init_bootstd(u_boot_console): @@ -329,6 +470,7 @@ def test_ut_dm_init_bootstd(u_boot_console): setup_bootflow_image(u_boot_console) setup_bootmenu_image(u_boot_console) setup_cedit_file(u_boot_console) + setup_cros_image(u_boot_console) # Restart so that the new mmc1.img is picked up u_boot_console.restart_uboot() diff --git a/test/unicode_ut.c b/test/unicode_ut.c index b27d711..1d0d90c 100644 --- a/test/unicode_ut.c +++ b/test/unicode_ut.c @@ -807,28 +807,28 @@ static int unicode_test_u16_strlcat(struct unit_test_state *uts) /* dest and src are empty string */ memset(buf, 0, sizeof(buf)); - ret = u16_strlcat(buf, &null_src, sizeof(buf)); - ut_asserteq(1, ret); + ret = u16_strlcat(buf, &null_src, ARRAY_SIZE(buf)); + ut_asserteq(0, ret); /* dest is empty string */ memset(buf, 0, sizeof(buf)); - ret = u16_strlcat(buf, src, sizeof(buf)); - ut_asserteq(5, ret); + ret = u16_strlcat(buf, src, ARRAY_SIZE(buf)); + ut_asserteq(4, ret); ut_assert(!unicode_test_u16_strcmp(buf, src, 40)); /* src is empty string */ memset(buf, 0xCD, (sizeof(buf) - sizeof(u16))); buf[39] = 0; memcpy(buf, dest, sizeof(dest)); - ret = u16_strlcat(buf, &null_src, sizeof(buf)); - ut_asserteq(6, ret); + ret = u16_strlcat(buf, &null_src, ARRAY_SIZE(buf)); + ut_asserteq(5, ret); ut_assert(!unicode_test_u16_strcmp(buf, dest, 40)); for (i = 0; i <= 40; i++) { memset(buf, 0xCD, (sizeof(buf) - sizeof(u16))); buf[39] = 0; memcpy(buf, dest, sizeof(dest)); - expected = 10; + expected = min(5, i) + 4; ret = u16_strlcat(buf, src, i); ut_asserteq(expected, ret); if (i <= 6) { |