blob: 12397f0ae17e8cae86b1d3deb28a99050cecba1d [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 +030020#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
25static int spl_sata_load_image_raw(struct spl_image_info *spl_image,
Pali Rohár2e0429b2022-01-14 14:31:38 +010026 struct spl_boot_device *bootdev,
Baruch Siach60ca9692019-07-14 17:54:21 +030027 struct blk_desc *stor_dev, unsigned long sector)
28{
Simon Glassf3543e62022-09-06 20:26:52 -060029 struct legacy_img_hdr *header;
Baruch Siach60ca9692019-07-14 17:54:21 +030030 unsigned long count;
31 u32 image_size_sectors;
Pali Rohár5fce2872021-07-23 11:14:27 +020032 u32 image_offset_sectors;
33 u32 image_offset;
Baruch Siach60ca9692019-07-14 17:54:21 +030034 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ár2e0429b2022-01-14 14:31:38 +010041 ret = spl_parse_image_header(spl_image, bootdev, header);
Baruch Siach60ca9692019-07-14 17:54:21 +030042 if (ret)
43 return ret;
44
45 image_size_sectors = DIV_ROUND_UP(spl_image->size, stor_dev->blksz);
Pali Rohár5fce2872021-07-23 11:14:27 +020046 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 Siach60ca9692019-07-14 17:54:21 +030050 (void *)spl_image->load_addr);
51 if (count != image_size_sectors)
52 return -EIO;
53
Pali Rohár5fce2872021-07-23 11:14:27 +020054 if (image_offset)
55 memmove((void *)spl_image->load_addr,
56 (void *)spl_image->load_addr + image_offset,
57 spl_image->size);
58
Baruch Siach60ca9692019-07-14 17:54:21 +030059 return 0;
60}
61
Simon Glass2a2ee2a2016-09-24 18:20:13 -060062static int spl_sata_load_image(struct spl_image_info *spl_image,
63 struct spl_boot_device *bootdev)
Dan Murphyfff40a72014-02-03 06:59:01 -060064{
Baruch Siachab2d4152019-05-16 13:03:54 +030065 int err = 0;
Simon Glass4101f682016-02-29 15:25:34 -070066 struct blk_desc *stor_dev;
Dan Murphyfff40a72014-02-03 06:59:01 -060067
Tom Rinid498c672022-05-13 13:37:30 -040068 /* try to recognize storage devices immediately */
69 scsi_scan(false);
Simon Glass8149b152022-09-17 09:00:09 -060070 stor_dev = blk_get_devnum_by_uclass_id(UCLASS_SCSI, 0);
Tom Rinid498c672022-05-13 13:37:30 -040071 if (!stor_dev)
72 return -ENODEV;
Dan Murphyfff40a72014-02-03 06:59:01 -060073
Tom Rini71150072021-10-30 23:03:48 -040074#if CONFIG_IS_ENABLED(OS_BOOT)
Simon Glass710e9ca2016-09-24 18:20:15 -060075 if (spl_start_uboot() ||
Pali Rohár2e0429b2022-01-14 14:31:38 +010076 spl_load_image_fat_os(spl_image, bootdev, stor_dev,
Simon Glass710e9ca2016-09-24 18:20:15 -060077 CONFIG_SYS_SATA_FAT_BOOT_PARTITION))
Dan Murphyfff40a72014-02-03 06:59:01 -060078#endif
Simon Glass710e9ca2016-09-24 18:20:15 -060079 {
Baruch Siach760ef302019-05-16 13:03:55 +030080 err = -ENOSYS;
81
82 if (IS_ENABLED(CONFIG_SPL_FS_FAT)) {
Pali Rohár2e0429b2022-01-14 14:31:38 +010083 err = spl_load_image_fat(spl_image, bootdev, stor_dev,
Simon Glass710e9ca2016-09-24 18:20:15 -060084 CONFIG_SYS_SATA_FAT_BOOT_PARTITION,
Baruch Siach760ef302019-05-16 13:03:55 +030085 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
Baruch Siach60ca9692019-07-14 17:54:21 +030086 } else if (IS_ENABLED(CONFIG_SPL_SATA_RAW_U_BOOT_USE_SECTOR)) {
Pali Rohár2e0429b2022-01-14 14:31:38 +010087 err = spl_sata_load_image_raw(spl_image, bootdev, stor_dev,
Baruch Siach60ca9692019-07-14 17:54:21 +030088 CONFIG_SPL_SATA_RAW_U_BOOT_SECTOR);
Baruch Siach760ef302019-05-16 13:03:55 +030089 }
Simon Glass710e9ca2016-09-24 18:20:15 -060090 }
Dan Murphyfff40a72014-02-03 06:59:01 -060091 if (err) {
92 puts("Error loading sata device\n");
Nikita Kiryanov36afd452015-11-08 17:11:49 +020093 return err;
Dan Murphyfff40a72014-02-03 06:59:01 -060094 }
Nikita Kiryanov36afd452015-11-08 17:11:49 +020095
96 return 0;
Dan Murphyfff40a72014-02-03 06:59:01 -060097}
Simon Glassebc4ef62016-11-30 15:30:50 -070098SPL_LOAD_IMAGE_METHOD("SATA", 0, BOOT_DEVICE_SATA, spl_sata_load_image);