cd9660.h revision 222191
1254721Semaste/*	$NetBSD: cd9660.h,v 1.15 2010/10/27 18:51:34 christos Exp $	*/
2254721Semaste
3254721Semaste/*
4254721Semaste * Copyright (c) 2005 Daniel Watt, Walter Deignan, Ryan Gabrys, Alan
5254721Semaste * Perez-Rathke and Ram Vedam.  All rights reserved.
6254721Semaste *
7254721Semaste * This code was written by Daniel Watt, Walter Deignan, Ryan Gabrys,
8254721Semaste * Alan Perez-Rathke and Ram Vedam.
9254721Semaste *
10254721Semaste * Redistribution and use in source and binary forms, with or
11254721Semaste * without modification, are permitted provided that the following
12254721Semaste * conditions are met:
13254721Semaste * 1. Redistributions of source code must retain the above copyright
14288943Sdim *    notice, this list of conditions and the following disclaimer.
15258054Semaste * 2. Redistributions in binary form must reproduce the above
16258054Semaste *    copyright notice, this list of conditions and the following
17254721Semaste *    disclaimer in the documentation and/or other materials provided
18254721Semaste *    with the distribution.
19288943Sdim *
20254721Semaste * THIS SOFTWARE IS PROVIDED BY DANIEL WATT, WALTER DEIGNAN, RYAN
21254721Semaste * GABRYS, ALAN PEREZ-RATHKE AND RAM VEDAM ``AS IS'' AND ANY EXPRESS OR
22276479Sdim * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23254721Semaste * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24254721Semaste * DISCLAIMED.  IN NO EVENT SHALL DANIEL WATT, WALTER DEIGNAN, RYAN
25254721Semaste * GABRYS, ALAN PEREZ-RATHKE AND RAM VEDAM BE LIABLE FOR ANY DIRECT, INDIRECT,
26288943Sdim * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27254721Semaste * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
28258054Semaste * USE,DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29254721Semaste * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30280031Sdim * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31254721Semaste * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
32254721Semaste * OF SUCH DAMAGE.
33276479Sdim *
34288943Sdim * $FreeBSD: head/usr.sbin/makefs/cd9660.h 222191 2011-05-22 19:56:14Z nwhitehorn $
35254721Semaste */
36288943Sdim
37276479Sdim#ifndef _MAKEFS_CD9660_H
38288943Sdim#define _MAKEFS_CD9660_H
39288943Sdim
40254721Semaste#include <inttypes.h>
41254721Semaste#include <assert.h>
42254721Semaste#include <errno.h>
43254721Semaste#include <fcntl.h>
44254721Semaste#include <stdarg.h>
45258054Semaste#include <stdio.h>
46254721Semaste#include <stdlib.h>
47288943Sdim#include <string.h>
48288943Sdim#include <unistd.h>
49288943Sdim#include <time.h>
50288943Sdim#include <limits.h>
51254721Semaste#include <sys/queue.h>
52254721Semaste#include <sys/param.h>
53288943Sdim
54254721Semaste#include "makefs.h"
55254721Semaste#include "iso.h"
56254721Semaste#include "iso_rrip.h"
57254721Semaste#include "cd9660/cd9660_eltorito.h"
58288943Sdim
59288943Sdim#ifdef DEBUG
60254721Semaste#define	INODE_WARNX(__x)	warnx __x
61254721Semaste#else /* DEBUG */
62254721Semaste#define	INODE_WARNX(__x)
63254721Semaste#endif /* DEBUG */
64254721Semaste
65254721Semaste#define CD9660MAXPATH 4096
66254721Semaste
67254721Semaste#define ISO_STRING_FILTER_NONE = 0x00
68254721Semaste#define ISO_STRING_FILTER_DCHARS = 0x01
69254721Semaste#define ISO_STRING_FILTER_ACHARS = 0x02
70276479Sdim
71254721Semaste/*
72276479SdimExtended preferences type, in the spirit of what makefs gives us (only ints)
73254721Semaste*/
74254721Semastetypedef struct {
75254721Semaste	const char  *shortName;		/* Short option */
76254721Semaste	const char	*name;		/* option name */
77254721Semaste	char		*value;		/* where to stuff the value */
78254721Semaste	int		minLength;	/* minimum for value */
79254721Semaste	int		maxLength;	/* maximum for value */
80258054Semaste	const char	*desc;		/* option description */
81276479Sdim	int		filterFlags;
82276479Sdim} string_option_t;
83258884Semaste
84276479Sdim/******** STRUCTURES **********/
85262528Semaste
86262528Semaste/*Defaults*/
87288943Sdim#define ISO_DEFAULT_VOLUMEID "MAKEFS_CD9660_IMAGE"
88262528Semaste#define ISO_DEFAULT_APPID "MAKEFS"
89276479Sdim#define ISO_DEFAULT_PUBLISHER "MAKEFS"
90288943Sdim#define ISO_DEFAULT_PREPARER "MAKEFS"
91254721Semaste
92254721Semaste#define ISO_VOLUME_DESCRIPTOR_STANDARD_ID "CD001"
93254721Semaste#define ISO_VOLUME_DESCRIPTOR_BOOT 0
94254721Semaste#define ISO_VOLUME_DESCRIPTOR_PVD 1
95254721Semaste#define ISO_VOLUME_DESCRIPTOR_TERMINATOR 255
96254721Semaste
97254721Semaste/*30 for name and extension, as well as version number and padding bit*/
98254721Semaste#define ISO_FILENAME_MAXLENGTH_BEFORE_VERSION 30
99254721Semaste#define ISO_FILENAME_MAXLENGTH	36
100254721Semaste#define ISO_FILENAME_MAXLENGTH_WITH_PADDING 37
101258054Semaste
102258054Semaste#define ISO_FLAG_CLEAR 0x00
103288943Sdim#define ISO_FLAG_HIDDEN 0x01
104296417Sdim#define ISO_FLAG_DIRECTORY 0x02
105296417Sdim#define ISO_FLAG_ASSOCIATED 0x04
106288943Sdim#define ISO_FLAG_PERMISSIONS 0x08
107276479Sdim#define ISO_FLAG_RESERVED5 0x10
108254721Semaste#define ISO_FLAG_RESERVED6 0x20
109254721Semaste#define ISO_FLAG_FINAL_RECORD 0x40
110254721Semaste
111254721Semaste#define ISO_PATHTABLE_ENTRY_BASESIZE 8
112254721Semaste
113254721Semaste#define ISO_RRIP_DEFAULT_MOVE_DIR_NAME "RR_MOVED"
114262528Semaste#define RRIP_DEFAULT_MOVE_DIR_NAME ".rr_moved"
115254721Semaste
116254721Semaste#define	CD9660_BLOCKS(__sector_size, __bytes)	\
117276479Sdim	howmany((__bytes), (__sector_size))
118254721Semaste
119254721Semaste#define CD9660_MEM_ALLOC_ERROR(_F)	\
120254721Semaste    err(EXIT_FAILURE, "%s, %s l. %d", _F, __FILE__, __LINE__)
121254721Semaste
122254721Semaste#define CD9660_IS_COMMAND_ARG_DUAL(var,short,long)\
123258054Semaste		(strcmp((var),(short)) == 0) || (strcmp((var),(long))==0)
124258054Semaste
125258054Semaste#define CD9660_IS_COMMAND_ARG(var,arg)\
126258054Semaste		(strcmp((var),(arg)) == 0)
127276479Sdim
128276479Sdim#define CD9660_TYPE_FILE	0x01
129262528Semaste#define CD9660_TYPE_DIR		0x02
130262528Semaste#define CD9660_TYPE_DOT		0x04
131254721Semaste#define CD9660_TYPE_DOTDOT	0x08
132254721Semaste#define CD9660_TYPE_VIRTUAL	0x80
133254721Semaste
134254721Semaste#define CD9660_INODE_HASH_SIZE	1024
135254721Semaste#define CD9660_SECTOR_SIZE	2048
136254721Semaste
137254721Semaste#define CD9660_END_PADDING	150
138254721Semaste
139254721Semaste/* Slight modification of the ISO structure in iso.h */
140254721Semastetypedef struct _iso_directory_record_cd9660 {
141254721Semaste	u_char length			[ISODCL (1, 1)];	/* 711 */
142254721Semaste	u_char ext_attr_length		[ISODCL (2, 2)];	/* 711 */
143254721Semaste	u_char extent			[ISODCL (3, 10)];	/* 733 */
144254721Semaste	u_char size			[ISODCL (11, 18)];	/* 733 */
145254721Semaste	u_char date			[ISODCL (19, 25)];	/* 7 by 711 */
146288943Sdim	u_char flags			[ISODCL (26, 26)];
147258884Semaste	u_char file_unit_size		[ISODCL (27, 27)];	/* 711 */
148254721Semaste	u_char interleave		[ISODCL (28, 28)];	/* 711 */
149254721Semaste	u_char volume_sequence_number	[ISODCL (29, 32)];	/* 723 */
150254721Semaste	u_char name_len			[ISODCL (33, 33)];	/* 711 */
151258884Semaste	char name			[ISO_FILENAME_MAXLENGTH_WITH_PADDING];
152262528Semaste} iso_directory_record_cd9660;
153262528Semaste
154262528Semaste/* TODO: Lots of optimization of this structure */
155262528Semastetypedef struct _cd9660node {
156262528Semaste	u_char	type;/* Used internally */
157262528Semaste	/* Tree structure */
158288943Sdim	struct _cd9660node	*parent;	/* parent (NULL if root) */
159262528Semaste	TAILQ_HEAD(cd9660_children_head, _cd9660node)	cn_children;
160258884Semaste	TAILQ_ENTRY(_cd9660node)		cn_next_child;
161258884Semaste
162258884Semaste	struct _cd9660node *dot_record; /* For directories, used mainly in RRIP */
163258884Semaste	struct _cd9660node *dot_dot_record;
164258884Semaste
165258884Semaste	fsnode		*node;		/* pointer to fsnode */
166258884Semaste	struct _iso_directory_record_cd9660	*isoDirRecord;
167258884Semaste	struct iso_extended_attributes	*isoExtAttributes;
168258884Semaste
169258884Semaste	/***** SIZE CALCULATION *****/
170258884Semaste	/*already stored in isoDirRecord, but this is an int version, and will be
171258884Semaste		copied to isoDirRecord on writing*/
172258884Semaste	uint32_t fileDataSector;
173258884Semaste
174258884Semaste	/*
175258884Semaste	 * same thing, though some notes:
176258884Semaste	 * If a file, this is the file size
177258884Semaste	 * If a directory, this is the size of all its children's
178258884Semaste	 *	directory records
179254721Semaste	 * plus necessary padding
180254721Semaste	 */
181254721Semaste	int64_t fileDataLength;
182258884Semaste
183288943Sdim	int64_t fileSectorsUsed;
184288943Sdim	int fileRecordSize;/*copy of a variable, int for quicker calculations*/
185288943Sdim
186288943Sdim	/* Old name, used for renaming - needs to be optimized but low priority */
187288943Sdim	char o_name [ISO_FILENAME_MAXLENGTH_WITH_PADDING];
188288943Sdim
189288943Sdim	/***** SPACE RESERVED FOR EXTENSIONS *****/
190288943Sdim	/* For memory efficiency's sake - we should move this to a separate struct
191288943Sdim		and point to null if not needed */
192288943Sdim	/* For Rock Ridge */
193288943Sdim	struct _cd9660node *rr_real_parent, *rr_relocated;
194262528Semaste
195262528Semaste	int64_t susp_entry_size;
196262528Semaste	int64_t susp_dot_entry_size;
197262528Semaste	int64_t susp_dot_dot_entry_size;
198262528Semaste
199262528Semaste	/* Continuation area stuff */
200288943Sdim	int64_t susp_entry_ce_start;
201262528Semaste	int64_t susp_dot_ce_start;
202262528Semaste	int64_t susp_dot_dot_ce_start;
203262528Semaste
204262528Semaste	int64_t susp_entry_ce_length;
205262528Semaste	int64_t susp_dot_ce_length;
206262528Semaste	int64_t susp_dot_dot_ce_length;
207262528Semaste
208262528Semaste	/* Data to put at the end of the System Use field */
209262528Semaste	int64_t su_tail_size;
210288943Sdim	char *su_tail_data;
211262528Semaste
212262528Semaste	/*** PATH TABLE STUFF ***/
213262528Semaste	int level;			/*depth*/
214262528Semaste	int ptnumber;
215262528Semaste	struct _cd9660node *ptnext, *ptprev, *ptlast;
216262528Semaste
217262528Semaste	/* SUSP entries */
218262528Semaste	TAILQ_HEAD(susp_linked_list, ISO_SUSP_ATTRIBUTES) head;
219262528Semaste} cd9660node;
220288943Sdim
221262528Semastetypedef struct _path_table_entry
222262528Semaste{
223276479Sdim	u_char length[ISODCL (1, 1)];
224276479Sdim	u_char extended_attribute_length[ISODCL (2, 2)];
225276479Sdim	u_char first_sector[ISODCL (3, 6)];
226276479Sdim	u_char parent_number[ISODCL (7, 8)];
227276479Sdim	u_char name[ISO_FILENAME_MAXLENGTH_WITH_PADDING];
228276479Sdim} path_table_entry;
229276479Sdim
230288943Sdimtypedef struct _volume_descriptor
231276479Sdim{
232276479Sdim	u_char *volumeDescriptorData; /*ALWAYS 2048 bytes long*/
233288943Sdim	int64_t sector;
234288943Sdim	struct _volume_descriptor *next;
235288943Sdim} volume_descriptor;
236288943Sdim
237288943Sdimtypedef struct _iso9660_disk {
238288943Sdim	int sectorSize;
239288943Sdim	struct iso_primary_descriptor		primaryDescriptor;
240288943Sdim	struct iso_supplementary_descriptor	supplementaryDescriptor;
241288943Sdim
242288943Sdim	volume_descriptor *firstVolumeDescriptor;
243262528Semaste
244262528Semaste	cd9660node *rootNode;
245262528Semaste
246262528Semaste	const char *rootFilesystemPath;
247262528Semaste
248262528Semaste	/* Important sector numbers here */
249262528Semaste	/* primaryDescriptor.type_l_path_table*/
250262528Semaste	int64_t primaryBigEndianTableSector;
251262528Semaste
252262528Semaste	/* primaryDescriptor.type_m_path_table*/
253262528Semaste	int64_t primaryLittleEndianTableSector;
254254721Semaste
255254721Semaste	/* primaryDescriptor.opt_type_l_path_table*/
256254721Semaste	int64_t secondaryBigEndianTableSector;
257254721Semaste
258254721Semaste	/* primaryDescriptor.opt_type_m_path_table*/
259254721Semaste	int64_t secondaryLittleEndianTableSector;
260254721Semaste
261276479Sdim	/* primaryDescriptor.path_table_size*/
262276479Sdim	int pathTableLength;
263276479Sdim	int64_t dataFirstSector;
264276479Sdim
265276479Sdim	int64_t totalSectors;
266288943Sdim	/* OPTIONS GO HERE */
267276479Sdim	int	isoLevel;
268254721Semaste
269288943Sdim	int include_padding_areas;
270254721Semaste
271254721Semaste	int follow_sym_links;
272254721Semaste	int verbose_level;
273254721Semaste	int displayHelp;
274254721Semaste	int keep_bad_images;
275254721Semaste
276258884Semaste	/* SUSP options and variables */
277254721Semaste	int64_t susp_continuation_area_start_sector;
278254721Semaste	int64_t susp_continuation_area_size;
279258884Semaste	int64_t susp_continuation_area_current_free;
280254721Semaste
281254721Semaste	int rock_ridge_enabled;
282254721Semaste	/* Other Rock Ridge Variables */
283254721Semaste	char *rock_ridge_renamed_dir_name;
284254721Semaste	int rock_ridge_move_count;
285254721Semaste	cd9660node *rr_moved_dir;
286254721Semaste
287254721Semaste	int archimedes_enabled;
288254721Semaste	int chrp_boot;
289254721Semaste
290262528Semaste	/* Spec breaking options */
291254721Semaste	u_char allow_deep_trees;
292254721Semaste	u_char allow_start_dot;
293254721Semaste	u_char allow_max_name; /* Allow 37 char filenames*/
294254721Semaste	u_char allow_illegal_chars; /* ~, !, # */
295254721Semaste	u_char allow_lowercase;
296254721Semaste	u_char allow_multidot;
297254721Semaste	u_char omit_trailing_period;
298254721Semaste
299254721Semaste	/* BOOT INFORMATION HERE */
300254721Semaste	int has_generic_bootimage; /* Default to 0 */
301254721Semaste	char *generic_bootimage;
302254721Semaste
303254721Semaste	int is_bootable;/* Default to 0 */
304254721Semaste	int64_t boot_catalog_sector;
305254721Semaste	boot_volume_descriptor *boot_descriptor;
306262528Semaste	char * boot_image_directory;
307254721Semaste
308254721Semaste	TAILQ_HEAD(boot_image_list,cd9660_boot_image) boot_images;
309254721Semaste	int image_serialno;
310254721Semaste	LIST_HEAD(boot_catalog_entries,boot_catalog_entry) boot_entries;
311254721Semaste
312254721Semaste} iso9660_disk;
313254721Semaste
314254721Semaste/******** GLOBAL VARIABLES ***********/
315254721Semasteextern iso9660_disk diskStructure;
316254721Semaste
317254721Semaste/************ FUNCTIONS **************/
318254721Semasteint			cd9660_valid_a_chars(const char *);
319254721Semasteint			cd9660_valid_d_chars(const char *);
320254721Semastevoid			cd9660_uppercase_characters(char *, int);
321254721Semaste
322254721Semaste/* ISO Data Types */
323254721Semastevoid			cd9660_721(uint16_t, unsigned char *);
324254721Semastevoid			cd9660_731(uint32_t, unsigned char *);
325254721Semastevoid			cd9660_722(uint16_t, unsigned char *);
326262528Semastevoid			cd9660_732(uint32_t, unsigned char *);
327254721Semastevoid 			cd9660_bothendian_dword(uint32_t dw, unsigned char *);
328254721Semastevoid 			cd9660_bothendian_word(uint16_t dw, unsigned char *);
329254721Semastevoid			cd9660_set_date(char *, time_t);
330254721Semastevoid			cd9660_time_8426(unsigned char *, time_t);
331254721Semastevoid			cd9660_time_915(unsigned char *, time_t);
332254721Semaste
333254721Semaste/*** Boot Functions ***/
334254721Semasteint	cd9660_write_generic_bootimage(FILE *);
335254721Semasteint	cd9660_add_generic_bootimage(const char *);
336254721Semasteint	cd9660_write_boot(FILE *);
337254721Semasteint	cd9660_add_boot_disk(const char *);
338254721Semasteint	cd9660_eltorito_add_boot_option(const char *, const char *);
339254721Semasteint	cd9660_setup_boot(int);
340288943Sdimint	cd9660_setup_boot_volume_descriptor(volume_descriptor *);
341254721Semaste
342288943Sdim
343288943Sdim/*** Write Functions ***/
344288943Sdimint	cd9660_write_image(const char *image);
345288943Sdimint	cd9660_copy_file(FILE *, off_t, const char *);
346288943Sdim
347288943Sdimvoid	cd9660_compute_full_filename(cd9660node *, char *, int);
348288943Sdimint	cd9660_compute_record_size(cd9660node *);
349288943Sdim
350288943Sdim/* Debugging functions */
351288943Sdimvoid	debug_print_tree(cd9660node *,int);
352288943Sdimvoid	debug_print_path_tree(cd9660node *);
353288943Sdimvoid	debug_print_volume_descriptor_information(void);
354288943Sdimvoid	debug_dump_to_xml_ptentry(path_table_entry *,int, int);
355288943Sdimvoid	debug_dump_to_xml_path_table(FILE *, off_t, int, int);
356288943Sdimvoid	debug_dump_to_xml(FILE *);
357288943Sdimint	debug_get_encoded_number(unsigned char *, int);
358288943Sdimvoid	debug_dump_integer(const char *, char *,int);
359288943Sdimvoid	debug_dump_string(const char *,unsigned char *,int);
360288943Sdimvoid	debug_dump_directory_record_9_1(unsigned char *);
361288943Sdimvoid	debug_dump_to_xml_volume_descriptor(unsigned char *,int);
362288943Sdim
363288943Sdimvoid	cd9660_pad_string_spaces(char *, int);
364288943Sdim
365288943Sdim#endif
366288943Sdim