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 <common.h>
10 #include <command.h>
11 #include <env.h>
12 #include <env_internal.h>
13 #include <part.h>
14 #include <malloc.h>
15 #include <memalign.h>
16 #include <search.h>
17 #include <errno.h>
18 #include <fat.h>
19 #include <mmc.h>
20 #include <scsi.h>
21 #include <asm/cache.h>
22 #include <asm/global_data.h>
23 #include <linux/stddef.h>
24 
25 #ifdef CONFIG_SPL_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 	static char *part_str;
45 
46 	if (!part_str) {
47 		part_str = CONFIG_ENV_FAT_DEVICE_AND_PART;
48 		if (!strcmp(CONFIG_ENV_FAT_INTERFACE, "mmc") && part_str[0] == ':') {
49 			part_str = "0" CONFIG_ENV_FAT_DEVICE_AND_PART;
50 			part_str[0] += mmc_get_env_dev();
51 		}
52 	}
53 
54 	return part_str;
55 #else
56 	return CONFIG_ENV_FAT_DEVICE_AND_PART;
57 #endif
58 }
59 
env_fat_save(void)60 static int env_fat_save(void)
61 {
62 	env_t __aligned(ARCH_DMA_MINALIGN) env_new;
63 	struct blk_desc *dev_desc = NULL;
64 	struct disk_partition info;
65 	const char *file = CONFIG_ENV_FAT_FILE;
66 	int dev, part;
67 	int err;
68 	loff_t size;
69 	const char *ifname = env_fat_get_intf();
70 	const char *dev_and_part = env_fat_get_dev_part();
71 
72 	err = env_export(&env_new);
73 	if (err)
74 		return err;
75 
76 	part = blk_get_device_part_str(ifname, dev_and_part,
77 				       &dev_desc, &info, 1);
78 	if (part < 0)
79 		return 1;
80 
81 	dev = dev_desc->devnum;
82 	if (fat_set_blk_dev(dev_desc, &info) != 0) {
83 		/*
84 		 * This printf is embedded in the messages from env_save that
85 		 * will calling it. The missing \n is intentional.
86 		 */
87 		printf("Unable to use %s %d:%d...\n", ifname, dev, part);
88 		return 1;
89 	}
90 
91 #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
92 	if (gd->env_valid == ENV_VALID)
93 		file = CONFIG_ENV_FAT_FILE_REDUND;
94 #endif
95 
96 	err = file_fat_write(file, (void *)&env_new, 0, sizeof(env_t), &size);
97 	if (err == -1) {
98 		/*
99 		 * This printf is embedded in the messages from env_save that
100 		 * will calling it. The missing \n is intentional.
101 		 */
102 		printf("Unable to write \"%s\" from %s%d:%d...\n", file, ifname, dev, part);
103 		return 1;
104 	}
105 
106 #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
107 	gd->env_valid = (gd->env_valid == ENV_REDUND) ? ENV_VALID : ENV_REDUND;
108 #endif
109 
110 	return 0;
111 }
112 
113 #ifdef LOADENV
env_fat_load(void)114 static int env_fat_load(void)
115 {
116 	ALLOC_CACHE_ALIGN_BUFFER(char, buf1, CONFIG_ENV_SIZE);
117 #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
118 	ALLOC_CACHE_ALIGN_BUFFER(char, buf2, CONFIG_ENV_SIZE);
119 	int err2;
120 #endif
121 	struct blk_desc *dev_desc = NULL;
122 	struct disk_partition info;
123 	int dev, part;
124 	int err1;
125 	const char *ifname = env_fat_get_intf();
126 	const char *dev_and_part = env_fat_get_dev_part();
127 
128 #ifdef CONFIG_MMC
129 	if (!strcmp(ifname, "mmc"))
130 		mmc_initialize(NULL);
131 #endif
132 #ifndef CONFIG_SPL_BUILD
133 #if defined(CONFIG_AHCI) || defined(CONFIG_SCSI)
134 	if (!strcmp(CONFIG_ENV_FAT_INTERFACE, "scsi"))
135 		scsi_scan(true);
136 #endif
137 #endif
138 	part = blk_get_device_part_str(ifname, dev_and_part,
139 				       &dev_desc, &info, 1);
140 	if (part < 0)
141 		goto err_env_relocate;
142 
143 	dev = dev_desc->devnum;
144 	if (fat_set_blk_dev(dev_desc, &info) != 0) {
145 		/*
146 		 * This printf is embedded in the messages from env_save that
147 		 * will calling it. The missing \n is intentional.
148 		 */
149 		printf("Unable to use %s %d:%d...\n", ifname, dev, part);
150 		goto err_env_relocate;
151 	}
152 
153 	err1 = file_fat_read(CONFIG_ENV_FAT_FILE, buf1, CONFIG_ENV_SIZE);
154 #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
155 	err2 = file_fat_read(CONFIG_ENV_FAT_FILE_REDUND, buf2, CONFIG_ENV_SIZE);
156 
157 	err1 = (err1 >= 0) ? 0 : -1;
158 	err2 = (err2 >= 0) ? 0 : -1;
159 	return env_import_redund(buf1, err1, buf2, err2, H_EXTERNAL);
160 #else
161 	if (err1 < 0) {
162 		/*
163 		 * This printf is embedded in the messages from env_save that
164 		 * will calling it. The missing \n is intentional.
165 		 */
166 		printf("Unable to read \"%s\" from %s%d:%d... \n",
167 			CONFIG_ENV_FAT_FILE, ifname, dev, part);
168 		goto err_env_relocate;
169 	}
170 
171 	return env_import(buf1, 1, H_EXTERNAL);
172 #endif
173 
174 err_env_relocate:
175 	env_set_default(NULL, 0);
176 
177 	return -EIO;
178 }
179 #endif /* LOADENV */
180 
181 U_BOOT_ENV_LOCATION(fat) = {
182 	.location	= ENVL_FAT,
183 	ENV_NAME("FAT")
184 #ifdef LOADENV
185 	.load		= env_fat_load,
186 #endif
187 	.save		= ENV_SAVE_PTR(env_fat_save),
188 };
189