disk.c 3.13 KB
Newer Older
1 2 3 4
/*
 * (C) Copyright 2000-2011
 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
 *
5
 * SPDX-License-Identifier:	GPL-2.0+
6 7 8
 */
#include <common.h>
#include <command.h>
9
#include <part.h>
10

11
#if defined(CONFIG_CMD_IDE) || defined(CONFIG_SCSI) || \
12
	defined(CONFIG_USB_STORAGE)
13 14 15
int common_diskboot(cmd_tbl_t *cmdtp, const char *intf, int argc,
		    char *const argv[])
{
16 17 18
	int dev, part;
	ulong addr = CONFIG_SYS_LOAD_ADDR;
	ulong cnt;
19
	disk_partition_t info;
20
#if defined(CONFIG_IMAGE_FORMAT_LEGACY)
21
	image_header_t *hdr;
22
#endif
23
	struct blk_desc *dev_desc;
24

25
#if CONFIG_IS_ENABLED(FIT)
26 27 28 29
	const void *fit_hdr = NULL;
#endif

	bootstage_mark(BOOTSTAGE_ID_IDE_START);
30
	if (argc > 3) {
31 32 33 34 35
		bootstage_error(BOOTSTAGE_ID_IDE_ADDR);
		return CMD_RET_USAGE;
	}
	bootstage_mark(BOOTSTAGE_ID_IDE_ADDR);

36 37
	if (argc > 1)
		addr = simple_strtoul(argv[1], NULL, 16);
38

39
	bootstage_mark(BOOTSTAGE_ID_IDE_BOOT_DEVICE);
40

41
	part = blk_get_device_part_str(intf, (argc == 3) ? argv[2] : NULL,
42
					&dev_desc, &info, 1);
43
	if (part < 0) {
44 45 46 47
		bootstage_error(BOOTSTAGE_ID_IDE_TYPE);
		return 1;
	}

48
	dev = dev_desc->devnum;
49
	bootstage_mark(BOOTSTAGE_ID_IDE_TYPE);
50

51 52 53
	printf("\nLoading from %s device %d, partition %d: "
	       "Name: %.32s  Type: %.32s\n", intf, dev, part, info.name,
	       info.type);
54

55 56
	debug("First Block: " LBAFU ",  # of blocks: " LBAFU
	      ", Block Size: %ld\n",
57 58
	      info.start, info.size, info.blksz);

59
	if (blk_dread(dev_desc, info.start, 1, (ulong *)addr) != 1) {
60 61 62 63 64 65 66
		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)) {
67
#if defined(CONFIG_IMAGE_FORMAT_LEGACY)
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
	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;
84
#endif
85
#if CONFIG_IS_ENABLED(FIT)
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
	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;

103 104
	if (blk_dread(dev_desc, info.start + 1, cnt,
		      (ulong *)(addr + info.blksz)) != cnt) {
105 106 107 108 109 110
		printf("** Read error on %d:%d\n", dev, part);
		bootstage_error(BOOTSTAGE_ID_IDE_READ);
		return 1;
	}
	bootstage_mark(BOOTSTAGE_ID_IDE_READ);

111
#if CONFIG_IS_ENABLED(FIT)
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
	/* 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]);
}
132
#endif