From f3504119ee77398ad2fdcc4abf10186648c03349 Mon Sep 17 00:00:00 2001 From: Morten Delenk Date: Sat, 16 Jul 2016 11:24:10 +0200 Subject: [PATCH] initial commit --- .gitignore | 7 ++++ CMakeLists.txt | 10 +++++ firmlink.cpp | 101 +++++++++++++++++++++++++++++++++++++++++++++ mtgcompat.c | 108 +++++++++++++++++++++++++++++++++++++++++++++++++ mtgcompat.h | 13 ++++++ mtgtools.h.in | 1 + 6 files changed, 240 insertions(+) create mode 100644 CMakeLists.txt create mode 100644 firmlink.cpp create mode 100644 mtgcompat.c create mode 100644 mtgcompat.h create mode 100644 mtgtools.h.in diff --git a/.gitignore b/.gitignore index 4581ef2..2b64478 100644 --- a/.gitignore +++ b/.gitignore @@ -27,3 +27,10 @@ *.exe *.out *.app + +#CMake +cmake_install.cmake +CMakeCache.txt +CMakeFiles/ +Makefile +mtgtools.h \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..2cba4cd --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,10 @@ +cmake_minimum_required (VERSION 2.6) +project (mtgos-tools) +include (CheckFunctionExists) +check_function_exists(getopt HAVE_GETOPT) +configure_file ("${PROJECT_SOURCE_DIR}/mtgtools.h.in" "${PROJECT_BINARY_DIR}/mtgtools.h") +include_directories("${PROJECT_BINARY_DIR}") +add_library(mtgcompat mtgcompat.c) +add_executable(firmlink firmlink.cpp) +target_link_libraries(firmlink mtgcompat) +install(TARGETS firmlink DESTINATION bin) \ No newline at end of file diff --git a/firmlink.cpp b/firmlink.cpp new file mode 100644 index 0000000..de1f2e0 --- /dev/null +++ b/firmlink.cpp @@ -0,0 +1,101 @@ +#include +#include +#include +#include +#include +#include +#include "mtgcompat.h" +#include +#define MIN(a,b) (((a)<(b))?(a):(b)) +#define MAX(a,b) (((a)>(b))?(a):(b)) +struct FIRM_sect { + uint32_t offset; + uint32_t physical; + uint32_t size; + bool arm11; + uint8_t SHA256[0x20]; //Implement later. +}__attribute__((packed)); +struct FIRM_header { + char magic[4]; + int reserved1; + uint32_t arm11Entry; + uint32_t arm9Entry; + uint8_t reserved2[0x30]; + FIRM_sect sections[4]; + uint8_t RSA2048[0x100]; //I'd need to find out big N's private key to do that! +}__attribute__((packed)); +int main(int argc, char** argv) { + std::vector infiles; + std::vector offsets; + std::string outfile="firm.bin"; + FIRM_header header; + //Populate header + char * tmp="FIRM"; + memcpy(header.magic,tmp,4); + header.reserved1=0; + memset(header.reserved2,0,sizeof(header.reserved2)); + memset(header.RSA2048,0,sizeof(header.RSA2048)); + header.arm11Entry=0x1FF80000; + header.arm9Entry=0x08000000; + char opt = (char)getopt(argc, argv, "O:o:e:E:"); //O = load point of segment; o= output file; e=ARM9 entry; E=ARM11 entry + while (opt!=-1) { + switch(opt) { + case 'O': { + std::stringstream tmp; + tmp<> entry; + offsets.push_back(entry); + break; } + case 'o': { + outfile=optarg; + break; } + case 'e': { + std::stringstream tmp; + tmp<> entry; + header.arm9Entry=entry; + break; } + case 'E': { + std::stringstream tmp; + tmp<> entry; + header.arm11Entry=entry; + break;} + case '?': { + std::cerr << "Usage: " <(&header),sizeof header); + for(int i=0;i +#include + +int opterr = 1, /* if error message should be printed */ +optind = 1, /* index into parent argv vector */ +optopt, /* character checked for validity */ +optreset; /* reset getopt */ +char *optarg; /* argument associated with option */ + +#define BADCH (int)'?' +#define BADARG (int)':' +#define EMSG "" + + /* + * getopt -- + * Parse argc/argv argument vector. + */ +int getopt(int nargc, char * const nargv[], const char *ostr) +{ + static char *place = EMSG; /* option letter processing */ + const char *oli; /* option letter list index */ + + if (optreset || !*place) { /* update scanning pointer */ + optreset = 0; + if (optind >= nargc || *(place = nargv[optind]) != '-') { + place = EMSG; + return (-1); + } + if (place[1] && *++place == '-') { /* found "--" */ + ++optind; + place = EMSG; + return (-1); + } + } /* option letter okay? */ + if ((optopt = (int)*place++) == (int)':' || + !(oli = strchr(ostr, optopt))) { + /* + * if the user didn't specify '-' as an option, + * assume it means -1. + */ + if (optopt == (int)'-') + return (-1); + if (!*place) + ++optind; + if (opterr && *ostr != ':') + (void)printf("illegal option -- %c\n", optopt); + return (BADCH); + } + if (*++oli != ':') { /* don't need argument */ + optarg = NULL; + if (!*place) + ++optind; + } + else { /* need an argument */ + if (*place) /* no white space */ + optarg = place; + else if (nargc <= ++optind) { /* no arg */ + place = EMSG; + if (*ostr == ':') + return (BADARG); + if (opterr) + (void)printf("option requires an argument -- %c\n", optopt); + return (BADCH); + } + else /* white space */ + optarg = nargv[optind]; + place = EMSG; + ++optind; + } + return (optopt); /* dump back option letter */ +} +#endif \ No newline at end of file diff --git a/mtgcompat.h b/mtgcompat.h new file mode 100644 index 0000000..4a60f02 --- /dev/null +++ b/mtgcompat.h @@ -0,0 +1,13 @@ +#ifndef MTGCOMPAT_H +#define MTGCOMPAT_H +#ifdef __cplusplus +extern "C" { +#endif +extern char *optarg; +extern int opterr, optind, optopt; + +int getopt(int nargc, char * const nargv[], const char *ostr); +#ifdef __cplusplus +} +#endif +#endif \ No newline at end of file diff --git a/mtgtools.h.in b/mtgtools.h.in new file mode 100644 index 0000000..a6acb4c --- /dev/null +++ b/mtgtools.h.in @@ -0,0 +1 @@ +#cmakedefine HAVE_GETOPT \ No newline at end of file