blob: 67fc620d9bea91d330b1dbbb1f9fc5269c387ced [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Dan Murphy0c3ab852014-02-03 06:59:01 -06002/*
3 * (C) Copyright 2013
4 * Texas Instruments, <www.ti.com>
5 *
6 * Dan Murphy <dmurphy@ti.com>
7 *
Dan Murphy0c3ab852014-02-03 06:59:01 -06008 * Derived work from spl_usb.c
9 */
10
Dan Murphy0c3ab852014-02-03 06:59:01 -060011#include <spl.h>
Dan Murphy0c3ab852014-02-03 06:59:01 -060012#include <sata.h>
Tom Rinieac5b832015-01-05 21:14:04 -050013#include <scsi.h>
Nikita Kiryanov33eefe42015-11-08 17:11:49 +020014#include <errno.h>
Dan Murphy0c3ab852014-02-03 06:59:01 -060015#include <fat.h>
Dan Murphy0c3ab852014-02-03 06:59:01 -060016#include <image.h>
17
Baruch Siachc9eff6a2019-07-14 17:54:21 +030018static int spl_sata_load_image_raw(struct spl_image_info *spl_image,
Pali Rohárdda8f882022-01-14 14:31:38 +010019 struct spl_boot_device *bootdev,
Baruch Siachc9eff6a2019-07-14 17:54:21 +030020 struct blk_desc *stor_dev, unsigned long sector)
21{
Simon Glassbb7d3bb2022-09-06 20:26:52 -060022 struct legacy_img_hdr *header;
Baruch Siachc9eff6a2019-07-14 17:54:21 +030023 unsigned long count;
24 u32 image_size_sectors;
Pali Rohár11041672021-07-23 11:14:27 +020025 u32 image_offset_sectors;
26 u32 image_offset;
Baruch Siachc9eff6a2019-07-14 17:54:21 +030027 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árdda8f882022-01-14 14:31:38 +010034 ret = spl_parse_image_header(spl_image, bootdev, header);
Baruch Siachc9eff6a2019-07-14 17:54:21 +030035 if (ret)
36 return ret;
37
38 image_size_sectors = DIV_ROUND_UP(spl_image->size, stor_dev->blksz);
Pali Rohár11041672021-07-23 11:14:27 +020039 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 Siachc9eff6a2019-07-14 17:54:21 +030043 (void *)spl_image->load_addr);
44 if (count != image_size_sectors)
45 return -EIO;
46
Pali Rohár11041672021-07-23 11:14:27 +020047 if (image_offset)
48 memmove((void *)spl_image->load_addr,
49 (void *)spl_image->load_addr + image_offset,
50 spl_image->size);
51
Baruch Siachc9eff6a2019-07-14 17:54:21 +030052 return 0;
53}
54
Simon Glassee306792016-09-24 18:20:13 -060055static int spl_sata_load_image(struct spl_image_info *spl_image,
56 struct spl_boot_device *bootdev)
Dan Murphy0c3ab852014-02-03 06:59:01 -060057{
Tom Rini05ca2772023-01-10 11:19:35 -050058 int err = -ENOSYS;
Simon Glasse3394752016-02-29 15:25:34 -070059 struct blk_desc *stor_dev;
Dan Murphy0c3ab852014-02-03 06:59:01 -060060
Tom Rini6c7a9e02022-05-13 13:37:30 -040061 /* try to recognize storage devices immediately */
62 scsi_scan(false);
Simon Glassfada3f92022-09-17 09:00:09 -060063 stor_dev = blk_get_devnum_by_uclass_id(UCLASS_SCSI, 0);
Tom Rini6c7a9e02022-05-13 13:37:30 -040064 if (!stor_dev)
65 return -ENODEV;
Dan Murphy0c3ab852014-02-03 06:59:01 -060066
Tom Rinic2e78882021-10-30 23:03:48 -040067#if CONFIG_IS_ENABLED(OS_BOOT)
Simon Glass3eb382a2016-09-24 18:20:15 -060068 if (spl_start_uboot() ||
Pali Rohárdda8f882022-01-14 14:31:38 +010069 spl_load_image_fat_os(spl_image, bootdev, stor_dev,
Simon Glass3eb382a2016-09-24 18:20:15 -060070 CONFIG_SYS_SATA_FAT_BOOT_PARTITION))
Dan Murphy0c3ab852014-02-03 06:59:01 -060071#endif
Simon Glass3eb382a2016-09-24 18:20:15 -060072 {
Tom Rini05ca2772023-01-10 11:19:35 -050073#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 Glass3eb382a2016-09-24 18:20:15 -060081 }
Dan Murphy0c3ab852014-02-03 06:59:01 -060082 if (err) {
83 puts("Error loading sata device\n");
Nikita Kiryanov33eefe42015-11-08 17:11:49 +020084 return err;
Dan Murphy0c3ab852014-02-03 06:59:01 -060085 }
Nikita Kiryanov33eefe42015-11-08 17:11:49 +020086
87 return 0;
Dan Murphy0c3ab852014-02-03 06:59:01 -060088}
Simon Glass4fc1f252016-11-30 15:30:50 -070089SPL_LOAD_IMAGE_METHOD("SATA", 0, BOOT_DEVICE_SATA, spl_sata_load_image);