Merge branch '2020-04-24-master-imports'
[oweals/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
11 #include <command.h>
12 #include <env.h>
13 #include <env_internal.h>
14 #include <linux/stddef.h>
15 #include <malloc.h>
16 #include <memalign.h>
17 #include <search.h>
18 #include <errno.h>
19 #include <fat.h>
20 #include <mmc.h>
21
22 #ifdef CONFIG_SPL_BUILD
23 /* TODO(sjg@chromium.org): Figure out why this is needed */
24 # if !defined(CONFIG_TARGET_AM335X_EVM) || defined(CONFIG_SPL_OS_BOOT)
25 #  define LOADENV
26 # endif
27 #else
28 # define LOADENV
29 #endif
30
31 static int env_fat_save(void)
32 {
33         env_t __aligned(ARCH_DMA_MINALIGN) env_new;
34         struct blk_desc *dev_desc = NULL;
35         disk_partition_t info;
36         int dev, part;
37         int err;
38         loff_t size;
39
40         err = env_export(&env_new);
41         if (err)
42                 return err;
43
44         part = blk_get_device_part_str(CONFIG_ENV_FAT_INTERFACE,
45                                         CONFIG_ENV_FAT_DEVICE_AND_PART,
46                                         &dev_desc, &info, 1);
47         if (part < 0)
48                 return 1;
49
50         dev = dev_desc->devnum;
51         if (fat_set_blk_dev(dev_desc, &info) != 0) {
52                 /*
53                  * This printf is embedded in the messages from env_save that
54                  * will calling it. The missing \n is intentional.
55                  */
56                 printf("Unable to use %s %d:%d... ",
57                        CONFIG_ENV_FAT_INTERFACE, dev, part);
58                 return 1;
59         }
60
61         err = file_fat_write(CONFIG_ENV_FAT_FILE, (void *)&env_new, 0, sizeof(env_t),
62                              &size);
63         if (err == -1) {
64                 /*
65                  * This printf is embedded in the messages from env_save that
66                  * will calling it. The missing \n is intentional.
67                  */
68                 printf("Unable to write \"%s\" from %s%d:%d... ",
69                         CONFIG_ENV_FAT_FILE, CONFIG_ENV_FAT_INTERFACE, dev, part);
70                 return 1;
71         }
72
73         return 0;
74 }
75
76 #ifdef LOADENV
77 static int env_fat_load(void)
78 {
79         ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
80         struct blk_desc *dev_desc = NULL;
81         disk_partition_t info;
82         int dev, part;
83         int err;
84
85 #ifdef CONFIG_MMC
86         if (!strcmp(CONFIG_ENV_FAT_INTERFACE, "mmc"))
87                 mmc_initialize(NULL);
88 #endif
89
90         part = blk_get_device_part_str(CONFIG_ENV_FAT_INTERFACE,
91                                         CONFIG_ENV_FAT_DEVICE_AND_PART,
92                                         &dev_desc, &info, 1);
93         if (part < 0)
94                 goto err_env_relocate;
95
96         dev = dev_desc->devnum;
97         if (fat_set_blk_dev(dev_desc, &info) != 0) {
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 use %s %d:%d... ",
103                        CONFIG_ENV_FAT_INTERFACE, dev, part);
104                 goto err_env_relocate;
105         }
106
107         err = file_fat_read(CONFIG_ENV_FAT_FILE, buf, CONFIG_ENV_SIZE);
108         if (err == -1) {
109                 /*
110                  * This printf is embedded in the messages from env_save that
111                  * will calling it. The missing \n is intentional.
112                  */
113                 printf("Unable to read \"%s\" from %s%d:%d... ",
114                         CONFIG_ENV_FAT_FILE, CONFIG_ENV_FAT_INTERFACE, dev, part);
115                 goto err_env_relocate;
116         }
117
118         return env_import(buf, 1);
119
120 err_env_relocate:
121         env_set_default(NULL, 0);
122
123         return -EIO;
124 }
125 #endif /* LOADENV */
126
127 U_BOOT_ENV_LOCATION(fat) = {
128         .location       = ENVL_FAT,
129         ENV_NAME("FAT")
130 #ifdef LOADENV
131         .load           = env_fat_load,
132 #endif
133         .save           = ENV_SAVE_PTR(env_fat_save),
134 };