blob: 437c1753740c848a2d184febd7b0c3f62ac1439c [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Rob Herringa20cbf92012-09-21 04:02:30 +00002/*
3 * (C) Copyright 2000-2011
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
Rob Herringa20cbf92012-09-21 04:02:30 +00005 */
6#include <common.h>
7#include <command.h>
Simon Glass63334482019-11-14 12:57:39 -07008#include <cpu_func.h>
Rob Herringede84362012-08-23 11:31:48 +00009#include <part.h>
Rob Herringa20cbf92012-09-21 04:02:30 +000010
11int common_diskboot(cmd_tbl_t *cmdtp, const char *intf, int argc,
12 char *const argv[])
13{
Marek Vasut74fcb4f2016-05-01 00:35:54 +020014 __maybe_unused int dev;
15 int part;
Rob Herringede84362012-08-23 11:31:48 +000016 ulong addr = CONFIG_SYS_LOAD_ADDR;
17 ulong cnt;
Rob Herringa20cbf92012-09-21 04:02:30 +000018 disk_partition_t info;
Tom Rinic220bd92019-05-23 07:14:07 -040019#if defined(CONFIG_LEGACY_IMAGE_FORMAT)
Rob Herringa20cbf92012-09-21 04:02:30 +000020 image_header_t *hdr;
Heiko Schocher515eb122014-05-28 11:33:33 +020021#endif
Simon Glasse3394752016-02-29 15:25:34 -070022 struct blk_desc *dev_desc;
Rob Herringa20cbf92012-09-21 04:02:30 +000023
Simon Glasse3ee2fb2016-02-22 22:55:43 -070024#if CONFIG_IS_ENABLED(FIT)
Rob Herringa20cbf92012-09-21 04:02:30 +000025 const void *fit_hdr = NULL;
26#endif
27
28 bootstage_mark(BOOTSTAGE_ID_IDE_START);
Rob Herringede84362012-08-23 11:31:48 +000029 if (argc > 3) {
Rob Herringa20cbf92012-09-21 04:02:30 +000030 bootstage_error(BOOTSTAGE_ID_IDE_ADDR);
31 return CMD_RET_USAGE;
32 }
33 bootstage_mark(BOOTSTAGE_ID_IDE_ADDR);
34
Rob Herringede84362012-08-23 11:31:48 +000035 if (argc > 1)
36 addr = simple_strtoul(argv[1], NULL, 16);
Rob Herringa20cbf92012-09-21 04:02:30 +000037
Rob Herringede84362012-08-23 11:31:48 +000038 bootstage_mark(BOOTSTAGE_ID_IDE_BOOT_DEVICE);
Rob Herringa20cbf92012-09-21 04:02:30 +000039
Simon Glasse76ee972016-02-29 15:25:44 -070040 part = blk_get_device_part_str(intf, (argc == 3) ? argv[2] : NULL,
Stephen Warrenc87ff222012-09-21 09:50:57 +000041 &dev_desc, &info, 1);
Rob Herringede84362012-08-23 11:31:48 +000042 if (part < 0) {
Rob Herringa20cbf92012-09-21 04:02:30 +000043 bootstage_error(BOOTSTAGE_ID_IDE_TYPE);
44 return 1;
45 }
Rob Herringa20cbf92012-09-21 04:02:30 +000046
Simon Glass2f26fff2016-02-29 15:25:51 -070047 dev = dev_desc->devnum;
Rob Herringede84362012-08-23 11:31:48 +000048 bootstage_mark(BOOTSTAGE_ID_IDE_TYPE);
Rob Herringa20cbf92012-09-21 04:02:30 +000049
Rob Herringede84362012-08-23 11:31:48 +000050 printf("\nLoading from %s device %d, partition %d: "
51 "Name: %.32s Type: %.32s\n", intf, dev, part, info.name,
52 info.type);
Rob Herringa20cbf92012-09-21 04:02:30 +000053
Frederic Leroye7ee0282013-06-26 18:11:25 +020054 debug("First Block: " LBAFU ", # of blocks: " LBAFU
55 ", Block Size: %ld\n",
Rob Herringa20cbf92012-09-21 04:02:30 +000056 info.start, info.size, info.blksz);
57
Simon Glass2ee8ada2016-02-29 15:25:52 -070058 if (blk_dread(dev_desc, info.start, 1, (ulong *)addr) != 1) {
Rob Herringa20cbf92012-09-21 04:02:30 +000059 printf("** Read error on %d:%d\n", dev, part);
60 bootstage_error(BOOTSTAGE_ID_IDE_PART_READ);
61 return 1;
62 }
63 bootstage_mark(BOOTSTAGE_ID_IDE_PART_READ);
64
65 switch (genimg_get_format((void *) addr)) {
Tom Rinic220bd92019-05-23 07:14:07 -040066#if defined(CONFIG_LEGACY_IMAGE_FORMAT)
Rob Herringa20cbf92012-09-21 04:02:30 +000067 case IMAGE_FORMAT_LEGACY:
68 hdr = (image_header_t *) addr;
69
70 bootstage_mark(BOOTSTAGE_ID_IDE_FORMAT);
71
72 if (!image_check_hcrc(hdr)) {
73 puts("\n** Bad Header Checksum **\n");
74 bootstage_error(BOOTSTAGE_ID_IDE_CHECKSUM);
75 return 1;
76 }
77 bootstage_mark(BOOTSTAGE_ID_IDE_CHECKSUM);
78
79 image_print_contents(hdr);
80
81 cnt = image_get_image_size(hdr);
82 break;
Heiko Schocher515eb122014-05-28 11:33:33 +020083#endif
Simon Glasse3ee2fb2016-02-22 22:55:43 -070084#if CONFIG_IS_ENABLED(FIT)
Rob Herringa20cbf92012-09-21 04:02:30 +000085 case IMAGE_FORMAT_FIT:
86 fit_hdr = (const void *) addr;
87 puts("Fit image detected...\n");
88
89 cnt = fit_get_size(fit_hdr);
90 break;
91#endif
92 default:
93 bootstage_error(BOOTSTAGE_ID_IDE_FORMAT);
94 puts("** Unknown image type\n");
95 return 1;
96 }
97
98 cnt += info.blksz - 1;
99 cnt /= info.blksz;
100 cnt -= 1;
101
Simon Glass2ee8ada2016-02-29 15:25:52 -0700102 if (blk_dread(dev_desc, info.start + 1, cnt,
103 (ulong *)(addr + info.blksz)) != cnt) {
Rob Herringa20cbf92012-09-21 04:02:30 +0000104 printf("** Read error on %d:%d\n", dev, part);
105 bootstage_error(BOOTSTAGE_ID_IDE_READ);
106 return 1;
107 }
108 bootstage_mark(BOOTSTAGE_ID_IDE_READ);
109
Simon Glasse3ee2fb2016-02-22 22:55:43 -0700110#if CONFIG_IS_ENABLED(FIT)
Rob Herringa20cbf92012-09-21 04:02:30 +0000111 /* This cannot be done earlier,
112 * we need complete FIT image in RAM first */
113 if (genimg_get_format((void *) addr) == IMAGE_FORMAT_FIT) {
114 if (!fit_check_format(fit_hdr)) {
115 bootstage_error(BOOTSTAGE_ID_IDE_FIT_READ);
116 puts("** Bad FIT image format\n");
117 return 1;
118 }
119 bootstage_mark(BOOTSTAGE_ID_IDE_FIT_READ_OK);
120 fit_print_contents(fit_hdr);
121 }
122#endif
123
124 flush_cache(addr, (cnt+1)*info.blksz);
125
126 /* Loading ok, update default load address */
127 load_addr = addr;
128
129 return bootm_maybe_autostart(cmdtp, argv[0]);
130}