1.\" $NetBSD: cnmagic.9,v 1.16 2019/07/07 21:24:20 gutteridge Exp $ 2.\" 3.\" Copyright (c) 2000 Eduardo Horvath 4.\" All rights reserved. 5.\" 6.\" Redistribution and use in source and binary forms, with or without 7.\" modification, are permitted provided that the following conditions 8.\" are met: 9.\" 1. Redistributions of source code must retain the above copyright 10.\" notice, this list of conditions and the following disclaimer. 11.\" 2. Redistributions in binary form must reproduce the above copyright 12.\" notice, this list of conditions and the following disclaimer in the 13.\" documentation and/or other materials provided with the distribution. 14.\" 3. All advertising materials mentioning features or use of this software 15.\" must display the following acknowledgement: 16.\" This product includes software developed for the 17.\" NetBSD Project. See http://www.NetBSD.org/ for 18.\" information about NetBSD. 19.\" 4. The name of the author may not be used to endorse or promote products 20.\" derived from this software without specific prior written permission. 21.\" 22.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 23.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 24.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 25.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 26.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 27.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 31.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32.\" 33.\" --(license Id: LICENSE.proto,v 1.1 2000/06/13 21:40:26 cgd Exp )-- 34.\" 35.Dd July 7, 2019 36.Dt CNMAGIC 9 37.Os 38.Sh NAME 39.Nm cn_init_magic , 40.Nm cn_trap , 41.Nm cn_isconsole , 42.Nm cn_check_magic , 43.Nm cn_destroy_magic , 44.Nm cn_set_magic , 45.Nm cn_get_magic 46.Nd console magic key sequence management 47.Sh SYNOPSIS 48.In sys/systm.h 49.Ft void 50.Fn cn_init_magic "cnm_state_t *cnms" 51.Ft void 52.Fn cn_trap 53.Ft int 54.Fn cn_isconsole "dev_t dev" 55.Ft void 56.Fn cn_check_magic "dev_t dev" "int k" "cnm_state_t *cnms" 57.Ft void 58.Fn cn_destroy_magic "cnm_state_t *cnms" 59.Ft int 60.Fn cn_set_magic "char *magic" 61.Ft int 62.Fn cn_get_magic "char *magic" "int len" 63.Sh DESCRIPTION 64The 65.Nx 66console magic key sequence management framework is designed to provide 67flexible methods to set, change, and detect magic key sequences on console 68devices and break into the debugger or ROM monitor with a minimum of interrupt 69latency. 70.Pp 71Drivers that generate console input should make use of these routines. 72A different 73.Va cnm_state_t 74should be used for each separate input stream. 75Multiple devices that share the same input stream, such as USB 76keyboards, can share the same 77.Va cnm_state_t . 78Once a 79.Va cnm_state_t 80is allocated, it should be initialized with 81.Fn cn_init_magic 82so it can be used by 83.Fn cn_check_magic . 84If a driver thinks it might be the console input device it can set the 85magic sequence with 86.Fn cn_set_magic 87to any arbitrary string. 88Whenever the driver receives input, it should call 89.Fn cn_check_magic 90to process the data and determine whether the magic sequence has 91been hit. 92.Pp 93The magic key sequence can be accessed through the 94.Va hw.cnmagic 95.Ic sysctl 96variable. 97This is the raw data and may be keycodes rather than 98processed characters, depending on the console device. 99.Sh FUNCTIONS 100The following functions describe the console magic interface. 101.Bl -tag -width indent 102.It Fn cn_init_magic "cnm" 103Initialize the console magic state pointed to by 104.Fa cnm 105to a usable state. 106.It Fn cn_trap 107Trap into the kernel debugger or ROM monitor. 108By default this routine is defined to be 109.Fn console_debugger 110but can be overridden in MI header files. 111.It Fn cn_isconsole "dev" 112Determine whether a given 113.Fa dev 114is the system console. 115This macro tests to see if 116.Fa dev 117is the same as 118.Va cn_tab->cn_dev 119but can be overridden in MI header files. 120.It Fn cn_check_magic "dev" "k" "cnms" 121All input should be passed through 122.Fn cn_check_magic 123so the state machine remains in a consistent state. 124.Fn cn_check_magic 125calls 126.Fn cn_isconsole 127with 128.Fa dev 129to determine if this is the console. 130If that returns true then it runs the input value 131.Fa k 132through the state machine. 133If the state machine completes a match of the current console magic sequence 134.Fn cn_trap 135is called. 136Some input may need to be translated to state machine 137values such as the serial line 138.Li BREAK 139sequence. 140.It Fn cn_destroy_magic "cnms" 141This should be called once what 142.Fa cnms 143points to is no longer needed. 144.It Fn cn_set_magic "magic" 145.Fn cn_set_magic 146encodes a 147.Li nul 148terminated arbitrary string into values that can be used by 149the state machine and installs it as the global magic sequence. 150The escape sequence is character value 151.Li 0x27 152and can be used to encode special values: 153.Pp 154.Bl -tag -width "0x001" -compact -offset indent 155.It 0x27 156The literal value 157.Li 0x27 . 158.It 0x01 159Serial 160.Li BREAK 161sequence. 162.It 0x02 163.Li Nul 164character. 165.El 166.Pp 167Returns 168.Li 0 169on success or a non-zero error value. 170.It Fn cn_get_magic "magic" "len" 171Extract the current magic sequence from the state machine and return 172up to 173.Fa len 174bytes of it in the buffer pointed to by 175.Fa magic . 176It uses the same encoding accepted by 177.Fn cn_set_magic . 178Returns 179.Li 0 180on success or a non-zero error value. 181.El 182.Sh SEE ALSO 183.Xr ddb 4 , 184.Xr sysctl 8 , 185.Xr cons 9 186.Sh HISTORY 187The 188.Nx 189console magic key sequence management framework 190first appeared in 191.Nx 1.6 . 192.Sh AUTHORS 193The 194.Nx 195console magic key sequence management framework was designed and 196implemented by 197.An Eduardo Horvath 198.Aq eeh@NetBSD.org . 199