Merge http://git.denx.de/u-boot-samsung
[oweals/u-boot.git] / common / spl / spl_fat.c
1 /*
2  * (C) Copyright 2014
3  * Texas Instruments, <www.ti.com>
4  *
5  * Dan Murphy <dmurphy@ti.com>
6  *
7  * SPDX-License-Identifier:     GPL-2.0+
8  *
9  * FAT Image Functions copied from spl_mmc.c
10  */
11
12 #include <common.h>
13 #include <spl.h>
14 #include <asm/u-boot.h>
15 #include <fat.h>
16 #include <errno.h>
17 #include <image.h>
18 #include <libfdt.h>
19
20 static int fat_registered;
21
22 #ifdef CONFIG_SPL_FAT_SUPPORT
23 static int spl_register_fat_device(struct blk_desc *block_dev, int partition)
24 {
25         int err = 0;
26
27         if (fat_registered)
28                 return err;
29
30         err = fat_register_device(block_dev, partition);
31         if (err) {
32 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
33                 printf("%s: fat register err - %d\n", __func__, err);
34 #endif
35                 return err;
36         }
37
38         fat_registered = 1;
39
40         return err;
41 }
42
43 static ulong spl_fit_read(struct spl_load_info *load, ulong file_offset,
44                           ulong size, void *buf)
45 {
46         loff_t actread;
47         int ret;
48         char *filename = (char *)load->filename;
49
50         ret = fat_read_file(filename, buf, file_offset, size, &actread);
51         if (ret)
52                 return ret;
53
54         return actread;
55 }
56
57 int spl_load_image_fat(struct blk_desc *block_dev,
58                                                 int partition,
59                                                 const char *filename)
60 {
61         int err;
62         struct image_header *header;
63
64         err = spl_register_fat_device(block_dev, partition);
65         if (err)
66                 goto end;
67
68         header = (struct image_header *)(CONFIG_SYS_TEXT_BASE -
69                                                 sizeof(struct image_header));
70
71         err = file_fat_read(filename, header, sizeof(struct image_header));
72         if (err <= 0)
73                 goto end;
74
75         if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
76             image_get_magic(header) == FDT_MAGIC) {
77                 struct spl_load_info load;
78
79                 debug("Found FIT\n");
80                 load.read = spl_fit_read;
81                 load.bl_len = 1;
82                 load.filename = (void *)filename;
83                 load.priv = NULL;
84
85                 return spl_load_simple_fit(&load, 0, header);
86         } else {
87                 err = spl_parse_image_header(header);
88                 if (err)
89                         goto end;
90
91                 err = file_fat_read(filename, (u8 *)spl_image.load_addr, 0);
92         }
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 <= 0);
102 }
103
104 #ifdef CONFIG_SPL_OS_BOOT
105 int spl_load_image_fat_os(struct blk_desc *block_dev, int partition)
106 {
107         int err;
108         __maybe_unused char *file;
109
110         err = spl_register_fat_device(block_dev, partition);
111         if (err)
112                 return err;
113
114 #if defined(CONFIG_SPL_ENV_SUPPORT) && defined(CONFIG_SPL_OS_BOOT)
115         file = getenv("falcon_args_file");
116         if (file) {
117                 err = file_fat_read(file, (void *)CONFIG_SYS_SPL_ARGS_ADDR, 0);
118                 if (err <= 0) {
119                         printf("spl: error reading image %s, err - %d, falling back to default\n",
120                                file, err);
121                         goto defaults;
122                 }
123                 file = getenv("falcon_image_file");
124                 if (file) {
125                         err = spl_load_image_fat(block_dev, partition, file);
126                         if (err != 0) {
127                                 puts("spl: falling back to default\n");
128                                 goto defaults;
129                         }
130
131                         return 0;
132                 } else
133                         puts("spl: falcon_image_file not set in environment, falling back to default\n");
134         } else
135                 puts("spl: falcon_args_file not set in environment, falling back to default\n");
136
137 defaults:
138 #endif
139
140         err = file_fat_read(CONFIG_SPL_FS_LOAD_ARGS_NAME,
141                             (void *)CONFIG_SYS_SPL_ARGS_ADDR, 0);
142         if (err <= 0) {
143 #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT
144                 printf("%s: error reading image %s, err - %d\n",
145                        __func__, CONFIG_SPL_FS_LOAD_ARGS_NAME, err);
146 #endif
147                 return -1;
148         }
149
150         return spl_load_image_fat(block_dev, partition,
151                         CONFIG_SPL_FS_LOAD_KERNEL_NAME);
152 }
153 #else
154 int spl_load_image_fat_os(struct blk_desc *block_dev, int partition)
155 {
156         return -ENOSYS;
157 }
158 #endif
159 #endif