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