File: faum-cd-to-iso.c

package info (click to toggle)
faumachine 20180503-4
  • links: PTS
  • area: main
  • in suites: buster
  • size: 61,272 kB
  • sloc: ansic: 272,290; makefile: 6,199; asm: 4,251; sh: 3,022; perl: 886; xml: 563; pascal: 311; lex: 214; vhdl: 204
file content (193 lines) | stat: -rw-r--r-- 4,420 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
/*
 * Copyright (C) 2007-2009 FAUmachine Team <info@faumachine.org>.
 * This program is free software. You can redistribute it and/or modify it
 * under the terms of the GNU General Public License, either version 2 of
 * the License, or (at your option) any later version. See COPYING.
 */

/*
 * faum_cd_to_iso - converts faum-cd-images to iso-images
 */

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

#include "../node-pc/simulator/cd_image.h"

const char * progname;

/* tell compiler we do not return */
static void
usage(int retval) __attribute__((__noreturn__));
static void
usage(int retval)
{
	fprintf(stderr, "%s [OPTION]\n", progname);
	fprintf(stderr, "Convert FAUmachine-CD-Image to ISO-9660-Images.\n\nOptions:\n"
		"\t-name    <imagename>    Filename of the FAUmachine-CD-Image\n"
		"\t-iso     <iso-filename> Filename of the output-iso-image\n"
		"\t-track   <trackno>      Number of the track (defaults to first track)\n"
#if 0
		"\t-session <trackno>      Number of the session (defaults to first session)\n"
#endif
		);
	exit(retval);
}

static void
msf_to_lba(uint8_t msf[3], int64_t *lba)
{
	/* msf[0] = min; msf[1] = sec; msf[2] = frame */
	if (msf[0] < 90) {
		*lba = (msf[0] * 60 + msf[1]) * 75 + msf[2] - 150;
	} else {
		*lba = (msf[0] * 60 + msf[1]) * 75 + msf[2] - 450150;
	}
}

int main(int argc, char ** argv)
{
	int fd;
	int srcfd;

	int trackno = 0;
#if 0
	int sessionno;
#endif
	int ret;
	cd_image media_desc;
	toc_session toc;
	mode1 cd_block;
	int sizeoftrack = 0;
	toc_entry *act_toc_line;
	int64_t lead_in_size;

	int option;
	const char * filename = NULL;
	const char * isoname = NULL;

	struct option longopts[] = {
		/* name, has_arg, flag, val */
		{ "name",	1, NULL, 'n' },
		{ "iso",	1, NULL, 'i' },
#if 0
		{ "session",	1, NULL, 's' },
#endif
		{ "track",	1, NULL, 't' },
		{ NULL, 0, NULL, 0 }
	};

	progname = *argv;
	
	if (argc == 1) {
		fprintf(stderr, "need parameters!\n");
		usage(EXIT_FAILURE);
	}

	while ((option = getopt_long_only(argc, argv, "", longopts, NULL)) != -1) {
		switch(option) {
		case 'n':
			filename = optarg;
			break;
		case 'i':
			isoname = optarg;
			break;
#if 0
		case 's':
			sessionno = strtol(optarg, (char **)NULL, 10);
#endif
		case 't':
			trackno = strtol(optarg, (char **)NULL, 10);
			if (99 < trackno) {
				fprintf(stderr, "Max # of tracks is 99\n");
				usage(EXIT_FAILURE);
			}
			break;
		default:
			usage(EXIT_FAILURE);
		}
	}

	if ( ! filename ) {
		fprintf(stderr, "No FAUM-CD-Image set\n");
		usage(EXIT_FAILURE);
	}

	if ( ! isoname ) {
		fprintf(stderr, "No ISO-Image set\n");
		usage(EXIT_FAILURE);
	}

	if ( trackno == 0 ) {
		fprintf(stderr, "No Trackno set\n");
		usage(EXIT_FAILURE);
	}

	if (argc != optind) {
		fprintf(stderr, "unknown parameter: %s\n", argv[optind]);
		usage(EXIT_FAILURE);
	}

	srcfd = open(filename, O_RDONLY);
	if (srcfd <= 0) {
		fprintf(stderr, "opening filename %s failed: %s\n",
						filename, strerror(errno));
		exit(EXIT_FAILURE);
	}

	fd = creat(isoname, 0666); 
	if (fd <= 0) {
		fprintf(stderr, "opening isoname %s failed: %s\n",
						isoname, strerror(errno));
		exit(EXIT_FAILURE);
	}


	ret = read(srcfd, &media_desc, sizeof(cd_image));
	
	if (strncmp(media_desc.magic,"\211FAUM-CDIMAGE",13) != 0) {
		fprintf(stderr, "no FAUM-CDimage\n");
		fprintf(stderr, "magic: %s\n", media_desc.magic);
		return 1;
	}


	/* set seek pointer to start of toc (after offset) */
	lseek(srcfd, media_desc.offset, SEEK_SET);

	ret = read(srcfd, &toc, sizeof(toc_session));

	act_toc_line = &(toc.x01) + ( trackno - 1 );
	sizeoftrack = act_toc_line->blkcount;

	/* set seek pointer to start of data (after offset and lead-in) */
	msf_to_lba((uint8_t *)&media_desc.start_of_leadin_m, &lead_in_size);
	lseek(srcfd, media_desc.offset + media_desc.toc_size + (-1 * lead_in_size) + 150 * 2352, SEEK_SET);
	
	while ( 0 < sizeoftrack ) {
		size_t sz;
		/* read 2352 byte from source and write 2048 to imagefile */
		sz = read(srcfd, &cd_block, sizeof(cd_block));
		assert(sz == sizeof(cd_block));
		sz = write(fd, &cd_block.data, sizeof(cd_block.data));
		assert(sz == sizeof(cd_block.data));
		sizeoftrack--;
	}

	ret = close(fd);
	assert(0 <= ret);
	ret = close(srcfd);
	assert(0 <= ret);
	
	return 0;
}