Skip to contentSkip to conversation

cmd/gpt: Support gpt command for all devices

PATCH Superseded Egbert Eich 3 participants 5 messages Oct 4, 2013 – Nov 8, 2013 1 message not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): 000101ce35f115206703f61350$%[email protected]

#1 lore raw

PATCH v3 1/1 cmd/gpt: Support gpt command for all devices

From: Egbert Eich <[email protected]>

The gpt command was only implemented for mmc devices. There is no reason why this command should not be generalized and be applied all other storage device classes.
This change both simplifies the implementation and eliminates a build failure for systems that don't support mmcs.

---
Changes for v2:
- Coding style cleanup.
Changes for v3:
- Removed wrong '&'
- Removed unused variable
- Fixed argument checking
Spotted by Piotr Wilczek <[email protected]>

 common/cmd_gpt.c | 45 +++++++++++++++++++--------------------------
 1 file changed, 19 insertions(+), 26 deletions(-)
common/cmd_gpt.c +19 -26
index a46f5cc..17b1180 100644
@@ -11,7 +11,6 @@
 #include <common.h>
 #include <malloc.h>
 #include <command.h>
-#include <mmc.h>
 #include <part_efi.h>
 #include <exports.h>
 #include <linux/ctype.h>
@@ -122,7 +121,7 @@ static int set_gpt_info(block_dev_desc_t *dev_desc,
 	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)
@@ -235,25 +234,18 @@ 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)
@@ -266,7 +258,7 @@ 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);
+	gpt_restore(blk_dev_desc, str_disk_guid, partitions, part_count);
 	free(str_disk_guid);
 	free(partitions);
 
@@ -287,27 +279,28 @@ 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;
 
 	if (argc < 5)
 		return CMD_RET_USAGE;
 
 	/* command: 'write' */
 	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;
+		char *ep;
+		block_dev_desc_t *blk_dev_desc;
+		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 (gpt_default(blk_dev_desc, argv[4]))
+			return CMD_RET_FAILURE;
 	} else {
 		return CMD_RET_USAGE;
 	}
Signature · 1 line
1.8.1.4
#2 lore raw

PATCH v3 1/1 cmd/gpt: Support gpt command for all devices

Dear Egbert Eich,

Quoted 105 lines

-----Original Message-----
From: Egbert Eich [mailto:egbert.eich at gmail.com]
Sent: Friday, October 04, 2013 6:53 PM
To: u-boot at lists.denx.de
Cc: Piotr Wilczek; Tom Rini; Egbert Eich; Egbert Eich
Subject: [Patch v3] cmd/gpt: Support gpt command for all devices

From: Egbert Eich <[email protected]>

The gpt command was only implemented for mmc devices. There is no reason why this command should not be generalized and be applied all other storage device classes.
This change both simplifies the implementation and eliminates a build failure for systems that don't support mmcs.

---
Changes for v2:
- Coding style cleanup.
Changes for v3:
- Removed wrong '&'
- Removed unused variable
- Fixed argument checking
Spotted by Piotr Wilczek <[email protected]>

 common/cmd_gpt.c | 45 +++++++++++++++++++--------------------------
 1 file changed, 19 insertions(+), 26 deletions(-)
common/cmd_gpt.c index a46f5cc..17b1180 renamed from common/cmd_gpt.c +0 -0

100644

common/cmd_gpt.c +5 -12
@@ -11,7 +11,6 @@
 #include <common.h>
 #include <malloc.h>
 #include <command.h>
-#include <mmc.h>
 #include <part_efi.h>
 #include <exports.h>
 #include <linux/ctype.h>
@@ -122,7 +121,7 @@ static int set_gpt_info(block_dev_desc_t *dev_desc,
 	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)
@@ -235,25 +234,18 @@ 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)
@@ -266,7 +258,7 @@ 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);
+	gpt_restore(blk_dev_desc, str_disk_guid, partitions, part_count);
 	free(str_disk_guid);
common/cmd_gpt.c +0 -6
 	free(partitions);
@@ -287,27 +279,28 @@ 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;
 
 	if (argc < 5)
 		return CMD_RET_USAGE;
 
 	/* command: 'write' */
 	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)) {

common/cmd_gpt.c static int do_gpt(cmd_tbl_t *cmdtp, int flag, int · line 289

Quoted 10 lines
common/cmd_gpt.c +2 -8
-					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;
+		char *ep;
+		block_dev_desc_t *blk_dev_desc;

This probably should be at the beginning of the function

common/cmd_gpt.c static int do_gpt(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) · line 298

Quoted 9 lines
common/cmd_gpt.c +8 -0
+		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);

I think it is not necessary since the mmc subsystem prints 'MMC Device not found'.

Quoted 10 lines
common/cmd_gpt.c +5 -1
+			return CMD_RET_FAILURE;
+		}
+
+		if (gpt_default(blk_dev_desc, argv[4]))
+			return CMD_RET_FAILURE;
 	} else {
 		return CMD_RET_USAGE;
 	}
