[U-Boot] [RFC PATCH v2 4/8] cmd: gpt: update command to support GPT over MTD

Patrick Delaunay patrick.delaunay73 at gmail.com
Wed Nov 30 12:01:24 CET 2016


From: Patrick Delaunay <patrick.delaunay at st.com>

support gpt write for MTD device
> gpt write nand 0
> gpt write nor 0

Signed-off-by: Patrick Delaunay <patrick.delaunay at st.com>
Signed-off-by: Patrick Delaunay <patrick.delaunay73 at gmail.com>
---

Changes in v2: None

 cmd/gpt.c | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++++-----------
 1 file changed, 82 insertions(+), 16 deletions(-)

diff --git a/cmd/gpt.c b/cmd/gpt.c
index 897596a..9e398e9 100644
--- a/cmd/gpt.c
+++ b/cmd/gpt.c
@@ -20,6 +20,10 @@
 #include <div64.h>
 #include <memalign.h>
 
+#ifdef CONFIG_EFI_PARTITION_MTD
+#include <linux/mtd/mtd.h>
+#endif
+
 #ifndef CONFIG_PARTITION_UUIDS
 #error CONFIG_PARTITION_UUIDS must be enabled for CONFIG_CMD_GPT to be enabled
 #endif
@@ -168,7 +172,8 @@ static bool found_key(const char *str, const char *key)
  * @return - zero on success, otherwise error
  *
  */
