2018-05-01 06:15:48 +01:00
|
|
|
/*
|
2018-08-05 12:40:32 +01:00
|
|
|
* Copyright (c) 2018 naehrwert
|
2019-10-18 16:02:06 +01:00
|
|
|
* Copyright (c) 2018-2019 CTCaer
|
2018-08-05 12:40:32 +01:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms and conditions of the GNU General Public License,
|
|
|
|
* version 2, as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope 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/>.
|
|
|
|
*/
|
2018-05-01 06:15:48 +01:00
|
|
|
|
|
|
|
#ifndef _PKG2_H_
|
|
|
|
#define _PKG2_H_
|
|
|
|
|
2018-08-13 09:58:24 +01:00
|
|
|
#include "../utils/types.h"
|
|
|
|
#include "../utils/list.h"
|
2018-05-01 06:15:48 +01:00
|
|
|
|
|
|
|
#define PKG2_MAGIC 0x31324B50
|
|
|
|
#define PKG2_SEC_BASE 0x80000000
|
|
|
|
#define PKG2_SEC_KERNEL 0
|
|
|
|
#define PKG2_SEC_INI1 1
|
|
|
|
|
|
|
|
#define INI1_MAGIC 0x31494E49
|
2019-09-12 21:37:00 +01:00
|
|
|
#define PKG2_NEWKERN_GET_INI1 0x44
|
|
|
|
#define PKG2_NEWKERN_START 0x800
|
|
|
|
|
|
|
|
u32 pkg2_newkern_ini1_val;
|
|
|
|
u32 pkg2_newkern_ini1_start;
|
|
|
|
u32 pkg2_newkern_ini1_end;
|
2018-05-01 06:15:48 +01:00
|
|
|
|
2018-07-01 03:03:17 +01:00
|
|
|
typedef struct _kernel_patch_t
|
|
|
|
{
|
|
|
|
u32 id;
|
|
|
|
u32 off;
|
|
|
|
u32 val;
|
|
|
|
u32 *ptr;
|
|
|
|
} kernel_patch_t;
|
|
|
|
|
|
|
|
#define KERNEL_PATCHSET_DEF(name, ...) \
|
|
|
|
kernel_patch_t name[] = { \
|
2018-06-05 00:04:08 +01:00
|
|
|
__VA_ARGS__, \
|
2018-07-01 03:03:17 +01:00
|
|
|
{0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, (u32 *)0xFFFFFFFF} \
|
2018-06-05 00:04:08 +01:00
|
|
|
}
|
|
|
|
|
2018-07-01 03:03:17 +01:00
|
|
|
enum
|
|
|
|
{
|
2019-02-11 22:36:24 +00:00
|
|
|
// Always applied.
|
|
|
|
SVC_GENERIC = 0,
|
|
|
|
// Generic instruction patches.
|
|
|
|
SVC_VERIFY_DS = 0x10,
|
2019-12-10 17:20:02 +00:00
|
|
|
DEBUG_MODE_EN = 0x11,
|
|
|
|
ATM_GEN_PATCH = 0x12,
|
|
|
|
ATM_SYSM_INCR = ATM_GEN_PATCH,
|
2018-07-01 03:03:17 +01:00
|
|
|
// >4 bytes patches. Value is a pointer of a u32 array.
|
2019-12-10 17:20:02 +00:00
|
|
|
ATM_ARR_PATCH = 0x13,
|
2018-07-01 03:03:17 +01:00
|
|
|
};
|
|
|
|
|
2018-05-01 06:15:48 +01:00
|
|
|
typedef struct _pkg2_hdr_t
|
|
|
|
{
|
|
|
|
u8 ctr[0x10];
|
|
|
|
u8 sec_ctr[0x40];
|
|
|
|
u32 magic;
|
|
|
|
u32 base;
|
|
|
|
u32 pad0;
|
|
|
|
u16 version;
|
|
|
|
u16 pad1;
|
|
|
|
u32 sec_size[4];
|
|
|
|
u32 sec_off[4];
|
|
|
|
u8 sec_sha256[0x80];
|
|
|
|
u8 data[];
|
|
|
|
} pkg2_hdr_t;
|
|
|
|
|
|
|
|
typedef struct _pkg2_ini1_t
|
|
|
|
{
|
|
|
|
u32 magic;
|
|
|
|
u32 size;
|
|
|
|
u32 num_procs;
|
|
|
|
u32 pad;
|
|
|
|
} pkg2_ini1_t;
|
|
|
|
|
|
|
|
typedef struct _pkg2_kip1_sec_t
|
|
|
|
{
|
|
|
|
u32 offset;
|
|
|
|
u32 size_decomp;
|
|
|
|
u32 size_comp;
|
|
|
|
u32 attrib;
|
|
|
|
} pkg2_kip1_sec_t;
|
|
|
|
|
|
|
|
#define KIP1_NUM_SECTIONS 6
|
|
|
|
|
|
|
|
typedef struct _pkg2_kip1_t
|
|
|
|
{
|
|
|
|
u32 magic;
|
|
|
|
u8 name[12];
|
|
|
|
u64 tid;
|
|
|
|
u32 proc_cat;
|
|
|
|
u8 main_thrd_prio;
|
|
|
|
u8 def_cpu_core;
|
|
|
|
u8 res;
|
|
|
|
u8 flags;
|
|
|
|
pkg2_kip1_sec_t sections[KIP1_NUM_SECTIONS];
|
|
|
|
u32 caps[0x20];
|
|
|
|
u8 data[];
|
|
|
|
} pkg2_kip1_t;
|
|
|
|
|
|
|
|
typedef struct _pkg2_kip1_info_t
|
|
|
|
{
|
|
|
|
pkg2_kip1_t *kip1;
|
|
|
|
u32 size;
|
|
|
|
link_t link;
|
|
|
|
} pkg2_kip1_info_t;
|
|
|
|
|
2018-06-05 00:04:08 +01:00
|
|
|
typedef struct _pkg2_kernel_id_t
|
|
|
|
{
|
2019-04-23 16:17:55 +01:00
|
|
|
u8 hash[8];
|
2018-07-01 03:03:17 +01:00
|
|
|
kernel_patch_t *kernel_patchset;
|
2018-06-05 00:04:08 +01:00
|
|
|
} pkg2_kernel_id_t;
|
|
|
|
|
2018-07-22 21:22:10 +01:00
|
|
|
typedef struct _kip1_patch_t
|
|
|
|
{
|
2019-02-24 00:54:32 +00:00
|
|
|
u32 offset; // section+offset of patch to apply.
|
|
|
|
u32 length; // In bytes, 0 means last patch.
|
2019-06-30 01:45:18 +01:00
|
|
|
char* srcData; // That must match.
|
|
|
|
char* dstData; // That it gets replaced by.
|
2018-07-22 21:22:10 +01:00
|
|
|
} kip1_patch_t;
|
|
|
|
|
|
|
|
typedef struct _kip1_patchset_t
|
|
|
|
{
|
2019-06-30 01:45:18 +01:00
|
|
|
char* name; // NULL means end.
|
2019-02-24 00:54:32 +00:00
|
|
|
kip1_patch_t* patches; // NULL means not necessary.
|
2018-07-22 21:22:10 +01:00
|
|
|
} kip1_patchset_t;
|
|
|
|
|
|
|
|
typedef struct _kip1_id_t
|
|
|
|
{
|
|
|
|
const char* name;
|
2019-04-23 16:17:55 +01:00
|
|
|
u8 hash[8];
|
2018-07-22 21:22:10 +01:00
|
|
|
kip1_patchset_t* patchset;
|
|
|
|
} kip1_id_t;
|
|
|
|
|
2019-09-12 21:37:00 +01:00
|
|
|
void pkg2_get_newkern_info(u8 *kern_data);
|
2019-04-23 01:41:07 +01:00
|
|
|
void pkg2_parse_kips(link_t *info, pkg2_hdr_t *pkg2, bool *new_pkg2);
|
2019-09-12 21:08:38 +01:00
|
|
|
int pkg2_has_kip(link_t *info, u64 tid);
|
2018-05-01 06:15:48 +01:00
|
|
|
void pkg2_replace_kip(link_t *info, u64 tid, pkg2_kip1_t *kip1);
|
|
|
|
void pkg2_add_kip(link_t *info, pkg2_kip1_t *kip1);
|
|
|
|
void pkg2_merge_kip(link_t *info, pkg2_kip1_t *kip1);
|
2018-07-22 21:22:10 +01:00
|
|
|
const char* pkg2_patch_kips(link_t *info, char* patchNames);
|
2018-05-01 06:15:48 +01:00
|
|
|
|
2019-04-23 16:17:55 +01:00
|
|
|
const pkg2_kernel_id_t *pkg2_identify(u8 *hash);
|
2019-09-12 21:39:47 +01:00
|
|
|
pkg2_hdr_t *pkg2_decrypt(void *data, u8 kb);
|
2019-04-21 15:37:12 +01:00
|
|
|
void pkg2_build_encrypt(void *dst, void *kernel, u32 kernel_size, link_t *kips_info, bool new_pkg2);
|
2018-05-01 06:15:48 +01:00
|
|
|
|
|
|
|
#endif
|