blob: a52f9e178e664813f0109305ab6e2bfa1ccf7631 [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>
Dan Murphyb7b5b0c2014-01-16 11:23:29 -060014#include <spl.h>
Sean Anderson09a46022023-11-08 11:48:49 -050015#include <spl_load.h>
Dan Murphyb7b5b0c2014-01-16 11:23:29 -060016#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;
Sean Andersonfcb91762023-11-08 11:48:42 -050054 char *filename = load->priv;
Lokesh Vutlaa0913a32016-05-24 10:34:39 +053055
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;
Sean Anderson09a46022023-11-08 11:48:49 -050069 loff_t size;
70 struct spl_load_info load;
Dan Murphyb7b5b0c2014-01-16 11:23:29 -060071
72 err = spl_register_fat_device(block_dev, partition);
Dan Murphycb367e12014-01-16 11:23:30 -060073 if (err)
Dan Murphyb7b5b0c2014-01-16 11:23:29 -060074 goto end;
75
Sean Anderson09a46022023-11-08 11:48:49 -050076 /*
77 * Avoid pulling in this function for other image types since we are
78 * very short on space on some boards.
79 */
80 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT_FULL)) {
81 err = fat_size(filename, &size);
Lokesh Vutlaa0913a32016-05-24 10:34:39 +053082 if (err)
83 goto end;
Sean Anderson09a46022023-11-08 11:48:49 -050084 } else {
85 size = 0;
Lokesh Vutlaa0913a32016-05-24 10:34:39 +053086 }
Dan Murphyb7b5b0c2014-01-16 11:23:29 -060087
Sean Anderson09a46022023-11-08 11:48:49 -050088 load.read = spl_fit_read;
Sean Anderson90251882023-11-08 11:48:58 -050089 if (IS_ENABLED(CONFIG_SPL_FS_FAT_DMA_ALIGN))
90 spl_set_bl_len(&load, ARCH_DMA_MINALIGN);
91 else
92 spl_set_bl_len(&load, 1);
Sean Anderson09a46022023-11-08 11:48:49 -050093 load.priv = (void *)filename;
94 err = spl_load(spl_image, bootdev, &load, size, 0);
95
Dan Murphyb7b5b0c2014-01-16 11:23:29 -060096end:
97#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Sean Anderson09a46022023-11-08 11:48:49 -050098 if (err < 0)
Dan Murphycb367e12014-01-16 11:23:30 -060099 printf("%s: error reading image %s, err - %d\n",
100 __func__, filename, err);
Dan Murphyb7b5b0c2014-01-16 11:23:29 -0600101#endif
102
Sean Anderson09a46022023-11-08 11:48:49 -0500103 return err;
Dan Murphyb7b5b0c2014-01-16 11:23:29 -0600104}
105
Tom Rinic2e78882021-10-30 23:03:48 -0400106#if CONFIG_IS_ENABLED(OS_BOOT)
Simon Glass3eb382a2016-09-24 18:20:15 -0600107int spl_load_image_fat_os(struct spl_image_info *spl_image,
Pali Rohárdda8f882022-01-14 14:31:38 +0100108 struct spl_boot_device *bootdev,
Simon Glass3eb382a2016-09-24 18:20:15 -0600109 struct blk_desc *block_dev, int partition)
Dan Murphyb7b5b0c2014-01-16 11:23:29 -0600110{
111 int err;
Tom Rini70566b62014-03-28 12:03:42 -0400112 __maybe_unused char *file;
Dan Murphyb7b5b0c2014-01-16 11:23:29 -0600113
114 err = spl_register_fat_device(block_dev, partition);
Dan Murphycb367e12014-01-16 11:23:30 -0600115 if (err)
116 return err;
Dan Murphyb7b5b0c2014-01-16 11:23:29 -0600117
Tom Rini70566b62014-03-28 12:03:42 -0400118#if defined(CONFIG_SPL_ENV_SUPPORT) && defined(CONFIG_SPL_OS_BOOT)
Simon Glass64b723f2017-08-03 12:22:12 -0600119 file = env_get("falcon_args_file");
Tom Rini70566b62014-03-28 12:03:42 -0400120 if (file) {
Simon Glass259cdb42023-09-26 08:14:17 -0600121 err = file_fat_read(file, (void *)CONFIG_SPL_PAYLOAD_ARGS_ADDR, 0);
Tom Rini70566b62014-03-28 12:03:42 -0400122 if (err <= 0) {
123 printf("spl: error reading image %s, err - %d, falling back to default\n",
124 file, err);
125 goto defaults;
126 }
Simon Glass64b723f2017-08-03 12:22:12 -0600127 file = env_get("falcon_image_file");
Tom Rini70566b62014-03-28 12:03:42 -0400128 if (file) {
Pali Rohárdda8f882022-01-14 14:31:38 +0100129 err = spl_load_image_fat(spl_image, bootdev, block_dev,
Simon Glass3eb382a2016-09-24 18:20:15 -0600130 partition, file);
Tom Rini70566b62014-03-28 12:03:42 -0400131 if (err != 0) {
132 puts("spl: falling back to default\n");
133 goto defaults;
134 }
135
136 return 0;
137 } else
138 puts("spl: falcon_image_file not set in environment, falling back to default\n");
139 } else
140 puts("spl: falcon_args_file not set in environment, falling back to default\n");
141
142defaults:
143#endif
144
Guillaume GARDET602a16c2014-10-15 17:53:11 +0200145 err = file_fat_read(CONFIG_SPL_FS_LOAD_ARGS_NAME,
Simon Glass259cdb42023-09-26 08:14:17 -0600146 (void *)CONFIG_SPL_PAYLOAD_ARGS_ADDR, 0);
Dan Murphyb7b5b0c2014-01-16 11:23:29 -0600147 if (err <= 0) {
148#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
Dan Murphycb367e12014-01-16 11:23:30 -0600149 printf("%s: error reading image %s, err - %d\n",
Guillaume GARDET602a16c2014-10-15 17:53:11 +0200150 __func__, CONFIG_SPL_FS_LOAD_ARGS_NAME, err);
Dan Murphyb7b5b0c2014-01-16 11:23:29 -0600151#endif
152 return -1;
153 }
154
Pali Rohárdda8f882022-01-14 14:31:38 +0100155 return spl_load_image_fat(spl_image, bootdev, block_dev, partition,
Guillaume GARDET602a16c2014-10-15 17:53:11 +0200156 CONFIG_SPL_FS_LOAD_KERNEL_NAME);
Dan Murphyb7b5b0c2014-01-16 11:23:29 -0600157}
Nikita Kiryanovcc49f552015-11-08 17:11:45 +0200158#else
Simon Glass3eb382a2016-09-24 18:20:15 -0600159int spl_load_image_fat_os(struct spl_image_info *spl_image,
Pali Rohárdda8f882022-01-14 14:31:38 +0100160 struct spl_boot_device *bootdev,
Simon Glass3eb382a2016-09-24 18:20:15 -0600161 struct blk_desc *block_dev, int partition)
Nikita Kiryanovcc49f552015-11-08 17:11:45 +0200162{
163 return -ENOSYS;
164}
Dan Murphyb7b5b0c2014-01-16 11:23:29 -0600165#endif