]> Git Repo - u-boot.git/blame - common/spl/spl_fat.c
Merge patch series "spl: Use common function for loading/parsing images"
[u-boot.git] / common / spl / spl_fat.c
CommitLineData
83d290c5 1// SPDX-License-Identifier: GPL-2.0+
773b5940
DM
2/*
3 * (C) Copyright 2014
4 * Texas Instruments, <www.ti.com>
5 *
6 * Dan Murphy <[email protected]>
7 *
773b5940
DM
8 * FAT Image Functions copied from spl_mmc.c
9 */
10
11#include <common.h>
7b51b576 12#include <env.h>
f7ae49fc 13#include <log.h>
773b5940 14#include <spl.h>
682184e9 15#include <spl_load.h>
773b5940
DM
16#include <asm/u-boot.h>
17#include <fat.h>
339245b7 18#include <errno.h>
773b5940 19#include <image.h>
b08c8c48 20#include <linux/libfdt.h>
773b5940
DM
21
22static int fat_registered;
23
93caa3ef
SA
24void spl_fat_force_reregister(void)
25{
26 fat_registered = 0;
27}
28
4101f687 29static int spl_register_fat_device(struct blk_desc *block_dev, int partition)
773b5940
DM
30{
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
8cffe5bd 39 printf("%s: fat register err - %d\n", __func__, err);
773b5940 40#endif
7d2b4e77 41 return err;
773b5940
DM
42 }
43
44 fat_registered = 1;
45
46 return err;
47}
48
97ca364f
LV
49static 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;
afdd2d98 54 char *filename = load->priv;
97ca364f
LV
55
56 ret = fat_read_file(filename, buf, file_offset, size, &actread);
57 if (ret)
58 return ret;
59
60 return actread;
61}
62
710e9ca5 63int spl_load_image_fat(struct spl_image_info *spl_image,
2e0429bc 64 struct spl_boot_device *bootdev,
710e9ca5
SG
65 struct blk_desc *block_dev, int partition,
66 const char *filename)
773b5940
DM
67{
68 int err;
682184e9
SA
69 loff_t size;
70 struct spl_load_info load;
773b5940
DM
71
72 err = spl_register_fat_device(block_dev, partition);
8cffe5bd 73 if (err)
773b5940
DM
74 goto end;
75
682184e9
SA
76 /*
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);
97ca364f
LV
82 if (err)
83 goto end;
682184e9
SA
84 } else {
85 size = 0;
97ca364f 86 }
773b5940 87
682184e9 88 load.read = spl_fit_read;
54a8d845
SA
89 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);
682184e9
SA
93 load.priv = (void *)filename;
94 err = spl_load(spl_image, bootdev, &load, size, 0);
95
773b5940
DM
96end:
97#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
682184e9 98 if (err < 0)
8cffe5bd
DM
99 printf("%s: error reading image %s, err - %d\n",
100 __func__, filename, err);
773b5940
DM
101#endif
102
682184e9 103 return err;
773b5940
DM
104}
105
7115007c 106#if CONFIG_IS_ENABLED(OS_BOOT)
710e9ca5 107int spl_load_image_fat_os(struct spl_image_info *spl_image,
2e0429bc 108 struct spl_boot_device *bootdev,
710e9ca5 109 struct blk_desc *block_dev, int partition)
773b5940
DM
110{
111 int err;
ae1590ed 112 __maybe_unused char *file;
773b5940
DM
113
114 err = spl_register_fat_device(block_dev, partition);
8cffe5bd
DM
115 if (err)
116 return err;
773b5940 117
ae1590ed 118#if defined(CONFIG_SPL_ENV_SUPPORT) && defined(CONFIG_SPL_OS_BOOT)
00caae6d 119 file = env_get("falcon_args_file");
ae1590ed 120 if (file) {
9cbdc3a0 121 err = file_fat_read(file, (void *)CONFIG_SPL_PAYLOAD_ARGS_ADDR, 0);
ae1590ed
TR
122 if (err <= 0) {
123 printf("spl: error reading image %s, err - %d, falling back to default\n",
124 file, err);
125 goto defaults;
126 }
00caae6d 127 file = env_get("falcon_image_file");
ae1590ed 128 if (file) {
2e0429bc 129 err = spl_load_image_fat(spl_image, bootdev, block_dev,
710e9ca5 130 partition, file);
ae1590ed
TR
131 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
205b4f33 145 err = file_fat_read(CONFIG_SPL_FS_LOAD_ARGS_NAME,
9cbdc3a0 146 (void *)CONFIG_SPL_PAYLOAD_ARGS_ADDR, 0);
773b5940
DM
147 if (err <= 0) {
148#ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
8cffe5bd 149 printf("%s: error reading image %s, err - %d\n",
205b4f33 150 __func__, CONFIG_SPL_FS_LOAD_ARGS_NAME, err);
773b5940
DM
151#endif
152 return -1;
153 }
154
2e0429bc 155 return spl_load_image_fat(spl_image, bootdev, block_dev, partition,
205b4f33 156 CONFIG_SPL_FS_LOAD_KERNEL_NAME);
773b5940 157}
339245b7 158#else
710e9ca5 159int spl_load_image_fat_os(struct spl_image_info *spl_image,
2e0429bc 160 struct spl_boot_device *bootdev,
710e9ca5 161 struct blk_desc *block_dev, int partition)
339245b7
NK
162{
163 return -ENOSYS;
164}
773b5940 165#endif
This page took 0.282097 seconds and 4 git commands to generate.