--

1.8.1.4

Except minor comments this patch looks good to me. I tested it on mmc device (Trats2) and works well.

Best regards,
Piotr Wilczek

#3 lore raw

PATCH v3 1/1 cmd/gpt: Support gpt command for all devices

Quoted 34 lines · Piotr Wilczek

Dear Egbert Eich,

Quoted 32 lines

-----Original Message-----
From: Egbert Eich [mailto:egbert.eich at gmail.com]
Sent: Friday, October 04, 2013 6:53 PM
To: u-boot at lists.denx.de
Cc: Piotr Wilczek; Tom Rini; Egbert Eich; Egbert Eich
Subject: [Patch v3] cmd/gpt: Support gpt command for all devices

From: Egbert Eich <[email protected]>

The gpt command was only implemented for mmc devices. There is no reason why this command should not be generalized and be applied all other storage device classes.
This change both simplifies the implementation and eliminates a build failure for systems that don't support mmcs.

---
Changes for v2:
- Coding style cleanup.
Changes for v3:
- Removed wrong '&'
- Removed unused variable
- Fixed argument checking
Spotted by Piotr Wilczek <[email protected]>

 common/cmd_gpt.c | 45 +++++++++++++++++++--------------------------
 1 file changed, 19 insertions(+), 26 deletions(-)
common/cmd_gpt.c index a46f5cc..17b1180 renamed from common/cmd_gpt.c +0 -0

100644

common/cmd_gpt.c +0 -0

[..]

