[U-Boot] [PATCH v4 5/6] cmd:gpt: randomly generate each partition uuid if undefined

Przemyslaw Marczak p.marczak at samsung.com
Wed Mar 19 18:58:30 CET 2014


Changes:
- randomly generate partition uuid if any is undefined and CONFIG_RAND_UUID
  is defined
- print debug info about set/unset/generated uuid
- update doc/README.gpt

Signed-off-by: Przemyslaw Marczak <p.marczak at samsung.com>
Acked-by: Lukasz Majewski <l.majewski at samsung.com>
Cc: Piotr Wilczek <p.wilczek at samsung.com>
Cc: Tom Rini <trini at ti.com>
Cc: Stephen Warren <swarren at nvidia.com>
Cc: Lukasz Majewski <l.majewski at samsung.com>

---
Changes v2:
- cmd_gpt: extract_env: change return type from char to int
- add tmp array to generate uuid string
- store generated uuid in env and next get it from it - don't need to alloc
  and maintain allcoated memory outside extract_env()

Changes v3:
- print info if uuid_gpt_* env is get from env/set random
- change some word in README.gpt to meaningful

Changes v4:
- change printf/puts to debug
- reduce indentation level in extract_env()
- generate rand uuid if CONFIG_RAND_UUID is defined
---
 common/cmd_gpt.c | 62 +++++++++++++++++++++++++++++++++++++++++---------------
 doc/README.gpt   | 24 ++++++++++++++++------
 lib/uuid.c       |  4 ++--
 3 files changed, 66 insertions(+), 24 deletions(-)

diff --git a/common/cmd_gpt.c b/common/cmd_gpt.c
index 1f12e6d..4048b77 100644
--- a/common/cmd_gpt.c
+++ b/common/cmd_gpt.c
@@ -29,30 +29,52 @@
  *
  * @return - zero on successful expand and env is set
  */
