Commit 89c8230dec063d894aec1a7b5c58f1dcadced738

Authored by Przemyslaw Marczak
Committed by Tom Rini
1 parent 4e4815feae

new commands: uuid and guid - generate random unique identifier

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>]
- guid [<varname>]

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

New config:
- CONFIG_CMD_UUID

Signed-off-by: Przemyslaw Marczak <p.marczak@samsung.com>
Cc: Stephen Warren <swarren@nvidia.com>
Cc: Lukasz Majewski <l.majewski@samsung.com>
Cc: trini@ti.com

Showing 3 changed files with 49 additions and 5 deletions Side-by-side Diff

... ... @@ -1012,7 +1012,7 @@
1012 1012 CONFIG_CMD_CDP * Cisco Discover Protocol support
1013 1013 CONFIG_CMD_MFSL * Microblaze FSL support
1014 1014 CONFIG_CMD_XIMG Load part of Multi Image
1015   -
  1015 + CONFIG_CMD_UUID * Generate random UUID or GUID string
1016 1016  
1017 1017 EXAMPLE: If you want all functions except of network
1018 1018 support you can write:
include/config_fallbacks.h
... ... @@ -58,14 +58,16 @@
58 58 #if (defined(CONFIG_PARTITION_UUIDS) || \
59 59 defined(CONFIG_EFI_PARTITION) || \
60 60 defined(CONFIG_RANDOM_UUID) || \
  61 + defined(CONFIG_CMD_UUID) || \
61 62 defined(CONFIG_BOOTP_PXE)) && \
62 63 !defined(CONFIG_LIB_UUID)
63 64 #define CONFIG_LIB_UUID
64 65 #endif
65 66  
66   -#if defined(CONFIG_RANDOM_UUID) && \
67   - !defined(CONFIG_LIB_RAND) && \
68   - !defined(CONFIG_LIB_HW_RAND)
  67 +#if (defined(CONFIG_RANDOM_UUID) || \
  68 + defined(CONFIG_CMD_UUID)) && \
  69 + (!defined(CONFIG_LIB_RAND) && \
  70 + !defined(CONFIG_LIB_HW_RAND))
69 71 #define CONFIG_LIB_RAND
70 72 #endif
71 73  
... ... @@ -4,6 +4,7 @@
4 4 * SPDX-License-Identifier: GPL-2.0+
5 5 */
6 6  
  7 +#include <common.h>
7 8 #include <linux/ctype.h>
8 9 #include <errno.h>
9 10 #include <common.h>
... ... @@ -171,7 +172,7 @@
171 172 *
172 173 * @param uuid_bin - pointer to allocated array [16B]. Output is in big endian.
173 174 */
174   -#ifdef CONFIG_RANDOM_UUID
  175 +#if defined(CONFIG_RANDOM_UUID) || defined(CONFIG_CMD_UUID)
175 176 void gen_rand_uuid(unsigned char *uuid_bin)
176 177 {
177 178 struct uuid uuid;
... ... @@ -210,5 +211,46 @@
210 211 /* Convert UUID bin to UUID or GUID formated STRING */
211 212 uuid_bin_to_str(uuid_bin, uuid_str, str_format);
212 213 }
  214 +
  215 +#ifdef CONFIG_CMD_UUID
  216 +int do_uuid(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  217 +{
  218 + char uuid[UUID_STR_LEN + 1];
  219 + int str_format;
  220 +
  221 + if (!strcmp(argv[0], "uuid"))
  222 + str_format = UUID_STR_FORMAT_STD;
  223 + else
  224 + str_format = UUID_STR_FORMAT_GUID;
  225 +
  226 + if (argc > 2)
  227 + return CMD_RET_USAGE;
  228 +
  229 + gen_rand_uuid_str(uuid, str_format);
  230 +
  231 + if (argc == 1)
  232 + printf("%s\n", uuid);
  233 + else
  234 + setenv(argv[1], uuid);
  235 +
  236 + return CMD_RET_SUCCESS;
  237 +}
  238 +
  239 +U_BOOT_CMD(uuid, CONFIG_SYS_MAXARGS, 1, do_uuid,
  240 + "UUID - generate random Universally Unique Identifier",
  241 + "[<varname>]\n"
  242 + "Argument:\n"
  243 + "varname: for set result in a environment variable\n"
  244 + "e.g. uuid uuid_env"
  245 +);
  246 +
  247 +U_BOOT_CMD(guid, CONFIG_SYS_MAXARGS, 1, do_uuid,
  248 + "GUID - generate Globally Unique Identifier based on random UUID",
  249 + "[<varname>]\n"
  250 + "Argument:\n"
  251 + "varname: for set result in a environment variable\n"
  252 + "e.g. guid guid_env"
  253 +);
  254 +#endif
213 255 #endif