Rob Herring | a20cbf9 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2000-2011 |
| 3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 4 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
Rob Herring | a20cbf9 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 6 | */ |
| 7 | #include <common.h> |
| 8 | #include <command.h> |
Rob Herring | ede8436 | 2012-08-23 11:31:48 +0000 | [diff] [blame] | 9 | #include <part.h> |
Rob Herring | a20cbf9 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 10 | |
| 11 | int common_diskboot(cmd_tbl_t *cmdtp, const char *intf, int argc, |
| 12 | char *const argv[]) |
| 13 | { |
Marek Vasut | 74fcb4f | 2016-05-01 00:35:54 +0200 | [diff] [blame] | 14 | __maybe_unused int dev; |
| 15 | int part; |
Rob Herring | ede8436 | 2012-08-23 11:31:48 +0000 | [diff] [blame] | 16 | ulong addr = CONFIG_SYS_LOAD_ADDR; |
| 17 | ulong cnt; |
Rob Herring | a20cbf9 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 18 | disk_partition_t info; |
Heiko Schocher | 515eb12 | 2014-05-28 11:33:33 +0200 | [diff] [blame] | 19 | #if defined(CONFIG_IMAGE_FORMAT_LEGACY) |
Rob Herring | a20cbf9 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 20 | image_header_t *hdr; |
Heiko Schocher | 515eb12 | 2014-05-28 11:33:33 +0200 | [diff] [blame] | 21 | #endif |
Simon Glass | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 22 | struct blk_desc *dev_desc; |
Rob Herring | a20cbf9 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 23 | |
Simon Glass | e3ee2fb | 2016-02-22 22:55:43 -0700 | [diff] [blame] | 24 | #if CONFIG_IS_ENABLED(FIT) |
Rob Herring | a20cbf9 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 25 | const void *fit_hdr = NULL; |
| 26 | #endif |
| 27 | |
| 28 | bootstage_mark(BOOTSTAGE_ID_IDE_START); |
Rob Herring | ede8436 | 2012-08-23 11:31:48 +0000 | [diff] [blame] | 29 | if (argc > 3) { |
Rob Herring | a20cbf9 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 30 | bootstage_error(BOOTSTAGE_ID_IDE_ADDR); |
| 31 | return CMD_RET_USAGE; |
| 32 | } |
| 33 | bootstage_mark(BOOTSTAGE_ID_IDE_ADDR); |
| 34 | |
Rob Herring | ede8436 | 2012-08-23 11:31:48 +0000 | [diff] [blame] | 35 | if (argc > 1) |
| 36 | addr = simple_strtoul(argv[1], NULL, 16); |
Rob Herring | a20cbf9 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 37 | |
Rob Herring | ede8436 | 2012-08-23 11:31:48 +0000 | [diff] [blame] | 38 | bootstage_mark(BOOTSTAGE_ID_IDE_BOOT_DEVICE); |
Rob Herring | a20cbf9 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 39 | |
Simon Glass | e76ee97 | 2016-02-29 15:25:44 -0700 | [diff] [blame] | 40 | part = blk_get_device_part_str(intf, (argc == 3) ? argv[2] : NULL, |
Stephen Warren | c87ff22 | 2012-09-21 09:50:57 +0000 | [diff] [blame] | 41 | &dev_desc, &info, 1); |
Rob Herring | ede8436 | 2012-08-23 11:31:48 +0000 | [diff] [blame] | 42 | if (part < 0) { |
Rob Herring | a20cbf9 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 43 | bootstage_error(BOOTSTAGE_ID_IDE_TYPE); |
| 44 | return 1; |
| 45 | } |
Rob Herring | a20cbf9 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 46 | |
Simon Glass | 2f26fff | 2016-02-29 15:25:51 -0700 | [diff] [blame] | 47 | dev = dev_desc->devnum; |
Rob Herring | ede8436 | 2012-08-23 11:31:48 +0000 | [diff] [blame] | 48 | bootstage_mark(BOOTSTAGE_ID_IDE_TYPE); |
Rob Herring | a20cbf9 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 49 | |
Rob Herring | ede8436 | 2012-08-23 11:31:48 +0000 | [diff] [blame] | 50 | printf("\nLoading from %s device %d, partition %d: " |
| 51 | "Name: %.32s Type: %.32s\n", intf, dev, part, info.name, |
| 52 | info.type); |
Rob Herring | a20cbf9 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 53 | |
Frederic Leroy | e7ee028 | 2013-06-26 18:11:25 +0200 | [diff] [blame] | 54 | debug("First Block: " LBAFU ", # of blocks: " LBAFU |
| 55 | ", Block Size: %ld\n", |
Rob Herring | a20cbf9 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 56 | info.start, info.size, info.blksz); |
| 57 | |
Simon Glass | 2ee8ada | 2016-02-29 15:25:52 -0700 | [diff] [blame] | 58 | if (blk_dread(dev_desc, info.start, 1, (ulong *)addr) != 1) { |
Rob Herring | a20cbf9 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 59 | 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)) { |
Heiko Schocher | 515eb12 | 2014-05-28 11:33:33 +0200 | [diff] [blame] | 66 | #if defined(CONFIG_IMAGE_FORMAT_LEGACY) |
Rob Herring | a20cbf9 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 67 | 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 Schocher | 515eb12 | 2014-05-28 11:33:33 +0200 | [diff] [blame] | 83 | #endif |
Simon Glass | e3ee2fb | 2016-02-22 22:55:43 -0700 | [diff] [blame] | 84 | #if CONFIG_IS_ENABLED(FIT) |
Rob Herring | a20cbf9 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 85 | 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 Glass | 2ee8ada | 2016-02-29 15:25:52 -0700 | [diff] [blame] | 102 | if (blk_dread(dev_desc, info.start + 1, cnt, |
| 103 | (ulong *)(addr + info.blksz)) != cnt) { |
Rob Herring | a20cbf9 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 104 | 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 Glass | e3ee2fb | 2016-02-22 22:55:43 -0700 | [diff] [blame] | 110 | #if CONFIG_IS_ENABLED(FIT) |
Rob Herring | a20cbf9 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 111 | /* 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 | } |