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