forked from pspdev/prxtool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NidMgr.C
515 lines (436 loc) · 9.79 KB
/
NidMgr.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
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
/***************************************************************
* PRXTool : Utility for PSP executables.
* (c) TyRaNiD 2k5
*
* NidMgr.C - Implementation of a class to manipulate a list
* of NID Libraries.
***************************************************************/
#include <stdlib.h>
#include <tinyxml/tinyxml.h>
#include "output.h"
#include "NidMgr.h"
#include "prxtypes.h"
struct SyslibEntry
{
unsigned int nid;
const char *name;
};
static SyslibEntry g_syslib[] = {
{ 0xd3744be0, "module_bootstart" },
{ 0xf01d73a7, "module_info" },
{ 0x2f064fa6, "module_reboot_before" },
{ 0xadf12745, "module_reboot_phase" },
{ 0xd632acdb, "module_start" },
{ 0x0f7c276c, "module_start_thread_parameter" },
{ 0xcee8593c, "module_stop" },
{ 0xcf0cc697, "module_stop_thread_parameter" },
{ 0x11b97506, "module_sdk_version" },
};
#define MASTER_NID_MAPPER "MasterNidMapper"
/* Default constructor */
CNidMgr::CNidMgr()
: m_pLibHead(NULL), m_pMasterNids(NULL)
{
}
/* Destructor */
CNidMgr::~CNidMgr()
{
FreeMemory();
}
/* Free allocated memory */
void CNidMgr::FreeMemory()
{
LibraryEntry* pLib;
pLib = m_pLibHead;
while(pLib != NULL)
{
LibraryEntry* pNext;
pNext = pLib->pNext;
if(pLib->pNids != NULL)
{
delete pLib->pNids;
pLib->pNids = NULL;
}
delete pLib;
pLib = pNext;
}
m_pLibHead = NULL;
for(unsigned int i = 0; i < m_funcMap.size(); i++)
{
FunctionType *p;
p = m_funcMap[i];
if(p)
{
delete p;
}
}
}
/* Generate a simple name based on the library and the nid */
const char *CNidMgr::GenName(const char *lib, u32 nid)
{
if(lib == NULL)
{
snprintf(m_szCurrName, LIB_SYMBOL_NAME_MAX, "syslib_%08X", nid);
}
else
{
snprintf(m_szCurrName, LIB_SYMBOL_NAME_MAX, "%s_%08X", lib, nid);
}
return m_szCurrName;
}
/* Search the NID list for a function and return the name */
const char *CNidMgr::SearchLibs(const char *lib, u32 nid)
{
const char *pName = NULL;
LibraryEntry *pLib;
if(m_pMasterNids)
{
pLib = m_pMasterNids;
}
else
{
pLib = m_pLibHead;
}
/* Very lazy, could be sped up using a hash table */
while(pLib != NULL)
{
if((strcmp(lib, pLib->lib_name) == 0) || (m_pMasterNids))
{
int iNidLoop;
for(iNidLoop = 0; iNidLoop < pLib->entry_count; iNidLoop++)
{
if(pLib->pNids[iNidLoop].nid == nid)
{
pName = pLib->pNids[iNidLoop].name;
COutput::Printf(LEVEL_DEBUG, "Using %s, nid %08X\n", pName, nid);
break;
}
}
if(pName != NULL)
{
break;
}
}
if(m_pMasterNids)
{
pLib = NULL;
}
else
{
pLib = pLib->pNext;
}
}
if(pName == NULL)
{
/* First check special case system library stuff */
if(strcmp(lib, PSP_SYSTEM_EXPORT) == 0)
{
int size;
int i;
size = sizeof(g_syslib) / sizeof(SyslibEntry);
for(i = 0; i < size; i++)
{
if(nid == g_syslib[i].nid)
{
pName = g_syslib[i].name;
break;
}
}
}
if(pName == NULL)
{
COutput::Puts(LEVEL_DEBUG, "Using default name");
pName = GenName(lib, nid);
}
}
return pName;
}
/* Read the NID data from the XML file */
const char* CNidMgr::ReadNid(TiXmlElement *pElement, u32 &nid)
{
TiXmlHandle nidHandle(pElement);
TiXmlText *pNid;
TiXmlText *pName;
const char* szName;
szName = NULL;
pNid = nidHandle.FirstChild("NID").FirstChild().Text();
pName = nidHandle.FirstChild("NAME").FirstChild().Text();
if((pNid != NULL) && (pName != NULL))
{
nid = strtoul(pNid->Value(), NULL, 16);
szName = pName->Value();
}
return szName;
}
/* Count the number of nids in the current element */
int CNidMgr::CountNids(TiXmlElement *pElement, const char *name)
{
TiXmlElement *pIterator;
u32 nid;
int iCount = 0;
pIterator = pElement;
while(pIterator != NULL)
{
if(ReadNid(pIterator, nid) != NULL)
{
iCount++;
}
pIterator = pIterator->NextSiblingElement(name);
}
return iCount;
}
/* Process a library XML element */
void CNidMgr::ProcessLibrary(TiXmlElement *pLibrary, const char *prx_name, const char *prx)
{
TiXmlHandle libHandle(pLibrary);
TiXmlText *elmName;
TiXmlText *elmFlags;
TiXmlElement *elmFunction;
TiXmlElement *elmVariable;
int fCount;
int vCount;
bool blMasterNids = false;
assert(prx_name != NULL);
assert(prx != NULL);
elmName = libHandle.FirstChild("NAME").FirstChild().Text();
elmFlags = libHandle.FirstChild("FLAGS").FirstChild().Text();
if(elmName)
{
LibraryEntry *pLib;
COutput::Printf(LEVEL_DEBUG, "Library %s\n", elmName->Value());
SAFE_ALLOC(pLib, LibraryEntry);
if(pLib != NULL)
{
memset(pLib, 0, sizeof(LibraryEntry));
strcpy(pLib->lib_name, elmName->Value());
if(strcmp(pLib->lib_name, MASTER_NID_MAPPER) == 0)
{
blMasterNids = true;
COutput::Printf(LEVEL_DEBUG, "Found master NID table\n");
}
if(elmFlags)
{
pLib->flags = strtoul(elmFlags->Value(), NULL, 16);
}
strcpy(pLib->prx_name, prx_name);
strcpy(pLib->prx, prx);
elmFunction = libHandle.FirstChild("FUNCTIONS").FirstChild("FUNCTION").Element();
elmVariable = libHandle.FirstChild("VARIABLES").FirstChild("VARIABLE").Element();
fCount = CountNids(elmFunction, "FUNCTION");
vCount = CountNids(elmVariable, "VARIABLE");
pLib->vcount = vCount;
pLib->fcount = fCount;
if((fCount+vCount) > 0)
{
SAFE_ALLOC(pLib->pNids, LibraryNid[vCount+fCount]);
if(pLib->pNids != NULL)
{
int iLoop;
const char *pName;
memset(pLib->pNids, 0, sizeof(LibraryNid) * (vCount+fCount));
pLib->entry_count = vCount + fCount;
iLoop = 0;
while(elmFunction != NULL)
{
pName = ReadNid(elmFunction, pLib->pNids[iLoop].nid);
if(pName)
{
pLib->pNids[iLoop].pParentLib = pLib;
strcpy(pLib->pNids[iLoop].name, pName);
COutput::Printf(LEVEL_DEBUG, "Read func:%s nid:0x%08X\n", pLib->pNids[iLoop].name, pLib->pNids[iLoop].nid);
iLoop++;
}
elmFunction = elmFunction->NextSiblingElement("FUNCTION");
}
while(elmVariable != NULL)
{
pName = ReadNid(elmVariable, pLib->pNids[iLoop].nid);
if(pName)
{
strcpy(pLib->pNids[iLoop].name, pName);
COutput::Printf(LEVEL_DEBUG, "Read var:%s nid:0x%08X\n", pLib->pNids[iLoop].name, pLib->pNids[iLoop].nid);
iLoop++;
}
elmVariable = elmVariable->NextSiblingElement("VARIABLE");
}
}
}
/* Link into list */
if(m_pLibHead == NULL)
{
m_pLibHead = pLib;
}
else
{
pLib->pNext = m_pLibHead;
m_pLibHead = pLib;
}
if(blMasterNids)
{
m_pMasterNids = pLib;
}
}
/* Allocate library memory */
}
}
/* Process a PRXFILE XML element */
void CNidMgr::ProcessPrxfile(TiXmlElement *pPrxfile)
{
TiXmlHandle prxHandle(pPrxfile);
TiXmlElement *elmLibrary;
TiXmlText *txtName;
TiXmlText *txtPrx;
const char *szPrx;
txtPrx = prxHandle.FirstChild("PRX").FirstChild().Text();
txtName = prxHandle.FirstChild("PRXNAME").FirstChild().Text();
elmLibrary = prxHandle.FirstChild("LIBRARIES").FirstChild("LIBRARY").Element();
while(elmLibrary)
{
COutput::Puts(LEVEL_DEBUG, "Found LIBRARY");
if(txtPrx == NULL)
{
szPrx = "unknown.prx";
}
else
{
szPrx = txtPrx->Value();
}
if(txtName != NULL)
{
ProcessLibrary(elmLibrary, txtName->Value(), szPrx);
}
elmLibrary = elmLibrary->NextSiblingElement("LIBRARY");
}
}
/* Add an XML file to the current library list */
bool CNidMgr::AddXmlFile(const char *szFilename)
{
TiXmlDocument doc(szFilename);
bool blRet = false;
if(doc.LoadFile())
{
COutput::Printf(LEVEL_DEBUG, "Loaded XML file %s", szFilename);
TiXmlHandle docHandle(&doc);
TiXmlElement *elmPrxfile;
elmPrxfile = docHandle.FirstChild("PSPLIBDOC").FirstChild("PRXFILES").FirstChild("PRXFILE").Element();
while(elmPrxfile)
{
COutput::Puts(LEVEL_DEBUG, "Found PRXFILE");
ProcessPrxfile(elmPrxfile);
elmPrxfile = elmPrxfile->NextSiblingElement("PRXFILE");
}
blRet = true;
}
else
{
COutput::Printf(LEVEL_ERROR, "Couldn't load xml file %s\n", szFilename);
}
return blRet;
}
/* Find the name based on our list of names */
const char *CNidMgr::FindLibName(const char *lib, u32 nid)
{
return SearchLibs(lib, nid);
}
LibraryEntry *CNidMgr::GetLibraries(void)
{
return m_pLibHead;
}
/* Find the name of the dependany library for a specified lib */
const char *CNidMgr::FindDependancy(const char *lib)
{
LibraryEntry *pLib;
pLib = m_pLibHead;
while(pLib != NULL)
{
if(strcmp(pLib->lib_name, lib) == 0)
{
return pLib->prx;
}
pLib = pLib->pNext;
}
return NULL;
}
static char *strip_whitesp(char *str)
{
int len;
while(isspace(*str))
{
str++;
}
len = strlen(str);
while((len > 0) && (isspace(str[len-1])))
{
str[len-1] = 0;
len--;
}
if(len == 0)
{
return NULL;
}
return str;
}
bool CNidMgr::AddFunctionFile(const char *szFilename)
{
FILE *fp;
fp = fopen(szFilename, "r");
if(fp)
{
char line[1024];
while(fgets(line, sizeof(line), fp))
{
char *name;
char *args = NULL;
char *ret = NULL;
name = strip_whitesp(line);
if(name == NULL)
{
continue;
}
args = strchr(name, '|');
if(args)
{
*args++ = 0;
ret = strchr(args, '|');
if(ret)
{
*ret++ = 0;
}
}
if((name) && (name[0] != '#'))
{
FunctionType *p = new FunctionType;
memset(p, 0, sizeof(FunctionType));
snprintf(p->name, FUNCTION_NAME_MAX, "%s", name);
if(args)
{
snprintf(p->args, FUNCTION_ARGS_MAX, "%s", args);
}
if(ret)
{
snprintf(p->ret, FUNCTION_RET_MAX, "%s", ret);
}
m_funcMap.insert(m_funcMap.end(), p);
COutput::Printf(LEVEL_DEBUG, "Function: %s %s(%s)\n", p->ret, p->name, p->args);
}
}
fclose(fp);
return true;
}
return false;
}
FunctionType *CNidMgr::FindFunctionType(const char *name)
{
FunctionType *ret = NULL;
for(unsigned int i = 0; i < m_funcMap.size(); i++)
{
FunctionType *p = NULL;
p = m_funcMap[i];
if((p) && (strcmp(name, p->name) == 0))
{
ret = p;
break;
}
}
return ret;
}