From 31b015fc08a13a5b63245808f7d1a49eadd8193a Mon Sep 17 00:00:00 2001 From: Drake Dowsett Date: Mon, 6 Nov 2006 16:54:10 +0100 Subject: [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 --- ubi-utils/src/pfiflash_error.h | 69 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 ubi-utils/src/pfiflash_error.h (limited to 'ubi-utils/src/pfiflash_error.h') diff --git a/ubi-utils/src/pfiflash_error.h b/ubi-utils/src/pfiflash_error.h new file mode 100644 index 0000000..34b705e --- /dev/null +++ b/ubi-utils/src/pfiflash_error.h @@ -0,0 +1,69 @@ +#ifndef __PFIFLASH_ERROR_H__ +#define __PFIFLASH_ERROR_H__ +/* + * Copyright (c) International Business Machines Corp., 2006 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See + * the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +/* + * Author: Drake Dowsett + * Contact: Andreas Arnez + */ + +enum pfiflash_err { + PFIFLASH_ERR_EOF = 1, + PFIFLASH_ERR_FIO, + PFIFLASH_ERR_UBI_OPEN, + PFIFLASH_ERR_UBI_CLOSE, + PFIFLASH_ERR_UBI_MKVOL, + PFIFLASH_ERR_UBI_RMVOL, + PFIFLASH_ERR_UBI_VOL_UPDATE, + PFIFLASH_ERR_UBI_VOL_FOPEN, + PFIFLASH_ERR_UBI_UNKNOWN, + PFIFLASH_ERR_UBI_VID_OOB, + PFIFLASH_ERR_BOOTENV_CREATE, + PFIFLASH_ERR_BOOTENV_READ, + PFIFLASH_ERR_BOOTENV_SIZE, + PFIFLASH_ERR_BOOTENV_WRITE, + PFIFLASH_ERR_PDD_UNKNOWN, + PFIFLASH_ERR_MTD_OPEN, + PFIFLASH_ERR_MTD_CLOSE, + PFIFLASH_ERR_CRC_CHECK +}; + +const char *const PFIFLASH_ERRSTR[] = { + "", + "unexpected EOF", + "file I/O error", + "couldn't open UBI", + "couldn't close UBI", + "couldn't make UBI volume %d", + "couldn't remove UBI volume %d", + "couldn't update UBI volume %d", + "couldn't open UBI volume %d", + "unknown UBI operation", + "PFI data contains out of bounds UBI id %d", + "couldn't create bootenv%s", + "couldn't read bootenv", + "couldn't resize bootenv", + "couldn't write bootenv on ubi%d_%d", + "unknown PDD handling algorithm", + "couldn't open MTD device %s", + "couldn't close MTD device %s", + "CRC check failed: given=0x%08x, calculated=0x%08x" +}; + +#endif /* __PFIFLASH_ERROR_H__ */ -- cgit v1.2.3 From 04d0f33ea243f307e045605603b96419ea458a26 Mon Sep 17 00:00:00 2001 From: Alexander Schmidt Date: Wed, 21 Feb 2007 10:40:13 +0100 Subject: UBI Utils: pfiflash did not erase block before writing to it Pfiflash should erase raw flash regions before overwriting them and check for bad blocks in case of NAND flash. Signed-off-by: Alexander Schmidt Signed-off-by: Frank Haverkamp Signed-off-by: Josh Boyer --- ubi-utils/src/libpfiflash.c | 52 +++++++++++++++++++++++++++++++++++++++++- ubi-utils/src/pfiflash_error.h | 6 +++-- 2 files changed, 55 insertions(+), 3 deletions(-) (limited to 'ubi-utils/src/pfiflash_error.h') diff --git a/ubi-utils/src/libpfiflash.c b/ubi-utils/src/libpfiflash.c index 0be1f2c..4dfafea 100644 --- a/ubi-utils/src/libpfiflash.c +++ b/ubi-utils/src/libpfiflash.c @@ -34,11 +34,13 @@ #define __USE_GNU #include #include +#include #include #include #include /* FIXME Is this ok here? */ +#include #include "pfiflash_error.h" #include "ubimirror.h" @@ -554,6 +556,45 @@ write_normal_volume(int devno, uint32_t id, size_t update_size, FILE* fp_in, return rc; } +static int +erase_mtd_region(FILE* file_p, int start, int length) +{ + int rc, fd; + erase_info_t erase; + mtd_info_t mtdinfo; + loff_t offset = start; + loff_t end = offset + length; + + fd = fileno(file_p); + if (fd < 0) + return -PFIFLASH_ERR_MTD_ERASE; + + rc = ioctl(fd, MEMGETINFO, &mtdinfo); + if (rc) + return -PFIFLASH_ERR_MTD_ERASE; + + /* check for bad blocks in case of NAND flash */ + if (mtdinfo.type == MTD_NANDFLASH) { + while (offset < end) { + rc = ioctl(fd, MEMGETBADBLOCK, &offset); + if (rc > 0) { + return -PFIFLASH_ERR_MTD_ERASE; + } + + offset += mtdinfo.erasesize; + } + } + + erase.start = start; + erase.length = length; + + rc = ioctl(fd, MEMERASE, &erase); + if (rc) { + return -PFIFLASH_ERR_MTD_ERASE; + } + + return rc; +} /** * process_raw_volumes - writes the raw sections of the PFI data @@ -582,7 +623,7 @@ process_raw_volumes(FILE* pfi, list_t pfi_raws, const char* rawdev, void *i; uint32_t crc, crc32_table[256]; size_t j, k; - FILE* mtd; + FILE* mtd = NULL; list_t ptr; if (is_empty(pfi_raws)) @@ -639,6 +680,12 @@ process_raw_volumes(FILE* pfi, list_t pfi_raws, const char* rawdev, } for (j = 0; j < r->starts_size; j++) { + rc = erase_mtd_region(mtd, r->starts[j], r->data_size); + if (rc) { + EBUF(PFIFLASH_ERRSTR[-rc]); + goto err; + } + fseek(mtd, r->starts[j], SEEK_SET); for (k = 0; k < r->data_size; k++) { int c = fputc((int)pfi_data[k], mtd); @@ -656,6 +703,7 @@ process_raw_volumes(FILE* pfi, list_t pfi_raws, const char* rawdev, } } rc = fclose(mtd); + mtd = NULL; if (rc != 0) { rc = -PFIFLASH_ERR_MTD_CLOSE; EBUF(PFIFLASH_ERRSTR[-rc], rawdev); @@ -664,6 +712,8 @@ process_raw_volumes(FILE* pfi, list_t pfi_raws, const char* rawdev, } err: + if (mtd != NULL) + fclose(mtd); if (pfi_data != NULL) free(pfi_data); return rc; diff --git a/ubi-utils/src/pfiflash_error.h b/ubi-utils/src/pfiflash_error.h index 34b705e..c06232a 100644 --- a/ubi-utils/src/pfiflash_error.h +++ b/ubi-utils/src/pfiflash_error.h @@ -41,7 +41,8 @@ enum pfiflash_err { PFIFLASH_ERR_PDD_UNKNOWN, PFIFLASH_ERR_MTD_OPEN, PFIFLASH_ERR_MTD_CLOSE, - PFIFLASH_ERR_CRC_CHECK + PFIFLASH_ERR_CRC_CHECK, + PFIFLASH_ERR_MTD_ERASE }; const char *const PFIFLASH_ERRSTR[] = { @@ -63,7 +64,8 @@ const char *const PFIFLASH_ERRSTR[] = { "unknown PDD handling algorithm", "couldn't open MTD device %s", "couldn't close MTD device %s", - "CRC check failed: given=0x%08x, calculated=0x%08x" + "CRC check failed: given=0x%08x, calculated=0x%08x", + "couldn't erase raw mtd region" }; #endif /* __PFIFLASH_ERROR_H__ */ -- cgit v1.2.3