-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
307 lines (265 loc) · 6.46 KB
/
main.cpp
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <getopt.h>
#include <CoreFoundation/CoreFoundation.h>
#include <IOKit/IOKitLib.h>
#include "nvmelib.hpp"
NVMeUpdateLib* _nvmeUpdateLib = NULL;
// TODO: populated struct array
static struct option options[] = {
{"query", 2, 0, 'q'},
{"queryVendor", 0, 0, 'f'},
{"queryUpdate", 1, 0, 'p'},
};
void get_nand_firmware_path(uint64_t* nandDescriptor, uint32_t mspType) {
char fileName[8];
if ( nandDescriptor == (void*)0xFFFFFF) {
puts("Invalid-FW-File.pak");
}
else {
if ( (uint32_t)(mspType + 1) >= 5 ) {
fprintf(stderr, "error. Unknown MSP type: 0x%x\n", mspType);
exit(1);
}
strlcpy((char*)&fileName, mspTypes[mspType + 1], 8);
printf("%s/%016llX.pak\n", (char*)&fileName, (uint64_t)nandDescriptor);
}
}
size_t read_stdin(uint64_t size, char **fwDataOut) {
uint64_t len = 0;
uint64_t sz = 0;
void* buffer = NULL;
fprintf(stderr, "Getting file ( %llu bytes ) from stdin\n", size);
buffer = valloc(sz);
bzero(buffer, sz);
len = fread(buffer, 1, sz, stdin);
if( len < sz )
{
printf("Error - read failed! readlen %llu expected %llu\n", len, sz);
free(buffer);
exit(1);
}
*fwDataOut = (char*)buffer;
return sz;
}
size_t file_get_contents(const char *path, char **outBuffer) {
struct stat st = {};
int fd = open(path, 0, 384);
if ( fd == - 1) {
printf("Error opening file %s\n", path);
exit(1);
}
if ( fstat(fd, &st) ) {
printf("Error - could not stat %s\n", path);
close(fd);
exit(1);
}
size_t statSize = st.st_size;
char* buffer = (char*)valloc(st.st_size);
bzero(buffer, statSize);
size_t size = pread(fd, buffer, statSize, 0);
if ( size != st.st_size )
{
puts("Error - pread failed!");
free(buffer);
exit(1);
}
close(fd);
*outBuffer = (char *)buffer; // Copy out
return st.st_size;
}
/*
* WIP
*
void perform_ofw_bfh(char* firmwarePath, uint64_t firmwareSize) {
uint64_t fwSize = firmwareSize;
char *fwPath = firmwarePath;
bool isBFHMode;
uint64_t err = 0;
uint32_t mspID;
char *fwData = NULL;
char* buffer = NULL;
if(_nvmeUpdateLib->IsBFHMode(&isBFHMode)) {
puts("Failed getting BFH status.");
//goto fail_and_exit;
}
err = _nvmeUpdateLib->GetMSPType(&mspID);
if(err) {
fprintf(stderr, "Failed getting MSP type. Error=0x%x\n", err);
//goto fail_and_exit;
}
if(fwPath) {
fwSize = file_get_contents(fwPath, &fwData);
}
else {
read_stdin(fwSize, &fwData);
}
puts("Performing BFH OFW stage...");
if(!isBFHMode)
//goto validate;
buffer = malloc(fwSize + 0x1000);
if(!buffer) {
fprintf(stderr, "%s: Failed allocating OFW BFH buffer.\n", __func__);
//goto fail_and_exit;
}
bzero(buffer, 0x1000);
if(mspID != -1)
{
*(uint32_t*)buffer = 'PUWF'; // firmware update magic
set_iokit_param("IODeviceTree:/arm-io/ans", "s4e-bfh-params", (uint64_t)buffer+4, 0, 0xFFC);
// Dump the BFH parameters
fwrite("s4e-bfh-params: ", 0x10, 1, stderr);
int i = 0;
while(i < 0x400) {
if(!*(uint32_t*)buffer[4*i])
break;
fprintf(stderr, "%08X ", *(uint32_t*)&buffer[4*i++]);
}
fputc('\n', stderr);
memcpy(buffer + 0x1000, fwData, (uint32_t)fwSize);
if(mspID == 2) {
kern_return_t bfhError = _nvmeUpdateLib->PerformBFH(fwData, fwSize);
if(!bfhError) {
free(buffer);
sleep(1);
if(!set_pci_port_state("perst-assert"))
{
sleep(1);
if(!set_pci_port_state("perst-deassert"))
{
sleep(1);
if(mspID != -1) {
_nvmeUpdateLib->SetBFHMode(0);
goto validate;
}
if(!set_nvme_state("enable"))
{
validate:
kern_return_t versionCheckErr = _nvmeUpdateLib->FirmwareVersionCheck(fwData, fwSize, mspID);
if(versionCheckErr)
{
puts("Error Checking FW!");
exit(-1);
}
if( mspID == -1 )
{
puts("Validating FW...");
err = _nvmeUpdateLib->FirmwareValidate(fwData);
}
}
}
}
}
}
}
}
*/
kern_return_t perform_bfh(char* loaderPath, uint64_t size) {
uint32_t sz = 0;
char* data = NULL;
uint32_t status = 0;
kern_return_t ret = 0;
char* bfhData = NULL;
if (loaderPath) {
sz = file_get_contents(loaderPath, &bfhData);
}
else
{
sz = size;
read_stdin(size, &bfhData);
}
data = bfhData;
status = _nvmeUpdateLib->PerformBFH(bfhData, sz);
if (status) {
printf("PerformBFH failed. status=0x%x\n", status);
ret = 1;
}
else
{
ret = 0;
}
free(data);
return ret;
}
void enter_bfh_mode(uint64_t bfhSize) {
char* loaderPath = NULL;
uint32_t mspType = 0;
uint32_t bfhLoaderID = 0;
uint64_t descriptor = 0xFFFFFF;
bool isBFHMode = 0;
uint32_t sz = 0;
uint32_t status = 0;
kern_return_t err = KERN_SUCCESS;
err = _nvmeUpdateLib->GetMSPType(&mspType);
if (err) {
printf("Failed getting MSP type. Error=0x%x\n", err);
goto fail_and_exit;
}
bfhLoaderID = mspType + 1;
if( mspType+1 >= 5 ) {
printf("error. Unknown MSP type: 0x%x\n", mspType);
goto fail_and_exit;
}
if( !_nvmeUpdateLib->IsBFHMode(&isBFHMode) ) {
if(!isBFHMode) {
fprintf(stderr, "Not in BFH mode\n");
goto fail_and_exit;
}
if (bfhLoaderID == 3) {
goto get_firmware;
}
if (bfhSize) {
fprintf(stderr, "Override BFH case\n");
loaderPath = NULL;
sz = bfhSize;
}
else {
loaderPath = kBFHLoaderPaths[bfhLoaderID]; // TODO
fprintf(stderr, "Non-override BFH case. BFH loader path: %s\n", kBFHLoaderPaths[bfhLoaderID]);
sz = 0;
}
status = perform_bfh(loaderPath, sz);
if(status) {
printf("perform_bfh failed. status=0x%x\n", status);
goto fail_and_exit;
}
sleep(1);
if( !_nvmeUpdateLib->IsBFHMode(&isBFHMode)) {
get_firmware:
err = _nvmeUpdateLib->GetNANDDescriptor(&descriptor);
if(!err) {
get_nand_firmware_path((uint64_t*)descriptor, mspType);
exit(0);
}
fprintf(stderr, "Failed getting NAND descriptor. Error=0x%x\n", err);
fail_and_exit:
exit(1);
}
}
puts("Failed getting BFH status.");
goto fail_and_exit;
}
void usage(char* programName)
{
printf(
"Usage: %s [--query] [--validate <path-to-version-plist>] [--update <path-to-fw-update>] [--identify] [--updatestdin]"
" [--bfh path] [--pci state] [ --skip]\n",
programName);
puts("Only one option processed per invocation");
exit(1);
}
int main(int argc, char *argv[]) {
if( (argc & 0xFFFFFFFE) != 2) {
puts("Missing/excess options/args");
usage(argv[0]);
}
_nvmeUpdateLib = new NVMeUpdateLib;
return 0;
}