summaryrefslogtreecommitdiffstats
path: root/src/usr/scom/genCentaurScomCacheRegDefs.pl
blob: 8e8574d42247611ec21c0391fc551eefb4a259e8 (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
#!/usr/bin/perl
# IBM_PROLOG_BEGIN_TAG
# This is an automatically generated prolog.
#
# $Source: src/usr/scom/genCentaurScomCacheRegDefs.pl $
#
# OpenPOWER HostBoot Project
#
# Contributors Listed Below - COPYRIGHT 2012,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;

################################################################################
# Imported packages
################################################################################

use Carp;
use Getopt::Long;
use Pod::Usage;

################################################################################
# Initialize some globals
################################################################################

use constant OUT_FILE_BASE=>"centaurScomCacheRegDefs";
use constant OUT_C_FILE=>OUT_FILE_BASE.".C";

################################################################################
# Process command line parameters, issue help text if needed.
################################################################################

my $cfgCsvFile ="Centaur_Register_List.csv";
my $cfgOutputDir = ".";
my $cfgHelp=0;
my $cfgMan=0;
my $cfgVerbose=0;

GetOptions("csv:s" => \$cfgCsvFile,
           "output-dir:s" =>  \$cfgOutputDir,
           "help" => \$cfgHelp,
           "man" => \$cfgMan,
           "verbose" => \$cfgVerbose ) || pod2usage(-verbose => 0);

pod2usage(-verbose => 1) if $cfgHelp;
pod2usage(-verbose => 2) if $cfgMan;

# Remove extraneous '/' from end of path names; use temporary version of $/ for
# the chomp
{
    local $/ = '/';
    chomp($cfgOutputDir);
    $cfgOutputDir .= "/";
}

if($cfgVerbose)
{
    print STDOUT "CSV file = $cfgCsvFile\n";
    print STDOUT "Output dir = $cfgOutputDir\n";
    print STDOUT "Output file name = " . OUT_C_FILE . "\n";
}

################################################################################
# Main logic
################################################################################

open(CSV_FILE,"<$cfgCsvFile")
    or croak("Failed to open CSV file of $cfgCsvFile.");
my $csvFile = *CSV_FILE;

my $outSrcFile="$cfgOutputDir".OUT_C_FILE;
open(OUT_SRC_FILE,">$outSrcFile")
    or croak("Failed to open output source file of "
        . "$outSrcFile");
my $sourceFile = *OUT_SRC_FILE;

writeScomRegDefSourceHeader($sourceFile);
writeScomRegDefSourceContent($csvFile,$sourceFile);
writeScomRegDefSourceFooter($sourceFile);

close CSV_FILE or croak("Failed to close CSV file of $cfgCsvFile");

close OUT_SRC_FILE or
    croak("Failed to close output source file of $outSrcFile");

################################################################################
#  @brief: Strips off leading and trailing whitespace from an input line
#      and returns it
#  @param[in] $line Line of input file
#  @return Line without leading and trailing whitespace
################################################################################

sub stripLeadingAndTrailingWhitespace {
    my($line) = @_;

    $line =~ s/^\s+|\s+$//g;

    return $line;
}

################################################################################
#  @brief: Verifies an input line ends with a comma
#  @param[in] $line Line of input file
################################################################################

sub verifyEndsWithComma
{
    my $line = shift;

    if ($line !~ m/,$/)
    {
         croak "BUG! CSV file doesn't end with comma\n\t$line";
    }
}

################################################################################
#  @brief: Verifies an input line has no starting comma
#  @param[in] $line Line of input file
################################################################################

sub verifyNoStartingComma
{
    my $line = shift;

    if ($line =~ m/^,/)
    {
         croak "BUG! CSV file should not start with comma\n\t$line";
    }
}

################################################################################
#  @brief: Returns whether number is a hex number without leading 0x
#  @param[in] $number Number to verify
#  @retval 0 if not a 0x-less hex number
#  @retval 1 if a 0x-less hex number
################################################################################

sub isValidNumber
{
    my $number=shift;
    my $valid=0;

    my $maxLength = 16;

    if ($number =~ m/^[0-9a-fA-F]{1,$maxLength}$/)
    {
        $valid=1;
    }

    return $valid;
}

################################################################################
#  @brief: Scrubs required register address and returns a sanitized copy
#  @param[in] $addr The register address
#  @param[in] $line Line of the input file to display on error conditions
#  @return A normalized register address
################################################################################

sub normalizeRequiredAddr
{
    my $addr=shift;
    my $line=shift;

    if(!defined $addr
       || $addr eq "")
    {
        croak "BUG! CSV file record had undefined register "
            . "address [$addr]\n\t$line";
    }

    if(!isValidNumber($addr))
    {
        croak "BUG! CSV file record has invalid address [$addr]\n\t$line";
    }

    $addr="0x".$addr."ULL";
    return $addr;
}

################################################################################
#  @brief: Scrubs optional input value and returns a sanitized copy
#  @param[in] $value The property value read from the input file
#  @param[in] $default The default value to use if file did not specify
#  @param[in] $name Name of the property to display on error conditions
#  @param[in] $line Line of the input file to display on error conditions
#  @return A normalized value
################################################################################

sub normalizeOptionalValue
{
    my $value=shift;
    my $default=shift;
    my $name=shift;
    my $line=shift;

    if(!defined $value
       || $value eq "")
    {
        $value=$default;
    }

    if(!isValidNumber($value))
    {
        croak "BUG! CSV file record has invalid optional $name "
            . "value [$value]\n"
            . "\tOffending line: $line";
    }

    $value="0x".$value."ULL";
    return $value;
}

################################################################################
#  @brief: Writes the leading mostly non-dynamic content for the output source
#      file
#  @param[in] $outFile Name of the output source file (no path components)
################################################################################

sub writeScomRegDefSourceHeader {
    my($outFile) = @_;

print $outFile <<VERBATIM;

/**
VERBATIM
print $outFile " *  \@file ".OUT_C_FILE."\n";
print $outFile <<VERBATIM;
 *
 *  \@brief Automatically generated source code to initialize the Centaur
 *      SCOM cache register definition vector
 */

//******************************************************************************
// Includes
//******************************************************************************

#include <scom/centaurScomCache.H>

namespace SECUREBOOT
{

namespace CENTAUR_SECURITY
{

ScomRegDefs* ScomCache::_initScomRegDefs() const
{
    ScomRegDefs* pScomRegDefs = new ScomRegDefs{
VERBATIM
}

################################################################################
#  @brief: Writes the dynamic register definition content to the output file
#
#  @param[in] $csvFile Input CSV file's handle to read from
#  @param[in] $outFile Output file's handle to write to
################################################################################

sub writeScomRegDefSourceContent {
    my($csvFile,$outFile) = @_;

    while( <$csvFile> )
    {
        my $line=stripLeadingAndTrailingWhitespace($_);

        #Ignore comments
        if ($line =~ m/^#/)
        {
            next;
        }

        verifyEndsWithComma($line);

        verifyNoStartingComma($line);

        my ($addr,$wandAddr,$worAddr,$init,$mask) = split(/,/,$line);

        $addr = normalizeRequiredAddr($addr,$line);

        $wandAddr = normalizeOptionalValue($wandAddr,"0","wand address",$line);

        $worAddr = normalizeOptionalValue($worAddr,"0","wor address",$line);

        $init = normalizeOptionalValue($init,"0","init",$line);

        my $defaultMask = "FFFFFFFFFFFFFFFF";

        $mask = normalizeOptionalValue(
            $mask,$defaultMask,"mask",$line);

        # Output columns are:
        # Address,WAND?,WOR?,base address,base index,init,mask

        if($wandAddr ne "0x0ULL")
        {
            print $outFile "        "
            . "{$wandAddr,0b1,0b0,$addr,-1,0x0ULL,0x0ULL},\n";

        }

        if($worAddr ne "0x0ULL")
        {
            print $outFile "        "
            . "{$worAddr,0b0,0b1,$addr,-1,0x0ULL,0x0ULL},\n";

        }

        print $outFile "        "
            . "{$addr,0b0,0b0,0x0ULL,-1,$init,$mask},\n";
    }
}

################################################################################
#  @brief: Writes the trailing non-dynamic content to the output source
#      file
#  @param[in] $outFile Output file handle to write to
################################################################################

sub writeScomRegDefSourceFooter {
    my($outFile) = @_;

print $outFile <<VERBATIM;
    };

    return pScomRegDefs;
}

} // End CENTAUR_SECURITY namespace

} // End SECUREBOOT namespace


VERBATIM
}

################################################################################
# Man page
################################################################################

1;

__END__

=head1 NAME

genCentaurScomCacheRegDefs.pl

=head1 SYNOPSIS

genCentaurScomCacheRegDefs.pl --csv=FILE --output-dir=DIR

=head1 OPTIONS

=over 8

=item B<--csv>

Absolute path and file name of CSV file containing register definitions

=item B<--output-dir>

Output dir to write the generated source file

=item B<--verbose>

Prints out some internal workings

=item B<--help>

Print a brief help message and exits

=item B<--man>

Prints the manual page and exits

=back

=head1 DESCRIPTION

B<getCentaurScomCacheRegDefs.pl> will generate a source file, which should be
compiled into Hostboot, that seeds the Secure Boot Centaur SCOM cache sensitive
register definitions

=cut
OpenPOWER on IntegriCloud