Those commands basis on implementation of random UUID generator version 4
which is described in RFC4122. The same algorithm is used for generation
both ids but string representation is different as below.

char:  0        9    14   19   24         36
       xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
UUID:     be     be   be   be       be
GUID:     le     le   le   be       be

Commands usage:
- uuid <varname(optional)>
- guid <varname(optional)>

The result is saved in environment as a "varname" variable if argument is given,
if not then its printed.

New config:
- CONFIG_CMD_UUID

Signed-off-by: Przemyslaw Marczak <p.marc...@samsung.com>
Cc: Stephen Warren <swar...@nvidia.com>
Cc: Lukasz Majewski <l.majew...@samsung.com>
Cc: tr...@ti.com

---
Changes v4:
- new commit
---
 README           |  2 +-
 include/common.h |  4 +++-
 lib/Makefile     |  2 ++
 lib/uuid.c       | 44 +++++++++++++++++++++++++++++++++++++++++++-
 4 files changed, 49 insertions(+), 3 deletions(-)

diff --git a/README b/README
index 216f0c7..f69b94e 100644
--- a/README
+++ b/README
@@ -1012,7 +1012,7 @@ The following options need to be configured:
                CONFIG_CMD_CDP          * Cisco Discover Protocol support
                CONFIG_CMD_MFSL         * Microblaze FSL support
                CONFIG_CMD_XIMG           Load part of Multi Image
-
+               CONFIG_CMD_UUID         * Generate random UUID or GUID string
 
                EXAMPLE: If you want all functions except of network
                support you can write:
diff --git a/include/common.h b/include/common.h
index deb08f2..de748f1 100644
--- a/include/common.h
+++ b/include/common.h
@@ -831,7 +831,9 @@ char *      strmhz(char *buf, unsigned long hz);
 #if defined(CONFIG_RANDOM_MACADDR) || \
        defined(CONFIG_BOOTP_RANDOM_DELAY) || \
        defined(CONFIG_CMD_LINK_LOCAL) || \
-       defined(CONFIG_RANDOM_UUID)
+       defined(CONFIG_RANDOM_UUID) || \
+       defined(CONFIG_CMD_UUID)
+
 #define RAND_MAX -1U
 void srand(unsigned int seed);
 unsigned int rand(void);
diff --git a/lib/Makefile b/lib/Makefile
index fd75e80..b85c825 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -65,6 +65,8 @@ obj-$(CONFIG_PARTITION_UUIDS) += uuid.o
 obj-y += vsprintf.o
 obj-$(CONFIG_RANDOM_UUID) += uuid.o
 obj-$(CONFIG_RANDOM_UUID) += rand.o
+obj-$(CONFIG_CMD_UUID) += uuid.o
+obj-$(CONFIG_CMD_UUID) += rand.o
 obj-$(CONFIG_RANDOM_MACADDR) += rand.o
 obj-$(CONFIG_BOOTP_RANDOM_DELAY) += rand.o
 obj-$(CONFIG_CMD_LINK_LOCAL) += rand.o
diff --git a/lib/uuid.c b/lib/uuid.c
index d3ba60e..0112b03 100644
--- a/lib/uuid.c
+++ b/lib/uuid.c
@@ -4,6 +4,7 @@
  * SPDX-License-Identifier:    GPL-2.0+
  */
 
+#include <common.h>
 #include <linux/ctype.h>
 #include <errno.h>
 #include <common.h>
@@ -172,7 +173,7 @@ void uuid_bin_to_str(unsigned char *uuid_bin, char 
*uuid_str,
  *
  * @param uuid_bin - pointer to allocated array [16B]. Output is in big endian.
 */
-#ifdef CONFIG_RANDOM_UUID
+#if defined(CONFIG_RANDOM_UUID) || defined(CONFIG_CMD_UUID)
 void gen_rand_uuid(unsigned char *uuid_bin)
 {
        struct uuid uuid;
@@ -211,4 +212,45 @@ void gen_rand_uuid_str(char *uuid_str, uuid_str_t 
str_format)
        /* Convert UUID bin to UUID or GUID formated STRING  */
        uuid_bin_to_str(uuid_bin, uuid_str, str_format);
 }
+
+#ifdef CONFIG_CMD_UUID
+int do_uuid(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+{
+       char uuid[UUID_STR_LEN + 1];
+       uuid_str_t str_format;
+
+       if (!strcmp(argv[0], "uuid"))
+               str_format = UUID_STR_FORMAT_STD;
+       else
+               str_format = UUID_STR_FORMAT_GUID;
+
+       if (argc == 1) {
+               gen_rand_uuid_str(uuid, str_format);
+               printf("%s\n", uuid);
+       } else if (argc == 2) {
+               gen_rand_uuid_str(uuid, str_format);
+               setenv(argv[1], uuid);
+       } else {
+               return CMD_RET_USAGE;
+       }
+
+       return CMD_RET_SUCCESS;
+}
+
+U_BOOT_CMD(uuid, CONFIG_SYS_MAXARGS, 1, do_uuid,
+       "UUID - generate Universally Unique Identifier version 4",
+       "<varname(optional)>\n"
+       "Argument:\n"
+       "varname: for set result in a environment variable\n"
+       "e.g. uuid uuid_env"
+);
+
+U_BOOT_CMD(guid, CONFIG_SYS_MAXARGS, 1, do_uuid,
+       "GUID - generate Globally Unique Identifier based on UUID version 4",
+       "<varname(optional)>\n"
+       "Argument:\n"
+       "varname: for set result in a environment variable\n"
+       "e.g. guid guid_env"
+);
+#endif
 #endif
-- 
1.9.0

_______________________________________________
U-Boot mailing list
U-Boot@lists.denx.de
http://lists.denx.de/mailman/listinfo/u-boot

Reply via email to