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> |
Rob Herring | 475c797 | 2012-08-23 11:31:48 +0000 | [diff] [blame] | 8 | #include <part.h> |
Rob Herring | 7405a13 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 9 | |
| 10 | int common_diskboot(cmd_tbl_t *cmdtp, const char *intf, int argc, |
| 11 | char *const argv[]) |
| 12 | { |
Marek Vasut | 04681cb | 2016-05-01 00:35:54 +0200 | [diff] [blame] | 13 | __maybe_unused int dev; |
| 14 | int part; |
Rob Herring | 475c797 | 2012-08-23 11:31:48 +0000 | [diff] [blame] | 15 | ulong addr = CONFIG_SYS_LOAD_ADDR; |
| 16 | ulong cnt; |
Rob Herring | 7405a13 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 17 | disk_partition_t info; |
Heiko Schocher | 21d29f7 | 2014-05-28 11:33:33 +0200 | [diff] [blame] | 18 | #if defined(CONFIG_IMAGE_FORMAT_LEGACY) |
Rob Herring | 7405a13 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 19 | image_header_t *hdr; |
Heiko Schocher | 21d29f7 | 2014-05-28 11:33:33 +0200 | [diff] [blame] | 20 | #endif |
Simon Glass | 4101f68 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 21 | struct blk_desc *dev_desc; |
Rob Herring | 7405a13 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 22 | |
Simon Glass | 73223f0 | 2016-02-22 22:55:43 -0700 | [diff] [blame] | 23 | #if CONFIG_IS_ENABLED(FIT) |
Rob Herring | 7405a13 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 24 | const void *fit_hdr = NULL; |
| 25 | #endif |
| 26 | |
| 27 | bootstage_mark(BOOTSTAGE_ID_IDE_START); |
Rob Herring | 475c797 | 2012-08-23 11:31:48 +0000 | [diff] [blame] | 28 | if (argc > 3) { |
Rob Herring | 7405a13 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 29 | bootstage_error(BOOTSTAGE_ID_IDE_ADDR); |
| 30 | return CMD_RET_USAGE; |
| 31 | } |
| 32 | bootstage_mark(BOOTSTAGE_ID_IDE_ADDR); |
| 33 | |
Rob Herring | 475c797 | 2012-08-23 11:31:48 +0000 | [diff] [blame] | 34 | if (argc > 1) |
| 35 | addr = simple_strtoul(argv[1], NULL, 16); |
| 36 | |
Rob Herring | 7405a13 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 37 | bootstage_mark(BOOTSTAGE_ID_IDE_BOOT_DEVICE); |
| 38 | |
Simon Glass | e35929e | 2016-02-29 15:25:44 -0700 | [diff] [blame] | 39 | part = blk_get_device_part_str(intf, (argc == 3) ? argv[2] : NULL, |
Stephen Warren | 10a37fd | 2012-09-21 09:50:57 +0000 | [diff] [blame] | 40 | &dev_desc, &info, 1); |
Rob Herring | 475c797 | 2012-08-23 11:31:48 +0000 | [diff] [blame] | 41 | if (part < 0) { |
Rob Herring | 7405a13 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 42 | bootstage_error(BOOTSTAGE_ID_IDE_TYPE); |
| 43 | return 1; |
| 44 | } |
Rob Herring | 475c797 | 2012-08-23 11:31:48 +0000 | [diff] [blame] | 45 | |
Simon Glass | bcce53d | 2016-02-29 15:25:51 -0700 | [diff] [blame] | 46 | dev = dev_desc->devnum; |
Rob Herring | 7405a13 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 47 | bootstage_mark(BOOTSTAGE_ID_IDE_TYPE); |
| 48 | |
Rob Herring | 475c797 | 2012-08-23 11:31:48 +0000 | [diff] [blame] | 49 | printf("\nLoading from %s device %d, partition %d: " |
| 50 | "Name: %.32s Type: %.32s\n", intf, dev, part, info.name, |
| 51 | info.type); |
Rob Herring | 7405a13 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 52 | |
Frederic Leroy | 04735e9 | 2013-06-26 18:11:25 +0200 | [diff] [blame] | 53 | debug("First Block: " LBAFU ", # of blocks: " LBAFU |
| 54 | ", Block Size: %ld\n", |
Rob Herring | 7405a13 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 55 | info.start, info.size, info.blksz); |
| 56 | |
Simon Glass | 2a981dc | 2016-02-29 15:25:52 -0700 | [diff] [blame] | 57 | if (blk_dread(dev_desc, info.start, 1, (ulong *)addr) != 1) { |
Rob Herring | 7405a13 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 58 | printf("** Read error on %d:%d\n", dev, part); |
| 59 | bootstage_error(BOOTSTAGE_ID_IDE_PART_READ); |
| 60 | return 1; |
| 61 | } |
| 62 | bootstage_mark(BOOTSTAGE_ID_IDE_PART_READ); |
| 63 | |
| 64 | switch (genimg_get_format((void *) addr)) { |
Heiko Schocher | 21d29f7 | 2014-05-28 11:33:33 +0200 | [diff] [blame] | 65 | #if defined(CONFIG_IMAGE_FORMAT_LEGACY) |
Rob Herring | 7405a13 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 66 | case IMAGE_FORMAT_LEGACY: |
| 67 | hdr = (image_header_t *) addr; |
| 68 | |
| 69 | bootstage_mark(BOOTSTAGE_ID_IDE_FORMAT); |
| 70 | |
| 71 | if (!image_check_hcrc(hdr)) { |
| 72 | puts("\n** Bad Header Checksum **\n"); |
| 73 | bootstage_error(BOOTSTAGE_ID_IDE_CHECKSUM); |
| 74 | return 1; |
| 75 | } |
| 76 | bootstage_mark(BOOTSTAGE_ID_IDE_CHECKSUM); |
| 77 | |
| 78 | image_print_contents(hdr); |
| 79 | |
| 80 | cnt = image_get_image_size(hdr); |
| 81 | break; |
Heiko Schocher | 21d29f7 | 2014-05-28 11:33:33 +0200 | [diff] [blame] | 82 | #endif |
Simon Glass | 73223f0 | 2016-02-22 22:55:43 -0700 | [diff] [blame] | 83 | #if CONFIG_IS_ENABLED(FIT) |
Rob Herring | 7405a13 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 84 | case IMAGE_FORMAT_FIT: |
| 85 | fit_hdr = (const void *) addr; |
| 86 | puts("Fit image detected...\n"); |
| 87 | |
| 88 | cnt = fit_get_size(fit_hdr); |
| 89 | break; |
| 90 | #endif |
| 91 | default: |
| 92 | bootstage_error(BOOTSTAGE_ID_IDE_FORMAT); |
| 93 | puts("** Unknown image type\n"); |
| 94 | return 1; |
| 95 | } |
| 96 | |
| 97 | cnt += info.blksz - 1; |
| 98 | cnt /= info.blksz; |
| 99 | cnt -= 1; |
| 100 | |
Simon Glass | 2a981dc | 2016-02-29 15:25:52 -0700 | [diff] [blame] | 101 | if (blk_dread(dev_desc, info.start + 1, cnt, |
| 102 | (ulong *)(addr + info.blksz)) != cnt) { |
Rob Herring | 7405a13 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 103 | printf("** Read error on %d:%d\n", dev, part); |
| 104 | bootstage_error(BOOTSTAGE_ID_IDE_READ); |
| 105 | return 1; |
| 106 | } |
| 107 | bootstage_mark(BOOTSTAGE_ID_IDE_READ); |
| 108 | |
Simon Glass | 73223f0 | 2016-02-22 22:55:43 -0700 | [diff] [blame] | 109 | #if CONFIG_IS_ENABLED(FIT) |
Rob Herring | 7405a13 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 110 | /* This cannot be done earlier, |
| 111 | * we need complete FIT image in RAM first */ |
| 112 | if (genimg_get_format((void *) addr) == IMAGE_FORMAT_FIT) { |
| 113 | if (!fit_check_format(fit_hdr)) { |
| 114 | bootstage_error(BOOTSTAGE_ID_IDE_FIT_READ); |
| 115 | puts("** Bad FIT image format\n"); |
| 116 | return 1; |
| 117 | } |
| 118 | bootstage_mark(BOOTSTAGE_ID_IDE_FIT_READ_OK); |
| 119 | fit_print_contents(fit_hdr); |
| 120 | } |
| 121 | #endif |
| 122 | |
| 123 | flush_cache(addr, (cnt+1)*info.blksz); |
| 124 | |
| 125 | /* Loading ok, update default load address */ |
| 126 | load_addr = addr; |
| 127 | |
| 128 | return bootm_maybe_autostart(cmdtp, argv[0]); |
| 129 | } |