summaryrefslogtreecommitdiffstats
path: root/src/import/hwpf/fapi2/tools/createIfAttrService.pl
blob: 0468b335f64e32dea969acb780d464146cc52a6b (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
#!/usr/bin/perl
# IBM_PROLOG_BEGIN_TAG
# This is an automatically generated prolog.
#
# $Source: src/import/hwpf/fapi2/tools/createIfAttrService.pl $
#
# OpenPOWER HostBoot Project
#
# Contributors Listed Below - COPYRIGHT 2015,2016
# [+] 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 qw(:config pass_through);

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

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

my $numArgs = $#ARGV + 1;
if (($numArgs < 2) || ($arg_output_dir eq undef))
{
    print ("Usage: createIfAttrService.pl --output-dir=<output dir>\n");
    print ("           [<if-attr-file1> <if-attr-file2> ...]\n");
    print ("           -a <attr-xml-file1> [<attr-xml-file2> ...]\n");
    print ("  This perl script will parse if-attr files (containing the\n");
    print ("  attributes used by the initfile) and attribute XML files\n");
    print ("  (containing all HWPF attributes) and create the\n");
    print ("  getInitFileAttr() function in a file called\n");
    print ("  fapi2_attribute_service.C. Only the attributes specified in\n");
    print ("  the if-attr files are supported. If no if-attr files are\n");
    print ("  specified then all attributes are supported\n");
    exit(1);
}

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

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

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

#------------------------------------------------------------------------------
# Open output file for writing
#------------------------------------------------------------------------------
my $asFile = $arg_output_dir;
$asFile .= "/";
$asFile .= "fapi2_attribute_service.C";
open(ASFILE, ">", $asFile);

#------------------------------------------------------------------------------
# Print Start of file information to fapi2_attribute_service.C
#------------------------------------------------------------------------------
print ASFILE "// fapi2_attribute_service.C\n";
print ASFILE "// This file is generated by perl script createIfAttrService.pl\n\n";
print ASFILE "#include <string.h>\n";
print ASFILE "#include <fapi2_attribute_service.H>\n";
print ASFILE "#include <fapi2_chip_ec_feature.H>\n";
print ASFILE "#include <plat_trace.H>\n\n";
print ASFILE "\n\n";
print ASFILE "#define FAPI_ATTR_GET_RAW(ID, TARGET, VAL) \\\n";
print ASFILE "     ID##_GETMACRO(ID, TARGET, VAL)\n\n";
print ASFILE "namespace fapi2\n";
print ASFILE "{\n\n";
print ASFILE "ReturnCode rawAccessAttr(const AttributeId i_id,\n";
print ASFILE "                         const Target<TARGET_TYPE_ALL>& i_target,\n";
print ASFILE "                         uint8_t * o_val)\n";
print ASFILE "{\n";
print ASFILE "    ReturnCode l_rc;\n\n";

my $xmlFiles = 0;
my $attCount = 0;
my $numIfAttrFiles = 0;
my @attrIds;

#------------------------------------------------------------------------------
# Element names
#------------------------------------------------------------------------------
my $attribute = 'attribute';

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

    if ($infile eq '-a')
    {
        # Start of attribute XML files
        $xmlFiles = 1;
        next;
    }

    if ($xmlFiles == 0)
    {
        #----------------------------------------------------------------------
        # Process initfile attr file. This file contains the HWPF attributes
        # that the initfile uses.
        #----------------------------------------------------------------------
        $numIfAttrFiles++;
        open(ATTRFILE, "<", $infile);

        # Read each line of the file (each line contains an attribute)
        while(my $fileAttrId = <ATTRFILE>)
        {
            # Remove newline
            chomp($fileAttrId);

            # Store the attribute in @attrIds if it does not already exist
            my $match = 0;

            foreach my $attrId (@attrIds)
            {
                if ($fileAttrId eq $attrId)
                {
                    $match = 1;
                    last;
                }
            }

            if (!($match))
            {
                push(@attrIds, $fileAttrId);
            }
        }

        close(ATTRFILE);
    }
    else
    {
        #----------------------------------------------------------------------
        # Process XML file. The ForceArray option ensures that there is an
        # array of attributes even if there is only one attribute in the file
        #----------------------------------------------------------------------
        my $attributes = $xml->XMLin($infile, ForceArray => [$attribute]);

        #----------------------------------------------------------------------
        # For each Attribute
        #----------------------------------------------------------------------
        foreach my $attr (@{$attributes->{attribute}})
        {
            #------------------------------------------------------------------
            # Check that the AttributeId exists
            #------------------------------------------------------------------
            if (! exists $attr->{id})
            {
                print ("createIfAttrService.pl ERROR. Att 'id' missing\n");
                exit(1);
            }

            #------------------------------------------------------------------
            # Find if the attribute is used by any initfile. If no if-attr
            # files were specified then support all attributes
            #------------------------------------------------------------------
            my $match = 0;

            if ($numIfAttrFiles)
            {
                foreach my $attrId (@attrIds)
                {
                    if ($attr->{id} eq $attrId)
                    {
                        $match = 1;
                        last;
                    }
                }
            }
            else
            {
                $match = 1;
            }

            if (!($match))
            {
                # Look at the next attribute in the XML file
                next;
            }

            #------------------------------------------------------------------
            # Figure out the number of attribute array dimensions
            #------------------------------------------------------------------
            my $numArrayDimensions = 0;
            if ($attr->{array})
            {
                # Remove leading whitespace
                my $dimText = $attr->{array};
                $dimText =~ s/^\s+//;

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

                $numArrayDimensions=@vals;
            }

            #------------------------------------------------------------------
            # Print the attribute get code to fapi2_attribute_service.C
            #------------------------------------------------------------------
            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";

            if (exists $attr->{privileged})
            {
                print ASFILE "        l_rc = FAPI_ATTR_GET_RAW(fapi2::$attr->{id}, i_target, l_attr);\n";
            }
            else
            {
                print ASFILE "        l_rc = FAPI_ATTR_GET_RAW(fapi2::$attr->{id}, i_target, l_attr);\n";
            }
            print ASFILE "        memcpy(o_val, &l_attr, sizeof($attr->{id}_Type));\n";
            print ASFILE "    }\n";
        }
    }
}

#------------------------------------------------------------------------------
# Print End of file information to fapi2_attribute_service.C
#--------------------------------------------------------------------------
if ($attCount > 0)
{
    print ASFILE "    else\n";
}
print ASFILE "    {\n";
print ASFILE "        FAPI_ERR(\"getInitFileAttr: Unrecognized attr ID: 0x%x\", i_id);\n";
print ASFILE "    }\n\n";
print ASFILE "    if (l_rc)\n";
print ASFILE "    {\n";
print ASFILE "            FAPI_ERR(\"getInitFileAttr: Error getting attr ID 0x%x from targType 0x%x\",\n";
print ASFILE "                     i_id, i_target.getType());\n";
print ASFILE "    }\n\n";
print ASFILE "    return l_rc;\n";
print ASFILE "}\n\n";
print ASFILE "}\n";


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