From 85fdbfb35437f5ab8a449235f2c86f410770925f Mon Sep 17 00:00:00 2001 From: pancake Date: Mon, 8 Oct 2007 00:25:56 +0200 Subject: * Finally implement the support for the FIASCO firmware format * Up version to 0.3 * Alphabetically order the help message flags * Cleaner short help message * Pseudo-api for creating firmwares (not yet finished) * Add '-F' flag to flash from a fiasco image --- doc/usage | 27 +++--- src/Makefile | 2 +- src/fiasco.c | 268 +++++++++++++++++++++++++++++++++++++++-------------------- src/main.c | 65 +++++++++++---- src/main.h | 12 +++ 5 files changed, 255 insertions(+), 119 deletions(-) diff --git a/doc/usage b/doc/usage index d297505..f784b1d 100644 --- a/doc/usage +++ b/doc/usage @@ -8,6 +8,7 @@ Tablets (n770 and n800). - reboot de mother board - extract the current firmware pieces from the device (dump) - set the USB host/slave mode + - pack/unpack fiasco firmwares - fine grained R&D flags support - identify firmware pieces - dump mtd partitions (embedded mtd-utils source code) @@ -19,27 +20,29 @@ Tablets (n770 and n800). This is the help message of the tool:
 ~$ 0xFFFF -h
-0xFFFF v0.2  // The Free Fiasco Firmware Flasher
+0xFFFF v0.3  // The Free Fiasco Firmware Flasher
  -b [arg]       boots the kernel with arguments
+ -c             console prompt mode
+ -C [/dev/mtd]  check bad blocks on mtd
+ -d [vid:pid]   injects a usb device into the supported list
+ -D [0|1|2]     sets the root device to flash (0), mmc (1) or usb (2)
  -e [path]      dump and extract pieces to path
- -r [0|1]       disable/enable R&D mode
  -f      set the given RD flags (see '-f help')
- -p [[p%]file]  piece-of-firmware % file-where-this-piece-is
- -u [fiasco]    unpack target fiasco image
- -U [0|1]       disable/enable the usb host mode
- -s [serial]    serial port console (minicom like terminal)
- -C [/dev/mtd]  check bad blocks on mtd
- -c             console prompt mode
+ -F [fiasco]    flash a fiasco firmware image
  -h             show this help message
+ -H [file]      calculate hash for file
  -i             show device information (let standby mode)
  -I [piece]     identify a firmware piece
- -l             list supported usb device ids
- -d [vid:pid]   injects a usb device into the supported list
- -D [0|1|2]     sets the root device to flash (0), mmc (1) or usb (2)
+ -l, -L         list supported usb device ids
+ -p [[p%]file]  piece-of-firmware % file-where-this-piece-is
+ -r [0|1]       disable/enable R&D mode
  -R             reboot the omap board
- -x             extract configuration entries from /dev/mtd1
+ -s [serial]    serial port console (minicom like terminal)
+ -u [fiasco]    unpack target fiasco image
+ -U [0|1]       disable/enable the usb host mode
  -v             be verbose and noisy
  -V             show 0xFFFF version information
+ -x             extract configuration entries from /dev/mtd1
 Pieces are: xloader secondary kernel initfs rootfs omap-nand 
 
