blob: aad0487c32805aea4ec94e488f498c842e18cef6 [file] [log] [blame]
Maximilian Schwerin57210c72012-03-12 23:57:50 +00001/*
2 * (c) Copyright 2011 by Tigris Elektronik GmbH
3 *
4 * Author:
5 * Maximilian Schwerin <mvs@tigris.de>
6 *
Wolfgang Denk3765b3e2013-10-07 13:07:26 +02007 * SPDX-License-Identifier: GPL-2.0+
Maximilian Schwerin57210c72012-03-12 23:57:50 +00008 */
9
10#include <common.h>
11
12#include <command.h>
13#include <environment.h>
14#include <linux/stddef.h>
15#include <malloc.h>
16#include <search.h>
17#include <errno.h>
18#include <fat.h>
19#include <mmc.h>
20
21char *env_name_spec = "FAT";
22
23env_t *env_ptr;
24
25DECLARE_GLOBAL_DATA_PTR;
26
Maximilian Schwerin57210c72012-03-12 23:57:50 +000027int env_init(void)
28{
29 /* use default */
30 gd->env_addr = (ulong)&default_environment[0];
31 gd->env_valid = 1;
32
33 return 0;
34}
35
36#ifdef CONFIG_CMD_SAVEENV
37int saveenv(void)
38{
Tom Rinicd0f4fa2013-04-05 14:55:21 -040039 env_t env_new;
Maximilian Schwerin57210c72012-03-12 23:57:50 +000040 block_dev_desc_t *dev_desc = NULL;
41 int dev = FAT_ENV_DEVICE;
42 int part = FAT_ENV_PART;
Igor Grinberg9aa90c12012-09-23 05:25:21 +000043 int err;
Maximilian Schwerin57210c72012-03-12 23:57:50 +000044
Marek Vasut7ce15262014-03-05 19:59:50 +010045 err = env_export(&env_new);
46 if (err)
47 return err;
Maximilian Schwerin57210c72012-03-12 23:57:50 +000048
49#ifdef CONFIG_MMC
Igor Grinberg9aa90c12012-09-23 05:25:21 +000050 if (strcmp(FAT_ENV_INTERFACE, "mmc") == 0) {
Maximilian Schwerin57210c72012-03-12 23:57:50 +000051 struct mmc *mmc = find_mmc_device(dev);
52
53 if (!mmc) {
54 printf("no mmc device at slot %x\n", dev);
55 return 1;
56 }
57
58 mmc->has_init = 0;
59 mmc_init(mmc);
60 }
61#endif /* CONFIG_MMC */
62
63 dev_desc = get_dev(FAT_ENV_INTERFACE, dev);
64 if (dev_desc == NULL) {
65 printf("Failed to find %s%d\n",
66 FAT_ENV_INTERFACE, dev);
67 return 1;
68 }
Igor Grinberg9aa90c12012-09-23 05:25:21 +000069
70 err = fat_register_device(dev_desc, part);
71 if (err) {
Maximilian Schwerin57210c72012-03-12 23:57:50 +000072 printf("Failed to register %s%d:%d\n",
73 FAT_ENV_INTERFACE, dev, part);
74 return 1;
75 }
76
Tom Rinicd0f4fa2013-04-05 14:55:21 -040077 err = file_fat_write(FAT_ENV_FILE, (void *)&env_new, sizeof(env_t));
Igor Grinberg9aa90c12012-09-23 05:25:21 +000078 if (err == -1) {
Maximilian Schwerin57210c72012-03-12 23:57:50 +000079 printf("\n** Unable to write \"%s\" from %s%d:%d **\n",
80 FAT_ENV_FILE, FAT_ENV_INTERFACE, dev, part);
81 return 1;
82 }
83
84 puts("done\n");
85 return 0;
86}
87#endif /* CONFIG_CMD_SAVEENV */
88
89void env_relocate_spec(void)
90{
Tom Rinicd0f4fa2013-04-05 14:55:21 -040091 char buf[CONFIG_ENV_SIZE];
Maximilian Schwerin57210c72012-03-12 23:57:50 +000092 block_dev_desc_t *dev_desc = NULL;
93 int dev = FAT_ENV_DEVICE;
94 int part = FAT_ENV_PART;
Igor Grinberg9aa90c12012-09-23 05:25:21 +000095 int err;
Maximilian Schwerin57210c72012-03-12 23:57:50 +000096
97#ifdef CONFIG_MMC
Igor Grinberg9aa90c12012-09-23 05:25:21 +000098 if (strcmp(FAT_ENV_INTERFACE, "mmc") == 0) {
Maximilian Schwerin57210c72012-03-12 23:57:50 +000099 struct mmc *mmc = find_mmc_device(dev);
100
101 if (!mmc) {
102 printf("no mmc device at slot %x\n", dev);
103 set_default_env(NULL);
104 return;
105 }
106
107 mmc->has_init = 0;
108 mmc_init(mmc);
109 }
110#endif /* CONFIG_MMC */
111
112 dev_desc = get_dev(FAT_ENV_INTERFACE, dev);
113 if (dev_desc == NULL) {
114 printf("Failed to find %s%d\n",
115 FAT_ENV_INTERFACE, dev);
116 set_default_env(NULL);
117 return;
118 }
Igor Grinberg9aa90c12012-09-23 05:25:21 +0000119
120 err = fat_register_device(dev_desc, part);
121 if (err) {
Maximilian Schwerin57210c72012-03-12 23:57:50 +0000122 printf("Failed to register %s%d:%d\n",
123 FAT_ENV_INTERFACE, dev, part);
124 set_default_env(NULL);
125 return;
126 }
127
Igor Grinberg9aa90c12012-09-23 05:25:21 +0000128 err = file_fat_read(FAT_ENV_FILE, (uchar *)&buf, CONFIG_ENV_SIZE);
129 if (err == -1) {
Maximilian Schwerin57210c72012-03-12 23:57:50 +0000130 printf("\n** Unable to read \"%s\" from %s%d:%d **\n",
131 FAT_ENV_FILE, FAT_ENV_INTERFACE, dev, part);
132 set_default_env(NULL);
133 return;
134 }
135
136 env_import(buf, 1);
137}