File: main.c

package info (click to toggle)
genisovh 0.1-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny, sarge, squeeze, wheezy
  • size: 52 kB
  • ctags: 107
  • sloc: ansic: 255; makefile: 58
file content (222 lines) | stat: -rw-r--r-- 4,930 bytes parent folder | download
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
/*
 * Copyright: (C) 2002 by Florian Lohoff <flo@rfc822.org>
 *            (C) 2004 by Thiemo Seufer <seufer@csv.ica.uni-stuttgart.de>
 *
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, Version 2, as published by the
 * Free Software Foundation.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>

#include "dvh.h"

#define MIN(a,b) (((a) < (b)) ? (a) : (b))

#if 1
#define SECTORS_PER_TRACK	32
#define BYTES_PER_SECTOR	512
#else
#define SECTORS_PER_TRACK	8
#define BYTES_PER_SECTOR	2048
#endif

#define swab16(x) \
({ \
	uint16_t __x = (x); \
	((uint16_t)( \
		(((uint16_t)(__x) & (uint16_t)0x00ffU) << 8) | \
		(((uint16_t)(__x) & (uint16_t)0xff00U) >> 8) )); \
})

#define swab32(x) \
({ \
	uint32_t __x = (x); \
	((uint32_t)( \
		(((uint32_t)(__x) & (uint32_t)0x000000ffUL) << 24) | \
		(((uint32_t)(__x) & (uint32_t)0x0000ff00UL) <<  8) | \
		(((uint32_t)(__x) & (uint32_t)0x00ff0000UL) >>  8) | \
		(((uint32_t)(__x) & (uint32_t)0xff000000UL) >> 24) )); \
})

#if __BYTE_ORDER == __LITTLE_ENDIAN
#define be32_to_cpu(x) swab32((x))
#define be16_to_cpu(x) swab16((x))
#define cpu_to_be32(x) swab32((x))
#define cpu_to_be16(x) swab16((x))
#else
#define be32_to_cpu(x) ((uint32_t)(x))
#define be16_to_cpu(x) ((uint16_t)(x))
#define cpu_to_be32(x) ((uint32_t)(x))
#define cpu_to_be16(x) ((uint16_t)(x))
#endif


static void vh_calc_checksum(struct volume_header *vh)
{
	uint32_t newsum = 0;
	uint32_t *buffer = (uint32_t *)vh;
	unsigned int i;

	vh->vh_csum = 0;

	for(i = 0; i < sizeof(struct volume_header)/sizeof(uint32_t); i++)
		newsum -= be32_to_cpu(buffer[i]);

	vh->vh_csum = cpu_to_be32(newsum);
}

static void swap_dp_out(struct device_parameters *dp)
{
	cpu_to_be16(dp->dp_cyls);
	cpu_to_be16(dp->dp_shd0);
	cpu_to_be16(dp->dp_trks0);
	cpu_to_be16(dp->dp_unused);
	cpu_to_be16(dp->dp_secs);
	cpu_to_be16(dp->dp_secbytes);
	cpu_to_be16(dp->dp_interleave);
	cpu_to_be32(dp->dp_flags);
	cpu_to_be32(dp->dp_datarate);
	cpu_to_be32(dp->dp_nretries);
	cpu_to_be32(dp->dp_mspw);
	cpu_to_be32(dp->dp_xgap1);
	cpu_to_be32(dp->dp_xsync);
	cpu_to_be32(dp->dp_xrdly);
	cpu_to_be32(dp->dp_xgap2);
	cpu_to_be32(dp->dp_xrgate);
	cpu_to_be32(dp->dp_xwcont);
}

static void swap_vd_out(struct volume_directory *vd)
{
	cpu_to_be32(vd->vd_lbn);
	cpu_to_be32(vd->vd_nbytes);
}

static void swap_pt_out(struct partition_table *pt)
{
	cpu_to_be32(pt->pt_nblks);
	cpu_to_be32(pt->pt_firstlbn);
	cpu_to_be32(pt->pt_type);
}

static void swap_vh_out(struct volume_header *vh)
{
	cpu_to_be32(vh->vh_magic);
	cpu_to_be16(vh->vh_rootpt);
	cpu_to_be16(vh->vh_swappt);
	swap_dp_out(&vh->vh_dp);
	swap_vd_out(vh->vh_vd);
	swap_pt_out(vh->vh_pt);
	cpu_to_be32(vh->vh_csum);
	cpu_to_be32(vh->vh_fill);
}

static void usage(void)
{
	printf("Usage:\ngenisovh <isofile> [<name:sblock,size> ...]\n");
	exit(1);
}

int main(int argc, char **argv)
{
	int fd;
	int i;
	int v;
	int block;
	int size;
	char *isoname;
	char *pos;
	struct stat st;
	struct volume_header vh;

	if (argc < 2)
		usage();

	isoname = argv[1];

	if (!(fd = open(isoname, O_RDWR))) {
		printf("File not found %s\n", isoname);
		return 1;
	}

	if (fstat(fd, &st)) {
		printf("Failed to stat file %s\n", isoname);
		close(fd);
		return 1;
	}
	
	memset(&vh, 0, sizeof(vh));

	vh.vh_magic = VHMAGIC;

	/* Values from an IRIX cd */
	vh.vh_dp.dp_secbytes = BYTES_PER_SECTOR;
	vh.vh_dp.dp_secs = SECTORS_PER_TRACK;
	vh.vh_dp.dp_flags = DP_RESEEK|DP_IGNOREERRORS|DP_TRKFWD;
	vh.vh_dp.dp_trks0 = 1;

	vh.vh_dp.dp_cyls = (st.st_size + BYTES_PER_SECTOR - 1)
		/ (SECTORS_PER_TRACK * BYTES_PER_SECTOR);

	for(i = 2, v = 0; i < argc; i++, v++) {
		char *cline = argv[i];

		/* ip22:3838383,3333 */
		if (!(pos = strchr(cline, ':'))) {
			printf("Couldn't find : in parameter %s\n", cline);	
			usage();
		}

		/* copy name */
		strncpy(vh.vh_vd[v].vd_name, cline, MIN(VDNAMESIZE, pos - cline));

		cline = pos + 1;

		if (!(pos = strchr(cline, ','))) {
			printf("Couldn't find , in parameter %s\n", cline);
			usage();
		}

		block = atoi(cline);	
		vh.vh_vd[v].vd_lbn = block;

		cline = pos + 1;

		size = atoi(cline);	
		vh.vh_vd[v].vd_nbytes = size;
	}

	/* Create volume partition on whole cd iso */
	vh.vh_pt[10].pt_nblks = (st.st_size + (BYTES_PER_SECTOR - 1))
		/ BYTES_PER_SECTOR;
	vh.vh_pt[10].pt_firstlbn = 0;
	vh.vh_pt[10].pt_type = PTYPE_VOLUME;

	/* Create volume header partition, also on WHOLE cd iso */
	vh.vh_pt[8].pt_nblks = (st.st_size + (BYTES_PER_SECTOR - 1))
		/ BYTES_PER_SECTOR;
	vh.vh_pt[8].pt_firstlbn = 0;
	vh.vh_pt[8].pt_type = PTYPE_VOLHDR;

	swap_vh_out(&vh);

	/* Create checksum */
	vh_calc_checksum(&vh);

	if (write(fd, &vh, sizeof(vh)) != sizeof(vh)) {
		printf("Failed to write volume header to iso image");
		close(fd);
		return 1;
	}

	close(fd);
	return 0;
}