blob: bd8aab253a9895aab6898734ca11068a79e4059b [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
Simon Glass0af6e2d2019-08-01 09:46:52 -060011#include <env.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Dan Murphyb7b5b0c2014-01-16 11:23:29 -060013#include <spl.h>
Sean Anderson09a46022023-11-08 11:48:49 -050014#include <spl_load.h>
Dan Murphyb7b5b0c2014-01-16 11:23:29 -060015#include <fat.h>
Nikita Kiryanovcc49f552015-11-08 17:11:45 +020016#include <errno.h>
Dan Murphyb7b5b0c2014-01-16 11:23:29 -060017#include <image.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090018#include <linux/libfdt.h>
Dan Murphyb7b5b0c2014-01-16 11:23:29 -060019
20static int fat_registered;
21
Sean Anderson13e6d912023-10-14 16:47:54 -040022void spl_fat_force_reregister(void)
23{
24 fat_registered = 0;
25}
26
Simon Glasse3394752016-02-29 15:25:34 -070027static int spl_register_fat_device(struct blk_desc *block_dev, int partition)
Dan Murphyb7b5b0c2014-01-16 11:23:29 -060028{
29 int err = 0;
30
31 if (fat_registered)
32 return err;
33
34 err = fat_register_device(block_dev, partition);
35 if (err) {
36#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Dan Murphycb367e12014-01-16 11:23:30 -060037 printf("%s: fat register err - %d\n", __func__, err);
Dan Murphyb7b5b0c2014-01-16 11:23:29 -060038#endif
Guillaume GARDETa23ec1d2014-10-15 17:53:14 +020039 return err;
Dan Murphyb7b5b0c2014-01-16 11:23:29 -060040 }
41
42 fat_registered = 1;
43
44 return err;
45}
46
Lokesh Vutlaa0913a32016-05-24 10:34:39 +053047static ulong spl_fit_read(struct spl_load_info *load, ulong file_offset,
48 ulong size, void *buf)
49{
50 loff_t actread;
51 int ret;
Sean Andersonfcb91762023-11-08 11:48:42 -050052 char *filename = load->priv;
Lokesh Vutlaa0913a32016-05-24 10:34:39 +053053
54 ret = fat_read_file(filename, buf, file_offset, size, &actread);
55 if (ret)
56 return ret;
57
58 return actread;
59}
60
Simon Glass3eb382a2016-09-24 18:20:15 -060061int spl_load_image_fat(struct spl_image_info *spl_image,
Pali Rohárdda8f882022-01-14 14:31:38 +010062 struct spl_boot_device *bootdev,
Simon Glass3eb382a2016-09-24 18:20:15 -060063 struct blk_desc *block_dev, int partition,
64 const char *filename)
Dan Murphyb7b5b0c2014-01-16 11:23:29 -060065{
66 int err;
Sean Anderson09a46022023-11-08 11:48:49 -050067 loff_t size;
68 struct spl_load_info load;
Dan Murphyb7b5b0c2014-01-16 11:23:29 -060069
70 err = spl_register_fat_device(block_dev, partition);
Dan Murphycb367e12014-01-16 11:23:30 -060071 if (err)
Dan Murphyb7b5b0c2014-01-16 11:23:29 -060072 goto end;
73
Sean Anderson09a46022023-11-08 11:48:49 -050074 /*
75 * Avoid pulling in this function for other image types since we are
76 * very short on space on some boards.
77 */
78 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT_FULL)) {
79 err = fat_size(filename, &size);
Lokesh Vutlaa0913a32016-05-24 10:34:39 +053080 if (err)
81 goto end;
Sean Anderson09a46022023-11-08 11:48:49 -050082 } else {
83 size = 0;
Lokesh Vutlaa0913a32016-05-24 10:34:39 +053084 }
Dan Murphyb7b5b0c2014-01-16 11:23:29 -060085
Sean Anderson09a46022023-11-08 11:48:49 -050086 load.read = spl_fit_read;
Sean Anderson90251882023-11-08 11:48:58 -050087 if (IS_ENABLED(CONFIG_SPL_FS_FAT_DMA_ALIGN))
88 spl_set_bl_len(&load, ARCH_DMA_MINALIGN);
89 else
90 spl_set_bl_len(&load, 1);
Sean Anderson09a46022023-11-08 11:48:49 -050091 load.priv = (void *)filename;
92 err = spl_load(spl_image, bootdev, &load, size, 0);
93
Dan Murphyb7b5b0c2014-01-16 11:23:29 -060094end:
95#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Sean Anderson09a46022023-11-08 11:48:49 -050096 if (err < 0)
Dan Murphycb367e12014-01-16 11:23:30 -060097 printf("%s: error reading image %s, err - %d\n",
98 __func__, filename, err);
Dan Murphyb7b5b0c2014-01-16 11:23:29 -060099#endif
100
Sean Anderson09a46022023-11-08 11:48:49 -0500101 return err;
Dan Murphyb7b5b0c2014-01-16 11:23:29 -0600102}
103
Tom Rinic2e78882021-10-30 23:03:48 -0400104#if CONFIG_IS_ENABLED(OS_BOOT)
Simon Glass3eb382a2016-09-24 18:20:15 -0600105int spl_load_image_fat_os(struct spl_image_info *spl_image,
Pali Rohárdda8f882022-01-14 14:31:38 +0100106 struct spl_boot_device *bootdev,
Simon Glass3eb382a2016-09-24 18:20:15 -0600107 struct blk_desc *block_dev, int partition)
Dan Murphyb7b5b0c2014-01-16 11:23:29 -0600108{
109 int err;
Tom Rini70566b62014-03-28 12:03:42 -0400110 __maybe_unused char *file;
Dan Murphyb7b5b0c2014-01-16 11:23:29 -0600111
112 err = spl_register_fat_device(block_dev, partition);
Dan Murphycb367e12014-01-16 11:23:30 -0600113 if (err)
114 return err;
Dan Murphyb7b5b0c2014-01-16 11:23:29 -0600115
Tom Rini70566b62014-03-28 12:03:42 -0400116#if defined(CONFIG_SPL_ENV_SUPPORT) && defined(CONFIG_SPL_OS_BOOT)
Simon Glass64b723f2017-08-03 12:22:12 -0600117 file = env_get("falcon_args_file");
Tom Rini70566b62014-03-28 12:03:42 -0400118 if (file) {
Simon Glass259cdb42023-09-26 08:14:17 -0600119 err = file_fat_read(file, (void *)CONFIG_SPL_PAYLOAD_ARGS_ADDR, 0);
Tom Rini70566b62014-03-28 12:03:42 -0400120 if (err <= 0) {
121 printf("spl: error reading image %s, err - %d, falling back to default\n",
122 file, err);
123 goto defaults;
124 }
Simon Glass64b723f2017-08-03 12:22:12 -0600125 file = env_get("falcon_image_file");
Tom Rini70566b62014-03-28 12:03:42 -0400126 if (file) {
Pali Rohárdda8f882022-01-14 14:31:38 +0100127 err = spl_load_image_fat(spl_image, bootdev, block_dev,
Simon Glass3eb382a2016-09-24 18:20:15 -0600128 partition, file);
Tom Rini70566b62014-03-28 12:03:42 -0400129 if (err != 0) {
130 puts("spl: falling back to default\n");
131 goto defaults;
132 }
133
134 return 0;
135 } else
136 puts("spl: falcon_image_file not set in environment, falling back to default\n");
137 } else
138 puts("spl: falcon_args_file not set in environment, falling back to default\n");
139
140defaults:
141#endif
142
Guillaume GARDET602a16c2014-10-15 17:53:11 +0200143 err = file_fat_read(CONFIG_SPL_FS_LOAD_ARGS_NAME,
Simon Glass259cdb42023-09-26 08:14:17 -0600144 (void *)CONFIG_SPL_PAYLOAD_ARGS_ADDR, 0);
Dan Murphyb7b5b0c2014-01-16 11:23:29 -0600145 if (err <= 0) {
146#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Dan Murphycb367e12014-01-16 11:23:30 -0600147 printf("%s: error reading image %s, err - %d\n",
Guillaume GARDET602a16c2014-10-15 17:53:11 +0200148 __func__, CONFIG_SPL_FS_LOAD_ARGS_NAME, err);
Dan Murphyb7b5b0c2014-01-16 11:23:29 -0600149#endif
150 return -1;
151 }
152
Pali Rohárdda8f882022-01-14 14:31:38 +0100153 return spl_load_image_fat(spl_image, bootdev, block_dev, partition,
Guillaume GARDET602a16c2014-10-15 17:53:11 +0200154 CONFIG_SPL_FS_LOAD_KERNEL_NAME);
Dan Murphyb7b5b0c2014-01-16 11:23:29 -0600155}
Nikita Kiryanovcc49f552015-11-08 17:11:45 +0200156#else
Simon Glass3eb382a2016-09-24 18:20:15 -0600157int spl_load_image_fat_os(struct spl_image_info *spl_image,
Pali Rohárdda8f882022-01-14 14:31:38 +0100158 struct spl_boot_device *bootdev,
Simon Glass3eb382a2016-09-24 18:20:15 -0600159 struct blk_desc *block_dev, int partition)
Nikita Kiryanovcc49f552015-11-08 17:11:45 +0200160{
161 return -ENOSYS;
162}
Dan Murphyb7b5b0c2014-01-16 11:23:29 -0600163#endif