-static int set_gpt_info(struct blk_desc *dev_desc,
+static int set_gpt_info(unsigned int lba,
+			unsigned int blksz,
 			const char *str_part,
 			char **str_disk_guid,
 			disk_partition_t **partitions,
@@ -183,8 +188,7 @@ static int set_gpt_info(struct blk_desc *dev_desc,
 	uint64_t size_ll, start_ll;
 	lbaint_t offset = 0;
 
-	debug("%s:  lba num: 0x%x %d\n", __func__,
-	      (unsigned int)dev_desc->lba, (unsigned int)dev_desc->lba);
+	debug("%s:  lba num: 0x%x %d\n", __func__, lba, lba);
 
 	if (str_part == NULL)
 		return -1;
@@ -302,7 +306,7 @@ static int set_gpt_info(struct blk_desc *dev_desc,
 			parts[i].size = 0;
 		} else {
 			size_ll = ustrtoull(p, &p, 0);
-			parts[i].size = lldiv(size_ll, dev_desc->blksz);
+			parts[i].size = lldiv(size_ll, blksz);
 		}
 
 		free(val);
@@ -313,7 +317,7 @@ static int set_gpt_info(struct blk_desc *dev_desc,
 			if (extract_env(val, &p))
 				p = val;
 			start_ll = ustrtoull(p, &p, 0);
-			parts[i].start = lldiv(start_ll, dev_desc->blksz);
+			parts[i].start = lldiv(start_ll, blksz);
 			free(val);
 		}
 
@@ -337,6 +341,16 @@ err:
 	return errno;
 }
 
+static void print_gpt_info_err(int ret)
+{
+	if (ret == -1)
+		printf("No partition list provided\n");
+	if (ret == -2)
+		printf("Missing disk guid\n");
+	if ((ret == -3) || (ret == -4))
+		printf("Partition list incomplete\n");
+}
+
 static int gpt_default(struct blk_desc *blk_dev_desc, const char *str_part)
 {
 	int ret;
@@ -344,16 +358,13 @@ static int gpt_default(struct blk_desc *blk_dev_desc, const char *str_part)
 	u8 part_count = 0;
 	disk_partition_t *partitions = NULL;
 
+	if (!str_part)
+		return -1;
 	/* fill partitions */
-	ret = set_gpt_info(blk_dev_desc, str_part,
+	ret = set_gpt_info(blk_dev_desc->lba, blk_dev_desc->blksz, str_part,
 			&str_disk_guid, &partitions, &part_count);
 	if (ret) {
-		if (ret == -1)
-			printf("No partition list provided\n");
-		if (ret == -2)
-			printf("Missing disk guid\n");
-		if ((ret == -3) || (ret == -4))
-			printf("Partition list incomplete\n");
+		print_gpt_info_err(ret);
 		return -1;
 	}
 
@@ -376,7 +387,7 @@ static int gpt_verify(struct blk_desc *blk_dev_desc, const char *str_part)
 	int ret = 0;
 
 	/* fill partitions */
-	ret = set_gpt_info(blk_dev_desc, str_part,
+	ret = set_gpt_info(blk_dev_desc->lba, blk_dev_desc->blksz, str_part,
 			&str_disk_guid, &partitions, &part_count);
 	if (ret) {
 		if (ret == -1) {
@@ -402,6 +413,35 @@ static int gpt_verify(struct blk_desc *blk_dev_desc, const char *str_part)
 	return ret;
 }
 
+#ifdef CONFIG_EFI_PARTITION_MTD
+static int gpt_default_mtd(struct mtd_info *mtd_info, const char *str_part)
+{
+	int ret;
+	char *str_disk_guid;
+	u8 part_count = 0;
+	disk_partition_t *partitions = NULL;
+	unsigned int lba = lldiv(mtd_info->size, MTD_LBA_SIZE);
+
+	if (!str_part)
+		return -1;
+
+	/* fill partitions */
+	ret = set_gpt_info(lba, MTD_LBA_SIZE, str_part,
+			   &str_disk_guid, &partitions, &part_count);
+	if (ret) {
+		print_gpt_info_err(ret);
+		return -1;
+	}
+
+	/* save partitions layout to disk */
+	ret = gpt_restore_mtd(mtd_info, str_disk_guid, partitions, part_count);
+	free(str_disk_guid);
+	free(partitions);
+
+	return ret;
+}
+#endif
+
 /**
  * do_gpt(): Perform GPT operations
  *
@@ -418,6 +458,10 @@ static int do_gpt(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 	int dev = 0;
 	char *ep;
 	struct blk_desc *blk_dev_desc = NULL;
+#ifdef CONFIG_EFI_PARTITION_MTD
+	struct mtd_info *mtd_info = NULL;
+	char mtd_dev[16];
+#endif
 
 	if (argc < 4 || argc > 5)
 		return CMD_RET_USAGE;
@@ -428,17 +472,38 @@ static int do_gpt(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 		return CMD_RET_USAGE;
 	}
 	blk_dev_desc = blk_get_dev(argv[2], dev);
+#ifdef CONFIG_EFI_PARTITION_MTD
 	if (!blk_dev_desc) {
+			sprintf(mtd_dev, "%s%d", argv[2], dev);
+			mtd_info = get_mtd_device_nm(mtd_dev);
+			if (IS_ERR(mtd_info))
+				ret = CMD_RET_FAILURE;
+	}
+#else
+	if (!blk_dev_desc)
+		ret = CMD_RET_FAILURE;
+#endif
+	if (ret) {
 		printf("%s: %s dev %d NOT available\n",
 		       __func__, argv[2], dev);
-		return CMD_RET_FAILURE;
+		return ret;
 	}
 
 	if ((strcmp(argv[1], "write") == 0) && (argc == 5)) {
 		printf("Writing GPT: ");
-		ret = gpt_default(blk_dev_desc, argv[4]);
+		if (blk_dev_desc)
+			ret = gpt_default(blk_dev_desc, argv[4]);
+#ifdef CONFIG_EFI_PARTITION_MTD
+		else if (mtd_info)
+			ret = gpt_default_mtd(mtd_info, argv[4]);
+#endif
+		else
+			ret = CMD_RET_FAILURE;
 	} else if ((strcmp(argv[1], "verify") == 0)) {
-		ret = gpt_verify(blk_dev_desc, argv[4]);
+		if (blk_dev_desc)
+			ret = gpt_verify(blk_dev_desc, argv[4]);
+		else
+			ret = CMD_RET_FAILURE;
 		printf("Verify GPT: ");
 	} else {
 		return CMD_RET_USAGE;
@@ -451,6 +516,7 @@ static int do_gpt(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 
 	printf("success!\n");
 	return CMD_RET_SUCCESS;
+
 }
 
 U_BOOT_CMD(gpt, CONFIG_SYS_MAXARGS, 1, do_gpt,
-- 
1.9.1



More information about the U-Boot mailing list