17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
53f2f09c1Sdp  * Common Development and Distribution License (the "License").
63f2f09c1Sdp  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22986fd29aSsetje  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <sys/types.h>
277c478bd9Sstevel@tonic-gate #include <sys/param.h>
283f2f09c1Sdp #include <sys/systm.h>
297c478bd9Sstevel@tonic-gate 
30986fd29aSsetje #include <sys/bootconf.h>
31986fd29aSsetje #include <sys/kobj_impl.h>
32986fd29aSsetje #include <sys/cmn_err.h>
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate /*
35986fd29aSsetje  * Standalone utility functions for use within krtld.
36986fd29aSsetje  * Many platforms implement optimized platmod versions of
37986fd29aSsetje  * utilities such as bcopy and any such are not yet available
38986fd29aSsetje  * until the kernel is more completely stitched together.
39986fd29aSsetje  * These standalones are referenced through vectors
40986fd29aSsetje  * kobj_bzero, etc.  Throughout krtld, the usual utility
41986fd29aSsetje  * is redefined to reference through the corresponding
42986fd29aSsetje  * vector so that krtld may simply refer to bzero etc.
43986fd29aSsetje  * as usual.  See kobj_impl.h.
447c478bd9Sstevel@tonic-gate  */
45*508de9f3SToomas Soome extern void vprintf(const char *, va_list);
467c478bd9Sstevel@tonic-gate 
47986fd29aSsetje /*ARGSUSED*/
48*508de9f3SToomas Soome static void
vkprintf(void * op,const char * fmt,va_list adx)49*508de9f3SToomas Soome vkprintf(void *op, const char *fmt, va_list adx)
50*508de9f3SToomas Soome {
51*508de9f3SToomas Soome 	vprintf(fmt, adx);
52*508de9f3SToomas Soome }
53*508de9f3SToomas Soome 
54986fd29aSsetje static void
kprintf(void * op,const char * fmt,...)55986fd29aSsetje kprintf(void *op, const char *fmt, ...)
567c478bd9Sstevel@tonic-gate {
57986fd29aSsetje 	va_list adx;
587c478bd9Sstevel@tonic-gate 
59986fd29aSsetje 	va_start(adx, fmt);
60*508de9f3SToomas Soome 	vkprintf(op, fmt, adx);
61986fd29aSsetje 	va_end(adx);
627c478bd9Sstevel@tonic-gate }
637c478bd9Sstevel@tonic-gate 
64986fd29aSsetje static void
stand_bzero(void * p_arg,size_t count)65986fd29aSsetje stand_bzero(void *p_arg, size_t count)
667c478bd9Sstevel@tonic-gate {
67986fd29aSsetje 	char zero = 0;
68986fd29aSsetje 	caddr_t p = p_arg;
697c478bd9Sstevel@tonic-gate 
70986fd29aSsetje 	while (count != 0)
71986fd29aSsetje 		*p++ = zero, count--;
727c478bd9Sstevel@tonic-gate }
737c478bd9Sstevel@tonic-gate 
74986fd29aSsetje static void
stand_bcopy(const void * src_arg,void * dest_arg,size_t count)75986fd29aSsetje stand_bcopy(const void *src_arg, void *dest_arg, size_t count)
767c478bd9Sstevel@tonic-gate {
77986fd29aSsetje 	caddr_t src = (caddr_t)src_arg;
78986fd29aSsetje 	caddr_t dest = dest_arg;
797c478bd9Sstevel@tonic-gate 
80986fd29aSsetje 	if (src < dest && (src + count) > dest) {
81986fd29aSsetje 		/* overlap copy */
82986fd29aSsetje 		while (--count != -1)
83986fd29aSsetje 			*(dest + count) = *(src + count);
84986fd29aSsetje 	} else {
85986fd29aSsetje 		while (--count != -1)
86986fd29aSsetje 			*dest++ = *src++;
87986fd29aSsetje 	}
887c478bd9Sstevel@tonic-gate }
897c478bd9Sstevel@tonic-gate 
90986fd29aSsetje static size_t
stand_strlcat(char * dst,const char * src,size_t dstsize)91986fd29aSsetje stand_strlcat(char *dst, const char *src, size_t dstsize)
923f2f09c1Sdp {
933f2f09c1Sdp 	char *df = dst;
943f2f09c1Sdp 	size_t left = dstsize;
953f2f09c1Sdp 	size_t l1;
963f2f09c1Sdp 	size_t l2 = strlen(src);
973f2f09c1Sdp 	size_t copied;
983f2f09c1Sdp 
993f2f09c1Sdp 	while (left-- != 0 && *df != '\0')
1003f2f09c1Sdp 		df++;
1013f2f09c1Sdp 	l1 = df - dst;
1023f2f09c1Sdp 	if (dstsize == l1)
1033f2f09c1Sdp 		return (l1 + l2);
1043f2f09c1Sdp 
1053f2f09c1Sdp 	copied = l1 + l2 >= dstsize ? dstsize - l1 - 1 : l2;
1063f2f09c1Sdp 	bcopy(src, dst + l1, copied);
1073f2f09c1Sdp 	dst[l1+copied] = '\0';
1083f2f09c1Sdp 	return (l1 + l2);
1093f2f09c1Sdp }
1103f2f09c1Sdp 
111986fd29aSsetje /*
112986fd29aSsetje  * Set up the krtld standalone utilty vectors
113986fd29aSsetje  */
1147c478bd9Sstevel@tonic-gate void
kobj_setup_standalone_vectors()115986fd29aSsetje kobj_setup_standalone_vectors()
1167c478bd9Sstevel@tonic-gate {
117*508de9f3SToomas Soome 	_kobj_printf = bop_printf;
118*508de9f3SToomas Soome 	_vkobj_printf = vbop_printf;
119986fd29aSsetje 	kobj_bcopy = stand_bcopy;
120986fd29aSsetje 	kobj_bzero = stand_bzero;
121986fd29aSsetje 	kobj_strlcat = stand_strlcat;
1227c478bd9Sstevel@tonic-gate }
1237c478bd9Sstevel@tonic-gate 
124986fd29aSsetje /*
125*508de9f3SToomas Soome  * Restore the kprintf/vkprintf/bcopy/bzero kobj vectors.
126986fd29aSsetje  * We need to undefine the override macros to
127986fd29aSsetje  * accomplish this.
128986fd29aSsetje  *
129986fd29aSsetje  * Do NOT add new code after the point or at least
130986fd29aSsetje  * certainly not code using bcopy or bzero which would
131986fd29aSsetje  * need to be vectored to the krtld equivalents.
132986fd29aSsetje  */
133986fd29aSsetje #undef	bcopy
134986fd29aSsetje #undef	bzero
135986fd29aSsetje #undef	strlcat
136986fd29aSsetje 
1377c478bd9Sstevel@tonic-gate void
kobj_restore_vectors()138986fd29aSsetje kobj_restore_vectors()
1397c478bd9Sstevel@tonic-gate {
140986fd29aSsetje 	_kobj_printf = kprintf;
141*508de9f3SToomas Soome 	_vkobj_printf = vkprintf;
142986fd29aSsetje 	kobj_bcopy = bcopy;
143986fd29aSsetje 	kobj_bzero = bzero;
144986fd29aSsetje 	kobj_strlcat = strlcat;
1457c478bd9Sstevel@tonic-gate }
146