summaryrefslogtreecommitdiffstats
path: root/src/build/buildpnor/buildSbePart.pl
blob: 11269bcfe807db48a078d7028d7de86636fb3b3b (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
#!/usr/bin/perl
# IBM_PROLOG_BEGIN_TAG
# This is an automatically generated prolog.
#
# $Source: src/build/buildpnor/buildSbePart.pl $
#
# IBM CONFIDENTIAL
#
# COPYRIGHT International Business Machines Corp. 2013
#
# 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 otherwise
# divested of its trade secrets, irrespective of what has been
# deposited with the U.S. Copyright Office.
#
# Origin: 30
#
# IBM_PROLOG_END_TAG

#Builds an SBE partition for PNOR based on user-provided SBE images
#It works for both Processor SBE-IPL images and Centaur SBE images

use strict;
use File::Basename;
use File::Temp qw/ tempfile tempdir /;

my $TRAC_ERR = 0;
# 0=errors, >0 for more traces, leaving at 1 to keep key milestone traces.
my $g_trace = 1;

my $progName = File::Basename::basename $0;
my $outBin = "";
my %ecImgs;
my $injectVerHdrs = undef;
my $tocVersion = 1;  #Only version 1 currently defined

if ($#ARGV < 0) {
    usage();
    exit;
}


#Parse the commandline args
for (my $i=0; $i < $#ARGV + 1; $i++)
{
    if ($ARGV[$i] =~ /-h/) {
        usage();
        exit 0;
    }
    elsif($ARGV[$i] =~ /--injectVersionHeaders/) {
        $injectVerHdrs = 1;
        trace(2, "injectVersionHeaders flag specified");
    }
    elsif($ARGV[$i] =~ /--sbeOutBin/) {
        $outBin = $ARGV[++$i];
        trace(2, "SBE output Binary File=$outBin");
    }
    elsif($ARGV[$i] =~ /--ecImg/) {
        my $argName = $ARGV[$i];
        my $argVal = $ARGV[++$i];
        saveInputFile("--ecImg", $argName, $argVal, \%ecImgs);
    }
    else {
        traceErr("Unrecognized Input: $ARGV[$i]");
        exit 1;
    }
}


#Verify all the SBE images exist
my $rc = verifyFilesExist(\%ecImgs);
if($rc != 0)
{
    trace(0, "$progName: Error detected from call to verifyFilesExist().  Exiting");
    exit 1;
}

#Generate the output image
my $rc = genOutputImage($injectVerHdrs, $tocVersion, $outBin, \%ecImgs);
if($rc != 0)
{
    trace(0, "$progName: Error detected from call to genOutputImage().  Exiting");
    exit 1;
}


################################################################################
# saveInputFile - save inputfile name into ecImgss array
################################################################################
sub saveInputFile
{
    my ($i_argPrefix, $i_argName, $i_argVal, $i_ecImgs) = @_;
    my $this_func = (caller(0))[3];

    #$i_argName will be something like --ecIMG_10
    #This substr command should return just the 10, which is the image EC Level
    my $ecLevel = substr($i_argName,
                         length($i_argPrefix)+1,
                         length($i_argName)-length($i_argPrefix));

    trace(10, "$this_func: $ecLevel=$i_argVal");

    $$i_ecImgs{$ecLevel} = $i_argVal;

    trace(10, "$this_func:           $$i_ecImgs{$ecLevel}");

    #no return code expected
}

################################################################################
# verifyFilesExist - Verify all the input files exist
################################################################################
sub verifyFilesExist
{
    my ($i_ecImgs) = @_;
    my $this_func = (caller(0))[3];
    my $key;
    my $rc = 0;

    for $key ( keys %$i_ecImgs)
    {
        unless(-e $$i_ecImgs{$key})
        {
            my $inputFile = $$i_ecImgs{$key};
            trace(0, "$this_func: Specified input file ($inputFile) for key ($key) does not exist.  Aborting!");
            $rc = 1;
            last;
        }
        else
        {
            trace(10, "$this_func: $$i_ecImgs{$key} exists");
        }
    }

    return $rc;
}

################################################################################
# genOutputImage - Generate output image
#          -Build SBE TOC in first 128 bytes
#          -Insert 4K header in front of each image with SHA512 as version
#          -Insert each SBE image after corresponding header
################################################################################
#Output image layout (Defined in Hostboot SBE Layout document)
#word 0: 'SBE\0' eyecatcher
#word 1: SBE TOC Layout version - currently only 1 is defined
#word 2: SBE image EC
#word 3: Sbe image offset in partition
#word 4: SBE image size
#Repeat words 2-4 for each supported EC

#Actual SBE Images start at offset 0x1000 and must always be on a 4k boundary.
################################################################################
sub genOutputImage
{
    my ($i_injectVerHdrs, $i_tocVersion, $i_outBin, $i_ecImgs) = @_;
    my $this_func = (caller(0))[3];
    my $key;
    my %ecOffsets;
    my $rc = 0;
    my $FILEHANDLE;
    my $curOffset = 0x1000;  #first offset is at 4k
    trace(4, "$this_func: >>Enter");

#open output file
    open( $FILEHANDLE, ">:raw", $i_outBin)
      or die "Can't open $i_outBin file for writing";

    #Build the TOC
    #WORD 0: EyeCatcher - "SBE\0" in ASCII
    my @charArray = split //, 'SBE';
    my $curChar;
    foreach $curChar (@charArray)
    {
        print $FILEHANDLE pack('C', ord($curChar));
    }

    #Pad byte for null character after SBE
    insertPadBytes($FILEHANDLE, 1);

     #WORD 1: Version = 0x00000001
    print $FILEHANDLE pack('N', 1);

    #Insert header data for each EC provided
    for $key ( keys %{$i_ecImgs})
    {
        trace(2, "$this_func: Inserting header for EC=$key");
        my $filesize = -s $$i_ecImgs{$key};

        if($i_injectVerHdrs)
        {
            $filesize += 0x1000;
        }

        #EC Word
        print $FILEHANDLE pack('N', hex($key));

        #Offset Word
        print $FILEHANDLE pack('N', $curOffset);

        #Size Word
        print $FILEHANDLE pack('N', $filesize);

        #safe offset for inserting images
        $ecOffsets{$key} = $curOffset;

        #generate next to 4k offset
        $curOffset += $filesize;
        if (($curOffset & 0x00000FFF) != 0)
        {
            $curOffset = $curOffset & 0xFFFFF000;
            $curOffset += 0x1000;
        }
    }

    close $FILEHANDLE;

    #Insert actual image for each EC provided
    for $key ( keys %{$i_ecImgs})
    {
        trace(2, "$this_func: Inserting data for EC=$key, offset=$ecOffsets{$key}");

        my $seekOffset = $ecOffsets{$key};
        my $inFile = $$i_ecImgs{$key};

        #Image is prefixed with 4k header containing version
        if($i_injectVerHdrs)
        {
            my $headerOffset = $seekOffset;
            $seekOffset += 0x1000;

            my $headerFh;
            my $headerFile;
            ($headerFh, $headerFile) = tempfile(UNLINK => 1);
            #Disable file handle buffering

            trace(0, "$this_func: headerFile=$headerFile");


            #Insert 'VERSION\0' Eyecatcher
            my @eyeCatchArray = split //, 'VERSION';
            foreach $curChar (@eyeCatchArray)
            {
                print $headerFh pack('C', ord($curChar));
            }
            #Pad byte for null character after VERSION
            insertPadBytes($headerFh, 1);

            #make sure date written to file handle is flushed to disk
            close $headerFh;

            #Create the SHA512 hash
            my $cmd = "sha512sum $inFile \| awk \'\{print $1\}\' \| xxd -ps -r \>> $headerFile";
            system( $cmd ) == 0 or die "Creating $headerFile failed!";

            #Write to output file
            my $hdrdd = "dd if=$headerFile of=$i_outBin bs=1 seek=$headerOffset";
            system ( $hdrdd ) == 0 or die "Couldn't Write $headerFile to $i_outBin!";
        }
        my $ddCmd = "dd if=$inFile of=$i_outBin bs=1 seek=$seekOffset";
        system ( $ddCmd ) == 0 or die "Couldn't Write $inFile to $i_outBin!";

    }

    trace(4, "$this_func: <<Exit");

    return $rc;
}

#################################################
# Insert specifed number of pad bytes into file
#
#################################################
sub insertPadBytes
{
    my ($i_FILEHANDLE, $i_padBytes) = @_;
    my $i;
    print $i_FILEHANDLE pack("C[$i_padBytes]", map { 0 } 1..$i_padBytes);

}



################################################################################
# trace
################################################################################
sub traceErr
{
    my $i_string = shift;
    trace($TRAC_ERR, $i_string);
}

################################################################################
# trace
################################################################################
sub trace
{
    my $i_traceLevel;
    my $i_string;

    ($i_traceLevel, $i_string) = @_;

    #traceLevel 0 is for errors
    if($i_traceLevel == 0)
    {
        print "ERROR: ".$i_string."\n";
    }
    elsif ($g_trace >= $i_traceLevel)
    {
        print "TRACE: ".$i_string."\n";
    }
}



################################################################################
# print usage instructions
################################################################################
sub usage
{
print <<"ENDUSAGE";
  $progName = Creates SBE partition with SBE TOC based on input data.

  Usage:
    $progName --sbeOutBin  <complete SBE Partition image>
                 [--ecImg_10 <EC 10 SBE image>] [--ecImg_20 <EC_20_SBE_image>]
                 [--injectVersionHeaders]

  Parms:
    -h                  Print this help text
    --sbeOutBin <file> Name of output file for PNOR Binary
    --ecImg_<EC> <file>  This is a special paramater.  It is used to specify
                        input files to use for populating the partition with
                        supported SBE EC specific images
                            Examples:  --binFile_10 s1_10.sbe_seeprom.bin
                                       --binFile_13 s1_13.sbe_seeprom.bin
                                       --binFile_20 s1_20.sbe_seeprom.bin
    --injectVersionHeaders Injects a 4k header at the top of each image
                           containing a SHA512 Hash computed over the image.


ENDUSAGE
}
OpenPOWER on IntegriCloud