1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate /* 23*7c478bd9Sstevel@tonic-gate * Copyright (c) 1994, by Sun Microsytems, Inc. 24*7c478bd9Sstevel@tonic-gate */ 25*7c478bd9Sstevel@tonic-gate 26*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 27*7c478bd9Sstevel@tonic-gate 28*7c478bd9Sstevel@tonic-gate #include "libtnf.h" 29*7c478bd9Sstevel@tonic-gate 30*7c478bd9Sstevel@tonic-gate /* 31*7c478bd9Sstevel@tonic-gate * Initiate a reader session 32*7c478bd9Sstevel@tonic-gate */ 33*7c478bd9Sstevel@tonic-gate 34*7c478bd9Sstevel@tonic-gate tnf_errcode_t 35*7c478bd9Sstevel@tonic-gate tnf_reader_begin(caddr_t base, size_t size, TNF **tnfret) 36*7c478bd9Sstevel@tonic-gate { 37*7c478bd9Sstevel@tonic-gate tnf_uint32_t magic; 38*7c478bd9Sstevel@tonic-gate int native; 39*7c478bd9Sstevel@tonic-gate TNF *tnf; 40*7c478bd9Sstevel@tonic-gate tnf_ref32_t *fhdr, *bhdr; 41*7c478bd9Sstevel@tonic-gate size_t tmpsz; 42*7c478bd9Sstevel@tonic-gate caddr_t p, genp, bvp; 43*7c478bd9Sstevel@tonic-gate tnf_errcode_t err; 44*7c478bd9Sstevel@tonic-gate 45*7c478bd9Sstevel@tonic-gate /* 46*7c478bd9Sstevel@tonic-gate * Check magic number 47*7c478bd9Sstevel@tonic-gate */ 48*7c478bd9Sstevel@tonic-gate 49*7c478bd9Sstevel@tonic-gate /* LINTED pointer cast may result in improper alignment */ 50*7c478bd9Sstevel@tonic-gate if ((magic = *(tnf_uint32_t *)base) == TNF_MAGIC) 51*7c478bd9Sstevel@tonic-gate native = 1; /* same endian */ 52*7c478bd9Sstevel@tonic-gate else if (magic == TNF_MAGIC_1) 53*7c478bd9Sstevel@tonic-gate native = 0; /* other endian */ 54*7c478bd9Sstevel@tonic-gate else 55*7c478bd9Sstevel@tonic-gate return (TNF_ERR_NOTTNF); 56*7c478bd9Sstevel@tonic-gate 57*7c478bd9Sstevel@tonic-gate /* 58*7c478bd9Sstevel@tonic-gate * Allocate TNF struct, initialize members 59*7c478bd9Sstevel@tonic-gate */ 60*7c478bd9Sstevel@tonic-gate 61*7c478bd9Sstevel@tonic-gate if ((tnf = (TNF*)calloc(1, sizeof (*tnf))) == (TNF*)NULL) 62*7c478bd9Sstevel@tonic-gate return (TNF_ERR_ALLOCFAIL); 63*7c478bd9Sstevel@tonic-gate 64*7c478bd9Sstevel@tonic-gate tnf->file_magic = magic; 65*7c478bd9Sstevel@tonic-gate tnf->file_native = native; 66*7c478bd9Sstevel@tonic-gate tnf->file_start = base; 67*7c478bd9Sstevel@tonic-gate tnf->file_size = size; 68*7c478bd9Sstevel@tonic-gate tnf->file_end = base + size; 69*7c478bd9Sstevel@tonic-gate 70*7c478bd9Sstevel@tonic-gate /* 71*7c478bd9Sstevel@tonic-gate * Examine file header 72*7c478bd9Sstevel@tonic-gate */ 73*7c478bd9Sstevel@tonic-gate 74*7c478bd9Sstevel@tonic-gate /* LINTED pointer cast may result in improper alignment */ 75*7c478bd9Sstevel@tonic-gate fhdr = (tnf_ref32_t *)(base + sizeof (magic)); /* second word */ 76*7c478bd9Sstevel@tonic-gate tnf->file_header = fhdr; 77*7c478bd9Sstevel@tonic-gate 78*7c478bd9Sstevel@tonic-gate /* Block size */ 79*7c478bd9Sstevel@tonic-gate p = _tnf_get_slot_named(tnf, fhdr, TNF_N_BLOCK_SIZE); 80*7c478bd9Sstevel@tonic-gate /* LINTED pointer cast may result in improper alignment */ 81*7c478bd9Sstevel@tonic-gate tnf->block_size = _GET_UINT32(tnf, (tnf_uint32_t *)p); 82*7c478bd9Sstevel@tonic-gate 83*7c478bd9Sstevel@tonic-gate /* Directory size */ 84*7c478bd9Sstevel@tonic-gate p = _tnf_get_slot_named(tnf, fhdr, TNF_N_DIRECTORY_SIZE); 85*7c478bd9Sstevel@tonic-gate /* LINTED pointer cast may result in improper alignment */ 86*7c478bd9Sstevel@tonic-gate tnf->directory_size = _GET_UINT32(tnf, (tnf_uint32_t *)p); 87*7c478bd9Sstevel@tonic-gate 88*7c478bd9Sstevel@tonic-gate /* Block count */ 89*7c478bd9Sstevel@tonic-gate p = _tnf_get_slot_named(tnf, fhdr, TNF_N_BLOCK_COUNT); 90*7c478bd9Sstevel@tonic-gate /* LINTED pointer cast may result in improper alignment */ 91*7c478bd9Sstevel@tonic-gate tnf->block_count = _GET_UINT32(tnf, (tnf_uint32_t *)p); 92*7c478bd9Sstevel@tonic-gate /* 93*7c478bd9Sstevel@tonic-gate * This member tracks data block count, not total block count 94*7c478bd9Sstevel@tonic-gate * (unlike the TNF file header). Discount directory blocks. 95*7c478bd9Sstevel@tonic-gate */ 96*7c478bd9Sstevel@tonic-gate tnf->block_count -= tnf->directory_size / tnf->block_size; 97*7c478bd9Sstevel@tonic-gate 98*7c478bd9Sstevel@tonic-gate /* 99*7c478bd9Sstevel@tonic-gate * 1196886: Clients may supply file_size information obtained 100*7c478bd9Sstevel@tonic-gate * by fstat() which is incorrect. Check it now and revise 101*7c478bd9Sstevel@tonic-gate * downwards if we have to. 102*7c478bd9Sstevel@tonic-gate */ 103*7c478bd9Sstevel@tonic-gate tmpsz = tnf->directory_size + tnf->block_count * tnf->block_size; 104*7c478bd9Sstevel@tonic-gate if (tmpsz != size) { 105*7c478bd9Sstevel@tonic-gate if (tmpsz > size) 106*7c478bd9Sstevel@tonic-gate /* missing data? */ 107*7c478bd9Sstevel@tonic-gate return (TNF_ERR_BADTNF); 108*7c478bd9Sstevel@tonic-gate else { 109*7c478bd9Sstevel@tonic-gate tnf->file_size = tmpsz; 110*7c478bd9Sstevel@tonic-gate tnf->file_end = base + tmpsz; 111*7c478bd9Sstevel@tonic-gate } 112*7c478bd9Sstevel@tonic-gate } 113*7c478bd9Sstevel@tonic-gate 114*7c478bd9Sstevel@tonic-gate /* Calculate block shift */ 115*7c478bd9Sstevel@tonic-gate tmpsz = 1; 116*7c478bd9Sstevel@tonic-gate while (tmpsz != tnf->block_size) { 117*7c478bd9Sstevel@tonic-gate tmpsz <<= 1; 118*7c478bd9Sstevel@tonic-gate tnf->block_shift++; 119*7c478bd9Sstevel@tonic-gate } 120*7c478bd9Sstevel@tonic-gate 121*7c478bd9Sstevel@tonic-gate /* Calculate block mask */ 122*7c478bd9Sstevel@tonic-gate tnf->block_mask = ~(tnf->block_size - 1); 123*7c478bd9Sstevel@tonic-gate 124*7c478bd9Sstevel@tonic-gate /* Generation shift */ 125*7c478bd9Sstevel@tonic-gate p = _tnf_get_slot_named(tnf, fhdr, TNF_N_FILE_LOGICAL_SIZE); 126*7c478bd9Sstevel@tonic-gate /* LINTED pointer cast may result in improper alignment */ 127*7c478bd9Sstevel@tonic-gate tnf->generation_shift = _GET_UINT32(tnf, (tnf_uint32_t *)p); 128*7c478bd9Sstevel@tonic-gate 129*7c478bd9Sstevel@tonic-gate /* Calculate the address mask */ 130*7c478bd9Sstevel@tonic-gate /* 131*7c478bd9Sstevel@tonic-gate * Following lint complaint is unwarranted, probably an 132*7c478bd9Sstevel@tonic-gate * uninitialized variable in lint or something ... 133*7c478bd9Sstevel@tonic-gate */ 134*7c478bd9Sstevel@tonic-gate /* LINTED constant truncated by assignment */ 135*7c478bd9Sstevel@tonic-gate tnf->address_mask = 0xffffffff; 136*7c478bd9Sstevel@tonic-gate tnf->address_mask <<= tnf->generation_shift; 137*7c478bd9Sstevel@tonic-gate tnf->address_mask = ~(tnf->address_mask); 138*7c478bd9Sstevel@tonic-gate 139*7c478bd9Sstevel@tonic-gate 140*7c478bd9Sstevel@tonic-gate /* 141*7c478bd9Sstevel@tonic-gate * Examine first block header in data area 142*7c478bd9Sstevel@tonic-gate */ 143*7c478bd9Sstevel@tonic-gate 144*7c478bd9Sstevel@tonic-gate tnf->data_start = tnf->file_start + tnf->directory_size; 145*7c478bd9Sstevel@tonic-gate /* LINTED pointer cast may result in improper alignment */ 146*7c478bd9Sstevel@tonic-gate bhdr = (tnf_ref32_t *)tnf->data_start; 147*7c478bd9Sstevel@tonic-gate 148*7c478bd9Sstevel@tonic-gate /* Block generation offset */ 149*7c478bd9Sstevel@tonic-gate genp = _tnf_get_slot_named(tnf, bhdr, TNF_N_GENERATION); 150*7c478bd9Sstevel@tonic-gate tnf->block_generation_offset = genp - (caddr_t)bhdr; 151*7c478bd9Sstevel@tonic-gate 152*7c478bd9Sstevel@tonic-gate /* Block bytes valid offset */ 153*7c478bd9Sstevel@tonic-gate bvp = _tnf_get_slot_named(tnf, bhdr, TNF_N_BYTES_VALID); 154*7c478bd9Sstevel@tonic-gate tnf->block_bytes_valid_offset = bvp - (caddr_t)bhdr; 155*7c478bd9Sstevel@tonic-gate 156*7c478bd9Sstevel@tonic-gate /* 157*7c478bd9Sstevel@tonic-gate * Bootstrap taginfo system and cache important taginfo 158*7c478bd9Sstevel@tonic-gate */ 159*7c478bd9Sstevel@tonic-gate 160*7c478bd9Sstevel@tonic-gate if ((err = _tnf_init_tags(tnf)) != TNF_ERR_NONE) 161*7c478bd9Sstevel@tonic-gate return (err); 162*7c478bd9Sstevel@tonic-gate 163*7c478bd9Sstevel@tonic-gate tnf->file_header_info = _tnf_get_info(tnf, _tnf_get_tag(tnf, fhdr)); 164*7c478bd9Sstevel@tonic-gate tnf->block_header_info = _tnf_get_info(tnf, _tnf_get_tag(tnf, bhdr)); 165*7c478bd9Sstevel@tonic-gate 166*7c478bd9Sstevel@tonic-gate /* 167*7c478bd9Sstevel@tonic-gate * Return TNF handle and error status 168*7c478bd9Sstevel@tonic-gate */ 169*7c478bd9Sstevel@tonic-gate 170*7c478bd9Sstevel@tonic-gate *tnfret = tnf; 171*7c478bd9Sstevel@tonic-gate return (TNF_ERR_NONE); 172*7c478bd9Sstevel@tonic-gate } 173*7c478bd9Sstevel@tonic-gate 174*7c478bd9Sstevel@tonic-gate /* 175*7c478bd9Sstevel@tonic-gate * Terminate a reader session 176*7c478bd9Sstevel@tonic-gate */ 177*7c478bd9Sstevel@tonic-gate 178*7c478bd9Sstevel@tonic-gate tnf_errcode_t 179*7c478bd9Sstevel@tonic-gate tnf_reader_end(TNF *tnf) 180*7c478bd9Sstevel@tonic-gate { 181*7c478bd9Sstevel@tonic-gate tnf_errcode_t err; 182*7c478bd9Sstevel@tonic-gate 183*7c478bd9Sstevel@tonic-gate /* Deallocate all taginfo */ 184*7c478bd9Sstevel@tonic-gate if ((err = _tnf_fini_tags(tnf)) != TNF_ERR_NONE) 185*7c478bd9Sstevel@tonic-gate return (err); 186*7c478bd9Sstevel@tonic-gate 187*7c478bd9Sstevel@tonic-gate /* Deallocate TNF */ 188*7c478bd9Sstevel@tonic-gate free(tnf); 189*7c478bd9Sstevel@tonic-gate 190*7c478bd9Sstevel@tonic-gate return (TNF_ERR_NONE); 191*7c478bd9Sstevel@tonic-gate } 192