10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*1991Sheppo * Common Development and Distribution License (the "License"). 6*1991Sheppo * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 21*1991Sheppo 220Sstevel@tonic-gate /* 23*1991Sheppo * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 280Sstevel@tonic-gate 290Sstevel@tonic-gate #include <stdio.h> 300Sstevel@tonic-gate #include <unistd.h> 310Sstevel@tonic-gate #include <stdlib.h> 320Sstevel@tonic-gate #include <sys/types.h> 330Sstevel@tonic-gate #include <alloca.h> 340Sstevel@tonic-gate #include <sys/stat.h> 350Sstevel@tonic-gate #include <malloc.h> 360Sstevel@tonic-gate #include <fcntl.h> 370Sstevel@tonic-gate #include <syslog.h> 380Sstevel@tonic-gate #include <mdesc.h> 390Sstevel@tonic-gate #include <string.h> 400Sstevel@tonic-gate #include <errno.h> 410Sstevel@tonic-gate 420Sstevel@tonic-gate #define MDESC_PATH "/devices/pseudo/mdesc@0:mdesc" 430Sstevel@tonic-gate #define SIZE 8192 440Sstevel@tonic-gate 45221Sla135387 static void mdesc_free(void *bufp, size_t size); 46*1991Sheppo uint8_t *md_bufp; 47221Sla135387 480Sstevel@tonic-gate md_t * 490Sstevel@tonic-gate mdesc_devinit(void) 500Sstevel@tonic-gate { 510Sstevel@tonic-gate int fh; 520Sstevel@tonic-gate int res; 530Sstevel@tonic-gate int size; 540Sstevel@tonic-gate int offset; 550Sstevel@tonic-gate md_t *mdp; 560Sstevel@tonic-gate 57*1991Sheppo if (md_bufp != NULL) 58*1991Sheppo return (NULL); 59*1991Sheppo 600Sstevel@tonic-gate fh = open(MDESC_PATH, O_RDONLY, 0); 610Sstevel@tonic-gate if (fh < 0) { 620Sstevel@tonic-gate return (NULL); 630Sstevel@tonic-gate } 640Sstevel@tonic-gate 650Sstevel@tonic-gate size = SIZE; /* initial size */ 660Sstevel@tonic-gate offset = 0; 670Sstevel@tonic-gate 68*1991Sheppo md_bufp = malloc(size); 69*1991Sheppo if (NULL == md_bufp) { 700Sstevel@tonic-gate return (NULL); 710Sstevel@tonic-gate } 720Sstevel@tonic-gate 730Sstevel@tonic-gate /* OK read until we get a EOF */ 740Sstevel@tonic-gate 750Sstevel@tonic-gate do { 760Sstevel@tonic-gate int len; 770Sstevel@tonic-gate 780Sstevel@tonic-gate len = size - offset; 790Sstevel@tonic-gate 800Sstevel@tonic-gate while (len < SIZE) { 810Sstevel@tonic-gate size += SIZE; 82*1991Sheppo md_bufp = realloc(md_bufp, size); 83*1991Sheppo if (NULL == md_bufp) 840Sstevel@tonic-gate return (NULL); 850Sstevel@tonic-gate len = size - offset; 860Sstevel@tonic-gate } 870Sstevel@tonic-gate 880Sstevel@tonic-gate do { 89*1991Sheppo res = read(fh, md_bufp + offset, len); 900Sstevel@tonic-gate } while ((res < 0) && (errno == EAGAIN)); 910Sstevel@tonic-gate 920Sstevel@tonic-gate if (res < 0) { 93*1991Sheppo free(md_bufp); 940Sstevel@tonic-gate return (NULL); 950Sstevel@tonic-gate } 960Sstevel@tonic-gate 970Sstevel@tonic-gate offset += res; 980Sstevel@tonic-gate } while (res > 0); 990Sstevel@tonic-gate 1000Sstevel@tonic-gate (void) close(fh); 1010Sstevel@tonic-gate 102*1991Sheppo md_bufp = realloc(md_bufp, offset); 103*1991Sheppo if (NULL == md_bufp) 1040Sstevel@tonic-gate return (NULL); 1050Sstevel@tonic-gate 106*1991Sheppo mdp = md_init_intern((uint64_t *)md_bufp, malloc, mdesc_free); 1070Sstevel@tonic-gate if (NULL == mdp) { 108*1991Sheppo free(md_bufp); 1090Sstevel@tonic-gate return (NULL); 1100Sstevel@tonic-gate } 1110Sstevel@tonic-gate 1120Sstevel@tonic-gate return (mdp); 1130Sstevel@tonic-gate } 114221Sla135387 115221Sla135387 /*ARGSUSED*/ 116221Sla135387 void 117221Sla135387 mdesc_free(void *bufp, size_t size) 118221Sla135387 { 119*1991Sheppo if (bufp) 120*1991Sheppo free(bufp); 121221Sla135387 } 122*1991Sheppo 123*1991Sheppo void 124*1991Sheppo mdesc_devfini(md_t *mdp) 125*1991Sheppo { 126*1991Sheppo if (mdp) 127*1991Sheppo (void) md_fini(mdp); 128*1991Sheppo 129*1991Sheppo if (md_bufp) 130*1991Sheppo free(md_bufp); 131*1991Sheppo md_bufp = NULL; 132*1991Sheppo } 133