Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Rob Herring | 7405a13 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2000-2011 |
| 4 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
Rob Herring | 7405a13 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 5 | */ |
| 6 | #include <common.h> |
Simon Glass | 52f2423 | 2020-05-10 11:40:00 -0600 | [diff] [blame] | 7 | #include <bootstage.h> |
Rob Herring | 7405a13 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 8 | #include <command.h> |
Simon Glass | 1eb69ae | 2019-11-14 12:57:39 -0700 | [diff] [blame] | 9 | #include <cpu_func.h> |
Simon Glass | 8e8ccfe | 2019-12-28 10:45:03 -0700 | [diff] [blame] | 10 | #include <image.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 11 | #include <log.h> |
Rob Herring | 475c797 | 2012-08-23 11:31:48 +0000 | [diff] [blame] | 12 | #include <part.h> |
Rob Herring | 7405a13 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 13 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 14 | int common_diskboot(struct cmd_tbl *cmdtp, const char *intf, int argc, |
Rob Herring | 7405a13 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 15 | char *const argv[]) |
| 16 | { |
Marek Vasut | 04681cb | 2016-05-01 00:35:54 +0200 | [diff] [blame] | 17 | __maybe_unused int dev; |
| 18 | int part; |
Rob Herring | 475c797 | 2012-08-23 11:31:48 +0000 | [diff] [blame] | 19 | ulong addr = CONFIG_SYS_LOAD_ADDR; |
| 20 | ulong cnt; |
Simon Glass | 0528979 | 2020-05-10 11:39:57 -0600 | [diff] [blame] | 21 | struct disk_partition info; |
Tom Rini | c76c93a | 2019-05-23 07:14:07 -0400 | [diff] [blame] | 22 | #if defined(CONFIG_LEGACY_IMAGE_FORMAT) |
Rob Herring | 7405a13 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 23 | image_header_t *hdr; |
Heiko Schocher | 21d29f7 | 2014-05-28 11:33:33 +0200 | [diff] [blame] | 24 | #endif |
Simon Glass | 4101f68 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 25 | struct blk_desc *dev_desc; |
Rob Herring | 7405a13 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 26 | |
Simon Glass | 73223f0 | 2016-02-22 22:55:43 -0700 | [diff] [blame] | 27 | #if CONFIG_IS_ENABLED(FIT) |
Rob Herring | 7405a13 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 28 | const void *fit_hdr = NULL; |
| 29 | #endif |
| 30 | |
| 31 | bootstage_mark(BOOTSTAGE_ID_IDE_START); |
Rob Herring | 475c797 | 2012-08-23 11:31:48 +0000 | [diff] [blame] | 32 | if (argc > 3) { |
Rob Herring | 7405a13 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 33 | bootstage_error(BOOTSTAGE_ID_IDE_ADDR); |
| 34 | return CMD_RET_USAGE; |
| 35 | } |
| 36 | bootstage_mark(BOOTSTAGE_ID_IDE_ADDR); |
| 37 | |
Rob Herring | 475c797 | 2012-08-23 11:31:48 +0000 | [diff] [blame] | 38 | if (argc > 1) |
| 39 | addr = simple_strtoul(argv[1], NULL, 16); |
| 40 | |
Rob Herring | 7405a13 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 41 | bootstage_mark(BOOTSTAGE_ID_IDE_BOOT_DEVICE); |
| 42 | |
Simon Glass | e35929e | 2016-02-29 15:25:44 -0700 | [diff] [blame] | 43 | part = blk_get_device_part_str(intf, (argc == 3) ? argv[2] : NULL, |
Stephen Warren | 10a37fd | 2012-09-21 09:50:57 +0000 | [diff] [blame] | 44 | &dev_desc, &info, 1); |
Rob Herring | 475c797 | 2012-08-23 11:31:48 +0000 | [diff] [blame] | 45 | if (part < 0) { |
Rob Herring | 7405a13 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 46 | bootstage_error(BOOTSTAGE_ID_IDE_TYPE); |
| 47 | return 1; |
| 48 | } |
Rob Herring | 475c797 | 2012-08-23 11:31:48 +0000 | [diff] [blame] | 49 | |
Simon Glass | bcce53d | 2016-02-29 15:25:51 -0700 | [diff] [blame] | 50 | dev = dev_desc->devnum; |
Rob Herring | 7405a13 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 51 | bootstage_mark(BOOTSTAGE_ID_IDE_TYPE); |
| 52 | |
Rob Herring | 475c797 | 2012-08-23 11:31:48 +0000 | [diff] [blame] | 53 | printf("\nLoading from %s device %d, partition %d: " |
| 54 | "Name: %.32s Type: %.32s\n", intf, dev, part, info.name, |
| 55 | info.type); |
Rob Herring | 7405a13 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 56 | |
Frederic Leroy | 04735e9 | 2013-06-26 18:11:25 +0200 | [diff] [blame] | 57 | debug("First Block: " LBAFU ", # of blocks: " LBAFU |
| 58 | ", Block Size: %ld\n", |
Rob Herring | 7405a13 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 59 | info.start, info.size, info.blksz); |
| 60 | |
Simon Glass | 2a981dc | 2016-02-29 15:25:52 -0700 | [diff] [blame] | 61 | if (blk_dread(dev_desc, info.start, 1, (ulong *)addr) != 1) { |
Rob Herring | 7405a13 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 62 | 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 Rini | c76c93a | 2019-05-23 07:14:07 -0400 | [diff] [blame] | 69 | #if defined(CONFIG_LEGACY_IMAGE_FORMAT) |
Rob Herring | 7405a13 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 70 | case IMAGE_FORMAT_LEGACY: |
| 71 | hdr = (image_header_t *) addr; |
| 72 | |
| 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 Schocher | 21d29f7 | 2014-05-28 11:33:33 +0200 | [diff] [blame] | 86 | #endif |
Simon Glass | 73223f0 | 2016-02-22 22:55:43 -0700 | [diff] [blame] | 87 | #if CONFIG_IS_ENABLED(FIT) |
Rob Herring | 7405a13 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 88 | 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 Glass | 2a981dc | 2016-02-29 15:25:52 -0700 | [diff] [blame] | 105 | if (blk_dread(dev_desc, info.start + 1, cnt, |
| 106 | (ulong *)(addr + info.blksz)) != cnt) { |
Rob Herring | 7405a13 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 107 | 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 Glass | 73223f0 | 2016-02-22 22:55:43 -0700 | [diff] [blame] | 113 | #if CONFIG_IS_ENABLED(FIT) |
Rob Herring | 7405a13 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 114 | /* This cannot be done earlier, |
| 115 | * we need complete FIT image in RAM first */ |
| 116 | if (genimg_get_format((void *) addr) == IMAGE_FORMAT_FIT) { |
| 117 | if (!fit_check_format(fit_hdr)) { |
| 118 | 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); |
| 123 | fit_print_contents(fit_hdr); |
| 124 | } |
| 125 | #endif |
| 126 | |
| 127 | flush_cache(addr, (cnt+1)*info.blksz); |
| 128 | |
| 129 | /* Loading ok, update default load address */ |
Simon Glass | bb872dd | 2019-12-28 10:45:02 -0700 | [diff] [blame] | 130 | image_load_addr = addr; |
Rob Herring | 7405a13 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 131 | |
| 132 | return bootm_maybe_autostart(cmdtp, argv[0]); |
| 133 | } |