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