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