blob: 708fd13dc72b25eba1bb3e2a86c0fe7a4706f37d [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 ssize_t len;
41 char *res;
42 block_dev_desc_t *dev_desc = NULL;
43 int dev = FAT_ENV_DEVICE;
44 int part = FAT_ENV_PART;
Igor Grinberg9aa90c12012-09-23 05:25:21 +000045 int err;
Maximilian Schwerin57210c72012-03-12 23:57:50 +000046
Tom Rinicd0f4fa2013-04-05 14:55:21 -040047 res = (char *)&env_new.data;
Joe Hershbergerbe112352012-12-11 22:16:23 -060048 len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
Maximilian Schwerin57210c72012-03-12 23:57:50 +000049 if (len < 0) {
50 error("Cannot export environment: errno = %d\n", errno);
51 return 1;
52 }
53
54#ifdef CONFIG_MMC
Igor Grinberg9aa90c12012-09-23 05:25:21 +000055 if (strcmp(FAT_ENV_INTERFACE, "mmc") == 0) {
Maximilian Schwerin57210c72012-03-12 23:57:50 +000056 struct mmc *mmc = find_mmc_device(dev);
57
58 if (!mmc) {
59 printf("no mmc device at slot %x\n", dev);
60 return 1;
61 }
62
63 mmc->has_init = 0;
64 mmc_init(mmc);
65 }
66#endif /* CONFIG_MMC */
67
68 dev_desc = get_dev(FAT_ENV_INTERFACE, dev);
69 if (dev_desc == NULL) {
70 printf("Failed to find %s%d\n",
71 FAT_ENV_INTERFACE, dev);
72 return 1;
73 }
Igor Grinberg9aa90c12012-09-23 05:25:21 +000074
75 err = fat_register_device(dev_desc, part);
76 if (err) {
Maximilian Schwerin57210c72012-03-12 23:57:50 +000077 printf("Failed to register %s%d:%d\n",
78 FAT_ENV_INTERFACE, dev, part);
79 return 1;
80 }
81
Tom Rinicd0f4fa2013-04-05 14:55:21 -040082 env_new.crc = crc32(0, env_new.data, ENV_SIZE);
83 err = file_fat_write(FAT_ENV_FILE, (void *)&env_new, sizeof(env_t));
Igor Grinberg9aa90c12012-09-23 05:25:21 +000084 if (err == -1) {
Maximilian Schwerin57210c72012-03-12 23:57:50 +000085 printf("\n** Unable to write \"%s\" from %s%d:%d **\n",
86 FAT_ENV_FILE, FAT_ENV_INTERFACE, dev, part);
87 return 1;
88 }
89
90 puts("done\n");
91 return 0;
92}
93#endif /* CONFIG_CMD_SAVEENV */
94
95void env_relocate_spec(void)
96{
Tom Rinicd0f4fa2013-04-05 14:55:21 -040097 char buf[CONFIG_ENV_SIZE];
Maximilian Schwerin57210c72012-03-12 23:57:50 +000098 block_dev_desc_t *dev_desc = NULL;
99 int dev = FAT_ENV_DEVICE;
100 int part = FAT_ENV_PART;
Igor Grinberg9aa90c12012-09-23 05:25:21 +0000101 int err;
Maximilian Schwerin57210c72012-03-12 23:57:50 +0000102
103#ifdef CONFIG_MMC
Igor Grinberg9aa90c12012-09-23 05:25:21 +0000104 if (strcmp(FAT_ENV_INTERFACE, "mmc") == 0) {
Maximilian Schwerin57210c72012-03-12 23:57:50 +0000105 struct mmc *mmc = find_mmc_device(dev);
106
107 if (!mmc) {
108 printf("no mmc device at slot %x\n", dev);
109 set_default_env(NULL);
110 return;
111 }
112
113 mmc->has_init = 0;
114 mmc_init(mmc);
115 }
116#endif /* CONFIG_MMC */
117
118 dev_desc = get_dev(FAT_ENV_INTERFACE, dev);
119 if (dev_desc == NULL) {
120 printf("Failed to find %s%d\n",
121 FAT_ENV_INTERFACE, dev);
122 set_default_env(NULL);
123 return;
124 }
Igor Grinberg9aa90c12012-09-23 05:25:21 +0000125
126 err = fat_register_device(dev_desc, part);
127 if (err) {
Maximilian Schwerin57210c72012-03-12 23:57:50 +0000128 printf("Failed to register %s%d:%d\n",
129 FAT_ENV_INTERFACE, dev, part);
130 set_default_env(NULL);
131 return;
132 }
133
Igor Grinberg9aa90c12012-09-23 05:25:21 +0000134 err = file_fat_read(FAT_ENV_FILE, (uchar *)&buf, CONFIG_ENV_SIZE);
135 if (err == -1) {
Maximilian Schwerin57210c72012-03-12 23:57:50 +0000136 printf("\n** Unable to read \"%s\" from %s%d:%d **\n",
137 FAT_ENV_FILE, FAT_ENV_INTERFACE, dev, part);
138 set_default_env(NULL);
139 return;
140 }
141
142 env_import(buf, 1);
143}