Reading MTD data with (p)read doesn't return any error when accessing
bad block. As the result, with current code, CRC32 covers "data" stored
in bad blocks.
That behavior doesn't match CFE's one (bootloader simply skips bad
blocks) and may result in:
1) Invalid CRC32
2) CFE refusing to boot firmware with a following error:
Boot program checksum is invalid
Fix that problem by checking every block before reading its content.
Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
(cherry picked from commit
0f54489f754e7bd34e0430c57a11b6a54740d58e)
include $(INCLUDE_DIR)/kernel.mk
PKG_NAME:=mtd
include $(INCLUDE_DIR)/kernel.mk
PKG_NAME:=mtd
PKG_BUILD_DIR := $(KERNEL_BUILD_DIR)/$(PKG_NAME)
STAMP_PREPARED := $(STAMP_PREPARED)_$(call confvar,CONFIG_MTD_REDBOOT_PARTS)
PKG_BUILD_DIR := $(KERNEL_BUILD_DIR)/$(PKG_NAME)
STAMP_PREPARED := $(STAMP_PREPARED)_$(call confvar,CONFIG_MTD_REDBOOT_PARTS)
uint32_t offsets[3]; /* Offsets of partitions from start of header */
};
uint32_t offsets[3]; /* Offsets of partitions from start of header */
};
+#define min(x,y) ({ \
+ typeof(x) _x = (x); \
+ typeof(y) _y = (y); \
+ (void) (&_x == &_y); \
+ _x < _y ? _x : _y; })
+
#if __BYTE_ORDER == __BIG_ENDIAN
#define STORE32_LE(X) ((((X) & 0x000000FF) << 24) | (((X) & 0x0000FF00) << 8) | (((X) & 0x00FF0000) >> 8) | (((X) & 0xFF000000) >> 24))
#elif __BYTE_ORDER == __LITTLE_ENDIAN
#if __BYTE_ORDER == __BIG_ENDIAN
#define STORE32_LE(X) ((((X) & 0x000000FF) << 24) | (((X) & 0x0000FF00) << 8) | (((X) & 0x00FF0000) >> 8) | (((X) & 0xFF000000) >> 24))
#elif __BYTE_ORDER == __LITTLE_ENDIAN
int fd;
struct trx_header *trx;
char *first_block;
int fd;
struct trx_header *trx;
char *first_block;
ssize_t res;
size_t block_offset;
ssize_t res;
size_t block_offset;
- res = pread(fd, buf, data_size, data_offset);
- if (res != data_size) {
- perror("pread");
- exit(1);
+ to = buf;
+ while (data_size) {
+ size_t read_block_offset = data_offset & ~(erasesize - 1);
+ size_t read_chunk;
+
+ read_chunk = erasesize - (data_offset & (erasesize - 1));
+ read_chunk = min(read_chunk, data_size);
+
+ /* Read from good blocks only to match CFE behavior */
+ if (!mtd_block_is_bad(fd, read_block_offset)) {
+ res = pread(fd, to, read_chunk, data_offset);
+ if (res != read_chunk) {
+ perror("pread");
+ exit(1);
+ }
+ to += read_chunk;
+ }
+
+ data_offset += read_chunk;
+ data_size -= read_chunk;
trx->len = STORE32_LE(data_size + offsetof(struct trx_header, flag_version));
trx->len = STORE32_LE(data_size + offsetof(struct trx_header, flag_version));