[U-Boot] [PATCH 1/9] combine block device load commands into common function

Rob Herring robherring2 at gmail.com
Thu Aug 23 23:31:42 CEST 2012


From: Rob Herring <rob.herring at 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 at 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



More information about the U-Boot mailing list