]> git.sur5r.net Git - u-boot/blob - common/env_fat.c
mtd: nand: omap: fix error-codes returned from omap-elm driver
[u-boot] / common / env_fat.c
1 /*
2  * (c) Copyright 2011 by Tigris Elektronik GmbH
3  *
4  * Author:
5  *  Maximilian Schwerin <mvs@tigris.de>
6  *
7  * SPDX-License-Identifier:     GPL-2.0+
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
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 {
39         env_t   env_new;
40         block_dev_desc_t *dev_desc = NULL;
41         int dev = FAT_ENV_DEVICE;
42         int part = FAT_ENV_PART;
43         int err;
44
45         err = env_export(&env_new);
46         if (err)
47                 return err;
48
49 #ifdef CONFIG_MMC
50         if (strcmp(FAT_ENV_INTERFACE, "mmc") == 0) {
51                 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         }
69
70         err = fat_register_device(dev_desc, part);
71         if (err) {
72                 printf("Failed to register %s%d:%d\n",
73                         FAT_ENV_INTERFACE, dev, part);
74                 return 1;
75         }
76
77         err = file_fat_write(FAT_ENV_FILE, (void *)&env_new, sizeof(env_t));
78         if (err == -1) {
79                 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
89 void env_relocate_spec(void)
90 {
91         char buf[CONFIG_ENV_SIZE];
92         block_dev_desc_t *dev_desc = NULL;
93         int dev = FAT_ENV_DEVICE;
94         int part = FAT_ENV_PART;
95         int err;
96
97 #ifdef CONFIG_MMC
98         if (strcmp(FAT_ENV_INTERFACE, "mmc") == 0) {
99                 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         }
119
120         err = fat_register_device(dev_desc, part);
121         if (err) {
122                 printf("Failed to register %s%d:%d\n",
123                         FAT_ENV_INTERFACE, dev, part);
124                 set_default_env(NULL);
125                 return;
126         }
127
128         err = file_fat_read(FAT_ENV_FILE, (uchar *)&buf, CONFIG_ENV_SIZE);
129         if (err == -1) {
130                 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 }