]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/burncd/burncd.c
This commit was generated by cvs2svn to compensate for changes in r90926,
[FreeBSD/FreeBSD.git] / usr.sbin / burncd / burncd.c
1 /*-
2  * Copyright (c) 2000,2001,2002 Søren Schmidt <sos@freebsd.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
16  *
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.
27  *
28  * $FreeBSD$
29  */
30
31 #include <arpa/inet.h>
32 #include <unistd.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <err.h>
37 #include <sysexits.h>
38 #include <fcntl.h>
39 #include <sys/errno.h>
40 #include <sys/ioctl.h>
41 #include <sys/stat.h>
42 #include <sys/cdio.h>
43 #include <sys/cdrio.h>
44 #include <sys/param.h>
45
46 #define BLOCKS  16
47
48 struct track_info {
49         int     file;
50         char    *file_name;
51         int     file_size;
52         int     block_size;
53         int     block_type;
54         int     pregap;
55         int     addr;
56 };
57 static struct track_info tracks[100];
58 static int fd, quiet, verbose, saved_block_size, notracks;
59
60 void add_track(char *, int, int, int);
61 void do_DAO(int, int);
62 void do_TAO(int, int);
63 int write_file(struct track_info *);
64 int roundup_blocks(struct track_info *);
65 void cue_ent(struct cdr_cue_entry *, int, int, int, int, int, int, int);
66 void cleanup(int);
67 void usage(void);
68
69 int
70 main(int argc, char **argv)
71 {
72         int ch, arg, addr;
73         int dao = 0, eject = 0, fixate = 0, list = 0, multi = 0, preemp = 0;
74         int nogap = 0, speed = 4, test_write = 0;
75         int block_size = 0, block_type = 0, cdopen = 0;
76         const char *dev = "/dev/acd0c";
77
78         while ((ch = getopt(argc, argv, "def:lmnpqs:tv")) != -1) {
79                 switch (ch) {
80                 case 'd':
81                         dao = 1;
82                         break;
83
84                 case 'e':
85                         eject = 1;
86                         break;
87
88                 case 'f':
89                         dev = optarg;
90                         break;
91
92                 case 'l':
93                         list = 1;
94                         break;
95
96                 case 'm':
97                         multi = 1;
98                         break;
99
100                 case 'n':
101                         nogap = 1;
102                         break;
103
104                 case 'p':
105                         preemp = 1;
106                         break;
107
108                 case 'q':
109                         quiet = 1;
110                         break;
111
112                 case 's':
113                         speed = atoi(optarg);
114                         if (speed <= 0)
115                                 errx(EX_USAGE, "Invalid speed: %s", optarg);
116                         break;
117
118                 case 't':
119                         test_write = 1;
120                         break;
121
122                 case 'v':
123                         verbose = 1;
124                         break;
125
126                 default: 
127                         usage();
128                 }
129         }
130         argc -= optind;
131         argv += optind;
132
133         if (argc == 0)
134                 usage();
135
136         if ((fd = open(dev, O_RDWR, 0)) < 0)
137                 err(EX_NOINPUT, "open(%s)", dev);
138
139         if (ioctl(fd, CDRIOCGETBLOCKSIZE, &saved_block_size) < 0) 
140                 err(EX_IOERR, "ioctl(CDRIOCGETBLOCKSIZE)");
141
142         if (ioctl(fd, CDRIOCWRITESPEED, &speed) < 0) 
143                 err(EX_IOERR, "ioctl(CDRIOCWRITESPEED)");
144
145         err_set_exit(cleanup);
146
147         for (arg = 0; arg < argc; arg++) {
148                 if (!strcasecmp(argv[arg], "fixate")) {
149                         fixate = 1;
150                         break;
151                 }
152                 if (!strcasecmp(argv[arg], "msinfo")) {
153                         struct ioc_read_toc_single_entry entry;
154                         struct ioc_toc_header header;
155
156                         if (ioctl(fd, CDIOREADTOCHEADER, &header) < 0) 
157                                 err(EX_IOERR, "ioctl(CDIOREADTOCHEADER)");
158                         bzero(&entry, sizeof(struct ioc_read_toc_single_entry));
159                         entry.address_format = CD_LBA_FORMAT;
160                         entry.track = header.ending_track;
161                         if (ioctl(fd, CDIOREADTOCENTRY, &entry) < 0) 
162                                 err(EX_IOERR, "ioctl(CDIOREADTOCENTRY)");
163                         if (ioctl(fd, CDRIOCNEXTWRITEABLEADDR, &addr) < 0) 
164                                 err(EX_IOERR, "ioctl(CDRIOCNEXTWRITEABLEADDR)");
165                         fprintf(stdout, "%d,%d\n", 
166                                 ntohl(entry.entry.addr.lba), addr);
167
168                         break;
169                 }
170                 if (!strcasecmp(argv[arg], "erase") || !strcasecmp(argv[arg], "blank")){
171                         int error, blank, percent;
172
173                         if (!strcasecmp(argv[arg], "erase"))
174                                 blank = CDR_B_ALL;
175                         else
176                                 blank = CDR_B_MIN;
177                         if (!quiet)
178                                 fprintf(stderr, "%sing CD, please wait..\r",
179                                         blank == CDR_B_ALL ? "eras" : "blank");
180
181                         if (ioctl(fd, CDRIOCBLANK, &blank) < 0)
182                                 err(EX_IOERR, "ioctl(CDRIOCBLANK)");
183                         while (1) {
184                                 sleep(1);
185                                 error = ioctl(fd, CDRIOCGETPROGRESS, &percent);
186                                 if (percent > 0 && !quiet)
187                                         fprintf(stderr, 
188                                                 "%sing CD - %d %% done     \r",
189                                                 blank == CDR_B_ALL ? 
190                                                 "eras" : "blank", percent);
191                                 if (error || percent == 100)
192                                         break;
193                         }
194                         if (!quiet)
195                                 printf("\n");
196                         continue;
197                 }
198                 if (!strcasecmp(argv[arg], "audio") || !strcasecmp(argv[arg], "raw")) {
199                         block_type = CDR_DB_RAW;
200                         block_size = 2352;
201                         continue;
202                 }
203                 if (!strcasecmp(argv[arg], "data") || !strcasecmp(argv[arg], "mode1")) {
204                         block_type = CDR_DB_ROM_MODE1;
205                         block_size = 2048;
206                         continue;
207                 }
208                 if (!strcasecmp(argv[arg], "mode2")) {
209                         block_type = CDR_DB_ROM_MODE2;
210                         block_size = 2336;
211                         continue;
212                 }
213                 if (!strcasecmp(argv[arg], "xamode1")) {
214                         block_type = CDR_DB_XA_MODE1;
215                         block_size = 2048;
216                         continue;
217                 }
218                 if (!strcasecmp(argv[arg], "xamode2")) {
219                         block_type = CDR_DB_XA_MODE2_F2;
220                         block_size = 2324;
221                         continue;
222                 }
223                 if (!strcasecmp(argv[arg], "vcd")) {
224                         block_type = CDR_DB_XA_MODE2_F2;
225                         block_size = 2352;
226                         dao = 1;
227                         nogap = 1;
228                         continue;
229                 }
230                 if (!block_size)
231                         err(EX_NOINPUT, "no data format selected");
232                 if (list) {
233                         char file_buf[MAXPATHLEN + 1], *eol;
234                         FILE *fp;
235
236                         if ((fp = fopen(argv[arg], "r")) == NULL)
237                                 err(EX_NOINPUT, "fopen(%s)", argv[arg]);
238
239                         while (fgets(file_buf, sizeof(file_buf), fp) != NULL) {
240                                 if (*file_buf == '#' || *file_buf == '\n')
241                                         continue;
242                                 if ((eol = strchr(file_buf, '\n')))
243                                         *eol = NULL;
244                                 add_track(file_buf, block_size, block_type, nogap);
245                         }
246                         if (feof(fp))
247                                 fclose(fp);
248                         else
249                                 err(EX_IOERR, "fgets(%s)", file_buf);
250                 }
251                 else
252                         add_track(argv[arg], block_size, block_type, nogap);
253         }
254         if (notracks) {
255                 if (ioctl(fd, CDIOCSTART, 0) < 0)
256                         err(EX_IOERR, "ioctl(CDIOCSTART)");
257                 if (!cdopen) {
258                         if (ioctl(fd, CDRIOCINITWRITER, &test_write) < 0)
259                                 err(EX_IOERR, "ioctl(CDRIOCINITWRITER)");
260                         cdopen = 1;
261                 }
262                 if (dao) 
263                         do_DAO(test_write, multi);
264                 else
265                         do_TAO(test_write, preemp);
266         }
267         if (fixate && !dao) {
268                 if (!quiet)
269                         fprintf(stderr, "fixating CD, please wait..\n");
270                 if (ioctl(fd, CDRIOCFIXATE, &multi) < 0)
271                         err(EX_IOERR, "ioctl(CDRIOCFIXATE)");
272         }
273
274         if (ioctl(fd, CDRIOCSETBLOCKSIZE, &saved_block_size) < 0) {
275                 err_set_exit(NULL);
276                 err(EX_IOERR, "ioctl(CDRIOCSETBLOCKSIZE)");
277         }
278
279         if (eject)
280                 if (ioctl(fd, CDIOCEJECT) < 0)
281                         err(EX_IOERR, "ioctl(CDIOCEJECT)");
282         close(fd);
283         exit(EX_OK);
284 }
285
286 void
287 add_track(char *name, int block_size, int block_type, int nogap)
288 {
289         struct stat sb;
290         int file;
291         static int done_stdin = 0;
292
293         if (!strcmp(name, "-")) {
294                 if (done_stdin) {
295                         warn("skipping multiple usages of stdin");
296                         return;
297                 }
298                 file = STDIN_FILENO;
299                 done_stdin = 1;
300         }
301         else if ((file = open(name, O_RDONLY, 0)) < 0)
302                 err(EX_NOINPUT, "open(%s)", name);
303         if (fstat(file, &sb) < 0)
304                 err(EX_IOERR, "fstat(%s)", name);
305         tracks[notracks].file = file;
306         tracks[notracks].file_name = name;
307         tracks[notracks].file_size = sb.st_size;
308         tracks[notracks].block_size = block_size;
309         tracks[notracks].block_type = block_type;
310
311         if (nogap && notracks)
312                 tracks[notracks].pregap = 0;
313         else {
314                 if (tracks[notracks - (notracks > 0)].block_type == block_type)
315                         tracks[notracks].pregap = 150;
316                 else
317                         tracks[notracks].pregap = 255;
318         }
319
320         if (verbose) {
321                 int pad = 0;
322
323                 if (tracks[notracks].file_size / tracks[notracks].block_size !=
324                     roundup_blocks(&tracks[notracks]))
325                         pad = 1;
326                 fprintf(stderr, 
327                         "adding type 0x%02x file %s size %d KB %d blocks %s\n",
328                         tracks[notracks].block_type, name, (int)sb.st_size/1024,
329                         roundup_blocks(&tracks[notracks]),
330                         pad ? "(0 padded)" : "");
331         }
332         notracks++;
333 }
334
335 void
336 do_DAO(int test_write, int multi)
337 {
338         struct cdr_cuesheet sheet;
339         struct cdr_cue_entry cue[100];
340         int format = CDR_SESS_CDROM;
341         int addr, i, j = 0;
342
343         int bt2ctl[16] = { 0x0,  -1,  -1,  -1,  -1,  -1,  -1,  -1,
344                            0x4, 0x4, 0x4, 0x4, 0x4, 0x4,  -1,  -1 };
345
346         int bt2df[16] = { 0x0,    -1,   -1,   -1,   -1,   -1,   -1,   -1,
347                           0x10, 0x30, 0x20,   -1, 0x21,   -1,   -1,   -1 };
348         
349         if (ioctl(fd, CDRIOCNEXTWRITEABLEADDR, &addr) < 0) 
350                 err(EX_IOERR, "ioctl(CDRIOCNEXTWRITEABLEADDR)");
351         if (verbose)
352                 fprintf(stderr, "next writeable LBA %d\n", addr);
353
354         cue_ent(&cue[j++], bt2ctl[tracks[0].block_type], 0x01, 0x00, 0x0,
355                 (bt2df[tracks[0].block_type] & 0xf0) | 
356                 (tracks[0].block_type < 8 ? 0x01 : 0x04), 0x00, addr);
357
358         for (i = 0; i < notracks; i++) {
359                 if (bt2ctl[tracks[i].block_type] < 0 ||
360                     bt2df[tracks[i].block_type] < 0)
361                         err(EX_IOERR, "track type not supported in DAO mode");
362
363                 if (tracks[i].block_type >= CDR_DB_XA_MODE1)
364                         format = CDR_SESS_CDROM_XA;
365
366                 if (i == 0) {
367                         addr += tracks[i].pregap;
368                         tracks[i].addr = addr;
369
370                         cue_ent(&cue[j++], bt2ctl[tracks[i].block_type], 
371                                 0x01, i+1, 0x1, bt2df[tracks[i].block_type],
372                                 0x00, addr);
373
374                 }
375                 else {
376                         if (tracks[i].pregap) {
377                                 if (tracks[i].block_type > 0x7) {
378                                         cue_ent(&cue[j++],bt2ctl[tracks[i].block_type], 
379                                                 0x01, i+1, 0x0,
380                                                 (bt2df[tracks[i].block_type] & 0xf0) | 
381                                                 (tracks[i].block_type < 8 ? 0x01 :0x04),
382                                                 0x00, addr);
383                                 }
384                                 else
385                                         cue_ent(&cue[j++],bt2ctl[tracks[i].block_type], 
386                                                 0x01, i+1, 0x0,
387                                                 bt2df[tracks[i].block_type],
388                                                 0x00, addr);
389                         }
390                         tracks[i].addr = tracks[i - 1].addr +
391                                 roundup_blocks(&tracks[i - 1]);
392
393                         cue_ent(&cue[j++], bt2ctl[tracks[i].block_type],
394                                 0x01, i+1, 0x1, bt2df[tracks[i].block_type],
395                                 0x00, addr + tracks[i].pregap);
396
397                         if (tracks[i].block_type > 0x7)
398                                 addr += tracks[i].pregap;
399                 }
400                 addr += roundup_blocks(&tracks[i]);
401         }
402
403         cue_ent(&cue[j++], bt2ctl[tracks[i - 1].block_type], 0x01, 0xaa, 0x01,
404                 (bt2df[tracks[i - 1].block_type] & 0xf0) | 
405                 (tracks[i - 1].block_type < 8 ? 0x01 : 0x04), 0x00, addr);
406
407         sheet.len = j * 8;
408         sheet.entries = cue;
409         sheet.test_write = test_write;
410         sheet.session_type = multi ? CDR_SESS_MULTI : CDR_SESS_NONE;
411         sheet.session_format = format;
412         if (verbose) {
413                 u_int8_t *ptr = (u_int8_t *)sheet.entries;
414                 
415                 fprintf(stderr,"CUE sheet:");
416                 for (i = 0; i < sheet.len; i++)
417                         if (i % 8)
418                                 fprintf(stderr," %02x", ptr[i]);
419                         else
420                                 fprintf(stderr,"\n%02x", ptr[i]);
421                 fprintf(stderr,"\n");
422         }
423         
424         if (ioctl(fd, CDRIOCSENDCUE, &sheet) < 0)
425                 err(EX_IOERR, "ioctl(CDRIOCSENDCUE)");
426
427         for (i = 0; i < notracks; i++) {
428                 if (write_file(&tracks[i]))
429                         err(EX_IOERR, "write_file");
430         }
431
432         ioctl(fd, CDRIOCFLUSH);
433 }
434
435 void
436 do_TAO(int test_write, int preemp)
437 {
438         struct cdr_track track;
439         int i;
440
441         for (i = 0; i < notracks; i++) {
442                 track.test_write = test_write;
443                 track.datablock_type = tracks[i].block_type;
444                 track.preemp = preemp;
445                 if (ioctl(fd, CDRIOCINITTRACK, &track) < 0)
446                         err(EX_IOERR, "ioctl(CDRIOCINITTRACK)");
447
448                 if (ioctl(fd, CDRIOCNEXTWRITEABLEADDR, &tracks[i].addr) < 0)
449                         err(EX_IOERR, "ioctl(CDRIOCNEXTWRITEABLEADDR)");
450                 if (!quiet)
451                         fprintf(stderr, "next writeable LBA %d\n",
452                                 tracks[i].addr);
453                 if (write_file(&tracks[i]))
454                         err(EX_IOERR, "write_file");
455                 if (ioctl(fd, CDRIOCFLUSH) < 0)
456                         err(EX_IOERR, "ioctl(CDRIOCFLUSH)");
457         }
458 }
459
460 int
461 write_file(struct track_info *track_info)
462 {
463         int size, count, filesize;
464         char buf[2352*BLOCKS];
465         static int tot_size = 0;
466
467         filesize = track_info->file_size / 1024;
468
469         if (ioctl(fd, CDRIOCSETBLOCKSIZE, &track_info->block_size) < 0)
470                 err(EX_IOERR, "ioctl(CDRIOCSETBLOCKSIZE)");
471
472         if (track_info->addr >= 0)
473                 lseek(fd, track_info->addr * track_info->block_size, SEEK_SET);
474
475         if (verbose)
476                 fprintf(stderr, "addr = %d size = %d blocks = %d\n",
477                         track_info->addr, track_info->file_size,
478                         roundup_blocks(track_info));
479
480         if (!quiet) {
481                 if (track_info->file == STDIN_FILENO)
482                         fprintf(stderr, "writing from stdin\n");
483                 else
484                         fprintf(stderr, 
485                                 "writing from file %s size %d KB\n",
486                                 track_info->file_name, filesize);
487         }
488         size = 0;
489         if (filesize == 0)
490                 filesize++;     /* cheat, avoid divide by zero */
491
492         while ((count = read(track_info->file, buf,
493                              MIN((track_info->file_size - size),
494                                  track_info->block_size * BLOCKS))) > 0) {      
495                 int res;
496
497                 if (count % track_info->block_size) {
498                         /* pad file to % block_size */
499                         bzero(&buf[count],
500                               (track_info->block_size * BLOCKS) - count);
501                         count = ((count / track_info->block_size) + 1) *
502                                 track_info->block_size;
503                 }
504                 if ((res = write(fd, buf, count)) != count) {
505                         fprintf(stderr, "\nonly wrote %d of %d bytes err=%d\n",
506                                 res, count, errno);
507                         break;
508                 }
509                 size += count;
510                 tot_size += count;
511                 if (!quiet) {
512                         int pct;
513
514                         fprintf(stderr, "written this track %d KB", size/1024);
515                         if (track_info->file != STDIN_FILENO) {
516                                 pct = (size / 1024) * 100 / filesize;
517                                 fprintf(stderr, " (%d%%)", pct);
518                         }
519                         fprintf(stderr, " total %d KB\r", tot_size/1024);
520                 }
521                 if (size >= track_info->file_size)
522                         break;
523         }
524
525         if (!quiet)
526                 fprintf(stderr, "\n");
527         close(track_info->file);
528         return 0;
529 }
530
531 int
532 roundup_blocks(struct track_info *track)
533 {
534         return ((track->file_size + track->block_size - 1) / track->block_size);
535 }
536
537 void
538 cue_ent(struct cdr_cue_entry *cue, int ctl, int adr, int track, int idx,
539         int dataform, int scms, int lba)
540 {
541         cue->adr = adr;
542         cue->ctl = ctl;
543         cue->track = track;
544         cue->index = idx;
545         cue->dataform = dataform;
546         cue->scms = scms;
547         lba += 150;
548         cue->min = lba / (60*75);
549         cue->sec = (lba % (60*75)) / 75;
550         cue->frame = (lba % (60*75)) % 75;
551 }
552
553 void
554 cleanup(int dummy __unused)
555 {
556         if (ioctl(fd, CDRIOCSETBLOCKSIZE, &saved_block_size) < 0) 
557                 err(EX_IOERR, "ioctl(CDRIOCSETBLOCKSIZE)");
558 }
559
560 void
561 usage(void)
562 {
563         fprintf(stderr,
564             "Usage: %s [-delmnpqtv] [-f device] [-s speed] [command]"
565             " [command file ...]\n", getprogname());
566         exit(EX_USAGE);
567 }