1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * (c) Copyright 2011 by Tigris Elektronik GmbH
4 *
5 * Author:
6 * Maximilian Schwerin <mvs@tigris.de>
7 */
8
9 #include <command.h>
10 #include <env.h>
11 #include <env_internal.h>
12 #include <part.h>
13 #include <malloc.h>
14 #include <memalign.h>
15 #include <search.h>
16 #include <errno.h>
17 #include <fat.h>
18 #include <mmc.h>
19 #include <scsi.h>
20 #include <virtio.h>
21 #include <asm/cache.h>
22 #include <asm/global_data.h>
23 #include <linux/stddef.h>
24
25 #ifdef CONFIG_XPL_BUILD
26 /* TODO(sjg@chromium.org): Figure out why this is needed */
27 # if !defined(CONFIG_TARGET_AM335X_EVM) || defined(CONFIG_SPL_OS_BOOT)
28 # define LOADENV
29 # endif
30 #else
31 # define LOADENV
32 #endif
33
34 DECLARE_GLOBAL_DATA_PTR;
35
env_fat_get_intf(void)36 __weak const char *env_fat_get_intf(void)
37 {
38 return (const char *)CONFIG_ENV_FAT_INTERFACE;
39 }
40
env_fat_get_dev_part(void)41 __weak char *env_fat_get_dev_part(void)
42 {
43 #ifdef CONFIG_MMC
44 /* reserve one more char for the manipulation below */
45 static char part_str[] = CONFIG_ENV_FAT_DEVICE_AND_PART "\0";
46
47 if (!strcmp(CONFIG_ENV_FAT_INTERFACE, "mmc") && part_str[0] == ':') {
48 part_str[0] = '0' + mmc_get_env_dev();
49 strcpy(&part_str[1], CONFIG_ENV_FAT_DEVICE_AND_PART);
50 }
51
52 return part_str;
53 #else
54 return CONFIG_ENV_FAT_DEVICE_AND_PART;
55 #endif
56 }
57
env_fat_save(void)58 static int env_fat_save(void)
59 {
60 env_t __aligned(ARCH_DMA_MINALIGN) env_new;
61 struct blk_desc *dev_desc = NULL;
62 struct disk_partition info;
63 const char *file = CONFIG_ENV_FAT_FILE;
64 int dev, part;
65 int err;
66 loff_t size;
67 const char *ifname = env_fat_get_intf();
68 const char *dev_and_part = env_fat_get_dev_part();
69
70 err = env_export(&env_new);
71 if (err)
72 return err;
73
74 part = blk_get_device_part_str(ifname, dev_and_part,
75 &dev_desc, &info, 1);
76 if (part < 0)
77 return 1;
78
79 dev = dev_desc->devnum;
80 if (fat_set_blk_dev(dev_desc, &info) != 0) {
81 /*
82 * This printf is embedded in the messages from env_save that
83 * will calling it. The missing \n is intentional.
84 */
85 printf("Unable to use %s %d:%d...\n", ifname, dev, part);
86 return 1;
87 }
88
89 #ifdef CONFIG_ENV_REDUNDANT
90 if (gd->env_valid == ENV_VALID)
91 file = CONFIG_ENV_FAT_FILE_REDUND;
92 #endif
93
94 err = file_fat_write(file, (void *)&env_new, 0, sizeof(env_t), &size);
95 if (err == -1) {
96 /*
97 * This printf is embedded in the messages from env_save that
98 * will calling it. The missing \n is intentional.
99 */
100 printf("Unable to write \"%s\" from %s%d:%d...\n", file, ifname, dev, part);
101 return 1;
102 }
103
104 #ifdef CONFIG_ENV_REDUNDANT
105 gd->env_valid = (gd->env_valid == ENV_REDUND) ? ENV_VALID : ENV_REDUND;
106 #endif
107
108 return 0;
109 }
110
111 #ifdef LOADENV
env_fat_load(void)112 static int env_fat_load(void)
113 {
114 ALLOC_CACHE_ALIGN_BUFFER(char, buf1, CONFIG_ENV_SIZE);
115 #ifdef CONFIG_ENV_REDUNDANT
116 ALLOC_CACHE_ALIGN_BUFFER(char, buf2, CONFIG_ENV_SIZE);
117 int err2;
118 #endif
119 struct blk_desc *dev_desc = NULL;
120 struct disk_partition info;
121 int dev, part;
122 int err1;
123 const char *ifname = env_fat_get_intf();
124 const char *dev_and_part = env_fat_get_dev_part();
125
126 #ifdef CONFIG_MMC
127 if (!strcmp(ifname, "mmc"))
128 mmc_initialize(NULL);
129 #endif
130 #ifndef CONFIG_XPL_BUILD
131 #if defined(CONFIG_AHCI) || defined(CONFIG_SCSI)
132 if (!strcmp(CONFIG_ENV_FAT_INTERFACE, "scsi"))
133 scsi_scan(true);
134 #endif
135 #if defined(CONFIG_VIRTIO)
136 if (!strcmp(ifname, "virtio"))
137 virtio_init();
138 #endif
139 #endif
140 part = blk_get_device_part_str(ifname, dev_and_part,
141 &dev_desc, &info, 1);
142 if (part < 0)
143 goto err_env_relocate;
144
145 dev = dev_desc->devnum;
146 if (fat_set_blk_dev(dev_desc, &info) != 0) {
147 /*
148 * This printf is embedded in the messages from env_save that
149 * will calling it. The missing \n is intentional.
150 */
151 printf("Unable to use %s %d:%d...\n", ifname, dev, part);
152 goto err_env_relocate;
153 }
154
155 err1 = file_fat_read(CONFIG_ENV_FAT_FILE, buf1, CONFIG_ENV_SIZE);
156 #ifdef CONFIG_ENV_REDUNDANT
157 err2 = file_fat_read(CONFIG_ENV_FAT_FILE_REDUND, buf2, CONFIG_ENV_SIZE);
158
159 err1 = (err1 >= 0) ? 0 : -1;
160 err2 = (err2 >= 0) ? 0 : -1;
161 return env_import_redund(buf1, err1, buf2, err2, H_EXTERNAL);
162 #else
163 if (err1 < 0) {
164 /*
165 * This printf is embedded in the messages from env_save that
166 * will calling it. The missing \n is intentional.
167 */
168 printf("Unable to read \"%s\" from %s%d:%d... \n",
169 CONFIG_ENV_FAT_FILE, ifname, dev, part);
170 goto err_env_relocate;
171 }
172
173 return env_import(buf1, 1, H_EXTERNAL);
174 #endif
175
176 err_env_relocate:
177 env_set_default(NULL, 0);
178
179 return -EIO;
180 }
181 #endif /* LOADENV */
182
183 U_BOOT_ENV_LOCATION(fat) = {
184 .location = ENVL_FAT,
185 ENV_NAME("FAT")
186 #ifdef LOADENV
187 .load = env_fat_load,
188 #endif
189 .save = ENV_SAVE_PTR(env_fat_save),
190 };
191