forked from MoatLab/FEMU
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsbe_xip_image.c
2572 lines (2013 loc) · 69.9 KB
/
sbe_xip_image.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
/* IBM_PROLOG_BEGIN_TAG */
/* This is an automatically generated prolog. */
/* */
/* $Source: src/usr/hwpf/hwp/build_winkle_images/p8_slw_build/sbe_xip_image.c $ */
/* */
/* OpenPOWER HostBoot Project */
/* */
/* Contributors Listed Below - COPYRIGHT 2012,2015 */
/* [+] International Business Machines Corp. */
/* */
/* */
/* Licensed under the Apache License, Version 2.0 (the "License"); */
/* you may not use this file except in compliance with the License. */
/* You may obtain a copy of the License at */
/* */
/* http://www.apache.org/licenses/LICENSE-2.0 */
/* */
/* Unless required by applicable law or agreed to in writing, software */
/* distributed under the License is distributed on an "AS IS" BASIS, */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or */
/* implied. See the License for the specific language governing */
/* permissions and limitations under the License. */
/* */
/* IBM_PROLOG_END_TAG */
// $Id: sbe_xip_image.c,v 1.31 2015/07/29 23:40:06 cmolsen Exp $
// $Source: /afs/awd/projects/eclipz/KnowledgeBase/.cvsroot/eclipz/chips/p8/working/procedures/ipl/sbe/sbe_xip_image.c,v $
//-----------------------------------------------------------------------------
// *! (C) Copyright International Business Machines Corp. 2011
// *! All Rights Reserved -- Property of IBM
// *! *** ***
//-----------------------------------------------------------------------------
// *! OWNER NAME: Bishop Brock Email: [email protected]
//------------------------------------------------------------------------------
/// \file sbe_xip_image.c
/// \brief APIs for validating, normalizing, searching and manipulating
/// SBE-XIP images.
///
/// The background, APIs and implementation details are documented in the
/// document "SBE-XIP Binary format" currently available at this link:
///
/// - https://mcdoc.boeblingen.de.ibm.com/out/out.ViewDocument.php?documentid=2678
///
/// \bug The sbe_xip_validate() API should be carefully reviewed to ensure
/// that validating even a corrupt image can not lead to a segfault, i.e., to
/// ensure that no memory outside of the putative bounds of the image is ever
/// referenced during validation.
#ifndef PLIC_MODULE
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#endif // PLIC_MODULE
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "sbe_xip_image.h"
////////////////////////////////////////////////////////////////////////////
// Local Functions
////////////////////////////////////////////////////////////////////////////
// PHYP has their own way of implementing the <string.h> functions. PHYP also
// does not allow static functions or data, so all of the XIP_STATIC functions
// defined here are global to PHYP.
#ifdef PPC_HYP
#ifdef PLIC_MODULE
#define strcpy(dest, src) hvstrcpy(dest, src)
#define strlen(s) hvstrlen(s)
#define strcmp(s1, s2) hvstrcmp(s1, s2)
#endif //PLIC_MODULE
#define XIP_STATIC
#else // PPC_HYP
#define XIP_STATIC static
#endif // PPC_HYP
#ifdef DEBUG_SBE_XIP_IMAGE
// Debugging support, normally disabled. All of the formatted I/O you see in
// the code is effectively under this switch.
#ifdef __FAPI
#include "fapi.H"
#define fprintf(stream, ...) FAPI_ERR(__VA_ARGS__)
#define printf(...) FAPI_INF(__VA_ARGS__)
#define TRACE_NEWLINE ""
#else // __FAPI
#include <stdio.h>
#define TRACE_NEWLINE "\n"
#endif // __FAPI
// Portable formatting of uint64_t. The ISO C99 standard requires
// __STDC_FORMAT_MACROS to be defined in order for PRIx64 etc. to be defined.
#define __STDC_FORMAT_MACROS
#include <inttypes.h>
#define F0x016llx "0x%016" PRIx64
#define F0x012llx "0x%012" PRIx64
XIP_STATIC SBE_XIP_ERROR_STRINGS(sbe_xip_error_strings);
#define TRACE_ERROR(x) \
({ \
fprintf(stderr, "%s:%d : Returning error code %d : %s" TRACE_NEWLINE, \
__FILE__, __LINE__, (x), \
SBE_XIP_ERROR_STRING(sbe_xip_error_strings, (x))); \
(x); \
})
#define TRACE_ERRORX(x, ...) \
({ \
TRACE_ERROR(x); \
fprintf(stderr, ##__VA_ARGS__); \
(x); \
})
// Uncomment these if required for debugging, otherwise we get warnings from
// GCC as they are not otherwise used.
#if 0
XIP_STATIC uint32_t xipRevLe32(const uint32_t i_x);
XIP_STATIC SBE_XIP_TYPE_STRINGS(type_strings);
XIP_STATIC void
dumpToc(int index, SbeXipToc* toc)
{
printf("TOC entry %d @ %p\n"
" iv_id = 0x%08x\n"
" iv_data = 0x%08x\n"
" iv_type = %s\n"
" iv_section = 0x%02x\n"
" iv_elements = %d\n",
index, toc,
xipRevLe32(toc->iv_id),
xipRevLe32(toc->iv_data),
SBE_XIP_TYPE_STRING(type_strings, toc->iv_type),
toc->iv_section,
toc->iv_elements);
}
#endif
#if 0
XIP_STATIC void
dumpItem(SbeXipItem* item)
{
printf("SbeXipItem @ %p\n"
" iv_toc = %p\n"
" iv_address = " F0x016llx "\n"
" iv_imageData = %p\n"
" iv_id = %s\n"
" iv_type = %s\n"
" iv_elements = %d\n",
item,
item->iv_toc,
item->iv_address,
item->iv_imageData,
item->iv_id,
SBE_XIP_TYPE_STRING(type_strings, item->iv_type),
item->iv_elements);
dumpToc(-1, item->iv_toc);
}
#endif /* 0 */
XIP_STATIC void
dumpSectionTable(const void* i_image)
{
int i, rc;
SbeXipSection section;
printf("Section table dump of image @ %p\n"
" Entry Offset Size\n"
"-------------------------------\n",
i_image);
for (i = 0; i < SBE_XIP_SECTIONS; i++) {
rc = sbe_xip_get_section(i_image, i, §ion);
if (rc) {
printf(">>> dumpSectionTable got error at entry %d : %s\n",
i, SBE_XIP_ERROR_STRING(sbe_xip_error_strings, rc));
break;
}
printf("%7d 0x%08x 0x%08x\n",
i, section.iv_offset, section.iv_size);
}
}
#else
#define TRACE_ERROR(x) (x)
#define TRACE_ERRORX(x, ...) (x)
#define dumpToc(...)
#define dumpItem(...)
#define dumpSectionTable(...)
#endif
// Note: For maximum flexibility we provide private versions of
// endian-conversion routines rather than counting on a system-specific header
// to provide these.
/// Byte-reverse a 16-bit integer if on a little-endian machine
XIP_STATIC uint16_t
xipRevLe16(const uint16_t i_x)
{
uint16_t rx;
#ifndef _BIG_ENDIAN
uint8_t *pix = (uint8_t*)(&i_x);
uint8_t *prx = (uint8_t*)(&rx);
prx[0] = pix[1];
prx[1] = pix[0];
#else
rx = i_x;
#endif
return rx;
}
/// Byte-reverse a 32-bit integer if on a little-endian machine
XIP_STATIC uint32_t
xipRevLe32(const uint32_t i_x)
{
uint32_t rx;
#ifndef _BIG_ENDIAN
uint8_t *pix = (uint8_t*)(&i_x);
uint8_t *prx = (uint8_t*)(&rx);
prx[0] = pix[3];
prx[1] = pix[2];
prx[2] = pix[1];
prx[3] = pix[0];
#else
rx = i_x;
#endif
return rx;
}
/// Byte-reverse a 64-bit integer if on a little-endian machine
XIP_STATIC uint64_t
xipRevLe64(const uint64_t i_x)
{
uint64_t rx;
#ifndef _BIG_ENDIAN
uint8_t *pix = (uint8_t*)(&i_x);
uint8_t *prx = (uint8_t*)(&rx);
prx[0] = pix[7];
prx[1] = pix[6];
prx[2] = pix[5];
prx[3] = pix[4];
prx[4] = pix[3];
prx[5] = pix[2];
prx[6] = pix[1];
prx[7] = pix[0];
#else
rx = i_x;
#endif
return rx;
}
/// What is the image link address?
XIP_STATIC uint64_t
xipLinkAddress(const void* i_image)
{
return xipRevLe64(((SbeXipHeader*)i_image)->iv_linkAddress);
}
/// What is the image size?
XIP_STATIC uint32_t
xipImageSize(const void* i_image)
{
return xipRevLe32(((SbeXipHeader*)i_image)->iv_imageSize);
}
/// Set the image size
XIP_STATIC void
xipSetImageSize(void* io_image, const size_t i_size)
{
((SbeXipHeader*)io_image)->iv_imageSize = xipRevLe32(i_size);
}
/// Re-establish the required final alignment
XIP_STATIC void
xipFinalAlignment(void* io_image)
{
uint32_t size;
size = xipImageSize(io_image);
if ((size % SBE_XIP_FINAL_ALIGNMENT) != 0) {
xipSetImageSize(io_image,
size + (SBE_XIP_FINAL_ALIGNMENT -
(size % SBE_XIP_FINAL_ALIGNMENT)));
}
}
/// Compute a host address from an image address and offset
XIP_STATIC void*
xipHostAddressFromOffset(const void* i_image, const uint32_t offset)
{
return (void*)((unsigned long)i_image + offset);
}
/// Convert a PORE address to a host address
XIP_STATIC void*
xipPore2Host(const void* i_image, const uint64_t i_poreAddress)
{
return xipHostAddressFromOffset(i_image,
i_poreAddress - xipLinkAddress(i_image));
}
XIP_STATIC int
xipValidatePoreAddress(const void* i_image,
const uint64_t i_poreAddress,
const uint32_t size)
{
int rc;
if ((i_poreAddress < xipLinkAddress(i_image)) ||
(i_poreAddress > (xipLinkAddress(i_image) +
xipImageSize(i_image) -
size))) {
rc = TRACE_ERRORX(SBE_XIP_INVALID_ARGUMENT,
"The PORE address " F0x012llx
" is outside the bounds "
"of the image ("
F0x012llx ":" F0x012llx
") for %u-byte access.\n",
i_poreAddress,
xipLinkAddress(i_image),
xipLinkAddress(i_image) + xipImageSize(i_image) - 1,
size);
} else {
rc = 0;
}
return rc;
}
/// Get the magic number from the image
XIP_STATIC uint64_t
xipMagic(const void* i_image)
{
return xipRevLe64(((SbeXipHeader*)i_image)->iv_magic);
}
/// Get the header version from the image
XIP_STATIC uint8_t
xipHeaderVersion(const void* i_image)
{
return ((SbeXipHeader*)i_image)->iv_headerVersion;
}
/// Has the image been normalized?
XIP_STATIC uint8_t
xipNormalized(const void* i_image)
{
return ((SbeXipHeader*)i_image)->iv_normalized;
}
/// Has the image TOC been sorted?
XIP_STATIC uint8_t
xipSorted(const void* i_image)
{
return ((SbeXipHeader*)i_image)->iv_tocSorted;
}
/// A quick check that the image exists, has the correct magic and header
/// version, and optionally is normalized.
XIP_STATIC int
xipQuickCheck(const void* i_image, const int i_normalizationRequired)
{
int rc;
do {
rc = 0;
if (i_image == 0) {
rc = TRACE_ERRORX(SBE_XIP_IMAGE_ERROR,
"Image pointer is NULL (0)\n");
break;
}
if ((xipMagic(i_image) >> 32) != SBE_XIP_MAGIC) {
rc = TRACE_ERRORX(SBE_XIP_IMAGE_ERROR,
"Magic number mismatch; Found "
"" F0x016llx ", expected 0x%08x........\n",
xipMagic(i_image), SBE_XIP_MAGIC);
break;
}
if ((xipHeaderVersion(i_image)) != SBE_XIP_HEADER_VERSION) {
rc = TRACE_ERRORX(SBE_XIP_IMAGE_ERROR,
"Header version mismatch; Expecting %d, "
"found %d\n",
SBE_XIP_HEADER_VERSION,
xipHeaderVersion(i_image));
break;
}
if (i_normalizationRequired && !xipNormalized(i_image)) {
rc = TRACE_ERRORX(SBE_XIP_NOT_NORMALIZED,
"Image not normalized\n");
break;
}
} while(0);
return rc;
}
/// Convert a 32-bit relocatable offset to a full PORE 48-bit address
XIP_STATIC uint64_t
xipFullAddress(const void* i_image, uint32_t offset)
{
return (xipLinkAddress(i_image) & 0x0000ffff00000000ull) + offset;
}
/// Translate a section table entry
XIP_STATIC void
xipTranslateSection(SbeXipSection* o_dest, const SbeXipSection* i_src)
{
#ifndef _BIG_ENDIAN
#if SBE_XIP_HEADER_VERSION != 8
#error This code assumes the SBE-XIP header version 8 layout
#endif
o_dest->iv_offset = xipRevLe32(i_src->iv_offset);
o_dest->iv_size = xipRevLe32(i_src->iv_size);
o_dest->iv_alignment = i_src->iv_alignment;
o_dest->iv_reserved8[0] = 0;
o_dest->iv_reserved8[1] = 0;
o_dest->iv_reserved8[2] = 0;
#else
if (o_dest != i_src) {
*o_dest = *i_src;
}
#endif /* _BIG_ENDIAN */
}
/// Translate a TOC entry
XIP_STATIC void
xipTranslateToc(SbeXipToc* o_dest, SbeXipToc* i_src)
{
#ifndef _BIG_ENDIAN
#if SBE_XIP_HEADER_VERSION != 8
#error This code assumes the SBE-XIP header version 8 layout
#endif
o_dest->iv_id = xipRevLe32(i_src->iv_id);
o_dest->iv_data = xipRevLe32(i_src->iv_data);
o_dest->iv_type = i_src->iv_type;
o_dest->iv_section = i_src->iv_section;
o_dest->iv_elements = i_src->iv_elements;
o_dest->iv_pad = 0;
#else
if (o_dest != i_src) {
*o_dest = *i_src;
}
#endif /* _BIG_ENDIAN */
}
/// Find the final (highest-address) section of the image
XIP_STATIC int
xipFinalSection(const void* i_image, int* o_sectionId)
{
int i, rc, found;
uint32_t offset;
SbeXipHeader hostHeader;
sbe_xip_translate_header(&hostHeader, (SbeXipHeader*)i_image);
found = 0;
offset = 0;
*o_sectionId = 0; /* Make GCC -O3 happy */
for (i = 0; i < SBE_XIP_SECTIONS; i++) {
if ((hostHeader.iv_section[i].iv_size != 0) &&
(hostHeader.iv_section[i].iv_offset >= offset)) {
*o_sectionId = i;
offset = hostHeader.iv_section[i].iv_offset;
found = 1;
}
}
if (!found) {
rc = TRACE_ERRORX(SBE_XIP_IMAGE_ERROR, "The image is empty\n");
} else {
rc = 0;
}
return rc;
}
/// Return a pointer to an image-format section table entry
XIP_STATIC int
xipGetSectionPointer(const void* i_image,
const int i_sectionId,
SbeXipSection** o_imageSection)
{
int rc;
if ((i_sectionId < 0) || (i_sectionId >= SBE_XIP_SECTIONS)) {
rc = TRACE_ERROR(SBE_XIP_INVALID_ARGUMENT);
} else {
*o_imageSection =
&(((SbeXipHeader*)i_image)->iv_section[i_sectionId]);
rc = 0;
}
return rc;
}
/// Restore a section table entry from host format to image format.
XIP_STATIC int
xipPutSection(const void* i_image,
const int i_sectionId,
SbeXipSection* i_hostSection)
{
int rc;
SbeXipSection *imageSection = NULL;
rc = xipGetSectionPointer(i_image, i_sectionId, &imageSection);
if (!rc) {
xipTranslateSection(imageSection, i_hostSection);
}
return rc;
}
/// Set the offset of a section
XIP_STATIC int
xipSetSectionOffset(void* io_image, const int i_section,
const uint32_t i_offset)
{
SbeXipSection* section = NULL;
int rc;
rc = xipGetSectionPointer(io_image, i_section, §ion);
if (!rc) {
section->iv_offset = xipRevLe32(i_offset);
}
return rc;
}
/// Set the size of a section
XIP_STATIC int
xipSetSectionSize(void* io_image, const int i_section, const uint32_t i_size)
{
SbeXipSection* section = NULL;
int rc;
rc = xipGetSectionPointer(io_image, i_section, §ion);
if (!rc) {
section->iv_size = xipRevLe32(i_size);
}
return rc;
}
/// Translate a PORE address in the image to a section and offset
// We first check to be sure that the PORE address is contained in the image,
// using the full 48-bit form. Then we scan the section table to see which
// section contains the address - if none then the image is corrupted. We can
// (must) use the 32-bit offset form of the address here.
XIP_STATIC int
xipPore2Section(const void* i_image,
const uint64_t i_poreAddress,
int* o_section,
uint32_t* o_offset)
{
int rc, sectionId;
SbeXipSection section;
uint32_t addressOffset;
do {
rc = 0;
if ((i_poreAddress < xipLinkAddress(i_image)) ||
(i_poreAddress >
(xipLinkAddress(i_image) + xipImageSize(i_image)))) {
rc = TRACE_ERRORX(SBE_XIP_INVALID_ARGUMENT,
"pore2section: The i_poreAddress argument "
"(" F0x016llx ")\nis outside the bounds of the "
"image (" F0x016llx ":" F0x016llx ")\n",
i_poreAddress,
xipLinkAddress(i_image),
xipLinkAddress(i_image) + xipImageSize(i_image));
break;
}
addressOffset = (i_poreAddress - xipLinkAddress(i_image)) & 0xffffffff;
for (sectionId = 0; sectionId < SBE_XIP_SECTIONS; sectionId++) {
rc = sbe_xip_get_section(i_image, sectionId, §ion);
if (rc) {
rc = TRACE_ERROR(SBE_XIP_BUG); /* Can't happen */
break;
}
if ((section.iv_size != 0) &&
(addressOffset >= section.iv_offset) &&
(addressOffset < (section.iv_offset + section.iv_size))) {
break;
}
}
if (rc) break;
if (sectionId == SBE_XIP_SECTIONS) {
rc = TRACE_ERRORX(SBE_XIP_IMAGE_ERROR,
"Error processing PORE address " F0x016llx ". "
"The address is not mapped in any section.\n"
"A section table dump appears below\n",
i_poreAddress);
dumpSectionTable(i_image);
break;
}
*o_section = sectionId;
*o_offset = addressOffset - section.iv_offset;
} while(0);
return rc;
}
/// Get the information required to search the TOC.
///
/// All return values are optional.
XIP_STATIC int
xipGetToc(void* i_image,
SbeXipToc** o_toc,
size_t* o_entries,
int* o_sorted,
char** o_strings)
{
int rc;
SbeXipSection tocSection, stringsSection;
do {
rc = sbe_xip_get_section(i_image, SBE_XIP_SECTION_TOC, &tocSection);
if (rc) break;
rc = sbe_xip_get_section(i_image, SBE_XIP_SECTION_STRINGS,
&stringsSection);
if (rc) break;
if (o_toc) {
*o_toc = (SbeXipToc*)((uint8_t*)i_image + tocSection.iv_offset);
}
if (o_entries) {
*o_entries = tocSection.iv_size / sizeof(SbeXipToc);
}
if (o_sorted) {
*o_sorted = xipSorted(i_image);
}
if (o_strings) {
*o_strings = (char*)i_image + stringsSection.iv_offset;
}
} while (0);
return rc;
}
/// Compare two normalized TOC entries for sorting.
XIP_STATIC int
xipCompareToc(const SbeXipToc* i_a, const SbeXipToc* i_b,
const char* i_strings)
{
return strcmp(i_strings + xipRevLe32(i_a->iv_id),
i_strings + xipRevLe32(i_b->iv_id));
}
/// Iterative quicksort of the TOC
// Note: The stack requirement is limited to 256 bytes + minor local storage.
XIP_STATIC void
xipQuickSort(SbeXipToc* io_toc, int i_left, int i_right,
const char* i_strings)
{
int i, j, left, right, sp;
SbeXipToc pivot, temp;
uint32_t stack[64];
sp = 0;
stack[sp++] = i_left;
stack[sp++] = i_right;
while (sp) {
right = stack[--sp];
left = stack[--sp];
i = left;
j = right;
pivot = io_toc[(i + j) / 2];
while (i <= j) {
while (xipCompareToc(&(io_toc[i]), &pivot, i_strings) < 0) {
i++;
}
while (xipCompareToc(&(io_toc[j]), &pivot, i_strings) > 0) {
j--;
}
if (i <= j) {
temp = io_toc[i];
io_toc[i] = io_toc[j];
io_toc[j] = temp;
i++;
j--;
}
}
if (left < j) {
stack[sp++] = left;
stack[sp++] = j;
}
if (i < right) {
stack[sp++] = i;
stack[sp++] = right;
}
}
}
/// TOC linear search
XIP_STATIC int
xipLinearSearch(void* i_image, const char* i_id, SbeXipToc** o_entry)
{
int rc;
SbeXipToc *imageToc, hostToc;
size_t entries;
char* strings;
*o_entry = 0;
rc = xipGetToc(i_image, &imageToc, &entries, 0, &strings);
if (!rc) {
for (; entries; entries--, imageToc++) {
xipTranslateToc(&hostToc, imageToc);
if (strcmp(i_id, strings + hostToc.iv_id) == 0) {
break;
}
}
if (entries) {
*o_entry = imageToc;
rc = 0;
} else {
*o_entry = 0;
rc = TRACE_ERROR(SBE_XIP_ITEM_NOT_FOUND);
}
}
return rc;
}
/// A classic binary search of a (presumed) sorted array
XIP_STATIC int
xipBinarySearch(void* i_image, const char* i_id, SbeXipToc** o_entry)
{
int rc;
SbeXipToc *imageToc;
size_t entries;
char* strings;
int sorted, left, right, next, sort;
do {
*o_entry = 0;
rc = xipGetToc(i_image, &imageToc, &entries, &sorted, &strings);
if (rc) break;
if (!sorted) {
rc = TRACE_ERROR(SBE_XIP_BUG);
break;
}
left = 0;
right = entries - 1;
while (left <= right) {
next = (left + right) / 2;
sort = strcmp(i_id, strings + xipRevLe32(imageToc[next].iv_id));
if (sort == 0) {
*o_entry = &(imageToc[next]);
break;
} else if (sort < 0) {
right = next - 1;
} else {
left = next + 1;
}
}
if (*o_entry == 0) {
rc = TRACE_ERROR(SBE_XIP_ITEM_NOT_FOUND);
break;
}
} while (0);
return rc;
}
/// Validate a TOC entry as a mapping function
///
/// The TOC is validated by searching for the entry, which will uncover
/// duplicate entries or problems with sorting/searching.
XIP_STATIC int
xipValidateTocEntry(void* io_image, const SbeXipItem* i_item, void* io_arg)
{
int rc;
SbeXipItem found;
(void)io_arg;
do {
rc = sbe_xip_find(io_image, i_item->iv_id, &found);
if (rc) {
rc = TRACE_ERRORX(rc, "TOC entry for %s not found\n",
i_item->iv_id);
} else if (found.iv_toc != i_item->iv_toc) {
rc = TRACE_ERRORX(SBE_XIP_TOC_ERROR,
"Duplicate TOC entry for '%s'\n", i_item->iv_id);
}
break;
} while (0);
return rc;
}
// This is the FNV-1a hash, used for hashing symbol names in the .fixed
// section into 32-bit hashes for the mini-TOC.
// According to the authors:
// "FNV hash algorithms and source code have been released into the public
// domain. The authors of the FNV algorithmm look deliberate steps to disclose
// the algorhtm (sic) in a public forum soon after it was invented. More than
// a year passed after this public disclosure and the authors deliberately took
// no steps to patent the FNV algorithm. Therefore it is safe to say that the
// FNV authors have no patent claims on the FNV algorithm as published."
#define FNV_OFFSET_BASIS 2166136261u
#define FNV_PRIME32 16777619u
static uint32_t
xipHash32(const char* s)
{
uint32_t hash;
hash = FNV_OFFSET_BASIS;
while (*s) {
hash ^= *s++;
hash *= FNV_PRIME32;
}
return hash;
}
// Normalize a TOC entry
// Normalize the TOC entry by converting relocatable pointers into 32-bit
// offsets from the beginning of the section containing the data. All
// addresses in the TOC are actually 32-bit offsets in the address space named
// in bits 16:31 of the link address of the image.
XIP_STATIC int
xipNormalizeToc(void* io_image, SbeXipToc *io_imageToc,
SbeXipHashedToc** io_fixedTocEntry,
size_t* io_fixedEntriesRemaining)
{
SbeXipToc hostToc;
int idSection, dataSection;
uint32_t idOffset, dataOffset;
char* hostString;
int rc;
do {
// Translate the TOC entry to host format. Then locate the
// sections/offsets of the Id string (which must be in .strings) and
// the data.
xipTranslateToc(&hostToc, io_imageToc);
hostString =
(char*)xipPore2Host(io_image,
xipFullAddress(io_image, hostToc.iv_id));
rc = xipPore2Section(io_image,
xipFullAddress(io_image, hostToc.iv_id),
&idSection,
&idOffset);
if (rc) break;
if (idSection != SBE_XIP_SECTION_STRINGS) {
rc = TRACE_ERROR(SBE_XIP_IMAGE_ERROR);
break;
}
rc = xipPore2Section(io_image,
xipFullAddress(io_image, hostToc.iv_data),
&dataSection,
&dataOffset);
if (rc) break;
// Now replace the Id and data pointers with their offsets, and update
// the data section in the TOC entry.
hostToc.iv_id = idOffset;
hostToc.iv_data = dataOffset;
hostToc.iv_section = dataSection;
// If this TOC entry is from .fixed, create a new record in .fixed_toc
if (hostToc.iv_section == SBE_XIP_SECTION_FIXED) {
if (*io_fixedEntriesRemaining == 0) {
rc = TRACE_ERRORX(SBE_XIP_TOC_ERROR,
"Too many TOC entries for .fixed\n");
break;
}
if (hostToc.iv_data != (uint16_t)hostToc.iv_data) {
rc = TRACE_ERRORX(SBE_XIP_IMAGE_ERROR,
"The .fixed section is too big to index\n");
break;
}