x86: ivybridge: Use syscon for the GMA device
[oweals/u-boot.git] / common / cmd_gpt.c
index da7705da6973371df4949cd3a36e2eac7848c5e9..d94d5530bc6fab6194a92cdd4b089dbbcf1f24af 100644 (file)
@@ -1,32 +1,24 @@
 /*
  * cmd_gpt.c -- GPT (GUID Partition Table) handling command
  *
+ * Copyright (C) 2015
+ * Lukasz Majewski <l.majewski@majess.pl>
+ *
  * Copyright (C) 2012 Samsung Electronics
  * author: Lukasz Majewski <l.majewski@samsung.com>
  * author: Piotr Wilczek <p.wilczek@samsung.com>
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ * SPDX-License-Identifier:    GPL-2.0+
  */
 
 #include <common.h>
 #include <malloc.h>
 #include <command.h>
-#include <mmc.h>
 #include <part_efi.h>
 #include <exports.h>
 #include <linux/ctype.h>
+#include <div64.h>
+#include <memalign.h>
 
 #ifndef CONFIG_PARTITION_UUIDS
 #error CONFIG_PARTITION_UUIDS must be enabled for CONFIG_CMD_GPT to be enabled
  *
  * @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;
 }
 
 /**
@@ -106,6 +121,40 @@ static char *extract_val(const char *str, const char *key)
        return new;
 }
 
+/**
+ * found_key(): Found key without value in parameter list (comma separated).
+ *
+ * @param str - pointer to string with key
+ * @param key - pointer to the key to search for
+ *
+ * @return - true on found key
+ */
+static bool found_key(const char *str, const char *key)
+{
+       char *k;
+       char *s, *strcopy;
+       bool result = false;
+
+       strcopy = strdup(str);
+       if (!strcopy)
+               return NULL;
+
+       s = strcopy;
+       while (s) {
+               k = strsep(&s, ",");
+               if (!k)
+                       break;
+               if  (strcmp(k, key) == 0) {
+                       result = true;
+                       break;
+               }
+       }
+
+       free(strcopy);
+
+       return result;
+}
+
 /**
  * set_gpt_info(): Fill partition information from string
  *             function allocates memory, remember to free!
@@ -131,8 +180,9 @@ static int set_gpt_info(block_dev_desc_t *dev_desc,
        int p_count;
        disk_partition_t *parts;
        int errno = 0;
+       uint64_t size_ll, start_ll;
 
-       debug("%s: MMC lba num: 0x%x %d\n", __func__,
+       debug("%s:  lba num: 0x%x %d\n", __func__,
              (unsigned int)dev_desc->lba, (unsigned int)dev_desc->lba);
 
        if (str_part == NULL)
@@ -142,17 +192,24 @@ static int set_gpt_info(block_dev_desc_t *dev_desc,
 
        /* extract disk guid */
        s = str;
-       tok = strsep(&s, ";");
-       val = extract_val(tok, "uuid_disk");
+       val = extract_val(str, "uuid_disk");
        if (!val) {
+#ifdef CONFIG_RANDOM_UUID
+               *str_disk_guid = malloc(UUID_STR_LEN + 1);
+               gen_rand_uuid_str(*str_disk_guid, UUID_STR_FORMAT_STD);
+#else
                free(str);
                return -2;
+#endif
+       } else {
+               val = strsep(&val, ";");
+               if (extract_env(val, &p))
+                       p = val;
+               *str_disk_guid = strdup(p);
+               free(val);
+               /* Move s to first partition */
+               strsep(&s, ";");
        }
-       if (extract_env(val, &p))
-               p = val;
-       *str_disk_guid = strdup(p);
-       free(val);
-
        if (strlen(s) == 0)
                return -3;
 
