Last active
August 29, 2015 14:17
-
-
Save 173210/d1d0b81a30dec5ee9e7a to your computer and use it in GitHub Desktop.
FIRM Loader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <errno.h> | |
#include <stdint.h> | |
#include <stdio.h> | |
typedef struct { | |
long offset; | |
void *p; | |
size_t size; | |
uint8_t hash[32]; | |
} firmSectHdr; | |
typedef struct { | |
uint32_t magic; | |
uint32_t unk0; | |
void *arm11Ent; | |
void *arm9Ent; | |
uint8_t unk1[48]; | |
firmSectHdr sect[4]; | |
uint8_t sig[256]; | |
} firmHdr; | |
int loadFirm(const char *file) | |
{ | |
firmHdr firm; | |
firmSectHdr *p; | |
FILE *fp; | |
if (file == NULL) { | |
errno = EFAULT; | |
return -1; | |
} | |
fp = fopen(file); | |
if (fp == NULL) | |
return -1; | |
if (fread(&firm, sizeof(firm), 1, fp) <= 0) { | |
fclose(fp); | |
return -1; | |
} | |
if (firm.magic != htobe32(0x4D524946)) { | |
fclose(fp); | |
errno = EILSEQ; | |
return -1; | |
} | |
for (p = firm.sect; p != firm.sect + sizeof(firm.sect) / sizeof(firmSectHdr); p++) { | |
if (!hdr->size) | |
continue; | |
if (fseek(fp, hdr->offset, SEEK_SET)) { | |
fclose(fp); | |
return -1; | |
} | |
if (fread(hdr->p, hdr->size, 1, fp) <= 0) { | |
fclose(fp); | |
return -1; | |
} | |
} | |
fclose(fp); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment