1## -----------------------------------------------------------------------
2##
3##   Copyright 2011 Gene Cumm
4##
5##   This program is free software; you can redistribute it and/or modify
6##   it under the terms of the GNU General Public License as published by
7##   the Free Software Foundation, Inc., 53 Temple Place Ste 330,
8##   Boston MA 02111-1307, USA; either version 2 of the License, or
9##   (at your option) any later version; incorporated herein by reference.
10##
11## -----------------------------------------------------------------------
12
13##
14## mk-lba-img.pl
15##
16## Make an image where each sector contains the LBA of the sector with
17## a head of an input file.
18##
19
20# use bytes;
21
22use constant SECTOR_SIZE => 512;
23use constant LBA_SIZE => 8;
24use constant LONG_SIZE => 4;
25use constant NUM_SECTORS => (256*63+1);
26# use constant NUM_SECTORS => 5;
27use constant DEBUG => 1;
28
29# sub dprint
30# {
31#     if (DEBUG) {
32# 	print($_);
33#     }
34# }
35
36($ifilen, $ofilen) = @ARGV;
37
38if ((!defined($ifilen)) || ($ifilen eq "-")) {	#
39    print(STDERR "Using stdin\n");
40    $IFILE = STDIN;
41} else {
42    open($IFILE, '<', $ifilen) or die "open:$!";
43    print(STDERR "Using $ifilen\n");
44}
45
46binmode($ifile);
47
48if (!defined($ofilen)) {
49    $OFILE = STDOUT;
50} else {
51    open($OFILE, '>', $ofilen) or die "open:$!";
52    print(STDERR "Using $ofilen\n");
53}
54
55binmode($OFILE);
56
57# $pk0 = pack('L', 0);
58$n_long = (SECTOR_SIZE/LONG_SIZE);
59$n_lba = (SECTOR_SIZE/LBA_SIZE);
60
61$len=0;
62while ( read($IFILE, $ch, 1) ) {
63    print($OFILE $ch);
64    $len++;
65}
66$tail = (SECTOR_SIZE - ($len % SECTOR_SIZE)) % SECTOR_SIZE;
67$ch = pack("C", 0);
68print("Len: $len\ttail: $tail\n");
69for ($i=0; $i<$tail; $i++) {
70    print($OFILE $ch);
71}
72
73$st = ($len + $tail) / SECTOR_SIZE;
74
75for ($i=$st; $i<(NUM_SECTORS); $i++) {
76    @ia = ();
77    for ($j=0; $j< $n_lba; $j++) {
78	push(@ia, $i, 0);
79    }
80    @ipk = pack("L[$n_long]", @ia);
81	# There is a 64-bit INT conversion but it normally isn't usable
82	# on a 32-bit platform
83    print($OFILE @ipk);	# Gently simulate a 64-bit LBA
84}
85
86if (defined($ifilen) && (!($ifilen eq "-"))) {
87    close($IFILE);
88}
89
90if (defined($ofilen)) {
91    close($OFILE);
92}
93
94exit 0;
95