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