newfs_udf.c revision 1.1.4.2 1 /* $NetBSD: newfs_udf.c,v 1.1.4.2 2008/06/02 13:21:23 mjf Exp $ */
2
3 /*
4 * Copyright (c) 2006, 2008 Reinoud Zandijk
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 *
27 */
28
29 /*
30 * TODO
31 * - implement meta data partition formatting.
32 * - implement support for a read-only companion partition?
33 */
34
35 #define _EXPOSE_MMC
36 #if 0
37 # define DEBUG
38 #endif
39
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <dirent.h>
43 #include <inttypes.h>
44 #include <stdint.h>
45 #include <string.h>
46 #include <errno.h>
47 #include <fcntl.h>
48 #include <unistd.h>
49 #include <util.h>
50 #include <fattr.h>
51 #include <time.h>
52 #include <assert.h>
53
54 #include <sys/ioctl.h>
55 #include <sys/stat.h>
56 #include <sys/types.h>
57 #include <sys/cdio.h>
58 #include <sys/disklabel.h>
59 #include <sys/dkio.h>
60 #include <sys/param.h>
61 #include <sys/queue.h>
62
63 #include <fs/udf/ecma167-udf.h>
64 #include <fs/udf/udf_mount.h>
65 #include "udf_create.h"
66
67 /* general settings */
68 #define UDF_512_TRACK 0 /* NOT recommended */
69
70 /* prototypes */
71 int newfs_udf(int argc, char **argv);
72 static void usage(void) __attribute__((__noreturn__));
73
74 int udf_derive_format(int req_en, int req_dis, int force);
75 int udf_proces_names(void);
76 int udf_do_newfs(void);
77
78 /* Identifying myself */
79 #define APP_NAME "*NetBSD newfs"
80 #define APP_VERSION_MAIN 0
81 #define APP_VERSION_SUB 2
82 #define IMPL_NAME "*NetBSD userland UDF"
83
84
85 /* global variables describing disc and format requests */
86 int fd; /* device: file descriptor */
87 char *dev; /* device: name */
88 struct mmc_discinfo mmc_discinfo; /* device: disc info */
89
90 char *format_str; /* format: string representation */
91 int format_flags; /* format: attribute flags */
92 int media_accesstype; /* derived from current mmc cap */
93 int check_surface; /* for rewritables */
94
95 int wrtrack_skew;
96
97
98 /* shared structure between udf_create.c users */
99 struct udf_create_context context;
100 struct udf_disclayout layout;
101
102
103 /* queue for temporary storage of sectors to be written out */
104 struct wrsect {
105 uint32_t sectornr;
106 uint8_t *sector_data;
107 TAILQ_ENTRY(wrsect) next;
108 };
109
110 /* write queue and track blocking skew */
111 TAILQ_HEAD(wrsect_list, wrsect) write_queue;
112
113
114 /* --------------------------------------------------------------------- */
115
116 /*
117 * write queue implementation
118 */
119
120 static int
121 udf_write_sector(void *sector, uint32_t location)
122 {
123 struct wrsect *pos, *seekpos;
124
125
126 /* search location */
127 TAILQ_FOREACH_REVERSE(seekpos, &write_queue, wrsect_list, next) {
128 if (seekpos->sectornr <= location)
129 break;
130 }
131 if ((seekpos == NULL) || (seekpos->sectornr != location)) {
132 pos = calloc(1, sizeof(struct wrsect));
133 if (pos == NULL)
134 return ENOMEM;
135 /* allocate space for copy of sector data */
136 pos->sector_data = calloc(1, context.sector_size);
137 if (pos->sector_data == NULL)
138 return ENOMEM;
139 pos->sectornr = location;
140
141 if (seekpos) {
142 TAILQ_INSERT_AFTER(&write_queue, seekpos, pos, next);
143 } else {
144 TAILQ_INSERT_HEAD(&write_queue, pos, next);
145 }
146 } else {
147 pos = seekpos;
148 }
149 memcpy(pos->sector_data, sector, context.sector_size);
150
151 return 0;
152 }
153
154
155 /*
156 * Now all write requests are queued in the TAILQ, write them out to the
157 * disc/file image. Special care needs to be taken for devices that are only
158 * strict overwritable i.e. only in packet size chunks
159 *
160 * XXX support for growing vnd?
161 */
162
163 static int
164 writeout_write_queue(void)
165 {
166 struct wrsect *pos;
167 uint64_t offset;
168 uint32_t line_len, line_offset;
169 uint32_t line_start, new_line_start, relpos;
170 uint32_t blockingnr;
171 uint8_t *linebuf, *adr;
172
173 blockingnr = layout.blockingnr;
174 line_len = blockingnr * context.sector_size;
175 line_offset = wrtrack_skew * context.sector_size;
176
177 linebuf = malloc(line_len);
178 if (linebuf == NULL)
179 return ENOMEM;
180
181 pos = TAILQ_FIRST(&write_queue);
182 bzero(linebuf, line_len);
183
184 /*
185 * Always writing out in whole lines now; this is slightly wastefull
186 * on logical overwrite volumes but it reduces complexity and the loss
187 * is near zero compared to disc size.
188 */
189 line_start = (pos->sectornr - wrtrack_skew) / blockingnr;
190 TAILQ_FOREACH(pos, &write_queue, next) {
191 new_line_start = (pos->sectornr - wrtrack_skew) / blockingnr;
192 if (new_line_start != line_start) {
193 /* write out */
194 offset = (uint64_t) line_start * line_len + line_offset;
195 #ifdef DEBUG
196 printf("WRITEOUT %08"PRIu64" + %02d -- "
197 "[%08"PRIu64"..%08"PRIu64"]\n",
198 offset / context.sector_size, blockingnr,
199 offset / context.sector_size,
200 offset / context.sector_size + blockingnr-1);
201 #endif
202 if (pwrite(fd, linebuf, line_len, offset) < 0) {
203 perror("Writing failed");
204 return errno;
205 }
206 line_start = new_line_start;
207 bzero(linebuf, line_len);
208 }
209
210 relpos = (pos->sectornr - wrtrack_skew) % blockingnr;
211 adr = linebuf + relpos * context.sector_size;
212 memcpy(adr, pos->sector_data, context.sector_size);
213 }
214 /* writeout last chunk */
215 offset = (uint64_t) line_start * line_len + line_offset;
216 #ifdef DEBUG
217 printf("WRITEOUT %08"PRIu64" + %02d -- [%08"PRIu64"..%08"PRIu64"]\n",
218 offset / context.sector_size, blockingnr,
219 offset / context.sector_size,
220 offset / context.sector_size + blockingnr-1);
221 #endif
222 if (pwrite(fd, linebuf, line_len, offset) < 0) {
223 perror("Writing failed");
224 return errno;
225 }
226
227 /* success */
228 return 0;
229 }
230
231 /* --------------------------------------------------------------------- */
232
233 /*
234 * mmc_discinfo and mmc_trackinfo readers modified from origional in udf main
235 * code in sys/fs/udf/
236 */
237
238 #ifdef DEBUG
239 static void
240 udf_dump_discinfo(struct mmc_discinfo *di)
241 {
242 char bits[128];
243
244 printf("Device/media info :\n");
245 printf("\tMMC profile 0x%02x\n", di->mmc_profile);
246 printf("\tderived class %d\n", di->mmc_class);
247 printf("\tsector size %d\n", di->sector_size);
248 printf("\tdisc state %d\n", di->disc_state);
249 printf("\tlast ses state %d\n", di->last_session_state);
250 printf("\tbg format state %d\n", di->bg_format_state);
251 printf("\tfrst track %d\n", di->first_track);
252 printf("\tfst on last ses %d\n", di->first_track_last_session);
253 printf("\tlst on last ses %d\n", di->last_track_last_session);
254 printf("\tlink block penalty %d\n", di->link_block_penalty);
255 snprintb(bits, sizeof(bits), MMC_DFLAGS_FLAGBITS, (uint64_t) di->disc_flags);
256 printf("\tdisc flags %s\n", bits);
257 printf("\tdisc id %x\n", di->disc_id);
258 printf("\tdisc barcode %"PRIx64"\n", di->disc_barcode);
259
260 printf("\tnum sessions %d\n", di->num_sessions);
261 printf("\tnum tracks %d\n", di->num_tracks);
262
263 snprintb(bits, sizeof(bits), MMC_CAP_FLAGBITS, di->mmc_cur);
264 printf("\tcapabilities cur %s\n", bits);
265 snprintb(bits, sizeof(bits), MMC_CAP_FLAGBITS, di->mmc_cap);
266 printf("\tcapabilities cap %s\n", bits);
267 printf("\n");
268 printf("\tlast_possible_lba %d\n", di->last_possible_lba);
269 printf("\n");
270 }
271 #else
272 #define udf_dump_discinfo(a);
273 #endif
274
275 /* --------------------------------------------------------------------- */
276
277 static int
278 udf_update_discinfo(struct mmc_discinfo *di)
279 {
280 struct disklabel disklab;
281 struct partition *dp;
282 struct stat st;
283 int partnr, error;
284
285 memset(di, 0, sizeof(struct mmc_discinfo));
286
287 /* check if we're on a MMC capable device, i.e. CD/DVD */
288 error = ioctl(fd, MMCGETDISCINFO, di);
289 if (error == 0)
290 return 0;
291
292 /*
293 * disc partition support; note we can't use DIOCGPART in userland so
294 * get disc label and use the stat info to get the partition number.
295 */
296 if (ioctl(fd, DIOCGDINFO, &disklab) == -1) {
297 /* failed to get disclabel! */
298 perror("disklabel");
299 return errno;
300 }
301
302 /* get disk partition it refers to */
303 fstat(fd, &st);
304 partnr = DISKPART(st.st_rdev);
305 dp = &disklab.d_partitions[partnr];
306
307 /* set up a disc info profile for partitions */
308 di->mmc_profile = 0x01; /* disc type */
309 di->mmc_class = MMC_CLASS_DISC;
310 di->disc_state = MMC_STATE_CLOSED;
311 di->last_session_state = MMC_STATE_CLOSED;
312 di->bg_format_state = MMC_BGFSTATE_COMPLETED;
313 di->link_block_penalty = 0;
314
315 di->mmc_cur = MMC_CAP_RECORDABLE | MMC_CAP_REWRITABLE |
316 MMC_CAP_ZEROLINKBLK | MMC_CAP_HW_DEFECTFREE;
317 di->mmc_cap = di->mmc_cur;
318 di->disc_flags = MMC_DFLAGS_UNRESTRICTED;
319
320 /* TODO problem with last_possible_lba on resizable VND; request */
321 if (dp->p_size == 0) {
322 perror("faulty disklabel partition returned, check label\n");
323 return EIO;
324 }
325 di->last_possible_lba = dp->p_size - 1;
326 di->sector_size = disklab.d_secsize;
327
328 di->num_sessions = 1;
329 di->num_tracks = 1;
330
331 di->first_track = 1;
332 di->first_track_last_session = di->last_track_last_session = 1;
333
334 return 0;
335 }
336
337
338 static int
339 udf_update_trackinfo(struct mmc_discinfo *di, struct mmc_trackinfo *ti)
340 {
341 int error, class;
342
343 class = di->mmc_class;
344 if (class != MMC_CLASS_DISC) {
345 /* tracknr specified in struct ti */
346 error = ioctl(fd, MMCGETTRACKINFO, ti);
347 return error;
348 }
349
350 /* discs partition support */
351 if (ti->tracknr != 1)
352 return EIO;
353
354 /* create fake ti (TODO check for resized vnds) */
355 ti->sessionnr = 1;
356
357 ti->track_mode = 0; /* XXX */
358 ti->data_mode = 0; /* XXX */
359 ti->flags = MMC_TRACKINFO_LRA_VALID | MMC_TRACKINFO_NWA_VALID;
360
361 ti->track_start = 0;
362 ti->packet_size = 1;
363
364 /* TODO support for resizable vnd */
365 ti->track_size = di->last_possible_lba;
366 ti->next_writable = di->last_possible_lba;
367 ti->last_recorded = ti->next_writable;
368 ti->free_blocks = 0;
369
370 return 0;
371 }
372
373
374 static int
375 udf_setup_writeparams(struct mmc_discinfo *di)
376 {
377 struct mmc_writeparams mmc_writeparams;
378 int error;
379
380 if (di->mmc_class == MMC_CLASS_DISC)
381 return 0;
382
383 /*
384 * only CD burning normally needs setting up, but other disc types
385 * might need other settings to be made. The MMC framework will set up
386 * the nessisary recording parameters according to the disc
387 * characteristics read in. Modifications can be made in the discinfo
388 * structure passed to change the nature of the disc.
389 */
390 memset(&mmc_writeparams, 0, sizeof(struct mmc_writeparams));
391 mmc_writeparams.mmc_class = di->mmc_class;
392 mmc_writeparams.mmc_cur = di->mmc_cur;
393
394 /*
395 * UDF dictates first track to determine track mode for the whole
396 * disc. [UDF 1.50/6.10.1.1, UDF 1.50/6.10.2.1]
397 * To prevent problems with a `reserved' track in front we start with
398 * the 2nd track and if that is not valid, go for the 1st.
399 */
400 mmc_writeparams.tracknr = 2;
401 mmc_writeparams.data_mode = MMC_DATAMODE_DEFAULT; /* XA disc */
402 mmc_writeparams.track_mode = MMC_TRACKMODE_DEFAULT; /* data */
403
404 error = ioctl(fd, MMCSETUPWRITEPARAMS, &mmc_writeparams);
405 if (error) {
406 mmc_writeparams.tracknr = 1;
407 error = ioctl(fd, MMCSETUPWRITEPARAMS, &mmc_writeparams);
408 }
409 return error;
410 }
411
412
413 static void
414 udf_synchronise_caches(void)
415 {
416 struct mmc_op mmc_op;
417
418 bzero(&mmc_op, sizeof(struct mmc_op));
419 mmc_op.operation = MMC_OP_SYNCHRONISECACHE;
420
421 /* this device might not know this ioct, so just be ignorant */
422 (void) ioctl(fd, MMCOP, &mmc_op);
423 }
424
425 /* --------------------------------------------------------------------- */
426
427 static int
428 udf_write_dscr_phys(union dscrptr *dscr, uint32_t location,
429 uint32_t sects)
430 {
431 uint32_t phys;
432 uint8_t *bpos;
433 int error, cnt;
434
435 dscr->tag.tag_loc = udf_rw32(location);
436 (void) udf_validate_tag_and_crc_sums(dscr);
437
438 for (cnt = 0; cnt < sects; cnt++) {
439 bpos = (uint8_t *) dscr;
440 bpos += context.sector_size * cnt;
441
442 phys = location + cnt;
443 error = udf_write_sector(bpos, phys);
444 if (error)
445 return error;
446 }
447 return 0;
448 }
449
450
451 static int
452 udf_write_dscr_virt(union dscrptr *dscr, uint32_t location, uint32_t vpart,
453 uint32_t sects)
454 {
455 uint32_t phys;
456 uint8_t *bpos;
457 int error, cnt;
458
459 dscr->tag.tag_loc = udf_rw32(location);
460 (void) udf_validate_tag_and_crc_sums(dscr);
461
462 for (cnt = 0; cnt < sects; cnt++) {
463 bpos = (uint8_t *) dscr;
464 bpos += context.sector_size * cnt;
465
466 /* NOTE liniar mapping assumed in the ranges used */
467 /* XXX 1:1 translation */
468 phys = layout.part_start_lba + location + cnt;
469
470 error = udf_write_sector(bpos, phys);
471 if (error)
472 return error;
473 }
474 return 0;
475 }
476
477 /* --------------------------------------------------------------------- */
478
479 /*
480 * udf_derive_format derives the format_flags from the disc's mmc_discinfo.
481 * The resulting flags uniquely define a disc format. Note there are at least
482 * 7 distinct format types defined in UDF.
483 */
484
485 #define UDF_VERSION(a) \
486 (((a) == 0x100) || ((a) == 0x102) || ((a) == 0x150) || ((a) == 0x200) || \
487 ((a) == 0x201) || ((a) == 0x250) || ((a) == 0x260))
488
489 int
490 udf_derive_format(int req_enable, int req_disable, int force)
491 {
492 /* disc writability, formatted, appendable */
493 if ((mmc_discinfo.mmc_cur & MMC_CAP_RECORDABLE) == 0) {
494 (void)printf("Can't newfs readonly device\n");
495 return EROFS;
496 }
497 if (mmc_discinfo.mmc_cur & MMC_CAP_SEQUENTIAL) {
498 /* sequentials need sessions appended */
499 if (mmc_discinfo.disc_state == MMC_STATE_CLOSED) {
500 (void)printf("Can't append session to a closed disc\n");
501 return EROFS;
502 }
503 if ((mmc_discinfo.disc_state != MMC_STATE_EMPTY) && !force) {
504 (void)printf("Disc not empty! Use -F to force "
505 "initialisation\n");
506 return EROFS;
507 }
508 } else {
509 /* check if disc (being) formatted or has been started on */
510 if (mmc_discinfo.disc_state == MMC_STATE_EMPTY) {
511 (void)printf("Disc is not formatted\n");
512 return EROFS;
513 }
514 }
515
516 /* determine UDF format */
517 format_flags = 0;
518 if (mmc_discinfo.mmc_cur & MMC_CAP_REWRITABLE) {
519 /* all rewritable media */
520 format_flags |= FORMAT_REWRITABLE;
521 if (context.min_udf >= 0x0250) {
522 /* standard dictates meta as default */
523 format_flags |= FORMAT_META;
524 }
525
526 if ((mmc_discinfo.mmc_cur & MMC_CAP_HW_DEFECTFREE) == 0) {
527 /* sparables for defect management */
528 if (context.min_udf >= 0x150)
529 format_flags |= FORMAT_SPARABLE;
530 }
531 } else {
532 /* all once recordable media */
533 format_flags |= FORMAT_WRITEONCE;
534 if (mmc_discinfo.mmc_cur & MMC_CAP_SEQUENTIAL) {
535 format_flags |= FORMAT_SEQUENTIAL;
536
537 if (mmc_discinfo.mmc_cur & MMC_CAP_PSEUDOOVERWRITE) {
538 /* logical overwritable */
539 format_flags |= FORMAT_LOW;
540 } else {
541 /* have to use VAT for overwriting */
542 format_flags |= FORMAT_VAT;
543 }
544 } else {
545 /* rare WORM devices, but BluRay has one, strat4096 */
546 format_flags |= FORMAT_WORM;
547 }
548 }
549
550 /* enable/disable requests */
551 if (req_disable & FORMAT_META) {
552 format_flags &= ~FORMAT_META;
553 req_disable &= ~FORMAT_META;
554 }
555 if (req_disable || req_enable) {
556 (void)printf("Internal error\n");
557 (void)printf("\tunrecognised enable/disable req.\n");
558 return EIO;
559 }
560 if ((format_flags && FORMAT_VAT) && UDF_512_TRACK)
561 format_flags |= FORMAT_TRACK512;
562
563 /* determine partition/media access type */
564 media_accesstype = UDF_ACCESSTYPE_NOT_SPECIFIED;
565 if (mmc_discinfo.mmc_cur & MMC_CAP_REWRITABLE) {
566 media_accesstype = UDF_ACCESSTYPE_OVERWRITABLE;
567 if (mmc_discinfo.mmc_cur & MMC_CAP_ERASABLE)
568 media_accesstype = UDF_ACCESSTYPE_REWRITEABLE;
569 } else {
570 /* all once recordable media */
571 media_accesstype = UDF_ACCESSTYPE_WRITE_ONCE;
572 }
573 if (mmc_discinfo.mmc_cur & MMC_CAP_PSEUDOOVERWRITE)
574 media_accesstype = UDF_ACCESSTYPE_PSEUDO_OVERWITE;
575
576 /* adjust minimum version limits */
577 if (format_flags & FORMAT_VAT)
578 context.min_udf = MAX(context.min_udf, 0x0150);
579 if (format_flags & FORMAT_SPARABLE)
580 context.min_udf = MAX(context.min_udf, 0x0150);
581 if (format_flags & FORMAT_META)
582 context.min_udf = MAX(context.min_udf, 0x0250);
583 if (format_flags & FORMAT_LOW)
584 context.min_udf = MAX(context.min_udf, 0x0260);
585
586 /* adjust maximum version limits not to tease or break things */
587 if (!(format_flags & FORMAT_META) && (context.max_udf > 0x200))
588 context.max_udf = 0x201;
589
590 if ((format_flags & (FORMAT_VAT | FORMAT_SPARABLE)) == 0)
591 if (context.max_udf <= 0x150)
592 context.min_udf = 0x102;
593
594 /* limit Ecma 167 descriptor if possible/needed */
595 context.dscrver = 3;
596 if ((context.min_udf < 0x200) || (context.max_udf < 0x200)) {
597 context.dscrver = 2;
598 context.max_udf = 0x150; /* last version < 0x200 */
599 }
600
601 /* is it possible ? */
602 if (context.min_udf > context.max_udf) {
603 (void)printf("Initialisation prohibited by specified maximum "
604 "UDF version 0x%04x. Minimum version required 0x%04x\n",
605 context.max_udf, context.min_udf);
606 return EPERM;
607 }
608
609 if (!UDF_VERSION(context.min_udf) || !UDF_VERSION(context.max_udf)) {
610 printf("Choose UDF version numbers from "
611 "0x102, 0x150, 0x200, 0x201, 0x250 and 0x260\n");
612 printf("Default version is 0x201\n");
613 return EPERM;
614 }
615
616 return 0;
617 }
618
619 #undef UDF_VERSION
620
621
622 /* --------------------------------------------------------------------- */
623
624 int
625 udf_proces_names(void)
626 {
627 uint32_t primary_nr;
628 uint64_t volset_nr;
629
630 if (context.logvol_name == NULL)
631 context.logvol_name = strdup("anonymous");
632 if (context.primary_name == NULL) {
633 if (mmc_discinfo.disc_flags & MMC_DFLAGS_DISCIDVALID) {
634 primary_nr = mmc_discinfo.disc_id;
635 } else {
636 primary_nr = (uint32_t) random();
637 }
638 context.primary_name = calloc(32, 1);
639 sprintf(context.primary_name, "%08"PRIx32, primary_nr);
640 }
641 if (context.volset_name == NULL) {
642 if (mmc_discinfo.disc_flags & MMC_DFLAGS_BARCODEVALID) {
643 volset_nr = mmc_discinfo.disc_barcode;
644 } else {
645 volset_nr = (uint32_t) random();
646 volset_nr |= ((uint64_t) random()) << 32;
647 }
648 context.volset_name = calloc(128,1);
649 sprintf(context.volset_name, "%016"PRIx64, volset_nr);
650 }
651 if (context.fileset_name == NULL)
652 context.fileset_name = strdup("anonymous");
653
654 /* check passed/created identifiers */
655 if (strlen(context.logvol_name) > 128) {
656 (void)printf("Logical volume name too long\n");
657 return EINVAL;
658 }
659 if (strlen(context.primary_name) > 32) {
660 (void)printf("Primary volume name too long\n");
661 return EINVAL;
662 }
663 if (strlen(context.volset_name) > 128) {
664 (void)printf("Volume set name too long\n");
665 return EINVAL;
666 }
667 if (strlen(context.fileset_name) > 32) {
668 (void)printf("Fileset name too long\n");
669 return EINVAL;
670 }
671
672 /* signal all OK */
673 return 0;
674 }
675
676 /* --------------------------------------------------------------------- */
677
678 static int
679 udf_prepare_disc(void)
680 {
681 struct mmc_trackinfo ti;
682 struct mmc_op op;
683 int tracknr, error;
684
685 /* If the last track is damaged, repair it */
686 ti.tracknr = mmc_discinfo.last_track_last_session;
687 error = udf_update_trackinfo(&mmc_discinfo, &ti);
688 if (error)
689 return error;
690
691 if (ti.flags & MMC_TRACKINFO_DAMAGED) {
692 /*
693 * Need to repair last track before anything can be done.
694 * this is an optional command, so ignore its error but report
695 * warning.
696 */
697 memset(&op, 0, sizeof(op));
698 op.operation = MMC_OP_REPAIRTRACK;
699 op.mmc_profile = mmc_discinfo.mmc_profile;
700 op.tracknr = ti.tracknr;
701 error = ioctl(fd, MMCOP, &op);
702
703 if (error)
704 (void)printf("Drive can't explicitly repair last "
705 "damaged track, but it might autorepair\n");
706 }
707 /* last track (if any) might not be damaged now, operations are ok now */
708
709 /* setup write parameters from discinfo */
710 error = udf_setup_writeparams(&mmc_discinfo);
711 if (error)
712 return error;
713
714 /* if the drive is not sequential, we're done */
715 if ((mmc_discinfo.mmc_cur & MMC_CAP_SEQUENTIAL) == 0)
716 return 0;
717
718 #ifdef notyet
719 /* if last track is not the reserved but an empty track, unreserve it */
720 if (ti.flags & MMC_TRACKINFO_BLANK) {
721 if (ti.flags & MMC_TRACKINFO_RESERVED == 0) {
722 memset(&op, 0, sizeof(op));
723 op.operation = MMC_OP_UNRESERVETRACK;
724 op.mmc_profile = mmc_discinfo.mmc_profile;
725 op.tracknr = ti.tracknr;
726 error = ioctl(fd, MMCOP, &op);
727 if (error)
728 return error;
729
730 /* update discinfo since it changed by the operation */
731 error = udf_update_discinfo(&mmc_discinfo);
732 if (error)
733 return error;
734 }
735 }
736 #endif
737
738 /* close the last session if its still open */
739 if (mmc_discinfo.last_session_state == MMC_STATE_INCOMPLETE) {
740 printf("Closing last open session if present\n");
741 /* close all associated tracks */
742 tracknr = mmc_discinfo.first_track_last_session;
743 while (tracknr <= mmc_discinfo.last_track_last_session) {
744 ti.tracknr = tracknr;
745 error = udf_update_trackinfo(&mmc_discinfo, &ti);
746 if (error)
747 return error;
748 printf("\tClosing open track %d\n", tracknr);
749 memset(&op, 0, sizeof(op));
750 op.operation = MMC_OP_CLOSETRACK;
751 op.mmc_profile = mmc_discinfo.mmc_profile;
752 op.tracknr = tracknr;
753 error = ioctl(fd, MMCOP, &op);
754 if (error)
755 return error;
756 tracknr ++;
757 }
758 printf("Closing session\n");
759 memset(&op, 0, sizeof(op));
760 op.operation = MMC_OP_CLOSESESSION;
761 op.mmc_profile = mmc_discinfo.mmc_profile;
762 op.sessionnr = mmc_discinfo.num_sessions;
763 error = ioctl(fd, MMCOP, &op);
764 if (error)
765 return error;
766
767 /* update discinfo since it changed by the operations */
768 error = udf_update_discinfo(&mmc_discinfo);
769 if (error)
770 return error;
771 }
772
773 if (format_flags & FORMAT_TRACK512) {
774 /* get last track again */
775 ti.tracknr = mmc_discinfo.last_track_last_session;
776 error = udf_update_trackinfo(&mmc_discinfo, &ti);
777 if (error)
778 return error;
779
780 /* Split up the space at 512 for iso cd9660 hooking */
781 memset(&op, 0, sizeof(op));
782 op.operation = MMC_OP_RESERVETRACK_NWA; /* UPTO nwa */
783 op.mmc_profile = mmc_discinfo.mmc_profile;
784 op.extent = 512; /* size */
785 error = ioctl(fd, MMCOP, &op);
786 if (error)
787 return error;
788 }
789
790 return 0;
791 }
792
793 /* --------------------------------------------------------------------- */
794
795 static int
796 udf_surface_check(void)
797 {
798 uint32_t loc, block_bytes;
799 uint32_t sector_size, blockingnr;
800 uint8_t *buffer;
801 int error, num_errors;
802 int bpos;
803
804 sector_size = context.sector_size;
805 blockingnr = layout.blockingnr;
806
807 block_bytes = layout.blockingnr * sector_size;
808 if ((buffer = malloc(block_bytes)) == NULL)
809 return ENOMEM;
810
811 /* set all one to not kill Flash memory? */
812 for (bpos = 0; bpos < block_bytes; bpos++)
813 buffer[bpos] = 0x00;
814
815 printf("\nChecking disc surface : phase 1 - writing\n");
816 num_errors = 0;
817 loc = layout.first_lba;
818 while (loc <= layout.last_lba) {
819 /* write blockingnr sectors */
820 error = pwrite(fd, buffer, block_bytes, loc*sector_size);
821 printf(" %08d + %d (%02d %%)\r", loc, blockingnr,
822 (int)((100.0 * loc)/layout.last_lba));
823 fflush(stdout);
824 if (error == -1) {
825 /* block is bad */
826 printf("BAD block at %08d + %d \n",
827 loc, layout.blockingnr);
828 if ((error = udf_register_bad_block(loc)))
829 return error;
830 num_errors ++;
831 }
832 loc += layout.blockingnr;
833 }
834
835 printf("\nChecking disc surface : phase 2 - reading\n");
836 num_errors = 0;
837 loc = layout.first_lba;
838 while (loc <= layout.last_lba) {
839 /* read blockingnr sectors */
840 error = pread(fd, buffer, block_bytes, loc*sector_size);
841 printf(" %08d + %d (%02d %%)\r", loc, blockingnr,
842 (int)((100.0 * loc)/layout.last_lba));
843 fflush(stdout);
844 if (error == -1) {
845 /* block is bad */
846 printf("BAD block at %08d + %d \n",
847 loc, layout.blockingnr);
848 if ((error = udf_register_bad_block(loc)))
849 return error;
850 num_errors ++;
851 }
852 loc += layout.blockingnr;
853 }
854 printf("Scan complete : %d bad blocks found\n", num_errors);
855 free(buffer);
856
857 return 0;
858 }
859
860 /* --------------------------------------------------------------------- */
861
862 static int
863 udf_write_iso9660_vrs(void)
864 {
865 struct vrs_desc *iso9660_vrs_desc;
866 uint32_t pos;
867 int error, cnt, dpos;
868
869 /* create ISO/Ecma-167 identification descriptors */
870 if ((iso9660_vrs_desc = calloc(1, context.sector_size)) == NULL)
871 return ENOMEM;
872
873 /*
874 * All UDF formats should have their ISO/Ecma-167 descriptors written
875 * except when not possible due to track reservation in the case of
876 * VAT
877 */
878 if ((format_flags & FORMAT_TRACK512) == 0) {
879 dpos = (2048 + context.sector_size - 1) / context.sector_size;
880
881 /* wipe at least 6 times 2048 byte `sectors' */
882 for (cnt = 0; cnt < 6 *dpos; cnt++) {
883 pos = layout.iso9660_vrs + cnt;
884 if ((error = udf_write_sector(iso9660_vrs_desc, pos)))
885 return error;
886 };
887
888 /* common VRS fields in all written out ISO descriptors */
889 iso9660_vrs_desc->struct_type = 0;
890 iso9660_vrs_desc->version = 1;
891 pos = layout.iso9660_vrs;
892
893 /* BEA01, NSR[23], TEA01 */
894 memcpy(iso9660_vrs_desc->identifier, "BEA01", 5);
895 if ((error = udf_write_sector(iso9660_vrs_desc, pos)))
896 return error;
897 pos += dpos;
898
899 if (context.dscrver == 2)
900 memcpy(iso9660_vrs_desc->identifier, "NSR02", 5);
901 else
902 memcpy(iso9660_vrs_desc->identifier, "NSR03", 5);
903 ;
904 if ((error = udf_write_sector(iso9660_vrs_desc, pos)))
905 return error;
906 pos += dpos;
907
908 memcpy(iso9660_vrs_desc->identifier, "TEA01", 5);
909 if ((error = udf_write_sector(iso9660_vrs_desc, pos)))
910 return error;
911 }
912
913 /* return success */
914 return 0;
915 }
916
917
918 /* --------------------------------------------------------------------- */
919
920 /*
921 * Main function that creates and writes out disc contents based on the
922 * format_flags's that uniquely define the type of disc to create.
923 */
924
925 int
926 udf_do_newfs(void)
927 {
928 union dscrptr *zero_dscr;
929 union dscrptr *terminator_dscr;
930 union dscrptr *root_dscr;
931 union dscrptr *vat_dscr;
932 union dscrptr *dscr;
933 struct mmc_trackinfo ti;
934 uint32_t sparable_blocks;
935 uint32_t sector_size, blockingnr;
936 uint32_t cnt, loc, len;
937 int sectcopy;
938 int error, integrity_type;
939 int data_part, metadata_part;
940
941 /* init */
942 sector_size = mmc_discinfo.sector_size;
943
944 /* determine span/size */
945 ti.tracknr = mmc_discinfo.first_track_last_session;
946 error = udf_update_trackinfo(&mmc_discinfo, &ti);
947 if (error)
948 return error;
949
950 if (mmc_discinfo.sector_size < context.sector_size) {
951 fprintf(stderr, "Impossible to format: sectorsize too small\n");
952 return EIO;
953 }
954 context.sector_size = sector_size;
955
956 /* determine blockingnr */
957 blockingnr = ti.packet_size;
958 if (blockingnr <= 1) {
959 /* paranoia on blockingnr */
960 switch (mmc_discinfo.mmc_profile) {
961 case 0x09 : /* CD-R */
962 case 0x0a : /* CD-RW */
963 blockingnr = 32; /* UDF requirement */
964 break;
965 case 0x11 : /* DVD-R (DL) */
966 case 0x1b : /* DVD+R */
967 case 0x2b : /* DVD+R Dual layer */
968 case 0x13 : /* DVD-RW restricted overwrite */
969 case 0x14 : /* DVD-RW sequential */
970 blockingnr = 16; /* SCSI definition */
971 break;
972 case 0x41 : /* BD-R Sequential recording (SRM) */
973 case 0x51 : /* HD DVD-R */
974 blockingnr = 32; /* SCSI definition */
975 break;
976 default:
977 break;
978 }
979
980 }
981 if (blockingnr <= 0) {
982 printf("Can't fixup blockingnumber for device "
983 "type %d\n", mmc_discinfo.mmc_profile);
984
985 printf("Device is not returning valid blocking"
986 " number and media type is unknown.\n");
987
988 return EINVAL;
989 }
990
991 /* setup sector writeout queue's */
992 TAILQ_INIT(&write_queue);
993 wrtrack_skew = ti.track_start % blockingnr;
994
995 if (mmc_discinfo.mmc_class == MMC_CLASS_CD) {
996 /* not too much for CD-RW, still 20Mb */
997 sparable_blocks = 32;
998 } else {
999 /* take a value for DVD*RW mainly, BD is `defect free' */
1000 sparable_blocks = 512;
1001 }
1002
1003 /* get layout */
1004 error = udf_calculate_disc_layout(format_flags, context.min_udf,
1005 wrtrack_skew,
1006 ti.track_start, mmc_discinfo.last_possible_lba,
1007 sector_size, blockingnr, sparable_blocks);
1008
1009 /* cache partition for we need it often */
1010 data_part = context.data_part;
1011 metadata_part = context.metadata_part;
1012
1013 /* Create sparing table descriptor if applicable */
1014 if (format_flags & FORMAT_SPARABLE) {
1015 if ((error = udf_create_sparing_tabled()))
1016 return error;
1017
1018 if (check_surface) {
1019 if ((error = udf_surface_check()))
1020 return error;
1021 }
1022 }
1023
1024 /* Create a generic terminator descriptor */
1025 terminator_dscr = calloc(1, sector_size);
1026 if (terminator_dscr == NULL)
1027 return ENOMEM;
1028 udf_create_terminator(terminator_dscr, 0);
1029
1030 /*
1031 * Start with wipeout of VRS1 upto start of partition. This allows
1032 * formatting for sequentials with the track reservation and it
1033 * cleans old rubbish on rewritables. For sequentuals without the
1034 * track reservation all is wiped from track start.
1035 */
1036 if ((zero_dscr = calloc(1, context.sector_size)) == NULL)
1037 return ENOMEM;
1038
1039 loc = (format_flags & FORMAT_TRACK512) ? layout.vds1 : ti.track_start;
1040 for (; loc < layout.part_start_lba; loc++) {
1041 if ((error = udf_write_sector(zero_dscr, loc)))
1042 return error;
1043 }
1044
1045 /* Create anchors */
1046 for (cnt = 0; cnt < 3; cnt++) {
1047 if ((error = udf_create_anchor(cnt)))
1048 return error;
1049 }
1050
1051 /*
1052 * Create the two Volume Descriptor Sets (VDS) each containing the
1053 * following descriptors : primary volume, partition space,
1054 * unallocated space, logical volume, implementation use and the
1055 * terminator
1056 */
1057
1058 /* start of volume recognision sequence building */
1059 context.vds_seq = 0;
1060
1061 /* Create primary volume descriptor */
1062 if ((error = udf_create_primaryd()))
1063 return error;
1064
1065 /* Create partition descriptor */
1066 if ((error = udf_create_partitiond(context.data_part, media_accesstype)))
1067 return error;
1068
1069 /* Create unallocated space descriptor */
1070 if ((error = udf_create_unalloc_spaced()))
1071 return error;
1072
1073 /* Create logical volume descriptor */
1074 if ((error = udf_create_logical_dscr(format_flags)))
1075 return error;
1076
1077 /* Create implementation use descriptor */
1078 /* TODO input of fields 1,2,3 and passing them */
1079 if ((error = udf_create_impvold(NULL, NULL, NULL)))
1080 return error;
1081
1082 /* write out what we've created so far */
1083
1084 /* writeout iso9660 vrs */
1085 if ((error = udf_write_iso9660_vrs()))
1086 return error;
1087
1088 /* Writeout anchors */
1089 for (cnt = 0; cnt < 3; cnt++) {
1090 dscr = (union dscrptr *) context.anchors[cnt];
1091 loc = layout.anchors[cnt];
1092 if ((error = udf_write_dscr_phys(dscr, loc, 1)))
1093 return error;
1094
1095 /* sequential media has only one anchor */
1096 if (format_flags & FORMAT_SEQUENTIAL)
1097 break;
1098 }
1099
1100 /* write out main and secondary VRS */
1101 for (sectcopy = 1; sectcopy <= 2; sectcopy++) {
1102 loc = (sectcopy == 1) ? layout.vds1 : layout.vds2;
1103
1104 /* primary volume descriptor */
1105 dscr = (union dscrptr *) context.primary_vol;
1106 error = udf_write_dscr_phys(dscr, loc, 1);
1107 if (error)
1108 return error;
1109 loc++;
1110
1111 /* partition descriptor(s) */
1112 for (cnt = 0; cnt < UDF_PARTITIONS; cnt++) {
1113 dscr = (union dscrptr *) context.partitions[cnt];
1114 if (dscr) {
1115 error = udf_write_dscr_phys(dscr, loc, 1);
1116 if (error)
1117 return error;
1118 loc++;
1119 }
1120 }
1121
1122 /* unallocated space descriptor */
1123 dscr = (union dscrptr *) context.unallocated;
1124 error = udf_write_dscr_phys(dscr, loc, 1);
1125 if (error)
1126 return error;
1127 loc++;
1128
1129 /* logical volume descriptor */
1130 dscr = (union dscrptr *) context.logical_vol;
1131 error = udf_write_dscr_phys(dscr, loc, 1);
1132 if (error)
1133 return error;
1134 loc++;
1135
1136 /* implementation use descriptor */
1137 dscr = (union dscrptr *) context.implementation;
1138 error = udf_write_dscr_phys(dscr, loc, 1);
1139 if (error)
1140 return error;
1141 loc++;
1142
1143 /* terminator descriptor */
1144 error = udf_write_dscr_phys(terminator_dscr, loc, 1);
1145 if (error)
1146 return error;
1147 loc++;
1148 }
1149
1150 /* writeout the two sparable table descriptors (if needed) */
1151 if (format_flags & FORMAT_SPARABLE) {
1152 for (sectcopy = 1; sectcopy <= 2; sectcopy++) {
1153 loc = (sectcopy == 1) ? layout.spt_1 : layout.spt_2;
1154 dscr = (union dscrptr *) context.sparing_table;
1155 len = layout.sparing_table_dscr_lbas;
1156
1157 /* writeout */
1158 error = udf_write_dscr_phys(dscr, loc, len);
1159 if (error)
1160 return error;
1161 }
1162 }
1163
1164 /*
1165 * Create unallocated space bitmap descriptor. Sequential recorded
1166 * media report their own free/used space; no free/used space tables
1167 * should be recorded for these.
1168 */
1169 if ((format_flags & FORMAT_SEQUENTIAL) == 0) {
1170 error = udf_create_space_bitmap(
1171 &context.part_unalloc_bits[data_part]);
1172 /* TODO: freed space bitmap if applicable */
1173 /* mark allocated space bitmap itself */
1174 udf_mark_allocated(layout.unalloc_space, data_part,
1175 layout.bitmap_dscr_size);
1176 }
1177
1178 /* create logical volume integrity descriptor */
1179 context.num_files = 0;
1180 context.num_directories = 0;
1181 integrity_type = UDF_INTEGRITY_OPEN;
1182 if ((error = udf_create_lvintd(integrity_type)))
1183 return error;
1184
1185 /* create secondary descriptors for the fileset and rootdir */
1186
1187 /* create FSD */
1188 if ((error = udf_create_fsd()))
1189 return error;
1190 udf_mark_allocated(layout.fsd, metadata_part, 1);
1191
1192 /* create root directory */
1193 assert(context.unique_id == 0x10);
1194 context.unique_id = 0;
1195 if ((error = udf_create_new_rootdir(&root_dscr)))
1196 return error;
1197 udf_mark_allocated(layout.rootdir, metadata_part, 1);
1198
1199 /* writeout FSD + rootdir */
1200 dscr = (union dscrptr *) context.fileset_desc;
1201 error = udf_write_dscr_virt(dscr, layout.fsd, metadata_part, 1);
1202 if (error)
1203 return error;
1204
1205 error = udf_write_dscr_virt(root_dscr, layout.rootdir, metadata_part, 1);
1206 if (error)
1207 return error;
1208
1209 /* writeout initial open integrity sequence + terminator */
1210 loc = layout.lvis;
1211 dscr = (union dscrptr *) context.logvol_integrity;
1212 error = udf_write_dscr_phys(dscr, loc, 1);
1213 if (error)
1214 return error;
1215 loc++;
1216 error = udf_write_dscr_phys(terminator_dscr, loc, 1);
1217 if (error)
1218 return error;
1219
1220
1221 /* XXX the place to add more files */
1222
1223
1224 if ((format_flags & FORMAT_SEQUENTIAL) == 0) {
1225 /* update lvint and mark it closed */
1226 udf_update_lvintd(UDF_INTEGRITY_CLOSED);
1227
1228 /* overwrite initial terminator */
1229 loc = layout.lvis+1;
1230 dscr = (union dscrptr *) context.logvol_integrity;
1231 error = udf_write_dscr_phys(dscr, loc, 1);
1232 if (error)
1233 return error;
1234 loc++;
1235
1236 /* mark end of integrity desciptor sequence again */
1237 error = udf_write_dscr_phys(terminator_dscr, loc, 1);
1238 if (error)
1239 return error;
1240 }
1241
1242 /* write out unallocated space bitmap on non sequential media */
1243 if ((format_flags & FORMAT_SEQUENTIAL) == 0) {
1244 /* writeout */
1245 loc = layout.unalloc_space;
1246 dscr = (union dscrptr *) (context.part_unalloc_bits[data_part]);
1247 len = layout.bitmap_dscr_size;
1248 error = udf_write_dscr_virt(dscr, loc, metadata_part, len);
1249 if (error)
1250 return error;
1251 }
1252
1253 /* create a VAT and account for FSD+root */
1254 vat_dscr = NULL;
1255 if (format_flags & FORMAT_VAT) {
1256 /* update lvint to reflect the newest values (no writeout) */
1257 udf_update_lvintd(UDF_INTEGRITY_CLOSED);
1258
1259 error = udf_create_new_VAT(&vat_dscr);
1260 if (error)
1261 return error;
1262
1263 loc = layout.vat;
1264 error = udf_write_dscr_virt(vat_dscr, loc, metadata_part, 1);
1265 if (error)
1266 return error;
1267 }
1268
1269 /* write out sectors */
1270 if ((error = writeout_write_queue()))
1271 return error;
1272
1273 /* done */
1274 return 0;
1275 }
1276
1277 /* --------------------------------------------------------------------- */
1278
1279 static void
1280 usage(void)
1281 {
1282 (void)fprintf(stderr, "Usage: %s [-c] [-F] [-L loglabel] "
1283 "[-M] [-v min_udf] [-V max_udf] [-P discid] [-s size] "
1284 "[-S setlabel] [-t gmtoff] special\n", getprogname());
1285 exit(EXIT_FAILURE);
1286 }
1287
1288
1289 int
1290 main(int argc, char **argv)
1291 {
1292 struct tm *tm;
1293 struct stat st;
1294 time_t now;
1295 char scrap[255];
1296 int ch, req_enable, req_disable, force;
1297 int error;
1298
1299 setprogname(argv[0]);
1300
1301 /* initialise */
1302 format_str = strdup("");
1303 req_enable = req_disable = 0;
1304 format_flags = FORMAT_INVALID;
1305 force = 0;
1306 check_surface = 0;
1307
1308 srandom((unsigned long) time(NULL));
1309 udf_init_create_context();
1310 context.app_name = APP_NAME;
1311 context.impl_name = IMPL_NAME;
1312 context.app_version_main = APP_VERSION_MAIN;
1313 context.app_version_sub = APP_VERSION_SUB;
1314
1315 /* minimum and maximum UDF versions we advise */
1316 context.min_udf = 0x201;
1317 context.max_udf = 0x201;
1318
1319 /* use user's time zone as default */
1320 (void)time(&now);
1321 tm = localtime(&now);
1322 context.gmtoff = tm->tm_gmtoff;
1323
1324 /* process options */
1325 while ((ch = getopt(argc, argv, "cFL:MP:s:S:t:v:V:")) != -1) {
1326 switch (ch) {
1327 case 'c' :
1328 check_surface = 1;
1329 break;
1330 case 'F' :
1331 force = 1;
1332 break;
1333 case 'L' :
1334 if (context.logvol_name) free(context.logvol_name);
1335 context.logvol_name = strdup(optarg);
1336 break;
1337 case 'M' :
1338 req_disable |= FORMAT_META;
1339 break;
1340 case 'v' :
1341 context.min_udf = a_num(optarg, "min_udf");
1342 if (context.min_udf > context.max_udf)
1343 context.max_udf = context.min_udf;
1344 break;
1345 case 'V' :
1346 context.max_udf = a_num(optarg, "max_udf");
1347 if (context.min_udf > context.max_udf)
1348 context.min_udf = context.max_udf;
1349 break;
1350 case 'P' :
1351 context.primary_name = strdup(optarg);
1352 break;
1353 case 's' :
1354 /* TODO size argument; recordable emulation */
1355 break;
1356 case 'S' :
1357 if (context.volset_name) free(context.volset_name);
1358 context.volset_name = strdup(optarg);
1359 break;
1360 case 't' :
1361 /* time zone overide */
1362 context.gmtoff = a_num(optarg, "gmtoff");
1363 break;
1364 default :
1365 usage();
1366 /* NOTREACHED */
1367 }
1368 }
1369
1370 if (optind + 1 != argc)
1371 usage();
1372
1373 /* get device and directory specifier */
1374 dev = argv[optind];
1375
1376 /* open device */
1377 if ((fd = open(dev, O_RDWR, 0)) == -1) {
1378 perror("can't open device");
1379 return EXIT_FAILURE;
1380 }
1381
1382 /* stat the device */
1383 if (fstat(fd, &st) != 0) {
1384 perror("can't stat the device");
1385 close(fd);
1386 return EXIT_FAILURE;
1387 }
1388
1389 /* Formatting can only be done on raw devices */
1390 if (!S_ISCHR(st.st_mode)) {
1391 printf("%s is not a raw device\n", dev);
1392 close(fd);
1393 return EXIT_FAILURE;
1394 }
1395
1396 /* just in case something went wrong, synchronise the drive's cache */
1397 udf_synchronise_caches();
1398
1399 /* get disc information */
1400 error = udf_update_discinfo(&mmc_discinfo);
1401 if (error) {
1402 perror("can't retrieve discinfo");
1403 close(fd);
1404 return EXIT_FAILURE;
1405 }
1406
1407 /* derive disc identifiers when not specified and check given */
1408 error = udf_proces_names();
1409 if (error) {
1410 /* error message has been printed */
1411 close(fd);
1412 return EXIT_FAILURE;
1413 }
1414
1415 /* derive newfs disc format from disc profile */
1416 error = udf_derive_format(req_enable, req_disable, force);
1417 if (error) {
1418 /* error message has been printed */
1419 close(fd);
1420 return EXIT_FAILURE;
1421 }
1422
1423 udf_dump_discinfo(&mmc_discinfo);
1424 printf("Formatting disc compatible with UDF version %x to %x\n\n",
1425 context.min_udf, context.max_udf);
1426 (void)snprintb(scrap, sizeof(scrap), FORMAT_FLAGBITS,
1427 (uint64_t) format_flags);
1428 printf("UDF properties %s\n", scrap);
1429 printf("Volume set `%s'\n", context.volset_name);
1430 printf("Primary volume `%s`\n", context.primary_name);
1431 printf("Logical volume `%s`\n", context.logvol_name);
1432 printf("\n");
1433
1434 /* prepare disc if nessisary (recordables mainly) */
1435 error = udf_prepare_disc();
1436 if (error) {
1437 perror("preparing disc failed");
1438 close(fd);
1439 return EXIT_FAILURE;
1440 };
1441
1442 /* set up administration */
1443 error = udf_do_newfs();
1444
1445 /* in any case, synchronise the drive's cache to prevent lockups */
1446 udf_synchronise_caches();
1447
1448 close(fd);
1449 if (error)
1450 return EXIT_FAILURE;
1451
1452 return EXIT_SUCCESS;
1453 }
1454
1455 /* --------------------------------------------------------------------- */
1456
1457