All of lore.kernel.org
 help / color / mirror / Atom feed
* [U-Boot] [PATCH 0/9] Auto partition selection and fs partition consolidation
@ 2012-08-23 21:31 Rob Herring
  2012-08-23 21:31 ` [U-Boot] [PATCH 1/9] combine block device load commands into common function Rob Herring
                   ` (8 more replies)
  0 siblings, 9 replies; 20+ messages in thread
From: Rob Herring @ 2012-08-23 21:31 UTC (permalink / raw)
  To: u-boot

From: Rob Herring <rob.herring@calxeda.com>

The primary goal of this series is to enable auto selection of a partition
using the 1st bootable partition as the default partition for disk based
boot commands. If a bootable partition is not found and partition is not
specified, then the first valid partition is used.

Every command that takes a "<dev>[:<part>]" option duplicates the same
parsing code, so this series consolidates the parsing code to a single
function and converts all block based filesystem code over to use th

This is based on Wolfgang's ext4 branch.

Rob

Rob Herring (9):
  combine block device load commands into common function
  disk/part: check bootable flag for DOS partitions
  disk/part: introduce get_device_and_partition
  ext4: remove init_fs/deinit_fs
  cmd_extX: use common get_device_and_partition function
  cmd_fat: use common get_device_and_partition function
  cmd_disk: use common get_device_and_partition function
  cmd_zfs: use common get_device_and_partition function
  cmd_reiser: use common get_device_and_partition function

 common/Makefile         |    1 +
 common/cmd_disk.c       |  122 ++++++++++++++++++++++++++++++++++++++
 common/cmd_ext4.c       |  106 +++------------------------------
 common/cmd_ext_common.c |  108 ++++++++-------------------------
 common/cmd_fat.c        |  100 +++++++++++--------------------
 common/cmd_ide.c        |  151 +----------------------------------------------
 common/cmd_reiser.c     |   81 ++++++-------------------
 common/cmd_scsi.c       |  123 +-------------------------------------
 common/cmd_usb.c        |  138 +------------------------------------------
 common/cmd_zfs.c        |   88 ++++++---------------------
 disk/part.c             |   90 +++++++++++++++++++++++++++-
 disk/part_dos.c         |   11 +++-
 fs/ext4/dev.c           |   32 ++++------
 fs/ext4/ext4_common.h   |    1 -
 fs/ext4/ext4fs.c        |   37 +-----------
 fs/reiserfs/dev.c       |   29 ++++-----
 fs/zfs/dev.c            |   35 ++++-------
 include/command.h       |    4 ++
 include/ext4fs.h        |    5 +-
 include/ext_common.h    |    2 +
 include/part.h          |   14 ++++-
 include/reiserfs.h      |    2 +-
 include/zfs_common.h    |    7 +--
 23 files changed, 380 insertions(+), 907 deletions(-)
 create mode 100644 common/cmd_disk.c

-- 
1.7.9.5

^ permalink raw reply	[flat|nested] 20+ messages in thread

* [U-Boot] [PATCH 1/9] combine block device load commands into common function
  2012-08-23 21:31 [U-Boot] [PATCH 0/9] Auto partition selection and fs partition consolidation Rob Herring
@ 2012-08-23 21:31 ` Rob Herring
  2012-09-05 23:36   ` Tom Rini
  2012-09-21 14:02   ` [U-Boot] [PATCH v2 " Rob Herring
  2012-08-23 21:31 ` [U-Boot] [PATCH 2/9] disk/part: check bootable flag for DOS partitions Rob Herring
                   ` (7 subsequent siblings)
  8 siblings, 2 replies; 20+ messages in thread
From: Rob Herring @ 2012-08-23 21:31 UTC (permalink / raw)
  To: u-boot

From: Rob Herring <rob.herring@calxeda.com>

All the raw block load commands duplicate the same code. Starting with
the ide version as it has progress updates convert ide, usb, and scsi boot
commands to all use a common version.

Signed-off-by: Rob Herring <rob.herring@calxeda.com>
---
 common/Makefile   |    1 +
 common/cmd_disk.c |  161 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 common/cmd_ide.c  |  151 +------------------------------------------------
 common/cmd_scsi.c |  123 +---------------------------------------
 common/cmd_usb.c  |  138 +--------------------------------------------
 include/command.h |    4 ++
 6 files changed, 169 insertions(+), 409 deletions(-)
 create mode 100644 common/cmd_disk.c

diff --git a/common/Makefile b/common/Makefile
index 4273484..0fac6d0 100644
--- a/common/Makefile
+++ b/common/Makefile
@@ -34,6 +34,7 @@ COBJS-$(CONFIG_SYS_HUSH_PARSER) += hush.o
 COBJS-y += s_record.o
 COBJS-$(CONFIG_SERIAL_MULTI) += serial.o
 COBJS-y += xyzModem.o
+COBJS-y += cmd_disk.o
 
 # core command
 COBJS-y += cmd_boot.o
diff --git a/common/cmd_disk.c b/common/cmd_disk.c
new file mode 100644
index 0000000..38420dc
--- /dev/null
+++ b/common/cmd_disk.c
@@ -0,0 +1,161 @@
+#include <common.h>
+#include <command.h>
+
+int common_diskboot(cmd_tbl_t *cmdtp, const char *intf, int argc, 
+		    char *const argv[])
+{
+	char *boot_device = NULL;
+	char *ep;
+	int dev, part = 0;
+	ulong addr, cnt;
+	disk_partition_t info;
+	image_header_t *hdr;
+	block_dev_desc_t *dev_desc;
+
+#if defined(CONFIG_FIT)
+	const void *fit_hdr = NULL;
+#endif
+
+	bootstage_mark(BOOTSTAGE_ID_IDE_START);
+	switch (argc) {
+	case 1:
+		addr = CONFIG_SYS_LOAD_ADDR;
+		boot_device = getenv("bootdevice");
+		break;
+	case 2:
+		addr = simple_strtoul(argv[1], NULL, 16);
+		boot_device = getenv("bootdevice");
+		break;
+	case 3:
+		addr = simple_strtoul(argv[1], NULL, 16);
+		boot_device = argv[2];
+		break;
+	default:
+		bootstage_error(BOOTSTAGE_ID_IDE_ADDR);
+		return CMD_RET_USAGE;
+	}
+	bootstage_mark(BOOTSTAGE_ID_IDE_ADDR);
+
+	if (!boot_device) {
+		puts("\n** No boot device **\n");
+		bootstage_error(BOOTSTAGE_ID_IDE_BOOT_DEVICE);
+		return 1;
+	}
+	bootstage_mark(BOOTSTAGE_ID_IDE_BOOT_DEVICE);
+
+	dev = simple_strtoul(boot_device, &ep, 16);
+
+	dev_desc = get_dev(intf, dev);
+	if (dev_desc->type == DEV_TYPE_UNKNOWN) {
+		printf("\n** Device %d not available\n", dev);
+		bootstage_error(BOOTSTAGE_ID_IDE_TYPE);
+		return 1;
+	}
+	bootstage_mark(BOOTSTAGE_ID_IDE_TYPE);
+
+	if (*ep) {
+		if (*ep != ':') {
+			puts("\n** Invalid boot device, use `dev[:part]' **\n");
+			bootstage_error(BOOTSTAGE_ID_IDE_PART);
+			return 1;
+		}
+		part = simple_strtoul(++ep, NULL, 16);
+	}
+	bootstage_mark(BOOTSTAGE_ID_IDE_PART);
+
+	if (get_partition_info(dev_desc, part, &info)) {
+		bootstage_error(BOOTSTAGE_ID_IDE_PART_INFO);
+		return 1;
+	}
+	bootstage_mark(BOOTSTAGE_ID_IDE_PART_INFO);
+
+	if ((strncmp((char *)info.type, BOOT_PART_TYPE, sizeof(info.type)) != 0)
+	    &&
+	    (strncmp((char *)info.type, BOOT_PART_COMP, sizeof(info.type)) != 0)
+	   ) {
+		printf("\n** Invalid partition type \"%.32s\"" " (expect \""
+			BOOT_PART_TYPE "\")\n",
+			info.type);
+		bootstage_error(BOOTSTAGE_ID_IDE_PART_TYPE);
+		return 1;
+	}
+	bootstage_mark(BOOTSTAGE_ID_IDE_PART_TYPE);
+
+	printf("\nLoading from disk device %d, partition %d: "
+	       "Name: %.32s  Type: %.32s\n", dev, part, info.name, info.type);
+
+	debug("First Block: %ld,  # of blocks: %ld, Block Size: %ld\n",
+	      info.start, info.size, info.blksz);
+
+	if (dev_desc->block_read(dev, info.start, 1, (ulong *) addr) != 1) {
+		printf("** Read error on %d:%d\n", dev, part);
+		bootstage_error(BOOTSTAGE_ID_IDE_PART_READ);
+		return 1;
+	}
+	bootstage_mark(BOOTSTAGE_ID_IDE_PART_READ);
+
+	switch (genimg_get_format((void *) addr)) {
+	case IMAGE_FORMAT_LEGACY:
+		hdr = (image_header_t *) addr;
+
+		bootstage_mark(BOOTSTAGE_ID_IDE_FORMAT);
+
+		if (!image_check_hcrc(hdr)) {
+			puts("\n** Bad Header Checksum **\n");
+			bootstage_error(BOOTSTAGE_ID_IDE_CHECKSUM);
+			return 1;
+		}
+		bootstage_mark(BOOTSTAGE_ID_IDE_CHECKSUM);
+
+		image_print_contents(hdr);
+
+		cnt = image_get_image_size(hdr);
+		break;
+#if defined(CONFIG_FIT)
+	case IMAGE_FORMAT_FIT:
+		fit_hdr = (const void *) addr;
+		puts("Fit image detected...\n");
+
+		cnt = fit_get_size(fit_hdr);
+		break;
+#endif
+	default:
+		bootstage_error(BOOTSTAGE_ID_IDE_FORMAT);
+		puts("** Unknown image type\n");
+		return 1;
+	}
+
+	cnt += info.blksz - 1;
+	cnt /= info.blksz;
+	cnt -= 1;
+
+	if (dev_desc->block_read(dev, info.start + 1, cnt,
+					 (ulong *)(addr + info.blksz)) != cnt) {
+		printf("** Read error on %d:%d\n", dev, part);
+		bootstage_error(BOOTSTAGE_ID_IDE_READ);
+		return 1;
+	}
+	bootstage_mark(BOOTSTAGE_ID_IDE_READ);
+
+#if defined(CONFIG_FIT)
+	/* This cannot be done earlier, 
+	 * we need complete FIT image in RAM first */
+	if (genimg_get_format((void *) addr) == IMAGE_FORMAT_FIT) {
+		if (!fit_check_format(fit_hdr)) {
+			bootstage_error(BOOTSTAGE_ID_IDE_FIT_READ);
+			puts("** Bad FIT image format\n");
+			return 1;
+		}
+		bootstage_mark(BOOTSTAGE_ID_IDE_FIT_READ_OK);
+		fit_print_contents(fit_hdr);
+	}
+#endif
+
+	flush_cache(addr, (cnt+1)*info.blksz);
+
+	/* Loading ok, update default load address */
+	load_addr = addr;
+
+	return bootm_maybe_autostart(cmdtp, argv[0]);
+}
+
diff --git a/common/cmd_ide.c b/common/cmd_ide.c
index f5b6c7b..6e1e568 100644
--- a/common/cmd_ide.c
+++ b/common/cmd_ide.c
@@ -334,156 +334,7 @@ int do_ide(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
 
 int do_diskboot(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
 {
-	char *boot_device = NULL;
-	char *ep;
-	int dev, part = 0;
-	ulong addr, cnt;
-	disk_partition_t info;
-	image_header_t *hdr;
-
-#if defined(CONFIG_FIT)
-	const void *fit_hdr = NULL;
-#endif
-
-	bootstage_mark(BOOTSTAGE_ID_IDE_START);
-	switch (argc) {
-	case 1:
-		addr = CONFIG_SYS_LOAD_ADDR;
-		boot_device = getenv("bootdevice");
-		break;
-	case 2:
-		addr = simple_strtoul(argv[1], NULL, 16);
-		boot_device = getenv("bootdevice");
-		break;
-	case 3:
-		addr = simple_strtoul(argv[1], NULL, 16);
-		boot_device = argv[2];
-		break;
-	default:
-		bootstage_error(BOOTSTAGE_ID_IDE_ADDR);
-		return CMD_RET_USAGE;
-	}
-	bootstage_mark(BOOTSTAGE_ID_IDE_ADDR);
-
-	if (!boot_device) {
-		puts("\n** No boot device **\n");
-		bootstage_error(BOOTSTAGE_ID_IDE_BOOT_DEVICE);
-		return 1;
-	}
-	bootstage_mark(BOOTSTAGE_ID_IDE_BOOT_DEVICE);
-
-	dev = simple_strtoul(boot_device, &ep, 16);
-
-	if (ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN) {
-		printf("\n** Device %d not available\n", dev);
-		bootstage_error(BOOTSTAGE_ID_IDE_TYPE);
-		return 1;
-	}
-	bootstage_mark(BOOTSTAGE_ID_IDE_TYPE);
-
-	if (*ep) {
-		if (*ep != ':') {
-			puts("\n** Invalid boot device, use `dev[:part]' **\n");
-			bootstage_error(BOOTSTAGE_ID_IDE_PART);
-			return 1;
-		}
-		part = simple_strtoul(++ep, NULL, 16);
-	}
-	bootstage_mark(BOOTSTAGE_ID_IDE_PART);
-
-	if (get_partition_info(&ide_dev_desc[dev], part, &info)) {
-		bootstage_error(BOOTSTAGE_ID_IDE_PART_INFO);
-		return 1;
-	}
-	bootstage_mark(BOOTSTAGE_ID_IDE_PART_INFO);
-
-	if ((strncmp((char *)info.type, BOOT_PART_TYPE, sizeof(info.type)) != 0)
-	    &&
-	    (strncmp((char *)info.type, BOOT_PART_COMP, sizeof(info.type)) != 0)
-	   ) {
-		printf("\n** Invalid partition type \"%.32s\"" " (expect \""
-			BOOT_PART_TYPE "\")\n",
-			info.type);
-		bootstage_error(BOOTSTAGE_ID_IDE_PART_TYPE);
-		return 1;
-	}
-	bootstage_mark(BOOTSTAGE_ID_IDE_PART_TYPE);
-
-	printf("\nLoading from IDE device %d, partition %d: "
-	       "Name: %.32s  Type: %.32s\n", dev, part, info.name, info.type);
-
-	debug("First Block: %ld,  # of blocks: %ld, Block Size: %ld\n",
-	      info.start, info.size, info.blksz);
-
-	if (ide_dev_desc[dev].
-	    block_read(dev, info.start, 1, (ulong *) addr) != 1) {
-		printf("** Read error on %d:%d\n", dev, part);
-		bootstage_error(BOOTSTAGE_ID_IDE_PART_READ);
-		return 1;
-	}
-	bootstage_mark(BOOTSTAGE_ID_IDE_PART_READ);
-
-	switch (genimg_get_format((void *) addr)) {
-	case IMAGE_FORMAT_LEGACY:
-		hdr = (image_header_t *) addr;
-
-		bootstage_mark(BOOTSTAGE_ID_IDE_FORMAT);
-
-		if (!image_check_hcrc(hdr)) {
-			puts("\n** Bad Header Checksum **\n");
-			bootstage_error(BOOTSTAGE_ID_IDE_CHECKSUM);
-			return 1;
-		}
-		bootstage_mark(BOOTSTAGE_ID_IDE_CHECKSUM);
-
-		image_print_contents(hdr);
-
-		cnt = image_get_image_size(hdr);
-		break;
-#if defined(CONFIG_FIT)
-	case IMAGE_FORMAT_FIT:
-		fit_hdr = (const void *) addr;
-		puts("Fit image detected...\n");
-
-		cnt = fit_get_size(fit_hdr);
-		break;
-#endif
-	default:
-		bootstage_error(BOOTSTAGE_ID_IDE_FORMAT);
-		puts("** Unknown image type\n");
-		return 1;
-	}
-
-	cnt += info.blksz - 1;
-	cnt /= info.blksz;
-	cnt -= 1;
-
-	if (ide_dev_desc[dev].block_read(dev, info.start + 1, cnt,
-					 (ulong *)(addr + info.blksz)) != cnt) {
-		printf("** Read error on %d:%d\n", dev, part);
-		bootstage_error(BOOTSTAGE_ID_IDE_READ);
-		return 1;
-	}
-	bootstage_mark(BOOTSTAGE_ID_IDE_READ);
-
-#if defined(CONFIG_FIT)
-	/* This cannot be done earlier, we need complete FIT image in RAM first */
-	if (genimg_get_format((void *) addr) == IMAGE_FORMAT_FIT) {
-		if (!fit_check_format(fit_hdr)) {
-			bootstage_error(BOOTSTAGE_ID_IDE_FIT_READ);
-			puts("** Bad FIT image format\n");
-			return 1;
-		}
-		bootstage_mark(BOOTSTAGE_ID_IDE_FIT_READ_OK);
-		fit_print_contents(fit_hdr);
-	}
-#endif
-
-	/* Loading ok, update default load address */
-
-	load_addr = addr;
-
-	return bootm_maybe_autostart(cmdtp, argv[0]);
+	return common_diskboot(cmdtp, "ide", argc, argv);
 }
 
 /* ------------------------------------------------------------------------- */
diff --git a/common/cmd_scsi.c b/common/cmd_scsi.c
index d15b567..22d0119 100644
--- a/common/cmd_scsi.c
+++ b/common/cmd_scsi.c
@@ -206,128 +206,7 @@ block_dev_desc_t * scsi_get_dev(int dev)
  */
 int do_scsiboot (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 {
-	char *boot_device = NULL;
-	char *ep;
-	int dev, part = 0;
-	ulong addr, cnt;
-	disk_partition_t info;
-	image_header_t *hdr;
-#if defined(CONFIG_FIT)
-	const void *fit_hdr = NULL;
-#endif
-
-	switch (argc) {
-	case 1:
-		addr = CONFIG_SYS_LOAD_ADDR;
-		boot_device = getenv ("bootdevice");
-		break;
-	case 2:
-		addr = simple_strtoul(argv[1], NULL, 16);
-		boot_device = getenv ("bootdevice");
-		break;
-	case 3:
-		addr = simple_strtoul(argv[1], NULL, 16);
-		boot_device = argv[2];
-		break;
-	default:
-		return CMD_RET_USAGE;
-	}
-
-	if (!boot_device) {
-		puts ("\n** No boot device **\n");
-		return 1;
-	}
-
-	dev = simple_strtoul(boot_device, &ep, 16);
-	printf("booting from dev %d\n",dev);
-	if (scsi_dev_desc[dev].type == DEV_TYPE_UNKNOWN) {
-		printf ("\n** Device %d not available\n", dev);
-		return 1;
-	}
-
-	if (*ep) {
-		if (*ep != ':') {
-			puts ("\n** Invalid boot device, use `dev[:part]' **\n");
-			return 1;
-		}
-		part = simple_strtoul(++ep, NULL, 16);
-	}
-	if (get_partition_info (&scsi_dev_desc[dev], part, &info)) {
-		printf("error reading partinfo\n");
-		return 1;
-	}
-	if ((strncmp((char *)(info.type), BOOT_PART_TYPE, sizeof(info.type)) != 0) &&
-	    (strncmp((char *)(info.type), BOOT_PART_COMP, sizeof(info.type)) != 0)) {
-		printf ("\n** Invalid partition type \"%.32s\""
-			" (expect \"" BOOT_PART_TYPE "\")\n",
-			info.type);
-		return 1;
-	}
-
-	printf ("\nLoading from SCSI device %d, partition %d: "
-		"Name: %.32s  Type: %.32s\n",
-		dev, part, info.name, info.type);
-
-	debug ("First Block: %ld,  # of blocks: %ld, Block Size: %ld\n",
-		info.start, info.size, info.blksz);
-
-	if (scsi_read (dev, info.start, 1, (ulong *)addr) != 1) {
-		printf ("** Read error on %d:%d\n", dev, part);
-		return 1;
-	}
-
-	switch (genimg_get_format ((void *)addr)) {
-	case IMAGE_FORMAT_LEGACY:
-		hdr = (image_header_t *)addr;
-
-		if (!image_check_hcrc (hdr)) {
-			puts ("\n** Bad Header Checksum **\n");
-			return 1;
-		}
-
-		image_print_contents (hdr);
-		cnt = image_get_image_size (hdr);
-		break;
-#if defined(CONFIG_FIT)
-	case IMAGE_FORMAT_FIT:
-		fit_hdr = (const void *)addr;
-		puts ("Fit image detected...\n");
-
-		cnt = fit_get_size (fit_hdr);
-		break;
-#endif
-	default:
-		puts ("** Unknown image type\n");
-		return 1;
-	}
-
-	cnt += info.blksz - 1;
-	cnt /= info.blksz;
-	cnt -= 1;
-
-	if (scsi_read (dev, info.start+1, cnt,
-		      (ulong *)(addr+info.blksz)) != cnt) {
-		printf ("** Read error on %d:%d\n", dev, part);
-		return 1;
-	}
-
-#if defined(CONFIG_FIT)
-	/* This cannot be done earlier, we need complete FIT image in RAM first */
-	if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT) {
-		if (!fit_check_format (fit_hdr)) {
-			puts ("** Bad FIT image format\n");
-			return 1;
-		}
-		fit_print_contents (fit_hdr);
-	}
-#endif
-
-	/* Loading ok, update default load address */
-	load_addr = addr;
-
-	flush_cache (addr, (cnt+1)*info.blksz);
-
-	return bootm_maybe_autostart(cmdtp, argv[0]);
+	return common_diskboot(cmdtp, "scsi", argc, argv);
 }
 
 /*********************************************************************************
diff --git a/common/cmd_usb.c b/common/cmd_usb.c
index a8e3ae5..181e727 100644
--- a/common/cmd_usb.c
+++ b/common/cmd_usb.c
@@ -355,143 +355,7 @@ void usb_show_tree(struct usb_device *dev)
 #ifdef CONFIG_USB_STORAGE
 int do_usbboot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 {
-	char *boot_device = NULL;
-	char *ep;
-	int dev, part = 1;
-	ulong addr, cnt;
-	disk_partition_t info;
-	image_header_t *hdr;
-	block_dev_desc_t *stor_dev;
-#if defined(CONFIG_FIT)
-	const void *fit_hdr = NULL;
-#endif
-
-	switch (argc) {
-	case 1:
-		addr = CONFIG_SYS_LOAD_ADDR;
-		boot_device = getenv("bootdevice");
-		break;
-	case 2:
-		addr = simple_strtoul(argv[1], NULL, 16);
-		boot_device = getenv("bootdevice");
-		break;
-	case 3:
-		addr = simple_strtoul(argv[1], NULL, 16);
-		boot_device = argv[2];
-		break;
-	default:
-		return CMD_RET_USAGE;
-	}
-
-	if (!boot_device) {
-		puts("\n** No boot device **\n");
-		return 1;
-	}
-
-	dev = simple_strtoul(boot_device, &ep, 16);
-	stor_dev = usb_stor_get_dev(dev);
-	if (stor_dev == NULL || stor_dev->type == DEV_TYPE_UNKNOWN) {
-		printf("\n** Device %d not available\n", dev);
-		return 1;
-	}
-	if (stor_dev->block_read == NULL) {
-		printf("storage device not initialized. Use usb scan\n");
-		return 1;
-	}
-	if (*ep) {
-		if (*ep != ':') {
-			puts("\n** Invalid boot device, use `dev[:part]' **\n");
-			return 1;
-		}
-		part = simple_strtoul(++ep, NULL, 16);
-	}
-
-	if (get_partition_info(stor_dev, part, &info)) {
-		/* try to boot raw .... */
-		strncpy((char *)&info.type[0], BOOT_PART_TYPE,
-			sizeof(BOOT_PART_TYPE));
-		strncpy((char *)&info.name[0], "Raw", 4);
-		info.start = 0;
-		info.blksz = 0x200;
-		info.size = 2880;
-		printf("error reading partinfo...try to boot raw\n");
-	}
-	if ((strncmp((char *)info.type, BOOT_PART_TYPE,
-	    sizeof(info.type)) != 0) &&
-	    (strncmp((char *)info.type, BOOT_PART_COMP,
-	    sizeof(info.type)) != 0)) {
-		printf("\n** Invalid partition type \"%.32s\""
-			" (expect \"" BOOT_PART_TYPE "\")\n",
-			info.type);
-		return 1;
-	}
-	printf("\nLoading from USB device %d, partition %d: "
-		"Name: %.32s  Type: %.32s\n",
-		dev, part, info.name, info.type);
-
-	debug("First Block: %ld,  # of blocks: %ld, Block Size: %ld\n",
-		info.start, info.size, info.blksz);
-
-	if (stor_dev->block_read(dev, info.start, 1, (ulong *)addr) != 1) {
-		printf("** Read error on %d:%d\n", dev, part);
-		return 1;
-	}
-
-	switch (genimg_get_format((void *)addr)) {
-	case IMAGE_FORMAT_LEGACY:
-		hdr = (image_header_t *)addr;
-
-		if (!image_check_hcrc(hdr)) {
-			puts("\n** Bad Header Checksum **\n");
-			return 1;
-		}
-
-		image_print_contents(hdr);
-
-		cnt = image_get_image_size(hdr);
-		break;
-#if defined(CONFIG_FIT)
-	case IMAGE_FORMAT_FIT:
-		fit_hdr = (const void *)addr;
-		puts("Fit image detected...\n");
-
-		cnt = fit_get_size(fit_hdr);
-		break;
-#endif
-	default:
-		puts("** Unknown image type\n");
-		return 1;
-	}
-
-	cnt += info.blksz - 1;
-	cnt /= info.blksz;
-	cnt -= 1;
-
-	if (stor_dev->block_read(dev, info.start+1, cnt,
-		      (ulong *)(addr+info.blksz)) != cnt) {
-		printf("\n** Read error on %d:%d\n", dev, part);
-		return 1;
-	}
-
-#if defined(CONFIG_FIT)
-	/* This cannot be done earlier, we need complete FIT image in RAM
-	 * first
-	 */
-	if (genimg_get_format((void *)addr) == IMAGE_FORMAT_FIT) {
-		if (!fit_check_format(fit_hdr)) {
-			puts("** Bad FIT image format\n");
-			return 1;
-		}
-		fit_print_contents(fit_hdr);
-	}
-#endif
-
-	/* Loading ok, update default load address */
-	load_addr = addr;
-
-	flush_cache(addr, (cnt+1)*info.blksz);
-
-	return bootm_maybe_autostart(cmdtp, argv[0]);
+	return common_diskboot(cmdtp, "usb", argc, argv);
 }
 #endif /* CONFIG_USB_STORAGE */
 
