blob: 92eaa02f4a1312d2ea83cb39211939b00d27c435 [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>
Simon Glass1ea97892020-05-10 11:40:00 -06007#include <bootstage.h>
Rob Herringa20cbf92012-09-21 04:02:30 +00008#include <command.h>
Simon Glass63334482019-11-14 12:57:39 -07009#include <cpu_func.h>
Simon Glass85f13782019-12-28 10:45:03 -070010#include <image.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Rob Herringede84362012-08-23 11:31:48 +000012#include <part.h>
Rob Herringa20cbf92012-09-21 04:02:30 +000013
Simon Glassed38aef2020-05-10 11:40:03 -060014int common_diskboot(struct cmd_tbl *cmdtp, const char *intf, int argc,
Rob Herringa20cbf92012-09-21 04:02:30 +000015 char *const argv[])
16{
Marek Vasut74fcb4f2016-05-01 00:35:54 +020017 __maybe_unused int dev;
18 int part;
Rob Herringede84362012-08-23 11:31:48 +000019 ulong addr = CONFIG_SYS_LOAD_ADDR;
20 ulong cnt;
Simon Glassc1c4a8f2020-05-10 11:39:57 -060021 struct disk_partition info;
Tom Rinic220bd92019-05-23 07:14:07 -040022#if defined(CONFIG_LEGACY_IMAGE_FORMAT)
Simon Glassbb7d3bb2022-09-06 20:26:52 -060023 struct legacy_img_hdr *hdr;
Heiko Schocher515eb122014-05-28 11:33:33 +020024#endif
Simon Glasse3394752016-02-29 15:25:34 -070025 struct blk_desc *dev_desc;
Rob Herringa20cbf92012-09-21 04:02:30 +000026
Simon Glasse3ee2fb2016-02-22 22:55:43 -070027#if CONFIG_IS_ENABLED(FIT)
Rob Herringa20cbf92012-09-21 04:02:30 +000028 const void *fit_hdr = NULL;
29#endif
30
31 bootstage_mark(BOOTSTAGE_ID_IDE_START);
Rob Herringede84362012-08-23 11:31:48 +000032 if (argc > 3) {
Rob Herringa20cbf92012-09-21 04:02:30 +000033 bootstage_error(BOOTSTAGE_ID_IDE_ADDR);
34 return CMD_RET_USAGE;
35 }
36 bootstage_mark(BOOTSTAGE_ID_IDE_ADDR);
37
Rob Herringede84362012-08-23 11:31:48 +000038 if (argc > 1)
Simon Glass3ff49ec2021-07-24 09:03:29 -060039 addr = hextoul(argv[1], NULL);
Rob Herringa20cbf92012-09-21 04:02:30 +000040
Rob Herringede84362012-08-23 11:31:48 +000041 bootstage_mark(BOOTSTAGE_ID_IDE_BOOT_DEVICE);
Rob Herringa20cbf92012-09-21 04:02:30 +000042
Simon Glass793a98e2023-11-18 14:05:20 -070043 part = blk_get_device_part_str(intf, cmd_arg2(argc, argv),
44 &dev_desc, &info, 1);
Rob Herringede84362012-08-23 11:31:48 +000045 if (part < 0) {
Rob Herringa20cbf92012-09-21 04:02:30 +000046 bootstage_error(BOOTSTAGE_ID_IDE_TYPE);
47 return 1;
48 }
Rob Herringa20cbf92012-09-21 04:02:30 +000049
Simon Glass2f26fff2016-02-29 15:25:51 -070050 dev = dev_desc->devnum;
Rob Herringede84362012-08-23 11:31:48 +000051 bootstage_mark(BOOTSTAGE_ID_IDE_TYPE);
Rob Herringa20cbf92012-09-21 04:02:30 +000052
Rob Herringede84362012-08-23 11:31:48 +000053 printf("\nLoading from %s device %d, partition %d: "
54 "Name: %.32s Type: %.32s\n", intf, dev, part, info.name,
55 info.type);
Rob Herringa20cbf92012-09-21 04:02:30 +000056
Frederic Leroye7ee0282013-06-26 18:11:25 +020057 debug("First Block: " LBAFU ", # of blocks: " LBAFU
58 ", Block Size: %ld\n",
Rob Herringa20cbf92012-09-21 04:02:30 +000059 info.start, info.size, info.blksz);
60
Simon Glass2ee8ada2016-02-29 15:25:52 -070061 if (blk_dread(dev_desc, info.start, 1, (ulong *)addr) != 1) {
Rob Herringa20cbf92012-09-21 04:02:30 +000062 printf("** Read error on %d:%d\n", dev, part);
63 bootstage_error(BOOTSTAGE_ID_IDE_PART_READ);
64 return 1;
65 }
66 bootstage_mark(BOOTSTAGE_ID_IDE_PART_READ);
67
68 switch (genimg_get_format((void *) addr)) {
Tom Rinic220bd92019-05-23 07:14:07 -040069#if defined(CONFIG_LEGACY_IMAGE_FORMAT)
Rob Herringa20cbf92012-09-21 04:02:30 +000070 case IMAGE_FORMAT_LEGACY:
Simon Glassbb7d3bb2022-09-06 20:26:52 -060071 hdr = (struct legacy_img_hdr *)addr;
Rob Herringa20cbf92012-09-21 04:02:30 +000072
73 bootstage_mark(BOOTSTAGE_ID_IDE_FORMAT);
74
75 if (!image_check_hcrc(hdr)) {
76 puts("\n** Bad Header Checksum **\n");
77 bootstage_error(BOOTSTAGE_ID_IDE_CHECKSUM);
78 return 1;
79 }
80 bootstage_mark(BOOTSTAGE_ID_IDE_CHECKSUM);
81
82 image_print_contents(hdr);
83
84 cnt = image_get_image_size(hdr);
85 break;
Heiko Schocher515eb122014-05-28 11:33:33 +020086#endif
Simon Glasse3ee2fb2016-02-22 22:55:43 -070087#if CONFIG_IS_ENABLED(FIT)
Rob Herringa20cbf92012-09-21 04:02:30 +000088 case IMAGE_FORMAT_FIT:
89 fit_hdr = (const void *) addr;
90 puts("Fit image detected...\n");
91
92 cnt = fit_get_size(fit_hdr);
93 break;
94#endif
95 default:
96 bootstage_error(BOOTSTAGE_ID_IDE_FORMAT);
97 puts("** Unknown image type\n");
98 return 1;
99 }
100
101 cnt += info.blksz - 1;
102 cnt /= info.blksz;
103 cnt -= 1;
104
Simon Glass2ee8ada2016-02-29 15:25:52 -0700105 if (blk_dread(dev_desc, info.start + 1, cnt,
106 (ulong *)(addr + info.blksz)) != cnt) {
Rob Herringa20cbf92012-09-21 04:02:30 +0000107 printf("** Read error on %d:%d\n", dev, part);
108 bootstage_error(BOOTSTAGE_ID_IDE_READ);
109 return 1;
110 }
111 bootstage_mark(BOOTSTAGE_ID_IDE_READ);
112
Simon Glasse3ee2fb2016-02-22 22:55:43 -0700113#if CONFIG_IS_ENABLED(FIT)
Rob Herringa20cbf92012-09-21 04:02:30 +0000114 /* This cannot be done earlier,
115 * we need complete FIT image in RAM first */
116 if (genimg_get_format((void *) addr) == IMAGE_FORMAT_FIT) {
Simon Glassd563c252021-02-15 17:08:09 -0700117 if (fit_check_format(fit_hdr, IMAGE_SIZE_INVAL)) {
Rob Herringa20cbf92012-09-21 04:02:30 +0000118 bootstage_error(BOOTSTAGE_ID_IDE_FIT_READ);
119 puts("** Bad FIT image format\n");
120 return 1;
121 }
122 bootstage_mark(BOOTSTAGE_ID_IDE_FIT_READ_OK);
Rob Herringa20cbf92012-09-21 04:02:30 +0000123 }
124#endif
125
126 flush_cache(addr, (cnt+1)*info.blksz);
127
128 /* Loading ok, update default load address */
Simon Glass892265d2019-12-28 10:45:02 -0700129 image_load_addr = addr;
Rob Herringa20cbf92012-09-21 04:02:30 +0000130
131 return bootm_maybe_autostart(cmdtp, argv[0]);
132}