]>
Commit | Line | Data |
---|---|---|
57210c7c MS |
1 | /* |
2 | * (c) Copyright 2011 by Tigris Elektronik GmbH | |
3 | * | |
4 | * Author: | |
5 | * Maximilian Schwerin <[email protected]> | |
6 | * | |
3765b3e7 | 7 | * SPDX-License-Identifier: GPL-2.0+ |
57210c7c MS |
8 | */ |
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 | ||
21 | char *env_name_spec = "FAT"; | |
22 | ||
23 | env_t *env_ptr; | |
24 | ||
25 | DECLARE_GLOBAL_DATA_PTR; | |
26 | ||
57210c7c MS |
27 | int 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 | |
37 | int saveenv(void) | |
38 | { | |
cd0f4fa1 | 39 | env_t env_new; |
57210c7c | 40 | block_dev_desc_t *dev_desc = NULL; |
be354c1a WJ |
41 | disk_partition_t info; |
42 | int dev, part; | |
9aa90c1d | 43 | int err; |
1ad0b98a | 44 | loff_t size; |
57210c7c | 45 | |
7ce1526e MV |
46 | err = env_export(&env_new); |
47 | if (err) | |
48 | return err; | |
57210c7c | 49 | |
be354c1a WJ |
50 | part = get_device_and_partition(FAT_ENV_INTERFACE, |
51 | FAT_ENV_DEVICE_AND_PART, | |
52 | &dev_desc, &info, 1); | |
53 | if (part < 0) | |
57210c7c | 54 | return 1; |
9aa90c1d | 55 | |
be354c1a WJ |
56 | 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); | |
57210c7c MS |
60 | return 1; |
61 | } | |
62 | ||
1ad0b98a SR |
63 | err = file_fat_write(FAT_ENV_FILE, (void *)&env_new, 0, sizeof(env_t), |
64 | &size); | |
9aa90c1d | 65 | if (err == -1) { |
57210c7c MS |
66 | 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 | ||
76 | void env_relocate_spec(void) | |
77 | { | |
6d1966e1 | 78 | ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE); |
57210c7c | 79 | block_dev_desc_t *dev_desc = NULL; |
be354c1a WJ |
80 | disk_partition_t info; |
81 | int dev, part; | |
9aa90c1d | 82 | int err; |
57210c7c | 83 | |
be354c1a WJ |
84 | 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; | |
89 | ||
90 | 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; | |
57210c7c MS |
95 | } |
96 | ||
6d1966e1 | 97 | err = file_fat_read(FAT_ENV_FILE, buf, CONFIG_ENV_SIZE); |
9aa90c1d | 98 | if (err == -1) { |
57210c7c MS |
99 | printf("\n** Unable to read \"%s\" from %s%d:%d **\n", |
100 | FAT_ENV_FILE, FAT_ENV_INTERFACE, dev, part); | |
be354c1a | 101 | goto err_env_relocate; |
57210c7c MS |
102 | } |
103 | ||
104 | env_import(buf, 1); | |
be354c1a WJ |
105 | return; |
106 | ||
107 | err_env_relocate: | |
108 | set_default_env(NULL); | |
57210c7c | 109 | } |