1*c316d7b2Schristos /* $NetBSD: cd9660.h,v 1.21 2015/12/24 15:52:37 christos Exp $ */ 23550dc98Sfvdl 33550dc98Sfvdl /* 43550dc98Sfvdl * Copyright (c) 2005 Daniel Watt, Walter Deignan, Ryan Gabrys, Alan 53550dc98Sfvdl * Perez-Rathke and Ram Vedam. All rights reserved. 63550dc98Sfvdl * 73550dc98Sfvdl * This code was written by Daniel Watt, Walter Deignan, Ryan Gabrys, 83550dc98Sfvdl * Alan Perez-Rathke and Ram Vedam. 93550dc98Sfvdl * 103550dc98Sfvdl * Redistribution and use in source and binary forms, with or 113550dc98Sfvdl * without modification, are permitted provided that the following 123550dc98Sfvdl * conditions are met: 133550dc98Sfvdl * 1. Redistributions of source code must retain the above copyright 143550dc98Sfvdl * notice, this list of conditions and the following disclaimer. 153550dc98Sfvdl * 2. Redistributions in binary form must reproduce the above 163550dc98Sfvdl * copyright notice, this list of conditions and the following 173550dc98Sfvdl * disclaimer in the documentation and/or other materials provided 183550dc98Sfvdl * with the distribution. 193550dc98Sfvdl * 203550dc98Sfvdl * THIS SOFTWARE IS PROVIDED BY DANIEL WATT, WALTER DEIGNAN, RYAN 213550dc98Sfvdl * GABRYS, ALAN PEREZ-RATHKE AND RAM VEDAM ``AS IS'' AND ANY EXPRESS OR 223550dc98Sfvdl * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 233550dc98Sfvdl * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 243550dc98Sfvdl * DISCLAIMED. IN NO EVENT SHALL DANIEL WATT, WALTER DEIGNAN, RYAN 253550dc98Sfvdl * GABRYS, ALAN PEREZ-RATHKE AND RAM VEDAM BE LIABLE FOR ANY DIRECT, INDIRECT, 263550dc98Sfvdl * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 273550dc98Sfvdl * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 283550dc98Sfvdl * USE,DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 293550dc98Sfvdl * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 303550dc98Sfvdl * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 313550dc98Sfvdl * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 323550dc98Sfvdl * OF SUCH DAMAGE. 333550dc98Sfvdl */ 343550dc98Sfvdl 353550dc98Sfvdl #ifndef _MAKEFS_CD9660_H 363550dc98Sfvdl #define _MAKEFS_CD9660_H 373550dc98Sfvdl 3865b1705eSfvdl #if HAVE_NBTOOL_CONFIG_H 3965b1705eSfvdl #include "nbtool_config.h" 4065b1705eSfvdl #endif 4165b1705eSfvdl 423550dc98Sfvdl #include <assert.h> 433550dc98Sfvdl #include <errno.h> 443550dc98Sfvdl #include <fcntl.h> 453550dc98Sfvdl #include <stdarg.h> 463550dc98Sfvdl #include <stdio.h> 473550dc98Sfvdl #include <stdlib.h> 483550dc98Sfvdl #include <string.h> 493550dc98Sfvdl #include <unistd.h> 503550dc98Sfvdl #include <time.h> 513550dc98Sfvdl #include <limits.h> 523550dc98Sfvdl #include <sys/queue.h> 533550dc98Sfvdl #include <sys/param.h> 540b23c5aaSenami #include <sys/endian.h> 553550dc98Sfvdl 563550dc98Sfvdl #include "makefs.h" 579d2dd62aSdyoung #include "iso.h" 589d2dd62aSdyoung #include "iso_rrip.h" 593550dc98Sfvdl #include "cd9660/cd9660_eltorito.h" 603550dc98Sfvdl 611da54664Sdyoung #ifdef DEBUG 621da54664Sdyoung #define INODE_WARNX(__x) warnx __x 631da54664Sdyoung #else /* DEBUG */ 641da54664Sdyoung #define INODE_WARNX(__x) 651da54664Sdyoung #endif /* DEBUG */ 663550dc98Sfvdl 673550dc98Sfvdl #define CD9660MAXPATH 4096 683550dc98Sfvdl 693550dc98Sfvdl #define ISO_STRING_FILTER_NONE = 0x00 703550dc98Sfvdl #define ISO_STRING_FILTER_DCHARS = 0x01 713550dc98Sfvdl #define ISO_STRING_FILTER_ACHARS = 0x02 723550dc98Sfvdl 733550dc98Sfvdl /* 743550dc98Sfvdl Extended preferences type, in the spirit of what makefs gives us (only ints) 753550dc98Sfvdl */ 763550dc98Sfvdl typedef struct { 773550dc98Sfvdl const char *shortName; /* Short option */ 783550dc98Sfvdl const char *name; /* option name */ 793550dc98Sfvdl char *value; /* where to stuff the value */ 803550dc98Sfvdl int minLength; /* minimum for value */ 813550dc98Sfvdl int maxLength; /* maximum for value */ 823550dc98Sfvdl const char *desc; /* option description */ 833550dc98Sfvdl int filterFlags; 843550dc98Sfvdl } string_option_t; 853550dc98Sfvdl 863550dc98Sfvdl /******** STRUCTURES **********/ 873550dc98Sfvdl 883550dc98Sfvdl /*Defaults*/ 893550dc98Sfvdl #define ISO_DEFAULT_VOLUMEID "MAKEFS_CD9660_IMAGE" 903550dc98Sfvdl #define ISO_DEFAULT_APPID "MAKEFS" 913550dc98Sfvdl #define ISO_DEFAULT_PUBLISHER "MAKEFS" 923550dc98Sfvdl #define ISO_DEFAULT_PREPARER "MAKEFS" 933550dc98Sfvdl 943550dc98Sfvdl #define ISO_VOLUME_DESCRIPTOR_STANDARD_ID "CD001" 953550dc98Sfvdl #define ISO_VOLUME_DESCRIPTOR_BOOT 0 963550dc98Sfvdl #define ISO_VOLUME_DESCRIPTOR_PVD 1 973550dc98Sfvdl #define ISO_VOLUME_DESCRIPTOR_TERMINATOR 255 983550dc98Sfvdl 993550dc98Sfvdl /*30 for name and extension, as well as version number and padding bit*/ 1003550dc98Sfvdl #define ISO_FILENAME_MAXLENGTH_BEFORE_VERSION 30 1013550dc98Sfvdl #define ISO_FILENAME_MAXLENGTH 36 1023550dc98Sfvdl #define ISO_FILENAME_MAXLENGTH_WITH_PADDING 37 1033550dc98Sfvdl 1043550dc98Sfvdl #define ISO_FLAG_CLEAR 0x00 1053550dc98Sfvdl #define ISO_FLAG_HIDDEN 0x01 1063550dc98Sfvdl #define ISO_FLAG_DIRECTORY 0x02 1073550dc98Sfvdl #define ISO_FLAG_ASSOCIATED 0x04 1083550dc98Sfvdl #define ISO_FLAG_PERMISSIONS 0x08 1093550dc98Sfvdl #define ISO_FLAG_RESERVED5 0x10 1103550dc98Sfvdl #define ISO_FLAG_RESERVED6 0x20 1113550dc98Sfvdl #define ISO_FLAG_FINAL_RECORD 0x40 1123550dc98Sfvdl 1133550dc98Sfvdl #define ISO_PATHTABLE_ENTRY_BASESIZE 8 1143550dc98Sfvdl 1153550dc98Sfvdl #define ISO_RRIP_DEFAULT_MOVE_DIR_NAME "RR_MOVED" 1163550dc98Sfvdl #define RRIP_DEFAULT_MOVE_DIR_NAME ".rr_moved" 1173550dc98Sfvdl 1183c2cee9dSdyoung #define CD9660_BLOCKS(__sector_size, __bytes) \ 1193c2cee9dSdyoung howmany((__bytes), (__sector_size)) 1203550dc98Sfvdl 121689c61f6Sdyoung #define CD9660_MEM_ALLOC_ERROR(_F) \ 122689c61f6Sdyoung err(EXIT_FAILURE, "%s, %s l. %d", _F, __FILE__, __LINE__) 1233550dc98Sfvdl 1243550dc98Sfvdl #define CD9660_TYPE_FILE 0x01 1253550dc98Sfvdl #define CD9660_TYPE_DIR 0x02 1263550dc98Sfvdl #define CD9660_TYPE_DOT 0x04 1273550dc98Sfvdl #define CD9660_TYPE_DOTDOT 0x08 1283550dc98Sfvdl #define CD9660_TYPE_VIRTUAL 0x80 1293550dc98Sfvdl 1301da54664Sdyoung #define CD9660_INODE_HASH_SIZE 1024 131203850f0Schristos #define CD9660_SECTOR_SIZE 2048 1323550dc98Sfvdl 1333550dc98Sfvdl #define CD9660_END_PADDING 150 1343550dc98Sfvdl 1353550dc98Sfvdl /* Slight modification of the ISO structure in iso.h */ 1363550dc98Sfvdl typedef struct _iso_directory_record_cd9660 { 1373550dc98Sfvdl u_char length [ISODCL (1, 1)]; /* 711 */ 1383550dc98Sfvdl u_char ext_attr_length [ISODCL (2, 2)]; /* 711 */ 1393550dc98Sfvdl u_char extent [ISODCL (3, 10)]; /* 733 */ 1403550dc98Sfvdl u_char size [ISODCL (11, 18)]; /* 733 */ 1413550dc98Sfvdl u_char date [ISODCL (19, 25)]; /* 7 by 711 */ 1423550dc98Sfvdl u_char flags [ISODCL (26, 26)]; 1433550dc98Sfvdl u_char file_unit_size [ISODCL (27, 27)]; /* 711 */ 1443550dc98Sfvdl u_char interleave [ISODCL (28, 28)]; /* 711 */ 1453550dc98Sfvdl u_char volume_sequence_number [ISODCL (29, 32)]; /* 723 */ 1463550dc98Sfvdl u_char name_len [ISODCL (33, 33)]; /* 711 */ 1473550dc98Sfvdl char name [ISO_FILENAME_MAXLENGTH_WITH_PADDING]; 1483550dc98Sfvdl } iso_directory_record_cd9660; 1493550dc98Sfvdl 1503550dc98Sfvdl /* TODO: Lots of optimization of this structure */ 1513550dc98Sfvdl typedef struct _cd9660node { 1523550dc98Sfvdl u_char type;/* Used internally */ 1533550dc98Sfvdl /* Tree structure */ 1543550dc98Sfvdl struct _cd9660node *parent; /* parent (NULL if root) */ 155689c61f6Sdyoung TAILQ_HEAD(cd9660_children_head, _cd9660node) cn_children; 156689c61f6Sdyoung TAILQ_ENTRY(_cd9660node) cn_next_child; 1573550dc98Sfvdl 1583550dc98Sfvdl struct _cd9660node *dot_record; /* For directories, used mainly in RRIP */ 1593550dc98Sfvdl struct _cd9660node *dot_dot_record; 1603550dc98Sfvdl 1613550dc98Sfvdl fsnode *node; /* pointer to fsnode */ 1623550dc98Sfvdl struct _iso_directory_record_cd9660 *isoDirRecord; 1633550dc98Sfvdl struct iso_extended_attributes *isoExtAttributes; 1643550dc98Sfvdl 1653550dc98Sfvdl /***** SIZE CALCULATION *****/ 1663550dc98Sfvdl /*already stored in isoDirRecord, but this is an int version, and will be 1673550dc98Sfvdl copied to isoDirRecord on writing*/ 1682fa0e02bSdyoung uint32_t fileDataSector; 1693550dc98Sfvdl 1703550dc98Sfvdl /* 1713550dc98Sfvdl * same thing, though some notes: 1723550dc98Sfvdl * If a file, this is the file size 1733550dc98Sfvdl * If a directory, this is the size of all its children's 1743550dc98Sfvdl * directory records 1753550dc98Sfvdl * plus necessary padding 1763550dc98Sfvdl */ 1773550dc98Sfvdl int64_t fileDataLength; 1783550dc98Sfvdl 1792ec6e0f1Schristos int64_t fileSectorsUsed; 1803550dc98Sfvdl int fileRecordSize;/*copy of a variable, int for quicker calculations*/ 1813550dc98Sfvdl 1823550dc98Sfvdl /* Old name, used for renaming - needs to be optimized but low priority */ 1833550dc98Sfvdl char o_name [ISO_FILENAME_MAXLENGTH_WITH_PADDING]; 1843550dc98Sfvdl 1853550dc98Sfvdl /***** SPACE RESERVED FOR EXTENSIONS *****/ 1863550dc98Sfvdl /* For memory efficiency's sake - we should move this to a separate struct 1873550dc98Sfvdl and point to null if not needed */ 1883550dc98Sfvdl /* For Rock Ridge */ 1893550dc98Sfvdl struct _cd9660node *rr_real_parent, *rr_relocated; 1903550dc98Sfvdl 1912ec6e0f1Schristos int64_t susp_entry_size; 1922ec6e0f1Schristos int64_t susp_dot_entry_size; 1932ec6e0f1Schristos int64_t susp_dot_dot_entry_size; 1943550dc98Sfvdl 1953550dc98Sfvdl /* Continuation area stuff */ 1962ec6e0f1Schristos int64_t susp_entry_ce_start; 1972ec6e0f1Schristos int64_t susp_dot_ce_start; 1982ec6e0f1Schristos int64_t susp_dot_dot_ce_start; 1993550dc98Sfvdl 2002ec6e0f1Schristos int64_t susp_entry_ce_length; 2012ec6e0f1Schristos int64_t susp_dot_ce_length; 2022ec6e0f1Schristos int64_t susp_dot_dot_ce_length; 2033550dc98Sfvdl 204f8474b32Sbjh21 /* Data to put at the end of the System Use field */ 2052ec6e0f1Schristos int64_t su_tail_size; 206f8474b32Sbjh21 char *su_tail_data; 207f8474b32Sbjh21 2083550dc98Sfvdl /*** PATH TABLE STUFF ***/ 2093550dc98Sfvdl int level; /*depth*/ 2103550dc98Sfvdl int ptnumber; 2113550dc98Sfvdl struct _cd9660node *ptnext, *ptprev, *ptlast; 2123550dc98Sfvdl 2133550dc98Sfvdl /* SUSP entries */ 214689c61f6Sdyoung TAILQ_HEAD(susp_linked_list, ISO_SUSP_ATTRIBUTES) head; 2153550dc98Sfvdl } cd9660node; 2163550dc98Sfvdl 2173550dc98Sfvdl typedef struct _path_table_entry 2183550dc98Sfvdl { 2193550dc98Sfvdl u_char length[ISODCL (1, 1)]; 2203550dc98Sfvdl u_char extended_attribute_length[ISODCL (2, 2)]; 2213550dc98Sfvdl u_char first_sector[ISODCL (3, 6)]; 2223550dc98Sfvdl u_char parent_number[ISODCL (7, 8)]; 2233550dc98Sfvdl u_char name[ISO_FILENAME_MAXLENGTH_WITH_PADDING]; 2243550dc98Sfvdl } path_table_entry; 2253550dc98Sfvdl 2263550dc98Sfvdl typedef struct _volume_descriptor 2273550dc98Sfvdl { 2283550dc98Sfvdl u_char *volumeDescriptorData; /*ALWAYS 2048 bytes long*/ 2292ec6e0f1Schristos int64_t sector; 2303550dc98Sfvdl struct _volume_descriptor *next; 2313550dc98Sfvdl } volume_descriptor; 2323550dc98Sfvdl 2333550dc98Sfvdl typedef struct _iso9660_disk { 2343550dc98Sfvdl int sectorSize; 2353550dc98Sfvdl struct iso_primary_descriptor primaryDescriptor; 2363550dc98Sfvdl struct iso_supplementary_descriptor supplementaryDescriptor; 2373550dc98Sfvdl 2383550dc98Sfvdl volume_descriptor *firstVolumeDescriptor; 2393550dc98Sfvdl 2403550dc98Sfvdl cd9660node *rootNode; 2411da54664Sdyoung 2423550dc98Sfvdl /* Important sector numbers here */ 2433550dc98Sfvdl /* primaryDescriptor.type_l_path_table*/ 2442ec6e0f1Schristos int64_t primaryBigEndianTableSector; 2453550dc98Sfvdl 2463550dc98Sfvdl /* primaryDescriptor.type_m_path_table*/ 2472ec6e0f1Schristos int64_t primaryLittleEndianTableSector; 2483550dc98Sfvdl 2493550dc98Sfvdl /* primaryDescriptor.opt_type_l_path_table*/ 2502ec6e0f1Schristos int64_t secondaryBigEndianTableSector; 2513550dc98Sfvdl 2523550dc98Sfvdl /* primaryDescriptor.opt_type_m_path_table*/ 2532ec6e0f1Schristos int64_t secondaryLittleEndianTableSector; 2543550dc98Sfvdl 2553550dc98Sfvdl /* primaryDescriptor.path_table_size*/ 2563550dc98Sfvdl int pathTableLength; 2572ec6e0f1Schristos int64_t dataFirstSector; 2583550dc98Sfvdl 2592ec6e0f1Schristos int64_t totalSectors; 2603550dc98Sfvdl /* OPTIONS GO HERE */ 2613550dc98Sfvdl int isoLevel; 2623550dc98Sfvdl 2633550dc98Sfvdl int include_padding_areas; 2643550dc98Sfvdl 2653550dc98Sfvdl int follow_sym_links; 2663550dc98Sfvdl int verbose_level; 2673550dc98Sfvdl int displayHelp; 2683550dc98Sfvdl int keep_bad_images; 2693550dc98Sfvdl 2703550dc98Sfvdl /* SUSP options and variables */ 2712ec6e0f1Schristos int64_t susp_continuation_area_start_sector; 2722ec6e0f1Schristos int64_t susp_continuation_area_size; 2732ec6e0f1Schristos int64_t susp_continuation_area_current_free; 2743550dc98Sfvdl 2753550dc98Sfvdl int rock_ridge_enabled; 2763550dc98Sfvdl /* Other Rock Ridge Variables */ 2773550dc98Sfvdl char *rock_ridge_renamed_dir_name; 2783550dc98Sfvdl int rock_ridge_move_count; 2793550dc98Sfvdl cd9660node *rr_moved_dir; 2803550dc98Sfvdl 281f8474b32Sbjh21 int archimedes_enabled; 2820477909eSchristos int chrp_boot; 2833550dc98Sfvdl 2843550dc98Sfvdl /* Spec breaking options */ 2853550dc98Sfvdl u_char allow_deep_trees; 2863550dc98Sfvdl u_char allow_start_dot; 2873550dc98Sfvdl u_char allow_max_name; /* Allow 37 char filenames*/ 2883550dc98Sfvdl u_char allow_illegal_chars; /* ~, !, # */ 2893550dc98Sfvdl u_char allow_lowercase; 2903550dc98Sfvdl u_char allow_multidot; 2913550dc98Sfvdl u_char omit_trailing_period; 2923550dc98Sfvdl 2933550dc98Sfvdl /* BOOT INFORMATION HERE */ 294989d6357Sskrll int has_generic_bootimage; /* Default to 0 */ 295989d6357Sskrll char *generic_bootimage; 296989d6357Sskrll 2973550dc98Sfvdl int is_bootable;/* Default to 0 */ 2982ec6e0f1Schristos int64_t boot_catalog_sector; 2993550dc98Sfvdl boot_volume_descriptor *boot_descriptor; 3003550dc98Sfvdl char * boot_image_directory; 3013550dc98Sfvdl 3023c2cee9dSdyoung TAILQ_HEAD(boot_image_list,cd9660_boot_image) boot_images; 3034a3fce43Sdyoung int image_serialno; 3043550dc98Sfvdl LIST_HEAD(boot_catalog_entries,boot_catalog_entry) boot_entries; 3053550dc98Sfvdl 3063550dc98Sfvdl } iso9660_disk; 3073550dc98Sfvdl 3083550dc98Sfvdl /************ FUNCTIONS **************/ 3093550dc98Sfvdl int cd9660_valid_a_chars(const char *); 3103550dc98Sfvdl int cd9660_valid_d_chars(const char *); 311*c316d7b2Schristos void cd9660_uppercase_characters(char *, size_t); 3123550dc98Sfvdl 3133550dc98Sfvdl /* ISO Data Types */ 3143550dc98Sfvdl void cd9660_721(uint16_t, unsigned char *); 3153550dc98Sfvdl void cd9660_731(uint32_t, unsigned char *); 3163550dc98Sfvdl void cd9660_722(uint16_t, unsigned char *); 3173550dc98Sfvdl void cd9660_732(uint32_t, unsigned char *); 3183550dc98Sfvdl void cd9660_bothendian_dword(uint32_t dw, unsigned char *); 3193550dc98Sfvdl void cd9660_bothendian_word(uint16_t dw, unsigned char *); 3203550dc98Sfvdl void cd9660_set_date(char *, time_t); 3213550dc98Sfvdl void cd9660_time_8426(unsigned char *, time_t); 3223550dc98Sfvdl void cd9660_time_915(unsigned char *, time_t); 3233550dc98Sfvdl 3243550dc98Sfvdl /*** Boot Functions ***/ 325989d6357Sskrll int cd9660_write_generic_bootimage(FILE *); 326e4989541Schristos int cd9660_write_boot(iso9660_disk *, FILE *); 327e4989541Schristos int cd9660_add_boot_disk(iso9660_disk *, const char *); 328e4989541Schristos int cd9660_eltorito_add_boot_option(iso9660_disk *, const char *, 329e4989541Schristos const char *); 330e4989541Schristos int cd9660_setup_boot(iso9660_disk *, int); 331e4989541Schristos int cd9660_setup_boot_volume_descriptor(iso9660_disk *, 332e4989541Schristos volume_descriptor *); 3333550dc98Sfvdl 3343550dc98Sfvdl 3353550dc98Sfvdl /*** Write Functions ***/ 336e4989541Schristos int cd9660_write_image(iso9660_disk *, const char *image); 337e4989541Schristos int cd9660_copy_file(iso9660_disk *, FILE *, off_t, const char *); 3383550dc98Sfvdl 339f1cc0951Schristos void cd9660_compute_full_filename(cd9660node *, char *); 340e4989541Schristos int cd9660_compute_record_size(iso9660_disk *, cd9660node *); 3413550dc98Sfvdl 3423550dc98Sfvdl /* Debugging functions */ 343e4989541Schristos void debug_print_tree(iso9660_disk *, cd9660node *,int); 3443550dc98Sfvdl void debug_print_path_tree(cd9660node *); 345e4989541Schristos void debug_print_volume_descriptor_information(iso9660_disk *); 3463550dc98Sfvdl void debug_dump_to_xml_ptentry(path_table_entry *,int, int); 347203850f0Schristos void debug_dump_to_xml_path_table(FILE *, off_t, int, int); 3483550dc98Sfvdl void debug_dump_to_xml(FILE *); 3493550dc98Sfvdl int debug_get_encoded_number(unsigned char *, int); 3503550dc98Sfvdl void debug_dump_integer(const char *, char *,int); 3513550dc98Sfvdl void debug_dump_string(const char *,unsigned char *,int); 3523550dc98Sfvdl void debug_dump_directory_record_9_1(unsigned char *); 3533550dc98Sfvdl void debug_dump_to_xml_volume_descriptor(unsigned char *,int); 3543550dc98Sfvdl 3553550dc98Sfvdl void cd9660_pad_string_spaces(char *, int); 3563550dc98Sfvdl 3573550dc98Sfvdl #endif 358