1224762Smarius/*	$NetBSD: cd9660.h,v 1.17 2011/06/23 02:35:56 enami Exp $	*/
2214921Scognet
3214921Scognet/*
4214921Scognet * Copyright (c) 2005 Daniel Watt, Walter Deignan, Ryan Gabrys, Alan
5214921Scognet * Perez-Rathke and Ram Vedam.  All rights reserved.
6214921Scognet *
7214921Scognet * This code was written by Daniel Watt, Walter Deignan, Ryan Gabrys,
8214921Scognet * Alan Perez-Rathke and Ram Vedam.
9214921Scognet *
10214921Scognet * Redistribution and use in source and binary forms, with or
11214921Scognet * without modification, are permitted provided that the following
12214921Scognet * conditions are met:
13214921Scognet * 1. Redistributions of source code must retain the above copyright
14214921Scognet *    notice, this list of conditions and the following disclaimer.
15214921Scognet * 2. Redistributions in binary form must reproduce the above
16214921Scognet *    copyright notice, this list of conditions and the following
17214921Scognet *    disclaimer in the documentation and/or other materials provided
18214921Scognet *    with the distribution.
19214921Scognet *
20214921Scognet * THIS SOFTWARE IS PROVIDED BY DANIEL WATT, WALTER DEIGNAN, RYAN
21214921Scognet * GABRYS, ALAN PEREZ-RATHKE AND RAM VEDAM ``AS IS'' AND ANY EXPRESS OR
22214921Scognet * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23214921Scognet * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24214921Scognet * DISCLAIMED.  IN NO EVENT SHALL DANIEL WATT, WALTER DEIGNAN, RYAN
25214921Scognet * GABRYS, ALAN PEREZ-RATHKE AND RAM VEDAM BE LIABLE FOR ANY DIRECT, INDIRECT,
26214921Scognet * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27214921Scognet * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
28214921Scognet * USE,DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29214921Scognet * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30214921Scognet * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31214921Scognet * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
32214921Scognet * OF SUCH DAMAGE.
33214921Scognet *
34214921Scognet * $FreeBSD$
35214921Scognet */
36214921Scognet
37214921Scognet#ifndef _MAKEFS_CD9660_H
38214921Scognet#define _MAKEFS_CD9660_H
39214921Scognet
40214921Scognet#include <inttypes.h>
41214921Scognet#include <assert.h>
42214921Scognet#include <errno.h>
43214921Scognet#include <fcntl.h>
44214921Scognet#include <stdarg.h>
45214921Scognet#include <stdio.h>
46214921Scognet#include <stdlib.h>
47214921Scognet#include <string.h>
48214921Scognet#include <unistd.h>
49214921Scognet#include <time.h>
50214921Scognet#include <limits.h>
51214921Scognet#include <sys/queue.h>
52214921Scognet#include <sys/param.h>
53224762Smarius#include <sys/endian.h>
54214921Scognet
55214921Scognet#include "makefs.h"
56214921Scognet#include "iso.h"
57214921Scognet#include "iso_rrip.h"
58214921Scognet#include "cd9660/cd9660_eltorito.h"
59214921Scognet
60214921Scognet#ifdef DEBUG
61214921Scognet#define	INODE_WARNX(__x)	warnx __x
62214921Scognet#else /* DEBUG */
63214921Scognet#define	INODE_WARNX(__x)
64214921Scognet#endif /* DEBUG */
65214921Scognet
66214921Scognet#define CD9660MAXPATH 4096
67214921Scognet
68214921Scognet#define ISO_STRING_FILTER_NONE = 0x00
69214921Scognet#define ISO_STRING_FILTER_DCHARS = 0x01
70214921Scognet#define ISO_STRING_FILTER_ACHARS = 0x02
71214921Scognet
72214921Scognet/*
73214921ScognetExtended preferences type, in the spirit of what makefs gives us (only ints)
74214921Scognet*/
75214921Scognettypedef struct {
76214921Scognet	const char  *shortName;		/* Short option */
77214921Scognet	const char	*name;		/* option name */
78214921Scognet	char		*value;		/* where to stuff the value */
79214921Scognet	int		minLength;	/* minimum for value */
80214921Scognet	int		maxLength;	/* maximum for value */
81214921Scognet	const char	*desc;		/* option description */
82214921Scognet	int		filterFlags;
83214921Scognet} string_option_t;
84214921Scognet
85214921Scognet/******** STRUCTURES **********/
86214921Scognet
87214921Scognet/*Defaults*/
88214921Scognet#define ISO_DEFAULT_VOLUMEID "MAKEFS_CD9660_IMAGE"
89214921Scognet#define ISO_DEFAULT_APPID "MAKEFS"
90214921Scognet#define ISO_DEFAULT_PUBLISHER "MAKEFS"
91214921Scognet#define ISO_DEFAULT_PREPARER "MAKEFS"
92214921Scognet
93214921Scognet#define ISO_VOLUME_DESCRIPTOR_STANDARD_ID "CD001"
94214921Scognet#define ISO_VOLUME_DESCRIPTOR_BOOT 0
95214921Scognet#define ISO_VOLUME_DESCRIPTOR_PVD 1
96214921Scognet#define ISO_VOLUME_DESCRIPTOR_TERMINATOR 255
97214921Scognet
98214921Scognet/*30 for name and extension, as well as version number and padding bit*/
99214921Scognet#define ISO_FILENAME_MAXLENGTH_BEFORE_VERSION 30
100214921Scognet#define ISO_FILENAME_MAXLENGTH	36
101214921Scognet#define ISO_FILENAME_MAXLENGTH_WITH_PADDING 37
102214921Scognet
103214921Scognet#define ISO_FLAG_CLEAR 0x00
104214921Scognet#define ISO_FLAG_HIDDEN 0x01
105214921Scognet#define ISO_FLAG_DIRECTORY 0x02
106214921Scognet#define ISO_FLAG_ASSOCIATED 0x04
107214921Scognet#define ISO_FLAG_PERMISSIONS 0x08
108214921Scognet#define ISO_FLAG_RESERVED5 0x10
109214921Scognet#define ISO_FLAG_RESERVED6 0x20
110214921Scognet#define ISO_FLAG_FINAL_RECORD 0x40
111214921Scognet
112214921Scognet#define ISO_PATHTABLE_ENTRY_BASESIZE 8
113214921Scognet
114214921Scognet#define ISO_RRIP_DEFAULT_MOVE_DIR_NAME "RR_MOVED"
115214921Scognet#define RRIP_DEFAULT_MOVE_DIR_NAME ".rr_moved"
116214921Scognet
117214921Scognet#define	CD9660_BLOCKS(__sector_size, __bytes)	\
118214921Scognet	howmany((__bytes), (__sector_size))
119214921Scognet
120214921Scognet#define CD9660_MEM_ALLOC_ERROR(_F)	\
121214921Scognet    err(EXIT_FAILURE, "%s, %s l. %d", _F, __FILE__, __LINE__)
122214921Scognet
123214921Scognet#define CD9660_IS_COMMAND_ARG_DUAL(var,short,long)\
124214921Scognet		(strcmp((var),(short)) == 0) || (strcmp((var),(long))==0)
125214921Scognet
126214921Scognet#define CD9660_IS_COMMAND_ARG(var,arg)\
127214921Scognet		(strcmp((var),(arg)) == 0)
128214921Scognet
129214921Scognet#define CD9660_TYPE_FILE	0x01
130214921Scognet#define CD9660_TYPE_DIR		0x02
131214921Scognet#define CD9660_TYPE_DOT		0x04
132214921Scognet#define CD9660_TYPE_DOTDOT	0x08
133214921Scognet#define CD9660_TYPE_VIRTUAL	0x80
134214921Scognet
135214921Scognet#define CD9660_INODE_HASH_SIZE	1024
136214921Scognet#define CD9660_SECTOR_SIZE	2048
137214921Scognet
138214921Scognet#define CD9660_END_PADDING	150
139214921Scognet
140214921Scognet/* Slight modification of the ISO structure in iso.h */
141214921Scognettypedef struct _iso_directory_record_cd9660 {
142214921Scognet	u_char length			[ISODCL (1, 1)];	/* 711 */
143214921Scognet	u_char ext_attr_length		[ISODCL (2, 2)];	/* 711 */
144214921Scognet	u_char extent			[ISODCL (3, 10)];	/* 733 */
145214921Scognet	u_char size			[ISODCL (11, 18)];	/* 733 */
146214921Scognet	u_char date			[ISODCL (19, 25)];	/* 7 by 711 */
147214921Scognet	u_char flags			[ISODCL (26, 26)];
148214921Scognet	u_char file_unit_size		[ISODCL (27, 27)];	/* 711 */
149214921Scognet	u_char interleave		[ISODCL (28, 28)];	/* 711 */
150214921Scognet	u_char volume_sequence_number	[ISODCL (29, 32)];	/* 723 */
151214921Scognet	u_char name_len			[ISODCL (33, 33)];	/* 711 */
152214921Scognet	char name			[ISO_FILENAME_MAXLENGTH_WITH_PADDING];
153214921Scognet} iso_directory_record_cd9660;
154214921Scognet
155214921Scognet/* TODO: Lots of optimization of this structure */
156214921Scognettypedef struct _cd9660node {
157214921Scognet	u_char	type;/* Used internally */
158214921Scognet	/* Tree structure */
159214921Scognet	struct _cd9660node	*parent;	/* parent (NULL if root) */
160214921Scognet	TAILQ_HEAD(cd9660_children_head, _cd9660node)	cn_children;
161214921Scognet	TAILQ_ENTRY(_cd9660node)		cn_next_child;
162214921Scognet
163214921Scognet	struct _cd9660node *dot_record; /* For directories, used mainly in RRIP */
164214921Scognet	struct _cd9660node *dot_dot_record;
165214921Scognet
166214921Scognet	fsnode		*node;		/* pointer to fsnode */
167214921Scognet	struct _iso_directory_record_cd9660	*isoDirRecord;
168214921Scognet	struct iso_extended_attributes	*isoExtAttributes;
169214921Scognet
170214921Scognet	/***** SIZE CALCULATION *****/
171214921Scognet	/*already stored in isoDirRecord, but this is an int version, and will be
172214921Scognet		copied to isoDirRecord on writing*/
173214921Scognet	uint32_t fileDataSector;
174214921Scognet
175214921Scognet	/*
176214921Scognet	 * same thing, though some notes:
177214921Scognet	 * If a file, this is the file size
178214921Scognet	 * If a directory, this is the size of all its children's
179214921Scognet	 *	directory records
180214921Scognet	 * plus necessary padding
181214921Scognet	 */
182214921Scognet	int64_t fileDataLength;
183214921Scognet
184214921Scognet	int64_t fileSectorsUsed;
185214921Scognet	int fileRecordSize;/*copy of a variable, int for quicker calculations*/
186214921Scognet
187214921Scognet	/* Old name, used for renaming - needs to be optimized but low priority */
188214921Scognet	char o_name [ISO_FILENAME_MAXLENGTH_WITH_PADDING];
189214921Scognet
190214921Scognet	/***** SPACE RESERVED FOR EXTENSIONS *****/
191214921Scognet	/* For memory efficiency's sake - we should move this to a separate struct
192214921Scognet		and point to null if not needed */
193214921Scognet	/* For Rock Ridge */
194214921Scognet	struct _cd9660node *rr_real_parent, *rr_relocated;
195214921Scognet
196214921Scognet	int64_t susp_entry_size;
197214921Scognet	int64_t susp_dot_entry_size;
198214921Scognet	int64_t susp_dot_dot_entry_size;
199214921Scognet
200214921Scognet	/* Continuation area stuff */
201214921Scognet	int64_t susp_entry_ce_start;
202214921Scognet	int64_t susp_dot_ce_start;
203214921Scognet	int64_t susp_dot_dot_ce_start;
204214921Scognet
205214921Scognet	int64_t susp_entry_ce_length;
206214921Scognet	int64_t susp_dot_ce_length;
207214921Scognet	int64_t susp_dot_dot_ce_length;
208214921Scognet
209214921Scognet	/* Data to put at the end of the System Use field */
210214921Scognet	int64_t su_tail_size;
211214921Scognet	char *su_tail_data;
212214921Scognet
213214921Scognet	/*** PATH TABLE STUFF ***/
214214921Scognet	int level;			/*depth*/
215214921Scognet	int ptnumber;
216214921Scognet	struct _cd9660node *ptnext, *ptprev, *ptlast;
217214921Scognet
218214921Scognet	/* SUSP entries */
219214921Scognet	TAILQ_HEAD(susp_linked_list, ISO_SUSP_ATTRIBUTES) head;
220214921Scognet} cd9660node;
221214921Scognet
222214921Scognettypedef struct _path_table_entry
223214921Scognet{
224214921Scognet	u_char length[ISODCL (1, 1)];
225214921Scognet	u_char extended_attribute_length[ISODCL (2, 2)];
226214921Scognet	u_char first_sector[ISODCL (3, 6)];
227214921Scognet	u_char parent_number[ISODCL (7, 8)];
228214921Scognet	u_char name[ISO_FILENAME_MAXLENGTH_WITH_PADDING];
229214921Scognet} path_table_entry;
230214921Scognet
231214921Scognettypedef struct _volume_descriptor
232214921Scognet{
233214921Scognet	u_char *volumeDescriptorData; /*ALWAYS 2048 bytes long*/
234214921Scognet	int64_t sector;
235214921Scognet	struct _volume_descriptor *next;
236214921Scognet} volume_descriptor;
237214921Scognet
238214921Scognettypedef struct _iso9660_disk {
239214921Scognet	int sectorSize;
240214921Scognet	struct iso_primary_descriptor		primaryDescriptor;
241214921Scognet	struct iso_supplementary_descriptor	supplementaryDescriptor;
242214921Scognet
243214921Scognet	volume_descriptor *firstVolumeDescriptor;
244214921Scognet
245214921Scognet	cd9660node *rootNode;
246214921Scognet
247214921Scognet	/* Important sector numbers here */
248214921Scognet	/* primaryDescriptor.type_l_path_table*/
249214921Scognet	int64_t primaryBigEndianTableSector;
250214921Scognet
251214921Scognet	/* primaryDescriptor.type_m_path_table*/
252214921Scognet	int64_t primaryLittleEndianTableSector;
253214921Scognet
254214921Scognet	/* primaryDescriptor.opt_type_l_path_table*/
255214921Scognet	int64_t secondaryBigEndianTableSector;
256214921Scognet
257214921Scognet	/* primaryDescriptor.opt_type_m_path_table*/
258214921Scognet	int64_t secondaryLittleEndianTableSector;
259214921Scognet
260214921Scognet	/* primaryDescriptor.path_table_size*/
261214921Scognet	int pathTableLength;
262214921Scognet	int64_t dataFirstSector;
263214921Scognet
264214921Scognet	int64_t totalSectors;
265214921Scognet	/* OPTIONS GO HERE */
266214921Scognet	int	isoLevel;
267214921Scognet
268214921Scognet	int include_padding_areas;
269214921Scognet
270214921Scognet	int follow_sym_links;
271214921Scognet	int verbose_level;
272214921Scognet	int displayHelp;
273214921Scognet	int keep_bad_images;
274214921Scognet
275214921Scognet	/* SUSP options and variables */
276214921Scognet	int64_t susp_continuation_area_start_sector;
277214921Scognet	int64_t susp_continuation_area_size;
278214921Scognet	int64_t susp_continuation_area_current_free;
279214921Scognet
280214921Scognet	int rock_ridge_enabled;
281214921Scognet	/* Other Rock Ridge Variables */
282214921Scognet	char *rock_ridge_renamed_dir_name;
283214921Scognet	int rock_ridge_move_count;
284214921Scognet	cd9660node *rr_moved_dir;
285214921Scognet
286214921Scognet	int archimedes_enabled;
287222191Snwhitehorn	int chrp_boot;
288214921Scognet
289214921Scognet	/* Spec breaking options */
290214921Scognet	u_char allow_deep_trees;
291214921Scognet	u_char allow_start_dot;
292214921Scognet	u_char allow_max_name; /* Allow 37 char filenames*/
293214921Scognet	u_char allow_illegal_chars; /* ~, !, # */
294214921Scognet	u_char allow_lowercase;
295214921Scognet	u_char allow_multidot;
296214921Scognet	u_char omit_trailing_period;
297214921Scognet
298214921Scognet	/* BOOT INFORMATION HERE */
299214921Scognet	int has_generic_bootimage; /* Default to 0 */
300214921Scognet	char *generic_bootimage;
301214921Scognet
302214921Scognet	int is_bootable;/* Default to 0 */
303214921Scognet	int64_t boot_catalog_sector;
304214921Scognet	boot_volume_descriptor *boot_descriptor;
305214921Scognet	char * boot_image_directory;
306214921Scognet
307214921Scognet	TAILQ_HEAD(boot_image_list,cd9660_boot_image) boot_images;
308214921Scognet	int image_serialno;
309214921Scognet	LIST_HEAD(boot_catalog_entries,boot_catalog_entry) boot_entries;
310214921Scognet
311214921Scognet} iso9660_disk;
312214921Scognet
313214921Scognet/******** GLOBAL VARIABLES ***********/
314214921Scognetextern iso9660_disk diskStructure;
315214921Scognet
316214921Scognet/************ FUNCTIONS **************/
317214921Scognetint			cd9660_valid_a_chars(const char *);
318214921Scognetint			cd9660_valid_d_chars(const char *);
319214921Scognetvoid			cd9660_uppercase_characters(char *, int);
320214921Scognet
321214921Scognet/* ISO Data Types */
322214921Scognetvoid			cd9660_721(uint16_t, unsigned char *);
323214921Scognetvoid			cd9660_731(uint32_t, unsigned char *);
324214921Scognetvoid			cd9660_722(uint16_t, unsigned char *);
325214921Scognetvoid			cd9660_732(uint32_t, unsigned char *);
326214921Scognetvoid 			cd9660_bothendian_dword(uint32_t dw, unsigned char *);
327214921Scognetvoid 			cd9660_bothendian_word(uint16_t dw, unsigned char *);
328214921Scognetvoid			cd9660_set_date(char *, time_t);
329214921Scognetvoid			cd9660_time_8426(unsigned char *, time_t);
330214921Scognetvoid			cd9660_time_915(unsigned char *, time_t);
331214921Scognet
332214921Scognet/*** Boot Functions ***/
333214921Scognetint	cd9660_write_generic_bootimage(FILE *);
334214921Scognetint	cd9660_add_generic_bootimage(const char *);
335214921Scognetint	cd9660_write_boot(FILE *);
336214921Scognetint	cd9660_add_boot_disk(const char *);
337214921Scognetint	cd9660_eltorito_add_boot_option(const char *, const char *);
338214921Scognetint	cd9660_setup_boot(int);
339214921Scognetint	cd9660_setup_boot_volume_descriptor(volume_descriptor *);
340214921Scognet
341214921Scognet
342214921Scognet/*** Write Functions ***/
343214921Scognetint	cd9660_write_image(const char *image);
344214921Scognetint	cd9660_copy_file(FILE *, off_t, const char *);
345214921Scognet
346230795Sjkimvoid	cd9660_compute_full_filename(cd9660node *, char *);
347214921Scognetint	cd9660_compute_record_size(cd9660node *);
348214921Scognet
349214921Scognet/* Debugging functions */
350214921Scognetvoid	debug_print_tree(cd9660node *,int);
351214921Scognetvoid	debug_print_path_tree(cd9660node *);
352214921Scognetvoid	debug_print_volume_descriptor_information(void);
353214921Scognetvoid	debug_dump_to_xml_ptentry(path_table_entry *,int, int);
354214921Scognetvoid	debug_dump_to_xml_path_table(FILE *, off_t, int, int);
355214921Scognetvoid	debug_dump_to_xml(FILE *);
356214921Scognetint	debug_get_encoded_number(unsigned char *, int);
357214921Scognetvoid	debug_dump_integer(const char *, char *,int);
358214921Scognetvoid	debug_dump_string(const char *,unsigned char *,int);
359214921Scognetvoid	debug_dump_directory_record_9_1(unsigned char *);
360214921Scognetvoid	debug_dump_to_xml_volume_descriptor(unsigned char *,int);
361214921Scognet
362214921Scognetvoid	cd9660_pad_string_spaces(char *, int);
363214921Scognet
364214921Scognet#endif
365