From patchwork Wed Apr 8 14:32:55 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 237412 List-Id: U-Boot discussion From: sjg at chromium.org (Simon Glass) Date: Wed, 8 Apr 2020 08:32:55 -0600 Subject: [PATCH v5 1/6] test: Add the beginnings of some string tests In-Reply-To: <20200408143300.52636-1-sjg@chromium.org> References: <20200408143300.52636-1-sjg@chromium.org> Message-ID: <20200408083251.v5.1.I9544b8bcada571406f30889eda869246c2b7cc7e@changeid> 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 --- Changes in v5: None Changes in v4: - Add a new patch with some string tests 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 0748185eaf7..6d4270fa33b 100644 --- a/include/test/suites.h +++ b/include/test/suites.h @@ -32,6 +32,7 @@ int do_ut_env(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]); int do_ut_lib(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 2fe41f489c3..917e54a3fcc 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-$(CONFIG_$(SPL_)LOG) += log/ diff --git a/test/cmd_ut.c b/test/cmd_ut.c index a3a9d49f7ec..b342c35e68e 100644 --- a/test/cmd_ut.c +++ b/test/cmd_ut.c @@ -71,6 +71,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 }; @@ -131,6 +133,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 00000000000..fab8de595cb --- /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 Wed Apr 8 14:32:56 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 237413 List-Id: U-Boot discussion From: sjg at chromium.org (Simon Glass) Date: Wed, 8 Apr 2020 08:32:56 -0600 Subject: [PATCH v5 2/6] lib: Add a function to convert a string to upper case In-Reply-To: <20200408143300.52636-1-sjg@chromium.org> References: <20200408143300.52636-1-sjg@chromium.org> Message-ID: <20200408083251.v5.2.I1ca01985ee4914667e7a2ba006de71c2546eada3@changeid> 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 --- Changes in v5: - Drop change to FAT - Add new tests for copying an empty string - Use size_t instead of int, require caller to use SIZE_MAX - Update the algorithm to avoid dealing with -1 Changes in v4: - Add a new patch to convert a string to upper case 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 56844dd2de8..d9fb68add0c 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 55ff9f7437d..c00bb5895df 100644 --- a/lib/strto.c +++ b/lib/strto.c @@ -163,3 +163,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 fab8de595cb..7c8015050ad 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 Wed Apr 8 14:32:57 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 237419 List-Id: U-Boot discussion From: sjg at chromium.org (Simon Glass) Date: Wed, 8 Apr 2020 08:32:57 -0600 Subject: [PATCH v5 3/6] usb: Update struct usb_device to indicate speed enum In-Reply-To: <20200408143300.52636-1-sjg@chromium.org> References: <20200408143300.52636-1-sjg@chromium.org> Message-ID: <20200408083251.v5.3.I0fd829dedf499bc0b05be5e4eb590ce829222962@changeid> The speed member actually uses an enum, so add this to the comment. Signed-off-by: Simon Glass --- Changes in v5: None Changes in v4: None include/usb.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/usb.h b/include/usb.h index efb67ea33ff..7fa8182ba86 100644 --- a/include/usb.h +++ b/include/usb.h @@ -103,7 +103,7 @@ enum { */ struct usb_device { int devnum; /* Device number on USB bus */ - int speed; /* full/low/high */ + int speed; /* enum usb_device_speed */ char mf[32]; /* manufacturer */ char prod[32]; /* product */ char serial[32]; /* serial number */ From patchwork Wed Apr 8 14:32:58 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 237418 List-Id: U-Boot discussion From: sjg at chromium.org (Simon Glass) Date: Wed, 8 Apr 2020 08:32:58 -0600 Subject: [PATCH v5 4/6] uuid: Use const char * where possible In-Reply-To: <20200408143300.52636-1-sjg@chromium.org> References: <20200408143300.52636-1-sjg@chromium.org> Message-ID: <20200408083251.v5.4.I6c2cc3ee6daf9e48e3b5dc49d5386f9c0bca2a4a@changeid> Update the arguments of these functions so they can be called from code which uses constant strings. Signed-off-by: Simon Glass --- Changes in v5: None Changes in v4: None include/uuid.h | 8 +++++--- lib/uuid.c | 6 ++++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/include/uuid.h b/include/uuid.h index abcc325eae9..73c5a89ec7c 100644 --- a/include/uuid.h +++ b/include/uuid.h @@ -35,11 +35,13 @@ struct uuid { #define UUID_VARIANT 0x1 int uuid_str_valid(const char *uuid); -int uuid_str_to_bin(char *uuid_str, unsigned char *uuid_bin, int str_format); -void uuid_bin_to_str(unsigned char *uuid_bin, char *uuid_str, int str_format); +int uuid_str_to_bin(const char *uuid_str, unsigned char *uuid_bin, + int str_format); +void uuid_bin_to_str(const unsigned char *uuid_bin, char *uuid_str, + int str_format); #ifdef CONFIG_PARTITION_TYPE_GUID int uuid_guid_get_bin(const char *guid_str, unsigned char *guid_bin); -int uuid_guid_get_str(unsigned char *guid_bin, char *guid_str); +int uuid_guid_get_str(const unsigned char *guid_bin, char *guid_str); #endif void gen_rand_uuid(unsigned char *uuid_bin); void gen_rand_uuid_str(char *uuid_str, int str_format); diff --git a/lib/uuid.c b/lib/uuid.c index 3d3c7abcaea..03edaf8b6cf 100644 --- a/lib/uuid.c +++ b/lib/uuid.c @@ -143,7 +143,8 @@ int uuid_guid_get_str(unsigned char *guid_bin, char *guid_str) * @param uuid_bin - pointer to allocated array for big endian output [16B] * @str_format - UUID string format: 0 - UUID; 1 - GUID */ -int uuid_str_to_bin(char *uuid_str, unsigned char *uuid_bin, int str_format) +int uuid_str_to_bin(const char *uuid_str, unsigned char *uuid_bin, + int str_format) { uint16_t tmp16; uint32_t tmp32; @@ -194,7 +195,8 @@ int uuid_str_to_bin(char *uuid_str, unsigned char *uuid_bin, int str_format) * @str_format: bit 0: 0 - UUID; 1 - GUID * bit 1: 0 - lower case; 2 - upper case */ -void uuid_bin_to_str(unsigned char *uuid_bin, char *uuid_str, int str_format) +void uuid_bin_to_str(const unsigned char *uuid_bin, char *uuid_str, + int str_format) { const u8 uuid_char_order[UUID_BIN_LEN] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}; From patchwork Wed Apr 8 14:32:59 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 237416 List-Id: U-Boot discussion From: sjg at chromium.org (Simon Glass) Date: Wed, 8 Apr 2020 08:32:59 -0600 Subject: [PATCH v5 5/6] pci: Add a macro to convert BDF from linux to U-Boot In-Reply-To: <20200408143300.52636-1-sjg@chromium.org> References: <20200408143300.52636-1-sjg@chromium.org> Message-ID: <20200408083251.v5.5.Ifb213e62095edbb017224d889263bb63c982c112@changeid> U-Boot's BDF format has its bits in the same position as the device tree PCI definition. Some x86 devices use linux format in their register format and it is useful to be able to convert to U-Boot format. Add a macro for this. Signed-off-by: Simon Glass --- Changes in v5: None Changes in v4: None include/pci.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/include/pci.h b/include/pci.h index 174ddd4460d..aff56b24f92 100644 --- a/include/pci.h +++ b/include/pci.h @@ -543,6 +543,9 @@ typedef int pci_dev_t; #define PCI_VENDEV(v, d) (((v) << 16) | (d)) #define PCI_ANY_ID (~0) +/* Convert from Linux format to U-Boot format */ +#define PCI_TO_BDF(val) ((val) << 8) + struct pci_device_id { unsigned int vendor, device; /* Vendor and device ID or PCI_ANY_ID */ unsigned int subvendor, subdevice; /* Subsystem ID's or PCI_ANY_ID */ From patchwork Wed Apr 8 14:33:00 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 237415 List-Id: U-Boot discussion From: sjg at chromium.org (Simon Glass) Date: Wed, 8 Apr 2020 08:33:00 -0600 Subject: [PATCH v5 6/6] dm: mmc: Update mmc_get_mmc_dev() to use const * In-Reply-To: <20200408143300.52636-1-sjg@chromium.org> References: <20200408143300.52636-1-sjg@chromium.org> Message-ID: <20200408083251.v5.6.Ic20e826b6d1a3ee0654b6064459d7beee3722309@changeid> This function does not modify the device to change it to use const *, so that callers with a const udevice * can call it without a cast. Signed-off-by: Simon Glass Reviewed-by: Jaehoon Chung --- Changes in v5: None Changes in v4: None drivers/mmc/mmc-uclass.c | 2 +- include/mmc.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/mmc/mmc-uclass.c b/drivers/mmc/mmc-uclass.c index c75892a72c1..88fc7d79f8e 100644 --- a/drivers/mmc/mmc-uclass.c +++ b/drivers/mmc/mmc-uclass.c @@ -223,7 +223,7 @@ int mmc_of_parse(struct udevice *dev, struct mmc_config *cfg) return 0; } -struct mmc *mmc_get_mmc_dev(struct udevice *dev) +struct mmc *mmc_get_mmc_dev(const struct udevice *dev) { struct mmc_uclass_priv *upriv; diff --git a/include/mmc.h b/include/mmc.h index e83c22423bf..0f9184b9714 100644 --- a/include/mmc.h +++ b/include/mmc.h @@ -374,7 +374,7 @@ struct mmc_uclass_priv { * @dev: Device * @return associated mmc struct pointer if available, else NULL */ -struct mmc *mmc_get_mmc_dev(struct udevice *dev); +struct mmc *mmc_get_mmc_dev(const struct udevice *dev); /* End of driver model support */