Merge tag 'u-boot-stm32-20220510' of https://source.denx.de/u-boot/custodians/u-boot-stm
[platform/kernel/u-boot.git] / env / fat.c
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 <asm/cache.h>
21 #include <asm/global_data.h>
22 #include <linux/stddef.h>
23
24 #ifdef CONFIG_SPL_BUILD
25 /* TODO(sjg@chromium.org): Figure out why this is needed */
26 # if !defined(CONFIG_TARGET_AM335X_EVM) || defined(CONFIG_SPL_OS_BOOT)
27 #  define LOADENV
28 # endif
29 #else
30 # define LOADENV
31 #endif
32
33 DECLARE_GLOBAL_DATA_PTR;
34
35 __weak const char *env_fat_get_intf(void)
36 {
37         return (const char *)CONFIG_ENV_FAT_INTERFACE;
38 }
39
40 __weak char *env_fat_get_dev_part(void)
41 {
42 #ifdef CONFIG_MMC
43         static char *part_str;
44
45         if (!part_str) {
46                 part_str = CONFIG_ENV_FAT_DEVICE_AND_PART;
47                 if (!strcmp(CONFIG_ENV_FAT_INTERFACE, "mmc") && part_str[0] == ':') {
48                         part_str = "0" CONFIG_ENV_FAT_DEVICE_AND_PART;
49                         part_str[0] += mmc_get_env_dev();
50                 }
51         }
52
53         return part_str;
54 #else
55         return CONFIG_ENV_FAT_DEVICE_AND_PART;
56 #endif
57 }
58
59 static int env_fat_save(void)
60 {
61         env_t __aligned(ARCH_DMA_MINALIGN) env_new;
62         struct blk_desc *dev_desc = NULL;
63         struct disk_partition info;
64         const char *file = CONFIG_ENV_FAT_FILE;
65         int dev, part;
66         int err;
67         loff_t size;
68         const char *ifname = env_fat_get_intf();
69         const char *dev_and_part = env_fat_get_dev_part();
70
71         err = env_export(&env_new);
72         if (err)
73                 return err;
74
75         part = blk_get_device_part_str(ifname, dev_and_part,
76                                        &dev_desc, &info, 1);
77         if (part < 0)
78                 return 1;
79
80         dev = dev_desc->devnum;
81         if (fat_set_blk_dev(dev_desc, &info) != 0) {
82                 /*
83                  * This printf is embedded in the messages from env_save that
84                  * will calling it. The missing \n is intentional.
85                  */
86                 printf("Unable to use %s %d:%d...\n", ifname, dev, part);
87                 return 1;
88         }
89
90 #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
91         if (gd->env_valid == ENV_VALID)
92                 file = CONFIG_ENV_FAT_FILE_REDUND;
93 #endif
94
95         err = file_fat_write(file, (void *)&env_new, 0, sizeof(env_t), &size);
96         if (err == -1) {
97                 /*
98                  * This printf is embedded in the messages from env_save that
99                  * will calling it. The missing \n is intentional.
100                  */
101                 printf("Unable to write \"%s\" from %s%d:%d...\n", file, ifname, dev, part);
102                 return 1;
103         }
104
105 #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
106         gd->env_valid = (gd->env_valid == ENV_REDUND) ? ENV_VALID : ENV_REDUND;
107 #endif
108
109         return 0;
110 }
111
112 #ifdef LOADENV
113 static int env_fat_load(void)
114 {
115         ALLOC_CACHE_ALIGN_BUFFER(char, buf1, CONFIG_ENV_SIZE);
116 #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
117         ALLOC_CACHE_ALIGN_BUFFER(char, buf2, CONFIG_ENV_SIZE);
118         int err2;
119 #endif
120         struct blk_desc *dev_desc = NULL;
121         struct disk_partition info;
122         int dev, part;
123         int err1;
124         const char *ifname = env_fat_get_intf();
125         const char *dev_and_part = env_fat_get_dev_part();
126
127 #ifdef CONFIG_MMC
128         if (!strcmp(ifname, "mmc"))
129                 mmc_initialize(NULL);
130 #endif
131
132         part = blk_get_device_part_str(ifname, dev_and_part,
133                                        &dev_desc, &info, 1);
134         if (part < 0)
135                 goto err_env_relocate;
136
137         dev = dev_desc->devnum;
138         if (fat_set_blk_dev(dev_desc, &info) != 0) {
139                 /*
140                  * This printf is embedded in the messages from env_save that
141                  * will calling it. The missing \n is intentional.
142                  */
143                 printf("Unable to use %s %d:%d...\n", ifname, dev, part);
144                 goto err_env_relocate;
145         }
146
147         err1 = file_fat_read(CONFIG_ENV_FAT_FILE, buf1, CONFIG_ENV_SIZE);
148 #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
149         err2 = file_fat_read(CONFIG_ENV_FAT_FILE_REDUND, buf2, CONFIG_ENV_SIZE);
150
151         err1 = (err1 >= 0) ? 0 : -1;
152         err2 = (err2 >= 0) ? 0 : -1;
153         return env_import_redund(buf1, err1, buf2, err2, H_EXTERNAL);
154 #else
155         if (err1 < 0) {
156                 /*
157                  * This printf is embedded in the messages from env_save that
158                  * will calling it. The missing \n is intentional.
159                  */
160                 printf("Unable to read \"%s\" from %s%d:%d... \n",
161                         CONFIG_ENV_FAT_FILE, ifname, dev, part);
162                 goto err_env_relocate;
163         }
164
165         return env_import(buf1, 1, H_EXTERNAL);
166 #endif
167
168 err_env_relocate:
169         env_set_default(NULL, 0);
170
171         return -EIO;
172 }
173 #endif /* LOADENV */
174
175 U_BOOT_ENV_LOCATION(fat) = {
176         .location       = ENVL_FAT,
177         ENV_NAME("FAT")
178 #ifdef LOADENV
179         .load           = env_fat_load,
180 #endif
181         .save           = ENV_SAVE_PTR(env_fat_save),
182 };