blob: 328c09d45ff4c9b6b5120908bfb8ca9825ac67da [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;
Wu, Joshbe354c12014-06-24 17:31:02 +080041 disk_partition_t info;
42 int dev, 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
Wu, Joshbe354c12014-06-24 17:31:02 +080049 part = get_device_and_partition(FAT_ENV_INTERFACE,
50 FAT_ENV_DEVICE_AND_PART,
51 &dev_desc, &info, 1);
52 if (part < 0)
Maximilian Schwerin57210c72012-03-12 23:57:50 +000053 return 1;
Igor Grinberg9aa90c12012-09-23 05:25:21 +000054
Wu, Joshbe354c12014-06-24 17:31:02 +080055 dev = dev_desc->dev;
56 if (fat_set_blk_dev(dev_desc, &info) != 0) {
57 printf("\n** Unable to use %s %d:%d for saveenv **\n",
58 FAT_ENV_INTERFACE, dev, part);
Maximilian Schwerin57210c72012-03-12 23:57:50 +000059 return 1;
60 }
61
Tom Rinicd0f4fa2013-04-05 14:55:21 -040062 err = file_fat_write(FAT_ENV_FILE, (void *)&env_new, sizeof(env_t));
Igor Grinberg9aa90c12012-09-23 05:25:21 +000063 if (err == -1) {
Maximilian Schwerin57210c72012-03-12 23:57:50 +000064 printf("\n** Unable to write \"%s\" from %s%d:%d **\n",
65 FAT_ENV_FILE, FAT_ENV_INTERFACE, dev, part);
66 return 1;
67 }
68
69 puts("done\n");
70 return 0;
71}
72#endif /* CONFIG_CMD_SAVEENV */
73
74void env_relocate_spec(void)
75{
Tom Rinicd0f4fa2013-04-05 14:55:21 -040076 char buf[CONFIG_ENV_SIZE];
Maximilian Schwerin57210c72012-03-12 23:57:50 +000077 block_dev_desc_t *dev_desc = NULL;
Wu, Joshbe354c12014-06-24 17:31:02 +080078 disk_partition_t info;
79 int dev, part;
Igor Grinberg9aa90c12012-09-23 05:25:21 +000080 int err;
Maximilian Schwerin57210c72012-03-12 23:57:50 +000081
Wu, Joshbe354c12014-06-24 17:31:02 +080082 part = get_device_and_partition(FAT_ENV_INTERFACE,
83 FAT_ENV_DEVICE_AND_PART,
84 &dev_desc, &info, 1);
85 if (part < 0)
86 goto err_env_relocate;
Maximilian Schwerin57210c72012-03-12 23:57:50 +000087
Wu, Joshbe354c12014-06-24 17:31:02 +080088 dev = dev_desc->dev;
89 if (fat_set_blk_dev(dev_desc, &info) != 0) {
90 printf("\n** Unable to use %s %d:%d for loading the env **\n",
91 FAT_ENV_INTERFACE, dev, part);
92 goto err_env_relocate;
Maximilian Schwerin57210c72012-03-12 23:57:50 +000093 }
94
Igor Grinberg9aa90c12012-09-23 05:25:21 +000095 err = file_fat_read(FAT_ENV_FILE, (uchar *)&buf, CONFIG_ENV_SIZE);
96 if (err == -1) {
Maximilian Schwerin57210c72012-03-12 23:57:50 +000097 printf("\n** Unable to read \"%s\" from %s%d:%d **\n",
98 FAT_ENV_FILE, FAT_ENV_INTERFACE, dev, part);
Wu, Joshbe354c12014-06-24 17:31:02 +080099 goto err_env_relocate;
Maximilian Schwerin57210c72012-03-12 23:57:50 +0000100 }
101
102 env_import(buf, 1);
Wu, Joshbe354c12014-06-24 17:31:02 +0800103 return;
104
105err_env_relocate:
106 set_default_env(NULL);
Maximilian Schwerin57210c72012-03-12 23:57:50 +0000107}