2007-04-02 17:10:48 +00:00
|
|
|
/*
|
|
|
|
*
|
2009-06-24 16:44:30 +00:00
|
|
|
* Copyright (C) 2007-2009 Gabor Juhos <juhosg@openwrt.org>
|
2007-04-02 17:10:48 +00:00
|
|
|
*
|
2007-05-30 10:05:27 +00:00
|
|
|
* This program was based on the code found in various Linux
|
2007-04-02 17:10:48 +00:00
|
|
|
* source tarballs released by Edimax for it's devices.
|
|
|
|
* Original author: David Hsu <davidhsu@realtek.com.tw>
|
|
|
|
*
|
|
|
|
* 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., 51 Franklin Street, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h> /* for unlink() */
|
|
|
|
#include <libgen.h>
|
|
|
|
#include <getopt.h> /* for getopt() */
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <endian.h> /* for __BYTE_ORDER */
|
|
|
|
#if defined(__CYGWIN__)
|
|
|
|
# include <byteswap.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "csysimg.h"
|
|
|
|
|
|
|
|
#if (__BYTE_ORDER == __LITTLE_ENDIAN)
|
|
|
|
# define HOST_TO_LE16(x) (x)
|
|
|
|
# define HOST_TO_LE32(x) (x)
|
|
|
|
# define LE16_TO_HOST(x) (x)
|
|
|
|
# define LE32_TO_HOST(x) (x)
|
|
|
|
#else
|
|
|
|
# define HOST_TO_LE16(x) bswap_16(x)
|
|
|
|
# define HOST_TO_LE32(x) bswap_32(x)
|
|
|
|
# define LE16_TO_HOST(x) bswap_16(x)
|
|
|
|
# define LE32_TO_HOST(x) bswap_32(x)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define MAX_NUM_BLOCKS 8
|
|
|
|
#define MAX_ARG_COUNT 32
|
|
|
|
#define MAX_ARG_LEN 1024
|
|
|
|
#define FILE_BUF_LEN (16*1024)
|
|
|
|
#define CSYS_PADC 0xFF
|
|
|
|
|
|
|
|
#define BLOCK_TYPE_BOOT 0
|
|
|
|
#define BLOCK_TYPE_CONF 1
|
|
|
|
#define BLOCK_TYPE_WEBP 2
|
|
|
|
#define BLOCK_TYPE_CODE 3
|
|
|
|
#define BLOCK_TYPE_XTRA 4
|
|
|
|
|
2007-11-03 14:53:24 +00:00
|
|
|
#define DEFAULT_BLOCK_ALIGN 0x10000U
|
|
|
|
|
|
|
|
#define CSUM_SIZE_NONE 0
|
|
|
|
#define CSUM_SIZE_8 1
|
|
|
|
#define CSUM_SIZE_16 2
|
|
|
|
|
2007-04-02 17:10:48 +00:00
|
|
|
|
|
|
|
struct csum_state{
|
|
|
|
int size;
|
|
|
|
uint16_t val;
|
|
|
|
uint16_t tmp;
|
|
|
|
int odd;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct csys_block {
|
|
|
|
int type; /* type of the block */
|
|
|
|
|
|
|
|
int need_file;
|
|
|
|
char *file_name; /* name of the file */
|
|
|
|
uint32_t file_size; /* length of the file */
|
|
|
|
|
2007-11-03 14:53:24 +00:00
|
|
|
unsigned char sig[SIG_LEN];
|
|
|
|
uint32_t addr;
|
|
|
|
int addr_set;
|
|
|
|
uint32_t align;
|
|
|
|
int align_set;
|
2007-04-02 17:10:48 +00:00
|
|
|
uint8_t padc;
|
|
|
|
|
2007-11-03 14:53:24 +00:00
|
|
|
uint32_t size;
|
2007-04-02 17:10:48 +00:00
|
|
|
uint32_t size_hdr;
|
|
|
|
uint32_t size_csum;
|
|
|
|
uint32_t size_avail;
|
|
|
|
|
|
|
|
struct csum_state *css;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct board_info {
|
|
|
|
char *model;
|
|
|
|
char *name;
|
|
|
|
uint32_t flash_size;
|
|
|
|
|
|
|
|
char sig_boot[SIG_LEN];
|
|
|
|
char sig_conf[SIG_LEN];
|
|
|
|
char sig_webp[SIG_LEN];
|
|
|
|
|
|
|
|
uint32_t boot_size;
|
|
|
|
uint32_t conf_size;
|
|
|
|
uint32_t webp_size;
|
|
|
|
uint32_t webp_size_max;
|
|
|
|
uint32_t code_size;
|
2007-05-30 10:05:27 +00:00
|
|
|
|
2007-04-02 17:10:48 +00:00
|
|
|
uint32_t addr_code;
|
|
|
|
uint32_t addr_webp;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define BOARD(m, n, f, sigb, sigw, bs, cs, ws, ac, aw) {\
|
|
|
|
.model = m, .name = n, .flash_size = f<<20, \
|
|
|
|
.sig_boot = sigb, .sig_conf = SIG_CONF, .sig_webp = sigw, \
|
|
|
|
.boot_size = bs, .conf_size = cs, \
|
|
|
|
.webp_size = ws, .webp_size_max = 3*0x10000, \
|
|
|
|
.addr_code = ac, .addr_webp = aw \
|
|
|
|
}
|
2007-05-30 10:05:27 +00:00
|
|
|
|
2007-04-02 17:10:48 +00:00
|
|
|
#define BOARD_ADM(m,n,f, sigw) BOARD(m,n,f, ADM_BOOT_SIG, sigw, \
|
|
|
|
ADM_BOOT_SIZE, ADM_CONF_SIZE, ADM_WEBP_SIZE, \
|
|
|
|
ADM_CODE_ADDR, ADM_WEBP_ADDR)
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Globals
|
|
|
|
*/
|
|
|
|
char *progname;
|
|
|
|
char *ofname = NULL;
|
|
|
|
int verblevel = 0;
|
2007-05-30 10:05:27 +00:00
|
|
|
int invalid_causes_error = 1;
|
|
|
|
int keep_invalid_images = 0;
|
2007-04-02 17:10:48 +00:00
|
|
|
|
|
|
|
struct board_info *board = NULL;
|
|
|
|
|
|
|
|
struct csys_block *boot_block = NULL;
|
|
|
|
struct csys_block *conf_block = NULL;
|
|
|
|
struct csys_block *webp_block = NULL;
|
|
|
|
struct csys_block *code_block = NULL;
|
|
|
|
|
|
|
|
struct csys_block blocks[MAX_NUM_BLOCKS];
|
|
|
|
int num_blocks = 0;
|
|
|
|
|
|
|
|
static struct board_info boards[] = {
|
|
|
|
/* The original Edimax products */
|
|
|
|
BOARD_ADM("BR-6104K", "Edimax BR-6104K", 2, SIG_BR6104K),
|
|
|
|
BOARD_ADM("BR-6104KP", "Edimax BR-6104KP", 2, SIG_BR6104KP),
|
2007-10-18 07:40:01 +00:00
|
|
|
BOARD_ADM("BR-6104Wg", "Edimax BR-6104Wg", 2, SIG_BR6104Wg),
|
2007-04-02 17:10:48 +00:00
|
|
|
BOARD_ADM("BR-6114WG", "Edimax BR-6114WG", 2, SIG_BR6114WG),
|
|
|
|
BOARD_ADM("BR-6524K", "Edimax BR-6524K", 2, SIG_BR6524K),
|
|
|
|
BOARD_ADM("BR-6524KP", "Edimax BR-6524KP", 2, SIG_BR6524KP),
|
|
|
|
BOARD_ADM("BR-6524WG", "Edimax BR-6524WG", 4, SIG_BR6524WG),
|
|
|
|
BOARD_ADM("BR-6524WP", "Edimax BR-6524WP", 4, SIG_BR6524WP),
|
|
|
|
BOARD_ADM("BR-6541K", "Edimax BR-6541K", 2, SIG_BR6541K),
|
|
|
|
BOARD_ADM("BR-6541KP", "Edimax BR-6541K", 2, SIG_BR6541KP),
|
|
|
|
BOARD_ADM("BR-6541WP", "Edimax BR-6541WP", 4, SIG_BR6541WP),
|
|
|
|
BOARD_ADM("EW-7207APg", "Edimax EW-7207APg", 2, SIG_EW7207APg),
|
|
|
|
BOARD_ADM("PS-1205UWg", "Edimax PS-1205UWg", 2, SIG_PS1205UWg),
|
|
|
|
BOARD_ADM("PS-3205U", "Edimax PS-3205U", 2, SIG_PS3205U),
|
|
|
|
BOARD_ADM("PS-3205UWg", "Edimax PS-3205UWg", 2, SIG_PS3205UWg),
|
|
|
|
|
|
|
|
/* Hawking products */
|
|
|
|
BOARD_ADM("H2BR4", "Hawking H2BR4", 2, SIG_H2BR4),
|
|
|
|
BOARD_ADM("H2WR54G", "Hawking H2WR54G", 4, SIG_H2WR54G),
|
|
|
|
|
|
|
|
/* Planet products */
|
|
|
|
BOARD_ADM("XRT-401D", "Planet XRT-401D", 2, SIG_XRT401D),
|
|
|
|
BOARD_ADM("XRT-402D", "Planet XRT-402D", 2, SIG_XRT402D),
|
2007-05-30 10:05:27 +00:00
|
|
|
|
2007-12-28 17:48:39 +00:00
|
|
|
/* Conceptronic products */
|
|
|
|
BOARD_ADM("C54BSR4", "Conceptronic C54BSR4", 2, SIG_C54BSR4),
|
|
|
|
|
2009-06-24 16:44:30 +00:00
|
|
|
/* OSBRiDGE products */
|
|
|
|
BOARD_ADM("5GXi", "OSBDRiDGE 5GXi", 2, SIG_5GXI),
|
|
|
|
|
2007-04-02 17:10:48 +00:00
|
|
|
{.model = NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
2007-05-30 10:05:27 +00:00
|
|
|
* Message macros
|
2007-04-02 17:10:48 +00:00
|
|
|
*/
|
2007-05-30 10:05:27 +00:00
|
|
|
#define ERR(fmt, ...) do { \
|
|
|
|
fflush(0); \
|
|
|
|
fprintf(stderr, "[%s] *** error: " fmt "\n", progname, ## __VA_ARGS__ ); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define ERRS(fmt, ...) do { \
|
|
|
|
int save = errno; \
|
|
|
|
fflush(0); \
|
|
|
|
fprintf(stderr, "[%s] *** error: " fmt "\n", progname, ## __VA_ARGS__ \
|
|
|
|
, strerror(save)); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define WARN(fmt, ...) do { \
|
|
|
|
fprintf(stderr, "[%s] *** warning: " fmt "\n", progname, ## __VA_ARGS__ ); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define DBG(lev, fmt, ...) do { \
|
|
|
|
if (verblevel < lev) \
|
|
|
|
break;\
|
|
|
|
fprintf(stderr, "[%s] " fmt "\n", progname, ## __VA_ARGS__ ); \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define ERR_FATAL -1
|
|
|
|
#define ERR_INVALID_IMAGE -2
|
2007-04-02 17:10:48 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
usage(int status)
|
|
|
|
{
|
|
|
|
FILE *stream = (status != EXIT_SUCCESS) ? stderr : stdout;
|
|
|
|
struct board_info *board;
|
|
|
|
|
|
|
|
fprintf(stream, "Usage: %s [OPTIONS...] <file>\n", progname);
|
|
|
|
fprintf(stream,
|
|
|
|
"\n"
|
|
|
|
"Options:\n"
|
|
|
|
" -B <board> create image for the board specified with <board>.\n"
|
|
|
|
" valid <board> values:\n"
|
|
|
|
);
|
|
|
|
for (board = boards; board->model != NULL; board++){
|
|
|
|
fprintf(stream,
|
2007-04-03 19:55:10 +00:00
|
|
|
" %-12s: %s\n",
|
2007-04-02 17:10:48 +00:00
|
|
|
board->model, board->name);
|
|
|
|
};
|
|
|
|
fprintf(stream,
|
2007-05-30 10:05:27 +00:00
|
|
|
" -d don't throw error on invalid images\n"
|
|
|
|
" -k keep invalid images\n"
|
2007-11-03 14:53:24 +00:00
|
|
|
" -b <file>[:<align>[:<padc>]]\n"
|
2007-04-02 17:10:48 +00:00
|
|
|
" add boot code to the image\n"
|
2007-11-03 14:53:24 +00:00
|
|
|
" -c <file>[:<align>[:<padc>]]\n"
|
2007-04-02 17:10:48 +00:00
|
|
|
" add configuration settings to the image\n"
|
2007-11-03 14:53:24 +00:00
|
|
|
" -r <file>:[<addr>][:<align>[:<padc>]]\n"
|
2007-04-03 19:55:10 +00:00
|
|
|
" add runtime code to the image\n"
|
2007-11-03 14:53:24 +00:00
|
|
|
" -w [<file>:[<addr>][:<align>[:<padc>]]]\n"
|
2007-04-03 19:55:10 +00:00
|
|
|
" add webpages to the image\n"
|
2007-11-03 14:53:24 +00:00
|
|
|
" -x <file>[:<align>[:<padc>]]\n"
|
2007-04-02 17:10:48 +00:00
|
|
|
" add extra data at the end of the image\n"
|
|
|
|
" -h show this screen\n"
|
|
|
|
"Parameters:\n"
|
|
|
|
" <file> write output to the file <file>\n"
|
|
|
|
);
|
|
|
|
|
|
|
|
exit(status);
|
|
|
|
}
|
|
|
|
|
2007-11-03 14:53:24 +00:00
|
|
|
static inline uint32_t align(uint32_t base, uint32_t alignment)
|
|
|
|
{
|
|
|
|
uint32_t ret;
|
|
|
|
|
|
|
|
if (alignment) {
|
|
|
|
ret = (base + alignment - 1);
|
|
|
|
ret &= ~(alignment-1);
|
|
|
|
} else {
|
|
|
|
ret = base;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2007-04-02 17:10:48 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* argument parsing
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
str2u32(char *arg, uint32_t *val)
|
|
|
|
{
|
|
|
|
char *err = NULL;
|
|
|
|
uint32_t t;
|
2007-05-30 10:05:27 +00:00
|
|
|
|
2007-04-02 17:10:48 +00:00
|
|
|
errno=0;
|
|
|
|
t = strtoul(arg, &err, 0);
|
|
|
|
if (errno || (err==arg) || ((err != NULL) && *err)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
*val = t;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
str2u16(char *arg, uint16_t *val)
|
|
|
|
{
|
|
|
|
char *err = NULL;
|
|
|
|
uint32_t t;
|
|
|
|
|
|
|
|
errno=0;
|
|
|
|
t = strtoul(arg, &err, 0);
|
|
|
|
if (errno || (err==arg) || ((err != NULL) && *err) || (t >= 0x10000)) {
|
|
|
|
return -1;
|
|
|
|
}
|
2007-05-30 10:05:27 +00:00
|
|
|
|
2007-04-02 17:10:48 +00:00
|
|
|
*val = t & 0xFFFF;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
str2u8(char *arg, uint8_t *val)
|
|
|
|
{
|
|
|
|
char *err = NULL;
|
|
|
|
uint32_t t;
|
|
|
|
|
|
|
|
errno=0;
|
|
|
|
t = strtoul(arg, &err, 0);
|
|
|
|
if (errno || (err==arg) || ((err != NULL) && *err) || (t >= 0x100)) {
|
|
|
|
return -1;
|
|
|
|
}
|
2007-05-30 10:05:27 +00:00
|
|
|
|
2007-04-02 17:10:48 +00:00
|
|
|
*val = t & 0xFF;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
str2sig(char *arg, uint32_t *sig)
|
|
|
|
{
|
|
|
|
if (strlen(arg) != 4)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
*sig = arg[0] | (arg[1] << 8) | (arg[2] << 16) | (arg[3] << 24);
|
2007-05-30 10:05:27 +00:00
|
|
|
|
2007-04-02 17:10:48 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
parse_arg(char *arg, char *buf, char *argv[])
|
|
|
|
{
|
|
|
|
int res = 0;
|
|
|
|
size_t argl;
|
|
|
|
char *tok;
|
|
|
|
char **ap = &buf;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
memset(argv, 0, MAX_ARG_COUNT * sizeof(void *));
|
|
|
|
|
|
|
|
if ((arg == NULL)) {
|
|
|
|
/* no arguments */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
argl = strlen(arg);
|
|
|
|
if (argl == 0) {
|
|
|
|
/* no arguments */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argl >= MAX_ARG_LEN) {
|
|
|
|
/* argument is too long */
|
|
|
|
argl = MAX_ARG_LEN-1;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(buf, arg, argl);
|
|
|
|
buf[argl] = '\0';
|
|
|
|
|
|
|
|
for (i = 0; i < MAX_ARG_COUNT; i++) {
|
|
|
|
tok = strsep(ap, ":");
|
|
|
|
if (tok == NULL) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#if 0
|
|
|
|
else if (tok[0] == '\0') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
argv[i] = tok;
|
|
|
|
res++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
required_arg(char c, char *arg)
|
|
|
|
{
|
|
|
|
if (arg == NULL || *arg != '-')
|
|
|
|
return 0;
|
|
|
|
|
2007-05-30 10:05:27 +00:00
|
|
|
ERR("option -%c requires an argument\n", c);
|
|
|
|
return ERR_FATAL;
|
2007-04-02 17:10:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
is_empty_arg(char *arg)
|
|
|
|
{
|
|
|
|
int ret = 1;
|
|
|
|
if (arg != NULL) {
|
|
|
|
if (*arg) ret = 0;
|
|
|
|
};
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
csum8_update(uint8_t *p, uint32_t len, struct csum_state *css)
|
|
|
|
{
|
|
|
|
for ( ; len > 0; len --) {
|
|
|
|
css->val += *p++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
uint16_t
|
|
|
|
csum8_get(struct csum_state *css)
|
|
|
|
{
|
|
|
|
uint8_t t;
|
2007-05-30 10:05:27 +00:00
|
|
|
|
2007-04-02 17:10:48 +00:00
|
|
|
t = css->val;
|
|
|
|
return ~t + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
csum16_update(uint8_t *p, uint32_t len, struct csum_state *css)
|
|
|
|
{
|
|
|
|
uint16_t t;
|
2007-05-30 10:05:27 +00:00
|
|
|
|
2007-04-02 17:10:48 +00:00
|
|
|
if (css->odd) {
|
|
|
|
t = css->tmp + (p[0]<<8);
|
|
|
|
css->val += LE16_TO_HOST(t);
|
|
|
|
css->odd = 0;
|
|
|
|
len--;
|
|
|
|
p++;
|
|
|
|
}
|
2007-05-30 10:05:27 +00:00
|
|
|
|
2007-04-02 17:10:48 +00:00
|
|
|
for ( ; len > 1; len -= 2, p +=2 ) {
|
|
|
|
t = p[0] + (p[1] << 8);
|
|
|
|
css->val += LE16_TO_HOST(t);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (len == 1) {
|
|
|
|
css->tmp = p[0];
|
|
|
|
css->odd = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
uint16_t
|
|
|
|
csum16_get(struct csum_state *css)
|
|
|
|
{
|
|
|
|
char pad = 0;
|
|
|
|
|
|
|
|
csum16_update(&pad, 1, css);
|
|
|
|
return ~css->val + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
csum_init(struct csum_state *css, int size)
|
|
|
|
{
|
|
|
|
css->val = 0;
|
|
|
|
css->tmp = 0;
|
|
|
|
css->odd = 0;
|
|
|
|
css->size = size;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
csum_update(uint8_t *p, uint32_t len, struct csum_state *css)
|
|
|
|
{
|
|
|
|
switch (css->size) {
|
2007-11-03 14:53:24 +00:00
|
|
|
case CSUM_SIZE_8:
|
2007-04-02 17:10:48 +00:00
|
|
|
csum8_update(p,len,css);
|
|
|
|
break;
|
2007-11-03 14:53:24 +00:00
|
|
|
case CSUM_SIZE_16:
|
2007-04-02 17:10:48 +00:00
|
|
|
csum16_update(p,len,css);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
uint16_t
|
|
|
|
csum_get(struct csum_state *css)
|
|
|
|
{
|
|
|
|
uint16_t ret;
|
|
|
|
|
|
|
|
switch (css->size) {
|
2007-11-03 14:53:24 +00:00
|
|
|
case CSUM_SIZE_8:
|
2007-04-02 17:10:48 +00:00
|
|
|
ret = csum8_get(css);
|
|
|
|
break;
|
2007-11-03 14:53:24 +00:00
|
|
|
case CSUM_SIZE_16:
|
2007-04-02 17:10:48 +00:00
|
|
|
ret = csum16_get(css);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* routines to write data to the output file
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
write_out_data(FILE *outfile, uint8_t *data, size_t len,
|
|
|
|
struct csum_state *css)
|
|
|
|
{
|
|
|
|
errno = 0;
|
|
|
|
|
|
|
|
fwrite(data, len, 1, outfile);
|
|
|
|
if (errno) {
|
2007-05-30 10:05:27 +00:00
|
|
|
ERRS("unable to write output file");
|
|
|
|
return ERR_FATAL;
|
2007-04-02 17:10:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (css) {
|
|
|
|
csum_update(data, len, css);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
write_out_padding(FILE *outfile, size_t len, uint8_t padc,
|
|
|
|
struct csum_state *css)
|
|
|
|
{
|
|
|
|
uint8_t buf[512];
|
|
|
|
size_t buflen = sizeof(buf);
|
2007-05-30 10:05:27 +00:00
|
|
|
int err;
|
2007-04-02 17:10:48 +00:00
|
|
|
|
|
|
|
memset(buf, padc, buflen);
|
|
|
|
while (len > 0) {
|
|
|
|
if (len < buflen)
|
|
|
|
buflen = len;
|
|
|
|
|
2007-05-30 10:05:27 +00:00
|
|
|
err = write_out_data(outfile, buf, buflen, css);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
2007-04-02 17:10:48 +00:00
|
|
|
len -= buflen;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
block_stat_file(struct csys_block *block)
|
|
|
|
{
|
|
|
|
struct stat st;
|
2007-05-30 10:05:27 +00:00
|
|
|
int err;
|
2007-04-02 17:10:48 +00:00
|
|
|
|
|
|
|
if (block->file_name == NULL)
|
|
|
|
return 0;
|
|
|
|
|
2007-05-30 10:05:27 +00:00
|
|
|
err = stat(block->file_name, &st);
|
|
|
|
if (err){
|
|
|
|
ERRS("stat failed on %s", block->file_name);
|
|
|
|
return ERR_FATAL;
|
2007-04-02 17:10:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
block->file_size = st.st_size;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
block_writeout_hdr(FILE *outfile, struct csys_block *block)
|
|
|
|
{
|
|
|
|
struct csys_header hdr;
|
|
|
|
int res;
|
|
|
|
|
|
|
|
if (block->size_hdr == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* setup header fields */
|
|
|
|
memcpy(hdr.sig, block->sig, 4);
|
|
|
|
hdr.addr = HOST_TO_LE32(block->addr);
|
2009-06-25 09:39:30 +00:00
|
|
|
hdr.size = HOST_TO_LE32(block->size - block->size_hdr - block->size_csum);
|
2007-04-02 17:10:48 +00:00
|
|
|
|
2007-05-30 10:05:27 +00:00
|
|
|
DBG(1,"writing header for block");
|
2007-04-02 17:10:48 +00:00
|
|
|
res = write_out_data(outfile, (uint8_t *)&hdr, sizeof(hdr),NULL);
|
|
|
|
return res;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
block_writeout_file(FILE *outfile, struct csys_block *block)
|
|
|
|
{
|
|
|
|
char buf[FILE_BUF_LEN];
|
|
|
|
size_t buflen = sizeof(buf);
|
|
|
|
FILE *f;
|
|
|
|
size_t len;
|
|
|
|
int res;
|
|
|
|
|
|
|
|
if (block->file_name == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (block->file_size == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
f = fopen(block->file_name,"r");
|
|
|
|
if (errno) {
|
2007-05-30 10:05:27 +00:00
|
|
|
ERRS("unable to open file: %s", block->file_name);
|
|
|
|
return ERR_FATAL;
|
2007-04-02 17:10:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
len = block->file_size;
|
|
|
|
while (len > 0) {
|
|
|
|
if (len < buflen)
|
|
|
|
buflen = len;
|
|
|
|
|
|
|
|
/* read data from source file */
|
|
|
|
errno = 0;
|
|
|
|
fread(buf, buflen, 1, f);
|
|
|
|
if (errno != 0) {
|
2007-05-30 10:05:27 +00:00
|
|
|
ERRS("unable to read from file: %s", block->file_name);
|
|
|
|
res = ERR_FATAL;
|
2007-04-02 17:10:48 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
res = write_out_data(outfile, buf, buflen, block->css);
|
|
|
|
if (res)
|
|
|
|
break;
|
|
|
|
|
|
|
|
len -= buflen;
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(f);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-05-30 10:05:27 +00:00
|
|
|
int
|
2007-04-02 17:10:48 +00:00
|
|
|
block_writeout_data(FILE *outfile, struct csys_block *block)
|
|
|
|
{
|
|
|
|
int res;
|
|
|
|
size_t padlen;
|
|
|
|
|
|
|
|
res = block_writeout_file(outfile, block);
|
|
|
|
if (res)
|
|
|
|
return res;
|
|
|
|
|
|
|
|
/* write padding data if neccesary */
|
|
|
|
padlen = block->size_avail - block->file_size;
|
2007-05-30 10:05:27 +00:00
|
|
|
DBG(1,"padding block, length=%d", padlen);
|
2007-04-02 17:10:48 +00:00
|
|
|
res = write_out_padding(outfile, padlen, block->padc, block->css);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-05-30 10:05:27 +00:00
|
|
|
int
|
2007-04-02 17:10:48 +00:00
|
|
|
block_writeout_csum(FILE *outfile, struct csys_block *block)
|
|
|
|
{
|
|
|
|
uint16_t csum;
|
|
|
|
int res;
|
|
|
|
|
|
|
|
if (block->size_csum == 0)
|
|
|
|
return 0;
|
|
|
|
|
2007-05-30 10:05:27 +00:00
|
|
|
DBG(1,"writing checksum for block");
|
2007-04-02 17:10:48 +00:00
|
|
|
csum = HOST_TO_LE16(csum_get(block->css));
|
|
|
|
res = write_out_data(outfile, (uint8_t *)&csum, block->size_csum, NULL);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
block_writeout(FILE *outfile, struct csys_block *block)
|
|
|
|
{
|
|
|
|
int res;
|
|
|
|
struct csum_state css;
|
2007-05-30 10:05:27 +00:00
|
|
|
|
2007-04-02 17:10:48 +00:00
|
|
|
res = 0;
|
|
|
|
|
|
|
|
if (block == NULL)
|
|
|
|
return res;
|
|
|
|
|
|
|
|
block->css = NULL;
|
|
|
|
|
2007-05-30 10:05:27 +00:00
|
|
|
DBG(2, "writing block, file=%s, file_size=%d, space=%d",
|
2007-04-02 17:10:48 +00:00
|
|
|
block->file_name, block->file_size, block->size_avail);
|
|
|
|
res = block_writeout_hdr(outfile, block);
|
|
|
|
if (res)
|
|
|
|
return res;
|
|
|
|
|
|
|
|
if (block->size_csum != 0) {
|
|
|
|
block->css = &css;
|
|
|
|
csum_init(&css, block->size_csum);
|
|
|
|
}
|
|
|
|
|
|
|
|
res = block_writeout_data(outfile, block);
|
|
|
|
if (res)
|
|
|
|
return res;
|
|
|
|
|
|
|
|
res = block_writeout_csum(outfile, block);
|
|
|
|
if (res)
|
|
|
|
return res;
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
write_out_blocks(FILE *outfile)
|
|
|
|
{
|
|
|
|
struct csys_block *block;
|
|
|
|
int i, res;
|
|
|
|
|
|
|
|
res = block_writeout(outfile, boot_block);
|
|
|
|
if (res)
|
|
|
|
return res;
|
|
|
|
|
|
|
|
res = block_writeout(outfile, conf_block);
|
|
|
|
if (res)
|
|
|
|
return res;
|
|
|
|
|
|
|
|
res = block_writeout(outfile, webp_block);
|
|
|
|
if (res)
|
|
|
|
return res;
|
|
|
|
|
|
|
|
res = block_writeout(outfile, code_block);
|
|
|
|
if (res)
|
|
|
|
return res;
|
|
|
|
|
|
|
|
res = 0;
|
|
|
|
for (i=0; i < num_blocks; i++) {
|
|
|
|
block = &blocks[i];
|
2007-05-30 10:05:27 +00:00
|
|
|
|
2007-04-02 17:10:48 +00:00
|
|
|
if (block->type != BLOCK_TYPE_XTRA)
|
|
|
|
continue;
|
2007-05-30 10:05:27 +00:00
|
|
|
|
2007-04-02 17:10:48 +00:00
|
|
|
res = block_writeout(outfile, block);
|
|
|
|
if (res)
|
|
|
|
break;
|
|
|
|
}
|
2007-05-30 10:05:27 +00:00
|
|
|
|
2007-04-02 17:10:48 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct board_info *
|
|
|
|
find_board(char *model)
|
|
|
|
{
|
|
|
|
struct board_info *ret;
|
|
|
|
struct board_info *board;
|
|
|
|
|
|
|
|
ret = NULL;
|
|
|
|
for (board = boards; board->model != NULL; board++){
|
2007-08-06 17:38:40 +00:00
|
|
|
if (strcasecmp(model, board->model) == 0) {
|
2007-04-02 17:10:48 +00:00
|
|
|
ret = board;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
parse_opt_board(char ch, char *arg)
|
|
|
|
{
|
|
|
|
|
2007-05-30 10:05:27 +00:00
|
|
|
DBG(1,"parsing board option: -%c %s", ch, arg);
|
2007-04-02 17:10:48 +00:00
|
|
|
|
|
|
|
if (board != NULL) {
|
2007-05-30 10:05:27 +00:00
|
|
|
ERR("only one board option allowed");
|
|
|
|
return ERR_FATAL;
|
2007-04-02 17:10:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (required_arg(ch, arg))
|
2007-05-30 10:05:27 +00:00
|
|
|
return ERR_FATAL;
|
2007-04-02 17:10:48 +00:00
|
|
|
|
|
|
|
board = find_board(arg);
|
|
|
|
if (board == NULL){
|
2007-05-30 10:05:27 +00:00
|
|
|
ERR("invalid/unknown board specified: %s", arg);
|
|
|
|
return ERR_FATAL;
|
2007-04-02 17:10:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
parse_opt_block(char ch, char *arg)
|
|
|
|
{
|
|
|
|
char buf[MAX_ARG_LEN];
|
|
|
|
char *argv[MAX_ARG_COUNT];
|
|
|
|
int argc;
|
|
|
|
char *p;
|
|
|
|
struct csys_block *block;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if ( num_blocks > MAX_NUM_BLOCKS ) {
|
2007-05-30 10:05:27 +00:00
|
|
|
ERR("too many blocks specified");
|
|
|
|
return ERR_FATAL;
|
2007-04-02 17:10:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
block = &blocks[num_blocks];
|
2007-05-30 10:05:27 +00:00
|
|
|
|
2007-04-02 17:10:48 +00:00
|
|
|
/* setup default field values */
|
|
|
|
block->need_file = 1;
|
|
|
|
block->padc = 0xFF;
|
2007-05-30 10:05:27 +00:00
|
|
|
|
2007-04-02 17:10:48 +00:00
|
|
|
switch (ch) {
|
|
|
|
case 'b':
|
|
|
|
if (boot_block) {
|
2007-05-30 10:05:27 +00:00
|
|
|
WARN("only one boot block allowed");
|
2007-04-02 17:10:48 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
block->type = BLOCK_TYPE_BOOT;
|
|
|
|
boot_block = block;
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
if (conf_block) {
|
2007-05-30 10:05:27 +00:00
|
|
|
WARN("only one config block allowed");
|
2007-04-02 17:10:48 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
block->type = BLOCK_TYPE_CONF;
|
|
|
|
conf_block = block;
|
|
|
|
break;
|
|
|
|
case 'w':
|
|
|
|
if (webp_block) {
|
2007-05-30 10:05:27 +00:00
|
|
|
WARN("only one web block allowed");
|
2007-04-02 17:10:48 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
block->type = BLOCK_TYPE_WEBP;
|
|
|
|
block->size_hdr = sizeof(struct csys_header);
|
2007-11-03 14:53:24 +00:00
|
|
|
block->size_csum = CSUM_SIZE_8;
|
2007-04-02 17:10:48 +00:00
|
|
|
block->need_file = 0;
|
|
|
|
webp_block = block;
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
if (code_block) {
|
2007-05-30 10:05:27 +00:00
|
|
|
WARN("only one runtime block allowed");
|
2007-04-02 17:10:48 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
block->type = BLOCK_TYPE_CODE;
|
|
|
|
block->size_hdr = sizeof(struct csys_header);
|
2007-11-03 14:53:24 +00:00
|
|
|
block->size_csum = CSUM_SIZE_16;
|
2007-04-02 17:10:48 +00:00
|
|
|
code_block = block;
|
|
|
|
break;
|
|
|
|
case 'x':
|
|
|
|
block->type = BLOCK_TYPE_XTRA;
|
|
|
|
break;
|
|
|
|
default:
|
2007-05-30 10:05:27 +00:00
|
|
|
ERR("unknown block type \"%c\"", ch);
|
|
|
|
return ERR_FATAL;
|
2007-04-02 17:10:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
argc = parse_arg(arg, buf, argv);
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
p = argv[i++];
|
|
|
|
if (!is_empty_arg(p)) {
|
|
|
|
block->file_name = strdup(p);
|
|
|
|
if (block->file_name == NULL) {
|
2007-05-30 10:05:27 +00:00
|
|
|
ERR("not enough memory");
|
|
|
|
return ERR_FATAL;
|
2007-04-02 17:10:48 +00:00
|
|
|
}
|
|
|
|
} else if (block->need_file){
|
2007-05-30 10:05:27 +00:00
|
|
|
ERR("no file specified in %s", arg);
|
|
|
|
return ERR_FATAL;
|
2007-04-02 17:10:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (block->size_hdr) {
|
|
|
|
p = argv[i++];
|
|
|
|
if (!is_empty_arg(p)) {
|
|
|
|
if (str2u32(p, &block->addr) != 0) {
|
2007-05-30 10:05:27 +00:00
|
|
|
ERR("invalid start address in %s", arg);
|
|
|
|
return ERR_FATAL;
|
2007-04-02 17:10:48 +00:00
|
|
|
}
|
|
|
|
block->addr_set = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
p = argv[i++];
|
|
|
|
if (!is_empty_arg(p)) {
|
2007-11-03 14:53:24 +00:00
|
|
|
if (str2u32(p, &block->align) != 0) {
|
|
|
|
ERR("invalid alignment value in %s", arg);
|
2007-05-30 10:05:27 +00:00
|
|
|
return ERR_FATAL;
|
2007-04-02 17:10:48 +00:00
|
|
|
}
|
2007-11-03 14:53:24 +00:00
|
|
|
block->align_set = 1;
|
2007-04-02 17:10:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
p = argv[i++];
|
|
|
|
if (!is_empty_arg(p) && (str2u8(p, &block->padc) != 0)) {
|
2007-05-30 10:05:27 +00:00
|
|
|
ERR("invalid paddig character in %s", arg);
|
|
|
|
return ERR_FATAL;
|
2007-04-02 17:10:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
num_blocks++;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
process_blocks(void)
|
|
|
|
{
|
|
|
|
struct csys_block *block;
|
2007-11-03 14:53:24 +00:00
|
|
|
uint32_t offs = 0;
|
2007-04-02 17:10:48 +00:00
|
|
|
int i;
|
|
|
|
int res;
|
|
|
|
|
2007-05-30 10:05:27 +00:00
|
|
|
res = 0;
|
2007-04-02 17:10:48 +00:00
|
|
|
/* collecting stats */
|
|
|
|
for (i = 0; i < num_blocks; i++) {
|
|
|
|
block = &blocks[i];
|
|
|
|
res = block_stat_file(block);
|
|
|
|
if (res)
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* bootloader */
|
|
|
|
block = boot_block;
|
|
|
|
if (block) {
|
2007-11-03 14:53:24 +00:00
|
|
|
block->size = board->boot_size;
|
|
|
|
if (block->file_size > board->boot_size) {
|
2007-05-30 10:05:27 +00:00
|
|
|
WARN("boot block is too big");
|
|
|
|
res = ERR_INVALID_IMAGE;
|
2007-04-02 17:10:48 +00:00
|
|
|
}
|
|
|
|
}
|
2007-11-03 14:53:24 +00:00
|
|
|
offs += board->boot_size;
|
2007-04-02 17:10:48 +00:00
|
|
|
|
|
|
|
/* configuration data */
|
|
|
|
block = conf_block;
|
|
|
|
if (block) {
|
2007-11-03 14:53:24 +00:00
|
|
|
block->size = board->conf_size;
|
|
|
|
if (block->file_size > board->conf_size) {
|
2007-05-30 10:05:27 +00:00
|
|
|
WARN("config block is too big");
|
|
|
|
res = ERR_INVALID_IMAGE;
|
2007-04-02 17:10:48 +00:00
|
|
|
}
|
|
|
|
}
|
2007-11-03 14:53:24 +00:00
|
|
|
offs += board->conf_size;
|
2007-04-02 17:10:48 +00:00
|
|
|
|
|
|
|
/* webpages */
|
|
|
|
block = webp_block;
|
|
|
|
if (block) {
|
2007-11-03 14:53:24 +00:00
|
|
|
|
|
|
|
memcpy(block->sig, board->sig_webp, 4);
|
|
|
|
|
|
|
|
if (block->addr_set == 0)
|
|
|
|
block->addr = board->addr_webp;
|
|
|
|
|
|
|
|
if (block->align_set == 0)
|
|
|
|
block->align = DEFAULT_BLOCK_ALIGN;
|
|
|
|
|
|
|
|
block->size = align(offs + block->file_size + block->size_hdr +
|
|
|
|
block->size_csum, block->align) - offs;
|
|
|
|
|
2007-04-02 17:10:48 +00:00
|
|
|
if (block->size > board->webp_size_max) {
|
2007-05-30 10:05:27 +00:00
|
|
|
WARN("webpages block is too big");
|
|
|
|
res = ERR_INVALID_IMAGE;
|
2007-04-02 17:10:48 +00:00
|
|
|
}
|
2007-11-03 14:53:24 +00:00
|
|
|
|
|
|
|
DBG(2,"webpages start at %08x, size=%08x", offs,
|
|
|
|
block->size);
|
|
|
|
|
|
|
|
offs += block->size;
|
|
|
|
if (offs > board->flash_size) {
|
|
|
|
WARN("webp block is too big");
|
|
|
|
res = ERR_INVALID_IMAGE;
|
|
|
|
}
|
2007-04-02 17:10:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* runtime code */
|
|
|
|
block = code_block;
|
|
|
|
if (block) {
|
2007-11-03 14:53:24 +00:00
|
|
|
memcpy(code_block->sig, SIG_CSYS, 4);
|
|
|
|
|
|
|
|
if (block->addr_set == 0)
|
|
|
|
block->addr = board->addr_code;
|
|
|
|
|
|
|
|
if (block->align_set == 0)
|
|
|
|
block->align = DEFAULT_BLOCK_ALIGN;
|
|
|
|
|
|
|
|
block->size = align(offs + block->file_size +
|
|
|
|
block->size_hdr + block->size_csum,
|
|
|
|
block->align) - offs;
|
|
|
|
|
|
|
|
DBG(2,"code block start at %08x, size=%08x", offs,
|
|
|
|
block->size);
|
|
|
|
|
|
|
|
offs += block->size;
|
|
|
|
if (offs > board->flash_size) {
|
2007-05-30 10:05:27 +00:00
|
|
|
WARN("code block is too big");
|
|
|
|
res = ERR_INVALID_IMAGE;
|
2007-04-02 17:10:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < num_blocks; i++) {
|
|
|
|
block = &blocks[i];
|
|
|
|
|
|
|
|
if (block->type != BLOCK_TYPE_XTRA)
|
|
|
|
continue;
|
|
|
|
|
2007-11-03 14:53:24 +00:00
|
|
|
if (block->align_set == 0)
|
|
|
|
block->align = DEFAULT_BLOCK_ALIGN;
|
2007-04-02 17:10:48 +00:00
|
|
|
|
2007-11-03 14:53:24 +00:00
|
|
|
block->size = align(offs + block->file_size,
|
|
|
|
block->align) - offs;
|
|
|
|
|
|
|
|
DBG(2,"file %s start at %08x, size=%08x, align=%08x",
|
|
|
|
block->file_name, offs, block->size, block->align);
|
|
|
|
|
|
|
|
offs += block->size;
|
|
|
|
if (offs > board->flash_size) {
|
2007-05-30 10:05:27 +00:00
|
|
|
WARN("file %s is too big, size=%d, avail=%d",
|
|
|
|
block->file_name, block->file_size,
|
2007-11-03 14:53:24 +00:00
|
|
|
board->flash_size - offs);
|
2007-05-30 10:05:27 +00:00
|
|
|
res = ERR_INVALID_IMAGE;
|
2007-04-02 17:10:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < num_blocks; i++) {
|
|
|
|
block = &blocks[i];
|
|
|
|
|
|
|
|
block->size_avail = block->size - block->size_hdr -
|
|
|
|
block->size_csum;
|
|
|
|
|
|
|
|
if (block->size_avail < block->file_size) {
|
2007-05-30 10:05:27 +00:00
|
|
|
WARN("file %s is too big, size=%d, avail=%d",
|
|
|
|
block->file_name, block->file_size,
|
2007-04-02 17:10:48 +00:00
|
|
|
block->size_avail);
|
2007-05-30 10:05:27 +00:00
|
|
|
res = ERR_INVALID_IMAGE;
|
2007-04-02 17:10:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
int optinvalid = 0; /* flag for invalid option */
|
|
|
|
int c;
|
2007-05-30 10:05:27 +00:00
|
|
|
int res = ERR_FATAL;
|
2007-04-02 17:10:48 +00:00
|
|
|
|
|
|
|
FILE *outfile;
|
2007-05-30 10:05:27 +00:00
|
|
|
|
2007-04-02 17:10:48 +00:00
|
|
|
progname=basename(argv[0]);
|
|
|
|
|
|
|
|
opterr = 0; /* could not print standard getopt error messages */
|
|
|
|
while ( 1 ) {
|
|
|
|
optinvalid = 0;
|
2007-05-30 10:05:27 +00:00
|
|
|
|
|
|
|
c = getopt(argc, argv, "b:B:c:dhkr:vw:x:");
|
2007-04-02 17:10:48 +00:00
|
|
|
if (c == -1)
|
|
|
|
break;
|
|
|
|
|
|
|
|
switch (c) {
|
|
|
|
case 'b':
|
|
|
|
case 'c':
|
|
|
|
case 'r':
|
|
|
|
case 'x':
|
|
|
|
optinvalid = parse_opt_block(c,optarg);
|
|
|
|
break;
|
|
|
|
case 'w':
|
|
|
|
if (optarg != NULL && *optarg == '-') {
|
|
|
|
/* rollback */
|
|
|
|
optind--;
|
|
|
|
optarg = NULL;
|
|
|
|
}
|
|
|
|
optinvalid = parse_opt_block(c,optarg);
|
|
|
|
break;
|
|
|
|
case 'd':
|
2007-05-30 10:05:27 +00:00
|
|
|
invalid_causes_error = 0;
|
|
|
|
break;
|
|
|
|
case 'k':
|
|
|
|
keep_invalid_images = 1;
|
2007-04-02 17:10:48 +00:00
|
|
|
break;
|
|
|
|
case 'B':
|
|
|
|
optinvalid = parse_opt_board(c,optarg);
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
verblevel++;
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
usage(EXIT_SUCCESS);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
optinvalid = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (optinvalid != 0 ){
|
2007-05-30 10:05:27 +00:00
|
|
|
ERR("invalid option: -%c", optopt);
|
2007-04-02 17:10:48 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-05-30 10:05:27 +00:00
|
|
|
if (board == NULL) {
|
|
|
|
ERR("no board specified");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2007-04-02 17:10:48 +00:00
|
|
|
if (optind == argc) {
|
2007-05-30 10:05:27 +00:00
|
|
|
ERR("no output file specified");
|
2007-04-02 17:10:48 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
ofname = argv[optind++];
|
|
|
|
|
|
|
|
if (optind < argc) {
|
2007-05-30 10:05:27 +00:00
|
|
|
ERR("invalid option: %s", argv[optind]);
|
2007-04-02 17:10:48 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2007-05-30 10:05:27 +00:00
|
|
|
res = process_blocks();
|
|
|
|
if (res == ERR_FATAL)
|
2007-04-02 17:10:48 +00:00
|
|
|
goto out;
|
|
|
|
|
2007-05-30 10:05:27 +00:00
|
|
|
if (res == ERR_INVALID_IMAGE) {
|
|
|
|
if (invalid_causes_error)
|
|
|
|
res = ERR_FATAL;
|
|
|
|
|
|
|
|
if (keep_invalid_images == 0) {
|
|
|
|
WARN("generation of invalid images disabled", ofname);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
WARN("generating invalid image", ofname);
|
2007-04-02 17:10:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
outfile = fopen(ofname, "w");
|
|
|
|
if (outfile == NULL) {
|
2007-05-30 10:05:27 +00:00
|
|
|
ERRS("could not open \"%s\" for writing", ofname);
|
|
|
|
res = ERR_FATAL;
|
2007-04-02 17:10:48 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2007-05-30 10:05:27 +00:00
|
|
|
if (write_out_blocks(outfile) != 0) {
|
|
|
|
res = ERR_FATAL;
|
2007-04-02 17:10:48 +00:00
|
|
|
goto out_flush;
|
2007-05-30 10:05:27 +00:00
|
|
|
}
|
2007-04-02 17:10:48 +00:00
|
|
|
|
2007-05-30 10:05:27 +00:00
|
|
|
DBG(1,"Image file %s completed.", ofname);
|
2007-04-02 17:10:48 +00:00
|
|
|
|
|
|
|
out_flush:
|
|
|
|
fflush(outfile);
|
|
|
|
fclose(outfile);
|
2007-05-30 10:05:27 +00:00
|
|
|
if (res == ERR_FATAL) {
|
2007-04-02 17:10:48 +00:00
|
|
|
unlink(ofname);
|
|
|
|
}
|
|
|
|
out:
|
2007-05-30 10:05:27 +00:00
|
|
|
if (res == ERR_FATAL)
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
2007-04-02 17:10:48 +00:00
|
|
|
}
|