diff options
-rw-r--r-- | src/fiasco.c | 162 | ||||
-rw-r--r-- | src/main.c | 50 | ||||
-rw-r--r-- | src/main.h | 7 |
3 files changed, 156 insertions, 63 deletions
diff --git a/src/fiasco.c b/src/fiasco.c index d31c846..4bbb114 100644 --- a/src/fiasco.c +++ b/src/fiasco.c @@ -26,6 +26,37 @@ #include <arpa/inet.h> #include "main.h" +#if 0 + +SPECS FOR TEH FIASCO FIRMWARE FILE FORMAT ffff! +----------------------------------------------- + +FILE HEADER + + 1 byte = 0xb4 -- signature + 4 bytes -- firmware name length (big endian) + 6 bytes -- versioning info ( byte 4 is format version ) + N-6 bytes -- firmware name (zero fill) + + +PIECE HEADER + + 1 byte = 0x54 -- piece signature + 6 bytes -- unknown + 2 bytes -- checksum for the piece contents (xorpair) + 12 bytes -- piece name (first byte is FF if is the last) + 4 bytes -- piece size (big endian) + 4 bytes -- unknown + block { + 1 byte -- if (value '1'-'9') { ..there's a comment.. } + 1 byte -- length of comment + N bytes -- comment + } + N bytes -- piece data + + +#endif + void (*fiasco_callback)(struct header_t *header) = NULL; int openfiasco(char *name) @@ -34,64 +65,60 @@ int openfiasco(char *name) unsigned char buf[128]; unsigned char data[128]; unsigned int namelen; + off_t off; int i; - int fd = open(name, O_RDONLY); + header.fd = open(name, O_RDONLY); - if (fd == -1) { + if (header.fd == -1) { fprintf(stderr, "Cannot open %s\n", name); return 1; } /* read header */ - read(fd, buf, 5); + read(header.fd, buf, 5); if (buf[0] != 0xb4) { printf("Invalid header\n"); - return close(fd); + return close(header.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); + return close(header.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]); + read(header.fd, buf, namelen); + printf("Fiasco version: %2d\n", buf[3]); strcpy(header.fwname, buf+6); - for(i=6;i<namelen;i+=strlen(buf+i)+1) { + for(i=6;i<namelen;i+=strlen(buf+i)+1) printf("Name: %s\n", buf+i); - } /* walk the tree */ while(1) { - if (read(fd, buf, 9)<9) + if (read(header.fd, buf, 9)<9) break; if (buf[0] != 0x54) { // 'T' - printf("unexpected header at %d, found %02x %02x %02x\n", - (int)lseek(fd, 0, SEEK_CUR), + 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; } header.hash = buf[7]<<8|buf[8]; - //printf("BYTE: %02x %02x %02x %02x %02x %02x\n", - // buf[1], buf[2], buf[3], buf[4], buf[5], buf[6]); /* piece name */ memset(data, '\0', 13); - if (read(fd, data, 12)<12) + if (read(header.fd, data, 12)<12) break; + if (data[0] == 0xff) { printf(" [eof]\n"); break; } else printf(" %s\n", data); strcpy(header.name, data); - if (read(fd, buf, 9)<9) + if (read(header.fd, buf, 9)<9) break; memcpy(&header.size, buf,4); header.size = ntohl(header.size); @@ -102,85 +129,103 @@ int openfiasco(char *name) /* XXX this is not ok */ //printf("BUF8: %02x\n", buf[8]); while (buf[8] && buf[8]>'0' && buf[8] < '9') { - if (read(fd, data, 1)<1) + if (read(header.fd, data, 1)<1) break; i = data[0]; - if (read(fd, data, i)<i) + if (read(header.fd, data, i)<i) break; if (data[0]) printf(" version: %s\n", data); strcpy(header.version, data); - if (read(fd, buf+8, 1)<1) + if (read(header.fd, buf+8, 1)<1) break; } /* callback */ + off = lseek(header.fd, 0, SEEK_CUR); if (fiasco_callback != NULL) { - header.data = (char *)malloc(header.size); - if (header.data == NULL) { - printf("Cannot alloc %d bytes\n", header.size); - break; - } - read(fd, header.data, header.size); fiasco_callback(&header); free(header.data); continue; } - lseek(fd, header.size, SEEK_CUR); + // XXX dup + lseek(header.fd, off, SEEK_SET); + lseek(header.fd, header.size, SEEK_CUR); } return 0; } -/* fiasco writer */ +void fiasco_data_read(struct header_t *header) +{ + header->data = (char *)malloc(header->size); + if (header->data == NULL) { + printf("Cannot alloc %d bytes\n", header->size); + return; + } + read(header->fd, header->data, header->size); +} +/* fiasco writer */ int fiasco_new(const char *filename, const char *name) { int fd; - int len = htonl(strlen(name)); - fd = open(filename, O_RDWR|O_CREAT); + int len = htonl(strlen(name)+1+6); + fd = open(filename, O_RDWR|O_CREAT, 0644); if (fd == -1) return -1; write(fd, "\xb4", 1); write(fd, &len, 4); /* version header */ write(fd, "\x00\x00\x00\x02\xe8\x0e", 6); + /* firmware name */ write(fd, name, strlen(name)+1); return fd; } int fiasco_add_eof(int fd) { - unsigned char buf[32]; + unsigned char buf[120]; if (fd == -1) return -1; - memset(buf,'\xff', 32); - write(fd, buf, 32); + memset(buf,'\xff', 120); + write(fd, buf, 120); return 0; } + int fiasco_add(int fd, const char *name, const char *file, const char *version) { - int gd; + int gd,ret; unsigned int sz; unsigned char len; unsigned short hash; unsigned char *ptr = &hash; - char bname[13]; + char buf[4096]; + char bname[32]; if (fd == -1) return -1; + if (file == NULL) + return -1; gd = open(file, O_RDONLY); if (gd == -1) return -1; + sz = htonl((unsigned int) lseek(gd, 0, SEEK_END)); + lseek(gd, 0, SEEK_SET); // 4 bytes big endian write(fd, "T\x02\x2e\x19\x01\x01\x00", 7); // header? /* checksum */ hash = do_hash_file(file); ptr[0]^=ptr[1]; ptr[1]=ptr[0]^ptr[1]; ptr[0]^=ptr[1]; - write(fd, hash, 2); + write(fd, &hash, 2); + printf("hash: %04x\n", hash); memset(bname, '\0', 13); - strncpy(bname, name, 12); + if (name == NULL) + strncpy(bname, file, 12); + else + strncpy(bname, name, 12); + write(fd, bname, 12); write(fd, &sz, 4); if (version) { @@ -194,16 +239,53 @@ int fiasco_add(int fd, const char *name, const char *file, const char *version) } else { write(fd, "\x00\x00\x00\x00\x9b", 5); } + + while(1) { + ret = read(gd, buf, 4096); + if (ret<1) + break; + write(fd, buf, ret); + } + return 0; } +int fiasco_pack(int optind, char *argv[]) +{ + char *file = argv[optind]; + char *type; + int fd, ret; + + fd = fiasco_new(file, file); // TODO use a format here + if (fd == -1) + return 1; + + printf("Package: %s\n", file); + while((file=argv[++optind])) { + type = fpid_file(file); + printf("Adding %s: %s..\n", type, file); + ret = fiasco_add(fd, type, file, NULL); + if (ret<0) { + printf("Error\n"); + close(fd); + return 1; + } + } + fiasco_add_eof(fd); + printf("Done!\n"); + close(fd); + return 0; +} /* local code */ #if 0 -void my_callback(struct header_t *header) +void my_callback(int fd, struct header_t *header) { + fiasco_data_read(header); + //read(fd, buf, header->size); printf("Dumping %s\n", header->name); printf("DATA: %02x\n", header->data[0]); + fiasco_data_free(header); } int main(int argc, char **argv) @@ -73,28 +73,29 @@ 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(" -f <flags> set the given RD flags (see '-f help')\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, -L list supported usb device ids\n"); + 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(" -f <flags> set the given RD flags (see '-f help')\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, -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(" -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(" -P [new-fiasco] creates a new fiasco package, pieces as arguments\n"); + printf(" -r [0|1] disable/enable R&D mode\n"); + printf(" -R reboot the omap board\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) @@ -111,6 +112,7 @@ void unpack_callback(struct header_t *header) printf("Cannot open file.\n"); return; } + fiasco_data_read(header); fwrite(header->data, header->size, 1, fd); fclose(fd); } @@ -208,7 +210,7 @@ int main(int argc, char **argv) { int c; - while((c = getopt(argc, argv, "C:cp:vVhRu:ib:U:r:e:Lld:I:D:f:F:s:xH:")) != -1) { + while((c = getopt(argc, argv, "C:cp:PvVhRu:ib:U:r:e:Lld:I:D:f:F:s:xH:")) != -1) { switch(c) { case 'H': printf("xorpair: %04x\n", do_hash_file(optarg)); @@ -260,6 +262,8 @@ int main(int argc, char **argv) case 'p': add_piece(optarg); break; + case 'P': + return fiasco_pack(optind, argv); case 'L': case 'l': list_valid_devices(); @@ -305,7 +309,7 @@ int main(int argc, char **argv) { 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"); + printf(" [-s serial-dev] [-u fiasco-image] [-U 0|1] | [-P new-fiasco] [piece1] [2] ..\n"); return 1; } @@ -78,8 +78,15 @@ extern char *modes[]; extern char *root_devices[]; // fiasco +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); +int fiasco_add(int fd, const char *name, const char *file, const char *version); +int fiasco_pack(int optind, char *argv[]); struct header_t { + int fd; char fwname[128]; char name[128]; char version[128]; |