blob: e108af0576aa971a1714a9170cc63173ae3d0bdf [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 Siacha4c61ff2019-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 Siach60ca9692019-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;
39 int ret;
40
41 header = spl_get_load_buffer(-sizeof(*header), stor_dev->blksz);
42 count = blk_dread(stor_dev, sector, 1, header);
43 if (count == 0)
44 return -EIO;
45
46 ret = spl_parse_image_header(spl_image, header);
47 if (ret)
48 return ret;
49
50 image_size_sectors = DIV_ROUND_UP(spl_image->size, stor_dev->blksz);
51 count = blk_dread(stor_dev, sector, image_size_sectors,
52 (void *)spl_image->load_addr);
53 if (count != image_size_sectors)
54 return -EIO;
55
56 return 0;
57}
58
Simon Glass2a2ee2a2016-09-24 18:20:13 -060059static int spl_sata_load_image(struct spl_image_info *spl_image,
60 struct spl_boot_device *bootdev)
Dan Murphyfff40a72014-02-03 06:59:01 -060061{
Baruch Siachab2d4152019-05-16 13:03:54 +030062 int err = 0;
Simon Glass4101f682016-02-29 15:25:34 -070063 struct blk_desc *stor_dev;
Dan Murphyfff40a72014-02-03 06:59:01 -060064
Baruch Siachab2d4152019-05-16 13:03:54 +030065#if !defined(CONFIG_DM_SCSI) && !defined(CONFIG_AHCI)
Dan Murphyfff40a72014-02-03 06:59:01 -060066 err = init_sata(CONFIG_SPL_SATA_BOOT_DEVICE);
Baruch Siachab2d4152019-05-16 13:03:54 +030067#endif
Dan Murphyfff40a72014-02-03 06:59:01 -060068 if (err) {
69#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
70 printf("spl: sata init failed: err - %d\n", err);
71#endif
Nikita Kiryanov36afd452015-11-08 17:11:49 +020072 return err;
Dan Murphyfff40a72014-02-03 06:59:01 -060073 } else {
74 /* try to recognize storage devices immediately */
Simon Glass8eab1a52017-06-14 21:28:41 -060075 scsi_scan(false);
Simon Glassedd82ab2016-05-01 11:36:16 -060076 stor_dev = blk_get_devnum_by_type(IF_TYPE_SCSI, 0);
Nikita Kiryanov36afd452015-11-08 17:11:49 +020077 if (!stor_dev)
78 return -ENODEV;
Dan Murphyfff40a72014-02-03 06:59:01 -060079 }
80
81#ifdef CONFIG_SPL_OS_BOOT
Simon Glass710e9ca2016-09-24 18:20:15 -060082 if (spl_start_uboot() ||
83 spl_load_image_fat_os(spl_image, stor_dev,
84 CONFIG_SYS_SATA_FAT_BOOT_PARTITION))
Dan Murphyfff40a72014-02-03 06:59:01 -060085#endif
Simon Glass710e9ca2016-09-24 18:20:15 -060086 {
Baruch Siach760ef302019-05-16 13:03:55 +030087 err = -ENOSYS;
88
89 if (IS_ENABLED(CONFIG_SPL_FS_FAT)) {
90 err = spl_load_image_fat(spl_image, stor_dev,
Simon Glass710e9ca2016-09-24 18:20:15 -060091 CONFIG_SYS_SATA_FAT_BOOT_PARTITION,
Baruch Siach760ef302019-05-16 13:03:55 +030092 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
Baruch Siach60ca9692019-07-14 17:54:21 +030093 } else if (IS_ENABLED(CONFIG_SPL_SATA_RAW_U_BOOT_USE_SECTOR)) {
94 err = spl_sata_load_image_raw(spl_image, stor_dev,
95 CONFIG_SPL_SATA_RAW_U_BOOT_SECTOR);
Baruch Siach760ef302019-05-16 13:03:55 +030096 }
Simon Glass710e9ca2016-09-24 18:20:15 -060097 }
Dan Murphyfff40a72014-02-03 06:59:01 -060098 if (err) {
99 puts("Error loading sata device\n");
Nikita Kiryanov36afd452015-11-08 17:11:49 +0200100 return err;
Dan Murphyfff40a72014-02-03 06:59:01 -0600101 }
Nikita Kiryanov36afd452015-11-08 17:11:49 +0200102
103 return 0;
Dan Murphyfff40a72014-02-03 06:59:01 -0600104}
Simon Glassebc4ef62016-11-30 15:30:50 -0700105SPL_LOAD_IMAGE_METHOD("SATA", 0, BOOT_DEVICE_SATA, spl_sata_load_image);