blob: 32746ce9f3cf70de6ad201a146ff93d3c97ce704 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Dan Murphyfff40a72014-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 Murphyfff40a72014-02-03 06:59:01 -06008 * 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 Rinifc89b2e2015-01-05 21:14:04 -050015#include <scsi.h>
Nikita Kiryanov36afd452015-11-08 17:11:49 +020016#include <errno.h>
Dan Murphyfff40a72014-02-03 06:59:01 -060017#include <fat.h>
Dan Murphyfff40a72014-02-03 06:59:01 -060018#include <image.h>
19
Baruch Siach60ca9692019-07-14 17:54:21 +030020static int spl_sata_load_image_raw(struct spl_image_info *spl_image,
Pali Rohár2e0429b2022-01-14 14:31:38 +010021 struct spl_boot_device *bootdev,
Baruch Siach60ca9692019-07-14 17:54:21 +030022 struct blk_desc *stor_dev, unsigned long sector)
23{
Simon Glassf3543e62022-09-06 20:26:52 -060024 struct legacy_img_hdr *header;
Baruch Siach60ca9692019-07-14 17:54:21 +030025 unsigned long count;
26 u32 image_size_sectors;
Pali Rohár5fce2872021-07-23 11:14:27 +020027 u32 image_offset_sectors;
28 u32 image_offset;
Baruch Siach60ca9692019-07-14 17:54:21 +030029 int ret;
30
31 header = spl_get_load_buffer(-sizeof(*header), stor_dev->blksz);
32 count = blk_dread(stor_dev, sector, 1, header);
33 if (count == 0)
34 return -EIO;
35
Pali Rohár2e0429b2022-01-14 14:31:38 +010036 ret = spl_parse_image_header(spl_image, bootdev, header);
Baruch Siach60ca9692019-07-14 17:54:21 +030037 if (ret)
38 return ret;
39
40 image_size_sectors = DIV_ROUND_UP(spl_image->size, stor_dev->blksz);
Pali Rohár5fce2872021-07-23 11:14:27 +020041 image_offset_sectors = spl_image->offset / stor_dev->blksz;
42 image_offset = spl_image->offset % stor_dev->blksz;
43 count = blk_dread(stor_dev, sector + image_offset_sectors,
44 image_size_sectors,
Baruch Siach60ca9692019-07-14 17:54:21 +030045 (void *)spl_image->load_addr);
46 if (count != image_size_sectors)
47 return -EIO;
48
Pali Rohár5fce2872021-07-23 11:14:27 +020049 if (image_offset)
50 memmove((void *)spl_image->load_addr,
51 (void *)spl_image->load_addr + image_offset,
52 spl_image->size);
53
Baruch Siach60ca9692019-07-14 17:54:21 +030054 return 0;
55}
56
Simon Glass2a2ee2a2016-09-24 18:20:13 -060057static int spl_sata_load_image(struct spl_image_info *spl_image,
58 struct spl_boot_device *bootdev)
Dan Murphyfff40a72014-02-03 06:59:01 -060059{
Tom Rini6e73ab32023-01-10 11:19:35 -050060 int err = -ENOSYS;
Simon Glass4101f682016-02-29 15:25:34 -070061 struct blk_desc *stor_dev;
Dan Murphyfff40a72014-02-03 06:59:01 -060062
Tom Rinid498c672022-05-13 13:37:30 -040063 /* try to recognize storage devices immediately */
64 scsi_scan(false);
Simon Glass8149b152022-09-17 09:00:09 -060065 stor_dev = blk_get_devnum_by_uclass_id(UCLASS_SCSI, 0);
Tom Rinid498c672022-05-13 13:37:30 -040066 if (!stor_dev)
67 return -ENODEV;
Dan Murphyfff40a72014-02-03 06:59:01 -060068
Tom Rini71150072021-10-30 23:03:48 -040069#if CONFIG_IS_ENABLED(OS_BOOT)
Simon Glass710e9ca2016-09-24 18:20:15 -060070 if (spl_start_uboot() ||
Pali Rohár2e0429b2022-01-14 14:31:38 +010071 spl_load_image_fat_os(spl_image, bootdev, stor_dev,
Simon Glass710e9ca2016-09-24 18:20:15 -060072 CONFIG_SYS_SATA_FAT_BOOT_PARTITION))
Dan Murphyfff40a72014-02-03 06:59:01 -060073#endif
Simon Glass710e9ca2016-09-24 18:20:15 -060074 {
Tom Rini6e73ab32023-01-10 11:19:35 -050075#ifdef CONFIG_SPL_FS_FAT
76 err = spl_load_image_fat(spl_image, bootdev, stor_dev,
77 CONFIG_SYS_SATA_FAT_BOOT_PARTITION,
78 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
79#elif defined(CONFIG_SPL_SATA_RAW_U_BOOT_USE_SECTOR)
80 err = spl_sata_load_image_raw(spl_image, bootdev, stor_dev,
81 CONFIG_SPL_SATA_RAW_U_BOOT_SECTOR);
82#endif
Simon Glass710e9ca2016-09-24 18:20:15 -060083 }
Dan Murphyfff40a72014-02-03 06:59:01 -060084 if (err) {
85 puts("Error loading sata device\n");
Nikita Kiryanov36afd452015-11-08 17:11:49 +020086 return err;
Dan Murphyfff40a72014-02-03 06:59:01 -060087 }
Nikita Kiryanov36afd452015-11-08 17:11:49 +020088
89 return 0;
Dan Murphyfff40a72014-02-03 06:59:01 -060090}
Simon Glassebc4ef62016-11-30 15:30:50 -070091SPL_LOAD_IMAGE_METHOD("SATA", 0, BOOT_DEVICE_SATA, spl_sata_load_image);