2007-04-20 18:20:22 +02:00
|
|
|
/*
|
2007-06-30 03:58:18 +02:00
|
|
|
* 0xFFFF - Open Free Fiasco Firmware Flasher
|
2011-12-16 16:17:17 +01:00
|
|
|
* Copyright (C) 2007-2011 pancake <pancake@youterm.com>
|
2012-06-23 10:43:20 +02:00
|
|
|
* Copyright (C) 2011-2012 Pali Rohár <pali.rohar@gmail.com>
|
2007-04-20 18:20:22 +02:00
|
|
|
*
|
2007-06-30 03:58:18 +02:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
2007-04-20 18:20:22 +02:00
|
|
|
*
|
2007-06-30 03:58:18 +02:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
2007-04-20 18:20:22 +02:00
|
|
|
*
|
2007-06-30 03:58:18 +02:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2007-04-20 18:20:22 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <stdlib.h>
|
2007-10-08 00:25:56 +02:00
|
|
|
#include <fcntl.h>
|
2007-04-20 18:20:22 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <sys/stat.h>
|
2007-10-08 00:25:56 +02:00
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
#include "main.h"
|
2007-10-20 02:32:24 +02:00
|
|
|
#include "hash.h"
|
2007-10-11 03:14:11 +02:00
|
|
|
|
2008-12-08 00:21:05 +01:00
|
|
|
int (*fiasco_callback)(struct header_t *header) = NULL;
|
2007-04-20 18:20:22 +02:00
|
|
|
|
2009-12-21 14:40:12 +01:00
|
|
|
int openfiasco(const char *name, const char *piece_grep, int v)
|
2007-04-20 18:20:22 +02:00
|
|
|
{
|
2007-10-08 00:25:56 +02:00
|
|
|
struct header_t header;
|
2009-12-21 14:40:12 +01:00
|
|
|
unsigned char buf[256];
|
|
|
|
|
unsigned char data[256];
|
|
|
|
|
unsigned char *pdata, *pdataend;
|
2007-10-08 00:25:56 +02:00
|
|
|
unsigned int namelen;
|
2008-12-08 00:21:05 +01:00
|
|
|
off_t off, here;
|
2011-12-16 16:17:17 +01:00
|
|
|
int i;
|
2008-12-08 18:13:36 +01:00
|
|
|
|
2012-06-23 10:43:20 +02:00
|
|
|
memset(&header, 0, sizeof(header));
|
2007-10-11 03:14:11 +02:00
|
|
|
header.fd = open(name, O_RDONLY);
|
2007-04-20 18:20:22 +02:00
|
|
|
|
2007-10-11 03:14:11 +02:00
|
|
|
if (header.fd == -1) {
|
2007-10-08 00:25:56 +02:00
|
|
|
fprintf(stderr, "Cannot open %s\n", name);
|
2007-04-20 18:20:22 +02:00
|
|
|
return 1;
|
2007-10-08 00:25:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* read header */
|
2009-12-21 14:40:12 +01:00
|
|
|
if (read(header.fd, buf, 5) != 5) {
|
|
|
|
|
printf("Invalid read of 5 bytes\n");
|
|
|
|
|
return close(header.fd);
|
|
|
|
|
}
|
2007-10-08 00:25:56 +02:00
|
|
|
if (buf[0] != 0xb4) {
|
|
|
|
|
printf("Invalid header\n");
|
2007-10-11 03:14:11 +02:00
|
|
|
return close(header.fd);
|
2007-10-08 00:25:56 +02:00
|
|
|
}
|
|
|
|
|
memcpy(&namelen,buf+1,4);
|
|
|
|
|
namelen = ntohl(namelen);
|
|
|
|
|
if (namelen>128) {
|
|
|
|
|
printf("Stupid length at header. Is this a joke?\n");
|
2007-10-11 03:14:11 +02:00
|
|
|
return close(header.fd);
|
2007-10-08 00:25:56 +02:00
|
|
|
}
|
|
|
|
|
memset(buf,'\0', 128);
|
2009-12-21 14:40:12 +01:00
|
|
|
if (read(header.fd, buf, namelen) != namelen) {
|
|
|
|
|
printf("Invalid read of %d bytes\n", namelen);
|
|
|
|
|
return close(header.fd);
|
|
|
|
|
}
|
2008-12-08 18:13:36 +01:00
|
|
|
if (v) printf("Fiasco version: %2d\n", buf[3]);
|
2007-11-20 04:14:13 +01:00
|
|
|
strcpy(header.fwname, (char *)buf+6);
|
2009-12-21 14:40:12 +01:00
|
|
|
if (v) for(i=6;i<namelen;i+=strlen((char *)(buf+i))+1)
|
|
|
|
|
printf("Name: %s\n", buf+i);
|
2007-10-08 00:25:56 +02:00
|
|
|
|
|
|
|
|
/* walk the tree */
|
|
|
|
|
while(1) {
|
2008-12-08 00:21:05 +01:00
|
|
|
here = lseek(header.fd, 0, SEEK_CUR);
|
2011-12-16 16:17:17 +01:00
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
|
while(1) {
|
2012-06-23 10:43:20 +02:00
|
|
|
if (read(header.fd, buf, 7)<7) {
|
|
|
|
|
printf("Next valid header not found\n");
|
2011-12-16 16:17:17 +01:00
|
|
|
return close(header.fd);
|
2012-06-23 10:43:20 +02:00
|
|
|
}
|
|
|
|
|
if (buf[0] == 0x54 && buf[2] == 0x2E && buf[3] == 0x19 && buf[4] == 0x01 && buf[5] == 0x01 && buf[6] == 0x00)
|
2011-12-16 16:17:17 +01:00
|
|
|
break;
|
|
|
|
|
lseek(header.fd, -6, SEEK_CUR);
|
|
|
|
|
++i;
|
2007-10-08 00:25:56 +02:00
|
|
|
}
|
2011-12-16 16:17:17 +01:00
|
|
|
if (i && v) printf("Skipping %d padding bytes\n", i);
|
|
|
|
|
|
|
|
|
|
if (read(header.fd, buf+7, 2)<2)
|
|
|
|
|
break;
|
2007-10-08 00:25:56 +02:00
|
|
|
header.hash = buf[7]<<8|buf[8];
|
|
|
|
|
|
|
|
|
|
/* piece name */
|
|
|
|
|
memset(data, '\0', 13);
|
2007-10-11 03:14:11 +02:00
|
|
|
if (read(header.fd, data, 12)<12)
|
2007-10-08 00:25:56 +02:00
|
|
|
break;
|
2007-10-11 03:14:11 +02:00
|
|
|
|
2007-10-08 00:25:56 +02:00
|
|
|
if (data[0] == 0xff) {
|
|
|
|
|
printf(" [eof]\n");
|
|
|
|
|
break;
|
2008-12-08 18:13:36 +01:00
|
|
|
} else if (v) printf(" %s\n", data);
|
2012-06-23 10:43:20 +02:00
|
|
|
strcpy(header.type, (char *)data);
|
2007-10-08 00:25:56 +02:00
|
|
|
|
2011-12-16 16:17:17 +01:00
|
|
|
if (v) {
|
|
|
|
|
printf(" header: ");
|
|
|
|
|
for (i=0; i<7;++i) printf(" 0x%.2X", (unsigned int)buf[i]);
|
|
|
|
|
printf("\n");
|
|
|
|
|
}
|
|
|
|
|
|
2007-10-11 03:14:11 +02:00
|
|
|
if (read(header.fd, buf, 9)<9)
|
2012-06-23 10:43:20 +02:00
|
|
|
return close(header.fd);
|
2007-10-08 00:25:56 +02:00
|
|
|
memcpy(&header.size, buf,4);
|
|
|
|
|
header.size = ntohl(header.size);
|
2008-12-08 18:13:36 +01:00
|
|
|
if (v) {
|
|
|
|
|
printf(" offset: 0x%08x\n", (unsigned int)here);
|
|
|
|
|
printf(" size: %d bytes\n", header.size);
|
|
|
|
|
printf(" hash: %04x\n", header.hash);
|
|
|
|
|
}
|
2007-10-08 00:25:56 +02:00
|
|
|
//printf("BYTE: %02x %02x %02x %02x %02x\n",
|
|
|
|
|
// buf[4], buf[5], buf[6], buf[7], buf[8]);
|
|
|
|
|
/* XXX this is not ok */
|
|
|
|
|
//printf("BUF8: %02x\n", buf[8]);
|
2012-06-23 10:43:20 +02:00
|
|
|
memset(header.device, 0, sizeof(header.device));
|
2012-06-23 13:09:44 +02:00
|
|
|
memset(header.hwrevs, 0, sizeof(header.hwrevs));
|
2012-06-23 10:43:20 +02:00
|
|
|
memset(header.version, 0, sizeof(header.version));
|
|
|
|
|
if (header.layout) {
|
|
|
|
|
free(header.layout);
|
|
|
|
|
header.layout = NULL;
|
|
|
|
|
}
|
|
|
|
|
while ((buf[8] >= '1' && buf[8] <= '4') || buf[8] == '/') {
|
2007-10-11 03:14:11 +02:00
|
|
|
if (read(header.fd, data, 1)<1)
|
2012-06-23 10:43:20 +02:00
|
|
|
return close(header.fd);
|
2007-10-08 00:25:56 +02:00
|
|
|
i = data[0];
|
2007-10-11 03:14:11 +02:00
|
|
|
if (read(header.fd, data, i)<i)
|
2012-06-23 10:43:20 +02:00
|
|
|
return close(header.fd);
|
2008-12-08 00:21:05 +01:00
|
|
|
if (data[0]) {
|
2008-12-08 18:13:36 +01:00
|
|
|
if (v) {
|
2012-06-23 10:43:20 +02:00
|
|
|
printf(" subinfo\n");
|
|
|
|
|
printf(" type: ");
|
|
|
|
|
if (buf[8] == '1') printf("version string");
|
|
|
|
|
else if (buf[8] == '2') printf("hw revision");
|
|
|
|
|
else if (buf[8] == '3') printf("layout");
|
|
|
|
|
else printf("unknown (%c)", buf[8]);
|
|
|
|
|
printf("\n");
|
|
|
|
|
printf(" length: %d\n", i);
|
2008-12-08 18:13:36 +01:00
|
|
|
}
|
2008-12-08 00:21:05 +01:00
|
|
|
pdata = data;
|
2009-12-21 14:40:12 +01:00
|
|
|
pdataend = data+i;
|
|
|
|
|
while(pdata<pdataend) {
|
2012-06-23 13:09:44 +02:00
|
|
|
char buf2[9];
|
|
|
|
|
if (buf[8] == '2' && pdata != data) {
|
|
|
|
|
memset(buf2, 0, 9);
|
|
|
|
|
strncpy(buf2, (char *)pdata, 8);
|
|
|
|
|
}
|
2012-06-23 10:43:20 +02:00
|
|
|
if (v) {
|
|
|
|
|
printf(" ");
|
|
|
|
|
if (buf[8] == '1') printf("version");
|
|
|
|
|
else if (buf[8] == '2' && pdata == data) printf("device");
|
|
|
|
|
else if (buf[8] == '2' && pdata != data) printf("hw revision");
|
|
|
|
|
else if (buf[8] == '3') printf("layout");
|
|
|
|
|
else printf("data");
|
2012-06-23 13:09:44 +02:00
|
|
|
if (buf[8] == '2' && pdata != data)
|
|
|
|
|
printf(": %s\n", buf2);
|
|
|
|
|
else if (buf[8] != '3' && buf[8] != '/')
|
2012-06-23 10:43:20 +02:00
|
|
|
printf(": %s\n", pdata);
|
|
|
|
|
else
|
|
|
|
|
printf(": (not printing)\n");
|
|
|
|
|
}
|
|
|
|
|
if (buf[8] == '1') {
|
|
|
|
|
strcpy(header.version, (char *)pdata);
|
|
|
|
|
} else if (buf[8] == '2' && pdata == data) {
|
|
|
|
|
strcpy(header.device, (char *)pdata);
|
2012-06-23 13:09:44 +02:00
|
|
|
} else if (buf[8] == '2' && pdata != data) {
|
|
|
|
|
if (header.hwrevs[0] == 0)
|
|
|
|
|
strcpy(header.hwrevs, buf2);
|
2012-06-23 10:43:20 +02:00
|
|
|
else {
|
2012-06-23 13:09:44 +02:00
|
|
|
strcat(header.hwrevs, ",");
|
|
|
|
|
strcat(header.hwrevs, buf2);
|
2012-06-23 10:43:20 +02:00
|
|
|
}
|
|
|
|
|
} else if (buf[8] == '3') {
|
|
|
|
|
if (header.layout) free(header.layout);
|
|
|
|
|
header.layout = malloc(strlen((char*)pdata)+1);
|
|
|
|
|
if (header.layout) strcpy(header.layout, (char *)pdata);
|
|
|
|
|
}
|
2012-06-23 13:09:44 +02:00
|
|
|
if (buf[8] == '2' && pdata != data && strlen((char *)pdata) > 8)
|
|
|
|
|
pdata += 8;
|
|
|
|
|
else
|
|
|
|
|
pdata += strlen((char*)pdata)+1;
|
2009-12-21 14:40:12 +01:00
|
|
|
for(;*pdata=='\0' && pdata<pdataend; pdata++);
|
2008-12-08 00:21:05 +01:00
|
|
|
}
|
2011-12-16 16:17:17 +01:00
|
|
|
} else {
|
|
|
|
|
/* End of comments, ignore one char and next is image data */
|
|
|
|
|
lseek(header.fd, 1, SEEK_CUR);
|
|
|
|
|
break;
|
2008-12-08 00:21:05 +01:00
|
|
|
}
|
2007-10-11 03:14:11 +02:00
|
|
|
if (read(header.fd, buf+8, 1)<1)
|
2012-06-23 10:43:20 +02:00
|
|
|
return close(header.fd);
|
|
|
|
|
}
|
|
|
|
|
strcpy(header.name, header.type);
|
|
|
|
|
if (header.device[0]) {
|
|
|
|
|
strcat(header.name, "-");
|
|
|
|
|
strcat(header.name, header.device);
|
|
|
|
|
}
|
2012-06-23 13:09:44 +02:00
|
|
|
if (header.hwrevs[0]) {
|
2012-06-23 10:43:20 +02:00
|
|
|
strcat(header.name, "-");
|
2012-06-23 13:09:44 +02:00
|
|
|
strcat(header.name, header.hwrevs);
|
2012-06-23 10:43:20 +02:00
|
|
|
}
|
|
|
|
|
if (header.version[0]) {
|
|
|
|
|
strcat(header.name, "-");
|
|
|
|
|
strcat(header.name, header.version);
|
2007-10-08 00:25:56 +02:00
|
|
|
}
|
|
|
|
|
/* callback */
|
2007-10-11 03:14:11 +02:00
|
|
|
off = lseek(header.fd, 0, SEEK_CUR);
|
2008-12-08 18:13:36 +01:00
|
|
|
if (v) {
|
2012-06-23 10:43:20 +02:00
|
|
|
printf(" version: %s\n", header.version);
|
|
|
|
|
printf(" device: %s\n", header.device);
|
2012-06-23 13:09:44 +02:00
|
|
|
printf(" hwrevs: %s\n", header.hwrevs);
|
2008-12-08 18:13:36 +01:00
|
|
|
printf(" name: %s\n", header.name);
|
|
|
|
|
printf(" body-at: 0x%08x\n", (unsigned int)off);
|
|
|
|
|
}
|
|
|
|
|
if (piece_grep==NULL || (strstr(header.name, piece_grep))) {
|
2011-12-16 16:17:17 +01:00
|
|
|
if (piece_grep)
|
|
|
|
|
printf("==> (%s) %s\n", piece_grep, header.name);
|
|
|
|
|
else
|
|
|
|
|
printf("==> %s\n", header.name);
|
2008-12-08 18:13:36 +01:00
|
|
|
if (fiasco_callback != NULL) {
|
|
|
|
|
fiasco_callback(&header);
|
2012-06-23 10:43:20 +02:00
|
|
|
if (header.layout) {
|
|
|
|
|
free(header.layout);
|
|
|
|
|
header.layout = NULL;
|
|
|
|
|
}
|
2008-12-08 18:13:36 +01:00
|
|
|
free(header.data);
|
|
|
|
|
continue;
|
|
|
|
|
} else {
|
|
|
|
|
// ??huh
|
|
|
|
|
}
|
2007-10-08 00:25:56 +02:00
|
|
|
}
|
2007-10-11 03:14:11 +02:00
|
|
|
// XXX dup
|
|
|
|
|
lseek(header.fd, off, SEEK_SET);
|
|
|
|
|
lseek(header.fd, header.size, SEEK_CUR);
|
2012-06-23 10:43:20 +02:00
|
|
|
if (header.layout) {
|
|
|
|
|
free(header.layout);
|
|
|
|
|
header.layout = NULL;
|
|
|
|
|
}
|
2007-10-08 00:25:56 +02:00
|
|
|
}
|
2008-12-08 18:13:36 +01:00
|
|
|
return close(header.fd);
|
2007-04-20 18:20:22 +02:00
|
|
|
}
|
|
|
|
|
|
2007-10-11 03:14:11 +02:00
|
|
|
void fiasco_data_read(struct header_t *header)
|
|
|
|
|
{
|
2007-11-20 04:14:13 +01:00
|
|
|
header->data = (unsigned char *)malloc(header->size);
|
2007-10-11 03:14:11 +02:00
|
|
|
if (header->data == NULL) {
|
|
|
|
|
printf("Cannot alloc %d bytes\n", header->size);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2009-12-21 14:40:12 +01:00
|
|
|
if (read (header->fd, header->data, header->size) != header->size) {
|
|
|
|
|
printf("Cannot read %d bytes\n", header->size);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2007-10-11 03:14:11 +02:00
|
|
|
}
|
2007-04-20 18:20:22 +02:00
|
|
|
|
2007-10-11 03:14:11 +02:00
|
|
|
/* fiasco writer */
|
2007-10-08 00:25:56 +02:00
|
|
|
int fiasco_new(const char *filename, const char *name)
|
2007-04-20 18:20:22 +02:00
|
|
|
{
|
|
|
|
|
int fd;
|
2007-10-20 02:32:24 +02:00
|
|
|
//int len = htonl(strlen(name)+1+6+14);
|
|
|
|
|
|
2007-10-11 03:14:11 +02:00
|
|
|
fd = open(filename, O_RDWR|O_CREAT, 0644);
|
2007-10-08 00:25:56 +02:00
|
|
|
if (fd == -1)
|
|
|
|
|
return -1;
|
2007-10-20 02:32:24 +02:00
|
|
|
#if 1
|
2009-12-21 14:40:12 +01:00
|
|
|
if (write(fd, "\xb4\x00\x00\x00\x14\x00\x00\x00\x01\xe8\x0e", 11) != 11) {
|
|
|
|
|
fprintf (stderr, "Cannot write 11 bytes to target file\n");
|
|
|
|
|
close (fd);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
if (write(fd, "OSSO UART+USB", 14) != 14) {
|
|
|
|
|
fprintf (stderr, "Cannot write 14 bytes to target file\n");
|
|
|
|
|
close (fd);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2007-10-20 02:32:24 +02:00
|
|
|
#else
|
|
|
|
|
/* 2nd format doesnt works. atm stay with old one */
|
2007-10-08 00:25:56 +02:00
|
|
|
write(fd, "\xb4", 1);
|
|
|
|
|
write(fd, &len, 4);
|
|
|
|
|
/* version header */
|
|
|
|
|
write(fd, "\x00\x00\x00\x02\xe8\x0e", 6);
|
2007-10-20 02:32:24 +02:00
|
|
|
/* firmware type */
|
|
|
|
|
write(fd, "OSSO UART+USB", 14);
|
2007-10-11 03:14:11 +02:00
|
|
|
/* firmware name */
|
2007-10-20 02:32:24 +02:00
|
|
|
write(fd, name, strlen(name));
|
|
|
|
|
write(fd, "", 1);
|
|
|
|
|
#endif
|
2007-10-08 00:25:56 +02:00
|
|
|
return fd;
|
|
|
|
|
}
|
2007-04-20 18:20:22 +02:00
|
|
|
|
2007-10-08 00:25:56 +02:00
|
|
|
int fiasco_add_eof(int fd)
|
|
|
|
|
{
|
2007-10-20 02:32:24 +02:00
|
|
|
#if 0
|
2007-10-11 03:14:11 +02:00
|
|
|
unsigned char buf[120];
|
2007-10-08 00:25:56 +02:00
|
|
|
if (fd == -1)
|
|
|
|
|
return -1;
|
2007-10-11 03:14:11 +02:00
|
|
|
memset(buf,'\xff', 120);
|
|
|
|
|
write(fd, buf, 120);
|
2007-10-20 02:32:24 +02:00
|
|
|
#endif
|
2007-10-08 00:25:56 +02:00
|
|
|
return 0;
|
|
|
|
|
}
|
2007-04-20 18:20:22 +02:00
|
|
|
|
2007-10-08 00:25:56 +02:00
|
|
|
int fiasco_add(int fd, const char *name, const char *file, const char *version)
|
|
|
|
|
{
|
2007-10-11 03:14:11 +02:00
|
|
|
int gd,ret;
|
2007-10-08 00:25:56 +02:00
|
|
|
unsigned int sz;
|
|
|
|
|
unsigned char len;
|
|
|
|
|
unsigned short hash;
|
2007-10-20 02:32:24 +02:00
|
|
|
unsigned char *ptr = (unsigned char *)&hash;
|
2007-10-11 03:14:11 +02:00
|
|
|
char buf[4096];
|
|
|
|
|
char bname[32];
|
2007-10-08 00:25:56 +02:00
|
|
|
|
|
|
|
|
if (fd == -1)
|
|
|
|
|
return -1;
|
2007-10-11 03:14:11 +02:00
|
|
|
if (file == NULL)
|
|
|
|
|
return -1;
|
2007-10-08 00:25:56 +02:00
|
|
|
gd = open(file, O_RDONLY);
|
|
|
|
|
if (gd == -1)
|
2007-04-20 18:20:22 +02:00
|
|
|
return -1;
|
2007-10-11 03:14:11 +02:00
|
|
|
|
2007-10-08 00:25:56 +02:00
|
|
|
sz = htonl((unsigned int) lseek(gd, 0, SEEK_END));
|
2007-10-11 03:14:11 +02:00
|
|
|
lseek(gd, 0, SEEK_SET);
|
2007-10-08 00:25:56 +02:00
|
|
|
// 4 bytes big endian
|
2007-10-20 02:32:24 +02:00
|
|
|
//write(fd, "T\x02\x2e\x19\x01\x01\x00", 7); // header?
|
2009-12-21 14:40:12 +01:00
|
|
|
// header (old piece format)
|
|
|
|
|
if (write (fd, "T\x01\x2e\x19\x01\x01\x00", 7) != 7) {
|
|
|
|
|
fprintf (stderr, "Cannot write 7 bytes\n");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2007-10-08 00:25:56 +02:00
|
|
|
/* checksum */
|
|
|
|
|
hash = do_hash_file(file);
|
|
|
|
|
ptr[0]^=ptr[1]; ptr[1]=ptr[0]^ptr[1]; ptr[0]^=ptr[1];
|
2007-10-11 03:14:11 +02:00
|
|
|
write(fd, &hash, 2);
|
|
|
|
|
printf("hash: %04x\n", hash);
|
2007-10-08 00:25:56 +02:00
|
|
|
|
|
|
|
|
memset(bname, '\0', 13);
|
2007-10-11 03:14:11 +02:00
|
|
|
if (name == NULL)
|
2007-10-20 02:32:24 +02:00
|
|
|
name = fpid_file(file);
|
|
|
|
|
/* failback */
|
|
|
|
|
if (name == NULL)
|
|
|
|
|
name = file;
|
|
|
|
|
|
|
|
|
|
strncpy(bname, name, 12);
|
2007-10-11 03:14:11 +02:00
|
|
|
write(fd, bname, 12);
|
2007-10-08 00:25:56 +02:00
|
|
|
write(fd, &sz, 4);
|
|
|
|
|
if (version) {
|
|
|
|
|
/* append metadata */
|
|
|
|
|
write(fd, "\x00\x00\x00\x00\x31", 5);
|
|
|
|
|
len = strlen(version);
|
|
|
|
|
write(fd, &len, 1);
|
|
|
|
|
write(fd, version, len);
|
|
|
|
|
write(fd, "\x00\x9b", 2);
|
|
|
|
|
} else {
|
|
|
|
|
write(fd, "\x00\x00\x00\x00\x9b", 5);
|
2007-04-20 18:20:22 +02:00
|
|
|
}
|
2007-10-11 03:14:11 +02:00
|
|
|
|
|
|
|
|
while(1) {
|
|
|
|
|
ret = read(gd, buf, 4096);
|
|
|
|
|
if (ret<1)
|
|
|
|
|
break;
|
2009-12-21 14:40:12 +01:00
|
|
|
if (write(fd, buf, ret) != ret) {
|
|
|
|
|
fprintf (stderr, "Cannot write %d bytes\n", ret);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2007-10-11 03:14:11 +02:00
|
|
|
}
|
|
|
|
|
|
2007-10-08 00:25:56 +02:00
|
|
|
return 0;
|
|
|
|
|
}
|
2007-04-20 18:20:22 +02:00
|
|
|
|
2007-10-11 03:14:11 +02:00
|
|
|
int fiasco_pack(int optind, char *argv[])
|
|
|
|
|
{
|
2007-10-20 02:32:24 +02:00
|
|
|
const char *file = argv[optind];
|
2007-10-11 03:14:11 +02:00
|
|
|
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])) {
|
2007-10-20 02:32:24 +02:00
|
|
|
type = (char *)fpid_file(file);
|
2007-10-11 03:14:11 +02:00
|
|
|
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;
|
|
|
|
|
}
|
2007-04-20 18:20:22 +02:00
|
|
|
|
2007-10-08 00:25:56 +02:00
|
|
|
/* local code */
|
|
|
|
|
#if 0
|
2007-10-11 03:14:11 +02:00
|
|
|
void my_callback(int fd, struct header_t *header)
|
2007-10-08 00:25:56 +02:00
|
|
|
{
|
2007-10-11 03:14:11 +02:00
|
|
|
fiasco_data_read(header);
|
|
|
|
|
//read(fd, buf, header->size);
|
2007-10-08 00:25:56 +02:00
|
|
|
printf("Dumping %s\n", header->name);
|
|
|
|
|
printf("DATA: %02x\n", header->data[0]);
|
2007-10-11 03:14:11 +02:00
|
|
|
fiasco_data_free(header);
|
2007-10-08 00:25:56 +02:00
|
|
|
}
|
2007-04-20 18:20:22 +02:00
|
|
|
|
2007-10-08 00:25:56 +02:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
|
{
|
|
|
|
|
if (argc!=2) {
|
|
|
|
|
printf("Usage: unfiasco [file]\n");
|
|
|
|
|
return 1;
|
2007-04-20 18:20:22 +02:00
|
|
|
}
|
|
|
|
|
|
2007-10-08 00:25:56 +02:00
|
|
|
/*
|
|
|
|
|
fd = fiasco_new("myfiasco", "pancake-edition");
|
|
|
|
|
fiasco_add(fd, "kernel", "zImage", "2.6.22");
|
2007-04-20 18:20:22 +02:00
|
|
|
close(fd);
|
2007-10-08 00:25:56 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
// fiasco_callback = &my_callback;
|
|
|
|
|
|
|
|
|
|
return openfiasco(argv[1]);
|
2007-04-20 18:20:22 +02:00
|
|
|
}
|
2007-10-08 00:25:56 +02:00
|
|
|
#endif
|