* Add documentation for the FIASCO format
* Initial fiasco writer api * Lot of new checks and code cleanup * fiasco callback now receives a filedescriptor inside header_t - the lseek and data free is autonatically done by the main loop * Split fiasco_data_read() code, useful for streams (embeddeds low mem footprint) * Add '-P' flag to create a new fiasco pack
This commit is contained in:
1 parent
85fdbfb354
commit
d15412a788
3 files changed
+156
-63
No files matched your search
+122
-40
@@ -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)
|
||||
|
||||
Reference in new issue
Block a user