]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
57210c7c MS |
2 | /* |
3 | * (c) Copyright 2011 by Tigris Elektronik GmbH | |
4 | * | |
5 | * Author: | |
6 | * Maximilian Schwerin <[email protected]> | |
57210c7c MS |
7 | */ |
8 | ||
9 | #include <common.h> | |
57210c7c | 10 | #include <command.h> |
0ac7d722 | 11 | #include <env.h> |
f3998fdc | 12 | #include <env_internal.h> |
e6f6f9e6 | 13 | #include <part.h> |
57210c7c | 14 | #include <malloc.h> |
cf92e05c | 15 | #include <memalign.h> |
57210c7c MS |
16 | #include <search.h> |
17 | #include <errno.h> | |
18 | #include <fat.h> | |
19 | #include <mmc.h> | |
54ee5ae8 | 20 | #include <scsi.h> |
e6f6f9e6 | 21 | #include <asm/cache.h> |
2339f01a | 22 | #include <asm/global_data.h> |
e6f6f9e6 | 23 | #include <linux/stddef.h> |
57210c7c | 24 | |
4415f1d1 SG |
25 | #ifdef CONFIG_SPL_BUILD |
26 | /* TODO([email protected]): 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 | |
4415f1d1 SG |
32 | #endif |
33 | ||
2339f01a BM |
34 | DECLARE_GLOBAL_DATA_PTR; |
35 | ||
eb68ead2 HY |
36 | __weak const char *env_fat_get_intf(void) |
37 | { | |
38 | return (const char *)CONFIG_ENV_FAT_INTERFACE; | |
39 | } | |
40 | ||
41 | __weak char *env_fat_get_dev_part(void) | |
6731bef6 DW |
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 | ||
e5bce247 | 60 | static int env_fat_save(void) |
57210c7c | 61 | { |
cda87ec5 | 62 | env_t __aligned(ARCH_DMA_MINALIGN) env_new; |
4101f687 | 63 | struct blk_desc *dev_desc = NULL; |
0528979f | 64 | struct disk_partition info; |
2339f01a | 65 | const char *file = CONFIG_ENV_FAT_FILE; |
be354c1a | 66 | int dev, part; |
9aa90c1d | 67 | int err; |
1ad0b98a | 68 | loff_t size; |
eb68ead2 HY |
69 | const char *ifname = env_fat_get_intf(); |
70 | const char *dev_and_part = env_fat_get_dev_part(); | |
57210c7c | 71 | |
7ce1526e MV |
72 | err = env_export(&env_new); |
73 | if (err) | |
74 | return err; | |
57210c7c | 75 | |
eb68ead2 HY |
76 | part = blk_get_device_part_str(ifname, dev_and_part, |
77 | &dev_desc, &info, 1); | |
be354c1a | 78 | if (part < 0) |
57210c7c | 79 | return 1; |
9aa90c1d | 80 | |
bcce53d0 | 81 | dev = dev_desc->devnum; |
be354c1a | 82 | if (fat_set_blk_dev(dev_desc, &info) != 0) { |
d0816da5 MR |
83 | /* |
84 | * This printf is embedded in the messages from env_save that | |
85 | * will calling it. The missing \n is intentional. | |
86 | */ | |
eb68ead2 | 87 | printf("Unable to use %s %d:%d...\n", ifname, dev, part); |
57210c7c MS |
88 | return 1; |
89 | } | |
90 | ||
2339f01a BM |
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); | |
9aa90c1d | 97 | if (err == -1) { |
d0816da5 MR |
98 | /* |
99 | * This printf is embedded in the messages from env_save that | |
100 | * will calling it. The missing \n is intentional. | |
101 | */ | |
eb68ead2 | 102 | printf("Unable to write \"%s\" from %s%d:%d...\n", file, ifname, dev, part); |
57210c7c MS |
103 | return 1; |
104 | } | |
105 | ||
2339f01a BM |
106 | #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT |
107 | gd->env_valid = (gd->env_valid == ENV_REDUND) ? ENV_VALID : ENV_REDUND; | |
108 | #endif | |
109 | ||
57210c7c MS |
110 | return 0; |
111 | } | |
57210c7c | 112 | |
4415f1d1 | 113 | #ifdef LOADENV |
c5951991 | 114 | static int env_fat_load(void) |
57210c7c | 115 | { |
2339f01a BM |
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 | |
4101f687 | 121 | struct blk_desc *dev_desc = NULL; |
0528979f | 122 | struct disk_partition info; |
be354c1a | 123 | int dev, part; |
2339f01a | 124 | int err1; |
eb68ead2 HY |
125 | const char *ifname = env_fat_get_intf(); |
126 | const char *dev_and_part = env_fat_get_dev_part(); | |
57210c7c | 127 | |
95058fbb | 128 | #ifdef CONFIG_MMC |
eb68ead2 | 129 | if (!strcmp(ifname, "mmc")) |
26862b4a | 130 | mmc_initialize(NULL); |
95058fbb | 131 | #endif |
54ee5ae8 RS |
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 | |
eb68ead2 HY |
138 | part = blk_get_device_part_str(ifname, dev_and_part, |
139 | &dev_desc, &info, 1); | |
be354c1a WJ |
140 | if (part < 0) |
141 | goto err_env_relocate; | |
142 | ||
bcce53d0 | 143 | dev = dev_desc->devnum; |
be354c1a | 144 | if (fat_set_blk_dev(dev_desc, &info) != 0) { |
d0816da5 MR |
145 | /* |
146 | * This printf is embedded in the messages from env_save that | |
147 | * will calling it. The missing \n is intentional. | |
148 | */ | |
eb68ead2 | 149 | printf("Unable to use %s %d:%d...\n", ifname, dev, part); |
be354c1a | 150 | goto err_env_relocate; |
57210c7c MS |
151 | } |
152 | ||
2339f01a BM |
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) { | |
d0816da5 MR |
162 | /* |
163 | * This printf is embedded in the messages from env_save that | |
164 | * will calling it. The missing \n is intentional. | |
165 | */ | |
f6f27be1 | 166 | printf("Unable to read \"%s\" from %s%d:%d... \n", |
eb68ead2 | 167 | CONFIG_ENV_FAT_FILE, ifname, dev, part); |
be354c1a | 168 | goto err_env_relocate; |
57210c7c MS |
169 | } |
170 | ||
2339f01a BM |
171 | return env_import(buf1, 1, H_EXTERNAL); |
172 | #endif | |
be354c1a WJ |
173 | |
174 | err_env_relocate: | |
0ac7d722 | 175 | env_set_default(NULL, 0); |
c5951991 SG |
176 | |
177 | return -EIO; | |
57210c7c | 178 | } |
4415f1d1 SG |
179 | #endif /* LOADENV */ |
180 | ||
181 | U_BOOT_ENV_LOCATION(fat) = { | |
182 | .location = ENVL_FAT, | |
ac358beb | 183 | ENV_NAME("FAT") |
4415f1d1 | 184 | #ifdef LOADENV |
e5bce247 | 185 | .load = env_fat_load, |
4415f1d1 | 186 | #endif |
3908bc93 | 187 | .save = ENV_SAVE_PTR(env_fat_save), |
4415f1d1 | 188 | }; |