xref: /freebsd/libexec/rc/rc.d/syscons (revision 3662862f558f6afbdb53b1d6570c482c7d180580)
1#!/bin/sh -
2#
3# Copyright (c) 2000  The FreeBSD Project
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#
15# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25# SUCH DAMAGE.
26#
27# $FreeBSD$
28#
29
30# PROVIDE: syscons
31# REQUIRE: LOGIN
32# KEYWORD: nojail
33
34. /etc/rc.subr
35
36name="syscons"
37desc="Configure the system console"
38extra_commands="setkeyboard"
39setkeyboard_cmd="syscons_setkeyboard"
40start_precmd="syscons_precmd"
41start_cmd="syscons_start"
42stop_cmd=":"
43
44# stdin must be redirected because it might be for a serial console
45#
46kbddev=/dev/ttyv0
47viddev=/dev/ttyv0
48
49_sc_config=
50_sc_console=
51_sc_initdone=
52_sc_keymap_msg=
53_sc_bootmethod=
54sc_init()
55{
56	local bootmethod
57
58	if [ -z "${_sc_initdone}" ]; then
59		if [ -z "${_sc_console}" ]; then
60			if [ x`sysctl -n kern.vty` = x"vt" ]; then
61				_sc_console="vt"
62			else
63				_sc_console="syscons"
64			fi
65			_sc_config="${_sc_console}"
66		fi
67		if [ -z "${_sc_bootmethod}" ]; then
68			bootmethod=$(sysctl -qn machdep.bootmethod)
69			case ${bootmethod} in
70				UEFI)
71					_sc_bootmethod="uefi"
72					;;
73				BIOS)
74					_sc_bootmethod="bios"
75					;;
76				PVH)
77					_sc_bootmethod="pvh"
78					;;
79				*)
80					_sc_bootmethod="uefi" # Default to UEFI
81					;;
82			esac
83		fi
84		echo -n "Configuring ${_sc_config}:"
85		_sc_initdone=yes
86	fi
87}
88
89# syscons to vt migration helper
90lookup_keymap_for_vt()
91{
92	keymap=`basename $1 .kbd`
93	case $keymap in
94hy.armscii-8)			echo am;;
95be.iso.acc)			echo be.acc;;
96be.iso)				echo be;;
97bg.bds.ctrlcaps)		echo bg.bds;;
98bg.phonetic.ctrlcaps)		echo bg.phonetic;;
99br275.iso.acc)			echo br;;
100br275.*)			echo br.noacc;;
101by.*)				echo by;;
102fr_CA.iso.acc)			echo ca-fr;;
103swissgerman.macbook.acc)	echo ch.macbook.acc;;
104swissgerman.iso.acc)		echo ch.acc;;
105swissgerman.*)			echo ch;;
106swissfrench.iso.acc)		echo ch-fr.acc;;
107swissfrench.*)			echo ch-fr;;
108ce.iso2)			echo centraleuropean.qwerty;;
109colemak.iso15.acc)		echo colemak.acc;;
110cs.*|cz.*)			echo cz;;
111german.iso.acc)			echo de.acc;;
112german.*)			echo de;;
113danish.iso.acc)			echo dk.acc;;
114danish.iso.macbook)		echo dk.macbook;;
115danish.*)			echo dk;;
116estonian.*)			echo ee;;
117spanish.dvorak)			echo es.dvorak;;
118spanish.iso*.acc)		echo es.acc;;
119spanish.iso)			echo es;;
120finnish.*)			echo fi;;
121fr.macbook.acc)			echo fr.macbook;;
122fr.iso.acc)			echo fr.acc;;
123fr.iso)				echo fr;;
124el.iso07)			echo gr;;
125gr.us101.acc)			echo gr.101.acc;;
126hr.iso)				echo hr;;
127hu.iso2.101keys)		echo hu.101;;
128hu.iso2.102keys)		echo hu.102;;
129iw.iso8)			echo il;;
130icelandic.iso.acc)		echo is.acc;;
131icelandic.iso)			echo is;;
132it.iso)				echo it;;
133jp.106x)			echo jp.capsctrl;;
134jp.106)				echo jp;;
135kk.pt154.io)			echo kz.io;;
136kk.pt154.kst)			echo kz.kst;;
137latinamerican.iso.acc)		echo latinamerican.acc;;
138lt.iso4)			echo lt;;
139norwegian.iso)			echo no;;
140norwegian.dvorak)		echo no.dvorak;;
141dutch.iso.acc)			echo nl;;
142eee_nordic)			echo nordic.asus-eee;;
143pl_PL.dvorak)			echo pl.dvorak;;
144pl_PL.ISO8859-2)		echo pl;;
145pt.iso.acc)			echo pt.acc;;
146pt.iso)				echo pt;;
147ru.koi8-r.shift)		echo ru.shift;;
148ru.koi8-r.win)			echo ru.win;;
149ru.*)				echo ru;;
150swedish.*)			echo se;;
151si.iso)				echo si;;
152sk.iso2)			echo sk;;
153tr.iso9.q)			echo tr;;
154ua.koi8-u.shift.alt)		echo ua.shift.alt;;
155ua.*)				echo ua;;
156uk.*-ctrl)			echo uk.capsctrl;;
157uk.dvorak)			echo uk.dvorak;;
158uk.*)				echo uk;;
159us.iso.acc)			echo us.acc;;
160us.pc-ctrl)			echo us.ctrl;;
161us.iso)				echo us;;
162	esac
163}
164
165kbdcontrol_load_keymap()
166{
167	errmsg=`kbdcontrol < ${kbddev} -l ${keymap} 2>&1`
168	if [ -n "${errmsg}" -a "${_sc_console}" = "vt" ]; then
169		_sc_keymap_msg="${errmsg}"
170		keymap_vt=`lookup_keymap_for_vt ${keymap}`
171		if [ -n "${keymap_vt}" ]; then
172			errmsg=`kbdcontrol < ${kbddev} -l ${keymap_vt} 2>&1`
173			if [ -z "${errmsg}" ]; then
174				_sc_keymap_msg="New keymap: In /etc/rc.conf replace 'keymap=${keymap}' by 'keymap=${keymap_vt}'"
175			fi
176		else
177			_sc_keymap_msg="No replacement found for keymap '${keymap}'.
178You may try to convert your keymap file using 'convert-keymap.pl', which is
179part of the system sources and located in /usr/src/tools/tools/vt/keymaps/"
180		fi
181	fi
182}
183
184# helper
185syscons_configure_keyboard()
186{
187	# keymap
188	#
189	case ${keymap} in
190	NO | '')
191		;;
192	*)
193		sc_init
194		echo -n ' keymap';	kbdcontrol_load_keymap
195		;;
196	esac
197
198	# keyrate
199	#
200	case ${keyrate} in
201	[Nn][Oo] | '')
202		;;
203	*)
204		sc_init
205		echo -n ' keyrate';	kbdcontrol < ${kbddev} -r ${keyrate}
206		;;
207	esac
208
209	# keybell
210	#
211	case ${keybell} in
212	[Nn][Oo] | '')
213		;;
214	*)
215		sc_init
216		echo -n ' keybell';	kbdcontrol < ${kbddev} -b ${keybell}
217		;;
218	esac
219
220	# change function keys
221	#
222	case ${keychange} in
223	[Nn][Oo] | '')
224		;;
225	*)
226		sc_init
227		echo -n ' keychange'
228		set -- ${keychange}
229		while [ $# -gt 0 ]; do
230			kbdcontrol <${kbddev} -f "$1" "$2"
231			shift; shift
232		done
233		;;
234	esac
235
236	# set this keyboard mode for all virtual terminals
237	#
238	if [ -n "${allscreens_kbdflags}" ]; then
239		sc_init
240		echo -n ' allscreens_kbd'
241		for ttyv in /dev/ttyv*; do
242			kbdcontrol ${allscreens_kbdflags} < ${ttyv} > ${ttyv} 2>&1
243		done
244	fi
245}
246
247syscons_setkeyboard()
248{
249	kbd=$1
250
251	if [ -z "${kbd}" ]; then
252		return 1
253	fi
254
255	# Check if the kbdmux(4) is the current active keyboard
256	kbdcontrol -i < ${kbddev} | grep kbdmux > /dev/null 2>&1
257	if [ $? -ne 0 ]; then
258		kbdcontrol -k ${kbd} < ${kbddev} > /dev/null 2>&1
259	fi
260
261	_sc_config="keyboard"
262	syscons_configure_keyboard
263
264	# Terminate keyboard configuration line and reset global variables.
265	#
266	if [ -n "${_sc_initdone}" ]; then
267		echo '.'
268		_sc_config="${_sc_console}"
269		_sc_initdone=
270	fi
271}
272
273syscons_precmd()
274{
275	if [ ! -c $kbddev ]
276	then
277		return 1
278	fi
279	return 0
280}
281
282syscons_bios_start()
283{
284	# cursor type
285	#
286	case ${cursor} in
287	[Nn][Oo] | '')
288		;;
289	*)
290		sc_init
291		echo -n ' cursor';	vidcontrol < ${viddev} -c ${cursor}
292		;;
293	esac
294
295	# screen mapping
296	#
297	case ${scrnmap} in
298	[Nn][Oo] | '')
299		;;
300	*)
301		sc_init
302		echo -n ' scrnmap';	vidcontrol < ${viddev} -l ${scrnmap}
303		;;
304	esac
305
306	# blank time
307	#
308	case ${blanktime} in
309	[Nn][Oo] | '')
310		;;
311	*)
312		sc_init
313		echo -n ' blanktime';	vidcontrol < ${viddev} -t ${blanktime}
314		;;
315	esac
316
317	# screen saver
318	#
319	case ${saver} in
320	[Nn][Oo] | '')
321		;;
322	*)
323		sc_init
324		echo -n ' screensaver'
325		for i in `kldstat | awk '$5 ~ "_saver\.ko$" { print $5 }'`; do
326			kldunload ${i}
327		done
328		load_kld -e _saver ${saver}_saver
329		;;
330	esac
331}
332
333syscons_start()
334{
335	# keyboard
336	#
337	if [ -n "${keyboard}" ]; then
338		syscons_setkeyboard ${keyboard}
339	fi
340
341	syscons_configure_keyboard
342
343	if [ "${_sc_bootmethod}" = "bios" ]; then
344		syscons_bios_start
345	fi
346
347	# font 8x16
348	#
349	case ${font8x16} in
350	[Nn][Oo] | '')
351		;;
352	*)
353		sc_init
354		echo -n ' font8x16';	vidcontrol < ${viddev} -f 8x16 ${font8x16}
355		;;
356	esac
357
358	# font 8x14
359	#
360	case ${font8x14} in
361	[Nn][Oo] | '')
362		;;
363	*)
364		sc_init
365		echo -n ' font8x14';	vidcontrol < ${viddev} -f 8x14 ${font8x14}
366		;;
367	esac
368
369	# font 8x8
370	#
371	case ${font8x8} in
372	[Nn][Oo] | '')
373		;;
374	*)
375		sc_init
376		echo -n ' font8x8';	vidcontrol < ${viddev} -f 8x8 ${font8x8}
377		;;
378	esac
379
380	# set this mode for all virtual screens
381	#
382	if [ -n "${allscreens_flags}" ]; then
383		sc_init
384		echo -n ' allscreens'
385		for ttyv in /dev/ttyv*; do
386			vidcontrol ${allscreens_flags} < ${ttyv} > ${ttyv} 2>&1
387		done
388	fi
389
390	[ -n "${_sc_initdone}" ] && echo '.'
391	if [ -n "${_sc_keymap_msg}" ]; then
392		echo
393		echo "WARNING:"
394		echo "${_sc_keymap_msg}."
395		echo
396	fi
397}
398
399load_rc_config $name
400run_rc_command $*
401
402