From a53e82a1e8b7efdaff172087101c2eec0d547333 Mon Sep 17 00:00:00 2001 From: Elizabeth Liner Date: Thu, 3 Aug 2017 17:44:15 -0500 Subject: Adding script to generate Memd header and binary file Change-Id: Ia16626a5a3370256bf7f1af0cec3f01545c93590 RTC:175158 Reviewed-on: http://ralgit01.raleigh.ibm.com/gerrit1/44196 Tested-by: Jenkins Server Tested-by: Jenkins OP Build CI Tested-by: Jenkins OP HW Tested-by: FSP CI Jenkins Reviewed-by: Martin Gloff Reviewed-by: Stephen M. Cprek Reviewed-by: Daniel M. Crowell --- src/build/buildpnor/memd_creation.pl | 176 +++++++++++++++++++++++++++++++++++ 1 file changed, 176 insertions(+) create mode 100755 src/build/buildpnor/memd_creation.pl (limited to 'src/build/buildpnor') diff --git a/src/build/buildpnor/memd_creation.pl b/src/build/buildpnor/memd_creation.pl new file mode 100755 index 000000000..aa0298988 --- /dev/null +++ b/src/build/buildpnor/memd_creation.pl @@ -0,0 +1,176 @@ +#!/usr/bin/perl +# IBM_PROLOG_BEGIN_TAG +# This is an automatically generated prolog. +# +# $Source: src/build/buildpnor/memd_creation.pl $ +# +# OpenPOWER HostBoot Project +# +# Contributors Listed Below - COPYRIGHT 2017 +# [+] 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 Pod::Usage; +use Getopt::Long qw(:config pass_through); +use strict; +use File::Basename; + +### CONSTANTS ### +use constant ROUNDING_DIVISOR => 1000; +use constant HEADER_INPUT_LOCATION => 12; + +print "Creating MEMD binary...\n"; + +# Header format: +# uint32_t eyecatch /* Eyecatch to determine validity "OKOK" */ +# uint32_t header_version /* What version of the header this is in */ +# uint32_t memd_version /* What version of the MEMD this includes */ +# uint32_t expected_size /* Size in megabytes of the biggest MEMD section */ +# uint16_t expected_num /* Number of MEMD instances in this section */ + +my $memd_dir = ""; +my $memd_output = ""; +my $help = 0; +my $man = 0; + +GetOptions( + "memd_dir=s" => \$memd_dir, + "memd_output=s" => \$memd_output, + "help" => \$help, + "man" => \$man) || pod2usage(-verbose=>0); + +pod2usage(-verbose => 1) if $help; +pod2usage(-verbose => 2) if $man; + +# Find files +my @memd_files = glob($memd_dir . '/*'); + +# Get the max file size - offset for each file binary +my $max_file_size = 0; +foreach my $file (@memd_files) +{ + my $cur_size = (stat $file)[7]; + if($cur_size > $max_file_size) + { + $max_file_size = $cur_size; + } +} +$max_file_size /= ROUNDING_DIVISOR; +$max_file_size = int($max_file_size) + 1; + +# Get the number of files - (expected_num) +my $number_of_files = scalar(@memd_files); + +# Generate header - add to file +my $header = "OKOK"; # shows that the memd partition is valid +$header .= "01.0"; # current metadata header version +$header .= "01.0"; # current memd version +$header .= "0000"; # expected size of each memd blob (in KB's) placeholder +$header .= "00"; # number of memd instances placeholder +$header .= "00000000"; # padding for the future +$header .= "000000"; # rounding up to 16 bytes + +# Create offset +my $offset = length $header; + +# Create file +open(my $fh, '>', $memd_output) or die "Could not open file '$memd_output' $!"; +print $fh $header; + +# Add in the actual size and the number of memd instances +seek($fh, HEADER_INPUT_LOCATION, 0); +my $size_bin = pack('N', $max_file_size); +print $fh $size_bin; +my $num_bin = pack('n', $number_of_files); +print $fh $num_bin; + +# Every max file size - rounded to an even number + headersize. +# Read in the MEMD binary, and concatenate to this file. +foreach my $file (@memd_files) +{ + seek($fh, $offset, 0); + print "Writing the file $file...\n"; + open(my $in_file, '<', $file) or die "Cound not open file '$file' $!"; + + while ( <$in_file> ) + { + print $fh $_; + } + close $in_file; + $offset = $offset + ($max_file_size * ROUNDING_DIVISOR); +} + + +close $fh; +print "Done. Memd binary is $memd_output.\n"; + +############### HELPER FUNCTIONS ############################ +# Function to first print, and then run a system command. Erroring out if the +# command does not complete successfully +sub run_command { + my $command = shift; + print "$command\n"; + my $rc = system($command); + if ($rc != 0){ + die "Error running command: $command. Nonzero return code of ($rc) returned.\n"; + } + return $rc; +} + +# Function to remove leading and trailing whitespace before returning that string +sub trim_string { + my $str = shift; + $str =~ s/^\s+//; + $str =~ s/\s+$//; + return $str; +} + +__END__ + +=head1 NAME + +memd_creation.pl + +=head1 SYNOPSIS + +memd_creation.pl + --memd_dir=DATA_DIRECTORY + --memd_output=DATA_OUTPUT_NAME + +=item B<--help> + +Prints a brief help message and exits + +=item B<--man> + +Prints the manual page and exists + +=item B<--memd_dir>=DIRECTORY + +Directory of MEMD binary files. + +=item B<--memd_output>=FILENAME + +Location and name of the resulting output binary. + +=head1 DESCRIPTION + +B will generate a binary image for the MEMD +section of the PNOR. It will generate a header based on the +input binary files and concatenate them together. + +=cut + -- cgit v1.2.1