Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Dan Murphy | fff40a7 | 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 | fff40a7 | 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 | fc89b2e | 2015-01-05 21:14:04 -0500 | [diff] [blame] | 15 | #include <scsi.h> |
Nikita Kiryanov | 36afd45 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 16 | #include <errno.h> |
Dan Murphy | fff40a7 | 2014-02-03 06:59:01 -0600 | [diff] [blame] | 17 | #include <fat.h> |
Dan Murphy | fff40a7 | 2014-02-03 06:59:01 -0600 | [diff] [blame] | 18 | #include <image.h> |
| 19 | |
Baruch Siach | 60ca969 | 2019-07-14 17:54:21 +0300 | [diff] [blame] | 20 | #ifndef CONFIG_SPL_SATA_RAW_U_BOOT_SECTOR |
| 21 | /* Dummy value to make the compiler happy */ |
| 22 | #define CONFIG_SPL_SATA_RAW_U_BOOT_SECTOR 0x100 |
| 23 | #endif |
| 24 | |
| 25 | static int spl_sata_load_image_raw(struct spl_image_info *spl_image, |
Pali Rohár | 2e0429b | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 26 | struct spl_boot_device *bootdev, |
Baruch Siach | 60ca969 | 2019-07-14 17:54:21 +0300 | [diff] [blame] | 27 | struct blk_desc *stor_dev, unsigned long sector) |
| 28 | { |
Simon Glass | f3543e6 | 2022-09-06 20:26:52 -0600 | [diff] [blame] | 29 | struct legacy_img_hdr *header; |
Baruch Siach | 60ca969 | 2019-07-14 17:54:21 +0300 | [diff] [blame] | 30 | unsigned long count; |
| 31 | u32 image_size_sectors; |
Pali Rohár | 5fce287 | 2021-07-23 11:14:27 +0200 | [diff] [blame] | 32 | u32 image_offset_sectors; |
| 33 | u32 image_offset; |
Baruch Siach | 60ca969 | 2019-07-14 17:54:21 +0300 | [diff] [blame] | 34 | int ret; |
| 35 | |
| 36 | header = spl_get_load_buffer(-sizeof(*header), stor_dev->blksz); |
| 37 | count = blk_dread(stor_dev, sector, 1, header); |
| 38 | if (count == 0) |
| 39 | return -EIO; |
| 40 | |
Pali Rohár | 2e0429b | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 41 | ret = spl_parse_image_header(spl_image, bootdev, header); |
Baruch Siach | 60ca969 | 2019-07-14 17:54:21 +0300 | [diff] [blame] | 42 | if (ret) |
| 43 | return ret; |
| 44 | |
| 45 | image_size_sectors = DIV_ROUND_UP(spl_image->size, stor_dev->blksz); |
Pali Rohár | 5fce287 | 2021-07-23 11:14:27 +0200 | [diff] [blame] | 46 | image_offset_sectors = spl_image->offset / stor_dev->blksz; |
| 47 | image_offset = spl_image->offset % stor_dev->blksz; |
| 48 | count = blk_dread(stor_dev, sector + image_offset_sectors, |
| 49 | image_size_sectors, |
Baruch Siach | 60ca969 | 2019-07-14 17:54:21 +0300 | [diff] [blame] | 50 | (void *)spl_image->load_addr); |
| 51 | if (count != image_size_sectors) |
| 52 | return -EIO; |
| 53 | |
Pali Rohár | 5fce287 | 2021-07-23 11:14:27 +0200 | [diff] [blame] | 54 | if (image_offset) |
| 55 | memmove((void *)spl_image->load_addr, |
| 56 | (void *)spl_image->load_addr + image_offset, |
| 57 | spl_image->size); |
| 58 | |
Baruch Siach | 60ca969 | 2019-07-14 17:54:21 +0300 | [diff] [blame] | 59 | return 0; |
| 60 | } |
| 61 | |
Simon Glass | 2a2ee2a | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 62 | static int spl_sata_load_image(struct spl_image_info *spl_image, |
| 63 | struct spl_boot_device *bootdev) |
Dan Murphy | fff40a7 | 2014-02-03 06:59:01 -0600 | [diff] [blame] | 64 | { |
Baruch Siach | ab2d415 | 2019-05-16 13:03:54 +0300 | [diff] [blame] | 65 | int err = 0; |
Simon Glass | 4101f68 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 66 | struct blk_desc *stor_dev; |
Dan Murphy | fff40a7 | 2014-02-03 06:59:01 -0600 | [diff] [blame] | 67 | |
Tom Rini | d498c67 | 2022-05-13 13:37:30 -0400 | [diff] [blame] | 68 | /* try to recognize storage devices immediately */ |
| 69 | scsi_scan(false); |
Simon Glass | 8149b15 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 70 | stor_dev = blk_get_devnum_by_uclass_id(UCLASS_SCSI, 0); |
Tom Rini | d498c67 | 2022-05-13 13:37:30 -0400 | [diff] [blame] | 71 | if (!stor_dev) |
| 72 | return -ENODEV; |
Dan Murphy | fff40a7 | 2014-02-03 06:59:01 -0600 | [diff] [blame] | 73 | |
Tom Rini | 7115007 | 2021-10-30 23:03:48 -0400 | [diff] [blame] | 74 | #if CONFIG_IS_ENABLED(OS_BOOT) |
Simon Glass | 710e9ca | 2016-09-24 18:20:15 -0600 | [diff] [blame] | 75 | if (spl_start_uboot() || |
Pali Rohár | 2e0429b | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 76 | spl_load_image_fat_os(spl_image, bootdev, stor_dev, |
Simon Glass | 710e9ca | 2016-09-24 18:20:15 -0600 | [diff] [blame] | 77 | CONFIG_SYS_SATA_FAT_BOOT_PARTITION)) |
Dan Murphy | fff40a7 | 2014-02-03 06:59:01 -0600 | [diff] [blame] | 78 | #endif |
Simon Glass | 710e9ca | 2016-09-24 18:20:15 -0600 | [diff] [blame] | 79 | { |
Baruch Siach | 760ef30 | 2019-05-16 13:03:55 +0300 | [diff] [blame] | 80 | err = -ENOSYS; |
| 81 | |
| 82 | if (IS_ENABLED(CONFIG_SPL_FS_FAT)) { |
Pali Rohár | 2e0429b | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 83 | err = spl_load_image_fat(spl_image, bootdev, stor_dev, |
Simon Glass | 710e9ca | 2016-09-24 18:20:15 -0600 | [diff] [blame] | 84 | CONFIG_SYS_SATA_FAT_BOOT_PARTITION, |
Baruch Siach | 760ef30 | 2019-05-16 13:03:55 +0300 | [diff] [blame] | 85 | CONFIG_SPL_FS_LOAD_PAYLOAD_NAME); |
Baruch Siach | 60ca969 | 2019-07-14 17:54:21 +0300 | [diff] [blame] | 86 | } else if (IS_ENABLED(CONFIG_SPL_SATA_RAW_U_BOOT_USE_SECTOR)) { |
Pali Rohár | 2e0429b | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 87 | err = spl_sata_load_image_raw(spl_image, bootdev, stor_dev, |
Baruch Siach | 60ca969 | 2019-07-14 17:54:21 +0300 | [diff] [blame] | 88 | CONFIG_SPL_SATA_RAW_U_BOOT_SECTOR); |
Baruch Siach | 760ef30 | 2019-05-16 13:03:55 +0300 | [diff] [blame] | 89 | } |
Simon Glass | 710e9ca | 2016-09-24 18:20:15 -0600 | [diff] [blame] | 90 | } |
Dan Murphy | fff40a7 | 2014-02-03 06:59:01 -0600 | [diff] [blame] | 91 | if (err) { |
| 92 | puts("Error loading sata device\n"); |
Nikita Kiryanov | 36afd45 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 93 | return err; |
Dan Murphy | fff40a7 | 2014-02-03 06:59:01 -0600 | [diff] [blame] | 94 | } |
Nikita Kiryanov | 36afd45 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 95 | |
| 96 | return 0; |
Dan Murphy | fff40a7 | 2014-02-03 06:59:01 -0600 | [diff] [blame] | 97 | } |
Simon Glass | ebc4ef6 | 2016-11-30 15:30:50 -0700 | [diff] [blame] | 98 | SPL_LOAD_IMAGE_METHOD("SATA", 0, BOOT_DEVICE_SATA, spl_sata_load_image); |