1 /* ----------------------------------------------------------------------- *
2  *
3  *   Copyright 2010 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.c
15  *
16  * Makes an image that contains the LBA in every *word of every sector
17  */
18 
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <errno.h>
22 #include <string.h>
23 #include <unistd.h>
24 
25 #define NUM_SECT (256*63+1)
26 #define BPS (512)
27 #define SECT_INT (BPS / sizeof(unsigned int))
28 
29 typedef unsigned char uint8_t;
30 typedef unsigned int uint32_t;
31 
32 const char DEF_FN[] = "-";
33 
main(int argc,char * argv[])34 int main(int argc, char *argv[])
35 {
36 	int i, rv = 0, one = 0;
37 	unsigned int lba, b[SECT_INT];
38 	int len;
39 	FILE *f;
40 	uint8_t tt = 0;
41 	const char *fn;
42 
43 	if (argc >= 2) {
44 		if (argc >= 3) {
45 			if (strcasecmp("-1", argv[1]) == 0) {
46 				fn = argv[2];
47 				one = 1;
48 			} else {
49 				fn = argv[1];
50 			}
51 		} else {
52 			fn = argv[1];
53 		}
54 	} else {
55 		fn = DEF_FN;
56 	}
57 
58 	if (!strcmp(fn, "-"))
59 		f = stdout;
60 	else
61 		f = fopen(fn, "w");
62 
63 	if (!f) {
64 		fprintf(stderr, "%s: %s: unable to open for writing: %s\n",
65 			argv[0], fn, strerror(errno));
66 		return 1;
67 	}
68 
69 	lba = 0;
70 	while ((len = fread(b, 1, BPS, stdin))) {
71 		if (len < BPS)
72 			memset((char *)b + len, 0, BPS - len);
73 		fwrite(b, 1, BPS, f);
74 		lba++;
75 	}
76 
77 	memset(b, 0, sizeof b);
78 
79 	while (lba < NUM_SECT) {
80 		if (one) {
81 			b[0] = lba;
82 		} else {
83 			for (i = 0; i < SECT_INT; i++)
84 				b[i] = lba;
85 		}
86 		fwrite(b, 1, BPS, f);
87 		lba++;
88 	}
89 
90 	if (f != stdout)
91 		fclose(f);
92 
93 	return rv;
94 }
95