Newer
Older
if (rc < 0)
return rc;
if (environment.flag_scheme == FLAG_BOOLEAN) {
/* Have to set obsolete flag */
off_t offset = DEVOFFSET (dev_current) +
offsetof (struct env_image_redundant, flags);
#ifdef DEBUG
fprintf(stderr,
"Setting obsolete flag in environment at 0x%lx on %s\n",
DEVOFFSET (dev_current), DEVNAME (dev_current));
#endif
flash_flag_obsolete (dev_current, fd_current, offset);
}
return 0;
}
static int flash_read (int fd)
{
struct mtd_info_user mtdinfo;
fprintf(stderr, "Cannot stat the file %s\n",
DEVNAME(dev_current));
if (S_ISCHR(st.st_mode)) {
rc = ioctl(fd, MEMGETINFO, &mtdinfo);
if (rc < 0) {
fprintf(stderr, "Cannot get MTD information for %s\n",
DEVNAME(dev_current));
return -1;
}
if (mtdinfo.type != MTD_NORFLASH &&
mtdinfo.type != MTD_NANDFLASH &&
mtdinfo.type != MTD_DATAFLASH &&
mtdinfo.type != MTD_UBIVOLUME) {
fprintf (stderr, "Unsupported flash type %u on %s\n",
mtdinfo.type, DEVNAME(dev_current));
return -1;
}
} else {
memset(&mtdinfo, 0, sizeof(mtdinfo));
mtdinfo.type = MTD_ABSENT;
}
DEVTYPE(dev_current) = mtdinfo.type;
rc = flash_read_buf(dev_current, fd, environment.image, CUR_ENVSIZE,
DEVOFFSET (dev_current), mtdinfo.type);
if (rc != CUR_ENVSIZE)
return -1;
}
static int flash_io (int mode)
{
int fd_current, fd_target, rc, dev_target;
/* dev_current: fd_current, erase_current */
fd_current = open (DEVNAME (dev_current), mode);
if (fd_current < 0) {
fprintf (stderr,
"Can't open %s: %s\n",
DEVNAME (dev_current), strerror (errno));
return -1;
}
if (mode == O_RDWR) {
if (HaveRedundEnv) {
/* switch to next partition for writing */
dev_target = !dev_current;
/* dev_target: fd_target, erase_target */
fd_target = open (DEVNAME (dev_target), mode);
if (fd_target < 0) {
fprintf (stderr,
"Can't open %s: %s\n",
DEVNAME (dev_target),
strerror (errno));
rc = -1;
goto exit;
} else {
dev_target = dev_current;
fd_target = fd_current;
rc = flash_write (fd_current, fd_target, dev_target);
if (HaveRedundEnv) {
fprintf (stderr,
}
} else {
fprintf (stderr,
"I/O error on %s: %s\n",
DEVNAME (dev_current), strerror (errno));
return -1;
}
/*
* 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++ == '=')
if (*s1 == '\0' && *(s2 - 1) == '=')
}
/*
* Prevent confusion if running from erased flash memory
*/
int fw_env_open(void)
int crc1, crc1_ok;
struct env_image_single *single;
struct env_image_redundant *redundant;
if (parse_config ()) /* should fill envdevices */
addr0 = calloc(1, CUR_ENVSIZE);
"Not enough memory for environment (%ld bytes)\n",
/* read environment from FLASH to local buffer */
environment.image = addr0;
if (HaveRedundEnv) {
redundant = addr0;
environment.crc = &redundant->crc;
environment.flags = &redundant->flags;
environment.data = redundant->data;
} else {
single = addr0;
environment.crc = &single->crc;
environment.flags = NULL;
environment.data = single->data;
dev_current = 0;
if (flash_io (O_RDONLY))
return -1;
crc0 = crc32 (0, (uint8_t *) environment.data, ENV_SIZE);
if (common_args.aes_flag) {
ret = env_aes_cbc_crypt(environment.data, 0,
common_args.aes_key);
if (ret)
return ret;
}
if (!HaveRedundEnv) {
fprintf (stderr,
"Warning: Bad CRC, using default environment\n");
memcpy(environment.data, default_environment, sizeof default_environment);
addr1 = calloc(1, CUR_ENVSIZE);
"Not enough memory for environment (%ld bytes)\n",
/*
* have to set environment.image for flash_read(), careful -
* other pointers in environment still point inside addr0
*/
environment.image = addr1;
if (flash_io (O_RDONLY))
return -1;
/* Check flag scheme compatibility */
if (DEVTYPE(dev_current) == MTD_NORFLASH &&
DEVTYPE(!dev_current) == MTD_NORFLASH) {
environment.flag_scheme = FLAG_BOOLEAN;
} else if (DEVTYPE(dev_current) == MTD_NANDFLASH &&
DEVTYPE(!dev_current) == MTD_NANDFLASH) {
environment.flag_scheme = FLAG_INCREMENTAL;
} else if (DEVTYPE(dev_current) == MTD_DATAFLASH &&
DEVTYPE(!dev_current) == MTD_DATAFLASH) {
environment.flag_scheme = FLAG_BOOLEAN;
} else if (DEVTYPE(dev_current) == MTD_UBIVOLUME &&
DEVTYPE(!dev_current) == MTD_UBIVOLUME) {
environment.flag_scheme = FLAG_INCREMENTAL;
} else if (DEVTYPE(dev_current) == MTD_ABSENT &&
DEVTYPE(!dev_current) == MTD_ABSENT) {
environment.flag_scheme = FLAG_INCREMENTAL;
} else {
fprintf (stderr, "Incompatible flash types!\n");
return -1;
crc1 = crc32 (0, (uint8_t *) redundant->data, ENV_SIZE);
if (common_args.aes_flag) {
ret = env_aes_cbc_crypt(redundant->data, 0,
common_args.aes_key);
if (ret)
return ret;
}
crc1_ok = (crc1 == redundant->crc);
flag1 = redundant->flags;
if (crc0_ok && !crc1_ok) {
dev_current = 0;
} else if (!crc0_ok && crc1_ok) {
dev_current = 1;
} else if (!crc0_ok && !crc1_ok) {
fprintf (stderr,
"Warning: Bad CRC, using default environment\n");
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
memcpy (environment.data, default_environment,
sizeof default_environment);
dev_current = 0;
} else {
switch (environment.flag_scheme) {
case FLAG_BOOLEAN:
if (flag0 == active_flag &&
flag1 == obsolete_flag) {
dev_current = 0;
} else if (flag0 == obsolete_flag &&
flag1 == active_flag) {
dev_current = 1;
} else if (flag0 == flag1) {
dev_current = 0;
} else if (flag0 == 0xFF) {
dev_current = 0;
} else if (flag1 == 0xFF) {
dev_current = 1;
} else {
dev_current = 0;
}
break;
case FLAG_INCREMENTAL:
if (flag0 == 255 && flag1 == 0)
dev_current = 1;
else if ((flag1 == 255 && flag0 == 0) ||
else /* flag1 > flag0 */
dev_current = 1;
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
break;
default:
fprintf (stderr, "Unknown flag scheme %u \n",
environment.flag_scheme);
return -1;
}
}
/*
* If we are reading, we don't need the flag and the CRC any
* more, if we are writing, we will re-calculate CRC and update
* flags before writing out
*/
if (dev_current) {
environment.image = addr1;
environment.crc = &redundant->crc;
environment.flags = &redundant->flags;
environment.data = redundant->data;
free (addr0);
} else {
environment.image = addr0;
/* Other pointers are already set */
#ifdef DEBUG
fprintf(stderr, "Selected env in %s\n", DEVNAME(dev_current));
#endif
{
struct stat st;
#if defined(CONFIG_FILE)
if (!common_args.config_file)
common_args.config_file = CONFIG_FILE;
/* Fills in DEVNAME(), ENVSIZE(), DEVESIZE(). Or don't. */
if (get_config(common_args.config_file)) {
fprintf(stderr, "Cannot parse config file '%s': %m\n",
common_args.config_file);
DEVNAME (0) = DEVICE1_NAME;
DEVOFFSET (0) = DEVICE1_OFFSET;
ENVSIZE (0) = ENV1_SIZE;
/* Default values are: erase-size=env-size */
DEVESIZE (0) = ENVSIZE (0);
/* #sectors=env-size/erase-size (rounded up) */
ENVSECTORS (0) = (ENVSIZE(0) + DEVESIZE(0) - 1) / DEVESIZE(0);
#ifdef DEVICE1_ESIZE
#endif
#ifdef DEVICE1_ENVSECTORS
#endif
#ifdef HAVE_REDUND
DEVNAME (1) = DEVICE2_NAME;
DEVOFFSET (1) = DEVICE2_OFFSET;
ENVSIZE (1) = ENV2_SIZE;
/* Default values are: erase-size=env-size */
DEVESIZE (1) = ENVSIZE (1);
/* #sectors=env-size/erase-size (rounded up) */
ENVSECTORS (1) = (ENVSIZE(1) + DEVESIZE(1) - 1) / DEVESIZE(1);
#ifdef DEVICE2_ESIZE
#endif
#ifdef DEVICE2_ENVSECTORS
HaveRedundEnv = 1;
if (stat (DEVNAME (0), &st)) {
fprintf (stderr,
"Cannot access MTD device %s: %s\n",
DEVNAME (0), strerror (errno));
if (HaveRedundEnv && stat (DEVNAME (1), &st)) {
fprintf (stderr,
"Cannot access MTD device %s: %s\n",
DEVNAME (1), strerror (errno));
return 0;
}
#if defined(CONFIG_FILE)
static int get_config (char *fname)
{
FILE *fp;
int i = 0;
int rc;
char dump[128];
fp = fopen (fname, "r");
if (fp == NULL)
return -1;
while (i < 2 && fgets (dump, sizeof (dump), fp)) {
/* Skip incomplete conversions and comment strings */
rc = sscanf (dump, "%ms %lx %lx %lx %lx",
&devname,
&DEVOFFSET (i),
&ENVSIZE (i),
&DEVESIZE (i),
&ENVSECTORS (i));
if (rc < 3)
DEVNAME(i) = devname;
if (rc < 4)
/* Assume the erase size is the same as the env-size */
DEVESIZE(i) = ENVSIZE(i);
/* Assume enough env sectors to cover the environment */
ENVSECTORS (i) = (ENVSIZE(i) + DEVESIZE(i) - 1) / DEVESIZE(i);
i++;
}
HaveRedundEnv = i - 1;
if (!i) { /* No valid entries found */
} else
return 0;
}
#endif