iPXE
dmesg.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2026 Michael Brown <mbrown@fensystems.co.uk>.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA.
18 *
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
22 */
23
24FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25FILE_SECBOOT ( PERMITTED );
26
27#include <errno.h>
28#include <ipxe/ansiesc.h>
29#include <ipxe/settings.h>
30#include <ipxe/console.h>
31
32/** @file
33 *
34 * In-memory ring buffer console
35 *
36 */
37
38/* Set default console usage if applicable */
39#if ! ( defined ( CONSOLE_DMESG ) && CONSOLE_EXPLICIT ( CONSOLE_DMESG ) )
40#undef CONSOLE_DMESG
41#define CONSOLE_DMESG ( CONSOLE_USAGE_ALL & ~CONSOLE_USAGE_TUI )
42#endif
43
44/** Size of ring buffer */
45#define DMESG_LEN 8192
46
47/** Ring buffer */
48static char dmesg[DMESG_LEN];
49
50/** Offset within ring buffer */
51static unsigned int dmesg_offset;
52
53/** Ring buffer ANSI escape sequence handlers */
55 { 0, NULL }
56};
57
58/** Ring buffer ANSI escape sequence context */
60 .handlers = dmesg_ansiesc_handlers,
61};
62
63/**
64 * Print a character to the ring buffer
65 *
66 * @v character Character to be printed
67 */
68static void dmesg_putchar ( int character ) {
69
70 /* Strip ANSI escape sequences */
71 character = ansiesc_process ( &dmesg_ansiesc_ctx, character );
72 if ( character < 0 )
73 return;
74
75 /* Handle backspace characters */
76 if ( character == '\b' ) {
77 if ( dmesg_offset )
79 return;
80 }
81
82 /* Record character */
83 dmesg[ dmesg_offset++ % DMESG_LEN ] = character;
84}
85
86/** Ring buffer console driver */
87struct console_driver dmesg_console __console_driver = {
88 .putchar = dmesg_putchar,
89 .usage = CONSOLE_DMESG,
90};
91
92/**
93 * Fetch ring buffer setting
94 *
95 * @v data Buffer to fill with setting data
96 * @v len Length of buffer
97 * @ret len Length of setting data, or negative error
98 */
99static int dmesg_fetch ( void *data, size_t len ) {
100 uint8_t *bytes = data;
101 size_t max;
102 unsigned int offset;
103
104 /* Calculate length */
106 if ( max > DMESG_LEN )
107 max = DMESG_LEN;
108 if ( len > max )
109 len = max;
110
111 /* Copy data */
112 offset = ( dmesg_offset - len );
113 while ( len-- )
114 *(bytes++) = dmesg[ offset++ % DMESG_LEN ];
115
116 return max;
117}
118
119/**
120 * Store ring buffer setting
121 *
122 * @v data Setting data, or NULL to clear setting
123 * @v len Length of setting data
124 * @ret rc Return status code
125 */
126static int dmesg_store ( const void *data __unused, size_t len ) {
127
128 /* Only clearing the log is supported */
129 if ( len )
130 return -ENOTSUP;
131
132 /* Clear log */
133 dmesg_offset = 0;
134
135 return 0;
136}
137
138/** Ring buffer setting */
139const struct setting dmesg_setting __setting ( SETTING_MISC, dmesg ) = {
140 .name = "dmesg",
141 .description = "Ring buffer",
142 .type = &setting_type_string,
143 .scope = &builtin_scope,
144};
145
146/** Ring buffer built-in setting */
147struct builtin_setting dmesg_builtin_setting __builtin_setting = {
148 .setting = &dmesg_setting,
149 .fetch = dmesg_fetch,
150 .store = dmesg_store,
151};
#define NULL
NULL pointer (VOID *).
Definition Base.h:321
int ansiesc_process(struct ansiesc_context *ctx, int c)
Process character that may be part of ANSI escape sequence.
Definition ansiesc.c:75
ANSI escape sequences.
unsigned char uint8_t
Definition stdint.h:10
#define max(x, y)
Definition ath.h:41
uint16_t offset
Offset to command line.
Definition bzimage.h:3
static struct ansiesc_handler dmesg_ansiesc_handlers[]
Ring buffer ANSI escape sequence handlers.
Definition dmesg.c:54
static int dmesg_fetch(void *data, size_t len)
Fetch ring buffer setting.
Definition dmesg.c:99
static void dmesg_putchar(int character)
Print a character to the ring buffer.
Definition dmesg.c:68
#define DMESG_LEN
Size of ring buffer.
Definition dmesg.c:45
static unsigned int dmesg_offset
Offset within ring buffer.
Definition dmesg.c:51
static struct ansiesc_context dmesg_ansiesc_ctx
Ring buffer ANSI escape sequence context.
Definition dmesg.c:59
static int dmesg_store(const void *data __unused, size_t len)
Store ring buffer setting.
Definition dmesg.c:126
#define CONSOLE_DMESG
Definition dmesg.c:41
static char dmesg[DMESG_LEN]
Ring buffer.
Definition dmesg.c:48
ring len
Length.
Definition dwmac.h:226
uint8_t data[48]
Additional event data.
Definition ena.h:11
Error codes.
#define __unused
Declare a variable or data structure as unused.
Definition compiler.h:598
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
Definition compiler.h:921
#define ENOTSUP
Operation not supported.
Definition errno.h:590
#define FILE_SECBOOT(_status)
Declare a file's UEFI Secure Boot permission status.
Definition compiler.h:951
#define SETTING_MISC
Miscellaneous settings.
Definition settings.h:81
uint8_t bytes[64]
Definition ib_mad.h:5
User interaction.
#define __console_driver
Mark a struct console_driver as being part of the console drivers table.
Definition console.h:134
Configuration settings.
#define __setting(setting_order, name)
Declare a configuration setting.
Definition settings.h:57
#define __builtin_setting
Declare a built-in setting.
Definition settings.h:292
const struct settings_scope builtin_scope
Built-in setting scope.
Definition settings.c:2506
ANSI escape sequence context.
Definition ansiesc.h:74
A handler for an escape sequence.
Definition ansiesc.h:35
A built-in setting.
Definition settings.h:268
A console driver.
Definition console.h:56
A setting.
Definition settings.h:24