Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Rob Herring | a20cbf9 | 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 | a20cbf9 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 5 | */ |
Simon Glass | 1ea9789 | 2020-05-10 11:40:00 -0600 | [diff] [blame] | 6 | #include <bootstage.h> |
Rob Herring | a20cbf9 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 7 | #include <command.h> |
Simon Glass | 6333448 | 2019-11-14 12:57:39 -0700 | [diff] [blame] | 8 | #include <cpu_func.h> |
Simon Glass | 85f1378 | 2019-12-28 10:45:03 -0700 | [diff] [blame] | 9 | #include <image.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 10 | #include <log.h> |
Rob Herring | ede8436 | 2012-08-23 11:31:48 +0000 | [diff] [blame] | 11 | #include <part.h> |
Rob Herring | a20cbf9 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 12 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 13 | int common_diskboot(struct cmd_tbl *cmdtp, const char *intf, int argc, |
Rob Herring | a20cbf9 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 14 | char *const argv[]) |
| 15 | { |
Marek Vasut | 74fcb4f | 2016-05-01 00:35:54 +0200 | [diff] [blame] | 16 | __maybe_unused int dev; |
| 17 | int part; |
Rob Herring | ede8436 | 2012-08-23 11:31:48 +0000 | [diff] [blame] | 18 | ulong addr = CONFIG_SYS_LOAD_ADDR; |
| 19 | ulong cnt; |
Simon Glass | c1c4a8f | 2020-05-10 11:39:57 -0600 | [diff] [blame] | 20 | struct disk_partition info; |
Tom Rini | c220bd9 | 2019-05-23 07:14:07 -0400 | [diff] [blame] | 21 | #if defined(CONFIG_LEGACY_IMAGE_FORMAT) |
Simon Glass | bb7d3bb | 2022-09-06 20:26:52 -0600 | [diff] [blame] | 22 | struct legacy_img_hdr *hdr; |
Heiko Schocher | 515eb12 | 2014-05-28 11:33:33 +0200 | [diff] [blame] | 23 | #endif |
Simon Glass | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 24 | struct blk_desc *dev_desc; |
Rob Herring | a20cbf9 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 25 | |
Simon Glass | e3ee2fb | 2016-02-22 22:55:43 -0700 | [diff] [blame] | 26 | #if CONFIG_IS_ENABLED(FIT) |
Rob Herring | a20cbf9 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 27 | const void *fit_hdr = NULL; |
| 28 | #endif |
| 29 | |
| 30 | bootstage_mark(BOOTSTAGE_ID_IDE_START); |
Rob Herring | ede8436 | 2012-08-23 11:31:48 +0000 | [diff] [blame] | 31 | if (argc > 3) { |
Rob Herring | a20cbf9 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 32 | bootstage_error(BOOTSTAGE_ID_IDE_ADDR); |
| 33 | return CMD_RET_USAGE; |
| 34 | } |
| 35 | bootstage_mark(BOOTSTAGE_ID_IDE_ADDR); |
| 36 | |
Rob Herring | ede8436 | 2012-08-23 11:31:48 +0000 | [diff] [blame] | 37 | if (argc > 1) |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 38 | addr = hextoul(argv[1], NULL); |
Rob Herring | a20cbf9 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 39 | |
Rob Herring | ede8436 | 2012-08-23 11:31:48 +0000 | [diff] [blame] | 40 | bootstage_mark(BOOTSTAGE_ID_IDE_BOOT_DEVICE); |
Rob Herring | a20cbf9 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 41 | |
Simon Glass | 793a98e | 2023-11-18 14:05:20 -0700 | [diff] [blame] | 42 | part = blk_get_device_part_str(intf, cmd_arg2(argc, argv), |
| 43 | &dev_desc, &info, 1); |
Rob Herring | ede8436 | 2012-08-23 11:31:48 +0000 | [diff] [blame] | 44 | if (part < 0) { |
Rob Herring | a20cbf9 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 45 | bootstage_error(BOOTSTAGE_ID_IDE_TYPE); |
| 46 | return 1; |
| 47 | } |
Rob Herring | a20cbf9 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 48 | |
Simon Glass | 2f26fff | 2016-02-29 15:25:51 -0700 | [diff] [blame] | 49 | dev = dev_desc->devnum; |
Rob Herring | ede8436 | 2012-08-23 11:31:48 +0000 | [diff] [blame] | 50 | bootstage_mark(BOOTSTAGE_ID_IDE_TYPE); |
Rob Herring | a20cbf9 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 51 | |
Rob Herring | ede8436 | 2012-08-23 11:31:48 +0000 | [diff] [blame] | 52 | printf("\nLoading from %s device %d, partition %d: " |
| 53 | "Name: %.32s Type: %.32s\n", intf, dev, part, info.name, |
| 54 | info.type); |
Rob Herring | a20cbf9 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 55 | |
Frederic Leroy | e7ee028 | 2013-06-26 18:11:25 +0200 | [diff] [blame] | 56 | debug("First Block: " LBAFU ", # of blocks: " LBAFU |
| 57 | ", Block Size: %ld\n", |
Rob Herring | a20cbf9 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 58 | info.start, info.size, info.blksz); |
| 59 | |
Simon Glass | 2ee8ada | 2016-02-29 15:25:52 -0700 | [diff] [blame] | 60 | if (blk_dread(dev_desc, info.start, 1, (ulong *)addr) != 1) { |
Rob Herring | a20cbf9 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 61 | printf("** Read error on %d:%d\n", dev, part); |
| 62 | bootstage_error(BOOTSTAGE_ID_IDE_PART_READ); |
| 63 | return 1; |
| 64 | } |
| 65 | bootstage_mark(BOOTSTAGE_ID_IDE_PART_READ); |
| 66 | |
| 67 | switch (genimg_get_format((void *) addr)) { |
Tom Rini | c220bd9 | 2019-05-23 07:14:07 -0400 | [diff] [blame] | 68 | #if defined(CONFIG_LEGACY_IMAGE_FORMAT) |
Rob Herring | a20cbf9 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 69 | case IMAGE_FORMAT_LEGACY: |
Simon Glass | bb7d3bb | 2022-09-06 20:26:52 -0600 | [diff] [blame] | 70 | hdr = (struct legacy_img_hdr *)addr; |
Rob Herring | a20cbf9 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 71 | |
| 72 | bootstage_mark(BOOTSTAGE_ID_IDE_FORMAT); |
| 73 | |
| 74 | if (!image_check_hcrc(hdr)) { |
| 75 | puts("\n** Bad Header Checksum **\n"); |
| 76 | bootstage_error(BOOTSTAGE_ID_IDE_CHECKSUM); |
| 77 | return 1; |
| 78 | } |
| 79 | bootstage_mark(BOOTSTAGE_ID_IDE_CHECKSUM); |
| 80 | |
| 81 | image_print_contents(hdr); |
| 82 | |
| 83 | cnt = image_get_image_size(hdr); |
| 84 | break; |
Heiko Schocher | 515eb12 | 2014-05-28 11:33:33 +0200 | [diff] [blame] | 85 | #endif |
Simon Glass | e3ee2fb | 2016-02-22 22:55:43 -0700 | [diff] [blame] | 86 | #if CONFIG_IS_ENABLED(FIT) |
Rob Herring | a20cbf9 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 87 | case IMAGE_FORMAT_FIT: |
| 88 | fit_hdr = (const void *) addr; |
| 89 | puts("Fit image detected...\n"); |
| 90 | |
| 91 | cnt = fit_get_size(fit_hdr); |
| 92 | break; |
| 93 | #endif |
| 94 | default: |
| 95 | bootstage_error(BOOTSTAGE_ID_IDE_FORMAT); |
| 96 | puts("** Unknown image type\n"); |
| 97 | return 1; |
| 98 | } |
| 99 | |
| 100 | cnt += info.blksz - 1; |
| 101 | cnt /= info.blksz; |
| 102 | cnt -= 1; |
| 103 | |
Simon Glass | 2ee8ada | 2016-02-29 15:25:52 -0700 | [diff] [blame] | 104 | if (blk_dread(dev_desc, info.start + 1, cnt, |
| 105 | (ulong *)(addr + info.blksz)) != cnt) { |
Rob Herring | a20cbf9 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 106 | printf("** Read error on %d:%d\n", dev, part); |
| 107 | bootstage_error(BOOTSTAGE_ID_IDE_READ); |
| 108 | return 1; |
| 109 | } |
| 110 | bootstage_mark(BOOTSTAGE_ID_IDE_READ); |
| 111 | |
Simon Glass | e3ee2fb | 2016-02-22 22:55:43 -0700 | [diff] [blame] | 112 | #if CONFIG_IS_ENABLED(FIT) |
Rob Herring | a20cbf9 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 113 | /* This cannot be done earlier, |
| 114 | * we need complete FIT image in RAM first */ |
| 115 | if (genimg_get_format((void *) addr) == IMAGE_FORMAT_FIT) { |
Simon Glass | d563c25 | 2021-02-15 17:08:09 -0700 | [diff] [blame] | 116 | if (fit_check_format(fit_hdr, IMAGE_SIZE_INVAL)) { |
Rob Herring | a20cbf9 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 117 | bootstage_error(BOOTSTAGE_ID_IDE_FIT_READ); |
| 118 | puts("** Bad FIT image format\n"); |
| 119 | return 1; |
| 120 | } |
| 121 | bootstage_mark(BOOTSTAGE_ID_IDE_FIT_READ_OK); |
Rob Herring | a20cbf9 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 122 | } |
| 123 | #endif |
| 124 | |
| 125 | flush_cache(addr, (cnt+1)*info.blksz); |
| 126 | |
| 127 | /* Loading ok, update default load address */ |
Simon Glass | 892265d | 2019-12-28 10:45:02 -0700 | [diff] [blame] | 128 | image_load_addr = addr; |
Rob Herring | a20cbf9 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 129 | |
| 130 | return bootm_maybe_autostart(cmdtp, argv[0]); |
| 131 | } |