]> git.ipfire.org Git - thirdparty/u-boot.git/blame - env/fat.c
Merge branch 'master' of git://git.denx.de/u-boot-socfpga
[thirdparty/u-boot.git] / env / fat.c
CommitLineData
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 <mvs@tigris.de>
57210c7c
MS
7 */
8
9#include <common.h>
10
11#include <command.h>
12#include <environment.h>
13#include <linux/stddef.h>
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>
20
4415f1d1
SG
21#ifdef CONFIG_SPL_BUILD
22/* TODO(sjg@chromium.org): Figure out why this is needed */
23# if !defined(CONFIG_TARGET_AM335X_EVM) || defined(CONFIG_SPL_OS_BOOT)
24# define LOADENV
25# endif
26#else
27# define LOADENV
28# if defined(CONFIG_CMD_SAVEENV)
29# define CMD_SAVEENV
30# endif
31#endif
32
4415f1d1 33#ifdef CMD_SAVEENV
e5bce247 34static int env_fat_save(void)
57210c7c 35{
cda87ec5 36 env_t __aligned(ARCH_DMA_MINALIGN) env_new;
4101f687 37 struct blk_desc *dev_desc = NULL;
be354c1a
WJ
38 disk_partition_t info;
39 int dev, part;
9aa90c1d 40 int err;
1ad0b98a 41 loff_t size;
57210c7c 42
7ce1526e
MV
43 err = env_export(&env_new);
44 if (err)
45 return err;
57210c7c 46
43ba3c59
TR
47 part = blk_get_device_part_str(CONFIG_ENV_FAT_INTERFACE,
48 CONFIG_ENV_FAT_DEVICE_AND_PART,
be354c1a
WJ
49 &dev_desc, &info, 1);
50 if (part < 0)
57210c7c 51 return 1;
9aa90c1d 52
bcce53d0 53 dev = dev_desc->devnum;
be354c1a 54 if (fat_set_blk_dev(dev_desc, &info) != 0) {
d0816da5
MR
55 /*
56 * This printf is embedded in the messages from env_save that
57 * will calling it. The missing \n is intentional.
58 */
59 printf("Unable to use %s %d:%d... ",
43ba3c59 60 CONFIG_ENV_FAT_INTERFACE, dev, part);
57210c7c
MS
61 return 1;
62 }
63
43ba3c59 64 err = file_fat_write(CONFIG_ENV_FAT_FILE, (void *)&env_new, 0, sizeof(env_t),
1ad0b98a 65 &size);
9aa90c1d 66 if (err == -1) {
d0816da5
MR
67 /*
68 * This printf is embedded in the messages from env_save that
69 * will calling it. The missing \n is intentional.
70 */
71 printf("Unable to write \"%s\" from %s%d:%d... ",
43ba3c59 72 CONFIG_ENV_FAT_FILE, CONFIG_ENV_FAT_INTERFACE, dev, part);
57210c7c
MS
73 return 1;
74 }
75
57210c7c
MS
76 return 0;
77}
4415f1d1 78#endif /* CMD_SAVEENV */
57210c7c 79
4415f1d1 80#ifdef LOADENV
c5951991 81static int env_fat_load(void)
57210c7c 82{
6d1966e1 83 ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
4101f687 84 struct blk_desc *dev_desc = NULL;
be354c1a
WJ
85 disk_partition_t info;
86 int dev, part;
9aa90c1d 87 int err;
57210c7c 88
95058fbb 89#ifdef CONFIG_MMC
26862b4a
FA
90 if (!strcmp(CONFIG_ENV_FAT_INTERFACE, "mmc"))
91 mmc_initialize(NULL);
95058fbb 92#endif
26862b4a 93
43ba3c59
TR
94 part = blk_get_device_part_str(CONFIG_ENV_FAT_INTERFACE,
95 CONFIG_ENV_FAT_DEVICE_AND_PART,
be354c1a
WJ
96 &dev_desc, &info, 1);
97 if (part < 0)
98 goto err_env_relocate;
99
bcce53d0 100 dev = dev_desc->devnum;
be354c1a 101 if (fat_set_blk_dev(dev_desc, &info) != 0) {
d0816da5
MR
102 /*
103 * This printf is embedded in the messages from env_save that
104 * will calling it. The missing \n is intentional.
105 */
106 printf("Unable to use %s %d:%d... ",
43ba3c59 107 CONFIG_ENV_FAT_INTERFACE, dev, part);
be354c1a 108 goto err_env_relocate;
57210c7c
MS
109 }
110
43ba3c59 111 err = file_fat_read(CONFIG_ENV_FAT_FILE, buf, CONFIG_ENV_SIZE);
9aa90c1d 112 if (err == -1) {
d0816da5
MR
113 /*
114 * This printf is embedded in the messages from env_save that
115 * will calling it. The missing \n is intentional.
116 */
117 printf("Unable to read \"%s\" from %s%d:%d... ",
43ba3c59 118 CONFIG_ENV_FAT_FILE, CONFIG_ENV_FAT_INTERFACE, dev, part);
be354c1a 119 goto err_env_relocate;
57210c7c
MS
120 }
121
2166ebf7 122 return env_import(buf, 1);
be354c1a
WJ
123
124err_env_relocate:
c5d548a9 125 set_default_env(NULL, 0);
c5951991
SG
126
127 return -EIO;
57210c7c 128}
4415f1d1
SG
129#endif /* LOADENV */
130
131U_BOOT_ENV_LOCATION(fat) = {
132 .location = ENVL_FAT,
ac358beb 133 ENV_NAME("FAT")
4415f1d1 134#ifdef LOADENV
e5bce247 135 .load = env_fat_load,
4415f1d1
SG
136#endif
137#ifdef CMD_SAVEENV
e5bce247 138 .save = env_save_ptr(env_fat_save),
4415f1d1 139#endif
4415f1d1 140};