#include <env_default.h>
static int flash_io (int mode);
-static char *envmatch (char * s1, char * s2);
static int parse_config(struct env_opts *opts);
#if defined(CONFIG_FILE)
}
/*
+ * s1 is either a simple 'name', or a 'name=value' pair.
+ * s2 is a 'name=value' pair.
+ * If the names match, return the value of s2, else NULL.
+ */
+static char *envmatch(char *s1, char *s2)
+{
+ if (s1 == NULL || s2 == NULL)
+ return NULL;
+
+ while (*s1 == *s2++)
+ if (*s1++ == '=')
+ return s2;
+ if (*s1 == '\0' && *(s2 - 1) == '=')
+ return s2;
+ return NULL;
+}
+
+/**
* Search the environment for a variable.
* Return the value, if found, or NULL, if not found.
*/
*/
int fw_printenv(int argc, char *argv[], int value_only, struct env_opts *opts)
{
- char *env, *nxt;
int i, rc = 0;
+ if (value_only && argc != 1) {
+ fprintf(stderr,
+ "## Error: `-n' option requires exactly one argument\n");
+ return -1;
+ }
+
if (!opts)
opts = &default_opts;
return -1;
if (argc == 0) { /* Print all env variables */
+ char *env, *nxt;
for (env = environment.data; *env; env = nxt + 1) {
for (nxt = env; *nxt; ++nxt) {
if (nxt >= &environment.data[ENV_SIZE]) {
return 0;
}
- if (value_only && argc != 1) {
- fprintf(stderr,
- "## Error: `-n' option requires exactly one argument\n");
- return -1;
- }
-
- for (i = 0; i < argc; ++i) { /* print single env variables */
+ for (i = 0; i < argc; ++i) { /* print a subset of env variables */
char *name = argv[i];
char *val = NULL;
- for (env = environment.data; *env; env = nxt + 1) {
-
- for (nxt = env; *nxt; ++nxt) {
- if (nxt >= &environment.data[ENV_SIZE]) {
- fprintf (stderr, "## Error: "
- "environment not terminated\n");
- return -1;
- }
- }
- val = envmatch (name, env);
- if (val) {
- if (!value_only) {
- fputs (name, stdout);
- putc ('=', stdout);
- }
- puts (val);
- break;
- }
- }
+ val = fw_getenv(name);
if (!val) {
fprintf (stderr, "## Error: \"%s\" not defined\n", name);
rc = -1;
+ continue;
}
+
+ if (value_only) {
+ puts(val);
+ break;
+ }
+
+ printf("%s=%s\n", name, val);
}
return rc;
if (badblock) {
#ifdef DEBUG
- fprintf (stderr, "Bad block at 0x%llx, "
- "skipping\n", *blockstart);
+ fprintf (stderr, "Bad block at 0x%llx, skipping\n",
+ (unsigned long long) *blockstart);
#endif
return badblock;
}
}
#ifdef DEBUG
fprintf(stderr, "Read 0x%x bytes at 0x%llx on %s\n",
- rc, blockstart + block_seek, DEVNAME(dev));
+ rc, (unsigned long long) blockstart + block_seek,
+ DEVNAME(dev));
#endif
processed += readlen;
readlen = min (blocklen, count - processed);
if (block_seek + count != write_total) {
if (block_seek != 0)
fprintf(stderr, " and ");
- fprintf(stderr, "0x%lx - 0x%x",
- block_seek + count, write_total - 1);
+ fprintf(stderr, "0x%lx - 0x%lx",
+ (unsigned long) block_seek + count,
+ (unsigned long) write_total - 1);
}
fprintf(stderr, "\n");
#endif
}
#ifdef DEBUG
- fprintf(stderr, "Write 0x%x bytes at 0x%llx\n", erasesize,
- blockstart);
+ fprintf(stderr, "Write 0x%llx bytes at 0x%llx\n",
+ (unsigned long long) erasesize,
+ (unsigned long long) blockstart);
#endif
if (write (fd, data + processed, erasesize) != erasesize) {
fprintf (stderr, "Write error on %s: %s\n",
return rc;
}
-/*
- * s1 is either a simple 'name', or a 'name=value' pair.
- * s2 is a 'name=value' pair.
- * If the names match, return the value of s2, else NULL.
- */
-
-static char *envmatch (char * s1, char * s2)
-{
- if (s1 == NULL || s2 == NULL)
- return NULL;
-
- while (*s1 == *s2++)
- if (*s1++ == '=')
- return s2;
- if (*s1 == '\0' && *(s2 - 1) == '=')
- return s2;
- return NULL;
-}
-
/*
* Prevent confusion if running from erased flash memory
*/
struct stat st;
int fd, rc = 0;
+ if (DEVOFFSET(dev) % DEVESIZE(dev) != 0) {
+ fprintf(stderr, "Environment does not start on erase block boundary\n");
+ errno = EINVAL;
+ return -1;
+ }
+
+ if (ENVSIZE(dev) > ENVSECTORS(dev) * DEVESIZE(dev)) {
+ fprintf(stderr, "Environment does not fit into available sectors\n");
+ errno = EINVAL;
+ return -1;
+ }
+
fd = open(DEVNAME(dev), O_RDONLY);
if (fd < 0) {
fprintf(stderr,