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