printf ("%s\n", env);
}
+ fw_env_close(opts);
return 0;
}
printf("%s=%s\n", name, val);
}
+ fw_env_close(opts);
+
return rc;
}
-int fw_env_close(struct env_opts *opts)
+int fw_env_flush(struct env_opts *opts)
{
int ret;
char *name, **valv;
char *value = NULL;
int valc;
+ int ret;
if (!opts)
opts = &default_opts;
valv = argv + 1;
valc = argc - 1;
- if (env_flags_validate_env_set_params(name, valv, valc) < 0)
+ if (env_flags_validate_env_set_params(name, valv, valc) < 0) {
+ fw_env_close(opts);
return -1;
+ }
len = 0;
for (i = 0; i < valc; ++i) {
free(value);
- return fw_env_close(opts);
+ ret = fw_env_flush(opts);
+ fw_env_close(opts);
+
+ return ret;
}
/*
if (strcmp(fname, "-") != 0)
fclose(fp);
- ret |= fw_env_close(opts);
+ ret |= fw_env_flush(opts);
+
+ fw_env_close(opts);
return ret;
}
{
int crc0, crc0_ok;
unsigned char flag0;
- void *addr0;
+ void *addr0 = NULL;
int crc1, crc1_ok;
unsigned char flag1;
- void *addr1;
+ void *addr1 = NULL;
int ret;
opts = &default_opts;
if (parse_config(opts)) /* should fill envdevices */
- return -1;
+ return -EINVAL;
addr0 = calloc(1, CUR_ENVSIZE);
if (addr0 == NULL) {
fprintf(stderr,
"Not enough memory for environment (%ld bytes)\n",
CUR_ENVSIZE);
- return -1;
+ ret = -ENOMEM;
+ goto open_cleanup;
}
/* read environment from FLASH to local buffer */
}
dev_current = 0;
- if (flash_io (O_RDONLY))
- return -1;
+ if (flash_io(O_RDONLY)) {
+ ret = -EIO;
+ goto open_cleanup;
+ }
crc0 = crc32 (0, (uint8_t *) environment.data, ENV_SIZE);
ret = env_aes_cbc_crypt(environment.data, 0,
opts->aes_key);
if (ret)
- return ret;
+ goto open_cleanup;
}
crc0_ok = (crc0 == *environment.crc);
fprintf(stderr,
"Not enough memory for environment (%ld bytes)\n",
CUR_ENVSIZE);
- return -1;
+ ret = -ENOMEM;
+ goto open_cleanup;
}
redundant = addr1;
* other pointers in environment still point inside addr0
*/
environment.image = addr1;
- if (flash_io (O_RDONLY))
- return -1;
+ if (flash_io(O_RDONLY)) {
+ ret = -EIO;
+ goto open_cleanup;
+ }
/* Check flag scheme compatibility */
if (DEVTYPE(dev_current) == MTD_NORFLASH &&
environment.flag_scheme = FLAG_INCREMENTAL;
} else {
fprintf (stderr, "Incompatible flash types!\n");
- return -1;
+ ret = -EINVAL;
+ goto open_cleanup;
}
crc1 = crc32 (0, (uint8_t *) redundant->data, ENV_SIZE);
ret = env_aes_cbc_crypt(redundant->data, 0,
opts->aes_key);
if (ret)
- return ret;
+ goto open_cleanup;
}
crc1_ok = (crc1 == redundant->crc);
#endif
}
return 0;
+
+open_cleanup:
+ if (addr0)
+ free(addr0);
+
+ if (addr1)
+ free(addr0);
+
+ return ret;
+}
+
+/*
+ * Simply free allocated buffer with environment
+ */
+int fw_env_close(struct env_opts *opts)
+{
+ if (environment.image)
+ free(environment.image);
+
+ environment.image = NULL;
+
+ return 0;
}
static int check_device_config(int dev)
* @opts: how to retrieve environment from flash, defaults are used if NULL
*
* Description:
- * Uses fw_env_open, fw_env_write, fw_env_close
+ * Uses fw_env_open, fw_env_write, fw_env_flush
*
* Return:
* 0 on success, -1 on failure (modifies errno)
* @opts: encryption key, configuration file, defaults are used if NULL
*
* Description:
- * Uses fw_env_open, fw_env_write, fw_env_close
+ * Uses fw_env_open, fw_env_write, fw_env_flush
*
* Return:
* 0 success, -1 on failure (modifies errno)
int fw_env_write(char *name, char *value);
/**
- * fw_env_close - write the environment from RAM cache back to flash
+ * fw_env_flush - write the environment from RAM cache back to flash
+ *
+ * @opts: encryption key, configuration file, defaults are used if NULL
+ *
+ * Return:
+ * 0 on success, -1 on failure (modifies errno)
+ */
+int fw_env_flush(struct env_opts *opts);
+
+/**
+ * fw_env_close - free allocated structure and close env
*
* @opts: encryption key, configuration file, defaults are used if NULL
*
*/
int fw_env_close(struct env_opts *opts);
+
/**
* fw_env_version - return the current version of the library
*