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