blob: 0403016bb433c7bdb2299bdcc215204c6cb6bcec [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>
12#include <spl.h>
13#include <asm/u-boot.h>
14#include <fat.h>
Nikita Kiryanovcc49f552015-11-08 17:11:45 +020015#include <errno.h>
Dan Murphyb7b5b0c2014-01-16 11:23:29 -060016#include <image.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090017#include <linux/libfdt.h>
Dan Murphyb7b5b0c2014-01-16 11:23:29 -060018
19static int fat_registered;
20
Simon Glasse3394752016-02-29 15:25:34 -070021static int spl_register_fat_device(struct blk_desc *block_dev, int partition)
Dan Murphyb7b5b0c2014-01-16 11:23:29 -060022{
23 int err = 0;
24
25 if (fat_registered)
26 return err;
27
28 err = fat_register_device(block_dev, partition);
29 if (err) {
30#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Dan Murphycb367e12014-01-16 11:23:30 -060031 printf("%s: fat register err - %d\n", __func__, err);
Dan Murphyb7b5b0c2014-01-16 11:23:29 -060032#endif
Guillaume GARDETa23ec1d2014-10-15 17:53:14 +020033 return err;
Dan Murphyb7b5b0c2014-01-16 11:23:29 -060034 }
35
36 fat_registered = 1;
37
38 return err;
39}
40
Lokesh Vutlaa0913a32016-05-24 10:34:39 +053041static ulong spl_fit_read(struct spl_load_info *load, ulong file_offset,
42 ulong size, void *buf)
43{
44 loff_t actread;
45 int ret;
46 char *filename = (char *)load->filename;
47
48 ret = fat_read_file(filename, buf, file_offset, size, &actread);
49 if (ret)
50 return ret;
51
52 return actread;
53}
54
Simon Glass3eb382a2016-09-24 18:20:15 -060055int spl_load_image_fat(struct spl_image_info *spl_image,
56 struct blk_desc *block_dev, int partition,
57 const char *filename)
Dan Murphyb7b5b0c2014-01-16 11:23:29 -060058{
59 int err;
60 struct image_header *header;
61
62 err = spl_register_fat_device(block_dev, partition);
Dan Murphycb367e12014-01-16 11:23:30 -060063 if (err)
Dan Murphyb7b5b0c2014-01-16 11:23:29 -060064 goto end;
65
66 header = (struct image_header *)(CONFIG_SYS_TEXT_BASE -
67 sizeof(struct image_header));
68
69 err = file_fat_read(filename, header, sizeof(struct image_header));
70 if (err <= 0)
71 goto end;
72
Marek Vasutc659e582018-05-31 17:59:19 +020073 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT_FULL) &&
74 image_get_magic(header) == FDT_MAGIC) {
75 err = file_fat_read(filename, (void *)CONFIG_SYS_LOAD_ADDR, 0);
76 if (err <= 0)
77 goto end;
78 err = spl_parse_image_header(spl_image,
79 (struct image_header *)CONFIG_SYS_LOAD_ADDR);
80 if (err == -EAGAIN)
81 return err;
82 if (err == 0)
83 err = 1;
84 } else if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
Lokesh Vutlaa0913a32016-05-24 10:34:39 +053085 image_get_magic(header) == FDT_MAGIC) {
86 struct spl_load_info load;
Dan Murphyb7b5b0c2014-01-16 11:23:29 -060087
Lokesh Vutlaa0913a32016-05-24 10:34:39 +053088 debug("Found FIT\n");
89 load.read = spl_fit_read;
90 load.bl_len = 1;
91 load.filename = (void *)filename;
92 load.priv = NULL;
93
Simon Glass43a734f2016-09-24 18:20:16 -060094 return spl_load_simple_fit(spl_image, &load, 0, header);
Lokesh Vutlaa0913a32016-05-24 10:34:39 +053095 } else {
Simon Glass3eb382a2016-09-24 18:20:15 -060096 err = spl_parse_image_header(spl_image, header);
Lokesh Vutlaa0913a32016-05-24 10:34:39 +053097 if (err)
98 goto end;
99
Michal Simeka0dfb842016-04-27 16:07:20 +0200100 err = file_fat_read(filename,
Simon Glass3eb382a2016-09-24 18:20:15 -0600101 (u8 *)(uintptr_t)spl_image->load_addr, 0);
Lokesh Vutlaa0913a32016-05-24 10:34:39 +0530102 }
Dan Murphyb7b5b0c2014-01-16 11:23:29 -0600103
104end:
105#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
106 if (err <= 0)
Dan Murphycb367e12014-01-16 11:23:30 -0600107 printf("%s: error reading image %s, err - %d\n",
108 __func__, filename, err);
Dan Murphyb7b5b0c2014-01-16 11:23:29 -0600109#endif
110
111 return (err <= 0);
112}
113
114#ifdef CONFIG_SPL_OS_BOOT
Simon Glass3eb382a2016-09-24 18:20:15 -0600115int spl_load_image_fat_os(struct spl_image_info *spl_image,
116 struct blk_desc *block_dev, int partition)
Dan Murphyb7b5b0c2014-01-16 11:23:29 -0600117{
118 int err;
Tom Rini70566b62014-03-28 12:03:42 -0400119 __maybe_unused char *file;
Dan Murphyb7b5b0c2014-01-16 11:23:29 -0600120
121 err = spl_register_fat_device(block_dev, partition);
Dan Murphycb367e12014-01-16 11:23:30 -0600122 if (err)
123 return err;
Dan Murphyb7b5b0c2014-01-16 11:23:29 -0600124
Tom Rini70566b62014-03-28 12:03:42 -0400125#if defined(CONFIG_SPL_ENV_SUPPORT) && defined(CONFIG_SPL_OS_BOOT)
Simon Glass64b723f2017-08-03 12:22:12 -0600126 file = env_get("falcon_args_file");
Tom Rini70566b62014-03-28 12:03:42 -0400127 if (file) {
128 err = file_fat_read(file, (void *)CONFIG_SYS_SPL_ARGS_ADDR, 0);
129 if (err <= 0) {
130 printf("spl: error reading image %s, err - %d, falling back to default\n",
131 file, err);
132 goto defaults;
133 }
Simon Glass64b723f2017-08-03 12:22:12 -0600134 file = env_get("falcon_image_file");
Tom Rini70566b62014-03-28 12:03:42 -0400135 if (file) {
Simon Glass3eb382a2016-09-24 18:20:15 -0600136 err = spl_load_image_fat(spl_image, block_dev,
137 partition, file);
Tom Rini70566b62014-03-28 12:03:42 -0400138 if (err != 0) {
139 puts("spl: falling back to default\n");
140 goto defaults;
141 }
142
143 return 0;
144 } else
145 puts("spl: falcon_image_file not set in environment, falling back to default\n");
146 } else
147 puts("spl: falcon_args_file not set in environment, falling back to default\n");
148
149defaults:
150#endif
151
Guillaume GARDET602a16c2014-10-15 17:53:11 +0200152 err = file_fat_read(CONFIG_SPL_FS_LOAD_ARGS_NAME,
Dan Murphyb7b5b0c2014-01-16 11:23:29 -0600153 (void *)CONFIG_SYS_SPL_ARGS_ADDR, 0);
154 if (err <= 0) {
155#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Dan Murphycb367e12014-01-16 11:23:30 -0600156 printf("%s: error reading image %s, err - %d\n",
Guillaume GARDET602a16c2014-10-15 17:53:11 +0200157 __func__, CONFIG_SPL_FS_LOAD_ARGS_NAME, err);
Dan Murphyb7b5b0c2014-01-16 11:23:29 -0600158#endif
159 return -1;
160 }
161
Simon Glass3eb382a2016-09-24 18:20:15 -0600162 return spl_load_image_fat(spl_image, block_dev, partition,
Guillaume GARDET602a16c2014-10-15 17:53:11 +0200163 CONFIG_SPL_FS_LOAD_KERNEL_NAME);
Dan Murphyb7b5b0c2014-01-16 11:23:29 -0600164}
Nikita Kiryanovcc49f552015-11-08 17:11:45 +0200165#else
Simon Glass3eb382a2016-09-24 18:20:15 -0600166int spl_load_image_fat_os(struct spl_image_info *spl_image,
167 struct blk_desc *block_dev, int partition)
Nikita Kiryanovcc49f552015-11-08 17:11:45 +0200168{
169 return -ENOSYS;
170}
Dan Murphyb7b5b0c2014-01-16 11:23:29 -0600171#endif