1*af28f636SEnrico Perla - Sun Microsystems /*
2*af28f636SEnrico Perla - Sun Microsystems  * CDDL HEADER START
3*af28f636SEnrico Perla - Sun Microsystems  *
4*af28f636SEnrico Perla - Sun Microsystems  * The contents of this file are subject to the terms of the
5*af28f636SEnrico Perla - Sun Microsystems  * Common Development and Distribution License (the "License").
6*af28f636SEnrico Perla - Sun Microsystems  * You may not use this file except in compliance with the License.
7*af28f636SEnrico Perla - Sun Microsystems  *
8*af28f636SEnrico Perla - Sun Microsystems  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*af28f636SEnrico Perla - Sun Microsystems  * or http://www.opensolaris.org/os/licensing.
10*af28f636SEnrico Perla - Sun Microsystems  * See the License for the specific language governing permissions
11*af28f636SEnrico Perla - Sun Microsystems  * and limitations under the License.
12*af28f636SEnrico Perla - Sun Microsystems  *
13*af28f636SEnrico Perla - Sun Microsystems  * When distributing Covered Code, include this CDDL HEADER in each
14*af28f636SEnrico Perla - Sun Microsystems  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*af28f636SEnrico Perla - Sun Microsystems  * If applicable, add the following below this CDDL HEADER, with the
16*af28f636SEnrico Perla - Sun Microsystems  * fields enclosed by brackets "[]" replaced with your own identifying
17*af28f636SEnrico Perla - Sun Microsystems  * information: Portions Copyright [yyyy] [name of copyright owner]
18*af28f636SEnrico Perla - Sun Microsystems  *
19*af28f636SEnrico Perla - Sun Microsystems  * CDDL HEADER END
20*af28f636SEnrico Perla - Sun Microsystems  */
21*af28f636SEnrico Perla - Sun Microsystems /*
22*af28f636SEnrico Perla - Sun Microsystems  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
23*af28f636SEnrico Perla - Sun Microsystems  */
24*af28f636SEnrico Perla - Sun Microsystems 
25*af28f636SEnrico Perla - Sun Microsystems #include <stdio.h>
26*af28f636SEnrico Perla - Sun Microsystems #include <libintl.h>
27*af28f636SEnrico Perla - Sun Microsystems #include <errno.h>
28*af28f636SEnrico Perla - Sun Microsystems #include <assert.h>
29*af28f636SEnrico Perla - Sun Microsystems #include <unistd.h>
30*af28f636SEnrico Perla - Sun Microsystems #include "bblk_einfo.h"
31*af28f636SEnrico Perla - Sun Microsystems #include "boot_utils.h"
32*af28f636SEnrico Perla - Sun Microsystems 
33*af28f636SEnrico Perla - Sun Microsystems boolean_t boot_debug = B_FALSE;
34*af28f636SEnrico Perla - Sun Microsystems boolean_t nowrite = B_FALSE;
35*af28f636SEnrico Perla - Sun Microsystems 
36*af28f636SEnrico Perla - Sun Microsystems void
boot_gdebug(const char * funcname,char * format,...)37*af28f636SEnrico Perla - Sun Microsystems boot_gdebug(const char *funcname, char *format, ...)
38*af28f636SEnrico Perla - Sun Microsystems {
39*af28f636SEnrico Perla - Sun Microsystems 	va_list ap;
40*af28f636SEnrico Perla - Sun Microsystems 
41*af28f636SEnrico Perla - Sun Microsystems 	if (boot_debug == B_FALSE)
42*af28f636SEnrico Perla - Sun Microsystems 		return;
43*af28f636SEnrico Perla - Sun Microsystems 
44*af28f636SEnrico Perla - Sun Microsystems 	(void) fprintf(stdout, "%s(): ", funcname);
45*af28f636SEnrico Perla - Sun Microsystems 
46*af28f636SEnrico Perla - Sun Microsystems 	va_start(ap, format);
47*af28f636SEnrico Perla - Sun Microsystems 	/* LINTED: E_SEC_PRINTF_VAR_FMT */
48*af28f636SEnrico Perla - Sun Microsystems 	(void) vfprintf(stdout, format, ap);
49*af28f636SEnrico Perla - Sun Microsystems 	va_end(ap);
50*af28f636SEnrico Perla - Sun Microsystems }
51*af28f636SEnrico Perla - Sun Microsystems 
52*af28f636SEnrico Perla - Sun Microsystems /*
53*af28f636SEnrico Perla - Sun Microsystems  * Common functions to write out and read in block-sized data to a file
54*af28f636SEnrico Perla - Sun Microsystems  * descriptor.
55*af28f636SEnrico Perla - Sun Microsystems  */
56*af28f636SEnrico Perla - Sun Microsystems int
write_out(int fd,void * buffer,size_t size,off_t off)57*af28f636SEnrico Perla - Sun Microsystems write_out(int fd, void *buffer, size_t size, off_t off)
58*af28f636SEnrico Perla - Sun Microsystems {
59*af28f636SEnrico Perla - Sun Microsystems 	int		ret;
60*af28f636SEnrico Perla - Sun Microsystems 	char		*buf = buffer;
61*af28f636SEnrico Perla - Sun Microsystems 
62*af28f636SEnrico Perla - Sun Microsystems 	if (size % SECTOR_SIZE != 0)
63*af28f636SEnrico Perla - Sun Microsystems 		BOOT_DEBUG("Expected block-sized data, got: %d\n", size);
64*af28f636SEnrico Perla - Sun Microsystems 
65*af28f636SEnrico Perla - Sun Microsystems 	/* Dry run. */
66*af28f636SEnrico Perla - Sun Microsystems 	if (nowrite)
67*af28f636SEnrico Perla - Sun Microsystems 		return (BC_SUCCESS);
68*af28f636SEnrico Perla - Sun Microsystems 
69*af28f636SEnrico Perla - Sun Microsystems 	for (;;) {
70*af28f636SEnrico Perla - Sun Microsystems 	again:
71*af28f636SEnrico Perla - Sun Microsystems 		ret = pwrite(fd, buf, size, off);
72*af28f636SEnrico Perla - Sun Microsystems 		if (ret == -1) {
73*af28f636SEnrico Perla - Sun Microsystems 			if (errno == EAGAIN)
74*af28f636SEnrico Perla - Sun Microsystems 				goto again;
75*af28f636SEnrico Perla - Sun Microsystems 			else
76*af28f636SEnrico Perla - Sun Microsystems 				return (BC_ERROR);
77*af28f636SEnrico Perla - Sun Microsystems 			}
78*af28f636SEnrico Perla - Sun Microsystems 		if (ret < size) {
79*af28f636SEnrico Perla - Sun Microsystems 			size -= ret;
80*af28f636SEnrico Perla - Sun Microsystems 			off += ret;
81*af28f636SEnrico Perla - Sun Microsystems 			buf += ret;
82*af28f636SEnrico Perla - Sun Microsystems 		} else {
83*af28f636SEnrico Perla - Sun Microsystems 			break;
84*af28f636SEnrico Perla - Sun Microsystems 		}
85*af28f636SEnrico Perla - Sun Microsystems 	}
86*af28f636SEnrico Perla - Sun Microsystems 	return (BC_SUCCESS);
87*af28f636SEnrico Perla - Sun Microsystems }
88*af28f636SEnrico Perla - Sun Microsystems 
89*af28f636SEnrico Perla - Sun Microsystems int
read_in(int fd,void * buffer,size_t size,off_t off)90*af28f636SEnrico Perla - Sun Microsystems read_in(int fd, void *buffer, size_t size, off_t off)
91*af28f636SEnrico Perla - Sun Microsystems {
92*af28f636SEnrico Perla - Sun Microsystems 	int		ret;
93*af28f636SEnrico Perla - Sun Microsystems 	char		*buf = buffer;
94*af28f636SEnrico Perla - Sun Microsystems 
95*af28f636SEnrico Perla - Sun Microsystems 	if (size % SECTOR_SIZE != 0)
96*af28f636SEnrico Perla - Sun Microsystems 		BOOT_DEBUG("Expected block-sized data, got: %d\n", size);
97*af28f636SEnrico Perla - Sun Microsystems 
98*af28f636SEnrico Perla - Sun Microsystems 	for (;;) {
99*af28f636SEnrico Perla - Sun Microsystems 	again:
100*af28f636SEnrico Perla - Sun Microsystems 		ret = pread(fd, buf, size, off);
101*af28f636SEnrico Perla - Sun Microsystems 		if (ret == -1) {
102*af28f636SEnrico Perla - Sun Microsystems 			if (errno == EAGAIN)
103*af28f636SEnrico Perla - Sun Microsystems 				goto again;
104*af28f636SEnrico Perla - Sun Microsystems 			else
105*af28f636SEnrico Perla - Sun Microsystems 				return (BC_ERROR);
106*af28f636SEnrico Perla - Sun Microsystems 			}
107*af28f636SEnrico Perla - Sun Microsystems 		if (ret < size) {
108*af28f636SEnrico Perla - Sun Microsystems 			size -= ret;
109*af28f636SEnrico Perla - Sun Microsystems 			off += ret;
110*af28f636SEnrico Perla - Sun Microsystems 			buf += ret;
111*af28f636SEnrico Perla - Sun Microsystems 		} else {
112*af28f636SEnrico Perla - Sun Microsystems 			break;
113*af28f636SEnrico Perla - Sun Microsystems 		}
114*af28f636SEnrico Perla - Sun Microsystems 	}
115*af28f636SEnrico Perla - Sun Microsystems 	return (BC_SUCCESS);
116*af28f636SEnrico Perla - Sun Microsystems }
117