xmodem can use filedata

This commit is contained in:
Olaf Rempel 2007-04-30 14:32:54 +02:00
parent d083594257
commit ee74ea8a08
2 changed files with 8 additions and 44 deletions

View File

@ -2,7 +2,7 @@ CFLAGS := -O2 -pipe -Wall
all: zyxel-revert compress decompress all: zyxel-revert compress decompress
zyxel-revert: configfile.o event.o logging.o context.o serial.o statemachine.o xmodem.o zyxel-revert.o zyxel-revert: configfile.o event.o filedata.o logging.o context.o serial.o statemachine.o xmodem.o zyxel-revert.o
$(CC) $(CFLAGS) $^ -o $@ $(CC) $(CFLAGS) $^ -o $@
compress: lzsc.o filedata.o compress.o compress: lzsc.o filedata.o compress.o

View File

@ -10,10 +10,10 @@
#include "configfile.h" #include "configfile.h"
#include "context.h" #include "context.h"
#include "filedata.h"
#include "logging.h" #include "logging.h"
static char *filedata; struct filedata *filedata;
static int filesize;
enum { enum {
XM_SOH = 0x01, XM_SOH = 0x01,
@ -70,7 +70,7 @@ int xmodem_read(int fd, void *privdata)
break; break;
case XM_ACK: /* next packet */ case XM_ACK: /* next packet */
if (ctx->lastpkt * 128 == filesize) if (ctx->lastpkt * 128 == filedata->size)
return -1; return -1;
pktnum = ctx->lastpkt +1; pktnum = ctx->lastpkt +1;
@ -81,12 +81,12 @@ int xmodem_read(int fd, void *privdata)
break; break;
} }
if (pktnum * 128 < filesize) { if (pktnum * 128 < filedata->size) {
pkt.header = XM_SOH; pkt.header = XM_SOH;
pkt.count = ((pktnum +1) & 0xFF); pkt.count = ((pktnum +1) & 0xFF);
pkt.ncount = 0xFF - pkt.count; pkt.ncount = 0xFF - pkt.count;
memcpy(pkt.data, filedata + pktnum * 128, 128); memcpy(pkt.data, (void *)(filedata->data) + pktnum * 128, 128);
calc_crc(&pkt); calc_crc(&pkt);
write(fd, &pkt, sizeof(pkt)); write(fd, &pkt, sizeof(pkt));
@ -101,50 +101,14 @@ int xmodem_read(int fd, void *privdata)
return 0; return 0;
} }
static int load_file_data(const char *filename)
{
int fd = open(filename, O_RDONLY);
if (fd < 0) {
log_print(LOG_WARN, "load_file_content(): open()");
return -1;
}
struct stat filestat;
if (fstat(fd, &filestat) < 0) {
log_print(LOG_WARN, "load_file_content(): fstat()");
close(fd);
return -1;
}
filesize = filestat.st_size;
filedata = malloc(filesize);
if (filedata == NULL) {
log_print(LOG_WARN, "load_file_content(): malloc()");
close(fd);
return -1;
}
/* TODO: padding */
int readsize = read(fd, filedata, filesize);
if (readsize != filesize) {
log_print(LOG_WARN, "load_file_content(): read()");
free(filedata);
close(fd);
return -1;
}
close(fd);
return 0;
}
int xmodem_init(void) int xmodem_init(void)
{ {
const char *filename = config_get_string("global", "configdata", NULL); const char *filename = config_get_string("global", "configdata", NULL);
if (filename == NULL) if (filename == NULL)
return -1; return -1;
if (load_file_data(filename) < 0) filedata = get_filedata(filename);
if (filedata == NULL)
return -1; return -1;
return 0; return 0;