-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract.c
658 lines (550 loc) · 13.6 KB
/
extract.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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include "os_structs.h"
#include "rom.h"
#include "extract.h"
#include "byte_swap.h"
#include "translate.h"
#include "utils.h"
#include "Crc.h"
#define ROM_BASE 0x10C00000
#define PALMOS_1_SIZE 0x00080000
#define PALMOS_1_BIG_ROM_OFFSET 0x00003000
#define PALMOS_4_BIG_ROM_OFFSET 0x00008000
#if 0
/* This doesn't work */
/*
* Read in a Palm RAM, byte swap and relocate relative to the RAM buffer.
*/
ROMPtr ReadRAM (int hROM,
UInt16 flags)
{
StorageHeaderType Store;
UInt32 ROM_base;
UInt32 Card_base;
UInt32 File_base;
ROMPtr pROM = NULL;
UInt32 nBytes;
/* How big is the RAM image? */
nBytes = lseek(hROM, 0, SEEK_END);
if (nBytes < sizeof(Store))
{
return(NULL);
}
if (lseek(hROM, 0, SEEK_SET) != 0)
return(NULL);
if (read(hROM, &Store, sizeof(Store)) != sizeof(Store))
{
return(NULL);
}
P2H_StorageHeader(&Store);
if (Store.signature != sysStoreSignature)
{
/* NOT a valid Store */
return(NULL);
}
//if (Store.version >= 0x00)
{
ROM_base = 0x00040000;
}
File_base = 0;
Card_base = ROM_base;
// Allocate size for the headers
if (! (pROM = (ROMPtr)malloc(sizeof(ROMType))))
return(NULL);
memset(pROM, 0, sizeof(*pROM));
pROM->ROM_base = ROM_base;
pROM->Card_base = Card_base;
pROM->File_base = File_base;
/*
* Allocate a ROM buffer to hold the entire RAM.
*/
pROM->ROMSize = nBytes - File_base;
pROM->CardSize = pROM->ROMSize;
pROM->pROM = (UInt8*)malloc(pROM->ROMSize);
if (pROM->pROM == NULL)
{
FreeROM(pROM);
return(NULL);
}
/*
* Read in the entire ROM
*/
lseek(hROM, File_base, SEEK_SET);
if (read(hROM, pROM->pROM, pROM->ROMSize) != pROM->ROMSize)
{
FreeROM(pROM);
return(0);
}
if (! Setup_ROM (pROM))
return (NULL);
/*
* Make a copy of the Database list that we can sort and sort it
*/
if (pROM->pDatabaseList)
{
UInt32 nBytes = sizeof(DatabaseHdrPtr) *
pROM->pDatabaseList->numDatabases;
pROM->pSortedDBList = (DatabaseHdrPtr*)malloc(nBytes);
if (pROM->pSortedDBList == NULL)
{
FreeROM(pROM);
return(NULL);
}
memcpy(pROM->pSortedDBList,
&(pROM->pDatabaseList->databaseOffset[0]), nBytes);
qsort((void*)pROM->pSortedDBList,
pROM->pDatabaseList->numDatabases,
sizeof(DatabaseHdrPtr), CompareAddrs);
}
return (pROM);
}
#endif
/*
* Generate a CRC for the given ROM
*
* This assumes that the ROM is currently in Palm Order and none of the
* pointers are valid for our address space.
*/
static int CheckCRC (ROMPtr pROM)
{
// From Bank_ROM.cpp
// The checksum is the cumulative checksum of the ROM image before
// the stored checksum value and the ROM image following the checksum
// value. First, calculate the first part.
//UInt32 chunkSize = offsetof (CardHeaderType, checksumValue);
UInt32 chunkSize;
UInt32 checksumBytes;
UInt16 checksumValue;
UInt16 origChecksum;
if (! pROM || ! pROM->pROM)
return(0);
if (! pROM->pCard)
pROM->pCard = (CardHeaderPtr)(pROM->pROM);
if (! pROM->pCard)
return(0);
checksumBytes = BYTE_SWAP_32(pROM->pCard->checksumBytes);
if ((checksumBytes < 1) || (checksumBytes > pROM->ROMSize))
return(0);
chunkSize = (UInt32)
&(pROM->pCard->checksumValue) - (UInt32)pROM->pCard;
checksumValue = Crc16CalcBigBlock (pROM->pROM, chunkSize, 0);
// Now calculate the second part.
checksumValue = Crc16CalcBigBlock (
pROM->pROM + chunkSize + sizeof (pROM->pCard->checksumValue),
checksumBytes - chunkSize - sizeof (pROM->pCard->checksumValue),
checksumValue);
origChecksum = BYTE_SWAP_16(pROM->pCard->checksumValue);
if (origChecksum != checksumValue)
{
fprintf (stderr, "*** Checksum over %ld bytes do NOT match:\n"
"*** orig 0x%x != 0x%x - invalid ROM! ***\n",
checksumBytes,
origChecksum, checksumValue);
return (0);
}
#ifdef DEBUG
else
{
fprintf (stdout, "--- Checksum over %ld bytes match:\n"
"--- orig 0x%x != 0x%x - valid ROM! ---\n",
checksumBytes,
origChecksum, checksumValue);
}
#endif // DEBUG
return (1); //checksumValue);
}
/*
* Read in a Palm ROM, byte swap and relocate relative to the ROM buffer.
*/
ROMPtr ReadROM (int hROM,
UInt16 flags)
{
CardHeaderType Card;
StorageHeaderType Store;
UInt32 ROM_base;
UInt32 Card_base;
UInt32 File_base;
ROMPtr pROM = NULL;
if (lseek(hROM, 0, SEEK_SET) != 0)
{
perror ("ReadROM");
return(NULL);
}
if (read(hROM, &Card, sizeof(Card)) != sizeof(Card))
{
return(NULL);
}
P2H_CardHeader (&Card);
if (Card.bigROMOffset >= 0xF0C00000)
{
printf ("big big ROM offset\n");
Card.bigROMOffset -= 0xF0C00000;
}
if (Card.signature != sysCardSignature)
{
/* NOT a valid Card */
return(NULL);
}
if (read(hROM, &Store, sizeof(Store)) != sizeof(Store))
{
return(NULL);
}
P2H_StorageHeader(&Store);
if (Store.signature != sysStoreSignature)
{
/* NOT a valid Store */
return(NULL);
}
if (Card.hdrVersion == 0x01 || Card.hdrVersion == 0x02)
{
// The ROM_BASE for version 1 and 2 is 0x00C00000. This is not right
// for the reset vector -- that must be handled specially.
ROM_base = 0x00C00000;
}
else
{
// The ROM_BASE for version 3 is 0x10C00000 (ROM_BASE)
ROM_base = ROM_BASE;
}
/*
* Figure out how to get the ROM we're interested in
*/
switch (flags & RT_TYPE_MASK)
{
case RT_SMALL:
if ((Store.initCodeOffset1 != 0) || (Store.initCodeOffset2 != 0))
{
/* The first card image is NOT a small ROM */
return(NULL);
}
File_base = 0;
Card_base = ROM_base;
break;
case RT_LARGE:
if ((Store.initCodeOffset1 != 0) || (Store.initCodeOffset2 != 0))
{
/* The first card image is large ROM */
File_base = 0;
if (Card.hdrVersion == 0x01)
Card_base = ROM_base + PALMOS_1_BIG_ROM_OFFSET;
else
Card_base = Card.bigROMOffset;
}
else
{
/* The first card image is small ROM */
if (Card.hdrVersion == 0x01)
{
File_base = PALMOS_1_BIG_ROM_OFFSET;
Card_base = ROM_base + PALMOS_1_BIG_ROM_OFFSET;
}
else
{
File_base = Card.bigROMOffset - ROM_base;
Card_base = Card.bigROMOffset;
}
}
break;
default:
return(NULL);
break;
}
//fprintf (stdout, "ROM_base [0x%08lX]\n", ROM_base);
//
// Allocate size for the headers
if (! (pROM = (ROMPtr)malloc(sizeof(ROMType))))
return(NULL);
memset(pROM, 0, sizeof(*pROM));
pROM->flags = flags & (RT_TYPE_MASK);
pROM->ROM_base = ROM_base;
pROM->Card_base = Card_base;
pROM->File_base = File_base;
/*
* Allocate a ROM buffer to hold the entire ROM.
*/
if (flags & RT_LARGE)
{
pROM->ROMSize = lseek(hROM, 0, SEEK_END) - File_base;
}
else
{
pROM->ROMSize = Card.hdrVersion == 0x01 ?
PALMOS_1_BIG_ROM_OFFSET :
Card.bigROMOffset - Card_base;
}
pROM->CardSize = pROM->ROMSize;
pROM->pROM = (UInt8*)malloc(pROM->ROMSize);
if (pROM->pROM == NULL)
{
FreeROM(pROM);
return(NULL);
}
/*
* Read in the entire ROM
*/
lseek(hROM, File_base, SEEK_SET);
if (read(hROM, pROM->pROM, pROM->ROMSize) != pROM->ROMSize)
{
FreeROM(pROM);
return(0);
}
#if 0
/*************************************************************
* Before anything else, check the checksum
*/
if (! CheckCRC(pROM))
/* Checksum ERROR */
return (NULL);
#else
/*
* There are many reasons why a checksum might not match: several hacks and
* flashpro modify the ROM without changing the checksum.
*/
CheckCRC(pROM);
#endif
if (! Setup_ROM (pROM))
return (NULL);
/*
* Make a copy of the Database list that we can sort and sort it
*/
if (pROM->pDatabaseList)
{
UInt32 nBytes = sizeof(DatabaseHdrPtr) *
pROM->pDatabaseList->numDatabases;
pROM->pSortedDBList = (DatabaseHdrPtr*)malloc(nBytes);
if (pROM->pSortedDBList == NULL)
{
FreeROM(pROM);
return(NULL);
}
memcpy(pROM->pSortedDBList,
&(pROM->pDatabaseList->databaseOffset[0]), nBytes);
qsort((void*)pROM->pSortedDBList,
pROM->pDatabaseList->numDatabases,
sizeof(DatabaseHdrPtr), CompareAddrs);
}
pROM->pVersion = CollectVersion (pROM);
return (pROM);
}
/*
* Copy a chunk into our buffer...
*/
static Int32 CopyChunk (ROMPtr pROM,
LocalID offset,
char** ppPRC,
UInt32 nBytes)
{
char* pNewPRC;
UInt32 nSize;
UInt16 version = memUChunkVer(pROM->pHeapList->heapOffset[0]);
// Locate the chunk that this points to and copy it
// into our buffer.
MemChunkHeaderUnionType* pChunk =
LocateChunk(pROM->pHeapList, offset);
if (! pChunk)
{
return(-1);
}
// the sizeAdj in the chunk header seems to indicate how many
// bytes short of a full chunk the data is
nSize = memUChunkPayloadSize(pChunk,version);
pNewPRC = (char*)realloc(*ppPRC, nBytes + nSize);
if (! pNewPRC)
{
return(-1);
}
*ppPRC = pNewPRC;
memcpy(*ppPRC + nBytes,
(UInt8*)pChunk + memUSizeOfChunkHeader(version), nSize);
return(nSize);
}
/*
* Read a PRC from a file, creating a relocated, byte-swapped database header
*/
PRCPtr ReadPRC (char* pFileName)
{
int hPRC;
UInt32 nRead;
PRCPtr pPRC;
if (! pFileName)
return(NULL);
pPRC = (PRCPtr)malloc(sizeof(*pPRC));
if (! pPRC)
return (NULL);
if ((hPRC = open(pFileName, O_RDONLY | O_BINARY)) < 0)
{
free (pPRC);
return(NULL);
}
pPRC->nBytes = lseek(hPRC, 0, SEEK_END);
lseek(hPRC, 0, SEEK_SET);
pPRC->pDB = (DatabaseHdrPtr)malloc(pPRC->nBytes);
if (! pPRC->pDB)
{
free (pPRC);
close(hPRC);
return(NULL);
}
if ((nRead = read(hPRC, pPRC->pDB, pPRC->nBytes)) != pPRC->nBytes)
{
free (pPRC);
free(pPRC->pDB);
close(hPRC);
return(NULL);
}
/*
* Now, byte-swap and relocate the database header based upon the
* beginning of pPRC->pDB.
*/
P2H_Translate_PRC (pPRC);
close(hPRC);
return(pPRC);
}
/*
* Extract the PRC (pDatabase) from the ROM
*/
int WritePRC (ROMPtr pROM,
DatabaseHdrPtr pDatabase)
{
UInt32 nBytes = sizeof(DatabaseHdrType);
char* pPRC = NULL;
Int32 nSize;
char FileName[dmDBNameLength + 8];
UInt32 hPRC;
nBytes += (pDatabase->recordList.numRecords *
(IsResource(pDatabase) ? sizeof(RsrcEntryType) :
sizeof(RecordEntryType)));
//fprintf (stdout, "Extracting %ld bytes of '%s'\n",
// nBytes, pDatabase->name);
fprintf (stdout, "Extracting '%s'\n", pDatabase->name);
pPRC = (char*)malloc(nBytes);
if (! pPRC)
return(0);
memcpy(pPRC, pDatabase, nBytes);
if (pDatabase->appInfoID != 0)
{
nSize = CopyChunk(pROM, pDatabase->appInfoID, &pPRC, nBytes);
if (nSize < 0)
{
free(pPRC);
return(0);
}
((DatabaseHdrPtr)pPRC)->appInfoID = nBytes;
nBytes += nSize;
}
if (pDatabase->sortInfoID != 0)
{
nSize = CopyChunk(pROM, pDatabase->sortInfoID, &pPRC, nBytes);
if (nSize < 0)
{
free(pPRC);
return(0);
}
((DatabaseHdrPtr)pPRC)->sortInfoID = nBytes;
nBytes += nSize;
}
if (IsResource(pDatabase))
{
RsrcEntryPtr pItem = (RsrcEntryPtr)
(&(pDatabase->recordList.firstEntry));
RsrcEntryPtr pItemNew;
UInt32 idex;
for (idex = 0; idex < pDatabase->recordList.numRecords; idex++)
{
nSize = CopyChunk(pROM, pItem->localChunkID, &pPRC, nBytes);
if (nSize < 0)
{
free(pPRC);
return(0);
}
// Locate the resource entry in our new PRC
pItemNew = ((RsrcEntryPtr)
(&(((DatabaseHdrPtr)pPRC)->recordList.firstEntry))) + idex;
// (UInt8*)(&(((DatabaseHdrPtr)pPRC)->recordList.firstEntry)) +
// (idex * sizeof(RsrcEntryType));
// Modify the pointer to the resource's data
// This value will NOT be correct until relocation
// occurs (relocation will be done based upon the
// base of the database)
pItemNew->localChunkID = nBytes;
pItem = (RsrcEntryPtr)((UInt8*)pItem + sizeof(RsrcEntryType));
nBytes += nSize;
}
}
else
{
RecordEntryPtr pItem = (RecordEntryPtr)
(&(pDatabase->recordList.firstEntry));
UInt32 idex;
RecordEntryPtr pItemNew;
for (idex = 0; idex < pDatabase->recordList.numRecords; idex++)
{
nSize = CopyChunk(pROM, pItem->localChunkID, &pPRC, nBytes);
if (nSize < 0)
{
free(pPRC);
return(0);
}
pItemNew = ((RecordEntryPtr)
(&(((DatabaseHdrPtr)pPRC)->recordList.firstEntry))) + idex;
// Modify the pointer to the resource's data
// This value will NOT be correct until relocation
// occurs (relocation will be done based upon the
// base of the database)
pItemNew->localChunkID = nBytes;
pItem = (RecordEntryPtr)((UInt8*)pItem + sizeof(RecordEntryType));
nBytes += nSize;
}
}
// Byte swap it...
{
OmOverlaySpecType* pOvly;
pOvly = LocateOverlayResource((DatabaseHdrPtr)pPRC);
if (pOvly)
H2P_OverlaySpec ((OmOverlaySpecType*)(pPRC + (UInt32)pOvly));
}
H2P_DatabaseHdr ((DatabaseHdrPtr)pPRC);
// Finally, output to a file
sprintf (FileName, "%s.%s", pDatabase->name,
(IsResource(pDatabase) ? "prc" : "pdb"));
hPRC = open(FileName, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
if (! hPRC)
{
free(pPRC);
return(0);
}
if (write(hPRC, pPRC, nBytes) != nBytes)
{
free(pPRC);
return(0);
}
close(hPRC);
free(pPRC);
return nBytes;
}
/*
*
*/
int WritePRCs (ROMPtr pROM,
TypeCtorType TypeCtorList[],
UInt32 numEntries)
{
UInt32 idex;
if ((! pROM) || ((numEntries > 0) && (TypeCtorList == NULL)))
return(0);
for (idex = 0; idex < pROM->pDatabaseList->numDatabases ; idex++)
{
DatabaseHdrPtr pDatabase = (DatabaseHdrPtr)
pROM->pDatabaseList->databaseOffset[idex];
if (! IsInTypeCtorList(pDatabase->type, pDatabase->creator,
TypeCtorList, numEntries))
continue;
if (! WritePRC(pROM, pDatabase))
return(0);
}
return(1);
}