2 * Copyright (c) 2000,2001,2002 Søren Schmidt <sos@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer,
10 * without modification, immediately at the beginning of the file.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 #include <sys/errno.h>
41 #include <sys/ioctl.h>
44 #include <sys/cdrio.h>
45 #include <sys/dvdio.h>
46 #include <sys/param.h>
47 #include <arpa/inet.h>
53 char file_name[MAXPATHLEN + 1];
60 static struct track_info tracks[100];
61 static int quiet, verbose, saved_block_size, notracks;
62 static volatile sig_atomic_t global_fd_for_cleanup;
64 void add_track(char *, int, int, int);
65 void do_DAO(int fd, int, int);
66 void do_TAO(int fd, int, int, int);
67 void do_format(int, int, char *);
68 int write_file(int fd, struct track_info *);
69 int roundup_blocks(struct track_info *);
70 void cue_ent(struct cdr_cue_entry *, int, int, int, int, int, int, int);
72 void cleanup_flush(void);
73 void cleanup_signal(int);
77 main(int argc, char **argv)
79 int arg, addr, ch, fd;
80 int dao = 0, eject = 0, fixate = 0, list = 0, multi = 0, preemp = 0;
81 int nogap = 0, speed = 4 * 177, test_write = 0, force = 0;
82 int block_size = 0, block_type = 0, cdopen = 0, dvdrw = 0;
83 const char *dev, *env_speed;
85 if ((dev = getenv("CDROM")) == NULL)
88 env_speed = getenv("BURNCD_SPEED");
90 while ((ch = getopt(argc, argv, "def:Flmnpqs:tv")) != -1) {
147 if (env_speed == NULL)
149 else if (strcasecmp("max", env_speed) == 0)
150 speed = CDR_MAX_SPEED;
152 speed = atoi(env_speed) * 177;
154 errx(EX_USAGE, "Invalid speed: %s", env_speed);
159 if ((fd = open(dev, O_RDWR, 0)) < 0)
160 err(EX_NOINPUT, "open(%s)", dev);
162 if (ioctl(fd, CDRIOCGETBLOCKSIZE, &saved_block_size) < 0)
163 err(EX_IOERR, "ioctl(CDRIOCGETBLOCKSIZE)");
165 if (ioctl(fd, CDRIOCWRITESPEED, &speed) < 0)
166 err(EX_IOERR, "ioctl(CDRIOCWRITESPEED)");
168 global_fd_for_cleanup = fd;
169 err_set_exit(cleanup);
170 signal(SIGHUP, cleanup_signal);
171 signal(SIGINT, cleanup_signal);
172 signal(SIGTERM, cleanup_signal);
174 for (arg = 0; arg < argc; arg++) {
175 if (!strcasecmp(argv[arg], "fixate")) {
179 if (!strcasecmp(argv[arg], "eject")) {
183 if (!strcasecmp(argv[arg], "msinfo")) {
184 struct ioc_read_toc_single_entry entry;
185 struct ioc_toc_header header;
187 if (ioctl(fd, CDIOREADTOCHEADER, &header) < 0)
188 err(EX_IOERR, "ioctl(CDIOREADTOCHEADER)");
189 bzero(&entry, sizeof(struct ioc_read_toc_single_entry));
190 entry.address_format = CD_LBA_FORMAT;
191 entry.track = header.ending_track;
192 if (ioctl(fd, CDIOREADTOCENTRY, &entry) < 0)
193 err(EX_IOERR, "ioctl(CDIOREADTOCENTRY)");
194 if (ioctl(fd, CDRIOCNEXTWRITEABLEADDR, &addr) < 0)
195 err(EX_IOERR, "ioctl(CDRIOCNEXTWRITEABLEADDR)");
196 fprintf(stdout, "%d,%d\n",
197 ntohl(entry.entry.addr.lba), addr);
201 if ((!strcasecmp(argv[arg], "erase") ||
202 !strcasecmp(argv[arg], "blank")) && !test_write) {
203 int blank, pct, last = 0;
205 if (!strcasecmp(argv[arg], "erase"))
210 fprintf(stderr, "%sing CD, please wait..\r",
211 blank == CDR_B_ALL ? "eras" : "blank");
213 if (ioctl(fd, CDRIOCBLANK, &blank) < 0)
214 err(EX_IOERR, "ioctl(CDRIOCBLANK)");
217 if (ioctl(fd, CDRIOCGETPROGRESS, &pct) == -1)
218 err(EX_IOERR,"ioctl(CDRIOGETPROGRESS)");
219 if (pct > 0 && !quiet)
221 "%sing CD - %d %% done \r",
223 "eras" : "blank", pct);
224 if (pct == 100 || (pct == 0 && last > 90))
232 if (!strcasecmp(argv[arg], "format") && !test_write) {
233 if (arg + 1 < argc &&
234 (!strcasecmp(argv[arg + 1], "dvd+rw") ||
235 !strcasecmp(argv[arg + 1], "dvd-rw")))
236 do_format(fd, force, argv[arg + 1]);
238 errx(EX_NOINPUT, "format media type invalid");
242 if (!strcasecmp(argv[arg], "audio") || !strcasecmp(argv[arg], "raw")) {
243 block_type = CDR_DB_RAW;
247 if (!strcasecmp(argv[arg], "data") || !strcasecmp(argv[arg], "mode1")) {
248 block_type = CDR_DB_ROM_MODE1;
252 if (!strcasecmp(argv[arg], "mode2")) {
253 block_type = CDR_DB_ROM_MODE2;
257 if (!strcasecmp(argv[arg], "xamode1")) {
258 block_type = CDR_DB_XA_MODE1;
262 if (!strcasecmp(argv[arg], "xamode2")) {
263 block_type = CDR_DB_XA_MODE2_F2;
267 if (!strcasecmp(argv[arg], "vcd")) {
268 block_type = CDR_DB_XA_MODE2_F2;
274 if (!strcasecmp(argv[arg], "dvdrw")) {
275 block_type = CDR_DB_ROM_MODE1;
282 errx(EX_NOINPUT, "no data format selected");
284 char file_buf[MAXPATHLEN + 1], *eol;
287 if ((fp = fopen(argv[arg], "r")) == NULL)
288 err(EX_NOINPUT, "fopen(%s)", argv[arg]);
290 while (fgets(file_buf, sizeof(file_buf), fp) != NULL) {
291 if (*file_buf == '#' || *file_buf == '\n')
293 if ((eol = strchr(file_buf, '\n')))
295 add_track(file_buf, block_size, block_type, nogap);
300 err(EX_IOERR, "fgets(%s)", file_buf);
303 add_track(argv[arg], block_size, block_type, nogap);
306 if (dvdrw && notracks > 1)
307 errx(EX_USAGE, "DVD's only have 1 track");
308 if (ioctl(fd, CDIOCSTART, 0) < 0)
309 err(EX_IOERR, "ioctl(CDIOCSTART)");
311 if (ioctl(fd, CDRIOCINITWRITER, &test_write) < 0)
312 err(EX_IOERR, "ioctl(CDRIOCINITWRITER)");
316 do_DAO(fd, test_write, multi);
318 do_TAO(fd, test_write, preemp, dvdrw);
320 if (!test_write && fixate && !dao && !dvdrw) {
322 fprintf(stderr, "fixating CD, please wait..\n");
323 if (ioctl(fd, CDRIOCFIXATE, &multi) < 0)
324 err(EX_IOERR, "ioctl(CDRIOCFIXATE)");
327 if (ioctl(fd, CDRIOCSETBLOCKSIZE, &saved_block_size) < 0) {
329 err(EX_IOERR, "ioctl(CDRIOCSETBLOCKSIZE)");
333 if (ioctl(fd, CDIOCEJECT) < 0)
334 err(EX_IOERR, "ioctl(CDIOCEJECT)");
336 signal(SIGHUP, SIG_DFL);
337 signal(SIGINT, SIG_DFL);
338 signal(SIGTERM, SIG_DFL);
344 add_track(char *name, int block_size, int block_type, int nogap)
348 static int done_stdin = 0;
350 if (!strcmp(name, "-")) {
352 warn("skipping multiple usages of stdin");
358 else if ((file = open(name, O_RDONLY, 0)) < 0)
359 err(EX_NOINPUT, "open(%s)", name);
360 if (fstat(file, &sb) < 0)
361 err(EX_IOERR, "fstat(%s)", name);
362 tracks[notracks].file = file;
363 strncpy(tracks[notracks].file_name, name, MAXPATHLEN);
364 if (file == STDIN_FILENO)
365 tracks[notracks].file_size = -1;
367 tracks[notracks].file_size = sb.st_size;
368 tracks[notracks].block_size = block_size;
369 tracks[notracks].block_type = block_type;
371 if (nogap && notracks)
372 tracks[notracks].pregap = 0;
374 if (tracks[notracks - (notracks > 0)].block_type == block_type)
375 tracks[notracks].pregap = 150;
377 tracks[notracks].pregap = 255;
383 if (tracks[notracks].file_size / tracks[notracks].block_size !=
384 roundup_blocks(&tracks[notracks]))
387 "adding type 0x%02x file %s size %jd KB %d blocks %s\n",
388 tracks[notracks].block_type, name,
389 (intmax_t)sb.st_size/1024,
390 roundup_blocks(&tracks[notracks]),
391 pad ? "(0 padded)" : "");
397 do_DAO(int fd, int test_write, int multi)
399 struct cdr_cuesheet sheet;
400 struct cdr_cue_entry cue[100];
401 int format = CDR_SESS_CDROM;
404 int bt2ctl[16] = { 0x0, -1, -1, -1, -1, -1, -1, -1,
405 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, -1, -1 };
407 int bt2df[16] = { 0x0, -1, -1, -1, -1, -1, -1, -1,
408 0x10, 0x30, 0x20, -1, 0x21, -1, -1, -1 };
410 if (ioctl(fd, CDRIOCNEXTWRITEABLEADDR, &addr) < 0)
411 err(EX_IOERR, "ioctl(CDRIOCNEXTWRITEABLEADDR)");
413 fprintf(stderr, "next writeable LBA %d\n", addr);
415 cue_ent(&cue[j++], bt2ctl[tracks[0].block_type], 0x01, 0x00, 0x0,
416 (bt2df[tracks[0].block_type] & 0xf0) |
417 (tracks[0].block_type < 8 ? 0x01 : 0x04), 0x00, addr);
419 for (i = 0; i < notracks; i++) {
420 if (bt2ctl[tracks[i].block_type] < 0 ||
421 bt2df[tracks[i].block_type] < 0)
422 errx(EX_IOERR, "track type not supported in DAO mode");
424 if (tracks[i].block_type >= CDR_DB_XA_MODE1)
425 format = CDR_SESS_CDROM_XA;
428 addr += tracks[i].pregap;
429 tracks[i].addr = addr;
431 cue_ent(&cue[j++], bt2ctl[tracks[i].block_type],
432 0x01, i+1, 0x1, bt2df[tracks[i].block_type],
437 if (tracks[i].pregap) {
438 if (tracks[i].block_type > 0x7) {
439 cue_ent(&cue[j++],bt2ctl[tracks[i].block_type],
441 (bt2df[tracks[i].block_type] & 0xf0) |
442 (tracks[i].block_type < 8 ? 0x01 :0x04),
446 cue_ent(&cue[j++],bt2ctl[tracks[i].block_type],
448 bt2df[tracks[i].block_type],
451 tracks[i].addr = tracks[i - 1].addr +
452 roundup_blocks(&tracks[i - 1]);
454 cue_ent(&cue[j++], bt2ctl[tracks[i].block_type],
455 0x01, i+1, 0x1, bt2df[tracks[i].block_type],
456 0x00, addr + tracks[i].pregap);
458 if (tracks[i].block_type > 0x7)
459 addr += tracks[i].pregap;
461 addr += roundup_blocks(&tracks[i]);
464 cue_ent(&cue[j++], bt2ctl[tracks[i - 1].block_type], 0x01, 0xaa, 0x01,
465 (bt2df[tracks[i - 1].block_type] & 0xf0) |
466 (tracks[i - 1].block_type < 8 ? 0x01 : 0x04), 0x00, addr);
470 sheet.test_write = test_write;
471 sheet.session_type = multi ? CDR_SESS_MULTI : CDR_SESS_NONE;
472 sheet.session_format = format;
474 u_int8_t *ptr = (u_int8_t *)sheet.entries;
476 fprintf(stderr,"CUE sheet:");
477 for (i = 0; i < sheet.len; i++)
479 fprintf(stderr," %02x", ptr[i]);
481 fprintf(stderr,"\n%02x", ptr[i]);
482 fprintf(stderr,"\n");
485 if (ioctl(fd, CDRIOCSENDCUE, &sheet) < 0)
486 err(EX_IOERR, "ioctl(CDRIOCSENDCUE)");
488 for (i = 0; i < notracks; i++) {
489 if (write_file(fd, &tracks[i])) {
491 err(EX_IOERR, "write_file");
495 ioctl(fd, CDRIOCFLUSH);
499 do_TAO(int fd, int test_write, int preemp, int dvdrw)
501 struct cdr_track track;
504 for (i = 0; i < notracks; i++) {
505 track.test_write = test_write;
506 track.datablock_type = tracks[i].block_type;
507 track.preemp = preemp;
508 if (ioctl(fd, CDRIOCINITTRACK, &track) < 0)
509 err(EX_IOERR, "ioctl(CDRIOCINITTRACK)");
514 if (ioctl(fd, CDRIOCNEXTWRITEABLEADDR,
515 &tracks[i].addr) < 0)
516 err(EX_IOERR, "ioctl(CDRIOCNEXTWRITEABLEADDR)");
519 fprintf(stderr, "next writeable LBA %d\n",
521 if (write_file(fd, &tracks[i])) {
523 err(EX_IOERR, "write_file");
525 if (ioctl(fd, CDRIOCFLUSH) < 0)
526 err(EX_IOERR, "ioctl(CDRIOCFLUSH)");
530 #define NTOH3B(x) ((x&0x0000ff)<<16) | (x&0x00ff00) | ((x&0xff0000)>>16)
533 do_format(int the_fd, int force, char *type)
535 struct cdr_format_capacities capacities;
536 struct cdr_format_params format_params;
537 int count, i, pct, last = 0;
539 if (ioctl(the_fd, CDRIOCREADFORMATCAPS, &capacities) == -1)
540 err(EX_IOERR, "ioctl(CDRIOCREADFORMATCAPS)");
543 fprintf(stderr, "format list entries=%zd\n",
544 capacities.length / sizeof(struct cdr_format_capacity));
545 fprintf(stderr, "current format: blocks=%u type=0x%x block_size=%u\n",
546 ntohl(capacities.blocks), capacities.type,
547 NTOH3B(capacities.block_size));
550 count = capacities.length / sizeof(struct cdr_format_capacity);
552 for (i = 0; i < count; ++i)
554 "format %d: blocks=%u type=0x%x param=%u\n",
555 i, ntohl(capacities.format[i].blocks),
556 capacities.format[i].type,
557 NTOH3B(capacities.format[i].param));
560 for (i = 0; i < count; ++i) {
561 if (!strcasecmp(type, "dvd+rw")) {
562 if (capacities.format[i].type == 0x26) {
566 if (!strcasecmp(type, "dvd-rw")) {
567 if (capacities.format[i].type == 0x0) {
573 errx(EX_IOERR, "could not find a valid format capacity");
576 fprintf(stderr,"formatting with blocks=%u type=0x%x param=%u\n",
577 ntohl(capacities.format[i].blocks),
578 capacities.format[i].type,
579 NTOH3B(capacities.format[i].param));
581 if (!force && capacities.type == 2)
582 errx(EX_IOERR, "media already formatted (use -F to override)");
584 memset(&format_params, 0, sizeof(struct cdr_format_params));
585 format_params.fov = 1;
586 format_params.immed = 1;
587 format_params.length = ntohs(sizeof(struct cdr_format_capacity));
588 memcpy(&format_params.format, &capacities.format[i],
589 sizeof(struct cdr_format_capacity));
591 if(ioctl(the_fd, CDRIOCFORMAT, &format_params) == -1)
592 err(EX_IOERR, "ioctl(CDRIOCFORMAT)");
596 if (ioctl(the_fd, CDRIOCGETPROGRESS, &pct) == -1)
597 err(EX_IOERR, "ioctl(CDRIOGETPROGRESS)");
598 if (pct > 0 && !quiet)
599 fprintf(stderr, "formatting DVD - %d %% done \r",
601 if (pct == 100 || (pct == 0 && last > 90))
606 fprintf(stderr, "\n");
610 write_file(int fd, struct track_info *track_info)
612 off_t size, count, filesize;
613 char buf[2352*BLOCKS];
614 static off_t tot_size = 0;
616 filesize = track_info->file_size / 1024;
618 if (ioctl(fd, CDRIOCSETBLOCKSIZE, &track_info->block_size) < 0)
619 err(EX_IOERR, "ioctl(CDRIOCSETBLOCKSIZE)");
621 if (track_info->addr >= 0)
622 lseek(fd, track_info->addr * track_info->block_size, SEEK_SET);
625 fprintf(stderr, "addr = %d size = %jd blocks = %d\n",
626 track_info->addr, (intmax_t)track_info->file_size,
627 roundup_blocks(track_info));
630 if (track_info->file == STDIN_FILENO)
631 fprintf(stderr, "writing from stdin\n");
634 "writing from file %s size %jd KB\n",
635 track_info->file_name, (intmax_t)filesize);
639 while ((count = read(track_info->file, buf,
640 track_info->file_size == -1
641 ? track_info->block_size * BLOCKS
642 : MIN((track_info->file_size - size),
643 track_info->block_size * BLOCKS))) > 0) {
646 if (count % track_info->block_size) {
647 /* pad file to % block_size */
649 (track_info->block_size * BLOCKS) - count);
650 count = ((count / track_info->block_size) + 1) *
651 track_info->block_size;
653 if ((res = write(fd, buf, count)) != count) {
655 fprintf(stderr, "\n");
656 close(track_info->file);
659 fprintf(stderr, "\nonly wrote %d of %jd"
660 " bytes\n", res, (intmax_t)count);
668 fprintf(stderr, "written this track %jd KB",
669 (intmax_t)size/1024);
670 if (track_info->file != STDIN_FILENO && filesize) {
671 pct = (size / 1024) * 100 / filesize;
672 fprintf(stderr, " (%d%%)", pct);
674 fprintf(stderr, " total %jd KB\r",
675 (intmax_t)tot_size / 1024);
677 if (track_info->file_size != -1
678 && size >= track_info->file_size)
683 fprintf(stderr, "\n");
684 close(track_info->file);
689 roundup_blocks(struct track_info *track)
691 return ((track->file_size + track->block_size - 1) / track->block_size);
695 cue_ent(struct cdr_cue_entry *cue, int ctl, int adr, int track, int idx,
696 int dataform, int scms, int lba)
702 cue->dataform = dataform;
705 cue->min = lba / (60*75);
706 cue->sec = (lba % (60*75)) / 75;
707 cue->frame = (lba % (60*75)) % 75;
711 cleanup(int dummy __unused)
713 if (ioctl(global_fd_for_cleanup, CDRIOCSETBLOCKSIZE,
714 &saved_block_size) < 0)
715 err(EX_IOERR, "ioctl(CDRIOCSETBLOCKSIZE)");
721 if (ioctl(global_fd_for_cleanup, CDRIOCFLUSH) < 0)
722 err(EX_IOERR, "ioctl(CDRIOCFLUSH)");
726 cleanup_signal(int sig)
728 signal(sig, SIG_IGN);
729 ioctl(global_fd_for_cleanup, CDRIOCFLUSH);
730 write(STDERR_FILENO, "\nAborted\n", 10);
738 "usage: %s [-deFlmnpqtv] [-f device] [-s speed] [command]"
739 " [command file ...]\n", getprogname());