Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Dan Murphy | 0c3ab85 | 2014-02-03 06:59:01 -0600 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2013 |
| 4 | * Texas Instruments, <www.ti.com> |
| 5 | * |
| 6 | * Dan Murphy <dmurphy@ti.com> |
| 7 | * |
Dan Murphy | 0c3ab85 | 2014-02-03 06:59:01 -0600 | [diff] [blame] | 8 | * Derived work from spl_usb.c |
| 9 | */ |
| 10 | |
| 11 | #include <common.h> |
| 12 | #include <spl.h> |
| 13 | #include <asm/u-boot.h> |
| 14 | #include <sata.h> |
Tom Rini | eac5b83 | 2015-01-05 21:14:04 -0500 | [diff] [blame] | 15 | #include <scsi.h> |
Nikita Kiryanov | 33eefe4 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 16 | #include <errno.h> |
Dan Murphy | 0c3ab85 | 2014-02-03 06:59:01 -0600 | [diff] [blame] | 17 | #include <fat.h> |
Dan Murphy | 0c3ab85 | 2014-02-03 06:59:01 -0600 | [diff] [blame] | 18 | #include <image.h> |
| 19 | |
Baruch Siach | b522464 | 2019-05-16 13:03:53 +0300 | [diff] [blame] | 20 | #ifndef CONFIG_SYS_SATA_FAT_BOOT_PARTITION |
| 21 | #define CONFIG_SYS_SATA_FAT_BOOT_PARTITION 1 |
| 22 | #endif |
| 23 | |
| 24 | #ifndef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME |
| 25 | #define CONFIG_SPL_FS_LOAD_PAYLOAD_NAME "u-boot.img" |
| 26 | #endif |
| 27 | |
Baruch Siach | c9eff6a | 2019-07-14 17:54:21 +0300 | [diff] [blame] | 28 | #ifndef CONFIG_SPL_SATA_RAW_U_BOOT_SECTOR |
| 29 | /* Dummy value to make the compiler happy */ |
| 30 | #define CONFIG_SPL_SATA_RAW_U_BOOT_SECTOR 0x100 |
| 31 | #endif |
| 32 | |
| 33 | static int spl_sata_load_image_raw(struct spl_image_info *spl_image, |
Pali Rohár | dda8f88 | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 34 | struct spl_boot_device *bootdev, |
Baruch Siach | c9eff6a | 2019-07-14 17:54:21 +0300 | [diff] [blame] | 35 | struct blk_desc *stor_dev, unsigned long sector) |
| 36 | { |
| 37 | struct image_header *header; |
| 38 | unsigned long count; |
| 39 | u32 image_size_sectors; |
Pali Rohár | 1104167 | 2021-07-23 11:14:27 +0200 | [diff] [blame] | 40 | u32 image_offset_sectors; |
| 41 | u32 image_offset; |
Baruch Siach | c9eff6a | 2019-07-14 17:54:21 +0300 | [diff] [blame] | 42 | int ret; |
| 43 | |
| 44 | header = spl_get_load_buffer(-sizeof(*header), stor_dev->blksz); |
| 45 | count = blk_dread(stor_dev, sector, 1, header); |
| 46 | if (count == 0) |
| 47 | return -EIO; |
| 48 | |
Pali Rohár | dda8f88 | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 49 | ret = spl_parse_image_header(spl_image, bootdev, header); |
Baruch Siach | c9eff6a | 2019-07-14 17:54:21 +0300 | [diff] [blame] | 50 | if (ret) |
| 51 | return ret; |
| 52 | |
| 53 | image_size_sectors = DIV_ROUND_UP(spl_image->size, stor_dev->blksz); |
Pali Rohár | 1104167 | 2021-07-23 11:14:27 +0200 | [diff] [blame] | 54 | image_offset_sectors = spl_image->offset / stor_dev->blksz; |
| 55 | image_offset = spl_image->offset % stor_dev->blksz; |
| 56 | count = blk_dread(stor_dev, sector + image_offset_sectors, |
| 57 | image_size_sectors, |
Baruch Siach | c9eff6a | 2019-07-14 17:54:21 +0300 | [diff] [blame] | 58 | (void *)spl_image->load_addr); |
| 59 | if (count != image_size_sectors) |
| 60 | return -EIO; |
| 61 | |
Pali Rohár | 1104167 | 2021-07-23 11:14:27 +0200 | [diff] [blame] | 62 | if (image_offset) |
| 63 | memmove((void *)spl_image->load_addr, |
| 64 | (void *)spl_image->load_addr + image_offset, |
| 65 | spl_image->size); |
| 66 | |
Baruch Siach | c9eff6a | 2019-07-14 17:54:21 +0300 | [diff] [blame] | 67 | return 0; |
| 68 | } |
| 69 | |
Simon Glass | ee30679 | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 70 | static int spl_sata_load_image(struct spl_image_info *spl_image, |
| 71 | struct spl_boot_device *bootdev) |
Dan Murphy | 0c3ab85 | 2014-02-03 06:59:01 -0600 | [diff] [blame] | 72 | { |
Baruch Siach | 6b6d862 | 2019-05-16 13:03:54 +0300 | [diff] [blame] | 73 | int err = 0; |
Simon Glass | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 74 | struct blk_desc *stor_dev; |
Dan Murphy | 0c3ab85 | 2014-02-03 06:59:01 -0600 | [diff] [blame] | 75 | |
Baruch Siach | 6b6d862 | 2019-05-16 13:03:54 +0300 | [diff] [blame] | 76 | #if !defined(CONFIG_DM_SCSI) && !defined(CONFIG_AHCI) |
Dan Murphy | 0c3ab85 | 2014-02-03 06:59:01 -0600 | [diff] [blame] | 77 | err = init_sata(CONFIG_SPL_SATA_BOOT_DEVICE); |
Baruch Siach | 6b6d862 | 2019-05-16 13:03:54 +0300 | [diff] [blame] | 78 | #endif |
Dan Murphy | 0c3ab85 | 2014-02-03 06:59:01 -0600 | [diff] [blame] | 79 | if (err) { |
| 80 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
| 81 | printf("spl: sata init failed: err - %d\n", err); |
| 82 | #endif |
Nikita Kiryanov | 33eefe4 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 83 | return err; |
Dan Murphy | 0c3ab85 | 2014-02-03 06:59:01 -0600 | [diff] [blame] | 84 | } else { |
| 85 | /* try to recognize storage devices immediately */ |
Simon Glass | 4822873 | 2017-06-14 21:28:41 -0600 | [diff] [blame] | 86 | scsi_scan(false); |
Simon Glass | 9a68432 | 2016-05-01 11:36:16 -0600 | [diff] [blame] | 87 | stor_dev = blk_get_devnum_by_type(IF_TYPE_SCSI, 0); |
Nikita Kiryanov | 33eefe4 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 88 | if (!stor_dev) |
| 89 | return -ENODEV; |
Dan Murphy | 0c3ab85 | 2014-02-03 06:59:01 -0600 | [diff] [blame] | 90 | } |
| 91 | |
Tom Rini | c2e7888 | 2021-10-30 23:03:48 -0400 | [diff] [blame] | 92 | #if CONFIG_IS_ENABLED(OS_BOOT) |
Simon Glass | 3eb382a | 2016-09-24 18:20:15 -0600 | [diff] [blame] | 93 | if (spl_start_uboot() || |
Pali Rohár | dda8f88 | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 94 | spl_load_image_fat_os(spl_image, bootdev, stor_dev, |
Simon Glass | 3eb382a | 2016-09-24 18:20:15 -0600 | [diff] [blame] | 95 | CONFIG_SYS_SATA_FAT_BOOT_PARTITION)) |
Dan Murphy | 0c3ab85 | 2014-02-03 06:59:01 -0600 | [diff] [blame] | 96 | #endif |
Simon Glass | 3eb382a | 2016-09-24 18:20:15 -0600 | [diff] [blame] | 97 | { |
Baruch Siach | 2f490e4 | 2019-05-16 13:03:55 +0300 | [diff] [blame] | 98 | err = -ENOSYS; |
| 99 | |
| 100 | if (IS_ENABLED(CONFIG_SPL_FS_FAT)) { |
Pali Rohár | dda8f88 | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 101 | err = spl_load_image_fat(spl_image, bootdev, stor_dev, |
Simon Glass | 3eb382a | 2016-09-24 18:20:15 -0600 | [diff] [blame] | 102 | CONFIG_SYS_SATA_FAT_BOOT_PARTITION, |
Baruch Siach | 2f490e4 | 2019-05-16 13:03:55 +0300 | [diff] [blame] | 103 | CONFIG_SPL_FS_LOAD_PAYLOAD_NAME); |
Baruch Siach | c9eff6a | 2019-07-14 17:54:21 +0300 | [diff] [blame] | 104 | } else if (IS_ENABLED(CONFIG_SPL_SATA_RAW_U_BOOT_USE_SECTOR)) { |
Pali Rohár | dda8f88 | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 105 | err = spl_sata_load_image_raw(spl_image, bootdev, stor_dev, |
Baruch Siach | c9eff6a | 2019-07-14 17:54:21 +0300 | [diff] [blame] | 106 | CONFIG_SPL_SATA_RAW_U_BOOT_SECTOR); |
Baruch Siach | 2f490e4 | 2019-05-16 13:03:55 +0300 | [diff] [blame] | 107 | } |
Simon Glass | 3eb382a | 2016-09-24 18:20:15 -0600 | [diff] [blame] | 108 | } |
Dan Murphy | 0c3ab85 | 2014-02-03 06:59:01 -0600 | [diff] [blame] | 109 | if (err) { |
| 110 | puts("Error loading sata device\n"); |
Nikita Kiryanov | 33eefe4 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 111 | return err; |
Dan Murphy | 0c3ab85 | 2014-02-03 06:59:01 -0600 | [diff] [blame] | 112 | } |
Nikita Kiryanov | 33eefe4 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 113 | |
| 114 | return 0; |
Dan Murphy | 0c3ab85 | 2014-02-03 06:59:01 -0600 | [diff] [blame] | 115 | } |
Simon Glass | 4fc1f25 | 2016-11-30 15:30:50 -0700 | [diff] [blame] | 116 | SPL_LOAD_IMAGE_METHOD("SATA", 0, BOOT_DEVICE_SATA, spl_sata_load_image); |