1 /* $NetBSD: newfs_udf.c,v 1.19 2016/03/09 19:48:24 christos Exp $ */ 2 3 /* 4 * Copyright (c) 2006, 2008, 2013 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 metadata formatting for BD-R 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 <time.h> 51 #include <assert.h> 52 #include <err.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 66 #include "mountprog.h" 67 #include "udf_create.h" 68 #include "udf_write.h" 69 #include "newfs_udf.h" 70 71 /* prototypes */ 72 int newfs_udf(int argc, char **argv); 73 static void usage(void) __attribute__((__noreturn__)); 74 75 76 /* queue for temporary storage of sectors to be written out */ 77 struct wrsect { 78 uint64_t sectornr; 79 uint8_t *sector_data; 80 TAILQ_ENTRY(wrsect) next; 81 }; 82 83 /* write queue and track blocking skew */ 84 TAILQ_HEAD(wrsect_list, wrsect) write_queue; 85 86 87 /* global variables describing disc and format requests */ 88 int fd; /* device: file descriptor */ 89 char *dev; /* device: name */ 90 struct mmc_discinfo mmc_discinfo; /* device: disc info */ 91 92 char *format_str; /* format: string representation */ 93 int format_flags; /* format: attribute flags */ 94 int media_accesstype; /* derived from current mmc cap */ 95 int check_surface; /* for rewritables */ 96 int imagefile_secsize; /* for files */ 97 int emul_packetsize; /* for discs and files */ 98 99 int wrtrack_skew; 100 int meta_perc = UDF_META_PERC; 101 float meta_fract = (float) UDF_META_PERC / 100.0; 102 103 104 /* --------------------------------------------------------------------- */ 105 106 /* 107 * write queue implementation 108 */ 109 110 int 111 udf_write_sector(void *sector, uint64_t location) 112 { 113 struct wrsect *pos, *seekpos; 114 115 116 /* search location */ 117 TAILQ_FOREACH_REVERSE(seekpos, &write_queue, wrsect_list, next) { 118 if (seekpos->sectornr <= location) 119 break; 120 } 121 if ((seekpos == NULL) || (seekpos->sectornr != location)) { 122 pos = calloc(1, sizeof(struct wrsect)); 123 if (pos == NULL) 124 return errno; 125 /* allocate space for copy of sector data */ 126 pos->sector_data = calloc(1, context.sector_size); 127 if (pos->sector_data == NULL) { 128 free(pos); 129 return errno; 130 } 131 pos->sectornr = location; 132 133 if (seekpos) { 134 TAILQ_INSERT_AFTER(&write_queue, seekpos, pos, next); 135 } else { 136 TAILQ_INSERT_HEAD(&write_queue, pos, next); 137 } 138 } else { 139 pos = seekpos; 140 } 141 memcpy(pos->sector_data, sector, context.sector_size); 142 143 return 0; 144 } 145 146 147 /* 148 * Now all write requests are queued in the TAILQ, write them out to the 149 * disc/file image. Special care needs to be taken for devices that are only 150 * strict overwritable i.e. only in packet size chunks 151 * 152 * XXX support for growing vnd? 153 */ 154 155 int 156 writeout_write_queue(void) 157 { 158 struct wrsect *pos; 159 uint64_t offset; 160 uint64_t line_start, new_line_start; 161 uint32_t line_len, line_offset, relpos; 162 uint32_t blockingnr; 163 uint8_t *linebuf, *adr; 164 165 blockingnr = layout.blockingnr; 166 line_len = blockingnr * context.sector_size; 167 line_offset = wrtrack_skew * context.sector_size; 168 169 linebuf = malloc(line_len); 170 if (linebuf == NULL) 171 return ENOMEM; 172 173 pos = TAILQ_FIRST(&write_queue); 174 bzero(linebuf, line_len); 175 176 /* 177 * Always writing out in whole lines now; this is slightly wastefull 178 * on logical overwrite volumes but it reduces complexity and the loss 179 * is near zero compared to disc size. 180 */ 181 line_start = (pos->sectornr - wrtrack_skew) / blockingnr; 182 TAILQ_FOREACH(pos, &write_queue, next) { 183 new_line_start = (pos->sectornr - wrtrack_skew) / blockingnr; 184 if (new_line_start != line_start) { 185 /* write out */ 186 offset = (uint64_t) line_start * line_len + line_offset; 187 #ifdef DEBUG 188 printf("WRITEOUT %08"PRIu64" + %02d -- " 189 "[%08"PRIu64"..%08"PRIu64"]\n", 190 offset / context.sector_size, blockingnr, 191 offset / context.sector_size, 192 offset / context.sector_size + blockingnr-1); 193 #endif 194 if (pwrite(fd, linebuf, line_len, offset) < 0) { 195 perror("Writing failed"); 196 return errno; 197 } 198 line_start = new_line_start; 199 bzero(linebuf, line_len); 200 } 201 202 relpos = (pos->sectornr - wrtrack_skew) % blockingnr; 203 adr = linebuf + relpos * context.sector_size; 204 memcpy(adr, pos->sector_data, context.sector_size); 205 } 206 /* writeout last chunk */ 207 offset = (uint64_t) line_start * line_len + line_offset; 208 #ifdef DEBUG 209 printf("WRITEOUT %08"PRIu64" + %02d -- [%08"PRIu64"..%08"PRIu64"]\n", 210 offset / context.sector_size, blockingnr, 211 offset / context.sector_size, 212 offset / context.sector_size + blockingnr-1); 213 #endif 214 if (pwrite(fd, linebuf, line_len, offset) < 0) { 215 perror("Writing failed"); 216 return errno; 217 } 218 219 /* success */ 220 return 0; 221 } 222 223 /* --------------------------------------------------------------------- */ 224 225 /* 226 * mmc_discinfo and mmc_trackinfo readers modified from origional in udf main 227 * code in sys/fs/udf/ 228 */ 229 230 #ifdef DEBUG 231 static void 232 udf_dump_discinfo(struct mmc_discinfo *di) 233 { 234 char bits[128]; 235 236 printf("Device/media info :\n"); 237 printf("\tMMC profile 0x%02x\n", di->mmc_profile); 238 printf("\tderived class %d\n", di->mmc_class); 239 printf("\tsector size %d\n", di->sector_size); 240 printf("\tdisc state %d\n", di->disc_state); 241 printf("\tlast ses state %d\n", di->last_session_state); 242 printf("\tbg format state %d\n", di->bg_format_state); 243 printf("\tfrst track %d\n", di->first_track); 244 printf("\tfst on last ses %d\n", di->first_track_last_session); 245 printf("\tlst on last ses %d\n", di->last_track_last_session); 246 printf("\tlink block penalty %d\n", di->link_block_penalty); 247 snprintb(bits, sizeof(bits), MMC_DFLAGS_FLAGBITS, (uint64_t) di->disc_flags); 248 printf("\tdisc flags %s\n", bits); 249 printf("\tdisc id %x\n", di->disc_id); 250 printf("\tdisc barcode %"PRIx64"\n", di->disc_barcode); 251 252 printf("\tnum sessions %d\n", di->num_sessions); 253 printf("\tnum tracks %d\n", di->num_tracks); 254 255 snprintb(bits, sizeof(bits), MMC_CAP_FLAGBITS, di->mmc_cur); 256 printf("\tcapabilities cur %s\n", bits); 257 snprintb(bits, sizeof(bits), MMC_CAP_FLAGBITS, di->mmc_cap); 258 printf("\tcapabilities cap %s\n", bits); 259 printf("\n"); 260 printf("\tlast_possible_lba %d\n", di->last_possible_lba); 261 printf("\n"); 262 } 263 #else 264 #define udf_dump_discinfo(a); 265 #endif 266 267 /* --------------------------------------------------------------------- */ 268 269 static int 270 udf_update_discinfo(struct mmc_discinfo *di) 271 { 272 struct stat st; 273 struct disklabel disklab; 274 struct partition *dp; 275 off_t size, sectors, secsize; 276 int partnr, error; 277 278 memset(di, 0, sizeof(struct mmc_discinfo)); 279 280 /* check if we're on a MMC capable device, i.e. CD/DVD */ 281 error = ioctl(fd, MMCGETDISCINFO, di); 282 if (error == 0) 283 return 0; 284 285 /* (re)fstat the file */ 286 fstat(fd, &st); 287 288 if (S_ISREG(st.st_mode)) { 289 /* file support; we pick the minimum sector size allowed */ 290 size = st.st_size; 291 secsize = imagefile_secsize; 292 sectors = size / secsize; 293 } else { 294 /* 295 * disc partition support; note we can't use DIOCGPART in 296 * userland so get disc label and use the stat info to get the 297 * partition number. 298 */ 299 if (ioctl(fd, DIOCGDINFO, &disklab) == -1) { 300 /* failed to get disclabel! */ 301 perror("disklabel"); 302 return errno; 303 } 304 305 /* get disk partition it refers to */ 306 fstat(fd, &st); 307 partnr = DISKPART(st.st_rdev); 308 dp = &disklab.d_partitions[partnr]; 309 310 /* TODO problem with last_possible_lba on resizable VND */ 311 if (dp->p_size == 0) { 312 perror("faulty disklabel partition returned, " 313 "check label\n"); 314 return EIO; 315 } 316 317 sectors = dp->p_size; 318 secsize = disklab.d_secsize; 319 } 320 321 /* set up a disc info profile for partitions */ 322 di->mmc_profile = 0x01; /* disc type */ 323 di->mmc_class = MMC_CLASS_DISC; 324 di->disc_state = MMC_STATE_CLOSED; 325 di->last_session_state = MMC_STATE_CLOSED; 326 di->bg_format_state = MMC_BGFSTATE_COMPLETED; 327 di->link_block_penalty = 0; 328 329 di->mmc_cur = MMC_CAP_RECORDABLE | MMC_CAP_REWRITABLE | 330 MMC_CAP_ZEROLINKBLK | MMC_CAP_HW_DEFECTFREE; 331 di->mmc_cap = di->mmc_cur; 332 di->disc_flags = MMC_DFLAGS_UNRESTRICTED; 333 334 di->last_possible_lba = sectors - 1; 335 di->sector_size = secsize; 336 337 di->num_sessions = 1; 338 di->num_tracks = 1; 339 340 di->first_track = 1; 341 di->first_track_last_session = di->last_track_last_session = 1; 342 343 return 0; 344 } 345 346 347 int 348 udf_update_trackinfo(struct mmc_discinfo *di, struct mmc_trackinfo *ti) 349 { 350 int error, class; 351 352 class = di->mmc_class; 353 if (class != MMC_CLASS_DISC) { 354 /* tracknr specified in struct ti */ 355 error = ioctl(fd, MMCGETTRACKINFO, ti); 356 return error; 357 } 358 359 /* discs partition support */ 360 if (ti->tracknr != 1) 361 return EIO; 362 363 /* create fake ti (TODO check for resized vnds) */ 364 ti->sessionnr = 1; 365 366 ti->track_mode = 0; /* XXX */ 367 ti->data_mode = 0; /* XXX */ 368 ti->flags = MMC_TRACKINFO_LRA_VALID | MMC_TRACKINFO_NWA_VALID; 369 370 ti->track_start = 0; 371 ti->packet_size = emul_packetsize; 372 373 /* TODO support for resizable vnd */ 374 ti->track_size = di->last_possible_lba; 375 ti->next_writable = di->last_possible_lba; 376 ti->last_recorded = ti->next_writable; 377 ti->free_blocks = 0; 378 379 return 0; 380 } 381 382 383 static int 384 udf_setup_writeparams(struct mmc_discinfo *di) 385 { 386 struct mmc_writeparams mmc_writeparams; 387 int error; 388 389 if (di->mmc_class == MMC_CLASS_DISC) 390 return 0; 391 392 /* 393 * only CD burning normally needs setting up, but other disc types 394 * might need other settings to be made. The MMC framework will set up 395 * the nessisary recording parameters according to the disc 396 * characteristics read in. Modifications can be made in the discinfo 397 * structure passed to change the nature of the disc. 398 */ 399 memset(&mmc_writeparams, 0, sizeof(struct mmc_writeparams)); 400 mmc_writeparams.mmc_class = di->mmc_class; 401 mmc_writeparams.mmc_cur = di->mmc_cur; 402 403 /* 404 * UDF dictates first track to determine track mode for the whole 405 * disc. [UDF 1.50/6.10.1.1, UDF 1.50/6.10.2.1] 406 * To prevent problems with a `reserved' track in front we start with 407 * the 2nd track and if that is not valid, go for the 1st. 408 */ 409 mmc_writeparams.tracknr = 2; 410 mmc_writeparams.data_mode = MMC_DATAMODE_DEFAULT; /* XA disc */ 411 mmc_writeparams.track_mode = MMC_TRACKMODE_DEFAULT; /* data */ 412 413 error = ioctl(fd, MMCSETUPWRITEPARAMS, &mmc_writeparams); 414 if (error) { 415 mmc_writeparams.tracknr = 1; 416 error = ioctl(fd, MMCSETUPWRITEPARAMS, &mmc_writeparams); 417 } 418 return error; 419 } 420 421 422 static void 423 udf_synchronise_caches(void) 424 { 425 struct mmc_op mmc_op; 426 427 bzero(&mmc_op, sizeof(struct mmc_op)); 428 mmc_op.operation = MMC_OP_SYNCHRONISECACHE; 429 430 /* this device might not know this ioct, so just be ignorant */ 431 (void) ioctl(fd, MMCOP, &mmc_op); 432 } 433 434 /* --------------------------------------------------------------------- */ 435 436 static int 437 udf_prepare_disc(void) 438 { 439 struct mmc_trackinfo ti; 440 struct mmc_op op; 441 int tracknr, error; 442 443 /* If the last track is damaged, repair it */ 444 ti.tracknr = mmc_discinfo.last_track_last_session; 445 error = udf_update_trackinfo(&mmc_discinfo, &ti); 446 if (error) 447 return error; 448 449 if (ti.flags & MMC_TRACKINFO_DAMAGED) { 450 /* 451 * Need to repair last track before anything can be done. 452 * this is an optional command, so ignore its error but report 453 * warning. 454 */ 455 memset(&op, 0, sizeof(op)); 456 op.operation = MMC_OP_REPAIRTRACK; 457 op.mmc_profile = mmc_discinfo.mmc_profile; 458 op.tracknr = ti.tracknr; 459 error = ioctl(fd, MMCOP, &op); 460 461 if (error) 462 (void)printf("Drive can't explicitly repair last " 463 "damaged track, but it might autorepair\n"); 464 } 465 /* last track (if any) might not be damaged now, operations are ok now */ 466 467 /* setup write parameters from discinfo */ 468 error = udf_setup_writeparams(&mmc_discinfo); 469 if (error) 470 return error; 471 472 /* if the drive is not sequential, we're done */ 473 if ((mmc_discinfo.mmc_cur & MMC_CAP_SEQUENTIAL) == 0) 474 return 0; 475 476 #ifdef notyet 477 /* if last track is not the reserved but an empty track, unreserve it */ 478 if (ti.flags & MMC_TRACKINFO_BLANK) { 479 if (ti.flags & MMC_TRACKINFO_RESERVED == 0) { 480 memset(&op, 0, sizeof(op)); 481 op.operation = MMC_OP_UNRESERVETRACK; 482 op.mmc_profile = mmc_discinfo.mmc_profile; 483 op.tracknr = ti.tracknr; 484 error = ioctl(fd, MMCOP, &op); 485 if (error) 486 return error; 487 488 /* update discinfo since it changed by the operation */ 489 error = udf_update_discinfo(&mmc_discinfo); 490 if (error) 491 return error; 492 } 493 } 494 #endif 495 496 /* close the last session if its still open */ 497 if (mmc_discinfo.last_session_state == MMC_STATE_INCOMPLETE) { 498 printf("Closing last open session if present\n"); 499 /* close all associated tracks */ 500 tracknr = mmc_discinfo.first_track_last_session; 501 while (tracknr <= mmc_discinfo.last_track_last_session) { 502 ti.tracknr = tracknr; 503 error = udf_update_trackinfo(&mmc_discinfo, &ti); 504 if (error) 505 return error; 506 printf("\tClosing open track %d\n", tracknr); 507 memset(&op, 0, sizeof(op)); 508 op.operation = MMC_OP_CLOSETRACK; 509 op.mmc_profile = mmc_discinfo.mmc_profile; 510 op.tracknr = tracknr; 511 error = ioctl(fd, MMCOP, &op); 512 if (error) 513 return error; 514 tracknr ++; 515 } 516 printf("Closing session\n"); 517 memset(&op, 0, sizeof(op)); 518 op.operation = MMC_OP_CLOSESESSION; 519 op.mmc_profile = mmc_discinfo.mmc_profile; 520 op.sessionnr = mmc_discinfo.num_sessions; 521 error = ioctl(fd, MMCOP, &op); 522 if (error) 523 return error; 524 525 /* update discinfo since it changed by the operations */ 526 error = udf_update_discinfo(&mmc_discinfo); 527 if (error) 528 return error; 529 } 530 531 if (format_flags & FORMAT_TRACK512) { 532 /* get last track again */ 533 ti.tracknr = mmc_discinfo.last_track_last_session; 534 error = udf_update_trackinfo(&mmc_discinfo, &ti); 535 if (error) 536 return error; 537 538 /* Split up the space at 512 for iso cd9660 hooking */ 539 memset(&op, 0, sizeof(op)); 540 op.operation = MMC_OP_RESERVETRACK_NWA; /* UPTO nwa */ 541 op.mmc_profile = mmc_discinfo.mmc_profile; 542 op.extent = 512; /* size */ 543 error = ioctl(fd, MMCOP, &op); 544 if (error) 545 return error; 546 } 547 548 return 0; 549 } 550 551 /* --------------------------------------------------------------------- */ 552 553 int 554 udf_surface_check(void) 555 { 556 uint32_t loc, block_bytes; 557 uint32_t sector_size, blockingnr, bpos; 558 uint8_t *buffer; 559 int error, num_errors; 560 561 sector_size = context.sector_size; 562 blockingnr = layout.blockingnr; 563 564 block_bytes = layout.blockingnr * sector_size; 565 if ((buffer = malloc(block_bytes)) == NULL) 566 return ENOMEM; 567 568 /* set all one to not kill Flash memory? */ 569 for (bpos = 0; bpos < block_bytes; bpos++) 570 buffer[bpos] = 0x00; 571 572 printf("\nChecking disc surface : phase 1 - writing\n"); 573 num_errors = 0; 574 loc = layout.first_lba; 575 while (loc <= layout.last_lba) { 576 /* write blockingnr sectors */ 577 error = pwrite(fd, buffer, block_bytes, loc*sector_size); 578 printf(" %08d + %d (%02d %%)\r", loc, blockingnr, 579 (int)((100.0 * loc)/layout.last_lba)); 580 fflush(stdout); 581 if (error == -1) { 582 /* block is bad */ 583 printf("BAD block at %08d + %d \n", 584 loc, layout.blockingnr); 585 if ((error = udf_register_bad_block(loc))) { 586 free(buffer); 587 return error; 588 } 589 num_errors ++; 590 } 591 loc += layout.blockingnr; 592 } 593 594 printf("\nChecking disc surface : phase 2 - reading\n"); 595 num_errors = 0; 596 loc = layout.first_lba; 597 while (loc <= layout.last_lba) { 598 /* read blockingnr sectors */ 599 error = pread(fd, buffer, block_bytes, loc*sector_size); 600 printf(" %08d + %d (%02d %%)\r", loc, blockingnr, 601 (int)((100.0 * loc)/layout.last_lba)); 602 fflush(stdout); 603 if (error == -1) { 604 /* block is bad */ 605 printf("BAD block at %08d + %d \n", 606 loc, layout.blockingnr); 607 if ((error = udf_register_bad_block(loc))) { 608 free(buffer); 609 return error; 610 } 611 num_errors ++; 612 } 613 loc += layout.blockingnr; 614 } 615 printf("Scan complete : %d bad blocks found\n", num_errors); 616 free(buffer); 617 618 return 0; 619 } 620 621 622 /* --------------------------------------------------------------------- */ 623 624 static int 625 udf_do_newfs(void) 626 { 627 int error; 628 629 error = udf_do_newfs_prefix(); 630 if (error) 631 return error; 632 error = udf_do_rootdir(); 633 if (error) 634 return error; 635 error = udf_do_newfs_postfix(); 636 637 return error; 638 } 639 640 641 642 /* --------------------------------------------------------------------- */ 643 644 static void 645 usage(void) 646 { 647 (void)fprintf(stderr, "Usage: %s [-cFM] [-L loglabel] " 648 "[-P discid] [-S sectorsize] [-s size] [-p perc] " 649 "[-t gmtoff] [-v min_udf] [-V max_udf] special\n", getprogname()); 650 exit(EXIT_FAILURE); 651 } 652 653 654 int 655 main(int argc, char **argv) 656 { 657 struct tm *tm; 658 struct stat st; 659 time_t now; 660 off_t setsize; 661 char scrap[255], *colon; 662 int ch, req_enable, req_disable, force; 663 int error; 664 665 setprogname(argv[0]); 666 667 /* initialise */ 668 format_str = strdup(""); 669 req_enable = req_disable = 0; 670 format_flags = FORMAT_INVALID; 671 force = 0; 672 check_surface = 0; 673 setsize = 0; 674 imagefile_secsize = 512; /* minimum allowed sector size */ 675 emul_packetsize = 32; /* reasonable default */ 676 677 srandom((unsigned long) time(NULL)); 678 udf_init_create_context(); 679 context.app_name = "*NetBSD newfs"; 680 context.app_version_main = APP_VERSION_MAIN; 681 context.app_version_sub = APP_VERSION_SUB; 682 context.impl_name = IMPL_NAME; 683 684 /* minimum and maximum UDF versions we advise */ 685 context.min_udf = 0x201; 686 context.max_udf = 0x201; 687 688 /* use user's time zone as default */ 689 (void)time(&now); 690 tm = localtime(&now); 691 context.gmtoff = tm->tm_gmtoff; 692 693 /* process options */ 694 while ((ch = getopt(argc, argv, "cFL:Mp:P:s:S:B:t:v:V:")) != -1) { 695 switch (ch) { 696 case 'c' : 697 check_surface = 1; 698 break; 699 case 'F' : 700 force = 1; 701 break; 702 case 'L' : 703 if (context.logvol_name) free(context.logvol_name); 704 context.logvol_name = strdup(optarg); 705 break; 706 case 'M' : 707 req_disable |= FORMAT_META; 708 break; 709 case 'p' : 710 meta_perc = a_num(optarg, "meta_perc"); 711 /* limit to `sensible` values */ 712 meta_perc = MIN(meta_perc, 99); 713 meta_perc = MAX(meta_perc, 1); 714 meta_fract = (float) meta_perc/100.0; 715 break; 716 case 'v' : 717 context.min_udf = a_udf_version(optarg, "min_udf"); 718 if (context.min_udf > context.max_udf) 719 context.max_udf = context.min_udf; 720 break; 721 case 'V' : 722 context.max_udf = a_udf_version(optarg, "max_udf"); 723 if (context.min_udf > context.max_udf) 724 context.min_udf = context.max_udf; 725 break; 726 case 'P' : 727 /* check if there is a ':' in the name */ 728 if ((colon = strstr(optarg, ":"))) { 729 if (context.volset_name) 730 free(context.volset_name); 731 *colon = 0; 732 context.volset_name = strdup(optarg); 733 optarg = colon+1; 734 } 735 if (context.primary_name) 736 free(context.primary_name); 737 if ((strstr(optarg, ":"))) { 738 perror("primary name can't have ':' in its name"); 739 return EXIT_FAILURE; 740 } 741 context.primary_name = strdup(optarg); 742 break; 743 case 's' : 744 /* support for files, set file size */ 745 /* XXX support for formatting recordables on vnd/file? */ 746 if (dehumanize_number(optarg, &setsize) < 0) { 747 perror("can't parse size argument"); 748 return EXIT_FAILURE; 749 } 750 setsize = MAX(0, setsize); 751 break; 752 case 'S' : 753 imagefile_secsize = a_num(optarg, "secsize"); 754 imagefile_secsize = MAX(512, imagefile_secsize); 755 break; 756 case 'B' : 757 emul_packetsize = a_num(optarg, 758 "blockingnr, packetsize"); 759 emul_packetsize = MAX(emul_packetsize, 1); 760 emul_packetsize = MIN(emul_packetsize, 32); 761 break; 762 case 't' : 763 /* time zone overide */ 764 context.gmtoff = a_num(optarg, "gmtoff"); 765 break; 766 default : 767 usage(); 768 /* NOTREACHED */ 769 } 770 } 771 772 if (optind + 1 != argc) 773 usage(); 774 775 /* get device and directory specifier */ 776 dev = argv[optind]; 777 778 /* open device */ 779 if ((fd = open(dev, O_RDWR, 0)) == -1) { 780 /* check if we need to create a file */ 781 fd = open(dev, O_RDONLY, 0); 782 if (fd > 0) { 783 perror("device is there but can't be opened for read/write"); 784 return EXIT_FAILURE; 785 } 786 if (!force) { 787 perror("can't open device"); 788 return EXIT_FAILURE; 789 } 790 if (setsize == 0) { 791 perror("need to create image file but no size specified"); 792 return EXIT_FAILURE; 793 } 794 /* need to create a file */ 795 fd = open(dev, O_RDWR | O_CREAT | O_TRUNC, 0777); 796 if (fd == -1) { 797 perror("can't create image file"); 798 return EXIT_FAILURE; 799 } 800 } 801 802 /* stat the device */ 803 if (fstat(fd, &st) != 0) { 804 perror("can't stat the device"); 805 close(fd); 806 return EXIT_FAILURE; 807 } 808 809 if (S_ISREG(st.st_mode)) { 810 if (setsize == 0) 811 setsize = st.st_size; 812 /* sanitise arguments */ 813 imagefile_secsize &= ~511; 814 setsize &= ~(imagefile_secsize-1); 815 816 if (ftruncate(fd, setsize)) { 817 perror("can't resize file"); 818 return EXIT_FAILURE; 819 } 820 } 821 822 /* formatting can only be done on raw devices */ 823 if (!S_ISREG(st.st_mode) && !S_ISCHR(st.st_mode)) { 824 printf("%s is not a raw device\n", dev); 825 close(fd); 826 return EXIT_FAILURE; 827 } 828 829 /* just in case something went wrong, synchronise the drive's cache */ 830 udf_synchronise_caches(); 831 832 /* get 'disc' information */ 833 error = udf_update_discinfo(&mmc_discinfo); 834 if (error) { 835 perror("can't retrieve discinfo"); 836 close(fd); 837 return EXIT_FAILURE; 838 } 839 840 /* derive disc identifiers when not specified and check given */ 841 error = udf_proces_names(); 842 if (error) { 843 /* error message has been printed */ 844 close(fd); 845 return EXIT_FAILURE; 846 } 847 848 /* derive newfs disc format from disc profile */ 849 error = udf_derive_format(req_enable, req_disable, force); 850 if (error) { 851 /* error message has been printed */ 852 close(fd); 853 return EXIT_FAILURE; 854 } 855 856 udf_dump_discinfo(&mmc_discinfo); 857 printf("Formatting disc compatible with UDF version %x to %x\n\n", 858 context.min_udf, context.max_udf); 859 (void)snprintb(scrap, sizeof(scrap), FORMAT_FLAGBITS, 860 (uint64_t) format_flags); 861 printf("UDF properties %s\n", scrap); 862 printf("Volume set `%s'\n", context.volset_name); 863 printf("Primary volume `%s`\n", context.primary_name); 864 printf("Logical volume `%s`\n", context.logvol_name); 865 if (format_flags & FORMAT_META) 866 printf("Metadata percentage %d %%\n", meta_perc); 867 printf("\n"); 868 869 /* prepare disc if nessisary (recordables mainly) */ 870 error = udf_prepare_disc(); 871 if (error) { 872 perror("preparing disc failed"); 873 close(fd); 874 return EXIT_FAILURE; 875 }; 876 877 /* setup sector writeout queue's */ 878 TAILQ_INIT(&write_queue); 879 880 /* perform the newfs itself */ 881 error = udf_do_newfs(); 882 if (!error) { 883 /* write out sectors */ 884 error = writeout_write_queue(); 885 } 886 887 /* in any case, synchronise the drive's cache to prevent lockups */ 888 udf_synchronise_caches(); 889 890 close(fd); 891 if (error) 892 return EXIT_FAILURE; 893 894 return EXIT_SUCCESS; 895 } 896 897 /* --------------------------------------------------------------------- */ 898 899