From patchwork Mon Apr 27 17:02:34 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 238675 List-Id: U-Boot discussion From: sjg at chromium.org (Simon Glass) Date: Mon, 27 Apr 2020 11:02:34 -0600 Subject: [PATCH 1/3] test: Add the beginnings of some string tests In-Reply-To: <20200427170236.156351-1-sjg@chromium.org> References: <20200427170236.156351-1-sjg@chromium.org> Message-ID: <20200427170236.156351-2-sjg@chromium.org> There are quite a few string functions in U-Boot with no tests. Make a start by adding a test for strtoul(). Signed-off-by: Simon Glass --- include/test/suites.h | 1 + test/Makefile | 1 + test/cmd_ut.c | 5 ++++ test/str_ut.c | 67 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 74 insertions(+) create mode 100644 test/str_ut.c diff --git a/include/test/suites.h b/include/test/suites.h index 39ad81a90f..213e3cee77 100644 --- a/include/test/suites.h +++ b/include/test/suites.h @@ -33,6 +33,7 @@ int do_ut_lib(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]); int do_ut_log(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]); int do_ut_optee(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]); int do_ut_overlay(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]); +int do_ut_str(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]); int do_ut_time(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]); int do_ut_unicode(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]); diff --git a/test/Makefile b/test/Makefile index 2971d0d87f..bab8f1a5c2 100644 --- a/test/Makefile +++ b/test/Makefile @@ -8,6 +8,7 @@ obj-$(CONFIG_UNIT_TEST) += ut.o obj-$(CONFIG_SANDBOX) += command_ut.o obj-$(CONFIG_SANDBOX) += compression.o obj-$(CONFIG_SANDBOX) += print_ut.o +obj-$(CONFIG_SANDBOX) += str_ut.o obj-$(CONFIG_UT_TIME) += time_ut.o obj-$(CONFIG_UT_UNICODE) += unicode_ut.o obj-y += log/ diff --git a/test/cmd_ut.c b/test/cmd_ut.c index 7fdcdbb1a6..bd20a69c55 100644 --- a/test/cmd_ut.c +++ b/test/cmd_ut.c @@ -74,6 +74,8 @@ static cmd_tbl_t cmd_ut_sub[] = { "", ""), U_BOOT_CMD_MKENT(bloblist, CONFIG_SYS_MAXARGS, 1, do_ut_bloblist, "", ""), + U_BOOT_CMD_MKENT(str, CONFIG_SYS_MAXARGS, 1, do_ut_str, + "", ""), #endif }; @@ -137,6 +139,9 @@ static char ut_help_text[] = #ifdef CONFIG_UT_OVERLAY "ut overlay [test-name]\n" #endif +#ifdef CONFIG_SANDBOX + "ut str - Basic test of string functions\n" +#endif #ifdef CONFIG_UT_TIME "ut time - Very basic test of time functions\n" #endif diff --git a/test/str_ut.c b/test/str_ut.c new file mode 100644 index 0000000000..fab8de595c --- /dev/null +++ b/test/str_ut.c @@ -0,0 +1,67 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Copyright 2020 Google LLC + */ + +#include +#include +#include +#include +#include + +/* This is large enough for any of the test strings */ +#define TEST_STR_SIZE 200 + +static const char str1[] = "I'm sorry I'm late."; +static const char str2[] = "1099abNo, don't bother apologising."; +static const char str3[] = "0xbI'm sorry you're alive."; + +/* Declare a new str test */ +#define STR_TEST(_name, _flags) UNIT_TEST(_name, _flags, str_test) + +static int run_strtoul(struct unit_test_state *uts, const char *str, int base, + ulong expect_val, int expect_endp_offset) +{ + char *endp; + ulong val; + + val = simple_strtoul(str, &endp, base); + ut_asserteq(expect_val, val); + ut_asserteq(expect_endp_offset, endp - str); + + return 0; +} + +static int str_simple_strtoul(struct unit_test_state *uts) +{ + /* Base 10 and base 16 */ + ut_assertok(run_strtoul(uts, str2, 10, 1099, 4)); + ut_assertok(run_strtoul(uts, str2, 16, 0x1099ab, 6)); + + /* Invalid string */ + ut_assertok(run_strtoul(uts, str1, 10, 0, 0)); + + /* Base 0 */ + ut_assertok(run_strtoul(uts, str1, 0, 0, 0)); + ut_assertok(run_strtoul(uts, str2, 0, 1099, 4)); + ut_assertok(run_strtoul(uts, str3, 0, 0xb, 3)); + + /* Base 2 */ + ut_assertok(run_strtoul(uts, str1, 2, 0, 0)); + ut_assertok(run_strtoul(uts, str2, 2, 2, 2)); + + /* Check endp being NULL */ + ut_asserteq(1099, simple_strtoul(str2, NULL, 0)); + + return 0; +} +STR_TEST(str_simple_strtoul, 0); + +int do_ut_str(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]) +{ + struct unit_test *tests = ll_entry_start(struct unit_test, + str_test); + const int n_ents = ll_entry_count(struct unit_test, str_test); + + return cmd_ut_category("str", "str_", tests, n_ents, argc, argv); +} From patchwork Mon Apr 27 17:02:35 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 238678 List-Id: U-Boot discussion From: sjg at chromium.org (Simon Glass) Date: Mon, 27 Apr 2020 11:02:35 -0600 Subject: [PATCH 2/3] lib: Add a function to convert a string to upper case In-Reply-To: <20200427170236.156351-1-sjg@chromium.org> References: <20200427170236.156351-1-sjg@chromium.org> Message-ID: <20200427170236.156351-3-sjg@chromium.org> Add a helper function for this operation. Update the strtoul() tests to check upper case as well. Signed-off-by: Simon Glass Reviewed-by: Heinrich Schuchardt --- include/vsprintf.h | 12 +++++++ lib/strto.c | 8 +++++ test/str_ut.c | 78 +++++++++++++++++++++++++++++++++++++--------- 3 files changed, 83 insertions(+), 15 deletions(-) diff --git a/include/vsprintf.h b/include/vsprintf.h index 56844dd2de..d9fb68add0 100644 --- a/include/vsprintf.h +++ b/include/vsprintf.h @@ -222,4 +222,16 @@ bool str2long(const char *p, ulong *num); * @hz: Value to convert */ char *strmhz(char *buf, unsigned long hz); + +/** + * str_to_upper() - Convert a string to upper case + * + * This simply uses toupper() on each character of the string. + * + * @in: String to convert (must be large enough to hold the output string) + * @out: Buffer to put converted string + * @len: Number of bytes available in @out (SIZE_MAX for all) + */ +void str_to_upper(const char *in, char *out, size_t len); + #endif diff --git a/lib/strto.c b/lib/strto.c index 1ac2b09c72..37e1fbe63f 100644 --- a/lib/strto.c +++ b/lib/strto.c @@ -176,3 +176,11 @@ long trailing_strtol(const char *str) { return trailing_strtoln(str, NULL); } + +void str_to_upper(const char *in, char *out, size_t len) +{ + for (; len > 0 && *in; len--) + *out++ = toupper(*in++); + if (len) + *out = '\0'; +} diff --git a/test/str_ut.c b/test/str_ut.c index fab8de595c..7c8015050a 100644 --- a/test/str_ut.c +++ b/test/str_ut.c @@ -19,36 +19,84 @@ static const char str3[] = "0xbI'm sorry you're alive."; /* Declare a new str test */ #define STR_TEST(_name, _flags) UNIT_TEST(_name, _flags, str_test) +static int str_test_upper(struct unit_test_state *uts) +{ + char out[TEST_STR_SIZE]; + + /* Make sure it adds a terminator */ + out[strlen(str1)] = 'a'; + str_to_upper(str1, out, SIZE_MAX); + ut_asserteq_str("I'M SORRY I'M LATE.", out); + + /* In-place operation */ + strcpy(out, str2); + str_to_upper(out, out, SIZE_MAX); + ut_asserteq_str("1099ABNO, DON'T BOTHER APOLOGISING.", out); + + /* Limited length */ + str_to_upper(str1, out, 7); + ut_asserteq_str("I'M SORO, DON'T BOTHER APOLOGISING.", out); + + /* In-place with limited length */ + strcpy(out, str2); + str_to_upper(out, out, 7); + ut_asserteq_str("1099ABNo, don't bother apologising.", out); + + /* Copy an empty string to a buffer with space*/ + out[1] = 0x7f; + str_to_upper("", out, SIZE_MAX); + ut_asserteq('\0', *out); + ut_asserteq(0x7f, out[1]); + + /* Copy an empty string to a buffer with no space*/ + out[0] = 0x7f; + str_to_upper("", out, 0); + ut_asserteq(0x7f, out[0]); + + return 0; +} +STR_TEST(str_test_upper, 0); + static int run_strtoul(struct unit_test_state *uts, const char *str, int base, - ulong expect_val, int expect_endp_offset) + ulong expect_val, int expect_endp_offset, bool upper) { + char out[TEST_STR_SIZE]; char *endp; ulong val; - val = simple_strtoul(str, &endp, base); + strcpy(out, str); + if (upper) + str_to_upper(out, out, -1); + + val = simple_strtoul(out, &endp, base); ut_asserteq(expect_val, val); - ut_asserteq(expect_endp_offset, endp - str); + ut_asserteq(expect_endp_offset, endp - out); return 0; } static int str_simple_strtoul(struct unit_test_state *uts) { - /* Base 10 and base 16 */ - ut_assertok(run_strtoul(uts, str2, 10, 1099, 4)); - ut_assertok(run_strtoul(uts, str2, 16, 0x1099ab, 6)); + int upper; + + /* Check that it is case-insentive */ + for (upper = 0; upper < 2; upper++) { + /* Base 10 and base 16 */ + ut_assertok(run_strtoul(uts, str2, 10, 1099, 4, upper)); + ut_assertok(run_strtoul(uts, str2, 16, 0x1099ab, 6, upper)); - /* Invalid string */ - ut_assertok(run_strtoul(uts, str1, 10, 0, 0)); + /* Invalid string */ + ut_assertok(run_strtoul(uts, str1, 10, 0, 0, upper)); - /* Base 0 */ - ut_assertok(run_strtoul(uts, str1, 0, 0, 0)); - ut_assertok(run_strtoul(uts, str2, 0, 1099, 4)); - ut_assertok(run_strtoul(uts, str3, 0, 0xb, 3)); + /* Base 0 */ + ut_assertok(run_strtoul(uts, str1, 0, 0, 0, upper)); + ut_assertok(run_strtoul(uts, str2, 0, 1099, 4, upper)); + ut_assertok(run_strtoul(uts, str3, 0, 0xb, 3, upper)); - /* Base 2 */ - ut_assertok(run_strtoul(uts, str1, 2, 0, 0)); - ut_assertok(run_strtoul(uts, str2, 2, 2, 2)); + /* Base 2 */ + ut_assertok(run_strtoul(uts, str1, 2, 0, 0, upper)); + ut_assertok(run_strtoul(uts, str2, 2, 2, 2, upper)); + } /* Check endp being NULL */ ut_asserteq(1099, simple_strtoul(str2, NULL, 0)); From patchwork Mon Apr 27 17:02:36 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 238677 List-Id: U-Boot discussion From: sjg at chromium.org (Simon Glass) Date: Mon, 27 Apr 2020 11:02:36 -0600 Subject: [PATCH 3/3] acpi: Fix-up patch to correct sandbox test errors In-Reply-To: <20200427170236.156351-1-sjg@chromium.org> References: <20200427170236.156351-1-sjg@chromium.org> Message-ID: <20200427170236.156351-4-sjg@chromium.org> Move the alignment code into acpi_setup_base_tables() so that test and production code are in alignment. This brings x86/master into line with patch series v8. Signed-off-by: Simon Glass --- arch/x86/lib/acpi_table.c | 5 ----- lib/acpi/acpi_table.c | 7 ++++++- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/arch/x86/lib/acpi_table.c b/arch/x86/lib/acpi_table.c index 600bde2f5f..13f1409de8 100644 --- a/arch/x86/lib/acpi_table.c +++ b/arch/x86/lib/acpi_table.c @@ -375,11 +375,6 @@ ulong write_acpi_tables(ulong start_addr) debug("ACPI: Writing ACPI tables at %lx\n", start_addr); acpi_setup_base_tables(ctx, start); - /* - * Per ACPI spec, the FACS table address must be aligned to a 64 byte - * boundary (Windows checks this, but Linux does not). - */ - acpi_align64(ctx); debug("ACPI: * FACS\n"); facs = ctx->current; diff --git a/lib/acpi/acpi_table.c b/lib/acpi/acpi_table.c index 5abf1cad50..1c253af3bf 100644 --- a/lib/acpi/acpi_table.c +++ b/lib/acpi/acpi_table.c @@ -145,7 +145,7 @@ int acpi_add_table(struct acpi_ctx *ctx, void *table) } if (i >= entries_num) { - debug("ACPI: Error: too many tables\n"); + log_err("ACPI: Error: too many tables\n"); return -E2BIG; } @@ -256,4 +256,9 @@ void acpi_setup_base_tables(struct acpi_ctx *ctx, void *start) acpi_write_rsdp(ctx->rsdp, ctx->rsdt, ctx->xsdt); acpi_write_rsdt(ctx->rsdt); acpi_write_xsdt(ctx->xsdt); + /* + * Per ACPI spec, the FACS table address must be aligned to a 64 byte + * boundary (Windows checks this, but Linux does not). + */ + acpi_align64(ctx); }