summaryrefslogtreecommitdiffstats
path: root/src/build/buildpnor/buildpnorOld.pl
blob: 94c0036d068694ef53ef399cec767a972b295934 (plain)
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
#!/usr/bin/perl
#  IBM_PROLOG_BEGIN_TAG
#  This is an automatically generated prolog.
#
#  $Source: src/build/buildpnor/buildpnor.pl $
#
#  IBM CONFIDENTIAL
#
#  COPYRIGHT International Business Machines Corp. 2012
#
#  p1
#
#  Object Code Only (OCO) source materials
#  Licensed Internal Code Source Materials
#  IBM HostBoot Licensed Internal Code
#
#  The source code for this program is not published or other-
#  wise divested of its trade secrets, irrespective of what has
#  been deposited with the U.S. Copyright Office.
#
#  Origin: 30
#
#  IBM_PROLOG_END_TAG
#Builds a PNOR image based on pnorLayout XML file.
#See usage function at bottom of file for details on how to use this script

#Limitations to address later
# number fields must be 4 or 8 bytes
# numbers cannot be over 32 bits


use strict;
use XML::Simple;
use Data::Dumper;
use File::Basename;

################################################################################
# Set PREFERRED_PARSER to XML::Parser. Otherwise it uses XML::SAX which contains
# bugs that result in XML parse errors that can be fixed by adjusting white-
# space (i.e. parse errors that do not make sense).
################################################################################
$XML::Simple::PREFERRED_PARSER = 'XML::Parser';

my $TRAC_ERR = 0;
# 0=errors, >0 for more traces, leaving at 1 to keep key milestone traces.
my $g_trace = 1;

my $programName = File::Basename::basename $0;
my %tocLayout;
my %pnorLayout;
my %binFiles;


my $pnorLayoutFile;
my $pnorBinName = "";
my $tocVersion = 0x1;
my $genTocFlag = 0;
my $g_TOCEyeCatch = "part";
my $emitTestSections = 0;

if ($#ARGV < 0) {
    usage();
    exit;
}