Quoted 2 lines
Quoted 2 lines
common/cmd_gpt.c +0 -0
@@ -287,27 +279,28 @@ static int do_gpt(cmd_tbl_t *cmdtp, int flag, int

[..]

Quoted 8 lines
Quoted 8 lines
common/cmd_gpt.c +0 -8
-		/* 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;
Quoted 8 lines

common/cmd_gpt.c static int do_gpt(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) · line 289

Quoted 7 lines
common/cmd_gpt.c +2 -5
-				}
-			dev = (int)simple_strtoul(argv[3], NULL, 10);
-			/* write to mmc */
-			if (gpt_mmc_default(dev, argv[4]))
-				return CMD_RET_FAILURE;
+		char *ep;
+		block_dev_desc_t *blk_dev_desc;

This probably should be at the beginning of the function

I personally prefer to keep symbols as local as possible (ie. declare them in the block they are used in if they are just used within a single block) for the following rasons:
1. It makes the code more readable ie. the definition is closeby to the location where it is used and doesn't require scrolling to the beginning of a function and the scope of the variable is is much more obvious.
2. The compiler can optimize much better as it knows that a variable can be discarded at the end of the block also by reusing stack slots stack sapce can be used much more efficiently by the compiler.

I agree that in the case at hand the second argument is not too relevant, it is more a coding style issue. If there is a coding style requirement to have those definitions at the beginning of the function I will create a new patch.

[..]

Quoted 6 lines

common/cmd_gpt.c static int do_gpt(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) · line 298

Quoted 4 lines
common/cmd_gpt.c +4 -0
+		blk_dev_desc = get_dev(argv[2], dev);
+		if (!blk_dev_desc) {
+			printf("%s: %s dev %d NOT available\n",
+			       __func__, argv[2], dev);

I think it is not necessary since the mmc subsystem prints 'MMC Device not found'.

I've done a quick look over the code - of all subsystems MMC seems to be the only one which prints a message when its get_dev() method is called but no device is found. Therefore I'd prefer to leave this there.

Quoted 3 lines

Except minor comments this patch looks good to me. I tested it on mmc device (Trats2) and works well.

Ok, thanks!

Quoted 3 lines

Thanks a lot for testing!

Cheers,
Egbert.

#4 lore raw

PATCH v3 1/1 cmd/gpt: Support gpt command for all devices

Quoted 84 lines

-----Original Message-----
From: Egbert Eich [mailto:egbert.eich at gmail.com]
Sent: Sunday, October 13, 2013 1:14 AM
To: Piotr Wilczek
Cc: 'Egbert Eich'; u-boot at lists.denx.de; 'Tom Rini'; 'Egbert Eich'
Subject: Re: [Patch v3] cmd/gpt: Support gpt command for all devices

Quoted 13 lines · Piotr Wilczek

Dear Egbert Eich,

Quoted 11 lines

-----Original Message-----
From: Egbert Eich [mailto:egbert.eich at gmail.com]
Sent: Friday, October 04, 2013 6:53 PM
To: u-boot at lists.denx.de
Cc: Piotr Wilczek; Tom Rini; Egbert Eich; Egbert Eich
Subject: [Patch v3] cmd/gpt: Support gpt command for all devices

From: Egbert Eich <[email protected]>

The gpt command was only implemented for mmc devices. There is no reason why this command should not be generalized and be applied

all

Quoted 14 lines
Quoted 14 lines

other storage device classes.
This change both simplifies the implementation and eliminates a build failure for systems that don't support mmcs.

---
Changes for v2:
- Coding style cleanup.
Changes for v3:
- Removed wrong '&'
- Removed unused variable
- Fixed argument checking
Spotted by Piotr Wilczek <[email protected]>

Quoted 1 line
Quoted 1 line
 common/cmd_gpt.c | 45 +++++++++++++++++++-------------------------

-

Quoted 7 lines
Quoted 7 lines
 1 file changed, 19 insertions(+), 26 deletions(-)
common/cmd_gpt.c index renamed from common/cmd_gpt.c +0 -0

a46f5cc..17b1180
100644

common/cmd_gpt.c +0 -0

[..]

Quoted 3 lines
Quoted 3 lines
common/cmd_gpt.c +0 -0
@@ -287,27 +279,28 @@ static int do_gpt(cmd_tbl_t *cmdtp, int flag,

int

[..]

Quoted 8 lines
Quoted 8 lines
common/cmd_gpt.c +0 -8
-		/* 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;
Quoted 8 lines

common/cmd_gpt.c static int do_gpt(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) · line 289

Quoted 7 lines
common/cmd_gpt.c +2 -5
-				}
-			dev = (int)simple_strtoul(argv[3], NULL, 10);
-			/* write to mmc */
-			if (gpt_mmc_default(dev, argv[4]))
-				return CMD_RET_FAILURE;
+		char *ep;
+		block_dev_desc_t *blk_dev_desc;

This probably should be at the beginning of the function

I personally prefer to keep symbols as local as possible (ie. declare them in the block they are used in if they are just used within a single block) for the following rasons:
1. It makes the code more readable ie. the definition is closeby to the location where it is used and doesn't require scrolling to the beginning of a function and the scope of the variable is is much more obvious.
2. The compiler can optimize much better as it knows that a variable can be discarded at the end of the block also by reusing stack slots stack sapce can be used much more efficiently by the compiler.

Quoted 5 lines

I agree that in the case at hand the second argument is not too relevant, it is more a coding style issue. If there is a coding style requirement to have those definitions at the beginning of the function I will create a new patch.

Agreed, I think it is a coding style rule in U-boot. Let the maintainer to decide.

Quoted 8 lines

[..]

Quoted 5 lines

common/cmd_gpt.c static int do_gpt(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) · line 298

Quoted 4 lines
common/cmd_gpt.c +4 -0
+		blk_dev_desc = get_dev(argv[2], dev);
+		if (!blk_dev_desc) {
+			printf("%s: %s dev %d NOT available\n",
+			       __func__, argv[2], dev);

I think it is not necessary since the mmc subsystem prints 'MMC

Quoted 8 lines

Device

Quoted 1 line

not found'.

I've done a quick look over the code - of all subsystems MMC seems to be the only one which prints a message when its get_dev() method is called but no device is found. Therefore I'd prefer to leave this there.

Ok.

Quoted 7 lines
Quoted 3 lines

Except minor comments this patch looks good to me. I tested it on mmc device (Trats2) and works well.

Ok, thanks!

Quoted 1 line
Quoted 7 lines
Quoted 2 lines

Thanks a lot for testing!

Cheers,
Egbert.

Best regards,
Piotr Wilczek

#5 lore raw

PATCH v3 1/1 cmd/gpt: Support gpt command for all devices

In reply to Egbert Eich
Quoted 10 lines · egbert.eich at gmail.com

From: Egbert Eich <[email protected]>

The gpt command was only implemented for mmc devices. There is no reason why this command should not be generalized and be applied all other storage device classes.
This change both simplifies the implementation and eliminates a build failure for systems that don't support mmcs.

Applied to u-boot/master with a slight change to the coding style (yes, we want variables declared at the start of the function and __maybe_unused for ones that would be hidden via #ifdef and cause a warning in some cases), thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: Digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20131108/4de11d76/attachment.pgp>