From: Michael Gernoth Date: Sun, 1 Feb 2009 22:26:27 +0000 (+0100) Subject: rename extract.{c,h} to filesystem.{c,h} X-Git-Url: http://git.zerfleddert.de/cgi-bin/gitweb.cgi/rsbs2/commitdiff_plain/e8563c43527e7f0fcb338069f39d74d252340d28 rename extract.{c,h} to filesystem.{c,h} --- diff --git a/Makefile b/Makefile index 25c0efe..15de0a8 100644 --- a/Makefile +++ b/Makefile @@ -2,17 +2,17 @@ CFLAGS=-Wall -O3 all: firmware -firmware: firmware.o rsb-crc.o rsb-lz.o extract.o +firmware: firmware.o rsb-crc.o rsb-lz.o filesystem.o -firmware.o: firmware.c rsb-crc.h extract.h +firmware.o: firmware.c rsb-crc.h filesystem.h rsb-crc.o: rsb-crc.c rsb-crc.h rsb-lz.o: rsb-lz.c rsb-lz.h -extract.o: extract.c extract.h rsb-lz.h +filesystem.o: filesystem.c filesystem.h rsb-lz.h clean: - rm -f firmware firmware.o rsb-crc.o rsb-lz.o extract.o + rm -f firmware firmware.o rsb-crc.o rsb-lz.o filesystem.o .PHONY: all clean diff --git a/extract.c b/extract.c deleted file mode 100644 index b420899..0000000 --- a/extract.c +++ /dev/null @@ -1,198 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "rsb-lz.h" -#include "extract.h" - -struct file_entry* get_next_file(unsigned char *fw, int len) -{ - static unsigned char *pos; - static unsigned char *start; - static unsigned char *end; - static struct file_entry fent; - int name_length; - - if (fw != NULL && len != 0) { - pos = fw + 0x28; - -#if 0 - printf("Start of filesystem: 0x%08x\n", *((unsigned int*)pos)); -#endif - start = fw; - pos = fw + *((unsigned int*)pos); - end = fw + len; - } - - fent.unknown = *pos; - pos++; - - if (fent.unknown == 0x00) { - /* EOF */ - int fill = (4 - ((pos - start) % 4)) % 4; - int i; - - for (i = 0; i < fill; i++) { - if (*pos != 0xff) { - fprintf(stderr, "Wrong fill byte after EOF: 0x%02x, aborting!\n", *pos); - exit(1); - } - pos++; - } - - if (pos != end) { - fprintf(stderr, "EOF marker not at end-of-file %p <-> %p, aborting!\n", pos, end); - exit(1); - } - - return NULL; - } - - - name_length = *((unsigned int*)pos); - pos += 4; - - fent.length = *((unsigned int*)pos); - pos += 4; - - if ((fent.length > (end - pos)) || - (name_length > (end - pos))) { - printf("EOF reached without hitting EOF marker, aborting " - "(unknown: 0x%02x, namelen: 0x%08x, contentlen: 0x%08x!\n", - fent.unknown, name_length, fent.length); - exit(1); - } - - fent.name = (char*)pos; - pos += name_length; - - fent.start = pos; - pos += fent.length; - - return &fent; -} - -void extract_files(unsigned char *fw, int len) -{ - struct file_entry *fent; - - fent = get_next_file(fw, len); - - while (fent) { - printf("%s: unknown: 0x%02x, length: %d", - fent->name, fent->unknown, fent->length); - - if (fent->length > 0) { - write_file(fent->name, fent->start, fent->length); - if (*((unsigned int*)fent->start) == LZ_MAGIC) { - char *lzname; - - if ((lzname = strdup(fent->name)) == NULL) { - perror("strdup"); - exit(1); - } - - if (!strcmp(lzname + strlen(lzname) - 4 , ".lz")) { - fprintf(stderr, "Ugh, can't derive filename...\n"); - exit(1); - } - lzname[strlen(lzname) - 3] = 0x00; - - printf("%s: packed file found", lzname); - - extract_lz_file(fent->start, (unsigned char*)lzname); - free(lzname); - } else if (!strcmp(fent->name, "firmware")) { - unsigned char *lzpos; - char lzname[128]; - - bzero(lzname, sizeof(lzname)); - strcpy(lzname, "firmware."); - - lzpos = fent->start + *((unsigned int*)(fent->start + 0x20)); - memcpy(lzname + strlen(lzname), lzpos - 4, 4); - lzpos += 4; - if (*((unsigned int*)(lzpos)) == LZ_MAGIC) { - printf("%s: compressed firmware part found", lzname); - extract_lz_file(lzpos, (unsigned char*)lzname); - } - } - } else { - printf(", ignoring...\n"); - } - fent = get_next_file(NULL, 0); - } -} - -void mkdir_p(char *dir) -{ - char *copy, *parent; - - if ((dir == NULL) || (!strcmp(dir, "."))) - return; - - if ((copy = strdup(dir)) == NULL) { - perror("strdup"); - exit(1); - } - parent = dirname(copy); - mkdir_p(parent); - - errno = 0; - if (mkdir(dir, 0755) == -1) { - if (errno != EEXIST) { - fprintf(stderr, "%s: ", dir); - perror("mkdir"); - exit(1); - } - } - free(copy); -} - -void write_file(char *fname, unsigned char *buf, int len) -{ - char filename[PATH_MAX]; - char *filename_c, *dirn; - int fd; - int remaining; - int ret; - - strcpy(filename, "extracted/"); - strcat(filename, fname); - - if ((filename_c = strdup(filename)) == NULL) { - perror("strdup"); - exit(1); - } - dirn = dirname(filename_c); - mkdir_p(dirn); - free(filename_c); - - if ((fd = open(filename, O_WRONLY|O_CREAT, 0644)) == -1) { - fprintf(stderr, "%s: ", filename); - perror("open"); - exit(1); - } - - remaining = len; - - while(remaining) { - ret = write(fd, buf + (len - remaining), remaining); - if (ret < 0) { - perror("write"); - exit(1); - } - remaining -= ret; - } - - printf(", %s written.\n", filename); - - close(fd); -} diff --git a/extract.h b/extract.h deleted file mode 100644 index ee35618..0000000 --- a/extract.h +++ /dev/null @@ -1,10 +0,0 @@ -struct file_entry { - char *name; - unsigned char *start; - int length; - unsigned char unknown; -}; - -struct file_entry* get_next_file(unsigned char *fw, int len); -void extract_files(unsigned char *fw, int len); -void write_file(char *fname, unsigned char *buf, int len); diff --git a/filesystem.c b/filesystem.c new file mode 100644 index 0000000..2d71946 --- /dev/null +++ b/filesystem.c @@ -0,0 +1,198 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "rsb-lz.h" +#include "filesystem.h" + +struct file_entry* get_next_file(unsigned char *fw, int len) +{ + static unsigned char *pos; + static unsigned char *start; + static unsigned char *end; + static struct file_entry fent; + int name_length; + + if (fw != NULL && len != 0) { + pos = fw + 0x28; + +#if 0 + printf("Start of filesystem: 0x%08x\n", *((unsigned int*)pos)); +#endif + start = fw; + pos = fw + *((unsigned int*)pos); + end = fw + len; + } + + fent.unknown = *pos; + pos++; + + if (fent.unknown == 0x00) { + /* EOF */ + int fill = (4 - ((pos - start) % 4)) % 4; + int i; + + for (i = 0; i < fill; i++) { + if (*pos != 0xff) { + fprintf(stderr, "Wrong fill byte after EOF: 0x%02x, aborting!\n", *pos); + exit(1); + } + pos++; + } + + if (pos != end) { + fprintf(stderr, "EOF marker not at end-of-file %p <-> %p, aborting!\n", pos, end); + exit(1); + } + + return NULL; + } + + + name_length = *((unsigned int*)pos); + pos += 4; + + fent.length = *((unsigned int*)pos); + pos += 4; + + if ((fent.length > (end - pos)) || + (name_length > (end - pos))) { + printf("EOF reached without hitting EOF marker, aborting " + "(unknown: 0x%02x, namelen: 0x%08x, contentlen: 0x%08x!\n", + fent.unknown, name_length, fent.length); + exit(1); + } + + fent.name = (char*)pos; + pos += name_length; + + fent.start = pos; + pos += fent.length; + + return &fent; +} + +void extract_files(unsigned char *fw, int len) +{ + struct file_entry *fent; + + fent = get_next_file(fw, len); + + while (fent) { + printf("%s: unknown: 0x%02x, length: %d", + fent->name, fent->unknown, fent->length); + + if (fent->length > 0) { + write_file(fent->name, fent->start, fent->length); + if (*((unsigned int*)fent->start) == LZ_MAGIC) { + char *lzname; + + if ((lzname = strdup(fent->name)) == NULL) { + perror("strdup"); + exit(1); + } + + if (!strcmp(lzname + strlen(lzname) - 4 , ".lz")) { + fprintf(stderr, "Ugh, can't derive filename...\n"); + exit(1); + } + lzname[strlen(lzname) - 3] = 0x00; + + printf("%s: packed file found", lzname); + + extract_lz_file(fent->start, (unsigned char*)lzname); + free(lzname); + } else if (!strcmp(fent->name, "firmware")) { + unsigned char *lzpos; + char lzname[128]; + + bzero(lzname, sizeof(lzname)); + strcpy(lzname, "firmware."); + + lzpos = fent->start + *((unsigned int*)(fent->start + 0x20)); + memcpy(lzname + strlen(lzname), lzpos - 4, 4); + lzpos += 4; + if (*((unsigned int*)(lzpos)) == LZ_MAGIC) { + printf("%s: compressed firmware part found", lzname); + extract_lz_file(lzpos, (unsigned char*)lzname); + } + } + } else { + printf(", ignoring...\n"); + } + fent = get_next_file(NULL, 0); + } +} + +void mkdir_p(char *dir) +{ + char *copy, *parent; + + if ((dir == NULL) || (!strcmp(dir, "."))) + return; + + if ((copy = strdup(dir)) == NULL) { + perror("strdup"); + exit(1); + } + parent = dirname(copy); + mkdir_p(parent); + + errno = 0; + if (mkdir(dir, 0755) == -1) { + if (errno != EEXIST) { + fprintf(stderr, "%s: ", dir); + perror("mkdir"); + exit(1); + } + } + free(copy); +} + +void write_file(char *fname, unsigned char *buf, int len) +{ + char filename[PATH_MAX]; + char *filename_c, *dirn; + int fd; + int remaining; + int ret; + + strcpy(filename, "extracted/"); + strcat(filename, fname); + + if ((filename_c = strdup(filename)) == NULL) { + perror("strdup"); + exit(1); + } + dirn = dirname(filename_c); + mkdir_p(dirn); + free(filename_c); + + if ((fd = open(filename, O_WRONLY|O_CREAT, 0644)) == -1) { + fprintf(stderr, "%s: ", filename); + perror("open"); + exit(1); + } + + remaining = len; + + while(remaining) { + ret = write(fd, buf + (len - remaining), remaining); + if (ret < 0) { + perror("write"); + exit(1); + } + remaining -= ret; + } + + printf(", %s written.\n", filename); + + close(fd); +} diff --git a/filesystem.h b/filesystem.h new file mode 100644 index 0000000..ee35618 --- /dev/null +++ b/filesystem.h @@ -0,0 +1,10 @@ +struct file_entry { + char *name; + unsigned char *start; + int length; + unsigned char unknown; +}; + +struct file_entry* get_next_file(unsigned char *fw, int len); +void extract_files(unsigned char *fw, int len); +void write_file(char *fname, unsigned char *buf, int len); diff --git a/firmware.c b/firmware.c index e4401da..9f599f3 100644 --- a/firmware.c +++ b/firmware.c @@ -8,7 +8,7 @@ #include #include #include "rsb-crc.h" -#include "extract.h" +#include "filesystem.h" #define FINDSTR(addr, str) (!strncmp((char*)addr, str, strlen(str))) diff --git a/rsb-lz.c b/rsb-lz.c index 3e6e638..c68ac8d 100644 --- a/rsb-lz.c +++ b/rsb-lz.c @@ -6,7 +6,7 @@ #include #include "rsb-crc.h" #include "rsb-lz.h" -#include "extract.h" +#include "filesystem.h" /* TODO: IMPLEMET THIS! */ /* Probably very broken lzw implementation by Agilent: