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