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> |
| 7 | #include <command.h> |
Simon Glass | 1eb69ae | 2019-11-14 12:57:39 -0700 | [diff] [blame] | 8 | #include <cpu_func.h> |
Simon Glass | 8e8ccfe | 2019-12-28 10:45:03 -0700 | [diff] [blame] | 9 | #include <image.h> |
Rob Herring | 475c797 | 2012-08-23 11:31:48 +0000 | [diff] [blame] | 10 | #include <part.h> |
Rob Herring | 7405a13 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 11 | |
| 12 | int common_diskboot(cmd_tbl_t *cmdtp, const char *intf, int argc, |
| 13 | char *const argv[]) |
| 14 | { |
Marek Vasut | 04681cb | 2016-05-01 00:35:54 +0200 | [diff] [blame] | 15 | __maybe_unused int dev; |
| 16 | int part; |
Rob Herring | 475c797 | 2012-08-23 11:31:48 +0000 | [diff] [blame] | 17 | ulong addr = CONFIG_SYS_LOAD_ADDR; |
| 18 | ulong cnt; |
Simon Glass | 0528979 | 2020-05-10 11:39:57 -0600 | [diff] [blame^] | 19 | struct disk_partition info; |
Tom Rini | c76c93a | 2019-05-23 07:14:07 -0400 | [diff] [blame] | 20 | #if defined(CONFIG_LEGACY_IMAGE_FORMAT) |
Rob Herring | 7405a13 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 21 | image_header_t *hdr; |
Heiko Schocher | 21d29f7 | 2014-05-28 11:33:33 +0200 | [diff] [blame] | 22 | #endif |
Simon Glass | 4101f68 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 23 | struct blk_desc *dev_desc; |
Rob Herring | 7405a13 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 24 | |
Simon Glass | 73223f0 | 2016-02-22 22:55:43 -0700 | [diff] [blame] | 25 | #if CONFIG_IS_ENABLED(FIT) |
Rob Herring | 7405a13 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 26 | const void *fit_hdr = NULL; |
| 27 | #endif |
| 28 | |
| 29 | bootstage_mark(BOOTSTAGE_ID_IDE_START); |
Rob Herring | 475c797 | 2012-08-23 11:31:48 +0000 | [diff] [blame] | 30 | if (argc > 3) { |
Rob Herring | 7405a13 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 31 | bootstage_error(BOOTSTAGE_ID_IDE_ADDR); |
| 32 | return CMD_RET_USAGE; |
| 33 | } |
| 34 | bootstage_mark(BOOTSTAGE_ID_IDE_ADDR); |
| 35 | |
Rob Herring | 475c797 | 2012-08-23 11:31:48 +0000 | [diff] [blame] | 36 | if (argc > 1) |
| 37 | addr = simple_strtoul(argv[1], NULL, 16); |
| 38 | |
Rob Herring | 7405a13 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 39 | bootstage_mark(BOOTSTAGE_ID_IDE_BOOT_DEVICE); |
| 40 | |
Simon Glass | e35929e | 2016-02-29 15:25:44 -0700 | [diff] [blame] | 41 | part = blk_get_device_part_str(intf, (argc == 3) ? argv[2] : NULL, |
Stephen Warren | 10a37fd | 2012-09-21 09:50:57 +0000 | [diff] [blame] | 42 | &dev_desc, &info, 1); |
Rob Herring | 475c797 | 2012-08-23 11:31:48 +0000 | [diff] [blame] | 43 | if (part < 0) { |
Rob Herring | 7405a13 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 44 | bootstage_error(BOOTSTAGE_ID_IDE_TYPE); |
| 45 | return 1; |
| 46 | } |
Rob Herring | 475c797 | 2012-08-23 11:31:48 +0000 | [diff] [blame] | 47 | |
Simon Glass | bcce53d | 2016-02-29 15:25:51 -0700 | [diff] [blame] | 48 | dev = dev_desc->devnum; |
Rob Herring | 7405a13 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 49 | bootstage_mark(BOOTSTAGE_ID_IDE_TYPE); |
| 50 | |
Rob Herring | 475c797 | 2012-08-23 11:31:48 +0000 | [diff] [blame] | 51 | printf("\nLoading from %s device %d, partition %d: " |
| 52 | "Name: %.32s Type: %.32s\n", intf, dev, part, info.name, |
| 53 | info.type); |
Rob Herring | 7405a13 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 54 | |
Frederic Leroy | 04735e9 | 2013-06-26 18:11:25 +0200 | [diff] [blame] | 55 | debug("First Block: " LBAFU ", # of blocks: " LBAFU |
| 56 | ", Block Size: %ld\n", |
Rob Herring | 7405a13 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 57 | info.start, info.size, info.blksz); |
| 58 | |
Simon Glass | 2a981dc | 2016-02-29 15:25:52 -0700 | [diff] [blame] | 59 | if (blk_dread(dev_desc, info.start, 1, (ulong *)addr) != 1) { |
Rob Herring | 7405a13 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 60 | 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)) { |
Tom Rini | c76c93a | 2019-05-23 07:14:07 -0400 | [diff] [blame] | 67 | #if defined(CONFIG_LEGACY_IMAGE_FORMAT) |
Rob Herring | 7405a13 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 68 | 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 Schocher | 21d29f7 | 2014-05-28 11:33:33 +0200 | [diff] [blame] | 84 | #endif |
Simon Glass | 73223f0 | 2016-02-22 22:55:43 -0700 | [diff] [blame] | 85 | #if CONFIG_IS_ENABLED(FIT) |
Rob Herring | 7405a13 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 86 | 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 Glass | 2a981dc | 2016-02-29 15:25:52 -0700 | [diff] [blame] | 103 | if (blk_dread(dev_desc, info.start + 1, cnt, |
| 104 | (ulong *)(addr + info.blksz)) != cnt) { |
Rob Herring | 7405a13 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 105 | 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 Glass | 73223f0 | 2016-02-22 22:55:43 -0700 | [diff] [blame] | 111 | #if CONFIG_IS_ENABLED(FIT) |
Rob Herring | 7405a13 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 112 | /* 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 */ |
Simon Glass | bb872dd | 2019-12-28 10:45:02 -0700 | [diff] [blame] | 128 | image_load_addr = addr; |
Rob Herring | 7405a13 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 129 | |
| 130 | return bootm_maybe_autostart(cmdtp, argv[0]); |
| 131 | } |