int nvram_add_env(partition_t part, char *envvar, int evlen, char *value, int vallen) { int freespace, last, len, offset; unsigned int i; /* Find offset where we can write */ last = find_last_envvar(part); /* How much space do we have left? */ freespace = part.addr+part.len-last; /* how long is the entry we want to write? */ len = evlen + vallen + 2; if(freespace<len) { // TODO try to increase partition size return -1; } offset=last; for (i = 0; i < evlen; i++) nvram_write_byte(offset++, envvar[i]); nvram_write_byte(offset++, '='); for (i = 0; i < vallen; i++) nvram_write_byte(offset++, value[i]); return 0; }
int nvram_del_env(partition_t part, char *envvar, int evlen) { int last, current, pos, i; char *buffer; if(!part.addr) return -1; last=find_last_envvar(part); current = pos = get_past_env_pos(part, envvar, evlen); // TODO is this really required? /* go back to non-0 value */ current--; while (nvram_read_byte(current)) current--; // TODO is this required? current++; buffer=get_nvram_buffer(last-pos); for (i=0; i<last-pos; i++) buffer[i]=nvram_read_byte(i+pos); for (i=0; i<last-pos; i++) nvram_write_byte(i+current, buffer[i]); free_nvram_buffer(buffer); erase_nvram(last, current+last-pos); return 0; }
void test (void *data) { int i; unsigned char c; for (;;) { printf (&uart, "\n\nTesting NVRAM, total %d bytes.", E2END+1); puts (&uart, "\nWriting: "); for (i=0; i<=E2END; ++i) { nvram_write_byte (i, ~i); if ((i & 63) == 63) putchar (&uart, '.'); } puts (&uart, "\nReading: "); for (i=0; i<=E2END; ++i) { c = nvram_read_byte (i); if (c != (unsigned char) ~i) { printf (&uart, "error on byte 0x%x: written 0x%x, read 0x%x\n", i, (unsigned char) ~i, c); continue; } if ((i & 63) == 63) putchar (&uart, '.'); } puts (&uart, "\nPress <Enter> to continue: "); getchar (&uart); } }
static ssize_t write_nvram(struct file *file, const char *buf, size_t count, loff_t *ppos) { unsigned int i; const char *p = buf; char c; if (verify_area(VERIFY_READ, buf, count)) return -EFAULT; if (*ppos >= NVRAM_SIZE) return 0; for (i = *ppos; count > 0 && i < NVRAM_SIZE; ++i, ++p, --count) { if (__get_user(c, p)) return -EFAULT; nvram_write_byte(c, i); } *ppos = i; return p - buf; }
static ssize_t write_nvram(struct file *file, const char __user *buf, size_t count, loff_t *ppos) { unsigned int i; const char __user *p = buf; char c; if (!access_ok(VERIFY_READ, buf, count)) return -EFAULT; if (*ppos >= nvram_len) return 0; for (i = *ppos; count > 0 && i < nvram_len; ++i, ++p, --count) { if (__get_user(c, p)) return -EFAULT; nvram_write_byte(c, i); } *ppos = i; return p - buf; }
int nvram_set_env(partition_t part, char *envvar, int evlen, char *value, int vallen) { char *oldvalue, *buffer; int last, current, buffersize, i; DEBUG("nvram_set_env %lx[%lx]: %p=>%p\n", part.addr, part.len, envvar, value); if(!part.addr) return -1; /* Check whether the environment variable exists already */ oldvalue = nvram_get_env(part, envvar, evlen); if (oldvalue == NULL) return nvram_add_env(part, envvar, evlen, value, vallen); /* The value did not change. So we succeeded! */ if (strlen(oldvalue) == vallen && !strncmp(oldvalue, value, vallen)) return 0; /* we need to overwrite environment variables, back them up first */ // DEBUG("overwriting existing environment variable\n"); /* allocate a buffer */ last=find_last_envvar(part); current = get_past_env_pos(part, envvar, evlen); buffersize = last - current; buffer=get_nvram_buffer(buffersize); if(!buffer) return -1; for (i=0; i<buffersize; i++) { buffer[i] = nvram_read_byte(current+i); } /* walk back until the = */ while (nvram_read_byte(current)!='=') { current--; } /* Start at envvar= */ current++; /* Write the new value */ for(i = 0; i < vallen; i++) { nvram_write_byte(current++, value[i]); } /* Write end of string marker */ nvram_write_byte(current++, 0); /* Copy back the buffer */ for (i=0; i<buffersize; i++) { nvram_write_byte(current++, buffer[i]); } free_nvram_buffer(buffer); /* If the new environment variable content is shorter than the old one, * we need to erase the rest of the bytes */ if (current<last) { for(i=current; i<last; i++) { nvram_write_byte(i, 0); } } return 0; /* success */ }