1## -----------------------------------------------------------------------
2##
3##   Copyright 2007-2009 H. Peter Anvin - All Rights Reserved
4##   Copyright 2009 Intel Corporation; author: H. Peter Anvin
5##
6##   This program is free software; you can redistribute it and/or modify
7##   it under the terms of the GNU General Public License as published by
8##   the Free Software Foundation, Inc., 53 Temple Place Ste 330,
9##   Boston MA 02111-1307, USA; either version 2 of the License, or
10##   (at your option) any later version; incorporated herein by reference.
11##
12## -----------------------------------------------------------------------
13
14##
15## checksize.pl
16##
17## Check the size of a binary file and pad it with zeroes to that size
18##
19
20use bytes;
21
22($file, $maxsize, $padsize) = @ARGV;
23
24if (!defined($maxsize)) {
25    # Defaults based on the filename
26    if ($file =~ /^mbr[^0-9a-z]/) {
27	$maxsize = $padsize = 440;
28    } elsif ($file =~ /^gptmbr[^0-9a-z]/) {
29	$maxsize = $padsize = 440;
30    } elsif ($file =~ /^isohdp[fp]x[^0-9a-z]/) {
31	$maxsize = $padsize = 432;
32    } elsif ($file =~ /^altmbr[^0-9a-z]/) {
33	$maxsize = $padsize = 439;
34    } else {
35	die "$0: no default size for filename: $file\n";
36    }
37}
38
39$padsize = $maxsize unless(defined($padsize));
40
41open(FILE, '+<', $file) or die;
42@st = stat(FILE);
43if (!defined($size = $st[7])) {
44    die "$0: $file: $!\n";
45}
46if ($size > $maxsize) {
47    print STDERR "$file: too big ($size > $maxsize)\n";
48    exit 1;
49} elsif ($size < $padsize) {
50    seek(FILE, $size, 0);
51    print FILE "\0" x ($padsize-$size);
52}
53
54exit 0;
55