blob: e4c848935ad19d7cac64d07102e7ebe39841b58b [file] [log] [blame]
Maximilian Schwerin9765c082012-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 Denkbd8ec7e2013-10-07 13:07:26 +02007 * SPDX-License-Identifier: GPL-2.0+
Maximilian Schwerin9765c082012-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 Schwerin9765c082012-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 Rinia840b8e2013-04-05 14:55:21 -040039 env_t env_new;
Maximilian Schwerin9765c082012-03-12 23:57:50 +000040 block_dev_desc_t *dev_desc = NULL;
Wu, Josh9b899f22014-06-24 17:31:02 +080041 disk_partition_t info;
42 int dev, part;
Igor Grinberg49eee6e2012-09-23 05:25:21 +000043 int err;
Suriyan Ramasami441c2232014-11-17 14:39:35 -080044 loff_t size;
Maximilian Schwerin9765c082012-03-12 23:57:50 +000045
Marek Vasutd73c1292014-03-05 19:59:50 +010046 err = env_export(&env_new);
47 if (err)
48 return err;
Maximilian Schwerin9765c082012-03-12 23:57:50 +000049
Wu, Josh9b899f22014-06-24 17:31:02 +080050 part = get_device_and_partition(FAT_ENV_INTERFACE,
51 FAT_ENV_DEVICE_AND_PART,
52 &dev_desc, &info, 1);
53 if (part < 0)
Maximilian Schwerin9765c082012-03-12 23:57:50 +000054 return 1;
Igor Grinberg49eee6e2012-09-23 05:25:21 +000055
Wu, Josh9b899f22014-06-24 17:31:02 +080056 dev = dev_desc->dev;
57 if (fat_set_blk_dev(dev_desc, &info) != 0) {
58 printf("\n** Unable to use %s %d:%d for saveenv **\n",
59 FAT_ENV_INTERFACE, dev, part);
Maximilian Schwerin9765c082012-03-12 23:57:50 +000060 return 1;
61 }
62
Suriyan Ramasami441c2232014-11-17 14:39:35 -080063 err = file_fat_write(FAT_ENV_FILE, (void *)&env_new, 0, sizeof(env_t),
64 &size);
Igor Grinberg49eee6e2012-09-23 05:25:21 +000065 if (err == -1) {
Maximilian Schwerin9765c082012-03-12 23:57:50 +000066 printf("\n** Unable to write \"%s\" from %s%d:%d **\n",
67 FAT_ENV_FILE, FAT_ENV_INTERFACE, dev, part);
68 return 1;
69 }
70
71 puts("done\n");
72 return 0;
73}
74#endif /* CONFIG_CMD_SAVEENV */
75
76void env_relocate_spec(void)
77{
Tom Rini0b635ed2014-08-01 13:59:10 -040078 ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
Maximilian Schwerin9765c082012-03-12 23:57:50 +000079 block_dev_desc_t *dev_desc = NULL;
Wu, Josh9b899f22014-06-24 17:31:02 +080080 disk_partition_t info;
81 int dev, part;
Igor Grinberg49eee6e2012-09-23 05:25:21 +000082 int err;
Maximilian Schwerin9765c082012-03-12 23:57:50 +000083
Wu, Josh9b899f22014-06-24 17:31:02 +080084 part = get_device_and_partition(FAT_ENV_INTERFACE,
85 FAT_ENV_DEVICE_AND_PART,
86 &dev_desc, &info, 1);
87 if (part < 0)
88 goto err_env_relocate;
Maximilian Schwerin9765c082012-03-12 23:57:50 +000089
Wu, Josh9b899f22014-06-24 17:31:02 +080090 dev = dev_desc->dev;
91 if (fat_set_blk_dev(dev_desc, &info) != 0) {
92 printf("\n** Unable to use %s %d:%d for loading the env **\n",
93 FAT_ENV_INTERFACE, dev, part);
94 goto err_env_relocate;
Maximilian Schwerin9765c082012-03-12 23:57:50 +000095 }
Maximilian Schwerin9765c082012-03-12 23:57:50 +000096
Tom Rini0b635ed2014-08-01 13:59:10 -040097 err = file_fat_read(FAT_ENV_FILE, buf, CONFIG_ENV_SIZE);
Igor Grinberg49eee6e2012-09-23 05:25:21 +000098 if (err == -1) {
Maximilian Schwerin9765c082012-03-12 23:57:50 +000099 printf("\n** Unable to read \"%s\" from %s%d:%d **\n",
100 FAT_ENV_FILE, FAT_ENV_INTERFACE, dev, part);
Wu, Josh9b899f22014-06-24 17:31:02 +0800101 goto err_env_relocate;
Maximilian Schwerin9765c082012-03-12 23:57:50 +0000102 }
103
104 env_import(buf, 1);
Wu, Josh9b899f22014-06-24 17:31:02 +0800105 return;
106
107err_env_relocate:
108 set_default_env(NULL);
Maximilian Schwerin9765c082012-03-12 23:57:50 +0000109}