1 /******************************************************************************
2 * Copyright (c) 2004, 2008 IBM Corporation
4 * This program and the accompanying materials
5 * are made available under the terms of the BSD License
6 * which accompanies this distribution, and is available at
7 * http://www.opensource.org/licenses/bsd-license.php
10 * IBM Corporation - initial implementation
11 *****************************************************************************/
14 #include "../libc/include/stdio.h"
15 #include "../libc/include/string.h"
16 #include "../libc/include/stdlib.h"
19 /* returns the offset of the first byte after the searched envvar */
20 static int get_past_env_pos(partition_t part, char *envvar, int evlen)
23 static char temp[256];
32 while((data=nvram_read_byte(offset++)) && len < 256) {
35 if (!strncmp(envvar, temp, evlen)) {
44 * @param partition name of the envvar partition
45 * @param envvar name of the environment variable
46 * @param evlen string length of the envvar parameter
47 * @return pointer to temporary string containing the value of envvar
49 char *nvram_get_env(partition_t part, char *envvar, int evlen)
51 static char temp[256+1];
55 DEBUG("nvram_get_env %p... ", envvar);
57 /* ERROR: No environment variable partition */
58 DEBUG("invalid partition.\n");
66 while((data=nvram_read_byte(offset++)) && len < 256) {
71 if (!strncmp(envvar, temp, evlen)) {
73 while (temp[pos]!='=' && pos < len) pos++;
74 // DEBUG("value='%s'\n", temp+pos+1);
83 static int find_last_envvar(partition_t part)
85 uint8_t last, current;
90 last=nvram_read_byte(part.addr);
92 for (offset=part.addr; offset<(int)(part.addr+part.len); offset++) {
93 current=nvram_read_byte(offset);
103 int nvram_add_env(partition_t part, char *envvar, int evlen, char *value, int vallen)
105 int freespace, last, len, offset;
108 /* Find offset where we can write */
109 last = find_last_envvar(part);
111 /* How much space do we have left? */
112 freespace = part.addr+part.len-last;
114 /* how long is the entry we want to write? */
115 len = evlen + vallen + 2;
118 // TODO try to increase partition size
124 for (i = 0; i < evlen; i++)
125 nvram_write_byte(offset++, envvar[i]);
127 nvram_write_byte(offset++, '=');
129 for (i = 0; i < vallen; i++)
130 nvram_write_byte(offset++, value[i]);
135 int nvram_del_env(partition_t part, char *envvar, int evlen)
137 int last, current, pos, i;
143 last=find_last_envvar(part);
144 current = pos = get_past_env_pos(part, envvar, evlen);
146 // TODO is this really required?
147 /* go back to non-0 value */
150 while (nvram_read_byte(current))
153 // TODO is this required?
156 buffer=get_nvram_buffer(last-pos);
158 for (i=0; i<last-pos; i++)
159 buffer[i]=nvram_read_byte(i+pos);
161 for (i=0; i<last-pos; i++)
162 nvram_write_byte(i+current, buffer[i]);
164 free_nvram_buffer(buffer);
166 erase_nvram(last, current+last-pos);
171 int nvram_set_env(partition_t part, char *envvar, int evlen, char *value, int vallen)
173 char *oldvalue, *buffer;
174 int last, current, buffersize, i;
176 DEBUG("nvram_set_env %lx[%lx]: %p=>%p\n", part.addr, part.len, envvar, value);
181 /* Check whether the environment variable exists already */
182 oldvalue = nvram_get_env(part, envvar, evlen);
184 if (oldvalue == NULL)
185 return nvram_add_env(part, envvar, evlen, value, vallen);
188 /* The value did not change. So we succeeded! */
189 if (strlen(oldvalue) == vallen && !strncmp(oldvalue, value, vallen))
192 /* we need to overwrite environment variables, back them up first */
194 // DEBUG("overwriting existing environment variable\n");
196 /* allocate a buffer */
197 last=find_last_envvar(part);
198 current = get_past_env_pos(part, envvar, evlen);
199 buffersize = last - current;
200 buffer=get_nvram_buffer(buffersize);
204 for (i=0; i<buffersize; i++) {
205 buffer[i] = nvram_read_byte(current+i);
208 /* walk back until the = */
209 while (nvram_read_byte(current)!='=') {
213 /* Start at envvar= */
216 /* Write the new value */
217 for(i = 0; i < vallen; i++) {
218 nvram_write_byte(current++, value[i]);
221 /* Write end of string marker */
222 nvram_write_byte(current++, 0);
224 /* Copy back the buffer */
225 for (i=0; i<buffersize; i++) {
226 nvram_write_byte(current++, buffer[i]);
229 free_nvram_buffer(buffer);
231 /* If the new environment variable content is shorter than the old one,
232 * we need to erase the rest of the bytes
236 for(i=current; i<last; i++) {
237 nvram_write_byte(i, 0);
241 return 0; /* success */