cmd/gpt: Support gpt command for all devices
[kernel/u-boot.git] / 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         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;
45         int err;
46
47         res = (char *)&env_new.data;
48         len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL);
49         if (len < 0) {
50                 error("Cannot export environment: errno = %d\n", errno);
51                 return 1;
52         }
53
54 #ifdef CONFIG_MMC
55         if (strcmp(FAT_ENV_INTERFACE, "mmc") == 0) {
56                 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         }
74
75         err = fat_register_device(dev_desc, part);
76         if (err) {
77                 printf("Failed to register %s%d:%d\n",
78                         FAT_ENV_INTERFACE, dev, part);
79                 return 1;
80         }
81
82         env_new.crc = crc32(0, env_new.data, ENV_SIZE);
83         err = file_fat_write(FAT_ENV_FILE, (void *)&env_new, sizeof(env_t));
84         if (err == -1) {
85                 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
95 void env_relocate_spec(void)
96 {
97         char buf[CONFIG_ENV_SIZE];
98         block_dev_desc_t *dev_desc = NULL;
99         int dev = FAT_ENV_DEVICE;
100         int part = FAT_ENV_PART;
101         int err;
102
103 #ifdef CONFIG_MMC
104         if (strcmp(FAT_ENV_INTERFACE, "mmc") == 0) {
105                 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         }
125
126         err = fat_register_device(dev_desc, part);
127         if (err) {
128                 printf("Failed to register %s%d:%d\n",
129                         FAT_ENV_INTERFACE, dev, part);
130                 set_default_env(NULL);
131                 return;
132         }
133
134         err = file_fat_read(FAT_ENV_FILE, (uchar *)&buf, CONFIG_ENV_SIZE);
135         if (err == -1) {
136                 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 }