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 | |
Rob Herring | ede8436 | 2012-08-23 11:31:48 +0000 | [diff] [blame] | 11 | #if defined(CONFIG_CMD_IDE) || defined(CONFIG_CMD_SCSI) || \ |
| 12 | defined(CONFIG_USB_STORAGE) |
Rob Herring | a20cbf9 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 13 | int common_diskboot(cmd_tbl_t *cmdtp, const char *intf, int argc, |
| 14 | char *const argv[]) |
| 15 | { |
Rob Herring | ede8436 | 2012-08-23 11:31:48 +0000 | [diff] [blame] | 16 | int dev, part; |
| 17 | ulong addr = CONFIG_SYS_LOAD_ADDR; |
| 18 | ulong cnt; |
Rob Herring | a20cbf9 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 19 | disk_partition_t info; |
| 20 | image_header_t *hdr; |
| 21 | block_dev_desc_t *dev_desc; |
| 22 | |
| 23 | #if defined(CONFIG_FIT) |
| 24 | const void *fit_hdr = NULL; |
| 25 | #endif |
| 26 | |
| 27 | bootstage_mark(BOOTSTAGE_ID_IDE_START); |
Rob Herring | ede8436 | 2012-08-23 11:31:48 +0000 | [diff] [blame] | 28 | if (argc > 3) { |
Rob Herring | a20cbf9 | 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 | ede8436 | 2012-08-23 11:31:48 +0000 | [diff] [blame] | 34 | if (argc > 1) |
| 35 | addr = simple_strtoul(argv[1], NULL, 16); |
Rob Herring | a20cbf9 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 36 | |
Rob Herring | ede8436 | 2012-08-23 11:31:48 +0000 | [diff] [blame] | 37 | bootstage_mark(BOOTSTAGE_ID_IDE_BOOT_DEVICE); |
Rob Herring | a20cbf9 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 38 | |
Rob Herring | ede8436 | 2012-08-23 11:31:48 +0000 | [diff] [blame] | 39 | part = get_device_and_partition(intf, (argc == 3) ? argv[2] : NULL, |
Stephen Warren | c87ff22 | 2012-09-21 09:50:57 +0000 | [diff] [blame] | 40 | &dev_desc, &info, 1); |
Rob Herring | ede8436 | 2012-08-23 11:31:48 +0000 | [diff] [blame] | 41 | if (part < 0) { |
Rob Herring | a20cbf9 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 42 | bootstage_error(BOOTSTAGE_ID_IDE_TYPE); |
| 43 | return 1; |
| 44 | } |
Rob Herring | a20cbf9 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 45 | |
Rob Herring | ede8436 | 2012-08-23 11:31:48 +0000 | [diff] [blame] | 46 | dev = dev_desc->dev; |
| 47 | bootstage_mark(BOOTSTAGE_ID_IDE_TYPE); |
Rob Herring | a20cbf9 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 48 | |
Rob Herring | ede8436 | 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 | a20cbf9 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 52 | |
Frederic Leroy | e7ee028 | 2013-06-26 18:11:25 +0200 | [diff] [blame] | 53 | debug("First Block: " LBAFU ", # of blocks: " LBAFU |
| 54 | ", Block Size: %ld\n", |
Rob Herring | a20cbf9 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 55 | info.start, info.size, info.blksz); |
| 56 | |
| 57 | if (dev_desc->block_read(dev, info.start, 1, (ulong *) addr) != 1) { |
| 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)) { |
| 65 | case IMAGE_FORMAT_LEGACY: |
| 66 | hdr = (image_header_t *) addr; |
| 67 | |
| 68 | bootstage_mark(BOOTSTAGE_ID_IDE_FORMAT); |
| 69 | |
| 70 | if (!image_check_hcrc(hdr)) { |
| 71 | puts("\n** Bad Header Checksum **\n"); |
| 72 | bootstage_error(BOOTSTAGE_ID_IDE_CHECKSUM); |
| 73 | return 1; |
| 74 | } |
| 75 | bootstage_mark(BOOTSTAGE_ID_IDE_CHECKSUM); |
| 76 | |
| 77 | image_print_contents(hdr); |
| 78 | |
| 79 | cnt = image_get_image_size(hdr); |
| 80 | break; |
| 81 | #if defined(CONFIG_FIT) |
| 82 | case IMAGE_FORMAT_FIT: |
| 83 | fit_hdr = (const void *) addr; |
| 84 | puts("Fit image detected...\n"); |
| 85 | |
| 86 | cnt = fit_get_size(fit_hdr); |
| 87 | break; |
| 88 | #endif |
| 89 | default: |
| 90 | bootstage_error(BOOTSTAGE_ID_IDE_FORMAT); |
| 91 | puts("** Unknown image type\n"); |
| 92 | return 1; |
| 93 | } |
| 94 | |
| 95 | cnt += info.blksz - 1; |
| 96 | cnt /= info.blksz; |
| 97 | cnt -= 1; |
| 98 | |
| 99 | if (dev_desc->block_read(dev, info.start + 1, cnt, |
| 100 | (ulong *)(addr + info.blksz)) != cnt) { |
| 101 | printf("** Read error on %d:%d\n", dev, part); |
| 102 | bootstage_error(BOOTSTAGE_ID_IDE_READ); |
| 103 | return 1; |
| 104 | } |
| 105 | bootstage_mark(BOOTSTAGE_ID_IDE_READ); |
| 106 | |
| 107 | #if defined(CONFIG_FIT) |
| 108 | /* This cannot be done earlier, |
| 109 | * we need complete FIT image in RAM first */ |
| 110 | if (genimg_get_format((void *) addr) == IMAGE_FORMAT_FIT) { |
| 111 | if (!fit_check_format(fit_hdr)) { |
| 112 | bootstage_error(BOOTSTAGE_ID_IDE_FIT_READ); |
| 113 | puts("** Bad FIT image format\n"); |
| 114 | return 1; |
| 115 | } |
| 116 | bootstage_mark(BOOTSTAGE_ID_IDE_FIT_READ_OK); |
| 117 | fit_print_contents(fit_hdr); |
| 118 | } |
| 119 | #endif |
| 120 | |
| 121 | flush_cache(addr, (cnt+1)*info.blksz); |
| 122 | |
| 123 | /* Loading ok, update default load address */ |
| 124 | load_addr = addr; |
| 125 | |
| 126 | return bootm_maybe_autostart(cmdtp, argv[0]); |
| 127 | } |
Rob Herring | ede8436 | 2012-08-23 11:31:48 +0000 | [diff] [blame] | 128 | #endif |