diff --git a/src/Makefile b/src/Makefile index 5d9203b..d8547ae 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,4 +1,4 @@ -VERSION=0.2 +VERSION=0.3 OBJ=main.o fiasco.o hexdump.o dump.o flash.o serial.o OBJ+=hash.o fpid.o query.o pieces.o utils.o devices.o console.o BIN=0xFFFF diff --git a/src/fiasco.c b/src/fiasco.c index 31198dd..d31c846 100644 --- a/src/fiasco.c +++ b/src/fiasco.c @@ -16,119 +16,211 @@ * along with this program. If not, see . */ -#include "main.h" #include #include #include +#include #include #include #include -#include +#include +#include "main.h" -#warning The FIASCO format is not yet fully implemented. +void (*fiasco_callback)(struct header_t *header) = NULL; -off_t piece_header_to_size(unsigned char *header) +int openfiasco(char *name) { - off_t sz = 0; - sz += header[0x15+3]; - sz += header[0x15+2] << 8; - sz += header[0x15+1] << 16; - sz += header[0x15+0] << 24; - return sz; -} + struct header_t header; + unsigned char buf[128]; + unsigned char data[128]; + unsigned int namelen; + int i; + int fd = open(name, O_RDONLY); -int piece_header_is_valid(unsigned char *header) -{ - if (header[0]=='T' && header[1]=='\x02') + if (fd == -1) { + fprintf(stderr, "Cannot open %s\n", name); return 1; + } + + /* read header */ + read(fd, buf, 5); + if (buf[0] != 0xb4) { + printf("Invalid header\n"); + return close(fd); + } + memcpy(&namelen,buf+1,4); + namelen = ntohl(namelen); + if (namelen>128) { + printf("Stupid length at header. Is this a joke?\n"); + return close(fd); + } + memset(buf,'\0', 128); + read(fd, buf, namelen); + // 6 first bytes are unknown + // buf 00 00 00 VERSION e8 0e +// printf("6Bytes: %02x %02x %02x %02x %02x %02x\n", +// buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]); + printf("Fiasco version: %2d\n", buf[3]); + strcpy(header.fwname, buf+6); + for(i=6;i'0' && buf[8] < '9') { + if (read(fd, data, 1)<1) + break; + i = data[0]; + if (read(fd, data, i)name); + printf("DATA: %02x\n", header->data[0]); +} - // print version header - if (read(fd, version, 55) != 55) { - printf("Error reading fiasco version.\n"); - goto __fiasco_read_image_end; +int main(int argc, char **argv) +{ + if (argc!=2) { + printf("Usage: unfiasco [file]\n"); + return 1; } - printf("# Fiasco header information:\n"); - printf("# ==========================\n"); - printf("# firmware type: %s\n", version); - version2 = version+strlen((char *)version)+3; - if (*version2=='.') - printf("firmware version: (null) (old firmware? no version string found?)\n"); - else printf("firmware version: %s\n", version2); - - // pieces: - // after the version2 string starts the header-body loop love - lseek(fd, 0xf + strlen(version2) + strlen(version), SEEK_SET); - do { - char piece_header[30]; - char description[256]; - unsigned char desc_len; - char *name; - off_t size; - off_t tmp = lseek(fd, 0, SEEK_CUR); - - size = read(fd, piece_header, 30); - if (size != 30) { - fprintf(stderr, "Unexpected end of file\n"); - break; - } - dump_bytes(piece_header,30); - if (!piece_header_is_valid(piece_header)) { - fprintf(stderr, "Oops. Invalid piece header.\n"); - break; - } - size = piece_header_to_size(piece_header); - name = piece_header_to_name(piece_header); - printf("# %s is %lld bytes\n", name, size); - - read(fd, description, 255); - printf("# version: %s\n", description); - /* lseek foreach subimage */ - lseek(fd, tmp, SEEK_SET); - lseek(fd, strlen(description) + 0x20, SEEK_CUR); - printf("rsc '%s' %lld '%s.bin' 0 %lld\n", file, lseek(fd,0,SEEK_CUR), name, size); - lseek(fd, size, SEEK_CUR); - } while(1); - - //printf("Image '%s', size %d bytes.\n", image, size); - -__fiasco_read_image_end: +/* + fd = fiasco_new("myfiasco", "pancake-edition"); + fiasco_add(fd, "kernel", "zImage", "2.6.22"); close(fd); - return 0; +*/ + +// fiasco_callback = &my_callback; + + return openfiasco(argv[1]); } +#endif diff --git a/src/main.c b/src/main.c index c5b01df..1022e78 100644 --- a/src/main.c +++ b/src/main.c @@ -73,27 +73,28 @@ void show_usage() { int i; show_title(); - printf(" -b [arg] boots the kernel with arguments\n"); + printf(" -c console prompt mode\n"); + printf(" -C [/dev/mtd] check bad blocks on mtd\n"); + printf(" -d [vid:pid] injects a usb device into the supported list\n"); + printf(" -D [0|1|2] sets the root device to flash (0), mmc (1) or usb (2)\n"); printf(" -e [path] dump and extract pieces to path\n"); - printf(" -r [0|1] disable/enable R&D mode\n"); printf(" -f set the given RD flags (see '-f help')\n"); - printf(" -p [[p%%]file] piece-of-firmware %% file-where-this-piece-is\n"); - printf(" -u [fiasco] unpack target fiasco image\n"); - printf(" -U [0|1] disable/enable the usb host mode\n"); - printf(" -s [serial] serial port console (minicom like terminal)\n"); - printf(" -C [/dev/mtd] check bad blocks on mtd\n"); - printf(" -c console prompt mode\n"); + printf(" -F [fiasco] flash a fiasco firmware image\n"); printf(" -h show this help message\n"); + printf(" -H [file] calculate hash for file\n"); printf(" -i show device information (let standby mode)\n"); printf(" -I [piece] identify a firmware piece\n"); - printf(" -l list supported usb device ids\n"); - printf(" -d [vid:pid] injects a usb device into the supported list\n"); - printf(" -D [0|1|2] sets the root device to flash (0), mmc (1) or usb (2)\n"); + printf(" -l, -L list supported usb device ids\n"); + printf(" -p [[p%%]file] piece-of-firmware %% file-where-this-piece-is\n"); + printf(" -r [0|1] disable/enable R&D mode\n"); printf(" -R reboot the omap board\n"); - printf(" -x extract configuration entries from /dev/mtd1\n"); + printf(" -s [serial] serial port console (minicom like terminal)\n"); + printf(" -u [fiasco] unpack target fiasco image\n"); + printf(" -U [0|1] disable/enable the usb host mode\n"); printf(" -v be verbose and noisy\n"); printf(" -V show 0xFFFF version information\n"); + printf(" -x extract configuration entries from /dev/mtd1\n"); printf("Pieces are: "); for(i=0;pieces[i];i++) printf("%s ", pieces[i]); printf("\n"); // serial port support is not yet done (cold flash is for flashing the 8kB nand) @@ -102,10 +103,32 @@ void show_usage() exit(0); } + +void unpack_callback(struct header_t *header) +{ + FILE *fd = fopen(header->name, "wb"); + if (fd == NULL) { + printf("Cannot open file.\n"); + return; + } + fwrite(header->data, header->size, 1, fd); + fclose(fd); +} + void unpack_fiasco_image(char *file) { - fiasco_read_image(file); - // TODO + printf("Dumping firmware pieces to disk.\n"); + fiasco_callback = &unpack_callback; + openfiasco( file ); +} + +int fiasco_flash(char *file) +{ + /* TODO */ + fiasco_callback = NULL; + openfiasco( file ); + + printf("\nTODO: Implement the fiasco flashing here.\n"); } int connect_via_usb() @@ -185,12 +208,17 @@ int main(int argc, char **argv) { int c; - while((c = getopt(argc, argv, "C:cp:vVhRu:ib:U:r:e:ld:I:D:f:s:x")) != -1) { + while((c = getopt(argc, argv, "C:cp:vVhRu:ib:U:r:e:Lld:I:D:f:F:s:xH:")) != -1) { switch(c) { + case 'H': + printf("xorpair: %04x\n", do_hash_file(optarg)); + return 0; case 'x': return dump_config(); case 'c': return console_prompt(); + case 'F': + return fiasco_flash(optarg); case 'd': sscanf(optarg, "%04hx:%04hx", &supported_devices[SUPPORTED_DEVICES-2].vendor_id, @@ -232,6 +260,7 @@ int main(int argc, char **argv) case 'p': add_piece(optarg); break; + case 'L': case 'l': list_valid_devices(); return 0; @@ -274,9 +303,9 @@ int main(int argc, char **argv) && (usb_mode == -1) && (root_device == -1)) { - printf("Usage: 0xFFFF [-hvVRi] [-e path] [-U 0|1] [-p [piece%%]file [-p ...]]\n"); - printf(" [-b boot-args] [-I piece [-I ...]] [-u fiasco-image] [-x]\n"); - printf(" [-D 0|1|2] [-F rd flags] [-s serial-dev] [-c] [-C mtd-dev]\n"); + printf("0xFFFF [-chiLRvVx] [-C mtd-dev] [-d vid:pid] [-D 0|1|2] [-e path] [-f flags]\n"); + printf(" [-F fiasco] [-H hash-file] [-I piece] [-p [piece%%]file]] [-r 0|1]\n"); + printf(" [-s serial-dev] [-u fiasco-image] [-U 0|1]\n"); return 1; } diff --git a/src/main.h b/src/main.h index dcbacff..d6dd46d 100644 --- a/src/main.h +++ b/src/main.h @@ -77,4 +77,16 @@ extern char *pieces[]; extern char *modes[]; extern char *root_devices[]; +// fiasco +extern void (*fiasco_callback)(struct header_t *header); +struct header_t { + char fwname[128]; + char name[128]; + char version[128]; + unsigned short hash; + unsigned int size; + unsigned char *data; +}; + + #endif -- cgit v1.2.3