]>
Commit | Line | Data |
---|---|---|
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 | ||
7b51b576 | 11 | #include <env.h> |
f7ae49fc | 12 | #include <log.h> |
773b5940 | 13 | #include <spl.h> |
682184e9 | 14 | #include <spl_load.h> |
773b5940 DM |
15 | #include <asm/u-boot.h> |
16 | #include <fat.h> | |
339245b7 | 17 | #include <errno.h> |
773b5940 | 18 | #include <image.h> |
b08c8c48 | 19 | #include <linux/libfdt.h> |
773b5940 DM |
20 | |
21 | static int fat_registered; | |
22 | ||
93caa3ef SA |
23 | void spl_fat_force_reregister(void) |
24 | { | |
25 | fat_registered = 0; | |
26 | } | |
27 | ||
4101f687 | 28 | static int spl_register_fat_device(struct blk_desc *block_dev, int partition) |
773b5940 DM |
29 | { |
30 | int err = 0; | |
31 | ||
32 | if (fat_registered) | |
33 | return err; | |
34 | ||
35 | err = fat_register_device(block_dev, partition); | |
36 | if (err) { | |
37 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT | |
8cffe5bd | 38 | printf("%s: fat register err - %d\n", __func__, err); |
773b5940 | 39 | #endif |
7d2b4e77 | 40 | return err; |
773b5940 DM |
41 | } |
42 | ||
43 | fat_registered = 1; | |
44 | ||
45 | return err; | |
46 | } | |
47 | ||
97ca364f LV |
48 | static ulong spl_fit_read(struct spl_load_info *load, ulong file_offset, |
49 | ulong size, void *buf) | |
50 | { | |
51 | loff_t actread; | |
52 | int ret; | |
afdd2d98 | 53 | char *filename = load->priv; |
97ca364f LV |
54 | |
55 | ret = fat_read_file(filename, buf, file_offset, size, &actread); | |
56 | if (ret) | |
57 | return ret; | |
58 | ||
59 | return actread; | |
60 | } | |
61 | ||
710e9ca5 | 62 | int spl_load_image_fat(struct spl_image_info *spl_image, |
2e0429bc | 63 | struct spl_boot_device *bootdev, |
710e9ca5 SG |
64 | struct blk_desc *block_dev, int partition, |
65 | const char *filename) | |
773b5940 DM |
66 | { |
67 | int err; | |
682184e9 SA |
68 | loff_t size; |
69 | struct spl_load_info load; | |
773b5940 DM |
70 | |
71 | err = spl_register_fat_device(block_dev, partition); | |
8cffe5bd | 72 | if (err) |
773b5940 DM |
73 | goto end; |
74 | ||
682184e9 SA |
75 | /* |
76 | * Avoid pulling in this function for other image types since we are | |
77 | * very short on space on some boards. | |
78 | */ | |
79 | if (IS_ENABLED(CONFIG_SPL_LOAD_FIT_FULL)) { | |
80 | err = fat_size(filename, &size); | |
97ca364f LV |
81 | if (err) |
82 | goto end; | |
682184e9 SA |
83 | } else { |
84 | size = 0; | |
97ca364f | 85 | } |
773b5940 | 86 | |
682184e9 | 87 | load.read = spl_fit_read; |
54a8d845 SA |
88 | if (IS_ENABLED(CONFIG_SPL_FS_FAT_DMA_ALIGN)) |
89 | spl_set_bl_len(&load, ARCH_DMA_MINALIGN); | |
90 | else | |
91 | spl_set_bl_len(&load, 1); | |
682184e9 SA |
92 | load.priv = (void *)filename; |
93 | err = spl_load(spl_image, bootdev, &load, size, 0); | |
94 | ||
773b5940 DM |
95 | end: |
96 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT | |
682184e9 | 97 | if (err < 0) |
8cffe5bd DM |
98 | printf("%s: error reading image %s, err - %d\n", |
99 | __func__, filename, err); | |
773b5940 DM |
100 | #endif |
101 | ||
682184e9 | 102 | return err; |
773b5940 DM |
103 | } |
104 | ||
7115007c | 105 | #if CONFIG_IS_ENABLED(OS_BOOT) |
710e9ca5 | 106 | int spl_load_image_fat_os(struct spl_image_info *spl_image, |
2e0429bc | 107 | struct spl_boot_device *bootdev, |
710e9ca5 | 108 | struct blk_desc *block_dev, int partition) |
773b5940 DM |
109 | { |
110 | int err; | |
ae1590ed | 111 | __maybe_unused char *file; |
773b5940 DM |
112 | |
113 | err = spl_register_fat_device(block_dev, partition); | |
8cffe5bd DM |
114 | if (err) |
115 | return err; | |
773b5940 | 116 | |
ae1590ed | 117 | #if defined(CONFIG_SPL_ENV_SUPPORT) && defined(CONFIG_SPL_OS_BOOT) |
00caae6d | 118 | file = env_get("falcon_args_file"); |
ae1590ed | 119 | if (file) { |
9cbdc3a0 | 120 | err = file_fat_read(file, (void *)CONFIG_SPL_PAYLOAD_ARGS_ADDR, 0); |
ae1590ed TR |
121 | if (err <= 0) { |
122 | printf("spl: error reading image %s, err - %d, falling back to default\n", | |
123 | file, err); | |
124 | goto defaults; | |
125 | } | |
00caae6d | 126 | file = env_get("falcon_image_file"); |
ae1590ed | 127 | if (file) { |
2e0429bc | 128 | err = spl_load_image_fat(spl_image, bootdev, block_dev, |
710e9ca5 | 129 | partition, file); |
ae1590ed TR |
130 | if (err != 0) { |
131 | puts("spl: falling back to default\n"); | |
132 | goto defaults; | |
133 | } | |
134 | ||
135 | return 0; | |
136 | } else | |
137 | puts("spl: falcon_image_file not set in environment, falling back to default\n"); | |
138 | } else | |
139 | puts("spl: falcon_args_file not set in environment, falling back to default\n"); | |
140 | ||
141 | defaults: | |
142 | #endif | |
143 | ||
205b4f33 | 144 | err = file_fat_read(CONFIG_SPL_FS_LOAD_ARGS_NAME, |
9cbdc3a0 | 145 | (void *)CONFIG_SPL_PAYLOAD_ARGS_ADDR, 0); |
773b5940 DM |
146 | if (err <= 0) { |
147 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT | |
8cffe5bd | 148 | printf("%s: error reading image %s, err - %d\n", |
205b4f33 | 149 | __func__, CONFIG_SPL_FS_LOAD_ARGS_NAME, err); |
773b5940 DM |
150 | #endif |
151 | return -1; | |
152 | } | |
153 | ||
2e0429bc | 154 | return spl_load_image_fat(spl_image, bootdev, block_dev, partition, |
205b4f33 | 155 | CONFIG_SPL_FS_LOAD_KERNEL_NAME); |
773b5940 | 156 | } |
339245b7 | 157 | #else |
710e9ca5 | 158 | int spl_load_image_fat_os(struct spl_image_info *spl_image, |
2e0429bc | 159 | struct spl_boot_device *bootdev, |
710e9ca5 | 160 | struct blk_desc *block_dev, int partition) |
339245b7 NK |
161 | { |
162 | return -ENOSYS; | |
163 | } | |
773b5940 | 164 | #endif |