]>
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> | |
e6f6f9e6 SG |
20 | #include <asm/cache.h> |
21 | #include <linux/stddef.h> | |
57210c7c | 22 | |
4415f1d1 SG |
23 | #ifdef CONFIG_SPL_BUILD |
24 | /* TODO([email protected]): Figure out why this is needed */ | |
25 | # if !defined(CONFIG_TARGET_AM335X_EVM) || defined(CONFIG_SPL_OS_BOOT) | |
26 | # define LOADENV | |
27 | # endif | |
28 | #else | |
29 | # define LOADENV | |
4415f1d1 SG |
30 | #endif |
31 | ||
6731bef6 DW |
32 | __weak int mmc_get_env_dev(void) |
33 | { | |
34 | #ifdef CONFIG_SYS_MMC_ENV_DEV | |
35 | return CONFIG_SYS_MMC_ENV_DEV; | |
36 | #else | |
37 | return 0; | |
38 | #endif | |
39 | } | |
40 | ||
41 | static char *env_fat_device_and_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 | ||
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; |
be354c1a | 65 | int dev, part; |
9aa90c1d | 66 | int err; |
1ad0b98a | 67 | loff_t size; |
57210c7c | 68 | |
7ce1526e MV |
69 | err = env_export(&env_new); |
70 | if (err) | |
71 | return err; | |
57210c7c | 72 | |
43ba3c59 | 73 | part = blk_get_device_part_str(CONFIG_ENV_FAT_INTERFACE, |
6731bef6 | 74 | env_fat_device_and_part(), |
be354c1a WJ |
75 | &dev_desc, &info, 1); |
76 | if (part < 0) | |
57210c7c | 77 | return 1; |
9aa90c1d | 78 | |
bcce53d0 | 79 | dev = dev_desc->devnum; |
be354c1a | 80 | if (fat_set_blk_dev(dev_desc, &info) != 0) { |
d0816da5 MR |
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... ", | |
43ba3c59 | 86 | CONFIG_ENV_FAT_INTERFACE, dev, part); |
57210c7c MS |
87 | return 1; |
88 | } | |
89 | ||
43ba3c59 | 90 | err = file_fat_write(CONFIG_ENV_FAT_FILE, (void *)&env_new, 0, sizeof(env_t), |
1ad0b98a | 91 | &size); |
9aa90c1d | 92 | if (err == -1) { |
d0816da5 MR |
93 | /* |
94 | * This printf is embedded in the messages from env_save that | |
95 | * will calling it. The missing \n is intentional. | |
96 | */ | |
97 | printf("Unable to write \"%s\" from %s%d:%d... ", | |
43ba3c59 | 98 | CONFIG_ENV_FAT_FILE, CONFIG_ENV_FAT_INTERFACE, dev, part); |
57210c7c MS |
99 | return 1; |
100 | } | |
101 | ||
57210c7c MS |
102 | return 0; |
103 | } | |
57210c7c | 104 | |
4415f1d1 | 105 | #ifdef LOADENV |
c5951991 | 106 | static int env_fat_load(void) |
57210c7c | 107 | { |
6d1966e1 | 108 | ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE); |
4101f687 | 109 | struct blk_desc *dev_desc = NULL; |
0528979f | 110 | struct disk_partition info; |
be354c1a | 111 | int dev, part; |
9aa90c1d | 112 | int err; |
57210c7c | 113 | |
95058fbb | 114 | #ifdef CONFIG_MMC |
26862b4a FA |
115 | if (!strcmp(CONFIG_ENV_FAT_INTERFACE, "mmc")) |
116 | mmc_initialize(NULL); | |
95058fbb | 117 | #endif |
26862b4a | 118 | |
43ba3c59 | 119 | part = blk_get_device_part_str(CONFIG_ENV_FAT_INTERFACE, |
6731bef6 | 120 | env_fat_device_and_part(), |
be354c1a WJ |
121 | &dev_desc, &info, 1); |
122 | if (part < 0) | |
123 | goto err_env_relocate; | |
124 | ||
bcce53d0 | 125 | dev = dev_desc->devnum; |
be354c1a | 126 | if (fat_set_blk_dev(dev_desc, &info) != 0) { |
d0816da5 MR |
127 | /* |
128 | * This printf is embedded in the messages from env_save that | |
129 | * will calling it. The missing \n is intentional. | |
130 | */ | |
131 | printf("Unable to use %s %d:%d... ", | |
43ba3c59 | 132 | CONFIG_ENV_FAT_INTERFACE, dev, part); |
be354c1a | 133 | goto err_env_relocate; |
57210c7c MS |
134 | } |
135 | ||
43ba3c59 | 136 | err = file_fat_read(CONFIG_ENV_FAT_FILE, buf, CONFIG_ENV_SIZE); |
9aa90c1d | 137 | if (err == -1) { |
d0816da5 MR |
138 | /* |
139 | * This printf is embedded in the messages from env_save that | |
140 | * will calling it. The missing \n is intentional. | |
141 | */ | |
142 | printf("Unable to read \"%s\" from %s%d:%d... ", | |
43ba3c59 | 143 | CONFIG_ENV_FAT_FILE, CONFIG_ENV_FAT_INTERFACE, dev, part); |
be354c1a | 144 | goto err_env_relocate; |
57210c7c MS |
145 | } |
146 | ||
890feeca | 147 | return env_import(buf, 1, H_EXTERNAL); |
be354c1a WJ |
148 | |
149 | err_env_relocate: | |
0ac7d722 | 150 | env_set_default(NULL, 0); |
c5951991 SG |
151 | |
152 | return -EIO; | |
57210c7c | 153 | } |
4415f1d1 SG |
154 | #endif /* LOADENV */ |
155 | ||
156 | U_BOOT_ENV_LOCATION(fat) = { | |
157 | .location = ENVL_FAT, | |
ac358beb | 158 | ENV_NAME("FAT") |
4415f1d1 | 159 | #ifdef LOADENV |
e5bce247 | 160 | .load = env_fat_load, |
4415f1d1 | 161 | #endif |
3908bc93 | 162 | .save = ENV_SAVE_PTR(env_fat_save), |
4415f1d1 | 163 | }; |