summaryrefslogtreecommitdiff
path: root/diag/geodsp/mk-lba-img.c
blob: eb1c339320ab8265457211dc3a74524963df9b65 (plain)
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
/* ----------------------------------------------------------------------- *
 *
 *   Copyright 2010 Gene Cumm
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, Inc., 53 Temple Place Ste 330,
 *   Boston MA 02111-1307, USA; either version 2 of the License, or
 *   (at your option) any later version; incorporated herein by reference.
 *
 * ----------------------------------------------------------------------- */

/*
 * mk-lba-img.c
 *
 * Makes an image that contains the LBA in every *word of every sector
 */

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>

#define NUM_SECT (256*63+1)
#define BPS (512)
#define SECT_INT (BPS / sizeof(unsigned int))

typedef unsigned char uint8_t;
typedef unsigned int uint32_t;

const char DEF_FN[] = "-";

int main(int argc, char *argv[])
{
	int i, rv = 0, one = 0;
	unsigned int lba, b[SECT_INT];
	int len;
	FILE *f;
	uint8_t tt = 0;
	const char *fn;

	if (argc >= 2) {
		if (argc >= 3) {
			if (strcasecmp("-1", argv[1]) == 0) {
				fn = argv[2];
				one = 1;
			} else {
				fn = argv[1];
			}
		} else {
			fn = argv[1];
		}
	} else {
		fn = DEF_FN;
	}

	if (!strcmp(fn, "-"))
		f = stdout;
	else
		f = fopen(fn, "w");

	if (!f) {
		fprintf(stderr, "%s: %s: unable to open for writing: %s\n",
			argv[0], fn, strerror(errno));
		return 1;
	}

	lba = 0;
	while ((len = fread(b, 1, BPS, stdin))) {
		if (len < BPS)
			memset((char *)b + len, 0, BPS - len);
		fwrite(b, 1, BPS, f);
		lba++;
	}

	memset(b, 0, sizeof b);

	while (lba < NUM_SECT) {
		if (one) {
			b[0] = lba;
		} else {
			for (i = 0; i < SECT_INT; i++)
				b[i] = lba;
		}
		fwrite(b, 1, BPS, f);
		lba++;
	}

	if (f != stdout)
		fclose(f);

	return rv;
}