#Parse the commandline args
for (my $i=0; $i < $#ARGV + 1; $i++)
{
    if ($ARGV[$i] =~ /-h/) {
        usage();
        exit 0;
    }
    elsif($ARGV[$i] =~ /--tocVersion/) {
        $tocVersion = getNumber($ARGV[++$i]);
        trace(2, "TOC Version to use=".sprintf("0x%x", $tocVersion));
    }
    elsif($ARGV[$i] =~ /--pnorLayout/) {
        $pnorLayoutFile = $ARGV[++$i];
        trace(2, "PNOR Layout XML File=$pnorLayoutFile");
    }
    elsif($ARGV[$i] =~ /--pnorOutBin/) {
        $pnorBinName = $ARGV[++$i];
        trace(2, "PNOR Binary File=$pnorBinName");
    }
    elsif($ARGV[$i] =~ /--genToc/) {
        $genTocFlag = 1;
        trace(2, "genTocFlag Set");
    }
    elsif($ARGV[$i] =~ /--binFile/) {
        my $argName = $ARGV[$i];
        my $argVal = $ARGV[++$i];
        saveInputFile("--binFile", $argName, $argVal, \%binFiles);
    }
    elsif($ARGV[$i] =~ /--test/) {
        $emitTestSections = 1;
    }
    else {
        traceErr("Unrecognized Input: $ARGV[$i]");
        exit 1;
	#Error!!
    }
}


#Load PNOR Layout XML file
my $rc = loadPnorLayout($pnorLayoutFile, \%pnorLayout);
if($rc != 0)
{
    trace(0, "Error detected from call to loadPnorLayout().  Exiting");
    exit 1;
}

#trace(1, Dumper(%pnorLayout->{1576960}));
#trace(1, Dumper(%binFiles));

#Verify all the section files exist
my $rc = verifyFilesExist($genTocFlag, \%pnorLayout, \%binFiles);
if($rc != 0)
{
    trace(0, "Error detected from call to verifyFilesExist().  Exiting");
    exit 1;
}

#Perform any data integrity manipulation (ECC, shaw-hash, etc)
$rc = robustifyImgs(\%pnorLayout, \%binFiles);
if($rc != 0)
{
    trace(0, "Error detected from call to robustifyImgs().  Exiting");
    exit 1;
}

#collect Actual image sizes
$rc = collectActSizes(\%pnorLayout, \%binFiles);
if($rc != 0)
{
    trace(0, "Error detected from call to collectActSizes().  Exiting");
    exit 1;
}

#Generate TOC if requested.
if ($genTocFlag != 0)
{
    $rc = fillTocActSize($tocVersion, \%pnorLayout);
    if($rc != 0)
    {
        trace(0, "Error detected from call to fillTocActSize().  Exiting");
        exit 1;
    }

    if(exists($binFiles{$g_TOCEyeCatch}))
    {
        $rc = genToc($tocVersion, $binFiles{$g_TOCEyeCatch}, \%pnorLayout);
        if($rc != 0)
        {
            trace(0, "Error detected from call to genToc().  Exiting");
            exit 1;
        }
    }
    else
    {
        trace(0, "No TOC Binary file name provided.  Aborting!");
        exit 1;
    }
}


$rc = checkSpaceConstraints(\%pnorLayout, \%binFiles);
if($rc != 0)
{
    trace(0, "Error detected from call to checkSpaceConstraints().  Exiting");
    exit 1;
}

$rc = assemblePnorImage($pnorBinName, \%pnorLayout, \%binFiles);
if($rc != 0)
{
    trace(0, "Error detected from call to assemblePnorImage().  Exiting");
    exit 1;
}

exit 0;

################################################################################
# loadPnorLayout
################################################################################
sub loadPnorLayout
{
    my ($i_pnorFile, $i_pnorLayoutRef) = @_;
    my $this_func = (caller(0))[3];

    unless(-e $i_pnorFile)
    {
        traceErr("$this_func: File not found: $i_pnorFile");
        return -1;
    }

    #parse the input XML file
    my $xs = new XML::Simple(keyattr=>[], forcearray => 1);
    my $xml = $xs->XMLin($i_pnorFile);

    #trace(1, "pnorLayoutXML \n ".Dumper($xml));

    #Save the meatadata - imageSize, blockSize, etc.
    foreach my $metadataEl (@{$xml->{metadata}})
    {
        my $imageSize = $metadataEl->{imageSize}[0];
        my $blockSize = $metadataEl->{blockSize}[0];
        my $partTableSize = $metadataEl->{partTableSize}[0];

        trace(3, "$this_func: metadata: imageSize = $imageSize, blockSize=$blockSize, partTableSize=$partTableSize");

        $imageSize = getNumber($imageSize);
        $blockSize = getNumber($blockSize);
        $partTableSize = getNumber($partTableSize);

        $$i_pnorLayoutRef{metadata}{imageSize} = $imageSize;
        $$i_pnorLayoutRef{metadata}{blockSize} = $blockSize;
        $$i_pnorLayoutRef{metadata}{partTableSize} = $partTableSize;
    }

    #Iterate over the <section> elements.
    foreach my $sectionEl (@{$xml->{section}})
    {
        #trace(1, "current Element: \n ".Dumper($sectionEl));
        my $description = $sectionEl->{description}[0];
        my $eyeCatch = $sectionEl->{eyeCatch}[0];
        my $physicalOffset = $sectionEl->{physicalOffset}[0];
        my $physicalRegionSize = $sectionEl->{physicalRegionSize}[0];
        my $ecc = $sectionEl->{ecc}[0];
        my $source = $sectionEl->{source}[0];
        my $sideless = $sectionEl->{sideless}[0];

        if (($emitTestSections == 0) && ($sectionEl->{testonly}[0] eq "yes"))
        {
            next;
        }

        my $actualRegionSize = 0;
        if(exists($sectionEl->{actualRegionSize}[0]))
        {
            $actualRegionSize = $sectionEl->{actualRegionSize}[0];
            $actualRegionSize = getNumber($actualRegionSize);
        }

        trace(3, "$this_func: description = $description, eyeCatch=$eyeCatch, physicalOffset = $physicalOffset, physicalRegionSize=$physicalRegionSize, ecc=$ecc, source=$source, actualRegionSize=$actualRegionSize");

        $physicalOffset = getNumber($physicalOffset);
        $physicalRegionSize = getNumber($physicalRegionSize);

        # trace(4, "$this_func: physicalOffset=$physicalOffset, physicalRegionSize=$physicalRegionSize");

        $$i_pnorLayoutRef{sections}{$physicalOffset}{description} = $description;
        $$i_pnorLayoutRef{sections}{$physicalOffset}{eyeCatch} = $eyeCatch;
        $$i_pnorLayoutRef{sections}{$physicalOffset}{physicalOffset} = $physicalOffset;
        $$i_pnorLayoutRef{sections}{$physicalOffset}{physicalRegionSize} = $physicalRegionSize;
        $$i_pnorLayoutRef{sections}{$physicalOffset}{actualRegionSize} = $actualRegionSize;
        $$i_pnorLayoutRef{sections}{$physicalOffset}{ecc} = $ecc;
        $$i_pnorLayoutRef{sections}{$physicalOffset}{source} = $source;
        $$i_pnorLayoutRef{sections}{$physicalOffset}{sideless} = $sideless;

    }

    return 0;
}

################################################################################
# genToc - generate Table of Contents image based on input data.
################################################################################
sub genToc
{

    my ($i_tocVersion, $i_tocBinName, $i_pnorLayoutRef) = @_;
    my $this_func = (caller(0))[3];
    my $key;
    my $partIdCount = 1;
    my $FILEHANDLE;
    trace(4, "$this_func: >>Enter");

    #open output file
    open( $FILEHANDLE, ">:raw", $i_tocBinName) or die "Can't open $i_tocBinName file for writing";

    #Insert FFS Header data
    #HEADER WORD 1: Magic Number - "PART" in ASCII
    my @charArray = split //, 'PART';
    my $curChar;
    foreach $curChar (@charArray)
    {
        print $FILEHANDLE pack('C', ord($curChar));
    }

    #HEADER WORD 2: FFS version to Binary
    print $FILEHANDLE pack('N', $i_tocVersion);

    #HEADER WORD 3: Size of Partition table in blocks - 4 bytes of space
    if(exists($$i_pnorLayoutRef{metadata}{partTableSize}))
    {
        my $val = $$i_pnorLayoutRef{metadata}{partTableSize};
        #verify number consumes less than 32 bits
        if($val > 0xFFFFFFFF)
        {
            trace(0, "$this_func: value=".$val.".  This is greater than 32 bits in hex and not currently supported!. \n Aborting program");
            exit 1;
        }
        print $FILEHANDLE pack('N', $val);
    }
    else
    {
        insertPadBytes($FILEHANDLE, 4);
    }

    #HEADER WORD 4: Size of Partition entry in bytes
    print $FILEHANDLE pack('N', getFFSEntrySize($i_tocVersion, $i_pnorLayoutRef));

    #HEADER WORD 5: Number of Partition Entries
    my %sectionHash = %{$$i_pnorLayoutRef{sections}};

    my $partitionCount = scalar keys %sectionHash;
    print $FILEHANDLE pack('N', $partitionCount);

    #HEADER WORD 6: block size
    my $blockSize = $$i_pnorLayoutRef{metadata}{blockSize};
    print $FILEHANDLE pack('N', $blockSize);

    #HEADER WORD 7: block count (size of flash in blocks)
    my $imageSize = $$i_pnorLayoutRef{metadata}{imageSize};
    my $blockCount = $imageSize/$blockSize;
    if ($blockCount != int($blockCount))
    {
        trace(0, "$this_func: Image size ($imageSize) is not an even multiple of erase blocks ($blockSize).  This is not supported.  Aborting!");
        exit 1;
    }
    print $FILEHANDLE pack('N', $blockCount);

    #HEADER WORD 8-11: Reserved
    #insert 16 pad bytes for 4 reserved words
    insertPadBytes($FILEHANDLE, 16);

    #HEADER WORD 12: checksum
    #insert 4 pad bytes for now.
    insertPadBytes($FILEHANDLE, 4);

    #key into hash data is the physical offset of section.  Need to sort the keys
    #so we put things in the correct order in toc.
    #Generally speaking, this loop is populating the FFS Header with records based on the
    #section data specified in the XML + actual sizes of the input binary files.
    for $key ( sort {$a<=> $b} keys %sectionHash)
    {

        # FFS Entry Word 1-4: eyecatcher
        if(exists($sectionHash{$key}{eyeCatch}))
        {
            my $char;
            my @charArray = unpack("C*", $sectionHash{$key}{eyeCatch});
            foreach $char (@charArray)
            {
                print $FILEHANDLE pack('C', $char);
            }
            #pad out to get 16 bytes
            my $zeroPad = 16-length($sectionHash{$key}{eyeCatch});
            insertPadBytes($FILEHANDLE, $zeroPad);
        }
        else
        {
            insertPadBytes($FILEHANDLE, 16);
        }

        #FFS Entry Word 5: base/physical offset in blocks
        if(exists($sectionHash{$key}{physicalOffset}))
        {
            my $val = $sectionHash{$key}{physicalOffset};
            $val = $val/$blockSize;
            if ($val != int($val))
            {
                trace(0, "$this_func: Partition offset ($val) is does not fall on an erase block ($blockSize) boundary.  This is not supported.  Aborting!");
                exit 1;
            }

            print $FILEHANDLE pack('N', $val);

        }
        else
        {
            insertPadBytes($FILEHANDLE, 4);
        }

        #FFS Entry Word 6: physical size in blocks
        if(exists($sectionHash{$key}{physicalRegionSize}))
        {
            my $val = $sectionHash{$key}{physicalRegionSize};
            $val = $val/$blockSize;
            if($val != int($val))
            {
                trace(0, "$this_func: Partition size ($val) is not an even multiple of erase blocks ($blockSize).  This is not supported.  Aborting!");
                exit 1;
            }

            print $FILEHANDLE pack('N', $val);

        }
        else
        {
            insertPadBytes($FILEHANDLE, 4);
        }

        #FFS Entry Word 7: PID (Parent ID) - always 0xFFFFFFFF for hostboot.
        print $FILEHANDLE pack('N', 0xFFFFFFFF);

        #FFS Entry Word 8: ID (Partition ID) - Incremented number for each partition.
        print $FILEHANDLE pack('N', $partIdCount++);

        #FFS Entry Word 9: type - usually 1, except TOC is 3
        if( $sectionHash{$key}{eyeCatch} =~ $g_TOCEyeCatch )
        {
            print $FILEHANDLE pack('N', 3);
        }
        else
        {
            print $FILEHANDLE pack('N', 1);
        }

        #FFS Entry Word 10: flags - always zero
        insertPadBytes($FILEHANDLE, 4);

        #FFS Entry Word 11-15: 5 Reserved words
        insertPadBytes($FILEHANDLE, 20);

        #FFS Entry Words 16-31: 16 User Words
        #FFS User Word 1 - Actual size actual size
        if(exists($sectionHash{$key}{actualRegionSize}))
        {
            my $val = $sectionHash{$key}{actualRegionSize};
            #verify number consumes less than 32 bits
            if($val > 0xFFFFFFFF)
            {
                trace(0, "value=".$val.".  This is greater than 32 bits in hex and not currently supported!. \n Aborting program");
                exit 1;
            }

            print $FILEHANDLE pack('N', $val);
        }
        else
        {
            insertPadBytes($FILEHANDLE, 4);
        }

        #FFS User Word 2-3: Miscellaneous information.
        #User Word 2, bit 18 indicates sideless.
        my $miscInfo = 0x00000000;
        if($sectionHash{$key}{sideless} =~ "yes")
        {
            $miscInfo = $miscInfo | 0x00004000;
        }
        print $FILEHANDLE pack('N', $miscInfo);
        #User Word 3 Not currently implemented.
        insertPadBytes($FILEHANDLE, 4);

        #FFS User Word 4-16: Free space User Data
        insertPadBytes($FILEHANDLE, 52);


        #FFS Entry Word 31: Checksum - not currently implemented
        insertPadBytes($FILEHANDLE, 4);


    }
    return 0;
}

################################################################################
# fillTocActSize - Fill in the toc actual size record based on number of PNOR records
################################################################################
sub fillTocActSize
{
    #Note: tocVersion is passed in because the toc Layout version could influence the
    #rules regarding how the size is calculated.
    my($i_tocVersion, $i_pnorLayoutRef) = @_;
    my $this_func = (caller(0))[3];
    my $rc = 0;
    my $ffsHeaderSize = 9*4;
    my $ffsEntrySize = getFFSEntrySize($i_tocVersion, $i_pnorLayoutRef);

    my %sectionHash = %{$$i_pnorLayoutRef{sections}};
    my $recordCount = scalar keys %sectionHash;
    my $size = ($recordCount*$ffsEntrySize)+$ffsHeaderSize;

    my $tocLayoutKey = findLayoutKeyByEyeCatch($g_TOCEyeCatch, \%$i_pnorLayoutRef);
    if( $tocLayoutKey == -1)
    {
        trace(0, "$this_func: Unable to find EyeCatcher=$g_TOCEyeCatch in PNOR layout for TOC entry. ");
        $rc = 1;
        last;
    }

    if($$i_pnorLayoutRef{sections}{$tocLayoutKey}{ecc} =~ "yes")
    {
        $size=$size*(9/8);
    }

    trace(2, "$this_func: PNOR FFS contains $recordCount records, total Size=$size");
    $$i_pnorLayoutRef{sections}{$tocLayoutKey}{actualRegionSize}=$size;


    return $rc;
}

################################################################################
# robustifyImgs - Perform any ECC or ShawHash manipulations
################################################################################
sub robustifyImgs
{
    my ($i_pnorLayoutRef, $i_binFiles) = @_;
    my $this_func = (caller(0))[3];

    #@TODO: ECC Correction
    #@TODO: maybe a little SHA hashing?

    return 0;
}

################################################################################
# collectActSizes - walk through all the images and set their actual sizes in the layout
################################################################################
sub collectActSizes
{
    my ($i_pnorLayoutRef, $i_binFiles) = @_;
    my $this_func = (caller(0))[3];
    my $key;
    my $rc = 0;

    for $key ( keys %{$i_binFiles})
    {
        my $filesize = -s $$i_binFiles{$key};

        my $layoutKey = findLayoutKeyByEyeCatch($key, \%$i_pnorLayoutRef);
        if( $layoutKey == -1)
        {
            trace(0, "$this_func: entry not found in PNOR layout for file $$i_binFiles{$key}, under eyecatcher $key");
            $rc = 1;
            last;
        }

        $$i_pnorLayoutRef{sections}{$layoutKey}{actualRegionSize} = $filesize;

        trace(2, "$this_func: $$i_binFiles{$key} size = $$i_pnorLayoutRef{sections}{$layoutKey}{actualRegionSize}");

    }

    return $rc;
}

################################################################################
# findLayoutKeyByEyeCatch - Figure out hash key based on eyeCatcher
################################################################################
sub findLayoutKeyByEyeCatch
{
    my $layoutKey = -1;
    my($eyeCatch, $i_pnorLayoutRef) = @_;
    my $key;

    my %sectionHash = %{$$i_pnorLayoutRef{sections}};
    for $key ( keys %sectionHash)
    {
        if($sectionHash{$key}{eyeCatch} =~ $eyeCatch)
        {
            $layoutKey = $key;
            last;
        }
    }

    return $layoutKey;
}

################################################################################
# verifyFilesExist - Verify all the input files exist, and there are files
#        provided for each PNOR section
################################################################################
sub verifyFilesExist
{
    my ($i_genToc, $i_pnorLayoutRef, $i_binFiles) = @_;
    my $this_func = (caller(0))[3];
    my $key;
    my $rc = 0;

    for $key ( keys %$i_binFiles)
    {
        #Don't check if toc file exists if we're going to generate it
        if(($key =~ $g_TOCEyeCatch) && ($i_genToc != 0))
        {
            next;
        }
        unless(-e $$i_binFiles{$key})
        {
            my $inputFile = $$i_binFiles{$key};
            trace(0, "Specified input file ($inputFile) for key ($key) does not exist.  Aborting!");
            $rc = 1;
            last;
        }
        else
        {
            trace(10, "$this_func: $$i_binFiles{$key} exists");
        }
    }

    if($rc != 0)
    {
        return $rc;
    }

    my %sectionHash = %{$$i_pnorLayoutRef{sections}};

    #Verify there is an input file for each section of PNOR
    for $key ( keys %sectionHash)
    {
        my $eyeCatch = $sectionHash{$key}{eyeCatch};

        #Don't check if toc file exists if we're going to generate it
        if(($eyeCatch =~ $g_TOCEyeCatch) && ($i_genToc != 0))
        {
            next;
        }
        #Ignore sections that are marked as blank
        elsif($sectionHash{$key}{source} =~ "Blank")
        {
            next;
        }
        #check if binFiles list has a file for eyeCatcher
        unless(exists($$i_binFiles{$eyeCatch}))
        {
            my $inputFile = $$i_binFiles{$eyeCatch};
            trace(0, "Input file not provided for PNOR section with eyeCatcher=$eyeCatch.  Aborting!");
            $rc = 1;
            last;
        }
        else
        {
            trace(10, "$this_func: Input file ($$i_binFiles{$eyeCatch}) provided for section $eyeCatch");
        }
    }

    return $rc;
}

################################################################################
# checkSpaceConstraints - Make sure provided files will fit in their sections
################################################################################
sub checkSpaceConstraints
{
    my ($i_pnorLayoutRef, $i_binFiles) = @_;
    my $rc = 0;
    my $this_func = (caller(0))[3];
    my $key;

    my %sectionHash = %{$$i_pnorLayoutRef{sections}};
    #Verify there is an input file for each section of PNOR
    for $key ( keys %sectionHash)
    {
        my $eyeCatch = $sectionHash{$key}{eyeCatch};
        my $physicalRegionSize = $sectionHash{$key}{physicalRegionSize};
        my $actualRegionSize = $sectionHash{$key}{actualRegionSize};

        if($actualRegionSize > $physicalRegionSize)
        {
            trace(0, "$this_func: Image provided ($$i_binFiles{$eyeCatch}) has size ($actualRegionSize) which is greater than allocated space ($physicalRegionSize) for section=$eyeCatch.  Aborting!");
            $rc = 1;
        }

    }

    return $rc;
}

################################################################################
# assemblePnorImage - Assemble actual PNOR image using the provided input data
################################################################################
sub assemblePnorImage
{
    my ($i_pnorBinName, $i_pnorLayoutRef, $i_binFiles) = @_;
    my $this_func = (caller(0))[3];
    my $rc = 0;
    my $key;

    #key is the physical offset into the file, however don't need to sort
    #as long as I tell dd not to truncate the file.
    my %sectionHash = %{$$i_pnorLayoutRef{sections}};
    for $key ( keys %sectionHash)
    {
        trace(3, "$this_func: key=$key");
        my $eyeCatch = $sectionHash{$key}{eyeCatch};
        my $physicalOffset = $sectionHash{$key}{physicalOffset};
        my $if = $$i_binFiles{$eyeCatch};

        #Ignore sections that are marked as blank
        if($sectionHash{$key}{source} =~ "Blank")
        {
            next;
        }

        #telling dd to read write 512 characters (1b) at a time, so need to seek
        #<bytes>/512 blocks.  This will work because PNOR offests will always be
        # on a page boundary
        my $blockSeek = $physicalOffset/512;

        trace(1, "$this_func: populating section $eyeCatch, filename=$if");
#        my $ddCmd = `dd if=$if ibs=8c of=$i_pnorBinName obs=8c seek=$physicalOffset`;
        my $ddCmd = `dd if=$if ibs=1b of=$i_pnorBinName obs=1b seek=$blockSeek conv=notrunc`;



    }

    return $rc;
}

################################################################################
# saveInputFile - save inputfile name into binFiles array
################################################################################
sub saveInputFile
{
    my ($i_argPrefix, $i_argName, $i_argVal, $i_binFiles) = @_;
    my $this_func = (caller(0))[3];

    #$i_argName will be something like --binFile_HBB
    #This substr command should return just the HBB, which should match an eyeCather
    my $eyeCatcher = substr($i_argName, length($i_argPrefix)+1, length($i_argName)-length($i_argPrefix));

    trace(10, "$this_func: $eyeCatcher=$i_argVal");

    $$i_binFiles{$eyeCatcher} = $i_argVal;

    trace(10, "$this_func:           $$i_binFiles{$eyeCatcher}");

    #no return code expected
}

#################################################
# getFFSEntrySize: Returns number of bytes in an ffs_entry based on specified version
#################################################
sub getFFSEntrySize
{
    my($i_tocVersion, $i_pnorLayoutRef) = @_;
    my $this_func = (caller(0))[3];
    my $ffsEntrySize = 0;

    if($i_tocVersion == 0x1)
    {
        #16 char name + 12 fixed words + 16 user data words
        $ffsEntrySize = 16+(12*4)+(16*4);
    }
    else
    {
        trace(0, "$this_func:  Layout Version Unsupported!  i_tocVersion=$i_tocVersion");
        exit 1;
    }
    return $ffsEntrySize;
}

#################################################
# Insert specifed number of pad bytes into file
#
#################################################
sub insertPadBytes
{
    my ($i_FILEHANDLE, $i_padBytes) = @_;
    my $i;
    print $i_FILEHANDLE pack("C[$i_padBytes]", map { 0 } 1..$i_padBytes);

}

################################################################################
# getNumber - handle hex or decimal input string
################################################################################
sub getNumber
{
    my $inVal = shift;
    if($inVal =~ "0x")
    {
	return oct($inVal);
    }
    else
    {
	return $inVal;
    }
}

################################################################################
# trace
################################################################################
sub traceErr
{
    my $i_string = shift;
    trace($TRAC_ERR, $i_string);
}

################################################################################
# trace
################################################################################
sub trace
{
    my $i_traceLevel;
    my $i_string;

    ($i_traceLevel, $i_string) = @_;

    #traceLevel 0 is for errors
    if($i_traceLevel == 0)
    {
	print "ERROR: ".$i_string."\n";
    }
    elsif ($g_trace >= $i_traceLevel)
    {
	print "TRACE: ".$i_string."\n";
    }
}



################################################################################
# print usage instructions
################################################################################
sub usage
{
print <<"ENDUSAGE";
  $programName = Creates the PNOR IMAGE and with TOC based on input data.

  Usage:
    $programName --pnorlayout <layout xml file> --genToc --tocVersion <TOC layout version
                 --pnorOutBin  <complete PNOR image>
                 --binFile_HBB <hostboot base image> --binFile_HBI <hostboot extended image>

  Parms:
    -h                  Print this help text
    --pnorlayout <file> PNOR Layout XML file
    --pnorOutBin <file> Name of output file for PNOR Binary
    --genToc            Indicates you wish to generate a table of contents.  It will
                        write the file indicated by --binFile_TOC.

    --binFile_<Section> <file>  This is a special paramater.  It is used to specify
                        input files to use for populating the various sections of
                        PNOR.
                            Examples:  --binFile_HBB hostboot.bin
                                       --binFile_HBI hostboot_extended.bin
                                       --binFile_TOC murano.toc
                         A section declared as Blank in the XML does not need to have a
                         binFile specified
    --test              Output test-only sections.

  Current Limitations:
      --TOC Records must be 4 or 8 bytes in length
      --Regions and offsets cannot be over 32 bits (in hex), not an issue with current PNOR size
     The script will print and error and exit with a non-zero return code if these
     conditions are encountered.

ENDUSAGE
}
OpenPOWER on IntegriCloud