Commit 39206382dea929b6a834c212b431821e06e68f39

Authored by Przemyslaw Marczak
Committed by Tom Rini
1 parent 89c8230dec

cmd:gpt: randomly generate each partition uuid if undefined

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@samsung.com>
Acked-by: Lukasz Majewski <l.majewski@samsung.com>
Cc: Piotr Wilczek <p.wilczek@samsung.com>
Cc: Tom Rini <trini@ti.com>
Cc: Stephen Warren <swarren@nvidia.com>
Cc: Lukasz Majewski <l.majewski@samsung.com>

Showing 3 changed files with 65 additions and 22 deletions Side-by-side Diff

... ... @@ -29,30 +29,53 @@
29 29 *
30 30 * @return - zero on successful expand and env is set
31 31 */
32   -static char extract_env(const char *str, char **env)
  32 +static int extract_env(const char *str, char **env)
33 33 {
  34 + int ret = -1;
34 35 char *e, *s;
  36 +#ifdef CONFIG_RANDOM_UUID
  37 + char uuid_str[UUID_STR_LEN + 1];
  38 +#endif
35 39  
36 40 if (!str || strlen(str) < 4)
37 41 return -1;
38 42  
39   - if ((strncmp(str, "${", 2) == 0) && (str[strlen(str) - 1] == '}')) {
40   - s = strdup(str);
41   - if (s == NULL)
42   - return -1;
43   - memset(s + strlen(s) - 1, '\0', 1);
44   - memmove(s, s + 2, strlen(s) - 1);
  43 + if (!((strncmp(str, "${", 2) == 0) && (str[strlen(str) - 1] == '}')))
  44 + return -1;
  45 +
  46 + s = strdup(str);
  47 + if (s == NULL)
  48 + return -1;
  49 +
  50 + memset(s + strlen(s) - 1, '\0', 1);
  51 + memmove(s, s + 2, strlen(s) - 1);
  52 +
  53 + e = getenv(s);
  54 + if (e == NULL) {
  55 +#ifdef CONFIG_RANDOM_UUID
  56 + debug("%s unset. ", str);
  57 + gen_rand_uuid_str(uuid_str, UUID_STR_FORMAT_STD);
  58 + setenv(s, uuid_str);
  59 +
45 60 e = getenv(s);
46   - free(s);
47   - if (e == NULL) {
48   - printf("Environmental '%s' not set\n", str);
49   - return -1; /* env not set */
  61 + if (e) {
  62 + debug("Set to random.\n");
  63 + ret = 0;
  64 + } else {
  65 + debug("Can't get random UUID.\n");
50 66 }
51   - *env = e;
52   - return 0;
  67 +#else
  68 + debug("%s unset.\n", str);
  69 +#endif
  70 + } else {
  71 + debug("%s get from environment.\n", str);
  72 + ret = 0;
53 73 }
54 74  
55   - return -1;
  75 + *env = e;
  76 + free(s);
  77 +
  78 + return ret;
56 79 }
57 80  
58 81 /**
59 82  
... ... @@ -299,8 +322,16 @@
299 322 return CMD_RET_FAILURE;
300 323 }
301 324  
302   - if (gpt_default(blk_dev_desc, argv[4]))
  325 + puts("Writing GPT: ");
  326 +
  327 + ret = gpt_default(blk_dev_desc, argv[4]);
  328 + if (!ret) {
  329 + puts("success!\n");
  330 + return CMD_RET_SUCCESS;
  331 + } else {
  332 + puts("error!\n");
303 333 return CMD_RET_FAILURE;
  334 + }
304 335 } else {
305 336 return CMD_RET_USAGE;
306 337 }
... ... @@ -132,8 +132,8 @@
132 132 ----------------------
133 133 Offset Size Description
134 134  
135   - 0 16 B Partition type GUID
136   - 16 16 B Unique partition GUID
  135 + 0 16 B Partition type GUID (Big Endian)
  136 + 16 16 B Unique partition GUID in (Big Endian)
137 137 32 8 B First LBA (Little Endian)
138 138 40 8 B Last LBA (inclusive)
139 139 48 8 B Attribute flags [+]
... ... @@ -160,6 +160,9 @@
160 160 Fields 'name', 'size' and 'uuid' are mandatory for every partition.
161 161 The field 'start' is optional.
162 162  
  163 + option: CONFIG_RANDOM_UUID
  164 + If any partition "UUID" no exists then it is randomly generated.
  165 +
163 166 2. Define 'CONFIG_EFI_PARTITION' and 'CONFIG_CMD_GPT'
164 167  
165 168 2. From u-boot prompt type:
166 169  
... ... @@ -168,12 +171,21 @@
168 171 Useful info:
169 172 ============
170 173  
171   -Two programs, namely: 'fdisk' and 'parted' are recommended to work with GPT
172   -recovery. Parted is able to handle GUID partitions. Unfortunately the 'fdisk'
173   -hasn't got such ability.
  174 +Two programs, namely: 'gdisk' and 'parted' are recommended to work with GPT
  175 +recovery. Both are able to handle GUID partitions.
174 176 Please, pay attention at -l switch for parted.
175 177  
176 178 "uuid" program is recommended to generate UUID string. Moreover it can decode
177 179 (-d switch) passed in UUID string. It can be used to generate partitions UUID
178 180 passed to u-boot environment variables.
  181 +If optional CONFIG_RANDOM_UUID is defined then for any partition which environment
  182 +uuid is unset, uuid is randomly generated and stored in correspond environment
  183 +variable.
  184 +
  185 +note:
  186 +Each string block of UUID generated by program "uuid" is in big endian and it is
  187 +also stored in big endian in disk GPT.
  188 +Partitions layout can be printed by typing "mmc part". Note that each partition
  189 +GUID has different byte order than UUID generated before, this is because first
  190 +three blocks of GUID string are in Little Endian.
... ... @@ -251,6 +251,6 @@
251 251 "varname: for set result in a environment variable\n"
252 252 "e.g. guid guid_env"
253 253 );
254   -#endif
255   -#endif
  254 +#endif /* CONFIG_CMD_UUID */
  255 +#endif /* CONFIG_RANDOM_UUID || CONFIG_CMD_UUID */