2000-01-06 22:46:20 +00:00
|
|
|
|
/*-
|
2001-01-10 19:28:37 +00:00
|
|
|
|
* Copyright (c) 2000,2001 S<EFBFBD>ren Schmidt
|
2000-01-06 22:46:20 +00:00
|
|
|
|
* 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>
|
2000-12-26 11:56:15 +00:00
|
|
|
|
#include <sys/errno.h>
|
2000-01-06 22:46:20 +00:00
|
|
|
|
#include <sys/ioctl.h>
|
2000-01-17 01:43:25 +00:00
|
|
|
|
#include <sys/stat.h>
|
2000-01-06 22:46:20 +00:00
|
|
|
|
#include <sys/cdio.h>
|
|
|
|
|
#include <sys/cdrio.h>
|
2001-01-10 19:28:37 +00:00
|
|
|
|
#include <sys/param.h>
|
2000-01-06 22:46:20 +00:00
|
|
|
|
|
2000-01-17 01:43:25 +00:00
|
|
|
|
#define BLOCKS 16
|
2000-01-06 22:46:20 +00:00
|
|
|
|
|
|
|
|
|
void cleanup(int);
|
2001-01-10 19:28:37 +00:00
|
|
|
|
void write_file(const char *, int);
|
|
|
|
|
void usage(const char *);
|
|
|
|
|
|
|
|
|
|
static int fd, quiet, saved_block_size;
|
|
|
|
|
static struct cdr_track track;
|
2000-01-06 22:46:20 +00:00
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
main(int argc, char **argv)
|
|
|
|
|
{
|
2001-01-10 19:28:37 +00:00
|
|
|
|
int ch, arg, addr;
|
|
|
|
|
int eject=0, list=0, multi=0, preemp=0, speed=1, test_write=0;
|
|
|
|
|
char *devname = "/dev/acd0c", *prog_name;
|
|
|
|
|
int block_size = 0;
|
2000-01-06 22:46:20 +00:00
|
|
|
|
|
2001-01-10 19:28:37 +00:00
|
|
|
|
prog_name = argv[0];
|
|
|
|
|
while ((ch = getopt(argc, argv, "ef:lmpqs:t")) != -1) {
|
2000-01-06 22:46:20 +00:00
|
|
|
|
switch (ch) {
|
|
|
|
|
case 'e':
|
|
|
|
|
eject = 1;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 'f':
|
|
|
|
|
devname = optarg;
|
|
|
|
|
break;
|
|
|
|
|
|
2001-01-10 19:28:37 +00:00
|
|
|
|
case 'l':
|
|
|
|
|
list = 1;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 'm':
|
|
|
|
|
multi = 1;
|
|
|
|
|
break;
|
|
|
|
|
|
2000-01-06 22:46:20 +00:00
|
|
|
|
case 'p':
|
|
|
|
|
preemp = 1;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 'q':
|
|
|
|
|
quiet = 1;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 's':
|
|
|
|
|
speed = atoi(optarg);
|
2001-01-10 19:28:37 +00:00
|
|
|
|
if (speed <= 0)
|
|
|
|
|
errx(EX_USAGE, "Invalid speed: %s", optarg);
|
2000-01-06 22:46:20 +00:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 't':
|
|
|
|
|
test_write = 1;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
2001-01-10 19:28:37 +00:00
|
|
|
|
usage(prog_name);
|
2000-01-06 22:46:20 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
argc -= optind;
|
|
|
|
|
argv += optind;
|
|
|
|
|
|
2001-01-10 19:28:37 +00:00
|
|
|
|
if (argc == 0)
|
|
|
|
|
usage(prog_name);
|
|
|
|
|
|
2000-01-06 22:46:20 +00:00
|
|
|
|
if ((fd = open(devname, O_RDWR, 0)) < 0)
|
|
|
|
|
err(EX_NOINPUT, "open(%s)", devname);
|
|
|
|
|
|
|
|
|
|
if (ioctl(fd, CDRIOCWRITESPEED, &speed) < 0)
|
|
|
|
|
err(EX_IOERR, "ioctl(CDRIOCWRITESPEED)");
|
|
|
|
|
|
|
|
|
|
if (ioctl(fd, CDRIOCGETBLOCKSIZE, &saved_block_size) < 0)
|
|
|
|
|
err(EX_IOERR, "ioctl(CDRIOCGETBLOCKSIZE)");
|
|
|
|
|
|
|
|
|
|
err_set_exit(cleanup);
|
|
|
|
|
|
|
|
|
|
for (arg = 0; arg < argc; arg++) {
|
|
|
|
|
if (!strcmp(argv[arg], "fixate")) {
|
|
|
|
|
if (!quiet)
|
|
|
|
|
fprintf(stderr, "fixating CD, please wait..\n");
|
2001-01-10 19:28:37 +00:00
|
|
|
|
if (ioctl(fd, CDRIOCCLOSEDISK, &multi) < 0)
|
2000-01-06 22:46:20 +00:00
|
|
|
|
err(EX_IOERR, "ioctl(CDRIOCCLOSEDISK)");
|
|
|
|
|
break;
|
|
|
|
|
}
|
2000-03-03 23:17:27 +00:00
|
|
|
|
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)");
|
2001-01-10 19:28:37 +00:00
|
|
|
|
fprintf(stderr, "%d, %d\n",
|
|
|
|
|
ntohl(entry.entry.addr.lba), addr);
|
|
|
|
|
|
2000-03-03 23:17:27 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
2001-01-10 19:28:37 +00:00
|
|
|
|
if (!strcmp(argv[arg], "erase") || !strcmp(argv[arg], "blank")){
|
|
|
|
|
int error, blank, percent;
|
2001-05-30 08:13:39 +00:00
|
|
|
|
|
2001-01-10 19:28:37 +00:00
|
|
|
|
if (!strcmp(argv[arg], "erase"))
|
|
|
|
|
blank = CDR_B_ALL;
|
|
|
|
|
else
|
|
|
|
|
blank = CDR_B_MIN;
|
2001-05-30 08:13:39 +00:00
|
|
|
|
if (!quiet)
|
|
|
|
|
fprintf(stderr, "%sing CD, please wait..\r",
|
|
|
|
|
blank == CDR_B_ALL ? "eras" : "blank");
|
2001-01-10 19:28:37 +00:00
|
|
|
|
|
|
|
|
|
if (ioctl(fd, CDRIOCBLANK, &blank) < 0)
|
2000-03-03 23:17:27 +00:00
|
|
|
|
err(EX_IOERR, "ioctl(CDRIOCBLANK)");
|
2000-12-26 11:56:15 +00:00
|
|
|
|
while (1) {
|
|
|
|
|
sleep(1);
|
|
|
|
|
error = ioctl(fd, CDRIOCGETPROGRESS, &percent);
|
|
|
|
|
if (percent > 0 && !quiet)
|
|
|
|
|
fprintf(stderr,
|
2001-05-30 08:13:39 +00:00
|
|
|
|
"%sing CD - %d %% done \r",
|
|
|
|
|
blank == CDR_B_ALL ?
|
|
|
|
|
"eras" : "blank", percent);
|
2000-12-26 11:56:15 +00:00
|
|
|
|
if (error || percent == 100)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (!quiet)
|
|
|
|
|
printf("\n");
|
2000-03-03 23:17:27 +00:00
|
|
|
|
continue;
|
|
|
|
|
}
|
2000-01-06 22:46:20 +00:00
|
|
|
|
if (!strcmp(argv[arg], "audio") || !strcmp(argv[arg], "raw")) {
|
|
|
|
|
track.test_write = test_write;
|
2001-01-10 19:28:37 +00:00
|
|
|
|
track.datablock_type = CDR_DB_RAW;
|
2000-01-06 22:46:20 +00:00
|
|
|
|
track.preemp = preemp;
|
|
|
|
|
block_size = 2352;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (!strcmp(argv[arg], "data") || !strcmp(argv[arg], "mode1")) {
|
|
|
|
|
track.test_write = test_write;
|
2001-01-10 19:28:37 +00:00
|
|
|
|
track.datablock_type = CDR_DB_ROM_MODE1;
|
2000-01-06 22:46:20 +00:00
|
|
|
|
track.preemp = 0;
|
|
|
|
|
block_size = 2048;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2000-02-02 13:38:02 +00:00
|
|
|
|
if (!strcmp(argv[arg], "mode2")) {
|
|
|
|
|
track.test_write = test_write;
|
2001-01-10 19:28:37 +00:00
|
|
|
|
track.datablock_type = CDR_DB_ROM_MODE2;
|
2000-02-02 13:38:02 +00:00
|
|
|
|
track.preemp = 0;
|
|
|
|
|
block_size = 2336;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (!strcmp(argv[arg], "XAmode1")) {
|
|
|
|
|
track.test_write = test_write;
|
2001-01-10 19:28:37 +00:00
|
|
|
|
track.datablock_type = CDR_DB_XA_MODE1;
|
2000-02-02 13:38:02 +00:00
|
|
|
|
track.preemp = 0;
|
|
|
|
|
block_size = 2048;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2000-02-18 16:36:28 +00:00
|
|
|
|
if (!block_size)
|
|
|
|
|
err(EX_NOINPUT, "no data format selected");
|
2001-01-10 19:28:37 +00:00
|
|
|
|
if (list) {
|
|
|
|
|
char file_buf[MAXPATHLEN + 1], *eol;
|
|
|
|
|
FILE *fp;
|
2000-02-18 16:36:28 +00:00
|
|
|
|
|
2001-01-10 19:28:37 +00:00
|
|
|
|
if ((fp = fopen(argv[arg], "r")) == NULL)
|
|
|
|
|
err(EX_NOINPUT, "fopen(%s)", argv[arg]);
|
2000-02-18 16:36:28 +00:00
|
|
|
|
|
2001-01-10 19:28:37 +00:00
|
|
|
|
while (fgets(file_buf, sizeof(file_buf), fp) != NULL) {
|
|
|
|
|
if (*file_buf == '#' || *file_buf == '\n')
|
|
|
|
|
continue;
|
|
|
|
|
if (eol = strchr(file_buf, '\n'))
|
|
|
|
|
*eol = NULL;
|
|
|
|
|
write_file(file_buf, block_size);
|
2000-03-02 21:49:10 +00:00
|
|
|
|
}
|
2001-01-10 19:28:37 +00:00
|
|
|
|
if (feof(fp))
|
|
|
|
|
fclose(fp);
|
|
|
|
|
else
|
|
|
|
|
err(EX_IOERR, "fgets(%s)", file_buf);
|
2000-01-06 22:46:20 +00:00
|
|
|
|
}
|
2001-01-10 19:28:37 +00:00
|
|
|
|
else
|
|
|
|
|
write_file(argv[arg], block_size);
|
2000-01-06 22:46:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
2001-01-10 19:28:37 +00:00
|
|
|
|
if (ioctl(fd, CDRIOCSETBLOCKSIZE, &saved_block_size) < 0) {
|
|
|
|
|
err_set_exit(NULL);
|
|
|
|
|
err(EX_IOERR, "ioctl(CDRIOCSETBLOCKSIZE)");
|
|
|
|
|
}
|
2000-01-06 22:46:20 +00:00
|
|
|
|
|
|
|
|
|
if (eject)
|
|
|
|
|
if (ioctl(fd, CDIOCEJECT) < 0)
|
|
|
|
|
err(EX_IOERR, "ioctl(CDIOCEJECT)");
|
|
|
|
|
close(fd);
|
2001-01-10 19:28:37 +00:00
|
|
|
|
exit(EX_OK);
|
2000-01-06 22:46:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
2001-01-10 19:28:37 +00:00
|
|
|
|
void
|
|
|
|
|
cleanup(int dummy)
|
2000-01-06 22:46:20 +00:00
|
|
|
|
{
|
2000-01-07 09:59:31 +00:00
|
|
|
|
if (ioctl(fd, CDRIOCSETBLOCKSIZE, &saved_block_size) < 0)
|
2001-01-10 19:28:37 +00:00
|
|
|
|
err(EX_IOERR, "ioctl(CDRIOCSETBLOCKSIZE)");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
usage(const char *prog_name)
|
|
|
|
|
{
|
|
|
|
|
fprintf(stderr, "Usage: %s [-f device] [-s speed] [-e] [-l] [-m] [-p]\n"
|
|
|
|
|
"\t[-q] [command] [command filename...]\n", prog_name);
|
|
|
|
|
exit(EX_USAGE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
write_file(const char *name, int block_size)
|
|
|
|
|
{
|
|
|
|
|
int addr, count, file, filesize, size;
|
|
|
|
|
char buf[2352*BLOCKS];
|
|
|
|
|
struct stat stat;
|
|
|
|
|
static int cdopen, done_stdin, tot_size = 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 (!cdopen) {
|
|
|
|
|
if (ioctl(fd, CDRIOCOPENDISK) < 0)
|
|
|
|
|
err(EX_IOERR, "ioctl(CDRIOCOPENDISK)");
|
|
|
|
|
cdopen = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ioctl(fd, CDRIOCOPENTRACK, &track) < 0)
|
|
|
|
|
err(EX_IOERR, "ioctl(CDRIOCOPENTRACK)");
|
|
|
|
|
|
|
|
|
|
if (ioctl(fd, CDRIOCNEXTWRITEABLEADDR, &addr) < 0)
|
|
|
|
|
err(EX_IOERR, "ioctl(CDRIOCNEXTWRITEABLEADDR)");
|
|
|
|
|
|
|
|
|
|
if (fstat(file, &stat) < 0)
|
|
|
|
|
err(EX_IOERR, "fstat(%s)", name);
|
|
|
|
|
filesize = stat.st_size / 1024;
|
|
|
|
|
|
|
|
|
|
if (!quiet) {
|
|
|
|
|
fprintf(stderr, "next writeable LBA %d\n", addr);
|
|
|
|
|
if (file == STDIN_FILENO)
|
|
|
|
|
fprintf(stderr, "writing from stdin\n");
|
|
|
|
|
else
|
|
|
|
|
fprintf(stderr,
|
|
|
|
|
"writing from file %s size %d KB\n",
|
|
|
|
|
name, filesize);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lseek(fd, addr * block_size, SEEK_SET);
|
|
|
|
|
size = 0;
|
|
|
|
|
if (filesize == 0)
|
|
|
|
|
filesize++; /* cheat, avoid divide by zero */
|
|
|
|
|
|
|
|
|
|
while ((count = read(file, buf, block_size * BLOCKS)) > 0) {
|
|
|
|
|
int res;
|
|
|
|
|
if (count % block_size) {
|
|
|
|
|
/* pad file to % block_size */
|
|
|
|
|
bzero(&buf[count], block_size * BLOCKS - count);
|
|
|
|
|
count = ((count / block_size) + 1) * block_size;
|
|
|
|
|
}
|
|
|
|
|
if ((res = write(fd, buf, count)) != count) {
|
|
|
|
|
fprintf(stderr, "\nonly wrote %d of %d bytes\n",
|
|
|
|
|
res, count);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
size += count;
|
|
|
|
|
tot_size += count;
|
|
|
|
|
if (!quiet) {
|
|
|
|
|
int pct;
|
|
|
|
|
|
|
|
|
|
fprintf(stderr, "written this track %d KB", size/1024);
|
|
|
|
|
if (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(file);
|
|
|
|
|
if (ioctl(fd, CDRIOCCLOSETRACK) < 0)
|
|
|
|
|
err(EX_IOERR, "ioctl(CDRIOCCLOSETRACK)");
|
2000-01-06 22:46:20 +00:00
|
|
|
|
}
|