Home | History | Annotate | Line # | Download | only in makefs
cd9660.h revision 1.8
      1 /*	$NetBSD: cd9660.h,v 1.8 2005/10/30 09:27:49 dyoung Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2005 Daniel Watt, Walter Deignan, Ryan Gabrys, Alan
      5  * Perez-Rathke and Ram Vedam.  All rights reserved.
      6  *
      7  * This code was written by Daniel Watt, Walter Deignan, Ryan Gabrys,
      8  * Alan Perez-Rathke and Ram Vedam.
      9  *
     10  * Redistribution and use in source and binary forms, with or
     11  * without modification, are permitted provided that the following
     12  * conditions are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above
     16  *    copyright notice, this list of conditions and the following
     17  *    disclaimer in the documentation and/or other materials provided
     18  *    with the distribution.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY DANIEL WATT, WALTER DEIGNAN, RYAN
     21  * GABRYS, ALAN PEREZ-RATHKE AND RAM VEDAM ``AS IS'' AND ANY EXPRESS OR
     22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     24  * DISCLAIMED.  IN NO EVENT SHALL DANIEL WATT, WALTER DEIGNAN, RYAN
     25  * GABRYS, ALAN PEREZ-RATHKE AND RAM VEDAM BE LIABLE FOR ANY DIRECT, INDIRECT,
     26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
     28  * USE,DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     29  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
     32  * OF SUCH DAMAGE.
     33  */
     34 
     35 #ifndef _MAKEFS_CD9660_H
     36 #define _MAKEFS_CD9660_H
     37 
     38 #if HAVE_NBTOOL_CONFIG_H
     39 #include "nbtool_config.h"
     40 #endif
     41 
     42 #include <assert.h>
     43 #include <errno.h>
     44 #include <fcntl.h>
     45 #include <stdarg.h>
     46 #include <stdio.h>
     47 #include <stdlib.h>
     48 #include <string.h>
     49 #include <unistd.h>
     50 #include <time.h>
     51 #include <limits.h>
     52 #include <sys/queue.h>
     53 #include <sys/param.h>
     54 
     55 #include "makefs.h"
     56 #include "cd9660/iso.h"
     57 #include "cd9660/iso_rrip.h"
     58 #include "cd9660/cd9660_eltorito.h"
     59 
     60 #ifdef DEBUG
     61 #define	INODE_WARNX(__x)	warnx __x
     62 #else /* DEBUG */
     63 #define	INODE_WARNX(__x)
     64 #endif /* DEBUG */
     65 
     66 #define CD9660MAXPATH 4096
     67 
     68 #define ISO_STRING_FILTER_NONE = 0x00
     69 #define ISO_STRING_FILTER_DCHARS = 0x01
     70 #define ISO_STRING_FILTER_ACHARS = 0x02
     71 
     72 /*
     73 Extended preferences type, in the spirit of what makefs gives us (only ints)
     74 */
     75 typedef struct {
     76 	const char  *shortName;		/* Short option */
     77 	const char	*name;		/* option name */
     78 	char		*value;		/* where to stuff the value */
     79 	int		minLength;	/* minimum for value */
     80 	int		maxLength;	/* maximum for value */
     81 	const char	*desc;		/* option description */
     82 	int		filterFlags;
     83 } string_option_t;
     84 
     85 /******** STRUCTURES **********/
     86 
     87 /*Defaults*/
     88 #define ISO_DEFAULT_VOLUMEID "MAKEFS_CD9660_IMAGE"
     89 #define ISO_DEFAULT_APPID "MAKEFS"
     90 #define ISO_DEFAULT_PUBLISHER "MAKEFS"
     91 #define ISO_DEFAULT_PREPARER "MAKEFS"
     92 
     93 #define ISO_VOLUME_DESCRIPTOR_STANDARD_ID "CD001"
     94 #define ISO_VOLUME_DESCRIPTOR_BOOT 0
     95 #define ISO_VOLUME_DESCRIPTOR_PVD 1
     96 #define ISO_VOLUME_DESCRIPTOR_TERMINATOR 255
     97 
     98 /*30 for name and extension, as well as version number and padding bit*/
     99 #define ISO_FILENAME_MAXLENGTH_BEFORE_VERSION 30
    100 #define ISO_FILENAME_MAXLENGTH	36
    101 #define ISO_FILENAME_MAXLENGTH_WITH_PADDING 37
    102 
    103 #define ISO_FLAG_CLEAR 0x00
    104 #define ISO_FLAG_HIDDEN 0x01
    105 #define ISO_FLAG_DIRECTORY 0x02
    106 #define ISO_FLAG_ASSOCIATED 0x04
    107 #define ISO_FLAG_PERMISSIONS 0x08
    108 #define ISO_FLAG_RESERVED5 0x10
    109 #define ISO_FLAG_RESERVED6 0x20
    110 #define ISO_FLAG_FINAL_RECORD 0x40
    111 
    112 #define ISO_PATHTABLE_ENTRY_BASESIZE 8
    113 
    114 #define ISO_RRIP_DEFAULT_MOVE_DIR_NAME "RR_MOVED"
    115 #define RRIP_DEFAULT_MOVE_DIR_NAME ".rr_moved"
    116 
    117 #define	CD9660_BLOCKS(__sector_size, __bytes)	\
    118 	howmany((__bytes), (__sector_size))
    119 
    120 #define CD9660_MEM_ALLOC_ERROR(_F)	\
    121     err(EXIT_FAILURE, "%s, %s l. %d", _F, __FILE__, __LINE__)
    122 
    123 #define CD9660_IS_COMMAND_ARG_DUAL(var,short,long)\
    124 		(strcmp((var),(short)) == 0) || (strcmp((var),(long))==0)
    125 
    126 #define CD9660_IS_COMMAND_ARG(var,arg)\
    127 		(strcmp((var),(arg)) == 0)
    128 
    129 #define CD9660_TYPE_FILE	0x01
    130 #define CD9660_TYPE_DIR		0x02
    131 #define CD9660_TYPE_DOT		0x04
    132 #define CD9660_TYPE_DOTDOT	0x08
    133 #define CD9660_TYPE_VIRTUAL 0x80
    134 
    135 #define CD9660_INODE_HASH_SIZE 1024
    136 
    137 #define CD9660_END_PADDING 150
    138 
    139 /* Slight modification of the ISO structure in iso.h */
    140 typedef struct _iso_directory_record_cd9660 {
    141 	u_char length			[ISODCL (1, 1)];	/* 711 */
    142 	u_char ext_attr_length		[ISODCL (2, 2)];	/* 711 */
    143 	u_char extent			[ISODCL (3, 10)];	/* 733 */
    144 	u_char size			[ISODCL (11, 18)];	/* 733 */
    145 	u_char date			[ISODCL (19, 25)];	/* 7 by 711 */
    146 	u_char flags			[ISODCL (26, 26)];
    147 	u_char file_unit_size		[ISODCL (27, 27)];	/* 711 */
    148 	u_char interleave		[ISODCL (28, 28)];	/* 711 */
    149 	u_char volume_sequence_number	[ISODCL (29, 32)];	/* 723 */
    150 	u_char name_len			[ISODCL (33, 33)];	/* 711 */
    151 	char name			[ISO_FILENAME_MAXLENGTH_WITH_PADDING];
    152 } iso_directory_record_cd9660;
    153 
    154 /* TODO: Lots of optimization of this structure */
    155 typedef struct _cd9660node {
    156 	u_char	type;/* Used internally */
    157 	/* Tree structure */
    158 	struct _cd9660node	*parent;	/* parent (NULL if root) */
    159 	TAILQ_HEAD(cd9660_children_head, _cd9660node)	cn_children;
    160 	TAILQ_ENTRY(_cd9660node)		cn_next_child;
    161 
    162 	struct _cd9660node *dot_record; /* For directories, used mainly in RRIP */
    163 	struct _cd9660node *dot_dot_record;
    164 
    165 	fsnode		*node;		/* pointer to fsnode */
    166 	struct _iso_directory_record_cd9660	*isoDirRecord;
    167 	struct iso_extended_attributes	*isoExtAttributes;
    168 
    169 	/***** SIZE CALCULATION *****/
    170 	/*already stored in isoDirRecord, but this is an int version, and will be
    171 		copied to isoDirRecord on writing*/
    172 	int fileDataSector;
    173 
    174 	/*
    175 	 * same thing, though some notes:
    176 	 * If a file, this is the file size
    177 	 * If a directory, this is the size of all its children's
    178 	 *	directory records
    179 	 * plus necessary padding
    180 	 */
    181 	int64_t fileDataLength;
    182 
    183 	/*
    184 	 * XXXfvdl sectors are int
    185 	 */
    186 	int fileSectorsUsed;
    187 	int fileRecordSize;/*copy of a variable, int for quicker calculations*/
    188 
    189 	/* Old name, used for renaming - needs to be optimized but low priority */
    190 	char o_name [ISO_FILENAME_MAXLENGTH_WITH_PADDING];
    191 
    192 	/***** SPACE RESERVED FOR EXTENSIONS *****/
    193 	/* For memory efficiency's sake - we should move this to a separate struct
    194 		and point to null if not needed */
    195 	/* For Rock Ridge */
    196 	struct _cd9660node *rr_real_parent, *rr_relocated;
    197 
    198 	int susp_entry_size;
    199 	int susp_dot_entry_size;
    200 	int susp_dot_dot_entry_size;
    201 
    202 	/* Continuation area stuff */
    203 	int susp_entry_ce_start;
    204 	int susp_dot_ce_start;
    205 	int susp_dot_dot_ce_start;
    206 
    207 	int susp_entry_ce_length;
    208 	int susp_dot_ce_length;
    209 	int susp_dot_dot_ce_length;
    210 
    211 	/*** PATH TABLE STUFF ***/
    212 	int level;			/*depth*/
    213 	int ptnumber;
    214 	struct _cd9660node *ptnext, *ptprev, *ptlast;
    215 
    216 	/* SUSP entries */
    217 	TAILQ_HEAD(susp_linked_list, ISO_SUSP_ATTRIBUTES) head;
    218 } cd9660node;
    219 
    220 struct cd9660_inode {
    221 	uint32_t			in_ino;
    222 	int				in_data_sector;
    223 	SLIST_ENTRY(cd9660_inode)	in_link;
    224 };
    225 
    226 #define	CD9660_INODE_HASH(__inode)	((__inode) % CD9660_INODE_HASH_SIZE)
    227 
    228 typedef struct _path_table_entry
    229 {
    230 	u_char length[ISODCL (1, 1)];
    231 	u_char extended_attribute_length[ISODCL (2, 2)];
    232 	u_char first_sector[ISODCL (3, 6)];
    233 	u_char parent_number[ISODCL (7, 8)];
    234 	u_char name[ISO_FILENAME_MAXLENGTH_WITH_PADDING];
    235 } path_table_entry;
    236 
    237 typedef struct _volume_descriptor
    238 {
    239 	u_char *volumeDescriptorData; /*ALWAYS 2048 bytes long*/
    240 	int sector;
    241 	struct _volume_descriptor *next;
    242 } volume_descriptor;
    243 
    244 typedef struct _iso9660_disk {
    245 	int sectorSize;
    246 	struct iso_primary_descriptor		primaryDescriptor;
    247 	struct iso_supplementary_descriptor	supplementaryDescriptor;
    248 
    249 	volume_descriptor *firstVolumeDescriptor;
    250 
    251 	cd9660node *rootNode;
    252 
    253 	SLIST_HEAD(cd9660_inode_head,
    254 	           cd9660_inode)	inode_hash[CD9660_INODE_HASH_SIZE];
    255 
    256 	const char *rootFilesystemPath;
    257 
    258 	/* Important sector numbers here */
    259 	/* primaryDescriptor.type_l_path_table*/
    260 	int primaryBigEndianTableSector;
    261 
    262 	/* primaryDescriptor.type_m_path_table*/
    263 	int primaryLittleEndianTableSector;
    264 
    265 	/* primaryDescriptor.opt_type_l_path_table*/
    266 	int secondaryBigEndianTableSector;
    267 
    268 	/* primaryDescriptor.opt_type_m_path_table*/
    269 	int secondaryLittleEndianTableSector;
    270 
    271 	/* primaryDescriptor.path_table_size*/
    272 	int pathTableLength;
    273 	int dataFirstSector;
    274 
    275 	int totalSectors;
    276 	/* OPTIONS GO HERE */
    277 	int	isoLevel;
    278 
    279 	int include_padding_areas;
    280 
    281 	int follow_sym_links;
    282 	int verbose_level;
    283 	int displayHelp;
    284 	int keep_bad_images;
    285 
    286 	/* SUSP options and variables */
    287 	int susp_continuation_area_start_sector;
    288 	int susp_continuation_area_size;
    289 	int susp_continuation_area_current_free;
    290 
    291 	int rock_ridge_enabled;
    292 	/* Other Rock Ridge Variables */
    293 	char *rock_ridge_renamed_dir_name;
    294 	int rock_ridge_move_count;
    295 	cd9660node *rr_moved_dir;
    296 
    297 
    298 	/* Spec breaking options */
    299 	u_char allow_deep_trees;
    300 	u_char allow_start_dot;
    301 	u_char allow_max_name; /* Allow 37 char filenames*/
    302 	u_char allow_illegal_chars; /* ~, !, # */
    303 	u_char allow_lowercase;
    304 	u_char allow_multidot;
    305 	u_char omit_trailing_period;
    306 
    307 	/* BOOT INFORMATION HERE */
    308 	int is_bootable;/* Default to 0 */
    309 	int boot_catalog_sector;
    310 	boot_volume_descriptor *boot_descriptor;
    311 	char * boot_image_directory;
    312 
    313 	TAILQ_HEAD(boot_image_list,cd9660_boot_image) boot_images;
    314 	int image_serialno;
    315 	LIST_HEAD(boot_catalog_entries,boot_catalog_entry) boot_entries;
    316 
    317 } iso9660_disk;
    318 
    319 /******** GLOBAL VARIABLES ***********/
    320 extern iso9660_disk diskStructure;
    321 
    322 /************ FUNCTIONS **************/
    323 int			cd9660_valid_a_chars(const char *);
    324 int			cd9660_valid_d_chars(const char *);
    325 void			cd9660_uppercase_characters(char *, int);
    326 
    327 /* ISO Data Types */
    328 void			cd9660_721(uint16_t, unsigned char *);
    329 void			cd9660_731(uint32_t, unsigned char *);
    330 void			cd9660_722(uint16_t, unsigned char *);
    331 void			cd9660_732(uint32_t, unsigned char *);
    332 void 			cd9660_bothendian_dword(uint32_t dw, unsigned char *);
    333 void 			cd9660_bothendian_word(uint16_t dw, unsigned char *);
    334 void			cd9660_set_date(char *, time_t);
    335 void			cd9660_time_8426(unsigned char *, time_t);
    336 void			cd9660_time_915(unsigned char *, time_t);
    337 
    338 /*** Boot Functions ***/
    339 int	cd9660_write_boot(FILE *);
    340 int	cd9660_add_boot_disk(const char *);
    341 int	cd9660_eltorito_add_boot_option(const char *, const char *);
    342 int	cd9660_setup_boot(int);
    343 int	cd9660_setup_boot_volume_descritpor(volume_descriptor *);
    344 
    345 
    346 /*** Write Functions ***/
    347 int	cd9660_write_image(const char *image);
    348 int	cd9660_copy_file(FILE *, int, const char *);
    349 
    350 void	cd9660_compute_full_filename(cd9660node *, char *, int);
    351 int	cd9660_compute_record_size(cd9660node *);
    352 
    353 /* Debugging functions */
    354 void	debug_print_tree(cd9660node *,int);
    355 void	debug_print_path_tree(cd9660node *);
    356 void	debug_print_volume_descriptor_information(void);
    357 void	debug_dump_to_xml_ptentry(path_table_entry *,int, int);
    358 void	debug_dump_to_xml_path_table(FILE *, int, int, int);
    359 void	debug_dump_to_xml(FILE *);
    360 int	debug_get_encoded_number(unsigned char *, int);
    361 void	debug_dump_integer(const char *, char *,int);
    362 void	debug_dump_string(const char *,unsigned char *,int);
    363 void	debug_dump_directory_record_9_1(unsigned char *);
    364 void	debug_dump_to_xml_volume_descriptor(unsigned char *,int);
    365 
    366 void	cd9660_pad_string_spaces(char *, int);
    367 
    368 #endif
    369