File: Nbi.pm

package info (click to toggle)
mknbi 1.4.4-1.1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 828 kB
  • ctags: 1,383
  • sloc: ansic: 3,511; asm: 2,374; perl: 1,368; makefile: 249; sh: 74; pascal: 37
file content (226 lines) | stat: -rw-r--r-- 5,911 bytes parent folder | download | duplicates (7)
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
# Class to handle tagged images
# Placed under GNU Public License by Ken Yap, April 2000

package Nbi;

use strict;
use IO::Seekable;

use constant;
use constant TFTPBLOCKSIZE => 512;
# This is correct for the current version of the netboot specs
# Note: reverse of the way it is in the specs because of Intel byte order
use constant MAGIC => "\x36\x13\x03\x1B";
# This is needed at the end of the boot block, again byte reversed
use constant MAGIC2 => "\x55\xAA";
# This is defined by the bootrom layout
use constant HEADERSIZE => 512;

use vars qw($libdir $bootseg $bootoff @segdescs);

sub new {
	my $class = shift;
	$libdir = shift;
	my $self = {};
	bless $self, $class;
#	$self->_initialize();
	return $self;
}

sub add_header ($$$$$)
{
	my ($class, $vendorinfo, $headerseg, $bootseg, $bootoff) = @_;
	my ($vilen);

	$vilen = length($vendorinfo);
	$vilen += 4;		# three plus one for null byte
	$vilen &= ~0x3;	# round to multiple of 4
	push(@segdescs, pack("A4V3a$vilen",
		MAGIC,
		($vilen << 2) + 4,
		$headerseg << 16,
		($bootseg << 16) + $bootoff,
		$vendorinfo));
}

sub add_pm_header ($$$$$)
{
	my ($class, $vendorinfo, $headerseg, $bootaddr, $progreturns) = @_;
	my ($vilen);

	$vilen = length($vendorinfo);
	$vilen += 4;		# three plus one for null byte
	$vilen &= ~0x3;	# round to multiple of 4
	push(@segdescs, pack("A4V3a$vilen",
		MAGIC,
		(($vilen << 2) + 4) | (1 << 31) | ($progreturns << 8),
		$headerseg << 16,
		$bootaddr,
		$vendorinfo));
}

sub roundup ($$)
{
# Round up to next multiple of $blocksize, assumes that it's a power of 2
	my ($size, $blocksize) = @_;

	# Default to TFTPBLOCKSIZE if not specified
	$blocksize = TFTPBLOCKSIZE if (!defined($blocksize));
	return ($size + $blocksize - 1) & ~($blocksize - 1);
}

# Grab N bytes from a file
sub peek_file ($$$$)
{
	my ($class, $descriptor, $dataptr, $datalen) = @_;
	my ($file, $fromoff, $status);

	$file = $$descriptor{'file'} if exists $$descriptor{'file'};
	$fromoff = $$descriptor{'fromoff'} if exists $$descriptor{'fromoff'};
	return 0 if !defined($file) or !open(R, "$file");
	binmode(R);
	if (defined($fromoff)) {
		return 0 if !seek(R, $fromoff, SEEK_SET);
	}
	# Read up to $datalen bytes
	$status = read(R, $$dataptr, $datalen);
	close(R);
	return ($status);
}

