blob: 5b270541fcc7f028d5387eebd80a2f90debd3b4c [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Dan Murphy773b5942014-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 Murphy773b5942014-01-16 11:23:29 -06008 * FAT Image Functions copied from spl_mmc.c
9 */
10
11#include <common.h>
Simon Glass7b51b572019-08-01 09:46:52 -060012#include <env.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060013#include <log.h>
Dan Murphy773b5942014-01-16 11:23:29 -060014#include <spl.h>
15#include <asm/u-boot.h>
16#include <fat.h>
Nikita Kiryanov339245b2015-11-08 17:11:45 +020017#include <errno.h>
Dan Murphy773b5942014-01-16 11:23:29 -060018#include <image.h>
Masahiro Yamadab08c8c42018-03-05 01:20:11 +090019#include <linux/libfdt.h>
Dan Murphy773b5942014-01-16 11:23:29 -060020
21static int fat_registered;
22
Simon Glass4101f682016-02-29 15:25:34 -070023static int spl_register_fat_device(struct blk_desc *block_dev, int partition)
Dan Murphy773b5942014-01-16 11:23:29 -060024{
25 int err = 0;
26
27 if (fat_registered)
28 return err;
29
30 err = fat_register_device(block_dev, partition);
31 if (err) {
32#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Dan Murphy8cffe5b2014-01-16 11:23:30 -060033 printf("%s: fat register err - %d\n", __func__, err);
Dan Murphy773b5942014-01-16 11:23:29 -060034#endif
Guillaume GARDET7d2b4e72014-10-15 17:53:14 +020035 return err;
Dan Murphy773b5942014-01-16 11:23:29 -060036 }
37
38 fat_registered = 1;
39
40 return err;
41}
42
Lokesh Vutla97ca3642016-05-24 10:34:39 +053043static ulong spl_fit_read(struct spl_load_info *load, ulong file_offset,
44 ulong size, void *buf)
45{
46 loff_t actread;
47 int ret;
48 char *filename = (char *)load->filename;
49
50 ret = fat_read_file(filename, buf, file_offset, size, &actread);
51 if (ret)
52 return ret;
53
54 return actread;
55}
56
Simon Glass710e9ca2016-09-24 18:20:15 -060057int spl_load_image_fat(struct spl_image_info *spl_image,
Pali Rohár2e0429b2022-01-14 14:31:38 +010058 struct spl_boot_device *bootdev,
Simon Glass710e9ca2016-09-24 18:20:15 -060059 struct blk_desc *block_dev, int partition,
60 const char *filename)
Dan Murphy773b5942014-01-16 11:23:29 -060061{
62 int err;
63 struct image_header *header;
64
65 err = spl_register_fat_device(block_dev, partition);
Dan Murphy8cffe5b2014-01-16 11:23:30 -060066 if (err)
Dan Murphy773b5942014-01-16 11:23:29 -060067 goto end;
68
Marek Vasut04ce5422018-08-14 11:27:02 +020069 header = spl_get_load_buffer(-sizeof(*header), sizeof(*header));
Dan Murphy773b5942014-01-16 11:23:29 -060070
71 err = file_fat_read(filename, header, sizeof(struct image_header));
72 if (err <= 0)
73 goto end;
74
Marek Vasut8b1531f2018-05-31 17:59:19 +020075 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT_FULL) &&
76 image_get_magic(header) == FDT_MAGIC) {
77 err = file_fat_read(filename, (void *)CONFIG_SYS_LOAD_ADDR, 0);
78 if (err <= 0)
79 goto end;
Pali Rohár2e0429b2022-01-14 14:31:38 +010080 err = spl_parse_image_header(spl_image, bootdev,
Marek Vasut8b1531f2018-05-31 17:59:19 +020081 (struct image_header *)CONFIG_SYS_LOAD_ADDR);
82 if (err == -EAGAIN)
83 return err;
84 if (err == 0)
85 err = 1;
86 } else if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
Lokesh Vutla97ca3642016-05-24 10:34:39 +053087 image_get_magic(header) == FDT_MAGIC) {
88 struct spl_load_info load;
Dan Murphy773b5942014-01-16 11:23:29 -060089
Lokesh Vutla97ca3642016-05-24 10:34:39 +053090 debug("Found FIT\n");
91 load.read = spl_fit_read;
92 load.bl_len = 1;
93 load.filename = (void *)filename;
94 load.priv = NULL;
95
Simon Glassf4d7d852016-09-24 18:20:16 -060096 return spl_load_simple_fit(spl_image, &load, 0, header);
Lokesh Vutla97ca3642016-05-24 10:34:39 +053097 } else {
Pali Rohár2e0429b2022-01-14 14:31:38 +010098 err = spl_parse_image_header(spl_image, bootdev, header);
Lokesh Vutla97ca3642016-05-24 10:34:39 +053099 if (err)
100 goto end;
101
Michal Simek1eefe142016-04-27 16:07:20 +0200102 err = file_fat_read(filename,
Simon Glass710e9ca2016-09-24 18:20:15 -0600103 (u8 *)(uintptr_t)spl_image->load_addr, 0);
Lokesh Vutla97ca3642016-05-24 10:34:39 +0530104 }
Dan Murphy773b5942014-01-16 11:23:29 -0600105
106end:
107#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
108 if (err <= 0)
Dan Murphy8cffe5b2014-01-16 11:23:30 -0600109 printf("%s: error reading image %s, err - %d\n",
110 __func__, filename, err);
Dan Murphy773b5942014-01-16 11:23:29 -0600111#endif
112
113 return (err <= 0);
114}
115
Tom Rini71150072021-10-30 23:03:48 -0400116#if CONFIG_IS_ENABLED(OS_BOOT)
Simon Glass710e9ca2016-09-24 18:20:15 -0600117int spl_load_image_fat_os(struct spl_image_info *spl_image,
Pali Rohár2e0429b2022-01-14 14:31:38 +0100118 struct spl_boot_device *bootdev,
Simon Glass710e9ca2016-09-24 18:20:15 -0600119 struct blk_desc *block_dev, int partition)
Dan Murphy773b5942014-01-16 11:23:29 -0600120{
121 int err;
Tom Riniae1590e2014-03-28 12:03:42 -0400122 __maybe_unused char *file;
Dan Murphy773b5942014-01-16 11:23:29 -0600123
124 err = spl_register_fat_device(block_dev, partition);
Dan Murphy8cffe5b2014-01-16 11:23:30 -0600125 if (err)
126 return err;
Dan Murphy773b5942014-01-16 11:23:29 -0600127
Tom Riniae1590e2014-03-28 12:03:42 -0400128#if defined(CONFIG_SPL_ENV_SUPPORT) && defined(CONFIG_SPL_OS_BOOT)
Simon Glass00caae62017-08-03 12:22:12 -0600129 file = env_get("falcon_args_file");
Tom Riniae1590e2014-03-28 12:03:42 -0400130 if (file) {
131 err = file_fat_read(file, (void *)CONFIG_SYS_SPL_ARGS_ADDR, 0);
132 if (err <= 0) {
133 printf("spl: error reading image %s, err - %d, falling back to default\n",
134 file, err);
135 goto defaults;
136 }
Simon Glass00caae62017-08-03 12:22:12 -0600137 file = env_get("falcon_image_file");
Tom Riniae1590e2014-03-28 12:03:42 -0400138 if (file) {
Pali Rohár2e0429b2022-01-14 14:31:38 +0100139 err = spl_load_image_fat(spl_image, bootdev, block_dev,
Simon Glass710e9ca2016-09-24 18:20:15 -0600140 partition, file);
Tom Riniae1590e2014-03-28 12:03:42 -0400141 if (err != 0) {
142 puts("spl: falling back to default\n");
143 goto defaults;
144 }
145
146 return 0;
147 } else
148 puts("spl: falcon_image_file not set in environment, falling back to default\n");
149 } else
150 puts("spl: falcon_args_file not set in environment, falling back to default\n");
151
152defaults:
153#endif
154
Guillaume GARDET205b4f32014-10-15 17:53:11 +0200155 err = file_fat_read(CONFIG_SPL_FS_LOAD_ARGS_NAME,
Dan Murphy773b5942014-01-16 11:23:29 -0600156 (void *)CONFIG_SYS_SPL_ARGS_ADDR, 0);
157 if (err <= 0) {
158#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Dan Murphy8cffe5b2014-01-16 11:23:30 -0600159 printf("%s: error reading image %s, err - %d\n",
Guillaume GARDET205b4f32014-10-15 17:53:11 +0200160 __func__, CONFIG_SPL_FS_LOAD_ARGS_NAME, err);
Dan Murphy773b5942014-01-16 11:23:29 -0600161#endif
162 return -1;
163 }
164
Pali Rohár2e0429b2022-01-14 14:31:38 +0100165 return spl_load_image_fat(spl_image, bootdev, block_dev, partition,
Guillaume GARDET205b4f32014-10-15 17:53:11 +0200166 CONFIG_SPL_FS_LOAD_KERNEL_NAME);
Dan Murphy773b5942014-01-16 11:23:29 -0600167}
Nikita Kiryanov339245b2015-11-08 17:11:45 +0200168#else
Simon Glass710e9ca2016-09-24 18:20:15 -0600169int spl_load_image_fat_os(struct spl_image_info *spl_image,
Pali Rohár2e0429b2022-01-14 14:31:38 +0100170 struct spl_boot_device *bootdev,
Simon Glass710e9ca2016-09-24 18:20:15 -0600171 struct blk_desc *block_dev, int partition)
Nikita Kiryanov339245b2015-11-08 17:11:45 +0200172{
173 return -ENOSYS;
174}
Dan Murphy773b5942014-01-16 11:23:29 -0600175#endif