env: Drop common init() functions
[oweals/u-boot.git] / 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 <memalign.h>
17 #include <search.h>
18 #include <errno.h>
19 #include <fat.h>
20 #include <mmc.h>
21
22 #ifdef CONFIG_SPL_BUILD
23 /* TODO(sjg@chromium.org): Figure out why this is needed */
24 # if !defined(CONFIG_TARGET_AM335X_EVM) || defined(CONFIG_SPL_OS_BOOT)
25 #  define LOADENV
26 # endif
27 #else
28 # define LOADENV
29 # if defined(CONFIG_CMD_SAVEENV)
30 #  define CMD_SAVEENV
31 # endif
32 #endif
33
34 char *env_name_spec = "FAT";
35
36 env_t *env_ptr;
37
38 DECLARE_GLOBAL_DATA_PTR;
39
40 #ifdef CMD_SAVEENV
41 static int env_fat_save(void)
42 {
43         env_t   env_new;
44         struct blk_desc *dev_desc = NULL;
45         disk_partition_t info;
46         int dev, part;
47         int err;
48         loff_t size;
49
50         err = env_export(&env_new);
51         if (err)
52                 return err;
53
54         part = blk_get_device_part_str(CONFIG_ENV_FAT_INTERFACE,
55                                         CONFIG_ENV_FAT_DEVICE_AND_PART,
56                                         &dev_desc, &info, 1);
57         if (part < 0)
58                 return 1;
59
60         dev = dev_desc->devnum;
61         if (fat_set_blk_dev(dev_desc, &info) != 0) {
62                 printf("\n** Unable to use %s %d:%d for saveenv **\n",
63                        CONFIG_ENV_FAT_INTERFACE, dev, part);
64                 return 1;
65         }
66
67         err = file_fat_write(CONFIG_ENV_FAT_FILE, (void *)&env_new, 0, sizeof(env_t),
68                              &size);
69         if (err == -1) {
70                 printf("\n** Unable to write \"%s\" from %s%d:%d **\n",
71                         CONFIG_ENV_FAT_FILE, CONFIG_ENV_FAT_INTERFACE, dev, part);
72                 return 1;
73         }
74
75         puts("done\n");
76         return 0;
77 }
78 #endif /* CMD_SAVEENV */
79
80 #ifdef LOADENV
81 static void env_fat_load(void)
82 {
83         ALLOC_CACHE_ALIGN_BUFFER(char, buf, CONFIG_ENV_SIZE);
84         struct blk_desc *dev_desc = NULL;
85         disk_partition_t info;
86         int dev, part;
87         int err;
88
89         part = blk_get_device_part_str(CONFIG_ENV_FAT_INTERFACE,
90                                         CONFIG_ENV_FAT_DEVICE_AND_PART,
91                                         &dev_desc, &info, 1);
92         if (part < 0)
93                 goto err_env_relocate;
94
95         dev = dev_desc->devnum;
96         if (fat_set_blk_dev(dev_desc, &info) != 0) {
97                 printf("\n** Unable to use %s %d:%d for loading the env **\n",
98                        CONFIG_ENV_FAT_INTERFACE, dev, part);
99                 goto err_env_relocate;
100         }
101
102         err = file_fat_read(CONFIG_ENV_FAT_FILE, buf, CONFIG_ENV_SIZE);
103         if (err == -1) {
104                 printf("\n** Unable to read \"%s\" from %s%d:%d **\n",
105                         CONFIG_ENV_FAT_FILE, CONFIG_ENV_FAT_INTERFACE, dev, part);
106                 goto err_env_relocate;
107         }
108
109         env_import(buf, 1);
110         return;
111
112 err_env_relocate:
113         set_default_env(NULL);
114 }
115 #endif /* LOADENV */
116
117 U_BOOT_ENV_LOCATION(fat) = {
118         .location       = ENVL_FAT,
119 #ifdef LOADENV
120         .load           = env_fat_load,
121 #endif
122 #ifdef CMD_SAVEENV
123         .save           = env_save_ptr(env_fat_save),
124 #endif
125 };