summaryrefslogtreecommitdiffstats
path: root/src/usr/targeting/xmltohb/fapi_utils.pl
blob: f1e7bd124b95fdef5476ea6711b7e0b2eeafa097 (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
#!/usr/bin/perl
# IBM_PROLOG_BEGIN_TAG
# This is an automatically generated prolog.
#
# $Source: src/usr/targeting/xmltohb/fapi_utils.pl $
#
# OpenPOWER HostBoot Project
#
# Contributors Listed Below - COPYRIGHT 2017,2019
# [+] 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
# A collection of utility functions to convert fapi attributes to targeting attributes

use XML::Simple;
use Digest::MD5 qw(md5_hex);
use strict;
$XML::Simple::PREFERRED_PARSER = 'XML::Parser';
my $xml = new XML::Simple (KeyAttr=>[]);


# Convert a FAPI2 target type to the equivalent TARGETING type
#   Input: fapi2 type
#   Output: targeting type
sub convertTargetFapi2Targ
{
    my $fapitype = shift;
    my $targtype;

    $targtype =~ s/\s//g;
    $targtype =~ s/TARGET_TYPE_//;
    $targtype =~ s/_ENDPOINT//;
    $targtype =~ s/_CHIPLET//;
    $targtype =~ s/_CHIP//;
    $targtype =~ s/^SYSTEM$/SYS/;

    #todo - check result against list of types from target_types?

    return $targtype;
}


# Convert a FAPI2 value type to the equivalent TARGETING type
#   Input: fapi2 type
#   Output: targeting type
sub convertValueFapi2Targ
{
    my $fapitype = shift;
    my $targtype = $fapitype;

    $targtype =~ s/(uint\d+)/$1_t/ if($fapitype =~ /^uint\d+$/);
    $targtype =~ s/(int\d+)/$1_t/  if($fapitype =~ /^int\d+$/);

    #todo - check result against list of types from target_types?

    return $targtype;
}

# Create a TARGETING style enumeration based on the enum tag from
#  a FAPI2 attribute definition
#   Input: fapi2 attribute
#   Output: targeting enumeration
sub createEnumFromAttr(\%)
{
    my($fapiattr) = @_;
    my @enums;

    if (exists $fapiattr->{enum})
    {
        # description: passed as-is
        my $fapiattr_id = $fapiattr->{id};
        my $id = $fapiattr_id;
        $id =~ s/ATTR_//;
        my $description = $fapiattr->{description};
        $description =~ s/^\s+|\s+$//g;


        my $enum = $fapiattr->{enum};
        my @enumerators = split( /,/, $enum);
        my @enumeratorHashArray;

        foreach my $enumerator (@enumerators)  {
            my %enumeratorHash;
            chomp($enumerator);
            $enumerator =~ s/^\s+|\s+$//g;

            my @nameVal = split( /=/, $enumerator);

            my $name = $nameVal[0];
            $name =~ s/^\s+|\s+$//g;
            my $value = $nameVal[1];
            $value =~ s/^\s+|\s+$//g;

            my %enumeratorHash = (
                name => $name,
                value => $value
            );

            push @enumeratorHashArray, \%enumeratorHash;
        }

        my %enumToAdd = (
            id => $id,
            description => $description,
        );
        $enumToAdd{'enumerator'} = [@enumeratorHashArray];
        return \%enumToAdd
    }
}

# Create full attribute definition from a fapi2 attribute definition
#   Input: hashmap of a single fapi attribute
#   Output: hashmap of a single targeting attribute
sub createAttrFromFapi(\%)
{
    my($fapiattr) = @_;
    my $targattr = {};

    # id: passed as-is
    my $fapiattr_id = $fapiattr->{id};
    my $id = $fapiattr_id;
    $id =~ s/ATTR_//;
    $targattr->{id} = $id;

    # description: passed as-is
    my $description = $fapiattr->{description};
    if(ref $fapiattr->{description} && eval {keys %{$fapiattr->{description}} == 0} )
    {
        $targattr->{description} = "place holder description";
    }
    else
    {
        $targattr->{description} = $description;
    }

    # valueType: convert
    my $valueType = convertValueFapi2Targ($fapiattr->{valueType});
    $targattr->{simpleType}->{$valueType} = {};

    # writeable: passed as-is
    if( exists $fapiattr->{writeable} )
    {
        $targattr->{writeable} = {};
    }

    #default: modifies simpleType
    if( exists $fapiattr->{default} )
    {
        $targattr->{simpleType}->{$valueType}->{default} =
            $fapiattr->{default};
    }

    #array: modifies simpleType
    if( exists $fapiattr->{array} )
    {
        my @dimensions = split(' ',$fapiattr->{array});
        my $dimensions_cs = @dimensions[0];
        for my $i ( 1 .. $#dimensions )
        {
            $dimensions_cs .= ",$dimensions[$i]";
        }
        $dimensions_cs =~ s/,,/,/g;
        $targattr->{simpleType}->{array} = $dimensions_cs;
    }

    #platInit: influences persistency
    #initToZero: influences persistency
    #overrideOnly: influences persistency
    if( exists $fapiattr->{platInit} )
    {
        if( exists $fapiattr->{overrideOnly} )
        {
            if( exists $fapiattr->{default} )
            {
                $targattr->{persistency} = "volatile";
            }
            else
            {
                $targattr->{persistency} = "volatile-zeroed";
            }
        }
        else
        {
            $targattr->{persistency} = "non-volatile";
        }
    }
    elsif( exists $fapiattr->{initToZero} )
    {
        if( exists $fapiattr->{default} )
        {
            print "INVALID - $fapiattr_id has initToZero and a default\n";
        }
        $targattr->{persistency} = "volatile-zeroed";
    }
    elsif( exists $fapiattr->{default} )
    {
        $targattr->{persistency} = "volatile";
    }
    else
    {
        $targattr->{persistency} = "volatile-zeroed";
    }

    #mrwHide:  convert to no_export to hide from ServerWiz
    if( exists $fapiattr->{mrwHide} )
    {
        $targattr->{no_export} = {};
    }

    #mssUnits: ignore
    #mssAccessorName: ignore
    #odmVisible: ignore
    #odmChangeable: ignore
    #persistent: ignore
    #persistRuntime: ignore

    #enum: ignored here
    #targetType: ignored here

    #always add these
    $targattr->{readable} = {};
    $targattr->{hwpfToHbAttrMap}->{id} = $fapiattr_id;
    $targattr->{hwpfToHbAttrMap}->{macro} = "DIRECT";

#    print Dumper($targattr);

#    printTargAttr($targattr);

    return $targattr;
}

# Create targetTypeExtensions from a fapi2 attribute definition
#   Input: hashmap of a single fapi attribute
#          array of all targetTypeExtensions
sub createTargetExtensionFromFapi(\%,\%)
{
    my($fapiattr,$alltargext) = @_;
    #print "createTargetExtensionFromFapi---\n";
    open my $FHSTDOUT, ">&STDOUT";

    # Conversions from FAPI2 to TARGETING types
    my $fapi2targ = {
    TARGET_TYPE_SYSTEM        => "sys-sys-power9",
    TARGET_TYPE_DIMM          => "lcard-dimm",
    TARGET_TYPE_PROC_CHIP     => "chip-processor",
    TARGET_TYPE_MEMBUF_CHIP   => "chip-membuf-centaur",
    TARGET_TYPE_EX            => "unit-ex-power9",
    TARGET_TYPE_MBA           => "unit-mba-centaur",
    TARGET_TYPE_MCS           => "unit-mcs-power9",
    TARGET_TYPE_XBUS          => "unit-xbus-power9",
    TARGET_TYPE_ABUS          => "unit-abus-power9",
    TARGET_TYPE_L4            => "unit-l4-power9",
    TARGET_TYPE_CORE          => "unit-core-power9",
    TARGET_TYPE_EQ            => "unit-eq-power9",
    TARGET_TYPE_MCA           => "unit-mca-power9",
    TARGET_TYPE_MCBIST        => "unit-mcbist-power9",
    TARGET_TYPE_MI            => "unit-mi-power9",
    TARGET_TYPE_CAPP          => "unit-capp-power9",
    TARGET_TYPE_DMI           => "unit-dmi-power9",
    TARGET_TYPE_OBUS          => "unit-obus-power9",
    TARGET_TYPE_OBUS_BRICK    => "unit-obus-brick-power9",
    TARGET_TYPE_SBE           => "unit-sbe-power9",
    TARGET_TYPE_PPE           => "unit-ppe-power9",
    TARGET_TYPE_PERV          => "unit-perv-power9",
    TARGET_TYPE_PEC           => "unit-pec-power9",
    TARGET_TYPE_PHB           => "unit-phb-power9",
    TARGET_TYPE_MC            => "unit-mc-power9",
    TARGET_TYPE_OMI           => "unit-omi-power9",
    TARGET_TYPE_OMIC          => "unit-omic-power9",
    TARGET_TYPE_MCC           => "unit-mcc-power9",
    TARGET_TYPE_OCMB_CHIP     => "chip-ocmb",
    TARGET_TYPE_MEM_PORT      => "unit-mem_port",
    TARGET_TYPE_PMIC          => "pmic",
    };

    # Loop through all of the targets that this attribute
    #  is needed on (per fapi xml)
    my @types = split(',',$fapiattr->{targetType});
    foreach my $type(@types)
    {
        my $foundmatch = 0;
        $type =~ s/\s//g;
        my $targtype = $fapi2targ->{$type};
        # print "type = $type -> $targtype\n";
        my $attrid = $fapiattr->{id};
        $attrid =~ s/ATTR_//;

        # create new attribute element
        my $newattr = {};
        $newattr->{id} = $attrid;

        # look for an existing targetTypeExtension entry
        #  to modify with new attribute
        foreach my $targ (@{$alltargext->{targetTypeExtension}})
        {
            if( $targ->{id} =~ $targtype )
            {
                #print "-Found it\n";
                $foundmatch = 1;
                #printTargExt($FHSTDOUT,$targ);
                my $attrlist = $targ->{attribute};
                push @$attrlist, $newattr;
                #printTargExt($FHSTDOUT,$targ);
                last;
            }
        }

        # no existing entry for this kind of target, make a new one
        if( $foundmatch == 0 )
        {
            #print "-No entry found for $targtype, creating new entry\n";
            my $newext = {};
            $newext->{id} = $targtype;
            my $newarray = [];
            push @$newarray, $newattr;
            $newext->{attribute} = $newarray;
            my $allext = $alltargext->{targetTypeExtension};
            push @$allext, $newext;
            #printTargExt($FHSTDOUT,$newext);
        }
    }


    #print Dumper($alltargets);


#    print "---\n";
#    printTargTarg($targtarg);
#    print "---done\n";
}



# Print string representation of a targeting attribute
#   Input: hashmap of a single targeting attribute
#   Output: string of xml tags
sub printTargAttr
{
    my($FH1,$targattr) = @_;
    print $FH1 $xml->XMLout( $targattr, RootName => 'attribute', NoAttr => 1 );
}

# Print string representation of a targeting enumeration
#   Input: hashmap of a single targeting enumeration
#   Output: string of xml tags
sub printTargEnum
{
    my($FH1,$targattr) = @_;
    print $FH1 $xml->XMLout( $targattr, RootName => 'enumerationType', NoAttr => 1 );
}

# Print string representation of a targeting target
#   Input: hashmap of a single targeting target
#   Output: string of xml tags
sub printTargTarg
{
    my($FH1,$targtarg) = @_;
    print $FH1 $xml->XMLout( $targtarg, RootName => 'targetType', NoAttr => 1 );
}

# Print string representation of a targeting targetExtension
#   Input: hashmap of a single targeting target
#   Output: string of xml tags
sub printTargExt
{
    my($FH1,$targtarg) = @_;
    print $FH1 $xml->XMLout( $targtarg, RootName => 'targetTypeExtension', NoAttr => 1 );
}

# getArrayDimmensions
# Description: for a given attribute hashMap , return the array dimmensions
#              if the attribute type is an array
# input      : hashMap of attribute xml
# return     : String of CSV list that lists the array dimmensions
sub getArrayDimmensions{
    my (%attrHash) = @_;
    my $retValue = "";

    my $simpleType = $attrHash{simpleType};

    my @keys = keys (%$simpleType);

    for my $key (@keys)
    {
        if( $key eq "array")
           {
                $retValue .= $attrHash{simpleType}->{$key};
           }
    }
    #eat whitespace
    $retValue =~ s/\s+//g;
    return $retValue;
}

# getFuncionBackedAttrs
# Description: Lookup all of the attributes that HB is backing with functions
# input      : full path to attribute_service.H
# return     : array of attribute IDs that are function backed
sub getFuncionBackedAttrs {
    my ($headerFile) = @_;
    my @attrIdArray;
    open(my $attr_service_fh, '<', $headerFile) || die "unable to open $headerFile";

    foreach my $row (<$attr_service_fh>)
    {
        my $attrIndex = index($row, "ATTR_");
        my $getMacroIndex = index($row, "_GETMACRO(ID");
        if($getMacroIndex != -1)
        {
            my $attrToAdd = substr $row, $attrIndex, $getMacroIndex - $attrIndex;
            push @attrIdArray, $attrToAdd;
            print " $0> FOUND $attrToAdd GETMACRO\n";
        }
    }
    return @attrIdArray;
}

# getAttrType
# Description: for a given attribute hashMap , return the attribute type
# input      : hashMap of attribute xml
# return     : string represention attribute type
sub getAttrType {
    my (%attrHash) = @_;
    my $retValue = "";

    my $simpleType = $attrHash{simpleType};

    my @keys = keys (%$simpleType);

    for my $key (@keys)
    {
        if( $key ne "array")
           {
                $retValue .= $key;
           }
    }
    return $retValue;
}


# need to return 1 for other modules to include this
1;
OpenPOWER on IntegriCloud