spl: Rework Kconfig to be more menu driven
[platform/kernel/u-boot.git] / common / spl / spl_sata.c
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_SATA_RAW_U_BOOT_SECTOR
25 /* Dummy value to make the compiler happy */
26 #define CONFIG_SPL_SATA_RAW_U_BOOT_SECTOR 0x100
27 #endif
28
29 static int spl_sata_load_image_raw(struct spl_image_info *spl_image,
30                 struct spl_boot_device *bootdev,
31                 struct blk_desc *stor_dev, unsigned long sector)
32 {
33         struct image_header *header;
34         unsigned long count;
35         u32 image_size_sectors;
36         u32 image_offset_sectors;
37         u32 image_offset;
38         int ret;
39
40         header = spl_get_load_buffer(-sizeof(*header), stor_dev->blksz);
41         count = blk_dread(stor_dev, sector, 1, header);
42         if (count == 0)
43                 return -EIO;
44
45         ret = spl_parse_image_header(spl_image, bootdev, header);
46         if (ret)
47                 return ret;
48
49         image_size_sectors = DIV_ROUND_UP(spl_image->size, stor_dev->blksz);
50         image_offset_sectors = spl_image->offset / stor_dev->blksz;
51         image_offset = spl_image->offset % stor_dev->blksz;
52         count = blk_dread(stor_dev, sector + image_offset_sectors,
53                         image_size_sectors,
54                         (void *)spl_image->load_addr);
55         if (count != image_size_sectors)
56                 return -EIO;
57
58         if (image_offset)
59                 memmove((void *)spl_image->load_addr,
60                         (void *)spl_image->load_addr + image_offset,
61                         spl_image->size);
62
63         return 0;
64 }
65
66 static int spl_sata_load_image(struct spl_image_info *spl_image,
67                                struct spl_boot_device *bootdev)
68 {
69         int err = 0;
70         struct blk_desc *stor_dev;
71
72         /* try to recognize storage devices immediately */
73         scsi_scan(false);
74         stor_dev = blk_get_devnum_by_type(IF_TYPE_SCSI, 0);
75         if (!stor_dev)
76                 return -ENODEV;
77
78 #if CONFIG_IS_ENABLED(OS_BOOT)
79         if (spl_start_uboot() ||
80             spl_load_image_fat_os(spl_image, bootdev, stor_dev,
81                                   CONFIG_SYS_SATA_FAT_BOOT_PARTITION))
82 #endif
83         {
84                 err = -ENOSYS;
85
86                 if (IS_ENABLED(CONFIG_SPL_FS_FAT)) {
87                         err = spl_load_image_fat(spl_image, bootdev, stor_dev,
88                                         CONFIG_SYS_SATA_FAT_BOOT_PARTITION,
89                                         CONFIG_SPL_FS_LOAD_PAYLOAD_NAME);
90                 } else if (IS_ENABLED(CONFIG_SPL_SATA_RAW_U_BOOT_USE_SECTOR)) {
91                         err = spl_sata_load_image_raw(spl_image, bootdev, stor_dev,
92                                 CONFIG_SPL_SATA_RAW_U_BOOT_SECTOR);
93                 }
94         }
95         if (err) {
96                 puts("Error loading sata device\n");
97                 return err;
98         }
99
100         return 0;
101 }
102 SPL_LOAD_IMAGE_METHOD("SATA", 0, BOOT_DEVICE_SATA, spl_sata_load_image);