-
Notifications
You must be signed in to change notification settings - Fork 24
/
mecha.c
253 lines (204 loc) · 5.16 KB
/
mecha.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/*
* Copyright (c) 2021 MechaResearch
*
* 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/>.
*/
#include <debug.h>
#include <stdio.h>
#include <string.h>
#include <kernel.h>
#include <sifrpc.h>
#include "mecha.h"
#include "mechaproxy.h"
static SifRpcClientData_t SifRpcClientMechaScmd;
static unsigned char RpcBuffer[0x1000] ALIGNED(64);
#define _printf(args...) // scr_printf(args)
int MechaInit()
{
SifInitRpc(0);
nopdelay();
while (SifBindRpc(&SifRpcClientMechaScmd, MECHAPROXY_SCMD_HEADER, 0) < 0 || SifRpcClientMechaScmd.server == NULL)
{
_printf("libsecr: bind failed\n");
}
return 1;
}
void MechaDeinit()
{
memset(&SifRpcClientMechaScmd, 0, sizeof(SifRpcClientData_t));
}
int MechaScmd(u8 cmd, void *input, u8 inputlength, void *output)
{
struct MechaScmdParams *params = (struct MechaScmdParams *)RpcBuffer;
params->cmd = cmd;
params->inputlength = inputlength;
memcpy(params->input, input, inputlength);
if (SifCallRpc(&SifRpcClientMechaScmd, 1, 0, RpcBuffer, sizeof(RpcBuffer), RpcBuffer, sizeof(RpcBuffer), NULL, NULL) < 0)
{
_printf("MechaScmd: rpc error\n");
return 0;
}
memcpy(output, params->output, 16);
return params->result;
}
char getMechaVersion(uint8_t *data)
{
u8 input[1];
u8 output[16];
input[0] = 0x00;
if (MechaScmd(0x03, input, sizeof(input), output) != 1)
{
return 0;
}
memcpy(data, &output[0], 4);
return 1;
}
char getMechaBuildDate(uint8_t *data)
{
u8 input[1];
u8 output[16];
input[0] = 0xfd;
if (MechaScmd(0x03, input, sizeof(input), output) != 1 || output[0] != 0)
{
_printf("Failed to read the build date!\n");
return 0;
}
memcpy(data, &output[1], 5);
return 1;
}
char OpenConfig(uint8_t id, char write, uint8_t blocks)
{
u8 input[3];
u8 output[16];
input[0] = write; // 0 = read, 1 = write
input[1] = id; // id
input[2] = blocks; // blocks
if (MechaScmd(0x40, input, sizeof(input), output) != 1)
{
if (output[0] != 0)
{
CloseConfig();
if (MechaScmd(0x40, input, sizeof(input), output) != 1 || output[0] != 0)
{
_printf("Failed to cmd OpenConfig!\n");
return 0;
}
}
}
return 1;
}
char ReadConfig(uint8_t *data)
{
u8 input[0];
u8 output[16];
if (MechaScmd(0x41, input, sizeof(input), output) != 1)
{
_printf("Failed to cmd ReadConfig!\n");
return 0;
}
memcpy(data, output, 16);
return 1;
}
char WriteConfig(const uint8_t *data)
{
u8 input[16];
u8 output[16];
memcpy(input, data, 16);
if (MechaScmd(0x42, input, sizeof(input), output) != 1 || output[0] != 0)
{
_printf("Failed to cmd WriteConfig!\n");
return 0;
}
return 1;
}
char CloseConfig()
{
u8 input[0];
u8 output[16];
output[0] = 1;
while (output[0])
{
if (MechaScmd(0x43, input, sizeof(input), output) != 1 || (output[0] != 0 && output[0] != 1))
{
_printf("Failed to cmd CloseConfig!\n");
return 0;
}
}
return 1;
}
char ReadNVM(uint16_t offset, uint16_t *data)
{
u8 input[2];
u8 output[16];
input[0] = offset >> 8;
input[1] = offset;
if (MechaScmd(0x0A, input, sizeof(input), output) != 1 || output[0] != 0)
{
_printf("Failed to cmd 0x0A!\n");
return 0;
}
*data = output[1];
*data <<= 8;
*data |= output[2];
return 1;
}
char WriteNVM(uint16_t offset, uint16_t data)
{
u8 input[4];
u8 output[16];
input[0] = offset >> 8;
input[1] = offset;
input[2] = data >> 8;
input[3] = data;
output[0] = 1;
while (output[0])
{
if (MechaScmd(0x0B, input, sizeof(input), output) != 1 || (output[0] != 0 && output[0] != 1))
{
_printf("Failed to cmd WriteNVM! (%d)\n", offset);
return 0;
}
}
return 1;
}
char IsNVMUnlocked()
{
uint16_t data;
if (!ReadNVM(400, &data))
return 0;
if (!WriteNVM(400, data))
return 0;
return 1;
}
char getSerial(uint32_t *serial)
{
uint16_t part1;
uint16_t part2;
uint8_t version[4];
if (!ReadNVM(0xFA, &part1))
return 0;
if (!ReadNVM(0xFB, &part2))
return 0;
if (getMechaVersion(version))
{
if (version[1] < 4)
{
if (!ReadNVM(0xE6, &part1))
return 0;
if (!ReadNVM(0xE7, &part2))
return 0;
}
}
*serial = ((part2 & 0xff) << 16) | part1;
return 1;
}