Home | History | Annotate | Line # | Download | only in makefs
cd9660.h revision 1.1
      1 /*	$NetBSD: cd9660.h,v 1.1 2005/08/13 01:53:01 fvdl 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 #include <assert.h>
     39 #include <errno.h>
     40 #include <fcntl.h>
     41 #include <stdarg.h>
     42 #include <stdio.h>
     43 #include <stdlib.h>
     44 #include <string.h>
     45 #include <unistd.h>
     46 #include <time.h>
     47 #include <limits.h>
     48 #include <sys/queue.h>
     49 #include <sys/param.h>
     50 
     51 #include "makefs.h"
     52 #include "cd9660/iso.h"
     53 #include "cd9660/iso_rrip.h"
     54 #include "cd9660/cd9660_eltorito.h"
     55 
     56 
     57 #define CD9660MAXPATH 4096
     58 
     59 #define ISO_STRING_FILTER_NONE = 0x00
     60 #define ISO_STRING_FILTER_DCHARS = 0x01
     61 #define ISO_STRING_FILTER_ACHARS = 0x02
     62 
     63 /*
     64 Extended preferences type, in the spirit of what makefs gives us (only ints)
     65 */
     66 typedef struct {
     67 	const char  *shortName;		/* Short option */
     68 	const char	*name;		/* option name */
     69 	char		*value;		/* where to stuff the value */
     70 	int		minLength;	/* minimum for value */
     71 	int		maxLength;	/* maximum for value */
     72 	const char	*desc;		/* option description */
     73 	int		filterFlags;
     74 } string_option_t;
     75 
     76 /******** STRUCTURES **********/
     77 
     78 /*Defaults*/
     79 #define ISO_DEFAULT_VOLUMEID "MAKEFS_CD9660_IMAGE"
     80 #define ISO_DEFAULT_APPID "MAKEFS"
     81 #define ISO_DEFAULT_PUBLISHER "MAKEFS"
     82 #define ISO_DEFAULT_PREPARER "MAKEFS"
     83 
     84 #define ISO_VOLUME_DESCRIPTOR_STANDARD_ID "CD001"
     85 #define ISO_VOLUME_DESCRIPTOR_BOOT 0
     86 #define ISO_VOLUME_DESCRIPTOR_PVD 1
     87 #define ISO_VOLUME_DESCRIPTOR_TERMINATOR 255
     88 
     89 /*30 for name and extension, as well as version number and padding bit*/
     90 #define ISO_FILENAME_MAXLENGTH_BEFORE_VERSION 30
     91 #define ISO_FILENAME_MAXLENGTH	36
     92 #define ISO_FILENAME_MAXLENGTH_WITH_PADDING 37
     93 
     94 #define ISO_FLAG_CLEAR 0x00
     95 #define ISO_FLAG_HIDDEN 0x01
     96 #define ISO_FLAG_DIRECTORY 0x02
     97 #define ISO_FLAG_ASSOCIATED 0x04
     98 #define ISO_FLAG_PERMISSIONS 0x08
     99 #define ISO_FLAG_RESERVED5 0x10
    100 #define ISO_FLAG_RESERVED6 0x20
    101 #define ISO_FLAG_FINAL_RECORD 0x40
    102 
    103 #define ISO_PATHTABLE_ENTRY_BASESIZE 8
    104 
    105 #define ISO_RRIP_DEFAULT_MOVE_DIR_NAME "RR_MOVED"
    106 #define RRIP_DEFAULT_MOVE_DIR_NAME ".rr_moved"
    107 
    108 #define	CD9660_BLOCKS(S,X)	(((X) / S) + (((X)%S)?1:0))
    109 
    110 #define CD9660_MEM_ALLOC_ERROR(F) printf("%s: memory allocation error. File \"%s\", line %i.\n",(F),__FILE__,__LINE__);
    111 
    112 #define CD9660_IS_COMMAND_ARG_DUAL(var,short,long)\
    113 		(strcmp((var),(short)) == 0) || (strcmp((var),(long))==0)
    114 
    115 #define CD9660_IS_COMMAND_ARG(var,arg)\
    116 		(strcmp((var),(arg)) == 0)
    117 
    118 #define CD9660_TYPE_FILE	0x01
    119 #define CD9660_TYPE_DIR		0x02
    120 #define CD9660_TYPE_DOT		0x04
    121 #define CD9660_TYPE_DOTDOT	0x08
    122 #define CD9660_TYPE_VIRTUAL 0x80
    123 
    124 #define CD9660_HASH_SIZE 512
    125 
    126 #define CD9660_END_PADDING 150
    127 
    128 /* Slight modification of the ISO structure in iso.h */
    129 typedef struct _iso_directory_record_cd9660 {
    130 	u_char length					[ISODCL (1, 1)];	/* 711 */
    131 	u_char ext_attr_length		[ISODCL (2, 2)];	/* 711 */
    132 	u_char extent				[ISODCL (3, 10)];	/* 733 */
    133 	u_char size					[ISODCL (11, 18)];	/* 733 */
    134 	u_char date					[ISODCL (19, 25)];	/* 7 by 711 */
    135 	u_char flags					[ISODCL (26, 26)];
    136 	u_char file_unit_size			[ISODCL (27, 27)];	/* 711 */
    137 	u_char interleave				[ISODCL (28, 28)];	/* 711 */
    138 	u_char volume_sequence_number	[ISODCL (29, 32)];	/* 723 */
    139 	u_char name_len				[ISODCL (33, 33)];	/* 711 */
    140 	char name					[ISO_FILENAME_MAXLENGTH_WITH_PADDING];
    141 } iso_directory_record_cd9660;
    142 
    143 /* TODO: Lots of optimization of this structure */
    144 typedef struct _cd9660node {
    145 	u_char	type;/* Used internally */
    146 	/* Tree structure */
    147 	struct _cd9660node	*parent;	/* parent (NULL if root) */
    148 	struct _cd9660node	*child;		/* child (if type == S_IFDIR) */
    149 	struct _cd9660node	*next;		/* next */
    150 	struct _cd9660node	*prev;		/* previous - to facilitate faster sorting */
    151 	struct _cd9660node	*first;		/* first node of current level (".") */
    152 	struct _cd9660node	*last;		/* last node of current level, for faster sorting - probably not used */
    153 
    154 	struct _cd9660node *dot_record; /* For directories, used mainly in RRIP */
    155 	struct _cd9660node *dot_dot_record;
    156 
    157 	fsnode		*node;		/* pointer to fsnode */
    158 	struct _iso_directory_record_cd9660	*isoDirRecord;
    159 	struct iso_extended_attributes	*isoExtAttributes;
    160 
    161 	/***** SIZE CALCULATION *****/
    162 	/*already stored in isoDirRecord, but this is an int version, and will be
    163 		copied to isoDirRecord on writing*/
    164 	int fileDataSector;
    165 
    166 	/*
    167 	 * same thing, though some notes:
    168 	 * If a file, this is the file size
    169 	 * If a directory, this is the size of all its children's
    170 	 *	directory records
    171 	 * plus necessary padding
    172 	 */
    173 	int64_t fileDataLength;
    174 
    175 	/*
    176 	 * XXXfvdl sectors are int
    177 	 */
    178 	int fileSectorsUsed;
    179 	int fileRecordSize;/*copy of a variable, int for quicker calculations*/
    180 
    181 	/* Old name, used for renaming - needs to be optimized but low priority */
    182 	char o_name [ISO_FILENAME_MAXLENGTH_WITH_PADDING];
    183 
    184 	/***** SPACE RESERVED FOR EXTENSIONS *****/
    185 	/* For memory efficiency's sake - we should move this to a separate struct
    186 		and point to null if not needed */
    187 	/* For Rock Ridge */
    188 	struct _cd9660node *rr_real_parent, *rr_relocated;
    189 
    190 	int susp_entry_size;
    191 	int susp_dot_entry_size;
    192 	int susp_dot_dot_entry_size;
    193 
    194 	/* Continuation area stuff */
    195 	int susp_entry_ce_start;
    196 	int susp_dot_ce_start;
    197 	int susp_dot_dot_ce_start;
    198 
    199 	int susp_entry_ce_length;
    200 	int susp_dot_ce_length;
    201 	int susp_dot_dot_ce_length;
    202 
    203 	/*** PATH TABLE STUFF ***/
    204 	int level;			/*depth*/
    205 	int ptnumber;
    206 	struct _cd9660node *ptnext, *ptprev, *ptlast;
    207 
    208 	/* SUSP entries */
    209 	LIST_HEAD(susp_linked_list, ISO_SUSP_ATTRIBUTES) head;
    210 } cd9660node;
    211 
    212 #if 0
    213 struct cd9660hash_node {
    214 	cd9660node *thenode;
    215 	LIST_ENTRY(cd9660hash_node) ll_struct;
    216 };
    217 
    218 struct cd9660hash_table {
    219 	LIST_HEAD(node_hash_table,cd9660_node_hash) node_hash[CD9660_HASH_SIZE];
    220 };
    221 #endif
    222 
    223 typedef struct _path_table_entry
    224 {
    225 	u_char length[ISODCL (1, 1)];
    226 	u_char extended_attribute_length[ISODCL (2, 2)];
    227 	u_char first_sector[ISODCL (3, 6)];
    228 	u_char parent_number[ISODCL (7, 8)];
    229 	u_char name[ISO_FILENAME_MAXLENGTH_WITH_PADDING];
    230 } path_table_entry;
    231 
    232 typedef struct _volume_descriptor
    233 {
    234 	u_char *volumeDescriptorData; /*ALWAYS 2048 bytes long*/
    235 	int sector;
    236 	struct _volume_descriptor *next;
    237 } volume_descriptor;
    238 
    239 typedef struct _iso9660_disk {
    240 	int sectorSize;
    241 	struct iso_primary_descriptor		primaryDescriptor;
    242 	struct iso_supplementary_descriptor	supplementaryDescriptor;
    243 
    244 	volume_descriptor *firstVolumeDescriptor;
    245 
    246 	cd9660node *rootNode;
    247 	/*struct cd9660hash_table name_hash;*/
    248 
    249 	const char *rootFilesystemPath;
    250 
    251 	/* Important sector numbers here */
    252 	/* primaryDescriptor.type_l_path_table*/
    253 	int primaryBigEndianTableSector;
    254 
    255 	/* primaryDescriptor.type_m_path_table*/
    256 	int primaryLittleEndianTableSector;
    257 
    258 	/* primaryDescriptor.opt_type_l_path_table*/
    259 	int secondaryBigEndianTableSector;
    260 
    261 	/* primaryDescriptor.opt_type_m_path_table*/
    262 	int secondaryLittleEndianTableSector;
    263 
    264 	/* primaryDescriptor.path_table_size*/
    265 	int pathTableLength;
    266 	int dataFirstSector;
    267 
    268 	int totalSectors;
    269 	/* OPTIONS GO HERE */
    270 	int	isoLevel;
    271 
    272 	int include_padding_areas;
    273 
    274 	int follow_sym_links;
    275 	int verbose_level;
    276 	int displayHelp;
    277 	int keep_bad_images;
    278 
    279 	/* SUSP options and variables */
    280 	int susp_continuation_area_start_sector;
    281 	int susp_continuation_area_size;
    282 	int susp_continuation_area_current_free;
    283 
    284 	int rock_ridge_enabled;
    285 	/* Other Rock Ridge Variables */
    286 	char *rock_ridge_renamed_dir_name;
    287 	int rock_ridge_move_count;
    288 	cd9660node *rr_moved_dir;
    289 
    290 
    291 	/* Spec breaking options */
    292 	u_char allow_deep_trees;
    293 	u_char allow_start_dot;
    294 	u_char allow_max_name; /* Allow 37 char filenames*/
    295 	u_char allow_illegal_chars; /* ~, !, # */
    296 	u_char allow_lowercase;
    297 	u_char allow_multidot;
    298 	u_char omit_trailing_period;
    299 
    300 	/* BOOT INFORMATION HERE */
    301 	int is_bootable;/* Default to 0 */
    302 	int boot_catalog_sector;
    303 	boot_volume_descriptor *boot_descriptor;
    304 	char * boot_image_directory;
    305 
    306 	LIST_HEAD(boot_image_list,cd9660_boot_image) boot_images;
    307 	LIST_HEAD(boot_catalog_entries,boot_catalog_entry) boot_entries;
    308 
    309 } iso9660_disk;
    310 
    311 /******** GLOBAL VARIABLES ***********/
    312 extern iso9660_disk diskStructure;
    313 
    314 /************ FUNCTIONS **************/
    315 int			cd9660_valid_a_chars(const char *);
    316 int			cd9660_valid_d_chars(const char *);
    317 void			cd9660_uppercase_characters(char *, int);
    318 
    319 /* ISO Data Types */
    320 void			cd9660_721(uint16_t, unsigned char *);
    321 void			cd9660_731(uint32_t, unsigned char *);
    322 void			cd9660_722(uint16_t, unsigned char *);
    323 void			cd9660_732(uint32_t, unsigned char *);
    324 void 			cd9660_bothendian_dword(uint32_t dw, unsigned char *);
    325 void 			cd9660_bothendian_word(uint16_t dw, unsigned char *);
    326 void			cd9660_set_date(char *, time_t);
    327 void			cd9660_time_8426(unsigned char *, time_t);
    328 void			cd9660_time_915(unsigned char *, time_t);
    329 
    330 /*** Boot Functions ***/
    331 int	cd9660_write_boot(FILE *);
    332 int	cd9660_add_boot_disk(const char *);
    333 int	cd9660_eltorito_add_boot_option(const char *, const char *);
    334 int	cd9660_setup_boot(int);
    335 int	cd9660_setup_boot_volume_descritpor(volume_descriptor *);
    336 
    337 
    338 /*** Write Functions ***/
    339 int	cd9660_write_image(const char *image);
    340 int	cd9660_copy_file(FILE *, int, const char *);
    341 
    342 void	cd9660_compute_full_filename(cd9660node *, char *, int);
    343 int	cd9660_compute_record_size(cd9660node *);
    344 
    345 /* Debugging functions */
    346 void	debug_print_tree(cd9660node *,int);
    347 void	debug_print_path_tree(cd9660node *);
    348 void	debug_print_volume_descriptor_information(void);
    349 void	debug_dump_to_xml_ptentry(path_table_entry *,int, int);
    350 void	debug_dump_to_xml_path_table(FILE *, int, int, int);
    351 void	debug_dump_to_xml(FILE *);
    352 int	debug_get_encoded_number(unsigned char *, int);
    353 void	debug_dump_integer(const char *, char *,int);
    354 void	debug_dump_string(const char *,unsigned char *,int);
    355 void	debug_dump_directory_record_9_1(unsigned char *);
    356 void	debug_dump_to_xml_volume_descriptor(unsigned char *,int);
    357 
    358 void	cd9660_pad_string_spaces(char *, int);
    359 
    360 #endif
    361