Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
CTCaer
GitHub Repository: CTCaer/hekate
Path: blob/master/bootloader/hos/pkg2.h
1476 views
1
/*
2
* Copyright (c) 2018 naehrwert
3
* Copyright (c) 2018-2025 CTCaer
4
*
5
* This program is free software; you can redistribute it and/or modify it
6
* under the terms and conditions of the GNU General Public License,
7
* version 2, as published by the Free Software Foundation.
8
*
9
* This program is distributed in the hope it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12
* more details.
13
*
14
* You should have received a copy of the GNU General Public License
15
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16
*/
17
18
#ifndef _PKG2_H_
19
#define _PKG2_H_
20
21
#include <bdk.h>
22
23
#define PKG2_MAGIC 0x31324B50
24
#define PKG2_SEC_BASE 0x80000000
25
#define PKG2_SEC_KERNEL 0
26
#define PKG2_SEC_INI1 1
27
#define PKG2_SEC_UNUSED 2
28
29
#define INI1_MAGIC 0x31494E49
30
31
//! TODO: Update on kernel change if needed.
32
// Offset of OP + 12 is the INI1 offset. On v2 with dynamic crt0 it's + 16.
33
#define PKG2_NEWKERN_GET_INI1_HEURISTIC 0xD2800015 // MOV X21, #0.
34
#define PKG2_NEWKERN_START 0x800
35
36
#define ATM_MESOSPHERE 0x3053534D
37
38
extern u32 pkg2_newkern_ini1_info;
39
extern u32 pkg2_newkern_ini1_start;
40
extern u32 pkg2_newkern_ini1_end;
41
42
typedef struct _kernel_patch_t
43
{
44
u32 id;
45
u32 off;
46
u32 val;
47
const u32 *ptr;
48
} kernel_patch_t;
49
50
#define KERNEL_PATCHSET_DEF(name, ...) \
51
static const kernel_patch_t name[] = { \
52
__VA_ARGS__, \
53
{0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, (u32 *)0xFFFFFFFF} \
54
}
55
56
enum
57
{
58
// Always applied.
59
SVC_GENERIC = 0,
60
// Generic instruction patches.
61
SVC_VERIFY_DS = 0x10,
62
DEBUG_MODE_EN = 0x11,
63
ATM_GEN_PATCH = 0x12,
64
ATM_SYSM_INCR = ATM_GEN_PATCH,
65
// >4 bytes patches. Value is a pointer of a u32 array.
66
ATM_ARR_PATCH = 0x13,
67
};
68
69
typedef struct _pkg2_hdr_t
70
{
71
/* 0x000 */ u8 ctr[0x10];
72
/* 0x010 */ u8 sec_ctr[0x40];
73
/* 0x050 */ u32 magic;
74
/* 0x054 */ u32 base;
75
/* 0x058 */ u32 pad0;
76
/* 0x05C */ u8 pkg2_ver;
77
/* 0x05D */ u8 bl_ver;
78
/* 0x05E */ u16 pad1;
79
/* 0x060 */ u32 sec_size[4];
80
/* 0x070 */ u32 sec_off[4];
81
/* 0x080 */ u8 sec_sha256[0x80];
82
/* 0x100 */ u8 data[];
83
} pkg2_hdr_t;
84
85
typedef struct _pkg2_ini1_t
86
{
87
u32 magic;
88
u32 size;
89
u32 num_procs;
90
u32 pad;
91
} pkg2_ini1_t;
92
93
typedef struct _pkg2_kip1_sec_t
94
{
95
u32 offset;
96
u32 size_decomp;
97
u32 size_comp;
98
u32 attrib;
99
} pkg2_kip1_sec_t;
100
101
#define KIP1_NUM_SECTIONS 6
102
103
typedef struct _pkg2_kip1_t
104
{
105
/* 0x000 */ u32 magic;
106
/* 0x004 */ u8 name[12];
107
/* 0x010 */ u64 tid;
108
/* 0x018 */ u32 proc_cat;
109
/* 0x01C */ u8 main_thrd_prio;
110
/* 0x01D */ u8 def_cpu_core;
111
/* 0x01E */ u8 res;
112
/* 0x01F */ u8 flags;
113
/* 0x020 */ pkg2_kip1_sec_t sections[KIP1_NUM_SECTIONS];
114
/* 0x080 */ u32 caps[0x20];
115
/* 0x100 */ u8 data[];
116
} pkg2_kip1_t;
117
118
typedef struct _pkg2_kip1_info_t
119
{
120
pkg2_kip1_t *kip1;
121
u32 size;
122
link_t link;
123
} pkg2_kip1_info_t;
124
125
typedef struct _pkg2_kernel_id_t
126
{
127
u8 hash[8];
128
const kernel_patch_t *kernel_patchset;
129
} pkg2_kernel_id_t;
130
131
#define KIP1_PATCH_SRC_NO_CHECK (char *)(-1)
132
133
typedef struct _kip1_patch_t
134
{
135
u32 offset; // section+offset of patch to apply.
136
u32 length; // In bytes, 0 means last patch.
137
char *src_data; // That must match.
138
char *dst_data; // That it gets replaced by.
139
} kip1_patch_t;
140
141
typedef struct _kip1_patchset_t
142
{
143
char *name; // NULL means end.
144
const kip1_patch_t *patches; // NULL means not necessary.
145
} kip1_patchset_t;
146
147
typedef struct _kip1_id_t
148
{
149
const char *name;
150
u8 hash[8];
151
const kip1_patchset_t *patchset;
152
} kip1_id_t;
153
154
bool pkg2_parse_kips(link_t *info, pkg2_hdr_t *pkg2, bool *new_pkg2);
155
int pkg2_has_kip(link_t *info, u64 tid);
156
void pkg2_replace_kip(link_t *info, u64 tid, pkg2_kip1_t *kip1);
157
void pkg2_add_kip(link_t *info, pkg2_kip1_t *kip1);
158
void pkg2_merge_kip(link_t *info, pkg2_kip1_t *kip1);
159
void pkg2_get_ids(kip1_id_t **ids, u32 *entries);
160
const char *pkg2_patch_kips(link_t *info, char *patch_names);
161
162
const pkg2_kernel_id_t *pkg2_identify(const u8 *hash);
163
pkg2_hdr_t *pkg2_decrypt(void *data, u8 kb, bool is_exo);
164
void pkg2_build_encrypt(void *dst, void *hos_ctxt, link_t *kips_info, bool is_exo);
165
166
#endif
167
168