blob: 92de3af8a5c0d6f5274f6923025c7559ec41f3b4 [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
Simon Glassc649e3c2016-05-01 11:36:02 -060011#if defined(CONFIG_CMD_IDE) || defined(CONFIG_SCSI) || \
Rob Herring475c7972012-08-23 11:31:48 +000012 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{
Marek Vasut04681cb2016-05-01 00:35:54 +020016 __maybe_unused int dev;
17 int part;
Rob Herring475c7972012-08-23 11:31:48 +000018 ulong addr = CONFIG_SYS_LOAD_ADDR;
19 ulong cnt;
Rob Herring7405a132012-09-21 04:02:30 +000020 disk_partition_t info;
Heiko Schocher21d29f72014-05-28 11:33:33 +020021#if defined(CONFIG_IMAGE_FORMAT_LEGACY)
Rob Herring7405a132012-09-21 04:02:30 +000022 image_header_t *hdr;
Heiko Schocher21d29f72014-05-28 11:33:33 +020023#endif
Simon Glass4101f682016-02-29 15:25:34 -070024 struct blk_desc *dev_desc;
Rob Herring7405a132012-09-21 04:02:30 +000025
Simon Glass73223f02016-02-22 22:55:43 -070026#if CONFIG_IS_ENABLED(FIT)
Rob Herring7405a132012-09-21 04:02:30 +000027 const void *fit_hdr = NULL;
28#endif
29
30 bootstage_mark(BOOTSTAGE_ID_IDE_START);
Rob Herring475c7972012-08-23 11:31:48 +000031 if (argc > 3) {
Rob Herring7405a132012-09-21 04:02:30 +000032 bootstage_error(BOOTSTAGE_ID_IDE_ADDR);
33 return CMD_RET_USAGE;
34 }
35 bootstage_mark(BOOTSTAGE_ID_IDE_ADDR);
36
Rob Herring475c7972012-08-23 11:31:48 +000037 if (argc > 1)
38 addr = simple_strtoul(argv[1], NULL, 16);
39
Rob Herring7405a132012-09-21 04:02:30 +000040 bootstage_mark(BOOTSTAGE_ID_IDE_BOOT_DEVICE);
41
Simon Glasse35929e2016-02-29 15:25:44 -070042 part = blk_get_device_part_str(intf, (argc == 3) ? argv[2] : NULL,
Stephen Warren10a37fd2012-09-21 09:50:57 +000043 &dev_desc, &info, 1);
Rob Herring475c7972012-08-23 11:31:48 +000044 if (part < 0) {
Rob Herring7405a132012-09-21 04:02:30 +000045 bootstage_error(BOOTSTAGE_ID_IDE_TYPE);
46 return 1;
47 }
Rob Herring475c7972012-08-23 11:31:48 +000048
Simon Glassbcce53d2016-02-29 15:25:51 -070049 dev = dev_desc->devnum;
Rob Herring7405a132012-09-21 04:02:30 +000050 bootstage_mark(BOOTSTAGE_ID_IDE_TYPE);
51
Rob Herring475c7972012-08-23 11:31:48 +000052 printf("\nLoading from %s device %d, partition %d: "
53 "Name: %.32s Type: %.32s\n", intf, dev, part, info.name,
54 info.type);
Rob Herring7405a132012-09-21 04:02:30 +000055
Frederic Leroy04735e92013-06-26 18:11:25 +020056 debug("First Block: " LBAFU ", # of blocks: " LBAFU
57 ", Block Size: %ld\n",
Rob Herring7405a132012-09-21 04:02:30 +000058 info.start, info.size, info.blksz);
59
Simon Glass2a981dc2016-02-29 15:25:52 -070060 if (blk_dread(dev_desc, info.start, 1, (ulong *)addr) != 1) {
Rob Herring7405a132012-09-21 04:02:30 +000061 printf("** Read error on %d:%d\n", dev, part);
62 bootstage_error(BOOTSTAGE_ID_IDE_PART_READ);
63 return 1;
64 }
65 bootstage_mark(BOOTSTAGE_ID_IDE_PART_READ);
66
67 switch (genimg_get_format((void *) addr)) {
Heiko Schocher21d29f72014-05-28 11:33:33 +020068#if defined(CONFIG_IMAGE_FORMAT_LEGACY)
Rob Herring7405a132012-09-21 04:02:30 +000069 case IMAGE_FORMAT_LEGACY:
70 hdr = (image_header_t *) addr;
71
72 bootstage_mark(BOOTSTAGE_ID_IDE_FORMAT);
73
74 if (!image_check_hcrc(hdr)) {
75 puts("\n** Bad Header Checksum **\n");
76 bootstage_error(BOOTSTAGE_ID_IDE_CHECKSUM);
77 return 1;
78 }
79 bootstage_mark(BOOTSTAGE_ID_IDE_CHECKSUM);
80
81 image_print_contents(hdr);
82
83 cnt = image_get_image_size(hdr);
84 break;
Heiko Schocher21d29f72014-05-28 11:33:33 +020085#endif
Simon Glass73223f02016-02-22 22:55:43 -070086#if CONFIG_IS_ENABLED(FIT)
Rob Herring7405a132012-09-21 04:02:30 +000087 case IMAGE_FORMAT_FIT:
88 fit_hdr = (const void *) addr;
89 puts("Fit image detected...\n");
90
91 cnt = fit_get_size(fit_hdr);
92 break;
93#endif
94 default:
95 bootstage_error(BOOTSTAGE_ID_IDE_FORMAT);
96 puts("** Unknown image type\n");
97 return 1;
98 }
99
100 cnt += info.blksz - 1;
101 cnt /= info.blksz;
102 cnt -= 1;
103
Simon Glass2a981dc2016-02-29 15:25:52 -0700104 if (blk_dread(dev_desc, info.start + 1, cnt,
105 (ulong *)(addr + info.blksz)) != cnt) {
Rob Herring7405a132012-09-21 04:02:30 +0000106 printf("** Read error on %d:%d\n", dev, part);
107 bootstage_error(BOOTSTAGE_ID_IDE_READ);
108 return 1;
109 }
110 bootstage_mark(BOOTSTAGE_ID_IDE_READ);
111
Simon Glass73223f02016-02-22 22:55:43 -0700112#if CONFIG_IS_ENABLED(FIT)
Rob Herring7405a132012-09-21 04:02:30 +0000113 /* This cannot be done earlier,
114 * we need complete FIT image in RAM first */
115 if (genimg_get_format((void *) addr) == IMAGE_FORMAT_FIT) {
116 if (!fit_check_format(fit_hdr)) {
117 bootstage_error(BOOTSTAGE_ID_IDE_FIT_READ);
118 puts("** Bad FIT image format\n");
119 return 1;
120 }
121 bootstage_mark(BOOTSTAGE_ID_IDE_FIT_READ_OK);
122 fit_print_contents(fit_hdr);
123 }
124#endif
125
126 flush_cache(addr, (cnt+1)*info.blksz);
127
128 /* Loading ok, update default load address */
129 load_addr = addr;
130
131 return bootm_maybe_autostart(cmdtp, argv[0]);
132}
Rob Herring475c7972012-08-23 11:31:48 +0000133#endif