mirror of
https://github.com/switchbrew/switch-tools.git
synced 2025-07-03 18:42:15 +02:00
Compare commits
54 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
22756068dd | ||
|
0098896307 | ||
|
28bd095683 | ||
|
e36a8251cd | ||
|
786ccde466 | ||
|
22471a1f5a | ||
|
becb4df6f6 | ||
|
70d270d001 | ||
|
6fd5ae6e48 | ||
|
87f4744e62 | ||
|
6bad2b90e6 | ||
|
3b5794ef95 | ||
|
6f1d635208 | ||
|
f6561ef910 | ||
|
660305f32e | ||
|
07835aa357 | ||
|
f85a558fc4 | ||
|
c2977a7167 | ||
|
1b660e22c3 | ||
|
72b9e5cc85 | ||
|
fe13e1c1da | ||
|
980dbbfe4c | ||
|
793b056f5b | ||
|
48ec411d85 | ||
|
b3a257b530 | ||
|
f1ffee8e1d | ||
|
2c7bedf37b | ||
|
17276a6bc4 | ||
|
d34444b575 | ||
|
e607ced3e2 | ||
|
ad647397ef | ||
|
20cf1efd2b | ||
|
026ed556df | ||
|
8125029796 | ||
|
d12b301b19 | ||
|
9d39e91fb0 | ||
|
ed43910d46 | ||
|
1c382d5c09 | ||
|
ab742daada | ||
|
23b1993b42 | ||
|
34a304999c | ||
|
3c91ce8d6b | ||
|
16fcc678e2 | ||
|
422537612f | ||
|
d82447b006 | ||
|
1aceda46f4 | ||
|
0e2d4806b1 | ||
|
251e3196b0 | ||
|
48d96a7033 | ||
|
d2a2a3db26 | ||
|
e2d64bb2fe | ||
|
e68cd31c1d | ||
|
227306d95a | ||
|
92268de97b |
8
.gitignore
vendored
8
.gitignore
vendored
@ -17,6 +17,14 @@ config.log
|
|||||||
.dirstamp
|
.dirstamp
|
||||||
elf2nro
|
elf2nro
|
||||||
elf2nso
|
elf2nso
|
||||||
|
elf2kip
|
||||||
nacptool
|
nacptool
|
||||||
|
npdmtool
|
||||||
nxlink
|
nxlink
|
||||||
*.o
|
*.o
|
||||||
|
*.elf
|
||||||
|
*.kip
|
||||||
|
*.nro
|
||||||
|
*.nso
|
||||||
|
*.npdm
|
||||||
|
*.json
|
||||||
|
16
Makefile.am
16
Makefile.am
@ -1,5 +1,5 @@
|
|||||||
# Makefile.am -- Process this file with automake to produce Makefile.in
|
# 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 npdmtool nxlink
|
||||||
|
|
||||||
build_pfs0_SOURCES = src/build_pfs0.c src/types.h
|
build_pfs0_SOURCES = src/build_pfs0.c src/types.h
|
||||||
|
|
||||||
@ -9,14 +9,22 @@ 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
|
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
|
nacptool_SOURCES = src/nacptool.c
|
||||||
|
|
||||||
|
npdmtool_SOURCES = src/npdmtool.c src/cJSON.c src/cJSON.h
|
||||||
|
|
||||||
nxlink_SOURCES = src/nxlink.c
|
nxlink_SOURCES = src/nxlink.c
|
||||||
|
|
||||||
nxlink_CPPFLAGS = @ZLIB_CFLAGS@
|
nxlink_CPPFLAGS = @ZLIB_CFLAGS@
|
||||||
|
|
||||||
elf2nso_LDADD = @LZ4_LIBS@
|
|
||||||
|
|
||||||
nxlink_LDADD = @ZLIB_LIBS@ @NET_LIBS@
|
nxlink_LDADD = @ZLIB_LIBS@ @NET_LIBS@
|
||||||
|
|
||||||
|
elf2nso_CPPFLAGS = @LZ4_CFLAGS@
|
||||||
|
elf2nso_LDADD = @LZ4_LIBS@
|
||||||
|
|
||||||
|
elf2nro_CPPFLAGS = @LZ4_CFLAGS@
|
||||||
|
elf2nro_LDADD = @LZ4_LIBS@
|
||||||
|
|
||||||
|
|
||||||
EXTRA_DIST = autogen.sh
|
EXTRA_DIST = autogen.sh
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
# Process this file with autoconf to produce a configure script.
|
# Process this file with autoconf to produce a configure script.
|
||||||
|
|
||||||
AC_PREREQ(2.61)
|
AC_PREREQ(2.61)
|
||||||
AC_INIT([switch-tools],[1.3.0],[https://github.com/switchbrew/switch-tools/issues])
|
AC_INIT([switch-tools],[1.13.1],[https://github.com/switchbrew/switch-tools/issues])
|
||||||
AC_CONFIG_SRCDIR([src/build_pfs0.c])
|
AC_CONFIG_SRCDIR([src/build_pfs0.c])
|
||||||
|
|
||||||
AM_INIT_AUTOMAKE([subdir-objects])
|
AM_INIT_AUTOMAKE([subdir-objects])
|
||||||
@ -12,6 +12,7 @@ AC_CANONICAL_HOST
|
|||||||
|
|
||||||
AC_PROG_CC
|
AC_PROG_CC
|
||||||
|
|
||||||
|
AC_SYS_LARGEFILE
|
||||||
|
|
||||||
PKG_CHECK_MODULES([LZ4],
|
PKG_CHECK_MODULES([LZ4],
|
||||||
[liblz4 >= 1.7.1 liblz4 < 100],
|
[liblz4 >= 1.7.1 liblz4 < 100],
|
||||||
@ -30,15 +31,16 @@ NET_LIBS=""
|
|||||||
case "$host" in
|
case "$host" in
|
||||||
*-*-mingw*)
|
*-*-mingw*)
|
||||||
NET_LIBS="-lws2_32"
|
NET_LIBS="-lws2_32"
|
||||||
CFLAGS="$CFLAGS -D__USE_MINGW_ANSI_STDIO"
|
CFLAGS="$CFLAGS -D__USE_MINGW_ANSI_STDIO -D_WIN32_WINNT=0x0600"
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
|
|
||||||
CFLAGS="$CFLAGS -std=gnu99"
|
CFLAGS="$CFLAGS -std=gnu99"
|
||||||
|
|
||||||
|
AC_SUBST(ZLIB_CFLAGS)
|
||||||
AC_SUBST(ZLIB_LIBS)
|
AC_SUBST(ZLIB_LIBS)
|
||||||
AC_SUBST(NET_LIBS)
|
AC_SUBST(NET_LIBS)
|
||||||
AC_SUBST(ZLIB_CFLAGS)
|
AC_SUBST(LZ4_CFLAGS)
|
||||||
AC_SUBST(LZ4_LIBS)
|
AC_SUBST(LZ4_LIBS)
|
||||||
AC_CONFIG_FILES([Makefile])
|
AC_CONFIG_FILES([Makefile])
|
||||||
AC_OUTPUT
|
AC_OUTPUT
|
||||||
|
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 + 15, 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);
|
@ -43,7 +43,7 @@ int build_pfs0(char *in_dirpath, char *out_pfs0_filepath)
|
|||||||
pfs0_fsentry fsentries[MAX_FS_ENTRIES];
|
pfs0_fsentry fsentries[MAX_FS_ENTRIES];
|
||||||
pfs0_fsentry *fsentry;
|
pfs0_fsentry *fsentry;
|
||||||
|
|
||||||
char objpath[256];
|
char objpath[257];
|
||||||
|
|
||||||
char stringtable[0x100];
|
char stringtable[0x100];
|
||||||
|
|
||||||
|
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
|
721
src/elf2kip.c
Normal file
721
src/elf2kip.c
Normal file
@ -0,0 +1,721 @@
|
|||||||
|
// 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 struct {
|
||||||
|
u32 DstOff;
|
||||||
|
u32 DecompSz;
|
||||||
|
u32 CompSz;
|
||||||
|
u32 Attribute;
|
||||||
|
} KipSegment;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
u8 Magic[4];
|
||||||
|
u8 Name[0xC];
|
||||||
|
u64 ProgramId;
|
||||||
|
u32 Version;
|
||||||
|
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_GetString(const cJSON *obj, const char *field, const char **out) {
|
||||||
|
const cJSON *config = cJSON_GetObjectItemCaseSensitive(obj, field);
|
||||||
|
if (cJSON_IsString(config)) {
|
||||||
|
*out = config->valuestring;
|
||||||
|
return 1;
|
||||||
|
} else {
|
||||||
|
fprintf(stderr, "Failed to get %s (field not present).\n", field);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
} else {
|
||||||
|
*out = 0;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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 = strtoull(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 = strtoull(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 cJSON_GetU32(const cJSON *obj, const char *field, u32 *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 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 program_id (or deprecated title_id). */
|
||||||
|
if (!cJSON_GetU64(npdm_json, "program_id", &kip_hdr->ProgramId) && !cJSON_GetU64(npdm_json, "title_id", &kip_hdr->ProgramId)) {
|
||||||
|
status = 0;
|
||||||
|
goto PARSE_CAPS_END;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Parse use secure memory. */
|
||||||
|
/* This field is optional, and defaults to true (set before this function is called). */
|
||||||
|
int use_secure_memory = 1;
|
||||||
|
if (cJSON_GetBooleanOptional(npdm_json, "use_secure_memory", &use_secure_memory)) {
|
||||||
|
if (use_secure_memory) {
|
||||||
|
kip_hdr->Flags |= 0x20;
|
||||||
|
} else {
|
||||||
|
kip_hdr->Flags &= ~0x20;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Parse immortality. */
|
||||||
|
/* This field is optional, and defaults to true (set before this function is called). */
|
||||||
|
int immortal = 1;
|
||||||
|
if (cJSON_GetBooleanOptional(npdm_json, "immortal", &immortal)) {
|
||||||
|
if (immortal) {
|
||||||
|
kip_hdr->Flags |= 0x40;
|
||||||
|
} else {
|
||||||
|
kip_hdr->Flags &= ~0x40;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 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_GetU32(npdm_json, "version", &kip_hdr->Version) && !cJSON_GetU32(npdm_json, "process_category", &kip_hdr->Version)) { // optional
|
||||||
|
kip_hdr->Version = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Parse capabilities. */
|
||||||
|
capabilities = cJSON_GetObjectItemCaseSensitive(npdm_json, "kernel_capabilities");
|
||||||
|
if (!(cJSON_IsArray(capabilities) || cJSON_IsObject(capabilities))) {
|
||||||
|
fprintf(stderr, "Kernel Capabilities must be an array!\n");
|
||||||
|
status = 0;
|
||||||
|
goto PARSE_CAPS_END;
|
||||||
|
}
|
||||||
|
|
||||||
|
int kac_obj = 0;
|
||||||
|
if (cJSON_IsObject(capabilities)) {
|
||||||
|
kac_obj = 1;
|
||||||
|
fprintf(stderr, "Using deprecated kernel_capabilities format. Please turn it into an array.\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
u32 cur_cap = 0;
|
||||||
|
u32 desc;
|
||||||
|
cJSON_ArrayForEach(capability, capabilities) {
|
||||||
|
desc = 0;
|
||||||
|
const char *type_str;
|
||||||
|
const cJSON *value;
|
||||||
|
|
||||||
|
if (kac_obj) {
|
||||||
|
type_str = capability->string;
|
||||||
|
value = capability;
|
||||||
|
} else {
|
||||||
|
if (!cJSON_GetString(capability, "type", &type_str)) {
|
||||||
|
status = 0;
|
||||||
|
goto PARSE_CAPS_END;
|
||||||
|
}
|
||||||
|
value = cJSON_GetObjectItemCaseSensitive(capability, "value");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
u8 real_highest_prio = (lowest_prio < highest_prio) ? lowest_prio : highest_prio;
|
||||||
|
u8 real_lowest_prio = (lowest_prio > highest_prio) ? lowest_prio : highest_prio;
|
||||||
|
|
||||||
|
desc = highest_cpu;
|
||||||
|
desc <<= 8;
|
||||||
|
desc |= lowest_cpu;
|
||||||
|
desc <<= 6;
|
||||||
|
desc |= (real_highest_prio & 0x3F);
|
||||||
|
desc <<= 6;
|
||||||
|
desc |= (real_lowest_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[8] = {0}; /* alignup(0xC0/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 >= 0xC0) {
|
||||||
|
fprintf(stderr, "Error: All syscall entries must be numbers in [0, 0xBF]\n");
|
||||||
|
status = 0;
|
||||||
|
goto PARSE_CAPS_END;
|
||||||
|
}
|
||||||
|
descriptors[syscall_value / 0x18] |= (1UL << (syscall_value % 0x18));
|
||||||
|
}
|
||||||
|
for (unsigned int i = 0; i < 8; 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) & 0x000FFFFFULL);
|
||||||
|
desc |= (u32)(((map_address >> 36) & 0xFULL) << 20);
|
||||||
|
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, "map_region")) {
|
||||||
|
if (cur_cap + 1 > 0x20) {
|
||||||
|
fprintf(stderr, "Error: Too many capabilities!\n");
|
||||||
|
status = 0;
|
||||||
|
goto PARSE_CAPS_END;
|
||||||
|
}
|
||||||
|
if (!cJSON_IsArray(value)) {
|
||||||
|
fprintf(stderr, "Map Region capability value must be array!\n");
|
||||||
|
status = 0;
|
||||||
|
goto PARSE_CAPS_END;
|
||||||
|
}
|
||||||
|
u8 regions[3] = {0};
|
||||||
|
int is_ro[3] = {0};
|
||||||
|
const cJSON *cur_region = NULL;
|
||||||
|
int index = 0;
|
||||||
|
cJSON_ArrayForEach(cur_region, value) {
|
||||||
|
if (index >= 3) {
|
||||||
|
fprintf(stderr, "Too many region descriptors!\n");
|
||||||
|
status = 0;
|
||||||
|
goto PARSE_CAPS_END;
|
||||||
|
}
|
||||||
|
if (!cJSON_IsObject(cur_region)) {
|
||||||
|
fprintf(stderr, "Region descriptor value must be object!\n");
|
||||||
|
status = 0;
|
||||||
|
goto PARSE_CAPS_END;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!cJSON_GetU8(cur_region, "region_type", ®ions[index]) ||
|
||||||
|
!cJSON_GetBoolean(cur_region, "is_ro", &is_ro[index])) {
|
||||||
|
status = 0;
|
||||||
|
goto PARSE_CAPS_END;
|
||||||
|
}
|
||||||
|
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
|
||||||
|
u32 capability = 0x3FF;
|
||||||
|
for (int i = 0; i < 3; ++i) {
|
||||||
|
capability |= ((regions[i] & 0x3F) | ((is_ro[i] & 1) << 6)) << (11 + 7 * i);
|
||||||
|
}
|
||||||
|
kip_hdr->Capabilities[cur_cap++] = capability;
|
||||||
|
} 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;
|
||||||
|
int desc_idx = 0;
|
||||||
|
cJSON_ArrayForEach(irq, value) {
|
||||||
|
if (cJSON_IsNull(irq)) {
|
||||||
|
desc |= 0x3FF << desc_idx;
|
||||||
|
} else if (cJSON_IsNumber(irq)) {
|
||||||
|
desc |= (((u16)(irq->valueint)) & 0x3FF) << desc_idx;
|
||||||
|
} else {
|
||||||
|
fprintf(stderr, "Failed to parse IRQ value.\n");
|
||||||
|
status = 0;
|
||||||
|
goto PARSE_CAPS_END;
|
||||||
|
}
|
||||||
|
desc_idx += 10;
|
||||||
|
}
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
u64 kern_ver = 0;
|
||||||
|
if (cJSON_IsNumber(value)) {
|
||||||
|
kern_ver = (u64)value->valueint;
|
||||||
|
} else if (!cJSON_IsString(value) || !cJSON_GetU64FromObjectValue(value, &kern_ver)) {
|
||||||
|
fprintf(stderr, "Error: Kernel version must be integer or hex strings.\n");
|
||||||
|
status = 0;
|
||||||
|
goto PARSE_CAPS_END;
|
||||||
|
}
|
||||||
|
desc = (kern_ver) & 0xFFFF;
|
||||||
|
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;
|
||||||
|
int force_debug_prod = 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;
|
||||||
|
}
|
||||||
|
if (!cJSON_GetBoolean(value, "force_debug_prod", &force_debug_prod)) {
|
||||||
|
status = 0;
|
||||||
|
goto PARSE_CAPS_END;
|
||||||
|
}
|
||||||
|
desc = (allow_debug & 1) | ((force_debug_prod & 1) << 1) | ((force_debug & 1) << 2);
|
||||||
|
kip_hdr->Capabilities[cur_cap++] = (u32)((desc << 17) | (0xFFFF));
|
||||||
|
} else {
|
||||||
|
fprintf(stderr, "Error: unknown capability %s\n", type_str);
|
||||||
|
status = 0;
|
||||||
|
goto PARSE_CAPS_END;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (u32 i = cur_cap; i < 0x20; i++) {
|
||||||
|
kip_hdr->Capabilities[i] = 0xFFFFFFFF;
|
||||||
|
}
|
||||||
|
|
||||||
|
status = 1;
|
||||||
|
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 = 0x7F;
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!ParseKipConfiguration(json, &kip_hdr)) {
|
||||||
|
fprintf(stderr, "Failed to parse kip configuration!\n");
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
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].CompSz = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
FileOffsets[i] = file_off;
|
||||||
|
kip_hdr.Segments[i].DecompSz = phdr->p_filesz;
|
||||||
|
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) + FileOffsets[i], 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;
|
||||||
|
}
|
@ -8,10 +8,6 @@
|
|||||||
#include "elf64.h"
|
#include "elf64.h"
|
||||||
#include "romfs.h"
|
#include "romfs.h"
|
||||||
|
|
||||||
typedef uint64_t u64;
|
|
||||||
typedef uint32_t u32;
|
|
||||||
typedef uint8_t u8;
|
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
u32 FileOff;
|
u32 FileOff;
|
||||||
u32 Size;
|
u32 Size;
|
||||||
@ -25,9 +21,9 @@ typedef struct {
|
|||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
u8 Magic[4];
|
u8 Magic[4];
|
||||||
u32 Unk1;
|
u32 version;
|
||||||
u32 size;
|
u32 size;
|
||||||
u32 Unk2;
|
u32 flags;
|
||||||
NsoSegment Segments[3];
|
NsoSegment Segments[3];
|
||||||
u32 bssSize;
|
u32 bssSize;
|
||||||
u32 Unk3;
|
u32 Unk3;
|
||||||
@ -82,6 +78,7 @@ int main(int argc, char* argv[]) {
|
|||||||
fprintf(stderr, "--nacp=<control.nacp> Embeds control.nacp into the output file.\n");
|
fprintf(stderr, "--nacp=<control.nacp> Embeds control.nacp into the output file.\n");
|
||||||
fprintf(stderr, "--romfs=<image> Embeds RomFS into the output file.\n");
|
fprintf(stderr, "--romfs=<image> Embeds RomFS into the output file.\n");
|
||||||
fprintf(stderr, "--romfsdir=<directory> Builds and embeds RomFS into the output file.\n");
|
fprintf(stderr, "--romfsdir=<directory> Builds and embeds RomFS into the output file.\n");
|
||||||
|
fprintf(stderr, "--alignedheader Sets the \"AlignedHeader\" flag in the output file.\n");
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -106,11 +103,13 @@ int main(int argc, char* argv[]) {
|
|||||||
|
|
||||||
int argi;
|
int argi;
|
||||||
char* icon_path = NULL, *nacp_path = NULL, *romfs_path = NULL, *romfs_dir_path = NULL;
|
char* icon_path = NULL, *nacp_path = NULL, *romfs_path = NULL, *romfs_dir_path = NULL;
|
||||||
|
u32 aligned_header = 0;
|
||||||
for (argi=3; argi<argc; argi++) {
|
for (argi=3; argi<argc; argi++) {
|
||||||
if (strncmp(argv[argi], "--icon=", 7)==0) icon_path = &argv[argi][7];
|
if (strncmp(argv[argi], "--icon=", 7)==0) icon_path = &argv[argi][7];
|
||||||
if (strncmp(argv[argi], "--nacp=", 7)==0) nacp_path = &argv[argi][7];
|
if (strncmp(argv[argi], "--nacp=", 7)==0) nacp_path = &argv[argi][7];
|
||||||
if (strncmp(argv[argi], "--romfs=", 8)==0) romfs_path = &argv[argi][8];
|
if (strncmp(argv[argi], "--romfs=", 8)==0) romfs_path = &argv[argi][8];
|
||||||
if (strncmp(argv[argi], "--romfsdir=", 11)==0) romfs_dir_path = &argv[argi][11];
|
if (strncmp(argv[argi], "--romfsdir=", 11)==0) romfs_dir_path = &argv[argi][11];
|
||||||
|
if (strncmp(argv[argi], "--alignedheader", 15)==0) aligned_header = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (romfs_dir_path != NULL && romfs_path != NULL) {
|
if (romfs_dir_path != NULL && romfs_path != NULL) {
|
||||||
@ -185,6 +184,25 @@ int main(int argc, char* argv[]) {
|
|||||||
file_off = (file_off + 0xFFF) & ~0xFFF;
|
file_off = (file_off + 0xFFF) & ~0xFFF;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Iterate over sections to find build id. */
|
||||||
|
size_t cur_sect_hdr_ofs = hdr->e_shoff;
|
||||||
|
for (unsigned int i = 0; i < hdr->e_shnum; i++) {
|
||||||
|
Elf64_Shdr *cur_shdr = (Elf64_Shdr *)(elf + cur_sect_hdr_ofs);
|
||||||
|
if (cur_shdr->sh_type == SHT_NOTE) {
|
||||||
|
Elf64_Nhdr *note_hdr = (Elf64_Nhdr *)(elf + cur_shdr->sh_offset);
|
||||||
|
u8 *note_name = (u8 *)((uintptr_t)note_hdr + sizeof(Elf64_Nhdr));
|
||||||
|
u8 *note_desc = note_name + note_hdr->n_namesz;
|
||||||
|
if (note_hdr->n_type == NT_GNU_BUILD_ID && note_hdr->n_namesz == 4 && memcmp(note_name, "GNU\x00", 4) == 0) {
|
||||||
|
size_t build_id_size = note_hdr->n_descsz;
|
||||||
|
if (build_id_size > 0x20) {
|
||||||
|
build_id_size = 0x20;
|
||||||
|
}
|
||||||
|
memcpy(nro_hdr.BuildId, note_desc, build_id_size);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cur_sect_hdr_ofs += hdr->e_shentsize;
|
||||||
|
}
|
||||||
|
|
||||||
FILE* out = fopen(argv[2], "wb");
|
FILE* out = fopen(argv[2], "wb");
|
||||||
|
|
||||||
if (out == NULL) {
|
if (out == NULL) {
|
||||||
@ -194,6 +212,9 @@ int main(int argc, char* argv[]) {
|
|||||||
|
|
||||||
nro_hdr.size = file_off;
|
nro_hdr.size = file_off;
|
||||||
|
|
||||||
|
nro_hdr.version = 0;
|
||||||
|
nro_hdr.flags = (aligned_header << 0);
|
||||||
|
|
||||||
// TODO check retvals
|
// TODO check retvals
|
||||||
|
|
||||||
for (i=0; i<3; i++)
|
for (i=0; i<3; i++)
|
||||||
|
@ -158,6 +158,25 @@ int main(int argc, char* argv[]) {
|
|||||||
file_off += comp_sz[i];
|
file_off += comp_sz[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Iterate over sections to find build id. */
|
||||||
|
size_t cur_sect_hdr_ofs = hdr->e_shoff;
|
||||||
|
for (unsigned int i = 0; i < hdr->e_shnum; i++) {
|
||||||
|
Elf64_Shdr *cur_shdr = (Elf64_Shdr *)(elf + cur_sect_hdr_ofs);
|
||||||
|
if (cur_shdr->sh_type == SHT_NOTE) {
|
||||||
|
Elf64_Nhdr *note_hdr = (Elf64_Nhdr *)(elf + cur_shdr->sh_offset);
|
||||||
|
u8 *note_name = (u8 *)((uintptr_t)note_hdr + sizeof(Elf64_Nhdr));
|
||||||
|
u8 *note_desc = note_name + note_hdr->n_namesz;
|
||||||
|
if (note_hdr->n_type == NT_GNU_BUILD_ID && note_hdr->n_namesz == 4 && memcmp(note_name, "GNU\x00", 4) == 0) {
|
||||||
|
size_t build_id_size = note_hdr->n_descsz;
|
||||||
|
if (build_id_size > 0x20) {
|
||||||
|
build_id_size = 0x20;
|
||||||
|
}
|
||||||
|
memcpy(nso_hdr.BuildId, note_desc, build_id_size);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cur_sect_hdr_ofs += hdr->e_shentsize;
|
||||||
|
}
|
||||||
|
|
||||||
FILE* out = fopen(argv[2], "wb");
|
FILE* out = fopen(argv[2], "wb");
|
||||||
|
|
||||||
if (out == NULL) {
|
if (out == NULL) {
|
||||||
|
@ -995,5 +995,6 @@ typedef struct {
|
|||||||
#define R_X86_64_TPOFF32 23 /* Offset in static TLS block */
|
#define R_X86_64_TPOFF32 23 /* Offset in static TLS block */
|
||||||
#define R_X86_64_IRELATIVE 37
|
#define R_X86_64_IRELATIVE 37
|
||||||
|
|
||||||
|
#define NT_GNU_BUILD_ID 3 /* Note type for .note.gnu.build-id */
|
||||||
|
|
||||||
#endif /* !_SYS_ELF_COMMON_H_ */
|
#endif /* !_SYS_ELF_COMMON_H_ */
|
||||||
|
@ -7,8 +7,6 @@
|
|||||||
#include <wchar.h>
|
#include <wchar.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define __USE_LARGEFILE64
|
|
||||||
|
|
||||||
#define MAX_SWITCHPATH 0x300
|
#define MAX_SWITCHPATH 0x300
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
@ -22,16 +20,9 @@ inline int fseeko64(FILE *__stream, long long __off, int __whence)
|
|||||||
{
|
{
|
||||||
return _fseeki64(__stream, __off, __whence);
|
return _fseeki64(__stream, __off, __whence);
|
||||||
}
|
}
|
||||||
#elif __APPLE__ || __CYGWIN__
|
|
||||||
// OS X file I/O is 64bit
|
|
||||||
#define fseeko64 fseek
|
|
||||||
#elif __linux__
|
|
||||||
extern int fseeko64 (FILE *__stream, __off64_t __off, int __whence);
|
|
||||||
#else
|
#else
|
||||||
/* fseeko is guaranteed by POSIX, hopefully the OS made their off_t definition 64-bit;
|
/* off_t is 64-bit with large file support */
|
||||||
* known sane on FreeBSD and OpenBSD.
|
#define fseeko64 fseek
|
||||||
*/
|
|
||||||
#define fseeko64 fseeko
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
@ -42,6 +33,7 @@ typedef struct _stati64 os_stat64_t;
|
|||||||
|
|
||||||
#define os_fopen _wfopen
|
#define os_fopen _wfopen
|
||||||
#define os_opendir _wopendir
|
#define os_opendir _wopendir
|
||||||
|
#define os_closedir _wclosedir
|
||||||
#define os_readdir _wreaddir
|
#define os_readdir _wreaddir
|
||||||
#define os_stat _wstati64
|
#define os_stat _wstati64
|
||||||
#define os_fclose fclose
|
#define os_fclose fclose
|
||||||
@ -53,12 +45,13 @@ typedef struct _stati64 os_stat64_t;
|
|||||||
typedef char oschar_t; /* utf-8 */
|
typedef char oschar_t; /* utf-8 */
|
||||||
typedef DIR osdir_t;
|
typedef DIR osdir_t;
|
||||||
typedef struct dirent osdirent_t;
|
typedef struct dirent osdirent_t;
|
||||||
typedef struct stat64 os_stat64_t;
|
typedef struct stat os_stat64_t;
|
||||||
|
|
||||||
#define os_fopen fopen
|
#define os_fopen fopen
|
||||||
#define os_opendir opendir
|
#define os_opendir opendir
|
||||||
|
#define os_closedir closedir
|
||||||
#define os_readdir readdir
|
#define os_readdir readdir
|
||||||
#define os_stat stat64
|
#define os_stat stat
|
||||||
#define os_fclose fclose
|
#define os_fclose fclose
|
||||||
|
|
||||||
#define OS_MODE_READ "rb"
|
#define OS_MODE_READ "rb"
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <inttypes.h>
|
||||||
|
|
||||||
typedef uint64_t u64;
|
typedef uint64_t u64;
|
||||||
typedef uint32_t u32;
|
typedef uint32_t u32;
|
||||||
@ -79,7 +80,7 @@ int main(int argc, char* argv[]) {
|
|||||||
int argi;
|
int argi;
|
||||||
u64 titleid=0;
|
u64 titleid=0;
|
||||||
for (argi=6; argi<argc; argi++) {
|
for (argi=6; argi<argc; argi++) {
|
||||||
if (strncmp(argv[argi], "--titleid=", 10)==0) sscanf(&argv[argi][10], "%016lX", &titleid);
|
if (strncmp(argv[argi], "--titleid=", 10)==0) sscanf(&argv[argi][10], "%016" SCNx64, &titleid);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i=0; i<12; i++) {//These are UTF-8.
|
for (i=0; i<12; i++) {//These are UTF-8.
|
||||||
|
913
src/npdmtool.c
Normal file
913
src/npdmtool.c
Normal file
@ -0,0 +1,913 @@
|
|||||||
|
// Copyright 2018 SciresM
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <limits.h>
|
||||||
|
#include "cJSON.h"
|
||||||
|
|
||||||
|
typedef uint64_t u64;
|
||||||
|
typedef uint32_t u32;
|
||||||
|
typedef uint16_t u16;
|
||||||
|
typedef uint8_t u8;
|
||||||
|
|
||||||
|
#define MAGIC_META 0x4154454D
|
||||||
|
#define MAGIC_ACID 0x44494341
|
||||||
|
#define MAGIC_ACI0 0x30494341
|
||||||
|
|
||||||
|
/* FAC, FAH need to be tightly packed. */
|
||||||
|
#pragma pack(push, 1)
|
||||||
|
typedef struct {
|
||||||
|
u8 Version;
|
||||||
|
u8 CoiCount;
|
||||||
|
u8 SdoiCount;
|
||||||
|
u8 pad;
|
||||||
|
u64 Perms;
|
||||||
|
u64 CoiMin;
|
||||||
|
u64 CoiMax;
|
||||||
|
u64 SdoiMin;
|
||||||
|
u64 SdoiMax;
|
||||||
|
} FilesystemAccessControl;
|
||||||
|
#pragma pack(pop)
|
||||||
|
|
||||||
|
#pragma pack(push, 1)
|
||||||
|
typedef struct {
|
||||||
|
u32 Version;
|
||||||
|
u64 Perms;
|
||||||
|
u32 CoiOffset;
|
||||||
|
u32 CoiSize;
|
||||||
|
u32 SdoiOffset;
|
||||||
|
u32 SdoiSize;
|
||||||
|
} FilesystemAccessHeader;
|
||||||
|
#pragma pack(pop)
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
u32 Magic;
|
||||||
|
u8 _0x4[0xC];
|
||||||
|
u64 ProgramId;
|
||||||
|
u64 _0x18;
|
||||||
|
u32 FahOffset;
|
||||||
|
u32 FahSize;
|
||||||
|
u32 SacOffset;
|
||||||
|
u32 SacSize;
|
||||||
|
u32 KacOffset;
|
||||||
|
u32 KacSize;
|
||||||
|
u64 Padding;
|
||||||
|
} NpdmAci0;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
u8 Signature[0x100];
|
||||||
|
u8 Modulus[0x100];
|
||||||
|
u32 Magic;
|
||||||
|
u32 Size;
|
||||||
|
u32 _0x208;
|
||||||
|
u32 Flags;
|
||||||
|
u64 ProgramIdRangeMin;
|
||||||
|
u64 ProgramIdRangeMax;
|
||||||
|
u32 FacOffset;
|
||||||
|
u32 FacSize;
|
||||||
|
u32 SacOffset;
|
||||||
|
u32 SacSize;
|
||||||
|
u32 KacOffset;
|
||||||
|
u32 KacSize;
|
||||||
|
u64 Padding;
|
||||||
|
} NpdmAcid;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
u32 Magic;
|
||||||
|
u32 SignatureKeyGeneration;
|
||||||
|
u32 _0x8;
|
||||||
|
u8 MmuFlags;
|
||||||
|
u8 _0xD;
|
||||||
|
u8 MainThreadPriority;
|
||||||
|
u8 DefaultCpuId;
|
||||||
|
u32 _0x10;
|
||||||
|
u32 SystemResourceSize;
|
||||||
|
u32 Version;
|
||||||
|
u32 MainThreadStackSize;
|
||||||
|
char Name[0x10];
|
||||||
|
char ProductCode[0x10];
|
||||||
|
u8 _0x40[0x30];
|
||||||
|
u32 Aci0Offset;
|
||||||
|
u32 Aci0Size;
|
||||||
|
u32 AcidOffset;
|
||||||
|
u32 AcidSize;
|
||||||
|
} NpdmHeader;
|
||||||
|
|
||||||
|
|
||||||
|
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_GetString(const cJSON *obj, const char *field, const char **out) {
|
||||||
|
const cJSON *config = cJSON_GetObjectItemCaseSensitive(obj, field);
|
||||||
|
if (cJSON_IsString(config)) {
|
||||||
|
*out = config->valuestring;
|
||||||
|
return 1;
|
||||||
|
} else {
|
||||||
|
fprintf(stderr, "Failed to get %s (field not present).\n", field);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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 = strtoull(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_GetU32(const cJSON *obj, const char *field, u32 *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 = strtoull(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 CreateNpdm(const char *json, void **dst, u32 *dst_size) {
|
||||||
|
NpdmHeader header = {0};
|
||||||
|
NpdmAci0 *aci0 = calloc(1, 0x100000);
|
||||||
|
NpdmAcid *acid = calloc(1, 0x100000);
|
||||||
|
if (aci0 == NULL || acid == NULL) {
|
||||||
|
fprintf(stderr, "Failed to allocate NPDM resources!\n");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
const cJSON *capability = NULL;
|
||||||
|
const cJSON *capabilities = NULL;
|
||||||
|
const cJSON *service = NULL;
|
||||||
|
const cJSON *services = NULL;
|
||||||
|
const cJSON *fsaccess = NULL;
|
||||||
|
const cJSON *cois = NULL;
|
||||||
|
const cJSON *coi = NULL;
|
||||||
|
const cJSON *sdois = NULL;
|
||||||
|
const cJSON *sdoi = 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 NPDM_BUILD_END;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Initialize default NPDM values. */
|
||||||
|
header.Magic = MAGIC_META; /* "META" */
|
||||||
|
|
||||||
|
|
||||||
|
/* Parse name. */
|
||||||
|
const cJSON *title_name = cJSON_GetObjectItemCaseSensitive(npdm_json, "name");
|
||||||
|
if (cJSON_IsString(title_name) && (title_name->valuestring != NULL)) {
|
||||||
|
strncpy(header.Name, title_name->valuestring, sizeof(header.Name) - 1);
|
||||||
|
} else {
|
||||||
|
fprintf(stderr, "Failed to get title name (name field not present).\n");
|
||||||
|
status = 0;
|
||||||
|
goto NPDM_BUILD_END;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Parse main_thread_stack_size. */
|
||||||
|
u64 stack_size = 0;
|
||||||
|
if (!cJSON_GetU64(npdm_json, "main_thread_stack_size", &stack_size)) {
|
||||||
|
status = 0;
|
||||||
|
goto NPDM_BUILD_END;
|
||||||
|
}
|
||||||
|
if (stack_size >> 32) {
|
||||||
|
fprintf(stderr, "Error: Main thread stack size must be a u32!\n");
|
||||||
|
status = 0;
|
||||||
|
goto NPDM_BUILD_END;
|
||||||
|
}
|
||||||
|
header.MainThreadStackSize = (u32)(stack_size & 0xFFFFFFFF);
|
||||||
|
|
||||||
|
/* Parse various config. */
|
||||||
|
if (!cJSON_GetU8(npdm_json, "main_thread_priority", &header.MainThreadPriority)) {
|
||||||
|
status = 0;
|
||||||
|
goto NPDM_BUILD_END;
|
||||||
|
}
|
||||||
|
if (!cJSON_GetU8(npdm_json, "default_cpu_id", &header.DefaultCpuId)) {
|
||||||
|
status = 0;
|
||||||
|
goto NPDM_BUILD_END;
|
||||||
|
}
|
||||||
|
|
||||||
|
cJSON_GetU32(npdm_json, "system_resource_size", &header.SystemResourceSize); // optional
|
||||||
|
|
||||||
|
/* Get version (deprecated name "process_category"). */
|
||||||
|
if (!cJSON_GetU32(npdm_json, "version", &header.Version) && !cJSON_GetU32(npdm_json, "process_category", &header.Version)) { // optional
|
||||||
|
header.Version = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!cJSON_GetU8(npdm_json, "address_space_type", (u8 *)&header.MmuFlags)) {
|
||||||
|
status = 0;
|
||||||
|
goto NPDM_BUILD_END;
|
||||||
|
}
|
||||||
|
header.MmuFlags &= 3;
|
||||||
|
header.MmuFlags <<= 1;
|
||||||
|
int is_64_bit;
|
||||||
|
if (!cJSON_GetBoolean(npdm_json, "is_64_bit", &is_64_bit)) {
|
||||||
|
status = 0;
|
||||||
|
goto NPDM_BUILD_END;
|
||||||
|
}
|
||||||
|
header.MmuFlags |= is_64_bit;
|
||||||
|
|
||||||
|
int optimize_memory_allocation; // optional
|
||||||
|
if (cJSON_GetBoolean(npdm_json, "optimize_memory_allocation", &optimize_memory_allocation)) {
|
||||||
|
header.MmuFlags |= ((optimize_memory_allocation & 1) << 4);
|
||||||
|
}
|
||||||
|
|
||||||
|
int disable_device_address_space_merge; // optional
|
||||||
|
if (cJSON_GetBoolean(npdm_json, "disable_device_address_space_merge", &disable_device_address_space_merge)) {
|
||||||
|
header.MmuFlags |= ((disable_device_address_space_merge & 1) << 5);
|
||||||
|
}
|
||||||
|
|
||||||
|
int enable_alias_region_extra_size; // optional
|
||||||
|
if (cJSON_GetBoolean(npdm_json, "enable_alias_region_extra_size", &enable_alias_region_extra_size)) {
|
||||||
|
header.MmuFlags |= ((enable_alias_region_extra_size & 1) << 6);
|
||||||
|
}
|
||||||
|
|
||||||
|
int prevent_code_reads; // optional
|
||||||
|
if (cJSON_GetBoolean(npdm_json, "prevent_code_reads", &prevent_code_reads)) {
|
||||||
|
header.MmuFlags |= ((prevent_code_reads & 1) << 7);
|
||||||
|
}
|
||||||
|
|
||||||
|
u8 signature_key_generation; // optional
|
||||||
|
if (cJSON_GetU8(npdm_json, "signature_key_generation", &signature_key_generation)) {
|
||||||
|
header.SignatureKeyGeneration = signature_key_generation;
|
||||||
|
} else {
|
||||||
|
header.SignatureKeyGeneration = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ACID. */
|
||||||
|
memset(acid->Signature, 0, sizeof(acid->Signature));
|
||||||
|
memset(acid->Modulus, 0, sizeof(acid->Modulus));
|
||||||
|
acid->Magic = MAGIC_ACID; /* "ACID" */
|
||||||
|
int is_retail;
|
||||||
|
if (!cJSON_GetBoolean(npdm_json, "is_retail", &is_retail)) {
|
||||||
|
status = 0;
|
||||||
|
goto NPDM_BUILD_END;
|
||||||
|
}
|
||||||
|
acid->Flags |= is_retail;
|
||||||
|
u8 pool_partition;
|
||||||
|
if (!cJSON_GetU8(npdm_json, "pool_partition", &pool_partition)) {
|
||||||
|
status = 0;
|
||||||
|
goto NPDM_BUILD_END;
|
||||||
|
}
|
||||||
|
acid->Flags |= (pool_partition & 3) << 2;
|
||||||
|
|
||||||
|
if (!cJSON_GetU64(npdm_json, "program_id_range_min", &acid->ProgramIdRangeMin) && !cJSON_GetU64(npdm_json, "title_id_range_min", &acid->ProgramIdRangeMin)) {
|
||||||
|
status = 0;
|
||||||
|
goto NPDM_BUILD_END;
|
||||||
|
}
|
||||||
|
if (!cJSON_GetU64(npdm_json, "program_id_range_max", &acid->ProgramIdRangeMax) && !cJSON_GetU64(npdm_json, "title_id_range_max", &acid->ProgramIdRangeMax)) {
|
||||||
|
status = 0;
|
||||||
|
goto NPDM_BUILD_END;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ACI0. */
|
||||||
|
aci0->Magic = MAGIC_ACI0; /* "ACI0" */
|
||||||
|
/* Parse program_id (or deprecated title_id). */
|
||||||
|
if (!cJSON_GetU64(npdm_json, "program_id", &aci0->ProgramId) && !cJSON_GetU64(npdm_json, "title_id", &aci0->ProgramId)) {
|
||||||
|
status = 0;
|
||||||
|
goto NPDM_BUILD_END;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Fac. */
|
||||||
|
fsaccess = cJSON_GetObjectItemCaseSensitive(npdm_json, "filesystem_access");
|
||||||
|
if (!cJSON_IsObject(fsaccess)) {
|
||||||
|
fprintf(stderr, "Filesystem Access must be an object!\n");
|
||||||
|
status = 0;
|
||||||
|
goto NPDM_BUILD_END;
|
||||||
|
}
|
||||||
|
|
||||||
|
FilesystemAccessControl *fac = (FilesystemAccessControl *)((u8 *)acid + sizeof(NpdmAcid));
|
||||||
|
fac->Version = 1;
|
||||||
|
if (!cJSON_GetU64(fsaccess, "permissions", &fac->Perms)) {
|
||||||
|
status = 0;
|
||||||
|
goto NPDM_BUILD_END;
|
||||||
|
}
|
||||||
|
|
||||||
|
fac->CoiMin = 0;
|
||||||
|
fac->CoiMax = 0;
|
||||||
|
fac->SdoiMin = 0;
|
||||||
|
fac->SdoiMax = 0;
|
||||||
|
fac->CoiCount = 0;
|
||||||
|
fac->SdoiCount = 0;
|
||||||
|
|
||||||
|
acid->FacOffset = sizeof(NpdmAcid);
|
||||||
|
acid->FacSize = sizeof(FilesystemAccessControl);
|
||||||
|
acid->SacOffset = (acid->FacOffset + acid->FacSize + 0xF) & ~0xF;
|
||||||
|
|
||||||
|
/* Fah. */
|
||||||
|
FilesystemAccessHeader *fah = (FilesystemAccessHeader *)((u8 *)aci0 + sizeof(NpdmAci0));
|
||||||
|
fah->Version = 1;
|
||||||
|
fah->Perms = fac->Perms;
|
||||||
|
fah->CoiOffset = sizeof(FilesystemAccessHeader);
|
||||||
|
fah->CoiSize = 0;
|
||||||
|
|
||||||
|
cois = cJSON_GetObjectItemCaseSensitive(fsaccess, "content_owner_ids");
|
||||||
|
if (cJSON_IsArray(cois)) {
|
||||||
|
u32 *count = (u32 *)((u8 *)fah + fah->CoiOffset);
|
||||||
|
u64 *id = (u64 *)((u8 *)count + sizeof(u32));
|
||||||
|
cJSON_ArrayForEach(coi, cois) {
|
||||||
|
if (!cJSON_GetU64FromObjectValue(coi, id)) {
|
||||||
|
status = 0;
|
||||||
|
goto NPDM_BUILD_END;
|
||||||
|
}
|
||||||
|
++id;
|
||||||
|
++(*count);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (*count > 0) {
|
||||||
|
fah->CoiSize = sizeof(u32) + sizeof(u64) * (*count);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fah->SdoiOffset = fah->CoiOffset + fah->CoiSize;
|
||||||
|
fah->SdoiSize = 0;
|
||||||
|
|
||||||
|
sdois = cJSON_GetObjectItemCaseSensitive(fsaccess, "save_data_owner_ids");
|
||||||
|
if (cJSON_IsArray(sdois)) {
|
||||||
|
u32 *count = (u32 *)((u8 *)fah + fah->SdoiOffset);
|
||||||
|
cJSON_ArrayForEach(sdoi, sdois) {
|
||||||
|
if (!cJSON_IsObject(sdoi)) {
|
||||||
|
status = 0;
|
||||||
|
goto NPDM_BUILD_END;
|
||||||
|
}
|
||||||
|
++(*count);
|
||||||
|
}
|
||||||
|
|
||||||
|
u8 *accessibility = (u8 *)count + sizeof(u32);
|
||||||
|
u64 *id = (u64 *)(accessibility + (((*count) + 3ULL) & ~3ULL));
|
||||||
|
|
||||||
|
cJSON_ArrayForEach(sdoi, sdois) {
|
||||||
|
if (!cJSON_GetU8(sdoi, "accessibility", accessibility)) {
|
||||||
|
status = 0;
|
||||||
|
goto NPDM_BUILD_END;
|
||||||
|
}
|
||||||
|
if (!cJSON_GetU64(sdoi, "id", id)) {
|
||||||
|
status = 0;
|
||||||
|
goto NPDM_BUILD_END;
|
||||||
|
}
|
||||||
|
|
||||||
|
++accessibility;
|
||||||
|
++id;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (*count > 0) {
|
||||||
|
fah->SdoiSize = sizeof(u32) + sizeof(u8) * ((((*count) + 3ULL) & ~3ULL)) + sizeof(u64) * (*count);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
aci0->FahOffset = sizeof(NpdmAci0);
|
||||||
|
aci0->FahSize = sizeof(FilesystemAccessHeader) + fah->CoiSize + fah->SdoiSize;
|
||||||
|
aci0->SacOffset = (aci0->FahOffset + aci0->FahSize + 0xF) & ~0xF;
|
||||||
|
|
||||||
|
/* Sac. */
|
||||||
|
u8 *sac = (u8*)aci0 + aci0->SacOffset;
|
||||||
|
u32 sac_size = 0;
|
||||||
|
|
||||||
|
services = cJSON_GetObjectItemCaseSensitive(npdm_json, "service_host");
|
||||||
|
if (services != NULL && !cJSON_IsArray(services)) {
|
||||||
|
fprintf(stderr, "Service Host must be an array!\n");
|
||||||
|
status = 0;
|
||||||
|
goto NPDM_BUILD_END;
|
||||||
|
}
|
||||||
|
|
||||||
|
cJSON_ArrayForEach(service, services) {
|
||||||
|
int is_host = 1;
|
||||||
|
char *service_name;
|
||||||
|
|
||||||
|
if (!cJSON_IsString(service)) {
|
||||||
|
fprintf(stderr, "service_access must be an array of string\n");
|
||||||
|
status = 0;
|
||||||
|
goto NPDM_BUILD_END;
|
||||||
|
}
|
||||||
|
service_name = service->valuestring;
|
||||||
|
|
||||||
|
int cur_srv_len = strlen(service_name);
|
||||||
|
if (cur_srv_len > 8 || cur_srv_len == 0) {
|
||||||
|
fprintf(stderr, "Services must have name length 1 <= len <= 8!\n");
|
||||||
|
status = 0;
|
||||||
|
goto NPDM_BUILD_END;
|
||||||
|
}
|
||||||
|
u8 ctrl = (u8)(cur_srv_len - 1);
|
||||||
|
if (is_host) {
|
||||||
|
ctrl |= 0x80;
|
||||||
|
}
|
||||||
|
sac[sac_size++] = ctrl;
|
||||||
|
memcpy(sac + sac_size, service_name, cur_srv_len);
|
||||||
|
sac_size += cur_srv_len;
|
||||||
|
}
|
||||||
|
|
||||||
|
services = cJSON_GetObjectItemCaseSensitive(npdm_json, "service_access");
|
||||||
|
if (!(services == NULL || cJSON_IsObject(services) || cJSON_IsArray(services))) {
|
||||||
|
fprintf(stderr, "Service Access must be an array!\n");
|
||||||
|
status = 0;
|
||||||
|
goto NPDM_BUILD_END;
|
||||||
|
}
|
||||||
|
|
||||||
|
int sac_obj = 0;
|
||||||
|
if (services != NULL && cJSON_IsObject(services)) {
|
||||||
|
sac_obj = 1;
|
||||||
|
fprintf(stderr, "Using deprecated service_access format. Please turn it into an array.\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
cJSON_ArrayForEach(service, services) {
|
||||||
|
int is_host = 0;
|
||||||
|
char *service_name;
|
||||||
|
|
||||||
|
if (sac_obj) {
|
||||||
|
if (!cJSON_IsBool(service)) {
|
||||||
|
fprintf(stderr, "Services must be of form service_name (str) : is_host (bool)\n");
|
||||||
|
status = 0;
|
||||||
|
goto NPDM_BUILD_END;
|
||||||
|
}
|
||||||
|
is_host = cJSON_IsTrue(service);
|
||||||
|
service_name = service->string;
|
||||||
|
} else {
|
||||||
|
if (!cJSON_IsString(service)) {
|
||||||
|
fprintf(stderr, "service_access must be an array of string\n");
|
||||||
|
status = 0;
|
||||||
|
goto NPDM_BUILD_END;
|
||||||
|
}
|
||||||
|
is_host = 0;
|
||||||
|
service_name = service->valuestring;
|
||||||
|
}
|
||||||
|
|
||||||
|
int cur_srv_len = strlen(service_name);
|
||||||
|
if (cur_srv_len > 8 || cur_srv_len == 0) {
|
||||||
|
fprintf(stderr, "Services must have name length 1 <= len <= 8!\n");
|
||||||
|
status = 0;
|
||||||
|
goto NPDM_BUILD_END;
|
||||||
|
}
|
||||||
|
u8 ctrl = (u8)(cur_srv_len - 1);
|
||||||
|
if (is_host) {
|
||||||
|
ctrl |= 0x80;
|
||||||
|
}
|
||||||
|
sac[sac_size++] = ctrl;
|
||||||
|
memcpy(sac + sac_size, service_name, cur_srv_len);
|
||||||
|
sac_size += cur_srv_len;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
memcpy((u8 *)acid + acid->SacOffset, sac, sac_size);
|
||||||
|
aci0->SacSize = sac_size;
|
||||||
|
acid->SacSize = sac_size;
|
||||||
|
aci0->KacOffset = (aci0->SacOffset + aci0->SacSize + 0xF) & ~0xF;
|
||||||
|
acid->KacOffset = (acid->SacOffset + acid->SacSize + 0xF) & ~0xF;
|
||||||
|
|
||||||
|
/* Parse capabilities. */
|
||||||
|
capabilities = cJSON_GetObjectItemCaseSensitive(npdm_json, "kernel_capabilities");
|
||||||
|
if (!(cJSON_IsArray(capabilities) || cJSON_IsObject(capabilities))) {
|
||||||
|
fprintf(stderr, "Kernel Capabilities must be an array!\n");
|
||||||
|
status = 0;
|
||||||
|
goto NPDM_BUILD_END;
|
||||||
|
}
|
||||||
|
|
||||||
|
int kac_obj = 0;
|
||||||
|
if (cJSON_IsObject(capabilities)) {
|
||||||
|
kac_obj = 1;
|
||||||
|
fprintf(stderr, "Using deprecated kernel_capabilities format. Please turn it into an array.\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
u32 *caps = (u32 *)((u8 *)aci0 + aci0->KacOffset);
|
||||||
|
u32 cur_cap = 0;
|
||||||
|
u32 desc;
|
||||||
|
cJSON_ArrayForEach(capability, capabilities) {
|
||||||
|
desc = 0;
|
||||||
|
const char *type_str;
|
||||||
|
const cJSON *value;
|
||||||
|
|
||||||
|
if (kac_obj) {
|
||||||
|
type_str = capability->string;
|
||||||
|
value = capability;
|
||||||
|
} else {
|
||||||
|
if (!cJSON_GetString(capability, "type", &type_str)) {
|
||||||
|
status = 0;
|
||||||
|
goto NPDM_BUILD_END;
|
||||||
|
}
|
||||||
|
value = cJSON_GetObjectItemCaseSensitive(capability, "value");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!strcmp(type_str, "kernel_flags")) {
|
||||||
|
if (!cJSON_IsObject(value)) {
|
||||||
|
fprintf(stderr, "Kernel Flags Capability value must be object!\n");
|
||||||
|
status = 0;
|
||||||
|
goto NPDM_BUILD_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 NPDM_BUILD_END;
|
||||||
|
}
|
||||||
|
|
||||||
|
u8 real_highest_prio = (lowest_prio < highest_prio) ? lowest_prio : highest_prio;
|
||||||
|
u8 real_lowest_prio = (lowest_prio > highest_prio) ? lowest_prio : highest_prio;
|
||||||
|
|
||||||
|
desc = highest_cpu;
|
||||||
|
desc <<= 8;
|
||||||
|
desc |= lowest_cpu;
|
||||||
|
desc <<= 6;
|
||||||
|
desc |= (real_highest_prio & 0x3F);
|
||||||
|
desc <<= 6;
|
||||||
|
desc |= (real_lowest_prio & 0x3F);
|
||||||
|
caps[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 NPDM_BUILD_END;
|
||||||
|
}
|
||||||
|
u32 num_descriptors;
|
||||||
|
u32 descriptors[8] = {0}; /* alignup(0xC0/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 NPDM_BUILD_END;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (syscall_value >= 0xC0) {
|
||||||
|
fprintf(stderr, "Error: All syscall entries must be numbers in [0, 0xBF]\n");
|
||||||
|
status = 0;
|
||||||
|
goto NPDM_BUILD_END;
|
||||||
|
}
|
||||||
|
descriptors[syscall_value / 0x18] |= (1UL << (syscall_value % 0x18));
|
||||||
|
}
|
||||||
|
for (unsigned int i = 0; i < 8; i++) {
|
||||||
|
if (descriptors[i]) {
|
||||||
|
desc = descriptors[i] | (i << 24);
|
||||||
|
caps[cur_cap++] = (u32)((desc << 5) | (0x000F));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if (!strcmp(type_str, "map")) {
|
||||||
|
if (!cJSON_IsObject(value)) {
|
||||||
|
fprintf(stderr, "Map Capability value must be object!\n");
|
||||||
|
status = 0;
|
||||||
|
goto NPDM_BUILD_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 NPDM_BUILD_END;
|
||||||
|
}
|
||||||
|
desc = (u32)((map_address >> 12) & 0x00FFFFFFULL);
|
||||||
|
desc |= is_ro << 24;
|
||||||
|
caps[cur_cap++] = (u32)((desc << 7) | (0x003F));
|
||||||
|
|
||||||
|
desc = (u32)((map_size >> 12) & 0x000FFFFFULL);
|
||||||
|
desc |= (u32)(((map_address >> 36) & 0xFULL) << 20);
|
||||||
|
is_io ^= 1;
|
||||||
|
desc |= is_io << 24;
|
||||||
|
caps[cur_cap++] = (u32)((desc << 7) | (0x003F));
|
||||||
|
} else if (!strcmp(type_str, "map_page")) {
|
||||||
|
u64 page_address = 0;
|
||||||
|
if (!cJSON_GetU64FromObjectValue(value, &page_address)) {
|
||||||
|
status = 0;
|
||||||
|
goto NPDM_BUILD_END;
|
||||||
|
}
|
||||||
|
desc = (u32)((page_address >> 12) & 0x00FFFFFFULL);
|
||||||
|
caps[cur_cap++] = (u32)((desc << 8) | (0x007F));
|
||||||
|
} else if (!strcmp(type_str, "map_region")) {
|
||||||
|
if (cur_cap + 1 > 0x20) {
|
||||||
|
fprintf(stderr, "Error: Too many capabilities!\n");
|
||||||
|
status = 0;
|
||||||
|
goto NPDM_BUILD_END;
|
||||||
|
}
|
||||||
|
if (!cJSON_IsArray(value)) {
|
||||||
|
fprintf(stderr, "Map Region capability value must be array!\n");
|
||||||
|
status = 0;
|
||||||
|
goto NPDM_BUILD_END;
|
||||||
|
}
|
||||||
|
u8 regions[3] = {0};
|
||||||
|
int is_ro[3] = {0};
|
||||||
|
const cJSON *cur_region = NULL;
|
||||||
|
int index = 0;
|
||||||
|
cJSON_ArrayForEach(cur_region, value) {
|
||||||
|
if (index >= 3) {
|
||||||
|
fprintf(stderr, "Too many region descriptors!\n");
|
||||||
|
status = 0;
|
||||||
|
goto NPDM_BUILD_END;
|
||||||
|
}
|
||||||
|
if (!cJSON_IsObject(cur_region)) {
|
||||||
|
fprintf(stderr, "Region descriptor value must be object!\n");
|
||||||
|
status = 0;
|
||||||
|
goto NPDM_BUILD_END;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!cJSON_GetU8(cur_region, "region_type", ®ions[index]) ||
|
||||||
|
!cJSON_GetBoolean(cur_region, "is_ro", &is_ro[index])) {
|
||||||
|
status = 0;
|
||||||
|
goto NPDM_BUILD_END;
|
||||||
|
}
|
||||||
|
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
|
||||||
|
u32 capability = 0x3FF;
|
||||||
|
for (int i = 0; i < 3; ++i) {
|
||||||
|
capability |= ((regions[i] & 0x3F) | ((is_ro[i] & 1) << 6)) << (11 + 7 * i);
|
||||||
|
}
|
||||||
|
caps[cur_cap++] = capability;
|
||||||
|
} else if (!strcmp(type_str, "irq_pair")) {
|
||||||
|
if (!cJSON_IsArray(value) || cJSON_GetArraySize(value) != 2) {
|
||||||
|
fprintf(stderr, "Error: IRQ Pairs must have size 2 array value.\n");
|
||||||
|
status = 0;
|
||||||
|
goto NPDM_BUILD_END;
|
||||||
|
}
|
||||||
|
const cJSON *irq = NULL;
|
||||||
|
int desc_idx = 0;
|
||||||
|
cJSON_ArrayForEach(irq, value) {
|
||||||
|
if (cJSON_IsNull(irq)) {
|
||||||
|
desc |= 0x3FF << desc_idx;
|
||||||
|
} else if (cJSON_IsNumber(irq)) {
|
||||||
|
desc |= (((u16)(irq->valueint)) & 0x3FF) << desc_idx;
|
||||||
|
} else {
|
||||||
|
fprintf(stderr, "Failed to parse IRQ value.\n");
|
||||||
|
status = 0;
|
||||||
|
goto NPDM_BUILD_END;
|
||||||
|
}
|
||||||
|
desc_idx += 10;
|
||||||
|
}
|
||||||
|
caps[cur_cap++] = (u32)((desc << 12) | (0x07FF));
|
||||||
|
} else if (!strcmp(type_str, "application_type")) {
|
||||||
|
if (!cJSON_GetU16FromObjectValue(value, (u16 *)&desc)) {
|
||||||
|
status = 0;
|
||||||
|
goto NPDM_BUILD_END;
|
||||||
|
}
|
||||||
|
desc &= 7;
|
||||||
|
caps[cur_cap++] = (u32)((desc << 14) | (0x1FFF));
|
||||||
|
} else if (!strcmp(type_str, "min_kernel_version")) {
|
||||||
|
u64 kern_ver = 0;
|
||||||
|
if (cJSON_IsNumber(value)) {
|
||||||
|
kern_ver = (u64)value->valueint;
|
||||||
|
} else if (!cJSON_IsString(value) || !cJSON_GetU64FromObjectValue(value, &kern_ver)) {
|
||||||
|
fprintf(stderr, "Error: Kernel version must be integer or hex strings.\n");
|
||||||
|
status = 0;
|
||||||
|
goto NPDM_BUILD_END;
|
||||||
|
}
|
||||||
|
desc = (kern_ver) & 0xFFFF;
|
||||||
|
caps[cur_cap++] = (u32)((desc << 15) | (0x3FFF));
|
||||||
|
} else if (!strcmp(type_str, "handle_table_size")) {
|
||||||
|
if (!cJSON_GetU16FromObjectValue(value, (u16 *)&desc)) {
|
||||||
|
status = 0;
|
||||||
|
goto NPDM_BUILD_END;
|
||||||
|
}
|
||||||
|
caps[cur_cap++] = (u32)((desc << 16) | (0x7FFF));
|
||||||
|
} else if (!strcmp(type_str, "debug_flags")) {
|
||||||
|
if (!cJSON_IsObject(value)) {
|
||||||
|
fprintf(stderr, "Debug Flag Capability value must be object!\n");
|
||||||
|
status = 0;
|
||||||
|
goto NPDM_BUILD_END;
|
||||||
|
}
|
||||||
|
int allow_debug = 0;
|
||||||
|
int force_debug = 0;
|
||||||
|
int force_debug_prod = 0;
|
||||||
|
cJSON_GetBoolean(value, "allow_debug", &allow_debug);
|
||||||
|
cJSON_GetBoolean(value, "force_debug", &force_debug);
|
||||||
|
cJSON_GetBoolean(value, "force_debug_prod", &force_debug_prod);
|
||||||
|
if ( allow_debug + force_debug + force_debug_prod > 1 ) {
|
||||||
|
fprintf(stderr, "Only one of allow_debug, force_debug, or force_debug_prod can be set!\n");
|
||||||
|
status = 0;
|
||||||
|
goto NPDM_BUILD_END;
|
||||||
|
}
|
||||||
|
desc = (allow_debug & 1) | ((force_debug_prod & 1) << 1) | ((force_debug & 1) << 2);
|
||||||
|
caps[cur_cap++] = (u32)((desc << 17) | (0xFFFF));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
aci0->KacSize = cur_cap * sizeof(u32);
|
||||||
|
acid->KacSize = aci0->KacSize;
|
||||||
|
memcpy((u8 *)acid + acid->KacOffset, caps, aci0->KacSize);
|
||||||
|
|
||||||
|
header.AcidOffset = sizeof(header);
|
||||||
|
header.AcidSize = acid->KacOffset + acid->KacSize;
|
||||||
|
acid->Size = header.AcidSize - sizeof(acid->Signature);
|
||||||
|
header.Aci0Offset = (header.AcidOffset + header.AcidSize + 0xF) & ~0xF;
|
||||||
|
header.Aci0Size = aci0->KacOffset + aci0->KacSize;
|
||||||
|
u32 total_size = header.Aci0Offset + header.Aci0Size;
|
||||||
|
u8 *npdm = calloc(1, total_size);
|
||||||
|
if (npdm == NULL) {
|
||||||
|
fprintf(stderr, "Failed to allocate output!\n");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
memcpy(npdm, &header, sizeof(header));
|
||||||
|
memcpy(npdm + header.AcidOffset, acid, header.AcidSize);
|
||||||
|
memcpy(npdm + header.Aci0Offset, aci0, header.Aci0Size);
|
||||||
|
free(acid);
|
||||||
|
free(aci0);
|
||||||
|
*dst = npdm;
|
||||||
|
*dst_size = total_size;
|
||||||
|
|
||||||
|
status = 1;
|
||||||
|
NPDM_BUILD_END:
|
||||||
|
cJSON_Delete(npdm_json);
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char* argv[]) {
|
||||||
|
if (argc != 3) {
|
||||||
|
fprintf(stderr, "%s <json-file> <npdm-file>\n", argv[0]);
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
void *npdm;
|
||||||
|
u32 npdm_size;
|
||||||
|
|
||||||
|
if (sizeof(NpdmHeader) != 0x80 || sizeof(NpdmAcid) != 0x240 || sizeof(NpdmAci0) != 0x40) {
|
||||||
|
fprintf(stderr, "Bad compile environment!\n");
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t json_len;
|
||||||
|
uint8_t* json = ReadEntireFile(argv[1], &json_len);
|
||||||
|
if (json == NULL) {
|
||||||
|
fprintf(stderr, "Failed to read descriptor json!\n");
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!CreateNpdm(json, &npdm, &npdm_size)) {
|
||||||
|
fprintf(stderr, "Failed to parse descriptor json!\n");
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
FILE *f_out = fopen(argv[2], "wb");
|
||||||
|
if (f_out == NULL) {
|
||||||
|
fprintf(stderr, "Failed to open %s for writing!\n", argv[2]);
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
if (fwrite(npdm, 1, npdm_size, f_out) != npdm_size) {
|
||||||
|
fprintf(stderr, "Failed to write NPDM to %s!\n", argv[2]);
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
fclose(f_out);
|
||||||
|
free(npdm);
|
||||||
|
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
424
src/nxlink.c
424
src/nxlink.c
@ -14,11 +14,21 @@
|
|||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
#include <netdb.h>
|
#include <netdb.h>
|
||||||
|
#include <poll.h>
|
||||||
|
#define closesocket close
|
||||||
#else
|
#else
|
||||||
#include <winsock2.h>
|
#include <winsock2.h>
|
||||||
#include <ws2tcpip.h>
|
#include <ws2tcpip.h>
|
||||||
typedef int socklen_t;
|
typedef int socklen_t;
|
||||||
typedef uint32_t in_addr_t;
|
typedef uint32_t in_addr_t;
|
||||||
|
#define SHUT_RD SD_RECEIVE
|
||||||
|
#define SHUT_WR SD_SEND
|
||||||
|
#define SHUT_RDWR SD_BOTH
|
||||||
|
#ifdef EWOULDBLOCK
|
||||||
|
#undef EWOULDBLOCK
|
||||||
|
#endif
|
||||||
|
#define EWOULDBLOCK WSAEWOULDBLOCK
|
||||||
|
#define poll WSAPoll
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <zlib.h>
|
#include <zlib.h>
|
||||||
@ -30,22 +40,19 @@ typedef uint32_t in_addr_t;
|
|||||||
#define NETLOADER_CLIENT_PORT 28771
|
#define NETLOADER_CLIENT_PORT 28771
|
||||||
|
|
||||||
|
|
||||||
char cmdbuf[3072];
|
static char cmdbuf[3072];
|
||||||
uint32_t cmdlen=0;
|
static uint32_t cmdlen=0;
|
||||||
|
|
||||||
//---------------------------------------------------------------------------------
|
//---------------------------------------------------------------------------------
|
||||||
void shutdownSocket(int socket) {
|
static void shutdownSocket(int socket, int flags) {
|
||||||
//---------------------------------------------------------------------------------
|
//---------------------------------------------------------------------------------
|
||||||
#ifdef __WIN32__
|
if (flags)
|
||||||
shutdown (socket, SD_SEND);
|
shutdown(socket, flags);
|
||||||
closesocket(socket);
|
closesocket(socket);
|
||||||
#else
|
|
||||||
close(socket);
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//---------------------------------------------------------------------------------
|
//---------------------------------------------------------------------------------
|
||||||
static int set_socket_nonblocking(int sock) {
|
static int setSocketNonblocking(int sock) {
|
||||||
//---------------------------------------------------------------------------------
|
//---------------------------------------------------------------------------------
|
||||||
|
|
||||||
#ifndef __WIN32__
|
#ifndef __WIN32__
|
||||||
@ -56,88 +63,86 @@ static int set_socket_nonblocking(int sock) {
|
|||||||
int rc = fcntl(sock, F_SETFL, flags | O_NONBLOCK);
|
int rc = fcntl(sock, F_SETFL, flags | O_NONBLOCK);
|
||||||
|
|
||||||
if (rc != 0) return -1;
|
if (rc != 0) return -1;
|
||||||
|
|
||||||
#else
|
#else
|
||||||
u_long opt = 1;
|
u_long iMode = 1; // non-blocking
|
||||||
ioctlsocket(sock, FIONBIO, &opt);
|
|
||||||
|
int rc = ioctlsocket(sock, FIONBIO, &iMode);
|
||||||
|
|
||||||
|
if (rc != NO_ERROR) return -1;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void socket_error(const char *msg) {
|
//---------------------------------------------------------------------------------
|
||||||
|
static int socketError(const char *msg) {
|
||||||
|
//---------------------------------------------------------------------------------
|
||||||
#ifndef _WIN32
|
#ifndef _WIN32
|
||||||
|
int ret = errno;
|
||||||
|
if (ret == EAGAIN)
|
||||||
|
ret = EWOULDBLOCK;
|
||||||
perror(msg);
|
perror(msg);
|
||||||
#else
|
#else
|
||||||
|
int ret = WSAGetLastError();
|
||||||
wchar_t *s = NULL;
|
wchar_t *s = NULL;
|
||||||
FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
|
FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
|
||||||
NULL, WSAGetLastError(),
|
NULL, ret,
|
||||||
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
||||||
(LPWSTR)&s, 0, NULL);
|
(LPWSTR)&s, 0, NULL);
|
||||||
fprintf(stderr, "%S\n", s);
|
fprintf(stderr, "%S\n", s);
|
||||||
LocalFree(s);
|
LocalFree(s);
|
||||||
|
if (ret == WSAEWOULDBLOCK)
|
||||||
|
ret = EWOULDBLOCK;
|
||||||
#endif
|
#endif
|
||||||
}
|
|
||||||
|
|
||||||
/*---------------------------------------------------------------------------------
|
return ret;
|
||||||
Subtract the `struct timeval' values Y from X,
|
|
||||||
storing the result in RESULT.
|
|
||||||
Return 1 if the difference is negative, otherwise 0.
|
|
||||||
|
|
||||||
From http://www.gnu.org/software/libtool/manual/libc/Elapsed-Time.html
|
|
||||||
---------------------------------------------------------------------------------*/
|
|
||||||
int timeval_subtract (struct timeval *result, struct timeval *x, struct timeval *y) {
|
|
||||||
//---------------------------------------------------------------------------------
|
|
||||||
struct timeval tmp;
|
|
||||||
tmp.tv_sec = y->tv_sec;
|
|
||||||
tmp.tv_usec = y->tv_usec;
|
|
||||||
|
|
||||||
/* Perform the carry for the later subtraction by updating y. */
|
|
||||||
if (x->tv_usec < tmp.tv_usec) {
|
|
||||||
int nsec = (tmp.tv_usec - x->tv_usec) / 1000000 + 1;
|
|
||||||
tmp.tv_usec -= 1000000 * nsec;
|
|
||||||
tmp.tv_sec += nsec;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (x->tv_usec - tmp.tv_usec > 1000000) {
|
|
||||||
int nsec = (x->tv_usec - tmp.tv_usec) / 1000000;
|
|
||||||
tmp.tv_usec += 1000000 * nsec;
|
|
||||||
tmp.tv_sec -= nsec;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Compute the time remaining to wait.
|
|
||||||
tv_usec is certainly positive. */
|
|
||||||
result->tv_sec = x->tv_sec - tmp.tv_sec;
|
|
||||||
result->tv_usec = x->tv_usec - tmp.tv_usec;
|
|
||||||
|
|
||||||
/* Return 1 if result is negative. */
|
|
||||||
return x->tv_sec < tmp.tv_sec;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//---------------------------------------------------------------------------------
|
//---------------------------------------------------------------------------------
|
||||||
void timeval_add (struct timeval *result, struct timeval *x, struct timeval *y) {
|
int pollSocket(int fd, int events, int timeout) {
|
||||||
//---------------------------------------------------------------------------------
|
//---------------------------------------------------------------------------------
|
||||||
result->tv_sec = x->tv_sec + y->tv_sec;
|
#ifndef __WIN32__
|
||||||
result->tv_usec = x->tv_usec + y->tv_usec;
|
struct pollfd pfd;
|
||||||
|
#else
|
||||||
|
WSAPOLLFD pfd;
|
||||||
|
#endif
|
||||||
|
|
||||||
if ( result->tv_usec > 1000000) {
|
pfd.fd = fd;
|
||||||
result->tv_sec += result->tv_usec / 1000000;
|
pfd.events = events;
|
||||||
result->tv_usec = result->tv_usec % 1000000;
|
pfd.revents = 0;
|
||||||
|
|
||||||
|
int ret = poll(&pfd, 1, timeout);
|
||||||
|
if (ret < 0) {
|
||||||
|
socketError("poll");
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (ret == 0)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
if (!(pfd.revents & events)) {
|
||||||
|
int err = 0;
|
||||||
|
int len = sizeof(err);
|
||||||
|
getsockopt(fd, SOL_SOCKET, SO_ERROR, (char*)&err, &len);
|
||||||
|
fprintf(stderr, "socket error 0x%x on poll\n", err);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
//---------------------------------------------------------------------------------
|
//---------------------------------------------------------------------------------
|
||||||
static struct in_addr findSwitch(int retries) {
|
static struct in_addr findSwitch(int retries) {
|
||||||
//---------------------------------------------------------------------------------
|
//---------------------------------------------------------------------------------
|
||||||
|
|
||||||
printf("pinging switch");
|
printf("pinging switch\n");
|
||||||
|
|
||||||
struct sockaddr_in s, remote, rs;
|
struct sockaddr_in s, remote, rs;
|
||||||
char recvbuf[256];
|
char recvbuf[256];
|
||||||
char mess[] = "nxboot";
|
char mess[] = "nxboot";
|
||||||
|
|
||||||
int broadcastSock = socket(PF_INET, SOCK_DGRAM, 0);
|
int broadcastSock = socket(PF_INET, SOCK_DGRAM, 0);
|
||||||
if(broadcastSock < 0) socket_error("create send socket");
|
if (broadcastSock < 0) socketError("create send socket");
|
||||||
|
|
||||||
int optval = 1, len;
|
int optval = 1, len;
|
||||||
setsockopt(broadcastSock, SOL_SOCKET, SO_BROADCAST, (char *)&optval, sizeof(optval));
|
setsockopt(broadcastSock, SOL_SOCKET, SO_BROADCAST, (char *)&optval, sizeof(optval));
|
||||||
@ -154,25 +159,16 @@ static struct in_addr findSwitch(int retries) {
|
|||||||
|
|
||||||
int recvSock = socket(PF_INET, SOCK_DGRAM, 0);
|
int recvSock = socket(PF_INET, SOCK_DGRAM, 0);
|
||||||
|
|
||||||
if (recvSock < 0) socket_error("create receive socket");
|
if (recvSock < 0) socketError("create receive socket");
|
||||||
|
|
||||||
if(bind(recvSock, (struct sockaddr*) &rs, sizeof(rs)) < 0) socket_error("bind receive socket");
|
if (bind(recvSock, (struct sockaddr*) &rs, sizeof(rs)) < 0) socketError("bind receive socket");
|
||||||
set_socket_nonblocking(recvSock);
|
setSocketNonblocking(recvSock);
|
||||||
|
|
||||||
struct timeval wanted, now, result;
|
while (retries) {
|
||||||
|
if (sendto(broadcastSock, mess, strlen(mess), 0, (struct sockaddr *)&s, sizeof(s)) < 0)
|
||||||
|
socketError("sendto");
|
||||||
|
|
||||||
gettimeofday(&wanted, NULL);
|
if (pollSocket(recvSock, POLLIN, 150) == 0) {
|
||||||
|
|
||||||
int timeout = retries;
|
|
||||||
while(timeout) {
|
|
||||||
gettimeofday(&now, NULL);
|
|
||||||
if ( timeval_subtract(&result,&wanted,&now)) {
|
|
||||||
if(sendto(broadcastSock, mess, strlen(mess), 0, (struct sockaddr *)&s, sizeof(s)) < 0) socket_error("sendto");
|
|
||||||
result.tv_sec=0;
|
|
||||||
result.tv_usec=150000;
|
|
||||||
timeval_add(&wanted,&now,&result);
|
|
||||||
timeout--;
|
|
||||||
}
|
|
||||||
socklen_t socklen = sizeof(remote);
|
socklen_t socklen = sizeof(remote);
|
||||||
len = recvfrom(recvSock, recvbuf, sizeof(recvbuf), 0, (struct sockaddr *)&remote, &socklen);
|
len = recvfrom(recvSock, recvbuf, sizeof(recvbuf), 0, (struct sockaddr *)&remote, &socklen);
|
||||||
if (len != -1) {
|
if (len != -1) {
|
||||||
@ -181,75 +177,71 @@ static struct in_addr findSwitch(int retries) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (timeout == 0) remote.sin_addr.s_addr = INADDR_NONE;
|
|
||||||
shutdownSocket(broadcastSock);
|
--retries;
|
||||||
shutdownSocket(recvSock);
|
}
|
||||||
|
|
||||||
|
if (retries == 0)
|
||||||
|
remote.sin_addr.s_addr = INADDR_NONE;
|
||||||
|
|
||||||
|
shutdownSocket(broadcastSock, 0);
|
||||||
|
shutdownSocket(recvSock, SHUT_RD);
|
||||||
|
|
||||||
return remote.sin_addr;
|
return remote.sin_addr;
|
||||||
}
|
}
|
||||||
|
|
||||||
//---------------------------------------------------------------------------------
|
//---------------------------------------------------------------------------------
|
||||||
int sendData(int sock, int sendsize, void *buffer) {
|
static int sendData(int sock, int sendsize, void *buffer) {
|
||||||
//---------------------------------------------------------------------------------
|
//---------------------------------------------------------------------------------
|
||||||
char *buf = (char*)buffer;
|
char *buf = (char*)buffer;
|
||||||
while (sendsize) {
|
while (sendsize) {
|
||||||
|
if (pollSocket(sock, POLLOUT, -1))
|
||||||
|
return 1;
|
||||||
|
|
||||||
int len = send(sock, buf, sendsize, 0);
|
int len = send(sock, buf, sendsize, 0);
|
||||||
if (len == 0) break;
|
if (len == 0)
|
||||||
if (len != -1) {
|
return 1;
|
||||||
|
|
||||||
|
if (len == -1) {
|
||||||
|
if (socketError("send") != EWOULDBLOCK)
|
||||||
|
return 1;
|
||||||
|
} else {
|
||||||
sendsize -= len;
|
sendsize -= len;
|
||||||
buf += len;
|
buf += len;
|
||||||
} else {
|
|
||||||
#ifdef _WIN32
|
|
||||||
int errcode = WSAGetLastError();
|
|
||||||
if (errcode != WSAEWOULDBLOCK) {
|
|
||||||
socket_error("sendData");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
#else
|
|
||||||
if ( errno != EWOULDBLOCK && errno != EAGAIN) {
|
|
||||||
socket_error("sendData");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return sendsize != 0;
|
return sendsize != 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
//---------------------------------------------------------------------------------
|
//---------------------------------------------------------------------------------
|
||||||
int recvData(int sock, void *buffer, int size, int flags) {
|
static int recvData(int sock, void *buffer, int size, int flags) {
|
||||||
//---------------------------------------------------------------------------------
|
//---------------------------------------------------------------------------------
|
||||||
int len, sizeleft = size;
|
int len, sizeleft = size;
|
||||||
char *buf = (char*)buffer;
|
char *buf = (char*)buffer;
|
||||||
while (sizeleft) {
|
while (sizeleft) {
|
||||||
|
if (pollSocket(sock, POLLIN, -1))
|
||||||
|
return 0;
|
||||||
|
|
||||||
len = recv(sock,buf,sizeleft,flags);
|
len = recv(sock,buf,sizeleft,flags);
|
||||||
if (len == 0) {
|
if (len == 0)
|
||||||
size = 0;
|
return 0;
|
||||||
break;
|
|
||||||
}
|
if (len == -1) {
|
||||||
if (len != -1) {
|
if (socketError("recv") != EWOULDBLOCK)
|
||||||
|
return 0;
|
||||||
|
} else {
|
||||||
sizeleft -=len;
|
sizeleft -=len;
|
||||||
buf +=len;
|
buf +=len;
|
||||||
} else {
|
|
||||||
#ifdef _WIN32
|
|
||||||
int errcode = WSAGetLastError();
|
|
||||||
if (errcode != WSAEWOULDBLOCK) {
|
|
||||||
printf("recvdata error %d\n",errcode);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
#else
|
|
||||||
if ( errno != EWOULDBLOCK && errno != EAGAIN) {
|
|
||||||
socket_error("recvdata");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return size;
|
return size;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//---------------------------------------------------------------------------------
|
//---------------------------------------------------------------------------------
|
||||||
int sendInt32LE(int socket, uint32_t size) {
|
static int sendInt32LE(int socket, uint32_t size) {
|
||||||
//---------------------------------------------------------------------------------
|
//---------------------------------------------------------------------------------
|
||||||
unsigned char lenbuf[4];
|
unsigned char lenbuf[4];
|
||||||
lenbuf[0] = size & 0xff;
|
lenbuf[0] = size & 0xff;
|
||||||
@ -261,7 +253,7 @@ int sendInt32LE(int socket, uint32_t size) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//---------------------------------------------------------------------------------
|
//---------------------------------------------------------------------------------
|
||||||
int recvInt32LE(int socket, int32_t *data) {
|
static int recvInt32LE(int socket, int32_t *data) {
|
||||||
//---------------------------------------------------------------------------------
|
//---------------------------------------------------------------------------------
|
||||||
unsigned char intbuf[4];
|
unsigned char intbuf[4];
|
||||||
int len = recvData(socket, intbuf, 4, 0);
|
int len = recvData(socket, intbuf, 4, 0);
|
||||||
@ -272,14 +264,13 @@ int recvInt32LE(int socket, int32_t *data) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned char in[ZLIB_CHUNK];
|
static unsigned char in[ZLIB_CHUNK];
|
||||||
unsigned char out[ZLIB_CHUNK];
|
static unsigned char out[ZLIB_CHUNK];
|
||||||
|
|
||||||
//---------------------------------------------------------------------------------
|
//---------------------------------------------------------------------------------
|
||||||
int sendNROFile(in_addr_t nxaddr, char *name, size_t filesize, FILE *fh) {
|
static int sendNROFile(in_addr_t nxaddr, char *name, size_t filesize, FILE *fh) {
|
||||||
//---------------------------------------------------------------------------------
|
//---------------------------------------------------------------------------------
|
||||||
|
|
||||||
int retval = 0;
|
int retval = 0;
|
||||||
@ -297,7 +288,7 @@ int sendNROFile(in_addr_t nxaddr, char *name, size_t filesize, FILE *fh) {
|
|||||||
|
|
||||||
int sock = socket(AF_INET,SOCK_STREAM,0);
|
int sock = socket(AF_INET,SOCK_STREAM,0);
|
||||||
if (sock < 0) {
|
if (sock < 0) {
|
||||||
socket_error("create connection socket");
|
socketError("create connection socket");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -421,23 +412,76 @@ int sendNROFile(in_addr_t nxaddr, char *name, size_t filesize, FILE *fh) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
error:
|
error:
|
||||||
shutdownSocket(sock);
|
shutdownSocket(sock, SHUT_WR);
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
//---------------------------------------------------------------------------------
|
//---------------------------------------------------------------------------------
|
||||||
void showHelp() {
|
static void showHelp() {
|
||||||
//---------------------------------------------------------------------------------
|
//---------------------------------------------------------------------------------
|
||||||
puts("Usage: nxlink [options] nrofile\n");
|
puts("Usage: nxlink [options] nrofile\n");
|
||||||
puts("--help, -h Display this information");
|
puts("--help, -h Display this information");
|
||||||
puts("--address, -a Hostname or IPv4 address of Switch");
|
puts("--address, -a Hostname or IPv4 address of Switch");
|
||||||
puts("--retries, -r number of times to ping before giving up");
|
puts("--retries, -r number of times to ping before giving up");
|
||||||
puts("--path , -p set upload path for file");
|
puts("--path , -p set upload path for file");
|
||||||
|
puts("--args args to send to nro");
|
||||||
puts("--server , -s start server after completed upload");
|
puts("--server , -s start server after completed upload");
|
||||||
puts("\n");
|
puts("\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//---------------------------------------------------------------------------------
|
||||||
|
static int addExtraArgs(int len, char *buf, char *extra_args) {
|
||||||
|
//---------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
if (NULL==extra_args) return len;
|
||||||
|
|
||||||
|
|
||||||
|
int extra_len = strlen(extra_args);
|
||||||
|
|
||||||
|
char *dst = &buf[len];
|
||||||
|
char *src = extra_args;
|
||||||
|
|
||||||
|
do {
|
||||||
|
int c;
|
||||||
|
|
||||||
|
do {
|
||||||
|
c = *src++;
|
||||||
|
extra_len--;
|
||||||
|
} while (c ==' ' && extra_len >= 0);
|
||||||
|
|
||||||
|
if (c == '\"' || c == '\'') {
|
||||||
|
int quote = c;
|
||||||
|
do {
|
||||||
|
c = *src++;
|
||||||
|
if (c != quote) *dst++ = c;
|
||||||
|
extra_len--;
|
||||||
|
} while (c != quote && extra_len >= 0);
|
||||||
|
|
||||||
|
*dst++ = '\0';
|
||||||
|
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
do {
|
||||||
|
*dst++ = c;
|
||||||
|
extra_len--;
|
||||||
|
c = *src++;
|
||||||
|
} while (c != ' ' && extra_len >= 0);
|
||||||
|
|
||||||
|
*dst++ = '\0';
|
||||||
|
} while (extra_len >= 0);
|
||||||
|
|
||||||
|
return dst - buf;
|
||||||
|
}
|
||||||
|
|
||||||
|
#define NRO_ARGS 1000
|
||||||
|
|
||||||
|
#ifdef __WIN32__
|
||||||
|
static void win32_socket_cleanup(void) {
|
||||||
|
WSACleanup();
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
//---------------------------------------------------------------------------------
|
//---------------------------------------------------------------------------------
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
//---------------------------------------------------------------------------------
|
//---------------------------------------------------------------------------------
|
||||||
@ -445,12 +489,13 @@ int main(int argc, char **argv) {
|
|||||||
char *basepath = NULL;
|
char *basepath = NULL;
|
||||||
char *finalpath = NULL;
|
char *finalpath = NULL;
|
||||||
char *endarg = NULL;
|
char *endarg = NULL;
|
||||||
|
char *extra_args = NULL;
|
||||||
int retries = 10;
|
int retries = 10;
|
||||||
static int server = 0;
|
static int server = 0;
|
||||||
|
|
||||||
if (argc < 2) {
|
if (argc < 2) {
|
||||||
showHelp();
|
showHelp();
|
||||||
return 1;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
@ -458,6 +503,7 @@ int main(int argc, char **argv) {
|
|||||||
{"address", required_argument, 0, 'a'},
|
{"address", required_argument, 0, 'a'},
|
||||||
{"retries", required_argument, 0, 'r'},
|
{"retries", required_argument, 0, 'r'},
|
||||||
{"path", required_argument, 0, 'p'},
|
{"path", required_argument, 0, 'p'},
|
||||||
|
{"args", required_argument, 0, NRO_ARGS},
|
||||||
{"help", no_argument, 0, 'h'},
|
{"help", no_argument, 0, 'h'},
|
||||||
{"server", no_argument, &server, 1 },
|
{"server", no_argument, &server, 1 },
|
||||||
{0, 0, 0, 0}
|
{0, 0, 0, 0}
|
||||||
@ -494,6 +540,9 @@ int main(int argc, char **argv) {
|
|||||||
break;
|
break;
|
||||||
case 'h':
|
case 'h':
|
||||||
showHelp();
|
showHelp();
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
case NRO_ARGS:
|
||||||
|
extra_args=optarg;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -502,7 +551,7 @@ int main(int argc, char **argv) {
|
|||||||
char *filename = argv[optind++];
|
char *filename = argv[optind++];
|
||||||
if (filename== NULL) {
|
if (filename== NULL) {
|
||||||
showHelp();
|
showHelp();
|
||||||
return 1;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
memset(cmdbuf, '\0', sizeof(cmdbuf));
|
memset(cmdbuf, '\0', sizeof(cmdbuf));
|
||||||
@ -510,7 +559,7 @@ int main(int argc, char **argv) {
|
|||||||
FILE *fh = fopen(filename,"rb");
|
FILE *fh = fopen(filename,"rb");
|
||||||
if (fh == NULL) {
|
if (fh == NULL) {
|
||||||
fprintf(stderr,"Failed to open %s\n",filename);
|
fprintf(stderr,"Failed to open %s\n",filename);
|
||||||
return -1;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
@ -552,6 +601,8 @@ int main(int argc, char **argv) {
|
|||||||
cmdlen+= len + 1;
|
cmdlen+= len + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cmdlen = addExtraArgs(cmdlen, &cmdbuf[4], extra_args);
|
||||||
|
|
||||||
cmdbuf[0] = cmdlen & 0xff;
|
cmdbuf[0] = cmdlen & 0xff;
|
||||||
cmdbuf[1] = (cmdlen>>8) & 0xff;
|
cmdbuf[1] = (cmdlen>>8) & 0xff;
|
||||||
cmdbuf[2] = (cmdlen>>16) & 0xff;
|
cmdbuf[2] = (cmdlen>>16) & 0xff;
|
||||||
@ -561,8 +612,9 @@ int main(int argc, char **argv) {
|
|||||||
WSADATA wsa_data;
|
WSADATA wsa_data;
|
||||||
if (WSAStartup (MAKEWORD(2,2), &wsa_data)) {
|
if (WSAStartup (MAKEWORD(2,2), &wsa_data)) {
|
||||||
printf ("WSAStartup failed\n");
|
printf ("WSAStartup failed\n");
|
||||||
return 1;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
atexit(&win32_socket_cleanup);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
struct in_addr nxaddr;
|
struct in_addr nxaddr;
|
||||||
@ -573,7 +625,7 @@ int main(int argc, char **argv) {
|
|||||||
|
|
||||||
if (nxaddr.s_addr == INADDR_NONE) {
|
if (nxaddr.s_addr == INADDR_NONE) {
|
||||||
printf("No response from Switch!\n");
|
printf("No response from Switch!\n");
|
||||||
return 1;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
@ -586,14 +638,19 @@ int main(int argc, char **argv) {
|
|||||||
|
|
||||||
if (nxaddr.s_addr == INADDR_NONE) {
|
if (nxaddr.s_addr == INADDR_NONE) {
|
||||||
fprintf(stderr,"Invalid address\n");
|
fprintf(stderr,"Invalid address\n");
|
||||||
return 1;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
int res = sendNROFile(nxaddr.s_addr,finalpath,filesize,fh);
|
int res = sendNROFile(nxaddr.s_addr,finalpath,filesize,fh);
|
||||||
|
|
||||||
fclose(fh);
|
fclose(fh);
|
||||||
|
|
||||||
if ( res == 0 && server) {
|
if (res != 0)
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
|
||||||
|
if (!server)
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
|
||||||
printf("starting server\n");
|
printf("starting server\n");
|
||||||
|
|
||||||
struct sockaddr_in serv_addr;
|
struct sockaddr_in serv_addr;
|
||||||
@ -604,97 +661,74 @@ int main(int argc, char **argv) {
|
|||||||
serv_addr.sin_port = htons(NETLOADER_CLIENT_PORT);
|
serv_addr.sin_port = htons(NETLOADER_CLIENT_PORT);
|
||||||
|
|
||||||
int listenfd = socket(AF_INET, SOCK_STREAM, 0);
|
int listenfd = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
int rc;
|
|
||||||
|
|
||||||
if (listenfd < 0) {
|
if (listenfd < 0) {
|
||||||
socket_error("socket");
|
socketError("socket");
|
||||||
} else {
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
rc = bind(listenfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
|
|
||||||
|
|
||||||
|
int rc = bind(listenfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
|
||||||
if (rc != 0) {
|
if (rc != 0) {
|
||||||
socket_error("bind listen socket");
|
socketError("bind listen socket");
|
||||||
} else {
|
shutdownSocket(listenfd, 0);
|
||||||
if (set_socket_nonblocking(listenfd) == -1) {
|
return EXIT_FAILURE;
|
||||||
socket_error("listen fcntl");
|
}
|
||||||
|
|
||||||
|
rc = setSocketNonblocking(listenfd);
|
||||||
|
if (rc == -1) {
|
||||||
|
socketError("listen fcntl");
|
||||||
|
shutdownSocket(listenfd, 0);
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
} else {
|
|
||||||
rc = listen(listenfd, 10);
|
rc = listen(listenfd, 10);
|
||||||
|
|
||||||
if (rc != 0) {
|
if (rc != 0) {
|
||||||
socket_error("listen");
|
socketError("listen");
|
||||||
} else {
|
shutdownSocket(listenfd, 0);
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
printf("server active ...\n");
|
printf("server active ...\n");
|
||||||
|
|
||||||
int datafd = -1;
|
int datafd = -1;
|
||||||
|
|
||||||
while (listenfd != -1 || datafd != -1) {
|
while (listenfd != -1 || datafd != -1) {
|
||||||
struct sockaddr_in sa_remote;
|
struct sockaddr_in sa_remote;
|
||||||
if(listenfd >= 0 && datafd < 0) {
|
|
||||||
|
if (pollSocket(listenfd >= 0 ? listenfd : datafd, POLLIN, -1))
|
||||||
|
break;
|
||||||
|
|
||||||
|
if (listenfd >= 0) {
|
||||||
socklen_t addrlen = sizeof(sa_remote);
|
socklen_t addrlen = sizeof(sa_remote);
|
||||||
datafd = accept(listenfd, (struct sockaddr*)&sa_remote, &addrlen);
|
datafd = accept(listenfd, (struct sockaddr*)&sa_remote, &addrlen);
|
||||||
|
|
||||||
if(datafd < 0) {
|
if (datafd < 0 && socketError("accept") != EWOULDBLOCK)
|
||||||
#ifdef _WIN32
|
break;
|
||||||
int errcode = WSAGetLastError();
|
|
||||||
if (errcode != WSAEWOULDBLOCK) {
|
|
||||||
socket_error("accept");
|
|
||||||
listenfd = -1;
|
|
||||||
}
|
|
||||||
#else
|
|
||||||
if ( errno != EWOULDBLOCK && errno != EAGAIN) {
|
|
||||||
socket_error("accept");
|
|
||||||
listenfd = -1;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
} else {
|
|
||||||
shutdownSocket(listenfd);
|
|
||||||
listenfd = -1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
if (datafd >= 0) {
|
if (datafd >= 0) {
|
||||||
char recvbuf[256];
|
shutdownSocket(listenfd, 0);
|
||||||
int len = recv(datafd,recvbuf,256,0);
|
listenfd = -1;
|
||||||
|
}
|
||||||
if (len > 0 ) {
|
|
||||||
recvbuf[len] = 0;
|
|
||||||
printf("%s", recvbuf);
|
|
||||||
} else {
|
} else {
|
||||||
|
char recvbuf[256];
|
||||||
|
int len = recv(datafd, recvbuf, sizeof(recvbuf), 0);
|
||||||
|
|
||||||
if (len == -1) {
|
if (len == 0 || (len < 0 && socketError("recv") != EWOULDBLOCK)) {
|
||||||
#ifdef _WIN32
|
shutdownSocket(datafd, 0);
|
||||||
int errcode = WSAGetLastError();
|
|
||||||
if (errcode != WSAEWOULDBLOCK) {
|
|
||||||
socket_error("recvdata");
|
|
||||||
len = 0;
|
|
||||||
}
|
|
||||||
#else
|
|
||||||
if ( errno != EWOULDBLOCK && errno != EAGAIN) {
|
|
||||||
perror("recvdata");
|
|
||||||
len = 0;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
if (len ==0 ) {
|
|
||||||
shutdownSocket(datafd);
|
|
||||||
datafd = -1;
|
datafd = -1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (len > 0)
|
||||||
|
fwrite(recvbuf, 1, len, stdout);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
if (listenfd >= 0)
|
||||||
|
shutdownSocket(listenfd, 0);
|
||||||
|
if (datafd >= 0)
|
||||||
|
shutdownSocket(datafd, SHUT_RD);
|
||||||
|
|
||||||
printf("exiting ... \n");
|
printf("exiting ... \n");
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef __WIN32__
|
return EXIT_SUCCESS;
|
||||||
WSACleanup ();
|
|
||||||
#endif
|
|
||||||
return res;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
21
src/romfs.c
21
src/romfs.c
@ -271,6 +271,7 @@ void romfs_visit_dir(romfs_dirent_ctx_t *parent, romfs_ctx_t *romfs_ctx) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
os_closedir(dir);
|
||||||
parent->child = child_dir_tree;
|
parent->child = child_dir_tree;
|
||||||
parent->file = child_file_tree;
|
parent->file = child_file_tree;
|
||||||
|
|
||||||
@ -409,9 +410,7 @@ size_t build_romfs_into_file(filepath_t *in_dirpath, FILE *f_out, off_t base_off
|
|||||||
cur_entry->name_size = name_size;
|
cur_entry->name_size = name_size;
|
||||||
memcpy(cur_entry->name, cur_dir->cur_path.char_path + 1, name_size);
|
memcpy(cur_entry->name, cur_dir->cur_path.char_path + 1, name_size);
|
||||||
|
|
||||||
romfs_dirent_ctx_t *temp = cur_dir;
|
|
||||||
cur_dir = cur_dir->next;
|
cur_dir = cur_dir->next;
|
||||||
free(temp);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
header.header_size = le_dword(sizeof(header));
|
header.header_size = le_dword(sizeof(header));
|
||||||
@ -473,11 +472,27 @@ size_t build_romfs_into_file(filepath_t *in_dirpath, FILE *f_out, off_t base_off
|
|||||||
|
|
||||||
os_fclose(f_in);
|
os_fclose(f_in);
|
||||||
|
|
||||||
|
cur_file = cur_file->next;
|
||||||
|
}
|
||||||
|
free(buffer);
|
||||||
|
|
||||||
|
/* Free all files. */
|
||||||
|
cur_file = romfs_ctx.files;
|
||||||
|
while (cur_file != NULL) {
|
||||||
romfs_fent_ctx_t *temp = cur_file;
|
romfs_fent_ctx_t *temp = cur_file;
|
||||||
cur_file = cur_file->next;
|
cur_file = cur_file->next;
|
||||||
free(temp);
|
free(temp);
|
||||||
}
|
}
|
||||||
free(buffer);
|
romfs_ctx.files = NULL;
|
||||||
|
|
||||||
|
/* Free all directories. */
|
||||||
|
cur_dir = root_ctx;
|
||||||
|
while (cur_dir != NULL) {
|
||||||
|
romfs_dirent_ctx_t *temp = cur_dir;
|
||||||
|
cur_dir = cur_dir->next;
|
||||||
|
free(temp);
|
||||||
|
}
|
||||||
|
root_ctx = NULL;
|
||||||
|
|
||||||
fseeko64(f_out, base_offset + dir_hash_table_ofs, SEEK_SET);
|
fseeko64(f_out, base_offset + dir_hash_table_ofs, SEEK_SET);
|
||||||
if (fwrite(dir_hash_table, 1, romfs_ctx.dir_hash_table_size, f_out) != romfs_ctx.dir_hash_table_size) {
|
if (fwrite(dir_hash_table, 1, romfs_ctx.dir_hash_table_size, f_out) != romfs_ctx.dir_hash_table_size) {
|
||||||
|
Loading…
Reference in New Issue
Block a user