blob: 3e457f672e12b63f3e18a7080269ca5a13978946 [file] [log] [blame]
Rob Herring7405a132012-09-21 04:02:30 +00001/*
2 * (C) Copyright 2000-2011
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
Rob Herring7405a132012-09-21 04:02:30 +00006 */
7#include <common.h>
8#include <command.h>
Rob Herring475c7972012-08-23 11:31:48 +00009#include <part.h>
Rob Herring7405a132012-09-21 04:02:30 +000010
Rob Herring475c7972012-08-23 11:31:48 +000011#if defined(CONFIG_CMD_IDE) || defined(CONFIG_CMD_SCSI) || \
12 defined(CONFIG_USB_STORAGE)
Rob Herring7405a132012-09-21 04:02:30 +000013int common_diskboot(cmd_tbl_t *cmdtp, const char *intf, int argc,
14 char *const argv[])
15{
Rob Herring475c7972012-08-23 11:31:48 +000016 int dev, part;
17 ulong addr = CONFIG_SYS_LOAD_ADDR;
18 ulong cnt;
Rob Herring7405a132012-09-21 04:02:30 +000019 disk_partition_t info;
20 image_header_t *hdr;
21 block_dev_desc_t *dev_desc;
22
23#if defined(CONFIG_FIT)
24 const void *fit_hdr = NULL;
25#endif
26
27 bootstage_mark(BOOTSTAGE_ID_IDE_START);
Rob Herring475c7972012-08-23 11:31:48 +000028 if (argc > 3) {
Rob Herring7405a132012-09-21 04:02:30 +000029 bootstage_error(BOOTSTAGE_ID_IDE_ADDR);
30 return CMD_RET_USAGE;
31 }
32 bootstage_mark(BOOTSTAGE_ID_IDE_ADDR);
33
Rob Herring475c7972012-08-23 11:31:48 +000034 if (argc > 1)
35 addr = simple_strtoul(argv[1], NULL, 16);
36
Rob Herring7405a132012-09-21 04:02:30 +000037 bootstage_mark(BOOTSTAGE_ID_IDE_BOOT_DEVICE);
38
Rob Herring475c7972012-08-23 11:31:48 +000039 part = get_device_and_partition(intf, (argc == 3) ? argv[2] : NULL,
Stephen Warren10a37fd2012-09-21 09:50:57 +000040 &dev_desc, &info, 1);
Rob Herring475c7972012-08-23 11:31:48 +000041 if (part < 0) {
Rob Herring7405a132012-09-21 04:02:30 +000042 bootstage_error(BOOTSTAGE_ID_IDE_TYPE);
43 return 1;
44 }
Rob Herring475c7972012-08-23 11:31:48 +000045
46 dev = dev_desc->dev;
Rob Herring7405a132012-09-21 04:02:30 +000047 bootstage_mark(BOOTSTAGE_ID_IDE_TYPE);
48
Rob Herring475c7972012-08-23 11:31:48 +000049 printf("\nLoading from %s device %d, partition %d: "
50 "Name: %.32s Type: %.32s\n", intf, dev, part, info.name,
51 info.type);
Rob Herring7405a132012-09-21 04:02:30 +000052
Frederic Leroy04735e92013-06-26 18:11:25 +020053 debug("First Block: " LBAFU ", # of blocks: " LBAFU
54 ", Block Size: %ld\n",
Rob Herring7405a132012-09-21 04:02:30 +000055 info.start, info.size, info.blksz);
56
57 if (dev_desc->block_read(dev, info.start, 1, (ulong *) addr) != 1) {
58 printf("** Read error on %d:%d\n", dev, part);
59 bootstage_error(BOOTSTAGE_ID_IDE_PART_READ);
60 return 1;
61 }
62 bootstage_mark(BOOTSTAGE_ID_IDE_PART_READ);
63
64 switch (genimg_get_format((void *) addr)) {
65 case IMAGE_FORMAT_LEGACY:
66 hdr = (image_header_t *) addr;
67
68 bootstage_mark(BOOTSTAGE_ID_IDE_FORMAT);
69
70 if (!image_check_hcrc(hdr)) {
71 puts("\n** Bad Header Checksum **\n");
72 bootstage_error(BOOTSTAGE_ID_IDE_CHECKSUM);
73 return 1;
74 }
75 bootstage_mark(BOOTSTAGE_ID_IDE_CHECKSUM);
76
77 image_print_contents(hdr);
78
79 cnt = image_get_image_size(hdr);
80 break;
81#if defined(CONFIG_FIT)
82 case IMAGE_FORMAT_FIT:
83 fit_hdr = (const void *) addr;
84 puts("Fit image detected...\n");
85
86 cnt = fit_get_size(fit_hdr);
87 break;
88#endif
89 default:
90 bootstage_error(BOOTSTAGE_ID_IDE_FORMAT);
91 puts("** Unknown image type\n");
92 return 1;
93 }
94
95 cnt += info.blksz - 1;
96 cnt /= info.blksz;
97 cnt -= 1;
98
99 if (dev_desc->block_read(dev, info.start + 1, cnt,
100 (ulong *)(addr + info.blksz)) != cnt) {
101 printf("** Read error on %d:%d\n", dev, part);
102 bootstage_error(BOOTSTAGE_ID_IDE_READ);
103 return 1;
104 }
105 bootstage_mark(BOOTSTAGE_ID_IDE_READ);
106
107#if defined(CONFIG_FIT)
108 /* This cannot be done earlier,
109 * we need complete FIT image in RAM first */
110 if (genimg_get_format((void *) addr) == IMAGE_FORMAT_FIT) {
111 if (!fit_check_format(fit_hdr)) {
112 bootstage_error(BOOTSTAGE_ID_IDE_FIT_READ);
113 puts("** Bad FIT image format\n");
114 return 1;
115 }
116 bootstage_mark(BOOTSTAGE_ID_IDE_FIT_READ_OK);
117 fit_print_contents(fit_hdr);
118 }
119#endif
120
121 flush_cache(addr, (cnt+1)*info.blksz);
122
123 /* Loading ok, update default load address */
124 load_addr = addr;
125
126 return bootm_maybe_autostart(cmdtp, argv[0]);
127}
Rob Herring475c7972012-08-23 11:31:48 +0000128#endif