From 3d427bfd7740731bfc203b047d557af564975cf6 Mon Sep 17 00:00:00 2001 From: pancake Date: Mon, 8 Dec 2008 00:21:05 +0100 Subject: * Added -n flag to do not write to disk or flash (simulation) * Added -S flag to specify subversion id manually while flashing or dumping * Fixed support for the last fiasco image - Skip paddings - More verbose output while parsing FIASCO headers * Initial implementation of the full direct FIASCO flasher - No need to dump manually before flashing * Some warnings and code cleanup * Fix building issue * Add HAVE_GUI directive in config.mk * Up to version 0.4 --- src/Makefile | 4 ++- src/fiasco.c | 43 ++++++++++++++++++++++++-------- src/flash.c | 2 +- src/gui/Makefile | 7 ++++++ src/main.c | 70 ++++++++++++++++++++++++++++++++++++++++++++--------- src/main.h | 12 ++++++--- src/qmode.c | 20 ++++++++------- src/squeue/squeue.c | 4 +++ src/utils.c | 4 +-- 9 files changed, 127 insertions(+), 39 deletions(-) (limited to 'src') diff --git a/src/Makefile b/src/Makefile index 9610497..e8dbffa 100644 --- a/src/Makefile +++ b/src/Makefile @@ -10,7 +10,9 @@ CFLAGS+=-pedantic -std=c99 -DHAVE_USB=${HAVE_USB} USBOBJ=../libusb/error.o ../libusb/usb.o ../libusb/descriptors.o USBOBJ+=../libusb/linux.o ## XXX not portable -all: ${OBJ} +all: ${BIN} + +${BIN}: ${OBJ} ${CC} ${LDFLAGS} -o ${BIN} ${OBJ} ${LIBS} allusb: ${OBJ} diff --git a/src/fiasco.c b/src/fiasco.c index 91b7481..192238f 100644 --- a/src/fiasco.c +++ b/src/fiasco.c @@ -27,16 +27,17 @@ #include "main.h" #include "hash.h" -void (*fiasco_callback)(struct header_t *header) = NULL; +int (*fiasco_callback)(struct header_t *header) = NULL; int openfiasco(char *name) { struct header_t header; unsigned char buf[128]; unsigned char data[128]; + unsigned char *pdata; unsigned int namelen; - off_t off; - int i; + off_t off, here; + int i,j; header.fd = open(name, O_RDONLY); if (header.fd == -1) { @@ -66,14 +67,22 @@ int openfiasco(char *name) /* walk the tree */ while(1) { + here = lseek(header.fd, 0, SEEK_CUR); if (read(header.fd, buf, 9)<9) break; - if (buf[0] != 0x54) { // 'T' - printf("unexpected header at 0x%x, found %02x %02x %02x\n", - ((int)lseek(header.fd, 0, SEEK_CUR))-9, - buf[0], buf[1], buf[2]); - break; + /* skipping some bytes */ + lseek(header.fd, -9, SEEK_CUR); + for(i=0;buf[0]!=0x54;i++) { + j = read(header.fd, buf, 1); + if (j == -1) { + printf("Cannot find next FIASCO header\n"); + return close(header.fd); + } + } + printf("Skipping %d padding bytes\n", i); + lseek(header.fd, -1, SEEK_CUR); + continue; } header.hash = buf[7]<<8|buf[8]; @@ -92,6 +101,7 @@ int openfiasco(char *name) break; memcpy(&header.size, buf,4); header.size = ntohl(header.size); + printf(" offset: 0x%08x\n", (unsigned int)here); printf(" size: %d bytes\n", header.size); printf(" hash: %04x\n", header.hash); //printf("BYTE: %02x %02x %02x %02x %02x\n", @@ -104,18 +114,31 @@ int openfiasco(char *name) i = data[0]; if (read(header.fd, data, i)name, "wb"); + FILE *fd; + + fiasco_data_read(header); + if (subverstr) { + if (strchr(header->name, ',') != NULL) { + if (!strstr(header->name, subverstr)) { + printf("Skipping '%s' does not matches -S subversion\n", + header->name); + return 1; + } + } + } + if (nomode) + return 1; + + fd = fopen(header->name, "wb"); if (fd == NULL) { printf("Cannot open file.\n"); - return; + return 1; } - fiasco_data_read(header); fwrite(header->data, header->size, 1, fd); fclose(fd); + + return 0; +} + +int flash_callback(struct header_t *header) +{ + int ret; + char *type; + + ret = unpack_callback(header); + if (ret) { + printf("ignored\n"); + return 1; + } + + type = (char *)fpid_file(header->name); + printf("Flashing %s (%s)\n", header->name, type); + flash_image(header->name, type, NULL); + + return 0; } void unpack_fiasco_image(char *file) @@ -126,21 +164,23 @@ void unpack_fiasco_image(char *file) int fiasco_flash(char *file) { - /* TODO */ - fiasco_callback = NULL; + check_nolo_order(); + get_sw_version(); + get_nolo_version(); + + fiasco_callback = &flash_callback; openfiasco( file ); - printf("\nTODO: Implement the fiasco flashing here.\n"); - return -1; + return 0; } #if HAVE_USB int connect_via_usb() { + static char pbc[]={'/','-','\\', '|'}; struct usb_device_descriptor udd; struct devices it_device; int c = 0; - static char pbc[]={'/','-','\\', '|'}; // usb_set_debug(5); usb_init(); @@ -214,7 +254,7 @@ int main(int argc, char **argv) { int c; - while((c = getopt(argc, argv, "QC:cp:PvVhRu:ib:U:r:e:ld:I:D:f:F:s:xH:")) != -1) { + while((c = getopt(argc, argv, "QC:cp:PvVhRu:ib:U:r:e:ld:I:D:f:F:s:xH:S:n")) != -1) { switch(c) { case 'H': printf("xorpair: %04x\n", do_hash_file(optarg)); @@ -227,6 +267,12 @@ int main(int argc, char **argv) case 'b': boot_cmdline = optarg; break; + case 'S': + subverstr = optarg; + break; + case 'n': + nomode = 1; + break; case 'U': usb_mode = atoi(optarg); break; @@ -321,8 +367,8 @@ int main(int argc, char **argv) { printf("# The Free Fiasco Firmware Flasher v"VERSION"\n" - "0xFFFF [-chilQRvVx] [-C mtd-dev] [-d vid:pid] [-D 0|1|2] [-e path] [-f flags]\n" - " [-F fiasco] [-H hash-file] [-I piece] [-p [piece%%]file]] [-r 0|1]\n" + "0xFFFF [-chinlQRvVx] [-C mtd-dev] [-d vid:pid] [-D 0|1|2] [-e path] [-f flags]\n" + " [-F fiasco] [-H hash-file] [-I piece] [-p [piece%%]file]] [-r 0|1] [-S subver]\n" " [-s serial-dev] [-u fiasco-image] [-U 0|1] | [-P new-fiasco] [piece1] [2] ..\n"); return 1; } diff --git a/src/main.h b/src/main.h index ed53d50..43937b5 100644 --- a/src/main.h +++ b/src/main.h @@ -5,7 +5,6 @@ #include "hash.h" #include "nolo.h" #include "os.h" - extern char strbuf[1024]; #define _FILE_OFFSET_BITS 64 @@ -16,11 +15,11 @@ struct devices; int queue_mode(); int reverse_extract_pieces(char *dir); -void flash_image(char *filename, char *piece, char *version); +void flash_image(const char *filename, const char *piece, const char *version); int fiasco_read_image(char *file); void check_nolo_order(); extern struct usb_dev_handle *dev; -unsigned long get_file_size(char *file); +unsigned long get_file_size(const char *file); void progressbar(unsigned long long part, unsigned long long total); const char *fpid_file(const char *filename); int add_piece(char *piece); @@ -107,10 +106,15 @@ int openfiasco(char *name); int fiasco_new(const char *filename, const char *name); void fiasco_data_read(struct header_t *header); int fiasco_add_eof(int fd); -extern void (*fiasco_callback)(struct header_t *header); +extern int (*fiasco_callback)(struct header_t *header); int fiasco_add(int fd, const char *name, const char *file, const char *version); int fiasco_pack(int optind, char *argv[]); int nanddump(char *mtddev, unsigned long start_addr, unsigned long length, char *dumpfile, int isbl, int ioob); +// +int reboot_board(); +int get_rd_flags(); +int get_usb_mode(); +int get_nolo_version(); #endif diff --git a/src/qmode.c b/src/qmode.c index 3aa73fa..5bfa765 100644 --- a/src/qmode.c +++ b/src/qmode.c @@ -19,7 +19,13 @@ #include "main.h" #include "os.h" #include +#include +#include #include +#include +#include +#include + #if HAVE_SQUEUE struct squeue_t *q; @@ -43,23 +49,19 @@ void process_message(char *msg) printf("[x] (%s)\n", msg); str = strdup(msg); arg = strchr(str, ':'); - if (c!=NULL) { + if (c!=0) { arg[0]='\0'; arg = arg +1; if (!strcmp(str, "flash")) { - char buf[1024]; - char *type = fpid_file(arg); + const char *type = fpid_file(arg); if (type == NULL) { squeue_push2(p, "error", "Unknown piece format", 1); - } else { - flash_image(arg, type, NULL); - } + } else flash_image(arg, type, NULL); } else if (!strcmp(str, "reset")) { if (reboot_board() == 0) { squeue_push2(p,"info", "Device reboots", 1); - } else - squeue_push2(p,"error", "Cannot reboot device", 1); + } else squeue_push2(p,"error", "Cannot reboot device", 1); } else if (!strcmp(str, "info")) { get_rd_flags(); @@ -95,7 +97,7 @@ int queue_mode() pid = dofork(); if (pid) { - wait(pid); + wait(&pid); return 0; } else { p = squeue_open("/tmp/0xFFFF.1", Q_CREAT); diff --git a/src/squeue/squeue.c b/src/squeue/squeue.c index 0d134f0..96f2b59 100644 --- a/src/squeue/squeue.c +++ b/src/squeue/squeue.c @@ -1,13 +1,17 @@ #include "squeue.h" +#define _XOPEN_SOURCE 500 /* Or: #define _BSD_SOURCE */ #include #include #include #include #include #include +#include +#include #include #include #include +#include #include #define POOL_SIZE ITEM_MAX*ITEM_SIZE diff --git a/src/utils.c b/src/utils.c index 2f4efbb..24872ed 100644 --- a/src/utils.c +++ b/src/utils.c @@ -22,7 +22,7 @@ #include #include -unsigned long get_file_size(char *file) +unsigned long get_file_size(const char *file) { FILE *fd = fopen(file, "r"); unsigned long len = 0; @@ -53,7 +53,7 @@ void progressbar(unsigned long long part, unsigned long long total) squeue_push2(p, "bar", msg, 0); } else { #endif - printf("\e[K %3d%% [", pc); + printf("\x1b[K %3d%% [", pc); if (columns) cols = atoi(columns); cols-=15; -- cgit v1.2.3