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