Perl
Practical Extraction and Report Language
Perl
指语言本身 perl
指程序运行的解释器
#!/usr/bin/env perl
#################################################################################
#
# The confidential and proprietary information contained in this file may
# only be used by a person authorised under and to the extent permitted
# by a subsisting licensing agreement from ARM Limited.
#
# (C) COPYRIGHT 2000-2011 ARM Limited.
# ALL RIGHTS RESERVED
#
# This entire notice must be reproduced on all copies of this file
# and copies of this file may only be made by a person if such person is
# permitted to do so under the terms of a subsisting license agreement
# from ARM Limited.
#
# Bin2Hex
# =======
#
################################################################################
$^W=1; # enable warnings (same as perl -w)
# ensure we are running at least version 5.00502
require 5.00502;
use File::Basename;
my $bigend = (exists $ENV{'VAL_BIGENDIAN'});
my $image36 = 1;
# check if you should directly convert a single binary to stdout
if (not -e "armcode.script") {
$image36 = 0;
BinToHex($ARGV[0], "-", $bigend);
exit 0;
}
$bigend = (defined $ARGV[0] and $ARGV[0] =~ /-bigend/);
BuildHexImages($bigend);
exit 0;
# --------------------------------------------------------------------------------
sub diemsg {
print "$0: (fatal) @_\n";
exit 1;
}
sub marine {
print "Hello, sailor number!\n";
print "Aloha!\n";
}
# --------------------------------------------------------------------------------
# INDEX:BuildHexImages subroutine:
#
# --------------------------------------------------------------------------------
sub BuildHexImages {
my $bigend = $_[0];
my $bin;
my $binhex;
my $address;
open (ARMCODE, "armcode.script") or diemsg("Could not open armcode.script");
open (ARMCODEHEX, ">armcode.hex") or diemsg("Could not open armcode.hex");
open (IMAGE36HEX, ">image36.hex") or diemsg("Could not open image36.hex");
while (<ARMCODE>) {
($bin, $address) = split;
diemsg("malformed armcode.script") if (!defined $bin or !defined $address);
# derive hex binary filename from binary file
$binhex = "bin/" . basename($bin) . ".hex";
# construct armcode.hex entry
print ARMCODEHEX "$binhex $address\n";
# write out starting address for image36 file
printf IMAGE36HEX "1%.16x\n", hex($address);
BinToHex("$bin", "$binhex", ($bigend && !($bin =~ /rispage/)));
}
# write out end delimiter
print IMAGE36HEX "200000000\n";
close IMAGE36HEX;
close ARMCODEHEX;
close ARMCODE;
}
# --------------------------------------------------------------------------------
# INDEX:subroutine: BinToHex
#
# This routine converts a binary file to hex text file
#
# BigEndian juggling has been removed, as it is believed to corrupt the image
# - support has been moved into testbench
#
# Big endian juggling reinstated at the request of Andy Rose - it is apparently
# necessary for endian switchable pure verilog memory models.
#
# Big endian has an impact on mmuris -- mmuris doesn't know the setting of the
# -bigend validation option. It always creates little endian binary files for
# it's random page tables.
# The output hex files need to always be little endian, which means that if -bigend
# was specified then the binaries need to be byteswapped before creating
# the hex files. But since the mmuris random page tables are always little
# endian, they don't need to be swapped ever, even if -bigend is specified.
#
# --------------------------------------------------------------------------------
sub BinToHex {
open(INFILE, " $_[0]") or diemsg("could not open $_[0]");
open(OUTFILE, ">$_[1]") or diemsg("could not open $_[1]");
my $bigend = $_[2];
my $word;
my $apple;
my $low;
my $high;
$n = 0;
if ($bigend) {
# read in the binary file in word chunks
while(read INFILE, $word, 4) {
# write out each word to image36 file
printf IMAGE36HEX "0%.8x\n", unpack("N*", $word) if ($image36);
# unpack each word 'string' into a 'long' big-endian and print it as hex
printf OUTFILE "%.8x\n", unpack("N*", $word);
}
}
else {
# read in the binary file in word chunks
while(read INFILE, $word, 4) {
# write out each word to image36 file
#printf IMAGE36HEX "0%.8x\n", unpack("V*", $word) if ($image36);
# unpack each word 'string' into a 'long' little-endian and print it as hex
#printf OUTFILE "%.16x\n", unpack("V*", $word);
#print $word;
$m = $n % 2;
if ($m)
{
$high = unpack("V*", $word);
$apple = sprintf("%.8x%.8x", $high, $low);
printf OUTFILE "%s\n", $apple;
}
else
{
$low = unpack("V*", $word);
}
$n += 1;
}
#last 8 bytes alain to 16bytes
$m = $n % 2;
if ($m)
{
$high = 0;
$apple = sprintf("%.8x%.8x", $high, $low);
printf OUTFILE "%s\n", $apple;
}
}
close(INFILE);
close(OUTFILE);
}