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