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