CNMAGIC(9) NetBSD Kernel Developer's Manual CNMAGIC(9)
NAME
cn_init_magic, cn_isconsole, cn_check_magic, cn_check_magic_impl, cn_destroy_magic, cn_set_magic, cn_get_magic, cn_set_magic_action, -- console magic sequence management
SYNOPSIS
#include <dev/cons.h> void cn_init_magic(cnm_state_t *cnms); bool cn_isconsole(dev_t dev); bool cn_check_magic(dev_t dev, int k, cnm_state_t *cnms); bool cn_check_magic_impl(int k, cnm_state_t *cnms); void cn_destroy_magic(cnm_state_t *cnms); int cn_set_magic(const char *magic); int cn_get_magic(char *magic, int len); int cn_set_magic_action(void (*func)(void));
DESCRIPTION
The NetBSD console magic sequence management framework is designed to provide flexible methods to set, change, and detect magic sequences on console devices and break into the debugger or ROM monitor with a minimum of interrupt latency. Drivers that generate console input should make use of these routines. A different cnm_state_t should be used for each separate input stream. Multiple devices that share the same input stream, such as USB keyboards, can share the same cnm_state_t. Once a cnm_state_t is allocated, it should be initialized with cn_init_magic() so it can be used by cn_check_magic(). If a driver thinks it might be the console input device it can set the magic sequence with cn_set_magic() to any arbitrary string. Whenever the driver receives input, it should call cn_check_magic() to process the data and determine whether the magic sequence has been hit. The magic sequence can be read and set through the hw.cnmagic sysctl(7) variable. This is the raw data and may be keycodes rather than processed characters, depending on the console device.
FUNCTIONS
The following functions describe the console magic interface. cn_init_magic(cnms) Initialize the console magic state pointed to by cnms to a usable state. cn_isconsole(dev) Determine whether a given dev is the system console. This macro tests to see if dev is the same as cn_tab->cn_dev. cn_check_magic(dev, k, cnms) This inline first checks that dev is the system console using cn_isconsole(). If true, it passes the remaining arguments to cn_check_magic_impl(). cn_check_magic_impl(k, cnms) Runs the input value k through the state machine and checks to see if the magic sequence has completed. If so, the magic sequence action is invoked and true is returned, otherwise returns false. All input should be passed through cn_check_magic() or cn_cneck_magic_impl() so the state machine remains in a consistent state. The state machine ignores all input and resets to the initial state when the kernel debug- ger is already active. To report serial line BREAK condition pass CNC_BREAK as input. cn_destroy_magic(cnms) This should be called once what cnms points to is no longer needed. cn_set_magic(magic) cn_set_magic() encodes a null-terminated arbitrary string into values that can be used by the state machine and installs it as the global magic sequence. The escape sequence is character value 0x27 and can be used to encode special values: 0x27 The literal value 0x27. 0x01 Serial BREAK sequence. 0x02 NUL character. Returns 0 on success or a non-zero error value. cn_get_magic(magic, len) Extract the current magic sequence from the state machine and return up to len bytes of it in the buffer pointed to by magic. It uses the same encoding accepted by cn_set_magic(). Returns 0 on success or a non-zero error value. cn_set_magic_action(cnms, func) The default action when a magic sequence is completed is to invoke the kernel debugger via the console_debugger() function. cn_set_magic_action() allows for an alternate action to be specified if a driver has special requirements.
SEE ALSO
ddb(4), sysctl(8), cons(9)
HISTORY
The NetBSD console magic sequence management framework first appeared in NetBSD 1.6.
AUTHORS
The NetBSD console magic sequence management framework was designed and implemented by Eduardo Horvath <eeh@NetBSD.org>.
CAVEATS
wscons(9) does not use cnmagic facility. NetBSD 11.99 November 8, 2026 NetBSD 11.99
Powered by man-cgi (2026-09-02). Maintained for NetBSD by Kimmo Suominen. Based on man-cgi by Panagiotis Christias.