1 /* 2 * (C) Copyright 2000-2010 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 8 * SPDX-License-Identifier: GPL-2.0+ 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 22 * #define CONFIG_SYS_NVRAM_ACCESS_ROUTINE. Note, that those functions are 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> 28 #include <command.h> 29 #include <environment.h> 30 #include <linux/stddef.h> 31 #include <search.h> 32 #include <errno.h> 33 34 DECLARE_GLOBAL_DATA_PTR; 35 36 #ifdef CONFIG_SYS_NVRAM_ACCESS_ROUTINE 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); 39 env_t *env_ptr; 40 #else 41 env_t *env_ptr = (env_t *)CONFIG_ENV_ADDR; 42 #endif 43 44 #ifdef CONFIG_SYS_NVRAM_ACCESS_ROUTINE 45 static uchar env_nvram_get_char(int index) 46 { 47 uchar c; 48 49 nvram_read(&c, CONFIG_ENV_ADDR + index, 1); 50 51 return c; 52 } 53 #endif 54 55 static void env_nvram_load(void) 56 { 57 char buf[CONFIG_ENV_SIZE]; 58 59 #if defined(CONFIG_SYS_NVRAM_ACCESS_ROUTINE) 60 nvram_read(buf, CONFIG_ENV_ADDR, CONFIG_ENV_SIZE); 61 #else 62 memcpy(buf, (void *)CONFIG_ENV_ADDR, CONFIG_ENV_SIZE); 63 #endif 64 env_import(buf, 1); 65 } 66 67 static int env_nvram_save(void) 68 { 69 env_t env_new; 70 int rcode = 0; 71 72 rcode = env_export(&env_new); 73 if (rcode) 74 return rcode; 75 76 #ifdef CONFIG_SYS_NVRAM_ACCESS_ROUTINE 77 nvram_write(CONFIG_ENV_ADDR, &env_new, CONFIG_ENV_SIZE); 78 #else 79 if (memcpy((char *)CONFIG_ENV_ADDR, &env_new, CONFIG_ENV_SIZE) == NULL) 80 rcode = 1; 81 #endif 82 return rcode; 83 } 84 85 /* 86 * Initialize Environment use 87 * 88 * We are still running from ROM, so data use is limited 89 */ 90 static int env_nvram_init(void) 91 { 92 #if defined(CONFIG_SYS_NVRAM_ACCESS_ROUTINE) 93 ulong crc; 94 uchar data[ENV_SIZE]; 95 96 nvram_read(&crc, CONFIG_ENV_ADDR, sizeof(ulong)); 97 nvram_read(data, CONFIG_ENV_ADDR + sizeof(ulong), ENV_SIZE); 98 99 if (crc32(0, data, ENV_SIZE) == crc) { 100 gd->env_addr = (ulong)CONFIG_ENV_ADDR + sizeof(long); 101 #else 102 if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) { 103 gd->env_addr = (ulong)&env_ptr->data; 104 #endif 105 gd->env_valid = ENV_VALID; 106 } else { 107 gd->env_addr = (ulong)&default_environment[0]; 108 gd->env_valid = 0; 109 } 110 111 return 0; 112 } 113 114 U_BOOT_ENV_LOCATION(nvram) = { 115 .location = ENVL_NVRAM, 116 ENV_NAME("NVRAM") 117 #ifdef CONFIG_SYS_NVRAM_ACCESS_ROUTINE 118 .get_char = env_nvram_get_char, 119 #endif 120 .load = env_nvram_load, 121 .save = env_save_ptr(env_nvram_save), 122 .init = env_nvram_init, 123 }; 124