diff --git a/include/command.h b/include/command.h
index 6e1bdc2..1f06aa1 100644
--- a/include/command.h
+++ b/include/command.h
@@ -110,6 +110,10 @@ static inline int bootm_maybe_autostart(cmd_tbl_t *cmdtp, const char *cmd)
 	return 0;
 }
 #endif
+
+extern int common_diskboot(cmd_tbl_t *cmdtp, const char *intf, int argc,
+			   char *const argv[]);
+
 extern int do_reset(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
 
 /*
-- 
1.7.9.5

^ permalink raw reply related	[flat|nested] 20+ messages in thread

* [U-Boot] [PATCH 2/9] disk/part: check bootable flag for DOS partitions
  2012-08-23 21:31 [U-Boot] [PATCH 0/9] Auto partition selection and fs partition consolidation Rob Herring
  2012-08-23 21:31 ` [U-Boot] [PATCH 1/9] combine block device load commands into common function Rob Herring
@ 2012-08-23 21:31 ` Rob Herring
  2012-08-23 21:31 ` [U-Boot] [PATCH 3/9] disk/part: introduce get_device_and_partition Rob Herring
                   ` (6 subsequent siblings)
  8 siblings, 0 replies; 20+ messages in thread
From: Rob Herring @ 2012-08-23 21:31 UTC (permalink / raw)
  To: u-boot

From: Rob Herring <rob.herring@calxeda.com>

Determine which partitions are bootable/active. In the partition listing,
print "Boot" for partitions with the bootable/active flag set.

Signed-off-by: Rob Herring <rob.herring@calxeda.com>
---
 disk/part_dos.c |   11 +++++++++--
 include/part.h  |    1 +
 2 files changed, 10 insertions(+), 2 deletions(-)

diff --git a/disk/part_dos.c b/disk/part_dos.c
index a43dd9c..24ac00c 100644
--- a/disk/part_dos.c
+++ b/disk/part_dos.c
@@ -60,14 +60,20 @@ static inline int is_extended(int part_type)
 	    part_type == 0x85);
 }
 
+static inline int is_bootable(dos_partition_t *p)
+{
+	return p->boot_ind == 0x80;
+}
+
 static void print_one_part (dos_partition_t *p, int ext_part_sector, int part_num)
 {
 	int lba_start = ext_part_sector + le32_to_int (p->start4);
 	int lba_size  = le32_to_int (p->size4);
 
-	printf ("%5d\t\t%10d\t%10d\t%2x%s\n",
+	printf("%5d\t\t%10d\t%10d\t%2x%s%s\n",
 		part_num, lba_start, lba_size, p->sys_ind,
-		(is_extended (p->sys_ind) ? " Extd" : ""));
+		(is_extended(p->sys_ind) ? " Extd" : ""),
+		(is_bootable(p) ? " Boot" : ""));
 }
 
 static int test_block_type(unsigned char *buffer)
@@ -222,6 +228,7 @@ static int get_partition_info_extended (block_dev_desc_t *dev_desc, int ext_part
 			}
 			/* sprintf(info->type, "%d, pt->sys_ind); */
 			sprintf ((char *)info->type, "U-Boot");
+			info->bootable = is_bootable(pt);
 			return 0;
 		}
 
diff --git a/include/part.h b/include/part.h
index e1478f4..447f69d 100644
--- a/include/part.h
+++ b/include/part.h
@@ -93,6 +93,7 @@ typedef struct disk_partition {
 	ulong	blksz;		/* block size in bytes			*/
 	uchar	name[32];	/* partition name			*/
 	uchar	type[32];	/* string type description		*/
+	int	bootable;	/* Active/Bootable flag is set		*/
 } disk_partition_t;
 
 /* Misc _get_dev functions */
-- 
1.7.9.5

^ permalink raw reply related	[flat|nested] 20+ messages in thread

* [U-Boot] [PATCH 3/9] disk/part: introduce get_device_and_partition
  2012-08-23 21:31 [U-Boot] [PATCH 0/9] Auto partition selection and fs partition consolidation Rob Herring
  2012-08-23 21:31 ` [U-Boot] [PATCH 1/9] combine block device load commands into common function Rob Herring
  2012-08-23 21:31 ` [U-Boot] [PATCH 2/9] disk/part: check bootable flag for DOS partitions Rob Herring
