summaryrefslogtreecommitdiff
path: root/ubi-utils/src/libpfiflash.c
diff options
context:
space:
mode:
authorDrake Dowsett <dowsett@de.ibm.com>2006-11-06 16:54:10 +0100
committerFrank Haverkamp <haver@vnet.ibm.com>2006-11-06 16:54:10 +0100
commit31b015fc08a13a5b63245808f7d1a49eadd8193a (patch)
tree6ab4ce9a35f18e7b53c95c5cb8063cc55e54615d /ubi-utils/src/libpfiflash.c
parent6918e0320c89bc892a6c5d2c6792e5d058e40a6e (diff)
[MTD] UBI: pfiflash needs to flash raw sections and check CRC
Flashing of raw partitions should be possible now. CRC checking of pfi files before flashing the content was added. Signed-off-by: Frank Haverkamp <haver@vnet.ibm.com>
Diffstat (limited to 'ubi-utils/src/libpfiflash.c')
-rw-r--r--ubi-utils/src/libpfiflash.c841
1 files changed, 620 insertions, 221 deletions
diff --git a/ubi-utils/src/libpfiflash.c b/ubi-utils/src/libpfiflash.c
index ed2af3c..1dc9f10 100644
--- a/ubi-utils/src/libpfiflash.c
+++ b/ubi-utils/src/libpfiflash.c
@@ -16,16 +16,13 @@
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
-/**
- * @file pfiflash.c
- *
- * @author Oliver Lohmann <oliloh@de.ibm.com>
- *
- * @brief This library is provides an interface to the pfiflash utility.
- *
- * <oliloh@de.ibm.com> Wed Mar 15 11:39:19 CET 2006 Initial creation.
- *
- * @TODO Comare data before writing it. This implies that the volume
+/*
+ * Authors: Oliver Lohmann <oliloh@de.ibm.com>
+ * Drake Dowsett <dowsett@de.ibm.com>
+ * Contact: Andreas Arnez <anrez@de.ibm.com>
+ */
+
+/* TODO Compare data before writing it. This implies that the volume
* parameters are compared first: size, alignment, name, type, ...,
* this is the same, compare the data. Volume deletion is deffered
* until the difference has been found out.
@@ -40,89 +37,130 @@
#include <libubi.h>
#include <pfiflash.h>
-//#include <mtd/ubi-user.h> /* FIXME Is this ok here!!?? */
+#include <mtd/ubi-user.h> /* FIXME Is this ok here? */
-#include "config.h"
+#include "pfiflash_error.h"
#include "ubimirror.h"
#include "error.h"
#include "reader.h"
#include "example_ubi.h"
#include "bootenv.h"
-static const char copyright [] __attribute__((unused)) =
+/* ubi-header.h and crc32.h needed for CRC checking */
+#include <mtd/ubi-header.h> /* FIXME Is this ok here? */
+#include "crc32.h"
+
+#define __unused __attribute__((unused))
+
+static const char copyright [] __unused =
"Copyright (c) International Business Machines Corp., 2006";
-#define EBUF(fmt...) do { \
- snprintf(err_buf, err_buf_size, fmt); \
+/* simply clear buffer, then write into front of it */
+#define EBUF(fmt...) \
+ snprintf(err_buf, err_buf_size, fmt);
+
+/* make a history of buffer and then prepend something in front */
+#define EBUF_PREPEND(fmt) \
+ do { \
+ int EBUF_HISTORY_LENGTH = strlen(err_buf); \
+ char EBUF_HISTORY[EBUF_HISTORY_LENGTH + 1]; \
+ strncpy(EBUF_HISTORY, err_buf, EBUF_HISTORY_LENGTH + 1);\
+ EBUF(fmt ": %s", EBUF_HISTORY); \
} while (0)
+/* An array of PDD function pointers indexed by the algorithm. */
static pdd_func_t pdd_funcs[PDD_HANDLING_NUM] =
{
&bootenv_pdd_keep,
&bootenv_pdd_merge,
&bootenv_pdd_overwrite
};
-/**< An array of PDD function pointers indexed by the algorithm. */
-
typedef enum ubi_update_process_t {
UBI_REMOVE = 0,
UBI_WRITE,
} ubi_update_process_t;
+
+/**
+ * skip_raw_volumes - reads data from pfi to advance fp past raw block
+ * @pfi: fp to pfi data
+ * @pfi_raws: header information
+ *
+ * Error handling):
+ * when early EOF in pfi data
+ * - returns -PFIFLASH_ERR_EOF, err_buf matches text to err
+ * when file I/O error
+ * - returns -PFIFLASH_ERR_FIO, err_buf matches text to err
+ **/
static int
-skip_raw_sections(FILE* pfi, list_t pfi_raws,
+skip_raw_volumes(FILE* pfi, list_t pfi_raws,
char* err_buf, size_t err_buf_size)
{
- int rc = 0;
-
+ int rc;
void *i;
list_t ptr;
- size_t j, skip_size;
if (is_empty(pfi_raws))
return 0;
+ rc = 0;
foreach(i, ptr, pfi_raws) {
- skip_size = ((pfi_raw_t)i)->data_size;
- for(j = 0; j < skip_size; j++) {
- fgetc(pfi);
- if (ferror(pfi)) {
- EBUF("Cannot skip raw section in PFI.");
- rc = -EIO;
+ size_t j;
+ pfi_raw_t raw;
+
+ raw = (pfi_raw_t)i;
+ for(j = 0; j < raw->data_size; j++) {
+ int c;
+
+ c = fgetc(pfi);
+ if (c == EOF)
+ rc = -PFIFLASH_ERR_EOF;
+ else if (ferror(pfi))
+ rc = -PFIFLASH_ERR_FIO;
+
+ if (rc != 0)
goto err;
- }
}
}
+
err:
+ EBUF(PFIFLASH_ERRSTR[-rc]);
return rc;
}
+
/**
- * @brief Wraps the ubi_mkvol functions and implements a hook for the bootenv
- * update.
- * @param devno UBI device number.
- * @param s Current seqnum.
- * @param u Information about the UBI volume from the PFI.
- * @param err_buf An error buffer.
- * @param err_buf_size The size of the error buffer.
- * @return 0 On Sucess.
- * @return else Error.
- */
+ * my_ubi_mkvol - wraps the ubi_mkvol functions and impl. bootenv update hook
+ * @devno: UBI device number.
+ * @s: Current seqnum.
+ * @u: Information about the UBI volume from the PFI.
+ *
+ * Error handling:
+ * when UBI system couldn't be opened
+ * - returns -PFIFLASH_ERR_UBI_OPEN, err_buf matches text to err
+ * when UBI system couldn't create a volume
+ * - returns -PFIFLASH_ERR_UBI_MKVOL, err_buf matches text to err
+ **/
static int
-my_ubi_mkvol(int devno, int s, pfi_ubi_t u, char *err_buf, size_t err_buf_size)
+my_ubi_mkvol(int devno, int s, pfi_ubi_t u,
+ char *err_buf, size_t err_buf_size)
{
- int rc = 0;
- int type;
- ubi_lib_t ulib = NULL;
+ int rc, type;
+ ubi_lib_t ulib;
+
+ rc = 0;
+ ulib = NULL;
- log_msg("%s(vol_id=%d, size=%d, data_size=%d, type=%d, "
- "alig=%d, nlen=%d, name=%s)", __func__,
+ log_msg("[ ubimkvol id=%d, size=%d, data_size=%d, type=%d, "
+ "alig=%d, nlen=%d, name=%s",
u->ids[s], u->size, u->data_size, u->type, u->alignment,
strnlen(u->names[s], PFI_UBI_VOL_NAME_LEN), u->names[s]);
rc = ubi_open(&ulib);
if (rc != 0) {
+ rc = -PFIFLASH_ERR_UBI_OPEN;
+ EBUF(PFIFLASH_ERRSTR[-rc]);
goto err;
}
@@ -130,7 +168,6 @@ my_ubi_mkvol(int devno, int s, pfi_ubi_t u, char *err_buf, size_t err_buf_size)
case pfi_ubi_static:
type = UBI_STATIC_VOLUME; break;
case pfi_ubi_dynamic:
- type = UBI_DYNAMIC_VOLUME; break;
default:
type = UBI_DYNAMIC_VOLUME;
}
@@ -138,58 +175,78 @@ my_ubi_mkvol(int devno, int s, pfi_ubi_t u, char *err_buf, size_t err_buf_size)
rc = ubi_mkvol(ulib, devno, u->ids[s], type, u->size, u->alignment,
u->names[s]);
if (rc != 0) {
- EBUF("Cannot create volume: %d", u->ids[s]);
+ rc = -PFIFLASH_ERR_UBI_MKVOL;
+ EBUF(PFIFLASH_ERRSTR[-rc], u->ids[s]);
goto err;
}
err:
if (ulib != NULL)
ubi_close(&ulib);
+
return rc;
}
+
/**
- * @brief A wrapper around the UBI library function ubi_rmvol.
- * @param devno UBI device number.
- * @param s Current seqnum.
- * @param u Information about the UBI volume from the PFI.
- * @param err_buf An error buffer.
- * @param err_buf_size The size of the error buffer.
+ * my_ubi_rmvol - a wrapper around the UBI library function ubi_rmvol
+ * @devno UBI device number
+ * @id UBI volume id to remove
*
* If the volume does not exist, the function will return success.
- */
+ *
+ * Error handling:
+ * when UBI system couldn't be opened
+ * - returns -PFIFLASH_ERR_UBI_OPEN, err_buf matches text to err
+ * when UBI system couldn't update (truncate) a volume
+ * - returns -PFIFLASH_ERR_UBI_VOL_UPDATE, err_buf matches text to err
+ * when UBI system couldn't remove a volume
+ * - returns -PFIFLASH_ERR_UBI_RMVOL, err_buf matches text to err
+ **/
static int
my_ubi_rmvol(int devno, uint32_t id,
- char *err_buf __unused, size_t err_buf_size __unused)
+ char *err_buf, size_t err_buf_size)
{
- int rc = 0;
- ubi_lib_t ulib = NULL;
- int fd;
+ int rc, fd;
+ ubi_lib_t ulib;
- log_msg("%s(id=%d)", __func__, id);
+ rc = 0;
+ ulib = NULL;
+
+ log_msg("[ ubirmvol id=%d", id);
rc = ubi_open(&ulib);
- if (rc != 0)
+ if (rc != 0) {
+ rc = -PFIFLASH_ERR_UBI_OPEN;
+ EBUF(PFIFLASH_ERRSTR[-rc]);
goto err;
+ }
- /**
- * Truncate if it exist or not.
- */
+ /* truncate whether it exist or not */
fd = ubi_vol_open(ulib, devno, id, O_RDWR);
if (fd == -1)
- return 0; /* not existent, return */
+ return 0; /* not existent, return 0 */
rc = ubi_vol_update(fd, 0);
+ ubi_vol_close(fd);
if (rc < 0) {
- fprintf(stderr, "update failed rc=%d errno=%d\n", rc, errno);
- ubi_vol_close(fd);
+ rc = -PFIFLASH_ERR_UBI_VOL_UPDATE;
+ EBUF(PFIFLASH_ERRSTR[-rc], id);
goto err; /* if EBUSY than empty device, continue */
}
- ubi_vol_close(fd);
rc = ubi_rmvol(ulib, devno, id);
if (rc != 0) {
- /* @TODO Define a ubi_rmvol return value which says
+#ifdef DEBUG
+ int rc_old = rc;
+ dbg_msg("Remove UBI volume %d returned with error: %d "
+ "errno=%d", id, rc_old, errno);
+#endif
+
+ rc = -PFIFLASH_ERR_UBI_RMVOL;
+ EBUF(PFIFLASH_ERRSTR[-rc], id);
+
+ /* TODO Define a ubi_rmvol return value which says
* sth like EUBI_NOSUCHDEV. In this case, a failed
* operation is acceptable. Everything else has to be
* classified as real error. But talk to Andreas Arnez
@@ -198,65 +255,128 @@ my_ubi_rmvol(int devno, uint32_t id,
/* if ((errno == EINVAL) || (errno == ENODEV))
return 0; */ /* currently it is EINVAL or ENODEV */
- dbg_msg("Remove UBI volume %d returned with error: %d "
- "errno=%d", id, rc, errno);
goto err;
}
+
err:
if (ulib != NULL)
ubi_close(&ulib);
+
return rc;
}
+
+/**
+ * read_bootenv_volume - reads the current bootenv data from id into be_old
+ * @devno UBI device number
+ * @id UBI volume id to remove
+ * @bootenv_old to hold old boot_env data
+ *
+ * Error handling:
+ * when UBI system couldn't be opened
+ * - returns -PFIFLASH_ERR_UBI_OPEN, err_buf matches text to err
+ * when UBI system couldn't open a volume to read
+ * - returns -PFIFLASH_ERR_UBI_VOL_FOPEN, err_buf matches text to err
+ * when couldn't read bootenv data
+ * - returns -PFIFLASH_ERR_BOOTENV_READ, err_buf matches text to err
+ **/
static int
read_bootenv_volume(int devno, uint32_t id, bootenv_t bootenv_old,
char *err_buf, size_t err_buf_size)
{
- int rc = 0;
- ubi_lib_t ulib = NULL;
- FILE* fp_in = NULL;
+ int rc;
+ FILE* fp_in;
+ ubi_lib_t ulib;
+
+ rc = 0;
+ fp_in = NULL;
+ ulib = NULL;
rc = ubi_open(&ulib);
- if (rc)
- return rc;
+ if (rc != 0) {
+ rc = -PFIFLASH_ERR_UBI_OPEN;
+ EBUF(PFIFLASH_ERRSTR[-rc]);
+ goto err;
+ }
fp_in = ubi_vol_fopen_read(ulib, devno, id);
if (!fp_in) {
- EBUF("Cannot open bootenv volume");
- rc = -EIO;
+ rc = -PFIFLASH_ERR_UBI_VOL_FOPEN;
+ EBUF(PFIFLASH_ERRSTR[-rc], id);
goto err;
}
- log_msg("%s reading old bootenvs", __func__);
+ log_msg("[ reading old bootenvs ...");
/* Save old bootenvs for reference */
rc = bootenv_read(fp_in, bootenv_old, BOOTENV_MAXSIZE);
- if (rc)
- EBUF("Cannot read bootenv_old");
+ if (rc != 0) {
+ rc = -PFIFLASH_ERR_BOOTENV_READ;
+ EBUF(PFIFLASH_ERRSTR[-rc]);
+ goto err;
+ }
+
err:
if (fp_in)
fclose(fp_in);
if (ulib)
ubi_close(&ulib);
+
return rc;
}
+
+/**
+ * write_bootenv_volume - writes data from PFI file int to bootenv UBI volume
+ * @devno UBI device number
+ * @id UBI volume id
+ * @bootend_old old PDD data from machine
+ * @pdd_f function to handle PDD with
+ * @fp_in new pdd data contained in PFI
+ * @fp_in_size data size of new pdd data in PFI
+ * @pfi_crc crc value from PFI header
+ *
+ * Error handling:
+ * when UBI system couldn't be opened
+ * - returns -PFIFLASH_ERR_UBI_OPEN, err_buf matches text to err
+ * when bootenv can't be created
+ * - returns -PFIFLASH_ERR_BOOTENV_CREATE, err_buf matches text to err
+ * when bootenv can't be read
+ * - returns -PFIFLASH_ERR_BOOTENV_READ, err_buf matches text to err
+ * when PDD handling function returns and error
+ * - passes rc and err_buf data
+ * when CRC check fails
+ * - returns -PFIFLASH_ERR_CRC_CHECK, err_buf matches text to err
+ * when bootenv can't be resized
+ * - returns -PFIFLASH_ERR_BOOTENV_SIZE, err_buf matches text to err
+ * when UBI system couldn't open a volume
+ * - returns -PFIFLASH_ERR_UBI_VOL_FOPEN, err_buf matches text to err
+ * when couldn't write bootenv data
+ * - returns -PFIFLASH_ERR_BOOTENV_WRITE, err_buf matches text to err
+ **/
static int
write_bootenv_volume(int devno, uint32_t id, bootenv_t bootenv_old,
- pdd_func_t pdd_f,
- FILE* fp_in, /* new pdd data contained in pfi */
- size_t fp_in_size, /* data size of new pdd data in pfi */
+ pdd_func_t pdd_f, FILE* fp_in, size_t fp_in_size,
+ uint32_t pfi_crc,
char *err_buf, size_t err_buf_size)
{
- int rc = 0;
- int warnings = 0;
- ubi_lib_t ulib = NULL;
- bootenv_t bootenv_new = NULL;
- bootenv_t bootenv_res = NULL;
- size_t update_size = 0;
- FILE *fp_out = NULL;
-
- log_msg("%s(id=%d, fp_in=%p)", __func__, id, fp_in);
+ int rc, warnings;
+ uint32_t crc;
+ size_t update_size;
+ FILE *fp_out;
+ bootenv_t bootenv_new, bootenv_res;
+ ubi_lib_t ulib;
+
+ rc = 0;
+ warnings = 0;
+ crc = 0;
+ update_size = 0;
+ fp_out = NULL;
+ bootenv_new = NULL;
+ bootenv_res = NULL;
+ ulib = NULL;
+
+ log_msg("[ ubiupdatevol bootenv id=%d, fp_in=%p", id, fp_in);
/* Workflow:
* 1. Apply PDD operation and get the size of the returning
@@ -269,43 +389,65 @@ write_bootenv_volume(int devno, uint32_t id, bootenv_t bootenv_old,
rc = ubi_open(&ulib);
if (rc != 0) {
+ rc = -PFIFLASH_ERR_UBI_OPEN;
+ EBUF(PFIFLASH_ERRSTR[-rc]);
goto err;
}
rc = bootenv_create(&bootenv_new);
- if (rc != 0)
+ if (rc != 0) {
+ rc = -PFIFLASH_ERR_BOOTENV_CREATE;
+ EBUF(PFIFLASH_ERRSTR[-rc], " 'new'");
goto err;
+ }
+
rc = bootenv_create(&bootenv_res);
- if (rc != 0)
+ if (rc != 0) {
+ rc = -PFIFLASH_ERR_BOOTENV_CREATE;
+ EBUF(PFIFLASH_ERRSTR[-rc], " 'res'");
goto err;
+ }
- rc = bootenv_read(fp_in, bootenv_new, fp_in_size);
- if (rc != 0)
+ rc = bootenv_read_crc(fp_in, bootenv_new, fp_in_size, &crc);
+ if (rc != 0) {
+ rc = -PFIFLASH_ERR_BOOTENV_READ;
+ EBUF(PFIFLASH_ERRSTR[-rc]);
+ goto err;
+ } else if (crc != pfi_crc) {
+ rc = -PFIFLASH_ERR_CRC_CHECK;
+ EBUF(PFIFLASH_ERRSTR[-rc], pfi_crc, crc);
goto err;
+ }
rc = pdd_f(bootenv_old, bootenv_new, &bootenv_res, &warnings,
err_buf, err_buf_size);
- if (rc != 0)
+ if (rc != 0) {
+ EBUF_PREPEND("handling PDD");
goto err;
- if (warnings) {
- /* @TODO Do sth with the warning */
+ }
+ else if (warnings)
+ /* TODO do something with warnings */
dbg_msg("A warning in the PDD operation occured: %d",
warnings);
- }
- log_msg("... (2)");
rc = bootenv_size(bootenv_res, &update_size);
- if (rc != 0)
+ if (rc != 0) {
+ rc = -PFIFLASH_ERR_BOOTENV_SIZE;
+ EBUF(PFIFLASH_ERRSTR[-rc]);
goto err;
+ }
fp_out = ubi_vol_fopen_update(ulib, devno, id, update_size);
- if (fp_out == NULL)
+ if (!fp_out) {
+ rc = -PFIFLASH_ERR_UBI_VOL_FOPEN;
+ EBUF(PFIFLASH_ERRSTR[-rc], id);
goto err;
+ }
rc = bootenv_write(fp_out, bootenv_res);
if (rc != 0) {
- EBUF("Write operation on ubi%d_%d failed.", devno, id);
- rc = -EIO;
+ rc = -PFIFLASH_ERR_BOOTENV_WRITE;
+ EBUF(PFIFLASH_ERRSTR[-rc], devno, id);
goto err;
}
@@ -318,102 +460,327 @@ write_bootenv_volume(int devno, uint32_t id, bootenv_t bootenv_old,
bootenv_destroy(&bootenv_res);
if (fp_out)
fclose(fp_out);
+
return rc;
}
+
+/**
+ * write_normal_volume - writes data from PFI file int to regular UBI volume
+ * @devno UBI device number
+ * @id UBI volume id
+ * @update_size size of data stream
+ * @fp_in PFI data file pointer
+ * @pfi_crc CRC data from PFI header
+ *
+ * Error handling:
+ * when UBI system couldn't be opened
+ * - returns -PFIFLASH_ERR_UBI_OPEN, err_buf matches text to err
+ * when UBI system couldn't open a volume
+ * - returns -PFIFLASH_ERR_UBI_VOL_FOPEN, err_buf matches text to err
+ * when unexpected EOF is encountered
+ * - returns -PFIFLASH_ERR_EOF, err_buf matches text to err
+ * when file I/O error
+ * - returns -PFIFLASH_ERR_FIO, err_buf matches text to err
+ * when CRC check fails
+ * - retruns -PFIFLASH_ERR_CRC_CHECK, err_buf matches text to err
+ **/
static int
write_normal_volume(int devno, uint32_t id, size_t update_size, FILE* fp_in,
- char *err_buf __unused, size_t err_buf_size __unused)
+ uint32_t pfi_crc,
+ char *err_buf, size_t err_buf_size)
{
- int rc = 0;
- ubi_lib_t ulib = NULL;
- FILE* fp_out = NULL;
- int c;
- size_t i;
+ int rc;
+ uint32_t crc, crc32_table[256];
+ size_t bytes_left;
+ FILE* fp_out;
+ ubi_lib_t ulib;
- log_msg("%s(id=%d, update_size=%d fp_in=%p)",
- __func__, id, update_size, fp_in);
+ rc = 0;
+ crc = UBI_CRC32_INIT;
+ bytes_left = update_size;
+ fp_out = NULL;
+ ulib = NULL;
+
+ log_msg("[ ubiupdatevol id=%d, update_size=%d fp_in=%p",
+ id, update_size, fp_in);
rc = ubi_open(&ulib);
- if (rc)
- return rc;
+ if (rc != 0) {
+ rc = -PFIFLASH_ERR_UBI_OPEN;
+ EBUF(PFIFLASH_ERRSTR[-rc]);
+ goto err;
+ }
fp_out = ubi_vol_fopen_update(ulib, devno, id, update_size);
- if (fp_out == NULL) {
- rc = -1;
+ if (!fp_out) {
+ rc = -PFIFLASH_ERR_UBI_VOL_FOPEN;
+ EBUF(PFIFLASH_ERRSTR[-rc], id);
goto err;
}
- log_msg("starting the update ... "); /* FIXME DBG */
- for (i = 0; i < update_size; i++) {
- c = getc(fp_in);
- if (c == EOF && ferror(fp_in)) {
- rc = -EIO;
+ init_crc32_table(crc32_table);
+ while (bytes_left) {
+ char buf[1024];
+ size_t to_rw = sizeof buf > bytes_left ?
+ bytes_left : sizeof buf;
+ if (fread(buf, 1, to_rw, fp_in) != to_rw) {
+ rc = -PFIFLASH_ERR_EOF;
+ EBUF(PFIFLASH_ERRSTR[-rc]);
goto err;
}
- if (putc(c, fp_out) == EOF) {
- rc = -EIO;
+ crc = clc_crc32(crc32_table, crc, buf, to_rw);
+ if (fwrite(buf, 1, to_rw, fp_out) != to_rw) {
+ rc = -PFIFLASH_ERR_FIO;
+ EBUF(PFIFLASH_ERRSTR[-rc]);
goto err;
}
- /* FIXME DBG */
- /* if ((i & 0xFFF) == 0xFFF) log_msg("."); */
+ bytes_left -= to_rw;
}
- /* log_msg("\n"); */ /* FIXME DBG */
+
+ if (crc != pfi_crc) {
+ rc = -PFIFLASH_ERR_CRC_CHECK;
+ EBUF(PFIFLASH_ERRSTR[-rc], pfi_crc, crc);
+ goto err;
+ }
+
err:
if (fp_out)
fclose(fp_out);
if (ulib)
ubi_close(&ulib);
+
return rc;
}
/**
- * @brief ...
- * @precondition The PFI file contains at least one ubi_id entry.
- * This is assured by the PFI read process.
- * @postcondition The used seqnum number is set in the UBI PFI
- * header list.
- * The UBI volumes specified by seqnum are processed.
- */
+ * process_raw_volumes - writes the raw sections of the PFI data
+ * @pfi PFI data file pointer
+ * @pfi_raws list of PFI raw headers
+ * @rawdev device to use to write raw data
+ *
+ * Error handling:
+ * when early EOF in PFI data
+ * - returns -PFIFLASH_ERR_EOF, err_buf matches text to err
+ * when file I/O error
+ * - returns -PFIFLASH_ERR_FIO, err_buf matches text to err
+ * when CRC check fails
+ * - returns -PFIFLASH_ERR_CRC_CHECK, err_buf matches text to err
+ * when opening MTD device fails
+ * - reutrns -PFIFLASH_ERR_MTD_OPEN, err_buf matches text to err
+ * when closing MTD device fails
+ * - returns -PFIFLASH_ERR_MTD_CLOSE, err_buf matches text to err
+ **/
+static int
+process_raw_volumes(FILE* pfi, list_t pfi_raws, const char* rawdev,
+ char* err_buf, size_t err_buf_size)
+{
+ int rc;
+ char *pfi_data;
+ void *i;
+ uint32_t crc, crc32_table[256];
+ size_t j, k;
+ FILE* mtd;
+ list_t ptr;
+
+ if (is_empty(pfi_raws))
+ return 0;
+
+ if (rawdev == NULL)
+ return 0;
+
+ rc = 0;
+
+ log_msg("[ rawupdate dev=%s", rawdev);
+
+ crc = UBI_CRC32_INIT;
+ init_crc32_table(crc32_table);
+
+ /* most likely only one element in list, but just in case */
+ foreach(i, ptr, pfi_raws) {
+ pfi_raw_t r = (pfi_raw_t)i;
+
+ /* read in pfi data */
+ pfi_data = malloc(r->data_size * sizeof(char));
+ for (j = 0; j < r->data_size; j++) {
+ int c = fgetc(pfi);
+ if (c == EOF) {
+ rc = -PFIFLASH_ERR_EOF;
+ EBUF(PFIFLASH_ERRSTR[-rc]);
+ goto err;
+ } else if (ferror(pfi)) {
+ rc = -PFIFLASH_ERR_FIO;
+ EBUF(PFIFLASH_ERRSTR[-rc]);
+ goto err;
+ }
+ pfi_data[j] = (char)c;
+ }
+ crc = clc_crc32(crc32_table, crc, pfi_data, r->data_size);
+
+ /* check crc */
+ if (crc != r->crc) {
+ rc = -PFIFLASH_ERR_CRC_CHECK;
+ EBUF(PFIFLASH_ERRSTR[-rc], r->crc, crc);
+ goto err;
+ }
+
+ /* open device */
+ mtd = fopen(rawdev, "r+");
+ if (mtd == NULL) {
+ rc = PFIFLASH_ERR_MTD_OPEN;
+ EBUF(PFIFLASH_ERRSTR[-rc], rawdev);
+ goto err;
+ }
+
+ for (j = 0; j < r->starts_size; j++) {
+ fseek(mtd, r->starts[j], SEEK_SET);
+ for (k = 0; k < r->data_size; k++) {
+ int c = fputc((int)pfi_data[k], mtd);
+ if (c == EOF) {
+ fclose(mtd);
+ rc = -PFIFLASH_ERR_EOF;
+ EBUF(PFIFLASH_ERRSTR[-rc]);
+ return rc;
+ }
+ if ((char)c != pfi_data[k]) {
+ fclose(mtd);
+ return -1;
+ }
+ }
+ }
+ rc = fclose(mtd);
+ if (rc != 0) {
+ rc = -PFIFLASH_ERR_MTD_CLOSE;
+ EBUF(PFIFLASH_ERRSTR[-rc], rawdev);
+ goto err;
+ }
+ }
+
+ err:
+ return rc;
+}
+
+
+/**
+ * erase_unmapped_ubi_volumes - skip volumes provided by PFI file, clear rest
+ * @devno UBI device number
+ * @pfi_ubis list of UBI header data
+ *
+ * Error handling:
+ * when UBI id is out of bounds
+ * - returns -PFIFLASH_ERR_UBI_VID_OOB, err_buf matches text to err
+ * when UBI volume can't be removed
+ * - passes rc, prepends err_buf with contextual aid
+ **/
+static int
+erase_unmapped_ubi_volumes(int devno, list_t pfi_ubis,
+ char *err_buf, size_t err_buf_size)
+{
+ int rc;
+ uint8_t ubi_volumes[PFI_UBI_MAX_VOLUMES];
+ size_t i;
+ list_t ptr;
+ pfi_ubi_t u;
+
+ rc = 0;
+
+ for (i = 0; i < PFI_UBI_MAX_VOLUMES; i++)
+ ubi_volumes[i] = 1;
+
+ foreach(u, ptr, pfi_ubis) {
+ /* iterate over each vol_id */
+ for(i = 0; i < u->ids_size; i++) {
+ if (u->ids[i] > PFI_UBI_MAX_VOLUMES) {
+ rc = -PFIFLASH_ERR_UBI_VID_OOB;
+ EBUF(PFIFLASH_ERRSTR[-rc], u->ids[i]);
+ goto err;
+ }
+ /* remove from removal list */
+ ubi_volumes[u->ids[i]] = 0;
+ }
+ }
+
+ for (i = 0; i < PFI_UBI_MAX_VOLUMES; i++) {
+ if (ubi_volumes[i]) {
+ rc = my_ubi_rmvol(devno, i, err_buf, err_buf_size);
+ if (rc != 0) {
+ EBUF_PREPEND("remove volume failed");
+ goto err;
+ }
+ }
+ }
+
+ err:
+ return rc;
+}
+
+
+/**
+ * process_ubi_volumes - delegate tasks regarding UBI volumes
+ * @pfi PFI data file pointer
+ * @seqnum sequence number
+ * @pfi_ubis list of UBI header data
+ * @bootenv_old storage for current system PDD
+ * @pdd_f function to handle PDD
+ * @ubi_update_process whether reading or writing
+ *
+ * Error handling:
+ * when and unknown ubi_update_process is given
+ * - returns -PFIFLASH_ERR_UBI_UNKNOWN, err_buf matches text to err
+ * otherwise
+ * - passes rc and err_buf
+ **/
static int
process_ubi_volumes(FILE* pfi, int seqnum, list_t pfi_ubis,
bootenv_t bootenv_old, pdd_func_t pdd_f,
ubi_update_process_t ubi_update_process,
char *err_buf, size_t err_buf_size)
{
- int rc = 0;
+ int rc;
pfi_ubi_t u;
list_t ptr;
+ rc = 0;
+
foreach(u, ptr, pfi_ubis) {
int s = seqnum;
- if (seqnum > ((int)u->ids_size - 1)) {
+
+ if (s > ((int)u->ids_size - 1))
s = 0; /* per default use the first */
- }
u->curr_seqnum = s;
switch (ubi_update_process) {
case UBI_REMOVE:
+ /* TODO are all these "EXAMPLE" vars okay? */
if ((u->ids[s] == EXAMPLE_BOOTENV_VOL_ID_1) ||
(u->ids[s] == EXAMPLE_BOOTENV_VOL_ID_2)) {
- rc =read_bootenv_volume(EXAMPLE_UBI_DEVICE,
- u->ids[s],
- bootenv_old, err_buf,
- err_buf_size);
+ rc = read_bootenv_volume(EXAMPLE_UBI_DEVICE,
+ u->ids[s], bootenv_old,
+ err_buf, err_buf_size);
+ /* it's okay if there is no bootenv
+ * we're going to write one */
+ if ((rc == -PFIFLASH_ERR_UBI_VOL_FOPEN) ||
+ (rc == -PFIFLASH_ERR_BOOTENV_READ))
+ rc = 0;
if (rc != 0)
goto err;
- }
- rc = my_ubi_rmvol(EXAMPLE_UBI_DEVICE, u->ids[s],
+ }
+
+ rc = my_ubi_rmvol(EXAMPLE_UBI_DEVICE, u->ids[s],
err_buf, err_buf_size);
if (rc != 0)
goto err;
+
break;
case UBI_WRITE:
rc = my_ubi_mkvol(EXAMPLE_UBI_DEVICE, s, u,
err_buf, err_buf_size);
- if (rc != 0)
+ if (rc != 0) {
+ EBUF_PREPEND("creating volume");
goto err;
+ }
+
if ((u->ids[s] == EXAMPLE_BOOTENV_VOL_ID_1) ||
(u->ids[s] == EXAMPLE_BOOTENV_VOL_ID_2)) {
rc = write_bootenv_volume(EXAMPLE_UBI_DEVICE,
@@ -421,87 +788,67 @@ process_ubi_volumes(FILE* pfi, int seqnum, list_t pfi_ubis,
bootenv_old, pdd_f,
pfi,
u->data_size,
+ u->crc,
err_buf,
err_buf_size);
- }
- else {
+ if (rc != 0)
+ EBUF_PREPEND("bootenv volume");
+ } else {
rc = write_normal_volume(EXAMPLE_UBI_DEVICE,
u->ids[s],
u->data_size, pfi,
+ u->crc,
err_buf,
err_buf_size);
+ if (rc != 0)
+ EBUF_PREPEND("normal volume");
}
if (rc != 0)
goto err;
+
break;
default:
- EBUF("Invoked unknown UBI operation.");
- rc = -1;
- goto err;
-
- }
- if (rc != 0) {
+ rc = -PFIFLASH_ERR_UBI_UNKNOWN;
+ EBUF(PFIFLASH_ERRSTR[-rc]);
goto err;
}
}
- err:
- return rc;
-
-}
-
-static int
-erase_unmapped_ubi_volumes(int devno, list_t pfi_ubis,
- char *err_buf, size_t err_buf_size)
-{
- int rc = 0;
- list_t ptr;
- pfi_ubi_t u;
- size_t i;
- uint8_t ubi_volumes[PFI_UBI_MAX_VOLUMES];
-
- for (i = 0; i < PFI_UBI_MAX_VOLUMES; i++) {
- ubi_volumes[i] = 1;
- }
-
- foreach(u, ptr, pfi_ubis) {
- /* iterate over each vol_id */
- for(i = 0; i < u->ids_size; i++) {
- if (u->ids[i] > PFI_UBI_MAX_VOLUMES) {
- EBUF("PFI file contains an invalid "
- "volume id: %d", u->ids[i]);
- goto err;
- }
- /* remove from removal list */
- ubi_volumes[u->ids[i]] = 0;
- }
- }
- for (i = 0; i < PFI_UBI_MAX_VOLUMES; i++) {
- if (ubi_volumes[i]) {
- rc = my_ubi_rmvol(devno, i, err_buf, err_buf_size);
- if (rc != 0)
- goto err;
- }
- }
err:
return rc;
}
+
+/**
+ * mirror_ubi_volumes - mirror redundant pairs of volumes
+ * @devno UBI device number
+ * @pfi_ubis list of PFI header data
+ *
+ * Error handling:
+ * when UBI system couldn't be opened
+ * - returns -PFIFLASH_ERR_UBI_OPEN, err_buf matches text to err
+ **/
static int
mirror_ubi_volumes(uint32_t devno, list_t pfi_ubis,
char *err_buf, size_t err_buf_size)
{
- int rc = 0;
- list_t ptr;
+ int rc;
uint32_t j;
+ list_t ptr;
pfi_ubi_t i;
- ubi_lib_t ulib = NULL;
+ ubi_lib_t ulib;
- log_msg("%s(...)", __func__);
+ rc = 0;
+ ulib = NULL;
+
+ log_msg("[ mirror ...");
rc = ubi_open(&ulib);
- if (rc != 0)
+ if (rc != 0) {
+ rc = -PFIFLASH_ERR_UBI_OPEN;
+ EBUF(PFIFLASH_ERRSTR[-rc]);
goto err;
+ }
/**
* Execute all mirror operations on redundant groups.
@@ -510,25 +857,26 @@ mirror_ubi_volumes(uint32_t devno, list_t pfi_ubis,
* ubimirror).
*/
foreach(i, ptr, pfi_ubis) {
- for(j = 0; j < i->ids_size; j++) {
+ for (j = 0; j < i->ids_size; j++) {
/* skip self-match */
if (i->ids[j] == i->ids[i->curr_seqnum])
continue;
- rc = my_ubi_rmvol(devno, i->ids[j], err_buf,
- err_buf_size);
+ rc = my_ubi_rmvol(devno, i->ids[j],
+ err_buf, err_buf_size);
if (rc != 0)
goto err;
- rc = my_ubi_mkvol(devno, j, i, err_buf, err_buf_size);
+ rc = my_ubi_mkvol(devno, j, i,
+ err_buf, err_buf_size);
if (rc != 0)
goto err;
}
}
foreach(i, ptr, pfi_ubis) {
- rc = ubimirror(devno, i->curr_seqnum, i->ids,
- i->ids_size, err_buf, err_buf_size);
+ rc = ubimirror(devno, i->curr_seqnum, i->ids, i->ids_size,
+ err_buf, err_buf_size);
if (rc != 0)
goto err;
}
@@ -537,44 +885,71 @@ mirror_ubi_volumes(uint32_t devno, list_t pfi_ubis,
err:
if (ulib != NULL)
ubi_close(&ulib);
+
return rc;
}
+
+/**
+ * pfiflash_with_raw - exposed func to flash memory with a PFI file
+ * @pfi PFI data file pointer
+ * @complete flag to erase unmapped volumes
+ * @seqnum sequence number
+ * @pdd_handling method to handle pdd (keep, merge, overwrite...)
+ *
+ * Error handling:
+ * when bootenv can't be created
+ * - returns -PFIFLASH_ERR_BOOTENV_CREATE, err_buf matches text to err
+ * when PFI headers can't be read, or
+ * when fail to skip raw sections, or
+ * when error occurs while processing raw volumes, or
+ * when fail to erase unmapped UBI vols, or
+ * when error occurs while processing UBI volumes, or
+ * when error occurs while mirroring UBI volumes
+ * - passes rc, prepends err_buf with contextual aid
+ **/
int
-pfiflash(FILE* pfi, int complete, int seqnum, pdd_handling_t pdd_handling,
- char *err_buf, size_t err_buf_size)
+pfiflash_with_raw(FILE* pfi, int complete, int seqnum,
+ pdd_handling_t pdd_handling, const char* rawdev,
+ char *err_buf, size_t err_buf_size)
{
- int rc = 0;
- pdd_func_t pdd_f = NULL;
+ int rc;
+ bootenv_t bootenv;
+ pdd_func_t pdd_f;
if (pfi == NULL)
return -EINVAL;
- /**
- * If the user didnt specify a seqnum we start per default
- * with the index 0
- */
+ rc = 0;
+ pdd_f = NULL;
+
+ /* If the user didnt specify a seqnum we start per default
+ * with the index 0 */
int curr_seqnum = seqnum < 0 ? 0 : seqnum;
list_t pfi_raws = mk_empty(); /* list of raw sections from a pfi */
list_t pfi_ubis = mk_empty(); /* list of ubi sections from a pfi */
- bootenv_t bootenv;
rc = bootenv_create(&bootenv);
if (rc != 0) {
- EBUF("Cannot create bootenv variable");
+ rc = -PFIFLASH_ERR_BOOTENV_CREATE;
+ EBUF(PFIFLASH_ERRSTR[-rc], "");
+ goto err;
}
- rc = read_pfi_headers(&pfi_raws, &pfi_ubis, pfi,
- err_buf, err_buf_size);
+ rc = read_pfi_headers(&pfi_raws, &pfi_ubis, pfi, err_buf, err_buf_size);
if (rc != 0) {
- EBUF("Cannot read PFI headers.");
+ EBUF_PREPEND("reading PFI header");
goto err;
}
- /* @TODO: If you want to implement an IPL update - start here. */
- rc = skip_raw_sections(pfi, pfi_raws, err_buf, err_buf_size);
+ if (rawdev == NULL)
+ rc = skip_raw_volumes(pfi, pfi_raws, err_buf, err_buf_size);
+ else
+ rc = process_raw_volumes(pfi, pfi_raws, rawdev, err_buf,
+ err_buf_size);
if (rc != 0) {
+ EBUF_PREPEND("handling raw section");
goto err;
}
@@ -582,33 +957,41 @@ pfiflash(FILE* pfi, int complete, int seqnum, pdd_handling_t pdd_handling,
rc = erase_unmapped_ubi_volumes(EXAMPLE_UBI_DEVICE, pfi_ubis,
err_buf, err_buf_size);
if (rc != 0) {
- EBUF("Cannot delete unmapped UBI volumes.");
+ EBUF_PREPEND("deleting unmapped UBI volumes");
goto err;
}
}
- if (((int)pdd_handling >= 0) && (pdd_handling < PDD_HANDLING_NUM)) {
+ if (((int)pdd_handling >= 0) &&
+ (pdd_handling < PDD_HANDLING_NUM))
pdd_f = pdd_funcs[pdd_handling];
- }
else {
- EBUF("Used unknown PDD handling algorithm (pdd_handling)");
+ rc = -PFIFLASH_ERR_PDD_UNKNOWN;
+ EBUF(PFIFLASH_ERRSTR[-rc]);
+ goto err;
}
rc = process_ubi_volumes(pfi, curr_seqnum, pfi_ubis, bootenv, pdd_f,
UBI_REMOVE, err_buf, err_buf_size);
- if (rc != 0) {
+ if (rc != 0) {
+ EBUF_PREPEND("removing UBI volumes");
goto err;
}
+
rc = process_ubi_volumes(pfi, curr_seqnum, pfi_ubis, bootenv, pdd_f,
UBI_WRITE, err_buf, err_buf_size);
if (rc != 0) {
+ EBUF_PREPEND("writing UBI volumes");
goto err;
}
+
if (seqnum < 0) { /* mirror redundant pairs */
rc = mirror_ubi_volumes(EXAMPLE_UBI_DEVICE, pfi_ubis,
err_buf, err_buf_size);
- if (rc != 0)
+ if (rc != 0) {
+ EBUF_PREPEND("mirroring UBI volumes");
goto err;
+ }
}
err:
@@ -617,3 +1000,19 @@ pfiflash(FILE* pfi, int complete, int seqnum, pdd_handling_t pdd_handling,
bootenv_destroy(&bootenv);
return rc;
}
+
+
+/**
+ * pfiflash - passes to pfiflash_with_raw
+ * @pfi PFI data file pointer
+ * @complete flag to erase unmapped volumes
+ * @seqnum sequence number
+ * @pdd_handling method to handle pdd (keep, merge, overwrite...)
+ **/
+int
+pfiflash(FILE* pfi, int complete, int seqnum, pdd_handling_t pdd_handling,
+ char *err_buf, size_t err_buf_size)
+{
+ return pfiflash_with_raw(pfi, complete, seqnum, pdd_handling,
+ NULL, err_buf, err_buf_size);
+}