]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/burncd/burncd.c
This commit was generated by cvs2svn to compensate for changes in r95565,
[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 <unistd.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <err.h>
36 #include <sysexits.h>
37 #include <fcntl.h>
38 #include <sys/errno.h>
39 #include <sys/ioctl.h>
40 #include <sys/stat.h>
41 #include <sys/cdio.h>
42 #include <sys/cdrio.h>
43 #include <sys/param.h>
44 #include <arpa/inet.h>
45
46 #define BLOCKS  16
47
48 struct track_info {
49         int     file;
50         char    *file_name;
51         u_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         if (file == STDIN_FILENO)
308                 tracks[notracks].file_size = -1;
309         else
310                 tracks[notracks].file_size = sb.st_size;
311         tracks[notracks].block_size = block_size;
312         tracks[notracks].block_type = block_type;
313
314         if (nogap && notracks)
315                 tracks[notracks].pregap = 0;
316         else {
317                 if (tracks[notracks - (notracks > 0)].block_type == block_type)
318                         tracks[notracks].pregap = 150;
319                 else
320                         tracks[notracks].pregap = 255;
321         }
322
323         if (verbose) {
324                 int pad = 0;
325
326                 if (tracks[notracks].file_size / tracks[notracks].block_size !=
327                     roundup_blocks(&tracks[notracks]))
328                         pad = 1;
329                 fprintf(stderr, 
330                         "adding type 0x%02x file %s size %d KB %d blocks %s\n",
331                         tracks[notracks].block_type, name, (int)sb.st_size/1024,
332                         roundup_blocks(&tracks[notracks]),
333                         pad ? "(0 padded)" : "");
334         }
335         notracks++;
336 }
337
338 void
339 do_DAO(int test_write, int multi)
340 {
341         struct cdr_cuesheet sheet;
342         struct cdr_cue_entry cue[100];
343         int format = CDR_SESS_CDROM;
344         int addr, i, j = 0;
345
346         int bt2ctl[16] = { 0x0,  -1,  -1,  -1,  -1,  -1,  -1,  -1,
347                            0x4, 0x4, 0x4, 0x4, 0x4, 0x4,  -1,  -1 };
348
349         int bt2df[16] = { 0x0,    -1,   -1,   -1,   -1,   -1,   -1,   -1,
350                           0x10, 0x30, 0x20,   -1, 0x21,   -1,   -1,   -1 };
351         
352         if (ioctl(fd, CDRIOCNEXTWRITEABLEADDR, &addr) < 0) 
353                 err(EX_IOERR, "ioctl(CDRIOCNEXTWRITEABLEADDR)");
354         if (verbose)
355                 fprintf(stderr, "next writeable LBA %d\n", addr);
356
357         cue_ent(&cue[j++], bt2ctl[tracks[0].block_type], 0x01, 0x00, 0x0,
358                 (bt2df[tracks[0].block_type] & 0xf0) | 
359                 (tracks[0].block_type < 8 ? 0x01 : 0x04), 0x00, addr);
360
361         for (i = 0; i < notracks; i++) {
362                 if (bt2ctl[tracks[i].block_type] < 0 ||
363                     bt2df[tracks[i].block_type] < 0)
364                         err(EX_IOERR, "track type not supported in DAO mode");
365
366                 if (tracks[i].block_type >= CDR_DB_XA_MODE1)
367                         format = CDR_SESS_CDROM_XA;
368
369                 if (i == 0) {
370                         addr += tracks[i].pregap;
371                         tracks[i].addr = addr;
372
373                         cue_ent(&cue[j++], bt2ctl[tracks[i].block_type], 
374                                 0x01, i+1, 0x1, bt2df[tracks[i].block_type],
375                                 0x00, addr);
376
377                 }
378                 else {
379                         if (tracks[i].pregap) {
380                                 if (tracks[i].block_type > 0x7) {
381                                         cue_ent(&cue[j++],bt2ctl[tracks[i].block_type], 
382                                                 0x01, i+1, 0x0,
383                                                 (bt2df[tracks[i].block_type] & 0xf0) | 
384                                                 (tracks[i].block_type < 8 ? 0x01 :0x04),
385                                                 0x00, addr);
386                                 }
387                                 else
388                                         cue_ent(&cue[j++],bt2ctl[tracks[i].block_type], 
389                                                 0x01, i+1, 0x0,
390                                                 bt2df[tracks[i].block_type],
391                                                 0x00, addr);
392                         }
393                         tracks[i].addr = tracks[i - 1].addr +
394                                 roundup_blocks(&tracks[i - 1]);
395
396                         cue_ent(&cue[j++], bt2ctl[tracks[i].block_type],
397                                 0x01, i+1, 0x1, bt2df[tracks[i].block_type],
398                                 0x00, addr + tracks[i].pregap);
399
400                         if (tracks[i].block_type > 0x7)
401                                 addr += tracks[i].pregap;
402                 }
403                 addr += roundup_blocks(&tracks[i]);
404         }
405
406         cue_ent(&cue[j++], bt2ctl[tracks[i - 1].block_type], 0x01, 0xaa, 0x01,
407                 (bt2df[tracks[i - 1].block_type] & 0xf0) | 
408                 (tracks[i - 1].block_type < 8 ? 0x01 : 0x04), 0x00, addr);
409
410         sheet.len = j * 8;
411         sheet.entries = cue;
412         sheet.test_write = test_write;
413         sheet.session_type = multi ? CDR_SESS_MULTI : CDR_SESS_NONE;
414         sheet.session_format = format;
415         if (verbose) {
416                 u_int8_t *ptr = (u_int8_t *)sheet.entries;
417                 
418                 fprintf(stderr,"CUE sheet:");
419                 for (i = 0; i < sheet.len; i++)
420                         if (i % 8)
421                                 fprintf(stderr," %02x", ptr[i]);
422                         else
423                                 fprintf(stderr,"\n%02x", ptr[i]);
424                 fprintf(stderr,"\n");
425         }
426         
427         if (ioctl(fd, CDRIOCSENDCUE, &sheet) < 0)
428                 err(EX_IOERR, "ioctl(CDRIOCSENDCUE)");
429
430         for (i = 0; i < notracks; i++) {
431                 if (write_file(&tracks[i]))
432                         err(EX_IOERR, "write_file");
433         }
434
435         ioctl(fd, CDRIOCFLUSH);
436 }
437
438 void
439 do_TAO(int test_write, int preemp)
440 {
441         struct cdr_track track;
442         int i;
443
444         for (i = 0; i < notracks; i++) {
445                 track.test_write = test_write;
446                 track.datablock_type = tracks[i].block_type;
447                 track.preemp = preemp;
448                 if (ioctl(fd, CDRIOCINITTRACK, &track) < 0)
449                         err(EX_IOERR, "ioctl(CDRIOCINITTRACK)");
450
451                 if (ioctl(fd, CDRIOCNEXTWRITEABLEADDR, &tracks[i].addr) < 0)
452                         err(EX_IOERR, "ioctl(CDRIOCNEXTWRITEABLEADDR)");
453                 if (!quiet)
454                         fprintf(stderr, "next writeable LBA %d\n",
455                                 tracks[i].addr);
456                 if (write_file(&tracks[i]))
457                         err(EX_IOERR, "write_file");
458                 if (ioctl(fd, CDRIOCFLUSH) < 0)
459                         err(EX_IOERR, "ioctl(CDRIOCFLUSH)");
460         }
461 }
462
463 int
464 write_file(struct track_info *track_info)
465 {
466         int size, count, filesize;
467         char buf[2352*BLOCKS];
468         static int tot_size = 0;
469
470         filesize = track_info->file_size / 1024;
471
472         if (ioctl(fd, CDRIOCSETBLOCKSIZE, &track_info->block_size) < 0)
473                 err(EX_IOERR, "ioctl(CDRIOCSETBLOCKSIZE)");
474
475         if (track_info->addr >= 0)
476                 lseek(fd, track_info->addr * track_info->block_size, SEEK_SET);
477
478         if (verbose)
479                 fprintf(stderr, "addr = %d size = %d blocks = %d\n",
480                         track_info->addr, track_info->file_size,
481                         roundup_blocks(track_info));
482
483         if (!quiet) {
484                 if (track_info->file == STDIN_FILENO)
485                         fprintf(stderr, "writing from stdin\n");
486                 else
487                         fprintf(stderr, 
488                                 "writing from file %s size %d KB\n",
489                                 track_info->file_name, filesize);
490         }
491         size = 0;
492
493         while ((count = read(track_info->file, buf,
494                              MIN((track_info->file_size - size),
495                                  track_info->block_size * BLOCKS))) > 0) {      
496                 int res;
497
498                 if (count % track_info->block_size) {
499                         /* pad file to % block_size */
500                         bzero(&buf[count],
501                               (track_info->block_size * BLOCKS) - count);
502                         count = ((count / track_info->block_size) + 1) *
503                                 track_info->block_size;
504                 }
505                 if ((res = write(fd, buf, count)) != count) {
506                         fprintf(stderr, "\nonly wrote %d of %d bytes err=%d\n",
507                                 res, count, errno);
508                         break;
509                 }
510                 size += count;
511                 tot_size += count;
512                 if (!quiet) {
513                         int pct;
514
515                         fprintf(stderr, "written this track %d KB", size/1024);
516                         if (track_info->file != STDIN_FILENO && filesize) {
517                                 pct = (size / 1024) * 100 / filesize;
518                                 fprintf(stderr, " (%d%%)", pct);
519                         }
520                         fprintf(stderr, " total %d KB\r", tot_size/1024);
521                 }
522                 if (size >= track_info->file_size)
523                         break;
524         }
525
526         if (!quiet)
527                 fprintf(stderr, "\n");
528         close(track_info->file);
529         return 0;
530 }
531
532 int
533 roundup_blocks(struct track_info *track)
534 {
535         return ((track->file_size + track->block_size - 1) / track->block_size);
536 }
537
538 void
539 cue_ent(struct cdr_cue_entry *cue, int ctl, int adr, int track, int idx,
540         int dataform, int scms, int lba)
541 {
542         cue->adr = adr;
543         cue->ctl = ctl;
544         cue->track = track;
545         cue->index = idx;
546         cue->dataform = dataform;
547         cue->scms = scms;
548         lba += 150;
549         cue->min = lba / (60*75);
550         cue->sec = (lba % (60*75)) / 75;
551         cue->frame = (lba % (60*75)) % 75;
552 }
553
554 void
555 cleanup(int dummy __unused)
556 {
557         if (ioctl(fd, CDRIOCSETBLOCKSIZE, &saved_block_size) < 0) 
558                 err(EX_IOERR, "ioctl(CDRIOCSETBLOCKSIZE)");
559 }
560
561 void
562 usage(void)
563 {
564         fprintf(stderr,
565             "usage: %s [-delmnpqtv] [-f device] [-s speed] [command]"
566             " [command file ...]\n", getprogname());
567         exit(EX_USAGE);
568 }