forked from SciresM/hactool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkip.c
146 lines (129 loc) · 5.87 KB
/
kip.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <string.h>
#include <stdio.h>
#include "kip.h"
#include "npdm.h"
void ini1_process(ini1_ctx_t *ctx) {
/* Read *just* safe amount. */
ini1_header_t raw_header;
fseeko64(ctx->file, 0, SEEK_SET);
if (fread(&raw_header, 1, sizeof(raw_header), ctx->file) != sizeof(raw_header)) {
fprintf(stderr, "Failed to read INI1 header!\n");
exit(EXIT_FAILURE);
}
if (raw_header.magic != MAGIC_INI1 || raw_header.num_processes > INI1_MAX_KIPS) {
printf("Error: INI1 is corrupt!\n");
exit(EXIT_FAILURE);
}
ctx->header = malloc(raw_header.size);
if (ctx->header == NULL) {
fprintf(stderr, "Failed to allocate INI1 header!\n");
exit(EXIT_FAILURE);
}
fseeko64(ctx->file, 0, SEEK_SET);
if (fread(ctx->header, 1, raw_header.size, ctx->file) != raw_header.size) {
fprintf(stderr, "Failed to read INI1!\n");
exit(EXIT_FAILURE);
}
uint64_t offset = 0;
for (unsigned int i = 0; i < ctx->header->num_processes; i++) {
ctx->kips[i].tool_ctx = ctx->tool_ctx;
ctx->kips[i].header = (kip1_header_t *)&ctx->header->kip_data[offset];
if (ctx->kips[i].header->magic != MAGIC_KIP1) {
fprintf(stderr, "INI1 is corrupted!\n");
exit(EXIT_FAILURE);
}
offset += kip1_get_size(&ctx->kips[i]);
}
if (ctx->tool_ctx->action & ACTION_INFO) {
ini1_print(ctx);
}
if (ctx->tool_ctx->action & ACTION_EXTRACT) {
ini1_save(ctx);
}
}
void ini1_print(ini1_ctx_t *ctx) {
printf("INI1:\n");
printf(" Number of Processes: %02"PRIx32"\n", ctx->header->num_processes);
printf(" Size: %08"PRIx32"\n", ctx->header->size);
printf("\n");
for (unsigned int i = 0; i < ctx->header->num_processes; i++) {
printf("Process %02"PRIx32":\n", i);
kip1_print(&ctx->kips[i], 1);
printf("\n");
}
printf("\n");
}
void ini1_save(ini1_ctx_t *ctx) {
filepath_t *dirpath = NULL;
if (ctx->tool_ctx->file_type == FILETYPE_INI1 && ctx->tool_ctx->settings.out_dir_path.enabled) {
dirpath = &ctx->tool_ctx->settings.out_dir_path.path;
}
if (dirpath == NULL || dirpath->valid != VALIDITY_VALID) {
dirpath = &ctx->tool_ctx->settings.ini1_dir_path;
}
if (dirpath != NULL && dirpath->valid == VALIDITY_VALID) {
os_makedir(dirpath->os_path);
for (unsigned int i = 0; i < ctx->header->num_processes; i++) {
char padded_name[0x20];
memset(&padded_name, 0, sizeof(padded_name));
memcpy(&padded_name, ctx->kips[i].header->name, sizeof(ctx->kips[i].header->name));
strcat(padded_name, ".kip1");
printf("Saving %s to %s/%s...\n", padded_name, dirpath->char_path, padded_name);
save_buffer_to_directory_file(ctx->kips[i].header, kip1_get_size(&ctx->kips[i]), dirpath, padded_name);
}
}
}
void kip1_process(kip1_ctx_t *ctx) {
/* Read *just* safe amount. */
kip1_header_t raw_header;
fseeko64(ctx->file, 0, SEEK_SET);
if (fread(&raw_header, 1, sizeof(raw_header), ctx->file) != sizeof(raw_header)) {
fprintf(stderr, "Failed to read KIP1 header!\n");
exit(EXIT_FAILURE);
}
if (raw_header.magic != MAGIC_KIP1) {
printf("Error: KIP1 is corrupt!\n");
exit(EXIT_FAILURE);
}
uint64_t size = kip1_get_size_from_header(&raw_header);
ctx->header = malloc(size);
if (ctx->header == NULL) {
fprintf(stderr, "Failed to allocate KIP1 header!\n");
exit(EXIT_FAILURE);
}
fseeko64(ctx->file, 0, SEEK_SET);
if (fread(ctx->header, 1, size, ctx->file) != size) {
fprintf(stderr, "Failed to read KIP1!\n");
exit(EXIT_FAILURE);
}
if (ctx->tool_ctx->action & ACTION_INFO) {
kip1_print(ctx, 0);
}
if (ctx->tool_ctx->action & ACTION_EXTRACT) {
kip1_save(ctx);
}
}
void kip1_print(kip1_ctx_t *ctx, int suppress) {
if (!suppress) printf("KIP1:\n");
printf(" Title ID: %016"PRIx64"\n", ctx->header->title_id);
char padded_name[13];
memset(&padded_name, 0, sizeof(padded_name));
memcpy(&padded_name, ctx->header->name, sizeof(ctx->header->name));
printf(" Name: %s\n", padded_name);
printf(" Process Category: %s\n", npdm_get_proc_category(ctx->header->process_category));
printf(" Main Thread Priority: %"PRId8"\n", ctx->header->main_thread_priority);
printf(" Default CPU Core: %"PRId8"\n", ctx->header->default_core);
printf(" Is 64 Bit: %s\n", (ctx->header->flags & (1 << 3)) ? "True" : "False");
printf(" Is Address Space 64 Bit: %s\n", (ctx->header->flags & (1 << 4)) ? "True" : "False");
printf(" Sections:\n");
printf(" .text: %08"PRIx32"-%08"PRIx32"\n", ctx->header->section_headers[0].out_offset, ctx->header->section_headers[0].out_offset + align(ctx->header->section_headers[0].out_size, 0x1000));
printf(" .rodata: %08"PRIx32"-%08"PRIx32"\n", ctx->header->section_headers[1].out_offset, ctx->header->section_headers[1].out_offset + align(ctx->header->section_headers[1].out_size, 0x1000));
printf(" .rwdata: %08"PRIx32"-%08"PRIx32"\n", ctx->header->section_headers[2].out_offset, ctx->header->section_headers[2].out_offset + align(ctx->header->section_headers[2].out_size, 0x1000));
printf(" .bss: %08"PRIx32"-%08"PRIx32"\n", ctx->header->section_headers[3].out_offset, ctx->header->section_headers[3].out_offset + align(ctx->header->section_headers[3].out_size, 0x1000));
printf(" Kernel Access Control:\n");
kac_print(ctx->header->capabilities, 0x20);
printf("\n");
}
void kip1_save(kip1_ctx_t *ctx) {
/* Do nothing. */
}