Blame view

env/nvram.c 3.08 KB
83d290c56   Tom Rini   SPDX: Convert all...
1
  // SPDX-License-Identifier: GPL-2.0+
c609719b8   wdenk   Initial revision
2
  /*
ea882baf9   Wolfgang Denk   New implementatio...
3
   * (C) Copyright 2000-2010
c609719b8   wdenk   Initial revision
4
5
6
7
   * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
   *
   * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
   * Andreas Heppel <aheppel@sysgo.de>
c609719b8   wdenk   Initial revision
8
9
10
11
12
13
14
15
16
17
18
19
20
   */
  
  /*
   * 09-18-2001 Andreas Heppel, Sysgo RTS GmbH <aheppel@sysgo.de>
   *
   * It might not be possible in all cases to use 'memcpy()' to copy
   * the environment to NVRAM, as the NVRAM might not be mapped into
   * the memory space. (I.e. this is the case for the BAB750). In those
   * cases it might be possible to access the NVRAM using a different
   * method. For example, the RTC on the BAB750 is accessible in IO
   * space using its address and data registers. To enable usage of
   * NVRAM in those cases I invented the functions 'nvram_read()' and
   * 'nvram_write()', which will be activated upon the configuration
6d0f6bcf3   Jean-Christophe PLAGNIOL-VILLARD   rename CFG_ macro...
21
   * #define CONFIG_SYS_NVRAM_ACCESS_ROUTINE. Note, that those functions are
c609719b8   wdenk   Initial revision
22
23
24
25
26
   * strongly dependent on the used HW, and must be redefined for each
   * board that wants to use them.
   */
  
  #include <common.h>
c609719b8   wdenk   Initial revision
27
  #include <command.h>
7b51b576d   Simon Glass   env: Move env_get...
28
  #include <env.h>
f3998fdc4   Simon Glass   env: Rename envir...
29
  #include <env_internal.h>
c609719b8   wdenk   Initial revision
30
  #include <linux/stddef.h>
ea882baf9   Wolfgang Denk   New implementatio...
31
32
  #include <search.h>
  #include <errno.h>
3db711085   Simon Glass   crc32: Use the cr...
33
  #include <u-boot/crc.h>
c609719b8   wdenk   Initial revision
34

957a0e695   Jean-Christophe PLAGNIOL-VILLARD   env_nvram: Move c...
35
  DECLARE_GLOBAL_DATA_PTR;
6d0f6bcf3   Jean-Christophe PLAGNIOL-VILLARD   rename CFG_ macro...
36
  #ifdef CONFIG_SYS_NVRAM_ACCESS_ROUTINE
c609719b8   wdenk   Initial revision
37
38
  extern void *nvram_read(void *dest, const long src, size_t count);
  extern void nvram_write(long dest, const void *src, size_t count);
c609719b8   wdenk   Initial revision
39
  #else
0e8d15866   Jean-Christophe PLAGNIOL-VILLARD   rename CFG_ENV ma...
40
  env_t *env_ptr = (env_t *)CONFIG_ENV_ADDR;
c609719b8   wdenk   Initial revision
41
  #endif
bf95df44f   Igor Grinberg   env: factor out t...
42
  #ifdef CONFIG_SYS_NVRAM_ACCESS_ROUTINE
b2cdef486   Goldschmidt Simon   env: restore old ...
43
44
45
46
  /** Call this function from overridden env_get_char_spec() if you need
   * this functionality.
   */
  int env_nvram_get_char(int index)
c609719b8   wdenk   Initial revision
47
  {
c609719b8   wdenk   Initial revision
48
  	uchar c;
91494ca6a   Igor Grinberg   env: clean env_nv...
49
  	nvram_read(&c, CONFIG_ENV_ADDR + index, 1);
c609719b8   wdenk   Initial revision
50
51
  
  	return c;
c609719b8   wdenk   Initial revision
52
  }
bf95df44f   Igor Grinberg   env: factor out t...
53
  #endif
c609719b8   wdenk   Initial revision
54

c59519919   Simon Glass   env: Adjust the l...
55
  static int env_nvram_load(void)
c609719b8   wdenk   Initial revision
56
  {
cd0f4fa1c   Tom Rini   Revert "env: fix ...
57
  	char buf[CONFIG_ENV_SIZE];
ea882baf9   Wolfgang Denk   New implementatio...
58

6d0f6bcf3   Jean-Christophe PLAGNIOL-VILLARD   rename CFG_ macro...
59
  #if defined(CONFIG_SYS_NVRAM_ACCESS_ROUTINE)
ea882baf9   Wolfgang Denk   New implementatio...
60
  	nvram_read(buf, CONFIG_ENV_ADDR, CONFIG_ENV_SIZE);
c609719b8   wdenk   Initial revision
61
  #else
cd0f4fa1c   Tom Rini   Revert "env: fix ...
62
  	memcpy(buf, (void *)CONFIG_ENV_ADDR, CONFIG_ENV_SIZE);
c609719b8   wdenk   Initial revision
63
  #endif
2166ebf78   Simon Goldschmidt   env: make env dri...
64
  	return env_import(buf, 1);
c609719b8   wdenk   Initial revision
65
  }
