blob: e9f6c5f050b4f393d987f64010c4416c6ad29a04 [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
11#include <common.h>
12#include <spl.h>
13#include <asm/u-boot.h>
14#include <sata.h>
Tom Rinieac5b832015-01-05 21:14:04 -050015#include <scsi.h>
Nikita Kiryanov33eefe42015-11-08 17:11:49 +020016#include <errno.h>
Dan Murphy0c3ab852014-02-03 06:59:01 -060017#include <fat.h>
Dan Murphy0c3ab852014-02-03 06:59:01 -060018#include <image.h>
19
Baruch Siachb5224642019-05-16 13:03:53 +030020#ifndef CONFIG_SYS_SATA_FAT_BOOT_PARTITION
21#define CONFIG_SYS_SATA_FAT_BOOT_PARTITION 1
22#endif
23
24#ifndef CONFIG_SPL_FS_LOAD_PAYLOAD_NAME
25#define CONFIG_SPL_FS_LOAD_PAYLOAD_NAME "u-boot.img"
26#endif
27
Baruch Siachc9eff6a2019-07-14 17:54:21 +030028#ifndef CONFIG_SPL_SATA_RAW_U_BOOT_SECTOR
29/* Dummy value to make the compiler happy */
30#define CONFIG_SPL_SATA_RAW_U_BOOT_SECTOR 0x100
31#endif
32
33static int spl_sata_load_image_raw(struct spl_image_info *spl_image,
34 struct blk_desc *stor_dev, unsigned long sector)
35{
36 struct image_header *header;
37 unsigned long count;
38 u32 image_size_sectors;
Pali Rohár11041672021-07-23 11:14:27 +020039 u32 image_offset_sectors;
40 u32 image_offset;
Baruch Siachc9eff6a2019-07-14 17:54:21 +030041 int ret;
42
43 header = spl_get_load_buffer(-sizeof(*header), stor_dev->blksz);
44 count = blk_dread(stor_dev, sector, 1, header);
45 if (count == 0)
46 return -EIO;
47
48 ret = spl_parse_image_header(spl_image, header);
49 if (ret)
50 return ret;
51
52 image_size_sectors = DIV_ROUND_UP(spl_image->size, stor_dev->blksz);
Pali Rohár11041672021-07-23 11:14:27 +020053 image_offset_sectors = spl_image->offset / stor_dev->blksz;
54 image_offset = spl_image->offset % stor_dev->blksz;
55 count = blk_dread(stor_dev, sector + image_offset_sectors,
56 image_size_sectors,
Baruch Siachc9eff6a2019-07-14 17:54:21 +030057 (void *)spl_image->load_addr);
58 if (count != image_size_sectors)
59 return -EIO;
60
Pali Rohár11041672021-07-23 11:14:27 +020061 if (image_offset)
62 memmove((void *)spl_image->load_addr,
63 (void *)spl_image->load_addr + image_offset,
64 spl_image->size);
65
Baruch Siachc9eff6a2019-07-14 17:54:21 +030066 return 0;
67}
68
Simon Glassee306792016-09-24 18:20:13 -060069static int spl_sata_load_image(struct spl_image_info *spl_image,
70 struct spl_boot_device *bootdev)
Dan Murphy0c3ab852014-02-03 06:59:01 -060071{
Baruch Siach6b6d8622019-05-16 13:03:54 +030072 int err = 0;
Simon Glasse3394752016-02-29 15:25:34 -070073 struct blk_desc *stor_dev;
Dan Murphy0c3ab852014-02-03 06:59:01 -060074
Baruch Siach6b6d8622019-05-16 13:03:54 +030075#if !defined(CONFIG_DM_SCSI) && !defined(CONFIG_AHCI)
Dan Murphy0c3ab852014-02-03 06:59:01 -060076 err = init_sata(CONFIG_SPL_SATA_BOOT_DEVICE);
Baruch Siach6b6d8622019-05-16 13:03:54 +030077#endif
Dan Murphy0c3ab852014-02-03 06:59:01 -060078 if (err) {
79#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
80 printf("spl: sata init failed: err - %d\n", err);
81#endif
Nikita Kiryanov33eefe42015-11-08 17:11:49 +020082 return err;
Dan Murphy0c3ab852014-02-03 06:59:01 -060083 } else {
84 /* try to recognize storage devices immediately */
Simon Glass48228732017-06-14 21:28:41 -060085 scsi_scan(false);
Simon Glass9a684322016-05-01 11:36:16 -060086 stor_dev = blk_get_devnum_by_type(IF_TYPE_SCSI, 0);
Nikita Kiryanov33eefe42015-11-08 17:11:49 +020087 if (!stor_dev)
88 return -ENODEV;
Dan Murphy0c3ab852014-02-03 06:59:01 -060089 }
90
Tom Rinic2e78882021-10-30 23:03:48 -040091#if CONFIG_IS_ENABLED(OS_BOOT)
Simon Glass3eb382a2016-09-24 18:20:15 -060092 if (spl_start_uboot() ||
93 spl_load_image_fat_os(spl_image, stor_dev,
94 CONFIG_SYS_SATA_FAT_BOOT_PARTITION))
Dan Murphy0c3ab852014-02-03 06:59:01 -060095#endif
Simon Glass3eb382a2016-09-24 18:20:15 -060096 {
Baruch Siach2f490e42019-05-16 13:03:55 +030097 err = -ENOSYS;
98
99 if (IS_ENABLED(CONFIG_SPL_FS_FAT)) {
100 err = spl_load_image_fat(spl_image, stor_dev,
Simon Glass3eb382a2016-09-24 18:20:15 -0600101 CONFIG_SYS_SATA_FAT_BOOT_PARTITION,
Baruch Siach2f490e42019-05-16 13:03:55 +0300102 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
Baruch Siachc9eff6a2019-07-14 17:54:21 +0300103 } else if (IS_ENABLED(CONFIG_SPL_SATA_RAW_U_BOOT_USE_SECTOR)) {
104 err = spl_sata_load_image_raw(spl_image, stor_dev,
105 CONFIG_SPL_SATA_RAW_U_BOOT_SECTOR);
Baruch Siach2f490e42019-05-16 13:03:55 +0300106 }
Simon Glass3eb382a2016-09-24 18:20:15 -0600107 }
Dan Murphy0c3ab852014-02-03 06:59:01 -0600108 if (err) {
109 puts("Error loading sata device\n");
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200110 return err;
Dan Murphy0c3ab852014-02-03 06:59:01 -0600111 }
Nikita Kiryanov33eefe42015-11-08 17:11:49 +0200112
113 return 0;
Dan Murphy0c3ab852014-02-03 06:59:01 -0600114}
Simon Glass4fc1f252016-11-30 15:30:50 -0700115SPL_LOAD_IMAGE_METHOD("SATA", 0, BOOT_DEVICE_SATA, spl_sata_load_image);