initial commit

This commit is contained in:
Morten Delenk 2016-07-16 11:24:10 +02:00
parent 78952ec974
commit f3504119ee
6 changed files with 240 additions and 0 deletions

7
.gitignore vendored
View file

@ -27,3 +27,10 @@
*.exe *.exe
*.out *.out
*.app *.app
#CMake
cmake_install.cmake
CMakeCache.txt
CMakeFiles/
Makefile
mtgtools.h

10
CMakeLists.txt Normal file
View file

@ -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)

101
firmlink.cpp Normal file
View file

@ -0,0 +1,101 @@
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <cstring>
#include <sstream>
#include "mtgcompat.h"
#include <stdint.h>
#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<std::string> infiles;
std::vector<uint32_t> 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<<std::hex << optarg;
uint32_t entry;
tmp >> entry;
offsets.push_back(entry);
break; }
case 'o': {
outfile=optarg;
break; }
case 'e': {
std::stringstream tmp;
tmp<<std::hex << optarg;
uint32_t entry;
tmp >> entry;
header.arm9Entry=entry;
break; }
case 'E': {
std::stringstream tmp;
tmp<<std::hex << optarg;
uint32_t entry;
tmp >> entry;
header.arm11Entry=entry;
break;}
case '?': {
std::cerr << "Usage: " <<argv[0] << " -O offset1 [-O offset2 [-O offset3 [-O offset4]]] [-o outfile] [-e ARM9-entry] [-E ARM11-entry] file1 [file2 [file3 [file4]]]" << std::endl;
break; }
}
opt=(char)getopt(argc, argv, "O:o:e:E:");
}
char** inputFiles = argv + optind;
int numInputFiles = argc - optind;
while(numInputFiles--) {
infiles.push_back(*(inputFiles++));
}
std::cout << "Found " << infiles.size() << " input files..." << std::endl;
std::ifstream files[4];
int foffset=sizeof(FIRM_header);
for(int i=0;i<MIN(MIN(infiles.size(),offsets.size()),4);i++) {
header.sections[i].offset=foffset;
header.sections[i].physical=offsets[i];
FILE *p_file = NULL;
p_file = fopen(infiles[i].c_str(),"rb");
fseek(p_file,0,SEEK_END);
int size = ftell(p_file);
foffset+=size;
header.sections[i].size=size;
fclose(p_file);
}
std::ofstream out(outfile.c_str(),std::ios::binary);
out.write(reinterpret_cast<char*>(&header),sizeof header);
for(int i=0;i<MIN(MIN(infiles.size(),offsets.size()),4);i++) {
std::ifstream file(infiles[i].c_str(),std::ios::binary);
char *buf=new char[header.sections[i].size];
file.read(buf,header.sections[i].size);
out.write(buf,header.sections[i].size);
}
return 0;
}

108
mtgcompat.c Normal file
View file

@ -0,0 +1,108 @@
#include "mtgcompat.h" // make sure you construct the header file as dictated above
#include "mtgtools.h"
#ifndef HAVE_GETOPT
/*
* Copyright (c) 1987, 1993, 1994
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <string.h>
#include <stdio.h>
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

13
mtgcompat.h Normal file
View file

@ -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

1
mtgtools.h.in Normal file
View file

@ -0,0 +1 @@
#cmakedefine HAVE_GETOPT