通过搜素比对字符串保存env。
所有的env都保存在一起,每个环境变量字段都以“\0\0”结束,最后一个命令字段以“\0\0\0”结束。
每次uboot启动时,先读出环境变量。当设置新数据时,遍历整个env,确认是否有重复的,
有重复的删除掉,并把新数据设置到环境变量最后。
只有当运行saveenv时才把数据保存到硬件存储设备中。
-----------------------------------------------------------------------------------------------------------
定义于include/environment.h
102 typedef struct environment_s {
103 uint32_t crc; /* CRC32 over data bytes */
104 #ifdef CFG_REDUNDAND_ENVIRONMENT
105 unsigned char flags; /* active/obsolete flags */
106 #endif
107 unsigned char data[ENV_SIZE]; /* Environment data */
108 } env_t;
common/env_common.c中存储默认环境变量:
54 /************************************************************************
55 * Default settings to be used when no valid environment is found
56 */
57 #define XMK_STR(x) #x
58 #define MK_STR(x) XMK_STR(x)
59
60 uchar default_environment[] = {
61 #ifdef CONFIG_BOOTARGS
62 "bootargs=" CONFIG_BOOTARGS "\0"
63 #endif
64 #ifdef CONFIG_BOOTCOMMAND
65 "bootcmd=" CONFIG_BOOTCOMMAND "\0"
66 #endif
67 #ifdef CONFIG_RAMBOOTCOMMAND
68 "ramboot=" CONFIG_RAMBOOTCOMMAND "\0"
69 #endif
70 #ifdef CONFIG_NFSBOOTCOMMAND
71 "nfsboot=" CONFIG_NFSBOOTCOMMAND "\0"
72 #endif
73 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
74 "bootdelay=" MK_STR(CONFIG_BOOTDELAY) "\0"
75 #endif
76 #if defined(CONFIG_BAUDRATE) && (CONFIG_BAUDRATE >= 0)
77 "baudrate=" MK_STR(CONFIG_BAUDRATE) "\0"
78 #endif
79 #ifdef CONFIG_LOADS_ECHO
80 "loads_echo=" MK_STR(CONFIG_LOADS_ECHO) "\0"
81 #endif
82 #ifdef CONFIG_ETHADDR
83 "ethaddr=" MK_STR(CONFIG_ETHADDR) "\0"
84 #endif
85 #ifdef CONFIG_ETH1ADDR
86 "eth1addr=" MK_STR(CONFIG_ETH1ADDR) "\0"
87 #endif
88 #ifdef CONFIG_ETH2ADDR
89 "eth2addr=" MK_STR(CONFIG_ETH2ADDR) "\0"
90 #endif
91 #ifdef CONFIG_ETH3ADDR
92 "eth3addr=" MK_STR(CONFIG_ETH3ADDR) "\0"
93 #endif
94 #ifdef CONFIG_IPADDR
95 "ipaddr=" MK_STR(CONFIG_IPADDR) "\0"
96 #endif
97 #ifdef CONFIG_SERVERIP
98 "serverip=" MK_STR(CONFIG_SERVERIP) "\0"
99 #endif
100 #ifdef CFG_AUTOLOAD
101 "autoload=" CFG_AUTOLOAD "\0"
102 #endif
103 #ifdef CONFIG_PREBOOT
104 "preboot=" CONFIG_PREBOOT "\0"
105 #endif
106 #ifdef CONFIG_ROOTPATH
107 "rootpath=" MK_STR(CONFIG_ROOTPATH) "\0"
108 #endif
109 #ifdef CONFIG_GATEWAYIP
110 "gatewayip=" MK_STR(CONFIG_GATEWAYIP) "\0"
111 #endif
112 #ifdef CONFIG_NETMASK
113 "netmask=" MK_STR(CONFIG_NETMASK) "\0"
114 #endif
115 #ifdef CONFIG_HOSTNAME
116 "hostname=" MK_STR(CONFIG_HOSTNAME) "\0"
117 #endif
118 #ifdef CONFIG_BOOTFILE
119 "bootfile=" MK_STR(CONFIG_BOOTFILE) "\0"
120 #endif
121 #ifdef CONFIG_LOADADDR
122 "loadaddr=" MK_STR(CONFIG_LOADADDR) "\0"
123 #endif
124 #ifdef CONFIG_CLOCKS_IN_MHZ
125 "clocks_in_mhz=1\0"
126 #endif
127 #if defined(CONFIG_PCI_BOOTDELAY) && (CONFIG_PCI_BOOTDELAY > 0)
128 "pcidelay=" MK_STR(CONFIG_PCI_BOOTDELAY) "\0"
129 #endif
130 #ifdef CONFIG_EXTRA_ENV_SETTINGS
131 CONFIG_EXTRA_ENV_SETTINGS
132 #endif
133 "\0"
134 };
宏定义位于配置文件中,include/configs/at91sam9g10ek
153 #define CFG_USE_NORFLASH 1
154 #define PHYS_FLASH_1 0x10000000
155 #define PHYS_FLASH_SIZE 0x800000 /* 8 MB main flash */
156 #define CFG_FLASH_BASE PHYS_FLASH_1
225 #if CFG_USE_NORFLASH
226 /* u-boot + env + linux in norflash */
227 #define CFG_ENV_IS_IN_FLASH 1
228 #define CFG_ENV_OFFSET 0x38000
229 #define CFG_ENV_ADDR (PHYS_FLASH_1 + CFG_ENV_OFFSET)
230 #define CFG_ENV_OFFSET_REDUND 0x40000
231 #define CFG_ENV_SIZE 0x8000
232 #define CONFIG_BOOTCOMMAND "cp.b 0x10100000 0x20008000 0x200000; bootm 0x20008000"
233 #if 0
234 #define CONFIG_BOOTARGS "console=ttyS0,115200 " \
235 "root=/dev/mtdblock0 " \
236 "rootfstype=jffs2 " \
237 "init=linuxrc " \
238 "mem=64M " \
239 "ip=192.168.0.100:::::eth1:off " \
240 "ip=192.168.0.110:::::eth0:off"
241 #else
242 #define CONFIG_BOOTARGS "console=ttyS0,115200 root=/dev/mtdblock0 rootfstype=jffs2 init=linuxrc mem=64M ip=192.168.0.100:::::eth1:off ip=192.168.0.110:::::eth0:off panic=10"
243 #endif
276 #define CFG_MALLOC_LEN ROUND(3 * CFG_ENV_SIZE + 128*1024, 0x1000)
277 #define CFG_GBL_DATA_SIZE 128 /* 128 bytes for initial data */
278
279 #define CONFIG_STACKSIZE (32*1024) /* regular stack */
280 #define CONFIG_STACKSIZE_IRQ (32*1024) /* regular stack */
281 #define CONFIG_STACKSIZE_FIQ (32*1024) /* regular stack */
------------------------------------------------------------------------------------------------------------------------
env命令--common/cmd_nvedit.c
common/env_common.c供uboot调用的通用函数接口,隐藏了env的不同实现方式。
common/env_flash.c,env存储在flash的实现。
env_init()完成环境变量初始化工作,不同存储介质不同。
初始化流程
start.s _start
lib_arm/board.c start_armboot() ----env_init()
----env_relocate()
common/main.c main_loop()
common/env_flash.c
68 #else /* ! ENV_IS_EMBEDDED */
69
70 env_t *env_ptr = (env_t *)CFG_ENV_ADDR;
71 #ifdef CMD_SAVEENV
72 static env_t *flash_addr = (env_t *)CFG_ENV_ADDR;
73 #endif
74
75 #endif /* ENV_IS_EMBEDDED */
250 int env_init(void)
251 {
252 if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
253 gd->env_addr = (ulong)&(env_ptr->data);
254 gd->env_valid = 1;
255 return(0);
256 }
257
258 gd->env_addr = (ulong)&default_environment[0];
259 gd->env_valid = 0;
260 return (0);
261 }
common/env_common.c
224 void env_relocate (void)
225 {
226 DEBUGF ("%s[%d] offset = 0x%lx\n", __FUNCTION__,__LINE__,
227 gd->reloc_off);
228
229 #ifdef CONFIG_AMIGAONEG3SE
230 enable_nvram();
231 #endif
232
233 #ifdef ENV_IS_EMBEDDED
234 /*
235 * The environment buffer is embedded with the text segment,
236 * just relocate the environment pointer
237 */
238 env_ptr = (env_t *)((ulong)env_ptr + gd->reloc_off);
239 DEBUGF ("%s[%d] embedded ENV at %p\n", __FUNCTION__,__LINE__,env_ptr);
240 #else
241 /*
242 * We must allocate a buffer for the environment
243 */
244 env_ptr = (env_t *)malloc (CFG_ENV_SIZE);
245 DEBUGF ("%s[%d] malloced ENV at %p\n", __FUNCTION__,__LINE__,env_ptr);
246 #endif
247
248 if (gd->env_valid == 0) {
249 #if defined(CONFIG_GTH) || defined(CFG_ENV_IS_NOWHERE) /* Environment not changable */
250 puts ("Using default environment\n\n");
251 #else
252 puts ("*** Warning - bad CRC, using default environment\n\n");
253 show_boot_progress (-60);
254 #endif
255 set_default_env();
256 }
257 else {
258 env_relocate_spec ();
259 }
260 gd->env_addr = (ulong)&(env_ptr->data);
261
262 #ifdef CONFIG_AMIGAONEG3SE
263 disable_nvram();
264 #endif
265 }
注:ENV_IS_EMBEDDED没有定义,其含义为:
The environment buffer is embedded with the text segment, just relocate the environment pointer