# Add a segment descriptor from a file or a string
sub add_segment ($$$)
{
	my ($class, $descriptor, $vendorinfo) = @_;
	my ($file, $string, $segment, $len, $maxlen, $fromoff, $align,
		$id, $end, $vilen);

	$end = 0;
	$file = $$descriptor{'file'} if exists $$descriptor{'file'};
	$string = $$descriptor{'string'} if exists $$descriptor{'string'};
	$segment = $$descriptor{'segment'} if exists $$descriptor{'segment'};
	$len = $$descriptor{'len'} if exists $$descriptor{'len'};
	$maxlen = $$descriptor{'maxlen'} if exists $$descriptor{'maxlen'};
	$fromoff = $$descriptor{'fromoff'} if exists $$descriptor{'fromoff'};
	$align = $$descriptor{'align'} if exists $$descriptor{'align'};
	$id = $$descriptor{'id'} if exists $$descriptor{'id'};
	$end = $$descriptor{'end'} if exists $$descriptor{'end'};
	if (!defined($len)) {
		if (defined($string)) {
			$len = length($string);
		} else {
			if (defined($fromoff)) {
				$len = (-s $file) - $fromoff;
			} else {
				$len = -s $file;
			}
			return 0 if !defined($len);		# no such file
		}
	}
	if (defined($align)) {
		$len = &roundup($len, $align);
	} else {
		$len = &roundup($len);
	}
	$maxlen = $len if (!defined($maxlen));
	if (!defined($vendorinfo)) {
		push(@segdescs, pack('V4',
			4 + ($id << 8) + ($end << 26),
			$segment << 4,
			$len,
			$maxlen));
	} else {
		$vilen = length($vendorinfo);
		$vilen += 3;           # three plus one for null byte
		$vilen &= ~0x3;        # round to multiple of 4
		push(@segdescs, pack("V4a$vilen",
			($vilen << 2) + 4 + ($id << 8) + ($end << 26),
			$segment << 4,
			$len,
			$maxlen,
			$vendorinfo));
	}
	return ($len);			# assumes always > 0
}

sub pad_with_nulls ($$)
{
	my ($i, $blocksize) = @_;

	$blocksize = TFTPBLOCKSIZE if (!defined($blocksize));
	# Pad with nulls to next block boundary
	$i %= $blocksize;
	print "\0" x ($blocksize - $i) if ($i != 0);
}

# Copy data from file to stdout
sub copy_file ($$)
{
	my ($class, $descriptor) = @_;
	my ($i, $file, $fromoff, $align, $len, $seglen, $nread, $data, $status);

	$file = $$descriptor{'file'} if exists $$descriptor{'file'};
	$fromoff = $$descriptor{'fromoff'} if exists $$descriptor{'fromoff'};
	$align = $$descriptor{'align'} if exists $$descriptor{'align'};
	$len = $$descriptor{'len'} if exists $$descriptor{'len'};
	return 0 if !open(R, "$file");
	if (defined($fromoff)) {
		return 0 if !seek(R, $fromoff, SEEK_SET);
		$len = (-s $file) - $fromoff if !defined($len);
	} else {
		$len = -s $file if !defined($len);
	}
	binmode(R);
	# Copy file in TFTPBLOCKSIZE chunks
	$nread = 0;
	while ($nread != $len) {
		$status = read(R, $data, TFTPBLOCKSIZE);
		last if (!defined($status) or $status == 0);
		print $data;
		$nread += $status;
	}
	close(R);
	if (defined($align)) {
		&pad_with_nulls($nread, $align);
	} else {
		&pad_with_nulls($nread);
	}
	return ($nread);
}

# Copy data from string to stdout
sub copy_string ($$)
{
	my ($class, $descriptor) = @_;
	my ($i, $string, $len, $align);

	$string = $$descriptor{'string'} if exists $$descriptor{'string'};
	$len = $$descriptor{'len'} if exists $$descriptor{'len'};
	$align = $$descriptor{'align'} if exists $$descriptor{'align'};
	return 0 if !defined($string);
	$len = length($string) if !defined($len);
	print substr($string, 0, $len);
	defined($align) ? &pad_with_nulls($len, $align) : &pad_with_nulls($len);
	return ($len);
}

sub dump_segments {
	my ($s, $len);

	$len = 0;
	while ($s = shift(@segdescs)) {
		$len += length($s);
		print $s;
	}
	print "\0" x (HEADERSIZE - 2 - $len), MAGIC2;
}

# This empty for now, but is available as a hook to do any actions
# before closing the image file

sub finalise_image {
}

@segdescs = ();

1;