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