blob: fce451b7664cca73d33f19d93431fd69d33f9023 [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
Simon Glasse8066862024-08-22 07:55:02 -060086 spl_load_init(&load, spl_fit_read, (void *)filename,
87 IS_ENABLED(CONFIG_SPL_FS_FAT_DMA_ALIGN) ?
88 ARCH_DMA_MINALIGN : 1);
89
Sean Anderson09a46022023-11-08 11:48:49 -050090 err = spl_load(spl_image, bootdev, &load, size, 0);
91
Dan Murphyb7b5b0c2014-01-16 11:23:29 -060092end:
93#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Sean Anderson09a46022023-11-08 11:48:49 -050094 if (err < 0)
Dan Murphycb367e12014-01-16 11:23:30 -060095 printf("%s: error reading image %s, err - %d\n",
96 __func__, filename, err);
Dan Murphyb7b5b0c2014-01-16 11:23:29 -060097#endif
98
Sean Anderson09a46022023-11-08 11:48:49 -050099 return err;
Dan Murphyb7b5b0c2014-01-16 11:23:29 -0600100}
101
Tom Rinic2e78882021-10-30 23:03:48 -0400102#if CONFIG_IS_ENABLED(OS_BOOT)
Simon Glass3eb382a2016-09-24 18:20:15 -0600103int spl_load_image_fat_os(struct spl_image_info *spl_image,
Pali Rohárdda8f882022-01-14 14:31:38 +0100104 struct spl_boot_device *bootdev,
Simon Glass3eb382a2016-09-24 18:20:15 -0600105 struct blk_desc *block_dev, int partition)
Dan Murphyb7b5b0c2014-01-16 11:23:29 -0600106{
107 int err;
Tom Rini70566b62014-03-28 12:03:42 -0400108 __maybe_unused char *file;
Dan Murphyb7b5b0c2014-01-16 11:23:29 -0600109
110 err = spl_register_fat_device(block_dev, partition);
Dan Murphycb367e12014-01-16 11:23:30 -0600111 if (err)
112 return err;
Dan Murphyb7b5b0c2014-01-16 11:23:29 -0600113
Tom Rini70566b62014-03-28 12:03:42 -0400114#if defined(CONFIG_SPL_ENV_SUPPORT) && defined(CONFIG_SPL_OS_BOOT)
Simon Glass64b723f2017-08-03 12:22:12 -0600115 file = env_get("falcon_args_file");
Tom Rini70566b62014-03-28 12:03:42 -0400116 if (file) {
Simon Glass259cdb42023-09-26 08:14:17 -0600117 err = file_fat_read(file, (void *)CONFIG_SPL_PAYLOAD_ARGS_ADDR, 0);
Tom Rini70566b62014-03-28 12:03:42 -0400118 if (err <= 0) {
119 printf("spl: error reading image %s, err - %d, falling back to default\n",
120 file, err);
121 goto defaults;
122 }
Simon Glass64b723f2017-08-03 12:22:12 -0600123 file = env_get("falcon_image_file");
Tom Rini70566b62014-03-28 12:03:42 -0400124 if (file) {
Pali Rohárdda8f882022-01-14 14:31:38 +0100125 err = spl_load_image_fat(spl_image, bootdev, block_dev,
Simon Glass3eb382a2016-09-24 18:20:15 -0600126 partition, file);
Tom Rini70566b62014-03-28 12:03:42 -0400127 if (err != 0) {
128 puts("spl: falling back to default\n");
129 goto defaults;
130 }
131
132 return 0;
133 } else
134 puts("spl: falcon_image_file not set in environment, falling back to default\n");
135 } else
136 puts("spl: falcon_args_file not set in environment, falling back to default\n");
137
138defaults:
139#endif
140
Guillaume GARDET602a16c2014-10-15 17:53:11 +0200141 err = file_fat_read(CONFIG_SPL_FS_LOAD_ARGS_NAME,
Simon Glass259cdb42023-09-26 08:14:17 -0600142 (void *)CONFIG_SPL_PAYLOAD_ARGS_ADDR, 0);
Dan Murphyb7b5b0c2014-01-16 11:23:29 -0600143 if (err <= 0) {
144#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Dan Murphycb367e12014-01-16 11:23:30 -0600145 printf("%s: error reading image %s, err - %d\n",
Guillaume GARDET602a16c2014-10-15 17:53:11 +0200146 __func__, CONFIG_SPL_FS_LOAD_ARGS_NAME, err);
Dan Murphyb7b5b0c2014-01-16 11:23:29 -0600147#endif
148 return -1;
149 }
150
Pali Rohárdda8f882022-01-14 14:31:38 +0100151 return spl_load_image_fat(spl_image, bootdev, block_dev, partition,
Guillaume GARDET602a16c2014-10-15 17:53:11 +0200152 CONFIG_SPL_FS_LOAD_KERNEL_NAME);
Dan Murphyb7b5b0c2014-01-16 11:23:29 -0600153}
Nikita Kiryanovcc49f552015-11-08 17:11:45 +0200154#else
Simon Glass3eb382a2016-09-24 18:20:15 -0600155int spl_load_image_fat_os(struct spl_image_info *spl_image,
Pali Rohárdda8f882022-01-14 14:31:38 +0100156 struct spl_boot_device *bootdev,
Simon Glass3eb382a2016-09-24 18:20:15 -0600157 struct blk_desc *block_dev, int partition)
Nikita Kiryanovcc49f552015-11-08 17:11:45 +0200158{
159 return -ENOSYS;
160}
Dan Murphyb7b5b0c2014-01-16 11:23:29 -0600161#endif