e5bce247b   Simon Glass   env: Switch over ...
66
  static int env_nvram_save(void)
c609719b8   wdenk   Initial revision
67
  {
cd0f4fa1c   Tom Rini   Revert "env: fix ...
68
  	env_t	env_new;
ea882baf9   Wolfgang Denk   New implementatio...
69
  	int	rcode = 0;
7ce1526ed   Marek Vasut   env: Add env_expo...
70
71
72
  	rcode = env_export(&env_new);
  	if (rcode)
  		return rcode;
ea882baf9   Wolfgang Denk   New implementatio...
73

6d0f6bcf3   Jean-Christophe PLAGNIOL-VILLARD   rename CFG_ macro...
74
  #ifdef CONFIG_SYS_NVRAM_ACCESS_ROUTINE
cd0f4fa1c   Tom Rini   Revert "env: fix ...
75
76
77
78
  	nvram_write(CONFIG_ENV_ADDR, &env_new, CONFIG_ENV_SIZE);
  #else
  	if (memcpy((char *)CONFIG_ENV_ADDR, &env_new, CONFIG_ENV_SIZE) == NULL)
  		rcode = 1;
c609719b8   wdenk   Initial revision
79
80
81
  #endif
  	return rcode;
  }
ea882baf9   Wolfgang Denk   New implementatio...
82
  /*
c609719b8   wdenk   Initial revision
83
84
85
86
   * Initialize Environment use
   *
   * We are still running from ROM, so data use is limited
   */
e5bce247b   Simon Glass   env: Switch over ...
87
  static int env_nvram_init(void)
c609719b8   wdenk   Initial revision
88
  {
6d0f6bcf3   Jean-Christophe PLAGNIOL-VILLARD   rename CFG_ macro...
89
  #if defined(CONFIG_SYS_NVRAM_ACCESS_ROUTINE)
c609719b8   wdenk   Initial revision
90
  	ulong crc;
cd0f4fa1c   Tom Rini   Revert "env: fix ...
91
  	uchar data[ENV_SIZE];
ea882baf9   Wolfgang Denk   New implementatio...
92
93
  
  	nvram_read(&crc, CONFIG_ENV_ADDR, sizeof(ulong));
91494ca6a   Igor Grinberg   env: clean env_nv...
94
  	nvram_read(data, CONFIG_ENV_ADDR + sizeof(ulong), ENV_SIZE);
c609719b8   wdenk   Initial revision
95
96
  
  	if (crc32(0, data, ENV_SIZE) == crc) {
91494ca6a   Igor Grinberg   env: clean env_nv...
97
  		gd->env_addr	= (ulong)CONFIG_ENV_ADDR + sizeof(long);
c609719b8   wdenk   Initial revision
98
99
  #else
  	if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
91494ca6a   Igor Grinberg   env: clean env_nv...
100
  		gd->env_addr	= (ulong)&env_ptr->data;
c609719b8   wdenk   Initial revision
101
  #endif
203e94f6c   Simon Glass   env: Add an enum ...
102
  		gd->env_valid = ENV_VALID;
c609719b8   wdenk   Initial revision
103
  	} else {
91494ca6a   Igor Grinberg   env: clean env_nv...
104
  		gd->env_addr	= (ulong)&default_environment[0];
2d7cb5b42   Simon Glass   env: Replace all ...
105
  		gd->env_valid	= ENV_INVALID;
c609719b8   wdenk   Initial revision
106
  	}
91494ca6a   Igor Grinberg   env: clean env_nv...
107
108
  
  	return 0;
c609719b8   wdenk   Initial revision
109
  }
4415f1d1f   Simon Glass   env: Create a loc...
110
111
112
  
  U_BOOT_ENV_LOCATION(nvram) = {
  	.location	= ENVL_NVRAM,
ac358beb8   Simon Glass   env: Drop the env...
113
  	ENV_NAME("NVRAM")
e5bce247b   Simon Glass   env: Switch over ...
114
115
116
  	.load		= env_nvram_load,
  	.save		= env_save_ptr(env_nvram_save),
  	.init		= env_nvram_init,
4415f1d1f   Simon Glass   env: Create a loc...
117
  };