@@ -171,7 +228,7 @@ static int set_gpt_info(block_dev_desc_t *dev_desc,
        /* allocate memory for partitions */
        parts = calloc(sizeof(disk_partition_t), p_count);
 
-       /* retrive partions data from string */
+       /* retrieve partitions data from string */
        for (i = 0; i < p_count; i++) {
                tok = strsep(&s, ";");
 
@@ -180,20 +237,42 @@ static int set_gpt_info(block_dev_desc_t *dev_desc,
 
                /* uuid */
                val = extract_val(tok, "uuid");
-               if (!val) { /* 'uuid' is mandatory */
+               if (!val) {
+                       /* 'uuid' is optional if random uuid's are enabled */
+#ifdef CONFIG_RANDOM_UUID
+                       gen_rand_uuid_str(parts[i].uuid, UUID_STR_FORMAT_STD);
+#else
                        errno = -4;
                        goto err;
+#endif
+               } else {
+                       if (extract_env(val, &p))
+                               p = val;
+                       if (strlen(p) >= sizeof(parts[i].uuid)) {
+                               printf("Wrong uuid format for partition %d\n", i);
+                               errno = -4;
+                               goto err;
+                       }
+                       strcpy((char *)parts[i].uuid, p);
+                       free(val);
                }
-               if (extract_env(val, &p))
-                       p = val;
-               if (strlen(p) >= sizeof(parts[i].uuid)) {
-                       printf("Wrong uuid format for partition %d\n", i);
-                       errno = -4;
-                       goto err;
+#ifdef CONFIG_PARTITION_TYPE_GUID
+               /* guid */
+               val = extract_val(tok, "type");
+               if (val) {
+                       /* 'type' is optional */
+                       if (extract_env(val, &p))
+                               p = val;
+                       if (strlen(p) >= sizeof(parts[i].type_guid)) {
+                               printf("Wrong type guid format for partition %d\n",
+                                      i);
+                               errno = -4;
+                               goto err;
+                       }
+                       strcpy((char *)parts[i].type_guid, p);
+                       free(val);
                }
-               strcpy((char *)parts[i].uuid, p);
-               free(val);
-
+#endif
                /* name */
                val = extract_val(tok, "name");
                if (!val) { /* name is mandatory */
@@ -217,8 +296,8 @@ static int set_gpt_info(block_dev_desc_t *dev_desc,
                }
                if (extract_env(val, &p))
                        p = val;
-               parts[i].size = ustrtoul(p, &p, 0);
-               parts[i].size /= dev_desc->blksz;
+               size_ll = ustrtoull(p, &p, 0);
+               parts[i].size = lldiv(size_ll, dev_desc->blksz);
                free(val);
 
                /* start address */
@@ -226,10 +305,14 @@ static int set_gpt_info(block_dev_desc_t *dev_desc,
                if (val) { /* start address is optional */
                        if (extract_env(val, &p))
                                p = val;
-                       parts[i].start = ustrtoul(p, &p, 0);
-                       parts[i].start /= dev_desc->blksz;
+                       start_ll = ustrtoull(p, &p, 0);
+                       parts[i].start = lldiv(start_ll, dev_desc->blksz);
                        free(val);
                }
+
+               /* bootable */
+               if (found_key(tok, "bootable"))
+                       parts[i].bootable = 1;
        }
 
        *parts_count = p_count;
@@ -245,25 +328,15 @@ err:
        return errno;
 }
 
-static int gpt_mmc_default(int dev, const char *str_part)
+static int gpt_default(block_dev_desc_t *blk_dev_desc, const char *str_part)
 {
        int ret;
        char *str_disk_guid;
        u8 part_count = 0;
        disk_partition_t *partitions = NULL;
 
-       struct mmc *mmc = find_mmc_device(dev);
-
-       if (mmc == NULL) {
-               printf("%s: mmc dev %d NOT available\n", __func__, dev);
-               return CMD_RET_FAILURE;
-       }
-
-       if (!str_part)
-               return -1;
-
        /* fill partitions */
-       ret = set_gpt_info(&mmc->block_dev, str_part,
+       ret = set_gpt_info(blk_dev_desc, str_part,
                        &str_disk_guid, &partitions, &part_count);
        if (ret) {
                if (ret == -1)
@@ -276,11 +349,48 @@ static int gpt_mmc_default(int dev, const char *str_part)
        }
 
        /* save partitions layout to disk */
-       gpt_restore(&mmc->block_dev, str_disk_guid, partitions, part_count);
+       ret = gpt_restore(blk_dev_desc, str_disk_guid, partitions, part_count);
        free(str_disk_guid);
        free(partitions);
 
-       return 0;
+       return ret;
+}
+
+static int gpt_verify(block_dev_desc_t *blk_dev_desc, const char *str_part)
+{
+       ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_head, 1,
+                                    blk_dev_desc->blksz);
+       disk_partition_t *partitions = NULL;
+       gpt_entry *gpt_pte = NULL;
+       char *str_disk_guid;
+       u8 part_count = 0;
+       int ret = 0;
+
+       /* fill partitions */
+       ret = set_gpt_info(blk_dev_desc, str_part,
+                       &str_disk_guid, &partitions, &part_count);
+       if (ret) {
+               if (ret == -1) {
+                       printf("No partition list provided - only basic check\n");
+                       ret = gpt_verify_headers(blk_dev_desc, gpt_head,
+                                                &gpt_pte);
+                       goto out;
+               }
+               if (ret == -2)
+                       printf("Missing disk guid\n");
+               if ((ret == -3) || (ret == -4))
+                       printf("Partition list incomplete\n");
+               return -1;
+       }
+
+       /* Check partition layout with provided pattern */
+       ret = gpt_verify_partitions(blk_dev_desc, partitions, part_count,
+                                   gpt_head, &gpt_pte);
+       free(str_disk_guid);
+       free(partitions);
+ out:
+       free(gpt_pte);
+       return ret;
 }
 
 /**
@@ -297,37 +407,50 @@ static int do_gpt(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 {
        int ret = CMD_RET_SUCCESS;
        int dev = 0;
-       char *pstr;
+       char *ep;
+       block_dev_desc_t *blk_dev_desc = NULL;
 
-       if (argc < 5)
+       if (argc < 4 || argc > 5)
                return CMD_RET_USAGE;
 
-       /* command: 'write' */
+       dev = (int)simple_strtoul(argv[3], &ep, 10);
+       if (!ep || ep[0] != '\0') {
+               printf("'%s' is not a number\n", argv[3]);
+               return CMD_RET_USAGE;
+       }
+       blk_dev_desc = get_dev(argv[2], dev);
+       if (!blk_dev_desc) {
+               printf("%s: %s dev %d NOT available\n",
+                      __func__, argv[2], dev);
+               return CMD_RET_FAILURE;
+       }
+
        if ((strcmp(argv[1], "write") == 0) && (argc == 5)) {
-               /* device: 'mmc' */
-               if (strcmp(argv[2], "mmc") == 0) {
-                       /* check if 'dev' is a number */
-                       for (pstr = argv[3]; *pstr != '\0'; pstr++)
-                               if (!isdigit(*pstr)) {
-                                       printf("'%s' is not a number\n",
-                                               argv[3]);
-                                       return CMD_RET_USAGE;
-                               }
-                       dev = (int)simple_strtoul(argv[3], NULL, 10);
-                       /* write to mmc */
-                       if (gpt_mmc_default(dev, argv[4]))
-                               return CMD_RET_FAILURE;
-               }
+               printf("Writing GPT: ");
+               ret = gpt_default(blk_dev_desc, argv[4]);
+       } else if ((strcmp(argv[1], "verify") == 0)) {
+               ret = gpt_verify(blk_dev_desc, argv[4]);
+               printf("Verify GPT: ");
        } else {
                return CMD_RET_USAGE;
        }
-       return ret;
+
+       if (ret) {
+               printf("error!\n");
+               return CMD_RET_FAILURE;
+       }
+
+       printf("success!\n");
+       return CMD_RET_SUCCESS;
 }
 
 U_BOOT_CMD(gpt, CONFIG_SYS_MAXARGS, 1, do_gpt,
        "GUID Partition Table",
-       "<command> <interface> <dev> <partions_list>\n"
-       " - GUID partition table restoration\n"
-       " Restore GPT information on a device connected\n"
+       "<command> <interface> <dev> <partitions_list>\n"
+       " - GUID partition table restoration and validity check\n"
+       " Restore or verify GPT information on a device connected\n"
        " to interface\n"
+       " Example usage:\n"
+       " gpt write mmc 0 $partitions\n"
+       " gpt verify mmc 0 $partitions\n"
 );