@ 2012-08-23 21:31 ` Rob Herring
  2012-08-23 22:36   ` Stephen Warren
  2012-09-21 14:08   ` [U-Boot] [PATCH v2 " Rob Herring
  2012-08-23 21:31 ` [U-Boot] [PATCH 4/9] ext4: remove init_fs/deinit_fs Rob Herring
                   ` (5 subsequent siblings)
  8 siblings, 2 replies; 20+ messages in thread
From: Rob Herring @ 2012-08-23 21:31 UTC (permalink / raw)
  To: u-boot

From: Rob Herring <rob.herring@calxeda.com>

All block device related commands (scsiboot, fatload, ext2ls, etc.) have
simliar duplicated device and partition parsing and selection code. This
adds a common function to replace various implementations.

The new function has some enhancements over current versions. If no device
or partition is specified on the command line, the bootdevice env variable
will be used (scsiboot does this). If the partition is not specified and
the device has partitions, then the first bootable partition will be used.
If a bootable partition is not found, the first valid partition is used.
The ret value is not needed since part will be zero when no partition is
found.

Signed-off-by: Rob Herring <rob.herring@calxeda.com>
---
 disk/part.c    |   90 ++++++++++++++++++++++++++++++++++++++++++++++++++++++--
 include/part.h |   13 ++++++--
 2 files changed, 99 insertions(+), 4 deletions(-)

diff --git a/disk/part.c b/disk/part.c
index 76f3939..1284e1a 100644
--- a/disk/part.c
+++ b/disk/part.c
@@ -70,7 +70,7 @@ static const struct block_drvr block_drvr[] = {
 
 DECLARE_GLOBAL_DATA_PTR;
 
-block_dev_desc_t *get_dev(char* ifname, int dev)
+block_dev_desc_t *get_dev(const char *ifname, int dev)
 {
 	const struct block_drvr *drvr = block_drvr;
 	block_dev_desc_t* (*reloc_get_dev)(int dev);
@@ -97,7 +97,7 @@ block_dev_desc_t *get_dev(char* ifname, int dev)
 	return NULL;
 }
 #else
-block_dev_desc_t *get_dev(char* ifname, int dev)
+block_dev_desc_t *get_dev(const char *ifname, int dev)
 {
 	return NULL;
 }
@@ -288,6 +288,7 @@ void init_part (block_dev_desc_t * dev_desc)
 	    return;
 	}
 #endif
+	dev_desc->part_type = PART_TYPE_UNKNOWN;
 }
 
 
@@ -433,3 +434,88 @@ void print_part (block_dev_desc_t * dev_desc)
 #endif
 
 #endif
+
+int get_device_and_partition(const char *ifname, const char *dev_str,
+			     block_dev_desc_t **dev_desc,
+			     disk_partition_t *info)
+{
+	int ret;
+	char *ep;
+	int dev;
+	block_dev_desc_t *desc;
+	int p;
+	int part = 0;
+	char *part_str;
+	disk_partition_t *best_part = NULL;
+
+	if (dev_str)
+		dev = simple_strtoul(dev_str, &ep, 16);
+
+	if (!dev_str || (dev_str == ep)) {
+		dev_str = getenv("bootdevice");
+		if (dev_str)
+			dev = simple_strtoul(dev_str, &ep, 16);
+		if (!dev_str || (dev_str == ep))
+			goto err;
+	}
+
+	desc = get_dev(ifname, dev);
+	if (!desc || (desc->type == DEV_TYPE_UNKNOWN))
+		goto err;
+
+	if (desc->part_type == PART_TYPE_UNKNOWN) {
+		/* disk doesn't use partition table */
+		if (!desc->lba) {
+			printf("**Bad disk size - %s %d:0 **\n", ifname, dev);
+			return -1;
+		}
+		info->start = 0;
+		info->size = desc->lba;
+		info->blksz = desc->blksz;
+
+		*dev_desc = desc;
+		return 0;
+	}
+
+	part_str = strchr(dev_str, ':');
+	if (part_str) {
+		part = (int)simple_strtoul(++part_str, NULL, 16);
+		ret = get_partition_info(desc, part, info);
+	} else {
+		/* find the first bootable partition. If none are bootable,
+		 * fall back to the first valid partition */
+		for (p = 1; p < 16; p++) {
+			ret = get_partition_info(desc, p, info);
+			if (ret)
+				continue;
+
+			if (!best_part || info->bootable) {
+				best_part = info;
+				part = p;
+			}
+
+			if (info->bootable)
+				break;
+		}
+		info = best_part;
+		if (part)
+			ret = 0;
+	}
+	if (ret) {
+		printf("** Invalid partition %d, use `dev[:part]' **\n", part);
+		return -1;
+	}
+	if (strncmp((char *)info->type, BOOT_PART_TYPE, sizeof(info->type)) != 0) {
+		printf("** Invalid partition type \"%.32s\""
+			" (expect \"" BOOT_PART_TYPE "\")\n",
+			info->type);
+		return -1;
+	}
+
+	*dev_desc = desc;
+	return part;
+
+ err:
+	puts("** Invalid boot device, use `dev[:part]' **\n");
+	return -1;
+}
diff --git a/include/part.h b/include/part.h
index 447f69d..a6d06f3 100644
--- a/include/part.h
+++ b/include/part.h
@@ -98,7 +98,7 @@ typedef struct disk_partition {
 
 /* Misc _get_dev functions */
 #ifdef CONFIG_PARTITIONS
-block_dev_desc_t* get_dev(char* ifname, int dev);
+block_dev_desc_t *get_dev(const char *ifname, int dev);
 block_dev_desc_t* ide_get_dev(int dev);
 block_dev_desc_t* sata_get_dev(int dev);
 block_dev_desc_t* scsi_get_dev(int dev);
@@ -112,8 +112,12 @@ int get_partition_info (block_dev_desc_t * dev_desc, int part, disk_partition_t
 void print_part (block_dev_desc_t *dev_desc);
 void  init_part (block_dev_desc_t *dev_desc);
 void dev_print(block_dev_desc_t *dev_desc);
+int get_device_and_partition(const char *ifname, const char *dev_str,
+			     block_dev_desc_t **dev_desc,
+			     disk_partition_t *info);
 #else
-static inline block_dev_desc_t* get_dev(char* ifname, int dev) { return NULL; }
+static inline block_dev_desc_t *get_dev(const char *ifname, int dev)
+{ return NULL; }
 static inline block_dev_desc_t* ide_get_dev(int dev) { return NULL; }
 static inline block_dev_desc_t* sata_get_dev(int dev) { return NULL; }
 static inline block_dev_desc_t* scsi_get_dev(int dev) { return NULL; }
@@ -127,6 +131,11 @@ static inline int get_partition_info (block_dev_desc_t * dev_desc, int part,
 static inline void print_part (block_dev_desc_t *dev_desc) {}
 static inline void  init_part (block_dev_desc_t *dev_desc) {}
 static inline void dev_print(block_dev_desc_t *dev_desc) {}
+static inline int get_device_and_partition(const char *ifname,
+					   const char *dev_str,
+					   block_dev_desc_t **dev_desc,
+					   disk_partition_t *info)
+{ *dev_desc = NULL; return -1; }
 #endif
 
 #ifdef CONFIG_MAC_PARTITION
-- 
1.7.9.5

^ permalink raw reply related	[flat|nested] 20+ messages in thread

* [U-Boot] [PATCH 4/9] ext4: remove init_fs/deinit_fs
  2012-08-23 21:31 [U-Boot] [PATCH 0/9] Auto partition selection and fs partition consolidation Rob Herring
                   ` (2 preceding siblings ...)
  2012-08-23 21:31 ` [U-Boot] [PATCH 3/9] disk/part: introduce get_device_and_partition Rob Herring
@ 2012-08-23 21:31 ` Rob Herring
  2012-08-23 21:31 ` [U-Boot] [PATCH 5/9] cmd_extX: use common get_device_and_partition function Rob Herring
                   ` (4 subsequent siblings)
  8 siblings, 0 replies; 20+ messages in thread
From: Rob Herring @ 2012-08-23 21:31 UTC (permalink / raw)
  To: u-boot

From: Rob Herring <rob.herring@calxeda.com>

There's no real need to expose this and it can be removed by using a static
allocation.

Signed-off-by: Rob Herring <rob.herring@calxeda.com>
---
 common/cmd_ext4.c       |   10 +++-------
 common/cmd_ext_common.c |   19 +++----------------
 fs/ext4/ext4fs.c        |   36 ++----------------------------------
 include/ext4fs.h        |    2 --
 include/zfs_common.h    |    2 --
 5 files changed, 8 insertions(+), 61 deletions(-)

diff --git a/common/cmd_ext4.c b/common/cmd_ext4.c
index 77094c4..e92c02f 100644
--- a/common/cmd_ext4.c
+++ b/common/cmd_ext4.c
@@ -151,8 +151,6 @@ int do_ext4_write(cmd_tbl_t *cmdtp, int flag, int argc,
 		printf("Block device %s %d not supported\n", argv[1], dev);
 		return 1;
 	}
-	if (init_fs(ext4_dev_desc))
-		return 1;
 
 	fs = get_fs();
 	if (*ep) {
@@ -173,21 +171,21 @@ int do_ext4_write(cmd_tbl_t *cmdtp, int flag, int argc,
 	file_size = simple_strtoul(argv[5], NULL, 10);
 
 	/* set the device as block device */
-	part_length = ext4fs_set_blk_dev(fs->dev_desc, part);
+	part_length = ext4fs_set_blk_dev(ext4_dev_desc, part);
 	if (part_length == 0) {
 		printf("Bad partition - %s %d:%lu\n", argv[1], dev, part);
 		goto fail;
 	}
 
 	/* register the device and partition */
-	if (ext4_register_device(fs->dev_desc, part) != 0) {
+	if (ext4_register_device(ext4_dev_desc, part) != 0) {
 		printf("Unable to use %s %d:%lu for fattable\n",
 		       argv[1], dev, part);
 		goto fail;
 	}
 
 	/* get the partition information */
-	if (!get_partition_info(fs->dev_desc, part, &info)) {
+	if (!get_partition_info(ext4_dev_desc, part, &info)) {
 		total_sector = (info.size * info.blksz) / SECTOR_SIZE;
 		fs->total_sect = total_sector;
 	} else {
@@ -207,13 +205,11 @@ int do_ext4_write(cmd_tbl_t *cmdtp, int flag, int argc,
 		goto fail;
 	}
 	ext4fs_close();
-	deinit_fs(fs->dev_desc);
 
 	return 0;
 
 fail:
 	ext4fs_close();
-	deinit_fs(fs->dev_desc);
 
 	return 1;
 }
diff --git a/common/cmd_ext_common.c b/common/cmd_ext_common.c
index 56ee9a5..8972ccc 100644
--- a/common/cmd_ext_common.c
+++ b/common/cmd_ext_common.c
@@ -75,7 +75,6 @@ int do_ext_load(cmd_tbl_t *cmdtp, int flag, int argc,
 	ulong part_length;
 	int filelen;
 	disk_partition_t info;
-	struct ext_filesystem *fs;
 	char buf[12];
 	unsigned long count;
 	const char *addr_str;
@@ -117,10 +116,7 @@ int do_ext_load(cmd_tbl_t *cmdtp, int flag, int argc,
 		printf("** Block device %s %d not supported\n", argv[1], dev);
 		return 1;
 	}
-	if (init_fs(ext4_dev_desc))
-		return 1;
 
-	fs = get_fs();
 	if (*ep) {
 		if (*ep != ':') {
 			puts("** Invalid boot device, use `dev[:part]' **\n");
@@ -130,7 +126,7 @@ int do_ext_load(cmd_tbl_t *cmdtp, int flag, int argc,
 	}
 
 	if (part != 0) {
-		if (get_partition_info(fs->dev_desc, part, &info)) {
+		if (get_partition_info(ext4_dev_desc, part, &info)) {
 			printf("** Bad partition %lu **\n", part);
 			goto fail;
 		}
@@ -149,7 +145,7 @@ int do_ext_load(cmd_tbl_t *cmdtp, int flag, int argc,
 		       filename, argv[1], dev);
 	}
 
-	part_length = ext4fs_set_blk_dev(fs->dev_desc, part);
+	part_length = ext4fs_set_blk_dev(ext4_dev_desc, part);
 	if (part_length == 0) {
 		printf("**Bad partition - %s %d:%lu **\n", argv[1], dev, part);
 		ext4fs_close();
@@ -180,7 +176,6 @@ int do_ext_load(cmd_tbl_t *cmdtp, int flag, int argc,
 	}
 
 	ext4fs_close();
-	deinit_fs(fs->dev_desc);
 	/* Loading ok, update default load address */
 	load_addr = addr;
 
@@ -190,7 +185,6 @@ int do_ext_load(cmd_tbl_t *cmdtp, int flag, int argc,
 
 	return 0;
 fail:
-	deinit_fs(fs->dev_desc);
 	return 1;
 }
 
@@ -200,7 +194,6 @@ int do_ext_ls(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
 	int dev;
 	unsigned long part = 1;
 	char *ep;
-	struct ext_filesystem *fs;
 	int part_length;
 	if (argc < 3)
 		return cmd_usage(cmdtp);
@@ -214,10 +207,6 @@ int do_ext_ls(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
 		return 1;
 	}
 
-	if (init_fs(ext4_dev_desc))
-		return 1;
-
-	fs = get_fs();
 	if (*ep) {
 		if (*ep != ':') {
 			puts("\n** Invalid boot device, use `dev[:part]' **\n");
@@ -229,7 +218,7 @@ int do_ext_ls(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
 	if (argc == 4)
 		filename = argv[3];
 
-	part_length = ext4fs_set_blk_dev(fs->dev_desc, part);
+	part_length = ext4fs_set_blk_dev(ext4_dev_desc, part);
 	if (part_length == 0) {
 		printf("** Bad partition - %s %d:%lu **\n", argv[1], dev, part);
 		ext4fs_close();
@@ -250,10 +239,8 @@ int do_ext_ls(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
 	};
 
 	ext4fs_close();
-	deinit_fs(fs->dev_desc);
 	return 0;
 
 fail:
-	deinit_fs(fs->dev_desc);
 	return 1;
 }
diff --git a/fs/ext4/ext4fs.c b/fs/ext4/ext4fs.c
index 114c2a2..c366e6f 100644
--- a/fs/ext4/ext4fs.c
+++ b/fs/ext4/ext4fs.c
@@ -44,43 +44,11 @@
 
 int ext4fs_symlinknest;
 block_dev_desc_t *ext4_dev_desc;
+struct ext_filesystem ext_fs;
 
 struct ext_filesystem *get_fs(void)
 {
-	if (ext4_dev_desc == NULL || ext4_dev_desc->priv == NULL)
-		printf("Invalid Input Arguments %s\n", __func__);
-
-	return ext4_dev_desc->priv;
-}
-
-int init_fs(block_dev_desc_t *dev_desc)
-{
-	struct ext_filesystem *fs;
-	if (dev_desc == NULL) {
-		printf("Invalid Input Arguments %s\n", __func__);
-		return -EINVAL;
-	}
-
-	fs = zalloc(sizeof(struct ext_filesystem));
-	if (fs == NULL) {
-		printf("malloc failed: %s\n", __func__);
-		return -ENOMEM;
-	}
-
-	fs->dev_desc = dev_desc;
-	dev_desc->priv = fs;
-
-	return 0;
-}
-
-void deinit_fs(block_dev_desc_t *dev_desc)
-{
-	if (dev_desc == NULL) {
-		printf("Invalid Input Arguments %s\n", __func__);
-		return;
-	}
-	free(dev_desc->priv);
-	dev_desc->priv = NULL;
+	return &ext_fs;
 }
 
 void ext4fs_free_node(struct ext2fs_node *node, struct ext2fs_node *currroot)
diff --git a/include/ext4fs.h b/include/ext4fs.h
index ab2983c..6ad008c 100644
--- a/include/ext4fs.h
+++ b/include/ext4fs.h
@@ -130,8 +130,6 @@ int ext4fs_write(const char *fname, unsigned char *buffer,
 #endif
 
 struct ext_filesystem *get_fs(void);
-int init_fs(block_dev_desc_t *dev_desc);
-void deinit_fs(block_dev_desc_t *dev_desc);
 int ext4fs_open(const char *filename);
 int ext4fs_read(char *buf, unsigned len);
 int ext4fs_mount(unsigned part_length);
diff --git a/include/zfs_common.h b/include/zfs_common.h
index 04e73d0..628231e 100644
--- a/include/zfs_common.h
+++ b/include/zfs_common.h
@@ -94,8 +94,6 @@ struct zfs_dirhook_info {
 
 
 struct zfs_filesystem *zfsget_fs(void);
-int init_fs(block_dev_desc_t *dev_desc);
-void deinit_fs(block_dev_desc_t *dev_desc);
 int zfs_open(zfs_file_t, const char *filename);
 uint64_t zfs_read(zfs_file_t, char *buf, uint64_t len);
 struct zfs_data *zfs_mount(device_t);
-- 
1.7.9.5

^ permalink raw reply related	[flat|nested] 20+ messages in thread

* [U-Boot] [PATCH 5/9] cmd_extX: use common get_device_and_partition function
  2012-08-23 21:31 [U-Boot] [PATCH 0/9] Auto partition selection and fs partition consolidation Rob Herring
                   ` (3 preceding siblings ...)
  2012-08-23 21:31 ` [U-Boot] [PATCH 4/9] ext4: remove init_fs/deinit_fs Rob Herring
@ 2012-08-23 21:31 ` Rob Herring
  2012-08-23 21:31 ` [U-Boot] [PATCH 6/9] cmd_fat: " Rob Herring
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 20+ messages in thread
From: Rob Herring @ 2012-08-23 21:31 UTC (permalink / raw)
  To: u-boot

From: Rob Herring <rob.herring@calxeda.com>

Convert ext2/4 load, ls, and write functions to use common device and
partition parsing function. With the common function "dev:part" can come
from the environment and a '-' can be used in that case.

Signed-off-by: Rob Herring <rob.herring@calxeda.com>
---
 common/cmd_ext4.c       |  102 ++++-------------------------------------------
 common/cmd_ext_common.c |   95 +++++++++++--------------------------------
 fs/ext4/dev.c           |   32 ++++++---------
 fs/ext4/ext4_common.h   |    1 -
 fs/ext4/ext4fs.c        |    1 -
 include/ext4fs.h        |    3 +-
 include/ext_common.h    |    2 +
 7 files changed, 46 insertions(+), 190 deletions(-)

diff --git a/common/cmd_ext4.c b/common/cmd_ext4.c
index e92c02f..48f9ba3 100644
--- a/common/cmd_ext4.c
+++ b/common/cmd_ext4.c
@@ -56,21 +56,6 @@
 #include <usb.h>
 #endif
 
-#if !defined(CONFIG_DOS_PARTITION) && !defined(CONFIG_EFI_PARTITION)
-#error DOS or EFI partition support must be selected
-#endif
-
-uint64_t total_sector;
-uint64_t part_offset;
-#if defined(CONFIG_CMD_EXT4_WRITE)
-static uint64_t part_size;
-static uint16_t cur_part = 1;
-#endif
-
-#define DOS_PART_MAGIC_OFFSET		0x1fe
-#define DOS_FS_TYPE_OFFSET		0x36
-#define DOS_FS32_TYPE_OFFSET		0x52
-
 int do_ext4_load(cmd_tbl_t *cmdtp, int flag, int argc,
 						char *const argv[])
 {
@@ -89,77 +74,24 @@ int do_ext4_ls(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
 }
 
 #if defined(CONFIG_CMD_EXT4_WRITE)
-static int ext4_register_device(block_dev_desc_t *dev_desc, int part_no)
-{
-	unsigned char buffer[SECTOR_SIZE];
-	disk_partition_t info;
-
-	if (!dev_desc->block_read)
-		return -1;
-
-	/* check if we have a MBR (on floppies we have only a PBR) */
-	if (dev_desc->block_read(dev_desc->dev, 0, 1, (ulong *) buffer) != 1) {
-		printf("** Can't read from device %d **\n", dev_desc->dev);
-		return -1;
-	}
-	if (buffer[DOS_PART_MAGIC_OFFSET] != 0x55 ||
-	    buffer[DOS_PART_MAGIC_OFFSET + 1] != 0xaa) {
-		/* no signature found */
-		return -1;
-	}
-
-	/* First we assume there is a MBR */
-	if (!get_partition_info(dev_desc, part_no, &info)) {
-		part_offset = info.start;
-		cur_part = part_no;
-		part_size = info.size;
-	} else if ((strncmp((char *)&buffer[DOS_FS_TYPE_OFFSET],
-			    "FAT", 3) == 0) || (strncmp((char *)&buffer
-							[DOS_FS32_TYPE_OFFSET],
-							"FAT32", 5) == 0)) {
-		/* ok, we assume we are on a PBR only */
-		cur_part = 1;
-		part_offset = 0;
-	} else {
-		printf("** Partition %d not valid on device %d **\n",
-		       part_no, dev_desc->dev);
-		return -1;
-	}
-
-	return 0;
-}
-
 int do_ext4_write(cmd_tbl_t *cmdtp, int flag, int argc,
 				char *const argv[])
 {
 	const char *filename = "/";
-	int part_length;
-	unsigned long part = 1;
-	int dev;
-	char *ep;
+	int dev, part;
 	unsigned long ram_address;
 	unsigned long file_size;
 	disk_partition_t info;
-	struct ext_filesystem *fs;
+	block_dev_desc_t *dev_desc;
 
 	if (argc < 6)
 		return cmd_usage(cmdtp);
 
-	dev = (int)simple_strtoul(argv[2], &ep, 16);
-	ext4_dev_desc = get_dev(argv[1], dev);
-	if (ext4_dev_desc == NULL) {
-		printf("Block device %s %d not supported\n", argv[1], dev);
+	part = get_device_and_partition(argv[1], argv[2], &dev_desc, &info);
+	if (part < 0)
 		return 1;
-	}
 
-	fs = get_fs();
-	if (*ep) {
-		if (*ep != ':') {
-			puts("Invalid boot device, use `dev[:part]'\n");
-			goto fail;
-		}
-		part = simple_strtoul(++ep, NULL, 16);
-	}
+	dev = dev_desc->dev;
 
 	/* get the filename */
 	filename = argv[3];
@@ -171,30 +103,10 @@ int do_ext4_write(cmd_tbl_t *cmdtp, int flag, int argc,
 	file_size = simple_strtoul(argv[5], NULL, 10);
 
 	/* set the device as block device */
-	part_length = ext4fs_set_blk_dev(ext4_dev_desc, part);
-	if (part_length == 0) {
-		printf("Bad partition - %s %d:%lu\n", argv[1], dev, part);
-		goto fail;
-	}
-
-	/* register the device and partition */
-	if (ext4_register_device(ext4_dev_desc, part) != 0) {
-		printf("Unable to use %s %d:%lu for fattable\n",
-		       argv[1], dev, part);
-		goto fail;
-	}
-
-	/* get the partition information */
-	if (!get_partition_info(ext4_dev_desc, part, &info)) {
-		total_sector = (info.size * info.blksz) / SECTOR_SIZE;
-		fs->total_sect = total_sector;
-	} else {
-		printf("error : get partition info\n");
-		goto fail;
-	}
+	ext4fs_set_blk_dev(dev_desc, &info);
 
 	/* mount the filesystem */
-	if (!ext4fs_mount(part_length)) {
+	if (!ext4fs_mount(info.size)) {
 		printf("Bad ext4 partition %s %d:%lu\n", argv[1], dev, part);
 		goto fail;
 	}
diff --git a/common/cmd_ext_common.c b/common/cmd_ext_common.c
index 8972ccc..7d26944 100644
--- a/common/cmd_ext_common.c
+++ b/common/cmd_ext_common.c
@@ -68,13 +68,11 @@ int do_ext_load(cmd_tbl_t *cmdtp, int flag, int argc,
 						char *const argv[])
 {
 	char *filename = NULL;
-	char *ep;
-	int dev;
-	unsigned long part = 1;
+	int dev, part;
 	ulong addr = 0;
-	ulong part_length;
 	int filelen;
 	disk_partition_t info;
+	block_dev_desc_t *dev_desc;
 	char buf[12];
 	unsigned long count;
 	const char *addr_str;
@@ -110,50 +108,19 @@ int do_ext_load(cmd_tbl_t *cmdtp, int flag, int argc,
 		return 1;
 	}
 
-	dev = (int)simple_strtoul(argv[2], &ep, 16);
-	ext4_dev_desc = get_dev(argv[1], dev);
-	if (ext4_dev_desc == NULL) {
-		printf("** Block device %s %d not supported\n", argv[1], dev);
+	part = get_device_and_partition(argv[1], argv[2], &dev_desc, &info);
+	if (part < 0)
 		return 1;
-	}
 
-	if (*ep) {
-		if (*ep != ':') {
-			puts("** Invalid boot device, use `dev[:part]' **\n");
-			goto fail;
-		}
-		part = simple_strtoul(++ep, NULL, 16);
-	}
+	dev = dev_desc->dev;
+	printf("Loading file \"%s\" from %s device %d%c%c\n",
+		filename, argv[1], dev,
+		part ? ':' : ' ', part ? part + '0' : ' ');
 
-	if (part != 0) {
-		if (get_partition_info(ext4_dev_desc, part, &info)) {
-			printf("** Bad partition %lu **\n", part);
-			goto fail;
-		}
-
-		if (strncmp((char *)info.type, BOOT_PART_TYPE,
-			    strlen(BOOT_PART_TYPE)) != 0) {
-			printf("** Invalid partition type \"%s\""
-			       " (expect \"" BOOT_PART_TYPE "\")\n", info.type);
-			goto fail;
-		}
-		printf("Loading file \"%s\" "
-		       "from %s device %d:%lu %s\n",
-		       filename, argv[1], dev, part, info.name);
-	} else {
-		printf("Loading file \"%s\" from %s device %d\n",
-		       filename, argv[1], dev);
-	}
+	ext4fs_set_blk_dev(dev_desc, &info);
 
-	part_length = ext4fs_set_blk_dev(ext4_dev_desc, part);
-	if (part_length == 0) {
-		printf("**Bad partition - %s %d:%lu **\n", argv[1], dev, part);
-		ext4fs_close();
-		goto fail;
-	}
-
-	if (!ext4fs_mount(part_length)) {
-		printf("** Bad ext2 partition or disk - %s %d:%lu **\n",
+	if (!ext4fs_mount(info.size)) {
+		printf("** Bad ext2 partition or disk - %s %d:%d **\n",
 		       argv[1], dev, part);
 		ext4fs_close();
 		goto fail;
@@ -169,7 +136,7 @@ int do_ext_load(cmd_tbl_t *cmdtp, int flag, int argc,
 		filelen = count;
 
 	if (ext4fs_read((char *)addr, filelen) != filelen) {
-		printf("** Unable to read \"%s\" from %s %d:%lu **\n",
+		printf("** Unable to read \"%s\" from %s %d:%d **\n",
 		       filename, argv[1], dev, part);
 		ext4fs_close();
 		goto fail;
@@ -192,41 +159,25 @@ int do_ext_ls(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
 {
 	const char *filename = "/";
 	int dev;
-	unsigned long part = 1;
-	char *ep;
-	int part_length;
-	if (argc < 3)
-		return cmd_usage(cmdtp);
-
-	dev = (int)simple_strtoul(argv[2], &ep, 16);
+	int part;
+	block_dev_desc_t *dev_desc;
+	disk_partition_t info;
 
-	ext4_dev_desc = get_dev(argv[1], dev);
+	if (argc < 2)
+		return cmd_usage(cmdtp);
 
-	if (ext4_dev_desc == NULL) {
-		printf("\n** Block device %s %d not supported\n", argv[1], dev);
+	part = get_device_and_partition(argv[1], argv[2], &dev_desc, &info);
+	if (part < 0)
 		return 1;
-	}
-
-	if (*ep) {
-		if (*ep != ':') {
-			puts("\n** Invalid boot device, use `dev[:part]' **\n");
-			goto fail;
-		}
-		part = simple_strtoul(++ep, NULL, 16);
-	}
 
 	if (argc == 4)
 		filename = argv[3];
 
-	part_length = ext4fs_set_blk_dev(ext4_dev_desc, part);
-	if (part_length == 0) {
-		printf("** Bad partition - %s %d:%lu **\n", argv[1], dev, part);
-		ext4fs_close();
-		goto fail;
-	}
+	dev = dev_desc->dev;
+	ext4fs_set_blk_dev(dev_desc, &info);
 
-	if (!ext4fs_mount(part_length)) {
-		printf("** Bad ext2 partition or disk - %s %d:%lu **\n",
+	if (!ext4fs_mount(info.size)) {
+		printf("** Bad ext2 partition or disk - %s %d:%d **\n",
 		       argv[1], dev, part);
 		ext4fs_close();
 		goto fail;
diff --git a/fs/ext4/dev.c b/fs/ext4/dev.c
index fb62f24..5a3fbef 100644
--- a/fs/ext4/dev.c
+++ b/fs/ext4/dev.c
@@ -38,26 +38,20 @@
 
 #include <common.h>
 #include <config.h>
+#include <ext4fs.h>
 #include <ext_common.h>
 
+unsigned long part_offset;
+
 static block_dev_desc_t *ext4fs_block_dev_desc;
-static disk_partition_t part_info;
+static disk_partition_t *part_info;
 
-int ext4fs_set_blk_dev(block_dev_desc_t *rbdd, int part)
+void ext4fs_set_blk_dev(block_dev_desc_t *rbdd, disk_partition_t *info)
 {
 	ext4fs_block_dev_desc = rbdd;
-
-	if (part == 0) {
-		/* disk doesn't use partition table */
-		part_info.start = 0;
-		part_info.size = rbdd->lba;
-		part_info.blksz = rbdd->blksz;
-	} else {
-		if (get_partition_info(ext4fs_block_dev_desc,
-					part, &part_info))
-			return 0;
-	}
-	return part_info.size;
+	part_info = info;
+	part_offset = info->start;
+	get_fs()->total_sect = (info->size * info->blksz) / SECTOR_SIZE;
 }
 
 int ext4fs_devread(int sector, int byte_offset, int byte_len, char *buf)
@@ -68,7 +62,7 @@ int ext4fs_devread(int sector, int byte_offset, int byte_len, char *buf)
 	/* Check partition boundaries */
 	if ((sector < 0)
 	    || ((sector + ((byte_offset + byte_len - 1) >> SECTOR_BITS)) >=
-		part_info.size)) {
+		part_info->size)) {
 		printf("%s read outside partition %d\n", __func__, sector);
 		return 0;
 	}
@@ -88,7 +82,7 @@ int ext4fs_devread(int sector, int byte_offset, int byte_len, char *buf)
 		/* read first part which isn't aligned with start of sector */
 		if (ext4fs_block_dev_desc->
 		    block_read(ext4fs_block_dev_desc->dev,
-				part_info.start + sector, 1,
+				part_info->start + sector, 1,
 				(unsigned long *) sec_buf) != 1) {
 			printf(" ** ext2fs_devread() read error **\n");
 			return 0;
@@ -111,14 +105,14 @@ int ext4fs_devread(int sector, int byte_offset, int byte_len, char *buf)
 
 		block_len = SECTOR_SIZE;
 		ext4fs_block_dev_desc->block_read(ext4fs_block_dev_desc->dev,
-						  part_info.start + sector,
+						  part_info->start + sector,
 						  1, (unsigned long *)p);
 		memcpy(buf, p, byte_len);
 		return 1;
 	}
 
 	if (ext4fs_block_dev_desc->block_read(ext4fs_block_dev_desc->dev,
-					       part_info.start + sector,
+					       part_info->start + sector,
 					       block_len / SECTOR_SIZE,
 					       (unsigned long *) buf) !=
 					       block_len / SECTOR_SIZE) {
@@ -134,7 +128,7 @@ int ext4fs_devread(int sector, int byte_offset, int byte_len, char *buf)
 		/* read rest of data which are not in whole sector */
 		if (ext4fs_block_dev_desc->
 		    block_read(ext4fs_block_dev_desc->dev,
-				part_info.start + sector, 1,
+				part_info->start + sector, 1,
 				(unsigned long *) sec_buf) != 1) {
 			printf("* %s read error - last part\n", __func__);
 			return 0;
diff --git a/fs/ext4/ext4_common.h b/fs/ext4/ext4_common.h
index 801b8b8..7489397 100644
--- a/fs/ext4/ext4_common.h
+++ b/fs/ext4/ext4_common.h
@@ -57,7 +57,6 @@
 
 #define zalloc(size) calloc(1, size)
 
-extern unsigned long part_offset;
 int ext4fs_read_inode(struct ext2_data *data, int ino,
 		      struct ext2_inode *inode);
 int ext4fs_read_file(struct ext2fs_node *node, int pos,
diff --git a/fs/ext4/ext4fs.c b/fs/ext4/ext4fs.c
index c366e6f..93dcb7e 100644
--- a/fs/ext4/ext4fs.c
+++ b/fs/ext4/ext4fs.c
@@ -43,7 +43,6 @@
 #include "ext4_common.h"
 
 int ext4fs_symlinknest;
-block_dev_desc_t *ext4_dev_desc;
 struct ext_filesystem ext_fs;
 
 struct ext_filesystem *get_fs(void)
diff --git a/include/ext4fs.h b/include/ext4fs.h
index 6ad008c..b6eedde 100644
--- a/include/ext4fs.h
+++ b/include/ext4fs.h
@@ -113,7 +113,6 @@ struct ext_filesystem {
 	block_dev_desc_t *dev_desc;
 };
 
-extern block_dev_desc_t *ext4_dev_desc;
 extern struct ext2_data *ext4fs_root;
 extern struct ext2fs_node *ext4fs_file;
 
@@ -137,6 +136,6 @@ void ext4fs_close(void);
 int ext4fs_ls(const char *dirname);
 void ext4fs_free_node(struct ext2fs_node *node, struct ext2fs_node *currroot);
 int ext4fs_devread(int sector, int byte_offset, int byte_len, char *buf);
-int ext4fs_set_blk_dev(block_dev_desc_t *rbdd, int part);
+void ext4fs_set_blk_dev(block_dev_desc_t *rbdd, disk_partition_t *info);
 long int read_allocated_block(struct ext2_inode *inode, int fileblock);
 #endif
diff --git a/include/ext_common.h b/include/ext_common.h
index 9b97522..ce73857 100644
--- a/include/ext_common.h
+++ b/include/ext_common.h
@@ -186,6 +186,8 @@ struct ext2_data {
 	struct ext2fs_node diropen;
 };
 
+extern unsigned long part_offset;
+
 int do_ext2ls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
 int do_ext2load(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
 int do_ext4_load(cmd_tbl_t *cmdtp, int flag, int argc,
-- 
1.7.9.5

^ permalink raw reply related	[flat|nested] 20+ messages in thread

* [U-Boot] [PATCH 6/9] cmd_fat: use common get_device_and_partition function
  2012-08-23 21:31 [U-Boot] [PATCH 0/9] Auto partition selection and fs partition consolidation Rob Herring
                   ` (4 preceding siblings ...)
  2012-08-23 21:31 ` [U-Boot] [PATCH 5/9] cmd_extX: use common get_device_and_partition function Rob Herring
@ 2012-08-23 21:31 ` Rob Herring
  2012-08-23 21:31 ` [U-Boot] [PATCH 7/9] cmd_disk: " Rob Herring
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 20+ messages in thread
From: Rob Herring @ 2012-08-23 21:31 UTC (permalink / raw)
  To: u-boot

From: Rob Herring <rob.herring@calxeda.com>

Convert fatload, fatls, and fatinfo to use common device and partition
parsing function. With the common function "dev:part" can come from the
environment and a '-' can be used in that case.

Signed-off-by: Rob Herring <rob.herring@calxeda.com>
---
 common/cmd_fat.c |  100 +++++++++++++++++++-----------------------------------
 1 file changed, 34 insertions(+), 66 deletions(-)

diff --git a/common/cmd_fat.c b/common/cmd_fat.c
index 559a16d..90412d6 100644
--- a/common/cmd_fat.c
+++ b/common/cmd_fat.c
@@ -40,29 +40,20 @@ int do_fat_fsload (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 	unsigned long count;
 	char buf [12];
 	block_dev_desc_t *dev_desc=NULL;
-	int dev=0;
-	int part=1;
-	char *ep;
+	disk_partition_t info;
+	int part, dev;
 
 	if (argc < 5) {
-		printf( "usage: fatload <interface> <dev[:part]> "
+		printf("usage: fatload <interface> [<dev[:part]>] "
 			"<addr> <filename> [bytes]\n");
 		return 1;
 	}
 
-	dev = (int)simple_strtoul(argv[2], &ep, 16);
-	dev_desc = get_dev(argv[1],dev);
-	if (dev_desc == NULL) {
-		puts("\n** Invalid boot device **\n");
+	part = get_device_and_partition(argv[1], argv[2], &dev_desc, &info);
+	if (part < 0)
 		return 1;
-	}
-	if (*ep) {
-		if (*ep != ':') {
-			puts("\n** Invalid boot device, use `dev[:part]' **\n");
-			return 1;
-		}
-		part = (int)simple_strtoul(++ep, NULL, 16);
-	}
+
+	dev = dev_desc->dev;
 	if (fat_register_device(dev_desc,part)!=0) {
 		printf("\n** Unable to use %s %d:%d for fatload **\n",
 			argv[1], dev, part);
@@ -93,7 +84,7 @@ int do_fat_fsload (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 U_BOOT_CMD(
 	fatload,	6,	0,	do_fat_fsload,
 	"load binary file from a dos filesystem",
-	"<interface> <dev[:part]>  <addr> <filename> [bytes]\n"
+	"<interface> [<dev[:part]>]  <addr> <filename> [bytes]\n"
 	"    - load binary file 'filename' from 'dev' on 'interface'\n"
 	"      to address 'addr' from dos filesystem"
 );
@@ -101,29 +92,20 @@ U_BOOT_CMD(
 int do_fat_ls (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 {
 	char *filename = "/";
-	int ret;
-	int dev=0;
-	int part=1;
-	char *ep;
+	int ret, dev, part;
 	block_dev_desc_t *dev_desc=NULL;
+	disk_partition_t info;
 
-	if (argc < 3) {
-		printf("usage: fatls <interface> <dev[:part]> [directory]\n");
+	if (argc < 2) {
+		printf("usage: fatls <interface> [<dev[:part]>] [directory]\n");
 		return 0;
 	}
-	dev = (int)simple_strtoul(argv[2], &ep, 16);
-	dev_desc = get_dev(argv[1],dev);
-	if (dev_desc == NULL) {
-		puts("\n** Invalid boot device **\n");
+
+	part = get_device_and_partition(argv[1], argv[2], &dev_desc, &info);
+	if (part < 0)
 		return 1;
-	}
-	if (*ep) {
-		if (*ep != ':') {
-			puts("\n** Invalid boot device, use `dev[:part]' **\n");
-			return 1;
-		}
-		part = (int)simple_strtoul(++ep, NULL, 16);
-	}
+
+	dev = dev_desc->dev;
 	if (fat_register_device(dev_desc,part)!=0) {
 		printf("\n** Unable to use %s %d:%d for fatls **\n",
 			argv[1], dev, part);
@@ -142,34 +124,26 @@ int do_fat_ls (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 U_BOOT_CMD(
 	fatls,	4,	1,	do_fat_ls,
 	"list files in a directory (default /)",
-	"<interface> <dev[:part]> [directory]\n"
+	"<interface> [<dev[:part]>] [directory]\n"
 	"    - list files from 'dev' on 'interface' in a 'directory'"
 );
 
 int do_fat_fsinfo (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 {
-	int dev=0;
-	int part=1;
-	char *ep;
-	block_dev_desc_t *dev_desc=NULL;
+	int dev, part;
+	block_dev_desc_t *dev_desc;
+	disk_partition_t info;
 
 	if (argc < 2) {
-		printf("usage: fatinfo <interface> <dev[:part]>\n");
+		printf("usage: fatinfo <interface> [<dev[:part]>]\n");
 		return 0;
 	}
-	dev = (int)simple_strtoul(argv[2], &ep, 16);
-	dev_desc = get_dev(argv[1],dev);
-	if (dev_desc == NULL) {
-		puts("\n** Invalid boot device **\n");
+
+	part = get_device_and_partition(argv[1], argv[2], &dev_desc, &info);
+	if (part < 0)
 		return 1;
-	}
-	if (*ep) {
-		if (*ep != ':') {
-			puts("\n** Invalid boot device, use `dev[:part]' **\n");
-			return 1;
-		}
-		part = (int)simple_strtoul(++ep, NULL, 16);
-	}
+
+	dev = dev_desc->dev;
 	if (fat_register_device(dev_desc,part)!=0) {
 		printf("\n** Unable to use %s %d:%d for fatinfo **\n",
 			argv[1], dev, part);
@@ -181,7 +155,7 @@ int do_fat_fsinfo (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 U_BOOT_CMD(
 	fatinfo,	3,	1,	do_fat_fsinfo,
 	"print information about filesystem",
-	"<interface> <dev[:part]>\n"
+	"<interface> [<dev[:part]>]\n"
 	"    - print information about filesystem from 'dev' on 'interface'"
 );
 
@@ -193,6 +167,7 @@ static int do_fat_fswrite(cmd_tbl_t *cmdtp, int flag,
 	unsigned long addr;
 	unsigned long count;
 	block_dev_desc_t *dev_desc = NULL;
+	disk_partition_t info;
 	int dev = 0;
 	int part = 1;
 	char *ep;
@@ -200,19 +175,12 @@ static int do_fat_fswrite(cmd_tbl_t *cmdtp, int flag,
 	if (argc < 5)
 		return cmd_usage(cmdtp);
 
-	dev = (int)simple_strtoul(argv[2], &ep, 16);
-	dev_desc = get_dev(argv[1], dev);
-	if (dev_desc == NULL) {
-		puts("\n** Invalid boot device **\n");
+	part = get_device_and_partition(argv[1], argv[2], &dev_desc, &info);
+	if (part < 0)
 		return 1;
-	}
-	if (*ep) {
-		if (*ep != ':') {
-			puts("\n** Invalid boot device, use `dev[:part]' **\n");
-			return 1;
-		}
-		part = (int)simple_strtoul(++ep, NULL, 16);
-	}
+
+	dev = dev_desc->dev;
+
 	if (fat_register_device(dev_desc, part) != 0) {
 		printf("\n** Unable to use %s %d:%d for fatwrite **\n",
 			argv[1], dev, part);
-- 
1.7.9.5

^ permalink raw reply related	[flat|nested] 20+ messages in thread

* [U-Boot] [PATCH 7/9] cmd_disk: use common get_device_and_partition function
  2012-08-23 21:31 [U-Boot] [PATCH 0/9] Auto partition selection and fs partition consolidation Rob Herring
                   ` (5 preceding siblings ...)
  2012-08-23 21:31 ` [U-Boot] [PATCH 6/9] cmd_fat: " Rob Herring
@ 2012-08-23 21:31 ` Rob Herring
  2012-08-23 21:31 ` [U-Boot] [PATCH 8/9] cmd_zfs: " Rob Herring
  2012-08-23 21:31 ` [U-Boot] [PATCH 9/9] cmd_reiser: " Rob Herring
  8 siblings, 0 replies; 20+ messages in thread
From: Rob Herring @ 2012-08-23 21:31 UTC (permalink / raw)
  To: u-boot

From: Rob Herring <rob.herring@calxeda.com>

Signed-off-by: Rob Herring <rob.herring@calxeda.com>
---
 common/cmd_disk.c |   77 +++++++++++++----------------------------------------
 1 file changed, 19 insertions(+), 58 deletions(-)

diff --git a/common/cmd_disk.c b/common/cmd_disk.c
index 38420dc..e6676b0 100644
--- a/common/cmd_disk.c
+++ b/common/cmd_disk.c
@@ -1,13 +1,15 @@
 #include <common.h>
 #include <command.h>
+#include <part.h>
 
+#if defined(CONFIG_CMD_IDE) || defined(CONFIG_CMD_SCSI) || \
+	defined(CONFIG_USB_STORAGE)
 int common_diskboot(cmd_tbl_t *cmdtp, const char *intf, int argc, 
 		    char *const argv[])
 {
-	char *boot_device = NULL;
-	char *ep;
-	int dev, part = 0;
-	ulong addr, cnt;
+	int dev, part;
+	ulong addr = CONFIG_SYS_LOAD_ADDR;
+	ulong cnt;
 	disk_partition_t info;
 	image_header_t *hdr;
 	block_dev_desc_t *dev_desc;
@@ -17,72 +19,30 @@ int common_diskboot(cmd_tbl_t *cmdtp, const char *intf, int argc,
 #endif
 
 	bootstage_mark(BOOTSTAGE_ID_IDE_START);
-	switch (argc) {
-	case 1:
-		addr = CONFIG_SYS_LOAD_ADDR;
-		boot_device = getenv("bootdevice");
-		break;
-	case 2:
-		addr = simple_strtoul(argv[1], NULL, 16);
-		boot_device = getenv("bootdevice");
-		break;
-	case 3:
-		addr = simple_strtoul(argv[1], NULL, 16);
-		boot_device = argv[2];
-		break;
-	default:
+	if (argc > 3) {
 		bootstage_error(BOOTSTAGE_ID_IDE_ADDR);
 		return CMD_RET_USAGE;
 	}
 	bootstage_mark(BOOTSTAGE_ID_IDE_ADDR);
 
-	if (!boot_device) {
-		puts("\n** No boot device **\n");
-		bootstage_error(BOOTSTAGE_ID_IDE_BOOT_DEVICE);
-		return 1;
-	}
-	bootstage_mark(BOOTSTAGE_ID_IDE_BOOT_DEVICE);
+	if (argc > 1)
+		addr = simple_strtoul(argv[1], NULL, 16);
 
-	dev = simple_strtoul(boot_device, &ep, 16);
+	bootstage_mark(BOOTSTAGE_ID_IDE_BOOT_DEVICE);
 
-	dev_desc = get_dev(intf, dev);
-	if (dev_desc->type == DEV_TYPE_UNKNOWN) {
-		printf("\n** Device %d not available\n", dev);
+	part = get_device_and_partition(intf, (argc == 3) ? argv[2] : NULL,
+					&dev_desc, &info);
+	if (part < 0) {
 		bootstage_error(BOOTSTAGE_ID_IDE_TYPE);
 		return 1;
 	}
-	bootstage_mark(BOOTSTAGE_ID_IDE_TYPE);
-
-	if (*ep) {
-		if (*ep != ':') {
-			puts("\n** Invalid boot device, use `dev[:part]' **\n");
-			bootstage_error(BOOTSTAGE_ID_IDE_PART);
-			return 1;
-		}
-		part = simple_strtoul(++ep, NULL, 16);
-	}
-	bootstage_mark(BOOTSTAGE_ID_IDE_PART);
 
-	if (get_partition_info(dev_desc, part, &info)) {
-		bootstage_error(BOOTSTAGE_ID_IDE_PART_INFO);
-		return 1;
-	}
-	bootstage_mark(BOOTSTAGE_ID_IDE_PART_INFO);
-
-	if ((strncmp((char *)info.type, BOOT_PART_TYPE, sizeof(info.type)) != 0)
-	    &&
-	    (strncmp((char *)info.type, BOOT_PART_COMP, sizeof(info.type)) != 0)
-	   ) {
-		printf("\n** Invalid partition type \"%.32s\"" " (expect \""
-			BOOT_PART_TYPE "\")\n",
-			info.type);
-		bootstage_error(BOOTSTAGE_ID_IDE_PART_TYPE);
-		return 1;
-	}
-	bootstage_mark(BOOTSTAGE_ID_IDE_PART_TYPE);
+	dev = dev_desc->dev;
+	bootstage_mark(BOOTSTAGE_ID_IDE_TYPE);
 
-	printf("\nLoading from disk device %d, partition %d: "
-	       "Name: %.32s  Type: %.32s\n", dev, part, info.name, info.type);
+	printf("\nLoading from %s device %d, partition %d: "
+	       "Name: %.32s  Type: %.32s\n", intf, dev, part, info.name,
+	       info.type);
 
 	debug("First Block: %ld,  # of blocks: %ld, Block Size: %ld\n",
 	      info.start, info.size, info.blksz);
@@ -158,4 +118,5 @@ int common_diskboot(cmd_tbl_t *cmdtp, const char *intf, int argc,
 
 	return bootm_maybe_autostart(cmdtp, argv[0]);
 }
+#endif
 
-- 
1.7.9.5

^ permalink raw reply related	[flat|nested] 20+ messages in thread

* [U-Boot] [PATCH 8/9] cmd_zfs: use common get_device_and_partition function
  2012-08-23 21:31 [U-Boot] [PATCH 0/9] Auto partition selection and fs partition consolidation Rob Herring
                   ` (6 preceding siblings ...)
  2012-08-23 21:31 ` [U-Boot] [PATCH 7/9] cmd_disk: " Rob Herring
@ 2012-08-23 21:31 ` Rob Herring
  2012-08-23 21:31 ` [U-Boot] [PATCH 9/9] cmd_reiser: " Rob Herring
  8 siblings, 0 replies; 20+ messages in thread
From: Rob Herring @ 2012-08-23 21:31 UTC (permalink / raw)
  To: u-boot

From: Rob Herring <rob.herring@calxeda.com>

Convert zfsload and zfsls to use common device and partition parsing
function. With the common function "dev:part" can come from the
environment and a '-' can be used in that case.

Signed-off-by: Rob Herring <rob.herring@calxeda.com>
---
 common/cmd_zfs.c     |   88 +++++++++++---------------------------------------
 fs/zfs/dev.c         |   35 +++++++-------------
 include/zfs_common.h |    5 +--
 3 files changed, 31 insertions(+), 97 deletions(-)

diff --git a/common/cmd_zfs.c b/common/cmd_zfs.c
index a6ea2c0..27f8856 100644
--- a/common/cmd_zfs.c
+++ b/common/cmd_zfs.c
@@ -49,12 +49,11 @@
 static int do_zfs_load(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
 {
 	char *filename = NULL;
-	char *ep;
 	int dev;
-	unsigned long part = 1;
+	int part;
 	ulong addr = 0;
-	ulong part_length;
 	disk_partition_t info;
+	block_dev_desc_t *dev_desc;
 	char buf[12];
 	unsigned long count;
 	const char *addr_str;
@@ -95,48 +94,17 @@ static int do_zfs_load(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
 		return 1;
 	}
 
-	dev = (int)simple_strtoul(argv[2], &ep, 16);
-	zfs_dev_desc = get_dev(argv[1], dev);
-	if (zfs_dev_desc == NULL) {
-		printf("** Block device %s %d not supported\n", argv[1], dev);
+	part = get_device_and_partition(argv[1], argv[2], &dev_desc, &info);
+	if (part < 0)
 		return 1;
-	}
 
-	if (*ep) {
-		if (*ep != ':') {
-			puts("** Invalid boot device, use `dev[:part]' **\n");
-			return 1;
-		}
-		part = simple_strtoul(++ep, NULL, 16);
-	}
+	dev = dev_desc->dev;
+	printf("Loading file \"%s\" from %s device %d%c%c\n",
+		filename, argv[1], dev,
+		part ? ':' : ' ', part ? part + '0' : ' ');
 
-	if (part != 0) {
-		if (get_partition_info(zfs_dev_desc, part, &info)) {
-			printf("** Bad partition %lu **\n", part);
-			return 1;
-		}
-
-		if (strncmp((char *)info.type, BOOT_PART_TYPE,
-					strlen(BOOT_PART_TYPE)) != 0) {
-			printf("** Invalid partition type \"%s\" (expect \"" BOOT_PART_TYPE "\")\n",
-				   info.type);
-			return 1;
-		}
-		printf("Loading file \"%s\" "
-			   "from %s device %d:%lu %s\n",
-			   filename, argv[1], dev, part, info.name);
-	} else {
-		printf("Loading file \"%s\" from %s device %d\n",
-			   filename, argv[1], dev);
-	}
-
-	part_length = zfs_set_blk_dev(zfs_dev_desc, part);
-	if (part_length == 0) {
-		printf("**Bad partition - %s %d:%lu **\n", argv[1], dev, part);
-		return 1;
-	}
-
-	vdev.part_length = part_length;
+	zfs_set_blk_dev(dev_desc, &info);
+	vdev.part_length = info.size;
 
 	memset(&zfile, 0, sizeof(zfile));
 	zfile.device = &vdev;
@@ -149,7 +117,7 @@ static int do_zfs_load(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
 		zfile.size = (uint64_t)count;
 
 	if (zfs_read(&zfile, (char *)addr, zfile.size) != zfile.size) {
-		printf("** Unable to read \"%s\" from %s %d:%lu **\n",
+		printf("** Unable to read \"%s\" from %s %d:%d **\n",
 			   filename, argv[1], dev, part);
 		zfs_close(&zfile);
 		return 1;
@@ -181,41 +149,23 @@ int zfs_print(const char *entry, const struct zfs_dirhook_info *data)
 static int do_zfs_ls(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
 {
 	const char *filename = "/";
-	int dev;
-	unsigned long part = 1;
-	char *ep;
-	int part_length;
+	int part;
+	block_dev_desc_t *dev_desc;
+	disk_partition_t info;
 	struct device_s vdev;
 
-	if (argc < 3)
+	if (argc < 2)
 		return cmd_usage(cmdtp);
 
-	dev = (int)simple_strtoul(argv[2], &ep, 16);
-	zfs_dev_desc = get_dev(argv[1], dev);
-
-	if (zfs_dev_desc == NULL) {
-		printf("\n** Block device %s %d not supported\n", argv[1], dev);
-		return 1;
-	}
-
-	if (*ep) {
-		if (*ep != ':') {
-			puts("\n** Invalid boot device, use `dev[:part]' **\n");
-			return 1;
-		}
-		part = simple_strtoul(++ep, NULL, 16);
-	}
-
 	if (argc == 4)
 		filename = argv[3];
 
-	part_length = zfs_set_blk_dev(zfs_dev_desc, part);
-	if (part_length == 0) {
-		printf("** Bad partition - %s %d:%lu **\n", argv[1], dev, part);
+	part = get_device_and_partition(argv[1], argv[2], &dev_desc, &info);
+	if (part < 0)
 		return 1;
-	}
 
-	vdev.part_length = part_length;
+	zfs_set_blk_dev(dev_desc, &info);
+	vdev.part_length = info.size;
 
 	zfs_ls(&vdev, filename,
 		   zfs_print);
diff --git a/fs/zfs/dev.c b/fs/zfs/dev.c
index d68372c..36be8f5 100644
--- a/fs/zfs/dev.c
+++ b/fs/zfs/dev.c
@@ -26,23 +26,12 @@
 #include <zfs_common.h>
 
 static block_dev_desc_t *zfs_block_dev_desc;
-static disk_partition_t part_info;
+static disk_partition_t *part_info;
 
-int zfs_set_blk_dev(block_dev_desc_t *rbdd, int part)
+void zfs_set_blk_dev(block_dev_desc_t *rbdd, disk_partition_t *info)
 {
 	zfs_block_dev_desc = rbdd;
-
-	if (part == 0) {
-		/* disk doesn't use partition table */
-		part_info.start = 0;
-		part_info.size = rbdd->lba;
-		part_info.blksz = rbdd->blksz;
-	} else {
-		if (get_partition_info(zfs_block_dev_desc, part, &part_info))
-			return 0;
-	}
-
-	return part_info.size;
+	part_info = info;
 }
 
 /* err */
@@ -57,7 +46,7 @@ int zfs_devread(int sector, int byte_offset, int byte_len, char *buf)
 	 */
 	if ((sector < 0) ||
 		((sector + ((byte_offset + byte_len - 1) >> SECTOR_BITS)) >=
-		 part_info.size)) {
+		 part_info->size)) {
 		/*		errnum = ERR_OUTSIDE_PART; */
 		printf(" ** zfs_devread() read outside partition sector %d\n", sector);
 		return 1;
@@ -79,8 +68,8 @@ int zfs_devread(int sector, int byte_offset, int byte_len, char *buf)
 	if (byte_offset != 0) {
 		/* read first part which isn't aligned with start of sector */
 		if (zfs_block_dev_desc->block_read(zfs_block_dev_desc->dev,
-										   part_info.start + sector, 1,
-										   (unsigned long *) sec_buf) != 1) {
+			part_info->start + sector, 1,
+			(unsigned long *)sec_buf) != 1) {
 			printf(" ** zfs_devread() read error **\n");
 			return 1;
 		}
@@ -102,17 +91,15 @@ int zfs_devread(int sector, int byte_offset, int byte_len, char *buf)
 
 		block_len = SECTOR_SIZE;
 		zfs_block_dev_desc->block_read(zfs_block_dev_desc->dev,
-									   part_info.start + sector,
-									   1, (unsigned long *)p);
+			part_info->start + sector,
+			1, (unsigned long *)p);
 		memcpy(buf, p, byte_len);
 		return 0;
 	}
 
 	if (zfs_block_dev_desc->block_read(zfs_block_dev_desc->dev,
-									   part_info.start + sector,
-									   block_len / SECTOR_SIZE,
-									   (unsigned long *) buf) !=
-		block_len / SECTOR_SIZE) {
+		part_info->start + sector, block_len / SECTOR_SIZE,
+		(unsigned long *) buf) != block_len / SECTOR_SIZE) {
 		printf(" ** zfs_devread() read error - block\n");
 		return 1;
 	}
@@ -126,7 +113,7 @@ int zfs_devread(int sector, int byte_offset, int byte_len, char *buf)
 		/* read rest of data which are not in whole sector */
 		if (zfs_block_dev_desc->
 			block_read(zfs_block_dev_desc->dev,
-					   part_info.start + sector, 1,
+					   part_info->start + sector, 1,
 					   (unsigned long *) sec_buf) != 1) {
 			printf(" ** zfs_devread() read error - last part\n");
 			return 1;
diff --git a/include/zfs_common.h b/include/zfs_common.h
index 628231e..3bd575e 100644
--- a/include/zfs_common.h
+++ b/include/zfs_common.h
@@ -66,9 +66,6 @@ struct zfs_filesystem {
 	block_dev_desc_t *dev_desc;
 };
 
-
-extern block_dev_desc_t *zfs_dev_desc;
-
 struct device_s {
 	uint64_t part_length;
 };
@@ -101,7 +98,7 @@ int zfs_close(zfs_file_t);
 int zfs_ls(device_t dev, const char *path,
 		   int (*hook) (const char *, const struct zfs_dirhook_info *));
 int zfs_devread(int sector, int byte_offset, int byte_len, char *buf);
-int zfs_set_blk_dev(block_dev_desc_t *rbdd, int part);
+void zfs_set_blk_dev(block_dev_desc_t *rbdd, disk_partition_t *info);
 void zfs_unmount(struct zfs_data *data);
 int lzjb_decompress(void *, void *, uint32_t, uint32_t);
 #endif
-- 
1.7.9.5

^ permalink raw reply related	[flat|nested] 20+ messages in thread

* [U-Boot] [PATCH 9/9] cmd_reiser: use common get_device_and_partition function
  2012-08-23 21:31 [U-Boot] [PATCH 0/9] Auto partition selection and fs partition consolidation Rob Herring
                   ` (7 preceding siblings ...)
  2012-08-23 21:31 ` [U-Boot] [PATCH 8/9] cmd_zfs: " Rob Herring
@ 2012-08-23 21:31 ` Rob Herring
  8 siblings, 0 replies; 20+ messages in thread
From: Rob Herring @ 2012-08-23 21:31 UTC (permalink / raw)
  To: u-boot

From: Rob Herring <rob.herring@calxeda.com>

Convert reiserload and reiserls to use common device and partition parsing
function. With the common function "dev:part" can come from the
environment and a '-' can be used in that case.

Signed-off-by: Rob Herring <rob.herring@calxeda.com>
---
 common/cmd_reiser.c |   81 +++++++++++----------------------------------------
 fs/reiserfs/dev.c   |   29 +++++++-----------
 include/reiserfs.h  |    2 +-
 3 files changed, 28 insertions(+), 84 deletions(-)

diff --git a/common/cmd_reiser.c b/common/cmd_reiser.c
index fbb9484..2865014 100644
--- a/common/cmd_reiser.c
+++ b/common/cmd_reiser.c
@@ -50,43 +50,27 @@
 int do_reiserls (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 {
 	char *filename = "/";
-	int dev=0;
-	int part=1;
-	char *ep;
+	int dev, part;
 	block_dev_desc_t *dev_desc=NULL;
-	int part_length;
+	disk_partition_t info;
 
 	if (argc < 3)
 		return CMD_RET_USAGE;
 
-	dev = (int)simple_strtoul (argv[2], &ep, 16);
-	dev_desc = get_dev(argv[1],dev);
-
-	if (dev_desc == NULL) {
-		printf ("\n** Block device %s %d not supported\n", argv[1], dev);
+	part = get_device_and_partition(argv[1], argv[2], &dev_desc, &info);
+	if (part < 0)
 		return 1;
-	}
-
-	if (*ep) {
-		if (*ep != ':') {
-			puts ("\n** Invalid boot device, use `dev[:part]' **\n");
-			return 1;
-		}
-		part = (int)simple_strtoul(++ep, NULL, 16);
-	}
 
 	if (argc == 4) {
 	    filename = argv[3];
 	}
 
+	dev = dev_desc->dev;
 	PRINTF("Using device %s %d:%d, directory: %s\n", argv[1], dev, part, filename);
 
-	if ((part_length = reiserfs_set_blk_dev(dev_desc, part)) == 0) {
-		printf ("** Bad partition - %s %d:%d **\n",  argv[1], dev, part);
-		return 1;
-	}
+	reiserfs_set_blk_dev(dev_desc, &info);
 
-	if (!reiserfs_mount(part_length)) {
+	if (!reiserfs_mount(info.size)) {
 		printf ("** Bad Reiserfs partition or disk - %s %d:%d **\n",  argv[1], dev, part);
 		return 1;
 	}
@@ -112,9 +96,8 @@ U_BOOT_CMD(
 int do_reiserload (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 {
 	char *filename = NULL;
-	char *ep;
-	int dev, part = 0;
-	ulong addr = 0, part_length, filelen;
+	int dev, part;
+	ulong addr = 0, filelen;
 	disk_partition_t info;
 	block_dev_desc_t *dev_desc = NULL;
 	char buf [12];
@@ -157,49 +140,19 @@ int do_reiserload (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 		return 1;
 	}
 
-	dev = (int)simple_strtoul (argv[2], &ep, 16);
-	dev_desc = get_dev(argv[1],dev);
-	if (dev_desc==NULL) {
-		printf ("\n** Block device %s %d not supported\n", argv[1], dev);
+	part = get_device_and_partition(argv[1], argv[2], &dev_desc, &info);
+	if (part < 0)
 		return 1;
-	}
-	if (*ep) {
-		if (*ep != ':') {
-			puts ("\n** Invalid boot device, use `dev[:part]' **\n");
-			return 1;
-		}
-		part = (int)simple_strtoul(++ep, NULL, 16);
-	}
 
-	PRINTF("Using device %s%d, partition %d\n", argv[1], dev, part);
+	dev = dev_desc->dev;
 
-	if (part != 0) {
-		if (get_partition_info (dev_desc, part, &info)) {
-			printf ("** Bad partition %d **\n", part);
-			return 1;
-		}
+	printf("Loading file \"%s\" from %s device %d%c%c\n",
+		filename, argv[1], dev,
+		part ? ':' : ' ', part ? part + '0' : ' ');
 
-		if (strncmp((char *)info.type, BOOT_PART_TYPE, sizeof(info.type)) != 0) {
-			printf ("\n** Invalid partition type \"%.32s\""
-				" (expect \"" BOOT_PART_TYPE "\")\n",
-				info.type);
-			return 1;
-		}
-		PRINTF ("\nLoading from block device %s device %d, partition %d: "
-			"Name: %.32s  Type: %.32s  File:%s\n",
-			argv[1], dev, part, info.name, info.type, filename);
-	} else {
-		PRINTF ("\nLoading from block device %s device %d, File:%s\n",
-			argv[1], dev, filename);
-	}
-
-
-	if ((part_length = reiserfs_set_blk_dev(dev_desc, part)) == 0) {
-		printf ("** Bad partition - %s %d:%d **\n",  argv[1], dev, part);
-		return 1;
-	}
+	reiserfs_set_blk_dev(dev_desc, &info);
 
-	if (!reiserfs_mount(part_length)) {
+	if (!reiserfs_mount(info.size)) {
 		printf ("** Bad Reiserfs partition or disk - %s %d:%d **\n",  argv[1], dev, part);
 		return 1;
 	}
diff --git a/fs/reiserfs/dev.c b/fs/reiserfs/dev.c
index 1facfaf..cb288d6 100644
--- a/fs/reiserfs/dev.c
+++ b/fs/reiserfs/dev.c
@@ -25,24 +25,13 @@
 #include "reiserfs_private.h"
 
 static block_dev_desc_t *reiserfs_block_dev_desc;
-static disk_partition_t part_info;
+static disk_partition_t *part_info;
 
 
-int reiserfs_set_blk_dev(block_dev_desc_t *rbdd, int part)
+void reiserfs_set_blk_dev(block_dev_desc_t *rbdd, disk_partition_t *info)
 {
 	reiserfs_block_dev_desc = rbdd;
-
-	if (part == 0) {
-		/* disk doesn't use partition table */
-		part_info.start = 0;
-		part_info.size = rbdd->lba;
-		part_info.blksz = rbdd->blksz;
-	} else {
-		if (get_partition_info (reiserfs_block_dev_desc, part, &part_info)) {
-			return 0;
-		}
-	}
-	return (part_info.size);
+	part_info = info;
 }
 
 
@@ -59,7 +48,7 @@ int reiserfs_devread (int sector, int byte_offset, int byte_len, char *buf)
 	*/
 	if (sector < 0
 	    || ((sector + ((byte_offset + byte_len - 1) >> SECTOR_BITS))
-	    >= part_info.size)) {
+	    >= part_info->size)) {
 /*		errnum = ERR_OUTSIDE_PART; */
 		printf (" ** reiserfs_devread() read outside partition\n");
 		return 0;
@@ -83,7 +72,8 @@ int reiserfs_devread (int sector, int byte_offset, int byte_len, char *buf)
 	if (byte_offset != 0) {
 		/* read first part which isn't aligned with start of sector */
 		if (reiserfs_block_dev_desc->block_read(reiserfs_block_dev_desc->dev,
-		    part_info.start+sector, 1, (unsigned long *)sec_buf) != 1) {
+		    part_info->start + sector, 1,
+		    (unsigned long *)sec_buf) != 1) {
 			printf (" ** reiserfs_devread() read error\n");
 			return 0;
 		}
@@ -96,8 +86,8 @@ int reiserfs_devread (int sector, int byte_offset, int byte_len, char *buf)
 	/* read sector aligned part */
 	block_len = byte_len & ~(SECTOR_SIZE-1);
 	if (reiserfs_block_dev_desc->block_read(reiserfs_block_dev_desc->dev,
-	    part_info.start+sector, block_len/SECTOR_SIZE, (unsigned long *)buf) !=
-	    block_len/SECTOR_SIZE) {
+	    part_info->start + sector, block_len/SECTOR_SIZE,
+	    (unsigned long *)buf) != block_len/SECTOR_SIZE) {
 		printf (" ** reiserfs_devread() read error - block\n");
 		return 0;
 	}
@@ -108,7 +98,8 @@ int reiserfs_devread (int sector, int byte_offset, int byte_len, char *buf)
 	if ( byte_len != 0 ) {
 		/* read rest of data which are not in whole sector */
 		if (reiserfs_block_dev_desc->block_read(reiserfs_block_dev_desc->dev,
-		    part_info.start+sector, 1, (unsigned long *)sec_buf) != 1) {
+		    part_info->start + sector, 1,
+		    (unsigned long *)sec_buf) != 1) {
 			printf (" ** reiserfs_devread() read error - last part\n");
 			return 0;
 		}
diff --git a/include/reiserfs.h b/include/reiserfs.h
index c465b3c..dc89342 100644
--- a/include/reiserfs.h
+++ b/include/reiserfs.h
@@ -75,7 +75,7 @@ typedef enum
 } reiserfs_error_t;
 
 
-extern int reiserfs_set_blk_dev(block_dev_desc_t *rbdd, int part);
+extern void reiserfs_set_blk_dev(block_dev_desc_t *rbdd, disk_partition_t *info);
 extern int reiserfs_ls (char *dirname);
 extern int reiserfs_open (char *filename);
 extern int reiserfs_read (char *buf, unsigned len);
-- 
1.7.9.5

^ permalink raw reply related	[flat|nested] 20+ messages in thread

* [U-Boot] [PATCH 3/9] disk/part: introduce get_device_and_partition
  2012-08-23 21:31 ` [U-Boot] [PATCH 3/9] disk/part: introduce get_device_and_partition Rob Herring
@ 2012-08-23 22:36   ` Stephen Warren
  2012-08-24  1:57     ` Rob Herring
  2012-09-21 14:08   ` [U-Boot] [PATCH v2 " Rob Herring
  1 sibling, 1 reply; 20+ messages in thread
From: Stephen Warren @ 2012-08-23 22:36 UTC (permalink / raw)
  To: u-boot

On 08/23/2012 03:31 PM, Rob Herring wrote:
> From: Rob Herring <rob.herring@calxeda.com>
> 
> All block device related commands (scsiboot, fatload, ext2ls, etc.) have
> simliar duplicated device and partition parsing and selection code. This
> adds a common function to replace various implementations.
> 
> The new function has some enhancements over current versions. If no device
> or partition is specified on the command line, the bootdevice env variable
> will be used (scsiboot does this). If the partition is not specified and
> the device has partitions, then the first bootable partition will be used.
> If a bootable partition is not found, the first valid partition is used.
> The ret value is not needed since part will be zero when no partition is
> found.

Two thoughts on this patch:

First, if I write "mmc 0" right now, command will always attempt to
access precisely partion 1, whereas after this patch, they will search
for the first bootable, or valid, partition. This is a change in
behavior. It's a pretty reasonable change, but I wonder if it might
cause problems somewhere.

Instead, perhaps this new feature should be explicitly requested,
supporting the following device/partition specifications:

# existing:
dev 0:0        # whole device
dev 0:n        # n >= 1: explicit partition
dev 0          # partition 1
# new:
dev 0:valid    # first valid partition
dev 0:bootable # first bootable partition
dev 0:default  # first bootable partition if there is one,
               # else first valid

That would allow scripts to be very explicit about whether they wanted
this new functionality.

Second, if I run a slew of ext2load commands:

ext2load mmc 0:bootable ${scriptaddr} boot.scr
source ${scriptaddr}
# script does:
ext2load mmc 0:bootable ${kernel_addr} zImage
ext2load mmc 0:bootable ${initrd_addr} initrd.bin
ext2load mmc 0:bootable ${fdt_addr} foo.dtb

Then there are two disadvantages:

1) I believe the partition table is read and decoded and search for
every one of those ext2load commands. Slightly inefficient.

2) There's no permanent record of the partition number, so this couldn't
be e.g. used to construct a kernel command-line etc.

Instead, I wonder if get_device_and_partition() should just support the
existing 3 device specification options, and we introduce a new command
to determine which partition to boot from, e.g.:

# writes result to "bootpart" variable
# or get-default or get-first-valid
part get-first-bootable mmc 0 bootpart

ext2load mmc 0:${bootpart} ${scriptaddr} boot.scr
source ${scriptaddr}
# script does:
ext2load mmc 0:${bootpart} ${kernel_addr} zImage
ext2load mmc 0:${bootpart} ${initrd_addr} initrd.bin
ext2load mmc 0:${bootpart} ${fdt_addr} foo.dtb

That solves those issues. Does anyone have any comment on the two
approaches?

(although perhaps e.g. ext2load always re-reads the partition table
anyway, so perhaps that advantage is moot?)

Aside from that, this series looks conceptually reasonable at a quick
glance. I'd be happy to provide an equivalent to patch 2 for GPT/EFI
partition tables.

^ permalink raw reply	[flat|nested] 20+ messages in thread

* [U-Boot] [PATCH 3/9] disk/part: introduce get_device_and_partition
  2012-08-23 22:36   ` Stephen Warren
@ 2012-08-24  1:57     ` Rob Herring
  2012-08-24  2:51       ` Stephen Warren
  0 siblings, 1 reply; 20+ messages in thread
From: Rob Herring @ 2012-08-24  1:57 UTC (permalink / raw)
  To: u-boot

On 08/23/2012 05:36 PM, Stephen Warren wrote:
> On 08/23/2012 03:31 PM, Rob Herring wrote:
>> From: Rob Herring <rob.herring@calxeda.com>
>>
>> All block device related commands (scsiboot, fatload, ext2ls, etc.) have
>> simliar duplicated device and partition parsing and selection code. This
>> adds a common function to replace various implementations.
>>
>> The new function has some enhancements over current versions. If no device
>> or partition is specified on the command line, the bootdevice env variable
>> will be used (scsiboot does this). If the partition is not specified and
>> the device has partitions, then the first bootable partition will be used.
>> If a bootable partition is not found, the first valid partition is used.
>> The ret value is not needed since part will be zero when no partition is
>> found.
> 
> Two thoughts on this patch:
> 
> First, if I write "mmc 0" right now, command will always attempt to
> access precisely partion 1, whereas after this patch, they will search
> for the first bootable, or valid, partition. This is a change in
> behavior. It's a pretty reasonable change, but I wonder if it might
> cause problems somewhere.
> 
> Instead, perhaps this new feature should be explicitly requested,
> supporting the following device/partition specifications:
> 
> # existing:
> dev 0:0        # whole device
> dev 0:n        # n >= 1: explicit partition
> dev 0          # partition 1
> # new:
> dev 0:valid    # first valid partition
> dev 0:bootable # first bootable partition
> dev 0:default  # first bootable partition if there is one,
>                # else first valid

I'm not sure we need to distinguish valid vs. bootable. Returning the
first valid partition was really just to maintain somewhat backwards
compatible behavior.

Perhaps just "0:-" would be sufficient.

> 
> That would allow scripts to be very explicit about whether they wanted
> this new functionality.
> 
> Second, if I run a slew of ext2load commands:
> 
> ext2load mmc 0:bootable ${scriptaddr} boot.scr
> source ${scriptaddr}
> # script does:
> ext2load mmc 0:bootable ${kernel_addr} zImage
> ext2load mmc 0:bootable ${initrd_addr} initrd.bin
> ext2load mmc 0:bootable ${fdt_addr} foo.dtb
> 
> Then there are two disadvantages:
> 
> 1) I believe the partition table is read and decoded and search for
> every one of those ext2load commands. Slightly inefficient.

It was already multiple times per command with the command function
calling get_partition_info and then the filesystem code calling it again
internally as well. Now it is only 1 time at least. I would think the
1st partition being bootable is the common case.

> 2) There's no permanent record of the partition number, so this couldn't
> be e.g. used to construct a kernel command-line etc.

You mean to setup rootfs? I don't think we want u-boot to do that. Or
what would be the use?

> Instead, I wonder if get_device_and_partition() should just support the
> existing 3 device specification options, and we introduce a new command
> to determine which partition to boot from, e.g.:
> 
> # writes result to "bootpart" variable
> # or get-default or get-first-valid
> part get-first-bootable mmc 0 bootpart
> 
> ext2load mmc 0:${bootpart} ${scriptaddr} boot.scr
> source ${scriptaddr}
> # script does:
> ext2load mmc 0:${bootpart} ${kernel_addr} zImage
> ext2load mmc 0:${bootpart} ${initrd_addr} initrd.bin
> ext2load mmc 0:${bootpart} ${fdt_addr} foo.dtb
> 
> That solves those issues. Does anyone have any comment on the two
> approaches?

I'm really open to either way.

Another option would be for the first command run to set bootpart and
then re-use that value on subsequent commands.

Rob

> 
> (although perhaps e.g. ext2load always re-reads the partition table
> anyway, so perhaps that advantage is moot?)
> 
> Aside from that, this series looks conceptually reasonable at a quick
> glance. I'd be happy to provide an equivalent to patch 2 for GPT/EFI
> partition tables.
> 

^ permalink raw reply	[flat|nested] 20+ messages in thread

* [U-Boot] [PATCH 3/9] disk/part: introduce get_device_and_partition
  2012-08-24  1:57     ` Rob Herring
@ 2012-08-24  2:51       ` Stephen Warren
  2012-09-05 23:53         ` Tom Rini
  0 siblings, 1 reply; 20+ messages in thread
From: Stephen Warren @ 2012-08-24  2:51 UTC (permalink / raw)
  To: u-boot

On 08/23/2012 07:57 PM, Rob Herring wrote:
> On 08/23/2012 05:36 PM, Stephen Warren wrote:
>> On 08/23/2012 03:31 PM, Rob Herring wrote:
>>> From: Rob Herring <rob.herring@calxeda.com>
>>>
>>> All block device related commands (scsiboot, fatload, ext2ls, etc.) have
>>> simliar duplicated device and partition parsing and selection code. This
>>> adds a common function to replace various implementations.
>>>
>>> The new function has some enhancements over current versions. If no device
>>> or partition is specified on the command line, the bootdevice env variable
>>> will be used (scsiboot does this). If the partition is not specified and
>>> the device has partitions, then the first bootable partition will be used.
>>> If a bootable partition is not found, the first valid partition is used.
>>> The ret value is not needed since part will be zero when no partition is
>>> found.
>>
>> Two thoughts on this patch:
>>
>> First, if I write "mmc 0" right now, command will always attempt to
>> access precisely partion 1, whereas after this patch, they will search
>> for the first bootable, or valid, partition. This is a change in
>> behavior. It's a pretty reasonable change, but I wonder if it might
>> cause problems somewhere.
>>
>> Instead, perhaps this new feature should be explicitly requested,
>> supporting the following device/partition specifications:
>>
>> # existing:
>> dev 0:0        # whole device
>> dev 0:n        # n >= 1: explicit partition
>> dev 0          # partition 1
>> # new:
>> dev 0:valid    # first valid partition
>> dev 0:bootable # first bootable partition
>> dev 0:default  # first bootable partition if there is one,
>>                # else first valid
> 
> I'm not sure we need to distinguish valid vs. bootable. Returning the
> first valid partition was really just to maintain somewhat backwards
> compatible behavior.
> 
> Perhaps just "0:-" would be sufficient.

I guess that syntax would be fine if we don't need to distinguish all
the cases. "-" isn't that descriptive though, and I've only seen it mean
"nothing" in U-Boot commands. So, bike-shedding a bit, it doesn't seem
exactly correct. Perhaps just "auto"?

>> That would allow scripts to be very explicit about whether they wanted
>> this new functionality.
>>
>> Second, if I run a slew of ext2load commands:
>>
>> ext2load mmc 0:bootable ${scriptaddr} boot.scr
>> source ${scriptaddr}
>> # script does:
>> ext2load mmc 0:bootable ${kernel_addr} zImage
>> ext2load mmc 0:bootable ${initrd_addr} initrd.bin
>> ext2load mmc 0:bootable ${fdt_addr} foo.dtb
>>
>> Then there are two disadvantages:
>>
>> 1) I believe the partition table is read and decoded and search for
>> every one of those ext2load commands. Slightly inefficient.
> 
> It was already multiple times per command with the command function
> calling get_partition_info and then the filesystem code calling it again
> internally as well. Now it is only 1 time at least. I would think the
> 1st partition being bootable is the common case.
> 
>> 2) There's no permanent record of the partition number, so this couldn't
>> be e.g. used to construct a kernel command-line etc.
> 
> You mean to setup rootfs? I don't think we want u-boot to do that. Or
> what would be the use?

I can imagine a boot.scr that does:

setenv bootargs root=/dev/mmcblk0p${bootpart}

But then, you may as well use the partition UUID feature instead of
that, so that boot.scr doesn't need to know the kernel's device name.

>> Instead, I wonder if get_device_and_partition() should just support the
>> existing 3 device specification options, and we introduce a new command
>> to determine which partition to boot from, e.g.:
>>
>> # writes result to "bootpart" variable
>> # or get-default or get-first-valid
>> part get-first-bootable mmc 0 bootpart
>>
>> ext2load mmc 0:${bootpart} ${scriptaddr} boot.scr
>> source ${scriptaddr}
>> # script does:
>> ext2load mmc 0:${bootpart} ${kernel_addr} zImage
>> ext2load mmc 0:${bootpart} ${initrd_addr} initrd.bin
>> ext2load mmc 0:${bootpart} ${fdt_addr} foo.dtb
>>
>> That solves those issues. Does anyone have any comment on the two
>> approaches?
> 
> I'm really open to either way.
> 
> Another option would be for the first command run to set bootpart and
> then re-use that value on subsequent commands.

That could work too, although commands using environment variables seems
a little implicit/hidden.

^ permalink raw reply	[flat|nested] 20+ messages in thread

* [U-Boot] [PATCH 1/9] combine block device load commands into common function
  2012-08-23 21:31 ` [U-Boot] [PATCH 1/9] combine block device load commands into common function Rob Herring
@ 2012-09-05 23:36   ` Tom Rini
  2012-09-05 23:47     ` Rob Herring
  2012-09-21 14:02   ` [U-Boot] [PATCH v2 " Rob Herring
  1 sibling, 1 reply; 20+ messages in thread
From: Tom Rini @ 2012-09-05 23:36 UTC (permalink / raw)
  To: u-boot

On Thu, Aug 23, 2012 at 04:31:42PM -0500, Rob Herring wrote:

> From: Rob Herring <rob.herring@calxeda.com>
> 
> All the raw block load commands duplicate the same code. Starting with
> the ide version as it has progress updates convert ide, usb, and scsi boot
> commands to all use a common version.
> 
> Signed-off-by: Rob Herring <rob.herring@calxeda.com>
[snip]
> diff --git a/common/cmd_disk.c b/common/cmd_disk.c
> new file mode 100644
> index 0000000..38420dc
> --- /dev/null
> +++ b/common/cmd_disk.c
> @@ -0,0 +1,161 @@
> +#include <common.h>
> +#include <command.h>

Many companies have legal departments that get upset by files without
license information.  Please add a GPLv2+ message, thanks.

-- 
Tom

^ permalink raw reply	[flat|nested] 20+ messages in thread

* [U-Boot] [PATCH 1/9] combine block device load commands into common function
  2012-09-05 23:36   ` Tom Rini
@ 2012-09-05 23:47     ` Rob Herring
  2012-09-05 23:50       ` Tom Rini
  0 siblings, 1 reply; 20+ messages in thread
From: Rob Herring @ 2012-09-05 23:47 UTC (permalink / raw)
  To: u-boot



On 09/05/2012 06:36 PM, Tom Rini wrote:
> On Thu, Aug 23, 2012 at 04:31:42PM -0500, Rob Herring wrote:
> 
>> From: Rob Herring <rob.herring@calxeda.com>
>>
>> All the raw block load commands duplicate the same code. Starting with
>> the ide version as it has progress updates convert ide, usb, and scsi boot
>> commands to all use a common version.
>>
>> Signed-off-by: Rob Herring <rob.herring@calxeda.com>
> [snip]
>> diff --git a/common/cmd_disk.c b/common/cmd_disk.c
>> new file mode 100644
>> index 0000000..38420dc
>> --- /dev/null
>> +++ b/common/cmd_disk.c
>> @@ -0,0 +1,161 @@
>> +#include <common.h>
>> +#include <command.h>
> 
> Many companies have legal departments that get upset by files without
> license information.  Please add a GPLv2+ message, thanks.

Probably whatever is on the ide code I copied this from is more
appropriate, but yes I need to add that.

Rob

^ permalink raw reply	[flat|nested] 20+ messages in thread

* [U-Boot] [PATCH 1/9] combine block device load commands into common function
  2012-09-05 23:47     ` Rob Herring
@ 2012-09-05 23:50       ` Tom Rini
  0 siblings, 0 replies; 20+ messages in thread
From: Tom Rini @ 2012-09-05 23:50 UTC (permalink / raw)
  To: u-boot

On Wed, Sep 05, 2012 at 06:47:45PM -0500, Rob Herring wrote:
> 
> 
> On 09/05/2012 06:36 PM, Tom Rini wrote:
> > On Thu, Aug 23, 2012 at 04:31:42PM -0500, Rob Herring wrote:
> > 
> >> From: Rob Herring <rob.herring@calxeda.com>
> >>
> >> All the raw block load commands duplicate the same code. Starting with
> >> the ide version as it has progress updates convert ide, usb, and scsi boot
> >> commands to all use a common version.
> >>
> >> Signed-off-by: Rob Herring <rob.herring@calxeda.com>
> > [snip]
> >> diff --git a/common/cmd_disk.c b/common/cmd_disk.c
> >> new file mode 100644
> >> index 0000000..38420dc
> >> --- /dev/null
> >> +++ b/common/cmd_disk.c
> >> @@ -0,0 +1,161 @@
> >> +#include <common.h>
> >> +#include <command.h>
> > 
> > Many companies have legal departments that get upset by files without
> > license information.  Please add a GPLv2+ message, thanks.
> 
> Probably whatever is on the ide code I copied this from is more
> appropriate, but yes I need to add that.

Yes, sorry, I meant the GPLv2+ message that all of the other files have
some variant of.  Copying the cmd_ide.c one on top sounds best.

-- 
Tom

^ permalink raw reply	[flat|nested] 20+ messages in thread

* [U-Boot] [PATCH 3/9] disk/part: introduce get_device_and_partition
  2012-08-24  2:51       ` Stephen Warren
@ 2012-09-05 23:53         ` Tom Rini
  0 siblings, 0 replies; 20+ messages in thread
From: Tom Rini @ 2012-09-05 23:53 UTC (permalink / raw)
  To: u-boot

On Thu, Aug 23, 2012 at 08:51:50PM -0600, Stephen Warren wrote:
> On 08/23/2012 07:57 PM, Rob Herring wrote:
> > On 08/23/2012 05:36 PM, Stephen Warren wrote:
> >> On 08/23/2012 03:31 PM, Rob Herring wrote:
> >>> From: Rob Herring <rob.herring@calxeda.com>
> >>>
> >>> All block device related commands (scsiboot, fatload, ext2ls, etc.) have
> >>> simliar duplicated device and partition parsing and selection code. This
> >>> adds a common function to replace various implementations.
> >>>
> >>> The new function has some enhancements over current versions. If no device
> >>> or partition is specified on the command line, the bootdevice env variable
> >>> will be used (scsiboot does this). If the partition is not specified and
> >>> the device has partitions, then the first bootable partition will be used.
> >>> If a bootable partition is not found, the first valid partition is used.
> >>> The ret value is not needed since part will be zero when no partition is
> >>> found.
> >>
> >> Two thoughts on this patch:
> >>
> >> First, if I write "mmc 0" right now, command will always attempt to
> >> access precisely partion 1, whereas after this patch, they will search
> >> for the first bootable, or valid, partition. This is a change in
> >> behavior. It's a pretty reasonable change, but I wonder if it might
> >> cause problems somewhere.
> >>
> >> Instead, perhaps this new feature should be explicitly requested,
> >> supporting the following device/partition specifications:
> >>
> >> # existing:
> >> dev 0:0        # whole device
> >> dev 0:n        # n >= 1: explicit partition
> >> dev 0          # partition 1
> >> # new:
> >> dev 0:valid    # first valid partition
> >> dev 0:bootable # first bootable partition
> >> dev 0:default  # first bootable partition if there is one,
> >>                # else first valid
> > 
> > I'm not sure we need to distinguish valid vs. bootable. Returning the
> > first valid partition was really just to maintain somewhat backwards
> > compatible behavior.
> > 
> > Perhaps just "0:-" would be sufficient.
> 
> I guess that syntax would be fine if we don't need to distinguish all
> the cases. "-" isn't that descriptive though, and I've only seen it mean
> "nothing" in U-Boot commands. So, bike-shedding a bit, it doesn't seem
> exactly correct. Perhaps just "auto"?

"auto" sounds like a good idea to me as well.

[snip]
> >> Instead, I wonder if get_device_and_partition() should just support the
> >> existing 3 device specification options, and we introduce a new command
> >> to determine which partition to boot from, e.g.:
> >>
> >> # writes result to "bootpart" variable
> >> # or get-default or get-first-valid
> >> part get-first-bootable mmc 0 bootpart
> >>
> >> ext2load mmc 0:${bootpart} ${scriptaddr} boot.scr
> >> source ${scriptaddr}
> >> # script does:
> >> ext2load mmc 0:${bootpart} ${kernel_addr} zImage
> >> ext2load mmc 0:${bootpart} ${initrd_addr} initrd.bin
> >> ext2load mmc 0:${bootpart} ${fdt_addr} foo.dtb
> >>
> >> That solves those issues. Does anyone have any comment on the two
> >> approaches?
> > 
> > I'm really open to either way.
> > 
> > Another option would be for the first command run to set bootpart and
> > then re-use that value on subsequent commands.
> 
> That could work too, although commands using environment variables seems
> a little implicit/hidden.

Agreed.  We should be very careful when changing behavior.  Adding a new
command so that folks can use this as they see fit sounds like the best
idea.  I shudder to think what the partition table on some SD cards I
have around looks like.

-- 
Tom

^ permalink raw reply	[flat|nested] 20+ messages in thread

* [U-Boot] [PATCH v2 1/9] combine block device load commands into common function
  2012-08-23 21:31 ` [U-Boot] [PATCH 1/9] combine block device load commands into common function Rob Herring
  2012-09-05 23:36   ` Tom Rini
@ 2012-09-21 14:02   ` Rob Herring
  2012-09-25 23:17     ` Tom Rini
  1 sibling, 1 reply; 20+ messages in thread
From: Rob Herring @ 2012-09-21 14:02 UTC (permalink / raw)
  To: u-boot

From: Rob Herring <rob.herring@calxeda.com>

All the raw block load commands duplicate the same code. Starting with
the ide version as it has progress updates convert ide, usb, and scsi boot
commands to all use a common version.

Signed-off-by: Rob Herring <rob.herring@calxeda.com>
---
v2:
- Add copyright to cmd_disk.c

 common/Makefile   |    1 +
 common/cmd_disk.c |  183 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 common/cmd_ide.c  |  151 +------------------------------------------
 common/cmd_scsi.c |  123 +----------------------------------
 common/cmd_usb.c  |  138 +---------------------------------------
 include/command.h |    4 ++
 6 files changed, 191 insertions(+), 409 deletions(-)
 create mode 100644 common/cmd_disk.c

diff --git a/common/Makefile b/common/Makefile
index 4273484..0fac6d0 100644
--- a/common/Makefile
+++ b/common/Makefile
@@ -34,6 +34,7 @@ COBJS-$(CONFIG_SYS_HUSH_PARSER) += hush.o
 COBJS-y += s_record.o
 COBJS-$(CONFIG_SERIAL_MULTI) += serial.o
 COBJS-y += xyzModem.o
+COBJS-y += cmd_disk.o
 
 # core command
 COBJS-y += cmd_boot.o
diff --git a/common/cmd_disk.c b/common/cmd_disk.c
new file mode 100644
index 0000000..e73fc4e
--- /dev/null
+++ b/common/cmd_disk.c
@@ -0,0 +1,183 @@
+/*
+ * (C) Copyright 2000-2011
+ * Wolfgang Denk, DENX Software Engineering, wd at denx.de.
+ *
+ * See file CREDITS for list of people who contributed to this
+ * project.
+ *
+ * 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
+ *
+ */
+#include <common.h>
+#include <command.h>
+
+int common_diskboot(cmd_tbl_t *cmdtp, const char *intf, int argc,
+		    char *const argv[])
+{
+	char *boot_device = NULL;
+	char *ep;
+	int dev, part = 0;
+	ulong addr, cnt;
+	disk_partition_t info;
+	image_header_t *hdr;
+	block_dev_desc_t *dev_desc;
+
+#if defined(CONFIG_FIT)
+	const void *fit_hdr = NULL;
+#endif
+
+	bootstage_mark(BOOTSTAGE_ID_IDE_START);
+	switch (argc) {
+	case 1:
+		addr = CONFIG_SYS_LOAD_ADDR;
+		boot_device = getenv("bootdevice");
+		break;
+	case 2:
+		addr = simple_strtoul(argv[1], NULL, 16);
+		boot_device = getenv("bootdevice");
+		break;
+	case 3:
+		addr = simple_strtoul(argv[1], NULL, 16);
+		boot_device = argv[2];
+		break;
+	default:
+		bootstage_error(BOOTSTAGE_ID_IDE_ADDR);
+		return CMD_RET_USAGE;
+	}
+	bootstage_mark(BOOTSTAGE_ID_IDE_ADDR);
+
+	if (!boot_device) {
+		puts("\n** No boot device **\n");
+		bootstage_error(BOOTSTAGE_ID_IDE_BOOT_DEVICE);
+		return 1;
+	}
+	bootstage_mark(BOOTSTAGE_ID_IDE_BOOT_DEVICE);
+
+	dev = simple_strtoul(boot_device, &ep, 16);
+
+	dev_desc = get_dev(intf, dev);
+	if (dev_desc->type == DEV_TYPE_UNKNOWN) {
+		printf("\n** Device %d not available\n", dev);
+		bootstage_error(BOOTSTAGE_ID_IDE_TYPE);
+		return 1;
+	}
+	bootstage_mark(BOOTSTAGE_ID_IDE_TYPE);
+
+	if (*ep) {
+		if (*ep != ':') {
+			puts("\n** Invalid boot device, use `dev[:part]' **\n");
+			bootstage_error(BOOTSTAGE_ID_IDE_PART);
+			return 1;
+		}
+		part = simple_strtoul(++ep, NULL, 16);
+	}
+	bootstage_mark(BOOTSTAGE_ID_IDE_PART);
+
+	if (get_partition_info(dev_desc, part, &info)) {
+		bootstage_error(BOOTSTAGE_ID_IDE_PART_INFO);
+		return 1;
+	}
+	bootstage_mark(BOOTSTAGE_ID_IDE_PART_INFO);
+
+	if ((strncmp((char *)info.type, BOOT_PART_TYPE, sizeof(info.type)) != 0)
+	    &&
+	    (strncmp((char *)info.type, BOOT_PART_COMP, sizeof(info.type)) != 0)
+	   ) {
+		printf("\n** Invalid partition type \"%.32s\"" " (expect \""
+			BOOT_PART_TYPE "\")\n",
+			info.type);
+		bootstage_error(BOOTSTAGE_ID_IDE_PART_TYPE);
+		return 1;
+	}
+	bootstage_mark(BOOTSTAGE_ID_IDE_PART_TYPE);
+
+	printf("\nLoading from disk device %d, partition %d: "
+	       "Name: %.32s  Type: %.32s\n", dev, part, info.name, info.type);
+
+	debug("First Block: %ld,  # of blocks: %ld, Block Size: %ld\n",
+	      info.start, info.size, info.blksz);
+
+	if (dev_desc->block_read(dev, info.start, 1, (ulong *) addr) != 1) {
+		printf("** Read error on %d:%d\n", dev, part);
+		bootstage_error(BOOTSTAGE_ID_IDE_PART_READ);
+		return 1;
+	}
+	bootstage_mark(BOOTSTAGE_ID_IDE_PART_READ);
+
+	switch (genimg_get_format((void *) addr)) {
+	case IMAGE_FORMAT_LEGACY:
+		hdr = (image_header_t *) addr;
+
+		bootstage_mark(BOOTSTAGE_ID_IDE_FORMAT);
+
+		if (!image_check_hcrc(hdr)) {
+			puts("\n** Bad Header Checksum **\n");
+			bootstage_error(BOOTSTAGE_ID_IDE_CHECKSUM);
+			return 1;
+		}
+		bootstage_mark(BOOTSTAGE_ID_IDE_CHECKSUM);
+
+		image_print_contents(hdr);
+
+		cnt = image_get_image_size(hdr);
+		break;
+#if defined(CONFIG_FIT)
+	case IMAGE_FORMAT_FIT:
+		fit_hdr = (const void *) addr;
+		puts("Fit image detected...\n");
+
+		cnt = fit_get_size(fit_hdr);
+		break;
+#endif
+	default:
+		bootstage_error(BOOTSTAGE_ID_IDE_FORMAT);
+		puts("** Unknown image type\n");
+		return 1;
+	}
+
+	cnt += info.blksz - 1;
+	cnt /= info.blksz;
+	cnt -= 1;
+
+	if (dev_desc->block_read(dev, info.start + 1, cnt,
+					 (ulong *)(addr + info.blksz)) != cnt) {
+		printf("** Read error on %d:%d\n", dev, part);
+		bootstage_error(BOOTSTAGE_ID_IDE_READ);
+		return 1;
+	}
+	bootstage_mark(BOOTSTAGE_ID_IDE_READ);
+
+#if defined(CONFIG_FIT)
+	/* This cannot be done earlier,
+	 * we need complete FIT image in RAM first */
+	if (genimg_get_format((void *) addr) == IMAGE_FORMAT_FIT) {
+		if (!fit_check_format(fit_hdr)) {
+			bootstage_error(BOOTSTAGE_ID_IDE_FIT_READ);
+			puts("** Bad FIT image format\n");
+			return 1;
+		}
+		bootstage_mark(BOOTSTAGE_ID_IDE_FIT_READ_OK);
+		fit_print_contents(fit_hdr);
+	}
+#endif
+
+	flush_cache(addr, (cnt+1)*info.blksz);
+
+	/* Loading ok, update default load address */
+	load_addr = addr;
+
+	return bootm_maybe_autostart(cmdtp, argv[0]);
+}
diff --git a/common/cmd_ide.c b/common/cmd_ide.c
index f5b6c7b..6e1e568 100644
--- a/common/cmd_ide.c
+++ b/common/cmd_ide.c
@@ -334,156 +334,7 @@ int do_ide(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
 
 int do_diskboot(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
 {
-	char *boot_device = NULL;
-	char *ep;
-	int dev, part = 0;
-	ulong addr, cnt;
-	disk_partition_t info;
-	image_header_t *hdr;
-
-#if defined(CONFIG_FIT)
-	const void *fit_hdr = NULL;
-#endif
-
-	bootstage_mark(BOOTSTAGE_ID_IDE_START);
-	switch (argc) {
-	case 1:
-		addr = CONFIG_SYS_LOAD_ADDR;
-		boot_device = getenv("bootdevice");
-		break;
-	case 2:
-		addr = simple_strtoul(argv[1], NULL, 16);
-		boot_device = getenv("bootdevice");
-		break;
-	case 3:
-		addr = simple_strtoul(argv[1], NULL, 16);
-		boot_device = argv[2];
-		break;
-	default:
-		bootstage_error(BOOTSTAGE_ID_IDE_ADDR);
-		return CMD_RET_USAGE;
-	}
-	bootstage_mark(BOOTSTAGE_ID_IDE_ADDR);
-
-	if (!boot_device) {
-		puts("\n** No boot device **\n");
-		bootstage_error(BOOTSTAGE_ID_IDE_BOOT_DEVICE);
-		return 1;
-	}
-	bootstage_mark(BOOTSTAGE_ID_IDE_BOOT_DEVICE);
-
-	dev = simple_strtoul(boot_device, &ep, 16);
-
-	if (ide_dev_desc[dev].type == DEV_TYPE_UNKNOWN) {
-		printf("\n** Device %d not available\n", dev);
-		bootstage_error(BOOTSTAGE_ID_IDE_TYPE);
-		return 1;
-	}
-	bootstage_mark(BOOTSTAGE_ID_IDE_TYPE);
-
-	if (*ep) {
-		if (*ep != ':') {
-			puts("\n** Invalid boot device, use `dev[:part]' **\n");
-			bootstage_error(BOOTSTAGE_ID_IDE_PART);
-			return 1;
-		}
-		part = simple_strtoul(++ep, NULL, 16);
-	}
-	bootstage_mark(BOOTSTAGE_ID_IDE_PART);
-
-	if (get_partition_info(&ide_dev_desc[dev], part, &info)) {
-		bootstage_error(BOOTSTAGE_ID_IDE_PART_INFO);
-		return 1;
-	}
-	bootstage_mark(BOOTSTAGE_ID_IDE_PART_INFO);
-
-	if ((strncmp((char *)info.type, BOOT_PART_TYPE, sizeof(info.type)) != 0)
-	    &&
-	    (strncmp((char *)info.type, BOOT_PART_COMP, sizeof(info.type)) != 0)
-	   ) {
-		printf("\n** Invalid partition type \"%.32s\"" " (expect \""
-			BOOT_PART_TYPE "\")\n",
-			info.type);
-		bootstage_error(BOOTSTAGE_ID_IDE_PART_TYPE);
-		return 1;
-	}
-	bootstage_mark(BOOTSTAGE_ID_IDE_PART_TYPE);
-
-	printf("\nLoading from IDE device %d, partition %d: "
-	       "Name: %.32s  Type: %.32s\n", dev, part, info.name, info.type);
-
-	debug("First Block: %ld,  # of blocks: %ld, Block Size: %ld\n",
-	      info.start, info.size, info.blksz);
-
-	if (ide_dev_desc[dev].
-	    block_read(dev, info.start, 1, (ulong *) addr) != 1) {
-		printf("** Read error on %d:%d\n", dev, part);
-		bootstage_error(BOOTSTAGE_ID_IDE_PART_READ);
-		return 1;
-	}
-	bootstage_mark(BOOTSTAGE_ID_IDE_PART_READ);
-
-	switch (genimg_get_format((void *) addr)) {
-	case IMAGE_FORMAT_LEGACY:
-		hdr = (image_header_t *) addr;
-
-		bootstage_mark(BOOTSTAGE_ID_IDE_FORMAT);
-
-		if (!image_check_hcrc(hdr)) {
-			puts("\n** Bad Header Checksum **\n");
-			bootstage_error(BOOTSTAGE_ID_IDE_CHECKSUM);
-			return 1;
-		}
-		bootstage_mark(BOOTSTAGE_ID_IDE_CHECKSUM);
-
-		image_print_contents(hdr);
-
-		cnt = image_get_image_size(hdr);
-		break;
-#if defined(CONFIG_FIT)
-	case IMAGE_FORMAT_FIT:
-		fit_hdr = (const void *) addr;
-		puts("Fit image detected...\n");
-
-		cnt = fit_get_size(fit_hdr);
-		break;
-#endif
-	default:
-		bootstage_error(BOOTSTAGE_ID_IDE_FORMAT);
-		puts("** Unknown image type\n");
-		return 1;
-	}
-
-	cnt += info.blksz - 1;
-	cnt /= info.blksz;
-	cnt -= 1;
-
-	if (ide_dev_desc[dev].block_read(dev, info.start + 1, cnt,
-					 (ulong *)(addr + info.blksz)) != cnt) {
-		printf("** Read error on %d:%d\n", dev, part);
-		bootstage_error(BOOTSTAGE_ID_IDE_READ);
-		return 1;
-	}
-	bootstage_mark(BOOTSTAGE_ID_IDE_READ);
-
-#if defined(CONFIG_FIT)
-	/* This cannot be done earlier, we need complete FIT image in RAM first */
-	if (genimg_get_format((void *) addr) == IMAGE_FORMAT_FIT) {
-		if (!fit_check_format(fit_hdr)) {
-			bootstage_error(BOOTSTAGE_ID_IDE_FIT_READ);
-			puts("** Bad FIT image format\n");
-			return 1;
-		}
-		bootstage_mark(BOOTSTAGE_ID_IDE_FIT_READ_OK);
-		fit_print_contents(fit_hdr);
-	}
-#endif
-
-	/* Loading ok, update default load address */
-
-	load_addr = addr;
-
-	return bootm_maybe_autostart(cmdtp, argv[0]);
+	return common_diskboot(cmdtp, "ide", argc, argv);
 }
 
 /* ------------------------------------------------------------------------- */
diff --git a/common/cmd_scsi.c b/common/cmd_scsi.c
index d15b567..22d0119 100644
--- a/common/cmd_scsi.c
+++ b/common/cmd_scsi.c
@@ -206,128 +206,7 @@ block_dev_desc_t * scsi_get_dev(int dev)
  */
 int do_scsiboot (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 {
-	char *boot_device = NULL;
-	char *ep;
-	int dev, part = 0;
-	ulong addr, cnt;
-	disk_partition_t info;
-	image_header_t *hdr;
-#if defined(CONFIG_FIT)
-	const void *fit_hdr = NULL;
-#endif
-
-	switch (argc) {
-	case 1:
-		addr = CONFIG_SYS_LOAD_ADDR;
-		boot_device = getenv ("bootdevice");
-		break;
-	case 2:
-		addr = simple_strtoul(argv[1], NULL, 16);
-		boot_device = getenv ("bootdevice");
-		break;
-	case 3:
-		addr = simple_strtoul(argv[1], NULL, 16);
-		boot_device = argv[2];
-		break;
-	default:
-		return CMD_RET_USAGE;
-	}
-
-	if (!boot_device) {
-		puts ("\n** No boot device **\n");
-		return 1;
-	}
-
-	dev = simple_strtoul(boot_device, &ep, 16);
-	printf("booting from dev %d\n",dev);
-	if (scsi_dev_desc[dev].type == DEV_TYPE_UNKNOWN) {
-		printf ("\n** Device %d not available\n", dev);
-		return 1;
-	}
-
-	if (*ep) {
-		if (*ep != ':') {
-			puts ("\n** Invalid boot device, use `dev[:part]' **\n");
-			return 1;
-		}
-		part = simple_strtoul(++ep, NULL, 16);
-	}
-	if (get_partition_info (&scsi_dev_desc[dev], part, &info)) {
-		printf("error reading partinfo\n");
-		return 1;
-	}
-	if ((strncmp((char *)(info.type), BOOT_PART_TYPE, sizeof(info.type)) != 0) &&
-	    (strncmp((char *)(info.type), BOOT_PART_COMP, sizeof(info.type)) != 0)) {
-		printf ("\n** Invalid partition type \"%.32s\""
-			" (expect \"" BOOT_PART_TYPE "\")\n",
-			info.type);
-		return 1;
-	}
-
-	printf ("\nLoading from SCSI device %d, partition %d: "
-		"Name: %.32s  Type: %.32s\n",
-		dev, part, info.name, info.type);
-
-	debug ("First Block: %ld,  # of blocks: %ld, Block Size: %ld\n",
-		info.start, info.size, info.blksz);
-
-	if (scsi_read (dev, info.start, 1, (ulong *)addr) != 1) {
-		printf ("** Read error on %d:%d\n", dev, part);
-		return 1;
-	}
-
-	switch (genimg_get_format ((void *)addr)) {
-	case IMAGE_FORMAT_LEGACY:
-		hdr = (image_header_t *)addr;
-
-		if (!image_check_hcrc (hdr)) {
-			puts ("\n** Bad Header Checksum **\n");
-			return 1;
-		}
-
-		image_print_contents (hdr);
-		cnt = image_get_image_size (hdr);
-		break;
-#if defined(CONFIG_FIT)
-	case IMAGE_FORMAT_FIT:
-		fit_hdr = (const void *)addr;
-		puts ("Fit image detected...\n");
-
-		cnt = fit_get_size (fit_hdr);
-		break;
-#endif
-	default:
-		puts ("** Unknown image type\n");
-		return 1;
-	}
-
-	cnt += info.blksz - 1;
-	cnt /= info.blksz;
-	cnt -= 1;
-
-	if (scsi_read (dev, info.start+1, cnt,
-		      (ulong *)(addr+info.blksz)) != cnt) {
-		printf ("** Read error on %d:%d\n", dev, part);
-		return 1;
-	}
-
-#if defined(CONFIG_FIT)
-	/* This cannot be done earlier, we need complete FIT image in RAM first */
-	if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT) {
-		if (!fit_check_format (fit_hdr)) {
-			puts ("** Bad FIT image format\n");
-			return 1;
-		}
-		fit_print_contents (fit_hdr);
-	}
-#endif
-
-	/* Loading ok, update default load address */
-	load_addr = addr;
-
-	flush_cache (addr, (cnt+1)*info.blksz);
-
-	return bootm_maybe_autostart(cmdtp, argv[0]);
+	return common_diskboot(cmdtp, "scsi", argc, argv);
 }
 
 /*********************************************************************************
diff --git a/common/cmd_usb.c b/common/cmd_usb.c
index a8e3ae5..181e727 100644
--- a/common/cmd_usb.c
+++ b/common/cmd_usb.c
@@ -355,143 +355,7 @@ void usb_show_tree(struct usb_device *dev)
 #ifdef CONFIG_USB_STORAGE
 int do_usbboot(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 {
-	char *boot_device = NULL;
-	char *ep;
-	int dev, part = 1;
-	ulong addr, cnt;
-	disk_partition_t info;
-	image_header_t *hdr;
-	block_dev_desc_t *stor_dev;
-#if defined(CONFIG_FIT)
-	const void *fit_hdr = NULL;
-#endif
-
-	switch (argc) {
-	case 1:
-		addr = CONFIG_SYS_LOAD_ADDR;
-		boot_device = getenv("bootdevice");
-		break;
-	case 2:
-		addr = simple_strtoul(argv[1], NULL, 16);
-		boot_device = getenv("bootdevice");
-		break;
-	case 3:
-		addr = simple_strtoul(argv[1], NULL, 16);
-		boot_device = argv[2];
-		break;
-	default:
-		return CMD_RET_USAGE;
-	}
-
-	if (!boot_device) {
-		puts("\n** No boot device **\n");
-		return 1;
-	}
-
-	dev = simple_strtoul(boot_device, &ep, 16);
-	stor_dev = usb_stor_get_dev(dev);
-	if (stor_dev == NULL || stor_dev->type == DEV_TYPE_UNKNOWN) {
-		printf("\n** Device %d not available\n", dev);
-		return 1;
-	}
-	if (stor_dev->block_read == NULL) {
-		printf("storage device not initialized. Use usb scan\n");
-		return 1;
-	}
-	if (*ep) {
-		if (*ep != ':') {
-			puts("\n** Invalid boot device, use `dev[:part]' **\n");
-			return 1;
-		}
-		part = simple_strtoul(++ep, NULL, 16);
-	}
-
-	if (get_partition_info(stor_dev, part, &info)) {
-		/* try to boot raw .... */
-		strncpy((char *)&info.type[0], BOOT_PART_TYPE,
-			sizeof(BOOT_PART_TYPE));
-		strncpy((char *)&info.name[0], "Raw", 4);
-		info.start = 0;
-		info.blksz = 0x200;
-		info.size = 2880;
-		printf("error reading partinfo...try to boot raw\n");
-	}
-	if ((strncmp((char *)info.type, BOOT_PART_TYPE,
-	    sizeof(info.type)) != 0) &&
-	    (strncmp((char *)info.type, BOOT_PART_COMP,
-	    sizeof(info.type)) != 0)) {
-		printf("\n** Invalid partition type \"%.32s\""
-			" (expect \"" BOOT_PART_TYPE "\")\n",
-			info.type);
-		return 1;
-	}
-	printf("\nLoading from USB device %d, partition %d: "
-		"Name: %.32s  Type: %.32s\n",
-		dev, part, info.name, info.type);
-
-	debug("First Block: %ld,  # of blocks: %ld, Block Size: %ld\n",
-		info.start, info.size, info.blksz);
-
-	if (stor_dev->block_read(dev, info.start, 1, (ulong *)addr) != 1) {
-		printf("** Read error on %d:%d\n", dev, part);
-		return 1;
-	}
-
-	switch (genimg_get_format((void *)addr)) {
-	case IMAGE_FORMAT_LEGACY:
-		hdr = (image_header_t *)addr;
-
-		if (!image_check_hcrc(hdr)) {
-			puts("\n** Bad Header Checksum **\n");
-			return 1;
-		}
-
-		image_print_contents(hdr);
-
-		cnt = image_get_image_size(hdr);
-		break;
-#if defined(CONFIG_FIT)
-	case IMAGE_FORMAT_FIT:
-		fit_hdr = (const void *)addr;
-		puts("Fit image detected...\n");
-
-		cnt = fit_get_size(fit_hdr);
-		break;
-#endif
-	default:
-		puts("** Unknown image type\n");
-		return 1;
-	}
-
-	cnt += info.blksz - 1;
-	cnt /= info.blksz;
-	cnt -= 1;
-
-	if (stor_dev->block_read(dev, info.start+1, cnt,
-		      (ulong *)(addr+info.blksz)) != cnt) {
-		printf("\n** Read error on %d:%d\n", dev, part);
-		return 1;
-	}
-
-#if defined(CONFIG_FIT)
-	/* This cannot be done earlier, we need complete FIT image in RAM
-	 * first
-	 */
-	if (genimg_get_format((void *)addr) == IMAGE_FORMAT_FIT) {
-		if (!fit_check_format(fit_hdr)) {
-			puts("** Bad FIT image format\n");
-			return 1;
-		}
-		fit_print_contents(fit_hdr);
-	}
-#endif
-
-	/* Loading ok, update default load address */
-	load_addr = addr;
-
-	flush_cache(addr, (cnt+1)*info.blksz);
-
-	return bootm_maybe_autostart(cmdtp, argv[0]);
+	return common_diskboot(cmdtp, "usb", argc, argv);
 }
 #endif /* CONFIG_USB_STORAGE */
 
diff --git a/include/command.h b/include/command.h
index 6e1bdc2..1f06aa1 100644
--- a/include/command.h
+++ b/include/command.h
@@ -110,6 +110,10 @@ static inline int bootm_maybe_autostart(cmd_tbl_t *cmdtp, const char *cmd)
 	return 0;
 }
 #endif
+
+extern int common_diskboot(cmd_tbl_t *cmdtp, const char *intf, int argc,
+			   char *const argv[]);
+
 extern int do_reset(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
 
 /*
-- 
1.7.9.5

^ permalink raw reply related	[flat|nested] 20+ messages in thread

* [U-Boot] [PATCH v2 3/9] disk/part: introduce get_device_and_partition
  2012-08-23 21:31 ` [U-Boot] [PATCH 3/9] disk/part: introduce get_device_and_partition Rob Herring
  2012-08-23 22:36   ` Stephen Warren
@ 2012-09-21 14:08   ` Rob Herring
  1 sibling, 0 replies; 20+ messages in thread
From: Rob Herring @ 2012-09-21 14:08 UTC (permalink / raw)
  To: u-boot

From: Rob Herring <rob.herring@calxeda.com>

All block device related commands (scsiboot, fatload, ext2ls, etc.) have
simliar duplicated device and partition parsing and selection code. This
adds a common function to replace various implementations.

The new function has an enhancement over current versions. If no device
or partition is specified on the command line, the bootdevice env variable
will be used (scsiboot does this).

Signed-off-by: Rob Herring <rob.herring@calxeda.com>
---
v2:
- Keep current behavior removing the bootable partition scanning. This will
be added in follow-up series from Stephen Warren.

 disk/part.c    |   69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++--
 include/part.h |   13 +++++++++--
 2 files changed, 78 insertions(+), 4 deletions(-)

diff --git a/disk/part.c b/disk/part.c
index 76f3939..958656c 100644
--- a/disk/part.c
+++ b/disk/part.c
@@ -70,7 +70,7 @@ static const struct block_drvr block_drvr[] = {
 
 DECLARE_GLOBAL_DATA_PTR;
 
-block_dev_desc_t *get_dev(char* ifname, int dev)
+block_dev_desc_t *get_dev(const char *ifname, int dev)
 {
 	const struct block_drvr *drvr = block_drvr;
 	block_dev_desc_t* (*reloc_get_dev)(int dev);
@@ -97,7 +97,7 @@ block_dev_desc_t *get_dev(char* ifname, int dev)
 	return NULL;
 }
 #else
-block_dev_desc_t *get_dev(char* ifname, int dev)
+block_dev_desc_t *get_dev(const char *ifname, int dev)
 {
 	return NULL;
 }
@@ -288,6 +288,7 @@ void init_part (block_dev_desc_t * dev_desc)
 	    return;
 	}
 #endif
+	dev_desc->part_type = PART_TYPE_UNKNOWN;
 }
 
 
@@ -433,3 +434,67 @@ void print_part (block_dev_desc_t * dev_desc)
 #endif
 
 #endif
+
+int get_device_and_partition(const char *ifname, const char *dev_str,
+			     block_dev_desc_t **dev_desc,
+			     disk_partition_t *info)
+{
+	int ret;
+	char *ep;
+	int dev;
+	block_dev_desc_t *desc;
+	int part = 0;
+	char *part_str;
+
+	if (dev_str)
+		dev = simple_strtoul(dev_str, &ep, 16);
+
+	if (!dev_str || (dev_str == ep)) {
+		dev_str = getenv("bootdevice");
+		if (dev_str)
+			dev = simple_strtoul(dev_str, &ep, 16);
+		if (!dev_str || (dev_str == ep))
+			goto err;
+	}
+
+	desc = get_dev(ifname, dev);
+	if (!desc || (desc->type == DEV_TYPE_UNKNOWN))
+		goto err;
+
+	if (desc->part_type == PART_TYPE_UNKNOWN) {
+		/* disk doesn't use partition table */
+		if (!desc->lba) {
+			printf("**Bad disk size - %s %d:0 **\n", ifname, dev);
+			return -1;
+		}
+		info->start = 0;
+		info->size = desc->lba;
+		info->blksz = desc->blksz;
+
+		*dev_desc = desc;
+		return 0;
+	}
+
+	part_str = strchr(dev_str, ':');
+	if (part_str)
+		part = (int)simple_strtoul(++part_str, NULL, 16);
+
+	ret = get_partition_info(desc, part, info);
+	if (ret) {
+		printf("** Invalid partition %d, use `dev[:part]' **\n", part);
+		return -1;
+	}
+	if (strncmp((char *)info->type, BOOT_PART_TYPE, sizeof(info->type)) != 0) {
+		printf("** Invalid partition type \"%.32s\""
+			" (expect \"" BOOT_PART_TYPE "\")\n",
+			info->type);
+		return -1;
+	}
+
+	*dev_desc = desc;
+	return part;
+
+ err:
+	puts("** Invalid boot device, use `dev[:part]' **\n");
+	return -1;
+}
diff --git a/include/part.h b/include/part.h
index 447f69d..a6d06f3 100644
--- a/include/part.h
+++ b/include/part.h
@@ -98,7 +98,7 @@ typedef struct disk_partition {
 
 /* Misc _get_dev functions */
 #ifdef CONFIG_PARTITIONS
-block_dev_desc_t* get_dev(char* ifname, int dev);
+block_dev_desc_t *get_dev(const char *ifname, int dev);
 block_dev_desc_t* ide_get_dev(int dev);
 block_dev_desc_t* sata_get_dev(int dev);
 block_dev_desc_t* scsi_get_dev(int dev);
@@ -112,8 +112,12 @@ int get_partition_info (block_dev_desc_t * dev_desc, int part, disk_partition_t
 void print_part (block_dev_desc_t *dev_desc);
 void  init_part (block_dev_desc_t *dev_desc);
 void dev_print(block_dev_desc_t *dev_desc);
+int get_device_and_partition(const char *ifname, const char *dev_str,
+			     block_dev_desc_t **dev_desc,
+			     disk_partition_t *info);
 #else
-static inline block_dev_desc_t* get_dev(char* ifname, int dev) { return NULL; }
+static inline block_dev_desc_t *get_dev(const char *ifname, int dev)
+{ return NULL; }
 static inline block_dev_desc_t* ide_get_dev(int dev) { return NULL; }
 static inline block_dev_desc_t* sata_get_dev(int dev) { return NULL; }
 static inline block_dev_desc_t* scsi_get_dev(int dev) { return NULL; }
@@ -127,6 +131,11 @@ static inline int get_partition_info (block_dev_desc_t * dev_desc, int part,
 static inline void print_part (block_dev_desc_t *dev_desc) {}
 static inline void  init_part (block_dev_desc_t *dev_desc) {}
 static inline void dev_print(block_dev_desc_t *dev_desc) {}
+static inline int get_device_and_partition(const char *ifname,
+					   const char *dev_str,
+					   block_dev_desc_t **dev_desc,
+					   disk_partition_t *info)
+{ *dev_desc = NULL; return -1; }
 #endif
 
 #ifdef CONFIG_MAC_PARTITION
-- 
1.7.9.5

^ permalink raw reply related	[flat|nested] 20+ messages in thread

* [U-Boot] [PATCH v2 1/9] combine block device load commands into common function
  2012-09-21 14:02   ` [U-Boot] [PATCH v2 " Rob Herring
@ 2012-09-25 23:17     ` Tom Rini
  0 siblings, 0 replies; 20+ messages in thread
From: Tom Rini @ 2012-09-25 23:17 UTC (permalink / raw)
  To: u-boot

On Fri, Sep 21, 2012 at 09:02:30AM -0500, Rob Herring wrote:

> From: Rob Herring <rob.herring@calxeda.com>
> 
> All the raw block load commands duplicate the same code. Starting with
> the ide version as it has progress updates convert ide, usb, and scsi boot
> commands to all use a common version.
> 
> Signed-off-by: Rob Herring <rob.herring@calxeda.com>

This along with the rest of the series (v2 on 3/9), are now applied to
u-boot/master, thanks!

-- 
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/20120925/67c906b3/attachment.pgp>

^ permalink raw reply	[flat|nested] 20+ messages in thread

end of thread, other threads:[~2012-09-25 23:17 UTC | newest]

Thread overview: 20+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-08-23 21:31 [U-Boot] [PATCH 0/9] Auto partition selection and fs partition consolidation Rob Herring
2012-08-23 21:31 ` [U-Boot] [PATCH 1/9] combine block device load commands into common function Rob Herring
2012-09-05 23:36   ` Tom Rini
2012-09-05 23:47     ` Rob Herring
2012-09-05 23:50       ` Tom Rini
2012-09-21 14:02   ` [U-Boot] [PATCH v2 " Rob Herring
2012-09-25 23:17     ` Tom Rini
2012-08-23 21:31 ` [U-Boot] [PATCH 2/9] disk/part: check bootable flag for DOS partitions Rob Herring
2012-08-23 21:31 ` [U-Boot] [PATCH 3/9] disk/part: introduce get_device_and_partition Rob Herring
2012-08-23 22:36   ` Stephen Warren
2012-08-24  1:57     ` Rob Herring
2012-08-24  2:51       ` Stephen Warren
2012-09-05 23:53         ` Tom Rini
2012-09-21 14:08   ` [U-Boot] [PATCH v2 " Rob Herring
2012-08-23 21:31 ` [U-Boot] [PATCH 4/9] ext4: remove init_fs/deinit_fs Rob Herring
2012-08-23 21:31 ` [U-Boot] [PATCH 5/9] cmd_extX: use common get_device_and_partition function Rob Herring
2012-08-23 21:31 ` [U-Boot] [PATCH 6/9] cmd_fat: " Rob Herring
2012-08-23 21:31 ` [U-Boot] [PATCH 7/9] cmd_disk: " Rob Herring
2012-08-23 21:31 ` [U-Boot] [PATCH 8/9] cmd_zfs: " Rob Herring
2012-08-23 21:31 ` [U-Boot] [PATCH 9/9] cmd_reiser: " Rob Herring

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.