2008-09-23 09:23:58 +00:00
|
|
|
/*
|
|
|
|
* trx.c
|
|
|
|
*
|
2010-03-27 17:38:33 +00:00
|
|
|
* Copyright (C) 2005 Mike Baker
|
2016-06-07 06:58:31 +00:00
|
|
|
* Copyright (C) 2008 Felix Fietkau <nbd@nbd.name>
|
2008-09-23 09:23:58 +00:00
|
|
|
*
|
|
|
|
* 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/stat.h>
|
2015-06-05 09:00:02 +00:00
|
|
|
#include <endian.h>
|
2008-09-23 09:23:58 +00:00
|
|
|
#include <string.h>
|
2010-12-26 04:17:51 +00:00
|
|
|
#include <errno.h>
|
2008-09-23 09:23:58 +00:00
|
|
|
|
|
|
|
#include <sys/ioctl.h>
|
2011-11-09 21:16:36 +00:00
|
|
|
#include <mtd/mtd-user.h>
|
2008-09-23 09:23:58 +00:00
|
|
|
#include "mtd.h"
|
2010-03-27 17:38:30 +00:00
|
|
|
#include "crc32.h"
|
2008-09-23 09:23:58 +00:00
|
|
|
|
|
|
|
#define TRX_MAGIC 0x30524448 /* "HDR0" */
|
2016-05-06 12:37:05 +00:00
|
|
|
#define TRX_CRC32_DATA_OFFSET 12 /* First 12 bytes are not covered by CRC32 */
|
|
|
|
#define TRX_CRC32_DATA_SIZE 16
|
2008-09-23 09:23:58 +00:00
|
|
|
struct trx_header {
|
2010-12-26 04:17:51 +00:00
|
|
|
uint32_t magic; /* "HDR0" */
|
|
|
|
uint32_t len; /* Length of file including header */
|
|
|
|
uint32_t crc32; /* 32-bit CRC from flag_version to end of file */
|
|
|
|
uint32_t flag_version; /* 0:15 flags, 16:31 version */
|
|
|
|
uint32_t offsets[3]; /* Offsets of partitions from start of header */
|
2008-09-23 09:23:58 +00:00
|
|
|
};
|
|
|
|
|
2018-07-15 14:51:41 +00:00
|
|
|
#define min(x,y) ({ \
|
|
|
|
typeof(x) _x = (x); \
|
|
|
|
typeof(y) _y = (y); \
|
|
|
|
(void) (&_x == &_y); \
|
|
|
|
_x < _y ? _x : _y; })
|
|
|
|
|
2010-12-26 04:17:51 +00:00
|
|
|
#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
|
|
|
|
#define STORE32_LE(X) (X)
|
|
|
|
#else
|
|
|
|
#error unknown endianness!
|
|
|
|
#endif
|
|
|
|
|
|
|
|
ssize_t pread(int fd, void *buf, size_t count, off_t offset);
|
|
|
|
ssize_t pwrite(int fd, const void *buf, size_t count, off_t offset);
|
|
|
|
|
2008-09-23 09:23:58 +00:00
|
|
|
int
|
|
|
|
trx_fixup(int fd, const char *name)
|
|
|
|
{
|
|
|
|
struct mtd_info_user mtdInfo;
|
|
|
|
unsigned long len;
|
|
|
|
struct trx_header *trx;
|
|
|
|
void *ptr, *scan;
|
|
|
|
int bfd;
|
|
|
|
|
|
|
|
if (ioctl(fd, MEMGETINFO, &mtdInfo) < 0) {
|
|
|
|
fprintf(stderr, "Failed to get mtd info\n");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
len = mtdInfo.size;
|
|
|
|
if (mtdInfo.size <= 0) {
|
|
|
|
fprintf(stderr, "Invalid MTD device size\n");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
bfd = mtd_open(name, true);
|
|
|
|
ptr = mmap(NULL, len, PROT_READ|PROT_WRITE, MAP_SHARED, bfd, 0);
|
|
|
|
if (!ptr || (ptr == (void *) -1)) {
|
|
|
|
perror("mmap");
|
2016-10-06 21:59:34 +00:00
|
|
|
fprintf(stderr, "Mapping the TRX header failed\n");
|
2008-09-23 09:23:58 +00:00
|
|
|
goto err1;
|
|
|
|
}
|
|
|
|
|
|
|
|
trx = ptr;
|
|
|
|
if (trx->magic != TRX_MAGIC) {
|
|
|
|
fprintf(stderr, "TRX header not found\n");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
scan = ptr + offsetof(struct trx_header, flag_version);
|
|
|
|
trx->crc32 = crc32buf(scan, trx->len - (scan - ptr));
|
|
|
|
msync(ptr, sizeof(struct trx_header), MS_SYNC|MS_INVALIDATE);
|
|
|
|
munmap(ptr, len);
|
|
|
|
close(bfd);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
err1:
|
|
|
|
close(bfd);
|
|
|
|
err:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-09-02 21:06:35 +00:00
|
|
|
#ifndef target_ar71xx
|
2008-09-23 09:23:58 +00:00
|
|
|
int
|
|
|
|
trx_check(int imagefd, const char *mtd, char *buf, int *len)
|
|
|
|
{
|
|
|
|
const struct trx_header *trx = (const struct trx_header *) buf;
|
|
|
|
int fd;
|
|
|
|
|
2014-09-02 17:13:42 +00:00
|
|
|
if (strcmp(mtd, "firmware") != 0)
|
2008-09-23 09:23:58 +00:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
if (*len < 32) {
|
2016-02-06 16:29:12 +00:00
|
|
|
*len += read(imagefd, buf + *len, 32 - *len);
|
|
|
|
if (*len < 32) {
|
|
|
|
fprintf(stdout, "Could not get image header, file too small (%d bytes)\n", *len);
|
|
|
|
return 0;
|
|
|
|
}
|
2008-09-23 09:23:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (trx->magic != TRX_MAGIC || trx->len < sizeof(struct trx_header)) {
|
|
|
|
if (quiet < 2) {
|
|
|
|
fprintf(stderr, "Bad trx header\n");
|
|
|
|
fprintf(stderr, "This is not the correct file format; refusing to flash.\n"
|
|
|
|
"Please specify the correct file or use -f to force.\n");
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check if image fits to mtd device */
|
|
|
|
fd = mtd_check_open(mtd);
|
|
|
|
if(fd < 0) {
|
|
|
|
fprintf(stderr, "Could not open mtd device: %s\n", mtd);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(mtdsize < trx->len) {
|
|
|
|
fprintf(stderr, "Image too big for partition: %s\n", mtd);
|
|
|
|
close(fd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
return 1;
|
|
|
|
}
|
2014-09-02 21:06:35 +00:00
|
|
|
#endif
|
2008-09-23 09:23:58 +00:00
|
|
|
|
2010-12-26 04:17:51 +00:00
|
|
|
int
|
2016-05-06 12:37:05 +00:00
|
|
|
mtd_fixtrx(const char *mtd, size_t offset, size_t data_size)
|
2010-12-26 04:17:51 +00:00
|
|
|
{
|
2016-05-06 12:37:05 +00:00
|
|
|
size_t data_offset;
|
2010-12-26 04:17:51 +00:00
|
|
|
int fd;
|
|
|
|
struct trx_header *trx;
|
2016-05-09 17:00:53 +00:00
|
|
|
char *first_block;
|
2018-07-15 14:51:41 +00:00
|
|
|
char *buf, *to;
|
2010-12-26 04:17:51 +00:00
|
|
|
ssize_t res;
|
|
|
|
size_t block_offset;
|
|
|
|
|
|
|
|
if (quiet < 2)
|
|
|
|
fprintf(stderr, "Trying to fix trx header in %s at 0x%x...\n", mtd, offset);
|
|
|
|
|
|
|
|
fd = mtd_check_open(mtd);
|
|
|
|
if(fd < 0) {
|
|
|
|
fprintf(stderr, "Could not open mtd device: %s\n", mtd);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2016-05-06 12:37:05 +00:00
|
|
|
data_offset = offset + TRX_CRC32_DATA_OFFSET;
|
|
|
|
if (data_size)
|
|
|
|
data_size += TRX_CRC32_DATA_SIZE;
|
|
|
|
else
|
|
|
|
data_size = erasesize - TRX_CRC32_DATA_OFFSET;
|
|
|
|
|
2012-07-25 17:28:32 +00:00
|
|
|
block_offset = offset & ~(erasesize - 1);
|
|
|
|
offset -= block_offset;
|
|
|
|
|
2016-05-06 12:37:05 +00:00
|
|
|
if (data_offset + data_size > mtdsize) {
|
2010-12-26 04:17:51 +00:00
|
|
|
fprintf(stderr, "Offset too large, device size 0x%x\n", mtdsize);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2016-05-09 17:00:53 +00:00
|
|
|
first_block = malloc(erasesize);
|
|
|
|
if (!first_block) {
|
2010-12-26 04:17:51 +00:00
|
|
|
perror("malloc");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2016-05-09 17:00:53 +00:00
|
|
|
res = pread(fd, first_block, erasesize, block_offset);
|
2010-12-26 04:17:51 +00:00
|
|
|
if (res != erasesize) {
|
|
|
|
perror("pread");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2016-05-09 17:00:53 +00:00
|
|
|
trx = (struct trx_header *)(first_block + offset);
|
2010-12-26 04:17:51 +00:00
|
|
|
if (trx->magic != STORE32_LE(0x30524448)) {
|
|
|
|
fprintf(stderr, "No trx magic found\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2016-05-06 12:37:05 +00:00
|
|
|
buf = malloc(data_size);
|
2016-05-09 17:00:53 +00:00
|
|
|
if (!buf) {
|
|
|
|
perror("malloc");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2018-07-15 14:51:41 +00:00
|
|
|
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;
|
2016-05-09 17:00:53 +00:00
|
|
|
}
|
2018-07-15 14:51:41 +00:00
|
|
|
data_size = to - buf;
|
2016-05-09 17:00:53 +00:00
|
|
|
|
2018-07-15 21:23:42 +00:00
|
|
|
if (trx->len == STORE32_LE(data_size + TRX_CRC32_DATA_OFFSET) &&
|
|
|
|
trx->crc32 == STORE32_LE(crc32buf(buf, data_size))) {
|
|
|
|
if (quiet < 2)
|
|
|
|
fprintf(stderr, "Header already fixed, exiting\n");
|
|
|
|
close(fd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-05-06 12:37:05 +00:00
|
|
|
trx->len = STORE32_LE(data_size + offsetof(struct trx_header, flag_version));
|
2010-12-26 04:17:51 +00:00
|
|
|
|
2016-05-06 12:37:05 +00:00
|
|
|
trx->crc32 = STORE32_LE(crc32buf(buf, data_size));
|
2010-12-26 04:17:51 +00:00
|
|
|
if (mtd_erase_block(fd, block_offset)) {
|
|
|
|
fprintf(stderr, "Can't erease block at 0x%x (%s)\n", block_offset, strerror(errno));
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (quiet < 2)
|
|
|
|
fprintf(stderr, "New crc32: 0x%x, rewriting block\n", trx->crc32);
|
|
|
|
|
2016-05-09 17:00:53 +00:00
|
|
|
if (pwrite(fd, first_block, erasesize, block_offset) != erasesize) {
|
2010-12-26 04:17:51 +00:00
|
|
|
fprintf(stderr, "Error writing block (%s)\n", strerror(errno));
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (quiet < 2)
|
|
|
|
fprintf(stderr, "Done.\n");
|
|
|
|
|
|
|
|
close (fd);
|
|
|
|
sync();
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|