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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
|
/*-
* Copyright (c) 2000,2001 Søren Schmidt <sos@freebsd.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer,
* without modification, immediately at the beginning of the file.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* $FreeBSD$
*/
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <err.h>
#include <sysexits.h>
#include <fcntl.h>
#include <sys/errno.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/cdio.h>
#include <sys/cdrio.h>
#include <sys/param.h>
#define BLOCKS 16
struct track_info {
int file;
char *file_name;
int file_size;
int block_size;
int block_type;
int addr;
int gap;
};
static struct track_info tracks[100];
static int fd, quiet, verbose, saved_block_size, notracks;
void add_track(char *, int, int);
void do_DAO(int);
void do_TAO(int, int);
int write_file(struct track_info *);
int roundup_blocks(struct track_info *);
void cue_ent(struct cdr_cue_entry *, int, int, int, int, int, int, int);
void cleanup(int);
void usage(void);
int
main(int argc, char **argv)
{
int ch, arg, addr;
int dao = 0, eject = 0, fixate = 0, list = 0, multi = 0, preemp = 0;
int speed = 4, test_write = 0;
int block_size = 0, block_type = 0, cdopen = 0;
char *devname = "/dev/acd0c";
while ((ch = getopt(argc, argv, "def:lmpqs:tv")) != -1) {
switch (ch) {
case 'd':
dao = 1;
break;
case 'e':
eject = 1;
break;
case 'f':
devname = optarg;
break;
case 'l':
list = 1;
break;
case 'm':
multi = 1;
break;
case 'p':
preemp = 1;
break;
case 'q':
quiet = 1;
break;
case 's':
speed = atoi(optarg);
if (speed <= 0)
errx(EX_USAGE, "Invalid speed: %s", optarg);
break;
case 't':
test_write = 1;
break;
case 'v':
verbose = 1;
break;
default:
usage();
}
}
argc -= optind;
argv += optind;
if (argc == 0)
usage();
if ((fd = open(devname, O_RDWR, 0)) < 0)
err(EX_NOINPUT, "open(%s)", devname);
if (ioctl(fd, CDRIOCGETBLOCKSIZE, &saved_block_size) < 0)
err(EX_IOERR, "ioctl(CDRIOCGETBLOCKSIZE)");
if (ioctl(fd, CDRIOCWRITESPEED, &speed) < 0)
err(EX_IOERR, "ioctl(CDRIOCWRITESPEED)");
err_set_exit(cleanup);
for (arg = 0; arg < argc; arg++) {
if (!strcmp(argv[arg], "fixate")) {
fixate = 1;
break;
}
if (!strcmp(argv[arg], "msinfo")) {
struct ioc_read_toc_single_entry entry;
bzero(&entry, sizeof(struct ioc_read_toc_single_entry));
entry.address_format = CD_LBA_FORMAT;
if (ioctl(fd, CDIOREADTOCENTRY, &entry) < 0)
err(EX_IOERR, "ioctl(CDIOREADTOCENTRY)");
if (ioctl(fd, CDRIOCNEXTWRITEABLEADDR, &addr) < 0)
err(EX_IOERR, "ioctl(CDRIOCNEXTWRITEABLEADDR)");
fprintf(stderr, "%d, %d\n",
ntohl(entry.entry.addr.lba), addr);
break;
}
if (!strcmp(argv[arg], "erase") || !strcmp(argv[arg], "blank")){
int error, blank, percent;
if (!strcmp(argv[arg], "erase"))
blank = CDR_B_ALL;
else
blank = CDR_B_MIN;
if (!quiet)
fprintf(stderr, "%sing CD, please wait..\r",
blank == CDR_B_ALL ? "eras" : "blank");
if (ioctl(fd, CDRIOCBLANK, &blank) < 0)
err(EX_IOERR, "ioctl(CDRIOCBLANK)");
while (1) {
sleep(1);
error = ioctl(fd, CDRIOCGETPROGRESS, &percent);
if (percent > 0 && !quiet)
fprintf(stderr,
"%sing CD - %d %% done \r",
blank == CDR_B_ALL ?
"eras" : "blank", percent);
if (error || percent == 100)
break;
}
if (!quiet)
printf("\n");
continue;
}
if (!strcmp(argv[arg], "audio") || !strcmp(argv[arg], "raw")) {
block_type = CDR_DB_RAW;
block_size = 2352;
continue;
}
if (!strcmp(argv[arg], "data") || !strcmp(argv[arg], "mode1")) {
block_type = CDR_DB_ROM_MODE1;
block_size = 2048;
continue;
}
if (!strcmp(argv[arg], "mode2")) {
block_type = CDR_DB_ROM_MODE2;
block_size = 2336;
continue;
}
if (!strcmp(argv[arg], "XAmode1")) {
block_type = CDR_DB_XA_MODE1;
block_size = 2048;
continue;
}
if (!strcmp(argv[arg], "XAmode2")) {
block_type = CDR_DB_XA_MODE2_F2;
block_size = 2324;
continue;
}
if (!block_size)
err(EX_NOINPUT, "no data format selected");
if (list) {
char file_buf[MAXPATHLEN + 1], *eol;
FILE *fp;
if ((fp = fopen(argv[arg], "r")) == NULL)
err(EX_NOINPUT, "fopen(%s)", argv[arg]);
while (fgets(file_buf, sizeof(file_buf), fp) != NULL) {
if (*file_buf == '#' || *file_buf == '\n')
continue;
if ((eol = strchr(file_buf, '\n')))
*eol = NULL;
add_track(file_buf, block_size, block_type);
}
if (feof(fp))
fclose(fp);
else
err(EX_IOERR, "fgets(%s)", file_buf);
}
else
add_track(argv[arg], block_size, block_type);
}
if (notracks) {
if (ioctl(fd, CDIOCSTART, 0) < 0)
err(EX_IOERR, "ioctl(CDIOCSTART)");
if (!cdopen) {
if (ioctl(fd, CDRIOCINITWRITER, &test_write) < 0)
err(EX_IOERR, "ioctl(CDRIOCINITWRITER)");
cdopen = 1;
}
if (dao)
do_DAO(test_write);
else
do_TAO(test_write, preemp);
}
if (fixate && !dao) {
if (!quiet)
fprintf(stderr, "fixating CD, please wait..\n");
if (ioctl(fd, CDRIOCFIXATE, &multi) < 0)
err(EX_IOERR, "ioctl(CDRIOCFIXATE)");
}
if (ioctl(fd, CDRIOCSETBLOCKSIZE, &saved_block_size) < 0) {
err_set_exit(NULL);
err(EX_IOERR, "ioctl(CDRIOCSETBLOCKSIZE)");
}
if (eject)
if (ioctl(fd, CDIOCEJECT) < 0)
err(EX_IOERR, "ioctl(CDIOCEJECT)");
close(fd);
exit(EX_OK);
}
void
add_track(char *name, int block_size, int block_type)
{
struct stat stat;
int file;
static int done_stdin = 0;
if (!strcmp(name, "-")) {
if (done_stdin) {
warn("skipping multiple usages of stdin");
return;
}
file = STDIN_FILENO;
done_stdin = 1;
}
else if ((file = open(name, O_RDONLY, 0)) < 0)
err(EX_NOINPUT, "open(%s)", name);
if (fstat(file, &stat) < 0)
err(EX_IOERR, "fstat(%s)", name);
tracks[notracks].file = file;
tracks[notracks].file_name = name;
tracks[notracks].file_size = stat.st_size;
tracks[notracks].block_size = block_size;
tracks[notracks].block_type = block_type;
if (verbose) {
int pad = 0;
if (tracks[notracks].file_size / tracks[notracks].block_size !=
roundup_blocks(&tracks[notracks]))
pad = 1;
fprintf(stderr,
"adding type 0x%02x file %s size %qd KB %d blocks %s\n",
tracks[notracks].block_type, name,
stat.st_size / 1024, roundup_blocks(&tracks[notracks]),
pad ? "(0 padded)" : "");
}
notracks++;
}
void
do_DAO(int test_write)
{
struct cdr_cuesheet sheet;
struct cdr_cue_entry cue[100];
int addr, i, j = 0;
int last_type;
int bt2ctl[16] = { 0x0, -1, -1, -1, -1, -1, -1, -1,
0x4, 0x4, 0x4, 0x4, 0x4, 0x4, -1, -1 };
int bt2df[16] = { 0x0, -1, -1, -1, -1, -1, -1, -1,
0x10, 0x30, 0x20, -1, 0x21, -1, -1, -1 };
if (ioctl(fd, CDRIOCNEXTWRITEABLEADDR, &addr) < 0)
err(EX_IOERR, "ioctl(CDRIOCNEXTWRITEABLEADDR)");
if (verbose)
fprintf(stderr, "next writeable LBA %d\n", addr);
cue_ent(&cue[j++], bt2ctl[tracks[0].block_type], 0x01, 0x00, 0x0,
(bt2df[tracks[0].block_type] & 0xf0) |
(tracks[0].block_type < 8 ? 0x01 : 0x04), 0x00, addr);
last_type = tracks[0].block_type;
for (i = 0; i < notracks; i++) {
if (bt2ctl[tracks[i].block_type] < 0 ||
bt2df[tracks[i].block_type] < 0)
err(EX_IOERR, "track type not supported in DAO mode");
if (tracks[i].block_type == last_type)
tracks[i].gap = 150;
else
tracks[i].gap = 255;
last_type = tracks[i].block_type;
if (i == 0) { /* gap uses no data */
cue_ent(&cue[j++], bt2ctl[tracks[i].block_type],
0x01, i+1, 0x0,
(bt2df[tracks[i].block_type] & 0xf0) |
(tracks[i].block_type < 8 ? 0x01 : 0x04),
0x00, addr);
addr += tracks[i].gap;
tracks[i].addr = addr;
cue_ent(&cue[j++], bt2ctl[tracks[i].block_type],
0x01, i+1, 0x1, bt2df[tracks[i].block_type],
0x00, addr);
}
else {
if (tracks[i].block_type > 0x7) {
cue_ent(&cue[j++],bt2ctl[tracks[i].block_type],
0x01, i+1, 0x0,
(bt2df[tracks[i].block_type] & 0xf0) |
(tracks[i].block_type < 8 ? 0x01 :0x04),
0x00, addr);
}
else
cue_ent(&cue[j++],bt2ctl[tracks[i].block_type],
0x01, i+1, 0x0,
bt2df[tracks[i].block_type],
0x00, addr);
tracks[i].addr = tracks[i - 1].addr +
roundup_blocks(&tracks[i - 1]);
cue_ent(&cue[j++], bt2ctl[tracks[i].block_type],
0x01, i+1, 0x1, bt2df[tracks[i].block_type],
0x00, addr + tracks[i].gap);
if (tracks[i].block_type > 0x7)
addr += tracks[i].gap;
}
addr += roundup_blocks(&tracks[i]);
}
cue_ent(&cue[j++], bt2ctl[tracks[i - 1].block_type], 0x01, 0xaa, 0x01,
(bt2df[tracks[i - 1].block_type] & 0xf0) |
(tracks[i - 1].block_type < 8 ? 0x01 : 0x04), 0x00, addr);
sheet.len = j * 8;
sheet.entries = cue;
sheet.test_write = test_write;
if (verbose) {
u_int8_t *ptr = (u_int8_t *)sheet.entries;
fprintf(stderr,"CUE sheet:");
for (i = 0; i < sheet.len; i++)
if (i % 8)
fprintf(stderr," %02x", ptr[i]);
else
fprintf(stderr,"\n%02x", ptr[i]);
fprintf(stderr,"\n");
}
if (ioctl(fd, CDRIOCSENDCUE, &sheet) < 0)
err(EX_IOERR, "ioctl(CDRIOCSENDCUE)");
#if 0
if (ioctl(fd, CDRIOCNEXTWRITEABLEADDR, &addr) < 0)
err(EX_IOERR, "ioctl(CDRIOCNEXTWRITEABLEADDR)");
#endif
for (i = 0; i < notracks; i++) {
if (write_file(&tracks[i]))
err(EX_IOERR, "write_file");
}
ioctl(fd, CDRIOCFLUSH);
}
void
do_TAO(int test_write, int preemp)
{
struct cdr_track track;
int i;
for (i = 0; i < notracks; i++) {
track.test_write = test_write;
track.datablock_type = tracks[i].block_type;
track.preemp = preemp;
if (ioctl(fd, CDRIOCINITTRACK, &track) < 0)
err(EX_IOERR, "ioctl(CDRIOCINITTRACK)");
if (ioctl(fd, CDRIOCNEXTWRITEABLEADDR, &tracks[i].addr) < 0)
err(EX_IOERR, "ioctl(CDRIOCNEXTWRITEABLEADDR)");
if (!quiet)
fprintf(stderr, "next writeable LBA %d\n",
tracks[i].addr);
if (write_file(&tracks[i]))
err(EX_IOERR, "write_file");
if (ioctl(fd, CDRIOCFLUSH) < 0)
err(EX_IOERR, "ioctl(CDRIOCFLUSH)");
}
}
int
write_file(struct track_info *track_info)
{
int size, count, filesize;
char buf[2352*BLOCKS];
static int tot_size = 0;
filesize = track_info->file_size / 1024;
if (ioctl(fd, CDRIOCSETBLOCKSIZE, &track_info->block_size) < 0)
err(EX_IOERR, "ioctl(CDRIOCSETBLOCKSIZE)");
if (track_info->addr >= 0)
lseek(fd, track_info->addr * track_info->block_size, SEEK_SET);
fprintf(stderr, "addr = %d size = %d blocks = %d\n",
track_info->addr, track_info->file_size, roundup_blocks(track_info)); /* SOS */
if (!quiet) {
if (track_info->file == STDIN_FILENO)
fprintf(stderr, "writing from stdin\n");
else
fprintf(stderr,
"writing from file %s size %d KB\n",
track_info->file_name, filesize);
}
size = 0;
if (filesize == 0)
filesize++; /* cheat, avoid divide by zero */
while ((count = read(track_info->file, buf,
track_info->block_size * BLOCKS)) > 0) {
int res;
if (count % track_info->block_size) {
/* pad file to % block_size */
bzero(&buf[count],
(track_info->block_size * BLOCKS) - count);
count = ((count / track_info->block_size) + 1) *
track_info->block_size;
}
if ((res = write(fd, buf, count)) != count) {
fprintf(stderr, "\nonly wrote %d of %d bytes err=%d\n",
res, count, errno);
break;
}
size += count;
tot_size += count;
if (!quiet) {
int pct;
fprintf(stderr, "written this track %d KB", size/1024);
if (track_info->file != STDIN_FILENO) {
pct = (size / 1024) * 100 / filesize;
fprintf(stderr, " (%d%%)", pct);
}
fprintf(stderr, " total %d KB\r", tot_size/1024);
}
}
if (!quiet)
fprintf(stderr, "\n");
close(track_info->file);
return 0;
}
int
roundup_blocks(struct track_info *track)
{
return ((track->file_size + track->block_size - 1) / track->block_size);
}
void
cue_ent(struct cdr_cue_entry *cue, int ctl, int adr, int track, int index,
int dataform, int scms, int lba)
{
cue->adr = adr;
cue->ctl = ctl;
cue->track = track;
cue->index = index;
cue->dataform = dataform;
cue->scms = scms;
lba += 150;
cue->min = lba / (60*75);
cue->sec = (lba % (60*75)) / 75;
cue->frame = (lba % (60*75)) % 75;
}
void
cleanup(int dummy)
{
if (ioctl(fd, CDRIOCSETBLOCKSIZE, &saved_block_size) < 0)
err(EX_IOERR, "ioctl(CDRIOCSETBLOCKSIZE)");
}
void
usage(void)
{
fprintf(stderr,
"Usage: %s [-delmpqtv] [-f device] [-s speed] [command]"
" [command file ...]\n", getprogname());
exit(EX_USAGE);
}
|