]> git.ipfire.org Git - thirdparty/u-boot.git/blame - env/fat.c
arm: Don't include common.h in header files
[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>
0ac7d722 12#include <env.h>
f3998fdc 13#include <env_internal.h>
57210c7c
MS
14#include <linux/stddef.h>
15#include <malloc.h>
cf92e05c 16#include <memalign.h>
57210c7c
MS
17#include <search.h>
18#include <errno.h>
19#include <fat.h>
20#include <mmc.h>
21
4415f1d1
SG
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
4415f1d1
SG
29#endif
30
e5bce247 31static int env_fat_save(void)
57210c7c 32{
cda87ec5 33 env_t __aligned(ARCH_DMA_MINALIGN) env_new;
4101f687 34 struct blk_desc *dev_desc = NULL;
be354c1a
WJ
35 disk_partition_t info;
36 int dev, part;
9aa90c1d 37 int err;
1ad0b98a 38 loff_t size;
57210c7c 39
7ce1526e
MV
40 err = env_export(&env_new);
41 if (err)
42 return err;
57210c7c 43
43ba3c59
TR
44 part = blk_get_device_part_str(CONFIG_ENV_FAT_INTERFACE,
45 CONFIG_ENV_FAT_DEVICE_AND_PART,
be354c1a
WJ
46 &dev_desc, &info, 1);
47 if (part < 0)
57210c7c 48 return 1;
9aa90c1d 49
bcce53d0 50 dev = dev_desc->devnum;
be354c1a 51 if (fat_set_blk_dev(dev_desc, &info) != 0) {
d0816da5
MR
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... ",
43ba3c59 57 CONFIG_ENV_FAT_INTERFACE, dev, part);
57210c7c
MS
58 return 1;
59 }
60
43ba3c59 61 err = file_fat_write(CONFIG_ENV_FAT_FILE, (void *)&env_new, 0, sizeof(env_t),
1ad0b98a 62 &size);
9aa90c1d 63 if (err == -1) {
d0816da5
MR
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... ",
43ba3c59 69 CONFIG_ENV_FAT_FILE, CONFIG_ENV_FAT_INTERFACE, dev, part);
57210c7c
MS
70 return 1;
71 }
72
57210c7c
MS
73 return 0;
74}
57210c7c 75
4415f1d1 76#ifdef LOADENV
c5951991 77static int env_fat_load(void)
57210c7c 78{
6d1966e1 79 ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
4101f687 80 struct blk_desc *dev_desc = NULL;
be354c1a
WJ
81 disk_partition_t info;
82 int dev, part;
9aa90c1d 83 int err;
57210c7c 84
95058fbb 85#ifdef CONFIG_MMC
26862b4a
FA
86 if (!strcmp(CONFIG_ENV_FAT_INTERFACE, "mmc"))
87 mmc_initialize(NULL);
95058fbb 88#endif
26862b4a 89
43ba3c59
TR
90 part = blk_get_device_part_str(CONFIG_ENV_FAT_INTERFACE,
91 CONFIG_ENV_FAT_DEVICE_AND_PART,
be354c1a
WJ
92 &dev_desc, &info, 1);
93 if (part < 0)
94 goto err_env_relocate;
95
bcce53d0 96 dev = dev_desc->devnum;
be354c1a 97 if (fat_set_blk_dev(dev_desc, &info) != 0) {
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 */
102 printf("Unable to use %s %d:%d... ",
43ba3c59 103 CONFIG_ENV_FAT_INTERFACE, dev, part);
be354c1a 104 goto err_env_relocate;
57210c7c
MS
105 }
106
43ba3c59 107 err = file_fat_read(CONFIG_ENV_FAT_FILE, buf, CONFIG_ENV_SIZE);
9aa90c1d 108 if (err == -1) {
d0816da5
MR
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... ",
43ba3c59 114 CONFIG_ENV_FAT_FILE, CONFIG_ENV_FAT_INTERFACE, dev, part);
be354c1a 115 goto err_env_relocate;
57210c7c
MS
116 }
117
2166ebf7 118 return env_import(buf, 1);
be354c1a
WJ
119
120err_env_relocate:
0ac7d722 121 env_set_default(NULL, 0);
c5951991
SG
122
123 return -EIO;
57210c7c 124}
4415f1d1
SG
125#endif /* LOADENV */
126
127U_BOOT_ENV_LOCATION(fat) = {
128 .location = ENVL_FAT,
ac358beb 129 ENV_NAME("FAT")
4415f1d1 130#ifdef LOADENV
e5bce247 131 .load = env_fat_load,
4415f1d1 132#endif
3908bc93 133 .save = ENV_SAVE_PTR(env_fat_save),
4415f1d1 134};