-
Notifications
You must be signed in to change notification settings - Fork 0
/
assemble.c
1762 lines (1500 loc) · 43.3 KB
/
assemble.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
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Deal with the assembly of a new Palm ROM given a set of system PRC/PDB
* files.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
/*
* This Cygwin patch is due to Isaac Salzman
*/
#ifndef __CYGWIN
#include <errno.h>
#else
#include <sys/errno.h>
#define sys_errlist _sys_errlist
#endif /* __CYGWIN */
#include "SystemResources.h"
#include "rom.h"
#include "os_structs.h"
#include "utils.h"
#include "assemble.h"
#include "translate.h"
#include "extract.h"
#include "Crc.h"
#include "byte_swap.h"
static void InitializeFreeChunk (MemChunkHeaderUnionType* pChunk,
UInt16 version,
UInt32 size)
{
if (! pChunk)
return;
memset (pChunk, 0, size);
if (version == 0x01)
{
Mem1ChunkHeaderPtr pChunk1 = &pChunk->header.ver1;
pChunk1->flags = memChunkFlagFree;
pChunk1->size = size;
pChunk1->lockOwner = dmDynOwnerID;
}
else
{
MemChunkHeaderPtr pChunk2 = &pChunk->header.ver2;
pChunk2->free = 1;
pChunk2->size = size;
pChunk2->owner = dmDynOwnerID;
}
}
static void InitializeHeap (MemHeapHeaderUnionType* hdr,
UInt16 ver,
UInt32 size)
{
MemChunkHeaderUnionType* pChunk;
UInt16 chunkVer;
if (! hdr)
return;
chunkVer = memUChunkVer(hdr);
// Initialize this entire heap to 0.
memset (hdr, 0, size); //memUSizeOfHeapHeader(ver));
if(ver > 2){
((MemHeapHeaderPtr)hdr)->size = size;
}else if (ver > 1){
((Mem2HeapHeaderPtr)hdr)->size = size;
}else{
((Mem1HeapHeaderPtr)hdr)->size = size;
}
memUHeapFlags(hdr) |= memHeapFlagReadOnly;
switch (ver)
{
case 1:
break;
case 2:
memUHeapFlags(hdr) |= memHeapFlagVers2;
break;
case 3:
memUHeapFlags(hdr) |= memHeapFlagVers3;
hdr->header.ver3.firstFreeChunkOffset = memUSizeOfHeapHeader(ver) >> 1;
break;
case 4:
memUHeapFlags(hdr) |= memHeapFlagVers4;
hdr->header.ver3.firstFreeChunkOffset = memUSizeOfHeapHeader(ver) >> 1;
break;
}
pChunk = memUHeapFirstChunk (hdr, ver);
InitializeFreeChunk (pChunk, chunkVer,
size - memUSizeOfHeapHeader(ver) -
memUSizeOfHeapTerminator(ver));
if (ver > 2)
{
if(chunkVer > 1){
((MemChunkHeaderPtr)hdr)->hOffset = ((MemChunkHeaderPtr)hdr)->hOffset >> 1;
}else{
((Mem1ChunkHeaderPtr)hdr)->hOffset = ((Mem1ChunkHeaderPtr)hdr)->hOffset >> 1;
}
}
}
/* Find the first point in the rom which is not contained in a heap */
static
void* FirstUnmanagedAddress (ROMPtr pROM)
{
UInt16 idex;
MemHeapHeaderUnionType* pHeapHdr;
void* first;
void* candidate;
if (! pROM || ! pROM->pHeapList || pROM->pHeapList->numHeaps == 0)
return NULL;
pHeapHdr = (MemHeapHeaderUnionType*)pROM->pHeapList->heapOffset[0];
first = (UInt8*)pHeapHdr + memUHeapInterpSize (pHeapHdr, memUHeapVer(pHeapHdr));
for (idex = 1; idex < pROM->pHeapList->numHeaps; idex++)
{
pHeapHdr = (MemHeapHeaderUnionType*)pROM->pHeapList->heapOffset[idex];
candidate = (UInt8*)pHeapHdr +
memUHeapInterpSize (pHeapHdr, memUHeapVer(pHeapHdr));
if (candidate > first)
first = candidate;
}
return first;
}
/* Find the first available spot for a heap and add one there */
static
MemHeapHeaderUnionType* AddHeap (ROMPtr pROM,
UInt16 ver,
UInt32 size)
{
MemHeapHeaderUnionType* hdr;
if (! pROM)
return NULL;
/*
* Make sure the requested version of heap header can handle the
* requested size.
*/
if (size > 0x010000 && sizeof (memUHeapSize(hdr,ver)) <= 2)
return NULL;
/* Make sure we can hold at least a heap header and chunk header */
if (size < (memUSizeOfHeapHeader(ver) +
memUSizeOfChunkHeader(memUChunkVerFromHeapVer(ver))))
return NULL;
hdr = FirstUnmanagedAddress (pROM);
if (! hdr)
return NULL;
/* Make sure the rom can hold the requested heap */
if (! IsValidPtr (pROM, (UInt8*)hdr + size))
return NULL;
InitializeHeap (hdr, ver, size);
return hdr;
}
/*
* A blank ROM consists of:
* Card Header
* Storage Header
* Block List (00 00 00 00 FF FF FF FF)
* Heap List
* 4 mystery bytes
* Heap List Header
* Memory Chunk Header (first real one)
*
* NOTE: We probably want to pass in a hint that tells what sort of
* ROM we're building. Otherwise, we will need to do size adjustments
* for the heap and last free chunk when writing out a Small ROM.
*/
ROMPtr InitializeROM (ROMVersion* pVersion,
UInt16 flags)
{
ROMPtr pROM = (ROMPtr)malloc(sizeof(ROMType));
BlockListPtr pSpecial;
if (! pROM || ! pVersion)
{
return(NULL);
}
memset(pROM, 0, sizeof(*pROM));
pROM->flags = flags;
/*
* Use a maximum size (2M) for both Small and Large ROMS.
* This is to guarantee that we never need to realloc
* (which would cause all the pointer that we've set up
* to potentially become invalid -- if realloc moved the memory).
*/
if (flags & RT_LARGE)
{
pROM->ROMSize = pVersion->big_ROMSize;
pROM->CardSize = pVersion->big_cardSize;
}
else
{
pROM->ROMSize = pVersion->small_ROMSize;
pROM->CardSize = pVersion->small_cardSize;
}
/* Make sure the size is a multiple of 4k */
pROM->ROMSize = (pROM->ROMSize + 0x0FFF) & 0xFFFFF000;
if (pROM->CardSize > pROM->ROMSize)
{
free(pROM);
return(NULL);
}
pROM->pROM = (UInt8*)malloc(pROM->ROMSize);
if (! pROM->pROM)
{
FreeROM(pROM);
return(NULL);
}
memset(pROM->pROM, 0xFF, pROM->ROMSize);
pROM->ROM_base = pVersion->ROM_base;
pROM->Card_base = pVersion->ROM_base;
if (flags & RT_LARGE)
pROM->Card_base += pVersion->card_bigROMOffset;
if (flags & RT_NON_EZ)
pROM->Card_base -= 0x10000000;
pROM->File_base = 0;
pROM->pCard = (CardHeaderPtr)(pROM->pROM);
pROM->pStore = (StorageHeaderPtr)(pROM->pCard + 1);
pSpecial = (BlockListPtr)(pROM->pStore + 1);
pROM->pHeapList = (HeapListPtr)(pSpecial + 1);
/*
* Now that we have pointers in place, intialize the structures...
*
* 1) Card header
* initStack will point into 'System's 1st boot resource
* resetVector will point to 'System's 1st boot resource
* halCodeOffset will point to 'SmallHAL' for the Small ROM
* 'BigHAL' for the Large ROM
*/
{
CardHeaderPtr pCard = pROM->pCard;
memset(pCard, 0, sizeof(*pCard));
pCard->signature = sysCardSignature;
pCard->hdrVersion = pVersion->card_hdrVersion;
pCard->flags = pVersion->card_flags;
if (flags & RT_NON_EZ)
pCard->flags = 0x0010;
strncpy(pCard->name, pVersion->card_name,
sizeof(pCard->name));
strncpy(pCard->manuf, pVersion->card_manuf,
sizeof(pCard->manuf));
pCard->version = pVersion->card_version;
pCard->creationDate = unix_time_to_pilot_time(time(NULL));
pCard->numRAMBlocks = 0x0001;
pCard->blockListOffset = (UInt32)pSpecial;
if (pCard->hdrVersion > 0x0001)
{
pCard->readWriteParmsOffset = 0x00000000;
pCard->readWriteParmsSize = 0x00000000;
// Trying to get -0x2000, since the RO params live at 0x6000 from
// the start of the ROM and the big ROM lives at 0x8000.
pCard->readOnlyParmsOffset = pVersion->card_readOnlyParmsOffset -
(pROM->Card_base - pROM->ROM_base) + (UInt32) pROM->pROM;
//0x00006000 + ((UInt32)pROM->pROM);
if (pROM->flags & RT_NON_EZ)
pCard->readOnlyParmsOffset += 0xF0000000;
pCard->bigROMOffset = (UInt32)pROM->pROM;
if (flags & RT_SMALL)
pCard->bigROMOffset += pVersion->card_bigROMOffset;
pCard->checksumBytes = pROM->CardSize;
pCard->checksumValue = 0x00000000;
}
if (pCard->hdrVersion > 0x0002)
{
pCard->readWriteWorkingOffset = 0x00000000;
pCard->readWriteWorkingSize = 0x00000000;
}
if (pCard->hdrVersion > 0x0003)
{
pCard->halCodeOffset = 0x00000000;
}
}
/*
* 2) Storage header
* initCodeOffset1 will point into 'System's 2nd boot resource
* initCodeOffset2 will point into 'System's 3rd boot resource
* databaseDirID will point to the database list
*/
{
StorageHeaderPtr pStore = pROM->pStore;
memset(pStore, 0, sizeof(*pStore));
pStore->signature = sysStoreSignature;
pStore->version = pVersion->store_version;
pStore->flags = pVersion->store_flags;
strncpy(pStore->name, pVersion->store_name,
sizeof(pStore->name));
pStore->creationDate = 0x00000000;
pStore->backupDate = 0x00000000;
pStore->heapListOffset = (UInt32)pROM->pHeapList;
if (pROM->pCard->hdrVersion > 0x0003)
{
pROM->pStore->nvParams.localeLanguage = pVersion->nvparams_localeLanguage;
pROM->pStore->nvParams.localeCountry = pVersion->nvparams_localeCountry;
}
}
/*
* 3) Special - appears to be a block list (whatever that is)
* It seems to always be '0x00000000 0xFFFFFFFF'
*/
memset(pSpecial, 0x00, 4);
memset((UInt8*)pSpecial+4, 0xFF, 4);
/*
* 4) Heap List
*/
{
HeapListPtr pList = pROM->pHeapList;
Int32 TotalHeapSize = (UInt32)pROM->pROM +
pROM->CardSize -
(UInt32)pList -
sizeof(*pList);
UInt32 SingleHeapSize;
UInt16 HeapVer;
UInt16 nHeaps = 0;
UInt16 idex;
HeapVer = memUHeapVerFromFlags(pVersion->heap_flags);
if (HeapVer < 2)
{
/*
* Maximum Heap size is 64K
*/
SingleHeapSize = 0x10000;
}
else
{
/*
* Maximum Heap size is 4G and our ROM can only be 2M
* so we're safe in hardcoding this value
* (until memory becomes REEEALLY cheap)
*/
SingleHeapSize = TotalHeapSize;
}
/*
* Now, we need to figure out how many heaps (given SingleHeapSize)
* the total heap should be divided into...
*/
while (TotalHeapSize > 0)
{
TotalHeapSize -= sizeof(pList->heapOffset[0]) + SingleHeapSize;
nHeaps++;
}
/*
* This may seem off by 1 because we need to include the
* NULL offset item at the end of the list.
*/
TotalHeapSize = (UInt32)pROM->pROM + pROM->CardSize -
(UInt32)pList - sizeof(*pList) -
sizeof(pList->heapOffset[0])*nHeaps;
if (HeapVer > 1)
{
/* Grab the newly computed value */
SingleHeapSize = TotalHeapSize;
}
/* For very early ROMS, the TotalHeapSize is less than 0x10000 */
SingleHeapSize = SingleHeapSize > TotalHeapSize ?
TotalHeapSize : SingleHeapSize;
memset(pList, 0, sizeof(*pList) + sizeof(pList->heapOffset[0])*nHeaps);
/*
* Using our computed sizes, initialize the heap list and
* it's constituent heaps.
*/
//pList->numHeaps = nHeaps;
pList->numHeaps = 1;
pList->heapOffset[0] = (UInt32)((UInt8*)(pList+1) +
nHeaps * sizeof(pList->heapOffset[0]));
InitializeHeap((MemHeapHeaderUnionType*)pList->heapOffset[0], HeapVer,
SingleHeapSize);
for (idex = 1; idex < nHeaps; idex++)
{
UInt32 size = SingleHeapSize;
if (TotalHeapSize < SingleHeapSize)
size = TotalHeapSize;
pList->heapOffset[idex] = (UInt32)AddHeap(pROM, HeapVer, size);
if (! pList->heapOffset[idex])
{
/*
* Assume this is the last heap and it's not big
* enough to contain even a heap header (which AddHeap checks).
*/
break;
}
TotalHeapSize -= size;
pList->numHeaps++;
}
}
pROM->pVersion = pVersion;
return (pROM);
}
/*
* Given a heap header, find the first free memory chunk in the heap
*/
static
MemChunkHeaderUnionType*
LocateFirstFreeChunk (MemHeapHeaderUnionType* pHeapHdr)
{
MemChunkHeaderUnionType* pChunk = NULL;
if (! pHeapHdr)
return(NULL);
/* We have to walk the chunk list to find the first free chunk */
UInt16 ver = memUHeapVer(pHeapHdr);
pChunk = memUHeapFirstChunk (pHeapHdr, ver);
ver = memUChunkVer(pHeapHdr);
while (! memUChunkIsTerminator(pChunk, ver) &&
! memUChunkFree(pChunk,ver))
{
pChunk = (MemChunkHeaderUnionType*)((UInt8*)pChunk +
memUChunkSize(pChunk,ver));
}
if (memUChunkIsTerminator(pChunk, ver))
{
/* We didn't find a free chunk */
pChunk = NULL;
}
return(pChunk);
}
/*
* Look for all the mem chunks that are owned by owner (usually dmPadOwnerID),
* and set free. The hOffset is left alone. For padding purposes, this
* function should only be called after everything else has been allocated in
* ROM, since we don't want
* anything to use our pad.
*/
static
void SetChunksFree (ROMPtr pROM,
UInt8 owner)
{
UInt16 numHeaps;
UInt16 idex;
if (! pROM)
return;
numHeaps = pROM->pHeapList->numHeaps;
for (idex = 0; idex < numHeaps; idex++)
{
MemHeapHeaderUnionType* pHeapHdr =
(MemHeapHeaderUnionType*) pROM->pHeapList->heapOffset[idex];
UInt16 heapVer = memUHeapVer(pHeapHdr);
UInt16 chunkVer = memUChunkVer (pHeapHdr);
MemChunkHeaderUnionType* pChunk = memUHeapFirstChunk (pHeapHdr, heapVer);
while (! memUChunkIsTerminator(pChunk, chunkVer))
{
if (memUChunkOwner (pChunk, chunkVer) == owner)
{
ROMfree(pROM, (void*)memUChunkData (pChunk,chunkVer));
pChunk = memUHeapFirstChunk (pHeapHdr, heapVer);
continue;
}
pChunk = memUChunkNext (pChunk, chunkVer);
}
}
}
/*
* Alloc size bytes, owned by owner in the given rom. owner is dmRecOwnerID
* for database records, and dmMgrOwnerID for everything else, i.e. database
* headers, database directories, ...
*/
static
void* HeapAlloc (MemHeapHeaderUnionType* pHeapHdr,
UInt32 size,
UInt8 owner)
{
UInt32 roundedSize = (size + 1) & 0xFFFFFFFE;
UInt32 newsize = 0;
UInt16 ver;
MemChunkHeaderUnionType* prevFree = NULL;
MemChunkHeaderUnionType* oldFree;
MemChunkHeaderUnionType* newFree;
if (! pHeapHdr)
return(NULL);
ver = memUChunkVer(pHeapHdr);
oldFree = LocateFirstFreeChunk(pHeapHdr);
// Locate a free chunk large enough to accomodate the request
while ((oldFree) && (! memUChunkIsTerminator(oldFree,ver)) &&
(roundedSize > memUChunkSize(oldFree, ver) - memUSizeOfChunkHeader(ver)))
{
prevFree = oldFree;
oldFree = memUChunkNextFree(oldFree, ver);
}
if ((! oldFree) || (memUChunkIsTerminator(oldFree,ver)))
return(NULL);
// Carve out the requested space
newFree = (MemChunkHeaderUnionType*)((UInt8*)oldFree + roundedSize +
memUSizeOfChunkHeader(ver));
newsize = memUChunkSize(oldFree,ver) - roundedSize -
memUSizeOfChunkHeader(ver);
if (newsize < memUSizeOfChunkHeader(ver))
{
/*
* Handle the case where the last chunk is not large
* enough to hold a chunk header
*/
roundedSize += newsize;
newsize = 0;
}
if (newsize)
InitializeFreeChunk (newFree, ver, newsize);
if (memUChunkHOffset(oldFree, ver) && newsize)
{
// Only update the hOffset (pointer to next free chunk) if
// the hOffset of the old free chunk was set.
if(ver>1){
((MemChunkHeaderPtr)newFree)->hOffset = memUChunkHOffset(oldFree, ver) -
((roundedSize + memUSizeOfChunkHeader(ver)) >>1);
}else{
((Mem1ChunkHeaderPtr)newFree)->hOffset = memUChunkHOffset(oldFree, ver) -
((roundedSize + memUSizeOfChunkHeader(ver)) >>1);
}
}
if ((memUHeapVer(pHeapHdr) > 2) && (! prevFree))
{
// Modify the pointer to the first free chunk
if (newsize)
pHeapHdr->header.ver3.firstFreeChunkOffset = ((UInt32)newFree -
(UInt32)pHeapHdr) >> 1;
else
pHeapHdr->header.ver3.firstFreeChunkOffset += memUChunkHOffset(oldFree,ver);
}
if (prevFree)
{
// The previous free chunk now needs to point to the new free chunk.
if (newsize){
if(ver>1){
((MemChunkHeaderPtr)prevFree)->hOffset = ((UInt32)newFree - (UInt32)prevFree) >> 1;
}else{
((Mem1ChunkHeaderPtr)prevFree)->hOffset = ((UInt32)newFree - (UInt32)prevFree) >> 1;
}
}else{
if(ver>1){
((MemChunkHeaderPtr)prevFree)->hOffset += memUChunkHOffset(oldFree,ver);
}else{
((Mem1ChunkHeaderPtr)prevFree)->hOffset += memUChunkHOffset(oldFree,ver);
}
}
}
if (ver > 1)
{
MemChunkHeaderPtr pChunk = &oldFree->header.ver2;
pChunk->free = 0x0;
pChunk->size = roundedSize + memUSizeOfChunkHeader(ver);
pChunk->sizeAdj = roundedSize - size;
pChunk->lockCount = memPtrLockCount;
pChunk->owner = owner;
pChunk->hOffset = 0x0;
}
else
{
Mem1ChunkHeaderPtr pChunk = &oldFree->header.ver1;
pChunk->size = roundedSize + memUSizeOfChunkHeader(ver);
pChunk->flags = roundedSize - size;
pChunk->lockOwner = (memPtrLockCount << 4) | owner;
pChunk->hOffset = 0x0;
}
return memUChunkData (oldFree, ver);
}
/*
* Look in each heap for a free chunk of sufficient size.
* If we find one, allocate the requested bytes there.
*/
void* ROMalloc (ROMPtr pROM,
UInt32 size,
UInt8 owner)
{
UInt32 idex;
void* pMem = NULL;
for (idex = 0; idex < pROM->pHeapList->numHeaps && ! pMem; idex++)
{
pMem = HeapAlloc((MemHeapHeaderUnionType*)pROM->pHeapList->heapOffset[idex],
size,owner);
}
return(pMem);
}
/*
* Given two adjacent free chunks, merge them iff they are contiguous
*/
static int
MergeIfAdjacent (MemChunkHeaderUnionType* pFirst,
MemChunkHeaderUnionType* pSecond,
UInt32 ver)
{
if ((pFirst && pSecond) &&
(! memUChunkIsTerminator(pFirst,ver) && ! memUChunkIsTerminator(pSecond,ver)) &&
(memUChunkFree(pFirst,ver) && memUChunkFree(pSecond,ver)) &&
memUChunkNext(pFirst,ver) == pSecond)
{
/* Free, contiguous and neither are terminators */
if (memUChunkHOffset(pSecond,ver) != 0){
if(ver>1){
((MemChunkHeaderPtr)pFirst)->hOffset += memUChunkHOffset(pSecond,ver);
}else{
((Mem1ChunkHeaderPtr)pFirst)->hOffset += memUChunkHOffset(pSecond,ver);
}
}else{
if(ver>1){
((MemChunkHeaderPtr)pFirst)->hOffset = 0;
}else{
((Mem1ChunkHeaderPtr)pFirst)->hOffset = 0;
}
}
if(ver>1){
((MemChunkHeaderPtr)pFirst)->size += memUChunkSize(pSecond,ver);
}else{
((Mem1ChunkHeaderPtr)pFirst)->size += memUChunkSize(pSecond,ver);
}
return (1);
}
return (0);
}
/*
* Free the given memory from its heap
*/
void ROMfree (ROMPtr pROM,
void* ptr)
{
UInt32 idex;
UInt32 heapVer;
UInt32 chunkVer;
MemHeapHeaderUnionType* pHeap = NULL;
MemChunkHeaderUnionType* pChunk = NULL;
MemChunkHeaderUnionType* pFreeChunk = NULL;
if (! pROM || ! ptr || ! IsValidPtr(pROM, ptr))
return;
for (idex = 0; idex < pROM->pHeapList->numHeaps; idex++)
{
pHeap = (MemHeapHeaderUnionType*)pROM->pHeapList->heapOffset[idex];
heapVer = memUHeapVer(pHeap);
if (((UInt32)pHeap < (UInt32)ptr) &&
((UInt32)ptr < ((UInt32)pHeap + memUHeapInterpSize(pHeap,heapVer))))
break;
pHeap = NULL;
}
if (! pHeap)
/* ARRRGH!! Cannot locate the memory in any heaps?!?!?!? */
return;
/*
* Grab a pointer to the chunk header that contains 'ptr'
*/
chunkVer = memUChunkVerFromHeapVer(heapVer);
pChunk = LocateAddrInHeap(pHeap, (UInt32)ptr);
if (ptr != (void*)memUChunkData (pChunk, chunkVer) || memUChunkFree (pChunk,chunkVer))
{
// Attempt to free memory in the middle of a chunk
return;
}
// Initialize this chunk as a free chunk.
InitializeFreeChunk(pChunk, chunkVer, memUChunkSize(pChunk, chunkVer));
pFreeChunk = LocateFirstFreeChunk(pHeap);
if ((! pFreeChunk) || (pFreeChunk > pChunk))
{
// This new free chunk will be the first one on the free list.
if (pFreeChunk){
if(chunkVer>1){
((MemChunkHeaderPtr)pChunk)->hOffset = ((UInt32)pFreeChunk - (UInt32)pChunk);
}else{
((Mem1ChunkHeaderPtr)pChunk)->hOffset = ((UInt32)pFreeChunk - (UInt32)pChunk) >> 1;
}
}
if (heapVer > 2)
{
if (! pFreeChunk){
// In version 3 and above, the final free chunk points
// to the heap terminator.
if(chunkVer>1){
((MemChunkHeaderPtr)pChunk)->hOffset = ((UInt32)pHeap +
memUHeapSize(pHeap, heapVer) -
memUSizeOfHeapTerminator(chunkVer) -
(UInt32)pChunk) >> 1;
}else{
((Mem1ChunkHeaderPtr)pChunk)->hOffset = ((UInt32)pHeap +
memUHeapSize(pHeap, heapVer) -
memUSizeOfHeapTerminator(chunkVer) -
(UInt32)pChunk) >> 1;
}
}
pHeap->header.ver3.firstFreeChunkOffset = ((UInt32)pChunk - (UInt32)pHeap)>>1;
}
}
else
{
// This new free chunk is somewhere on the list (possibly at the end, but
// NOT at the beginning)
UInt32 hOffset;
while (((hOffset = memUChunkHOffset(pFreeChunk,chunkVer)) != 0) &&
(pChunk > memUChunkNextFree(pFreeChunk,chunkVer)))
{
pFreeChunk = memUChunkNextFree(pFreeChunk,chunkVer);
}
// pFreeChunk should be pointing to the free chunk that should
// immediately preceed us.
if (hOffset)
hOffset = ((UInt32)memUChunkNextFree(pFreeChunk,chunkVer) - (UInt32)pChunk)
>> 1;
if(chunkVer>1){
((MemChunkHeaderPtr)pChunk)->hOffset = hOffset;
((MemChunkHeaderPtr)pFreeChunk)->hOffset = ((UInt32)pChunk - (UInt32)pFreeChunk)>>1;
}else{
((Mem1ChunkHeaderPtr)pChunk)->hOffset = hOffset;
((Mem1ChunkHeaderPtr)pFreeChunk)->hOffset = ((UInt32)pChunk - (UInt32)pFreeChunk)>>1;
}
}
MergeIfAdjacent(pChunk, memUChunkNextFree(pChunk,chunkVer), chunkVer);
if (pFreeChunk)
MergeIfAdjacent(pFreeChunk, pChunk, chunkVer);
}
/*
* Add some portion of the provided PRC to the ROM. The flags indicate which
* part of the PRC (either the header or the records) to add. Returns 1 on
* success and 0 on failure.
*
* Flag values:
* PR_HEADER
* PR_RECORDS (includes the app info block and sort info block, if
* any)
*/
int AddPRC (ROMPtr pROM,
DatabaseListPtr pDBList,
PRCPtr pPRC,
UInt16 flags,
int DBIdex)
{
UInt32 DBHeaderSize;
DatabaseHdrPtr pDB;
RecordListPtr pList;
RsrcEntryPtr pRsrc;
RecordEntryPtr pRecord;
RsrcEntryPtr pRsrcNew;
RecordEntryPtr pRecordNew;
int idex;
if (! pROM || ! pDBList || ! pPRC || ! pPRC->pDB)
return 0;
pList = &(pPRC->pDB->recordList);
pRsrc = (RsrcEntryPtr)(&pList->firstEntry);
pRecord = (RecordEntryPtr)(&pList->firstEntry);
DBHeaderSize = sizeof (*pDB) + pList->numRecords *
(IsResource (pPRC->pDB) ? sizeof (RsrcEntryType) :
sizeof (RecordEntryType));
if (flags & PR_HEADER)
{
/* We haven't done the header, so do it now */
pDB = (DatabaseHdrPtr) ROMalloc (pROM, DBHeaderSize, dmMgrOwnerID);
if (! pDB)
return 0;
memcpy (pDB, pPRC->pDB, DBHeaderSize);
pDBList->databaseOffset[DBIdex] = (UInt32)pDB;
}
if (pDBList->databaseOffset[DBIdex])
{
/* We've already done the header, so we need to grab it to correct the
* pointers in the record list in ROM */
pDB = (DatabaseHdrPtr) pDBList->databaseOffset[DBIdex];
}
else
{
/* We haven't done the header, but we still have to keep track of the
* record list */
pDB = pPRC->pDB;
}
/*
* By now, pDB points to the correct place to update the record list
* pointers, whether or not they are in the ROM.
*/
if (! (flags & PR_RECORDS))
return 1;
pRsrcNew = (RsrcEntryPtr)(&pDB->recordList.firstEntry);
pRecordNew = (RecordEntryPtr)(&pDB->recordList.firstEntry);
/* Copy and Relocate the appinfo block */
if (pPRC->pDB->appInfoID)
{
UInt32 appInfoSize;
LocalID appInfoID;
/* Figure out how big the appInfoBlock is */
if (pPRC->pDB->sortInfoID)
{
appInfoSize = pPRC->pDB->sortInfoID - pPRC->pDB->appInfoID;
}
else if (pList->numRecords > 0)
{
if (IsResource(pPRC->pDB))
appInfoSize = pRsrc->localChunkID - pPRC->pDB->appInfoID;
else
appInfoSize = pRecord->localChunkID - pPRC->pDB->appInfoID;
}
else
{
appInfoSize = pPRC->nBytes -
(pPRC->pDB->appInfoID - (UInt32)pPRC->pDB);
}
/* Allocate space for it in the rom and copy it over */
appInfoID = pPRC->pDB->appInfoID;
pDB->appInfoID = (LocalID)ROMalloc (pROM, appInfoSize, dmRecOwnerID);
if (! pDB->appInfoID)
return 0;
memcpy ((char*)pDB->appInfoID, (char*)appInfoID,appInfoSize);
}
/* Copy and Relocate the sortinfo block */
if (pPRC->pDB->sortInfoID)
{
UInt32 sortInfoSize;
LocalID sortInfoID;
/* Figure out how big the sortInfoBlock is */
if (pList->numRecords > 0)
{
if (IsResource(pPRC->pDB))
sortInfoSize = pRsrc->localChunkID - pPRC->pDB->sortInfoID;
else
sortInfoSize = pRecord->localChunkID - pPRC->pDB->sortInfoID;
}
else
{
sortInfoSize = pPRC->nBytes -
(pPRC->pDB->sortInfoID - (UInt32)pPRC->pDB);
}
/* Allocate space for it in the rom and copy it over */
sortInfoID = pPRC->pDB->sortInfoID;
pDB->sortInfoID = (LocalID)ROMalloc (pROM, sortInfoSize, dmRecOwnerID);
if (! pDB->sortInfoID)
return 0;
memcpy ((char*)pDB->sortInfoID, (char*)sortInfoID, sortInfoSize);
}
/* Copy and Relocate the resources (or records) */
for (idex = 0; idex < pList->numRecords; idex++)
{
int recordSize;
LocalID recordFrom;
LocalID recordTo;
/* How big is this resource/record, and where did it come from? */
if (IsResource (pDB))
{
if (idex < pList->numRecords - 1)
{
recordSize = (pRsrc + 1)->localChunkID - pRsrc->localChunkID;
}
else
{
recordSize = pPRC->nBytes -
(pRsrc->localChunkID - (UInt32)pPRC->pDB);
}
recordFrom = pRsrc->localChunkID;
}
else
{
if (idex < pList->numRecords - 1)
{
recordSize = (pRecord+1)->localChunkID - pRecord->localChunkID;
}
else
{
recordSize = pPRC->nBytes -
(pRecord->localChunkID - (UInt32)pPRC->pDB);
}
recordFrom = pRecord->localChunkID;
}
/* Copy the data */
recordTo = (LocalID)ROMalloc (pROM, recordSize, dmRecOwnerID);
if (! recordTo)
return 0;
memcpy ((char*)recordTo, (char*)recordFrom, recordSize);
/* Remember where we put it */
if (IsResource(pDB))
{
pRsrcNew->localChunkID = recordTo;
}
else
{