One of the pieces I have implemented in 5.1.x is the ability to read
the ELF note section, and read various interesting pieces of
information, most of which are not yet defined.
The most useful piece I have built so far is the ability to verify
a checksum on the entire loaded image. At the moment I am using
the IP checksum because that is a moderately strong checksum, and
every bootrom need to implement it for other reasons as well.
Ken has all of the utilities in perl, and I have a one as well that
prepare network bootable images. And for the longest time I could
not figure out how to perl to do a reasonably fast job of computing
the ip checksum. Reasonable as in under a minute. The attached
code accomplishes that.
Does anyone have any objects to included this checksum ability
into mknbi? Or ideas on how to speed of the ckecksum computation,
even more?
Eric
sub compute_ip_checksum
{
my ($str) = @_;
my ($checksum, $i, $size, $shorts);
$checksum = 0;
$size = length($str);
$shorts = $size >> 1;
# Perl has a fairly large loop overhead so a straight forward
# implementation of the ip checksum is intolerably slow.
# Instead we use the unpack checksum computation function,
# and sum 16bit little endian words into a 32bit number, on at
# most 64K of data at a time. This ensures we do not overflow
# the 32bit sum allowing carry wrap around to be implemented by
# hand.
for($i = 0; $i < $shorts; $i += 32768) {
$checksum += unpack("%32v32768", substr($str, $i <<1, 65536));
while($checksum > 0xffff) {
$checksum = ($checksum & 0xffff) + ($checksum >> 16);
}
}
if ($size & 1) {
$checksum += unpack('C', substr($str, -1, 1));
while($checksum > 0xffff) {
$checksum = ($checksum & 0xffff) + ($checksum >> 16);
}
}
$checksum = (~$checksum) & 0xFFFF;
return $checksum;
}
|