blob: 014074f85be4e0dd0faf7731a44d898bfecd878c [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Dan Murphyb7b5b0c2014-01-16 11:23:29 -06002/*
3 * (C) Copyright 2014
4 * Texas Instruments, <www.ti.com>
5 *
6 * Dan Murphy <dmurphy@ti.com>
7 *
Dan Murphyb7b5b0c2014-01-16 11:23:29 -06008 * FAT Image Functions copied from spl_mmc.c
9 */
10
11#include <common.h>
Simon Glass0af6e2d2019-08-01 09:46:52 -060012#include <env.h>
Simon Glass0f2af882020-05-10 11:40:05 -060013#include <log.h>
Sean Anderson5ff77722023-10-14 16:47:55 -040014#include <mapmem.h>
Dan Murphyb7b5b0c2014-01-16 11:23:29 -060015#include <spl.h>
16#include <asm/u-boot.h>
17#include <fat.h>
Nikita Kiryanovcc49f552015-11-08 17:11:45 +020018#include <errno.h>
Dan Murphyb7b5b0c2014-01-16 11:23:29 -060019#include <image.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090020#include <linux/libfdt.h>
Dan Murphyb7b5b0c2014-01-16 11:23:29 -060021
22static int fat_registered;
23
Sean Anderson13e6d912023-10-14 16:47:54 -040024void spl_fat_force_reregister(void)
25{
26 fat_registered = 0;
27}
28
Simon Glasse3394752016-02-29 15:25:34 -070029static int spl_register_fat_device(struct blk_desc *block_dev, int partition)
Dan Murphyb7b5b0c2014-01-16 11:23:29 -060030{
31 int err = 0;
32
33 if (fat_registered)
34 return err;
35
36 err = fat_register_device(block_dev, partition);
37 if (err) {
38#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Dan Murphycb367e12014-01-16 11:23:30 -060039 printf("%s: fat register err - %d\n", __func__, err);
Dan Murphyb7b5b0c2014-01-16 11:23:29 -060040#endif
Guillaume GARDETa23ec1d2014-10-15 17:53:14 +020041 return err;
Dan Murphyb7b5b0c2014-01-16 11:23:29 -060042 }
43
44 fat_registered = 1;
45
46 return err;
47}
48
Lokesh Vutlaa0913a32016-05-24 10:34:39 +053049static ulong spl_fit_read(struct spl_load_info *load, ulong file_offset,
50 ulong size, void *buf)
51{
52 loff_t actread;
53 int ret;
54 char *filename = (char *)load->filename;
55
56 ret = fat_read_file(filename, buf, file_offset, size, &actread);
57 if (ret)
58 return ret;
59
60 return actread;
61}
62
Simon Glass3eb382a2016-09-24 18:20:15 -060063int spl_load_image_fat(struct spl_image_info *spl_image,
Pali Rohárdda8f882022-01-14 14:31:38 +010064 struct spl_boot_device *bootdev,
Simon Glass3eb382a2016-09-24 18:20:15 -060065 struct blk_desc *block_dev, int partition,
66 const char *filename)
Dan Murphyb7b5b0c2014-01-16 11:23:29 -060067{
68 int err;
Simon Glassbb7d3bb2022-09-06 20:26:52 -060069 struct legacy_img_hdr *header;
Dan Murphyb7b5b0c2014-01-16 11:23:29 -060070
71 err = spl_register_fat_device(block_dev, partition);
Dan Murphycb367e12014-01-16 11:23:30 -060072 if (err)
Dan Murphyb7b5b0c2014-01-16 11:23:29 -060073 goto end;
74
Marek Vasut1dda46f2018-08-14 11:27:02 +020075 header = spl_get_load_buffer(-sizeof(*header), sizeof(*header));
Dan Murphyb7b5b0c2014-01-16 11:23:29 -060076
Simon Glassbb7d3bb2022-09-06 20:26:52 -060077 err = file_fat_read(filename, header, sizeof(struct legacy_img_hdr));
Dan Murphyb7b5b0c2014-01-16 11:23:29 -060078 if (err <= 0)
79 goto end;
80
Marek Vasutc659e582018-05-31 17:59:19 +020081 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT_FULL) &&
82 image_get_magic(header) == FDT_MAGIC) {
Sean Anderson5ff77722023-10-14 16:47:55 -040083 err = file_fat_read(filename,
84 map_sysmem(CONFIG_SYS_LOAD_ADDR, 0), 0);
Marek Vasutc659e582018-05-31 17:59:19 +020085 if (err <= 0)
86 goto end;
Pali Rohárdda8f882022-01-14 14:31:38 +010087 err = spl_parse_image_header(spl_image, bootdev,
Sean Anderson5ff77722023-10-14 16:47:55 -040088 map_sysmem(CONFIG_SYS_LOAD_ADDR,
89 err));
Marek Vasutc659e582018-05-31 17:59:19 +020090 if (err == -EAGAIN)
91 return err;
92 if (err == 0)
93 err = 1;
94 } else if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
Lokesh Vutlaa0913a32016-05-24 10:34:39 +053095 image_get_magic(header) == FDT_MAGIC) {
96 struct spl_load_info load;
Dan Murphyb7b5b0c2014-01-16 11:23:29 -060097
Lokesh Vutlaa0913a32016-05-24 10:34:39 +053098 debug("Found FIT\n");
99 load.read = spl_fit_read;
100 load.bl_len = 1;
101 load.filename = (void *)filename;
102 load.priv = NULL;
103
Simon Glass43a734f2016-09-24 18:20:16 -0600104 return spl_load_simple_fit(spl_image, &load, 0, header);
Lokesh Vutlaa0913a32016-05-24 10:34:39 +0530105 } else {
Pali Rohárdda8f882022-01-14 14:31:38 +0100106 err = spl_parse_image_header(spl_image, bootdev, header);
Lokesh Vutlaa0913a32016-05-24 10:34:39 +0530107 if (err)
108 goto end;
109
Sean Anderson5ff77722023-10-14 16:47:55 -0400110 err = file_fat_read(filename, map_sysmem(spl_image->load_addr,
111 spl_image->size), 0);
Lokesh Vutlaa0913a32016-05-24 10:34:39 +0530112 }
Dan Murphyb7b5b0c2014-01-16 11:23:29 -0600113
114end:
115#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
116 if (err <= 0)
Dan Murphycb367e12014-01-16 11:23:30 -0600117 printf("%s: error reading image %s, err - %d\n",
118 __func__, filename, err);
Dan Murphyb7b5b0c2014-01-16 11:23:29 -0600119#endif
120
121 return (err <= 0);
122}
123
Tom Rinic2e78882021-10-30 23:03:48 -0400124#if CONFIG_IS_ENABLED(OS_BOOT)
Simon Glass3eb382a2016-09-24 18:20:15 -0600125int spl_load_image_fat_os(struct spl_image_info *spl_image,
Pali Rohárdda8f882022-01-14 14:31:38 +0100126 struct spl_boot_device *bootdev,
Simon Glass3eb382a2016-09-24 18:20:15 -0600127 struct blk_desc *block_dev, int partition)
Dan Murphyb7b5b0c2014-01-16 11:23:29 -0600128{
129 int err;
Tom Rini70566b62014-03-28 12:03:42 -0400130 __maybe_unused char *file;
Dan Murphyb7b5b0c2014-01-16 11:23:29 -0600131
132 err = spl_register_fat_device(block_dev, partition);
Dan Murphycb367e12014-01-16 11:23:30 -0600133 if (err)
134 return err;
Dan Murphyb7b5b0c2014-01-16 11:23:29 -0600135
Tom Rini70566b62014-03-28 12:03:42 -0400136#if defined(CONFIG_SPL_ENV_SUPPORT) && defined(CONFIG_SPL_OS_BOOT)
Simon Glass64b723f2017-08-03 12:22:12 -0600137 file = env_get("falcon_args_file");
Tom Rini70566b62014-03-28 12:03:42 -0400138 if (file) {
Simon Glass259cdb42023-09-26 08:14:17 -0600139 err = file_fat_read(file, (void *)CONFIG_SPL_PAYLOAD_ARGS_ADDR, 0);
Tom Rini70566b62014-03-28 12:03:42 -0400140 if (err <= 0) {
141 printf("spl: error reading image %s, err - %d, falling back to default\n",
142 file, err);
143 goto defaults;
144 }
Simon Glass64b723f2017-08-03 12:22:12 -0600145 file = env_get("falcon_image_file");
Tom Rini70566b62014-03-28 12:03:42 -0400146 if (file) {
Pali Rohárdda8f882022-01-14 14:31:38 +0100147 err = spl_load_image_fat(spl_image, bootdev, block_dev,
Simon Glass3eb382a2016-09-24 18:20:15 -0600148 partition, file);
Tom Rini70566b62014-03-28 12:03:42 -0400149 if (err != 0) {
150 puts("spl: falling back to default\n");
151 goto defaults;
152 }
153
154 return 0;
155 } else
156 puts("spl: falcon_image_file not set in environment, falling back to default\n");
157 } else
158 puts("spl: falcon_args_file not set in environment, falling back to default\n");
159
160defaults:
161#endif
162
Guillaume GARDET602a16c2014-10-15 17:53:11 +0200163 err = file_fat_read(CONFIG_SPL_FS_LOAD_ARGS_NAME,
Simon Glass259cdb42023-09-26 08:14:17 -0600164 (void *)CONFIG_SPL_PAYLOAD_ARGS_ADDR, 0);
Dan Murphyb7b5b0c2014-01-16 11:23:29 -0600165 if (err <= 0) {
166#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Dan Murphycb367e12014-01-16 11:23:30 -0600167 printf("%s: error reading image %s, err - %d\n",
Guillaume GARDET602a16c2014-10-15 17:53:11 +0200168 __func__, CONFIG_SPL_FS_LOAD_ARGS_NAME, err);
Dan Murphyb7b5b0c2014-01-16 11:23:29 -0600169#endif
170 return -1;
171 }
172
Pali Rohárdda8f882022-01-14 14:31:38 +0100173 return spl_load_image_fat(spl_image, bootdev, block_dev, partition,
Guillaume GARDET602a16c2014-10-15 17:53:11 +0200174 CONFIG_SPL_FS_LOAD_KERNEL_NAME);
Dan Murphyb7b5b0c2014-01-16 11:23:29 -0600175}
Nikita Kiryanovcc49f552015-11-08 17:11:45 +0200176#else
Simon Glass3eb382a2016-09-24 18:20:15 -0600177int spl_load_image_fat_os(struct spl_image_info *spl_image,
Pali Rohárdda8f882022-01-14 14:31:38 +0100178 struct spl_boot_device *bootdev,
Simon Glass3eb382a2016-09-24 18:20:15 -0600179 struct blk_desc *block_dev, int partition)
Nikita Kiryanovcc49f552015-11-08 17:11:45 +0200180{
181 return -ENOSYS;
182}
Dan Murphyb7b5b0c2014-01-16 11:23:29 -0600183#endif