blob: 2fd1717e6a44680107f2c242ab29855a9baaad41 [file] [log] [blame]
Rob Herringa20cbf92012-09-21 04:02:30 +00001/*
2 * (C) Copyright 2000-2011
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
Rob Herringa20cbf92012-09-21 04:02:30 +00006 */
7#include <common.h>
8#include <command.h>
Rob Herringede84362012-08-23 11:31:48 +00009#include <part.h>
Rob Herringa20cbf92012-09-21 04:02:30 +000010
Rob Herringede84362012-08-23 11:31:48 +000011#if defined(CONFIG_CMD_IDE) || defined(CONFIG_CMD_SCSI) || \
12 defined(CONFIG_USB_STORAGE)
Rob Herringa20cbf92012-09-21 04:02:30 +000013int common_diskboot(cmd_tbl_t *cmdtp, const char *intf, int argc,
14 char *const argv[])
15{
Rob Herringede84362012-08-23 11:31:48 +000016 int dev, part;
17 ulong addr = CONFIG_SYS_LOAD_ADDR;
18 ulong cnt;
Rob Herringa20cbf92012-09-21 04:02:30 +000019 disk_partition_t info;
Heiko Schocher515eb122014-05-28 11:33:33 +020020#if defined(CONFIG_IMAGE_FORMAT_LEGACY)
Rob Herringa20cbf92012-09-21 04:02:30 +000021 image_header_t *hdr;
Heiko Schocher515eb122014-05-28 11:33:33 +020022#endif
Simon Glasse3394752016-02-29 15:25:34 -070023 struct blk_desc *dev_desc;
Rob Herringa20cbf92012-09-21 04:02:30 +000024
Simon Glasse3ee2fb2016-02-22 22:55:43 -070025#if CONFIG_IS_ENABLED(FIT)
Rob Herringa20cbf92012-09-21 04:02:30 +000026 const void *fit_hdr = NULL;
27#endif
28
29 bootstage_mark(BOOTSTAGE_ID_IDE_START);
Rob Herringede84362012-08-23 11:31:48 +000030 if (argc > 3) {
Rob Herringa20cbf92012-09-21 04:02:30 +000031 bootstage_error(BOOTSTAGE_ID_IDE_ADDR);
32 return CMD_RET_USAGE;
33 }
34 bootstage_mark(BOOTSTAGE_ID_IDE_ADDR);
35
Rob Herringede84362012-08-23 11:31:48 +000036 if (argc > 1)
37 addr = simple_strtoul(argv[1], NULL, 16);
Rob Herringa20cbf92012-09-21 04:02:30 +000038
Rob Herringede84362012-08-23 11:31:48 +000039 bootstage_mark(BOOTSTAGE_ID_IDE_BOOT_DEVICE);
Rob Herringa20cbf92012-09-21 04:02:30 +000040
Simon Glasse76ee972016-02-29 15:25:44 -070041 part = blk_get_device_part_str(intf, (argc == 3) ? argv[2] : NULL,
Stephen Warrenc87ff222012-09-21 09:50:57 +000042 &dev_desc, &info, 1);
Rob Herringede84362012-08-23 11:31:48 +000043 if (part < 0) {
Rob Herringa20cbf92012-09-21 04:02:30 +000044 bootstage_error(BOOTSTAGE_ID_IDE_TYPE);
45 return 1;
46 }
Rob Herringa20cbf92012-09-21 04:02:30 +000047
Simon Glass2f26fff2016-02-29 15:25:51 -070048 dev = dev_desc->devnum;
Rob Herringede84362012-08-23 11:31:48 +000049 bootstage_mark(BOOTSTAGE_ID_IDE_TYPE);
Rob Herringa20cbf92012-09-21 04:02:30 +000050
Rob Herringede84362012-08-23 11:31:48 +000051 printf("\nLoading from %s device %d, partition %d: "
52 "Name: %.32s Type: %.32s\n", intf, dev, part, info.name,
53 info.type);
Rob Herringa20cbf92012-09-21 04:02:30 +000054
Frederic Leroye7ee0282013-06-26 18:11:25 +020055 debug("First Block: " LBAFU ", # of blocks: " LBAFU
56 ", Block Size: %ld\n",
Rob Herringa20cbf92012-09-21 04:02:30 +000057 info.start, info.size, info.blksz);
58
Simon Glass2ee8ada2016-02-29 15:25:52 -070059 if (blk_dread(dev_desc, info.start, 1, (ulong *)addr) != 1) {
Rob Herringa20cbf92012-09-21 04:02:30 +000060 printf("** Read error on %d:%d\n", dev, part);
61 bootstage_error(BOOTSTAGE_ID_IDE_PART_READ);
62 return 1;
63 }
64 bootstage_mark(BOOTSTAGE_ID_IDE_PART_READ);
65
66 switch (genimg_get_format((void *) addr)) {
Heiko Schocher515eb122014-05-28 11:33:33 +020067#if defined(CONFIG_IMAGE_FORMAT_LEGACY)
Rob Herringa20cbf92012-09-21 04:02:30 +000068 case IMAGE_FORMAT_LEGACY:
69 hdr = (image_header_t *) addr;
70
71 bootstage_mark(BOOTSTAGE_ID_IDE_FORMAT);
72
73 if (!image_check_hcrc(hdr)) {
74 puts("\n** Bad Header Checksum **\n");
75 bootstage_error(BOOTSTAGE_ID_IDE_CHECKSUM);
76 return 1;
77 }
78 bootstage_mark(BOOTSTAGE_ID_IDE_CHECKSUM);
79
80 image_print_contents(hdr);
81
82 cnt = image_get_image_size(hdr);
83 break;
Heiko Schocher515eb122014-05-28 11:33:33 +020084#endif
Simon Glasse3ee2fb2016-02-22 22:55:43 -070085#if CONFIG_IS_ENABLED(FIT)
Rob Herringa20cbf92012-09-21 04:02:30 +000086 case IMAGE_FORMAT_FIT:
87 fit_hdr = (const void *) addr;
88 puts("Fit image detected...\n");
89
90 cnt = fit_get_size(fit_hdr);
91 break;
92#endif
93 default:
94 bootstage_error(BOOTSTAGE_ID_IDE_FORMAT);
95 puts("** Unknown image type\n");
96 return 1;
97 }
98
99 cnt += info.blksz - 1;
100 cnt /= info.blksz;
101 cnt -= 1;
102
Simon Glass2ee8ada2016-02-29 15:25:52 -0700103 if (blk_dread(dev_desc, info.start + 1, cnt,
104 (ulong *)(addr + info.blksz)) != cnt) {
Rob Herringa20cbf92012-09-21 04:02:30 +0000105 printf("** Read error on %d:%d\n", dev, part);
106 bootstage_error(BOOTSTAGE_ID_IDE_READ);
107 return 1;
108 }
109 bootstage_mark(BOOTSTAGE_ID_IDE_READ);
110
Simon Glasse3ee2fb2016-02-22 22:55:43 -0700111#if CONFIG_IS_ENABLED(FIT)
Rob Herringa20cbf92012-09-21 04:02:30 +0000112 /* This cannot be done earlier,
113 * we need complete FIT image in RAM first */
114 if (genimg_get_format((void *) addr) == IMAGE_FORMAT_FIT) {
115 if (!fit_check_format(fit_hdr)) {
116 bootstage_error(BOOTSTAGE_ID_IDE_FIT_READ);
117 puts("** Bad FIT image format\n");
118 return 1;
119 }
120 bootstage_mark(BOOTSTAGE_ID_IDE_FIT_READ_OK);
121 fit_print_contents(fit_hdr);
122 }
123#endif
124
125 flush_cache(addr, (cnt+1)*info.blksz);
126
127 /* Loading ok, update default load address */
128 load_addr = addr;
129
130 return bootm_maybe_autostart(cmdtp, argv[0]);
131}
Rob Herringede84362012-08-23 11:31:48 +0000132#endif