-static char extract_env(const char *str, char **env)
+static int extract_env(const char *str, char **env)
 {
+	int ret = -1;
 	char *e, *s;
-
+#ifdef CONFIG_RANDOM_UUID
+	char uuid_str[UUID_STR_LEN + 1];
+#endif
 	if (!str || strlen(str) < 4)
 		return -1;
 
-	if ((strncmp(str, "${", 2) == 0) && (str[strlen(str) - 1] == '}')) {
-		s = strdup(str);
-		if (s == NULL)
-			return -1;
-		memset(s + strlen(s) - 1, '\0', 1);
-		memmove(s, s + 2, strlen(s) - 1);
+	if (!((strncmp(str, "${", 2) == 0) && (str[strlen(str) - 1] == '}')))
+		return -1;
+
+	s = strdup(str);
+	if (s == NULL)
+		return -1;
+
+	memset(s + strlen(s) - 1, '\0', 1);
+	memmove(s, s + 2, strlen(s) - 1);
+
+	e = getenv(s);
+	if (e == NULL) {
+#ifdef CONFIG_RANDOM_UUID
+		debug("%s unset. ", str);
+		gen_rand_uuid_str(uuid_str, UUID_STR_FORMAT_STD);
+		setenv(s, uuid_str);
+
 		e = getenv(s);
-		free(s);
-		if (e == NULL) {
-			printf("Environmental '%s' not set\n", str);
-			return -1; /* env not set */
+		if (e) {
+			debug("Set to random.\n");
+			ret = 0;
+		} else {
+			debug("Can't get random UUID.\n");
 		}
-		*env = e;
-		return 0;
+#else
+		debug("%s unset.\n", str);
+#endif
+	} else {
+		debug("%s get from environment.\n", str);
+		ret = 0;
 	}
 
-	return -1;
+	*env = e;
+	free(s);
+
+	return ret;
 }
 
 /**
@@ -299,8 +321,16 @@ static int do_gpt(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 			return CMD_RET_FAILURE;
 		}
 
-		if (gpt_default(blk_dev_desc, argv[4]))
+		puts("Writing GPT: ");
+
+		ret = gpt_default(blk_dev_desc, argv[4]);
+		if (!ret) {
+			puts("success!\n");
+			return CMD_RET_SUCCESS;
+		} else {
+			puts("error!\n");
 			return CMD_RET_FAILURE;
+		}
 	} else {
 		return CMD_RET_USAGE;
 	}
diff --git a/doc/README.gpt b/doc/README.gpt
index 5c133f3..51515c8 100644
--- a/doc/README.gpt
+++ b/doc/README.gpt
@@ -101,7 +101,7 @@ Offset  Size    Description
 40      8 B     First usable LBA for partitions (primary partition table last
 		LBA + 1)
 48      8 B     Last usable LBA (secondary partition table first LBA - 1)
-56      16 B    Disk GUID (also referred as UUID on UNIXes)
+56      16 B    Disk GUID (also referred as UUID on UNIXes) in big endian
 72      8 B     Partition entries starting LBA (always 2 in primary copy)
 80      4 B     Number of partition entries
 84      4 B     Size of a partition entry (usually 128)
@@ -132,8 +132,8 @@ of the Primary.
 	   ----------------------
 	   Offset  Size    Description
 
-	   0       16 B    Partition type GUID
-	   16      16 B    Unique partition GUID
+	   0       16 B    Partition type GUID (Big Endian)
+	   16      16 B    Unique partition GUID in (Big Endian)
 	   32      8  B    First LBA (Little Endian)
 	   40      8  B    Last LBA (inclusive)
 	   48      8  B    Attribute flags [+]
@@ -160,6 +160,9 @@ To restore GUID partition table one needs to:
    Fields 'name', 'size' and 'uuid' are mandatory for every partition.
    The field 'start' is optional.
 
+   option: CONFIG_RANDOM_UUID
+   If any partition "uuid" no exists then it is randomly generated.
+
 2. Define 'CONFIG_EFI_PARTITION' and 'CONFIG_CMD_GPT'
 
 2. From u-boot prompt type:
@@ -168,11 +171,20 @@ To restore GUID partition table one needs to:
 Useful info:
 ============
 
-Two programs, namely: 'fdisk' and 'parted' are recommended to work with GPT
-recovery. Parted is able to handle GUID partitions. Unfortunately the 'fdisk'
-hasn't got such ability.
+Two programs, namely: 'gdisk' and 'parted' are recommended to work with GPT
+recovery. Both are able to handle GUID partitions.
 Please, pay attention at -l switch for parted.
 
 "uuid" program is recommended to generate UUID string. Moreover it can decode
 (-d switch) passed in UUID string. It can be used to generate partitions UUID
 passed to u-boot environment variables.
+If optional CONFIG_RANDOM_UUID is defined then for any partition which environment
+uuid is unset, uuid is randomly generated and stored in correspond environment
+variable.
+
+note:
+Each string block of UUID generated by program "uuid" is in big endian and it is
+also stored in big endian in disk GPT.
+Partitions layout can be printed by typing "mmc part". Note that each partition
+GUID has different byte order than UUID generated before, this is because first
+three blocks of GUID string are in Little Endian.
diff --git a/lib/uuid.c b/lib/uuid.c
index 0112b03..a15fd0d 100644
--- a/lib/uuid.c
+++ b/lib/uuid.c
@@ -252,5 +252,5 @@ U_BOOT_CMD(guid, CONFIG_SYS_MAXARGS, 1, do_uuid,
 	"varname: for set result in a environment variable\n"
 	"e.g. guid guid_env"
 );
-#endif
-#endif
+#endif /* CONFIG_CMD_UUID */
+#endif /* CONFIG_RANDOM_UUID || CONFIG_CMD_UUID */
-- 
1.9.0



More information about the U-Boot mailing list