xref: /openbmc/u-boot/env/nvram.c (revision 83d290c56fab2d38cd1ab4c4cc7099559c1d5046)
1*83d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
20649cd0dSSimon Glass /*
30649cd0dSSimon Glass  * (C) Copyright 2000-2010
40649cd0dSSimon Glass  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
50649cd0dSSimon Glass  *
60649cd0dSSimon Glass  * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
70649cd0dSSimon Glass  * Andreas Heppel <aheppel@sysgo.de>
80649cd0dSSimon Glass  */
90649cd0dSSimon Glass 
100649cd0dSSimon Glass /*
110649cd0dSSimon Glass  * 09-18-2001 Andreas Heppel, Sysgo RTS GmbH <aheppel@sysgo.de>
120649cd0dSSimon Glass  *
130649cd0dSSimon Glass  * It might not be possible in all cases to use 'memcpy()' to copy
140649cd0dSSimon Glass  * the environment to NVRAM, as the NVRAM might not be mapped into
150649cd0dSSimon Glass  * the memory space. (I.e. this is the case for the BAB750). In those
160649cd0dSSimon Glass  * cases it might be possible to access the NVRAM using a different
170649cd0dSSimon Glass  * method. For example, the RTC on the BAB750 is accessible in IO
180649cd0dSSimon Glass  * space using its address and data registers. To enable usage of
190649cd0dSSimon Glass  * NVRAM in those cases I invented the functions 'nvram_read()' and
200649cd0dSSimon Glass  * 'nvram_write()', which will be activated upon the configuration
210649cd0dSSimon Glass  * #define CONFIG_SYS_NVRAM_ACCESS_ROUTINE. Note, that those functions are
220649cd0dSSimon Glass  * strongly dependent on the used HW, and must be redefined for each
230649cd0dSSimon Glass  * board that wants to use them.
240649cd0dSSimon Glass  */
250649cd0dSSimon Glass 
260649cd0dSSimon Glass #include <common.h>
270649cd0dSSimon Glass #include <command.h>
280649cd0dSSimon Glass #include <environment.h>
290649cd0dSSimon Glass #include <linux/stddef.h>
300649cd0dSSimon Glass #include <search.h>
310649cd0dSSimon Glass #include <errno.h>
320649cd0dSSimon Glass 
330649cd0dSSimon Glass DECLARE_GLOBAL_DATA_PTR;
340649cd0dSSimon Glass 
350649cd0dSSimon Glass #ifdef CONFIG_SYS_NVRAM_ACCESS_ROUTINE
360649cd0dSSimon Glass extern void *nvram_read(void *dest, const long src, size_t count);
370649cd0dSSimon Glass extern void nvram_write(long dest, const void *src, size_t count);
380649cd0dSSimon Glass #else
390649cd0dSSimon Glass env_t *env_ptr = (env_t *)CONFIG_ENV_ADDR;
400649cd0dSSimon Glass #endif
410649cd0dSSimon Glass 
420649cd0dSSimon Glass #ifdef CONFIG_SYS_NVRAM_ACCESS_ROUTINE
43b2cdef48SGoldschmidt Simon /** Call this function from overridden env_get_char_spec() if you need
44b2cdef48SGoldschmidt Simon  * this functionality.
45b2cdef48SGoldschmidt Simon  */
env_nvram_get_char(int index)46b2cdef48SGoldschmidt Simon int env_nvram_get_char(int index)
470649cd0dSSimon Glass {
480649cd0dSSimon Glass 	uchar c;
490649cd0dSSimon Glass 
500649cd0dSSimon Glass 	nvram_read(&c, CONFIG_ENV_ADDR + index, 1);
510649cd0dSSimon Glass 
520649cd0dSSimon Glass 	return c;
530649cd0dSSimon Glass }
540649cd0dSSimon Glass #endif
550649cd0dSSimon Glass 
env_nvram_load(void)56c5951991SSimon Glass static int env_nvram_load(void)
570649cd0dSSimon Glass {
580649cd0dSSimon Glass 	char buf[CONFIG_ENV_SIZE];
590649cd0dSSimon Glass 
600649cd0dSSimon Glass #if defined(CONFIG_SYS_NVRAM_ACCESS_ROUTINE)
610649cd0dSSimon Glass 	nvram_read(buf, CONFIG_ENV_ADDR, CONFIG_ENV_SIZE);
620649cd0dSSimon Glass #else
630649cd0dSSimon Glass 	memcpy(buf, (void *)CONFIG_ENV_ADDR, CONFIG_ENV_SIZE);
640649cd0dSSimon Glass #endif
652166ebf7SSimon Goldschmidt 	return env_import(buf, 1);
660649cd0dSSimon Glass }
670649cd0dSSimon Glass 
env_nvram_save(void)68e5bce247SSimon Glass static int env_nvram_save(void)
690649cd0dSSimon Glass {
700649cd0dSSimon Glass 	env_t	env_new;
710649cd0dSSimon Glass 	int	rcode = 0;
720649cd0dSSimon Glass 
730649cd0dSSimon Glass 	rcode = env_export(&env_new);
740649cd0dSSimon Glass 	if (rcode)
750649cd0dSSimon Glass 		return rcode;
760649cd0dSSimon Glass 
770649cd0dSSimon Glass #ifdef CONFIG_SYS_NVRAM_ACCESS_ROUTINE
780649cd0dSSimon Glass 	nvram_write(CONFIG_ENV_ADDR, &env_new, CONFIG_ENV_SIZE);
790649cd0dSSimon Glass #else
800649cd0dSSimon Glass 	if (memcpy((char *)CONFIG_ENV_ADDR, &env_new, CONFIG_ENV_SIZE) == NULL)
810649cd0dSSimon Glass 		rcode = 1;
820649cd0dSSimon Glass #endif
830649cd0dSSimon Glass 	return rcode;
840649cd0dSSimon Glass }
850649cd0dSSimon Glass 
860649cd0dSSimon Glass /*
870649cd0dSSimon Glass  * Initialize Environment use
880649cd0dSSimon Glass  *
890649cd0dSSimon Glass  * We are still running from ROM, so data use is limited
900649cd0dSSimon Glass  */
env_nvram_init(void)91e5bce247SSimon Glass static int env_nvram_init(void)
920649cd0dSSimon Glass {
930649cd0dSSimon Glass #if defined(CONFIG_SYS_NVRAM_ACCESS_ROUTINE)
940649cd0dSSimon Glass 	ulong crc;
950649cd0dSSimon Glass 	uchar data[ENV_SIZE];
960649cd0dSSimon Glass 
970649cd0dSSimon Glass 	nvram_read(&crc, CONFIG_ENV_ADDR, sizeof(ulong));
980649cd0dSSimon Glass 	nvram_read(data, CONFIG_ENV_ADDR + sizeof(ulong), ENV_SIZE);
990649cd0dSSimon Glass 
1000649cd0dSSimon Glass 	if (crc32(0, data, ENV_SIZE) == crc) {
1010649cd0dSSimon Glass 		gd->env_addr	= (ulong)CONFIG_ENV_ADDR + sizeof(long);
1020649cd0dSSimon Glass #else
1030649cd0dSSimon Glass 	if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
1040649cd0dSSimon Glass 		gd->env_addr	= (ulong)&env_ptr->data;
1050649cd0dSSimon Glass #endif
106203e94f6SSimon Glass 		gd->env_valid = ENV_VALID;
1070649cd0dSSimon Glass 	} else {
1080649cd0dSSimon Glass 		gd->env_addr	= (ulong)&default_environment[0];
1092d7cb5b4SSimon Glass 		gd->env_valid	= ENV_INVALID;
1100649cd0dSSimon Glass 	}
1110649cd0dSSimon Glass 
1120649cd0dSSimon Glass 	return 0;
1130649cd0dSSimon Glass }
1144415f1d1SSimon Glass 
1154415f1d1SSimon Glass U_BOOT_ENV_LOCATION(nvram) = {
1164415f1d1SSimon Glass 	.location	= ENVL_NVRAM,
117ac358bebSSimon Glass 	ENV_NAME("NVRAM")
118e5bce247SSimon Glass 	.load		= env_nvram_load,
119e5bce247SSimon Glass 	.save		= env_save_ptr(env_nvram_save),
120e5bce247SSimon Glass 	.init		= env_nvram_init,
1214415f1d1SSimon Glass };
122