]> git.zerfleddert.de Git - rsbs2/blobdiff - src/filesystem.c
firmware: fix firmware extraction
[rsbs2] / src / filesystem.c
index ef26de4fb3c30d5fde0c8efab5cc1eca23333f35..f0465a8c621d98ea11bc90ed8d4b47784cff1bfa 100644 (file)
@@ -4,6 +4,7 @@
 #include <fcntl.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <fcntl.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <stdint.h>
 #include <strings.h>
 #include <string.h>
 #include <unistd.h>
 #include <strings.h>
 #include <string.h>
 #include <unistd.h>
 #include "rsb-lz.h"
 #include "filesystem.h"
 
 #include "rsb-lz.h"
 #include "filesystem.h"
 
-struct file_entry* get_next_file(unsigned char *fw, int len)
+struct file_entry* get_next_file(uint8_t *fw, int32_t len)
 {
 {
-       static unsigned char *pos;
-       static unsigned char *start;
-       static unsigned char *end;
+       static uint8_t *pos;
+       static uint8_t *start;
+       static uint8_t *end;
        static struct file_entry fent;
        static struct file_entry fent;
-       int name_length;
+       int32_t name_length;
 
        if (fw != NULL && len != 0) {
                pos = fw + 0x28;
 
 #if 0
 
        if (fw != NULL && len != 0) {
                pos = fw + 0x28;
 
 #if 0
-               printf("Start of filesystem: 0x%08x\n", *((unsigned int*)pos));
+               printf("Start of filesystem: 0x%08x\n", *((uint32_t*)pos));
 #endif
                start = fw;
 #endif
                start = fw;
-               pos = fw + *((unsigned int*)pos);
+               pos = fw + *((uint32_t*)pos);
                end = fw + len;
        }
 
                end = fw + len;
        }
 
@@ -36,8 +37,8 @@ struct file_entry* get_next_file(unsigned char *fw, int len)
 
        if (fent.unknown == 0x00) {
                /* EOF */
 
        if (fent.unknown == 0x00) {
                /* EOF */
-               int fill = (4 - ((pos - start) % 4)) % 4;
-               int i;
+               int32_t fill = (4 - ((pos - start) % 4)) % 4;
+               int32_t i;
 
                for (i = 0; i < fill; i++) {
                        if (*pos != 0xff) {
 
                for (i = 0; i < fill; i++) {
                        if (*pos != 0xff) {
@@ -56,10 +57,10 @@ struct file_entry* get_next_file(unsigned char *fw, int len)
        }
 
 
        }
 
 
-       name_length = *((unsigned int*)pos);
+       name_length = *((uint32_t*)pos);
        pos += 4;
 
        pos += 4;
 
-       fent.length = *((unsigned int*)pos);
+       fent.length = *((uint32_t*)pos);
        pos += 4;
 
        if ((fent.length > (end - pos)) ||
        pos += 4;
 
        if ((fent.length > (end - pos)) ||
@@ -79,7 +80,7 @@ struct file_entry* get_next_file(unsigned char *fw, int len)
        return &fent;
 }
 
        return &fent;
 }
 
-void extract_files(unsigned char *fw, int len)
+void extract_files(uint8_t *fw, int32_t len)
 {
        struct file_entry *fent;
 
 {
        struct file_entry *fent;
 
@@ -91,10 +92,10 @@ void extract_files(unsigned char *fw, int len)
 
                if (fent->length > 0) {
                        write_file(extracted_file(fent->name), fent->start, fent->length);
 
                if (fent->length > 0) {
                        write_file(extracted_file(fent->name), fent->start, fent->length);
-                       if (*((unsigned int*)fent->start) == LZ_MAGIC) {
+                       if (*((uint32_t*)fent->start) == LZ_MAGIC) {
                                char *lzname;
                                char *lzname;
-                               unsigned char *outbuf;
-                               unsigned int outlen;
+                               uint8_t *outbuf;
+                               uint32_t outlen;
 
                                if ((lzname = strdup(fent->name)) == NULL) {
                                        perror("strdup");
 
                                if ((lzname = strdup(fent->name)) == NULL) {
                                        perror("strdup");
@@ -115,18 +116,18 @@ void extract_files(unsigned char *fw, int len)
                                free(outbuf);
                                free(lzname);
                        } else if (!strcmp(fent->name, "firmware")) {
                                free(outbuf);
                                free(lzname);
                        } else if (!strcmp(fent->name, "firmware")) {
-                               unsigned char *lzpos;
+                               uint8_t *lzpos;
                                char lzname[128];
 
                                bzero(lzname, sizeof(lzname));
                                strcpy(lzname, "firmware.");
 
                                char lzname[128];
 
                                bzero(lzname, sizeof(lzname));
                                strcpy(lzname, "firmware.");
 
-                               lzpos = fent->start + *((unsigned int*)(fent->start + 0x20));
+                               lzpos = fent->start + *((uint32_t*)(fent->start + 0x20));
                                memcpy(lzname + strlen(lzname), lzpos - 4, 4);
                                lzpos += 4;
                                memcpy(lzname + strlen(lzname), lzpos - 4, 4);
                                lzpos += 4;
-                               if (*((unsigned int*)(lzpos)) == LZ_MAGIC) {
-                                       unsigned char *outbuf;
-                                       unsigned int outlen;
+                               if (*((uint32_t*)(lzpos)) == LZ_MAGIC) {
+                                       uint8_t *outbuf;
+                                       uint32_t outlen;
 
                                        printf("%s: compressed firmware part found", lzname);
                                        outbuf = extract_lz_file(lzpos, &outlen, 1);
 
                                        printf("%s: compressed firmware part found", lzname);
                                        outbuf = extract_lz_file(lzpos, &outlen, 1);
@@ -143,13 +144,13 @@ void extract_files(unsigned char *fw, int len)
        }
 }
 
        }
 }
 
-void replace_add_file(unsigned char *fw, int len, char *fwname, char *lname)
+void replace_add_file(uint8_t *fw, int32_t len, char *fwname, char *lname)
 {
        fprintf(stderr, "%s: Implement me!\n", __func__);
        exit(1);
 }
 
 {
        fprintf(stderr, "%s: Implement me!\n", __func__);
        exit(1);
 }
 
-void list_files(unsigned char *fw, int len)
+void list_files(uint8_t *fw, int32_t len)
 {
        struct file_entry *fent;
 
 {
        struct file_entry *fent;
 
@@ -162,11 +163,19 @@ void list_files(unsigned char *fw, int len)
 void mkdir_p(char *dir)
 {
        char *parent;
 void mkdir_p(char *dir)
 {
        char *parent;
+       char *tmpdir;
 
        if ((dir == NULL) || (!strcmp(dir, ".")))
                return;
 
 
        if ((dir == NULL) || (!strcmp(dir, ".")))
                return;
 
-       parent = strdup(dirname(dir));
+       tmpdir = strdup(dir);
+       if (tmpdir == NULL) {
+               perror("strdup");
+               exit(1);
+       }
+
+       parent = strdup(dirname(tmpdir));
+       free(tmpdir);
        if (parent == NULL) {
                perror("strdup");
                exit(1);
        if (parent == NULL) {
                perror("strdup");
                exit(1);
@@ -185,12 +194,12 @@ void mkdir_p(char *dir)
        free(parent);
 }
 
        free(parent);
 }
 
-void write_file(char *fname, unsigned char *buf, int len)
+void write_file(char *fname, uint8_t *buf, int32_t len)
 {
        char *filename_c, *dirn;
 {
        char *filename_c, *dirn;
-       int fd;
-       int remaining;
-       int ret;
+       int32_t fd;
+       int32_t remaining;
+       int32_t ret;
 
        if ((filename_c = strdup(fname)) == NULL) {
                perror("strdup");
 
        if ((filename_c = strdup(fname)) == NULL) {
                perror("strdup");
Impressum, Datenschutz