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