1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * (C) Copyright 2013
4 * Texas Instruments, <www.ti.com>
5 *
6 * Dan Murphy <dmurphy@ti.com>
7 *
8 * 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>
15 #include <scsi.h>
16 #include <errno.h>
17 #include <fat.h>
18 #include <image.h>
19
20 #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
28 #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
spl_sata_load_image_raw(struct spl_image_info * spl_image,struct blk_desc * stor_dev,unsigned long sector)33 static 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 u32 image_offset_sectors;
40 u32 image_offset;
41 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);
53 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,
57 (void *)spl_image->load_addr);
58 if (count != image_size_sectors)
59 return -EIO;
60
61 if (image_offset)
62 memmove((void *)spl_image->load_addr,
63 (void *)spl_image->load_addr + image_offset,
64 spl_image->size);
65
66 return 0;
67 }
68
spl_sata_load_image(struct spl_image_info * spl_image,struct spl_boot_device * bootdev)69 static int spl_sata_load_image(struct spl_image_info *spl_image,
70 struct spl_boot_device *bootdev)
71 {
72 int err = 0;
73 struct blk_desc *stor_dev;
74
75 #if !defined(CONFIG_DM_SCSI) && !defined(CONFIG_AHCI)
76 err = init_sata(CONFIG_SPL_SATA_BOOT_DEVICE);
77 #endif
78 if (err) {
79 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
80 printf("spl: sata init failed: err - %d\n", err);
81 #endif
82 return err;
83 } else {
84 /* try to recognize storage devices immediately */
85 scsi_scan(false);
86 stor_dev = blk_get_devnum_by_type(IF_TYPE_SCSI, 0);
87 if (!stor_dev)
88 return -ENODEV;
89 }
90
91 #ifdef CONFIG_SPL_OS_BOOT
92 if (spl_start_uboot() ||
93 spl_load_image_fat_os(spl_image, stor_dev,
94 CONFIG_SYS_SATA_FAT_BOOT_PARTITION))
95 #endif
96 {
97 err = -ENOSYS;
98
99 if (IS_ENABLED(CONFIG_SPL_FS_FAT)) {
100 err = spl_load_image_fat(spl_image, stor_dev,
101 CONFIG_SYS_SATA_FAT_BOOT_PARTITION,
102 CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
103 } 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);
106 }
107 }
108 if (err) {
109 puts("Error loading sata device\n");
110 return err;
111 }
112
113 return 0;
114 }
115 SPL_LOAD_IMAGE_METHOD("SATA", 0, BOOT_DEVICE_SATA, spl_sata_load_image);
116