summaryrefslogtreecommitdiffstats
path: root/src/import/hwpf/fapi2/tools/parseAttributeInfo.pl
blob: 915b713703c978ba20b237b887098820bf06e92a (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
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
#!/usr/bin/perl
# IBM_PROLOG_BEGIN_TAG
# This is an automatically generated prolog.
#
# $Source: src/import/hwpf/fapi2/tools/parseAttributeInfo.pl $
#
# OpenPOWER HostBoot Project
#
# Contributors Listed Below - COPYRIGHT 2015,2018
# [+] 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

use strict;
use Getopt::Long;

#------------------------------------------------------------------------------
# Print Command Line Help
#------------------------------------------------------------------------------
my $arg_output_dir = undef;
my $parse_defaults = '';

# Get the options from the command line - the rest of @ARGV will
# be filenames
GetOptions(
    "output-dir=s"   => \$arg_output_dir,
    "parse_defaults" => \$parse_defaults
);

my $numArgs = $#ARGV + 1;
if ( ( $numArgs < 1 ) || ( $arg_output_dir eq undef ) )
{
    print(
        "Usage: parseAttributeInfo.pl --output-dir=<output dir> [--parse_defaults] <attr-xml-file1> [<attr-xml-file2> ...]\n"
    );
    print("  This perl script will parse attribute XML files and create the following files:\n");
    print("  - attribute_ids.H.          Contains IDs, type, value enums and other information\n");
    print(
        "                              When --parse_defaults is specified, default tags are handled and added to this file too.\n"
    );
    print("  - fapi2_chip_ec_feature.H   Contains a function to query chip EC features\n");
    print("  - attribute_plat_check.H    Contains compile time checks that all attributes are\n");
    print("                              handled by the platform\n");
    print("  - attributesSupported.html  Contains the HWPF attributes supported\n");
    print("  - attrInfo.csv              Used to process Attribute Override Text files\n");
    print("  - attrEnumInfo.csv          Used to process Attribute Override Text files\n");
    exit(1);
}

#------------------------------------------------------------------------------
# Specify perl modules to use
#------------------------------------------------------------------------------
use Digest::MD5 qw(md5_hex);
use XML::Simple;
my $xml = new XML::Simple( KeyAttr => [] );

# Uncomment to enable debug output
#use Data::Dumper;

#------------------------------------------------------------------------------
# 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';

#------------------------------------------------------------------------------
# Open output files for writing
#------------------------------------------------------------------------------
my $aiFile = $arg_output_dir;
$aiFile .= "/";
$aiFile .= "attribute_ids.H";
open( AIFILE, ">", $aiFile );

my $ecHFile = $arg_output_dir;
$ecHFile .= "/";
$ecHFile .= "fapi2_chip_ec_feature.H";
open( ECHFILE, ">", $ecHFile );

my $acFile = $arg_output_dir;
$acFile .= "/";
$acFile .= "attribute_plat_check.H";
open( ACFILE, ">", $acFile );

my $asFile = $arg_output_dir;
$asFile .= "/";
$asFile .= "attributesSupported.html";
open( ASFILE, ">", $asFile );

my $itFile = $arg_output_dir;
$itFile .= "/";
$itFile .= "attrInfo.csv";
open( ITFILE, ">", $itFile );

my $etFile = $arg_output_dir;
$etFile .= "/";
$etFile .= "attrEnumInfo.csv";
open( ETFILE, ">", $etFile );

# TODO: This for platform use only, will support later via RTC 128106 for HB
my $fmFile = $arg_output_dir;
$fmFile .= "/";
$fmFile .= "fapi2AttrOverrideData.H";
open( FMFILE, ">", $fmFile );

# TODO: This for platform use only, will support later via RTC 128106 for HB
my $fsFile = $arg_output_dir;
$fsFile .= "/";
$fsFile .= "fapi2AttrSyncData.H";
open( FSFILE, ">", $fsFile );

my $feFile = $arg_output_dir;
$feFile .= "/";
$feFile .= "fapi2AttrOverrideEnums.H";
open( FEFILE, ">", $feFile );

#------------------------------------------------------------------------------
# Print Start of file information to attribute_ids.H
#------------------------------------------------------------------------------
print AIFILE "// attribute_ids.H\n";
print AIFILE "// This file is generated by perl script parseAttributeInfo.pl\n\n";
print AIFILE "#include <target_types.H>\n\n";
print AIFILE "#ifndef ATTRIBUTEIDS_H_\n";
print AIFILE "#define ATTRIBUTEIDS_H_\n\n";
print AIFILE "namespace fapi2\n";
print AIFILE "{\n\n";
print AIFILE "\/**\n";
print AIFILE " * \@brief Enumeration of attribute IDs\n";
print AIFILE " *\/\n";
print AIFILE "enum AttributeId\n{\n";

#------------------------------------------------------------------------------
# Print Start of file information to fapi2_chip_ec_feature.H
#------------------------------------------------------------------------------
print ECHFILE "// This file is generated by perl script parseAttributeInfo.pl\n";
print ECHFILE "// It implements the fapi2_chip_ec_feature function\n\n";
print ECHFILE "#ifndef __FAPI2_CHIP_EC_FEATURE_H_\n";
print ECHFILE "#define __FAPI2_CHIP_EC_FEATURE_H_\n";
print ECHFILE "#include <fapi2_attribute_service.H>\n";
print ECHFILE "#include <attribute_ids.H>\n";
print ECHFILE "namespace fapi2\n";
print ECHFILE "{\n\n";
print ECHFILE "// create a unique type from an int ( or attribute id) \n";
print ECHFILE "template<int I>\n";
print ECHFILE "struct int2Type {\n";
print ECHFILE "enum { value = I };\n";
print ECHFILE "};\n";
print ECHFILE "ReturnCode queryChipEcAndName(\n";
print ECHFILE "                    const Target<fapi2::TARGET_TYPE_ALL>& i_target,\n";
print ECHFILE "                    fapi2::ATTR_NAME_Type& , fapi2::ATTR_EC_Type & );\n\n";
print ECHFILE "template<int T>\n";
print ECHFILE "ReturnCode queryChipEcFeature(int2Type<T> id,\n";
print ECHFILE "                    const Target<fapi2::TARGET_TYPE_ALL>& i_target,\n";
print ECHFILE "                    uint8_t & o_hasFeature)\n";
print ECHFILE "{\n";
print ECHFILE "    fapi2::ATTR_NAME_Type l_chipName;\n";
print ECHFILE "    fapi2::ATTR_EC_Type l_chipEc;\n\n";
print ECHFILE "    o_hasFeature = 0;\n\n";
print ECHFILE "    ReturnCode l_rc = queryChipEcAndName(i_target, l_chipName, l_chipEc);\n";
print ECHFILE "    if (l_rc)\n";
print ECHFILE "    {\n";
print ECHFILE "        FAPI_ERR(\"queryChipEcFeature: error getting chip name\");\n";
print ECHFILE "    }\n";
print ECHFILE "    else\n";
print ECHFILE "    {\n";
print ECHFILE "        o_hasFeature = hasFeature(int2Type<T>(), l_chipName, l_chipEc);\n";
print ECHFILE "    }\n";
print ECHFILE "    return l_rc;\n";
print ECHFILE "}\n\n";

#------------------------------------------------------------------------------
# Print Start of file information to attribute_plat_check.H
#------------------------------------------------------------------------------
print ACFILE "// attribute_plat_check.H\n";
print ACFILE "// This file is generated by perl script parseAttributeInfo.pl\n";
print ACFILE "// A platform can include it to ensure that it handles all HWPF\n";
print ACFILE "// attributes\n\n";
print ACFILE "#ifndef ATTRIBUTEPLATCHECK_H_\n";
print ACFILE "#define ATTRIBUTEPLATCHECK_H_\n\n";

#------------------------------------------------------------------------------
# Print Start of file information to attributesSupported.html
#------------------------------------------------------------------------------
print ASFILE "<html>\n";
print ASFILE "<body>\n\n";
print ASFILE "<!-- attributesSupported.html -->\n";
print ASFILE "<!-- This file is generated by perl script parseAttributeInfo.pl -->\n";
print ASFILE "<!-- It lists all HWPF attributes supported -->\n\n";
print ASFILE "<h4>HWPF Attributes supported by this build.</h4>\n";
print ASFILE "<table border=\"4\">\n";
print ASFILE "<tr><th>Attribute ID</th><th>Attribute Description</th></tr>";

#------------------------------------------------------------------------------
# Print Start of file information to attrInfo.csv
#------------------------------------------------------------------------------
print ITFILE "# attrInfo.csv\n";
print ITFILE "# This file is generated by perl script parseAttributeInfo.pl\n";
print ITFILE "# It lists information about FAPI attributes and is used to\n";
print ITFILE "# process FAPI Attribute text files (overrides/syncs)\n";
print ITFILE "# Format:\n";
print ITFILE "# <FAPI-ATTR-ID-STR>,<LAYER-ATTR-ID-STR>,<ATTR-ID-VAL>,<ATTR-TYPE>\n";
print ITFILE "# Note that for the AttributeTanks at the FAPI layer, the\n";
print ITFILE "# FAPI-ATTR-ID-STR and LAYER-ATTR-ID-STR will be identical\n";

#------------------------------------------------------------------------------
# Print Start of file information to attrEnumInfo.csv
#------------------------------------------------------------------------------
print ETFILE "# attrEnumInfo.csv\n";
print ETFILE "# This file is generated by perl script parseAttributeInfo.pl\n";
print ETFILE "# It lists information about FAPI attribute enumerators and is\n";
print ETFILE "# used to process FAPI Attribute text files (overrides/syncs)\n";
print ETFILE "# Format:\n";
print ETFILE "# <ENUM-STR>,<ENUM-VAL>\n";

#-------------------------------------------------------------------------------
# Print header of getFapiAttrData.C
# ------------------------------------------------------------------------------
print FMFILE "const AttributeData g_FapiAttrs[] = {\n";
my %attrOverrideData = ();

#-------------------------------------------------------------------------------
# Print header of fapi2AttrSyncData.H
# ------------------------------------------------------------------------------
print FSFILE "// fapi2AttrSyncData.H\n";
print FSFILE "// This file is generated by perl script parseAttributeInfo.pl\n\n";
print FSFILE "#ifndef FAPI2ATTRSYNCDATA_H_\n";
print FSFILE "#define FAPI2ATTRSYNCDATA_H_\n\n";
print FSFILE "#include <target_types.H>\n\n";
print FSFILE "namespace fapi2\n";
print FSFILE "{\n\n";
print FSFILE "struct AttributeSyncInfo\n";
print FSFILE "{\n";
print FSFILE "    uint32_t iv_attrId;\n";
print FSFILE "    size_t iv_attrElemSizeBytes;\n";
print FSFILE "    size_t iv_dims[4];\n";
print FSFILE "};\n\n";
my %attrSyncData = ();

#-------------------------------------------------------------------------------
# Print header of getFapiAttrEnumData.C
# ------------------------------------------------------------------------------
print FEFILE "// Created via parseAttributeInfo.pl\n";
print FEFILE "const AttributeEnum g_FapiEnums[] = {\n";
my @attrOverrideEnums = ();

my %attrIdHash;     # Records which Attribute IDs have been used
my %attrValHash;    # Records which Attribute values have been used

#------------------------------------------------------------------------------
# For each XML file
#------------------------------------------------------------------------------
foreach my $argnum ( 0 .. $#ARGV )
{
    my $infile = $ARGV[$argnum];

    # read XML file. The ForceArray option ensures that there is an array of
    # elements even if there is only one such element in the file
    my $attributes = $xml->XMLin( $infile, ForceArray => ['attribute'] );

    # Uncomment to get debug output of all attributes
    #print "\nFile: ", $infile, "\n", Dumper($attributes), "\n";

    #--------------------------------------------------------------------------
    # For each Attribute
    #--------------------------------------------------------------------------
    foreach my $attr ( @{ $attributes->{attribute} } )
    {
        #----------------------------------------------------------------------
        # Print the Attribute ID and calculated value to attribute_ids.H and
        # attribute_ids.txt. The value for an attribute is a hash value
        # generated from the attribute name, this ties a specific value to a
        # specific attribute name. This is done for Cronus so that if a HWP is
        # not recompiled against a new eCMD/Cronus version where the attributes
        # have changed then there will not be a mismatch in enumerator values.
        # Historically in P8, this is a 28bit hash value because the Initfile
        # compiler has a requirement that the top nibble of the 32 bit attribute
        # ID be zero to store flags.  In P9, this may change because the
        # Initfile compiler is re-designed.
        #----------------------------------------------------------------------
        if ( !exists $attr->{id} )
        {
            print("parseAttributeInfo.pl ERROR. Att 'id' missing\n");
            exit(1);
        }

        if ( exists( $attrIdHash{ $attr->{id} } ) )
        {
            # Two different attributes with the same id!
            print( "parseAttributeInfo.pl ERROR. Duplicate attr id ", $attr->{id}, "\n" );
            exit(1);
        }

        # Calculate a 28 bit hash value.
        my $attrHash128Bit = md5_hex( $attr->{id} );
        my $attrHash28Bit = substr( $attrHash128Bit, 0, 7 );

        # Print the attribute ID/value to attribute_ids.H
        print AIFILE "    $attr->{id} = 0x$attrHash28Bit,\n";

        if ( exists( $attrValHash{$attrHash28Bit} ) )
        {
            # Two different attributes generate the same hash-value!
            print( "parseAttributeInfo.pl ERROR. Duplicate attr id hash value for ", $attr->{id}, "\n" );
            exit(1);
        }

        $attrIdHash{ $attr->{id} } = $attrHash28Bit;
        $attrValHash{$attrHash28Bit} = 1;
    }
}

#------------------------------------------------------------------------------
# Print AttributeId enumeration end to attribute_ids.H
#------------------------------------------------------------------------------
print AIFILE "};\n\n";

#------------------------------------------------------------------------------
# Print Attribute Information comment to attribute_ids.H
#------------------------------------------------------------------------------
print AIFILE "\/**\n";
print AIFILE " * \@brief Attribute Information\n";
print AIFILE " *\/\n";

#------------------------------------------------------------------------------
# For each XML file
#------------------------------------------------------------------------------
foreach my $argnum ( 0 .. $#ARGV )
{
    my $infile = $ARGV[$argnum];

    # read XML file. The ForceArray option ensures that there is an array of
    # elements even if there is only one such element in the file
    my $attributes = $xml->XMLin( $infile, ForceArray => [ 'attribute', 'chip' ] );

    #--------------------------------------------------------------------------
    # For each Attribute
    #--------------------------------------------------------------------------

    foreach my $attr ( @{ $attributes->{attribute} } )
    {
        my $attrOverride = "";
        my $attrSync     = "";

        #----------------------------------------------------------------------
        # Print a comment with the attribute ID attribute_ids.H
        #----------------------------------------------------------------------
        print AIFILE "/* $attr->{id} */\n";

        #----------------------------------------------------------------------
        # Print the AttributeId and description to attributesSupported.html
        #----------------------------------------------------------------------
        if ( !exists $attr->{description} )
        {
            print("parseAttributeInfo.pl ERROR. Att 'description' missing\n");
            exit(1);
        }

        print ASFILE "<tr>\n";
        print ASFILE "  <td>$attr->{id}</td>\n";
        print ASFILE "  <td>$attr->{description}</td>\n";
        print ASFILE "</tr>\n";

        #----------------------------------------------------------------------
        # Print the assignment of each attribute to the local l_name
        #----------------------------------------------------------------------
        $attrOverride .= "\t{\n";
        $attrOverride .= "\t\t\"$attr->{id}\",\n";
        $attrSync     .= "\t{\n";

        #----------------------------------------------------------------------
        # Figure out the attribute array dimensions (if array)
        #----------------------------------------------------------------------
        my $arrayDimensions = "";
        my @arrayDims       = ();
        if ( $attr->{array} )
        {
            # Remove leading whitespace
            my $dimText = $attr->{array};
            $dimText =~ s/^\s+//;

            # Split on commas or whitespace
            @arrayDims = split( /\s*,\s*|\s+/, $dimText );

            foreach my $val (@arrayDims)
            {
                $arrayDimensions .= "[${val}]";
            }
        }
        until ( $#arrayDims == 3 )
        {
            push @arrayDims, 1;
        }
        my $arrayDimString = join( ", ", @arrayDims );

        #----------------------------------------------------------------------
        # Print the typedef for each attribute's val type to attribute_ids.H
        # Print the attribute information to attrInfo.csv
        #----------------------------------------------------------------------
        if ( exists $attr->{chipEcFeature} )
        {
            # The value type of chip EC feature attributes is uint8_t
            print AIFILE "typedef uint8_t $attr->{id}_Type;\n";
            print ITFILE "$attr->{id},$attr->{id},";
            print ITFILE "0x$attrIdHash{$attr->{id}},u8\n";
            $attrOverride .= "\t\t0x$attrIdHash{$attr->{id}},\n";
            $attrOverride .= "\t\tsizeof(uint8_t),\n";
            $attrOverride .= "\t\t{ $arrayDimString }\n";
            $attrOverride .= "\t},\n";
            $attrSync     .= "\t\t0x$attrIdHash{$attr->{id}},\n";
            $attrSync     .= "\t\tsizeof(uint8_t),\n";
            $attrSync     .= "\t\t{ $arrayDimString }\n";
            $attrSync     .= "\t},\n";
        }
        else
        {
            if ( !exists $attr->{valueType} )
            {
                print("parseAttributeInfo.pl ERROR. Att 'valueType' missing\n");
                exit(1);
            }

            #These array/fapi sizes need to stay in sync
            my @sizes     = ( 'uint8', 'uint16', 'uint32', 'uint64', 'int8', 'int16', 'int32', 'int64' );
            my @fapiSizes = ( 'u8',    'u16',    'u32',    'u64',    's8',   's16',   's32',   's64' );
            my $actualSize = '';
            my $actualFapi = '';
            my $index      = 0;
            foreach my $size (@sizes)
            {

                if ( $attr->{valueType} eq $size )
                {
                    $actualSize = $size;
                    $actualFapi = @fapiSizes[$index];
                    last;
                }
                $index++;
            }
            if ( $actualSize ne '' )
            {

                print AIFILE "typedef ${actualSize}_t $attr->{id}_Type$arrayDimensions;\n";
                print ITFILE "$attr->{id},$attr->{id},0x$attrIdHash{$attr->{id}},$actualFapi" . "$arrayDimensions\n";
                $attrOverride .= "\t\t0x$attrIdHash{$attr->{id}},\n";
                $attrOverride .= "\t\tsizeof(${actualSize}_t),\n";
                $attrSync     .= "\t\t0x$attrIdHash{$attr->{id}},\n";
                $attrSync     .= "\t\tsizeof(${actualSize}_t),\n";
            }
            else
            {
                print("parseAttributeInfo.pl ERROR. valueType not recognized: ");
                print $attr->{valueType}, "\n";
                exit(1);
            }
            $attrOverride .= "\t\t{ $arrayDimString }\n";
            $attrOverride .= "\t},\n";
            $attrSync     .= "\t\t{ $arrayDimString }\n";
            $attrSync     .= "\t},\n";
        }

        #----------------------------------------------------------------------
        # Print if the attribute is privileged
        #----------------------------------------------------------------------
        if ( exists $attr->{privileged} )
        {
            print AIFILE "const bool $attr->{id}_Privileged = true;\n";
        }
        else
        {
            print AIFILE "const bool $attr->{id}_Privileged = false;\n";
        }

        #----------------------------------------------------------------------
        # Print if the attribute is a initToZero attribute
        #----------------------------------------------------------------------
        if ( exists $attr->{initToZero} )
        {
            print AIFILE "const bool $attr->{id}_InitToZero = true;\n";
        }
        else
        {
            print AIFILE "const bool $attr->{id}_InitToZero = false;\n";
        }

        #----------------------------------------------------------------------
        # Print the target type(s) that the attribute is associated with
        #----------------------------------------------------------------------
        if ( !exists $attr->{targetType} )
        {
            print("parseAttributeInfo.pl ERROR. Att 'targetType' missing\n");
            exit(1);
        }

        print AIFILE "const fapi2::TargetType $attr->{id}_TargetType = ";

        # Split on commas
        my @targTypes = split( ',', $attr->{targetType} );

        my $targTypeCount = 0;
        foreach my $targType (@targTypes)
        {
            # Remove newlines and leading/trailing whitespace
            $targType =~ s/\n//;
            $targType =~ s/^\s+//;
            $targType =~ s/\s+$//;

            if ( $targTypeCount != 0 )
            {
                print AIFILE " | ";
            }
            print AIFILE "fapi2::$targType";
            $targTypeCount++;

            $attrSyncData{$targType}{ $attr->{id} } = $attrSync;
        }
        print AIFILE ";\n";

        #----------------------------------------------------------------------
        # Print if the attribute is a platInit attribute
        #----------------------------------------------------------------------
        if ( exists $attr->{platInit} )
        {
            print AIFILE "const bool $attr->{id}_PlatInit = true;\n";
        }
        else
        {
            print AIFILE "const bool $attr->{id}_PlatInit = false;\n";
        }

        #----------------------------------------------------------------------
        # Print the value enumeration (if specified) to attribute_ids.H and
        # attributeEnums.txt
        #----------------------------------------------------------------------
        if ( exists $attr->{enum} )
        {
            print AIFILE "enum $attr->{id}_Enum\n{\n";

            # Values must be separated by commas to allow for values to be
            # specified: <enum>VAL_A = 3, VAL_B = 5, VAL_C = 0x23</enum>
            my @vals = split( ',', $attr->{enum} );

            foreach my $val (@vals)
            {
                # Remove newlines and leading/trailing whitespace
                $val =~ s/\n//;
                $val =~ s/^\s+//;
                $val =~ s/\s+$//;

                my @values = split( '=', ${val} );

                # Remove newlines and leading/trailing whitespace

                foreach my $value (@values)
                {
                    $value =~ s/\n//;
                    $value =~ s/^\s+//;
                    $value =~ s/\s+$//;
                }

                # Need to add LL/ULL extensions so this generated file
                # will compile in x86.nfp context
                my $number = $values[1];
                if ( $attr->{valueType} eq 'uint64' )
                {
                    $number .= "ULL";
                }
                elsif ( $attr->{valueType} eq 'int64' )
                {
                    $number .= "LL";
                }
                push @attrOverrideEnums, "\t{ \"$attr->{id}_$values[0]\", $number },\n";

                # Print the attribute enum to attribute_ids.H
                print AIFILE "    ENUM_$attr->{id}_${val}";

                # Print the attribute enum to attrEnumInfo.csv
                my $attrEnumTxt = "$attr->{id}_${val}\n";

                $attrEnumTxt =~ s/ = /,/;
                print ETFILE $attrEnumTxt;

                if ( $attr->{valueType} eq 'uint64' )
                {
                    print AIFILE "ULL";
                }
                elsif ( $attr->{valueType} eq 'int64' )
                {
                    print AIFILE "LL";
                }

                print AIFILE ",\n";
            }

            print AIFILE "};\n";
        }

        #----------------------------------------------------------------------
        # Print if the attribute has a default value
        #----------------------------------------------------------------------
        if ( $parse_defaults == 1 )
        {
            if ( exists $attr->{default} )
            {
                print AIFILE "const bool $attr->{id}_hasDefault = true;\n";

                #----------------------------------------------------------------
                # Handle default attribute values and printing
                #----------------------------------------------------------------

                # Need to look for and convert ENUM values.
                # Need to add ULL/LL to 64 bit values
                # Need to handle array inits
                my $defaultVal   = $attr->{default};
                my @defaultVals  = split( ',', $defaultVal );
                my @defaultArray = ();

                foreach my $defVal (@defaultVals)
                {
                    # Remove leading/trailing whitespace
                    $defVal =~ s/^\s+//;
                    $defVal =~ s/\s+$//;

                    # Determine if this is an enum.  If it contains a character, and it doesn't start with "0x"
                    # then assume it's an enum
                    my $defaultIsEnum = ( ( $defVal =~ /[[:alpha:]]/ ) && ( substr( $defVal, 0, 2 ) ne "0x" ) );
                    if ( $defaultIsEnum && ( exists $attr->{enum} ) )
                    {
                        $defaultVal = "fapi2::ENUM_$attr->{id}_$defVal";
                    }
                    else
                    {
                        $defaultVal = $defVal;
                        if ( $attr->{valueType} eq 'uint64' )
                        {
                            $defaultVal = $defaultVal . "ULL";
                        }
                        elsif ( $attr->{valueType} eq 'int64' )
                        {
                            $defaultVal = $defaultVal . "LL";
                        }
                    }
                    if ( $attr->{array} )
                    {
                        push @defaultArray, $defaultVal;
                    }
                }

                if ( $attr->{array} )
                {
                    for my $idx ( 0 .. $#defaultArray )
                    {
                        if ( $idx == 0 )
                        {
                            $defaultVal = "{" . @defaultArray[$idx];
                        }
                        else
                        {
                            $defaultVal = $defaultVal . "," . @defaultArray[$idx];
                        }

                    }
                    $defaultVal = $defaultVal . "}";

                }

                print AIFILE "const $attr->{id}_Type $attr->{id}_Default = $defaultVal;\n";

            }
            else
            {
                print AIFILE "const bool $attr->{id}_hasDefault = false;\n";
            }

        }

        #----------------------------------------------------------------------
        # Print _GETMACRO and _SETMACRO where appropriate to attribute_ids.H
        #----------------------------------------------------------------------
        if ( exists $attr->{chipEcFeature} )
        {
            #------------------------------------------------------------------
            # The attribute is a Chip EC Feature, define _GETMACRO to call a
            # fapi2 function and define _SETMACRO to something that will cause a
            # compile failure if a set is attempted
            #------------------------------------------------------------------
            print AIFILE "#define $attr->{id}_GETMACRO(ID, PTARGET, VAL) \\\n";
            print AIFILE "    PLAT_GET_CHIP_EC_FEATURE_OVERRIDE(ID, PTARGET, VAL) ? fapi2::ReturnCode() : \\\n";
            print AIFILE "    queryChipEcFeature(fapi2::int2Type<ID>(), PTARGET, VAL)\n";
            print AIFILE "#define $attr->{id}_SETMACRO(ID, PTARGET, VAL) ";
            print AIFILE "CHIP_EC_FEATURE_ATTRIBUTE_NOT_WRITABLE\n";
        }
        elsif ( !exists $attr->{writeable} )
        {
            #------------------------------------------------------------------
            # The attribute is read-only, define the _SETMACRO to something
            # that will cause a compile failure if a set is attempted
            #------------------------------------------------------------------
            if ( !exists $attr->{writeable} )
            {
                print AIFILE "#define $attr->{id}_SETMACRO ATTRIBUTE_NOT_WRITABLE\n";
            }
        }

        #----------------------------------------------------------------------
        # If the attribute is a Chip EC Feature, print the chip EC feature
        # query to fapi2_chip_ec_feature.H
        #----------------------------------------------------------------------
        # Each EC attribute will generate a new inline overloaded version of
        # hasFeature with the attribute specific logic
        if ( exists $attr->{chipEcFeature} )
        {
            my $chipCount     = 0;
            my $falseIfMatch  = exists $attr->{chipEcFeature}->{falseIfMatch};
            my $noMatchValue  = $falseIfMatch ? 1 : 0;
            my $yesMatchValue = $falseIfMatch ? 0 : 1;

            print ECHFILE " inline uint8_t hasFeature(int2Type<$attr->{id}>,\n";
            print ECHFILE "                       fapi2::ATTR_NAME_Type i_name,\n";
            print ECHFILE "                       fapi2::ATTR_EC_Type i_ec)\n";
            print ECHFILE " {\n";
            print ECHFILE "    uint8_t hasFeature = $noMatchValue;\n\n";
            print ECHFILE "    if(";

            foreach my $chip ( @{ $attr->{chipEcFeature}->{chip} } )
            {
                $chipCount++;

                if ( !exists $chip->{name} )
                {
                    print("parseAttributeInfo.pl ERROR. Att 'name' missing\n");
                    exit(1);
                }

                if ( !exists $chip->{ec} )
                {
                    print("parseAttributeInfo.pl ERROR. Att 'ec' missing\n");
                    exit(1);
                }

                if ( !exists $chip->{ec}->{value} )
                {
                    print("parseAttributeInfo.pl ERROR. Att 'value' missing\n");
                    exit(1);
                }

                if ( !exists $chip->{ec}->{test} )
                {
                    print("parseAttributeInfo.pl ERROR. Att 'test' missing\n");
                    exit(1);
                }

                my $test;
                if ( $chip->{ec}->{test} eq 'EQUAL' )
                {
                    $test = '==';
                }
                elsif ( $chip->{ec}->{test} eq 'GREATER_THAN' )
                {
                    $test = '>';
                }
                elsif ( $chip->{ec}->{test} eq 'GREATER_THAN_OR_EQUAL' )
                {
                    $test = '>=';
                }
                elsif ( $chip->{ec}->{test} eq 'LESS_THAN' )
                {
                    $test = '<';
                }
                elsif ( $chip->{ec}->{test} eq 'LESS_THAN_OR_EQUAL' )
                {
                    $test = '<=';
                }
                else
                {
                    print("parseAttributeInfo.pl ERROR. test '$chip->{ec}->{test}' unrecognized\n");
                    exit(1);
                }

                if ( $chipCount > 1 )
                {
                    print ECHFILE " ||\n\t";
                }
                print ECHFILE "((i_name == $chip->{name}) && ";
                print ECHFILE " (i_ec $test $chip->{ec}->{value}))";
            }
            print ECHFILE ")\n";
            print ECHFILE "     {\n";
            print ECHFILE "         hasFeature = $yesMatchValue;\n";
            print ECHFILE "     }\n";
            print ECHFILE "     return hasFeature;\n";
            print ECHFILE " };\n";
        }

        #----------------------------------------------------------------------
        # Print the platform attribute checks to attribute_plat_check.H
        #----------------------------------------------------------------------
        if ( exists $attr->{writeable} )
        {
            print ACFILE "#ifndef $attr->{id}_SETMACRO\n";
            print ACFILE "#error Platform does not support set of HWPF attr $attr->{id}\n";
            print ACFILE "#endif\n";
        }

        print ACFILE "#ifndef $attr->{id}_GETMACRO\n";
        print ACFILE "#error Platform does not support get of HWPF attr $attr->{id}\n";
        print ACFILE "#endif\n\n";

        #----------------------------------------------------------------------
        # Print newline between each attribute's info to attribute_ids.H
        #----------------------------------------------------------------------
        print AIFILE "\n";

        #----------------------------------------------------------------------
        # Add attribute override string to map.
        #----------------------------------------------------------------------
        $attrOverrideData{ $attr->{id} } = $attrOverride;
    }
}

#------------------------------------------------------------------------------
# Print End of file information to attribute_ids.H
#------------------------------------------------------------------------------
print AIFILE "}\n\n";
print AIFILE "#endif\n";

print ECHFILE "}\n";
print ECHFILE "#endif\n";

#------------------------------------------------------------------------------
# Print End of file information to attribute_plat_check.H
#------------------------------------------------------------------------------
print ACFILE "#endif\n";

#------------------------------------------------------------------------------
# Print End of file information to attributesSupported.html
#------------------------------------------------------------------------------
print ASFILE "</table>\n\n";
print ASFILE "</body>\n";
print ASFILE "</html>\n";

#------------------------------------------------------------------------------
# Print content for getFapiAttrData.C
#------------------------------------------------------------------------------
foreach my $override ( sort keys %attrOverrideData )
{
    print FMFILE $attrOverrideData{$override};
}
print FMFILE "};\n";

#------------------------------------------------------------------------------
# Print content for fapi2AttrSyncData.H
#------------------------------------------------------------------------------
while ( ( my $syncTarget, my $syncList ) = each(%attrSyncData) )
{
    $syncTarget =~ s/TARGET_TYPE_//g;

    #print "TYPE: $syncTarget\n";

    print FSFILE "const AttributeSyncInfo g_${syncTarget}_syncInfo[] = {\n";
    while ( ( my $attrName, my $attrData ) = each( %{$syncList} ) )
    {
        print FSFILE "\n\t//$attrName\n";
        print FSFILE "$attrData";
    }
    print FSFILE "};\n\n";
}

#print helper functions
print FSFILE "const AttributeSyncInfo * get_fapi_target_attr_array(fapi2::TargetType i_type";
print FSFILE ", size_t & o_size)\n";
print FSFILE "{\n";
print FSFILE "\tconst AttributeSyncInfo * l_arr = NULL;\n";
print FSFILE "\to_size = 0;\n";
print FSFILE "\tswitch(i_type)\n";
print FSFILE "\t{\n";

while ( ( my $syncTarget, my $syncList ) = each(%attrSyncData) )
{
    my $name = $syncTarget;
    $syncTarget =~ s/TARGET_TYPE_//g;

    print FSFILE "\t\tcase fapi2::$name :\n";
    print FSFILE "\t\t\tl_arr = g_${syncTarget}_syncInfo;\n";
    print FSFILE "\t\t\to_size = sizeof(g_${syncTarget}_syncInfo)/";
    print FSFILE "sizeof(g_${syncTarget}_syncInfo[0]);\n";
    print FSFILE "\t\t\tbreak;\n\n";
}

print FSFILE "\t\tdefault:\n";
print FSFILE "\t\t\tl_arr = NULL;\n";
print FSFILE "\t};\n";
print FSFILE "\treturn l_arr;\n";
print FSFILE "}\n";
print FSFILE "}\n\n";
print FSFILE "#endif\n";

#------------------------------------------------------------------------------
# Print footer for getFapiAttrEnumData.C
#------------------------------------------------------------------------------
foreach my $override ( sort @attrOverrideEnums )
{
    print FEFILE $override;
}
print FEFILE "};\n";

#------------------------------------------------------------------------------
# Close output files
#------------------------------------------------------------------------------
close(AIFILE);
close(ECHFILE);
close(ACFILE);
close(ASFILE);
close(ITFILE);
close(ETFILE);
close(FMFILE);
close(FSFILE);
close(FEFILE);
OpenPOWER on IntegriCloud