chiark / gitweb /
gummiboot/sd-boot/systemd-boot: rename galore
[elogind.git] / src / boot / efi / console.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /*
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation; either version 2.1 of the License, or
7  * (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  * Lesser General Public License for more details.
13  *
14  * Copyright (C) 2012-2013 Kay Sievers <kay@vrfy.org>
15  * Copyright (C) 2012 Harald Hoyer <harald@redhat.com>
16  */
17
18 #include <efi.h>
19 #include <efilib.h>
20
21 #include "util.h"
22 #include "console.h"
23
24 #define EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL_GUID \
25         { 0xdd9e7534, 0x7762, 0x4698, { 0x8c, 0x14, 0xf5, 0x85, 0x17, 0xa6, 0x25, 0xaa } }
26
27 struct _EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL;
28
29 typedef EFI_STATUS (EFIAPI *EFI_INPUT_RESET_EX)(
30         struct _EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This;
31         BOOLEAN ExtendedVerification;
32 );
33
34 typedef UINT8 EFI_KEY_TOGGLE_STATE;
35
36 typedef struct {
37         UINT32 KeyShiftState;
38         EFI_KEY_TOGGLE_STATE KeyToggleState;
39 } EFI_KEY_STATE;
40
41 typedef struct {
42         EFI_INPUT_KEY Key;
43         EFI_KEY_STATE KeyState;
44 } EFI_KEY_DATA;
45
46 typedef EFI_STATUS (EFIAPI *EFI_INPUT_READ_KEY_EX)(
47         struct _EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This;
48         EFI_KEY_DATA *KeyData;
49 );
50
51 typedef EFI_STATUS (EFIAPI *EFI_SET_STATE)(
52         struct _EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This;
53         EFI_KEY_TOGGLE_STATE *KeyToggleState;
54 );
55
56 typedef EFI_STATUS (EFIAPI *EFI_KEY_NOTIFY_FUNCTION)(
57         EFI_KEY_DATA *KeyData;
58 );
59
60 typedef EFI_STATUS (EFIAPI *EFI_REGISTER_KEYSTROKE_NOTIFY)(
61         struct _EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This;
62         EFI_KEY_DATA KeyData;
63         EFI_KEY_NOTIFY_FUNCTION KeyNotificationFunction;
64         VOID **NotifyHandle;
65 );
66
67 typedef EFI_STATUS (EFIAPI *EFI_UNREGISTER_KEYSTROKE_NOTIFY)(
68         struct _EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *This;
69         VOID *NotificationHandle;
70 );
71
72 typedef struct _EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL {
73         EFI_INPUT_RESET_EX Reset;
74         EFI_INPUT_READ_KEY_EX ReadKeyStrokeEx;
75         EFI_EVENT WaitForKeyEx;
76         EFI_SET_STATE SetState;
77         EFI_REGISTER_KEYSTROKE_NOTIFY RegisterKeyNotify;
78         EFI_UNREGISTER_KEYSTROKE_NOTIFY UnregisterKeyNotify;
79 } EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL;
80
81 EFI_STATUS console_key_read(UINT64 *key, BOOLEAN wait) {
82         EFI_GUID EfiSimpleTextInputExProtocolGuid = EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL_GUID;
83         static EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *TextInputEx;
84         static BOOLEAN checked;
85         UINTN index;
86         EFI_INPUT_KEY k;
87         EFI_STATUS err;
88
89         if (!checked) {
90                 err = LibLocateProtocol(&EfiSimpleTextInputExProtocolGuid, (VOID **)&TextInputEx);
91                 if (EFI_ERROR(err))
92                         TextInputEx = NULL;
93
94                 checked = TRUE;
95         }
96
97         /* wait until key is pressed */
98         if (wait) {
99                 if (TextInputEx)
100                         uefi_call_wrapper(BS->WaitForEvent, 3, 1, &TextInputEx->WaitForKeyEx, &index);
101                 else
102                         uefi_call_wrapper(BS->WaitForEvent, 3, 1, &ST->ConIn->WaitForKey, &index);
103         }
104
105         if (TextInputEx) {
106                 EFI_KEY_DATA keydata;
107                 UINT64 keypress;
108
109                 err = uefi_call_wrapper(TextInputEx->ReadKeyStrokeEx, 2, TextInputEx, &keydata);
110                 if (!EFI_ERROR(err)) {
111                         UINT32 shift = 0;
112
113                         /* do not distinguish between left and right keys */
114                         if (keydata.KeyState.KeyShiftState & EFI_SHIFT_STATE_VALID) {
115                                 if (keydata.KeyState.KeyShiftState & (EFI_RIGHT_CONTROL_PRESSED|EFI_LEFT_CONTROL_PRESSED))
116                                         shift |= EFI_CONTROL_PRESSED;
117                                 if (keydata.KeyState.KeyShiftState & (EFI_RIGHT_ALT_PRESSED|EFI_LEFT_ALT_PRESSED))
118                                         shift |= EFI_ALT_PRESSED;
119                         };
120
121                         /* 32 bit modifier keys + 16 bit scan code + 16 bit unicode */
122                         keypress = KEYPRESS(shift, keydata.Key.ScanCode, keydata.Key.UnicodeChar);
123                         if (keypress > 0) {
124                                 *key = keypress;
125                                 return 0;
126                         }
127                 }
128         }
129
130         /* fallback for firmware which does not support SimpleTextInputExProtocol
131          *
132          * This is also called in case ReadKeyStrokeEx did not return a key, because
133          * some broken firmwares offer SimpleTextInputExProtocol, but never acually
134          * handle any key. */
135         err  = uefi_call_wrapper(ST->ConIn->ReadKeyStroke, 2, ST->ConIn, &k);
136         if (EFI_ERROR(err))
137                 return err;
138
139         *key = KEYPRESS(0, k.ScanCode, k.UnicodeChar);
140         return 0;
141 }