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 | |
Dan Murphy | 0c3ab85 | 2014-02-03 06:59:01 -0600 | [diff] [blame] | 11 | #include <spl.h> |
Dan Murphy | 0c3ab85 | 2014-02-03 06:59:01 -0600 | [diff] [blame] | 12 | #include <sata.h> |
Tom Rini | eac5b83 | 2015-01-05 21:14:04 -0500 | [diff] [blame] | 13 | #include <scsi.h> |
Nikita Kiryanov | 33eefe4 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 14 | #include <errno.h> |
Dan Murphy | 0c3ab85 | 2014-02-03 06:59:01 -0600 | [diff] [blame] | 15 | #include <fat.h> |
Dan Murphy | 0c3ab85 | 2014-02-03 06:59:01 -0600 | [diff] [blame] | 16 | #include <image.h> |
| 17 | |
Baruch Siach | c9eff6a | 2019-07-14 17:54:21 +0300 | [diff] [blame] | 18 | 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] | 19 | struct spl_boot_device *bootdev, |
Baruch Siach | c9eff6a | 2019-07-14 17:54:21 +0300 | [diff] [blame] | 20 | struct blk_desc *stor_dev, unsigned long sector) |
| 21 | { |
Simon Glass | bb7d3bb | 2022-09-06 20:26:52 -0600 | [diff] [blame] | 22 | struct legacy_img_hdr *header; |
Baruch Siach | c9eff6a | 2019-07-14 17:54:21 +0300 | [diff] [blame] | 23 | unsigned long count; |
| 24 | u32 image_size_sectors; |
Pali Rohár | 1104167 | 2021-07-23 11:14:27 +0200 | [diff] [blame] | 25 | u32 image_offset_sectors; |
| 26 | u32 image_offset; |
Baruch Siach | c9eff6a | 2019-07-14 17:54:21 +0300 | [diff] [blame] | 27 | int ret; |
| 28 | |
| 29 | header = spl_get_load_buffer(-sizeof(*header), stor_dev->blksz); |
| 30 | count = blk_dread(stor_dev, sector, 1, header); |
| 31 | if (count == 0) |
| 32 | return -EIO; |
| 33 | |
Pali Rohár | dda8f88 | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 34 | ret = spl_parse_image_header(spl_image, bootdev, header); |
Baruch Siach | c9eff6a | 2019-07-14 17:54:21 +0300 | [diff] [blame] | 35 | if (ret) |
| 36 | return ret; |
| 37 | |
| 38 | 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] | 39 | image_offset_sectors = spl_image->offset / stor_dev->blksz; |
| 40 | image_offset = spl_image->offset % stor_dev->blksz; |
| 41 | count = blk_dread(stor_dev, sector + image_offset_sectors, |
| 42 | image_size_sectors, |
Baruch Siach | c9eff6a | 2019-07-14 17:54:21 +0300 | [diff] [blame] | 43 | (void *)spl_image->load_addr); |
| 44 | if (count != image_size_sectors) |
| 45 | return -EIO; |
| 46 | |
Pali Rohár | 1104167 | 2021-07-23 11:14:27 +0200 | [diff] [blame] | 47 | if (image_offset) |
| 48 | memmove((void *)spl_image->load_addr, |
| 49 | (void *)spl_image->load_addr + image_offset, |
| 50 | spl_image->size); |
| 51 | |
Baruch Siach | c9eff6a | 2019-07-14 17:54:21 +0300 | [diff] [blame] | 52 | return 0; |
| 53 | } |
| 54 | |
Simon Glass | ee30679 | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 55 | static int spl_sata_load_image(struct spl_image_info *spl_image, |
| 56 | struct spl_boot_device *bootdev) |
Dan Murphy | 0c3ab85 | 2014-02-03 06:59:01 -0600 | [diff] [blame] | 57 | { |
Tom Rini | 05ca277 | 2023-01-10 11:19:35 -0500 | [diff] [blame] | 58 | int err = -ENOSYS; |
Simon Glass | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 59 | struct blk_desc *stor_dev; |
Dan Murphy | 0c3ab85 | 2014-02-03 06:59:01 -0600 | [diff] [blame] | 60 | |
Tom Rini | 6c7a9e0 | 2022-05-13 13:37:30 -0400 | [diff] [blame] | 61 | /* try to recognize storage devices immediately */ |
| 62 | scsi_scan(false); |
Simon Glass | fada3f9 | 2022-09-17 09:00:09 -0600 | [diff] [blame] | 63 | stor_dev = blk_get_devnum_by_uclass_id(UCLASS_SCSI, 0); |
Tom Rini | 6c7a9e0 | 2022-05-13 13:37:30 -0400 | [diff] [blame] | 64 | if (!stor_dev) |
| 65 | return -ENODEV; |
Dan Murphy | 0c3ab85 | 2014-02-03 06:59:01 -0600 | [diff] [blame] | 66 | |
Tom Rini | c2e7888 | 2021-10-30 23:03:48 -0400 | [diff] [blame] | 67 | #if CONFIG_IS_ENABLED(OS_BOOT) |
Simon Glass | 3eb382a | 2016-09-24 18:20:15 -0600 | [diff] [blame] | 68 | if (spl_start_uboot() || |
Pali Rohár | dda8f88 | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 69 | spl_load_image_fat_os(spl_image, bootdev, stor_dev, |
Simon Glass | 3eb382a | 2016-09-24 18:20:15 -0600 | [diff] [blame] | 70 | CONFIG_SYS_SATA_FAT_BOOT_PARTITION)) |
Dan Murphy | 0c3ab85 | 2014-02-03 06:59:01 -0600 | [diff] [blame] | 71 | #endif |
Simon Glass | 3eb382a | 2016-09-24 18:20:15 -0600 | [diff] [blame] | 72 | { |
Tom Rini | 05ca277 | 2023-01-10 11:19:35 -0500 | [diff] [blame] | 73 | #ifdef CONFIG_SPL_FS_FAT |
| 74 | err = spl_load_image_fat(spl_image, bootdev, stor_dev, |
| 75 | CONFIG_SYS_SATA_FAT_BOOT_PARTITION, |
| 76 | CONFIG_SPL_FS_LOAD_PAYLOAD_NAME); |
| 77 | #elif defined(CONFIG_SPL_SATA_RAW_U_BOOT_USE_SECTOR) |
| 78 | err = spl_sata_load_image_raw(spl_image, bootdev, stor_dev, |
| 79 | CONFIG_SPL_SATA_RAW_U_BOOT_SECTOR); |
| 80 | #endif |
Simon Glass | 3eb382a | 2016-09-24 18:20:15 -0600 | [diff] [blame] | 81 | } |
Dan Murphy | 0c3ab85 | 2014-02-03 06:59:01 -0600 | [diff] [blame] | 82 | if (err) { |
| 83 | puts("Error loading sata device\n"); |
Nikita Kiryanov | 33eefe4 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 84 | return err; |
Dan Murphy | 0c3ab85 | 2014-02-03 06:59:01 -0600 | [diff] [blame] | 85 | } |
Nikita Kiryanov | 33eefe4 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 86 | |
| 87 | return 0; |
Dan Murphy | 0c3ab85 | 2014-02-03 06:59:01 -0600 | [diff] [blame] | 88 | } |
Simon Glass | 4fc1f25 | 2016-11-30 15:30:50 -0700 | [diff] [blame] | 89 | SPL_LOAD_IMAGE_METHOD("SATA", 0, BOOT_DEVICE_SATA, spl_sata_load_image); |