mirror of
https://github.com/switchbrew/switch-tools.git
synced 2025-06-21 13:32:39 +02:00
parent
034f2de382
commit
92268de97b
6
.gitignore
vendored
6
.gitignore
vendored
@ -17,6 +17,12 @@ config.log
|
||||
.dirstamp
|
||||
elf2nro
|
||||
elf2nso
|
||||
elf2kip
|
||||
nacptool
|
||||
nxlink
|
||||
*.o
|
||||
*.elf
|
||||
*.kip
|
||||
*.nro
|
||||
*.nso
|
||||
*.json
|
||||
|
@ -1,5 +1,5 @@
|
||||
# Makefile.am -- Process this file with automake to produce Makefile.in
|
||||
bin_PROGRAMS = elf2nso elf2nro build_pfs0 build_romfs nacptool nxlink
|
||||
bin_PROGRAMS = elf2nso elf2nro elf2kip build_pfs0 build_romfs nacptool nxlink
|
||||
|
||||
build_pfs0_SOURCES = src/build_pfs0.c src/types.h
|
||||
|
||||
@ -9,6 +9,8 @@ elf2nro_SOURCES = src/elf2nro.c src/elf64.h src/romfs.c src/filepath.c src/filep
|
||||
|
||||
elf2nso_SOURCES = src/elf2nso.c src/sha256.c src/sha256.h src/elf64.h src/elf_common.h
|
||||
|
||||
elf2kip_SOURCES = src/elf2kip.c src/cJSON.c src/cJSON.h src/blz.c src/blz.h src/elf64.h src/elf_common.h
|
||||
|
||||
nacptool_SOURCES = src/nacptool.c
|
||||
|
||||
nxlink_SOURCES = src/nxlink.c
|
||||
|
338
src/blz.c
Normal file
338
src/blz.c
Normal file
@ -0,0 +1,338 @@
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/*-- blz.c - Bottom LZ coding for Nintendo GBA/DS --*/
|
||||
/*-- Copyright (C) 2011 CUE --*/
|
||||
/*-- --*/
|
||||
/*-- This program is free software: you can redistribute it and/or modify --*/
|
||||
/*-- it under the terms of the GNU General Public License as published by --*/
|
||||
/*-- the Free Software Foundation, either version 3 of the License, or --*/
|
||||
/*-- (at your option) any later version. --*/
|
||||
/*-- --*/
|
||||
/*-- This program is distributed in the hope that it will be useful, --*/
|
||||
/*-- but WITHOUT ANY WARRANTY; without even the implied warranty of --*/
|
||||
/*-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the --*/
|
||||
/*-- GNU General Public License for more details. --*/
|
||||
/*-- --*/
|
||||
/*-- You should have received a copy of the GNU General Public License --*/
|
||||
/*-- along with this program. If not, see <http://www.gnu.org/licenses/>. --*/
|
||||
/*----------------------------------------------------------------------------*/
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
#include "blz.h"
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
#define CMD_DECODE 0x00 // decode
|
||||
#define CMD_ENCODE 0x01 // encode
|
||||
|
||||
#define BLZ_SHIFT 1 // bits to shift
|
||||
#define BLZ_MASK 0x80 // bits to check:
|
||||
// ((((1 << BLZ_SHIFT) - 1) << (8 - BLZ_SHIFT)
|
||||
|
||||
#define BLZ_THRESHOLD 2 // max number of bytes to not encode
|
||||
#define BLZ_N 0x1002 // max offset ((1 << 12) + 2)
|
||||
#define BLZ_F 0x12 // max coded ((1 << 4) + BLZ_THRESHOLD)
|
||||
|
||||
#define RAW_MINIM 0x00000000 // empty file, 0 bytes
|
||||
#define RAW_MAXIM 0x00FFFFFF // 3-bytes length, 16MB - 1
|
||||
|
||||
#define BLZ_MINIM 0x00000004 // header only (empty RAW file)
|
||||
#define BLZ_MAXIM 0x01400000 // 0x0120000A, padded to 20MB:
|
||||
// * length, RAW_MAXIM
|
||||
// * flags, (RAW_MAXIM + 7) / 8
|
||||
// * header, 11
|
||||
// 0x00FFFFFF + 0x00200000 + 12 + padding
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
#define BREAK(text) { printf(text); return; }
|
||||
#define EXIT(text) { printf(text); exit(-1); }
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
u8 *Memory(int length, int size);
|
||||
|
||||
u8 *BLZ_Code(u8 *raw_buffer, int raw_len, u32 *new_len, int best);
|
||||
void BLZ_Invert(u8 *buffer, int length);
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
u8 *Memory(int length, int size) {
|
||||
u8 *fb;
|
||||
|
||||
fb = (u8 *) calloc(length * size, size);
|
||||
if (fb == NULL) EXIT("\nMemory error\n");
|
||||
|
||||
return(fb);
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
void BLZ_Decode(char *filename) {
|
||||
// u8 *pak_buffer, *raw_buffer, *pak, *raw, *pak_end, *raw_end;
|
||||
// u32 pak_len, raw_len, len, pos, inc_len, hdr_len, enc_len, dec_len;
|
||||
// u8 flags, mask;
|
||||
|
||||
// printf("- decoding '%s'", filename);
|
||||
|
||||
// // pak_buffer = Load(filename, &pak_len, BLZ_MINIM, BLZ_MAXIM);
|
||||
|
||||
// inc_len = *(u32 *)(pak_buffer + pak_len - 4);
|
||||
// if (!inc_len) {
|
||||
// enc_len = 0;
|
||||
// dec_len = pak_len - 4;
|
||||
// pak_len = 0;
|
||||
// raw_len = dec_len;
|
||||
// } else {
|
||||
// if (pak_len < 8) EXIT("File has a bad header\n");
|
||||
// hdr_len = pak_buffer[pak_len - 5];
|
||||
// if ((hdr_len < 0x08) || (hdr_len > 0x0B)) EXIT("Bad header length\n");
|
||||
// if (pak_len <= hdr_len) EXIT("Bad length\n");
|
||||
// enc_len = *(u32 *)(pak_buffer + pak_len - 8) & 0x00FFFFFF;
|
||||
// dec_len = pak_len - enc_len;
|
||||
// pak_len = enc_len - hdr_len;
|
||||
// raw_len = dec_len + enc_len + inc_len;
|
||||
// if (raw_len > RAW_MAXIM) EXIT("Bad decoded length\n");
|
||||
// }
|
||||
|
||||
// raw_buffer = (u8 *) Memory(raw_len, sizeof(char));
|
||||
|
||||
// pak = pak_buffer;
|
||||
// raw = raw_buffer;
|
||||
// pak_end = pak_buffer + dec_len + pak_len;
|
||||
// raw_end = raw_buffer + raw_len;
|
||||
|
||||
// for (len = 0; len < dec_len; len++) *(raw++) = *(pak++);
|
||||
|
||||
// BLZ_Invert(pak_buffer + dec_len, pak_len);
|
||||
|
||||
// mask = 0;
|
||||
|
||||
// while (raw < raw_end) {
|
||||
// if (!(mask >>= BLZ_SHIFT)) {
|
||||
// if (pak == pak_end) break;
|
||||
// flags = *pak++;
|
||||
// mask = BLZ_MASK;
|
||||
// }
|
||||
|
||||
// if (!(flags & mask)) {
|
||||
// if (pak == pak_end) break;
|
||||
// *raw++ = *pak++;
|
||||
// } else {
|
||||
// if (pak + 1 >= pak_end) break;
|
||||
// pos = *pak++ << 8;
|
||||
// pos |= *pak++;
|
||||
// len = (pos >> 12) + BLZ_THRESHOLD + 1;
|
||||
// if (raw + len > raw_end) {
|
||||
// printf(", WARNING: wrong decoded length!");
|
||||
// len = raw_end - raw;
|
||||
// }
|
||||
// pos = (pos & 0xFFF) + 3;
|
||||
// while (len--) *(raw++) = *(raw - pos);
|
||||
// }
|
||||
// }
|
||||
|
||||
// BLZ_Invert(raw_buffer + dec_len, raw_len - dec_len);
|
||||
|
||||
// raw_len = raw - raw_buffer;
|
||||
|
||||
// if (raw != raw_end) printf(", WARNING: unexpected end of encoded file!");
|
||||
|
||||
// // Save(filename, raw_buffer, raw_len);
|
||||
|
||||
// free(raw_buffer);
|
||||
// free(pak_buffer);
|
||||
|
||||
// printf("\n");
|
||||
}
|
||||
|
||||
u8 *Load(char *filename, u32 *length, int min, int max) {
|
||||
FILE *fp;
|
||||
int fs;
|
||||
u8 *fb;
|
||||
|
||||
if ((fp = fopen(filename, "rb")) == NULL) EXIT("\nFile open error\n");
|
||||
fseek(fp, 0, SEEK_END);
|
||||
fs = ftell(fp);
|
||||
fseek(fp, 0, SEEK_SET);
|
||||
if ((fs < min) || (fs > max)) EXIT("\nFile size error\n");
|
||||
fb = Memory(fs + 3, sizeof(char));
|
||||
if (fread(fb, 1, fs, fp) != fs) EXIT("\nFile read error\n");
|
||||
if (fclose(fp) == EOF) EXIT("\nFile close error\n");
|
||||
|
||||
*length = fs;
|
||||
|
||||
return(fb);
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
u8* BLZ_Encode(char *filename, u32* pak_len, int mode) {
|
||||
u8 *raw_buffer, *pak_buffer, *new_buffer;
|
||||
u32 raw_len, new_len;
|
||||
|
||||
raw_buffer = Load(filename, &raw_len, RAW_MINIM, RAW_MAXIM);
|
||||
|
||||
pak_buffer = NULL;
|
||||
*pak_len = BLZ_MAXIM + 1;
|
||||
|
||||
new_buffer = BLZ_Code(raw_buffer, raw_len, &new_len, mode);
|
||||
if (new_len < *pak_len) {
|
||||
if (pak_buffer != NULL) free(pak_buffer);
|
||||
pak_buffer = new_buffer;
|
||||
*pak_len = new_len;
|
||||
}
|
||||
|
||||
return pak_buffer;
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
u8 *BLZ_Code(u8 *raw_buffer, int raw_len, u32 *new_len, int best) {
|
||||
u8 *pak_buffer, *pak, *raw, *raw_end, *flg = NULL, *tmp;
|
||||
u32 pak_len, inc_len, hdr_len, enc_len, len, pos, max;
|
||||
u32 len_best, pos_best = 0, len_next, pos_next, len_post, pos_post;
|
||||
u32 pak_tmp, raw_tmp;
|
||||
u8 mask;
|
||||
|
||||
#define SEARCH(l,p) { \
|
||||
l = BLZ_THRESHOLD; \
|
||||
\
|
||||
max = raw - raw_buffer >= BLZ_N ? BLZ_N : raw - raw_buffer; \
|
||||
for (pos = 3; pos <= max; pos++) { \
|
||||
for (len = 0; len < BLZ_F; len++) { \
|
||||
if (raw + len == raw_end) break; \
|
||||
if (len >= pos) break; \
|
||||
if (*(raw + len) != *(raw + len - pos)) break; \
|
||||
} \
|
||||
\
|
||||
if (len > l) { \
|
||||
p = pos; \
|
||||
if ((l = len) == BLZ_F) break; \
|
||||
} \
|
||||
} \
|
||||
}
|
||||
|
||||
pak_tmp = 0;
|
||||
raw_tmp = raw_len;
|
||||
|
||||
pak_len = raw_len + ((raw_len + 7) / 8) + 15;
|
||||
pak_buffer = (u8 *) Memory(pak_len, sizeof(char));
|
||||
|
||||
BLZ_Invert(raw_buffer, raw_len);
|
||||
|
||||
pak = pak_buffer;
|
||||
raw = raw_buffer;
|
||||
raw_end = raw_buffer + raw_len;
|
||||
|
||||
mask = 0;
|
||||
|
||||
while (raw < raw_end) {
|
||||
if (!(mask >>= BLZ_SHIFT)) {
|
||||
*(flg = pak++) = 0;
|
||||
mask = BLZ_MASK;
|
||||
}
|
||||
|
||||
SEARCH(len_best, pos_best);
|
||||
|
||||
// LZ-CUE optimization start
|
||||
if (best) {
|
||||
if (len_best > BLZ_THRESHOLD) {
|
||||
if (raw + len_best < raw_end) {
|
||||
raw += len_best;
|
||||
SEARCH(len_next, pos_next);
|
||||
raw -= len_best - 1;
|
||||
SEARCH(len_post, pos_post);
|
||||
raw--;
|
||||
|
||||
if (len_next <= BLZ_THRESHOLD) len_next = 1;
|
||||
if (len_post <= BLZ_THRESHOLD) len_post = 1;
|
||||
|
||||
if (len_best + len_next <= 1 + len_post) len_best = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
// LZ-CUE optimization end
|
||||
|
||||
*flg <<= 1;
|
||||
if (len_best > BLZ_THRESHOLD) {
|
||||
raw += len_best;
|
||||
*flg |= 1;
|
||||
*pak++ = ((len_best - (BLZ_THRESHOLD+1)) << 4) | ((pos_best - 3) >> 8);
|
||||
*pak++ = (pos_best - 3) & 0xFF;
|
||||
} else {
|
||||
*pak++ = *raw++;
|
||||
}
|
||||
|
||||
if (pak - pak_buffer + raw_len - (raw - raw_buffer) < pak_tmp + raw_tmp) {
|
||||
pak_tmp = pak - pak_buffer;
|
||||
raw_tmp = raw_len - (raw - raw_buffer);
|
||||
}
|
||||
}
|
||||
|
||||
while (mask && (mask != 1)) {
|
||||
mask >>= BLZ_SHIFT;
|
||||
*flg <<= 1;
|
||||
}
|
||||
|
||||
pak_len = pak - pak_buffer;
|
||||
|
||||
BLZ_Invert(raw_buffer, raw_len);
|
||||
BLZ_Invert(pak_buffer, pak_len);
|
||||
|
||||
if (!pak_tmp || (raw_len + 4 < ((pak_tmp + raw_tmp + 3) & -4) + 8)) {
|
||||
pak = pak_buffer;
|
||||
raw = raw_buffer;
|
||||
raw_end = raw_buffer + raw_len;
|
||||
|
||||
while (raw < raw_end) *pak++ = *raw++;
|
||||
|
||||
while ((pak - pak_buffer) & 3) *pak++ = 0;
|
||||
|
||||
*(u32 *)pak = 0; pak += 4;
|
||||
} else {
|
||||
tmp = (u8 *) Memory(raw_tmp + pak_tmp + 11, sizeof(char));
|
||||
|
||||
for (len = 0; len < raw_tmp; len++)
|
||||
tmp[len] = raw_buffer[len];
|
||||
|
||||
for (len = 0; len < pak_tmp; len++)
|
||||
tmp[raw_tmp + len] = pak_buffer[len + pak_len - pak_tmp];
|
||||
|
||||
pak = pak_buffer;
|
||||
pak_buffer = tmp;
|
||||
|
||||
free(pak);
|
||||
|
||||
pak = pak_buffer + raw_tmp + pak_tmp;
|
||||
|
||||
enc_len = pak_tmp;
|
||||
hdr_len = 12;
|
||||
inc_len = raw_len - pak_tmp - raw_tmp;
|
||||
|
||||
while ((pak - pak_buffer) & 3) {
|
||||
*pak++ = 0xFF;
|
||||
hdr_len++;
|
||||
}
|
||||
|
||||
*(u32 *)pak = enc_len + hdr_len; pak += 4;
|
||||
*(u32 *)pak = hdr_len; pak += 4;
|
||||
*(u32 *)pak = inc_len - hdr_len; pak += 4;
|
||||
}
|
||||
|
||||
*new_len = pak - pak_buffer;
|
||||
|
||||
return(pak_buffer);
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
void BLZ_Invert(u8 *buffer, int length) {
|
||||
u8 *bottom, ch;
|
||||
|
||||
bottom = buffer + length - 1;
|
||||
|
||||
while (buffer < bottom) {
|
||||
ch = *buffer;
|
||||
*buffer++ = *bottom;
|
||||
*bottom-- = ch;
|
||||
}
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------------*/
|
||||
/*-- EOF Copyright (C) 2011 CUE --*/
|
||||
/*----------------------------------------------------------------------------*/
|
13
src/blz.h
Normal file
13
src/blz.h
Normal file
@ -0,0 +1,13 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
typedef uint64_t u64;
|
||||
typedef uint32_t u32;
|
||||
typedef uint16_t u16;
|
||||
typedef uint8_t u8;
|
||||
|
||||
#define BLZ_NORMAL 0 // normal mode
|
||||
#define BLZ_BEST 1 // best mode
|
||||
|
||||
u8 *BLZ_Code(u8 *raw_buffer, int raw_len, u32 *new_len, int best);
|
2933
src/cJSON.c
Normal file
2933
src/cJSON.c
Normal file
File diff suppressed because it is too large
Load Diff
277
src/cJSON.h
Normal file
277
src/cJSON.h
Normal file
@ -0,0 +1,277 @@
|
||||
/*
|
||||
Copyright (c) 2009-2017 Dave Gamble and cJSON contributors
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef cJSON__h
|
||||
#define cJSON__h
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
/* project version */
|
||||
#define CJSON_VERSION_MAJOR 1
|
||||
#define CJSON_VERSION_MINOR 7
|
||||
#define CJSON_VERSION_PATCH 6
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
/* cJSON Types: */
|
||||
#define cJSON_Invalid (0)
|
||||
#define cJSON_False (1 << 0)
|
||||
#define cJSON_True (1 << 1)
|
||||
#define cJSON_NULL (1 << 2)
|
||||
#define cJSON_Number (1 << 3)
|
||||
#define cJSON_String (1 << 4)
|
||||
#define cJSON_Array (1 << 5)
|
||||
#define cJSON_Object (1 << 6)
|
||||
#define cJSON_Raw (1 << 7) /* raw json */
|
||||
|
||||
#define cJSON_IsReference 256
|
||||
#define cJSON_StringIsConst 512
|
||||
|
||||
/* The cJSON structure: */
|
||||
typedef struct cJSON
|
||||
{
|
||||
/* next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem */
|
||||
struct cJSON *next;
|
||||
struct cJSON *prev;
|
||||
/* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */
|
||||
struct cJSON *child;
|
||||
|
||||
/* The type of the item, as above. */
|
||||
int type;
|
||||
|
||||
/* The item's string, if type==cJSON_String and type == cJSON_Raw */
|
||||
char *valuestring;
|
||||
/* writing to valueint is DEPRECATED, use cJSON_SetNumberValue instead */
|
||||
int valueint;
|
||||
/* The item's number, if type==cJSON_Number */
|
||||
double valuedouble;
|
||||
|
||||
/* The item's name string, if this item is the child of, or is in the list of subitems of an object. */
|
||||
char *string;
|
||||
} cJSON;
|
||||
|
||||
typedef struct cJSON_Hooks
|
||||
{
|
||||
void *(*malloc_fn)(size_t sz);
|
||||
void (*free_fn)(void *ptr);
|
||||
} cJSON_Hooks;
|
||||
|
||||
typedef int cJSON_bool;
|
||||
|
||||
#if !defined(__WINDOWS__) && (defined(WIN32) || defined(WIN64) || defined(_MSC_VER) || defined(_WIN32))
|
||||
#define __WINDOWS__
|
||||
#endif
|
||||
#ifdef __WINDOWS__
|
||||
|
||||
/* When compiling for windows, we specify a specific calling convention to avoid issues where we are being called from a project with a different default calling convention. For windows you have 2 define options:
|
||||
|
||||
CJSON_HIDE_SYMBOLS - Define this in the case where you don't want to ever dllexport symbols
|
||||
CJSON_EXPORT_SYMBOLS - Define this on library build when you want to dllexport symbols (default)
|
||||
CJSON_IMPORT_SYMBOLS - Define this if you want to dllimport symbol
|
||||
|
||||
For *nix builds that support visibility attribute, you can define similar behavior by
|
||||
|
||||
setting default visibility to hidden by adding
|
||||
-fvisibility=hidden (for gcc)
|
||||
or
|
||||
-xldscope=hidden (for sun cc)
|
||||
to CFLAGS
|
||||
|
||||
then using the CJSON_API_VISIBILITY flag to "export" the same symbols the way CJSON_EXPORT_SYMBOLS does
|
||||
|
||||
*/
|
||||
|
||||
/* export symbols by default, this is necessary for copy pasting the C and header file */
|
||||
#if !defined(CJSON_HIDE_SYMBOLS) && !defined(CJSON_IMPORT_SYMBOLS) && !defined(CJSON_EXPORT_SYMBOLS)
|
||||
#define CJSON_EXPORT_SYMBOLS
|
||||
#endif
|
||||
|
||||
#if defined(CJSON_HIDE_SYMBOLS)
|
||||
#define CJSON_PUBLIC(type) type __stdcall
|
||||
#elif defined(CJSON_EXPORT_SYMBOLS)
|
||||
#define CJSON_PUBLIC(type) __declspec(dllexport) type __stdcall
|
||||
#elif defined(CJSON_IMPORT_SYMBOLS)
|
||||
#define CJSON_PUBLIC(type) __declspec(dllimport) type __stdcall
|
||||
#endif
|
||||
#else /* !WIN32 */
|
||||
#if (defined(__GNUC__) || defined(__SUNPRO_CC) || defined (__SUNPRO_C)) && defined(CJSON_API_VISIBILITY)
|
||||
#define CJSON_PUBLIC(type) __attribute__((visibility("default"))) type
|
||||
#else
|
||||
#define CJSON_PUBLIC(type) type
|
||||
#endif
|
||||
#endif
|
||||
|
||||
/* Limits how deeply nested arrays/objects can be before cJSON rejects to parse them.
|
||||
* This is to prevent stack overflows. */
|
||||
#ifndef CJSON_NESTING_LIMIT
|
||||
#define CJSON_NESTING_LIMIT 1000
|
||||
#endif
|
||||
|
||||
/* returns the version of cJSON as a string */
|
||||
CJSON_PUBLIC(const char*) cJSON_Version(void);
|
||||
|
||||
/* Supply malloc, realloc and free functions to cJSON */
|
||||
CJSON_PUBLIC(void) cJSON_InitHooks(cJSON_Hooks* hooks);
|
||||
|
||||
/* Memory Management: the caller is always responsible to free the results from all variants of cJSON_Parse (with cJSON_Delete) and cJSON_Print (with stdlib free, cJSON_Hooks.free_fn, or cJSON_free as appropriate). The exception is cJSON_PrintPreallocated, where the caller has full responsibility of the buffer. */
|
||||
/* Supply a block of JSON, and this returns a cJSON object you can interrogate. */
|
||||
CJSON_PUBLIC(cJSON *) cJSON_Parse(const char *value);
|
||||
/* ParseWithOpts allows you to require (and check) that the JSON is null terminated, and to retrieve the pointer to the final byte parsed. */
|
||||
/* If you supply a ptr in return_parse_end and parsing fails, then return_parse_end will contain a pointer to the error so will match cJSON_GetErrorPtr(). */
|
||||
CJSON_PUBLIC(cJSON *) cJSON_ParseWithOpts(const char *value, const char **return_parse_end, cJSON_bool require_null_terminated);
|
||||
|
||||
/* Render a cJSON entity to text for transfer/storage. */
|
||||
CJSON_PUBLIC(char *) cJSON_Print(const cJSON *item);
|
||||
/* Render a cJSON entity to text for transfer/storage without any formatting. */
|
||||
CJSON_PUBLIC(char *) cJSON_PrintUnformatted(const cJSON *item);
|
||||
/* Render a cJSON entity to text using a buffered strategy. prebuffer is a guess at the final size. guessing well reduces reallocation. fmt=0 gives unformatted, =1 gives formatted */
|
||||
CJSON_PUBLIC(char *) cJSON_PrintBuffered(const cJSON *item, int prebuffer, cJSON_bool fmt);
|
||||
/* Render a cJSON entity to text using a buffer already allocated in memory with given length. Returns 1 on success and 0 on failure. */
|
||||
/* NOTE: cJSON is not always 100% accurate in estimating how much memory it will use, so to be safe allocate 5 bytes more than you actually need */
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_PrintPreallocated(cJSON *item, char *buffer, const int length, const cJSON_bool format);
|
||||
/* Delete a cJSON entity and all subentities. */
|
||||
CJSON_PUBLIC(void) cJSON_Delete(cJSON *c);
|
||||
|
||||
/* Returns the number of items in an array (or object). */
|
||||
CJSON_PUBLIC(int) cJSON_GetArraySize(const cJSON *array);
|
||||
/* Retrieve item number "item" from array "array". Returns NULL if unsuccessful. */
|
||||
CJSON_PUBLIC(cJSON *) cJSON_GetArrayItem(const cJSON *array, int index);
|
||||
/* Get item "string" from object. Case insensitive. */
|
||||
CJSON_PUBLIC(cJSON *) cJSON_GetObjectItem(const cJSON * const object, const char * const string);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_GetObjectItemCaseSensitive(const cJSON * const object, const char * const string);
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_HasObjectItem(const cJSON *object, const char *string);
|
||||
/* For analysing failed parses. This returns a pointer to the parse error. You'll probably need to look a few chars back to make sense of it. Defined when cJSON_Parse() returns 0. 0 when cJSON_Parse() succeeds. */
|
||||
CJSON_PUBLIC(const char *) cJSON_GetErrorPtr(void);
|
||||
|
||||
/* Check if the item is a string and return its valuestring */
|
||||
CJSON_PUBLIC(char *) cJSON_GetStringValue(cJSON *item);
|
||||
|
||||
/* These functions check the type of an item */
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_IsInvalid(const cJSON * const item);
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_IsFalse(const cJSON * const item);
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_IsTrue(const cJSON * const item);
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_IsBool(const cJSON * const item);
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_IsNull(const cJSON * const item);
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_IsNumber(const cJSON * const item);
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_IsString(const cJSON * const item);
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_IsArray(const cJSON * const item);
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_IsObject(const cJSON * const item);
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_IsRaw(const cJSON * const item);
|
||||
|
||||
/* These calls create a cJSON item of the appropriate type. */
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateNull(void);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateTrue(void);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateFalse(void);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateBool(cJSON_bool boolean);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateNumber(double num);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateString(const char *string);
|
||||
/* raw json */
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateRaw(const char *raw);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateArray(void);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateObject(void);
|
||||
|
||||
/* Create a string where valuestring references a string so
|
||||
* it will not be freed by cJSON_Delete */
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateStringReference(const char *string);
|
||||
/* Create an object/arrray that only references it's elements so
|
||||
* they will not be freed by cJSON_Delete */
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateObjectReference(const cJSON *child);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateArrayReference(const cJSON *child);
|
||||
|
||||
/* These utilities create an Array of count items. */
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateIntArray(const int *numbers, int count);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateFloatArray(const float *numbers, int count);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateDoubleArray(const double *numbers, int count);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateStringArray(const char **strings, int count);
|
||||
|
||||
/* Append item to the specified array/object. */
|
||||
CJSON_PUBLIC(void) cJSON_AddItemToArray(cJSON *array, cJSON *item);
|
||||
CJSON_PUBLIC(void) cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item);
|
||||
/* Use this when string is definitely const (i.e. a literal, or as good as), and will definitely survive the cJSON object.
|
||||
* WARNING: When this function was used, make sure to always check that (item->type & cJSON_StringIsConst) is zero before
|
||||
* writing to `item->string` */
|
||||
CJSON_PUBLIC(void) cJSON_AddItemToObjectCS(cJSON *object, const char *string, cJSON *item);
|
||||
/* Append reference to item to the specified array/object. Use this when you want to add an existing cJSON to a new cJSON, but don't want to corrupt your existing cJSON. */
|
||||
CJSON_PUBLIC(void) cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item);
|
||||
CJSON_PUBLIC(void) cJSON_AddItemReferenceToObject(cJSON *object, const char *string, cJSON *item);
|
||||
|
||||
/* Remove/Detatch items from Arrays/Objects. */
|
||||
CJSON_PUBLIC(cJSON *) cJSON_DetachItemViaPointer(cJSON *parent, cJSON * const item);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromArray(cJSON *array, int which);
|
||||
CJSON_PUBLIC(void) cJSON_DeleteItemFromArray(cJSON *array, int which);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObject(cJSON *object, const char *string);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObjectCaseSensitive(cJSON *object, const char *string);
|
||||
CJSON_PUBLIC(void) cJSON_DeleteItemFromObject(cJSON *object, const char *string);
|
||||
CJSON_PUBLIC(void) cJSON_DeleteItemFromObjectCaseSensitive(cJSON *object, const char *string);
|
||||
|
||||
/* Update array items. */
|
||||
CJSON_PUBLIC(void) cJSON_InsertItemInArray(cJSON *array, int which, cJSON *newitem); /* Shifts pre-existing items to the right. */
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemViaPointer(cJSON * const parent, cJSON * const item, cJSON * replacement);
|
||||
CJSON_PUBLIC(void) cJSON_ReplaceItemInArray(cJSON *array, int which, cJSON *newitem);
|
||||
CJSON_PUBLIC(void) cJSON_ReplaceItemInObject(cJSON *object,const char *string,cJSON *newitem);
|
||||
CJSON_PUBLIC(void) cJSON_ReplaceItemInObjectCaseSensitive(cJSON *object,const char *string,cJSON *newitem);
|
||||
|
||||
/* Duplicate a cJSON item */
|
||||
CJSON_PUBLIC(cJSON *) cJSON_Duplicate(const cJSON *item, cJSON_bool recurse);
|
||||
/* Duplicate will create a new, identical cJSON item to the one you pass, in new memory that will
|
||||
need to be released. With recurse!=0, it will duplicate any children connected to the item.
|
||||
The item->next and ->prev pointers are always zero on return from Duplicate. */
|
||||
/* Recursively compare two cJSON items for equality. If either a or b is NULL or invalid, they will be considered unequal.
|
||||
* case_sensitive determines if object keys are treated case sensitive (1) or case insensitive (0) */
|
||||
CJSON_PUBLIC(cJSON_bool) cJSON_Compare(const cJSON * const a, const cJSON * const b, const cJSON_bool case_sensitive);
|
||||
|
||||
|
||||
CJSON_PUBLIC(void) cJSON_Minify(char *json);
|
||||
|
||||
/* Helper functions for creating and adding items to an object at the same time.
|
||||
* They return the added item or NULL on failure. */
|
||||
CJSON_PUBLIC(cJSON*) cJSON_AddNullToObject(cJSON * const object, const char * const name);
|
||||
CJSON_PUBLIC(cJSON*) cJSON_AddTrueToObject(cJSON * const object, const char * const name);
|
||||
CJSON_PUBLIC(cJSON*) cJSON_AddFalseToObject(cJSON * const object, const char * const name);
|
||||
CJSON_PUBLIC(cJSON*) cJSON_AddBoolToObject(cJSON * const object, const char * const name, const cJSON_bool boolean);
|
||||
CJSON_PUBLIC(cJSON*) cJSON_AddNumberToObject(cJSON * const object, const char * const name, const double number);
|
||||
CJSON_PUBLIC(cJSON*) cJSON_AddStringToObject(cJSON * const object, const char * const name, const char * const string);
|
||||
CJSON_PUBLIC(cJSON*) cJSON_AddRawToObject(cJSON * const object, const char * const name, const char * const raw);
|
||||
CJSON_PUBLIC(cJSON*) cJSON_AddObjectToObject(cJSON * const object, const char * const name);
|
||||
CJSON_PUBLIC(cJSON*) cJSON_AddArrayToObject(cJSON * const object, const char * const name);
|
||||
|
||||
/* When assigning an integer value, it needs to be propagated to valuedouble too. */
|
||||
#define cJSON_SetIntValue(object, number) ((object) ? (object)->valueint = (object)->valuedouble = (number) : (number))
|
||||
/* helper for the cJSON_SetNumberValue macro */
|
||||
CJSON_PUBLIC(double) cJSON_SetNumberHelper(cJSON *object, double number);
|
||||
#define cJSON_SetNumberValue(object, number) ((object != NULL) ? cJSON_SetNumberHelper(object, (double)number) : (number))
|
||||
|
||||
/* Macro for iterating over an array or object */
|
||||
#define cJSON_ArrayForEach(element, array) for(element = (array != NULL) ? (array)->child : NULL; element != NULL; element = element->next)
|
||||
|
||||
/* malloc/free objects using the malloc/free functions that have been set with cJSON_InitHooks */
|
||||
CJSON_PUBLIC(void *) cJSON_malloc(size_t size);
|
||||
CJSON_PUBLIC(void) cJSON_free(void *object);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
585
src/elf2kip.c
Normal file
585
src/elf2kip.c
Normal file
@ -0,0 +1,585 @@
|
||||
// Copyright 2018 SciresM
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <limits.h>
|
||||
#include "cJSON.h"
|
||||
#include "blz.h"
|
||||
#include "elf64.h"
|
||||
|
||||
typedef uint64_t u64;
|
||||
typedef uint32_t u32;
|
||||
typedef uint16_t u16;
|
||||
typedef uint8_t u8;
|
||||
|
||||
typedef struct {
|
||||
u32 DstOff;
|
||||
u32 DecompSz;
|
||||
u32 CompSz;
|
||||
u32 Attribute;
|
||||
} KipSegment;
|
||||
|
||||
typedef struct {
|
||||
u8 Magic[4];
|
||||
u8 Name[0xC];
|
||||
u64 TitleId;
|
||||
u32 ProcessCategory;
|
||||
u8 MainThreadPriority;
|
||||
u8 DefaultCpuId;
|
||||
u8 Unk;
|
||||
u8 Flags;
|
||||
KipSegment Segments[6];
|
||||
u32 Capabilities[0x20];
|
||||
} KipHeader;
|
||||
|
||||
uint8_t* ReadEntireFile(const char* fn, size_t* len_out) {
|
||||
FILE* fd = fopen(fn, "rb");
|
||||
if (fd == NULL)
|
||||
return NULL;
|
||||
|
||||
fseek(fd, 0, SEEK_END);
|
||||
size_t len = ftell(fd);
|
||||
fseek(fd, 0, SEEK_SET);
|
||||
|
||||
uint8_t* buf = malloc(len);
|
||||
if (buf == NULL) {
|
||||
fclose(fd);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
size_t rc = fread(buf, 1, len, fd);
|
||||
if (rc != len) {
|
||||
fclose(fd);
|
||||
free(buf);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
*len_out = len;
|
||||
return buf;
|
||||
}
|
||||
|
||||
int cJSON_GetU8(const cJSON *obj, const char *field, u8 *out) {
|
||||
const cJSON *config = cJSON_GetObjectItemCaseSensitive(obj, field);
|
||||
if (cJSON_IsNumber(config)) {
|
||||
*out = (u8)config->valueint;
|
||||
return 1;
|
||||
} else {
|
||||
fprintf(stderr, "Failed to get %s (field not present).\n", field);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
int cJSON_GetU16(const cJSON *obj, const char *field, u16 *out) {
|
||||
const cJSON *config = cJSON_GetObjectItemCaseSensitive(obj, field);
|
||||
if (cJSON_IsNumber(config)) {
|
||||
*out = (u16)config->valueint;
|
||||
return 1;
|
||||
} else {
|
||||
fprintf(stderr, "Failed to get %s (field not present).\n", field);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
int cJSON_GetU16FromObjectValue(const cJSON *config, u16 *out) {
|
||||
if (cJSON_IsNumber(config)) {
|
||||
*out = (u16)config->valueint;
|
||||
return 1;
|
||||
} else {
|
||||
fprintf(stderr, "Failed to get %s (field not present).\n", config->string);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
int cJSON_GetBoolean(const cJSON *obj, const char *field, int *out) {
|
||||
const cJSON *config = cJSON_GetObjectItemCaseSensitive(obj, field);
|
||||
if (cJSON_IsBool(config)) {
|
||||
if (cJSON_IsTrue(config)) {
|
||||
*out = 1;
|
||||
} else if (cJSON_IsFalse(config)) {
|
||||
*out = 0;
|
||||
} else {
|
||||
fprintf(stderr, "Unknown boolean value in %s.\n", field);
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
} else {
|
||||
fprintf(stderr, "Failed to get %s (field not present).\n", field);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
int cJSON_GetBooleanOptional(const cJSON *obj, const char *field, int *out) {
|
||||
const cJSON *config = cJSON_GetObjectItemCaseSensitive(obj, field);
|
||||
if (cJSON_IsBool(config)) {
|
||||
if (cJSON_IsTrue(config)) {
|
||||
*out = 1;
|
||||
} else if (cJSON_IsFalse(config)) {
|
||||
*out = 0;
|
||||
} else {
|
||||
fprintf(stderr, "Unknown boolean value in %s.\n", field);
|
||||
return 0;
|
||||
}
|
||||
} else {
|
||||
*out = 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int cJSON_GetU64(const cJSON *obj, const char *field, u64 *out) {
|
||||
const cJSON *config = cJSON_GetObjectItemCaseSensitive(obj, field);
|
||||
if (cJSON_IsString(config) && (config->valuestring != NULL)) {
|
||||
char *endptr = NULL;
|
||||
*out = strtoul(config->valuestring, &endptr, 16);
|
||||
if (config->valuestring == endptr) {
|
||||
fprintf(stderr, "Failed to get %s (empty string)\n", field);
|
||||
return 0;
|
||||
} else if (errno == ERANGE) {
|
||||
fprintf(stderr, "Failed to get %s (value out of range)\n", field);
|
||||
return 0;
|
||||
} else if (errno == EINVAL) {
|
||||
fprintf(stderr, "Failed to get %s (not base16 string)\n", field);
|
||||
return 0;
|
||||
} else if (errno) {
|
||||
fprintf(stderr, "Failed to get %s (unknown error)\n", field);
|
||||
return 0;
|
||||
} else {
|
||||
return 1;
|
||||
}
|
||||
} else {
|
||||
fprintf(stderr, "Failed to get %s (field not present).\n", field);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
int cJSON_GetU64FromObjectValue(const cJSON *config, u64 *out) {
|
||||
if (cJSON_IsString(config) && (config->valuestring != NULL)) {
|
||||
char *endptr = NULL;
|
||||
*out = strtoul(config->valuestring, &endptr, 16);
|
||||
if (config->valuestring == endptr) {
|
||||
fprintf(stderr, "Failed to get %s (empty string)\n", config->string);
|
||||
return 0;
|
||||
} else if (errno == ERANGE) {
|
||||
fprintf(stderr, "Failed to get %s (value out of range)\n", config->string);
|
||||
return 0;
|
||||
} else if (errno == EINVAL) {
|
||||
fprintf(stderr, "Failed to get %s (not base16 string)\n", config->string);
|
||||
return 0;
|
||||
} else if (errno) {
|
||||
fprintf(stderr, "Failed to get %s (unknown error)\n", config->string);
|
||||
return 0;
|
||||
} else {
|
||||
return 1;
|
||||
}
|
||||
} else {
|
||||
fprintf(stderr, "Failed to get %s (field not present).\n", config->string);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
int ParseKipConfiguration(const char *json, KipHeader *kip_hdr) {
|
||||
const cJSON *capability = NULL;
|
||||
const cJSON *capabilities = NULL;
|
||||
int status = 0;
|
||||
cJSON *npdm_json = cJSON_Parse(json);
|
||||
if (npdm_json == NULL) {
|
||||
const char *error_ptr = cJSON_GetErrorPtr();
|
||||
if (error_ptr != NULL) {
|
||||
fprintf(stderr, "JSON Parse Error: %s\n", error_ptr);
|
||||
}
|
||||
status = 0;
|
||||
goto PARSE_CAPS_END;
|
||||
}
|
||||
|
||||
/* Parse name. */
|
||||
const cJSON *title_name = cJSON_GetObjectItemCaseSensitive(npdm_json, "name");
|
||||
if (cJSON_IsString(title_name) && (title_name->valuestring != NULL)) {
|
||||
strncpy(kip_hdr->Name, title_name->valuestring, sizeof(kip_hdr->Name) - 1);
|
||||
} else {
|
||||
fprintf(stderr, "Failed to get title name (name field not present).\n");
|
||||
status = 0;
|
||||
goto PARSE_CAPS_END;
|
||||
}
|
||||
|
||||
/* Parse title_id. */
|
||||
if (!cJSON_GetU64(npdm_json, "title_id", &kip_hdr->TitleId)) {
|
||||
status = 0;
|
||||
goto PARSE_CAPS_END;
|
||||
}
|
||||
|
||||
/* Parse main_thread_stack_size. */
|
||||
u64 stack_size = 0;
|
||||
if (!cJSON_GetU64(npdm_json, "main_thread_stack_size", &stack_size)) {
|
||||
status = 0;
|
||||
goto PARSE_CAPS_END;
|
||||
}
|
||||
if (stack_size >> 32) {
|
||||
fprintf(stderr, "Error: Main thread stack size must be a u32!\n");
|
||||
status = 0;
|
||||
goto PARSE_CAPS_END;
|
||||
}
|
||||
kip_hdr->Segments[1].Attribute = (u32)(stack_size & 0xFFFFFFFF);
|
||||
|
||||
/* Parse various config. */
|
||||
if (!cJSON_GetU8(npdm_json, "main_thread_priority", &kip_hdr->MainThreadPriority)) {
|
||||
status = 0;
|
||||
goto PARSE_CAPS_END;
|
||||
}
|
||||
if (!cJSON_GetU8(npdm_json, "default_cpu_id", &kip_hdr->DefaultCpuId)) {
|
||||
status = 0;
|
||||
goto PARSE_CAPS_END;
|
||||
}
|
||||
if (!cJSON_GetU8(npdm_json, "process_category", (u8 *)&kip_hdr->ProcessCategory)) {
|
||||
status = 0;
|
||||
goto PARSE_CAPS_END;
|
||||
}
|
||||
|
||||
/* Parse capabilities. */
|
||||
capabilities = cJSON_GetObjectItemCaseSensitive(npdm_json, "kernel_capabilities");
|
||||
if (!cJSON_IsObject(capabilities)) {
|
||||
fprintf(stderr, "Kernel Capabilities must be an object!\n");
|
||||
status = 0;
|
||||
goto PARSE_CAPS_END;
|
||||
}
|
||||
|
||||
u32 cur_cap = 0;
|
||||
u32 desc;
|
||||
cJSON_ArrayForEach(capability, capabilities) {
|
||||
desc = 0;
|
||||
const char *type_str = capability->string;
|
||||
|
||||
const cJSON *value = capability;
|
||||
if (!strcmp(type_str, "kernel_flags")) {
|
||||
if (cur_cap + 1 > 0x20) {
|
||||
fprintf(stderr, "Error: Too many capabilities!\n");
|
||||
status = 0;
|
||||
goto PARSE_CAPS_END;
|
||||
}
|
||||
if (!cJSON_IsObject(value)) {
|
||||
fprintf(stderr, "Kernel Flags Capability value must be object!\n");
|
||||
status = 0;
|
||||
goto PARSE_CAPS_END;
|
||||
}
|
||||
u8 highest_prio = 0, lowest_prio = 0, lowest_cpu = 0, highest_cpu = 0;
|
||||
if (!cJSON_GetU8(value, "highest_thread_priority", &highest_prio) ||
|
||||
!cJSON_GetU8(value, "lowest_thread_priority", &lowest_prio) ||
|
||||
!cJSON_GetU8(value, "highest_cpu_id", &highest_cpu) ||
|
||||
!cJSON_GetU8(value, "lowest_cpu_id", &lowest_cpu)) {
|
||||
status = 0;
|
||||
goto PARSE_CAPS_END;
|
||||
}
|
||||
desc = highest_cpu;
|
||||
desc <<= 8;
|
||||
desc |= lowest_cpu;
|
||||
desc <<= 8;
|
||||
desc |= (lowest_prio & 0x3F);
|
||||
desc <<= 6;
|
||||
desc |= (highest_prio & 0x3F);
|
||||
kip_hdr->Capabilities[cur_cap++] = (u32)((desc << 4) | (0x0007));
|
||||
} else if (!strcmp(type_str, "syscalls")) {
|
||||
if (!cJSON_IsObject(value)) {
|
||||
fprintf(stderr, "Syscalls Capability value must be object!\n");
|
||||
status = 0;
|
||||
goto PARSE_CAPS_END;
|
||||
}
|
||||
u32 num_descriptors;
|
||||
u32 descriptors[6] = {0}; /* alignup(0x80/0x18); */
|
||||
char field_name[8] = {0};
|
||||
const cJSON *cur_syscall = NULL;
|
||||
u64 syscall_value = 0;
|
||||
cJSON_ArrayForEach(cur_syscall, value) {
|
||||
if (cJSON_IsNumber(cur_syscall)) {
|
||||
syscall_value = (u64)cur_syscall->valueint;
|
||||
} else if (!cJSON_IsString(cur_syscall) || !cJSON_GetU64(value, cur_syscall->string, &syscall_value)) {
|
||||
fprintf(stderr, "Error: Syscall entries must be integers or hex strings.\n");
|
||||
status = 0;
|
||||
goto PARSE_CAPS_END;
|
||||
}
|
||||
|
||||
if (syscall_value >= 0x80) {
|
||||
fprintf(stderr, "Error: All syscall entries must be numbers in [0, 0x7F]\n");
|
||||
status = 0;
|
||||
goto PARSE_CAPS_END;
|
||||
}
|
||||
descriptors[syscall_value / 0x18] |= (1UL << (syscall_value % 0x18));
|
||||
}
|
||||
for (unsigned int i = 0; i < 6; i++) {
|
||||
if (descriptors[i]) {
|
||||
if (cur_cap + 1 > 0x20) {
|
||||
fprintf(stderr, "Error: Too many capabilities!\n");
|
||||
status = 0;
|
||||
goto PARSE_CAPS_END;
|
||||
}
|
||||
desc = descriptors[i] | (i << 24);
|
||||
kip_hdr->Capabilities[cur_cap++] = (u32)((desc << 5) | (0x000F));
|
||||
}
|
||||
}
|
||||
} else if (!strcmp(type_str, "map")) {
|
||||
if (cur_cap + 2 > 0x20) {
|
||||
fprintf(stderr, "Error: Too many capabilities!\n");
|
||||
status = 0;
|
||||
goto PARSE_CAPS_END;
|
||||
}
|
||||
if (!cJSON_IsObject(value)) {
|
||||
fprintf(stderr, "Map Capability value must be object!\n");
|
||||
status = 0;
|
||||
goto PARSE_CAPS_END;
|
||||
}
|
||||
u64 map_address = 0;
|
||||
u64 map_size = 0;
|
||||
int is_ro;
|
||||
int is_io;
|
||||
if (!cJSON_GetU64(value, "address", &map_address) ||
|
||||
!cJSON_GetU64(value, "size", &map_size) ||
|
||||
!cJSON_GetBoolean(value, "is_ro", &is_ro) ||
|
||||
!cJSON_GetBoolean(value, "is_io", &is_io)) {
|
||||
status = 0;
|
||||
goto PARSE_CAPS_END;
|
||||
}
|
||||
desc = (u32)((map_address >> 12) & 0x00FFFFFFULL);
|
||||
desc |= is_ro << 24;
|
||||
kip_hdr->Capabilities[cur_cap++] = (u32)((desc << 7) | (0x003F));
|
||||
|
||||
desc = (u32)((map_size >> 12) & 0x00FFFFFFULL);
|
||||
is_io ^= 1;
|
||||
desc |= is_io << 24;
|
||||
kip_hdr->Capabilities[cur_cap++] = (u32)((desc << 7) | (0x003F));
|
||||
} else if (!strcmp(type_str, "map_page")) {
|
||||
if (cur_cap + 1 > 0x20) {
|
||||
fprintf(stderr, "Error: Too many capabilities!\n");
|
||||
status = 0;
|
||||
goto PARSE_CAPS_END;
|
||||
}
|
||||
u64 page_address = 0;
|
||||
if (!cJSON_GetU64FromObjectValue(value, &page_address)) {
|
||||
status = 0;
|
||||
goto PARSE_CAPS_END;
|
||||
}
|
||||
desc = (u32)((page_address >> 12) & 0x00FFFFFFULL);
|
||||
kip_hdr->Capabilities[cur_cap++] = (u32)((desc << 8) | (0x007F));
|
||||
} else if (!strcmp(type_str, "irq_pair")) {
|
||||
if (cur_cap + 1 > 0x20) {
|
||||
fprintf(stderr, "Error: Too many capabilities!\n");
|
||||
status = 0;
|
||||
goto PARSE_CAPS_END;
|
||||
}
|
||||
if (!cJSON_IsArray(value) || cJSON_GetArraySize(value) != 2) {
|
||||
fprintf(stderr, "Error: IRQ Pairs must have size 2 array value.\n");
|
||||
status = 0;
|
||||
goto PARSE_CAPS_END;
|
||||
}
|
||||
const cJSON *irq = NULL;
|
||||
cJSON_ArrayForEach(irq, value) {
|
||||
desc <<= 10;
|
||||
if (cJSON_IsNull(irq)) {
|
||||
desc |= 0x3FF;
|
||||
} else if (cJSON_IsNumber(irq)) {
|
||||
desc |= ((u16)(irq->valueint)) & 0x3FF;
|
||||
} else {
|
||||
fprintf(stderr, "Failed to parse IRQ value.\n");
|
||||
status = 0;
|
||||
goto PARSE_CAPS_END;
|
||||
}
|
||||
}
|
||||
kip_hdr->Capabilities[cur_cap++] = (u32)((desc << 12) | (0x07FF));
|
||||
} else if (!strcmp(type_str, "application_type")) {
|
||||
if (cur_cap + 1 > 0x20) {
|
||||
fprintf(stderr, "Error: Too many capabilities!\n");
|
||||
status = 0;
|
||||
goto PARSE_CAPS_END;
|
||||
}
|
||||
if (!cJSON_GetU16FromObjectValue(value, (u16 *)&desc)) {
|
||||
status = 0;
|
||||
goto PARSE_CAPS_END;
|
||||
}
|
||||
desc &= 7;
|
||||
kip_hdr->Capabilities[cur_cap++] = (u32)((desc << 14) | (0x1FFF));
|
||||
} else if (!strcmp(type_str, "min_kernel_version")) {
|
||||
if (cur_cap + 1 > 0x20) {
|
||||
fprintf(stderr, "Error: Too many capabilities!\n");
|
||||
status = 0;
|
||||
goto PARSE_CAPS_END;
|
||||
}
|
||||
if (!cJSON_GetU16FromObjectValue(value, (u16 *)&desc)) {
|
||||
status = 0;
|
||||
goto PARSE_CAPS_END;
|
||||
}
|
||||
kip_hdr->Capabilities[cur_cap++] = (u32)((desc << 15) | (0x3FFF));
|
||||
} else if (!strcmp(type_str, "handle_table_size")) {
|
||||
if (cur_cap + 1 > 0x20) {
|
||||
fprintf(stderr, "Error: Too many capabilities!\n");
|
||||
status = 0;
|
||||
goto PARSE_CAPS_END;
|
||||
}
|
||||
if (!cJSON_GetU16FromObjectValue(value, (u16 *)&desc)) {
|
||||
status = 0;
|
||||
goto PARSE_CAPS_END;
|
||||
}
|
||||
kip_hdr->Capabilities[cur_cap++] = (u32)((desc << 16) | (0x7FFF));
|
||||
} else if (!strcmp(type_str, "debug_flags")) {
|
||||
if (cur_cap + 1 > 0x20) {
|
||||
fprintf(stderr, "Error: Too many capabilities!\n");
|
||||
status = 0;
|
||||
goto PARSE_CAPS_END;
|
||||
}
|
||||
if (!cJSON_IsObject(value)) {
|
||||
fprintf(stderr, "Debug Flag Capability value must be object!\n");
|
||||
status = 0;
|
||||
goto PARSE_CAPS_END;
|
||||
}
|
||||
int allow_debug = 0;
|
||||
int force_debug = 0;
|
||||
if (!cJSON_GetBoolean(value, "allow_debug", &allow_debug)) {
|
||||
status = 0;
|
||||
goto PARSE_CAPS_END;
|
||||
}
|
||||
if (!cJSON_GetBoolean(value, "force_debug", &force_debug)) {
|
||||
status = 0;
|
||||
goto PARSE_CAPS_END;
|
||||
}
|
||||
desc = (allow_debug & 1) | ((force_debug & 1) << 1);
|
||||
kip_hdr->Capabilities[cur_cap++] = (u32)((desc << 17) | (0xFFFF));
|
||||
}
|
||||
}
|
||||
|
||||
for (u32 i = cur_cap; i < 0x20; i++) {
|
||||
kip_hdr->Capabilities[i] = 0xFFFFFFFF;
|
||||
}
|
||||
|
||||
PARSE_CAPS_END:
|
||||
cJSON_Delete(npdm_json);
|
||||
return status;
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
if (argc != 4) {
|
||||
fprintf(stderr, "%s <elf-file> <json-file> <kip-file>\n", argv[0]);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
KipHeader kip_hdr = {0};
|
||||
memcpy(kip_hdr.Magic, "KIP1", 4);
|
||||
kip_hdr.Flags = 0x1F;
|
||||
|
||||
if (sizeof(KipHeader) != 0x100) {
|
||||
fprintf(stderr, "Bad compile environment!\n");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
size_t json_len;
|
||||
uint8_t* json = ReadEntireFile(argv[2], &json_len);
|
||||
if (json == NULL) {
|
||||
fprintf(stderr, "Failed to read descriptor json!\n");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
ParseKipConfiguration(json, &kip_hdr);
|
||||
|
||||
size_t elf_len;
|
||||
uint8_t* elf = ReadEntireFile(argv[1], &elf_len);
|
||||
if (elf == NULL) {
|
||||
fprintf(stderr, "Failed to open input!\n");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
if (elf_len < sizeof(Elf64_Ehdr)) {
|
||||
fprintf(stderr, "Input file doesn't fit ELF header!\n");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
Elf64_Ehdr* hdr = (Elf64_Ehdr*) elf;
|
||||
if (hdr->e_machine != EM_AARCH64) {
|
||||
fprintf(stderr, "Invalid ELF: expected AArch64!\n");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
Elf64_Off ph_end = hdr->e_phoff + hdr->e_phnum * sizeof(Elf64_Phdr);
|
||||
|
||||
if (ph_end < hdr->e_phoff || ph_end > elf_len) {
|
||||
fprintf(stderr, "Invalid ELF: phdrs outside file!\n");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
Elf64_Phdr* phdrs = (Elf64_Phdr*) &elf[hdr->e_phoff];
|
||||
size_t i, j = 0;
|
||||
size_t file_off = 0;
|
||||
size_t dst_off = 0;
|
||||
size_t tmpsize;
|
||||
|
||||
uint8_t* buf[3];
|
||||
uint8_t* cmp[3];
|
||||
size_t FileOffsets[3];
|
||||
|
||||
for (i=0; i<4; i++) {
|
||||
Elf64_Phdr* phdr = NULL;
|
||||
while (j < hdr->e_phnum) {
|
||||
Elf64_Phdr* cur = &phdrs[j];
|
||||
if (i < 2 || (i==2 && cur->p_type != PT_LOAD)) j++;
|
||||
if (cur->p_type == PT_LOAD || i == 3) {
|
||||
phdr = cur;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (phdr == NULL) {
|
||||
fprintf(stderr, "Invalid ELF: expected 3 loadable phdrs and a bss!\n");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
|
||||
kip_hdr.Segments[i].DstOff = dst_off;
|
||||
|
||||
// .bss is special
|
||||
if (i == 3) {
|
||||
tmpsize = (phdr->p_filesz + 0xFFF) & ~0xFFF;
|
||||
if ( phdr->p_memsz > tmpsize) {
|
||||
kip_hdr.Segments[i].DecompSz = ((phdr->p_memsz - tmpsize) + 0xFFF) & ~0xFFF;
|
||||
} else {
|
||||
kip_hdr.Segments[i].DecompSz = 0;
|
||||
}
|
||||
kip_hdr.Segments[i].DstOff = phdr->p_vaddr;
|
||||
kip_hdr.Segments[i].CompSz = 0;
|
||||
break;
|
||||
}
|
||||
|
||||
FileOffsets[i] = phdr->p_vaddr;
|
||||
kip_hdr.Segments[i].DecompSz = (phdr->p_filesz + 0xFFF) & ~0xFFF;
|
||||
buf[i] = malloc(kip_hdr.Segments[i].DecompSz);
|
||||
|
||||
if (buf[i] == NULL) {
|
||||
fprintf(stderr, "Out of memory!\n");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
memset(buf[i], 0, kip_hdr.Segments[i].DecompSz);
|
||||
|
||||
memcpy(buf[i], &elf[phdr->p_offset], phdr->p_filesz);
|
||||
cmp[i] = BLZ_Code(buf[i], phdr->p_filesz, &kip_hdr.Segments[i].CompSz, BLZ_BEST);
|
||||
|
||||
file_off += kip_hdr.Segments[i].CompSz;
|
||||
dst_off += kip_hdr.Segments[i].DecompSz;
|
||||
dst_off = (dst_off + 0xFFF) & ~0xFFF;
|
||||
}
|
||||
|
||||
FILE* out = fopen(argv[3], "wb");
|
||||
|
||||
if (out == NULL) {
|
||||
fprintf(stderr, "Failed to open output file!\n");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
// TODO check retvals
|
||||
|
||||
for (i=0; i<3; i++)
|
||||
{
|
||||
fseek(out, sizeof(kip_hdr) + kip_hdr.Segments[i].DstOff, SEEK_SET);
|
||||
fwrite(cmp[i], kip_hdr.Segments[i].CompSz, 1, out);
|
||||
}
|
||||
|
||||
fseek(out, 0, SEEK_SET);
|
||||
fwrite(&kip_hdr, sizeof(kip_hdr), 1, out);
|
||||
|
||||
fclose(out);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
Loading…
Reference in New Issue
Block a user