wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 1 | /* |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 2 | * (C) Copyright 2000-2010 |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 4 | * |
| 5 | * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com> |
| 6 | * Andreas Heppel <aheppel@sysgo.de> |
| 7 | |
Wolfgang Denk | bd8ec7e | 2013-10-07 13:07:26 +0200 | [diff] [blame] | 8 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | /* |
| 12 | * 09-18-2001 Andreas Heppel, Sysgo RTS GmbH <aheppel@sysgo.de> |
| 13 | * |
| 14 | * It might not be possible in all cases to use 'memcpy()' to copy |
| 15 | * the environment to NVRAM, as the NVRAM might not be mapped into |
| 16 | * the memory space. (I.e. this is the case for the BAB750). In those |
| 17 | * cases it might be possible to access the NVRAM using a different |
| 18 | * method. For example, the RTC on the BAB750 is accessible in IO |
| 19 | * space using its address and data registers. To enable usage of |
| 20 | * NVRAM in those cases I invented the functions 'nvram_read()' and |
| 21 | * 'nvram_write()', which will be activated upon the configuration |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 22 | * #define CONFIG_SYS_NVRAM_ACCESS_ROUTINE. Note, that those functions are |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 23 | * strongly dependent on the used HW, and must be redefined for each |
| 24 | * board that wants to use them. |
| 25 | */ |
| 26 | |
| 27 | #include <common.h> |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 28 | #include <command.h> |
| 29 | #include <environment.h> |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 30 | #include <linux/stddef.h> |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 31 | #include <search.h> |
| 32 | #include <errno.h> |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 33 | |
Jean-Christophe PLAGNIOL-VILLARD | d13dd35 | 2008-09-10 22:47:59 +0200 | [diff] [blame] | 34 | DECLARE_GLOBAL_DATA_PTR; |
| 35 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 36 | #ifdef CONFIG_SYS_NVRAM_ACCESS_ROUTINE |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 37 | extern void *nvram_read(void *dest, const long src, size_t count); |
| 38 | extern void nvram_write(long dest, const void *src, size_t count); |
Igor Grinberg | 0448286 | 2011-11-07 01:14:04 +0000 | [diff] [blame] | 39 | env_t *env_ptr; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 40 | #else |
Jean-Christophe PLAGNIOL-VILLARD | 7e1cda6 | 2008-09-10 22:48:06 +0200 | [diff] [blame] | 41 | env_t *env_ptr = (env_t *)CONFIG_ENV_ADDR; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 42 | #endif |
| 43 | |
Igor Grinberg | 0448286 | 2011-11-07 01:14:04 +0000 | [diff] [blame] | 44 | char *env_name_spec = "NVRAM"; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 45 | |
Igor Grinberg | 76c02dc | 2011-12-26 03:33:10 +0000 | [diff] [blame] | 46 | #ifdef CONFIG_SYS_NVRAM_ACCESS_ROUTINE |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 47 | uchar env_get_char_spec(int index) |
wdenk | bb444c9 | 2002-12-07 00:20:59 +0000 | [diff] [blame] | 48 | { |
wdenk | bb444c9 | 2002-12-07 00:20:59 +0000 | [diff] [blame] | 49 | uchar c; |
| 50 | |
Igor Grinberg | 0448286 | 2011-11-07 01:14:04 +0000 | [diff] [blame] | 51 | nvram_read(&c, CONFIG_ENV_ADDR + index, 1); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 52 | |
wdenk | bb444c9 | 2002-12-07 00:20:59 +0000 | [diff] [blame] | 53 | return c; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 54 | } |
Igor Grinberg | 76c02dc | 2011-12-26 03:33:10 +0000 | [diff] [blame] | 55 | #endif |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 56 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 57 | void env_relocate_spec(void) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 58 | { |
Tom Rini | a840b8e | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 59 | char buf[CONFIG_ENV_SIZE]; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 60 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 61 | #if defined(CONFIG_SYS_NVRAM_ACCESS_ROUTINE) |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 62 | nvram_read(buf, CONFIG_ENV_ADDR, CONFIG_ENV_SIZE); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 63 | #else |
Tom Rini | a840b8e | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 64 | memcpy(buf, (void *)CONFIG_ENV_ADDR, CONFIG_ENV_SIZE); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 65 | #endif |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 66 | env_import(buf, 1); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 69 | int saveenv(void) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 70 | { |
Tom Rini | a840b8e | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 71 | env_t env_new; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 72 | ssize_t len; |
| 73 | char *res; |
| 74 | int rcode = 0; |
| 75 | |
Tom Rini | a840b8e | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 76 | res = (char *)&env_new.data; |
Joe Hershberger | 79a905e | 2012-12-11 22:16:23 -0600 | [diff] [blame] | 77 | len = hexport_r(&env_htab, '\0', 0, &res, ENV_SIZE, 0, NULL); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 78 | if (len < 0) { |
| 79 | error("Cannot export environment: errno = %d\n", errno); |
| 80 | return 1; |
| 81 | } |
Tom Rini | a840b8e | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 82 | env_new.crc = crc32(0, env_new.data, ENV_SIZE); |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 83 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 84 | #ifdef CONFIG_SYS_NVRAM_ACCESS_ROUTINE |
Tom Rini | a840b8e | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 85 | nvram_write(CONFIG_ENV_ADDR, &env_new, CONFIG_ENV_SIZE); |
| 86 | #else |
| 87 | if (memcpy((char *)CONFIG_ENV_ADDR, &env_new, CONFIG_ENV_SIZE) == NULL) |
| 88 | rcode = 1; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 89 | #endif |
| 90 | return rcode; |
| 91 | } |
| 92 | |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 93 | /* |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 94 | * Initialize Environment use |
| 95 | * |
| 96 | * We are still running from ROM, so data use is limited |
| 97 | */ |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 98 | int env_init(void) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 99 | { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 100 | #if defined(CONFIG_SYS_NVRAM_ACCESS_ROUTINE) |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 101 | ulong crc; |
Tom Rini | a840b8e | 2013-04-05 14:55:21 -0400 | [diff] [blame] | 102 | uchar data[ENV_SIZE]; |
Wolfgang Denk | 460a9ff | 2010-06-20 23:33:59 +0200 | [diff] [blame] | 103 | |
| 104 | nvram_read(&crc, CONFIG_ENV_ADDR, sizeof(ulong)); |
Igor Grinberg | 0448286 | 2011-11-07 01:14:04 +0000 | [diff] [blame] | 105 | nvram_read(data, CONFIG_ENV_ADDR + sizeof(ulong), ENV_SIZE); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 106 | |
| 107 | if (crc32(0, data, ENV_SIZE) == crc) { |
Igor Grinberg | 0448286 | 2011-11-07 01:14:04 +0000 | [diff] [blame] | 108 | gd->env_addr = (ulong)CONFIG_ENV_ADDR + sizeof(long); |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 109 | #else |
| 110 | if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) { |
Igor Grinberg | 0448286 | 2011-11-07 01:14:04 +0000 | [diff] [blame] | 111 | gd->env_addr = (ulong)&env_ptr->data; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 112 | #endif |
Igor Grinberg | 0448286 | 2011-11-07 01:14:04 +0000 | [diff] [blame] | 113 | gd->env_valid = 1; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 114 | } else { |
Igor Grinberg | 0448286 | 2011-11-07 01:14:04 +0000 | [diff] [blame] | 115 | gd->env_addr = (ulong)&default_environment[0]; |
| 116 | gd->env_valid = 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 117 | } |
Igor Grinberg | 0448286 | 2011-11-07 01:14:04 +0000 | [diff] [blame] | 118 | |
| 119 | return 0; |
wdenk | c609719 | 2002-11-03 00:24:07 +0000 | [diff] [blame] | 120 | } |