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