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