2 * Copyright (C) 2017 Google, Inc
3 * Written by Simon Glass <sjg@chromium.org>
5 * SPDX-License-Identifier: GPL-2.0+
9 #include <environment.h>
11 DECLARE_GLOBAL_DATA_PTR;
13 static struct env_driver *env_driver_lookup(enum env_location loc)
15 struct env_driver *drv;
16 const int n_ents = ll_entry_count(struct env_driver, env_driver);
17 struct env_driver *entry;
19 drv = ll_entry_start(struct env_driver, env_driver);
20 for (entry = drv; entry != drv + n_ents; entry++) {
21 if (loc == entry->location)
29 static enum env_location env_get_default_location(void)
31 if IS_ENABLED(CONFIG_ENV_IS_IN_DATAFLASH)
32 return ENVL_DATAFLASH;
33 else if IS_ENABLED(CONFIG_ENV_IS_IN_EEPROM)
35 else if IS_ENABLED(CONFIG_ENV_IS_IN_FAT)
37 else if IS_ENABLED(CONFIG_ENV_IS_IN_FLASH)
39 else if IS_ENABLED(CONFIG_ENV_IS_IN_MMC)
41 else if IS_ENABLED(CONFIG_ENV_IS_IN_NAND)
43 else if IS_ENABLED(CONFIG_ENV_IS_IN_NVRAM)
45 else if IS_ENABLED(CONFIG_ENV_IS_IN_REMOTE)
47 else if IS_ENABLED(CONFIG_ENV_IS_IN_SPI_FLASH)
48 return ENVL_SPI_FLASH;
49 else if IS_ENABLED(CONFIG_ENV_IS_IN_UBI)
51 else if IS_ENABLED(CONFIG_ENV_IS_NOWHERE)
57 struct env_driver *env_driver_lookup_default(void)
59 enum env_location loc = env_get_default_location();
60 struct env_driver *drv;
62 drv = env_driver_lookup(loc);
64 debug("%s: No environment driver for location %d\n", __func__,
72 int env_get_char_new(int index)
74 struct env_driver *drv = env_driver_lookup_default();
80 return *(uchar *)(gd->env_addr + index);
81 ret = drv->get_char(index);
83 debug("%s: Environment failed to load (err=%d)\n",
92 struct env_driver *drv = env_driver_lookup_default();
99 drv->load(); /* TODO(sjg@chromium.org): Make this return an error */
101 debug("%s: Environment failed to load (err=%d)\n", __func__,
111 struct env_driver *drv = env_driver_lookup_default();
120 debug("%s: Environment failed to save (err=%d)\n", __func__,
128 int env_init_new(void)
130 struct env_driver *drv = env_driver_lookup_default();
137 if (ret == -ENOENT) {
138 gd->env_addr = (ulong)&default_environment[0];
143 debug("%s: Environment failed to init (err=%d)\n", __func__,
151 unsigned char env_get_char_spec(int index)
153 return *(uchar *)(gd->env_addr + index);
156 void env_relocate_spec(void)
168 return env_init_new();