1 /*	$NetBSD: printk.h,v 1.13 2022/07/29 23:50:44 riastradh Exp $	*/
2 
3 /*-
4  * Copyright (c) 2013 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Taylor R. Campbell.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #ifndef _LINUX_PRINTK_H_
33 #define _LINUX_PRINTK_H_
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <linux/export.h>
38 
39 #define	printk		printf
40 #define	vprintk		vprintf
41 #define	printk_once	printf
42 #define	pr_err		printf	/* XXX */
43 #define	pr_cont		printf	/* XXX */
44 #define	pr_info		printf	/* XXX */
45 #define	pr_info_once	printf	/* XXX */
46 #define	pr_info_ratelimited	printf	/* XXX */
47 #define	pr_warn		printf	/* XXX */
48 #define	pr_warn_once	printf	/* XXX */
49 #define	pr_notice	printf	/* XXX */
50 #define	pr_debug	printf	/* XXX */
51 #define	KERN_EMERG	"emerg: "
52 #define	KERN_ALERT	"alert: "
53 #define	KERN_CRIT	"crit: "
54 #define	KERN_ERR	"error: "
55 #define	KERN_WARNING	"warning: "
56 #define	KERN_NOTICE	"notice: "
57 #define	KERN_INFO	""
58 #define	KERN_DEBUG	"debug: "
59 #define	KERN_CONT	""
60 
61 #define	printk_ratelimit()	0 /* XXX */
62 
63 struct va_format {
64 	const char	*fmt;
65 	va_list		*va;
66 };
67 
68 #define	DUMP_PREFIX_NONE	0
69 #define	DUMP_PREFIX_OFFSET	1
70 #define	DUMP_PREFIX_ADDRESS	2
71 
72 static inline size_t
hex_dump_to_buffer(const void * buf,size_t buf_size,int bytes_per_line,int bytes_per_group,char * output,size_t output_size,bool ascii __unused)73 hex_dump_to_buffer(const void *buf, size_t buf_size, int bytes_per_line,
74     int bytes_per_group, char *output, size_t output_size, bool ascii __unused)
75 {
76 	const uint8_t *bytes = buf;
77 	int i = 0, t = 0, n;
78 
79 	KASSERT(output_size >= 1);
80 	KASSERT((bytes_per_line == 16) || (bytes_per_line == 32));
81 	KASSERT(powerof2(bytes_per_group));
82 	KASSERT(0 < bytes_per_group);
83 	KASSERT(bytes_per_group <= 8);
84 
85 	output[output_size - 1] = '\0';
86 	while (i < buf_size) {
87 		n = snprintf(output, output_size, "%02x", bytes[i++]);
88 		t += n;
89 		if (n >= output_size)
90 			break;
91 		output += n; output_size -= n;
92 		if ((i == buf_size) || (0 == (i % bytes_per_line)))
93 			n = snprintf(output, output_size, "\n");
94 		else if ((0 < i) && (0 == (i % bytes_per_group)))
95 			n = snprintf(output, output_size, " ");
96 		else
97 			n = 0;
98 		t += n;
99 		if (n >= output_size)
100 			break;
101 		output += n; output_size -= n;
102 	}
103 
104 	return t;
105 }
106 
107 static inline void
print_hex_dump(const char * level,const char * prefix,int prefix_type,int bytes_per_line,int bytes_per_group,const void * buf,size_t buf_size,bool ascii)108 print_hex_dump(const char *level, const char *prefix, int prefix_type,
109     int bytes_per_line, int bytes_per_group, const void *buf, size_t buf_size,
110     bool ascii)
111 {
112 	const uint8_t *bytes = buf;
113 	/* Two digits and one space/newline per byte, plus a null.  */
114 	char line[32*3 + 1];
115 
116 	KASSERT((bytes_per_line == 16) || (bytes_per_line == 32));
117 	KASSERT(powerof2(bytes_per_group));
118 	KASSERT(0 < bytes_per_group);
119 	KASSERT(bytes_per_group <= 8);
120 
121 	while (0 < buf_size) {
122 		const size_t n = MIN(buf_size, 32);
123 
124 		switch (prefix_type) {
125 		case DUMP_PREFIX_OFFSET:
126 			printf("%08zu: ", (bytes - (const uint8_t *)buf));
127 			break;
128 
129 		case DUMP_PREFIX_ADDRESS:
130 			printf("%p: ", bytes);
131 			break;
132 
133 		case DUMP_PREFIX_NONE:
134 		default:
135 			break;
136 		}
137 
138 		hex_dump_to_buffer(bytes, n, bytes_per_line, bytes_per_group,
139 		    line, sizeof(line), ascii);
140 		KASSERT(0 < strnlen(line, sizeof(line)));
141 		KASSERT(line[strnlen(line, sizeof(line)) - 1] == '\n');
142 		printf("%s", line);
143 
144 		bytes += n;
145 		buf_size -= n;
146 	}
147 }
148 
149 #endif  /* _LINUX_PRINTK_H_ */
150