]> Git Repo - u-boot.git/blob - common/spl/spl_fat.c
global: Make <asm/global_data.h> include <asm/u-boot.h>
[u-boot.git] / common / spl / spl_fat.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2014
4  * Texas Instruments, <www.ti.com>
5  *
6  * Dan Murphy <[email protected]>
7  *
8  * FAT Image Functions copied from spl_mmc.c
9  */
10
11 #include <env.h>
12 #include <log.h>
13 #include <spl.h>
14 #include <spl_load.h>
15 #include <fat.h>
16 #include <errno.h>
17 #include <image.h>
18 #include <linux/libfdt.h>
19
20 static int fat_registered;
21
22 void spl_fat_force_reregister(void)
23 {
24         fat_registered = 0;
25 }
26
27 static int spl_register_fat_device(struct blk_desc *block_dev, int partition)
28 {
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
37                 printf("%s: fat register err - %d\n", __func__, err);
38 #endif
39                 return err;
40         }
41
42         fat_registered = 1;
43
44         return err;
45 }
46
47 static 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;
52         char *filename = load->priv;
53
54         ret = fat_read_file(filename, buf, file_offset, size, &actread);
55         if (ret)
56                 return ret;
57
58         return actread;
59 }
60
61 int spl_load_image_fat(struct spl_image_info *spl_image,
62                        struct spl_boot_device *bootdev,
63                        struct blk_desc *block_dev, int partition,
64                        const char *filename)
65 {
66         int err;
67         loff_t size;
68         struct spl_load_info load;
69
70         err = spl_register_fat_device(block_dev, partition);
71         if (err)
72                 goto end;
73
74         /*
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);
80                 if (err)
81                         goto end;
82         } else {
83                 size = 0;
84         }
85
86         load.read = spl_fit_read;
87         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);
91         load.priv = (void *)filename;
92         err = spl_load(spl_image, bootdev, &load, size, 0);
93
94 end:
95 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
96         if (err < 0)
97                 printf("%s: error reading image %s, err - %d\n",
98                        __func__, filename, err);
99 #endif
100
101         return err;
102 }
103
104 #if CONFIG_IS_ENABLED(OS_BOOT)
105 int spl_load_image_fat_os(struct spl_image_info *spl_image,
106                           struct spl_boot_device *bootdev,
107                           struct blk_desc *block_dev, int partition)
108 {
109         int err;
110         __maybe_unused char *file;
111
112         err = spl_register_fat_device(block_dev, partition);
113         if (err)
114                 return err;
115
116 #if defined(CONFIG_SPL_ENV_SUPPORT) && defined(CONFIG_SPL_OS_BOOT)
117         file = env_get("falcon_args_file");
118         if (file) {
119                 err = file_fat_read(file, (void *)CONFIG_SPL_PAYLOAD_ARGS_ADDR, 0);
120                 if (err <= 0) {
121                         printf("spl: error reading image %s, err - %d, falling back to default\n",
122                                file, err);
123                         goto defaults;
124                 }
125                 file = env_get("falcon_image_file");
126                 if (file) {
127                         err = spl_load_image_fat(spl_image, bootdev, block_dev,
128                                                  partition, file);
129                         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
140 defaults:
141 #endif
142
143         err = file_fat_read(CONFIG_SPL_FS_LOAD_ARGS_NAME,
144                             (void *)CONFIG_SPL_PAYLOAD_ARGS_ADDR, 0);
145         if (err <= 0) {
146 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
147                 printf("%s: error reading image %s, err - %d\n",
148                        __func__, CONFIG_SPL_FS_LOAD_ARGS_NAME, err);
149 #endif
150                 return -1;
151         }
152
153         return spl_load_image_fat(spl_image, bootdev, block_dev, partition,
154                         CONFIG_SPL_FS_LOAD_KERNEL_NAME);
155 }
156 #else
157 int spl_load_image_fat_os(struct spl_image_info *spl_image,
158                           struct spl_boot_device *bootdev,
159                           struct blk_desc *block_dev, int partition)
160 {
161         return -ENOSYS;
162 }
163 #endif
This page took 0.035605 seconds and 4 git commands to generate.