1 /* $NetBSD: udf_create.h,v 1.7 2013/08/09 15:11:08 reinoud Exp $ */ 2 3 /* 4 * Copyright (c) 2006, 2008 Reinoud Zandijk 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 * 27 */ 28 29 #ifndef _FS_UDF_UDF_CREATE_H_ 30 #define _FS_UDF_UDF_CREATE_H_ 31 32 #include <sys/types.h> 33 #include <sys/stat.h> 34 #if !HAVE_NBTOOL_CONFIG_H 35 #include <fs/udf/ecma167-udf.h> 36 #else 37 #include "../../sys/fs/udf/ecma167-udf.h" 38 #endif 39 #include "udf_bswap.h" 40 #include "udf_osta.h" 41 42 43 /* format flags indicating properties of disc to create */ 44 #define FORMAT_WRITEONCE 0x00001 45 #define FORMAT_SEQUENTIAL 0x00002 46 #define FORMAT_REWRITABLE 0x00004 47 #define FORMAT_SPARABLE 0x00008 48 #define FORMAT_META 0x00010 49 #define FORMAT_LOW 0x00020 50 #define FORMAT_VAT 0x00040 51 #define FORMAT_WORM 0x00080 52 #define FORMAT_TRACK512 0x00100 53 #define FORMAT_INVALID 0x00200 54 #define FORMAT_READONLY 0x00400 55 #define FORMAT_FLAGBITS \ 56 "\10\1WRITEONCE\2SEQUENTIAL\3REWRITABLE\4SPARABLE\5META\6LOW" \ 57 "\7VAT\10WORM\11TRACK512\12INVALID\13READONLY" 58 59 60 /* structure space */ 61 #define UDF_ANCHORS 4 /* 256, 512, N-256, N */ 62 #define UDF_PARTITIONS 4 /* overkill */ 63 #define UDF_PMAPS 4 /* overkill */ 64 65 /* misc constants */ 66 #define UDF_MAX_NAMELEN 255 /* as per SPEC */ 67 68 /* translation constants */ 69 #define UDF_VTOP_RAWPART UDF_PMAPS /* [0..UDF_PMAPS> are normal */ 70 71 /* virtual to physical mapping types */ 72 #define UDF_VTOP_TYPE_RAW 0 73 #define UDF_VTOP_TYPE_UNKNOWN 0 74 #define UDF_VTOP_TYPE_PHYS 1 75 #define UDF_VTOP_TYPE_VIRT 2 76 #define UDF_VTOP_TYPE_SPARABLE 3 77 #define UDF_VTOP_TYPE_META 4 78 79 #define UDF_TRANS_ZERO ((uint64_t) -1) 80 #define UDF_TRANS_UNMAPPED ((uint64_t) -2) 81 #define UDF_TRANS_INTERN ((uint64_t) -3) 82 #define UDF_MAX_SECTOR ((uint64_t) -10) /* high water mark */ 83 84 /* handys */ 85 #define UDF_ROUNDUP(val, gran) \ 86 ((uint64_t) (gran) * (((uint64_t)(val) + (gran)-1) / (gran))) 87 88 #define UDF_ROUNDDOWN(val, gran) \ 89 ((uint64_t) (gran) * (((uint64_t)(val)) / (gran))) 90 91 92 /* disc offsets for various structures and their sizes */ 93 struct udf_disclayout { 94 uint32_t wrtrack_skew; 95 96 uint32_t iso9660_vrs; 97 uint32_t anchors[UDF_ANCHORS]; 98 uint32_t vds_size, vds1, vds2; 99 uint32_t lvis_size, lvis; 100 101 uint32_t first_lba, last_lba; 102 uint32_t sector_size; 103 uint32_t blockingnr, align_blockingnr, sparable_blockingnr; 104 uint32_t meta_blockingnr, meta_alignment; 105 106 /* sparables */ 107 uint32_t sparable_blocks; 108 uint32_t sparable_area, sparable_area_size; 109 uint32_t sparing_table_dscr_lbas; 110 uint32_t spt_1, spt_2; 111 112 /* bitmaps */ 113 uint32_t alloc_bitmap_dscr_size; 114 uint32_t unalloc_space, freed_space; 115 116 uint32_t meta_bitmap_dscr_size; 117 uint32_t meta_bitmap_space; 118 119 /* metadata partition */ 120 uint32_t meta_file, meta_mirror, meta_bitmap; 121 uint32_t meta_part_start_lba, meta_part_size_lba; 122 123 /* main partition */ 124 uint32_t part_start_lba, part_size_lba; 125 126 uint32_t fsd, rootdir, vat; 127 128 }; 129 130 131 /* all info about discs and descriptors building */ 132 struct udf_create_context { 133 /* descriptors */ 134 int dscrver; /* 2 or 3 */ 135 int min_udf; /* hex */ 136 int max_udf; /* hex */ 137 int serialnum; /* format serialno */ 138 139 int gmtoff; /* in minutes */ 140 141 /* XXX to layout? */ 142 uint32_t sector_size; 143 144 /* identification */ 145 char *logvol_name; 146 char *primary_name; 147 char *volset_name; 148 char *fileset_name; 149 150 char const *app_name; 151 char const *impl_name; 152 int app_version_main; 153 int app_version_sub; 154 155 /* building */ 156 int vds_seq; /* for building functions */ 157 int unique_id; /* only first few are used */ 158 159 /* constructed structures */ 160 struct anchor_vdp *anchors[UDF_ANCHORS]; /* anchors to VDS */ 161 struct pri_vol_desc *primary_vol; /* identification */ 162 struct logvol_desc *logical_vol; /* main mapping v->p */ 163 struct unalloc_sp_desc *unallocated; /* free UDF space */ 164 struct impvol_desc *implementation; /* likely reduntant */ 165 struct logvol_int_desc *logvol_integrity; /* current integrity */ 166 struct part_desc *partitions[UDF_PARTITIONS]; /* partitions */ 167 168 /* XXX to layout? */ 169 int data_part; 170 int metadata_part; 171 172 /* block numbers as offset in partition */ 173 uint32_t metadata_alloc_pos; 174 uint32_t data_alloc_pos; 175 176 /* derived; points *into* other structures */ 177 struct udf_logvol_info *logvol_info; /* inside integrity */ 178 179 /* fileset and root directories */ 180 struct fileset_desc *fileset_desc; /* normally one */ 181 182 /* logical to physical translations */ 183 int vtop[UDF_PMAPS+1]; /* vpartnr trans */ 184 int vtop_tp[UDF_PMAPS+1]; /* type of trans */ 185 uint64_t vtop_offset[UDF_PMAPS+1]; /* offset in lb */ 186 187 /* sparable */ 188 struct udf_sparing_table*sparing_table; /* replacements */ 189 190 /* VAT file */ 191 uint32_t vat_size; /* length */ 192 uint32_t vat_allocated; /* allocated length */ 193 uint32_t vat_start; /* offset 1st entry */ 194 uint8_t *vat_contents; /* the VAT */ 195 196 /* meta data partition */ 197 struct extfile_entry *meta_file; 198 struct extfile_entry *meta_mirror; 199 struct extfile_entry *meta_bitmap; 200 201 /* lvint */ 202 int num_files; 203 int num_directories; 204 uint32_t part_size[UDF_PARTITIONS]; 205 uint32_t part_free[UDF_PARTITIONS]; 206 207 struct space_bitmap_desc*part_unalloc_bits[UDF_PARTITIONS]; 208 struct space_bitmap_desc*part_freed_bits [UDF_PARTITIONS]; 209 }; 210 211 212 /* globals */ 213 214 extern struct udf_create_context context; 215 extern struct udf_disclayout layout; 216 217 /* prototypes */ 218 void udf_init_create_context(void); 219 int a_udf_version(const char *s, const char *id_type); 220 221 int udf_calculate_disc_layout(int format_flags, int min_udf, 222 uint32_t wrtrack_skew, 223 uint32_t first_lba, uint32_t last_lba, 224 uint32_t sector_size, uint32_t blockingnr, 225 uint32_t sparable_blocks, 226 float meta_fract); 227 228 void udf_osta_charset(struct charspec *charspec); 229 void udf_encode_osta_id(char *osta_id, uint16_t len, char *text); 230 231 void udf_set_regid(struct regid *regid, char const *name); 232 void udf_add_domain_regid(struct regid *regid); 233 void udf_add_udf_regid(struct regid *regid); 234 void udf_add_impl_regid(struct regid *regid); 235 void udf_add_app_regid(struct regid *regid); 236 237 int udf_validate_tag_sum(union dscrptr *dscr); 238 int udf_validate_tag_and_crc_sums(union dscrptr *dscr); 239 240 void udf_set_timestamp_now(struct timestamp *timestamp); 241 242 void udf_inittag(struct desc_tag *tag, int tagid, uint32_t loc); 243 int udf_create_anchor(int num); 244 245 void udf_create_terminator(union dscrptr *dscr, uint32_t loc); 246 int udf_create_primaryd(void); 247 int udf_create_partitiond(int part_num, int part_accesstype); 248 int udf_create_unalloc_spaced(void); 249 int udf_create_sparing_tabled(void); 250 int udf_create_space_bitmap(uint32_t dscr_size, uint32_t part_size_lba, 251 struct space_bitmap_desc **sbdp); 252 int udf_create_logical_dscr(int format_flags); 253 int udf_create_impvold(char *field1, char *field2, char *field3); 254 int udf_create_fsd(void); 255 int udf_create_lvintd(int type); 256 void udf_update_lvintd(int type); 257 258 int udf_register_bad_block(uint32_t location); 259 void udf_mark_allocated(uint32_t start_lb, int partnr, uint32_t blocks); 260 261 int udf_create_new_fe(struct file_entry **fep, int file_type, 262 struct stat *st); 263 int udf_create_new_efe(struct extfile_entry **efep, int file_type, 264 struct stat *st); 265 266 int udf_encode_symlink(uint8_t **pathbufp, uint32_t *pathlenp, char *target); 267 268 void udf_advance_uniqueid(void); 269 int udf_fidsize(struct fileid_desc *fid); 270 void udf_create_fid(uint32_t diroff, struct fileid_desc *fid, 271 char *name, int namelen, struct long_ad *ref); 272 int udf_create_parentfid(struct fileid_desc *fid, struct long_ad *parent); 273 274 int udf_create_meta_files(void); 275 int udf_create_new_rootdir(union dscrptr **dscr); 276 277 int udf_create_VAT(union dscrptr **vat_dscr); 278 void udf_prepend_VAT_file(void); 279 void udf_vat_update(uint32_t virt, uint32_t phys); 280 int udf_append_VAT_file(void); 281 282 #endif /* _FS_UDF_UDF_CREATE_H_ */ 283 284