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