summaryrefslogtreecommitdiffstats
path: root/src/usr/hwpf/fapi/fapiParseAttributeInfo.pl
blob: 2d7051b5e6dc4974d742988129af22575413b3ba (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
#!/usr/bin/perl
#  IBM_PROLOG_BEGIN_TAG
#  This is an automatically generated prolog.
#
#  $Source: src/usr/hwpf/fapi/fapiParseAttributeInfo.pl $
#
#  IBM CONFIDENTIAL
#
#  COPYRIGHT International Business Machines Corp. 2011
#
#  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
#
# Purpose:  This perl script will parse HWP Attribute XML files
# and add attribute information to a file called fapiAttributeIds.H
#
# Author: CamVan Nguyen
# Last Updated: 06/23/2011
#
# Version: 1.0
#
# Change Log **********************************************************
#
#  Flag  Track#    Userid    Date      Description
#  ----  --------  --------  --------  -----------
#                  camvanng  06/03/11  Created
#                  mjjones   06/06/11  Minor updates for integration
#                  mjjones   06/10/11  Added "use strict;"
#                  mjjones   06/23/11  Parse more info
#                  mjjones   07/05/11  Take output dir as parameter
#                  mjjones   09/06/11  Remove string/defaultVal support 
#                  mjjones   10/07/11  Create fapiAttributeService.C
#                  mjjones   10/17/11  Support enums with values
#                  mjjones   10/18/11  Support multiple attr files and
#                                      multi-line descriptions
#                  camvanng  10/20/11  Changed i_pTarget to "const" ptr
#
# End Change Log ******************************************************

#
# Usage:
# fapiParseAttributeInfo.pl <output dir> <filename1> <filename2> ...

use strict;

#------------------------------------------------------------------------------
# Print Command Line Help
#------------------------------------------------------------------------------
my $numArgs = $#ARGV + 1;
if ($numArgs < 2)
{
    print ("Usage: fapiParseAttributeInfo.pl <output dir> <filename1> <filename2> ...\n");
    print ("  This perl script will parse attribute XML files and add\n");
    print ("  attribute information to a file called fapiAttributeIds.H\n");
    exit(1);
}

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

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

#------------------------------------------------------------------------------
# Open output files for writing
#------------------------------------------------------------------------------
my $aiFile = $ARGV[0];
$aiFile .= "/";
$aiFile .= "fapiAttributeIds.H";
open(AIFILE, ">", $aiFile);

my $asFile = $ARGV[0];
$asFile .= "/";
$asFile .= "fapiAttributeService.C";
open(ASFILE, ">", $asFile);

#------------------------------------------------------------------------------
# Print Start of file information to fapiAttributeIds.H
#------------------------------------------------------------------------------
print AIFILE "// fapiAttributeIds.H\n";
print AIFILE "// This file is generated by perl script fapiParseAttributeInfo.pl\n\n";
print AIFILE "#ifndef FAPIATTRIBUTEIDS_H_\n";
print AIFILE "#define FAPIATTRIBUTEIDS_H_\n\n";
print AIFILE "namespace fapi\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 fapiAttributeService.C
#------------------------------------------------------------------------------
print ASFILE "// fapiAttributeService.C\n";
print ASFILE "// This file is generated by perl script fapiParseAttributeInfo.pl\n\n";
print ASFILE "#include <fapiAttributeService.H>\n\n";
print ASFILE "namespace fapi\n";
print ASFILE "{\n\n";
print ASFILE "ReturnCode fapiGetInitFileAttr(const AttributeId i_id,\n";
print ASFILE "                               const Target * i_pTarget,\n";
print ASFILE "                               uint64_t & o_val,\n";
print ASFILE "                               const uint32_t i_arrayIndex1,\n";
print ASFILE "                               const uint32_t i_arrayIndex2,\n";
print ASFILE "                               const uint32_t i_arrayIndex3,\n";
print ASFILE "                               const uint32_t i_arrayIndex4)\n";
print ASFILE "{\n";
print ASFILE "    ReturnCode l_rc;\n\n";

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

    # read XML file
    my $attributes = $xml->XMLin($infile);

    # 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 AttributeId to fapiAttributeIds.H
        #----------------------------------------------------------------------
        if (! exists $attr->{id})
        {
            print ("fapiParseAttributeInfo.pl ERROR. Att 'id' missing\n");
            exit(1);
        }

        print AIFILE "    ", $attr->{id}, ",\n";
    };
}

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

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

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

    # read XML file
    my $attributes = $xml->XMLin($infile);

    #--------------------------------------------------------------------------
    # For each Attribute
    #--------------------------------------------------------------------------
    my $attCount = 0;

    foreach my $attr (@{$attributes->{attribute}})
    {
        #----------------------------------------------------------------------
        # Print a comment with the attribute description to fapiAttributeIds.H
        #----------------------------------------------------------------------
        if ($attr->{description})
        {
            print AIFILE "/* $attr->{id}: $attr->{description} */\n";
        }

        #----------------------------------------------------------------------
        # Figure out the attribute array dimensions (if array)
        #----------------------------------------------------------------------
        my $arrayDimensions = "";
        my $numArrayDimensions = 0;
        if ($attr->{array})
        {
            # Figure out the array dimensions

            # Remove leading whitespace
            my $dimText = $attr->{array};
            $dimText =~ s/^\s+//;

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

            foreach my $val (@vals)
            {
                $arrayDimensions .= "[${val}]";
                $numArrayDimensions++;
            }
        }

        #----------------------------------------------------------------------
        # Print the typedef for each attribute's val type to fapiAttributeIds.H
        #----------------------------------------------------------------------
        if (! exists $attr->{valueType})
        {
            print ("fapiParseAttributeInfo.pl ERROR. Att 'valueType' missing\n");
            exit(1);
        }

        if ($attr->{valueType} eq 'uint8')
        {
            print AIFILE "typedef uint8_t $attr->{id}_Type$arrayDimensions;\n";
        }
        elsif ($attr->{valueType} eq 'uint32')
        {
            print AIFILE "typedef uint32_t $attr->{id}_Type$arrayDimensions;\n";
        }
        elsif ($attr->{valueType} eq 'uint64')
        {
            print AIFILE "typedef uint64_t $attr->{id}_Type$arrayDimensions;\n";
        }
        else
        {
            print ("fapiParseAttributeInfo.pl ERROR. valueType not recognized: ");
            print $attr->{valueType}, "\n";
            exit(1);
        }

        #----------------------------------------------------------------------
        # Print if the platform initializes the value to fapiAttributeIds.H
        #----------------------------------------------------------------------
        if (exists $attr->{platInit})
        {
            print AIFILE "#define $attr->{id}_PLATINIT true\n"
        }
        else
        {
            print AIFILE "#define $attr->{id}_PLATINIT false\n"
        }

        #----------------------------------------------------------------------
        # Print the value enumeration (if specified) to fapiAttributeIds.H
        #----------------------------------------------------------------------
        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 leading spaces
                $val =~ s/^\s+//; 
                print AIFILE "    $attr->{id}_${val},\n";
            }

            print AIFILE "};\n";
        }

        #----------------------------------------------------------------------
        # If the attribute is read-only then 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";
        }

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

        #----------------------------------------------------------------------
        # Print the attribute get code to fapiAttributeService.C
        # *** TODO ***
        # Currently, all attributes are supported in fapiGetInitFileAttr() in
        # fapiAttributeService.C. Ideally only the initfile attributes should
        # be supported to minimize code size. Need to figure out how to do this
        #----------------------------------------------------------------------
        if ($attCount > 0)
        {
            print ASFILE "    else ";
        }
        else
        {
            print ASFILE "    ";
        }
        $attCount++;

        print ASFILE "if (i_id == $attr->{id})\n";
        print ASFILE "    {\n"; 
        print ASFILE "        $attr->{id}_Type l_attr;\n";
        print ASFILE "        l_rc = FAPI_ATTR_GET($attr->{id}, i_pTarget, l_attr);\n";
        print ASFILE "        o_val = l_attr";

        if ($numArrayDimensions >= 5)
        {
            print ("fapiParseAttributeInfo.pl ERROR. More than 4 array dimensions!!\n");
            exit(1);
        }
        else
        {
            for (my $i = 0; $i < $numArrayDimensions; $i++)
            {
                print ASFILE "[i_arrayIndex";
                print ASFILE $i+1;
                print ASFILE "]";
            }
        }

        print ASFILE ";\n";
        print ASFILE "    }\n";
    };
}

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

#------------------------------------------------------------------------------
# Print End of file information to fapiAttributeService.C
#--------------------------------------------------------------------------
print ASFILE "    else\n";
print ASFILE "    {\n";
print ASFILE "        FAPI_ERR(\"fapiGetInitFileAttr: Unrecognized attr: %d\", i_id);\n";
print ASFILE "        ReturnCodeFfdc::addEIFfdc(l_rc, static_cast<uint32_t>(i_id));";
print ASFILE "        l_rc = FAPI_RC_INVALID_ATTR_GET;\n";
print ASFILE "    }\n\n";
print ASFILE "    if (l_rc)\n";
print ASFILE "    {\n";
print ASFILE "        FAPI_ERR(\"fapiGetInitFileAttr: Error getting attr\");\n";
print ASFILE "    }\n\n";
print ASFILE "    return l_rc;\n";
print ASFILE "}\n\n";
print ASFILE "}\n";


#------------------------------------------------------------------------------
# Close output files
#------------------------------------------------------------------------------
close(AIFILE);
close(ASFILE);
OpenPOWER on IntegriCloud