History log of /u-boot/lib/efi_loader/efi_console.c
Revision Date Author Comments
(<<< Hide modified files)
(Show modified files >>>)
# c38cb227 14-Dec-2023 Tom Rini <trini@konsulko.com>

efi_loader: Remove <common.h>

We largely do not need <common.h> in these files, so drop it. The only
exception here is that efi_freestanding.c needs <linux/types.h> and had
been getting that via <common.h>.

Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Tom Rini <trini@konsulko.com>

# d3970e04 03-Mar-2023 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: describe term_get_char()

Add a function description.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
Reviewed-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>

# e7175f93 10-Feb-2023 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: static functions in efi_console.c

Define function set_shift_mask() as static as it is not used externally.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>

# e585b79e 18-Jan-2023 Jan Kiszka <jan.kiszka@siemens.com>

efi_loader: Avoid overwriting previous outputs on console screen clearing

Before clearing the screen, ensure that no previous output of firmware
or UEFI programs will be overwritten on serial devices or other
streaming consoles. This helps generating complete boot logs.

Tested regarding multi-output against qemu-x86_defconfig. Still, there
were remaining concerns about side effects, so this is provided as an
opt-in feature.

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
Reviewed-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>

# de94f0f3 18-Jan-2023 Jan Kiszka <jan.kiszka@siemens.com>

efi_loader: Set default console colors on efi_cout_clear_screen if needed

Ensures a consistent background color of the whole screen for succeeding
outputs as both demanded by the spec and implemented in EDK2 as well.

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
Reviewed-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>

# b86986c7 18-Oct-2022 Simon Glass <sjg@chromium.org>

video: Rename CONFIG_DM_VIDEO to CONFIG_VIDEO

Now that all the old code is gone, rename this option. Driver model
migration is now complete.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 7831d36f 14-Oct-2022 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: avoid EFI_CALL() when draining console

Use internal function.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>

# f3272366 15-Oct-2022 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: avoid EFI_CALL() for clearing screen

Carve out function efi_clear_screen.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>

# 05c4c9e2 06-Oct-2022 Ilias Apalodimas <ilias.apalodimas@linaro.org>

efi_loader: define internal implementations of install/uninstallmultiple

A following patch is cleaning up the core EFI code trying to remove
sequences of efi_create_handle, efi_add_protocol.

Although this works fine there's a problem with the latter since it is
usually combined with efi_delete_handle() which blindly removes all
protocols on a handle and deletes the handle. We should try to adhere to
the EFI spec which only deletes a handle if the last instance of a protocol
has been removed. Another problem is that efi_delete_handle() never checks
for opened protocols, but the EFI spec defines that the caller is
responsible for ensuring that there are no references to a protocol
interface that is going to be removed.

So let's fix this by replacing all callsites of
efi_create_handle(), efi_add_protocol() , efi_delete_handle() with
Install/UninstallMultipleProtocol.

In order to do that redefine functions that can be used by the U-Boot
proper internally and add '_ext' variants that will be used from the
EFI API

Signed-off-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>
Reviewed-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# e9a1ff97 19-Sep-2022 Tom Rini <trini@konsulko.com>

Merge branch 'master' into next

Signed-off-by: Tom Rini <trini@konsulko.com>


# 87d79142 12-Sep-2022 Masahisa Kojima <masahisa.kojima@linaro.org>

eficonfig: menu-driven addition of UEFI boot option

This commit add the "eficonfig" command.
The "eficonfig" command implements the menu-driven UEFI boot option
maintenance feature. This commit implements the addition of
new boot option. User can select the block device volume having
efi_simple_file_system_protocol and select the file corresponding
to the Boot#### variable. User can also enter the description and
optional_data of the BOOT#### variable in utf8.

This commit adds "include/efi_config.h", it contains the common
definition to be used from other menus such as UEFI Secure Boot
key management.

Signed-off-by: Masahisa Kojima <masahisa.kojima@linaro.org>

# e053a144 01-Sep-2022 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: support CTRL+\ - CTRL+_

In the extended text input protocol support input of control letters
0x1c - 0x1f.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
Reviewed-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>

# 0b7b56d7 01-Sep-2022 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: compliance Simple Text Input Ex Protocol

We cannot expect the buffers passed to the input protocols to be zero
filled. If only modifier keys are pressed, we have to return EFI_NOT_READY
but we still have to fill the key structure.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
Reviewed-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>

# 68edbed4 14-Jun-2022 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: initialize console size late

If CONFIG_VIDEO_DM=n we query the display size from the serial console.
Especially when using a remote console the response can be so late that
it interferes with autoboot.

Only query the console size when running an EFI binary.

Add debug output showing the determined console size.

Reported-by: Fabio Estevam <festevam@gmail.com>
Fixes: a57ad20d07e8 ("efi_loader: split efi_init_obj_list() into two stages")
Fixes: a9bf024b2933 ("efi_loader: disk: a helper function to create efi_disk objects from udevice")
Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
Tested-by: Fabio Estevam <festevam@denx.de>
Tested-by: Heiko Thiery <heiko.thiery@gmail.com>

# 5c1037d5 30-Apr-2022 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: reset colors before clearing screen

When resetting the text console the colors have to be set before clearing
the screen. Otherwise the background color may be wrong.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>

# 3c95b323 04-Feb-2022 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: add handle for UART

When loading an EFI binary via the UART we assign a UART device path to it.
But we lack a handle with that device path.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>

# abd62e4d 29-Jun-2021 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: missing parentheses in query_console_size

After if we should use parentheses to keep the code readable.

Fixes: a95f4c885991 ("efi_loader: NULL dereference in EFI console")
Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# a95f4c88 15-Mar-2021 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: NULL dereference in EFI console

Even if CONFIG_DM_VIDEO=y and stdout="vidconsole", a video device may not
be available. Check the return values of the relevant functions.

If no video output device is available, assume that the serial console is
in use.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 997b57fc 28-Feb-2021 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: console size of vidconsole

If stdout is 'vidconsole', we correctly set the console size.
If stdout is 'vidconsole,serial', the video console is ignored.

We should always evaluate the size of vidconsole if it is the primary
console.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 7913c7dc 04-Jan-2021 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: typedef efi_string_t text output protocol

We do not want to use typedefs in U-Boot.

Do not use efi_string_t in the EFI_TEXT_OUTPUT_PROTOCOL.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 9abb01af 27-Dec-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: escape key handling

Up to now the escape key was not correctly detected in UEFI applications.
We had to hit it twice for a single escape to be recognized.

Use a 10 ms delay to detect if we are dealing with the escape key or an
escape sequence.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# c670aeee 07-Oct-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

common: rename getc() to getchar()

The sandbox is built with the SDL2 library with invokes the X11 library
which in turn calls getc(). But getc() in glibc is defined as

int getc(FILE *)

This does not match our definition.

int getc(void)

The sandbox crashes when called with parameter -l.

Rename our library symbol getc() to getchar().

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Tom Rini <trini@konsulko.com>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 325567d3 04-Jun-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: comments EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL

Provide missing comments for the functions implementing the
EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 336d4615 03-Feb-2020 Simon Glass <sjg@chromium.org>

dm: core: Create a new header file for 'compat' features

At present dm/device.h includes the linux-compatible features. This
requires including linux/compat.h which in turn includes a lot of headers.
One of these is malloc.h which we thus end up including in every file in
U-Boot. Apart from the inefficiency of this, it is problematic for sandbox
which needs to use the system malloc() in some files.

Move the compatibility features into a separate header file.

Signed-off-by: Simon Glass <sjg@chromium.org>

# b0ad9b5b 22-Dec-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: clear screen should move cursor to home

On a VT100 terminal <ESC>[2J should be enough to both clear the whole
screen and set the cursor to position (1, 1). But the Linux console does
not behave like this. So send an extra <ESC>[H. For reference see the
console_codes(4) man page.

Add a function description.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 1045315d 14-Nov-2019 Simon Glass <sjg@chromium.org>

common: Move get_ticks() function out of common.h

This function belongs in time.h so move it over and add a comment.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Tom Rini <trini@konsulko.com>

# fe1a81c1 05-Sep-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

doc: UEFI API documentation

Add some more files to the UEFI API documentation.

Correct some Sphinx comments.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 03446987 04-Sep-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: do not set invalid screen mode

EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.SetMode() should return EFI_UNDEFINED if a
screen mode is not available.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Alexander Graf <agraf@csgraf.de>

# 97ea0690 04-Sep-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: cursor positioning

When backspacing in column 0 do no set the column index to ULONG_MAX.
Ensure that the row number is not set to ULONG_MAX even if the row count is
advertised as 0.
Ignore control characters other the 0x08, 0x0a, 0x0d when updating the
column.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 7b51b576 01-Aug-2019 Simon Glass <sjg@chromium.org>

env: Move env_get() to env.h

Move env_get() over to the new header file.

Acked-by: Joe Hershberger <joe.hershberger@ni.com>
Signed-off-by: Simon Glass <sjg@chromium.org>

# 3b435c11 16-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: console incorrectly advertised left logo key

Avoid to signal that the left logo key is pressed, when it is not.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 3b2b2de8 16-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: alternative scan codes for F5, END, HOME

Depending on the key board alternative scan codes are used for F5, END, and
HOME.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 3950f0f8 13-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix SetAttribute()

The SetAttribute() service and the Reset() service of the simple text
output protocol must update the attribute value in the mode information.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 2ad238fc 13-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix SetMode()

Correct the check of the mode number in SetMode() service of the simple
text output protocol.

Clear the screen in SetMode().

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 7eaa900e 06-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: event signaling in ExitBootServices

ExitBootServices() has to stop timer related activity before calling the
events of the EFI_EVENT_GROUP_EXIT_BOOT_SERVICES event group. But our
current implementation was stopping all other events.

All events have to observe the task priority level.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 25e6fb5e 02-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix EnableCursor()

The EnableCursor() service of the simple text output protocol must update
the the CursorVisible field of the output mode.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# b31ca6bf 18-May-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: parameter check OutputString

Check the parameters against NULL.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# acee9652 18-May-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.SetState() correct parameter

KeyToggleState is a pointer according to UEFI spec 2.8.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# bfc2dd53 06-Apr-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: correct CTRL-A - CTRL-Z console input

In the extended text input protocol CTRL-A - CTRL-Z have to be signaled as
Unicode characters a-z or A-Z depending on the shift state and not as 0x01
to 0x1a.

Update Python unit test.

This patch is required for using the EFI shell `edit` command.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# dd1a1ec2 04-Mar-2019 Matthias Brugger <mbrugger@suse.com>

efi_loader: Fix serial console size detection

Function term_read_reply tries to read from the serial console until
the end_char was read. This can hang forever if we are, for some reason,
not able to read the full response (e.g. serial buffer too small,
frame error). This patch moves the timeout detection into
term_read_reply() to assure we will make progress.

Fixes: 6bb591f704 ("efi_loader: query serial console size reliably")
Signed-off-by: Matthias Brugger <mbrugger@suse.com>

Throw missing error when an incomplete reply for the cursor position is
received.

Change type of argument of term_get_char() *s32. This renders the function
reusable in efi_cin_read_key().

Reviewed-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# e1fec152 18-Oct-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix typos

Fix typos in EFI subsystem comments.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# fae0118e 25-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: eliminate handle member

A pointer to a struct efi_object is a handle. We do not need any handle
member in this structure. Let's eliminate it.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 6f566c23 01-Oct-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: return type efi_console_register()

Use a return type that can encompass the return value.

This fixes CoverityScan CID 184090.

Reported-by: Tom Rini <trini@konsulko.com>
Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 6bb591f7 15-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: query serial console size reliably

Not all terminals understand CSI [18t for querying the console size.
We should adhere to escape sequences documented in the console_codes
manpage and the ECMA-48 standard.

So here we follow a different approach. We position the cursor to the
bottom right and query its position. Before leaving the function we
restore the original cursor position.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# aaace4b0 14-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: terminal left upper corner is [1, 1]

For the VT102 and the Linux console the left upper corner is [1, 1].
EFI uses [0, 0] as left upper corner.

Check parameters of the SetCursorPosition() service.

Use CSI H like EDK2 for positioning.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 4fdcf066 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: implement key notify functions

Implement registering and unregistreing key notify functions in the
EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 55fbdf99 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: support modifiers for F1 - F4

Support modifiers for F1 - F4.
Add support for letters with ALT key.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 110c6280 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL

This patch implements the EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.

The implementation of notification functions is postponed to a later
patch.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 0dfd13a4 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: rework event handling for console

Preread the next key in the console timer event.

The EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL requires to trigger registered key
notification functions based on the prefetched key.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 35cbb796 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: support Unicode text input

Up to now the EFI_TEXT_INPUT_PROTOCOL only supported ASCII characters.
With the patch it can consume UTF-8 from the console.

Currently only the serial console and the console can deliver UTF-8.
Local consoles are restricted to ASCII.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 14d103bb 08-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: typos in efi_console.c

Fix a few typos.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 3e603ec7 08-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: struct efi_simple_text_input_protocol

%s/efi_simple_input_interface/efi_simple_text_input_protocol/

We should be consistent in the naming of the EFI protocol interface
structures. The protocol is called EFI_SIMPLE_TEXT_INPUT_PROTOCOL.

%s/ExtendedVerification/extended_verification/

Use consistent naming of function parameters. Do not use CamelCase.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 40e3e757 04-Sep-2018 Alexander Graf <agraf@csgraf.de>

efi_loader: Add console handle references to in/out/err

We register the console in/out protocol on the loaded image handle, but
never tell anyone about it in the systab.

Because of this Shell.efi can not find the input protocol. So let's add
a proper reference.

Signed-off-by: Alexander Graf <agraf@suse.de>
Reviewed-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# ba7bd5c2 31-Aug-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: don't use unlimited stack as buffer

The length of a string printed to the console by the
EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL is not limited by the UEFI spec.
Hence should not allocate a buffer for it on the stack.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 1dde0d57 31-Aug-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: rename utf16_strlen, utf16_strnlen

The function names utf16_strlen() and utf16_strnlen() are misnomers.
The functions do not count utf-16 characters but non-zero words.
So let's rename them to u16_strlen and u16_strnlen().

In utf16_dup() avoid assignment in if clause.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 9d12daff 05-Jul-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.Reset()

Implement the reset service of the EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.

This should resolve the error reported by the SCT in
Protocol/SimpleTextOut/BlackBoxTest/SimpleTextOutBBTestFunction_uefi.c:639

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# e67ff94d 05-Jul-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: clear screen has to reset cursor position

After clearing the screen the cursor position is row 0, column 0.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 4f187897 05-Jul-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_INPUT_PROTOCOL.Reset()

Implement the reset service of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL.

This should resolve the error reported by the SCT in
Protocol/SimpleTextIn/BlackBoxTest/SimpleTextInBBTestFunction.c:193

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 80483b2a 03-Jun-2018 Alexander Graf <agraf@csgraf.de>

efi_loader: Fix warning in raw/cols query

The code to determine rows / cols on the screen could potentially run
into a case where it doesn't know how big the screen is. In that case,
assume 80x25.

Signed-off-by: Alexander Graf <agraf@suse.de>

# 62217295 16-May-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: avoid using unitialized values as console size

If a request for the console size would be answered with a response
with less then three values, uninitialized stack memory would be
copied to the number of rows and columns of the terminal.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# a4aa7bef 29-Apr-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: always check parameters in efi_cout_query_mode()

If we cannot determine the size of the serial terminal we still have
to check the parameters of efi_cout_query_mode().

Querying the size of the serial terminal drains the keyboard buffer.
So make sure we do this during the initialization and not in the midst
of an EFI application.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 7ca7c3c0 29-Apr-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: updating the cursor position

The current coding advances the column by the number of UTF-8 bytes.
The column should be increased by one per unicode character.

The UEFI spec provides advance rules for U+0000, U+0008, U+000A,
and U000D. All other characters, including control characters
U+0007 (bel) and U+0009 (tab), have to increase the column by one.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# f739fcd8 07-May-2018 Tom Rini <trini@konsulko.com>

SPDX: Convert a few files that were missed before

As part of the main conversion a few files were missed. These files had
additional whitespace after the '*' and before the SPDX tag and my
previous regex was too strict. This time I did a grep for all SPDX tags
and then filtered out anything that matched the correct styles.

Fixes: 83d290c56fab ("SPDX: Convert all of our single license tags to Linux Kernel style")
Reported-by: Heinrich Schuchardt <xypron.debian@gmx.de>
Signed-off-by: Tom Rini <trini@konsulko.com>

# b095f3c8 18-Feb-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: implement event groups

If an event of a group event is signaled all other events of the same
group are signaled too.

Function efi_signal_event is renamed to efi_queue_event.
A new function efi_signal_event is introduced that checks if an event
belongs to a group and than signals all events of the group.
Event group notifciation is implemented for ExitBootServices,
InstallConfigurationTable, and ResetSystem.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 0fb4169e 19-Feb-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: correct input of special keys

Don't set unicode_char if scan_code is set.
Add support for page up, page down, and insert.
Correct input of function keys.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 8396e3fd 03-Mar-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: remove deprecated ConsoleControlProtocol

The console control protocol is not defined in the UEFI standard.

It exists in EDK2's EdkCompatiblityPkg package. But this package
is deprecated according to
https://github.com/tianocore/tianocore.github.io/wiki/Differences-between-EDK-and-EDK-II

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 9bc9664d 19-Jan-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: add check_tpl parameter to efi_signal_event

In ExitBootServices we need to signal events irrespective of the current
TPL level. A new parameter check_tpl is added to efi_signal_event().

Function efi_console_timer_notify() gets some comments.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 2074f700 11-Jan-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: consistently use efi_handle_t for handles

We should consistently use the efi_handle_t typedef when
referring to handles.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# ebb4dd5b 26-Oct-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: efi_console: use helper functions

Use helper functions efi_created_handle and efi_add_protocol
for creating the console handles and instaling the respective
protocols.

This change is needed if we want to move from an array of
protocols to a linked list of protocols.

Eliminate EFI_PROTOCOL_OBJECT which is not used anymore.

Currently we have not defined protocol interfaces to be const.
So efi_con_out and efi_console_control cannot be defined as const.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 2d5dc2a5 10-Oct-2017 Rob Clark <robdclark@gmail.com>

efi_loader: console support for color attributes

Shell.efi uses this, and supporting color attributes makes things look
nicer. Map the EFI fg/bg color attributes to ANSI escape sequences.
Not all colors have a perfect match, but spec just says "Devices
supporting a different number of text colors are required to emulate the
above colors to the best of the device’s capabilities".

Signed-off-by: Rob Clark <robdclark@gmail.com>
Tested-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Alexander Graf <agraf@suse.de>
[agraf: s/unsigned/unsigned int/]
Signed-off-by: Alexander Graf <agraf@suse.de>

# e190e897 04-Oct-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: use type bool for event states

Queued and signaled describe boolean states of events.
So let's use type bool and rename the structure members to is_queued
and is_signaled.

Update the comments for is_queued and is_signaled.

Reported-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Rob Clark <robdclark@gmail.com>
Reviewed-by: Rob Clark <robdclark@gmail.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 3a45bc7f 13-Sep-2017 Rob Clark <robdclark@gmail.com>

efi_loader: Some console improvements for vidconsole

1) use fputs() to reduce cache flushes from once-per-char to
once-per-string
2) handle \r, \t, and \b in addition to just \n for tracking
cursor position
3) cursor row/col are zero based, not one based

Signed-off-by: Rob Clark <robdclark@gmail.com>
[agraf: s/unsigned/unsigned int/]
Signed-off-by: Alexander Graf <agraf@suse.de>

# a18c5a83 13-Sep-2017 Rob Clark <robdclark@gmail.com>

efi_loader: Correctly figure out size for vidconsole

If stdout is vidconsole, we cannot rely on ANSI escape sequences to
query the size, as vidconsole cannot reply on stdin. Instead special-
case this if stdout is vidconsole.

Signed-off-by: Rob Clark <robdclark@gmail.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 71cc25c3 13-Sep-2017 Rob Clark <robdclark@gmail.com>

efi_loader: split out escape sequence based size query

We need to do something different for vidconsole, since it cannot
respond to the query on stdin. Prep work for next patch.

Signed-off-by: Rob Clark <robdclark@gmail.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# ca62a4f5 15-Sep-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: implement queueing of the notification function

For the correct implementation of the task priority level (TPL)
calling the notification function must be queued.

Add a status field 'queued' to events.

In function efi_signal_event set status queued if a notification
function exists and reset it after we have called the function.
A later patch will add a check of the TPL here.

In efi_create_event and efi_close_event unset the queued status.

In function efi_wait_for_event and efi_check_event
queue the notification function.

In efi_timer_check call the efi_notify_event
if the status queued is set.
For all timer events set status signaled.

In efi_console_timer_notify set the signaled state of the
WaitForKey event.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 78178bb0 09-Sep-2017 Rob Clark <robdclark@gmail.com>

lib: add some utf16 handling helpers

We'll eventually want these in a few places in efi_loader, and also
vsprintf.

Signed-off-by: Rob Clark <robdclark@gmail.com>

# a17e62cc 24-Jul-2017 Rob Clark <robdclark@gmail.com>

efi_loader: expose protocols via GUID

shim.efi (or rather gnu-efi's LibLocateProtocol() which shim.efi uses)
resolves protocols via efi_locate_handle() so the console protocols
need to be added to the efi object list.

Signed-off-by: Rob Clark <robdclark@gmail.com>
[agraf: whitespace fixes]
Signed-off-by: Alexander Graf <agraf@suse.de>

# ff925938 19-Jul-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_console: use EFIAPI for callback functions

The call to efi_create_event requires notification functions
flagged as EFIAPI.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 91be9a77 18-Jul-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_console: set up events

Set up a timer event and the WaitForKey event.
In the notify function of the timer event check for console input
and signal the WaitForKey event accordingly.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 5be8b0a3 07-Nov-2016 Emmanuel Vadot <manu@bidouilliste.com>

efi_loader: console: Correctly report modes

Add support for EFI console modes.
Mode 0 is always 80x25 and present by EFI specification.
Mode 1 is always 80x50 and not mandatory.
Mode 2 and above is freely usable.

If the terminal can handle mode 1, we mark it as supported.
If the terminal size is greater than mode 0 and different than mode 1,
we install it as mode 2.

Modes can be switch with cout_set_mode.

Changes in V5:
Correctly detect mode before enabling mode 2.

Changes in V4:
Reset cursor positon on mode switch
Use local variables in console query code

Changes in V3:
Valid mode are 0 to EFIMode-1
Fix style

Changes in V2:
Add mode switch
Report only the modes that we support

Signed-off-by: Emmanuel Vadot <manu@bidouilliste.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# c1311ad4 03-Mar-2016 Alexander Graf <agraf@csgraf.de>

efi_loader: Add console interface

One of the basic EFI interfaces is the console interface. Using it an EFI
application can interface with the user. This patch implements an EFI console
interface using getc() and putc().

Today, we only implement text based consoles. We also convert the EFI Unicode
characters to UTF-8 on the fly, hoping that everyone managed to jump on the
train by now.

Signed-off-by: Alexander Graf <agraf@suse.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Tested-by: Simon Glass <sjg@chromium.org>

# d3970e04 03-Mar-2023 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: describe term_get_char()

Add a function description.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
Reviewed-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>

# e7175f93 10-Feb-2023 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: static functions in efi_console.c

Define function set_shift_mask() as static as it is not used externally.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>

# e585b79e 18-Jan-2023 Jan Kiszka <jan.kiszka@siemens.com>

efi_loader: Avoid overwriting previous outputs on console screen clearing

Before clearing the screen, ensure that no previous output of firmware
or UEFI programs will be overwritten on serial devices or other
streaming consoles. This helps generating complete boot logs.

Tested regarding multi-output against qemu-x86_defconfig. Still, there
were remaining concerns about side effects, so this is provided as an
opt-in feature.

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
Reviewed-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>

# de94f0f3 18-Jan-2023 Jan Kiszka <jan.kiszka@siemens.com>

efi_loader: Set default console colors on efi_cout_clear_screen if needed

Ensures a consistent background color of the whole screen for succeeding
outputs as both demanded by the spec and implemented in EDK2 as well.

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
Reviewed-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>

# b86986c7 18-Oct-2022 Simon Glass <sjg@chromium.org>

video: Rename CONFIG_DM_VIDEO to CONFIG_VIDEO

Now that all the old code is gone, rename this option. Driver model
migration is now complete.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 7831d36f 14-Oct-2022 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: avoid EFI_CALL() when draining console

Use internal function.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>

# f3272366 15-Oct-2022 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: avoid EFI_CALL() for clearing screen

Carve out function efi_clear_screen.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>

# 05c4c9e2 06-Oct-2022 Ilias Apalodimas <ilias.apalodimas@linaro.org>

efi_loader: define internal implementations of install/uninstallmultiple

A following patch is cleaning up the core EFI code trying to remove
sequences of efi_create_handle, efi_add_protocol.

Although this works fine there's a problem with the latter since it is
usually combined with efi_delete_handle() which blindly removes all
protocols on a handle and deletes the handle. We should try to adhere to
the EFI spec which only deletes a handle if the last instance of a protocol
has been removed. Another problem is that efi_delete_handle() never checks
for opened protocols, but the EFI spec defines that the caller is
responsible for ensuring that there are no references to a protocol
interface that is going to be removed.

So let's fix this by replacing all callsites of
efi_create_handle(), efi_add_protocol() , efi_delete_handle() with
Install/UninstallMultipleProtocol.

In order to do that redefine functions that can be used by the U-Boot
proper internally and add '_ext' variants that will be used from the
EFI API

Signed-off-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>
Reviewed-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# e9a1ff97 19-Sep-2022 Tom Rini <trini@konsulko.com>

Merge branch 'master' into next

Signed-off-by: Tom Rini <trini@konsulko.com>


# 87d79142 12-Sep-2022 Masahisa Kojima <masahisa.kojima@linaro.org>

eficonfig: menu-driven addition of UEFI boot option

This commit add the "eficonfig" command.
The "eficonfig" command implements the menu-driven UEFI boot option
maintenance feature. This commit implements the addition of
new boot option. User can select the block device volume having
efi_simple_file_system_protocol and select the file corresponding
to the Boot#### variable. User can also enter the description and
optional_data of the BOOT#### variable in utf8.

This commit adds "include/efi_config.h", it contains the common
definition to be used from other menus such as UEFI Secure Boot
key management.

Signed-off-by: Masahisa Kojima <masahisa.kojima@linaro.org>

# e053a144 01-Sep-2022 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: support CTRL+\ - CTRL+_

In the extended text input protocol support input of control letters
0x1c - 0x1f.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
Reviewed-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>

# 0b7b56d7 01-Sep-2022 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: compliance Simple Text Input Ex Protocol

We cannot expect the buffers passed to the input protocols to be zero
filled. If only modifier keys are pressed, we have to return EFI_NOT_READY
but we still have to fill the key structure.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
Reviewed-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>

# 68edbed4 14-Jun-2022 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: initialize console size late

If CONFIG_VIDEO_DM=n we query the display size from the serial console.
Especially when using a remote console the response can be so late that
it interferes with autoboot.

Only query the console size when running an EFI binary.

Add debug output showing the determined console size.

Reported-by: Fabio Estevam <festevam@gmail.com>
Fixes: a57ad20d07e8 ("efi_loader: split efi_init_obj_list() into two stages")
Fixes: a9bf024b2933 ("efi_loader: disk: a helper function to create efi_disk objects from udevice")
Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
Tested-by: Fabio Estevam <festevam@denx.de>
Tested-by: Heiko Thiery <heiko.thiery@gmail.com>

# 5c1037d5 30-Apr-2022 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: reset colors before clearing screen

When resetting the text console the colors have to be set before clearing
the screen. Otherwise the background color may be wrong.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>

# 3c95b323 04-Feb-2022 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: add handle for UART

When loading an EFI binary via the UART we assign a UART device path to it.
But we lack a handle with that device path.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>

# abd62e4d 29-Jun-2021 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: missing parentheses in query_console_size

After if we should use parentheses to keep the code readable.

Fixes: a95f4c885991 ("efi_loader: NULL dereference in EFI console")
Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# a95f4c88 15-Mar-2021 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: NULL dereference in EFI console

Even if CONFIG_DM_VIDEO=y and stdout="vidconsole", a video device may not
be available. Check the return values of the relevant functions.

If no video output device is available, assume that the serial console is
in use.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 997b57fc 28-Feb-2021 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: console size of vidconsole

If stdout is 'vidconsole', we correctly set the console size.
If stdout is 'vidconsole,serial', the video console is ignored.

We should always evaluate the size of vidconsole if it is the primary
console.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 7913c7dc 04-Jan-2021 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: typedef efi_string_t text output protocol

We do not want to use typedefs in U-Boot.

Do not use efi_string_t in the EFI_TEXT_OUTPUT_PROTOCOL.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 9abb01af 27-Dec-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: escape key handling

Up to now the escape key was not correctly detected in UEFI applications.
We had to hit it twice for a single escape to be recognized.

Use a 10 ms delay to detect if we are dealing with the escape key or an
escape sequence.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# c670aeee 07-Oct-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

common: rename getc() to getchar()

The sandbox is built with the SDL2 library with invokes the X11 library
which in turn calls getc(). But getc() in glibc is defined as

int getc(FILE *)

This does not match our definition.

int getc(void)

The sandbox crashes when called with parameter -l.

Rename our library symbol getc() to getchar().

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Tom Rini <trini@konsulko.com>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 325567d3 04-Jun-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: comments EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL

Provide missing comments for the functions implementing the
EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 336d4615 03-Feb-2020 Simon Glass <sjg@chromium.org>

dm: core: Create a new header file for 'compat' features

At present dm/device.h includes the linux-compatible features. This
requires including linux/compat.h which in turn includes a lot of headers.
One of these is malloc.h which we thus end up including in every file in
U-Boot. Apart from the inefficiency of this, it is problematic for sandbox
which needs to use the system malloc() in some files.

Move the compatibility features into a separate header file.

Signed-off-by: Simon Glass <sjg@chromium.org>

# b0ad9b5b 22-Dec-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: clear screen should move cursor to home

On a VT100 terminal <ESC>[2J should be enough to both clear the whole
screen and set the cursor to position (1, 1). But the Linux console does
not behave like this. So send an extra <ESC>[H. For reference see the
console_codes(4) man page.

Add a function description.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 1045315d 14-Nov-2019 Simon Glass <sjg@chromium.org>

common: Move get_ticks() function out of common.h

This function belongs in time.h so move it over and add a comment.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Tom Rini <trini@konsulko.com>

# fe1a81c1 05-Sep-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

doc: UEFI API documentation

Add some more files to the UEFI API documentation.

Correct some Sphinx comments.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 03446987 04-Sep-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: do not set invalid screen mode

EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.SetMode() should return EFI_UNDEFINED if a
screen mode is not available.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Alexander Graf <agraf@csgraf.de>

# 97ea0690 04-Sep-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: cursor positioning

When backspacing in column 0 do no set the column index to ULONG_MAX.
Ensure that the row number is not set to ULONG_MAX even if the row count is
advertised as 0.
Ignore control characters other the 0x08, 0x0a, 0x0d when updating the
column.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 7b51b576 01-Aug-2019 Simon Glass <sjg@chromium.org>

env: Move env_get() to env.h

Move env_get() over to the new header file.

Acked-by: Joe Hershberger <joe.hershberger@ni.com>
Signed-off-by: Simon Glass <sjg@chromium.org>

# 3b435c11 16-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: console incorrectly advertised left logo key

Avoid to signal that the left logo key is pressed, when it is not.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 3b2b2de8 16-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: alternative scan codes for F5, END, HOME

Depending on the key board alternative scan codes are used for F5, END, and
HOME.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 3950f0f8 13-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix SetAttribute()

The SetAttribute() service and the Reset() service of the simple text
output protocol must update the attribute value in the mode information.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 2ad238fc 13-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix SetMode()

Correct the check of the mode number in SetMode() service of the simple
text output protocol.

Clear the screen in SetMode().

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 7eaa900e 06-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: event signaling in ExitBootServices

ExitBootServices() has to stop timer related activity before calling the
events of the EFI_EVENT_GROUP_EXIT_BOOT_SERVICES event group. But our
current implementation was stopping all other events.

All events have to observe the task priority level.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 25e6fb5e 02-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix EnableCursor()

The EnableCursor() service of the simple text output protocol must update
the the CursorVisible field of the output mode.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# b31ca6bf 18-May-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: parameter check OutputString

Check the parameters against NULL.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# acee9652 18-May-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.SetState() correct parameter

KeyToggleState is a pointer according to UEFI spec 2.8.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# bfc2dd53 06-Apr-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: correct CTRL-A - CTRL-Z console input

In the extended text input protocol CTRL-A - CTRL-Z have to be signaled as
Unicode characters a-z or A-Z depending on the shift state and not as 0x01
to 0x1a.

Update Python unit test.

This patch is required for using the EFI shell `edit` command.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# dd1a1ec2 04-Mar-2019 Matthias Brugger <mbrugger@suse.com>

efi_loader: Fix serial console size detection

Function term_read_reply tries to read from the serial console until
the end_char was read. This can hang forever if we are, for some reason,
not able to read the full response (e.g. serial buffer too small,
frame error). This patch moves the timeout detection into
term_read_reply() to assure we will make progress.

Fixes: 6bb591f704 ("efi_loader: query serial console size reliably")
Signed-off-by: Matthias Brugger <mbrugger@suse.com>

Throw missing error when an incomplete reply for the cursor position is
received.

Change type of argument of term_get_char() *s32. This renders the function
reusable in efi_cin_read_key().

Reviewed-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# e1fec152 18-Oct-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix typos

Fix typos in EFI subsystem comments.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# fae0118e 25-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: eliminate handle member

A pointer to a struct efi_object is a handle. We do not need any handle
member in this structure. Let's eliminate it.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 6f566c23 01-Oct-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: return type efi_console_register()

Use a return type that can encompass the return value.

This fixes CoverityScan CID 184090.

Reported-by: Tom Rini <trini@konsulko.com>
Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 6bb591f7 15-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: query serial console size reliably

Not all terminals understand CSI [18t for querying the console size.
We should adhere to escape sequences documented in the console_codes
manpage and the ECMA-48 standard.

So here we follow a different approach. We position the cursor to the
bottom right and query its position. Before leaving the function we
restore the original cursor position.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# aaace4b0 14-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: terminal left upper corner is [1, 1]

For the VT102 and the Linux console the left upper corner is [1, 1].
EFI uses [0, 0] as left upper corner.

Check parameters of the SetCursorPosition() service.

Use CSI H like EDK2 for positioning.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 4fdcf066 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: implement key notify functions

Implement registering and unregistreing key notify functions in the
EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 55fbdf99 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: support modifiers for F1 - F4

Support modifiers for F1 - F4.
Add support for letters with ALT key.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 110c6280 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL

This patch implements the EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.

The implementation of notification functions is postponed to a later
patch.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 0dfd13a4 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: rework event handling for console

Preread the next key in the console timer event.

The EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL requires to trigger registered key
notification functions based on the prefetched key.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 35cbb796 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: support Unicode text input

Up to now the EFI_TEXT_INPUT_PROTOCOL only supported ASCII characters.
With the patch it can consume UTF-8 from the console.

Currently only the serial console and the console can deliver UTF-8.
Local consoles are restricted to ASCII.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 14d103bb 08-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: typos in efi_console.c

Fix a few typos.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 3e603ec7 08-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: struct efi_simple_text_input_protocol

%s/efi_simple_input_interface/efi_simple_text_input_protocol/

We should be consistent in the naming of the EFI protocol interface
structures. The protocol is called EFI_SIMPLE_TEXT_INPUT_PROTOCOL.

%s/ExtendedVerification/extended_verification/

Use consistent naming of function parameters. Do not use CamelCase.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 40e3e757 04-Sep-2018 Alexander Graf <agraf@csgraf.de>

efi_loader: Add console handle references to in/out/err

We register the console in/out protocol on the loaded image handle, but
never tell anyone about it in the systab.

Because of this Shell.efi can not find the input protocol. So let's add
a proper reference.

Signed-off-by: Alexander Graf <agraf@suse.de>
Reviewed-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# ba7bd5c2 31-Aug-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: don't use unlimited stack as buffer

The length of a string printed to the console by the
EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL is not limited by the UEFI spec.
Hence should not allocate a buffer for it on the stack.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 1dde0d57 31-Aug-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: rename utf16_strlen, utf16_strnlen

The function names utf16_strlen() and utf16_strnlen() are misnomers.
The functions do not count utf-16 characters but non-zero words.
So let's rename them to u16_strlen and u16_strnlen().

In utf16_dup() avoid assignment in if clause.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 9d12daff 05-Jul-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.Reset()

Implement the reset service of the EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.

This should resolve the error reported by the SCT in
Protocol/SimpleTextOut/BlackBoxTest/SimpleTextOutBBTestFunction_uefi.c:639

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# e67ff94d 05-Jul-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: clear screen has to reset cursor position

After clearing the screen the cursor position is row 0, column 0.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 4f187897 05-Jul-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_INPUT_PROTOCOL.Reset()

Implement the reset service of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL.

This should resolve the error reported by the SCT in
Protocol/SimpleTextIn/BlackBoxTest/SimpleTextInBBTestFunction.c:193

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 80483b2a 03-Jun-2018 Alexander Graf <agraf@csgraf.de>

efi_loader: Fix warning in raw/cols query

The code to determine rows / cols on the screen could potentially run
into a case where it doesn't know how big the screen is. In that case,
assume 80x25.

Signed-off-by: Alexander Graf <agraf@suse.de>

# 62217295 16-May-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: avoid using unitialized values as console size

If a request for the console size would be answered with a response
with less then three values, uninitialized stack memory would be
copied to the number of rows and columns of the terminal.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# a4aa7bef 29-Apr-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: always check parameters in efi_cout_query_mode()

If we cannot determine the size of the serial terminal we still have
to check the parameters of efi_cout_query_mode().

Querying the size of the serial terminal drains the keyboard buffer.
So make sure we do this during the initialization and not in the midst
of an EFI application.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 7ca7c3c0 29-Apr-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: updating the cursor position

The current coding advances the column by the number of UTF-8 bytes.
The column should be increased by one per unicode character.

The UEFI spec provides advance rules for U+0000, U+0008, U+000A,
and U000D. All other characters, including control characters
U+0007 (bel) and U+0009 (tab), have to increase the column by one.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# f739fcd8 07-May-2018 Tom Rini <trini@konsulko.com>

SPDX: Convert a few files that were missed before

As part of the main conversion a few files were missed. These files had
additional whitespace after the '*' and before the SPDX tag and my
previous regex was too strict. This time I did a grep for all SPDX tags
and then filtered out anything that matched the correct styles.

Fixes: 83d290c56fab ("SPDX: Convert all of our single license tags to Linux Kernel style")
Reported-by: Heinrich Schuchardt <xypron.debian@gmx.de>
Signed-off-by: Tom Rini <trini@konsulko.com>

# b095f3c8 18-Feb-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: implement event groups

If an event of a group event is signaled all other events of the same
group are signaled too.

Function efi_signal_event is renamed to efi_queue_event.
A new function efi_signal_event is introduced that checks if an event
belongs to a group and than signals all events of the group.
Event group notifciation is implemented for ExitBootServices,
InstallConfigurationTable, and ResetSystem.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 0fb4169e 19-Feb-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: correct input of special keys

Don't set unicode_char if scan_code is set.
Add support for page up, page down, and insert.
Correct input of function keys.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 8396e3fd 03-Mar-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: remove deprecated ConsoleControlProtocol

The console control protocol is not defined in the UEFI standard.

It exists in EDK2's EdkCompatiblityPkg package. But this package
is deprecated according to
https://github.com/tianocore/tianocore.github.io/wiki/Differences-between-EDK-and-EDK-II

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 9bc9664d 19-Jan-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: add check_tpl parameter to efi_signal_event

In ExitBootServices we need to signal events irrespective of the current
TPL level. A new parameter check_tpl is added to efi_signal_event().

Function efi_console_timer_notify() gets some comments.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 2074f700 11-Jan-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: consistently use efi_handle_t for handles

We should consistently use the efi_handle_t typedef when
referring to handles.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# ebb4dd5b 26-Oct-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: efi_console: use helper functions

Use helper functions efi_created_handle and efi_add_protocol
for creating the console handles and instaling the respective
protocols.

This change is needed if we want to move from an array of
protocols to a linked list of protocols.

Eliminate EFI_PROTOCOL_OBJECT which is not used anymore.

Currently we have not defined protocol interfaces to be const.
So efi_con_out and efi_console_control cannot be defined as const.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 2d5dc2a5 10-Oct-2017 Rob Clark <robdclark@gmail.com>

efi_loader: console support for color attributes

Shell.efi uses this, and supporting color attributes makes things look
nicer. Map the EFI fg/bg color attributes to ANSI escape sequences.
Not all colors have a perfect match, but spec just says "Devices
supporting a different number of text colors are required to emulate the
above colors to the best of the device’s capabilities".

Signed-off-by: Rob Clark <robdclark@gmail.com>
Tested-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Alexander Graf <agraf@suse.de>
[agraf: s/unsigned/unsigned int/]
Signed-off-by: Alexander Graf <agraf@suse.de>

# e190e897 04-Oct-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: use type bool for event states

Queued and signaled describe boolean states of events.
So let's use type bool and rename the structure members to is_queued
and is_signaled.

Update the comments for is_queued and is_signaled.

Reported-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Rob Clark <robdclark@gmail.com>
Reviewed-by: Rob Clark <robdclark@gmail.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 3a45bc7f 13-Sep-2017 Rob Clark <robdclark@gmail.com>

efi_loader: Some console improvements for vidconsole

1) use fputs() to reduce cache flushes from once-per-char to
once-per-string
2) handle \r, \t, and \b in addition to just \n for tracking
cursor position
3) cursor row/col are zero based, not one based

Signed-off-by: Rob Clark <robdclark@gmail.com>
[agraf: s/unsigned/unsigned int/]
Signed-off-by: Alexander Graf <agraf@suse.de>

# a18c5a83 13-Sep-2017 Rob Clark <robdclark@gmail.com>

efi_loader: Correctly figure out size for vidconsole

If stdout is vidconsole, we cannot rely on ANSI escape sequences to
query the size, as vidconsole cannot reply on stdin. Instead special-
case this if stdout is vidconsole.

Signed-off-by: Rob Clark <robdclark@gmail.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 71cc25c3 13-Sep-2017 Rob Clark <robdclark@gmail.com>

efi_loader: split out escape sequence based size query

We need to do something different for vidconsole, since it cannot
respond to the query on stdin. Prep work for next patch.

Signed-off-by: Rob Clark <robdclark@gmail.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# ca62a4f5 15-Sep-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: implement queueing of the notification function

For the correct implementation of the task priority level (TPL)
calling the notification function must be queued.

Add a status field 'queued' to events.

In function efi_signal_event set status queued if a notification
function exists and reset it after we have called the function.
A later patch will add a check of the TPL here.

In efi_create_event and efi_close_event unset the queued status.

In function efi_wait_for_event and efi_check_event
queue the notification function.

In efi_timer_check call the efi_notify_event
if the status queued is set.
For all timer events set status signaled.

In efi_console_timer_notify set the signaled state of the
WaitForKey event.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 78178bb0 09-Sep-2017 Rob Clark <robdclark@gmail.com>

lib: add some utf16 handling helpers

We'll eventually want these in a few places in efi_loader, and also
vsprintf.

Signed-off-by: Rob Clark <robdclark@gmail.com>

# a17e62cc 24-Jul-2017 Rob Clark <robdclark@gmail.com>

efi_loader: expose protocols via GUID

shim.efi (or rather gnu-efi's LibLocateProtocol() which shim.efi uses)
resolves protocols via efi_locate_handle() so the console protocols
need to be added to the efi object list.

Signed-off-by: Rob Clark <robdclark@gmail.com>
[agraf: whitespace fixes]
Signed-off-by: Alexander Graf <agraf@suse.de>

# ff925938 19-Jul-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_console: use EFIAPI for callback functions

The call to efi_create_event requires notification functions
flagged as EFIAPI.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 91be9a77 18-Jul-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_console: set up events

Set up a timer event and the WaitForKey event.
In the notify function of the timer event check for console input
and signal the WaitForKey event accordingly.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 5be8b0a3 07-Nov-2016 Emmanuel Vadot <manu@bidouilliste.com>

efi_loader: console: Correctly report modes

Add support for EFI console modes.
Mode 0 is always 80x25 and present by EFI specification.
Mode 1 is always 80x50 and not mandatory.
Mode 2 and above is freely usable.

If the terminal can handle mode 1, we mark it as supported.
If the terminal size is greater than mode 0 and different than mode 1,
we install it as mode 2.

Modes can be switch with cout_set_mode.

Changes in V5:
Correctly detect mode before enabling mode 2.

Changes in V4:
Reset cursor positon on mode switch
Use local variables in console query code

Changes in V3:
Valid mode are 0 to EFIMode-1
Fix style

Changes in V2:
Add mode switch
Report only the modes that we support

Signed-off-by: Emmanuel Vadot <manu@bidouilliste.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# c1311ad4 03-Mar-2016 Alexander Graf <agraf@csgraf.de>

efi_loader: Add console interface

One of the basic EFI interfaces is the console interface. Using it an EFI
application can interface with the user. This patch implements an EFI console
interface using getc() and putc().

Today, we only implement text based consoles. We also convert the EFI Unicode
characters to UTF-8 on the fly, hoping that everyone managed to jump on the
train by now.

Signed-off-by: Alexander Graf <agraf@suse.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Tested-by: Simon Glass <sjg@chromium.org>

# e7175f93 10-Feb-2023 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: static functions in efi_console.c

Define function set_shift_mask() as static as it is not used externally.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>

# e585b79e 18-Jan-2023 Jan Kiszka <jan.kiszka@siemens.com>

efi_loader: Avoid overwriting previous outputs on console screen clearing

Before clearing the screen, ensure that no previous output of firmware
or UEFI programs will be overwritten on serial devices or other
streaming consoles. This helps generating complete boot logs.

Tested regarding multi-output against qemu-x86_defconfig. Still, there
were remaining concerns about side effects, so this is provided as an
opt-in feature.

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
Reviewed-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>

# de94f0f3 18-Jan-2023 Jan Kiszka <jan.kiszka@siemens.com>

efi_loader: Set default console colors on efi_cout_clear_screen if needed

Ensures a consistent background color of the whole screen for succeeding
outputs as both demanded by the spec and implemented in EDK2 as well.

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
Reviewed-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>

# b86986c7 18-Oct-2022 Simon Glass <sjg@chromium.org>

video: Rename CONFIG_DM_VIDEO to CONFIG_VIDEO

Now that all the old code is gone, rename this option. Driver model
migration is now complete.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 7831d36f 14-Oct-2022 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: avoid EFI_CALL() when draining console

Use internal function.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>

# f3272366 15-Oct-2022 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: avoid EFI_CALL() for clearing screen

Carve out function efi_clear_screen.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>

# 05c4c9e2 06-Oct-2022 Ilias Apalodimas <ilias.apalodimas@linaro.org>

efi_loader: define internal implementations of install/uninstallmultiple

A following patch is cleaning up the core EFI code trying to remove
sequences of efi_create_handle, efi_add_protocol.

Although this works fine there's a problem with the latter since it is
usually combined with efi_delete_handle() which blindly removes all
protocols on a handle and deletes the handle. We should try to adhere to
the EFI spec which only deletes a handle if the last instance of a protocol
has been removed. Another problem is that efi_delete_handle() never checks
for opened protocols, but the EFI spec defines that the caller is
responsible for ensuring that there are no references to a protocol
interface that is going to be removed.

So let's fix this by replacing all callsites of
efi_create_handle(), efi_add_protocol() , efi_delete_handle() with
Install/UninstallMultipleProtocol.

In order to do that redefine functions that can be used by the U-Boot
proper internally and add '_ext' variants that will be used from the
EFI API

Signed-off-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>
Reviewed-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# e9a1ff97 19-Sep-2022 Tom Rini <trini@konsulko.com>

Merge branch 'master' into next

Signed-off-by: Tom Rini <trini@konsulko.com>


# 87d79142 12-Sep-2022 Masahisa Kojima <masahisa.kojima@linaro.org>

eficonfig: menu-driven addition of UEFI boot option

This commit add the "eficonfig" command.
The "eficonfig" command implements the menu-driven UEFI boot option
maintenance feature. This commit implements the addition of
new boot option. User can select the block device volume having
efi_simple_file_system_protocol and select the file corresponding
to the Boot#### variable. User can also enter the description and
optional_data of the BOOT#### variable in utf8.

This commit adds "include/efi_config.h", it contains the common
definition to be used from other menus such as UEFI Secure Boot
key management.

Signed-off-by: Masahisa Kojima <masahisa.kojima@linaro.org>

# e053a144 01-Sep-2022 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: support CTRL+\ - CTRL+_

In the extended text input protocol support input of control letters
0x1c - 0x1f.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
Reviewed-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>

# 0b7b56d7 01-Sep-2022 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: compliance Simple Text Input Ex Protocol

We cannot expect the buffers passed to the input protocols to be zero
filled. If only modifier keys are pressed, we have to return EFI_NOT_READY
but we still have to fill the key structure.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
Reviewed-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>

# 68edbed4 14-Jun-2022 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: initialize console size late

If CONFIG_VIDEO_DM=n we query the display size from the serial console.
Especially when using a remote console the response can be so late that
it interferes with autoboot.

Only query the console size when running an EFI binary.

Add debug output showing the determined console size.

Reported-by: Fabio Estevam <festevam@gmail.com>
Fixes: a57ad20d07e8 ("efi_loader: split efi_init_obj_list() into two stages")
Fixes: a9bf024b2933 ("efi_loader: disk: a helper function to create efi_disk objects from udevice")
Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
Tested-by: Fabio Estevam <festevam@denx.de>
Tested-by: Heiko Thiery <heiko.thiery@gmail.com>

# 5c1037d5 30-Apr-2022 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: reset colors before clearing screen

When resetting the text console the colors have to be set before clearing
the screen. Otherwise the background color may be wrong.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>

# 3c95b323 04-Feb-2022 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: add handle for UART

When loading an EFI binary via the UART we assign a UART device path to it.
But we lack a handle with that device path.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>

# abd62e4d 29-Jun-2021 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: missing parentheses in query_console_size

After if we should use parentheses to keep the code readable.

Fixes: a95f4c885991 ("efi_loader: NULL dereference in EFI console")
Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# a95f4c88 15-Mar-2021 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: NULL dereference in EFI console

Even if CONFIG_DM_VIDEO=y and stdout="vidconsole", a video device may not
be available. Check the return values of the relevant functions.

If no video output device is available, assume that the serial console is
in use.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 997b57fc 28-Feb-2021 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: console size of vidconsole

If stdout is 'vidconsole', we correctly set the console size.
If stdout is 'vidconsole,serial', the video console is ignored.

We should always evaluate the size of vidconsole if it is the primary
console.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 7913c7dc 04-Jan-2021 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: typedef efi_string_t text output protocol

We do not want to use typedefs in U-Boot.

Do not use efi_string_t in the EFI_TEXT_OUTPUT_PROTOCOL.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 9abb01af 27-Dec-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: escape key handling

Up to now the escape key was not correctly detected in UEFI applications.
We had to hit it twice for a single escape to be recognized.

Use a 10 ms delay to detect if we are dealing with the escape key or an
escape sequence.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# c670aeee 07-Oct-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

common: rename getc() to getchar()

The sandbox is built with the SDL2 library with invokes the X11 library
which in turn calls getc(). But getc() in glibc is defined as

int getc(FILE *)

This does not match our definition.

int getc(void)

The sandbox crashes when called with parameter -l.

Rename our library symbol getc() to getchar().

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Tom Rini <trini@konsulko.com>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 325567d3 04-Jun-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: comments EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL

Provide missing comments for the functions implementing the
EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 336d4615 03-Feb-2020 Simon Glass <sjg@chromium.org>

dm: core: Create a new header file for 'compat' features

At present dm/device.h includes the linux-compatible features. This
requires including linux/compat.h which in turn includes a lot of headers.
One of these is malloc.h which we thus end up including in every file in
U-Boot. Apart from the inefficiency of this, it is problematic for sandbox
which needs to use the system malloc() in some files.

Move the compatibility features into a separate header file.

Signed-off-by: Simon Glass <sjg@chromium.org>

# b0ad9b5b 22-Dec-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: clear screen should move cursor to home

On a VT100 terminal <ESC>[2J should be enough to both clear the whole
screen and set the cursor to position (1, 1). But the Linux console does
not behave like this. So send an extra <ESC>[H. For reference see the
console_codes(4) man page.

Add a function description.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 1045315d 14-Nov-2019 Simon Glass <sjg@chromium.org>

common: Move get_ticks() function out of common.h

This function belongs in time.h so move it over and add a comment.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Tom Rini <trini@konsulko.com>

# fe1a81c1 05-Sep-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

doc: UEFI API documentation

Add some more files to the UEFI API documentation.

Correct some Sphinx comments.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 03446987 04-Sep-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: do not set invalid screen mode

EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.SetMode() should return EFI_UNDEFINED if a
screen mode is not available.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Alexander Graf <agraf@csgraf.de>

# 97ea0690 04-Sep-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: cursor positioning

When backspacing in column 0 do no set the column index to ULONG_MAX.
Ensure that the row number is not set to ULONG_MAX even if the row count is
advertised as 0.
Ignore control characters other the 0x08, 0x0a, 0x0d when updating the
column.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 7b51b576 01-Aug-2019 Simon Glass <sjg@chromium.org>

env: Move env_get() to env.h

Move env_get() over to the new header file.

Acked-by: Joe Hershberger <joe.hershberger@ni.com>
Signed-off-by: Simon Glass <sjg@chromium.org>

# 3b435c11 16-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: console incorrectly advertised left logo key

Avoid to signal that the left logo key is pressed, when it is not.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 3b2b2de8 16-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: alternative scan codes for F5, END, HOME

Depending on the key board alternative scan codes are used for F5, END, and
HOME.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 3950f0f8 13-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix SetAttribute()

The SetAttribute() service and the Reset() service of the simple text
output protocol must update the attribute value in the mode information.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 2ad238fc 13-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix SetMode()

Correct the check of the mode number in SetMode() service of the simple
text output protocol.

Clear the screen in SetMode().

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 7eaa900e 06-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: event signaling in ExitBootServices

ExitBootServices() has to stop timer related activity before calling the
events of the EFI_EVENT_GROUP_EXIT_BOOT_SERVICES event group. But our
current implementation was stopping all other events.

All events have to observe the task priority level.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 25e6fb5e 02-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix EnableCursor()

The EnableCursor() service of the simple text output protocol must update
the the CursorVisible field of the output mode.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# b31ca6bf 18-May-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: parameter check OutputString

Check the parameters against NULL.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# acee9652 18-May-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.SetState() correct parameter

KeyToggleState is a pointer according to UEFI spec 2.8.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# bfc2dd53 06-Apr-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: correct CTRL-A - CTRL-Z console input

In the extended text input protocol CTRL-A - CTRL-Z have to be signaled as
Unicode characters a-z or A-Z depending on the shift state and not as 0x01
to 0x1a.

Update Python unit test.

This patch is required for using the EFI shell `edit` command.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# dd1a1ec2 04-Mar-2019 Matthias Brugger <mbrugger@suse.com>

efi_loader: Fix serial console size detection

Function term_read_reply tries to read from the serial console until
the end_char was read. This can hang forever if we are, for some reason,
not able to read the full response (e.g. serial buffer too small,
frame error). This patch moves the timeout detection into
term_read_reply() to assure we will make progress.

Fixes: 6bb591f704 ("efi_loader: query serial console size reliably")
Signed-off-by: Matthias Brugger <mbrugger@suse.com>

Throw missing error when an incomplete reply for the cursor position is
received.

Change type of argument of term_get_char() *s32. This renders the function
reusable in efi_cin_read_key().

Reviewed-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# e1fec152 18-Oct-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix typos

Fix typos in EFI subsystem comments.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# fae0118e 25-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: eliminate handle member

A pointer to a struct efi_object is a handle. We do not need any handle
member in this structure. Let's eliminate it.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 6f566c23 01-Oct-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: return type efi_console_register()

Use a return type that can encompass the return value.

This fixes CoverityScan CID 184090.

Reported-by: Tom Rini <trini@konsulko.com>
Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 6bb591f7 15-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: query serial console size reliably

Not all terminals understand CSI [18t for querying the console size.
We should adhere to escape sequences documented in the console_codes
manpage and the ECMA-48 standard.

So here we follow a different approach. We position the cursor to the
bottom right and query its position. Before leaving the function we
restore the original cursor position.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# aaace4b0 14-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: terminal left upper corner is [1, 1]

For the VT102 and the Linux console the left upper corner is [1, 1].
EFI uses [0, 0] as left upper corner.

Check parameters of the SetCursorPosition() service.

Use CSI H like EDK2 for positioning.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 4fdcf066 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: implement key notify functions

Implement registering and unregistreing key notify functions in the
EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 55fbdf99 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: support modifiers for F1 - F4

Support modifiers for F1 - F4.
Add support for letters with ALT key.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 110c6280 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL

This patch implements the EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.

The implementation of notification functions is postponed to a later
patch.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 0dfd13a4 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: rework event handling for console

Preread the next key in the console timer event.

The EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL requires to trigger registered key
notification functions based on the prefetched key.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 35cbb796 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: support Unicode text input

Up to now the EFI_TEXT_INPUT_PROTOCOL only supported ASCII characters.
With the patch it can consume UTF-8 from the console.

Currently only the serial console and the console can deliver UTF-8.
Local consoles are restricted to ASCII.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 14d103bb 08-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: typos in efi_console.c

Fix a few typos.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 3e603ec7 08-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: struct efi_simple_text_input_protocol

%s/efi_simple_input_interface/efi_simple_text_input_protocol/

We should be consistent in the naming of the EFI protocol interface
structures. The protocol is called EFI_SIMPLE_TEXT_INPUT_PROTOCOL.

%s/ExtendedVerification/extended_verification/

Use consistent naming of function parameters. Do not use CamelCase.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 40e3e757 04-Sep-2018 Alexander Graf <agraf@csgraf.de>

efi_loader: Add console handle references to in/out/err

We register the console in/out protocol on the loaded image handle, but
never tell anyone about it in the systab.

Because of this Shell.efi can not find the input protocol. So let's add
a proper reference.

Signed-off-by: Alexander Graf <agraf@suse.de>
Reviewed-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# ba7bd5c2 31-Aug-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: don't use unlimited stack as buffer

The length of a string printed to the console by the
EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL is not limited by the UEFI spec.
Hence should not allocate a buffer for it on the stack.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 1dde0d57 31-Aug-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: rename utf16_strlen, utf16_strnlen

The function names utf16_strlen() and utf16_strnlen() are misnomers.
The functions do not count utf-16 characters but non-zero words.
So let's rename them to u16_strlen and u16_strnlen().

In utf16_dup() avoid assignment in if clause.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 9d12daff 05-Jul-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.Reset()

Implement the reset service of the EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.

This should resolve the error reported by the SCT in
Protocol/SimpleTextOut/BlackBoxTest/SimpleTextOutBBTestFunction_uefi.c:639

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# e67ff94d 05-Jul-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: clear screen has to reset cursor position

After clearing the screen the cursor position is row 0, column 0.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 4f187897 05-Jul-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_INPUT_PROTOCOL.Reset()

Implement the reset service of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL.

This should resolve the error reported by the SCT in
Protocol/SimpleTextIn/BlackBoxTest/SimpleTextInBBTestFunction.c:193

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 80483b2a 03-Jun-2018 Alexander Graf <agraf@csgraf.de>

efi_loader: Fix warning in raw/cols query

The code to determine rows / cols on the screen could potentially run
into a case where it doesn't know how big the screen is. In that case,
assume 80x25.

Signed-off-by: Alexander Graf <agraf@suse.de>

# 62217295 16-May-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: avoid using unitialized values as console size

If a request for the console size would be answered with a response
with less then three values, uninitialized stack memory would be
copied to the number of rows and columns of the terminal.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# a4aa7bef 29-Apr-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: always check parameters in efi_cout_query_mode()

If we cannot determine the size of the serial terminal we still have
to check the parameters of efi_cout_query_mode().

Querying the size of the serial terminal drains the keyboard buffer.
So make sure we do this during the initialization and not in the midst
of an EFI application.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 7ca7c3c0 29-Apr-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: updating the cursor position

The current coding advances the column by the number of UTF-8 bytes.
The column should be increased by one per unicode character.

The UEFI spec provides advance rules for U+0000, U+0008, U+000A,
and U000D. All other characters, including control characters
U+0007 (bel) and U+0009 (tab), have to increase the column by one.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# f739fcd8 07-May-2018 Tom Rini <trini@konsulko.com>

SPDX: Convert a few files that were missed before

As part of the main conversion a few files were missed. These files had
additional whitespace after the '*' and before the SPDX tag and my
previous regex was too strict. This time I did a grep for all SPDX tags
and then filtered out anything that matched the correct styles.

Fixes: 83d290c56fab ("SPDX: Convert all of our single license tags to Linux Kernel style")
Reported-by: Heinrich Schuchardt <xypron.debian@gmx.de>
Signed-off-by: Tom Rini <trini@konsulko.com>

# b095f3c8 18-Feb-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: implement event groups

If an event of a group event is signaled all other events of the same
group are signaled too.

Function efi_signal_event is renamed to efi_queue_event.
A new function efi_signal_event is introduced that checks if an event
belongs to a group and than signals all events of the group.
Event group notifciation is implemented for ExitBootServices,
InstallConfigurationTable, and ResetSystem.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 0fb4169e 19-Feb-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: correct input of special keys

Don't set unicode_char if scan_code is set.
Add support for page up, page down, and insert.
Correct input of function keys.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 8396e3fd 03-Mar-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: remove deprecated ConsoleControlProtocol

The console control protocol is not defined in the UEFI standard.

It exists in EDK2's EdkCompatiblityPkg package. But this package
is deprecated according to
https://github.com/tianocore/tianocore.github.io/wiki/Differences-between-EDK-and-EDK-II

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 9bc9664d 19-Jan-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: add check_tpl parameter to efi_signal_event

In ExitBootServices we need to signal events irrespective of the current
TPL level. A new parameter check_tpl is added to efi_signal_event().

Function efi_console_timer_notify() gets some comments.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 2074f700 11-Jan-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: consistently use efi_handle_t for handles

We should consistently use the efi_handle_t typedef when
referring to handles.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# ebb4dd5b 26-Oct-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: efi_console: use helper functions

Use helper functions efi_created_handle and efi_add_protocol
for creating the console handles and instaling the respective
protocols.

This change is needed if we want to move from an array of
protocols to a linked list of protocols.

Eliminate EFI_PROTOCOL_OBJECT which is not used anymore.

Currently we have not defined protocol interfaces to be const.
So efi_con_out and efi_console_control cannot be defined as const.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 2d5dc2a5 10-Oct-2017 Rob Clark <robdclark@gmail.com>

efi_loader: console support for color attributes

Shell.efi uses this, and supporting color attributes makes things look
nicer. Map the EFI fg/bg color attributes to ANSI escape sequences.
Not all colors have a perfect match, but spec just says "Devices
supporting a different number of text colors are required to emulate the
above colors to the best of the device’s capabilities".

Signed-off-by: Rob Clark <robdclark@gmail.com>
Tested-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Alexander Graf <agraf@suse.de>
[agraf: s/unsigned/unsigned int/]
Signed-off-by: Alexander Graf <agraf@suse.de>

# e190e897 04-Oct-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: use type bool for event states

Queued and signaled describe boolean states of events.
So let's use type bool and rename the structure members to is_queued
and is_signaled.

Update the comments for is_queued and is_signaled.

Reported-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Rob Clark <robdclark@gmail.com>
Reviewed-by: Rob Clark <robdclark@gmail.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 3a45bc7f 13-Sep-2017 Rob Clark <robdclark@gmail.com>

efi_loader: Some console improvements for vidconsole

1) use fputs() to reduce cache flushes from once-per-char to
once-per-string
2) handle \r, \t, and \b in addition to just \n for tracking
cursor position
3) cursor row/col are zero based, not one based

Signed-off-by: Rob Clark <robdclark@gmail.com>
[agraf: s/unsigned/unsigned int/]
Signed-off-by: Alexander Graf <agraf@suse.de>

# a18c5a83 13-Sep-2017 Rob Clark <robdclark@gmail.com>

efi_loader: Correctly figure out size for vidconsole

If stdout is vidconsole, we cannot rely on ANSI escape sequences to
query the size, as vidconsole cannot reply on stdin. Instead special-
case this if stdout is vidconsole.

Signed-off-by: Rob Clark <robdclark@gmail.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 71cc25c3 13-Sep-2017 Rob Clark <robdclark@gmail.com>

efi_loader: split out escape sequence based size query

We need to do something different for vidconsole, since it cannot
respond to the query on stdin. Prep work for next patch.

Signed-off-by: Rob Clark <robdclark@gmail.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# ca62a4f5 15-Sep-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: implement queueing of the notification function

For the correct implementation of the task priority level (TPL)
calling the notification function must be queued.

Add a status field 'queued' to events.

In function efi_signal_event set status queued if a notification
function exists and reset it after we have called the function.
A later patch will add a check of the TPL here.

In efi_create_event and efi_close_event unset the queued status.

In function efi_wait_for_event and efi_check_event
queue the notification function.

In efi_timer_check call the efi_notify_event
if the status queued is set.
For all timer events set status signaled.

In efi_console_timer_notify set the signaled state of the
WaitForKey event.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 78178bb0 09-Sep-2017 Rob Clark <robdclark@gmail.com>

lib: add some utf16 handling helpers

We'll eventually want these in a few places in efi_loader, and also
vsprintf.

Signed-off-by: Rob Clark <robdclark@gmail.com>

# a17e62cc 24-Jul-2017 Rob Clark <robdclark@gmail.com>

efi_loader: expose protocols via GUID

shim.efi (or rather gnu-efi's LibLocateProtocol() which shim.efi uses)
resolves protocols via efi_locate_handle() so the console protocols
need to be added to the efi object list.

Signed-off-by: Rob Clark <robdclark@gmail.com>
[agraf: whitespace fixes]
Signed-off-by: Alexander Graf <agraf@suse.de>

# ff925938 19-Jul-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_console: use EFIAPI for callback functions

The call to efi_create_event requires notification functions
flagged as EFIAPI.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 91be9a77 18-Jul-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_console: set up events

Set up a timer event and the WaitForKey event.
In the notify function of the timer event check for console input
and signal the WaitForKey event accordingly.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 5be8b0a3 07-Nov-2016 Emmanuel Vadot <manu@bidouilliste.com>

efi_loader: console: Correctly report modes

Add support for EFI console modes.
Mode 0 is always 80x25 and present by EFI specification.
Mode 1 is always 80x50 and not mandatory.
Mode 2 and above is freely usable.

If the terminal can handle mode 1, we mark it as supported.
If the terminal size is greater than mode 0 and different than mode 1,
we install it as mode 2.

Modes can be switch with cout_set_mode.

Changes in V5:
Correctly detect mode before enabling mode 2.

Changes in V4:
Reset cursor positon on mode switch
Use local variables in console query code

Changes in V3:
Valid mode are 0 to EFIMode-1
Fix style

Changes in V2:
Add mode switch
Report only the modes that we support

Signed-off-by: Emmanuel Vadot <manu@bidouilliste.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# c1311ad4 03-Mar-2016 Alexander Graf <agraf@csgraf.de>

efi_loader: Add console interface

One of the basic EFI interfaces is the console interface. Using it an EFI
application can interface with the user. This patch implements an EFI console
interface using getc() and putc().

Today, we only implement text based consoles. We also convert the EFI Unicode
characters to UTF-8 on the fly, hoping that everyone managed to jump on the
train by now.

Signed-off-by: Alexander Graf <agraf@suse.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Tested-by: Simon Glass <sjg@chromium.org>

# e585b79e 18-Jan-2023 Jan Kiszka <jan.kiszka@siemens.com>

efi_loader: Avoid overwriting previous outputs on console screen clearing

Before clearing the screen, ensure that no previous output of firmware
or UEFI programs will be overwritten on serial devices or other
streaming consoles. This helps generating complete boot logs.

Tested regarding multi-output against qemu-x86_defconfig. Still, there
were remaining concerns about side effects, so this is provided as an
opt-in feature.

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
Reviewed-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>

# de94f0f3 18-Jan-2023 Jan Kiszka <jan.kiszka@siemens.com>

efi_loader: Set default console colors on efi_cout_clear_screen if needed

Ensures a consistent background color of the whole screen for succeeding
outputs as both demanded by the spec and implemented in EDK2 as well.

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
Reviewed-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>

# b86986c7 18-Oct-2022 Simon Glass <sjg@chromium.org>

video: Rename CONFIG_DM_VIDEO to CONFIG_VIDEO

Now that all the old code is gone, rename this option. Driver model
migration is now complete.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 7831d36f 14-Oct-2022 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: avoid EFI_CALL() when draining console

Use internal function.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>

# f3272366 15-Oct-2022 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: avoid EFI_CALL() for clearing screen

Carve out function efi_clear_screen.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>

# 05c4c9e2 06-Oct-2022 Ilias Apalodimas <ilias.apalodimas@linaro.org>

efi_loader: define internal implementations of install/uninstallmultiple

A following patch is cleaning up the core EFI code trying to remove
sequences of efi_create_handle, efi_add_protocol.

Although this works fine there's a problem with the latter since it is
usually combined with efi_delete_handle() which blindly removes all
protocols on a handle and deletes the handle. We should try to adhere to
the EFI spec which only deletes a handle if the last instance of a protocol
has been removed. Another problem is that efi_delete_handle() never checks
for opened protocols, but the EFI spec defines that the caller is
responsible for ensuring that there are no references to a protocol
interface that is going to be removed.

So let's fix this by replacing all callsites of
efi_create_handle(), efi_add_protocol() , efi_delete_handle() with
Install/UninstallMultipleProtocol.

In order to do that redefine functions that can be used by the U-Boot
proper internally and add '_ext' variants that will be used from the
EFI API

Signed-off-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>
Reviewed-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# e9a1ff97 19-Sep-2022 Tom Rini <trini@konsulko.com>

Merge branch 'master' into next

Signed-off-by: Tom Rini <trini@konsulko.com>


# 87d79142 12-Sep-2022 Masahisa Kojima <masahisa.kojima@linaro.org>

eficonfig: menu-driven addition of UEFI boot option

This commit add the "eficonfig" command.
The "eficonfig" command implements the menu-driven UEFI boot option
maintenance feature. This commit implements the addition of
new boot option. User can select the block device volume having
efi_simple_file_system_protocol and select the file corresponding
to the Boot#### variable. User can also enter the description and
optional_data of the BOOT#### variable in utf8.

This commit adds "include/efi_config.h", it contains the common
definition to be used from other menus such as UEFI Secure Boot
key management.

Signed-off-by: Masahisa Kojima <masahisa.kojima@linaro.org>

# e053a144 01-Sep-2022 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: support CTRL+\ - CTRL+_

In the extended text input protocol support input of control letters
0x1c - 0x1f.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
Reviewed-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>

# 0b7b56d7 01-Sep-2022 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: compliance Simple Text Input Ex Protocol

We cannot expect the buffers passed to the input protocols to be zero
filled. If only modifier keys are pressed, we have to return EFI_NOT_READY
but we still have to fill the key structure.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
Reviewed-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>

# 68edbed4 14-Jun-2022 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: initialize console size late

If CONFIG_VIDEO_DM=n we query the display size from the serial console.
Especially when using a remote console the response can be so late that
it interferes with autoboot.

Only query the console size when running an EFI binary.

Add debug output showing the determined console size.

Reported-by: Fabio Estevam <festevam@gmail.com>
Fixes: a57ad20d07e8 ("efi_loader: split efi_init_obj_list() into two stages")
Fixes: a9bf024b2933 ("efi_loader: disk: a helper function to create efi_disk objects from udevice")
Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
Tested-by: Fabio Estevam <festevam@denx.de>
Tested-by: Heiko Thiery <heiko.thiery@gmail.com>

# 5c1037d5 30-Apr-2022 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: reset colors before clearing screen

When resetting the text console the colors have to be set before clearing
the screen. Otherwise the background color may be wrong.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>

# 3c95b323 04-Feb-2022 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: add handle for UART

When loading an EFI binary via the UART we assign a UART device path to it.
But we lack a handle with that device path.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>

# abd62e4d 29-Jun-2021 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: missing parentheses in query_console_size

After if we should use parentheses to keep the code readable.

Fixes: a95f4c885991 ("efi_loader: NULL dereference in EFI console")
Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# a95f4c88 15-Mar-2021 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: NULL dereference in EFI console

Even if CONFIG_DM_VIDEO=y and stdout="vidconsole", a video device may not
be available. Check the return values of the relevant functions.

If no video output device is available, assume that the serial console is
in use.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 997b57fc 28-Feb-2021 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: console size of vidconsole

If stdout is 'vidconsole', we correctly set the console size.
If stdout is 'vidconsole,serial', the video console is ignored.

We should always evaluate the size of vidconsole if it is the primary
console.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 7913c7dc 04-Jan-2021 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: typedef efi_string_t text output protocol

We do not want to use typedefs in U-Boot.

Do not use efi_string_t in the EFI_TEXT_OUTPUT_PROTOCOL.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 9abb01af 27-Dec-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: escape key handling

Up to now the escape key was not correctly detected in UEFI applications.
We had to hit it twice for a single escape to be recognized.

Use a 10 ms delay to detect if we are dealing with the escape key or an
escape sequence.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# c670aeee 07-Oct-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

common: rename getc() to getchar()

The sandbox is built with the SDL2 library with invokes the X11 library
which in turn calls getc(). But getc() in glibc is defined as

int getc(FILE *)

This does not match our definition.

int getc(void)

The sandbox crashes when called with parameter -l.

Rename our library symbol getc() to getchar().

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Tom Rini <trini@konsulko.com>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 325567d3 04-Jun-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: comments EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL

Provide missing comments for the functions implementing the
EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 336d4615 03-Feb-2020 Simon Glass <sjg@chromium.org>

dm: core: Create a new header file for 'compat' features

At present dm/device.h includes the linux-compatible features. This
requires including linux/compat.h which in turn includes a lot of headers.
One of these is malloc.h which we thus end up including in every file in
U-Boot. Apart from the inefficiency of this, it is problematic for sandbox
which needs to use the system malloc() in some files.

Move the compatibility features into a separate header file.

Signed-off-by: Simon Glass <sjg@chromium.org>

# b0ad9b5b 22-Dec-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: clear screen should move cursor to home

On a VT100 terminal <ESC>[2J should be enough to both clear the whole
screen and set the cursor to position (1, 1). But the Linux console does
not behave like this. So send an extra <ESC>[H. For reference see the
console_codes(4) man page.

Add a function description.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 1045315d 14-Nov-2019 Simon Glass <sjg@chromium.org>

common: Move get_ticks() function out of common.h

This function belongs in time.h so move it over and add a comment.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Tom Rini <trini@konsulko.com>

# fe1a81c1 05-Sep-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

doc: UEFI API documentation

Add some more files to the UEFI API documentation.

Correct some Sphinx comments.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 03446987 04-Sep-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: do not set invalid screen mode

EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.SetMode() should return EFI_UNDEFINED if a
screen mode is not available.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Alexander Graf <agraf@csgraf.de>

# 97ea0690 04-Sep-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: cursor positioning

When backspacing in column 0 do no set the column index to ULONG_MAX.
Ensure that the row number is not set to ULONG_MAX even if the row count is
advertised as 0.
Ignore control characters other the 0x08, 0x0a, 0x0d when updating the
column.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 7b51b576 01-Aug-2019 Simon Glass <sjg@chromium.org>

env: Move env_get() to env.h

Move env_get() over to the new header file.

Acked-by: Joe Hershberger <joe.hershberger@ni.com>
Signed-off-by: Simon Glass <sjg@chromium.org>

# 3b435c11 16-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: console incorrectly advertised left logo key

Avoid to signal that the left logo key is pressed, when it is not.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 3b2b2de8 16-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: alternative scan codes for F5, END, HOME

Depending on the key board alternative scan codes are used for F5, END, and
HOME.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 3950f0f8 13-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix SetAttribute()

The SetAttribute() service and the Reset() service of the simple text
output protocol must update the attribute value in the mode information.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 2ad238fc 13-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix SetMode()

Correct the check of the mode number in SetMode() service of the simple
text output protocol.

Clear the screen in SetMode().

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 7eaa900e 06-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: event signaling in ExitBootServices

ExitBootServices() has to stop timer related activity before calling the
events of the EFI_EVENT_GROUP_EXIT_BOOT_SERVICES event group. But our
current implementation was stopping all other events.

All events have to observe the task priority level.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 25e6fb5e 02-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix EnableCursor()

The EnableCursor() service of the simple text output protocol must update
the the CursorVisible field of the output mode.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# b31ca6bf 18-May-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: parameter check OutputString

Check the parameters against NULL.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# acee9652 18-May-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.SetState() correct parameter

KeyToggleState is a pointer according to UEFI spec 2.8.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# bfc2dd53 06-Apr-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: correct CTRL-A - CTRL-Z console input

In the extended text input protocol CTRL-A - CTRL-Z have to be signaled as
Unicode characters a-z or A-Z depending on the shift state and not as 0x01
to 0x1a.

Update Python unit test.

This patch is required for using the EFI shell `edit` command.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# dd1a1ec2 04-Mar-2019 Matthias Brugger <mbrugger@suse.com>

efi_loader: Fix serial console size detection

Function term_read_reply tries to read from the serial console until
the end_char was read. This can hang forever if we are, for some reason,
not able to read the full response (e.g. serial buffer too small,
frame error). This patch moves the timeout detection into
term_read_reply() to assure we will make progress.

Fixes: 6bb591f704 ("efi_loader: query serial console size reliably")
Signed-off-by: Matthias Brugger <mbrugger@suse.com>

Throw missing error when an incomplete reply for the cursor position is
received.

Change type of argument of term_get_char() *s32. This renders the function
reusable in efi_cin_read_key().

Reviewed-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# e1fec152 18-Oct-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix typos

Fix typos in EFI subsystem comments.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# fae0118e 25-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: eliminate handle member

A pointer to a struct efi_object is a handle. We do not need any handle
member in this structure. Let's eliminate it.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 6f566c23 01-Oct-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: return type efi_console_register()

Use a return type that can encompass the return value.

This fixes CoverityScan CID 184090.

Reported-by: Tom Rini <trini@konsulko.com>
Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 6bb591f7 15-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: query serial console size reliably

Not all terminals understand CSI [18t for querying the console size.
We should adhere to escape sequences documented in the console_codes
manpage and the ECMA-48 standard.

So here we follow a different approach. We position the cursor to the
bottom right and query its position. Before leaving the function we
restore the original cursor position.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# aaace4b0 14-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: terminal left upper corner is [1, 1]

For the VT102 and the Linux console the left upper corner is [1, 1].
EFI uses [0, 0] as left upper corner.

Check parameters of the SetCursorPosition() service.

Use CSI H like EDK2 for positioning.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 4fdcf066 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: implement key notify functions

Implement registering and unregistreing key notify functions in the
EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 55fbdf99 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: support modifiers for F1 - F4

Support modifiers for F1 - F4.
Add support for letters with ALT key.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 110c6280 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL

This patch implements the EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.

The implementation of notification functions is postponed to a later
patch.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 0dfd13a4 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: rework event handling for console

Preread the next key in the console timer event.

The EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL requires to trigger registered key
notification functions based on the prefetched key.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 35cbb796 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: support Unicode text input

Up to now the EFI_TEXT_INPUT_PROTOCOL only supported ASCII characters.
With the patch it can consume UTF-8 from the console.

Currently only the serial console and the console can deliver UTF-8.
Local consoles are restricted to ASCII.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 14d103bb 08-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: typos in efi_console.c

Fix a few typos.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 3e603ec7 08-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: struct efi_simple_text_input_protocol

%s/efi_simple_input_interface/efi_simple_text_input_protocol/

We should be consistent in the naming of the EFI protocol interface
structures. The protocol is called EFI_SIMPLE_TEXT_INPUT_PROTOCOL.

%s/ExtendedVerification/extended_verification/

Use consistent naming of function parameters. Do not use CamelCase.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 40e3e757 04-Sep-2018 Alexander Graf <agraf@csgraf.de>

efi_loader: Add console handle references to in/out/err

We register the console in/out protocol on the loaded image handle, but
never tell anyone about it in the systab.

Because of this Shell.efi can not find the input protocol. So let's add
a proper reference.

Signed-off-by: Alexander Graf <agraf@suse.de>
Reviewed-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# ba7bd5c2 31-Aug-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: don't use unlimited stack as buffer

The length of a string printed to the console by the
EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL is not limited by the UEFI spec.
Hence should not allocate a buffer for it on the stack.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 1dde0d57 31-Aug-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: rename utf16_strlen, utf16_strnlen

The function names utf16_strlen() and utf16_strnlen() are misnomers.
The functions do not count utf-16 characters but non-zero words.
So let's rename them to u16_strlen and u16_strnlen().

In utf16_dup() avoid assignment in if clause.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 9d12daff 05-Jul-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.Reset()

Implement the reset service of the EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.

This should resolve the error reported by the SCT in
Protocol/SimpleTextOut/BlackBoxTest/SimpleTextOutBBTestFunction_uefi.c:639

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# e67ff94d 05-Jul-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: clear screen has to reset cursor position

After clearing the screen the cursor position is row 0, column 0.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 4f187897 05-Jul-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_INPUT_PROTOCOL.Reset()

Implement the reset service of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL.

This should resolve the error reported by the SCT in
Protocol/SimpleTextIn/BlackBoxTest/SimpleTextInBBTestFunction.c:193

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 80483b2a 03-Jun-2018 Alexander Graf <agraf@csgraf.de>

efi_loader: Fix warning in raw/cols query

The code to determine rows / cols on the screen could potentially run
into a case where it doesn't know how big the screen is. In that case,
assume 80x25.

Signed-off-by: Alexander Graf <agraf@suse.de>

# 62217295 16-May-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: avoid using unitialized values as console size

If a request for the console size would be answered with a response
with less then three values, uninitialized stack memory would be
copied to the number of rows and columns of the terminal.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# a4aa7bef 29-Apr-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: always check parameters in efi_cout_query_mode()

If we cannot determine the size of the serial terminal we still have
to check the parameters of efi_cout_query_mode().

Querying the size of the serial terminal drains the keyboard buffer.
So make sure we do this during the initialization and not in the midst
of an EFI application.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 7ca7c3c0 29-Apr-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: updating the cursor position

The current coding advances the column by the number of UTF-8 bytes.
The column should be increased by one per unicode character.

The UEFI spec provides advance rules for U+0000, U+0008, U+000A,
and U000D. All other characters, including control characters
U+0007 (bel) and U+0009 (tab), have to increase the column by one.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# f739fcd8 07-May-2018 Tom Rini <trini@konsulko.com>

SPDX: Convert a few files that were missed before

As part of the main conversion a few files were missed. These files had
additional whitespace after the '*' and before the SPDX tag and my
previous regex was too strict. This time I did a grep for all SPDX tags
and then filtered out anything that matched the correct styles.

Fixes: 83d290c56fab ("SPDX: Convert all of our single license tags to Linux Kernel style")
Reported-by: Heinrich Schuchardt <xypron.debian@gmx.de>
Signed-off-by: Tom Rini <trini@konsulko.com>

# b095f3c8 18-Feb-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: implement event groups

If an event of a group event is signaled all other events of the same
group are signaled too.

Function efi_signal_event is renamed to efi_queue_event.
A new function efi_signal_event is introduced that checks if an event
belongs to a group and than signals all events of the group.
Event group notifciation is implemented for ExitBootServices,
InstallConfigurationTable, and ResetSystem.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 0fb4169e 19-Feb-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: correct input of special keys

Don't set unicode_char if scan_code is set.
Add support for page up, page down, and insert.
Correct input of function keys.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 8396e3fd 03-Mar-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: remove deprecated ConsoleControlProtocol

The console control protocol is not defined in the UEFI standard.

It exists in EDK2's EdkCompatiblityPkg package. But this package
is deprecated according to
https://github.com/tianocore/tianocore.github.io/wiki/Differences-between-EDK-and-EDK-II

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 9bc9664d 19-Jan-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: add check_tpl parameter to efi_signal_event

In ExitBootServices we need to signal events irrespective of the current
TPL level. A new parameter check_tpl is added to efi_signal_event().

Function efi_console_timer_notify() gets some comments.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 2074f700 11-Jan-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: consistently use efi_handle_t for handles

We should consistently use the efi_handle_t typedef when
referring to handles.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# ebb4dd5b 26-Oct-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: efi_console: use helper functions

Use helper functions efi_created_handle and efi_add_protocol
for creating the console handles and instaling the respective
protocols.

This change is needed if we want to move from an array of
protocols to a linked list of protocols.

Eliminate EFI_PROTOCOL_OBJECT which is not used anymore.

Currently we have not defined protocol interfaces to be const.
So efi_con_out and efi_console_control cannot be defined as const.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 2d5dc2a5 10-Oct-2017 Rob Clark <robdclark@gmail.com>

efi_loader: console support for color attributes

Shell.efi uses this, and supporting color attributes makes things look
nicer. Map the EFI fg/bg color attributes to ANSI escape sequences.
Not all colors have a perfect match, but spec just says "Devices
supporting a different number of text colors are required to emulate the
above colors to the best of the device’s capabilities".

Signed-off-by: Rob Clark <robdclark@gmail.com>
Tested-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Alexander Graf <agraf@suse.de>
[agraf: s/unsigned/unsigned int/]
Signed-off-by: Alexander Graf <agraf@suse.de>

# e190e897 04-Oct-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: use type bool for event states

Queued and signaled describe boolean states of events.
So let's use type bool and rename the structure members to is_queued
and is_signaled.

Update the comments for is_queued and is_signaled.

Reported-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Rob Clark <robdclark@gmail.com>
Reviewed-by: Rob Clark <robdclark@gmail.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 3a45bc7f 13-Sep-2017 Rob Clark <robdclark@gmail.com>

efi_loader: Some console improvements for vidconsole

1) use fputs() to reduce cache flushes from once-per-char to
once-per-string
2) handle \r, \t, and \b in addition to just \n for tracking
cursor position
3) cursor row/col are zero based, not one based

Signed-off-by: Rob Clark <robdclark@gmail.com>
[agraf: s/unsigned/unsigned int/]
Signed-off-by: Alexander Graf <agraf@suse.de>

# a18c5a83 13-Sep-2017 Rob Clark <robdclark@gmail.com>

efi_loader: Correctly figure out size for vidconsole

If stdout is vidconsole, we cannot rely on ANSI escape sequences to
query the size, as vidconsole cannot reply on stdin. Instead special-
case this if stdout is vidconsole.

Signed-off-by: Rob Clark <robdclark@gmail.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 71cc25c3 13-Sep-2017 Rob Clark <robdclark@gmail.com>

efi_loader: split out escape sequence based size query

We need to do something different for vidconsole, since it cannot
respond to the query on stdin. Prep work for next patch.

Signed-off-by: Rob Clark <robdclark@gmail.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# ca62a4f5 15-Sep-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: implement queueing of the notification function

For the correct implementation of the task priority level (TPL)
calling the notification function must be queued.

Add a status field 'queued' to events.

In function efi_signal_event set status queued if a notification
function exists and reset it after we have called the function.
A later patch will add a check of the TPL here.

In efi_create_event and efi_close_event unset the queued status.

In function efi_wait_for_event and efi_check_event
queue the notification function.

In efi_timer_check call the efi_notify_event
if the status queued is set.
For all timer events set status signaled.

In efi_console_timer_notify set the signaled state of the
WaitForKey event.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 78178bb0 09-Sep-2017 Rob Clark <robdclark@gmail.com>

lib: add some utf16 handling helpers

We'll eventually want these in a few places in efi_loader, and also
vsprintf.

Signed-off-by: Rob Clark <robdclark@gmail.com>

# a17e62cc 24-Jul-2017 Rob Clark <robdclark@gmail.com>

efi_loader: expose protocols via GUID

shim.efi (or rather gnu-efi's LibLocateProtocol() which shim.efi uses)
resolves protocols via efi_locate_handle() so the console protocols
need to be added to the efi object list.

Signed-off-by: Rob Clark <robdclark@gmail.com>
[agraf: whitespace fixes]
Signed-off-by: Alexander Graf <agraf@suse.de>

# ff925938 19-Jul-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_console: use EFIAPI for callback functions

The call to efi_create_event requires notification functions
flagged as EFIAPI.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 91be9a77 18-Jul-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_console: set up events

Set up a timer event and the WaitForKey event.
In the notify function of the timer event check for console input
and signal the WaitForKey event accordingly.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 5be8b0a3 07-Nov-2016 Emmanuel Vadot <manu@bidouilliste.com>

efi_loader: console: Correctly report modes

Add support for EFI console modes.
Mode 0 is always 80x25 and present by EFI specification.
Mode 1 is always 80x50 and not mandatory.
Mode 2 and above is freely usable.

If the terminal can handle mode 1, we mark it as supported.
If the terminal size is greater than mode 0 and different than mode 1,
we install it as mode 2.

Modes can be switch with cout_set_mode.

Changes in V5:
Correctly detect mode before enabling mode 2.

Changes in V4:
Reset cursor positon on mode switch
Use local variables in console query code

Changes in V3:
Valid mode are 0 to EFIMode-1
Fix style

Changes in V2:
Add mode switch
Report only the modes that we support

Signed-off-by: Emmanuel Vadot <manu@bidouilliste.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# c1311ad4 03-Mar-2016 Alexander Graf <agraf@csgraf.de>

efi_loader: Add console interface

One of the basic EFI interfaces is the console interface. Using it an EFI
application can interface with the user. This patch implements an EFI console
interface using getc() and putc().

Today, we only implement text based consoles. We also convert the EFI Unicode
characters to UTF-8 on the fly, hoping that everyone managed to jump on the
train by now.

Signed-off-by: Alexander Graf <agraf@suse.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Tested-by: Simon Glass <sjg@chromium.org>

# b86986c7 18-Oct-2022 Simon Glass <sjg@chromium.org>

video: Rename CONFIG_DM_VIDEO to CONFIG_VIDEO

Now that all the old code is gone, rename this option. Driver model
migration is now complete.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 7831d36f 14-Oct-2022 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: avoid EFI_CALL() when draining console

Use internal function.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>

# f3272366 15-Oct-2022 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: avoid EFI_CALL() for clearing screen

Carve out function efi_clear_screen.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>

# 05c4c9e2 06-Oct-2022 Ilias Apalodimas <ilias.apalodimas@linaro.org>

efi_loader: define internal implementations of install/uninstallmultiple

A following patch is cleaning up the core EFI code trying to remove
sequences of efi_create_handle, efi_add_protocol.

Although this works fine there's a problem with the latter since it is
usually combined with efi_delete_handle() which blindly removes all
protocols on a handle and deletes the handle. We should try to adhere to
the EFI spec which only deletes a handle if the last instance of a protocol
has been removed. Another problem is that efi_delete_handle() never checks
for opened protocols, but the EFI spec defines that the caller is
responsible for ensuring that there are no references to a protocol
interface that is going to be removed.

So let's fix this by replacing all callsites of
efi_create_handle(), efi_add_protocol() , efi_delete_handle() with
Install/UninstallMultipleProtocol.

In order to do that redefine functions that can be used by the U-Boot
proper internally and add '_ext' variants that will be used from the
EFI API

Signed-off-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>
Reviewed-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# e9a1ff97 19-Sep-2022 Tom Rini <trini@konsulko.com>

Merge branch 'master' into next

Signed-off-by: Tom Rini <trini@konsulko.com>


# 87d79142 12-Sep-2022 Masahisa Kojima <masahisa.kojima@linaro.org>

eficonfig: menu-driven addition of UEFI boot option

This commit add the "eficonfig" command.
The "eficonfig" command implements the menu-driven UEFI boot option
maintenance feature. This commit implements the addition of
new boot option. User can select the block device volume having
efi_simple_file_system_protocol and select the file corresponding
to the Boot#### variable. User can also enter the description and
optional_data of the BOOT#### variable in utf8.

This commit adds "include/efi_config.h", it contains the common
definition to be used from other menus such as UEFI Secure Boot
key management.

Signed-off-by: Masahisa Kojima <masahisa.kojima@linaro.org>

# e053a144 01-Sep-2022 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: support CTRL+\ - CTRL+_

In the extended text input protocol support input of control letters
0x1c - 0x1f.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
Reviewed-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>

# 0b7b56d7 01-Sep-2022 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: compliance Simple Text Input Ex Protocol

We cannot expect the buffers passed to the input protocols to be zero
filled. If only modifier keys are pressed, we have to return EFI_NOT_READY
but we still have to fill the key structure.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
Reviewed-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>

# 68edbed4 14-Jun-2022 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: initialize console size late

If CONFIG_VIDEO_DM=n we query the display size from the serial console.
Especially when using a remote console the response can be so late that
it interferes with autoboot.

Only query the console size when running an EFI binary.

Add debug output showing the determined console size.

Reported-by: Fabio Estevam <festevam@gmail.com>
Fixes: a57ad20d07e8 ("efi_loader: split efi_init_obj_list() into two stages")
Fixes: a9bf024b2933 ("efi_loader: disk: a helper function to create efi_disk objects from udevice")
Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
Tested-by: Fabio Estevam <festevam@denx.de>
Tested-by: Heiko Thiery <heiko.thiery@gmail.com>

# 5c1037d5 30-Apr-2022 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: reset colors before clearing screen

When resetting the text console the colors have to be set before clearing
the screen. Otherwise the background color may be wrong.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>

# 3c95b323 04-Feb-2022 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: add handle for UART

When loading an EFI binary via the UART we assign a UART device path to it.
But we lack a handle with that device path.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>

# abd62e4d 29-Jun-2021 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: missing parentheses in query_console_size

After if we should use parentheses to keep the code readable.

Fixes: a95f4c885991 ("efi_loader: NULL dereference in EFI console")
Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# a95f4c88 15-Mar-2021 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: NULL dereference in EFI console

Even if CONFIG_DM_VIDEO=y and stdout="vidconsole", a video device may not
be available. Check the return values of the relevant functions.

If no video output device is available, assume that the serial console is
in use.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 997b57fc 28-Feb-2021 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: console size of vidconsole

If stdout is 'vidconsole', we correctly set the console size.
If stdout is 'vidconsole,serial', the video console is ignored.

We should always evaluate the size of vidconsole if it is the primary
console.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 7913c7dc 04-Jan-2021 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: typedef efi_string_t text output protocol

We do not want to use typedefs in U-Boot.

Do not use efi_string_t in the EFI_TEXT_OUTPUT_PROTOCOL.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 9abb01af 27-Dec-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: escape key handling

Up to now the escape key was not correctly detected in UEFI applications.
We had to hit it twice for a single escape to be recognized.

Use a 10 ms delay to detect if we are dealing with the escape key or an
escape sequence.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# c670aeee 07-Oct-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

common: rename getc() to getchar()

The sandbox is built with the SDL2 library with invokes the X11 library
which in turn calls getc(). But getc() in glibc is defined as

int getc(FILE *)

This does not match our definition.

int getc(void)

The sandbox crashes when called with parameter -l.

Rename our library symbol getc() to getchar().

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Tom Rini <trini@konsulko.com>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 325567d3 04-Jun-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: comments EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL

Provide missing comments for the functions implementing the
EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 336d4615 03-Feb-2020 Simon Glass <sjg@chromium.org>

dm: core: Create a new header file for 'compat' features

At present dm/device.h includes the linux-compatible features. This
requires including linux/compat.h which in turn includes a lot of headers.
One of these is malloc.h which we thus end up including in every file in
U-Boot. Apart from the inefficiency of this, it is problematic for sandbox
which needs to use the system malloc() in some files.

Move the compatibility features into a separate header file.

Signed-off-by: Simon Glass <sjg@chromium.org>

# b0ad9b5b 22-Dec-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: clear screen should move cursor to home

On a VT100 terminal <ESC>[2J should be enough to both clear the whole
screen and set the cursor to position (1, 1). But the Linux console does
not behave like this. So send an extra <ESC>[H. For reference see the
console_codes(4) man page.

Add a function description.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 1045315d 14-Nov-2019 Simon Glass <sjg@chromium.org>

common: Move get_ticks() function out of common.h

This function belongs in time.h so move it over and add a comment.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Tom Rini <trini@konsulko.com>

# fe1a81c1 05-Sep-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

doc: UEFI API documentation

Add some more files to the UEFI API documentation.

Correct some Sphinx comments.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 03446987 04-Sep-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: do not set invalid screen mode

EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.SetMode() should return EFI_UNDEFINED if a
screen mode is not available.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Alexander Graf <agraf@csgraf.de>

# 97ea0690 04-Sep-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: cursor positioning

When backspacing in column 0 do no set the column index to ULONG_MAX.
Ensure that the row number is not set to ULONG_MAX even if the row count is
advertised as 0.
Ignore control characters other the 0x08, 0x0a, 0x0d when updating the
column.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 7b51b576 01-Aug-2019 Simon Glass <sjg@chromium.org>

env: Move env_get() to env.h

Move env_get() over to the new header file.

Acked-by: Joe Hershberger <joe.hershberger@ni.com>
Signed-off-by: Simon Glass <sjg@chromium.org>

# 3b435c11 16-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: console incorrectly advertised left logo key

Avoid to signal that the left logo key is pressed, when it is not.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 3b2b2de8 16-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: alternative scan codes for F5, END, HOME

Depending on the key board alternative scan codes are used for F5, END, and
HOME.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 3950f0f8 13-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix SetAttribute()

The SetAttribute() service and the Reset() service of the simple text
output protocol must update the attribute value in the mode information.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 2ad238fc 13-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix SetMode()

Correct the check of the mode number in SetMode() service of the simple
text output protocol.

Clear the screen in SetMode().

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 7eaa900e 06-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: event signaling in ExitBootServices

ExitBootServices() has to stop timer related activity before calling the
events of the EFI_EVENT_GROUP_EXIT_BOOT_SERVICES event group. But our
current implementation was stopping all other events.

All events have to observe the task priority level.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 25e6fb5e 02-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix EnableCursor()

The EnableCursor() service of the simple text output protocol must update
the the CursorVisible field of the output mode.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# b31ca6bf 18-May-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: parameter check OutputString

Check the parameters against NULL.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# acee9652 18-May-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.SetState() correct parameter

KeyToggleState is a pointer according to UEFI spec 2.8.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# bfc2dd53 06-Apr-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: correct CTRL-A - CTRL-Z console input

In the extended text input protocol CTRL-A - CTRL-Z have to be signaled as
Unicode characters a-z or A-Z depending on the shift state and not as 0x01
to 0x1a.

Update Python unit test.

This patch is required for using the EFI shell `edit` command.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# dd1a1ec2 04-Mar-2019 Matthias Brugger <mbrugger@suse.com>

efi_loader: Fix serial console size detection

Function term_read_reply tries to read from the serial console until
the end_char was read. This can hang forever if we are, for some reason,
not able to read the full response (e.g. serial buffer too small,
frame error). This patch moves the timeout detection into
term_read_reply() to assure we will make progress.

Fixes: 6bb591f704 ("efi_loader: query serial console size reliably")
Signed-off-by: Matthias Brugger <mbrugger@suse.com>

Throw missing error when an incomplete reply for the cursor position is
received.

Change type of argument of term_get_char() *s32. This renders the function
reusable in efi_cin_read_key().

Reviewed-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# e1fec152 18-Oct-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix typos

Fix typos in EFI subsystem comments.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# fae0118e 25-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: eliminate handle member

A pointer to a struct efi_object is a handle. We do not need any handle
member in this structure. Let's eliminate it.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 6f566c23 01-Oct-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: return type efi_console_register()

Use a return type that can encompass the return value.

This fixes CoverityScan CID 184090.

Reported-by: Tom Rini <trini@konsulko.com>
Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 6bb591f7 15-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: query serial console size reliably

Not all terminals understand CSI [18t for querying the console size.
We should adhere to escape sequences documented in the console_codes
manpage and the ECMA-48 standard.

So here we follow a different approach. We position the cursor to the
bottom right and query its position. Before leaving the function we
restore the original cursor position.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# aaace4b0 14-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: terminal left upper corner is [1, 1]

For the VT102 and the Linux console the left upper corner is [1, 1].
EFI uses [0, 0] as left upper corner.

Check parameters of the SetCursorPosition() service.

Use CSI H like EDK2 for positioning.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 4fdcf066 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: implement key notify functions

Implement registering and unregistreing key notify functions in the
EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 55fbdf99 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: support modifiers for F1 - F4

Support modifiers for F1 - F4.
Add support for letters with ALT key.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 110c6280 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL

This patch implements the EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.

The implementation of notification functions is postponed to a later
patch.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 0dfd13a4 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: rework event handling for console

Preread the next key in the console timer event.

The EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL requires to trigger registered key
notification functions based on the prefetched key.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 35cbb796 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: support Unicode text input

Up to now the EFI_TEXT_INPUT_PROTOCOL only supported ASCII characters.
With the patch it can consume UTF-8 from the console.

Currently only the serial console and the console can deliver UTF-8.
Local consoles are restricted to ASCII.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 14d103bb 08-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: typos in efi_console.c

Fix a few typos.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 3e603ec7 08-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: struct efi_simple_text_input_protocol

%s/efi_simple_input_interface/efi_simple_text_input_protocol/

We should be consistent in the naming of the EFI protocol interface
structures. The protocol is called EFI_SIMPLE_TEXT_INPUT_PROTOCOL.

%s/ExtendedVerification/extended_verification/

Use consistent naming of function parameters. Do not use CamelCase.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 40e3e757 04-Sep-2018 Alexander Graf <agraf@csgraf.de>

efi_loader: Add console handle references to in/out/err

We register the console in/out protocol on the loaded image handle, but
never tell anyone about it in the systab.

Because of this Shell.efi can not find the input protocol. So let's add
a proper reference.

Signed-off-by: Alexander Graf <agraf@suse.de>
Reviewed-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# ba7bd5c2 31-Aug-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: don't use unlimited stack as buffer

The length of a string printed to the console by the
EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL is not limited by the UEFI spec.
Hence should not allocate a buffer for it on the stack.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 1dde0d57 31-Aug-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: rename utf16_strlen, utf16_strnlen

The function names utf16_strlen() and utf16_strnlen() are misnomers.
The functions do not count utf-16 characters but non-zero words.
So let's rename them to u16_strlen and u16_strnlen().

In utf16_dup() avoid assignment in if clause.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 9d12daff 05-Jul-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.Reset()

Implement the reset service of the EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.

This should resolve the error reported by the SCT in
Protocol/SimpleTextOut/BlackBoxTest/SimpleTextOutBBTestFunction_uefi.c:639

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# e67ff94d 05-Jul-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: clear screen has to reset cursor position

After clearing the screen the cursor position is row 0, column 0.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 4f187897 05-Jul-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_INPUT_PROTOCOL.Reset()

Implement the reset service of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL.

This should resolve the error reported by the SCT in
Protocol/SimpleTextIn/BlackBoxTest/SimpleTextInBBTestFunction.c:193

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 80483b2a 03-Jun-2018 Alexander Graf <agraf@csgraf.de>

efi_loader: Fix warning in raw/cols query

The code to determine rows / cols on the screen could potentially run
into a case where it doesn't know how big the screen is. In that case,
assume 80x25.

Signed-off-by: Alexander Graf <agraf@suse.de>

# 62217295 16-May-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: avoid using unitialized values as console size

If a request for the console size would be answered with a response
with less then three values, uninitialized stack memory would be
copied to the number of rows and columns of the terminal.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# a4aa7bef 29-Apr-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: always check parameters in efi_cout_query_mode()

If we cannot determine the size of the serial terminal we still have
to check the parameters of efi_cout_query_mode().

Querying the size of the serial terminal drains the keyboard buffer.
So make sure we do this during the initialization and not in the midst
of an EFI application.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 7ca7c3c0 29-Apr-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: updating the cursor position

The current coding advances the column by the number of UTF-8 bytes.
The column should be increased by one per unicode character.

The UEFI spec provides advance rules for U+0000, U+0008, U+000A,
and U000D. All other characters, including control characters
U+0007 (bel) and U+0009 (tab), have to increase the column by one.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# f739fcd8 07-May-2018 Tom Rini <trini@konsulko.com>

SPDX: Convert a few files that were missed before

As part of the main conversion a few files were missed. These files had
additional whitespace after the '*' and before the SPDX tag and my
previous regex was too strict. This time I did a grep for all SPDX tags
and then filtered out anything that matched the correct styles.

Fixes: 83d290c56fab ("SPDX: Convert all of our single license tags to Linux Kernel style")
Reported-by: Heinrich Schuchardt <xypron.debian@gmx.de>
Signed-off-by: Tom Rini <trini@konsulko.com>

# b095f3c8 18-Feb-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: implement event groups

If an event of a group event is signaled all other events of the same
group are signaled too.

Function efi_signal_event is renamed to efi_queue_event.
A new function efi_signal_event is introduced that checks if an event
belongs to a group and than signals all events of the group.
Event group notifciation is implemented for ExitBootServices,
InstallConfigurationTable, and ResetSystem.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 0fb4169e 19-Feb-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: correct input of special keys

Don't set unicode_char if scan_code is set.
Add support for page up, page down, and insert.
Correct input of function keys.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 8396e3fd 03-Mar-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: remove deprecated ConsoleControlProtocol

The console control protocol is not defined in the UEFI standard.

It exists in EDK2's EdkCompatiblityPkg package. But this package
is deprecated according to
https://github.com/tianocore/tianocore.github.io/wiki/Differences-between-EDK-and-EDK-II

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 9bc9664d 19-Jan-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: add check_tpl parameter to efi_signal_event

In ExitBootServices we need to signal events irrespective of the current
TPL level. A new parameter check_tpl is added to efi_signal_event().

Function efi_console_timer_notify() gets some comments.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 2074f700 11-Jan-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: consistently use efi_handle_t for handles

We should consistently use the efi_handle_t typedef when
referring to handles.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# ebb4dd5b 26-Oct-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: efi_console: use helper functions

Use helper functions efi_created_handle and efi_add_protocol
for creating the console handles and instaling the respective
protocols.

This change is needed if we want to move from an array of
protocols to a linked list of protocols.

Eliminate EFI_PROTOCOL_OBJECT which is not used anymore.

Currently we have not defined protocol interfaces to be const.
So efi_con_out and efi_console_control cannot be defined as const.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 2d5dc2a5 10-Oct-2017 Rob Clark <robdclark@gmail.com>

efi_loader: console support for color attributes

Shell.efi uses this, and supporting color attributes makes things look
nicer. Map the EFI fg/bg color attributes to ANSI escape sequences.
Not all colors have a perfect match, but spec just says "Devices
supporting a different number of text colors are required to emulate the
above colors to the best of the device’s capabilities".

Signed-off-by: Rob Clark <robdclark@gmail.com>
Tested-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Alexander Graf <agraf@suse.de>
[agraf: s/unsigned/unsigned int/]
Signed-off-by: Alexander Graf <agraf@suse.de>

# e190e897 04-Oct-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: use type bool for event states

Queued and signaled describe boolean states of events.
So let's use type bool and rename the structure members to is_queued
and is_signaled.

Update the comments for is_queued and is_signaled.

Reported-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Rob Clark <robdclark@gmail.com>
Reviewed-by: Rob Clark <robdclark@gmail.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 3a45bc7f 13-Sep-2017 Rob Clark <robdclark@gmail.com>

efi_loader: Some console improvements for vidconsole

1) use fputs() to reduce cache flushes from once-per-char to
once-per-string
2) handle \r, \t, and \b in addition to just \n for tracking
cursor position
3) cursor row/col are zero based, not one based

Signed-off-by: Rob Clark <robdclark@gmail.com>
[agraf: s/unsigned/unsigned int/]
Signed-off-by: Alexander Graf <agraf@suse.de>

# a18c5a83 13-Sep-2017 Rob Clark <robdclark@gmail.com>

efi_loader: Correctly figure out size for vidconsole

If stdout is vidconsole, we cannot rely on ANSI escape sequences to
query the size, as vidconsole cannot reply on stdin. Instead special-
case this if stdout is vidconsole.

Signed-off-by: Rob Clark <robdclark@gmail.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 71cc25c3 13-Sep-2017 Rob Clark <robdclark@gmail.com>

efi_loader: split out escape sequence based size query

We need to do something different for vidconsole, since it cannot
respond to the query on stdin. Prep work for next patch.

Signed-off-by: Rob Clark <robdclark@gmail.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# ca62a4f5 15-Sep-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: implement queueing of the notification function

For the correct implementation of the task priority level (TPL)
calling the notification function must be queued.

Add a status field 'queued' to events.

In function efi_signal_event set status queued if a notification
function exists and reset it after we have called the function.
A later patch will add a check of the TPL here.

In efi_create_event and efi_close_event unset the queued status.

In function efi_wait_for_event and efi_check_event
queue the notification function.

In efi_timer_check call the efi_notify_event
if the status queued is set.
For all timer events set status signaled.

In efi_console_timer_notify set the signaled state of the
WaitForKey event.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 78178bb0 09-Sep-2017 Rob Clark <robdclark@gmail.com>

lib: add some utf16 handling helpers

We'll eventually want these in a few places in efi_loader, and also
vsprintf.

Signed-off-by: Rob Clark <robdclark@gmail.com>

# a17e62cc 24-Jul-2017 Rob Clark <robdclark@gmail.com>

efi_loader: expose protocols via GUID

shim.efi (or rather gnu-efi's LibLocateProtocol() which shim.efi uses)
resolves protocols via efi_locate_handle() so the console protocols
need to be added to the efi object list.

Signed-off-by: Rob Clark <robdclark@gmail.com>
[agraf: whitespace fixes]
Signed-off-by: Alexander Graf <agraf@suse.de>

# ff925938 19-Jul-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_console: use EFIAPI for callback functions

The call to efi_create_event requires notification functions
flagged as EFIAPI.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 91be9a77 18-Jul-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_console: set up events

Set up a timer event and the WaitForKey event.
In the notify function of the timer event check for console input
and signal the WaitForKey event accordingly.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 5be8b0a3 07-Nov-2016 Emmanuel Vadot <manu@bidouilliste.com>

efi_loader: console: Correctly report modes

Add support for EFI console modes.
Mode 0 is always 80x25 and present by EFI specification.
Mode 1 is always 80x50 and not mandatory.
Mode 2 and above is freely usable.

If the terminal can handle mode 1, we mark it as supported.
If the terminal size is greater than mode 0 and different than mode 1,
we install it as mode 2.

Modes can be switch with cout_set_mode.

Changes in V5:
Correctly detect mode before enabling mode 2.

Changes in V4:
Reset cursor positon on mode switch
Use local variables in console query code

Changes in V3:
Valid mode are 0 to EFIMode-1
Fix style

Changes in V2:
Add mode switch
Report only the modes that we support

Signed-off-by: Emmanuel Vadot <manu@bidouilliste.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# c1311ad4 03-Mar-2016 Alexander Graf <agraf@csgraf.de>

efi_loader: Add console interface

One of the basic EFI interfaces is the console interface. Using it an EFI
application can interface with the user. This patch implements an EFI console
interface using getc() and putc().

Today, we only implement text based consoles. We also convert the EFI Unicode
characters to UTF-8 on the fly, hoping that everyone managed to jump on the
train by now.

Signed-off-by: Alexander Graf <agraf@suse.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Tested-by: Simon Glass <sjg@chromium.org>

# 7831d36f 14-Oct-2022 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: avoid EFI_CALL() when draining console

Use internal function.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>

# f3272366 15-Oct-2022 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: avoid EFI_CALL() for clearing screen

Carve out function efi_clear_screen.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>

# 05c4c9e2 06-Oct-2022 Ilias Apalodimas <ilias.apalodimas@linaro.org>

efi_loader: define internal implementations of install/uninstallmultiple

A following patch is cleaning up the core EFI code trying to remove
sequences of efi_create_handle, efi_add_protocol.

Although this works fine there's a problem with the latter since it is
usually combined with efi_delete_handle() which blindly removes all
protocols on a handle and deletes the handle. We should try to adhere to
the EFI spec which only deletes a handle if the last instance of a protocol
has been removed. Another problem is that efi_delete_handle() never checks
for opened protocols, but the EFI spec defines that the caller is
responsible for ensuring that there are no references to a protocol
interface that is going to be removed.

So let's fix this by replacing all callsites of
efi_create_handle(), efi_add_protocol() , efi_delete_handle() with
Install/UninstallMultipleProtocol.

In order to do that redefine functions that can be used by the U-Boot
proper internally and add '_ext' variants that will be used from the
EFI API

Signed-off-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>
Reviewed-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# e9a1ff97 19-Sep-2022 Tom Rini <trini@konsulko.com>

Merge branch 'master' into next

Signed-off-by: Tom Rini <trini@konsulko.com>


# 87d79142 12-Sep-2022 Masahisa Kojima <masahisa.kojima@linaro.org>

eficonfig: menu-driven addition of UEFI boot option

This commit add the "eficonfig" command.
The "eficonfig" command implements the menu-driven UEFI boot option
maintenance feature. This commit implements the addition of
new boot option. User can select the block device volume having
efi_simple_file_system_protocol and select the file corresponding
to the Boot#### variable. User can also enter the description and
optional_data of the BOOT#### variable in utf8.

This commit adds "include/efi_config.h", it contains the common
definition to be used from other menus such as UEFI Secure Boot
key management.

Signed-off-by: Masahisa Kojima <masahisa.kojima@linaro.org>

# e053a144 01-Sep-2022 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: support CTRL+\ - CTRL+_

In the extended text input protocol support input of control letters
0x1c - 0x1f.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
Reviewed-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>

# 0b7b56d7 01-Sep-2022 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: compliance Simple Text Input Ex Protocol

We cannot expect the buffers passed to the input protocols to be zero
filled. If only modifier keys are pressed, we have to return EFI_NOT_READY
but we still have to fill the key structure.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
Reviewed-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>

# 68edbed4 14-Jun-2022 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: initialize console size late

If CONFIG_VIDEO_DM=n we query the display size from the serial console.
Especially when using a remote console the response can be so late that
it interferes with autoboot.

Only query the console size when running an EFI binary.

Add debug output showing the determined console size.

Reported-by: Fabio Estevam <festevam@gmail.com>
Fixes: a57ad20d07e8 ("efi_loader: split efi_init_obj_list() into two stages")
Fixes: a9bf024b2933 ("efi_loader: disk: a helper function to create efi_disk objects from udevice")
Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
Tested-by: Fabio Estevam <festevam@denx.de>
Tested-by: Heiko Thiery <heiko.thiery@gmail.com>

# 5c1037d5 30-Apr-2022 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: reset colors before clearing screen

When resetting the text console the colors have to be set before clearing
the screen. Otherwise the background color may be wrong.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>

# 3c95b323 04-Feb-2022 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: add handle for UART

When loading an EFI binary via the UART we assign a UART device path to it.
But we lack a handle with that device path.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>

# abd62e4d 29-Jun-2021 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: missing parentheses in query_console_size

After if we should use parentheses to keep the code readable.

Fixes: a95f4c885991 ("efi_loader: NULL dereference in EFI console")
Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# a95f4c88 15-Mar-2021 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: NULL dereference in EFI console

Even if CONFIG_DM_VIDEO=y and stdout="vidconsole", a video device may not
be available. Check the return values of the relevant functions.

If no video output device is available, assume that the serial console is
in use.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 997b57fc 28-Feb-2021 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: console size of vidconsole

If stdout is 'vidconsole', we correctly set the console size.
If stdout is 'vidconsole,serial', the video console is ignored.

We should always evaluate the size of vidconsole if it is the primary
console.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 7913c7dc 04-Jan-2021 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: typedef efi_string_t text output protocol

We do not want to use typedefs in U-Boot.

Do not use efi_string_t in the EFI_TEXT_OUTPUT_PROTOCOL.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 9abb01af 27-Dec-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: escape key handling

Up to now the escape key was not correctly detected in UEFI applications.
We had to hit it twice for a single escape to be recognized.

Use a 10 ms delay to detect if we are dealing with the escape key or an
escape sequence.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# c670aeee 07-Oct-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

common: rename getc() to getchar()

The sandbox is built with the SDL2 library with invokes the X11 library
which in turn calls getc(). But getc() in glibc is defined as

int getc(FILE *)

This does not match our definition.

int getc(void)

The sandbox crashes when called with parameter -l.

Rename our library symbol getc() to getchar().

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Tom Rini <trini@konsulko.com>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 325567d3 04-Jun-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: comments EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL

Provide missing comments for the functions implementing the
EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 336d4615 03-Feb-2020 Simon Glass <sjg@chromium.org>

dm: core: Create a new header file for 'compat' features

At present dm/device.h includes the linux-compatible features. This
requires including linux/compat.h which in turn includes a lot of headers.
One of these is malloc.h which we thus end up including in every file in
U-Boot. Apart from the inefficiency of this, it is problematic for sandbox
which needs to use the system malloc() in some files.

Move the compatibility features into a separate header file.

Signed-off-by: Simon Glass <sjg@chromium.org>

# b0ad9b5b 22-Dec-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: clear screen should move cursor to home

On a VT100 terminal <ESC>[2J should be enough to both clear the whole
screen and set the cursor to position (1, 1). But the Linux console does
not behave like this. So send an extra <ESC>[H. For reference see the
console_codes(4) man page.

Add a function description.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 1045315d 14-Nov-2019 Simon Glass <sjg@chromium.org>

common: Move get_ticks() function out of common.h

This function belongs in time.h so move it over and add a comment.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Tom Rini <trini@konsulko.com>

# fe1a81c1 05-Sep-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

doc: UEFI API documentation

Add some more files to the UEFI API documentation.

Correct some Sphinx comments.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 03446987 04-Sep-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: do not set invalid screen mode

EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.SetMode() should return EFI_UNDEFINED if a
screen mode is not available.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Alexander Graf <agraf@csgraf.de>

# 97ea0690 04-Sep-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: cursor positioning

When backspacing in column 0 do no set the column index to ULONG_MAX.
Ensure that the row number is not set to ULONG_MAX even if the row count is
advertised as 0.
Ignore control characters other the 0x08, 0x0a, 0x0d when updating the
column.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 7b51b576 01-Aug-2019 Simon Glass <sjg@chromium.org>

env: Move env_get() to env.h

Move env_get() over to the new header file.

Acked-by: Joe Hershberger <joe.hershberger@ni.com>
Signed-off-by: Simon Glass <sjg@chromium.org>

# 3b435c11 16-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: console incorrectly advertised left logo key

Avoid to signal that the left logo key is pressed, when it is not.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 3b2b2de8 16-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: alternative scan codes for F5, END, HOME

Depending on the key board alternative scan codes are used for F5, END, and
HOME.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 3950f0f8 13-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix SetAttribute()

The SetAttribute() service and the Reset() service of the simple text
output protocol must update the attribute value in the mode information.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 2ad238fc 13-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix SetMode()

Correct the check of the mode number in SetMode() service of the simple
text output protocol.

Clear the screen in SetMode().

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 7eaa900e 06-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: event signaling in ExitBootServices

ExitBootServices() has to stop timer related activity before calling the
events of the EFI_EVENT_GROUP_EXIT_BOOT_SERVICES event group. But our
current implementation was stopping all other events.

All events have to observe the task priority level.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 25e6fb5e 02-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix EnableCursor()

The EnableCursor() service of the simple text output protocol must update
the the CursorVisible field of the output mode.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# b31ca6bf 18-May-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: parameter check OutputString

Check the parameters against NULL.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# acee9652 18-May-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.SetState() correct parameter

KeyToggleState is a pointer according to UEFI spec 2.8.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# bfc2dd53 06-Apr-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: correct CTRL-A - CTRL-Z console input

In the extended text input protocol CTRL-A - CTRL-Z have to be signaled as
Unicode characters a-z or A-Z depending on the shift state and not as 0x01
to 0x1a.

Update Python unit test.

This patch is required for using the EFI shell `edit` command.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# dd1a1ec2 04-Mar-2019 Matthias Brugger <mbrugger@suse.com>

efi_loader: Fix serial console size detection

Function term_read_reply tries to read from the serial console until
the end_char was read. This can hang forever if we are, for some reason,
not able to read the full response (e.g. serial buffer too small,
frame error). This patch moves the timeout detection into
term_read_reply() to assure we will make progress.

Fixes: 6bb591f704 ("efi_loader: query serial console size reliably")
Signed-off-by: Matthias Brugger <mbrugger@suse.com>

Throw missing error when an incomplete reply for the cursor position is
received.

Change type of argument of term_get_char() *s32. This renders the function
reusable in efi_cin_read_key().

Reviewed-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# e1fec152 18-Oct-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix typos

Fix typos in EFI subsystem comments.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# fae0118e 25-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: eliminate handle member

A pointer to a struct efi_object is a handle. We do not need any handle
member in this structure. Let's eliminate it.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 6f566c23 01-Oct-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: return type efi_console_register()

Use a return type that can encompass the return value.

This fixes CoverityScan CID 184090.

Reported-by: Tom Rini <trini@konsulko.com>
Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 6bb591f7 15-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: query serial console size reliably

Not all terminals understand CSI [18t for querying the console size.
We should adhere to escape sequences documented in the console_codes
manpage and the ECMA-48 standard.

So here we follow a different approach. We position the cursor to the
bottom right and query its position. Before leaving the function we
restore the original cursor position.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# aaace4b0 14-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: terminal left upper corner is [1, 1]

For the VT102 and the Linux console the left upper corner is [1, 1].
EFI uses [0, 0] as left upper corner.

Check parameters of the SetCursorPosition() service.

Use CSI H like EDK2 for positioning.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 4fdcf066 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: implement key notify functions

Implement registering and unregistreing key notify functions in the
EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 55fbdf99 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: support modifiers for F1 - F4

Support modifiers for F1 - F4.
Add support for letters with ALT key.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 110c6280 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL

This patch implements the EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.

The implementation of notification functions is postponed to a later
patch.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 0dfd13a4 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: rework event handling for console

Preread the next key in the console timer event.

The EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL requires to trigger registered key
notification functions based on the prefetched key.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 35cbb796 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: support Unicode text input

Up to now the EFI_TEXT_INPUT_PROTOCOL only supported ASCII characters.
With the patch it can consume UTF-8 from the console.

Currently only the serial console and the console can deliver UTF-8.
Local consoles are restricted to ASCII.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 14d103bb 08-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: typos in efi_console.c

Fix a few typos.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 3e603ec7 08-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: struct efi_simple_text_input_protocol

%s/efi_simple_input_interface/efi_simple_text_input_protocol/

We should be consistent in the naming of the EFI protocol interface
structures. The protocol is called EFI_SIMPLE_TEXT_INPUT_PROTOCOL.

%s/ExtendedVerification/extended_verification/

Use consistent naming of function parameters. Do not use CamelCase.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 40e3e757 04-Sep-2018 Alexander Graf <agraf@csgraf.de>

efi_loader: Add console handle references to in/out/err

We register the console in/out protocol on the loaded image handle, but
never tell anyone about it in the systab.

Because of this Shell.efi can not find the input protocol. So let's add
a proper reference.

Signed-off-by: Alexander Graf <agraf@suse.de>
Reviewed-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# ba7bd5c2 31-Aug-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: don't use unlimited stack as buffer

The length of a string printed to the console by the
EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL is not limited by the UEFI spec.
Hence should not allocate a buffer for it on the stack.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 1dde0d57 31-Aug-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: rename utf16_strlen, utf16_strnlen

The function names utf16_strlen() and utf16_strnlen() are misnomers.
The functions do not count utf-16 characters but non-zero words.
So let's rename them to u16_strlen and u16_strnlen().

In utf16_dup() avoid assignment in if clause.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 9d12daff 05-Jul-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.Reset()

Implement the reset service of the EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.

This should resolve the error reported by the SCT in
Protocol/SimpleTextOut/BlackBoxTest/SimpleTextOutBBTestFunction_uefi.c:639

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# e67ff94d 05-Jul-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: clear screen has to reset cursor position

After clearing the screen the cursor position is row 0, column 0.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 4f187897 05-Jul-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_INPUT_PROTOCOL.Reset()

Implement the reset service of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL.

This should resolve the error reported by the SCT in
Protocol/SimpleTextIn/BlackBoxTest/SimpleTextInBBTestFunction.c:193

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 80483b2a 03-Jun-2018 Alexander Graf <agraf@csgraf.de>

efi_loader: Fix warning in raw/cols query

The code to determine rows / cols on the screen could potentially run
into a case where it doesn't know how big the screen is. In that case,
assume 80x25.

Signed-off-by: Alexander Graf <agraf@suse.de>

# 62217295 16-May-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: avoid using unitialized values as console size

If a request for the console size would be answered with a response
with less then three values, uninitialized stack memory would be
copied to the number of rows and columns of the terminal.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# a4aa7bef 29-Apr-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: always check parameters in efi_cout_query_mode()

If we cannot determine the size of the serial terminal we still have
to check the parameters of efi_cout_query_mode().

Querying the size of the serial terminal drains the keyboard buffer.
So make sure we do this during the initialization and not in the midst
of an EFI application.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 7ca7c3c0 29-Apr-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: updating the cursor position

The current coding advances the column by the number of UTF-8 bytes.
The column should be increased by one per unicode character.

The UEFI spec provides advance rules for U+0000, U+0008, U+000A,
and U000D. All other characters, including control characters
U+0007 (bel) and U+0009 (tab), have to increase the column by one.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# f739fcd8 07-May-2018 Tom Rini <trini@konsulko.com>

SPDX: Convert a few files that were missed before

As part of the main conversion a few files were missed. These files had
additional whitespace after the '*' and before the SPDX tag and my
previous regex was too strict. This time I did a grep for all SPDX tags
and then filtered out anything that matched the correct styles.

Fixes: 83d290c56fab ("SPDX: Convert all of our single license tags to Linux Kernel style")
Reported-by: Heinrich Schuchardt <xypron.debian@gmx.de>
Signed-off-by: Tom Rini <trini@konsulko.com>

# b095f3c8 18-Feb-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: implement event groups

If an event of a group event is signaled all other events of the same
group are signaled too.

Function efi_signal_event is renamed to efi_queue_event.
A new function efi_signal_event is introduced that checks if an event
belongs to a group and than signals all events of the group.
Event group notifciation is implemented for ExitBootServices,
InstallConfigurationTable, and ResetSystem.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 0fb4169e 19-Feb-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: correct input of special keys

Don't set unicode_char if scan_code is set.
Add support for page up, page down, and insert.
Correct input of function keys.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 8396e3fd 03-Mar-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: remove deprecated ConsoleControlProtocol

The console control protocol is not defined in the UEFI standard.

It exists in EDK2's EdkCompatiblityPkg package. But this package
is deprecated according to
https://github.com/tianocore/tianocore.github.io/wiki/Differences-between-EDK-and-EDK-II

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 9bc9664d 19-Jan-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: add check_tpl parameter to efi_signal_event

In ExitBootServices we need to signal events irrespective of the current
TPL level. A new parameter check_tpl is added to efi_signal_event().

Function efi_console_timer_notify() gets some comments.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 2074f700 11-Jan-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: consistently use efi_handle_t for handles

We should consistently use the efi_handle_t typedef when
referring to handles.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# ebb4dd5b 26-Oct-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: efi_console: use helper functions

Use helper functions efi_created_handle and efi_add_protocol
for creating the console handles and instaling the respective
protocols.

This change is needed if we want to move from an array of
protocols to a linked list of protocols.

Eliminate EFI_PROTOCOL_OBJECT which is not used anymore.

Currently we have not defined protocol interfaces to be const.
So efi_con_out and efi_console_control cannot be defined as const.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 2d5dc2a5 10-Oct-2017 Rob Clark <robdclark@gmail.com>

efi_loader: console support for color attributes

Shell.efi uses this, and supporting color attributes makes things look
nicer. Map the EFI fg/bg color attributes to ANSI escape sequences.
Not all colors have a perfect match, but spec just says "Devices
supporting a different number of text colors are required to emulate the
above colors to the best of the device’s capabilities".

Signed-off-by: Rob Clark <robdclark@gmail.com>
Tested-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Alexander Graf <agraf@suse.de>
[agraf: s/unsigned/unsigned int/]
Signed-off-by: Alexander Graf <agraf@suse.de>

# e190e897 04-Oct-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: use type bool for event states

Queued and signaled describe boolean states of events.
So let's use type bool and rename the structure members to is_queued
and is_signaled.

Update the comments for is_queued and is_signaled.

Reported-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Rob Clark <robdclark@gmail.com>
Reviewed-by: Rob Clark <robdclark@gmail.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 3a45bc7f 13-Sep-2017 Rob Clark <robdclark@gmail.com>

efi_loader: Some console improvements for vidconsole

1) use fputs() to reduce cache flushes from once-per-char to
once-per-string
2) handle \r, \t, and \b in addition to just \n for tracking
cursor position
3) cursor row/col are zero based, not one based

Signed-off-by: Rob Clark <robdclark@gmail.com>
[agraf: s/unsigned/unsigned int/]
Signed-off-by: Alexander Graf <agraf@suse.de>

# a18c5a83 13-Sep-2017 Rob Clark <robdclark@gmail.com>

efi_loader: Correctly figure out size for vidconsole

If stdout is vidconsole, we cannot rely on ANSI escape sequences to
query the size, as vidconsole cannot reply on stdin. Instead special-
case this if stdout is vidconsole.

Signed-off-by: Rob Clark <robdclark@gmail.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 71cc25c3 13-Sep-2017 Rob Clark <robdclark@gmail.com>

efi_loader: split out escape sequence based size query

We need to do something different for vidconsole, since it cannot
respond to the query on stdin. Prep work for next patch.

Signed-off-by: Rob Clark <robdclark@gmail.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# ca62a4f5 15-Sep-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: implement queueing of the notification function

For the correct implementation of the task priority level (TPL)
calling the notification function must be queued.

Add a status field 'queued' to events.

In function efi_signal_event set status queued if a notification
function exists and reset it after we have called the function.
A later patch will add a check of the TPL here.

In efi_create_event and efi_close_event unset the queued status.

In function efi_wait_for_event and efi_check_event
queue the notification function.

In efi_timer_check call the efi_notify_event
if the status queued is set.
For all timer events set status signaled.

In efi_console_timer_notify set the signaled state of the
WaitForKey event.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 78178bb0 09-Sep-2017 Rob Clark <robdclark@gmail.com>

lib: add some utf16 handling helpers

We'll eventually want these in a few places in efi_loader, and also
vsprintf.

Signed-off-by: Rob Clark <robdclark@gmail.com>

# a17e62cc 24-Jul-2017 Rob Clark <robdclark@gmail.com>

efi_loader: expose protocols via GUID

shim.efi (or rather gnu-efi's LibLocateProtocol() which shim.efi uses)
resolves protocols via efi_locate_handle() so the console protocols
need to be added to the efi object list.

Signed-off-by: Rob Clark <robdclark@gmail.com>
[agraf: whitespace fixes]
Signed-off-by: Alexander Graf <agraf@suse.de>

# ff925938 19-Jul-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_console: use EFIAPI for callback functions

The call to efi_create_event requires notification functions
flagged as EFIAPI.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 91be9a77 18-Jul-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_console: set up events

Set up a timer event and the WaitForKey event.
In the notify function of the timer event check for console input
and signal the WaitForKey event accordingly.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 5be8b0a3 07-Nov-2016 Emmanuel Vadot <manu@bidouilliste.com>

efi_loader: console: Correctly report modes

Add support for EFI console modes.
Mode 0 is always 80x25 and present by EFI specification.
Mode 1 is always 80x50 and not mandatory.
Mode 2 and above is freely usable.

If the terminal can handle mode 1, we mark it as supported.
If the terminal size is greater than mode 0 and different than mode 1,
we install it as mode 2.

Modes can be switch with cout_set_mode.

Changes in V5:
Correctly detect mode before enabling mode 2.

Changes in V4:
Reset cursor positon on mode switch
Use local variables in console query code

Changes in V3:
Valid mode are 0 to EFIMode-1
Fix style

Changes in V2:
Add mode switch
Report only the modes that we support

Signed-off-by: Emmanuel Vadot <manu@bidouilliste.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# c1311ad4 03-Mar-2016 Alexander Graf <agraf@csgraf.de>

efi_loader: Add console interface

One of the basic EFI interfaces is the console interface. Using it an EFI
application can interface with the user. This patch implements an EFI console
interface using getc() and putc().

Today, we only implement text based consoles. We also convert the EFI Unicode
characters to UTF-8 on the fly, hoping that everyone managed to jump on the
train by now.

Signed-off-by: Alexander Graf <agraf@suse.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Tested-by: Simon Glass <sjg@chromium.org>

# 05c4c9e2 06-Oct-2022 Ilias Apalodimas <ilias.apalodimas@linaro.org>

efi_loader: define internal implementations of install/uninstallmultiple

A following patch is cleaning up the core EFI code trying to remove
sequences of efi_create_handle, efi_add_protocol.

Although this works fine there's a problem with the latter since it is
usually combined with efi_delete_handle() which blindly removes all
protocols on a handle and deletes the handle. We should try to adhere to
the EFI spec which only deletes a handle if the last instance of a protocol
has been removed. Another problem is that efi_delete_handle() never checks
for opened protocols, but the EFI spec defines that the caller is
responsible for ensuring that there are no references to a protocol
interface that is going to be removed.

So let's fix this by replacing all callsites of
efi_create_handle(), efi_add_protocol() , efi_delete_handle() with
Install/UninstallMultipleProtocol.

In order to do that redefine functions that can be used by the U-Boot
proper internally and add '_ext' variants that will be used from the
EFI API

Signed-off-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>
Reviewed-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# e9a1ff97 19-Sep-2022 Tom Rini <trini@konsulko.com>

Merge branch 'master' into next

Signed-off-by: Tom Rini <trini@konsulko.com>


# 87d79142 12-Sep-2022 Masahisa Kojima <masahisa.kojima@linaro.org>

eficonfig: menu-driven addition of UEFI boot option

This commit add the "eficonfig" command.
The "eficonfig" command implements the menu-driven UEFI boot option
maintenance feature. This commit implements the addition of
new boot option. User can select the block device volume having
efi_simple_file_system_protocol and select the file corresponding
to the Boot#### variable. User can also enter the description and
optional_data of the BOOT#### variable in utf8.

This commit adds "include/efi_config.h", it contains the common
definition to be used from other menus such as UEFI Secure Boot
key management.

Signed-off-by: Masahisa Kojima <masahisa.kojima@linaro.org>

# e053a144 01-Sep-2022 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: support CTRL+\ - CTRL+_

In the extended text input protocol support input of control letters
0x1c - 0x1f.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
Reviewed-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>

# 0b7b56d7 01-Sep-2022 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: compliance Simple Text Input Ex Protocol

We cannot expect the buffers passed to the input protocols to be zero
filled. If only modifier keys are pressed, we have to return EFI_NOT_READY
but we still have to fill the key structure.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
Reviewed-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>

# 68edbed4 14-Jun-2022 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: initialize console size late

If CONFIG_VIDEO_DM=n we query the display size from the serial console.
Especially when using a remote console the response can be so late that
it interferes with autoboot.

Only query the console size when running an EFI binary.

Add debug output showing the determined console size.

Reported-by: Fabio Estevam <festevam@gmail.com>
Fixes: a57ad20d07e8 ("efi_loader: split efi_init_obj_list() into two stages")
Fixes: a9bf024b2933 ("efi_loader: disk: a helper function to create efi_disk objects from udevice")
Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
Tested-by: Fabio Estevam <festevam@denx.de>
Tested-by: Heiko Thiery <heiko.thiery@gmail.com>

# 5c1037d5 30-Apr-2022 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: reset colors before clearing screen

When resetting the text console the colors have to be set before clearing
the screen. Otherwise the background color may be wrong.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>

# 3c95b323 04-Feb-2022 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: add handle for UART

When loading an EFI binary via the UART we assign a UART device path to it.
But we lack a handle with that device path.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>

# abd62e4d 29-Jun-2021 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: missing parentheses in query_console_size

After if we should use parentheses to keep the code readable.

Fixes: a95f4c885991 ("efi_loader: NULL dereference in EFI console")
Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# a95f4c88 15-Mar-2021 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: NULL dereference in EFI console

Even if CONFIG_DM_VIDEO=y and stdout="vidconsole", a video device may not
be available. Check the return values of the relevant functions.

If no video output device is available, assume that the serial console is
in use.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 997b57fc 28-Feb-2021 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: console size of vidconsole

If stdout is 'vidconsole', we correctly set the console size.
If stdout is 'vidconsole,serial', the video console is ignored.

We should always evaluate the size of vidconsole if it is the primary
console.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 7913c7dc 04-Jan-2021 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: typedef efi_string_t text output protocol

We do not want to use typedefs in U-Boot.

Do not use efi_string_t in the EFI_TEXT_OUTPUT_PROTOCOL.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 9abb01af 27-Dec-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: escape key handling

Up to now the escape key was not correctly detected in UEFI applications.
We had to hit it twice for a single escape to be recognized.

Use a 10 ms delay to detect if we are dealing with the escape key or an
escape sequence.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# c670aeee 07-Oct-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

common: rename getc() to getchar()

The sandbox is built with the SDL2 library with invokes the X11 library
which in turn calls getc(). But getc() in glibc is defined as

int getc(FILE *)

This does not match our definition.

int getc(void)

The sandbox crashes when called with parameter -l.

Rename our library symbol getc() to getchar().

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Tom Rini <trini@konsulko.com>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 325567d3 04-Jun-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: comments EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL

Provide missing comments for the functions implementing the
EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 336d4615 03-Feb-2020 Simon Glass <sjg@chromium.org>

dm: core: Create a new header file for 'compat' features

At present dm/device.h includes the linux-compatible features. This
requires including linux/compat.h which in turn includes a lot of headers.
One of these is malloc.h which we thus end up including in every file in
U-Boot. Apart from the inefficiency of this, it is problematic for sandbox
which needs to use the system malloc() in some files.

Move the compatibility features into a separate header file.

Signed-off-by: Simon Glass <sjg@chromium.org>

# b0ad9b5b 22-Dec-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: clear screen should move cursor to home

On a VT100 terminal <ESC>[2J should be enough to both clear the whole
screen and set the cursor to position (1, 1). But the Linux console does
not behave like this. So send an extra <ESC>[H. For reference see the
console_codes(4) man page.

Add a function description.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 1045315d 14-Nov-2019 Simon Glass <sjg@chromium.org>

common: Move get_ticks() function out of common.h

This function belongs in time.h so move it over and add a comment.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Tom Rini <trini@konsulko.com>

# fe1a81c1 05-Sep-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

doc: UEFI API documentation

Add some more files to the UEFI API documentation.

Correct some Sphinx comments.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 03446987 04-Sep-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: do not set invalid screen mode

EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.SetMode() should return EFI_UNDEFINED if a
screen mode is not available.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Alexander Graf <agraf@csgraf.de>

# 97ea0690 04-Sep-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: cursor positioning

When backspacing in column 0 do no set the column index to ULONG_MAX.
Ensure that the row number is not set to ULONG_MAX even if the row count is
advertised as 0.
Ignore control characters other the 0x08, 0x0a, 0x0d when updating the
column.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 7b51b576 01-Aug-2019 Simon Glass <sjg@chromium.org>

env: Move env_get() to env.h

Move env_get() over to the new header file.

Acked-by: Joe Hershberger <joe.hershberger@ni.com>
Signed-off-by: Simon Glass <sjg@chromium.org>

# 3b435c11 16-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: console incorrectly advertised left logo key

Avoid to signal that the left logo key is pressed, when it is not.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 3b2b2de8 16-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: alternative scan codes for F5, END, HOME

Depending on the key board alternative scan codes are used for F5, END, and
HOME.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 3950f0f8 13-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix SetAttribute()

The SetAttribute() service and the Reset() service of the simple text
output protocol must update the attribute value in the mode information.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 2ad238fc 13-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix SetMode()

Correct the check of the mode number in SetMode() service of the simple
text output protocol.

Clear the screen in SetMode().

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 7eaa900e 06-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: event signaling in ExitBootServices

ExitBootServices() has to stop timer related activity before calling the
events of the EFI_EVENT_GROUP_EXIT_BOOT_SERVICES event group. But our
current implementation was stopping all other events.

All events have to observe the task priority level.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 25e6fb5e 02-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix EnableCursor()

The EnableCursor() service of the simple text output protocol must update
the the CursorVisible field of the output mode.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# b31ca6bf 18-May-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: parameter check OutputString

Check the parameters against NULL.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# acee9652 18-May-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.SetState() correct parameter

KeyToggleState is a pointer according to UEFI spec 2.8.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# bfc2dd53 06-Apr-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: correct CTRL-A - CTRL-Z console input

In the extended text input protocol CTRL-A - CTRL-Z have to be signaled as
Unicode characters a-z or A-Z depending on the shift state and not as 0x01
to 0x1a.

Update Python unit test.

This patch is required for using the EFI shell `edit` command.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# dd1a1ec2 04-Mar-2019 Matthias Brugger <mbrugger@suse.com>

efi_loader: Fix serial console size detection

Function term_read_reply tries to read from the serial console until
the end_char was read. This can hang forever if we are, for some reason,
not able to read the full response (e.g. serial buffer too small,
frame error). This patch moves the timeout detection into
term_read_reply() to assure we will make progress.

Fixes: 6bb591f704 ("efi_loader: query serial console size reliably")
Signed-off-by: Matthias Brugger <mbrugger@suse.com>

Throw missing error when an incomplete reply for the cursor position is
received.

Change type of argument of term_get_char() *s32. This renders the function
reusable in efi_cin_read_key().

Reviewed-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# e1fec152 18-Oct-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix typos

Fix typos in EFI subsystem comments.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# fae0118e 25-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: eliminate handle member

A pointer to a struct efi_object is a handle. We do not need any handle
member in this structure. Let's eliminate it.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 6f566c23 01-Oct-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: return type efi_console_register()

Use a return type that can encompass the return value.

This fixes CoverityScan CID 184090.

Reported-by: Tom Rini <trini@konsulko.com>
Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 6bb591f7 15-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: query serial console size reliably

Not all terminals understand CSI [18t for querying the console size.
We should adhere to escape sequences documented in the console_codes
manpage and the ECMA-48 standard.

So here we follow a different approach. We position the cursor to the
bottom right and query its position. Before leaving the function we
restore the original cursor position.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# aaace4b0 14-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: terminal left upper corner is [1, 1]

For the VT102 and the Linux console the left upper corner is [1, 1].
EFI uses [0, 0] as left upper corner.

Check parameters of the SetCursorPosition() service.

Use CSI H like EDK2 for positioning.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 4fdcf066 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: implement key notify functions

Implement registering and unregistreing key notify functions in the
EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 55fbdf99 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: support modifiers for F1 - F4

Support modifiers for F1 - F4.
Add support for letters with ALT key.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 110c6280 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL

This patch implements the EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.

The implementation of notification functions is postponed to a later
patch.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 0dfd13a4 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: rework event handling for console

Preread the next key in the console timer event.

The EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL requires to trigger registered key
notification functions based on the prefetched key.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 35cbb796 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: support Unicode text input

Up to now the EFI_TEXT_INPUT_PROTOCOL only supported ASCII characters.
With the patch it can consume UTF-8 from the console.

Currently only the serial console and the console can deliver UTF-8.
Local consoles are restricted to ASCII.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 14d103bb 08-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: typos in efi_console.c

Fix a few typos.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 3e603ec7 08-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: struct efi_simple_text_input_protocol

%s/efi_simple_input_interface/efi_simple_text_input_protocol/

We should be consistent in the naming of the EFI protocol interface
structures. The protocol is called EFI_SIMPLE_TEXT_INPUT_PROTOCOL.

%s/ExtendedVerification/extended_verification/

Use consistent naming of function parameters. Do not use CamelCase.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 40e3e757 04-Sep-2018 Alexander Graf <agraf@csgraf.de>

efi_loader: Add console handle references to in/out/err

We register the console in/out protocol on the loaded image handle, but
never tell anyone about it in the systab.

Because of this Shell.efi can not find the input protocol. So let's add
a proper reference.

Signed-off-by: Alexander Graf <agraf@suse.de>
Reviewed-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# ba7bd5c2 31-Aug-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: don't use unlimited stack as buffer

The length of a string printed to the console by the
EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL is not limited by the UEFI spec.
Hence should not allocate a buffer for it on the stack.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 1dde0d57 31-Aug-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: rename utf16_strlen, utf16_strnlen

The function names utf16_strlen() and utf16_strnlen() are misnomers.
The functions do not count utf-16 characters but non-zero words.
So let's rename them to u16_strlen and u16_strnlen().

In utf16_dup() avoid assignment in if clause.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 9d12daff 05-Jul-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.Reset()

Implement the reset service of the EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.

This should resolve the error reported by the SCT in
Protocol/SimpleTextOut/BlackBoxTest/SimpleTextOutBBTestFunction_uefi.c:639

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# e67ff94d 05-Jul-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: clear screen has to reset cursor position

After clearing the screen the cursor position is row 0, column 0.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 4f187897 05-Jul-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_INPUT_PROTOCOL.Reset()

Implement the reset service of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL.

This should resolve the error reported by the SCT in
Protocol/SimpleTextIn/BlackBoxTest/SimpleTextInBBTestFunction.c:193

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 80483b2a 03-Jun-2018 Alexander Graf <agraf@csgraf.de>

efi_loader: Fix warning in raw/cols query

The code to determine rows / cols on the screen could potentially run
into a case where it doesn't know how big the screen is. In that case,
assume 80x25.

Signed-off-by: Alexander Graf <agraf@suse.de>

# 62217295 16-May-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: avoid using unitialized values as console size

If a request for the console size would be answered with a response
with less then three values, uninitialized stack memory would be
copied to the number of rows and columns of the terminal.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# a4aa7bef 29-Apr-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: always check parameters in efi_cout_query_mode()

If we cannot determine the size of the serial terminal we still have
to check the parameters of efi_cout_query_mode().

Querying the size of the serial terminal drains the keyboard buffer.
So make sure we do this during the initialization and not in the midst
of an EFI application.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 7ca7c3c0 29-Apr-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: updating the cursor position

The current coding advances the column by the number of UTF-8 bytes.
The column should be increased by one per unicode character.

The UEFI spec provides advance rules for U+0000, U+0008, U+000A,
and U000D. All other characters, including control characters
U+0007 (bel) and U+0009 (tab), have to increase the column by one.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# f739fcd8 07-May-2018 Tom Rini <trini@konsulko.com>

SPDX: Convert a few files that were missed before

As part of the main conversion a few files were missed. These files had
additional whitespace after the '*' and before the SPDX tag and my
previous regex was too strict. This time I did a grep for all SPDX tags
and then filtered out anything that matched the correct styles.

Fixes: 83d290c56fab ("SPDX: Convert all of our single license tags to Linux Kernel style")
Reported-by: Heinrich Schuchardt <xypron.debian@gmx.de>
Signed-off-by: Tom Rini <trini@konsulko.com>

# b095f3c8 18-Feb-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: implement event groups

If an event of a group event is signaled all other events of the same
group are signaled too.

Function efi_signal_event is renamed to efi_queue_event.
A new function efi_signal_event is introduced that checks if an event
belongs to a group and than signals all events of the group.
Event group notifciation is implemented for ExitBootServices,
InstallConfigurationTable, and ResetSystem.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 0fb4169e 19-Feb-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: correct input of special keys

Don't set unicode_char if scan_code is set.
Add support for page up, page down, and insert.
Correct input of function keys.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 8396e3fd 03-Mar-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: remove deprecated ConsoleControlProtocol

The console control protocol is not defined in the UEFI standard.

It exists in EDK2's EdkCompatiblityPkg package. But this package
is deprecated according to
https://github.com/tianocore/tianocore.github.io/wiki/Differences-between-EDK-and-EDK-II

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 9bc9664d 19-Jan-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: add check_tpl parameter to efi_signal_event

In ExitBootServices we need to signal events irrespective of the current
TPL level. A new parameter check_tpl is added to efi_signal_event().

Function efi_console_timer_notify() gets some comments.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 2074f700 11-Jan-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: consistently use efi_handle_t for handles

We should consistently use the efi_handle_t typedef when
referring to handles.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# ebb4dd5b 26-Oct-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: efi_console: use helper functions

Use helper functions efi_created_handle and efi_add_protocol
for creating the console handles and instaling the respective
protocols.

This change is needed if we want to move from an array of
protocols to a linked list of protocols.

Eliminate EFI_PROTOCOL_OBJECT which is not used anymore.

Currently we have not defined protocol interfaces to be const.
So efi_con_out and efi_console_control cannot be defined as const.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 2d5dc2a5 10-Oct-2017 Rob Clark <robdclark@gmail.com>

efi_loader: console support for color attributes

Shell.efi uses this, and supporting color attributes makes things look
nicer. Map the EFI fg/bg color attributes to ANSI escape sequences.
Not all colors have a perfect match, but spec just says "Devices
supporting a different number of text colors are required to emulate the
above colors to the best of the device’s capabilities".

Signed-off-by: Rob Clark <robdclark@gmail.com>
Tested-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Alexander Graf <agraf@suse.de>
[agraf: s/unsigned/unsigned int/]
Signed-off-by: Alexander Graf <agraf@suse.de>

# e190e897 04-Oct-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: use type bool for event states

Queued and signaled describe boolean states of events.
So let's use type bool and rename the structure members to is_queued
and is_signaled.

Update the comments for is_queued and is_signaled.

Reported-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Rob Clark <robdclark@gmail.com>
Reviewed-by: Rob Clark <robdclark@gmail.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 3a45bc7f 13-Sep-2017 Rob Clark <robdclark@gmail.com>

efi_loader: Some console improvements for vidconsole

1) use fputs() to reduce cache flushes from once-per-char to
once-per-string
2) handle \r, \t, and \b in addition to just \n for tracking
cursor position
3) cursor row/col are zero based, not one based

Signed-off-by: Rob Clark <robdclark@gmail.com>
[agraf: s/unsigned/unsigned int/]
Signed-off-by: Alexander Graf <agraf@suse.de>

# a18c5a83 13-Sep-2017 Rob Clark <robdclark@gmail.com>

efi_loader: Correctly figure out size for vidconsole

If stdout is vidconsole, we cannot rely on ANSI escape sequences to
query the size, as vidconsole cannot reply on stdin. Instead special-
case this if stdout is vidconsole.

Signed-off-by: Rob Clark <robdclark@gmail.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 71cc25c3 13-Sep-2017 Rob Clark <robdclark@gmail.com>

efi_loader: split out escape sequence based size query

We need to do something different for vidconsole, since it cannot
respond to the query on stdin. Prep work for next patch.

Signed-off-by: Rob Clark <robdclark@gmail.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# ca62a4f5 15-Sep-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: implement queueing of the notification function

For the correct implementation of the task priority level (TPL)
calling the notification function must be queued.

Add a status field 'queued' to events.

In function efi_signal_event set status queued if a notification
function exists and reset it after we have called the function.
A later patch will add a check of the TPL here.

In efi_create_event and efi_close_event unset the queued status.

In function efi_wait_for_event and efi_check_event
queue the notification function.

In efi_timer_check call the efi_notify_event
if the status queued is set.
For all timer events set status signaled.

In efi_console_timer_notify set the signaled state of the
WaitForKey event.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 78178bb0 09-Sep-2017 Rob Clark <robdclark@gmail.com>

lib: add some utf16 handling helpers

We'll eventually want these in a few places in efi_loader, and also
vsprintf.

Signed-off-by: Rob Clark <robdclark@gmail.com>

# a17e62cc 24-Jul-2017 Rob Clark <robdclark@gmail.com>

efi_loader: expose protocols via GUID

shim.efi (or rather gnu-efi's LibLocateProtocol() which shim.efi uses)
resolves protocols via efi_locate_handle() so the console protocols
need to be added to the efi object list.

Signed-off-by: Rob Clark <robdclark@gmail.com>
[agraf: whitespace fixes]
Signed-off-by: Alexander Graf <agraf@suse.de>

# ff925938 19-Jul-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_console: use EFIAPI for callback functions

The call to efi_create_event requires notification functions
flagged as EFIAPI.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 91be9a77 18-Jul-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_console: set up events

Set up a timer event and the WaitForKey event.
In the notify function of the timer event check for console input
and signal the WaitForKey event accordingly.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 5be8b0a3 07-Nov-2016 Emmanuel Vadot <manu@bidouilliste.com>

efi_loader: console: Correctly report modes

Add support for EFI console modes.
Mode 0 is always 80x25 and present by EFI specification.
Mode 1 is always 80x50 and not mandatory.
Mode 2 and above is freely usable.

If the terminal can handle mode 1, we mark it as supported.
If the terminal size is greater than mode 0 and different than mode 1,
we install it as mode 2.

Modes can be switch with cout_set_mode.

Changes in V5:
Correctly detect mode before enabling mode 2.

Changes in V4:
Reset cursor positon on mode switch
Use local variables in console query code

Changes in V3:
Valid mode are 0 to EFIMode-1
Fix style

Changes in V2:
Add mode switch
Report only the modes that we support

Signed-off-by: Emmanuel Vadot <manu@bidouilliste.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# c1311ad4 03-Mar-2016 Alexander Graf <agraf@csgraf.de>

efi_loader: Add console interface

One of the basic EFI interfaces is the console interface. Using it an EFI
application can interface with the user. This patch implements an EFI console
interface using getc() and putc().

Today, we only implement text based consoles. We also convert the EFI Unicode
characters to UTF-8 on the fly, hoping that everyone managed to jump on the
train by now.

Signed-off-by: Alexander Graf <agraf@suse.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Tested-by: Simon Glass <sjg@chromium.org>

# e9a1ff97 19-Sep-2022 Tom Rini <trini@konsulko.com>

Merge branch 'master' into next

Signed-off-by: Tom Rini <trini@konsulko.com>


# 87d79142 12-Sep-2022 Masahisa Kojima <masahisa.kojima@linaro.org>

eficonfig: menu-driven addition of UEFI boot option

This commit add the "eficonfig" command.
The "eficonfig" command implements the menu-driven UEFI boot option
maintenance feature. This commit implements the addition of
new boot option. User can select the block device volume having
efi_simple_file_system_protocol and select the file corresponding
to the Boot#### variable. User can also enter the description and
optional_data of the BOOT#### variable in utf8.

This commit adds "include/efi_config.h", it contains the common
definition to be used from other menus such as UEFI Secure Boot
key management.

Signed-off-by: Masahisa Kojima <masahisa.kojima@linaro.org>

# e053a144 01-Sep-2022 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: support CTRL+\ - CTRL+_

In the extended text input protocol support input of control letters
0x1c - 0x1f.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
Reviewed-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>

# 0b7b56d7 01-Sep-2022 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: compliance Simple Text Input Ex Protocol

We cannot expect the buffers passed to the input protocols to be zero
filled. If only modifier keys are pressed, we have to return EFI_NOT_READY
but we still have to fill the key structure.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
Reviewed-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>

# 68edbed4 14-Jun-2022 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: initialize console size late

If CONFIG_VIDEO_DM=n we query the display size from the serial console.
Especially when using a remote console the response can be so late that
it interferes with autoboot.

Only query the console size when running an EFI binary.

Add debug output showing the determined console size.

Reported-by: Fabio Estevam <festevam@gmail.com>
Fixes: a57ad20d07e8 ("efi_loader: split efi_init_obj_list() into two stages")
Fixes: a9bf024b2933 ("efi_loader: disk: a helper function to create efi_disk objects from udevice")
Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
Tested-by: Fabio Estevam <festevam@denx.de>
Tested-by: Heiko Thiery <heiko.thiery@gmail.com>

# 5c1037d5 30-Apr-2022 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: reset colors before clearing screen

When resetting the text console the colors have to be set before clearing
the screen. Otherwise the background color may be wrong.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>

# 3c95b323 04-Feb-2022 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: add handle for UART

When loading an EFI binary via the UART we assign a UART device path to it.
But we lack a handle with that device path.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>

# abd62e4d 29-Jun-2021 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: missing parentheses in query_console_size

After if we should use parentheses to keep the code readable.

Fixes: a95f4c885991 ("efi_loader: NULL dereference in EFI console")
Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# a95f4c88 15-Mar-2021 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: NULL dereference in EFI console

Even if CONFIG_DM_VIDEO=y and stdout="vidconsole", a video device may not
be available. Check the return values of the relevant functions.

If no video output device is available, assume that the serial console is
in use.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 997b57fc 28-Feb-2021 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: console size of vidconsole

If stdout is 'vidconsole', we correctly set the console size.
If stdout is 'vidconsole,serial', the video console is ignored.

We should always evaluate the size of vidconsole if it is the primary
console.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 7913c7dc 04-Jan-2021 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: typedef efi_string_t text output protocol

We do not want to use typedefs in U-Boot.

Do not use efi_string_t in the EFI_TEXT_OUTPUT_PROTOCOL.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 9abb01af 27-Dec-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: escape key handling

Up to now the escape key was not correctly detected in UEFI applications.
We had to hit it twice for a single escape to be recognized.

Use a 10 ms delay to detect if we are dealing with the escape key or an
escape sequence.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# c670aeee 07-Oct-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

common: rename getc() to getchar()

The sandbox is built with the SDL2 library with invokes the X11 library
which in turn calls getc(). But getc() in glibc is defined as

int getc(FILE *)

This does not match our definition.

int getc(void)

The sandbox crashes when called with parameter -l.

Rename our library symbol getc() to getchar().

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Tom Rini <trini@konsulko.com>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 325567d3 04-Jun-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: comments EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL

Provide missing comments for the functions implementing the
EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 336d4615 03-Feb-2020 Simon Glass <sjg@chromium.org>

dm: core: Create a new header file for 'compat' features

At present dm/device.h includes the linux-compatible features. This
requires including linux/compat.h which in turn includes a lot of headers.
One of these is malloc.h which we thus end up including in every file in
U-Boot. Apart from the inefficiency of this, it is problematic for sandbox
which needs to use the system malloc() in some files.

Move the compatibility features into a separate header file.

Signed-off-by: Simon Glass <sjg@chromium.org>

# b0ad9b5b 22-Dec-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: clear screen should move cursor to home

On a VT100 terminal <ESC>[2J should be enough to both clear the whole
screen and set the cursor to position (1, 1). But the Linux console does
not behave like this. So send an extra <ESC>[H. For reference see the
console_codes(4) man page.

Add a function description.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 1045315d 14-Nov-2019 Simon Glass <sjg@chromium.org>

common: Move get_ticks() function out of common.h

This function belongs in time.h so move it over and add a comment.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Tom Rini <trini@konsulko.com>

# fe1a81c1 05-Sep-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

doc: UEFI API documentation

Add some more files to the UEFI API documentation.

Correct some Sphinx comments.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 03446987 04-Sep-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: do not set invalid screen mode

EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.SetMode() should return EFI_UNDEFINED if a
screen mode is not available.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Alexander Graf <agraf@csgraf.de>

# 97ea0690 04-Sep-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: cursor positioning

When backspacing in column 0 do no set the column index to ULONG_MAX.
Ensure that the row number is not set to ULONG_MAX even if the row count is
advertised as 0.
Ignore control characters other the 0x08, 0x0a, 0x0d when updating the
column.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 7b51b576 01-Aug-2019 Simon Glass <sjg@chromium.org>

env: Move env_get() to env.h

Move env_get() over to the new header file.

Acked-by: Joe Hershberger <joe.hershberger@ni.com>
Signed-off-by: Simon Glass <sjg@chromium.org>

# 3b435c11 16-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: console incorrectly advertised left logo key

Avoid to signal that the left logo key is pressed, when it is not.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 3b2b2de8 16-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: alternative scan codes for F5, END, HOME

Depending on the key board alternative scan codes are used for F5, END, and
HOME.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 3950f0f8 13-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix SetAttribute()

The SetAttribute() service and the Reset() service of the simple text
output protocol must update the attribute value in the mode information.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 2ad238fc 13-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix SetMode()

Correct the check of the mode number in SetMode() service of the simple
text output protocol.

Clear the screen in SetMode().

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 7eaa900e 06-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: event signaling in ExitBootServices

ExitBootServices() has to stop timer related activity before calling the
events of the EFI_EVENT_GROUP_EXIT_BOOT_SERVICES event group. But our
current implementation was stopping all other events.

All events have to observe the task priority level.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 25e6fb5e 02-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix EnableCursor()

The EnableCursor() service of the simple text output protocol must update
the the CursorVisible field of the output mode.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# b31ca6bf 18-May-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: parameter check OutputString

Check the parameters against NULL.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# acee9652 18-May-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.SetState() correct parameter

KeyToggleState is a pointer according to UEFI spec 2.8.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# bfc2dd53 06-Apr-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: correct CTRL-A - CTRL-Z console input

In the extended text input protocol CTRL-A - CTRL-Z have to be signaled as
Unicode characters a-z or A-Z depending on the shift state and not as 0x01
to 0x1a.

Update Python unit test.

This patch is required for using the EFI shell `edit` command.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# dd1a1ec2 04-Mar-2019 Matthias Brugger <mbrugger@suse.com>

efi_loader: Fix serial console size detection

Function term_read_reply tries to read from the serial console until
the end_char was read. This can hang forever if we are, for some reason,
not able to read the full response (e.g. serial buffer too small,
frame error). This patch moves the timeout detection into
term_read_reply() to assure we will make progress.

Fixes: 6bb591f704 ("efi_loader: query serial console size reliably")
Signed-off-by: Matthias Brugger <mbrugger@suse.com>

Throw missing error when an incomplete reply for the cursor position is
received.

Change type of argument of term_get_char() *s32. This renders the function
reusable in efi_cin_read_key().

Reviewed-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# e1fec152 18-Oct-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix typos

Fix typos in EFI subsystem comments.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# fae0118e 25-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: eliminate handle member

A pointer to a struct efi_object is a handle. We do not need any handle
member in this structure. Let's eliminate it.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 6f566c23 01-Oct-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: return type efi_console_register()

Use a return type that can encompass the return value.

This fixes CoverityScan CID 184090.

Reported-by: Tom Rini <trini@konsulko.com>
Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 6bb591f7 15-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: query serial console size reliably

Not all terminals understand CSI [18t for querying the console size.
We should adhere to escape sequences documented in the console_codes
manpage and the ECMA-48 standard.

So here we follow a different approach. We position the cursor to the
bottom right and query its position. Before leaving the function we
restore the original cursor position.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# aaace4b0 14-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: terminal left upper corner is [1, 1]

For the VT102 and the Linux console the left upper corner is [1, 1].
EFI uses [0, 0] as left upper corner.

Check parameters of the SetCursorPosition() service.

Use CSI H like EDK2 for positioning.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 4fdcf066 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: implement key notify functions

Implement registering and unregistreing key notify functions in the
EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 55fbdf99 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: support modifiers for F1 - F4

Support modifiers for F1 - F4.
Add support for letters with ALT key.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 110c6280 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL

This patch implements the EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.

The implementation of notification functions is postponed to a later
patch.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 0dfd13a4 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: rework event handling for console

Preread the next key in the console timer event.

The EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL requires to trigger registered key
notification functions based on the prefetched key.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 35cbb796 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: support Unicode text input

Up to now the EFI_TEXT_INPUT_PROTOCOL only supported ASCII characters.
With the patch it can consume UTF-8 from the console.

Currently only the serial console and the console can deliver UTF-8.
Local consoles are restricted to ASCII.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 14d103bb 08-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: typos in efi_console.c

Fix a few typos.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 3e603ec7 08-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: struct efi_simple_text_input_protocol

%s/efi_simple_input_interface/efi_simple_text_input_protocol/

We should be consistent in the naming of the EFI protocol interface
structures. The protocol is called EFI_SIMPLE_TEXT_INPUT_PROTOCOL.

%s/ExtendedVerification/extended_verification/

Use consistent naming of function parameters. Do not use CamelCase.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 40e3e757 04-Sep-2018 Alexander Graf <agraf@csgraf.de>

efi_loader: Add console handle references to in/out/err

We register the console in/out protocol on the loaded image handle, but
never tell anyone about it in the systab.

Because of this Shell.efi can not find the input protocol. So let's add
a proper reference.

Signed-off-by: Alexander Graf <agraf@suse.de>
Reviewed-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# ba7bd5c2 31-Aug-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: don't use unlimited stack as buffer

The length of a string printed to the console by the
EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL is not limited by the UEFI spec.
Hence should not allocate a buffer for it on the stack.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 1dde0d57 31-Aug-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: rename utf16_strlen, utf16_strnlen

The function names utf16_strlen() and utf16_strnlen() are misnomers.
The functions do not count utf-16 characters but non-zero words.
So let's rename them to u16_strlen and u16_strnlen().

In utf16_dup() avoid assignment in if clause.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 9d12daff 05-Jul-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.Reset()

Implement the reset service of the EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.

This should resolve the error reported by the SCT in
Protocol/SimpleTextOut/BlackBoxTest/SimpleTextOutBBTestFunction_uefi.c:639

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# e67ff94d 05-Jul-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: clear screen has to reset cursor position

After clearing the screen the cursor position is row 0, column 0.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 4f187897 05-Jul-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_INPUT_PROTOCOL.Reset()

Implement the reset service of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL.

This should resolve the error reported by the SCT in
Protocol/SimpleTextIn/BlackBoxTest/SimpleTextInBBTestFunction.c:193

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 80483b2a 03-Jun-2018 Alexander Graf <agraf@csgraf.de>

efi_loader: Fix warning in raw/cols query

The code to determine rows / cols on the screen could potentially run
into a case where it doesn't know how big the screen is. In that case,
assume 80x25.

Signed-off-by: Alexander Graf <agraf@suse.de>

# 62217295 16-May-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: avoid using unitialized values as console size

If a request for the console size would be answered with a response
with less then three values, uninitialized stack memory would be
copied to the number of rows and columns of the terminal.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# a4aa7bef 29-Apr-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: always check parameters in efi_cout_query_mode()

If we cannot determine the size of the serial terminal we still have
to check the parameters of efi_cout_query_mode().

Querying the size of the serial terminal drains the keyboard buffer.
So make sure we do this during the initialization and not in the midst
of an EFI application.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 7ca7c3c0 29-Apr-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: updating the cursor position

The current coding advances the column by the number of UTF-8 bytes.
The column should be increased by one per unicode character.

The UEFI spec provides advance rules for U+0000, U+0008, U+000A,
and U000D. All other characters, including control characters
U+0007 (bel) and U+0009 (tab), have to increase the column by one.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# f739fcd8 07-May-2018 Tom Rini <trini@konsulko.com>

SPDX: Convert a few files that were missed before

As part of the main conversion a few files were missed. These files had
additional whitespace after the '*' and before the SPDX tag and my
previous regex was too strict. This time I did a grep for all SPDX tags
and then filtered out anything that matched the correct styles.

Fixes: 83d290c56fab ("SPDX: Convert all of our single license tags to Linux Kernel style")
Reported-by: Heinrich Schuchardt <xypron.debian@gmx.de>
Signed-off-by: Tom Rini <trini@konsulko.com>

# b095f3c8 18-Feb-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: implement event groups

If an event of a group event is signaled all other events of the same
group are signaled too.

Function efi_signal_event is renamed to efi_queue_event.
A new function efi_signal_event is introduced that checks if an event
belongs to a group and than signals all events of the group.
Event group notifciation is implemented for ExitBootServices,
InstallConfigurationTable, and ResetSystem.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 0fb4169e 19-Feb-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: correct input of special keys

Don't set unicode_char if scan_code is set.
Add support for page up, page down, and insert.
Correct input of function keys.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 8396e3fd 03-Mar-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: remove deprecated ConsoleControlProtocol

The console control protocol is not defined in the UEFI standard.

It exists in EDK2's EdkCompatiblityPkg package. But this package
is deprecated according to
https://github.com/tianocore/tianocore.github.io/wiki/Differences-between-EDK-and-EDK-II

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 9bc9664d 19-Jan-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: add check_tpl parameter to efi_signal_event

In ExitBootServices we need to signal events irrespective of the current
TPL level. A new parameter check_tpl is added to efi_signal_event().

Function efi_console_timer_notify() gets some comments.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 2074f700 11-Jan-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: consistently use efi_handle_t for handles

We should consistently use the efi_handle_t typedef when
referring to handles.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# ebb4dd5b 26-Oct-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: efi_console: use helper functions

Use helper functions efi_created_handle and efi_add_protocol
for creating the console handles and instaling the respective
protocols.

This change is needed if we want to move from an array of
protocols to a linked list of protocols.

Eliminate EFI_PROTOCOL_OBJECT which is not used anymore.

Currently we have not defined protocol interfaces to be const.
So efi_con_out and efi_console_control cannot be defined as const.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 2d5dc2a5 10-Oct-2017 Rob Clark <robdclark@gmail.com>

efi_loader: console support for color attributes

Shell.efi uses this, and supporting color attributes makes things look
nicer. Map the EFI fg/bg color attributes to ANSI escape sequences.
Not all colors have a perfect match, but spec just says "Devices
supporting a different number of text colors are required to emulate the
above colors to the best of the device’s capabilities".

Signed-off-by: Rob Clark <robdclark@gmail.com>
Tested-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Alexander Graf <agraf@suse.de>
[agraf: s/unsigned/unsigned int/]
Signed-off-by: Alexander Graf <agraf@suse.de>

# e190e897 04-Oct-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: use type bool for event states

Queued and signaled describe boolean states of events.
So let's use type bool and rename the structure members to is_queued
and is_signaled.

Update the comments for is_queued and is_signaled.

Reported-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Rob Clark <robdclark@gmail.com>
Reviewed-by: Rob Clark <robdclark@gmail.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 3a45bc7f 13-Sep-2017 Rob Clark <robdclark@gmail.com>

efi_loader: Some console improvements for vidconsole

1) use fputs() to reduce cache flushes from once-per-char to
once-per-string
2) handle \r, \t, and \b in addition to just \n for tracking
cursor position
3) cursor row/col are zero based, not one based

Signed-off-by: Rob Clark <robdclark@gmail.com>
[agraf: s/unsigned/unsigned int/]
Signed-off-by: Alexander Graf <agraf@suse.de>

# a18c5a83 13-Sep-2017 Rob Clark <robdclark@gmail.com>

efi_loader: Correctly figure out size for vidconsole

If stdout is vidconsole, we cannot rely on ANSI escape sequences to
query the size, as vidconsole cannot reply on stdin. Instead special-
case this if stdout is vidconsole.

Signed-off-by: Rob Clark <robdclark@gmail.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 71cc25c3 13-Sep-2017 Rob Clark <robdclark@gmail.com>

efi_loader: split out escape sequence based size query

We need to do something different for vidconsole, since it cannot
respond to the query on stdin. Prep work for next patch.

Signed-off-by: Rob Clark <robdclark@gmail.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# ca62a4f5 15-Sep-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: implement queueing of the notification function

For the correct implementation of the task priority level (TPL)
calling the notification function must be queued.

Add a status field 'queued' to events.

In function efi_signal_event set status queued if a notification
function exists and reset it after we have called the function.
A later patch will add a check of the TPL here.

In efi_create_event and efi_close_event unset the queued status.

In function efi_wait_for_event and efi_check_event
queue the notification function.

In efi_timer_check call the efi_notify_event
if the status queued is set.
For all timer events set status signaled.

In efi_console_timer_notify set the signaled state of the
WaitForKey event.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 78178bb0 09-Sep-2017 Rob Clark <robdclark@gmail.com>

lib: add some utf16 handling helpers

We'll eventually want these in a few places in efi_loader, and also
vsprintf.

Signed-off-by: Rob Clark <robdclark@gmail.com>

# a17e62cc 24-Jul-2017 Rob Clark <robdclark@gmail.com>

efi_loader: expose protocols via GUID

shim.efi (or rather gnu-efi's LibLocateProtocol() which shim.efi uses)
resolves protocols via efi_locate_handle() so the console protocols
need to be added to the efi object list.

Signed-off-by: Rob Clark <robdclark@gmail.com>
[agraf: whitespace fixes]
Signed-off-by: Alexander Graf <agraf@suse.de>

# ff925938 19-Jul-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_console: use EFIAPI for callback functions

The call to efi_create_event requires notification functions
flagged as EFIAPI.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 91be9a77 18-Jul-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_console: set up events

Set up a timer event and the WaitForKey event.
In the notify function of the timer event check for console input
and signal the WaitForKey event accordingly.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 5be8b0a3 07-Nov-2016 Emmanuel Vadot <manu@bidouilliste.com>

efi_loader: console: Correctly report modes

Add support for EFI console modes.
Mode 0 is always 80x25 and present by EFI specification.
Mode 1 is always 80x50 and not mandatory.
Mode 2 and above is freely usable.

If the terminal can handle mode 1, we mark it as supported.
If the terminal size is greater than mode 0 and different than mode 1,
we install it as mode 2.

Modes can be switch with cout_set_mode.

Changes in V5:
Correctly detect mode before enabling mode 2.

Changes in V4:
Reset cursor positon on mode switch
Use local variables in console query code

Changes in V3:
Valid mode are 0 to EFIMode-1
Fix style

Changes in V2:
Add mode switch
Report only the modes that we support

Signed-off-by: Emmanuel Vadot <manu@bidouilliste.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# c1311ad4 03-Mar-2016 Alexander Graf <agraf@csgraf.de>

efi_loader: Add console interface

One of the basic EFI interfaces is the console interface. Using it an EFI
application can interface with the user. This patch implements an EFI console
interface using getc() and putc().

Today, we only implement text based consoles. We also convert the EFI Unicode
characters to UTF-8 on the fly, hoping that everyone managed to jump on the
train by now.

Signed-off-by: Alexander Graf <agraf@suse.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Tested-by: Simon Glass <sjg@chromium.org>

# e053a144 01-Sep-2022 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: support CTRL+\ - CTRL+_

In the extended text input protocol support input of control letters
0x1c - 0x1f.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
Reviewed-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>

# 0b7b56d7 01-Sep-2022 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: compliance Simple Text Input Ex Protocol

We cannot expect the buffers passed to the input protocols to be zero
filled. If only modifier keys are pressed, we have to return EFI_NOT_READY
but we still have to fill the key structure.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
Reviewed-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>

# 68edbed4 14-Jun-2022 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: initialize console size late

If CONFIG_VIDEO_DM=n we query the display size from the serial console.
Especially when using a remote console the response can be so late that
it interferes with autoboot.

Only query the console size when running an EFI binary.

Add debug output showing the determined console size.

Reported-by: Fabio Estevam <festevam@gmail.com>
Fixes: a57ad20d07e8 ("efi_loader: split efi_init_obj_list() into two stages")
Fixes: a9bf024b2933 ("efi_loader: disk: a helper function to create efi_disk objects from udevice")
Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
Tested-by: Fabio Estevam <festevam@denx.de>
Tested-by: Heiko Thiery <heiko.thiery@gmail.com>

# 5c1037d5 30-Apr-2022 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: reset colors before clearing screen

When resetting the text console the colors have to be set before clearing
the screen. Otherwise the background color may be wrong.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>

# 3c95b323 04-Feb-2022 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: add handle for UART

When loading an EFI binary via the UART we assign a UART device path to it.
But we lack a handle with that device path.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>

# abd62e4d 29-Jun-2021 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: missing parentheses in query_console_size

After if we should use parentheses to keep the code readable.

Fixes: a95f4c885991 ("efi_loader: NULL dereference in EFI console")
Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# a95f4c88 15-Mar-2021 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: NULL dereference in EFI console

Even if CONFIG_DM_VIDEO=y and stdout="vidconsole", a video device may not
be available. Check the return values of the relevant functions.

If no video output device is available, assume that the serial console is
in use.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 997b57fc 28-Feb-2021 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: console size of vidconsole

If stdout is 'vidconsole', we correctly set the console size.
If stdout is 'vidconsole,serial', the video console is ignored.

We should always evaluate the size of vidconsole if it is the primary
console.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 7913c7dc 04-Jan-2021 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: typedef efi_string_t text output protocol

We do not want to use typedefs in U-Boot.

Do not use efi_string_t in the EFI_TEXT_OUTPUT_PROTOCOL.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 9abb01af 27-Dec-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: escape key handling

Up to now the escape key was not correctly detected in UEFI applications.
We had to hit it twice for a single escape to be recognized.

Use a 10 ms delay to detect if we are dealing with the escape key or an
escape sequence.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# c670aeee 07-Oct-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

common: rename getc() to getchar()

The sandbox is built with the SDL2 library with invokes the X11 library
which in turn calls getc(). But getc() in glibc is defined as

int getc(FILE *)

This does not match our definition.

int getc(void)

The sandbox crashes when called with parameter -l.

Rename our library symbol getc() to getchar().

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Tom Rini <trini@konsulko.com>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 325567d3 04-Jun-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: comments EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL

Provide missing comments for the functions implementing the
EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 336d4615 03-Feb-2020 Simon Glass <sjg@chromium.org>

dm: core: Create a new header file for 'compat' features

At present dm/device.h includes the linux-compatible features. This
requires including linux/compat.h which in turn includes a lot of headers.
One of these is malloc.h which we thus end up including in every file in
U-Boot. Apart from the inefficiency of this, it is problematic for sandbox
which needs to use the system malloc() in some files.

Move the compatibility features into a separate header file.

Signed-off-by: Simon Glass <sjg@chromium.org>

# b0ad9b5b 22-Dec-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: clear screen should move cursor to home

On a VT100 terminal <ESC>[2J should be enough to both clear the whole
screen and set the cursor to position (1, 1). But the Linux console does
not behave like this. So send an extra <ESC>[H. For reference see the
console_codes(4) man page.

Add a function description.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 1045315d 14-Nov-2019 Simon Glass <sjg@chromium.org>

common: Move get_ticks() function out of common.h

This function belongs in time.h so move it over and add a comment.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Tom Rini <trini@konsulko.com>

# fe1a81c1 05-Sep-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

doc: UEFI API documentation

Add some more files to the UEFI API documentation.

Correct some Sphinx comments.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 03446987 04-Sep-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: do not set invalid screen mode

EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.SetMode() should return EFI_UNDEFINED if a
screen mode is not available.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Alexander Graf <agraf@csgraf.de>

# 97ea0690 04-Sep-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: cursor positioning

When backspacing in column 0 do no set the column index to ULONG_MAX.
Ensure that the row number is not set to ULONG_MAX even if the row count is
advertised as 0.
Ignore control characters other the 0x08, 0x0a, 0x0d when updating the
column.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 7b51b576 01-Aug-2019 Simon Glass <sjg@chromium.org>

env: Move env_get() to env.h

Move env_get() over to the new header file.

Acked-by: Joe Hershberger <joe.hershberger@ni.com>
Signed-off-by: Simon Glass <sjg@chromium.org>

# 3b435c11 16-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: console incorrectly advertised left logo key

Avoid to signal that the left logo key is pressed, when it is not.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 3b2b2de8 16-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: alternative scan codes for F5, END, HOME

Depending on the key board alternative scan codes are used for F5, END, and
HOME.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 3950f0f8 13-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix SetAttribute()

The SetAttribute() service and the Reset() service of the simple text
output protocol must update the attribute value in the mode information.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 2ad238fc 13-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix SetMode()

Correct the check of the mode number in SetMode() service of the simple
text output protocol.

Clear the screen in SetMode().

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 7eaa900e 06-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: event signaling in ExitBootServices

ExitBootServices() has to stop timer related activity before calling the
events of the EFI_EVENT_GROUP_EXIT_BOOT_SERVICES event group. But our
current implementation was stopping all other events.

All events have to observe the task priority level.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 25e6fb5e 02-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix EnableCursor()

The EnableCursor() service of the simple text output protocol must update
the the CursorVisible field of the output mode.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# b31ca6bf 18-May-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: parameter check OutputString

Check the parameters against NULL.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# acee9652 18-May-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.SetState() correct parameter

KeyToggleState is a pointer according to UEFI spec 2.8.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# bfc2dd53 06-Apr-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: correct CTRL-A - CTRL-Z console input

In the extended text input protocol CTRL-A - CTRL-Z have to be signaled as
Unicode characters a-z or A-Z depending on the shift state and not as 0x01
to 0x1a.

Update Python unit test.

This patch is required for using the EFI shell `edit` command.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# dd1a1ec2 04-Mar-2019 Matthias Brugger <mbrugger@suse.com>

efi_loader: Fix serial console size detection

Function term_read_reply tries to read from the serial console until
the end_char was read. This can hang forever if we are, for some reason,
not able to read the full response (e.g. serial buffer too small,
frame error). This patch moves the timeout detection into
term_read_reply() to assure we will make progress.

Fixes: 6bb591f704 ("efi_loader: query serial console size reliably")
Signed-off-by: Matthias Brugger <mbrugger@suse.com>

Throw missing error when an incomplete reply for the cursor position is
received.

Change type of argument of term_get_char() *s32. This renders the function
reusable in efi_cin_read_key().

Reviewed-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# e1fec152 18-Oct-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix typos

Fix typos in EFI subsystem comments.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# fae0118e 25-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: eliminate handle member

A pointer to a struct efi_object is a handle. We do not need any handle
member in this structure. Let's eliminate it.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 6f566c23 01-Oct-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: return type efi_console_register()

Use a return type that can encompass the return value.

This fixes CoverityScan CID 184090.

Reported-by: Tom Rini <trini@konsulko.com>
Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 6bb591f7 15-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: query serial console size reliably

Not all terminals understand CSI [18t for querying the console size.
We should adhere to escape sequences documented in the console_codes
manpage and the ECMA-48 standard.

So here we follow a different approach. We position the cursor to the
bottom right and query its position. Before leaving the function we
restore the original cursor position.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# aaace4b0 14-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: terminal left upper corner is [1, 1]

For the VT102 and the Linux console the left upper corner is [1, 1].
EFI uses [0, 0] as left upper corner.

Check parameters of the SetCursorPosition() service.

Use CSI H like EDK2 for positioning.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 4fdcf066 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: implement key notify functions

Implement registering and unregistreing key notify functions in the
EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 55fbdf99 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: support modifiers for F1 - F4

Support modifiers for F1 - F4.
Add support for letters with ALT key.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 110c6280 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL

This patch implements the EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.

The implementation of notification functions is postponed to a later
patch.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 0dfd13a4 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: rework event handling for console

Preread the next key in the console timer event.

The EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL requires to trigger registered key
notification functions based on the prefetched key.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 35cbb796 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: support Unicode text input

Up to now the EFI_TEXT_INPUT_PROTOCOL only supported ASCII characters.
With the patch it can consume UTF-8 from the console.

Currently only the serial console and the console can deliver UTF-8.
Local consoles are restricted to ASCII.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 14d103bb 08-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: typos in efi_console.c

Fix a few typos.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 3e603ec7 08-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: struct efi_simple_text_input_protocol

%s/efi_simple_input_interface/efi_simple_text_input_protocol/

We should be consistent in the naming of the EFI protocol interface
structures. The protocol is called EFI_SIMPLE_TEXT_INPUT_PROTOCOL.

%s/ExtendedVerification/extended_verification/

Use consistent naming of function parameters. Do not use CamelCase.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 40e3e757 04-Sep-2018 Alexander Graf <agraf@csgraf.de>

efi_loader: Add console handle references to in/out/err

We register the console in/out protocol on the loaded image handle, but
never tell anyone about it in the systab.

Because of this Shell.efi can not find the input protocol. So let's add
a proper reference.

Signed-off-by: Alexander Graf <agraf@suse.de>
Reviewed-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# ba7bd5c2 31-Aug-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: don't use unlimited stack as buffer

The length of a string printed to the console by the
EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL is not limited by the UEFI spec.
Hence should not allocate a buffer for it on the stack.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 1dde0d57 31-Aug-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: rename utf16_strlen, utf16_strnlen

The function names utf16_strlen() and utf16_strnlen() are misnomers.
The functions do not count utf-16 characters but non-zero words.
So let's rename them to u16_strlen and u16_strnlen().

In utf16_dup() avoid assignment in if clause.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 9d12daff 05-Jul-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.Reset()

Implement the reset service of the EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.

This should resolve the error reported by the SCT in
Protocol/SimpleTextOut/BlackBoxTest/SimpleTextOutBBTestFunction_uefi.c:639

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# e67ff94d 05-Jul-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: clear screen has to reset cursor position

After clearing the screen the cursor position is row 0, column 0.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 4f187897 05-Jul-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_INPUT_PROTOCOL.Reset()

Implement the reset service of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL.

This should resolve the error reported by the SCT in
Protocol/SimpleTextIn/BlackBoxTest/SimpleTextInBBTestFunction.c:193

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 80483b2a 03-Jun-2018 Alexander Graf <agraf@csgraf.de>

efi_loader: Fix warning in raw/cols query

The code to determine rows / cols on the screen could potentially run
into a case where it doesn't know how big the screen is. In that case,
assume 80x25.

Signed-off-by: Alexander Graf <agraf@suse.de>

# 62217295 16-May-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: avoid using unitialized values as console size

If a request for the console size would be answered with a response
with less then three values, uninitialized stack memory would be
copied to the number of rows and columns of the terminal.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# a4aa7bef 29-Apr-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: always check parameters in efi_cout_query_mode()

If we cannot determine the size of the serial terminal we still have
to check the parameters of efi_cout_query_mode().

Querying the size of the serial terminal drains the keyboard buffer.
So make sure we do this during the initialization and not in the midst
of an EFI application.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 7ca7c3c0 29-Apr-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: updating the cursor position

The current coding advances the column by the number of UTF-8 bytes.
The column should be increased by one per unicode character.

The UEFI spec provides advance rules for U+0000, U+0008, U+000A,
and U000D. All other characters, including control characters
U+0007 (bel) and U+0009 (tab), have to increase the column by one.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# f739fcd8 07-May-2018 Tom Rini <trini@konsulko.com>

SPDX: Convert a few files that were missed before

As part of the main conversion a few files were missed. These files had
additional whitespace after the '*' and before the SPDX tag and my
previous regex was too strict. This time I did a grep for all SPDX tags
and then filtered out anything that matched the correct styles.

Fixes: 83d290c56fab ("SPDX: Convert all of our single license tags to Linux Kernel style")
Reported-by: Heinrich Schuchardt <xypron.debian@gmx.de>
Signed-off-by: Tom Rini <trini@konsulko.com>

# b095f3c8 18-Feb-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: implement event groups

If an event of a group event is signaled all other events of the same
group are signaled too.

Function efi_signal_event is renamed to efi_queue_event.
A new function efi_signal_event is introduced that checks if an event
belongs to a group and than signals all events of the group.
Event group notifciation is implemented for ExitBootServices,
InstallConfigurationTable, and ResetSystem.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 0fb4169e 19-Feb-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: correct input of special keys

Don't set unicode_char if scan_code is set.
Add support for page up, page down, and insert.
Correct input of function keys.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 8396e3fd 03-Mar-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: remove deprecated ConsoleControlProtocol

The console control protocol is not defined in the UEFI standard.

It exists in EDK2's EdkCompatiblityPkg package. But this package
is deprecated according to
https://github.com/tianocore/tianocore.github.io/wiki/Differences-between-EDK-and-EDK-II

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 9bc9664d 19-Jan-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: add check_tpl parameter to efi_signal_event

In ExitBootServices we need to signal events irrespective of the current
TPL level. A new parameter check_tpl is added to efi_signal_event().

Function efi_console_timer_notify() gets some comments.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 2074f700 11-Jan-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: consistently use efi_handle_t for handles

We should consistently use the efi_handle_t typedef when
referring to handles.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# ebb4dd5b 26-Oct-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: efi_console: use helper functions

Use helper functions efi_created_handle and efi_add_protocol
for creating the console handles and instaling the respective
protocols.

This change is needed if we want to move from an array of
protocols to a linked list of protocols.

Eliminate EFI_PROTOCOL_OBJECT which is not used anymore.

Currently we have not defined protocol interfaces to be const.
So efi_con_out and efi_console_control cannot be defined as const.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 2d5dc2a5 10-Oct-2017 Rob Clark <robdclark@gmail.com>

efi_loader: console support for color attributes

Shell.efi uses this, and supporting color attributes makes things look
nicer. Map the EFI fg/bg color attributes to ANSI escape sequences.
Not all colors have a perfect match, but spec just says "Devices
supporting a different number of text colors are required to emulate the
above colors to the best of the device’s capabilities".

Signed-off-by: Rob Clark <robdclark@gmail.com>
Tested-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Alexander Graf <agraf@suse.de>
[agraf: s/unsigned/unsigned int/]
Signed-off-by: Alexander Graf <agraf@suse.de>

# e190e897 04-Oct-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: use type bool for event states

Queued and signaled describe boolean states of events.
So let's use type bool and rename the structure members to is_queued
and is_signaled.

Update the comments for is_queued and is_signaled.

Reported-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Rob Clark <robdclark@gmail.com>
Reviewed-by: Rob Clark <robdclark@gmail.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 3a45bc7f 13-Sep-2017 Rob Clark <robdclark@gmail.com>

efi_loader: Some console improvements for vidconsole

1) use fputs() to reduce cache flushes from once-per-char to
once-per-string
2) handle \r, \t, and \b in addition to just \n for tracking
cursor position
3) cursor row/col are zero based, not one based

Signed-off-by: Rob Clark <robdclark@gmail.com>
[agraf: s/unsigned/unsigned int/]
Signed-off-by: Alexander Graf <agraf@suse.de>

# a18c5a83 13-Sep-2017 Rob Clark <robdclark@gmail.com>

efi_loader: Correctly figure out size for vidconsole

If stdout is vidconsole, we cannot rely on ANSI escape sequences to
query the size, as vidconsole cannot reply on stdin. Instead special-
case this if stdout is vidconsole.

Signed-off-by: Rob Clark <robdclark@gmail.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 71cc25c3 13-Sep-2017 Rob Clark <robdclark@gmail.com>

efi_loader: split out escape sequence based size query

We need to do something different for vidconsole, since it cannot
respond to the query on stdin. Prep work for next patch.

Signed-off-by: Rob Clark <robdclark@gmail.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# ca62a4f5 15-Sep-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: implement queueing of the notification function

For the correct implementation of the task priority level (TPL)
calling the notification function must be queued.

Add a status field 'queued' to events.

In function efi_signal_event set status queued if a notification
function exists and reset it after we have called the function.
A later patch will add a check of the TPL here.

In efi_create_event and efi_close_event unset the queued status.

In function efi_wait_for_event and efi_check_event
queue the notification function.

In efi_timer_check call the efi_notify_event
if the status queued is set.
For all timer events set status signaled.

In efi_console_timer_notify set the signaled state of the
WaitForKey event.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 78178bb0 09-Sep-2017 Rob Clark <robdclark@gmail.com>

lib: add some utf16 handling helpers

We'll eventually want these in a few places in efi_loader, and also
vsprintf.

Signed-off-by: Rob Clark <robdclark@gmail.com>

# a17e62cc 24-Jul-2017 Rob Clark <robdclark@gmail.com>

efi_loader: expose protocols via GUID

shim.efi (or rather gnu-efi's LibLocateProtocol() which shim.efi uses)
resolves protocols via efi_locate_handle() so the console protocols
need to be added to the efi object list.

Signed-off-by: Rob Clark <robdclark@gmail.com>
[agraf: whitespace fixes]
Signed-off-by: Alexander Graf <agraf@suse.de>

# ff925938 19-Jul-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_console: use EFIAPI for callback functions

The call to efi_create_event requires notification functions
flagged as EFIAPI.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 91be9a77 18-Jul-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_console: set up events

Set up a timer event and the WaitForKey event.
In the notify function of the timer event check for console input
and signal the WaitForKey event accordingly.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 5be8b0a3 07-Nov-2016 Emmanuel Vadot <manu@bidouilliste.com>

efi_loader: console: Correctly report modes

Add support for EFI console modes.
Mode 0 is always 80x25 and present by EFI specification.
Mode 1 is always 80x50 and not mandatory.
Mode 2 and above is freely usable.

If the terminal can handle mode 1, we mark it as supported.
If the terminal size is greater than mode 0 and different than mode 1,
we install it as mode 2.

Modes can be switch with cout_set_mode.

Changes in V5:
Correctly detect mode before enabling mode 2.

Changes in V4:
Reset cursor positon on mode switch
Use local variables in console query code

Changes in V3:
Valid mode are 0 to EFIMode-1
Fix style

Changes in V2:
Add mode switch
Report only the modes that we support

Signed-off-by: Emmanuel Vadot <manu@bidouilliste.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# c1311ad4 03-Mar-2016 Alexander Graf <agraf@csgraf.de>

efi_loader: Add console interface

One of the basic EFI interfaces is the console interface. Using it an EFI
application can interface with the user. This patch implements an EFI console
interface using getc() and putc().

Today, we only implement text based consoles. We also convert the EFI Unicode
characters to UTF-8 on the fly, hoping that everyone managed to jump on the
train by now.

Signed-off-by: Alexander Graf <agraf@suse.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Tested-by: Simon Glass <sjg@chromium.org>

# 68edbed4 14-Jun-2022 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: initialize console size late

If CONFIG_VIDEO_DM=n we query the display size from the serial console.
Especially when using a remote console the response can be so late that
it interferes with autoboot.

Only query the console size when running an EFI binary.

Add debug output showing the determined console size.

Reported-by: Fabio Estevam <festevam@gmail.com>
Fixes: a57ad20d07e8 ("efi_loader: split efi_init_obj_list() into two stages")
Fixes: a9bf024b2933 ("efi_loader: disk: a helper function to create efi_disk objects from udevice")
Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
Tested-by: Fabio Estevam <festevam@denx.de>
Tested-by: Heiko Thiery <heiko.thiery@gmail.com>

# 5c1037d5 30-Apr-2022 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: reset colors before clearing screen

When resetting the text console the colors have to be set before clearing
the screen. Otherwise the background color may be wrong.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>

# 3c95b323 04-Feb-2022 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: add handle for UART

When loading an EFI binary via the UART we assign a UART device path to it.
But we lack a handle with that device path.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>

# abd62e4d 29-Jun-2021 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: missing parentheses in query_console_size

After if we should use parentheses to keep the code readable.

Fixes: a95f4c885991 ("efi_loader: NULL dereference in EFI console")
Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# a95f4c88 15-Mar-2021 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: NULL dereference in EFI console

Even if CONFIG_DM_VIDEO=y and stdout="vidconsole", a video device may not
be available. Check the return values of the relevant functions.

If no video output device is available, assume that the serial console is
in use.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 997b57fc 28-Feb-2021 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: console size of vidconsole

If stdout is 'vidconsole', we correctly set the console size.
If stdout is 'vidconsole,serial', the video console is ignored.

We should always evaluate the size of vidconsole if it is the primary
console.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 7913c7dc 04-Jan-2021 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: typedef efi_string_t text output protocol

We do not want to use typedefs in U-Boot.

Do not use efi_string_t in the EFI_TEXT_OUTPUT_PROTOCOL.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 9abb01af 27-Dec-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: escape key handling

Up to now the escape key was not correctly detected in UEFI applications.
We had to hit it twice for a single escape to be recognized.

Use a 10 ms delay to detect if we are dealing with the escape key or an
escape sequence.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# c670aeee 07-Oct-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

common: rename getc() to getchar()

The sandbox is built with the SDL2 library with invokes the X11 library
which in turn calls getc(). But getc() in glibc is defined as

int getc(FILE *)

This does not match our definition.

int getc(void)

The sandbox crashes when called with parameter -l.

Rename our library symbol getc() to getchar().

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Tom Rini <trini@konsulko.com>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 325567d3 04-Jun-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: comments EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL

Provide missing comments for the functions implementing the
EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 336d4615 03-Feb-2020 Simon Glass <sjg@chromium.org>

dm: core: Create a new header file for 'compat' features

At present dm/device.h includes the linux-compatible features. This
requires including linux/compat.h which in turn includes a lot of headers.
One of these is malloc.h which we thus end up including in every file in
U-Boot. Apart from the inefficiency of this, it is problematic for sandbox
which needs to use the system malloc() in some files.

Move the compatibility features into a separate header file.

Signed-off-by: Simon Glass <sjg@chromium.org>

# b0ad9b5b 22-Dec-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: clear screen should move cursor to home

On a VT100 terminal <ESC>[2J should be enough to both clear the whole
screen and set the cursor to position (1, 1). But the Linux console does
not behave like this. So send an extra <ESC>[H. For reference see the
console_codes(4) man page.

Add a function description.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 1045315d 14-Nov-2019 Simon Glass <sjg@chromium.org>

common: Move get_ticks() function out of common.h

This function belongs in time.h so move it over and add a comment.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Tom Rini <trini@konsulko.com>

# fe1a81c1 05-Sep-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

doc: UEFI API documentation

Add some more files to the UEFI API documentation.

Correct some Sphinx comments.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 03446987 04-Sep-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: do not set invalid screen mode

EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.SetMode() should return EFI_UNDEFINED if a
screen mode is not available.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Alexander Graf <agraf@csgraf.de>

# 97ea0690 04-Sep-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: cursor positioning

When backspacing in column 0 do no set the column index to ULONG_MAX.
Ensure that the row number is not set to ULONG_MAX even if the row count is
advertised as 0.
Ignore control characters other the 0x08, 0x0a, 0x0d when updating the
column.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 7b51b576 01-Aug-2019 Simon Glass <sjg@chromium.org>

env: Move env_get() to env.h

Move env_get() over to the new header file.

Acked-by: Joe Hershberger <joe.hershberger@ni.com>
Signed-off-by: Simon Glass <sjg@chromium.org>

# 3b435c11 16-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: console incorrectly advertised left logo key

Avoid to signal that the left logo key is pressed, when it is not.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 3b2b2de8 16-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: alternative scan codes for F5, END, HOME

Depending on the key board alternative scan codes are used for F5, END, and
HOME.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 3950f0f8 13-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix SetAttribute()

The SetAttribute() service and the Reset() service of the simple text
output protocol must update the attribute value in the mode information.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 2ad238fc 13-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix SetMode()

Correct the check of the mode number in SetMode() service of the simple
text output protocol.

Clear the screen in SetMode().

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 7eaa900e 06-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: event signaling in ExitBootServices

ExitBootServices() has to stop timer related activity before calling the
events of the EFI_EVENT_GROUP_EXIT_BOOT_SERVICES event group. But our
current implementation was stopping all other events.

All events have to observe the task priority level.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 25e6fb5e 02-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix EnableCursor()

The EnableCursor() service of the simple text output protocol must update
the the CursorVisible field of the output mode.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# b31ca6bf 18-May-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: parameter check OutputString

Check the parameters against NULL.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# acee9652 18-May-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.SetState() correct parameter

KeyToggleState is a pointer according to UEFI spec 2.8.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# bfc2dd53 06-Apr-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: correct CTRL-A - CTRL-Z console input

In the extended text input protocol CTRL-A - CTRL-Z have to be signaled as
Unicode characters a-z or A-Z depending on the shift state and not as 0x01
to 0x1a.

Update Python unit test.

This patch is required for using the EFI shell `edit` command.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# dd1a1ec2 04-Mar-2019 Matthias Brugger <mbrugger@suse.com>

efi_loader: Fix serial console size detection

Function term_read_reply tries to read from the serial console until
the end_char was read. This can hang forever if we are, for some reason,
not able to read the full response (e.g. serial buffer too small,
frame error). This patch moves the timeout detection into
term_read_reply() to assure we will make progress.

Fixes: 6bb591f704 ("efi_loader: query serial console size reliably")
Signed-off-by: Matthias Brugger <mbrugger@suse.com>

Throw missing error when an incomplete reply for the cursor position is
received.

Change type of argument of term_get_char() *s32. This renders the function
reusable in efi_cin_read_key().

Reviewed-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# e1fec152 18-Oct-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix typos

Fix typos in EFI subsystem comments.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# fae0118e 25-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: eliminate handle member

A pointer to a struct efi_object is a handle. We do not need any handle
member in this structure. Let's eliminate it.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 6f566c23 01-Oct-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: return type efi_console_register()

Use a return type that can encompass the return value.

This fixes CoverityScan CID 184090.

Reported-by: Tom Rini <trini@konsulko.com>
Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 6bb591f7 15-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: query serial console size reliably

Not all terminals understand CSI [18t for querying the console size.
We should adhere to escape sequences documented in the console_codes
manpage and the ECMA-48 standard.

So here we follow a different approach. We position the cursor to the
bottom right and query its position. Before leaving the function we
restore the original cursor position.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# aaace4b0 14-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: terminal left upper corner is [1, 1]

For the VT102 and the Linux console the left upper corner is [1, 1].
EFI uses [0, 0] as left upper corner.

Check parameters of the SetCursorPosition() service.

Use CSI H like EDK2 for positioning.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 4fdcf066 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: implement key notify functions

Implement registering and unregistreing key notify functions in the
EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 55fbdf99 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: support modifiers for F1 - F4

Support modifiers for F1 - F4.
Add support for letters with ALT key.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 110c6280 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL

This patch implements the EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.

The implementation of notification functions is postponed to a later
patch.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 0dfd13a4 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: rework event handling for console

Preread the next key in the console timer event.

The EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL requires to trigger registered key
notification functions based on the prefetched key.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 35cbb796 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: support Unicode text input

Up to now the EFI_TEXT_INPUT_PROTOCOL only supported ASCII characters.
With the patch it can consume UTF-8 from the console.

Currently only the serial console and the console can deliver UTF-8.
Local consoles are restricted to ASCII.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 14d103bb 08-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: typos in efi_console.c

Fix a few typos.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 3e603ec7 08-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: struct efi_simple_text_input_protocol

%s/efi_simple_input_interface/efi_simple_text_input_protocol/

We should be consistent in the naming of the EFI protocol interface
structures. The protocol is called EFI_SIMPLE_TEXT_INPUT_PROTOCOL.

%s/ExtendedVerification/extended_verification/

Use consistent naming of function parameters. Do not use CamelCase.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 40e3e757 04-Sep-2018 Alexander Graf <agraf@csgraf.de>

efi_loader: Add console handle references to in/out/err

We register the console in/out protocol on the loaded image handle, but
never tell anyone about it in the systab.

Because of this Shell.efi can not find the input protocol. So let's add
a proper reference.

Signed-off-by: Alexander Graf <agraf@suse.de>
Reviewed-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# ba7bd5c2 31-Aug-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: don't use unlimited stack as buffer

The length of a string printed to the console by the
EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL is not limited by the UEFI spec.
Hence should not allocate a buffer for it on the stack.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 1dde0d57 31-Aug-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: rename utf16_strlen, utf16_strnlen

The function names utf16_strlen() and utf16_strnlen() are misnomers.
The functions do not count utf-16 characters but non-zero words.
So let's rename them to u16_strlen and u16_strnlen().

In utf16_dup() avoid assignment in if clause.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 9d12daff 05-Jul-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.Reset()

Implement the reset service of the EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.

This should resolve the error reported by the SCT in
Protocol/SimpleTextOut/BlackBoxTest/SimpleTextOutBBTestFunction_uefi.c:639

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# e67ff94d 05-Jul-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: clear screen has to reset cursor position

After clearing the screen the cursor position is row 0, column 0.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 4f187897 05-Jul-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_INPUT_PROTOCOL.Reset()

Implement the reset service of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL.

This should resolve the error reported by the SCT in
Protocol/SimpleTextIn/BlackBoxTest/SimpleTextInBBTestFunction.c:193

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 80483b2a 03-Jun-2018 Alexander Graf <agraf@csgraf.de>

efi_loader: Fix warning in raw/cols query

The code to determine rows / cols on the screen could potentially run
into a case where it doesn't know how big the screen is. In that case,
assume 80x25.

Signed-off-by: Alexander Graf <agraf@suse.de>

# 62217295 16-May-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: avoid using unitialized values as console size

If a request for the console size would be answered with a response
with less then three values, uninitialized stack memory would be
copied to the number of rows and columns of the terminal.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# a4aa7bef 29-Apr-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: always check parameters in efi_cout_query_mode()

If we cannot determine the size of the serial terminal we still have
to check the parameters of efi_cout_query_mode().

Querying the size of the serial terminal drains the keyboard buffer.
So make sure we do this during the initialization and not in the midst
of an EFI application.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 7ca7c3c0 29-Apr-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: updating the cursor position

The current coding advances the column by the number of UTF-8 bytes.
The column should be increased by one per unicode character.

The UEFI spec provides advance rules for U+0000, U+0008, U+000A,
and U000D. All other characters, including control characters
U+0007 (bel) and U+0009 (tab), have to increase the column by one.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# f739fcd8 07-May-2018 Tom Rini <trini@konsulko.com>

SPDX: Convert a few files that were missed before

As part of the main conversion a few files were missed. These files had
additional whitespace after the '*' and before the SPDX tag and my
previous regex was too strict. This time I did a grep for all SPDX tags
and then filtered out anything that matched the correct styles.

Fixes: 83d290c56fab ("SPDX: Convert all of our single license tags to Linux Kernel style")
Reported-by: Heinrich Schuchardt <xypron.debian@gmx.de>
Signed-off-by: Tom Rini <trini@konsulko.com>

# b095f3c8 18-Feb-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: implement event groups

If an event of a group event is signaled all other events of the same
group are signaled too.

Function efi_signal_event is renamed to efi_queue_event.
A new function efi_signal_event is introduced that checks if an event
belongs to a group and than signals all events of the group.
Event group notifciation is implemented for ExitBootServices,
InstallConfigurationTable, and ResetSystem.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 0fb4169e 19-Feb-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: correct input of special keys

Don't set unicode_char if scan_code is set.
Add support for page up, page down, and insert.
Correct input of function keys.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 8396e3fd 03-Mar-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: remove deprecated ConsoleControlProtocol

The console control protocol is not defined in the UEFI standard.

It exists in EDK2's EdkCompatiblityPkg package. But this package
is deprecated according to
https://github.com/tianocore/tianocore.github.io/wiki/Differences-between-EDK-and-EDK-II

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 9bc9664d 19-Jan-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: add check_tpl parameter to efi_signal_event

In ExitBootServices we need to signal events irrespective of the current
TPL level. A new parameter check_tpl is added to efi_signal_event().

Function efi_console_timer_notify() gets some comments.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 2074f700 11-Jan-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: consistently use efi_handle_t for handles

We should consistently use the efi_handle_t typedef when
referring to handles.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# ebb4dd5b 26-Oct-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: efi_console: use helper functions

Use helper functions efi_created_handle and efi_add_protocol
for creating the console handles and instaling the respective
protocols.

This change is needed if we want to move from an array of
protocols to a linked list of protocols.

Eliminate EFI_PROTOCOL_OBJECT which is not used anymore.

Currently we have not defined protocol interfaces to be const.
So efi_con_out and efi_console_control cannot be defined as const.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 2d5dc2a5 10-Oct-2017 Rob Clark <robdclark@gmail.com>

efi_loader: console support for color attributes

Shell.efi uses this, and supporting color attributes makes things look
nicer. Map the EFI fg/bg color attributes to ANSI escape sequences.
Not all colors have a perfect match, but spec just says "Devices
supporting a different number of text colors are required to emulate the
above colors to the best of the device’s capabilities".

Signed-off-by: Rob Clark <robdclark@gmail.com>
Tested-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Alexander Graf <agraf@suse.de>
[agraf: s/unsigned/unsigned int/]
Signed-off-by: Alexander Graf <agraf@suse.de>

# e190e897 04-Oct-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: use type bool for event states

Queued and signaled describe boolean states of events.
So let's use type bool and rename the structure members to is_queued
and is_signaled.

Update the comments for is_queued and is_signaled.

Reported-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Rob Clark <robdclark@gmail.com>
Reviewed-by: Rob Clark <robdclark@gmail.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 3a45bc7f 13-Sep-2017 Rob Clark <robdclark@gmail.com>

efi_loader: Some console improvements for vidconsole

1) use fputs() to reduce cache flushes from once-per-char to
once-per-string
2) handle \r, \t, and \b in addition to just \n for tracking
cursor position
3) cursor row/col are zero based, not one based

Signed-off-by: Rob Clark <robdclark@gmail.com>
[agraf: s/unsigned/unsigned int/]
Signed-off-by: Alexander Graf <agraf@suse.de>

# a18c5a83 13-Sep-2017 Rob Clark <robdclark@gmail.com>

efi_loader: Correctly figure out size for vidconsole

If stdout is vidconsole, we cannot rely on ANSI escape sequences to
query the size, as vidconsole cannot reply on stdin. Instead special-
case this if stdout is vidconsole.

Signed-off-by: Rob Clark <robdclark@gmail.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 71cc25c3 13-Sep-2017 Rob Clark <robdclark@gmail.com>

efi_loader: split out escape sequence based size query

We need to do something different for vidconsole, since it cannot
respond to the query on stdin. Prep work for next patch.

Signed-off-by: Rob Clark <robdclark@gmail.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# ca62a4f5 15-Sep-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: implement queueing of the notification function

For the correct implementation of the task priority level (TPL)
calling the notification function must be queued.

Add a status field 'queued' to events.

In function efi_signal_event set status queued if a notification
function exists and reset it after we have called the function.
A later patch will add a check of the TPL here.

In efi_create_event and efi_close_event unset the queued status.

In function efi_wait_for_event and efi_check_event
queue the notification function.

In efi_timer_check call the efi_notify_event
if the status queued is set.
For all timer events set status signaled.

In efi_console_timer_notify set the signaled state of the
WaitForKey event.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 78178bb0 09-Sep-2017 Rob Clark <robdclark@gmail.com>

lib: add some utf16 handling helpers

We'll eventually want these in a few places in efi_loader, and also
vsprintf.

Signed-off-by: Rob Clark <robdclark@gmail.com>

# a17e62cc 24-Jul-2017 Rob Clark <robdclark@gmail.com>

efi_loader: expose protocols via GUID

shim.efi (or rather gnu-efi's LibLocateProtocol() which shim.efi uses)
resolves protocols via efi_locate_handle() so the console protocols
need to be added to the efi object list.

Signed-off-by: Rob Clark <robdclark@gmail.com>
[agraf: whitespace fixes]
Signed-off-by: Alexander Graf <agraf@suse.de>

# ff925938 19-Jul-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_console: use EFIAPI for callback functions

The call to efi_create_event requires notification functions
flagged as EFIAPI.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 91be9a77 18-Jul-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_console: set up events

Set up a timer event and the WaitForKey event.
In the notify function of the timer event check for console input
and signal the WaitForKey event accordingly.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 5be8b0a3 07-Nov-2016 Emmanuel Vadot <manu@bidouilliste.com>

efi_loader: console: Correctly report modes

Add support for EFI console modes.
Mode 0 is always 80x25 and present by EFI specification.
Mode 1 is always 80x50 and not mandatory.
Mode 2 and above is freely usable.

If the terminal can handle mode 1, we mark it as supported.
If the terminal size is greater than mode 0 and different than mode 1,
we install it as mode 2.

Modes can be switch with cout_set_mode.

Changes in V5:
Correctly detect mode before enabling mode 2.

Changes in V4:
Reset cursor positon on mode switch
Use local variables in console query code

Changes in V3:
Valid mode are 0 to EFIMode-1
Fix style

Changes in V2:
Add mode switch
Report only the modes that we support

Signed-off-by: Emmanuel Vadot <manu@bidouilliste.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# c1311ad4 03-Mar-2016 Alexander Graf <agraf@csgraf.de>

efi_loader: Add console interface

One of the basic EFI interfaces is the console interface. Using it an EFI
application can interface with the user. This patch implements an EFI console
interface using getc() and putc().

Today, we only implement text based consoles. We also convert the EFI Unicode
characters to UTF-8 on the fly, hoping that everyone managed to jump on the
train by now.

Signed-off-by: Alexander Graf <agraf@suse.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Tested-by: Simon Glass <sjg@chromium.org>

# 5c1037d5 30-Apr-2022 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: reset colors before clearing screen

When resetting the text console the colors have to be set before clearing
the screen. Otherwise the background color may be wrong.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>

# 3c95b323 04-Feb-2022 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: add handle for UART

When loading an EFI binary via the UART we assign a UART device path to it.
But we lack a handle with that device path.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>

# abd62e4d 29-Jun-2021 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: missing parentheses in query_console_size

After if we should use parentheses to keep the code readable.

Fixes: a95f4c885991 ("efi_loader: NULL dereference in EFI console")
Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# a95f4c88 15-Mar-2021 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: NULL dereference in EFI console

Even if CONFIG_DM_VIDEO=y and stdout="vidconsole", a video device may not
be available. Check the return values of the relevant functions.

If no video output device is available, assume that the serial console is
in use.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 997b57fc 28-Feb-2021 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: console size of vidconsole

If stdout is 'vidconsole', we correctly set the console size.
If stdout is 'vidconsole,serial', the video console is ignored.

We should always evaluate the size of vidconsole if it is the primary
console.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 7913c7dc 04-Jan-2021 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: typedef efi_string_t text output protocol

We do not want to use typedefs in U-Boot.

Do not use efi_string_t in the EFI_TEXT_OUTPUT_PROTOCOL.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 9abb01af 27-Dec-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: escape key handling

Up to now the escape key was not correctly detected in UEFI applications.
We had to hit it twice for a single escape to be recognized.

Use a 10 ms delay to detect if we are dealing with the escape key or an
escape sequence.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# c670aeee 07-Oct-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

common: rename getc() to getchar()

The sandbox is built with the SDL2 library with invokes the X11 library
which in turn calls getc(). But getc() in glibc is defined as

int getc(FILE *)

This does not match our definition.

int getc(void)

The sandbox crashes when called with parameter -l.

Rename our library symbol getc() to getchar().

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Tom Rini <trini@konsulko.com>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 325567d3 04-Jun-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: comments EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL

Provide missing comments for the functions implementing the
EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 336d4615 03-Feb-2020 Simon Glass <sjg@chromium.org>

dm: core: Create a new header file for 'compat' features

At present dm/device.h includes the linux-compatible features. This
requires including linux/compat.h which in turn includes a lot of headers.
One of these is malloc.h which we thus end up including in every file in
U-Boot. Apart from the inefficiency of this, it is problematic for sandbox
which needs to use the system malloc() in some files.

Move the compatibility features into a separate header file.

Signed-off-by: Simon Glass <sjg@chromium.org>

# b0ad9b5b 22-Dec-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: clear screen should move cursor to home

On a VT100 terminal <ESC>[2J should be enough to both clear the whole
screen and set the cursor to position (1, 1). But the Linux console does
not behave like this. So send an extra <ESC>[H. For reference see the
console_codes(4) man page.

Add a function description.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 1045315d 14-Nov-2019 Simon Glass <sjg@chromium.org>

common: Move get_ticks() function out of common.h

This function belongs in time.h so move it over and add a comment.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Tom Rini <trini@konsulko.com>

# fe1a81c1 05-Sep-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

doc: UEFI API documentation

Add some more files to the UEFI API documentation.

Correct some Sphinx comments.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 03446987 04-Sep-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: do not set invalid screen mode

EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.SetMode() should return EFI_UNDEFINED if a
screen mode is not available.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Alexander Graf <agraf@csgraf.de>

# 97ea0690 04-Sep-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: cursor positioning

When backspacing in column 0 do no set the column index to ULONG_MAX.
Ensure that the row number is not set to ULONG_MAX even if the row count is
advertised as 0.
Ignore control characters other the 0x08, 0x0a, 0x0d when updating the
column.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 7b51b576 01-Aug-2019 Simon Glass <sjg@chromium.org>

env: Move env_get() to env.h

Move env_get() over to the new header file.

Acked-by: Joe Hershberger <joe.hershberger@ni.com>
Signed-off-by: Simon Glass <sjg@chromium.org>

# 3b435c11 16-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: console incorrectly advertised left logo key

Avoid to signal that the left logo key is pressed, when it is not.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 3b2b2de8 16-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: alternative scan codes for F5, END, HOME

Depending on the key board alternative scan codes are used for F5, END, and
HOME.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 3950f0f8 13-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix SetAttribute()

The SetAttribute() service and the Reset() service of the simple text
output protocol must update the attribute value in the mode information.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 2ad238fc 13-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix SetMode()

Correct the check of the mode number in SetMode() service of the simple
text output protocol.

Clear the screen in SetMode().

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 7eaa900e 06-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: event signaling in ExitBootServices

ExitBootServices() has to stop timer related activity before calling the
events of the EFI_EVENT_GROUP_EXIT_BOOT_SERVICES event group. But our
current implementation was stopping all other events.

All events have to observe the task priority level.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 25e6fb5e 02-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix EnableCursor()

The EnableCursor() service of the simple text output protocol must update
the the CursorVisible field of the output mode.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# b31ca6bf 18-May-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: parameter check OutputString

Check the parameters against NULL.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# acee9652 18-May-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.SetState() correct parameter

KeyToggleState is a pointer according to UEFI spec 2.8.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# bfc2dd53 06-Apr-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: correct CTRL-A - CTRL-Z console input

In the extended text input protocol CTRL-A - CTRL-Z have to be signaled as
Unicode characters a-z or A-Z depending on the shift state and not as 0x01
to 0x1a.

Update Python unit test.

This patch is required for using the EFI shell `edit` command.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# dd1a1ec2 04-Mar-2019 Matthias Brugger <mbrugger@suse.com>

efi_loader: Fix serial console size detection

Function term_read_reply tries to read from the serial console until
the end_char was read. This can hang forever if we are, for some reason,
not able to read the full response (e.g. serial buffer too small,
frame error). This patch moves the timeout detection into
term_read_reply() to assure we will make progress.

Fixes: 6bb591f704 ("efi_loader: query serial console size reliably")
Signed-off-by: Matthias Brugger <mbrugger@suse.com>

Throw missing error when an incomplete reply for the cursor position is
received.

Change type of argument of term_get_char() *s32. This renders the function
reusable in efi_cin_read_key().

Reviewed-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# e1fec152 18-Oct-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix typos

Fix typos in EFI subsystem comments.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# fae0118e 25-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: eliminate handle member

A pointer to a struct efi_object is a handle. We do not need any handle
member in this structure. Let's eliminate it.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 6f566c23 01-Oct-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: return type efi_console_register()

Use a return type that can encompass the return value.

This fixes CoverityScan CID 184090.

Reported-by: Tom Rini <trini@konsulko.com>
Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 6bb591f7 15-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: query serial console size reliably

Not all terminals understand CSI [18t for querying the console size.
We should adhere to escape sequences documented in the console_codes
manpage and the ECMA-48 standard.

So here we follow a different approach. We position the cursor to the
bottom right and query its position. Before leaving the function we
restore the original cursor position.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# aaace4b0 14-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: terminal left upper corner is [1, 1]

For the VT102 and the Linux console the left upper corner is [1, 1].
EFI uses [0, 0] as left upper corner.

Check parameters of the SetCursorPosition() service.

Use CSI H like EDK2 for positioning.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 4fdcf066 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: implement key notify functions

Implement registering and unregistreing key notify functions in the
EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 55fbdf99 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: support modifiers for F1 - F4

Support modifiers for F1 - F4.
Add support for letters with ALT key.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 110c6280 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL

This patch implements the EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.

The implementation of notification functions is postponed to a later
patch.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 0dfd13a4 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: rework event handling for console

Preread the next key in the console timer event.

The EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL requires to trigger registered key
notification functions based on the prefetched key.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 35cbb796 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: support Unicode text input

Up to now the EFI_TEXT_INPUT_PROTOCOL only supported ASCII characters.
With the patch it can consume UTF-8 from the console.

Currently only the serial console and the console can deliver UTF-8.
Local consoles are restricted to ASCII.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 14d103bb 08-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: typos in efi_console.c

Fix a few typos.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 3e603ec7 08-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: struct efi_simple_text_input_protocol

%s/efi_simple_input_interface/efi_simple_text_input_protocol/

We should be consistent in the naming of the EFI protocol interface
structures. The protocol is called EFI_SIMPLE_TEXT_INPUT_PROTOCOL.

%s/ExtendedVerification/extended_verification/

Use consistent naming of function parameters. Do not use CamelCase.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 40e3e757 04-Sep-2018 Alexander Graf <agraf@csgraf.de>

efi_loader: Add console handle references to in/out/err

We register the console in/out protocol on the loaded image handle, but
never tell anyone about it in the systab.

Because of this Shell.efi can not find the input protocol. So let's add
a proper reference.

Signed-off-by: Alexander Graf <agraf@suse.de>
Reviewed-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# ba7bd5c2 31-Aug-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: don't use unlimited stack as buffer

The length of a string printed to the console by the
EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL is not limited by the UEFI spec.
Hence should not allocate a buffer for it on the stack.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 1dde0d57 31-Aug-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: rename utf16_strlen, utf16_strnlen

The function names utf16_strlen() and utf16_strnlen() are misnomers.
The functions do not count utf-16 characters but non-zero words.
So let's rename them to u16_strlen and u16_strnlen().

In utf16_dup() avoid assignment in if clause.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 9d12daff 05-Jul-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.Reset()

Implement the reset service of the EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.

This should resolve the error reported by the SCT in
Protocol/SimpleTextOut/BlackBoxTest/SimpleTextOutBBTestFunction_uefi.c:639

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# e67ff94d 05-Jul-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: clear screen has to reset cursor position

After clearing the screen the cursor position is row 0, column 0.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 4f187897 05-Jul-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_INPUT_PROTOCOL.Reset()

Implement the reset service of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL.

This should resolve the error reported by the SCT in
Protocol/SimpleTextIn/BlackBoxTest/SimpleTextInBBTestFunction.c:193

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 80483b2a 03-Jun-2018 Alexander Graf <agraf@csgraf.de>

efi_loader: Fix warning in raw/cols query

The code to determine rows / cols on the screen could potentially run
into a case where it doesn't know how big the screen is. In that case,
assume 80x25.

Signed-off-by: Alexander Graf <agraf@suse.de>

# 62217295 16-May-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: avoid using unitialized values as console size

If a request for the console size would be answered with a response
with less then three values, uninitialized stack memory would be
copied to the number of rows and columns of the terminal.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# a4aa7bef 29-Apr-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: always check parameters in efi_cout_query_mode()

If we cannot determine the size of the serial terminal we still have
to check the parameters of efi_cout_query_mode().

Querying the size of the serial terminal drains the keyboard buffer.
So make sure we do this during the initialization and not in the midst
of an EFI application.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 7ca7c3c0 29-Apr-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: updating the cursor position

The current coding advances the column by the number of UTF-8 bytes.
The column should be increased by one per unicode character.

The UEFI spec provides advance rules for U+0000, U+0008, U+000A,
and U000D. All other characters, including control characters
U+0007 (bel) and U+0009 (tab), have to increase the column by one.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# f739fcd8 07-May-2018 Tom Rini <trini@konsulko.com>

SPDX: Convert a few files that were missed before

As part of the main conversion a few files were missed. These files had
additional whitespace after the '*' and before the SPDX tag and my
previous regex was too strict. This time I did a grep for all SPDX tags
and then filtered out anything that matched the correct styles.

Fixes: 83d290c56fab ("SPDX: Convert all of our single license tags to Linux Kernel style")
Reported-by: Heinrich Schuchardt <xypron.debian@gmx.de>
Signed-off-by: Tom Rini <trini@konsulko.com>

# b095f3c8 18-Feb-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: implement event groups

If an event of a group event is signaled all other events of the same
group are signaled too.

Function efi_signal_event is renamed to efi_queue_event.
A new function efi_signal_event is introduced that checks if an event
belongs to a group and than signals all events of the group.
Event group notifciation is implemented for ExitBootServices,
InstallConfigurationTable, and ResetSystem.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 0fb4169e 19-Feb-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: correct input of special keys

Don't set unicode_char if scan_code is set.
Add support for page up, page down, and insert.
Correct input of function keys.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 8396e3fd 03-Mar-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: remove deprecated ConsoleControlProtocol

The console control protocol is not defined in the UEFI standard.

It exists in EDK2's EdkCompatiblityPkg package. But this package
is deprecated according to
https://github.com/tianocore/tianocore.github.io/wiki/Differences-between-EDK-and-EDK-II

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 9bc9664d 19-Jan-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: add check_tpl parameter to efi_signal_event

In ExitBootServices we need to signal events irrespective of the current
TPL level. A new parameter check_tpl is added to efi_signal_event().

Function efi_console_timer_notify() gets some comments.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 2074f700 11-Jan-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: consistently use efi_handle_t for handles

We should consistently use the efi_handle_t typedef when
referring to handles.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# ebb4dd5b 26-Oct-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: efi_console: use helper functions

Use helper functions efi_created_handle and efi_add_protocol
for creating the console handles and instaling the respective
protocols.

This change is needed if we want to move from an array of
protocols to a linked list of protocols.

Eliminate EFI_PROTOCOL_OBJECT which is not used anymore.

Currently we have not defined protocol interfaces to be const.
So efi_con_out and efi_console_control cannot be defined as const.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 2d5dc2a5 10-Oct-2017 Rob Clark <robdclark@gmail.com>

efi_loader: console support for color attributes

Shell.efi uses this, and supporting color attributes makes things look
nicer. Map the EFI fg/bg color attributes to ANSI escape sequences.
Not all colors have a perfect match, but spec just says "Devices
supporting a different number of text colors are required to emulate the
above colors to the best of the device’s capabilities".

Signed-off-by: Rob Clark <robdclark@gmail.com>
Tested-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Alexander Graf <agraf@suse.de>
[agraf: s/unsigned/unsigned int/]
Signed-off-by: Alexander Graf <agraf@suse.de>

# e190e897 04-Oct-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: use type bool for event states

Queued and signaled describe boolean states of events.
So let's use type bool and rename the structure members to is_queued
and is_signaled.

Update the comments for is_queued and is_signaled.

Reported-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Rob Clark <robdclark@gmail.com>
Reviewed-by: Rob Clark <robdclark@gmail.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 3a45bc7f 13-Sep-2017 Rob Clark <robdclark@gmail.com>

efi_loader: Some console improvements for vidconsole

1) use fputs() to reduce cache flushes from once-per-char to
once-per-string
2) handle \r, \t, and \b in addition to just \n for tracking
cursor position
3) cursor row/col are zero based, not one based

Signed-off-by: Rob Clark <robdclark@gmail.com>
[agraf: s/unsigned/unsigned int/]
Signed-off-by: Alexander Graf <agraf@suse.de>

# a18c5a83 13-Sep-2017 Rob Clark <robdclark@gmail.com>

efi_loader: Correctly figure out size for vidconsole

If stdout is vidconsole, we cannot rely on ANSI escape sequences to
query the size, as vidconsole cannot reply on stdin. Instead special-
case this if stdout is vidconsole.

Signed-off-by: Rob Clark <robdclark@gmail.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 71cc25c3 13-Sep-2017 Rob Clark <robdclark@gmail.com>

efi_loader: split out escape sequence based size query

We need to do something different for vidconsole, since it cannot
respond to the query on stdin. Prep work for next patch.

Signed-off-by: Rob Clark <robdclark@gmail.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# ca62a4f5 15-Sep-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: implement queueing of the notification function

For the correct implementation of the task priority level (TPL)
calling the notification function must be queued.

Add a status field 'queued' to events.

In function efi_signal_event set status queued if a notification
function exists and reset it after we have called the function.
A later patch will add a check of the TPL here.

In efi_create_event and efi_close_event unset the queued status.

In function efi_wait_for_event and efi_check_event
queue the notification function.

In efi_timer_check call the efi_notify_event
if the status queued is set.
For all timer events set status signaled.

In efi_console_timer_notify set the signaled state of the
WaitForKey event.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 78178bb0 09-Sep-2017 Rob Clark <robdclark@gmail.com>

lib: add some utf16 handling helpers

We'll eventually want these in a few places in efi_loader, and also
vsprintf.

Signed-off-by: Rob Clark <robdclark@gmail.com>

# a17e62cc 24-Jul-2017 Rob Clark <robdclark@gmail.com>

efi_loader: expose protocols via GUID

shim.efi (or rather gnu-efi's LibLocateProtocol() which shim.efi uses)
resolves protocols via efi_locate_handle() so the console protocols
need to be added to the efi object list.

Signed-off-by: Rob Clark <robdclark@gmail.com>
[agraf: whitespace fixes]
Signed-off-by: Alexander Graf <agraf@suse.de>

# ff925938 19-Jul-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_console: use EFIAPI for callback functions

The call to efi_create_event requires notification functions
flagged as EFIAPI.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 91be9a77 18-Jul-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_console: set up events

Set up a timer event and the WaitForKey event.
In the notify function of the timer event check for console input
and signal the WaitForKey event accordingly.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 5be8b0a3 07-Nov-2016 Emmanuel Vadot <manu@bidouilliste.com>

efi_loader: console: Correctly report modes

Add support for EFI console modes.
Mode 0 is always 80x25 and present by EFI specification.
Mode 1 is always 80x50 and not mandatory.
Mode 2 and above is freely usable.

If the terminal can handle mode 1, we mark it as supported.
If the terminal size is greater than mode 0 and different than mode 1,
we install it as mode 2.

Modes can be switch with cout_set_mode.

Changes in V5:
Correctly detect mode before enabling mode 2.

Changes in V4:
Reset cursor positon on mode switch
Use local variables in console query code

Changes in V3:
Valid mode are 0 to EFIMode-1
Fix style

Changes in V2:
Add mode switch
Report only the modes that we support

Signed-off-by: Emmanuel Vadot <manu@bidouilliste.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# c1311ad4 03-Mar-2016 Alexander Graf <agraf@csgraf.de>

efi_loader: Add console interface

One of the basic EFI interfaces is the console interface. Using it an EFI
application can interface with the user. This patch implements an EFI console
interface using getc() and putc().

Today, we only implement text based consoles. We also convert the EFI Unicode
characters to UTF-8 on the fly, hoping that everyone managed to jump on the
train by now.

Signed-off-by: Alexander Graf <agraf@suse.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Tested-by: Simon Glass <sjg@chromium.org>

# 3c95b323 04-Feb-2022 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: add handle for UART

When loading an EFI binary via the UART we assign a UART device path to it.
But we lack a handle with that device path.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>

# abd62e4d 29-Jun-2021 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: missing parentheses in query_console_size

After if we should use parentheses to keep the code readable.

Fixes: a95f4c885991 ("efi_loader: NULL dereference in EFI console")
Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# a95f4c88 15-Mar-2021 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: NULL dereference in EFI console

Even if CONFIG_DM_VIDEO=y and stdout="vidconsole", a video device may not
be available. Check the return values of the relevant functions.

If no video output device is available, assume that the serial console is
in use.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 997b57fc 28-Feb-2021 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: console size of vidconsole

If stdout is 'vidconsole', we correctly set the console size.
If stdout is 'vidconsole,serial', the video console is ignored.

We should always evaluate the size of vidconsole if it is the primary
console.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 7913c7dc 04-Jan-2021 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: typedef efi_string_t text output protocol

We do not want to use typedefs in U-Boot.

Do not use efi_string_t in the EFI_TEXT_OUTPUT_PROTOCOL.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 9abb01af 27-Dec-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: escape key handling

Up to now the escape key was not correctly detected in UEFI applications.
We had to hit it twice for a single escape to be recognized.

Use a 10 ms delay to detect if we are dealing with the escape key or an
escape sequence.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# c670aeee 07-Oct-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

common: rename getc() to getchar()

The sandbox is built with the SDL2 library with invokes the X11 library
which in turn calls getc(). But getc() in glibc is defined as

int getc(FILE *)

This does not match our definition.

int getc(void)

The sandbox crashes when called with parameter -l.

Rename our library symbol getc() to getchar().

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Tom Rini <trini@konsulko.com>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 325567d3 04-Jun-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: comments EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL

Provide missing comments for the functions implementing the
EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 336d4615 03-Feb-2020 Simon Glass <sjg@chromium.org>

dm: core: Create a new header file for 'compat' features

At present dm/device.h includes the linux-compatible features. This
requires including linux/compat.h which in turn includes a lot of headers.
One of these is malloc.h which we thus end up including in every file in
U-Boot. Apart from the inefficiency of this, it is problematic for sandbox
which needs to use the system malloc() in some files.

Move the compatibility features into a separate header file.

Signed-off-by: Simon Glass <sjg@chromium.org>

# b0ad9b5b 22-Dec-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: clear screen should move cursor to home

On a VT100 terminal <ESC>[2J should be enough to both clear the whole
screen and set the cursor to position (1, 1). But the Linux console does
not behave like this. So send an extra <ESC>[H. For reference see the
console_codes(4) man page.

Add a function description.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 1045315d 14-Nov-2019 Simon Glass <sjg@chromium.org>

common: Move get_ticks() function out of common.h

This function belongs in time.h so move it over and add a comment.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Tom Rini <trini@konsulko.com>

# fe1a81c1 05-Sep-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

doc: UEFI API documentation

Add some more files to the UEFI API documentation.

Correct some Sphinx comments.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 03446987 04-Sep-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: do not set invalid screen mode

EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.SetMode() should return EFI_UNDEFINED if a
screen mode is not available.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Alexander Graf <agraf@csgraf.de>

# 97ea0690 04-Sep-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: cursor positioning

When backspacing in column 0 do no set the column index to ULONG_MAX.
Ensure that the row number is not set to ULONG_MAX even if the row count is
advertised as 0.
Ignore control characters other the 0x08, 0x0a, 0x0d when updating the
column.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 7b51b576 01-Aug-2019 Simon Glass <sjg@chromium.org>

env: Move env_get() to env.h

Move env_get() over to the new header file.

Acked-by: Joe Hershberger <joe.hershberger@ni.com>
Signed-off-by: Simon Glass <sjg@chromium.org>

# 3b435c11 16-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: console incorrectly advertised left logo key

Avoid to signal that the left logo key is pressed, when it is not.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 3b2b2de8 16-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: alternative scan codes for F5, END, HOME

Depending on the key board alternative scan codes are used for F5, END, and
HOME.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 3950f0f8 13-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix SetAttribute()

The SetAttribute() service and the Reset() service of the simple text
output protocol must update the attribute value in the mode information.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 2ad238fc 13-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix SetMode()

Correct the check of the mode number in SetMode() service of the simple
text output protocol.

Clear the screen in SetMode().

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 7eaa900e 06-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: event signaling in ExitBootServices

ExitBootServices() has to stop timer related activity before calling the
events of the EFI_EVENT_GROUP_EXIT_BOOT_SERVICES event group. But our
current implementation was stopping all other events.

All events have to observe the task priority level.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 25e6fb5e 02-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix EnableCursor()

The EnableCursor() service of the simple text output protocol must update
the the CursorVisible field of the output mode.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# b31ca6bf 18-May-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: parameter check OutputString

Check the parameters against NULL.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# acee9652 18-May-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.SetState() correct parameter

KeyToggleState is a pointer according to UEFI spec 2.8.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# bfc2dd53 06-Apr-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: correct CTRL-A - CTRL-Z console input

In the extended text input protocol CTRL-A - CTRL-Z have to be signaled as
Unicode characters a-z or A-Z depending on the shift state and not as 0x01
to 0x1a.

Update Python unit test.

This patch is required for using the EFI shell `edit` command.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# dd1a1ec2 04-Mar-2019 Matthias Brugger <mbrugger@suse.com>

efi_loader: Fix serial console size detection

Function term_read_reply tries to read from the serial console until
the end_char was read. This can hang forever if we are, for some reason,
not able to read the full response (e.g. serial buffer too small,
frame error). This patch moves the timeout detection into
term_read_reply() to assure we will make progress.

Fixes: 6bb591f704 ("efi_loader: query serial console size reliably")
Signed-off-by: Matthias Brugger <mbrugger@suse.com>

Throw missing error when an incomplete reply for the cursor position is
received.

Change type of argument of term_get_char() *s32. This renders the function
reusable in efi_cin_read_key().

Reviewed-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# e1fec152 18-Oct-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix typos

Fix typos in EFI subsystem comments.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# fae0118e 25-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: eliminate handle member

A pointer to a struct efi_object is a handle. We do not need any handle
member in this structure. Let's eliminate it.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 6f566c23 01-Oct-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: return type efi_console_register()

Use a return type that can encompass the return value.

This fixes CoverityScan CID 184090.

Reported-by: Tom Rini <trini@konsulko.com>
Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 6bb591f7 15-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: query serial console size reliably

Not all terminals understand CSI [18t for querying the console size.
We should adhere to escape sequences documented in the console_codes
manpage and the ECMA-48 standard.

So here we follow a different approach. We position the cursor to the
bottom right and query its position. Before leaving the function we
restore the original cursor position.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# aaace4b0 14-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: terminal left upper corner is [1, 1]

For the VT102 and the Linux console the left upper corner is [1, 1].
EFI uses [0, 0] as left upper corner.

Check parameters of the SetCursorPosition() service.

Use CSI H like EDK2 for positioning.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 4fdcf066 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: implement key notify functions

Implement registering and unregistreing key notify functions in the
EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 55fbdf99 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: support modifiers for F1 - F4

Support modifiers for F1 - F4.
Add support for letters with ALT key.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 110c6280 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL

This patch implements the EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.

The implementation of notification functions is postponed to a later
patch.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 0dfd13a4 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: rework event handling for console

Preread the next key in the console timer event.

The EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL requires to trigger registered key
notification functions based on the prefetched key.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 35cbb796 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: support Unicode text input

Up to now the EFI_TEXT_INPUT_PROTOCOL only supported ASCII characters.
With the patch it can consume UTF-8 from the console.

Currently only the serial console and the console can deliver UTF-8.
Local consoles are restricted to ASCII.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 14d103bb 08-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: typos in efi_console.c

Fix a few typos.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 3e603ec7 08-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: struct efi_simple_text_input_protocol

%s/efi_simple_input_interface/efi_simple_text_input_protocol/

We should be consistent in the naming of the EFI protocol interface
structures. The protocol is called EFI_SIMPLE_TEXT_INPUT_PROTOCOL.

%s/ExtendedVerification/extended_verification/

Use consistent naming of function parameters. Do not use CamelCase.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 40e3e757 04-Sep-2018 Alexander Graf <agraf@csgraf.de>

efi_loader: Add console handle references to in/out/err

We register the console in/out protocol on the loaded image handle, but
never tell anyone about it in the systab.

Because of this Shell.efi can not find the input protocol. So let's add
a proper reference.

Signed-off-by: Alexander Graf <agraf@suse.de>
Reviewed-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# ba7bd5c2 31-Aug-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: don't use unlimited stack as buffer

The length of a string printed to the console by the
EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL is not limited by the UEFI spec.
Hence should not allocate a buffer for it on the stack.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 1dde0d57 31-Aug-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: rename utf16_strlen, utf16_strnlen

The function names utf16_strlen() and utf16_strnlen() are misnomers.
The functions do not count utf-16 characters but non-zero words.
So let's rename them to u16_strlen and u16_strnlen().

In utf16_dup() avoid assignment in if clause.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 9d12daff 05-Jul-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.Reset()

Implement the reset service of the EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.

This should resolve the error reported by the SCT in
Protocol/SimpleTextOut/BlackBoxTest/SimpleTextOutBBTestFunction_uefi.c:639

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# e67ff94d 05-Jul-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: clear screen has to reset cursor position

After clearing the screen the cursor position is row 0, column 0.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 4f187897 05-Jul-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_INPUT_PROTOCOL.Reset()

Implement the reset service of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL.

This should resolve the error reported by the SCT in
Protocol/SimpleTextIn/BlackBoxTest/SimpleTextInBBTestFunction.c:193

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 80483b2a 03-Jun-2018 Alexander Graf <agraf@csgraf.de>

efi_loader: Fix warning in raw/cols query

The code to determine rows / cols on the screen could potentially run
into a case where it doesn't know how big the screen is. In that case,
assume 80x25.

Signed-off-by: Alexander Graf <agraf@suse.de>

# 62217295 16-May-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: avoid using unitialized values as console size

If a request for the console size would be answered with a response
with less then three values, uninitialized stack memory would be
copied to the number of rows and columns of the terminal.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# a4aa7bef 29-Apr-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: always check parameters in efi_cout_query_mode()

If we cannot determine the size of the serial terminal we still have
to check the parameters of efi_cout_query_mode().

Querying the size of the serial terminal drains the keyboard buffer.
So make sure we do this during the initialization and not in the midst
of an EFI application.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 7ca7c3c0 29-Apr-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: updating the cursor position

The current coding advances the column by the number of UTF-8 bytes.
The column should be increased by one per unicode character.

The UEFI spec provides advance rules for U+0000, U+0008, U+000A,
and U000D. All other characters, including control characters
U+0007 (bel) and U+0009 (tab), have to increase the column by one.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# f739fcd8 07-May-2018 Tom Rini <trini@konsulko.com>

SPDX: Convert a few files that were missed before

As part of the main conversion a few files were missed. These files had
additional whitespace after the '*' and before the SPDX tag and my
previous regex was too strict. This time I did a grep for all SPDX tags
and then filtered out anything that matched the correct styles.

Fixes: 83d290c56fab ("SPDX: Convert all of our single license tags to Linux Kernel style")
Reported-by: Heinrich Schuchardt <xypron.debian@gmx.de>
Signed-off-by: Tom Rini <trini@konsulko.com>

# b095f3c8 18-Feb-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: implement event groups

If an event of a group event is signaled all other events of the same
group are signaled too.

Function efi_signal_event is renamed to efi_queue_event.
A new function efi_signal_event is introduced that checks if an event
belongs to a group and than signals all events of the group.
Event group notifciation is implemented for ExitBootServices,
InstallConfigurationTable, and ResetSystem.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 0fb4169e 19-Feb-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: correct input of special keys

Don't set unicode_char if scan_code is set.
Add support for page up, page down, and insert.
Correct input of function keys.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 8396e3fd 03-Mar-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: remove deprecated ConsoleControlProtocol

The console control protocol is not defined in the UEFI standard.

It exists in EDK2's EdkCompatiblityPkg package. But this package
is deprecated according to
https://github.com/tianocore/tianocore.github.io/wiki/Differences-between-EDK-and-EDK-II

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 9bc9664d 19-Jan-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: add check_tpl parameter to efi_signal_event

In ExitBootServices we need to signal events irrespective of the current
TPL level. A new parameter check_tpl is added to efi_signal_event().

Function efi_console_timer_notify() gets some comments.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 2074f700 11-Jan-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: consistently use efi_handle_t for handles

We should consistently use the efi_handle_t typedef when
referring to handles.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# ebb4dd5b 26-Oct-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: efi_console: use helper functions

Use helper functions efi_created_handle and efi_add_protocol
for creating the console handles and instaling the respective
protocols.

This change is needed if we want to move from an array of
protocols to a linked list of protocols.

Eliminate EFI_PROTOCOL_OBJECT which is not used anymore.

Currently we have not defined protocol interfaces to be const.
So efi_con_out and efi_console_control cannot be defined as const.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 2d5dc2a5 10-Oct-2017 Rob Clark <robdclark@gmail.com>

efi_loader: console support for color attributes

Shell.efi uses this, and supporting color attributes makes things look
nicer. Map the EFI fg/bg color attributes to ANSI escape sequences.
Not all colors have a perfect match, but spec just says "Devices
supporting a different number of text colors are required to emulate the
above colors to the best of the device’s capabilities".

Signed-off-by: Rob Clark <robdclark@gmail.com>
Tested-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Alexander Graf <agraf@suse.de>
[agraf: s/unsigned/unsigned int/]
Signed-off-by: Alexander Graf <agraf@suse.de>

# e190e897 04-Oct-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: use type bool for event states

Queued and signaled describe boolean states of events.
So let's use type bool and rename the structure members to is_queued
and is_signaled.

Update the comments for is_queued and is_signaled.

Reported-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Rob Clark <robdclark@gmail.com>
Reviewed-by: Rob Clark <robdclark@gmail.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 3a45bc7f 13-Sep-2017 Rob Clark <robdclark@gmail.com>

efi_loader: Some console improvements for vidconsole

1) use fputs() to reduce cache flushes from once-per-char to
once-per-string
2) handle \r, \t, and \b in addition to just \n for tracking
cursor position
3) cursor row/col are zero based, not one based

Signed-off-by: Rob Clark <robdclark@gmail.com>
[agraf: s/unsigned/unsigned int/]
Signed-off-by: Alexander Graf <agraf@suse.de>

# a18c5a83 13-Sep-2017 Rob Clark <robdclark@gmail.com>

efi_loader: Correctly figure out size for vidconsole

If stdout is vidconsole, we cannot rely on ANSI escape sequences to
query the size, as vidconsole cannot reply on stdin. Instead special-
case this if stdout is vidconsole.

Signed-off-by: Rob Clark <robdclark@gmail.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 71cc25c3 13-Sep-2017 Rob Clark <robdclark@gmail.com>

efi_loader: split out escape sequence based size query

We need to do something different for vidconsole, since it cannot
respond to the query on stdin. Prep work for next patch.

Signed-off-by: Rob Clark <robdclark@gmail.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# ca62a4f5 15-Sep-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: implement queueing of the notification function

For the correct implementation of the task priority level (TPL)
calling the notification function must be queued.

Add a status field 'queued' to events.

In function efi_signal_event set status queued if a notification
function exists and reset it after we have called the function.
A later patch will add a check of the TPL here.

In efi_create_event and efi_close_event unset the queued status.

In function efi_wait_for_event and efi_check_event
queue the notification function.

In efi_timer_check call the efi_notify_event
if the status queued is set.
For all timer events set status signaled.

In efi_console_timer_notify set the signaled state of the
WaitForKey event.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 78178bb0 09-Sep-2017 Rob Clark <robdclark@gmail.com>

lib: add some utf16 handling helpers

We'll eventually want these in a few places in efi_loader, and also
vsprintf.

Signed-off-by: Rob Clark <robdclark@gmail.com>

# a17e62cc 24-Jul-2017 Rob Clark <robdclark@gmail.com>

efi_loader: expose protocols via GUID

shim.efi (or rather gnu-efi's LibLocateProtocol() which shim.efi uses)
resolves protocols via efi_locate_handle() so the console protocols
need to be added to the efi object list.

Signed-off-by: Rob Clark <robdclark@gmail.com>
[agraf: whitespace fixes]
Signed-off-by: Alexander Graf <agraf@suse.de>

# ff925938 19-Jul-2017 xypron.glpk@gmx.de <xypron.glpk@gmx.de>

efi_console: use EFIAPI for callback functions

The call to efi_create_event requires notification functions
flagged as EFIAPI.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 91be9a77 18-Jul-2017 xypron.glpk@gmx.de <xypron.glpk@gmx.de>

efi_console: set up events

Set up a timer event and the WaitForKey event.
In the notify function of the timer event check for console input
and signal the WaitForKey event accordingly.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 5be8b0a3 07-Nov-2016 Emmanuel Vadot <manu@bidouilliste.com>

efi_loader: console: Correctly report modes

Add support for EFI console modes.
Mode 0 is always 80x25 and present by EFI specification.
Mode 1 is always 80x50 and not mandatory.
Mode 2 and above is freely usable.

If the terminal can handle mode 1, we mark it as supported.
If the terminal size is greater than mode 0 and different than mode 1,
we install it as mode 2.

Modes can be switch with cout_set_mode.

Changes in V5:
Correctly detect mode before enabling mode 2.

Changes in V4:
Reset cursor positon on mode switch
Use local variables in console query code

Changes in V3:
Valid mode are 0 to EFIMode-1
Fix style

Changes in V2:
Add mode switch
Report only the modes that we support

Signed-off-by: Emmanuel Vadot <manu@bidouilliste.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# c1311ad4 03-Mar-2016 Alexander Graf <agraf@csgraf.de>

efi_loader: Add console interface

One of the basic EFI interfaces is the console interface. Using it an EFI
application can interface with the user. This patch implements an EFI console
interface using getc() and putc().

Today, we only implement text based consoles. We also convert the EFI Unicode
characters to UTF-8 on the fly, hoping that everyone managed to jump on the
train by now.

Signed-off-by: Alexander Graf <agraf@suse.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Tested-by: Simon Glass <sjg@chromium.org>

# abd62e4d 29-Jun-2021 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: missing parentheses in query_console_size

After if we should use parentheses to keep the code readable.

Fixes: a95f4c885991 ("efi_loader: NULL dereference in EFI console")
Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# a95f4c88 15-Mar-2021 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: NULL dereference in EFI console

Even if CONFIG_DM_VIDEO=y and stdout="vidconsole", a video device may not
be available. Check the return values of the relevant functions.

If no video output device is available, assume that the serial console is
in use.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 997b57fc 28-Feb-2021 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: console size of vidconsole

If stdout is 'vidconsole', we correctly set the console size.
If stdout is 'vidconsole,serial', the video console is ignored.

We should always evaluate the size of vidconsole if it is the primary
console.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 7913c7dc 04-Jan-2021 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: typedef efi_string_t text output protocol

We do not want to use typedefs in U-Boot.

Do not use efi_string_t in the EFI_TEXT_OUTPUT_PROTOCOL.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 9abb01af 27-Dec-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: escape key handling

Up to now the escape key was not correctly detected in UEFI applications.
We had to hit it twice for a single escape to be recognized.

Use a 10 ms delay to detect if we are dealing with the escape key or an
escape sequence.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# c670aeee 07-Oct-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

common: rename getc() to getchar()

The sandbox is built with the SDL2 library with invokes the X11 library
which in turn calls getc(). But getc() in glibc is defined as

int getc(FILE *)

This does not match our definition.

int getc(void)

The sandbox crashes when called with parameter -l.

Rename our library symbol getc() to getchar().

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Tom Rini <trini@konsulko.com>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 325567d3 04-Jun-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: comments EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL

Provide missing comments for the functions implementing the
EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 336d4615 03-Feb-2020 Simon Glass <sjg@chromium.org>

dm: core: Create a new header file for 'compat' features

At present dm/device.h includes the linux-compatible features. This
requires including linux/compat.h which in turn includes a lot of headers.
One of these is malloc.h which we thus end up including in every file in
U-Boot. Apart from the inefficiency of this, it is problematic for sandbox
which needs to use the system malloc() in some files.

Move the compatibility features into a separate header file.

Signed-off-by: Simon Glass <sjg@chromium.org>

# b0ad9b5b 22-Dec-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: clear screen should move cursor to home

On a VT100 terminal <ESC>[2J should be enough to both clear the whole
screen and set the cursor to position (1, 1). But the Linux console does
not behave like this. So send an extra <ESC>[H. For reference see the
console_codes(4) man page.

Add a function description.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 1045315d 14-Nov-2019 Simon Glass <sjg@chromium.org>

common: Move get_ticks() function out of common.h

This function belongs in time.h so move it over and add a comment.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Tom Rini <trini@konsulko.com>

# fe1a81c1 05-Sep-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

doc: UEFI API documentation

Add some more files to the UEFI API documentation.

Correct some Sphinx comments.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 03446987 04-Sep-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: do not set invalid screen mode

EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.SetMode() should return EFI_UNDEFINED if a
screen mode is not available.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Alexander Graf <agraf@csgraf.de>

# 97ea0690 04-Sep-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: cursor positioning

When backspacing in column 0 do no set the column index to ULONG_MAX.
Ensure that the row number is not set to ULONG_MAX even if the row count is
advertised as 0.
Ignore control characters other the 0x08, 0x0a, 0x0d when updating the
column.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 7b51b576 01-Aug-2019 Simon Glass <sjg@chromium.org>

env: Move env_get() to env.h

Move env_get() over to the new header file.

Acked-by: Joe Hershberger <joe.hershberger@ni.com>
Signed-off-by: Simon Glass <sjg@chromium.org>

# 3b435c11 16-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: console incorrectly advertised left logo key

Avoid to signal that the left logo key is pressed, when it is not.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 3b2b2de8 16-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: alternative scan codes for F5, END, HOME

Depending on the key board alternative scan codes are used for F5, END, and
HOME.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 3950f0f8 13-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix SetAttribute()

The SetAttribute() service and the Reset() service of the simple text
output protocol must update the attribute value in the mode information.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 2ad238fc 13-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix SetMode()

Correct the check of the mode number in SetMode() service of the simple
text output protocol.

Clear the screen in SetMode().

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 7eaa900e 06-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: event signaling in ExitBootServices

ExitBootServices() has to stop timer related activity before calling the
events of the EFI_EVENT_GROUP_EXIT_BOOT_SERVICES event group. But our
current implementation was stopping all other events.

All events have to observe the task priority level.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 25e6fb5e 02-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix EnableCursor()

The EnableCursor() service of the simple text output protocol must update
the the CursorVisible field of the output mode.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# b31ca6bf 18-May-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: parameter check OutputString

Check the parameters against NULL.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# acee9652 18-May-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.SetState() correct parameter

KeyToggleState is a pointer according to UEFI spec 2.8.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# bfc2dd53 06-Apr-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: correct CTRL-A - CTRL-Z console input

In the extended text input protocol CTRL-A - CTRL-Z have to be signaled as
Unicode characters a-z or A-Z depending on the shift state and not as 0x01
to 0x1a.

Update Python unit test.

This patch is required for using the EFI shell `edit` command.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# dd1a1ec2 04-Mar-2019 Matthias Brugger <mbrugger@suse.com>

efi_loader: Fix serial console size detection

Function term_read_reply tries to read from the serial console until
the end_char was read. This can hang forever if we are, for some reason,
not able to read the full response (e.g. serial buffer too small,
frame error). This patch moves the timeout detection into
term_read_reply() to assure we will make progress.

Fixes: 6bb591f704 ("efi_loader: query serial console size reliably")
Signed-off-by: Matthias Brugger <mbrugger@suse.com>

Throw missing error when an incomplete reply for the cursor position is
received.

Change type of argument of term_get_char() *s32. This renders the function
reusable in efi_cin_read_key().

Reviewed-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# e1fec152 18-Oct-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix typos

Fix typos in EFI subsystem comments.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# fae0118e 25-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: eliminate handle member

A pointer to a struct efi_object is a handle. We do not need any handle
member in this structure. Let's eliminate it.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 6f566c23 01-Oct-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: return type efi_console_register()

Use a return type that can encompass the return value.

This fixes CoverityScan CID 184090.

Reported-by: Tom Rini <trini@konsulko.com>
Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 6bb591f7 15-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: query serial console size reliably

Not all terminals understand CSI [18t for querying the console size.
We should adhere to escape sequences documented in the console_codes
manpage and the ECMA-48 standard.

So here we follow a different approach. We position the cursor to the
bottom right and query its position. Before leaving the function we
restore the original cursor position.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# aaace4b0 14-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: terminal left upper corner is [1, 1]

For the VT102 and the Linux console the left upper corner is [1, 1].
EFI uses [0, 0] as left upper corner.

Check parameters of the SetCursorPosition() service.

Use CSI H like EDK2 for positioning.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 4fdcf066 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: implement key notify functions

Implement registering and unregistreing key notify functions in the
EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 55fbdf99 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: support modifiers for F1 - F4

Support modifiers for F1 - F4.
Add support for letters with ALT key.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 110c6280 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL

This patch implements the EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.

The implementation of notification functions is postponed to a later
patch.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 0dfd13a4 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: rework event handling for console

Preread the next key in the console timer event.

The EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL requires to trigger registered key
notification functions based on the prefetched key.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 35cbb796 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: support Unicode text input

Up to now the EFI_TEXT_INPUT_PROTOCOL only supported ASCII characters.
With the patch it can consume UTF-8 from the console.

Currently only the serial console and the console can deliver UTF-8.
Local consoles are restricted to ASCII.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 14d103bb 08-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: typos in efi_console.c

Fix a few typos.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 3e603ec7 08-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: struct efi_simple_text_input_protocol

%s/efi_simple_input_interface/efi_simple_text_input_protocol/

We should be consistent in the naming of the EFI protocol interface
structures. The protocol is called EFI_SIMPLE_TEXT_INPUT_PROTOCOL.

%s/ExtendedVerification/extended_verification/

Use consistent naming of function parameters. Do not use CamelCase.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 40e3e757 04-Sep-2018 Alexander Graf <agraf@csgraf.de>

efi_loader: Add console handle references to in/out/err

We register the console in/out protocol on the loaded image handle, but
never tell anyone about it in the systab.

Because of this Shell.efi can not find the input protocol. So let's add
a proper reference.

Signed-off-by: Alexander Graf <agraf@suse.de>
Reviewed-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# ba7bd5c2 31-Aug-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: don't use unlimited stack as buffer

The length of a string printed to the console by the
EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL is not limited by the UEFI spec.
Hence should not allocate a buffer for it on the stack.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 1dde0d57 31-Aug-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: rename utf16_strlen, utf16_strnlen

The function names utf16_strlen() and utf16_strnlen() are misnomers.
The functions do not count utf-16 characters but non-zero words.
So let's rename them to u16_strlen and u16_strnlen().

In utf16_dup() avoid assignment in if clause.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 9d12daff 05-Jul-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.Reset()

Implement the reset service of the EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.

This should resolve the error reported by the SCT in
Protocol/SimpleTextOut/BlackBoxTest/SimpleTextOutBBTestFunction_uefi.c:639

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# e67ff94d 05-Jul-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: clear screen has to reset cursor position

After clearing the screen the cursor position is row 0, column 0.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 4f187897 05-Jul-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_INPUT_PROTOCOL.Reset()

Implement the reset service of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL.

This should resolve the error reported by the SCT in
Protocol/SimpleTextIn/BlackBoxTest/SimpleTextInBBTestFunction.c:193

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 80483b2a 03-Jun-2018 Alexander Graf <agraf@csgraf.de>

efi_loader: Fix warning in raw/cols query

The code to determine rows / cols on the screen could potentially run
into a case where it doesn't know how big the screen is. In that case,
assume 80x25.

Signed-off-by: Alexander Graf <agraf@suse.de>

# 62217295 16-May-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: avoid using unitialized values as console size

If a request for the console size would be answered with a response
with less then three values, uninitialized stack memory would be
copied to the number of rows and columns of the terminal.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# a4aa7bef 29-Apr-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: always check parameters in efi_cout_query_mode()

If we cannot determine the size of the serial terminal we still have
to check the parameters of efi_cout_query_mode().

Querying the size of the serial terminal drains the keyboard buffer.
So make sure we do this during the initialization and not in the midst
of an EFI application.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 7ca7c3c0 29-Apr-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: updating the cursor position

The current coding advances the column by the number of UTF-8 bytes.
The column should be increased by one per unicode character.

The UEFI spec provides advance rules for U+0000, U+0008, U+000A,
and U000D. All other characters, including control characters
U+0007 (bel) and U+0009 (tab), have to increase the column by one.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# f739fcd8 07-May-2018 Tom Rini <trini@konsulko.com>

SPDX: Convert a few files that were missed before

As part of the main conversion a few files were missed. These files had
additional whitespace after the '*' and before the SPDX tag and my
previous regex was too strict. This time I did a grep for all SPDX tags
and then filtered out anything that matched the correct styles.

Fixes: 83d290c56fab ("SPDX: Convert all of our single license tags to Linux Kernel style")
Reported-by: Heinrich Schuchardt <xypron.debian@gmx.de>
Signed-off-by: Tom Rini <trini@konsulko.com>

# b095f3c8 18-Feb-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: implement event groups

If an event of a group event is signaled all other events of the same
group are signaled too.

Function efi_signal_event is renamed to efi_queue_event.
A new function efi_signal_event is introduced that checks if an event
belongs to a group and than signals all events of the group.
Event group notifciation is implemented for ExitBootServices,
InstallConfigurationTable, and ResetSystem.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 0fb4169e 19-Feb-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: correct input of special keys

Don't set unicode_char if scan_code is set.
Add support for page up, page down, and insert.
Correct input of function keys.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 8396e3fd 03-Mar-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: remove deprecated ConsoleControlProtocol

The console control protocol is not defined in the UEFI standard.

It exists in EDK2's EdkCompatiblityPkg package. But this package
is deprecated according to
https://github.com/tianocore/tianocore.github.io/wiki/Differences-between-EDK-and-EDK-II

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 9bc9664d 19-Jan-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: add check_tpl parameter to efi_signal_event

In ExitBootServices we need to signal events irrespective of the current
TPL level. A new parameter check_tpl is added to efi_signal_event().

Function efi_console_timer_notify() gets some comments.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 2074f700 11-Jan-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: consistently use efi_handle_t for handles

We should consistently use the efi_handle_t typedef when
referring to handles.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# ebb4dd5b 26-Oct-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: efi_console: use helper functions

Use helper functions efi_created_handle and efi_add_protocol
for creating the console handles and instaling the respective
protocols.

This change is needed if we want to move from an array of
protocols to a linked list of protocols.

Eliminate EFI_PROTOCOL_OBJECT which is not used anymore.

Currently we have not defined protocol interfaces to be const.
So efi_con_out and efi_console_control cannot be defined as const.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 2d5dc2a5 10-Oct-2017 Rob Clark <robdclark@gmail.com>

efi_loader: console support for color attributes

Shell.efi uses this, and supporting color attributes makes things look
nicer. Map the EFI fg/bg color attributes to ANSI escape sequences.
Not all colors have a perfect match, but spec just says "Devices
supporting a different number of text colors are required to emulate the
above colors to the best of the device’s capabilities".

Signed-off-by: Rob Clark <robdclark@gmail.com>
Tested-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Alexander Graf <agraf@suse.de>
[agraf: s/unsigned/unsigned int/]
Signed-off-by: Alexander Graf <agraf@suse.de>

# e190e897 04-Oct-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: use type bool for event states

Queued and signaled describe boolean states of events.
So let's use type bool and rename the structure members to is_queued
and is_signaled.

Update the comments for is_queued and is_signaled.

Reported-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Rob Clark <robdclark@gmail.com>
Reviewed-by: Rob Clark <robdclark@gmail.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 3a45bc7f 13-Sep-2017 Rob Clark <robdclark@gmail.com>

efi_loader: Some console improvements for vidconsole

1) use fputs() to reduce cache flushes from once-per-char to
once-per-string
2) handle \r, \t, and \b in addition to just \n for tracking
cursor position
3) cursor row/col are zero based, not one based

Signed-off-by: Rob Clark <robdclark@gmail.com>
[agraf: s/unsigned/unsigned int/]
Signed-off-by: Alexander Graf <agraf@suse.de>

# a18c5a83 13-Sep-2017 Rob Clark <robdclark@gmail.com>

efi_loader: Correctly figure out size for vidconsole

If stdout is vidconsole, we cannot rely on ANSI escape sequences to
query the size, as vidconsole cannot reply on stdin. Instead special-
case this if stdout is vidconsole.

Signed-off-by: Rob Clark <robdclark@gmail.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 71cc25c3 13-Sep-2017 Rob Clark <robdclark@gmail.com>

efi_loader: split out escape sequence based size query

We need to do something different for vidconsole, since it cannot
respond to the query on stdin. Prep work for next patch.

Signed-off-by: Rob Clark <robdclark@gmail.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# ca62a4f5 15-Sep-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: implement queueing of the notification function

For the correct implementation of the task priority level (TPL)
calling the notification function must be queued.

Add a status field 'queued' to events.

In function efi_signal_event set status queued if a notification
function exists and reset it after we have called the function.
A later patch will add a check of the TPL here.

In efi_create_event and efi_close_event unset the queued status.

In function efi_wait_for_event and efi_check_event
queue the notification function.

In efi_timer_check call the efi_notify_event
if the status queued is set.
For all timer events set status signaled.

In efi_console_timer_notify set the signaled state of the
WaitForKey event.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 78178bb0 09-Sep-2017 Rob Clark <robdclark@gmail.com>

lib: add some utf16 handling helpers

We'll eventually want these in a few places in efi_loader, and also
vsprintf.

Signed-off-by: Rob Clark <robdclark@gmail.com>

# a17e62cc 24-Jul-2017 Rob Clark <robdclark@gmail.com>

efi_loader: expose protocols via GUID

shim.efi (or rather gnu-efi's LibLocateProtocol() which shim.efi uses)
resolves protocols via efi_locate_handle() so the console protocols
need to be added to the efi object list.

Signed-off-by: Rob Clark <robdclark@gmail.com>
[agraf: whitespace fixes]
Signed-off-by: Alexander Graf <agraf@suse.de>

# ff925938 19-Jul-2017 xypron.glpk@gmx.de <xypron.glpk@gmx.de>

efi_console: use EFIAPI for callback functions

The call to efi_create_event requires notification functions
flagged as EFIAPI.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 91be9a77 18-Jul-2017 xypron.glpk@gmx.de <xypron.glpk@gmx.de>

efi_console: set up events

Set up a timer event and the WaitForKey event.
In the notify function of the timer event check for console input
and signal the WaitForKey event accordingly.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 5be8b0a3 07-Nov-2016 Emmanuel Vadot <manu@bidouilliste.com>

efi_loader: console: Correctly report modes

Add support for EFI console modes.
Mode 0 is always 80x25 and present by EFI specification.
Mode 1 is always 80x50 and not mandatory.
Mode 2 and above is freely usable.

If the terminal can handle mode 1, we mark it as supported.
If the terminal size is greater than mode 0 and different than mode 1,
we install it as mode 2.

Modes can be switch with cout_set_mode.

Changes in V5:
Correctly detect mode before enabling mode 2.

Changes in V4:
Reset cursor positon on mode switch
Use local variables in console query code

Changes in V3:
Valid mode are 0 to EFIMode-1
Fix style

Changes in V2:
Add mode switch
Report only the modes that we support

Signed-off-by: Emmanuel Vadot <manu@bidouilliste.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# c1311ad4 03-Mar-2016 Alexander Graf <agraf@csgraf.de>

efi_loader: Add console interface

One of the basic EFI interfaces is the console interface. Using it an EFI
application can interface with the user. This patch implements an EFI console
interface using getc() and putc().

Today, we only implement text based consoles. We also convert the EFI Unicode
characters to UTF-8 on the fly, hoping that everyone managed to jump on the
train by now.

Signed-off-by: Alexander Graf <agraf@suse.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Tested-by: Simon Glass <sjg@chromium.org>

# a95f4c88 15-Mar-2021 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: NULL dereference in EFI console

Even if CONFIG_DM_VIDEO=y and stdout="vidconsole", a video device may not
be available. Check the return values of the relevant functions.

If no video output device is available, assume that the serial console is
in use.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 997b57fc 28-Feb-2021 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: console size of vidconsole

If stdout is 'vidconsole', we correctly set the console size.
If stdout is 'vidconsole,serial', the video console is ignored.

We should always evaluate the size of vidconsole if it is the primary
console.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 7913c7dc 04-Jan-2021 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: typedef efi_string_t text output protocol

We do not want to use typedefs in U-Boot.

Do not use efi_string_t in the EFI_TEXT_OUTPUT_PROTOCOL.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 9abb01af 27-Dec-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: escape key handling

Up to now the escape key was not correctly detected in UEFI applications.
We had to hit it twice for a single escape to be recognized.

Use a 10 ms delay to detect if we are dealing with the escape key or an
escape sequence.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# c670aeee 07-Oct-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

common: rename getc() to getchar()

The sandbox is built with the SDL2 library with invokes the X11 library
which in turn calls getc(). But getc() in glibc is defined as

int getc(FILE *)

This does not match our definition.

int getc(void)

The sandbox crashes when called with parameter -l.

Rename our library symbol getc() to getchar().

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Tom Rini <trini@konsulko.com>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 325567d3 04-Jun-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: comments EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL

Provide missing comments for the functions implementing the
EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 336d4615 03-Feb-2020 Simon Glass <sjg@chromium.org>

dm: core: Create a new header file for 'compat' features

At present dm/device.h includes the linux-compatible features. This
requires including linux/compat.h which in turn includes a lot of headers.
One of these is malloc.h which we thus end up including in every file in
U-Boot. Apart from the inefficiency of this, it is problematic for sandbox
which needs to use the system malloc() in some files.

Move the compatibility features into a separate header file.

Signed-off-by: Simon Glass <sjg@chromium.org>

# b0ad9b5b 22-Dec-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: clear screen should move cursor to home

On a VT100 terminal <ESC>[2J should be enough to both clear the whole
screen and set the cursor to position (1, 1). But the Linux console does
not behave like this. So send an extra <ESC>[H. For reference see the
console_codes(4) man page.

Add a function description.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 1045315d 14-Nov-2019 Simon Glass <sjg@chromium.org>

common: Move get_ticks() function out of common.h

This function belongs in time.h so move it over and add a comment.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Tom Rini <trini@konsulko.com>

# fe1a81c1 05-Sep-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

doc: UEFI API documentation

Add some more files to the UEFI API documentation.

Correct some Sphinx comments.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 03446987 04-Sep-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: do not set invalid screen mode

EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.SetMode() should return EFI_UNDEFINED if a
screen mode is not available.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Alexander Graf <agraf@csgraf.de>

# 97ea0690 04-Sep-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: cursor positioning

When backspacing in column 0 do no set the column index to ULONG_MAX.
Ensure that the row number is not set to ULONG_MAX even if the row count is
advertised as 0.
Ignore control characters other the 0x08, 0x0a, 0x0d when updating the
column.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 7b51b576 01-Aug-2019 Simon Glass <sjg@chromium.org>

env: Move env_get() to env.h

Move env_get() over to the new header file.

Acked-by: Joe Hershberger <joe.hershberger@ni.com>
Signed-off-by: Simon Glass <sjg@chromium.org>

# 3b435c11 16-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: console incorrectly advertised left logo key

Avoid to signal that the left logo key is pressed, when it is not.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 3b2b2de8 16-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: alternative scan codes for F5, END, HOME

Depending on the key board alternative scan codes are used for F5, END, and
HOME.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 3950f0f8 13-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix SetAttribute()

The SetAttribute() service and the Reset() service of the simple text
output protocol must update the attribute value in the mode information.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 2ad238fc 13-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix SetMode()

Correct the check of the mode number in SetMode() service of the simple
text output protocol.

Clear the screen in SetMode().

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 7eaa900e 06-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: event signaling in ExitBootServices

ExitBootServices() has to stop timer related activity before calling the
events of the EFI_EVENT_GROUP_EXIT_BOOT_SERVICES event group. But our
current implementation was stopping all other events.

All events have to observe the task priority level.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 25e6fb5e 02-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix EnableCursor()

The EnableCursor() service of the simple text output protocol must update
the the CursorVisible field of the output mode.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# b31ca6bf 18-May-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: parameter check OutputString

Check the parameters against NULL.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# acee9652 18-May-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.SetState() correct parameter

KeyToggleState is a pointer according to UEFI spec 2.8.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# bfc2dd53 06-Apr-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: correct CTRL-A - CTRL-Z console input

In the extended text input protocol CTRL-A - CTRL-Z have to be signaled as
Unicode characters a-z or A-Z depending on the shift state and not as 0x01
to 0x1a.

Update Python unit test.

This patch is required for using the EFI shell `edit` command.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# dd1a1ec2 04-Mar-2019 Matthias Brugger <mbrugger@suse.com>

efi_loader: Fix serial console size detection

Function term_read_reply tries to read from the serial console until
the end_char was read. This can hang forever if we are, for some reason,
not able to read the full response (e.g. serial buffer too small,
frame error). This patch moves the timeout detection into
term_read_reply() to assure we will make progress.

Fixes: 6bb591f704 ("efi_loader: query serial console size reliably")
Signed-off-by: Matthias Brugger <mbrugger@suse.com>

Throw missing error when an incomplete reply for the cursor position is
received.

Change type of argument of term_get_char() *s32. This renders the function
reusable in efi_cin_read_key().

Reviewed-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# e1fec152 18-Oct-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix typos

Fix typos in EFI subsystem comments.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# fae0118e 25-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: eliminate handle member

A pointer to a struct efi_object is a handle. We do not need any handle
member in this structure. Let's eliminate it.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 6f566c23 01-Oct-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: return type efi_console_register()

Use a return type that can encompass the return value.

This fixes CoverityScan CID 184090.

Reported-by: Tom Rini <trini@konsulko.com>
Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 6bb591f7 15-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: query serial console size reliably

Not all terminals understand CSI [18t for querying the console size.
We should adhere to escape sequences documented in the console_codes
manpage and the ECMA-48 standard.

So here we follow a different approach. We position the cursor to the
bottom right and query its position. Before leaving the function we
restore the original cursor position.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# aaace4b0 14-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: terminal left upper corner is [1, 1]

For the VT102 and the Linux console the left upper corner is [1, 1].
EFI uses [0, 0] as left upper corner.

Check parameters of the SetCursorPosition() service.

Use CSI H like EDK2 for positioning.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 4fdcf066 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: implement key notify functions

Implement registering and unregistreing key notify functions in the
EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 55fbdf99 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: support modifiers for F1 - F4

Support modifiers for F1 - F4.
Add support for letters with ALT key.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 110c6280 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL

This patch implements the EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.

The implementation of notification functions is postponed to a later
patch.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 0dfd13a4 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: rework event handling for console

Preread the next key in the console timer event.

The EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL requires to trigger registered key
notification functions based on the prefetched key.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 35cbb796 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: support Unicode text input

Up to now the EFI_TEXT_INPUT_PROTOCOL only supported ASCII characters.
With the patch it can consume UTF-8 from the console.

Currently only the serial console and the console can deliver UTF-8.
Local consoles are restricted to ASCII.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 14d103bb 08-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: typos in efi_console.c

Fix a few typos.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 3e603ec7 08-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: struct efi_simple_text_input_protocol

%s/efi_simple_input_interface/efi_simple_text_input_protocol/

We should be consistent in the naming of the EFI protocol interface
structures. The protocol is called EFI_SIMPLE_TEXT_INPUT_PROTOCOL.

%s/ExtendedVerification/extended_verification/

Use consistent naming of function parameters. Do not use CamelCase.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 40e3e757 04-Sep-2018 Alexander Graf <agraf@csgraf.de>

efi_loader: Add console handle references to in/out/err

We register the console in/out protocol on the loaded image handle, but
never tell anyone about it in the systab.

Because of this Shell.efi can not find the input protocol. So let's add
a proper reference.

Signed-off-by: Alexander Graf <agraf@suse.de>
Reviewed-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# ba7bd5c2 31-Aug-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: don't use unlimited stack as buffer

The length of a string printed to the console by the
EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL is not limited by the UEFI spec.
Hence should not allocate a buffer for it on the stack.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 1dde0d57 31-Aug-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: rename utf16_strlen, utf16_strnlen

The function names utf16_strlen() and utf16_strnlen() are misnomers.
The functions do not count utf-16 characters but non-zero words.
So let's rename them to u16_strlen and u16_strnlen().

In utf16_dup() avoid assignment in if clause.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 9d12daff 05-Jul-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.Reset()

Implement the reset service of the EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.

This should resolve the error reported by the SCT in
Protocol/SimpleTextOut/BlackBoxTest/SimpleTextOutBBTestFunction_uefi.c:639

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# e67ff94d 05-Jul-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: clear screen has to reset cursor position

After clearing the screen the cursor position is row 0, column 0.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 4f187897 05-Jul-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_INPUT_PROTOCOL.Reset()

Implement the reset service of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL.

This should resolve the error reported by the SCT in
Protocol/SimpleTextIn/BlackBoxTest/SimpleTextInBBTestFunction.c:193

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 80483b2a 03-Jun-2018 Alexander Graf <agraf@csgraf.de>

efi_loader: Fix warning in raw/cols query

The code to determine rows / cols on the screen could potentially run
into a case where it doesn't know how big the screen is. In that case,
assume 80x25.

Signed-off-by: Alexander Graf <agraf@suse.de>

# 62217295 16-May-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: avoid using unitialized values as console size

If a request for the console size would be answered with a response
with less then three values, uninitialized stack memory would be
copied to the number of rows and columns of the terminal.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# a4aa7bef 29-Apr-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: always check parameters in efi_cout_query_mode()

If we cannot determine the size of the serial terminal we still have
to check the parameters of efi_cout_query_mode().

Querying the size of the serial terminal drains the keyboard buffer.
So make sure we do this during the initialization and not in the midst
of an EFI application.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 7ca7c3c0 29-Apr-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: updating the cursor position

The current coding advances the column by the number of UTF-8 bytes.
The column should be increased by one per unicode character.

The UEFI spec provides advance rules for U+0000, U+0008, U+000A,
and U000D. All other characters, including control characters
U+0007 (bel) and U+0009 (tab), have to increase the column by one.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# f739fcd8 07-May-2018 Tom Rini <trini@konsulko.com>

SPDX: Convert a few files that were missed before

As part of the main conversion a few files were missed. These files had
additional whitespace after the '*' and before the SPDX tag and my
previous regex was too strict. This time I did a grep for all SPDX tags
and then filtered out anything that matched the correct styles.

Fixes: 83d290c56fab ("SPDX: Convert all of our single license tags to Linux Kernel style")
Reported-by: Heinrich Schuchardt <xypron.debian@gmx.de>
Signed-off-by: Tom Rini <trini@konsulko.com>

# b095f3c8 18-Feb-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: implement event groups

If an event of a group event is signaled all other events of the same
group are signaled too.

Function efi_signal_event is renamed to efi_queue_event.
A new function efi_signal_event is introduced that checks if an event
belongs to a group and than signals all events of the group.
Event group notifciation is implemented for ExitBootServices,
InstallConfigurationTable, and ResetSystem.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 0fb4169e 19-Feb-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: correct input of special keys

Don't set unicode_char if scan_code is set.
Add support for page up, page down, and insert.
Correct input of function keys.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 8396e3fd 03-Mar-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: remove deprecated ConsoleControlProtocol

The console control protocol is not defined in the UEFI standard.

It exists in EDK2's EdkCompatiblityPkg package. But this package
is deprecated according to
https://github.com/tianocore/tianocore.github.io/wiki/Differences-between-EDK-and-EDK-II

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 9bc9664d 19-Jan-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: add check_tpl parameter to efi_signal_event

In ExitBootServices we need to signal events irrespective of the current
TPL level. A new parameter check_tpl is added to efi_signal_event().

Function efi_console_timer_notify() gets some comments.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 2074f700 11-Jan-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: consistently use efi_handle_t for handles

We should consistently use the efi_handle_t typedef when
referring to handles.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# ebb4dd5b 26-Oct-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: efi_console: use helper functions

Use helper functions efi_created_handle and efi_add_protocol
for creating the console handles and instaling the respective
protocols.

This change is needed if we want to move from an array of
protocols to a linked list of protocols.

Eliminate EFI_PROTOCOL_OBJECT which is not used anymore.

Currently we have not defined protocol interfaces to be const.
So efi_con_out and efi_console_control cannot be defined as const.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 2d5dc2a5 10-Oct-2017 Rob Clark <robdclark@gmail.com>

efi_loader: console support for color attributes

Shell.efi uses this, and supporting color attributes makes things look
nicer. Map the EFI fg/bg color attributes to ANSI escape sequences.
Not all colors have a perfect match, but spec just says "Devices
supporting a different number of text colors are required to emulate the
above colors to the best of the device’s capabilities".

Signed-off-by: Rob Clark <robdclark@gmail.com>
Tested-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Alexander Graf <agraf@suse.de>
[agraf: s/unsigned/unsigned int/]
Signed-off-by: Alexander Graf <agraf@suse.de>

# e190e897 04-Oct-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: use type bool for event states

Queued and signaled describe boolean states of events.
So let's use type bool and rename the structure members to is_queued
and is_signaled.

Update the comments for is_queued and is_signaled.

Reported-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Rob Clark <robdclark@gmail.com>
Reviewed-by: Rob Clark <robdclark@gmail.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 3a45bc7f 13-Sep-2017 Rob Clark <robdclark@gmail.com>

efi_loader: Some console improvements for vidconsole

1) use fputs() to reduce cache flushes from once-per-char to
once-per-string
2) handle \r, \t, and \b in addition to just \n for tracking
cursor position
3) cursor row/col are zero based, not one based

Signed-off-by: Rob Clark <robdclark@gmail.com>
[agraf: s/unsigned/unsigned int/]
Signed-off-by: Alexander Graf <agraf@suse.de>

# a18c5a83 13-Sep-2017 Rob Clark <robdclark@gmail.com>

efi_loader: Correctly figure out size for vidconsole

If stdout is vidconsole, we cannot rely on ANSI escape sequences to
query the size, as vidconsole cannot reply on stdin. Instead special-
case this if stdout is vidconsole.

Signed-off-by: Rob Clark <robdclark@gmail.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 71cc25c3 13-Sep-2017 Rob Clark <robdclark@gmail.com>

efi_loader: split out escape sequence based size query

We need to do something different for vidconsole, since it cannot
respond to the query on stdin. Prep work for next patch.

Signed-off-by: Rob Clark <robdclark@gmail.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# ca62a4f5 15-Sep-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: implement queueing of the notification function

For the correct implementation of the task priority level (TPL)
calling the notification function must be queued.

Add a status field 'queued' to events.

In function efi_signal_event set status queued if a notification
function exists and reset it after we have called the function.
A later patch will add a check of the TPL here.

In efi_create_event and efi_close_event unset the queued status.

In function efi_wait_for_event and efi_check_event
queue the notification function.

In efi_timer_check call the efi_notify_event
if the status queued is set.
For all timer events set status signaled.

In efi_console_timer_notify set the signaled state of the
WaitForKey event.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 78178bb0 09-Sep-2017 Rob Clark <robdclark@gmail.com>

lib: add some utf16 handling helpers

We'll eventually want these in a few places in efi_loader, and also
vsprintf.

Signed-off-by: Rob Clark <robdclark@gmail.com>

# a17e62cc 24-Jul-2017 Rob Clark <robdclark@gmail.com>

efi_loader: expose protocols via GUID

shim.efi (or rather gnu-efi's LibLocateProtocol() which shim.efi uses)
resolves protocols via efi_locate_handle() so the console protocols
need to be added to the efi object list.

Signed-off-by: Rob Clark <robdclark@gmail.com>
[agraf: whitespace fixes]
Signed-off-by: Alexander Graf <agraf@suse.de>

# ff925938 19-Jul-2017 xypron.glpk@gmx.de <xypron.glpk@gmx.de>

efi_console: use EFIAPI for callback functions

The call to efi_create_event requires notification functions
flagged as EFIAPI.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 91be9a77 18-Jul-2017 xypron.glpk@gmx.de <xypron.glpk@gmx.de>

efi_console: set up events

Set up a timer event and the WaitForKey event.
In the notify function of the timer event check for console input
and signal the WaitForKey event accordingly.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 5be8b0a3 07-Nov-2016 Emmanuel Vadot <manu@bidouilliste.com>

efi_loader: console: Correctly report modes

Add support for EFI console modes.
Mode 0 is always 80x25 and present by EFI specification.
Mode 1 is always 80x50 and not mandatory.
Mode 2 and above is freely usable.

If the terminal can handle mode 1, we mark it as supported.
If the terminal size is greater than mode 0 and different than mode 1,
we install it as mode 2.

Modes can be switch with cout_set_mode.

Changes in V5:
Correctly detect mode before enabling mode 2.

Changes in V4:
Reset cursor positon on mode switch
Use local variables in console query code

Changes in V3:
Valid mode are 0 to EFIMode-1
Fix style

Changes in V2:
Add mode switch
Report only the modes that we support

Signed-off-by: Emmanuel Vadot <manu@bidouilliste.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# c1311ad4 03-Mar-2016 Alexander Graf <agraf@csgraf.de>

efi_loader: Add console interface

One of the basic EFI interfaces is the console interface. Using it an EFI
application can interface with the user. This patch implements an EFI console
interface using getc() and putc().

Today, we only implement text based consoles. We also convert the EFI Unicode
characters to UTF-8 on the fly, hoping that everyone managed to jump on the
train by now.

Signed-off-by: Alexander Graf <agraf@suse.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Tested-by: Simon Glass <sjg@chromium.org>

# 997b57fc 28-Feb-2021 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: console size of vidconsole

If stdout is 'vidconsole', we correctly set the console size.
If stdout is 'vidconsole,serial', the video console is ignored.

We should always evaluate the size of vidconsole if it is the primary
console.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 7913c7dc 04-Jan-2021 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: typedef efi_string_t text output protocol

We do not want to use typedefs in U-Boot.

Do not use efi_string_t in the EFI_TEXT_OUTPUT_PROTOCOL.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 9abb01af 27-Dec-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: escape key handling

Up to now the escape key was not correctly detected in UEFI applications.
We had to hit it twice for a single escape to be recognized.

Use a 10 ms delay to detect if we are dealing with the escape key or an
escape sequence.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# c670aeee 07-Oct-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

common: rename getc() to getchar()

The sandbox is built with the SDL2 library with invokes the X11 library
which in turn calls getc(). But getc() in glibc is defined as

int getc(FILE *)

This does not match our definition.

int getc(void)

The sandbox crashes when called with parameter -l.

Rename our library symbol getc() to getchar().

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Tom Rini <trini@konsulko.com>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 325567d3 04-Jun-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: comments EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL

Provide missing comments for the functions implementing the
EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 336d4615 03-Feb-2020 Simon Glass <sjg@chromium.org>

dm: core: Create a new header file for 'compat' features

At present dm/device.h includes the linux-compatible features. This
requires including linux/compat.h which in turn includes a lot of headers.
One of these is malloc.h which we thus end up including in every file in
U-Boot. Apart from the inefficiency of this, it is problematic for sandbox
which needs to use the system malloc() in some files.

Move the compatibility features into a separate header file.

Signed-off-by: Simon Glass <sjg@chromium.org>

# b0ad9b5b 22-Dec-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: clear screen should move cursor to home

On a VT100 terminal <ESC>[2J should be enough to both clear the whole
screen and set the cursor to position (1, 1). But the Linux console does
not behave like this. So send an extra <ESC>[H. For reference see the
console_codes(4) man page.

Add a function description.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 1045315d 14-Nov-2019 Simon Glass <sjg@chromium.org>

common: Move get_ticks() function out of common.h

This function belongs in time.h so move it over and add a comment.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Tom Rini <trini@konsulko.com>

# fe1a81c1 05-Sep-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

doc: UEFI API documentation

Add some more files to the UEFI API documentation.

Correct some Sphinx comments.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 03446987 04-Sep-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: do not set invalid screen mode

EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.SetMode() should return EFI_UNDEFINED if a
screen mode is not available.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Alexander Graf <agraf@csgraf.de>

# 97ea0690 04-Sep-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: cursor positioning

When backspacing in column 0 do no set the column index to ULONG_MAX.
Ensure that the row number is not set to ULONG_MAX even if the row count is
advertised as 0.
Ignore control characters other the 0x08, 0x0a, 0x0d when updating the
column.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 7b51b576 01-Aug-2019 Simon Glass <sjg@chromium.org>

env: Move env_get() to env.h

Move env_get() over to the new header file.

Acked-by: Joe Hershberger <joe.hershberger@ni.com>
Signed-off-by: Simon Glass <sjg@chromium.org>

# 3b435c11 16-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: console incorrectly advertised left logo key

Avoid to signal that the left logo key is pressed, when it is not.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 3b2b2de8 16-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: alternative scan codes for F5, END, HOME

Depending on the key board alternative scan codes are used for F5, END, and
HOME.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 3950f0f8 13-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix SetAttribute()

The SetAttribute() service and the Reset() service of the simple text
output protocol must update the attribute value in the mode information.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 2ad238fc 13-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix SetMode()

Correct the check of the mode number in SetMode() service of the simple
text output protocol.

Clear the screen in SetMode().

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 7eaa900e 06-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: event signaling in ExitBootServices

ExitBootServices() has to stop timer related activity before calling the
events of the EFI_EVENT_GROUP_EXIT_BOOT_SERVICES event group. But our
current implementation was stopping all other events.

All events have to observe the task priority level.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 25e6fb5e 02-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix EnableCursor()

The EnableCursor() service of the simple text output protocol must update
the the CursorVisible field of the output mode.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# b31ca6bf 18-May-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: parameter check OutputString

Check the parameters against NULL.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# acee9652 18-May-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.SetState() correct parameter

KeyToggleState is a pointer according to UEFI spec 2.8.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# bfc2dd53 06-Apr-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: correct CTRL-A - CTRL-Z console input

In the extended text input protocol CTRL-A - CTRL-Z have to be signaled as
Unicode characters a-z or A-Z depending on the shift state and not as 0x01
to 0x1a.

Update Python unit test.

This patch is required for using the EFI shell `edit` command.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# dd1a1ec2 04-Mar-2019 Matthias Brugger <mbrugger@suse.com>

efi_loader: Fix serial console size detection

Function term_read_reply tries to read from the serial console until
the end_char was read. This can hang forever if we are, for some reason,
not able to read the full response (e.g. serial buffer too small,
frame error). This patch moves the timeout detection into
term_read_reply() to assure we will make progress.

Fixes: 6bb591f704 ("efi_loader: query serial console size reliably")
Signed-off-by: Matthias Brugger <mbrugger@suse.com>

Throw missing error when an incomplete reply for the cursor position is
received.

Change type of argument of term_get_char() *s32. This renders the function
reusable in efi_cin_read_key().

Reviewed-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# e1fec152 18-Oct-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix typos

Fix typos in EFI subsystem comments.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# fae0118e 25-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: eliminate handle member

A pointer to a struct efi_object is a handle. We do not need any handle
member in this structure. Let's eliminate it.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 6f566c23 01-Oct-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: return type efi_console_register()

Use a return type that can encompass the return value.

This fixes CoverityScan CID 184090.

Reported-by: Tom Rini <trini@konsulko.com>
Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 6bb591f7 15-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: query serial console size reliably

Not all terminals understand CSI [18t for querying the console size.
We should adhere to escape sequences documented in the console_codes
manpage and the ECMA-48 standard.

So here we follow a different approach. We position the cursor to the
bottom right and query its position. Before leaving the function we
restore the original cursor position.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# aaace4b0 14-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: terminal left upper corner is [1, 1]

For the VT102 and the Linux console the left upper corner is [1, 1].
EFI uses [0, 0] as left upper corner.

Check parameters of the SetCursorPosition() service.

Use CSI H like EDK2 for positioning.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 4fdcf066 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: implement key notify functions

Implement registering and unregistreing key notify functions in the
EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 55fbdf99 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: support modifiers for F1 - F4

Support modifiers for F1 - F4.
Add support for letters with ALT key.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 110c6280 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL

This patch implements the EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.

The implementation of notification functions is postponed to a later
patch.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 0dfd13a4 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: rework event handling for console

Preread the next key in the console timer event.

The EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL requires to trigger registered key
notification functions based on the prefetched key.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 35cbb796 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: support Unicode text input

Up to now the EFI_TEXT_INPUT_PROTOCOL only supported ASCII characters.
With the patch it can consume UTF-8 from the console.

Currently only the serial console and the console can deliver UTF-8.
Local consoles are restricted to ASCII.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 14d103bb 08-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: typos in efi_console.c

Fix a few typos.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 3e603ec7 08-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: struct efi_simple_text_input_protocol

%s/efi_simple_input_interface/efi_simple_text_input_protocol/

We should be consistent in the naming of the EFI protocol interface
structures. The protocol is called EFI_SIMPLE_TEXT_INPUT_PROTOCOL.

%s/ExtendedVerification/extended_verification/

Use consistent naming of function parameters. Do not use CamelCase.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 40e3e757 04-Sep-2018 Alexander Graf <agraf@csgraf.de>

efi_loader: Add console handle references to in/out/err

We register the console in/out protocol on the loaded image handle, but
never tell anyone about it in the systab.

Because of this Shell.efi can not find the input protocol. So let's add
a proper reference.

Signed-off-by: Alexander Graf <agraf@suse.de>
Reviewed-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# ba7bd5c2 31-Aug-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: don't use unlimited stack as buffer

The length of a string printed to the console by the
EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL is not limited by the UEFI spec.
Hence should not allocate a buffer for it on the stack.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 1dde0d57 31-Aug-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: rename utf16_strlen, utf16_strnlen

The function names utf16_strlen() and utf16_strnlen() are misnomers.
The functions do not count utf-16 characters but non-zero words.
So let's rename them to u16_strlen and u16_strnlen().

In utf16_dup() avoid assignment in if clause.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 9d12daff 05-Jul-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.Reset()

Implement the reset service of the EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.

This should resolve the error reported by the SCT in
Protocol/SimpleTextOut/BlackBoxTest/SimpleTextOutBBTestFunction_uefi.c:639

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# e67ff94d 05-Jul-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: clear screen has to reset cursor position

After clearing the screen the cursor position is row 0, column 0.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 4f187897 05-Jul-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_INPUT_PROTOCOL.Reset()

Implement the reset service of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL.

This should resolve the error reported by the SCT in
Protocol/SimpleTextIn/BlackBoxTest/SimpleTextInBBTestFunction.c:193

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 80483b2a 03-Jun-2018 Alexander Graf <agraf@csgraf.de>

efi_loader: Fix warning in raw/cols query

The code to determine rows / cols on the screen could potentially run
into a case where it doesn't know how big the screen is. In that case,
assume 80x25.

Signed-off-by: Alexander Graf <agraf@suse.de>

# 62217295 16-May-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: avoid using unitialized values as console size

If a request for the console size would be answered with a response
with less then three values, uninitialized stack memory would be
copied to the number of rows and columns of the terminal.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# a4aa7bef 29-Apr-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: always check parameters in efi_cout_query_mode()

If we cannot determine the size of the serial terminal we still have
to check the parameters of efi_cout_query_mode().

Querying the size of the serial terminal drains the keyboard buffer.
So make sure we do this during the initialization and not in the midst
of an EFI application.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 7ca7c3c0 29-Apr-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: updating the cursor position

The current coding advances the column by the number of UTF-8 bytes.
The column should be increased by one per unicode character.

The UEFI spec provides advance rules for U+0000, U+0008, U+000A,
and U000D. All other characters, including control characters
U+0007 (bel) and U+0009 (tab), have to increase the column by one.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# f739fcd8 07-May-2018 Tom Rini <trini@konsulko.com>

SPDX: Convert a few files that were missed before

As part of the main conversion a few files were missed. These files had
additional whitespace after the '*' and before the SPDX tag and my
previous regex was too strict. This time I did a grep for all SPDX tags
and then filtered out anything that matched the correct styles.

Fixes: 83d290c56fab ("SPDX: Convert all of our single license tags to Linux Kernel style")
Reported-by: Heinrich Schuchardt <xypron.debian@gmx.de>
Signed-off-by: Tom Rini <trini@konsulko.com>

# b095f3c8 18-Feb-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: implement event groups

If an event of a group event is signaled all other events of the same
group are signaled too.

Function efi_signal_event is renamed to efi_queue_event.
A new function efi_signal_event is introduced that checks if an event
belongs to a group and than signals all events of the group.
Event group notifciation is implemented for ExitBootServices,
InstallConfigurationTable, and ResetSystem.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 0fb4169e 19-Feb-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: correct input of special keys

Don't set unicode_char if scan_code is set.
Add support for page up, page down, and insert.
Correct input of function keys.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 8396e3fd 03-Mar-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: remove deprecated ConsoleControlProtocol

The console control protocol is not defined in the UEFI standard.

It exists in EDK2's EdkCompatiblityPkg package. But this package
is deprecated according to
https://github.com/tianocore/tianocore.github.io/wiki/Differences-between-EDK-and-EDK-II

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 9bc9664d 19-Jan-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: add check_tpl parameter to efi_signal_event

In ExitBootServices we need to signal events irrespective of the current
TPL level. A new parameter check_tpl is added to efi_signal_event().

Function efi_console_timer_notify() gets some comments.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 2074f700 11-Jan-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: consistently use efi_handle_t for handles

We should consistently use the efi_handle_t typedef when
referring to handles.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# ebb4dd5b 26-Oct-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: efi_console: use helper functions

Use helper functions efi_created_handle and efi_add_protocol
for creating the console handles and instaling the respective
protocols.

This change is needed if we want to move from an array of
protocols to a linked list of protocols.

Eliminate EFI_PROTOCOL_OBJECT which is not used anymore.

Currently we have not defined protocol interfaces to be const.
So efi_con_out and efi_console_control cannot be defined as const.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 2d5dc2a5 10-Oct-2017 Rob Clark <robdclark@gmail.com>

efi_loader: console support for color attributes

Shell.efi uses this, and supporting color attributes makes things look
nicer. Map the EFI fg/bg color attributes to ANSI escape sequences.
Not all colors have a perfect match, but spec just says "Devices
supporting a different number of text colors are required to emulate the
above colors to the best of the device’s capabilities".

Signed-off-by: Rob Clark <robdclark@gmail.com>
Tested-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Alexander Graf <agraf@suse.de>
[agraf: s/unsigned/unsigned int/]
Signed-off-by: Alexander Graf <agraf@suse.de>

# e190e897 04-Oct-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: use type bool for event states

Queued and signaled describe boolean states of events.
So let's use type bool and rename the structure members to is_queued
and is_signaled.

Update the comments for is_queued and is_signaled.

Reported-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Rob Clark <robdclark@gmail.com>
Reviewed-by: Rob Clark <robdclark@gmail.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 3a45bc7f 13-Sep-2017 Rob Clark <robdclark@gmail.com>

efi_loader: Some console improvements for vidconsole

1) use fputs() to reduce cache flushes from once-per-char to
once-per-string
2) handle \r, \t, and \b in addition to just \n for tracking
cursor position
3) cursor row/col are zero based, not one based

Signed-off-by: Rob Clark <robdclark@gmail.com>
[agraf: s/unsigned/unsigned int/]
Signed-off-by: Alexander Graf <agraf@suse.de>

# a18c5a83 13-Sep-2017 Rob Clark <robdclark@gmail.com>

efi_loader: Correctly figure out size for vidconsole

If stdout is vidconsole, we cannot rely on ANSI escape sequences to
query the size, as vidconsole cannot reply on stdin. Instead special-
case this if stdout is vidconsole.

Signed-off-by: Rob Clark <robdclark@gmail.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 71cc25c3 13-Sep-2017 Rob Clark <robdclark@gmail.com>

efi_loader: split out escape sequence based size query

We need to do something different for vidconsole, since it cannot
respond to the query on stdin. Prep work for next patch.

Signed-off-by: Rob Clark <robdclark@gmail.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# ca62a4f5 15-Sep-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: implement queueing of the notification function

For the correct implementation of the task priority level (TPL)
calling the notification function must be queued.

Add a status field 'queued' to events.

In function efi_signal_event set status queued if a notification
function exists and reset it after we have called the function.
A later patch will add a check of the TPL here.

In efi_create_event and efi_close_event unset the queued status.

In function efi_wait_for_event and efi_check_event
queue the notification function.

In efi_timer_check call the efi_notify_event
if the status queued is set.
For all timer events set status signaled.

In efi_console_timer_notify set the signaled state of the
WaitForKey event.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 78178bb0 09-Sep-2017 Rob Clark <robdclark@gmail.com>

lib: add some utf16 handling helpers

We'll eventually want these in a few places in efi_loader, and also
vsprintf.

Signed-off-by: Rob Clark <robdclark@gmail.com>

# a17e62cc 24-Jul-2017 Rob Clark <robdclark@gmail.com>

efi_loader: expose protocols via GUID

shim.efi (or rather gnu-efi's LibLocateProtocol() which shim.efi uses)
resolves protocols via efi_locate_handle() so the console protocols
need to be added to the efi object list.

Signed-off-by: Rob Clark <robdclark@gmail.com>
[agraf: whitespace fixes]
Signed-off-by: Alexander Graf <agraf@suse.de>

# ff925938 19-Jul-2017 xypron.glpk@gmx.de <xypron.glpk@gmx.de>

efi_console: use EFIAPI for callback functions

The call to efi_create_event requires notification functions
flagged as EFIAPI.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 91be9a77 18-Jul-2017 xypron.glpk@gmx.de <xypron.glpk@gmx.de>

efi_console: set up events

Set up a timer event and the WaitForKey event.
In the notify function of the timer event check for console input
and signal the WaitForKey event accordingly.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 5be8b0a3 07-Nov-2016 Emmanuel Vadot <manu@bidouilliste.com>

efi_loader: console: Correctly report modes

Add support for EFI console modes.
Mode 0 is always 80x25 and present by EFI specification.
Mode 1 is always 80x50 and not mandatory.
Mode 2 and above is freely usable.

If the terminal can handle mode 1, we mark it as supported.
If the terminal size is greater than mode 0 and different than mode 1,
we install it as mode 2.

Modes can be switch with cout_set_mode.

Changes in V5:
Correctly detect mode before enabling mode 2.

Changes in V4:
Reset cursor positon on mode switch
Use local variables in console query code

Changes in V3:
Valid mode are 0 to EFIMode-1
Fix style

Changes in V2:
Add mode switch
Report only the modes that we support

Signed-off-by: Emmanuel Vadot <manu@bidouilliste.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# c1311ad4 03-Mar-2016 Alexander Graf <agraf@csgraf.de>

efi_loader: Add console interface

One of the basic EFI interfaces is the console interface. Using it an EFI
application can interface with the user. This patch implements an EFI console
interface using getc() and putc().

Today, we only implement text based consoles. We also convert the EFI Unicode
characters to UTF-8 on the fly, hoping that everyone managed to jump on the
train by now.

Signed-off-by: Alexander Graf <agraf@suse.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Tested-by: Simon Glass <sjg@chromium.org>

# 7913c7dc 04-Jan-2021 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: typedef efi_string_t text output protocol

We do not want to use typedefs in U-Boot.

Do not use efi_string_t in the EFI_TEXT_OUTPUT_PROTOCOL.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 9abb01af 27-Dec-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: escape key handling

Up to now the escape key was not correctly detected in UEFI applications.
We had to hit it twice for a single escape to be recognized.

Use a 10 ms delay to detect if we are dealing with the escape key or an
escape sequence.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# c670aeee 07-Oct-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

common: rename getc() to getchar()

The sandbox is built with the SDL2 library with invokes the X11 library
which in turn calls getc(). But getc() in glibc is defined as

int getc(FILE *)

This does not match our definition.

int getc(void)

The sandbox crashes when called with parameter -l.

Rename our library symbol getc() to getchar().

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Tom Rini <trini@konsulko.com>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 325567d3 04-Jun-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: comments EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL

Provide missing comments for the functions implementing the
EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 336d4615 03-Feb-2020 Simon Glass <sjg@chromium.org>

dm: core: Create a new header file for 'compat' features

At present dm/device.h includes the linux-compatible features. This
requires including linux/compat.h which in turn includes a lot of headers.
One of these is malloc.h which we thus end up including in every file in
U-Boot. Apart from the inefficiency of this, it is problematic for sandbox
which needs to use the system malloc() in some files.

Move the compatibility features into a separate header file.

Signed-off-by: Simon Glass <sjg@chromium.org>

# b0ad9b5b 22-Dec-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: clear screen should move cursor to home

On a VT100 terminal <ESC>[2J should be enough to both clear the whole
screen and set the cursor to position (1, 1). But the Linux console does
not behave like this. So send an extra <ESC>[H. For reference see the
console_codes(4) man page.

Add a function description.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 1045315d 14-Nov-2019 Simon Glass <sjg@chromium.org>

common: Move get_ticks() function out of common.h

This function belongs in time.h so move it over and add a comment.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Tom Rini <trini@konsulko.com>

# fe1a81c1 05-Sep-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

doc: UEFI API documentation

Add some more files to the UEFI API documentation.

Correct some Sphinx comments.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 03446987 04-Sep-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: do not set invalid screen mode

EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.SetMode() should return EFI_UNDEFINED if a
screen mode is not available.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Alexander Graf <agraf@csgraf.de>

# 97ea0690 04-Sep-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: cursor positioning

When backspacing in column 0 do no set the column index to ULONG_MAX.
Ensure that the row number is not set to ULONG_MAX even if the row count is
advertised as 0.
Ignore control characters other the 0x08, 0x0a, 0x0d when updating the
column.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 7b51b576 01-Aug-2019 Simon Glass <sjg@chromium.org>

env: Move env_get() to env.h

Move env_get() over to the new header file.

Acked-by: Joe Hershberger <joe.hershberger@ni.com>
Signed-off-by: Simon Glass <sjg@chromium.org>

# 3b435c11 16-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: console incorrectly advertised left logo key

Avoid to signal that the left logo key is pressed, when it is not.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 3b2b2de8 16-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: alternative scan codes for F5, END, HOME

Depending on the key board alternative scan codes are used for F5, END, and
HOME.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 3950f0f8 13-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix SetAttribute()

The SetAttribute() service and the Reset() service of the simple text
output protocol must update the attribute value in the mode information.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 2ad238fc 13-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix SetMode()

Correct the check of the mode number in SetMode() service of the simple
text output protocol.

Clear the screen in SetMode().

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 7eaa900e 06-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: event signaling in ExitBootServices

ExitBootServices() has to stop timer related activity before calling the
events of the EFI_EVENT_GROUP_EXIT_BOOT_SERVICES event group. But our
current implementation was stopping all other events.

All events have to observe the task priority level.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 25e6fb5e 02-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix EnableCursor()

The EnableCursor() service of the simple text output protocol must update
the the CursorVisible field of the output mode.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# b31ca6bf 18-May-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: parameter check OutputString

Check the parameters against NULL.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# acee9652 18-May-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.SetState() correct parameter

KeyToggleState is a pointer according to UEFI spec 2.8.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# bfc2dd53 06-Apr-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: correct CTRL-A - CTRL-Z console input

In the extended text input protocol CTRL-A - CTRL-Z have to be signaled as
Unicode characters a-z or A-Z depending on the shift state and not as 0x01
to 0x1a.

Update Python unit test.

This patch is required for using the EFI shell `edit` command.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# dd1a1ec2 04-Mar-2019 Matthias Brugger <mbrugger@suse.com>

efi_loader: Fix serial console size detection

Function term_read_reply tries to read from the serial console until
the end_char was read. This can hang forever if we are, for some reason,
not able to read the full response (e.g. serial buffer too small,
frame error). This patch moves the timeout detection into
term_read_reply() to assure we will make progress.

Fixes: 6bb591f704 ("efi_loader: query serial console size reliably")
Signed-off-by: Matthias Brugger <mbrugger@suse.com>

Throw missing error when an incomplete reply for the cursor position is
received.

Change type of argument of term_get_char() *s32. This renders the function
reusable in efi_cin_read_key().

Reviewed-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# e1fec152 18-Oct-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix typos

Fix typos in EFI subsystem comments.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# fae0118e 25-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: eliminate handle member

A pointer to a struct efi_object is a handle. We do not need any handle
member in this structure. Let's eliminate it.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 6f566c23 01-Oct-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: return type efi_console_register()

Use a return type that can encompass the return value.

This fixes CoverityScan CID 184090.

Reported-by: Tom Rini <trini@konsulko.com>
Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 6bb591f7 15-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: query serial console size reliably

Not all terminals understand CSI [18t for querying the console size.
We should adhere to escape sequences documented in the console_codes
manpage and the ECMA-48 standard.

So here we follow a different approach. We position the cursor to the
bottom right and query its position. Before leaving the function we
restore the original cursor position.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# aaace4b0 14-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: terminal left upper corner is [1, 1]

For the VT102 and the Linux console the left upper corner is [1, 1].
EFI uses [0, 0] as left upper corner.

Check parameters of the SetCursorPosition() service.

Use CSI H like EDK2 for positioning.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 4fdcf066 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: implement key notify functions

Implement registering and unregistreing key notify functions in the
EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 55fbdf99 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: support modifiers for F1 - F4

Support modifiers for F1 - F4.
Add support for letters with ALT key.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 110c6280 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL

This patch implements the EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.

The implementation of notification functions is postponed to a later
patch.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 0dfd13a4 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: rework event handling for console

Preread the next key in the console timer event.

The EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL requires to trigger registered key
notification functions based on the prefetched key.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 35cbb796 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: support Unicode text input

Up to now the EFI_TEXT_INPUT_PROTOCOL only supported ASCII characters.
With the patch it can consume UTF-8 from the console.

Currently only the serial console and the console can deliver UTF-8.
Local consoles are restricted to ASCII.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 14d103bb 08-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: typos in efi_console.c

Fix a few typos.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 3e603ec7 08-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: struct efi_simple_text_input_protocol

%s/efi_simple_input_interface/efi_simple_text_input_protocol/

We should be consistent in the naming of the EFI protocol interface
structures. The protocol is called EFI_SIMPLE_TEXT_INPUT_PROTOCOL.

%s/ExtendedVerification/extended_verification/

Use consistent naming of function parameters. Do not use CamelCase.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 40e3e757 04-Sep-2018 Alexander Graf <agraf@csgraf.de>

efi_loader: Add console handle references to in/out/err

We register the console in/out protocol on the loaded image handle, but
never tell anyone about it in the systab.

Because of this Shell.efi can not find the input protocol. So let's add
a proper reference.

Signed-off-by: Alexander Graf <agraf@suse.de>
Reviewed-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# ba7bd5c2 31-Aug-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: don't use unlimited stack as buffer

The length of a string printed to the console by the
EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL is not limited by the UEFI spec.
Hence should not allocate a buffer for it on the stack.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 1dde0d57 31-Aug-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: rename utf16_strlen, utf16_strnlen

The function names utf16_strlen() and utf16_strnlen() are misnomers.
The functions do not count utf-16 characters but non-zero words.
So let's rename them to u16_strlen and u16_strnlen().

In utf16_dup() avoid assignment in if clause.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 9d12daff 05-Jul-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.Reset()

Implement the reset service of the EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.

This should resolve the error reported by the SCT in
Protocol/SimpleTextOut/BlackBoxTest/SimpleTextOutBBTestFunction_uefi.c:639

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# e67ff94d 05-Jul-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: clear screen has to reset cursor position

After clearing the screen the cursor position is row 0, column 0.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 4f187897 05-Jul-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_INPUT_PROTOCOL.Reset()

Implement the reset service of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL.

This should resolve the error reported by the SCT in
Protocol/SimpleTextIn/BlackBoxTest/SimpleTextInBBTestFunction.c:193

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 80483b2a 03-Jun-2018 Alexander Graf <agraf@csgraf.de>

efi_loader: Fix warning in raw/cols query

The code to determine rows / cols on the screen could potentially run
into a case where it doesn't know how big the screen is. In that case,
assume 80x25.

Signed-off-by: Alexander Graf <agraf@suse.de>

# 62217295 16-May-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: avoid using unitialized values as console size

If a request for the console size would be answered with a response
with less then three values, uninitialized stack memory would be
copied to the number of rows and columns of the terminal.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# a4aa7bef 29-Apr-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: always check parameters in efi_cout_query_mode()

If we cannot determine the size of the serial terminal we still have
to check the parameters of efi_cout_query_mode().

Querying the size of the serial terminal drains the keyboard buffer.
So make sure we do this during the initialization and not in the midst
of an EFI application.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 7ca7c3c0 29-Apr-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: updating the cursor position

The current coding advances the column by the number of UTF-8 bytes.
The column should be increased by one per unicode character.

The UEFI spec provides advance rules for U+0000, U+0008, U+000A,
and U000D. All other characters, including control characters
U+0007 (bel) and U+0009 (tab), have to increase the column by one.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# f739fcd8 07-May-2018 Tom Rini <trini@konsulko.com>

SPDX: Convert a few files that were missed before

As part of the main conversion a few files were missed. These files had
additional whitespace after the '*' and before the SPDX tag and my
previous regex was too strict. This time I did a grep for all SPDX tags
and then filtered out anything that matched the correct styles.

Fixes: 83d290c56fab ("SPDX: Convert all of our single license tags to Linux Kernel style")
Reported-by: Heinrich Schuchardt <xypron.debian@gmx.de>
Signed-off-by: Tom Rini <trini@konsulko.com>

# b095f3c8 18-Feb-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: implement event groups

If an event of a group event is signaled all other events of the same
group are signaled too.

Function efi_signal_event is renamed to efi_queue_event.
A new function efi_signal_event is introduced that checks if an event
belongs to a group and than signals all events of the group.
Event group notifciation is implemented for ExitBootServices,
InstallConfigurationTable, and ResetSystem.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 0fb4169e 19-Feb-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: correct input of special keys

Don't set unicode_char if scan_code is set.
Add support for page up, page down, and insert.
Correct input of function keys.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 8396e3fd 03-Mar-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: remove deprecated ConsoleControlProtocol

The console control protocol is not defined in the UEFI standard.

It exists in EDK2's EdkCompatiblityPkg package. But this package
is deprecated according to
https://github.com/tianocore/tianocore.github.io/wiki/Differences-between-EDK-and-EDK-II

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 9bc9664d 19-Jan-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: add check_tpl parameter to efi_signal_event

In ExitBootServices we need to signal events irrespective of the current
TPL level. A new parameter check_tpl is added to efi_signal_event().

Function efi_console_timer_notify() gets some comments.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 2074f700 11-Jan-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: consistently use efi_handle_t for handles

We should consistently use the efi_handle_t typedef when
referring to handles.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# ebb4dd5b 26-Oct-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: efi_console: use helper functions

Use helper functions efi_created_handle and efi_add_protocol
for creating the console handles and instaling the respective
protocols.

This change is needed if we want to move from an array of
protocols to a linked list of protocols.

Eliminate EFI_PROTOCOL_OBJECT which is not used anymore.

Currently we have not defined protocol interfaces to be const.
So efi_con_out and efi_console_control cannot be defined as const.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 2d5dc2a5 10-Oct-2017 Rob Clark <robdclark@gmail.com>

efi_loader: console support for color attributes

Shell.efi uses this, and supporting color attributes makes things look
nicer. Map the EFI fg/bg color attributes to ANSI escape sequences.
Not all colors have a perfect match, but spec just says "Devices
supporting a different number of text colors are required to emulate the
above colors to the best of the device’s capabilities".

Signed-off-by: Rob Clark <robdclark@gmail.com>
Tested-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Alexander Graf <agraf@suse.de>
[agraf: s/unsigned/unsigned int/]
Signed-off-by: Alexander Graf <agraf@suse.de>

# e190e897 04-Oct-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: use type bool for event states

Queued and signaled describe boolean states of events.
So let's use type bool and rename the structure members to is_queued
and is_signaled.

Update the comments for is_queued and is_signaled.

Reported-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Rob Clark <robdclark@gmail.com>
Reviewed-by: Rob Clark <robdclark@gmail.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 3a45bc7f 13-Sep-2017 Rob Clark <robdclark@gmail.com>

efi_loader: Some console improvements for vidconsole

1) use fputs() to reduce cache flushes from once-per-char to
once-per-string
2) handle \r, \t, and \b in addition to just \n for tracking
cursor position
3) cursor row/col are zero based, not one based

Signed-off-by: Rob Clark <robdclark@gmail.com>
[agraf: s/unsigned/unsigned int/]
Signed-off-by: Alexander Graf <agraf@suse.de>

# a18c5a83 13-Sep-2017 Rob Clark <robdclark@gmail.com>

efi_loader: Correctly figure out size for vidconsole

If stdout is vidconsole, we cannot rely on ANSI escape sequences to
query the size, as vidconsole cannot reply on stdin. Instead special-
case this if stdout is vidconsole.

Signed-off-by: Rob Clark <robdclark@gmail.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 71cc25c3 13-Sep-2017 Rob Clark <robdclark@gmail.com>

efi_loader: split out escape sequence based size query

We need to do something different for vidconsole, since it cannot
respond to the query on stdin. Prep work for next patch.

Signed-off-by: Rob Clark <robdclark@gmail.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# ca62a4f5 15-Sep-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: implement queueing of the notification function

For the correct implementation of the task priority level (TPL)
calling the notification function must be queued.

Add a status field 'queued' to events.

In function efi_signal_event set status queued if a notification
function exists and reset it after we have called the function.
A later patch will add a check of the TPL here.

In efi_create_event and efi_close_event unset the queued status.

In function efi_wait_for_event and efi_check_event
queue the notification function.

In efi_timer_check call the efi_notify_event
if the status queued is set.
For all timer events set status signaled.

In efi_console_timer_notify set the signaled state of the
WaitForKey event.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 78178bb0 09-Sep-2017 Rob Clark <robdclark@gmail.com>

lib: add some utf16 handling helpers

We'll eventually want these in a few places in efi_loader, and also
vsprintf.

Signed-off-by: Rob Clark <robdclark@gmail.com>

# a17e62cc 24-Jul-2017 Rob Clark <robdclark@gmail.com>

efi_loader: expose protocols via GUID

shim.efi (or rather gnu-efi's LibLocateProtocol() which shim.efi uses)
resolves protocols via efi_locate_handle() so the console protocols
need to be added to the efi object list.

Signed-off-by: Rob Clark <robdclark@gmail.com>
[agraf: whitespace fixes]
Signed-off-by: Alexander Graf <agraf@suse.de>

# ff925938 19-Jul-2017 xypron.glpk@gmx.de <xypron.glpk@gmx.de>

efi_console: use EFIAPI for callback functions

The call to efi_create_event requires notification functions
flagged as EFIAPI.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 91be9a77 18-Jul-2017 xypron.glpk@gmx.de <xypron.glpk@gmx.de>

efi_console: set up events

Set up a timer event and the WaitForKey event.
In the notify function of the timer event check for console input
and signal the WaitForKey event accordingly.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 5be8b0a3 07-Nov-2016 Emmanuel Vadot <manu@bidouilliste.com>

efi_loader: console: Correctly report modes

Add support for EFI console modes.
Mode 0 is always 80x25 and present by EFI specification.
Mode 1 is always 80x50 and not mandatory.
Mode 2 and above is freely usable.

If the terminal can handle mode 1, we mark it as supported.
If the terminal size is greater than mode 0 and different than mode 1,
we install it as mode 2.

Modes can be switch with cout_set_mode.

Changes in V5:
Correctly detect mode before enabling mode 2.

Changes in V4:
Reset cursor positon on mode switch
Use local variables in console query code

Changes in V3:
Valid mode are 0 to EFIMode-1
Fix style

Changes in V2:
Add mode switch
Report only the modes that we support

Signed-off-by: Emmanuel Vadot <manu@bidouilliste.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# c1311ad4 03-Mar-2016 Alexander Graf <agraf@csgraf.de>

efi_loader: Add console interface

One of the basic EFI interfaces is the console interface. Using it an EFI
application can interface with the user. This patch implements an EFI console
interface using getc() and putc().

Today, we only implement text based consoles. We also convert the EFI Unicode
characters to UTF-8 on the fly, hoping that everyone managed to jump on the
train by now.

Signed-off-by: Alexander Graf <agraf@suse.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Tested-by: Simon Glass <sjg@chromium.org>

# 9abb01af 27-Dec-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: escape key handling

Up to now the escape key was not correctly detected in UEFI applications.
We had to hit it twice for a single escape to be recognized.

Use a 10 ms delay to detect if we are dealing with the escape key or an
escape sequence.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# c670aeee 07-Oct-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

common: rename getc() to getchar()

The sandbox is built with the SDL2 library with invokes the X11 library
which in turn calls getc(). But getc() in glibc is defined as

int getc(FILE *)

This does not match our definition.

int getc(void)

The sandbox crashes when called with parameter -l.

Rename our library symbol getc() to getchar().

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Tom Rini <trini@konsulko.com>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 325567d3 04-Jun-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: comments EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL

Provide missing comments for the functions implementing the
EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 336d4615 03-Feb-2020 Simon Glass <sjg@chromium.org>

dm: core: Create a new header file for 'compat' features

At present dm/device.h includes the linux-compatible features. This
requires including linux/compat.h which in turn includes a lot of headers.
One of these is malloc.h which we thus end up including in every file in
U-Boot. Apart from the inefficiency of this, it is problematic for sandbox
which needs to use the system malloc() in some files.

Move the compatibility features into a separate header file.

Signed-off-by: Simon Glass <sjg@chromium.org>

# b0ad9b5b 22-Dec-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: clear screen should move cursor to home

On a VT100 terminal <ESC>[2J should be enough to both clear the whole
screen and set the cursor to position (1, 1). But the Linux console does
not behave like this. So send an extra <ESC>[H. For reference see the
console_codes(4) man page.

Add a function description.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 1045315d 14-Nov-2019 Simon Glass <sjg@chromium.org>

common: Move get_ticks() function out of common.h

This function belongs in time.h so move it over and add a comment.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Tom Rini <trini@konsulko.com>

# fe1a81c1 05-Sep-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

doc: UEFI API documentation

Add some more files to the UEFI API documentation.

Correct some Sphinx comments.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 03446987 04-Sep-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: do not set invalid screen mode

EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.SetMode() should return EFI_UNDEFINED if a
screen mode is not available.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Alexander Graf <agraf@csgraf.de>

# 97ea0690 04-Sep-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: cursor positioning

When backspacing in column 0 do no set the column index to ULONG_MAX.
Ensure that the row number is not set to ULONG_MAX even if the row count is
advertised as 0.
Ignore control characters other the 0x08, 0x0a, 0x0d when updating the
column.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 7b51b576 01-Aug-2019 Simon Glass <sjg@chromium.org>

env: Move env_get() to env.h

Move env_get() over to the new header file.

Acked-by: Joe Hershberger <joe.hershberger@ni.com>
Signed-off-by: Simon Glass <sjg@chromium.org>

# 3b435c11 16-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: console incorrectly advertised left logo key

Avoid to signal that the left logo key is pressed, when it is not.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 3b2b2de8 16-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: alternative scan codes for F5, END, HOME

Depending on the key board alternative scan codes are used for F5, END, and
HOME.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 3950f0f8 13-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix SetAttribute()

The SetAttribute() service and the Reset() service of the simple text
output protocol must update the attribute value in the mode information.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 2ad238fc 13-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix SetMode()

Correct the check of the mode number in SetMode() service of the simple
text output protocol.

Clear the screen in SetMode().

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 7eaa900e 06-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: event signaling in ExitBootServices

ExitBootServices() has to stop timer related activity before calling the
events of the EFI_EVENT_GROUP_EXIT_BOOT_SERVICES event group. But our
current implementation was stopping all other events.

All events have to observe the task priority level.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 25e6fb5e 02-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix EnableCursor()

The EnableCursor() service of the simple text output protocol must update
the the CursorVisible field of the output mode.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# b31ca6bf 18-May-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: parameter check OutputString

Check the parameters against NULL.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# acee9652 18-May-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.SetState() correct parameter

KeyToggleState is a pointer according to UEFI spec 2.8.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# bfc2dd53 06-Apr-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: correct CTRL-A - CTRL-Z console input

In the extended text input protocol CTRL-A - CTRL-Z have to be signaled as
Unicode characters a-z or A-Z depending on the shift state and not as 0x01
to 0x1a.

Update Python unit test.

This patch is required for using the EFI shell `edit` command.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# dd1a1ec2 04-Mar-2019 Matthias Brugger <mbrugger@suse.com>

efi_loader: Fix serial console size detection

Function term_read_reply tries to read from the serial console until
the end_char was read. This can hang forever if we are, for some reason,
not able to read the full response (e.g. serial buffer too small,
frame error). This patch moves the timeout detection into
term_read_reply() to assure we will make progress.

Fixes: 6bb591f704 ("efi_loader: query serial console size reliably")
Signed-off-by: Matthias Brugger <mbrugger@suse.com>

Throw missing error when an incomplete reply for the cursor position is
received.

Change type of argument of term_get_char() *s32. This renders the function
reusable in efi_cin_read_key().

Reviewed-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# e1fec152 18-Oct-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix typos

Fix typos in EFI subsystem comments.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# fae0118e 25-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: eliminate handle member

A pointer to a struct efi_object is a handle. We do not need any handle
member in this structure. Let's eliminate it.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 6f566c23 01-Oct-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: return type efi_console_register()

Use a return type that can encompass the return value.

This fixes CoverityScan CID 184090.

Reported-by: Tom Rini <trini@konsulko.com>
Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 6bb591f7 15-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: query serial console size reliably

Not all terminals understand CSI [18t for querying the console size.
We should adhere to escape sequences documented in the console_codes
manpage and the ECMA-48 standard.

So here we follow a different approach. We position the cursor to the
bottom right and query its position. Before leaving the function we
restore the original cursor position.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# aaace4b0 14-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: terminal left upper corner is [1, 1]

For the VT102 and the Linux console the left upper corner is [1, 1].
EFI uses [0, 0] as left upper corner.

Check parameters of the SetCursorPosition() service.

Use CSI H like EDK2 for positioning.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 4fdcf066 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: implement key notify functions

Implement registering and unregistreing key notify functions in the
EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 55fbdf99 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: support modifiers for F1 - F4

Support modifiers for F1 - F4.
Add support for letters with ALT key.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 110c6280 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL

This patch implements the EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.

The implementation of notification functions is postponed to a later
patch.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 0dfd13a4 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: rework event handling for console

Preread the next key in the console timer event.

The EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL requires to trigger registered key
notification functions based on the prefetched key.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 35cbb796 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: support Unicode text input

Up to now the EFI_TEXT_INPUT_PROTOCOL only supported ASCII characters.
With the patch it can consume UTF-8 from the console.

Currently only the serial console and the console can deliver UTF-8.
Local consoles are restricted to ASCII.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 14d103bb 08-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: typos in efi_console.c

Fix a few typos.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 3e603ec7 08-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: struct efi_simple_text_input_protocol

%s/efi_simple_input_interface/efi_simple_text_input_protocol/

We should be consistent in the naming of the EFI protocol interface
structures. The protocol is called EFI_SIMPLE_TEXT_INPUT_PROTOCOL.

%s/ExtendedVerification/extended_verification/

Use consistent naming of function parameters. Do not use CamelCase.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 40e3e757 04-Sep-2018 Alexander Graf <agraf@csgraf.de>

efi_loader: Add console handle references to in/out/err

We register the console in/out protocol on the loaded image handle, but
never tell anyone about it in the systab.

Because of this Shell.efi can not find the input protocol. So let's add
a proper reference.

Signed-off-by: Alexander Graf <agraf@suse.de>
Reviewed-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# ba7bd5c2 31-Aug-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: don't use unlimited stack as buffer

The length of a string printed to the console by the
EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL is not limited by the UEFI spec.
Hence should not allocate a buffer for it on the stack.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 1dde0d57 31-Aug-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: rename utf16_strlen, utf16_strnlen

The function names utf16_strlen() and utf16_strnlen() are misnomers.
The functions do not count utf-16 characters but non-zero words.
So let's rename them to u16_strlen and u16_strnlen().

In utf16_dup() avoid assignment in if clause.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 9d12daff 05-Jul-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.Reset()

Implement the reset service of the EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.

This should resolve the error reported by the SCT in
Protocol/SimpleTextOut/BlackBoxTest/SimpleTextOutBBTestFunction_uefi.c:639

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# e67ff94d 05-Jul-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: clear screen has to reset cursor position

After clearing the screen the cursor position is row 0, column 0.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 4f187897 05-Jul-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_INPUT_PROTOCOL.Reset()

Implement the reset service of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL.

This should resolve the error reported by the SCT in
Protocol/SimpleTextIn/BlackBoxTest/SimpleTextInBBTestFunction.c:193

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 80483b2a 03-Jun-2018 Alexander Graf <agraf@csgraf.de>

efi_loader: Fix warning in raw/cols query

The code to determine rows / cols on the screen could potentially run
into a case where it doesn't know how big the screen is. In that case,
assume 80x25.

Signed-off-by: Alexander Graf <agraf@suse.de>

# 62217295 16-May-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: avoid using unitialized values as console size

If a request for the console size would be answered with a response
with less then three values, uninitialized stack memory would be
copied to the number of rows and columns of the terminal.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# a4aa7bef 29-Apr-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: always check parameters in efi_cout_query_mode()

If we cannot determine the size of the serial terminal we still have
to check the parameters of efi_cout_query_mode().

Querying the size of the serial terminal drains the keyboard buffer.
So make sure we do this during the initialization and not in the midst
of an EFI application.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 7ca7c3c0 29-Apr-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: updating the cursor position

The current coding advances the column by the number of UTF-8 bytes.
The column should be increased by one per unicode character.

The UEFI spec provides advance rules for U+0000, U+0008, U+000A,
and U000D. All other characters, including control characters
U+0007 (bel) and U+0009 (tab), have to increase the column by one.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# f739fcd8 07-May-2018 Tom Rini <trini@konsulko.com>

SPDX: Convert a few files that were missed before

As part of the main conversion a few files were missed. These files had
additional whitespace after the '*' and before the SPDX tag and my
previous regex was too strict. This time I did a grep for all SPDX tags
and then filtered out anything that matched the correct styles.

Fixes: 83d290c56fab ("SPDX: Convert all of our single license tags to Linux Kernel style")
Reported-by: Heinrich Schuchardt <xypron.debian@gmx.de>
Signed-off-by: Tom Rini <trini@konsulko.com>

# b095f3c8 18-Feb-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: implement event groups

If an event of a group event is signaled all other events of the same
group are signaled too.

Function efi_signal_event is renamed to efi_queue_event.
A new function efi_signal_event is introduced that checks if an event
belongs to a group and than signals all events of the group.
Event group notifciation is implemented for ExitBootServices,
InstallConfigurationTable, and ResetSystem.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 0fb4169e 19-Feb-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: correct input of special keys

Don't set unicode_char if scan_code is set.
Add support for page up, page down, and insert.
Correct input of function keys.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 8396e3fd 03-Mar-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: remove deprecated ConsoleControlProtocol

The console control protocol is not defined in the UEFI standard.

It exists in EDK2's EdkCompatiblityPkg package. But this package
is deprecated according to
https://github.com/tianocore/tianocore.github.io/wiki/Differences-between-EDK-and-EDK-II

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 9bc9664d 19-Jan-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: add check_tpl parameter to efi_signal_event

In ExitBootServices we need to signal events irrespective of the current
TPL level. A new parameter check_tpl is added to efi_signal_event().

Function efi_console_timer_notify() gets some comments.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 2074f700 11-Jan-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: consistently use efi_handle_t for handles

We should consistently use the efi_handle_t typedef when
referring to handles.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# ebb4dd5b 26-Oct-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: efi_console: use helper functions

Use helper functions efi_created_handle and efi_add_protocol
for creating the console handles and instaling the respective
protocols.

This change is needed if we want to move from an array of
protocols to a linked list of protocols.

Eliminate EFI_PROTOCOL_OBJECT which is not used anymore.

Currently we have not defined protocol interfaces to be const.
So efi_con_out and efi_console_control cannot be defined as const.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 2d5dc2a5 10-Oct-2017 Rob Clark <robdclark@gmail.com>

efi_loader: console support for color attributes

Shell.efi uses this, and supporting color attributes makes things look
nicer. Map the EFI fg/bg color attributes to ANSI escape sequences.
Not all colors have a perfect match, but spec just says "Devices
supporting a different number of text colors are required to emulate the
above colors to the best of the device’s capabilities".

Signed-off-by: Rob Clark <robdclark@gmail.com>
Tested-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Alexander Graf <agraf@suse.de>
[agraf: s/unsigned/unsigned int/]
Signed-off-by: Alexander Graf <agraf@suse.de>

# e190e897 04-Oct-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: use type bool for event states

Queued and signaled describe boolean states of events.
So let's use type bool and rename the structure members to is_queued
and is_signaled.

Update the comments for is_queued and is_signaled.

Reported-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Rob Clark <robdclark@gmail.com>
Reviewed-by: Rob Clark <robdclark@gmail.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 3a45bc7f 13-Sep-2017 Rob Clark <robdclark@gmail.com>

efi_loader: Some console improvements for vidconsole

1) use fputs() to reduce cache flushes from once-per-char to
once-per-string
2) handle \r, \t, and \b in addition to just \n for tracking
cursor position
3) cursor row/col are zero based, not one based

Signed-off-by: Rob Clark <robdclark@gmail.com>
[agraf: s/unsigned/unsigned int/]
Signed-off-by: Alexander Graf <agraf@suse.de>

# a18c5a83 13-Sep-2017 Rob Clark <robdclark@gmail.com>

efi_loader: Correctly figure out size for vidconsole

If stdout is vidconsole, we cannot rely on ANSI escape sequences to
query the size, as vidconsole cannot reply on stdin. Instead special-
case this if stdout is vidconsole.

Signed-off-by: Rob Clark <robdclark@gmail.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 71cc25c3 13-Sep-2017 Rob Clark <robdclark@gmail.com>

efi_loader: split out escape sequence based size query

We need to do something different for vidconsole, since it cannot
respond to the query on stdin. Prep work for next patch.

Signed-off-by: Rob Clark <robdclark@gmail.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# ca62a4f5 15-Sep-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: implement queueing of the notification function

For the correct implementation of the task priority level (TPL)
calling the notification function must be queued.

Add a status field 'queued' to events.

In function efi_signal_event set status queued if a notification
function exists and reset it after we have called the function.
A later patch will add a check of the TPL here.

In efi_create_event and efi_close_event unset the queued status.

In function efi_wait_for_event and efi_check_event
queue the notification function.

In efi_timer_check call the efi_notify_event
if the status queued is set.
For all timer events set status signaled.

In efi_console_timer_notify set the signaled state of the
WaitForKey event.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 78178bb0 09-Sep-2017 Rob Clark <robdclark@gmail.com>

lib: add some utf16 handling helpers

We'll eventually want these in a few places in efi_loader, and also
vsprintf.

Signed-off-by: Rob Clark <robdclark@gmail.com>

# a17e62cc 24-Jul-2017 Rob Clark <robdclark@gmail.com>

efi_loader: expose protocols via GUID

shim.efi (or rather gnu-efi's LibLocateProtocol() which shim.efi uses)
resolves protocols via efi_locate_handle() so the console protocols
need to be added to the efi object list.

Signed-off-by: Rob Clark <robdclark@gmail.com>
[agraf: whitespace fixes]
Signed-off-by: Alexander Graf <agraf@suse.de>

# ff925938 19-Jul-2017 xypron.glpk@gmx.de <xypron.glpk@gmx.de>

efi_console: use EFIAPI for callback functions

The call to efi_create_event requires notification functions
flagged as EFIAPI.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 91be9a77 18-Jul-2017 xypron.glpk@gmx.de <xypron.glpk@gmx.de>

efi_console: set up events

Set up a timer event and the WaitForKey event.
In the notify function of the timer event check for console input
and signal the WaitForKey event accordingly.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 5be8b0a3 07-Nov-2016 Emmanuel Vadot <manu@bidouilliste.com>

efi_loader: console: Correctly report modes

Add support for EFI console modes.
Mode 0 is always 80x25 and present by EFI specification.
Mode 1 is always 80x50 and not mandatory.
Mode 2 and above is freely usable.

If the terminal can handle mode 1, we mark it as supported.
If the terminal size is greater than mode 0 and different than mode 1,
we install it as mode 2.

Modes can be switch with cout_set_mode.

Changes in V5:
Correctly detect mode before enabling mode 2.

Changes in V4:
Reset cursor positon on mode switch
Use local variables in console query code

Changes in V3:
Valid mode are 0 to EFIMode-1
Fix style

Changes in V2:
Add mode switch
Report only the modes that we support

Signed-off-by: Emmanuel Vadot <manu@bidouilliste.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# c1311ad4 03-Mar-2016 Alexander Graf <agraf@csgraf.de>

efi_loader: Add console interface

One of the basic EFI interfaces is the console interface. Using it an EFI
application can interface with the user. This patch implements an EFI console
interface using getc() and putc().

Today, we only implement text based consoles. We also convert the EFI Unicode
characters to UTF-8 on the fly, hoping that everyone managed to jump on the
train by now.

Signed-off-by: Alexander Graf <agraf@suse.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Tested-by: Simon Glass <sjg@chromium.org>

# c670aeee 07-Oct-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

common: rename getc() to getchar()

The sandbox is built with the SDL2 library with invokes the X11 library
which in turn calls getc(). But getc() in glibc is defined as

int getc(FILE *)

This does not match our definition.

int getc(void)

The sandbox crashes when called with parameter -l.

Rename our library symbol getc() to getchar().

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Tom Rini <trini@konsulko.com>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 325567d3 04-Jun-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: comments EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL

Provide missing comments for the functions implementing the
EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 336d4615 03-Feb-2020 Simon Glass <sjg@chromium.org>

dm: core: Create a new header file for 'compat' features

At present dm/device.h includes the linux-compatible features. This
requires including linux/compat.h which in turn includes a lot of headers.
One of these is malloc.h which we thus end up including in every file in
U-Boot. Apart from the inefficiency of this, it is problematic for sandbox
which needs to use the system malloc() in some files.

Move the compatibility features into a separate header file.

Signed-off-by: Simon Glass <sjg@chromium.org>

# b0ad9b5b 22-Dec-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: clear screen should move cursor to home

On a VT100 terminal <ESC>[2J should be enough to both clear the whole
screen and set the cursor to position (1, 1). But the Linux console does
not behave like this. So send an extra <ESC>[H. For reference see the
console_codes(4) man page.

Add a function description.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 1045315d 14-Nov-2019 Simon Glass <sjg@chromium.org>

common: Move get_ticks() function out of common.h

This function belongs in time.h so move it over and add a comment.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Tom Rini <trini@konsulko.com>

# fe1a81c1 05-Sep-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

doc: UEFI API documentation

Add some more files to the UEFI API documentation.

Correct some Sphinx comments.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 03446987 04-Sep-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: do not set invalid screen mode

EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.SetMode() should return EFI_UNDEFINED if a
screen mode is not available.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Alexander Graf <agraf@csgraf.de>

# 97ea0690 04-Sep-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: cursor positioning

When backspacing in column 0 do no set the column index to ULONG_MAX.
Ensure that the row number is not set to ULONG_MAX even if the row count is
advertised as 0.
Ignore control characters other the 0x08, 0x0a, 0x0d when updating the
column.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 7b51b576 01-Aug-2019 Simon Glass <sjg@chromium.org>

env: Move env_get() to env.h

Move env_get() over to the new header file.

Acked-by: Joe Hershberger <joe.hershberger@ni.com>
Signed-off-by: Simon Glass <sjg@chromium.org>

# 3b435c11 16-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: console incorrectly advertised left logo key

Avoid to signal that the left logo key is pressed, when it is not.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 3b2b2de8 16-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: alternative scan codes for F5, END, HOME

Depending on the key board alternative scan codes are used for F5, END, and
HOME.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 3950f0f8 13-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix SetAttribute()

The SetAttribute() service and the Reset() service of the simple text
output protocol must update the attribute value in the mode information.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 2ad238fc 13-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix SetMode()

Correct the check of the mode number in SetMode() service of the simple
text output protocol.

Clear the screen in SetMode().

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 7eaa900e 06-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: event signaling in ExitBootServices

ExitBootServices() has to stop timer related activity before calling the
events of the EFI_EVENT_GROUP_EXIT_BOOT_SERVICES event group. But our
current implementation was stopping all other events.

All events have to observe the task priority level.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 25e6fb5e 02-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix EnableCursor()

The EnableCursor() service of the simple text output protocol must update
the the CursorVisible field of the output mode.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# b31ca6bf 18-May-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: parameter check OutputString

Check the parameters against NULL.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# acee9652 18-May-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.SetState() correct parameter

KeyToggleState is a pointer according to UEFI spec 2.8.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# bfc2dd53 06-Apr-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: correct CTRL-A - CTRL-Z console input

In the extended text input protocol CTRL-A - CTRL-Z have to be signaled as
Unicode characters a-z or A-Z depending on the shift state and not as 0x01
to 0x1a.

Update Python unit test.

This patch is required for using the EFI shell `edit` command.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# dd1a1ec2 04-Mar-2019 Matthias Brugger <mbrugger@suse.com>

efi_loader: Fix serial console size detection

Function term_read_reply tries to read from the serial console until
the end_char was read. This can hang forever if we are, for some reason,
not able to read the full response (e.g. serial buffer too small,
frame error). This patch moves the timeout detection into
term_read_reply() to assure we will make progress.

Fixes: 6bb591f704 ("efi_loader: query serial console size reliably")
Signed-off-by: Matthias Brugger <mbrugger@suse.com>

Throw missing error when an incomplete reply for the cursor position is
received.

Change type of argument of term_get_char() *s32. This renders the function
reusable in efi_cin_read_key().

Reviewed-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# e1fec152 18-Oct-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix typos

Fix typos in EFI subsystem comments.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# fae0118e 25-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: eliminate handle member

A pointer to a struct efi_object is a handle. We do not need any handle
member in this structure. Let's eliminate it.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 6f566c23 01-Oct-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: return type efi_console_register()

Use a return type that can encompass the return value.

This fixes CoverityScan CID 184090.

Reported-by: Tom Rini <trini@konsulko.com>
Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 6bb591f7 15-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: query serial console size reliably

Not all terminals understand CSI [18t for querying the console size.
We should adhere to escape sequences documented in the console_codes
manpage and the ECMA-48 standard.

So here we follow a different approach. We position the cursor to the
bottom right and query its position. Before leaving the function we
restore the original cursor position.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# aaace4b0 14-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: terminal left upper corner is [1, 1]

For the VT102 and the Linux console the left upper corner is [1, 1].
EFI uses [0, 0] as left upper corner.

Check parameters of the SetCursorPosition() service.

Use CSI H like EDK2 for positioning.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 4fdcf066 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: implement key notify functions

Implement registering and unregistreing key notify functions in the
EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 55fbdf99 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: support modifiers for F1 - F4

Support modifiers for F1 - F4.
Add support for letters with ALT key.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 110c6280 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL

This patch implements the EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.

The implementation of notification functions is postponed to a later
patch.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 0dfd13a4 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: rework event handling for console

Preread the next key in the console timer event.

The EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL requires to trigger registered key
notification functions based on the prefetched key.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 35cbb796 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: support Unicode text input

Up to now the EFI_TEXT_INPUT_PROTOCOL only supported ASCII characters.
With the patch it can consume UTF-8 from the console.

Currently only the serial console and the console can deliver UTF-8.
Local consoles are restricted to ASCII.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 14d103bb 08-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: typos in efi_console.c

Fix a few typos.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 3e603ec7 08-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: struct efi_simple_text_input_protocol

%s/efi_simple_input_interface/efi_simple_text_input_protocol/

We should be consistent in the naming of the EFI protocol interface
structures. The protocol is called EFI_SIMPLE_TEXT_INPUT_PROTOCOL.

%s/ExtendedVerification/extended_verification/

Use consistent naming of function parameters. Do not use CamelCase.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 40e3e757 04-Sep-2018 Alexander Graf <agraf@csgraf.de>

efi_loader: Add console handle references to in/out/err

We register the console in/out protocol on the loaded image handle, but
never tell anyone about it in the systab.

Because of this Shell.efi can not find the input protocol. So let's add
a proper reference.

Signed-off-by: Alexander Graf <agraf@suse.de>
Reviewed-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# ba7bd5c2 31-Aug-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: don't use unlimited stack as buffer

The length of a string printed to the console by the
EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL is not limited by the UEFI spec.
Hence should not allocate a buffer for it on the stack.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 1dde0d57 31-Aug-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: rename utf16_strlen, utf16_strnlen

The function names utf16_strlen() and utf16_strnlen() are misnomers.
The functions do not count utf-16 characters but non-zero words.
So let's rename them to u16_strlen and u16_strnlen().

In utf16_dup() avoid assignment in if clause.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 9d12daff 05-Jul-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.Reset()

Implement the reset service of the EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.

This should resolve the error reported by the SCT in
Protocol/SimpleTextOut/BlackBoxTest/SimpleTextOutBBTestFunction_uefi.c:639

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# e67ff94d 05-Jul-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: clear screen has to reset cursor position

After clearing the screen the cursor position is row 0, column 0.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 4f187897 05-Jul-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_INPUT_PROTOCOL.Reset()

Implement the reset service of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL.

This should resolve the error reported by the SCT in
Protocol/SimpleTextIn/BlackBoxTest/SimpleTextInBBTestFunction.c:193

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 80483b2a 03-Jun-2018 Alexander Graf <agraf@csgraf.de>

efi_loader: Fix warning in raw/cols query

The code to determine rows / cols on the screen could potentially run
into a case where it doesn't know how big the screen is. In that case,
assume 80x25.

Signed-off-by: Alexander Graf <agraf@suse.de>

# 62217295 16-May-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: avoid using unitialized values as console size

If a request for the console size would be answered with a response
with less then three values, uninitialized stack memory would be
copied to the number of rows and columns of the terminal.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# a4aa7bef 29-Apr-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: always check parameters in efi_cout_query_mode()

If we cannot determine the size of the serial terminal we still have
to check the parameters of efi_cout_query_mode().

Querying the size of the serial terminal drains the keyboard buffer.
So make sure we do this during the initialization and not in the midst
of an EFI application.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 7ca7c3c0 29-Apr-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: updating the cursor position

The current coding advances the column by the number of UTF-8 bytes.
The column should be increased by one per unicode character.

The UEFI spec provides advance rules for U+0000, U+0008, U+000A,
and U000D. All other characters, including control characters
U+0007 (bel) and U+0009 (tab), have to increase the column by one.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# f739fcd8 07-May-2018 Tom Rini <trini@konsulko.com>

SPDX: Convert a few files that were missed before

As part of the main conversion a few files were missed. These files had
additional whitespace after the '*' and before the SPDX tag and my
previous regex was too strict. This time I did a grep for all SPDX tags
and then filtered out anything that matched the correct styles.

Fixes: 83d290c56fab ("SPDX: Convert all of our single license tags to Linux Kernel style")
Reported-by: Heinrich Schuchardt <xypron.debian@gmx.de>
Signed-off-by: Tom Rini <trini@konsulko.com>

# b095f3c8 18-Feb-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: implement event groups

If an event of a group event is signaled all other events of the same
group are signaled too.

Function efi_signal_event is renamed to efi_queue_event.
A new function efi_signal_event is introduced that checks if an event
belongs to a group and than signals all events of the group.
Event group notifciation is implemented for ExitBootServices,
InstallConfigurationTable, and ResetSystem.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 0fb4169e 19-Feb-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: correct input of special keys

Don't set unicode_char if scan_code is set.
Add support for page up, page down, and insert.
Correct input of function keys.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 8396e3fd 03-Mar-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: remove deprecated ConsoleControlProtocol

The console control protocol is not defined in the UEFI standard.

It exists in EDK2's EdkCompatiblityPkg package. But this package
is deprecated according to
https://github.com/tianocore/tianocore.github.io/wiki/Differences-between-EDK-and-EDK-II

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 9bc9664d 19-Jan-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: add check_tpl parameter to efi_signal_event

In ExitBootServices we need to signal events irrespective of the current
TPL level. A new parameter check_tpl is added to efi_signal_event().

Function efi_console_timer_notify() gets some comments.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 2074f700 11-Jan-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: consistently use efi_handle_t for handles

We should consistently use the efi_handle_t typedef when
referring to handles.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# ebb4dd5b 26-Oct-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: efi_console: use helper functions

Use helper functions efi_created_handle and efi_add_protocol
for creating the console handles and instaling the respective
protocols.

This change is needed if we want to move from an array of
protocols to a linked list of protocols.

Eliminate EFI_PROTOCOL_OBJECT which is not used anymore.

Currently we have not defined protocol interfaces to be const.
So efi_con_out and efi_console_control cannot be defined as const.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 2d5dc2a5 10-Oct-2017 Rob Clark <robdclark@gmail.com>

efi_loader: console support for color attributes

Shell.efi uses this, and supporting color attributes makes things look
nicer. Map the EFI fg/bg color attributes to ANSI escape sequences.
Not all colors have a perfect match, but spec just says "Devices
supporting a different number of text colors are required to emulate the
above colors to the best of the device’s capabilities".

Signed-off-by: Rob Clark <robdclark@gmail.com>
Tested-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Alexander Graf <agraf@suse.de>
[agraf: s/unsigned/unsigned int/]
Signed-off-by: Alexander Graf <agraf@suse.de>

# e190e897 04-Oct-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: use type bool for event states

Queued and signaled describe boolean states of events.
So let's use type bool and rename the structure members to is_queued
and is_signaled.

Update the comments for is_queued and is_signaled.

Reported-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Rob Clark <robdclark@gmail.com>
Reviewed-by: Rob Clark <robdclark@gmail.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 3a45bc7f 13-Sep-2017 Rob Clark <robdclark@gmail.com>

efi_loader: Some console improvements for vidconsole

1) use fputs() to reduce cache flushes from once-per-char to
once-per-string
2) handle \r, \t, and \b in addition to just \n for tracking
cursor position
3) cursor row/col are zero based, not one based

Signed-off-by: Rob Clark <robdclark@gmail.com>
[agraf: s/unsigned/unsigned int/]
Signed-off-by: Alexander Graf <agraf@suse.de>

# a18c5a83 13-Sep-2017 Rob Clark <robdclark@gmail.com>

efi_loader: Correctly figure out size for vidconsole

If stdout is vidconsole, we cannot rely on ANSI escape sequences to
query the size, as vidconsole cannot reply on stdin. Instead special-
case this if stdout is vidconsole.

Signed-off-by: Rob Clark <robdclark@gmail.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 71cc25c3 13-Sep-2017 Rob Clark <robdclark@gmail.com>

efi_loader: split out escape sequence based size query

We need to do something different for vidconsole, since it cannot
respond to the query on stdin. Prep work for next patch.

Signed-off-by: Rob Clark <robdclark@gmail.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# ca62a4f5 15-Sep-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: implement queueing of the notification function

For the correct implementation of the task priority level (TPL)
calling the notification function must be queued.

Add a status field 'queued' to events.

In function efi_signal_event set status queued if a notification
function exists and reset it after we have called the function.
A later patch will add a check of the TPL here.

In efi_create_event and efi_close_event unset the queued status.

In function efi_wait_for_event and efi_check_event
queue the notification function.

In efi_timer_check call the efi_notify_event
if the status queued is set.
For all timer events set status signaled.

In efi_console_timer_notify set the signaled state of the
WaitForKey event.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 78178bb0 09-Sep-2017 Rob Clark <robdclark@gmail.com>

lib: add some utf16 handling helpers

We'll eventually want these in a few places in efi_loader, and also
vsprintf.

Signed-off-by: Rob Clark <robdclark@gmail.com>

# a17e62cc 24-Jul-2017 Rob Clark <robdclark@gmail.com>

efi_loader: expose protocols via GUID

shim.efi (or rather gnu-efi's LibLocateProtocol() which shim.efi uses)
resolves protocols via efi_locate_handle() so the console protocols
need to be added to the efi object list.

Signed-off-by: Rob Clark <robdclark@gmail.com>
[agraf: whitespace fixes]
Signed-off-by: Alexander Graf <agraf@suse.de>

# ff925938 19-Jul-2017 xypron.glpk@gmx.de <xypron.glpk@gmx.de>

efi_console: use EFIAPI for callback functions

The call to efi_create_event requires notification functions
flagged as EFIAPI.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 91be9a77 18-Jul-2017 xypron.glpk@gmx.de <xypron.glpk@gmx.de>

efi_console: set up events

Set up a timer event and the WaitForKey event.
In the notify function of the timer event check for console input
and signal the WaitForKey event accordingly.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 5be8b0a3 07-Nov-2016 Emmanuel Vadot <manu@bidouilliste.com>

efi_loader: console: Correctly report modes

Add support for EFI console modes.
Mode 0 is always 80x25 and present by EFI specification.
Mode 1 is always 80x50 and not mandatory.
Mode 2 and above is freely usable.

If the terminal can handle mode 1, we mark it as supported.
If the terminal size is greater than mode 0 and different than mode 1,
we install it as mode 2.

Modes can be switch with cout_set_mode.

Changes in V5:
Correctly detect mode before enabling mode 2.

Changes in V4:
Reset cursor positon on mode switch
Use local variables in console query code

Changes in V3:
Valid mode are 0 to EFIMode-1
Fix style

Changes in V2:
Add mode switch
Report only the modes that we support

Signed-off-by: Emmanuel Vadot <manu@bidouilliste.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# c1311ad4 03-Mar-2016 Alexander Graf <agraf@csgraf.de>

efi_loader: Add console interface

One of the basic EFI interfaces is the console interface. Using it an EFI
application can interface with the user. This patch implements an EFI console
interface using getc() and putc().

Today, we only implement text based consoles. We also convert the EFI Unicode
characters to UTF-8 on the fly, hoping that everyone managed to jump on the
train by now.

Signed-off-by: Alexander Graf <agraf@suse.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Tested-by: Simon Glass <sjg@chromium.org>

# 325567d3 04-Jun-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: comments EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL

Provide missing comments for the functions implementing the
EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 336d4615 03-Feb-2020 Simon Glass <sjg@chromium.org>

dm: core: Create a new header file for 'compat' features

At present dm/device.h includes the linux-compatible features. This
requires including linux/compat.h which in turn includes a lot of headers.
One of these is malloc.h which we thus end up including in every file in
U-Boot. Apart from the inefficiency of this, it is problematic for sandbox
which needs to use the system malloc() in some files.

Move the compatibility features into a separate header file.

Signed-off-by: Simon Glass <sjg@chromium.org>

# b0ad9b5b 22-Dec-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: clear screen should move cursor to home

On a VT100 terminal <ESC>[2J should be enough to both clear the whole
screen and set the cursor to position (1, 1). But the Linux console does
not behave like this. So send an extra <ESC>[H. For reference see the
console_codes(4) man page.

Add a function description.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 1045315d 14-Nov-2019 Simon Glass <sjg@chromium.org>

common: Move get_ticks() function out of common.h

This function belongs in time.h so move it over and add a comment.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Tom Rini <trini@konsulko.com>

# fe1a81c1 05-Sep-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

doc: UEFI API documentation

Add some more files to the UEFI API documentation.

Correct some Sphinx comments.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 03446987 04-Sep-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: do not set invalid screen mode

EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.SetMode() should return EFI_UNDEFINED if a
screen mode is not available.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Alexander Graf <agraf@csgraf.de>

# 97ea0690 04-Sep-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: cursor positioning

When backspacing in column 0 do no set the column index to ULONG_MAX.
Ensure that the row number is not set to ULONG_MAX even if the row count is
advertised as 0.
Ignore control characters other the 0x08, 0x0a, 0x0d when updating the
column.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 7b51b576 01-Aug-2019 Simon Glass <sjg@chromium.org>

env: Move env_get() to env.h

Move env_get() over to the new header file.

Acked-by: Joe Hershberger <joe.hershberger@ni.com>
Signed-off-by: Simon Glass <sjg@chromium.org>

# 3b435c11 16-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: console incorrectly advertised left logo key

Avoid to signal that the left logo key is pressed, when it is not.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 3b2b2de8 16-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: alternative scan codes for F5, END, HOME

Depending on the key board alternative scan codes are used for F5, END, and
HOME.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 3950f0f8 13-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix SetAttribute()

The SetAttribute() service and the Reset() service of the simple text
output protocol must update the attribute value in the mode information.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 2ad238fc 13-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix SetMode()

Correct the check of the mode number in SetMode() service of the simple
text output protocol.

Clear the screen in SetMode().

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 7eaa900e 06-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: event signaling in ExitBootServices

ExitBootServices() has to stop timer related activity before calling the
events of the EFI_EVENT_GROUP_EXIT_BOOT_SERVICES event group. But our
current implementation was stopping all other events.

All events have to observe the task priority level.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 25e6fb5e 02-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix EnableCursor()

The EnableCursor() service of the simple text output protocol must update
the the CursorVisible field of the output mode.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# b31ca6bf 18-May-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: parameter check OutputString

Check the parameters against NULL.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# acee9652 18-May-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.SetState() correct parameter

KeyToggleState is a pointer according to UEFI spec 2.8.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# bfc2dd53 06-Apr-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: correct CTRL-A - CTRL-Z console input

In the extended text input protocol CTRL-A - CTRL-Z have to be signaled as
Unicode characters a-z or A-Z depending on the shift state and not as 0x01
to 0x1a.

Update Python unit test.

This patch is required for using the EFI shell `edit` command.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# dd1a1ec2 04-Mar-2019 Matthias Brugger <mbrugger@suse.com>

efi_loader: Fix serial console size detection

Function term_read_reply tries to read from the serial console until
the end_char was read. This can hang forever if we are, for some reason,
not able to read the full response (e.g. serial buffer too small,
frame error). This patch moves the timeout detection into
term_read_reply() to assure we will make progress.

Fixes: 6bb591f704 ("efi_loader: query serial console size reliably")
Signed-off-by: Matthias Brugger <mbrugger@suse.com>

Throw missing error when an incomplete reply for the cursor position is
received.

Change type of argument of term_get_char() *s32. This renders the function
reusable in efi_cin_read_key().

Reviewed-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# e1fec152 18-Oct-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix typos

Fix typos in EFI subsystem comments.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# fae0118e 25-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: eliminate handle member

A pointer to a struct efi_object is a handle. We do not need any handle
member in this structure. Let's eliminate it.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 6f566c23 01-Oct-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: return type efi_console_register()

Use a return type that can encompass the return value.

This fixes CoverityScan CID 184090.

Reported-by: Tom Rini <trini@konsulko.com>
Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 6bb591f7 15-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: query serial console size reliably

Not all terminals understand CSI [18t for querying the console size.
We should adhere to escape sequences documented in the console_codes
manpage and the ECMA-48 standard.

So here we follow a different approach. We position the cursor to the
bottom right and query its position. Before leaving the function we
restore the original cursor position.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# aaace4b0 14-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: terminal left upper corner is [1, 1]

For the VT102 and the Linux console the left upper corner is [1, 1].
EFI uses [0, 0] as left upper corner.

Check parameters of the SetCursorPosition() service.

Use CSI H like EDK2 for positioning.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 4fdcf066 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: implement key notify functions

Implement registering and unregistreing key notify functions in the
EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 55fbdf99 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: support modifiers for F1 - F4

Support modifiers for F1 - F4.
Add support for letters with ALT key.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 110c6280 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL

This patch implements the EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.

The implementation of notification functions is postponed to a later
patch.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 0dfd13a4 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: rework event handling for console

Preread the next key in the console timer event.

The EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL requires to trigger registered key
notification functions based on the prefetched key.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 35cbb796 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: support Unicode text input

Up to now the EFI_TEXT_INPUT_PROTOCOL only supported ASCII characters.
With the patch it can consume UTF-8 from the console.

Currently only the serial console and the console can deliver UTF-8.
Local consoles are restricted to ASCII.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 14d103bb 08-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: typos in efi_console.c

Fix a few typos.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 3e603ec7 08-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: struct efi_simple_text_input_protocol

%s/efi_simple_input_interface/efi_simple_text_input_protocol/

We should be consistent in the naming of the EFI protocol interface
structures. The protocol is called EFI_SIMPLE_TEXT_INPUT_PROTOCOL.

%s/ExtendedVerification/extended_verification/

Use consistent naming of function parameters. Do not use CamelCase.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 40e3e757 04-Sep-2018 Alexander Graf <agraf@csgraf.de>

efi_loader: Add console handle references to in/out/err

We register the console in/out protocol on the loaded image handle, but
never tell anyone about it in the systab.

Because of this Shell.efi can not find the input protocol. So let's add
a proper reference.

Signed-off-by: Alexander Graf <agraf@suse.de>
Reviewed-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# ba7bd5c2 31-Aug-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: don't use unlimited stack as buffer

The length of a string printed to the console by the
EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL is not limited by the UEFI spec.
Hence should not allocate a buffer for it on the stack.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 1dde0d57 31-Aug-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: rename utf16_strlen, utf16_strnlen

The function names utf16_strlen() and utf16_strnlen() are misnomers.
The functions do not count utf-16 characters but non-zero words.
So let's rename them to u16_strlen and u16_strnlen().

In utf16_dup() avoid assignment in if clause.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 9d12daff 05-Jul-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.Reset()

Implement the reset service of the EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.

This should resolve the error reported by the SCT in
Protocol/SimpleTextOut/BlackBoxTest/SimpleTextOutBBTestFunction_uefi.c:639

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# e67ff94d 05-Jul-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: clear screen has to reset cursor position

After clearing the screen the cursor position is row 0, column 0.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 4f187897 05-Jul-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_INPUT_PROTOCOL.Reset()

Implement the reset service of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL.

This should resolve the error reported by the SCT in
Protocol/SimpleTextIn/BlackBoxTest/SimpleTextInBBTestFunction.c:193

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 80483b2a 03-Jun-2018 Alexander Graf <agraf@csgraf.de>

efi_loader: Fix warning in raw/cols query

The code to determine rows / cols on the screen could potentially run
into a case where it doesn't know how big the screen is. In that case,
assume 80x25.

Signed-off-by: Alexander Graf <agraf@suse.de>

# 62217295 16-May-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: avoid using unitialized values as console size

If a request for the console size would be answered with a response
with less then three values, uninitialized stack memory would be
copied to the number of rows and columns of the terminal.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# a4aa7bef 29-Apr-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: always check parameters in efi_cout_query_mode()

If we cannot determine the size of the serial terminal we still have
to check the parameters of efi_cout_query_mode().

Querying the size of the serial terminal drains the keyboard buffer.
So make sure we do this during the initialization and not in the midst
of an EFI application.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 7ca7c3c0 29-Apr-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: updating the cursor position

The current coding advances the column by the number of UTF-8 bytes.
The column should be increased by one per unicode character.

The UEFI spec provides advance rules for U+0000, U+0008, U+000A,
and U000D. All other characters, including control characters
U+0007 (bel) and U+0009 (tab), have to increase the column by one.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# f739fcd8 07-May-2018 Tom Rini <trini@konsulko.com>

SPDX: Convert a few files that were missed before

As part of the main conversion a few files were missed. These files had
additional whitespace after the '*' and before the SPDX tag and my
previous regex was too strict. This time I did a grep for all SPDX tags
and then filtered out anything that matched the correct styles.

Fixes: 83d290c56fab ("SPDX: Convert all of our single license tags to Linux Kernel style")
Reported-by: Heinrich Schuchardt <xypron.debian@gmx.de>
Signed-off-by: Tom Rini <trini@konsulko.com>

# b095f3c8 18-Feb-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: implement event groups

If an event of a group event is signaled all other events of the same
group are signaled too.

Function efi_signal_event is renamed to efi_queue_event.
A new function efi_signal_event is introduced that checks if an event
belongs to a group and than signals all events of the group.
Event group notifciation is implemented for ExitBootServices,
InstallConfigurationTable, and ResetSystem.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 0fb4169e 19-Feb-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: correct input of special keys

Don't set unicode_char if scan_code is set.
Add support for page up, page down, and insert.
Correct input of function keys.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 8396e3fd 03-Mar-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: remove deprecated ConsoleControlProtocol

The console control protocol is not defined in the UEFI standard.

It exists in EDK2's EdkCompatiblityPkg package. But this package
is deprecated according to
https://github.com/tianocore/tianocore.github.io/wiki/Differences-between-EDK-and-EDK-II

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 9bc9664d 19-Jan-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: add check_tpl parameter to efi_signal_event

In ExitBootServices we need to signal events irrespective of the current
TPL level. A new parameter check_tpl is added to efi_signal_event().

Function efi_console_timer_notify() gets some comments.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 2074f700 11-Jan-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: consistently use efi_handle_t for handles

We should consistently use the efi_handle_t typedef when
referring to handles.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# ebb4dd5b 26-Oct-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: efi_console: use helper functions

Use helper functions efi_created_handle and efi_add_protocol
for creating the console handles and instaling the respective
protocols.

This change is needed if we want to move from an array of
protocols to a linked list of protocols.

Eliminate EFI_PROTOCOL_OBJECT which is not used anymore.

Currently we have not defined protocol interfaces to be const.
So efi_con_out and efi_console_control cannot be defined as const.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 2d5dc2a5 10-Oct-2017 Rob Clark <robdclark@gmail.com>

efi_loader: console support for color attributes

Shell.efi uses this, and supporting color attributes makes things look
nicer. Map the EFI fg/bg color attributes to ANSI escape sequences.
Not all colors have a perfect match, but spec just says "Devices
supporting a different number of text colors are required to emulate the
above colors to the best of the device’s capabilities".

Signed-off-by: Rob Clark <robdclark@gmail.com>
Tested-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Alexander Graf <agraf@suse.de>
[agraf: s/unsigned/unsigned int/]
Signed-off-by: Alexander Graf <agraf@suse.de>

# e190e897 04-Oct-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: use type bool for event states

Queued and signaled describe boolean states of events.
So let's use type bool and rename the structure members to is_queued
and is_signaled.

Update the comments for is_queued and is_signaled.

Reported-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Rob Clark <robdclark@gmail.com>
Reviewed-by: Rob Clark <robdclark@gmail.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 3a45bc7f 13-Sep-2017 Rob Clark <robdclark@gmail.com>

efi_loader: Some console improvements for vidconsole

1) use fputs() to reduce cache flushes from once-per-char to
once-per-string
2) handle \r, \t, and \b in addition to just \n for tracking
cursor position
3) cursor row/col are zero based, not one based

Signed-off-by: Rob Clark <robdclark@gmail.com>
[agraf: s/unsigned/unsigned int/]
Signed-off-by: Alexander Graf <agraf@suse.de>

# a18c5a83 13-Sep-2017 Rob Clark <robdclark@gmail.com>

efi_loader: Correctly figure out size for vidconsole

If stdout is vidconsole, we cannot rely on ANSI escape sequences to
query the size, as vidconsole cannot reply on stdin. Instead special-
case this if stdout is vidconsole.

Signed-off-by: Rob Clark <robdclark@gmail.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 71cc25c3 13-Sep-2017 Rob Clark <robdclark@gmail.com>

efi_loader: split out escape sequence based size query

We need to do something different for vidconsole, since it cannot
respond to the query on stdin. Prep work for next patch.

Signed-off-by: Rob Clark <robdclark@gmail.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# ca62a4f5 15-Sep-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: implement queueing of the notification function

For the correct implementation of the task priority level (TPL)
calling the notification function must be queued.

Add a status field 'queued' to events.

In function efi_signal_event set status queued if a notification
function exists and reset it after we have called the function.
A later patch will add a check of the TPL here.

In efi_create_event and efi_close_event unset the queued status.

In function efi_wait_for_event and efi_check_event
queue the notification function.

In efi_timer_check call the efi_notify_event
if the status queued is set.
For all timer events set status signaled.

In efi_console_timer_notify set the signaled state of the
WaitForKey event.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 78178bb0 09-Sep-2017 Rob Clark <robdclark@gmail.com>

lib: add some utf16 handling helpers

We'll eventually want these in a few places in efi_loader, and also
vsprintf.

Signed-off-by: Rob Clark <robdclark@gmail.com>

# a17e62cc 24-Jul-2017 Rob Clark <robdclark@gmail.com>

efi_loader: expose protocols via GUID

shim.efi (or rather gnu-efi's LibLocateProtocol() which shim.efi uses)
resolves protocols via efi_locate_handle() so the console protocols
need to be added to the efi object list.

Signed-off-by: Rob Clark <robdclark@gmail.com>
[agraf: whitespace fixes]
Signed-off-by: Alexander Graf <agraf@suse.de>

# ff925938 19-Jul-2017 xypron.glpk@gmx.de <xypron.glpk@gmx.de>

efi_console: use EFIAPI for callback functions

The call to efi_create_event requires notification functions
flagged as EFIAPI.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 91be9a77 18-Jul-2017 xypron.glpk@gmx.de <xypron.glpk@gmx.de>

efi_console: set up events

Set up a timer event and the WaitForKey event.
In the notify function of the timer event check for console input
and signal the WaitForKey event accordingly.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 5be8b0a3 07-Nov-2016 Emmanuel Vadot <manu@bidouilliste.com>

efi_loader: console: Correctly report modes

Add support for EFI console modes.
Mode 0 is always 80x25 and present by EFI specification.
Mode 1 is always 80x50 and not mandatory.
Mode 2 and above is freely usable.

If the terminal can handle mode 1, we mark it as supported.
If the terminal size is greater than mode 0 and different than mode 1,
we install it as mode 2.

Modes can be switch with cout_set_mode.

Changes in V5:
Correctly detect mode before enabling mode 2.

Changes in V4:
Reset cursor positon on mode switch
Use local variables in console query code

Changes in V3:
Valid mode are 0 to EFIMode-1
Fix style

Changes in V2:
Add mode switch
Report only the modes that we support

Signed-off-by: Emmanuel Vadot <manu@bidouilliste.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# c1311ad4 03-Mar-2016 Alexander Graf <agraf@csgraf.de>

efi_loader: Add console interface

One of the basic EFI interfaces is the console interface. Using it an EFI
application can interface with the user. This patch implements an EFI console
interface using getc() and putc().

Today, we only implement text based consoles. We also convert the EFI Unicode
characters to UTF-8 on the fly, hoping that everyone managed to jump on the
train by now.

Signed-off-by: Alexander Graf <agraf@suse.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Tested-by: Simon Glass <sjg@chromium.org>

# 336d4615 03-Feb-2020 Simon Glass <sjg@chromium.org>

dm: core: Create a new header file for 'compat' features

At present dm/device.h includes the linux-compatible features. This
requires including linux/compat.h which in turn includes a lot of headers.
One of these is malloc.h which we thus end up including in every file in
U-Boot. Apart from the inefficiency of this, it is problematic for sandbox
which needs to use the system malloc() in some files.

Move the compatibility features into a separate header file.

Signed-off-by: Simon Glass <sjg@chromium.org>

# b0ad9b5b 22-Dec-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: clear screen should move cursor to home

On a VT100 terminal <ESC>[2J should be enough to both clear the whole
screen and set the cursor to position (1, 1). But the Linux console does
not behave like this. So send an extra <ESC>[H. For reference see the
console_codes(4) man page.

Add a function description.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 1045315d 14-Nov-2019 Simon Glass <sjg@chromium.org>

common: Move get_ticks() function out of common.h

This function belongs in time.h so move it over and add a comment.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Tom Rini <trini@konsulko.com>

# fe1a81c1 05-Sep-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

doc: UEFI API documentation

Add some more files to the UEFI API documentation.

Correct some Sphinx comments.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 03446987 04-Sep-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: do not set invalid screen mode

EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.SetMode() should return EFI_UNDEFINED if a
screen mode is not available.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Alexander Graf <agraf@csgraf.de>

# 97ea0690 04-Sep-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: cursor positioning

When backspacing in column 0 do no set the column index to ULONG_MAX.
Ensure that the row number is not set to ULONG_MAX even if the row count is
advertised as 0.
Ignore control characters other the 0x08, 0x0a, 0x0d when updating the
column.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 7b51b576 01-Aug-2019 Simon Glass <sjg@chromium.org>

env: Move env_get() to env.h

Move env_get() over to the new header file.

Acked-by: Joe Hershberger <joe.hershberger@ni.com>
Signed-off-by: Simon Glass <sjg@chromium.org>

# 3b435c11 16-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: console incorrectly advertised left logo key

Avoid to signal that the left logo key is pressed, when it is not.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 3b2b2de8 16-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: alternative scan codes for F5, END, HOME

Depending on the key board alternative scan codes are used for F5, END, and
HOME.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 3950f0f8 13-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix SetAttribute()

The SetAttribute() service and the Reset() service of the simple text
output protocol must update the attribute value in the mode information.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 2ad238fc 13-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix SetMode()

Correct the check of the mode number in SetMode() service of the simple
text output protocol.

Clear the screen in SetMode().

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 7eaa900e 06-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: event signaling in ExitBootServices

ExitBootServices() has to stop timer related activity before calling the
events of the EFI_EVENT_GROUP_EXIT_BOOT_SERVICES event group. But our
current implementation was stopping all other events.

All events have to observe the task priority level.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 25e6fb5e 02-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix EnableCursor()

The EnableCursor() service of the simple text output protocol must update
the the CursorVisible field of the output mode.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# b31ca6bf 18-May-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: parameter check OutputString

Check the parameters against NULL.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# acee9652 18-May-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.SetState() correct parameter

KeyToggleState is a pointer according to UEFI spec 2.8.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# bfc2dd53 06-Apr-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: correct CTRL-A - CTRL-Z console input

In the extended text input protocol CTRL-A - CTRL-Z have to be signaled as
Unicode characters a-z or A-Z depending on the shift state and not as 0x01
to 0x1a.

Update Python unit test.

This patch is required for using the EFI shell `edit` command.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# dd1a1ec2 04-Mar-2019 Matthias Brugger <mbrugger@suse.com>

efi_loader: Fix serial console size detection

Function term_read_reply tries to read from the serial console until
the end_char was read. This can hang forever if we are, for some reason,
not able to read the full response (e.g. serial buffer too small,
frame error). This patch moves the timeout detection into
term_read_reply() to assure we will make progress.

Fixes: 6bb591f704 ("efi_loader: query serial console size reliably")
Signed-off-by: Matthias Brugger <mbrugger@suse.com>

Throw missing error when an incomplete reply for the cursor position is
received.

Change type of argument of term_get_char() *s32. This renders the function
reusable in efi_cin_read_key().

Reviewed-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# e1fec152 18-Oct-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix typos

Fix typos in EFI subsystem comments.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# fae0118e 25-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: eliminate handle member

A pointer to a struct efi_object is a handle. We do not need any handle
member in this structure. Let's eliminate it.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 6f566c23 01-Oct-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: return type efi_console_register()

Use a return type that can encompass the return value.

This fixes CoverityScan CID 184090.

Reported-by: Tom Rini <trini@konsulko.com>
Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 6bb591f7 15-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: query serial console size reliably

Not all terminals understand CSI [18t for querying the console size.
We should adhere to escape sequences documented in the console_codes
manpage and the ECMA-48 standard.

So here we follow a different approach. We position the cursor to the
bottom right and query its position. Before leaving the function we
restore the original cursor position.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# aaace4b0 14-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: terminal left upper corner is [1, 1]

For the VT102 and the Linux console the left upper corner is [1, 1].
EFI uses [0, 0] as left upper corner.

Check parameters of the SetCursorPosition() service.

Use CSI H like EDK2 for positioning.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 4fdcf066 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: implement key notify functions

Implement registering and unregistreing key notify functions in the
EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 55fbdf99 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: support modifiers for F1 - F4

Support modifiers for F1 - F4.
Add support for letters with ALT key.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 110c6280 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL

This patch implements the EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.

The implementation of notification functions is postponed to a later
patch.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 0dfd13a4 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: rework event handling for console

Preread the next key in the console timer event.

The EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL requires to trigger registered key
notification functions based on the prefetched key.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 35cbb796 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: support Unicode text input

Up to now the EFI_TEXT_INPUT_PROTOCOL only supported ASCII characters.
With the patch it can consume UTF-8 from the console.

Currently only the serial console and the console can deliver UTF-8.
Local consoles are restricted to ASCII.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 14d103bb 08-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: typos in efi_console.c

Fix a few typos.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 3e603ec7 08-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: struct efi_simple_text_input_protocol

%s/efi_simple_input_interface/efi_simple_text_input_protocol/

We should be consistent in the naming of the EFI protocol interface
structures. The protocol is called EFI_SIMPLE_TEXT_INPUT_PROTOCOL.

%s/ExtendedVerification/extended_verification/

Use consistent naming of function parameters. Do not use CamelCase.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 40e3e757 04-Sep-2018 Alexander Graf <agraf@csgraf.de>

efi_loader: Add console handle references to in/out/err

We register the console in/out protocol on the loaded image handle, but
never tell anyone about it in the systab.

Because of this Shell.efi can not find the input protocol. So let's add
a proper reference.

Signed-off-by: Alexander Graf <agraf@suse.de>
Reviewed-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# ba7bd5c2 31-Aug-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: don't use unlimited stack as buffer

The length of a string printed to the console by the
EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL is not limited by the UEFI spec.
Hence should not allocate a buffer for it on the stack.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 1dde0d57 31-Aug-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: rename utf16_strlen, utf16_strnlen

The function names utf16_strlen() and utf16_strnlen() are misnomers.
The functions do not count utf-16 characters but non-zero words.
So let's rename them to u16_strlen and u16_strnlen().

In utf16_dup() avoid assignment in if clause.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 9d12daff 05-Jul-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.Reset()

Implement the reset service of the EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.

This should resolve the error reported by the SCT in
Protocol/SimpleTextOut/BlackBoxTest/SimpleTextOutBBTestFunction_uefi.c:639

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# e67ff94d 05-Jul-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: clear screen has to reset cursor position

After clearing the screen the cursor position is row 0, column 0.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 4f187897 05-Jul-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_INPUT_PROTOCOL.Reset()

Implement the reset service of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL.

This should resolve the error reported by the SCT in
Protocol/SimpleTextIn/BlackBoxTest/SimpleTextInBBTestFunction.c:193

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 80483b2a 03-Jun-2018 Alexander Graf <agraf@csgraf.de>

efi_loader: Fix warning in raw/cols query

The code to determine rows / cols on the screen could potentially run
into a case where it doesn't know how big the screen is. In that case,
assume 80x25.

Signed-off-by: Alexander Graf <agraf@suse.de>

# 62217295 16-May-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: avoid using unitialized values as console size

If a request for the console size would be answered with a response
with less then three values, uninitialized stack memory would be
copied to the number of rows and columns of the terminal.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# a4aa7bef 29-Apr-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: always check parameters in efi_cout_query_mode()

If we cannot determine the size of the serial terminal we still have
to check the parameters of efi_cout_query_mode().

Querying the size of the serial terminal drains the keyboard buffer.
So make sure we do this during the initialization and not in the midst
of an EFI application.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 7ca7c3c0 29-Apr-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: updating the cursor position

The current coding advances the column by the number of UTF-8 bytes.
The column should be increased by one per unicode character.

The UEFI spec provides advance rules for U+0000, U+0008, U+000A,
and U000D. All other characters, including control characters
U+0007 (bel) and U+0009 (tab), have to increase the column by one.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# f739fcd8 07-May-2018 Tom Rini <trini@konsulko.com>

SPDX: Convert a few files that were missed before

As part of the main conversion a few files were missed. These files had
additional whitespace after the '*' and before the SPDX tag and my
previous regex was too strict. This time I did a grep for all SPDX tags
and then filtered out anything that matched the correct styles.

Fixes: 83d290c56fab ("SPDX: Convert all of our single license tags to Linux Kernel style")
Reported-by: Heinrich Schuchardt <xypron.debian@gmx.de>
Signed-off-by: Tom Rini <trini@konsulko.com>

# b095f3c8 18-Feb-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: implement event groups

If an event of a group event is signaled all other events of the same
group are signaled too.

Function efi_signal_event is renamed to efi_queue_event.
A new function efi_signal_event is introduced that checks if an event
belongs to a group and than signals all events of the group.
Event group notifciation is implemented for ExitBootServices,
InstallConfigurationTable, and ResetSystem.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 0fb4169e 19-Feb-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: correct input of special keys

Don't set unicode_char if scan_code is set.
Add support for page up, page down, and insert.
Correct input of function keys.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 8396e3fd 03-Mar-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: remove deprecated ConsoleControlProtocol

The console control protocol is not defined in the UEFI standard.

It exists in EDK2's EdkCompatiblityPkg package. But this package
is deprecated according to
https://github.com/tianocore/tianocore.github.io/wiki/Differences-between-EDK-and-EDK-II

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 9bc9664d 19-Jan-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: add check_tpl parameter to efi_signal_event

In ExitBootServices we need to signal events irrespective of the current
TPL level. A new parameter check_tpl is added to efi_signal_event().

Function efi_console_timer_notify() gets some comments.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 2074f700 11-Jan-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: consistently use efi_handle_t for handles

We should consistently use the efi_handle_t typedef when
referring to handles.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# ebb4dd5b 26-Oct-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: efi_console: use helper functions

Use helper functions efi_created_handle and efi_add_protocol
for creating the console handles and instaling the respective
protocols.

This change is needed if we want to move from an array of
protocols to a linked list of protocols.

Eliminate EFI_PROTOCOL_OBJECT which is not used anymore.

Currently we have not defined protocol interfaces to be const.
So efi_con_out and efi_console_control cannot be defined as const.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 2d5dc2a5 10-Oct-2017 Rob Clark <robdclark@gmail.com>

efi_loader: console support for color attributes

Shell.efi uses this, and supporting color attributes makes things look
nicer. Map the EFI fg/bg color attributes to ANSI escape sequences.
Not all colors have a perfect match, but spec just says "Devices
supporting a different number of text colors are required to emulate the
above colors to the best of the device’s capabilities".

Signed-off-by: Rob Clark <robdclark@gmail.com>
Tested-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Alexander Graf <agraf@suse.de>
[agraf: s/unsigned/unsigned int/]
Signed-off-by: Alexander Graf <agraf@suse.de>

# e190e897 04-Oct-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: use type bool for event states

Queued and signaled describe boolean states of events.
So let's use type bool and rename the structure members to is_queued
and is_signaled.

Update the comments for is_queued and is_signaled.

Reported-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Rob Clark <robdclark@gmail.com>
Reviewed-by: Rob Clark <robdclark@gmail.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 3a45bc7f 13-Sep-2017 Rob Clark <robdclark@gmail.com>

efi_loader: Some console improvements for vidconsole

1) use fputs() to reduce cache flushes from once-per-char to
once-per-string
2) handle \r, \t, and \b in addition to just \n for tracking
cursor position
3) cursor row/col are zero based, not one based

Signed-off-by: Rob Clark <robdclark@gmail.com>
[agraf: s/unsigned/unsigned int/]
Signed-off-by: Alexander Graf <agraf@suse.de>

# a18c5a83 13-Sep-2017 Rob Clark <robdclark@gmail.com>

efi_loader: Correctly figure out size for vidconsole

If stdout is vidconsole, we cannot rely on ANSI escape sequences to
query the size, as vidconsole cannot reply on stdin. Instead special-
case this if stdout is vidconsole.

Signed-off-by: Rob Clark <robdclark@gmail.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 71cc25c3 13-Sep-2017 Rob Clark <robdclark@gmail.com>

efi_loader: split out escape sequence based size query

We need to do something different for vidconsole, since it cannot
respond to the query on stdin. Prep work for next patch.

Signed-off-by: Rob Clark <robdclark@gmail.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# ca62a4f5 15-Sep-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: implement queueing of the notification function

For the correct implementation of the task priority level (TPL)
calling the notification function must be queued.

Add a status field 'queued' to events.

In function efi_signal_event set status queued if a notification
function exists and reset it after we have called the function.
A later patch will add a check of the TPL here.

In efi_create_event and efi_close_event unset the queued status.

In function efi_wait_for_event and efi_check_event
queue the notification function.

In efi_timer_check call the efi_notify_event
if the status queued is set.
For all timer events set status signaled.

In efi_console_timer_notify set the signaled state of the
WaitForKey event.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 78178bb0 09-Sep-2017 Rob Clark <robdclark@gmail.com>

lib: add some utf16 handling helpers

We'll eventually want these in a few places in efi_loader, and also
vsprintf.

Signed-off-by: Rob Clark <robdclark@gmail.com>

# a17e62cc 24-Jul-2017 Rob Clark <robdclark@gmail.com>

efi_loader: expose protocols via GUID

shim.efi (or rather gnu-efi's LibLocateProtocol() which shim.efi uses)
resolves protocols via efi_locate_handle() so the console protocols
need to be added to the efi object list.

Signed-off-by: Rob Clark <robdclark@gmail.com>
[agraf: whitespace fixes]
Signed-off-by: Alexander Graf <agraf@suse.de>

# ff925938 19-Jul-2017 xypron.glpk@gmx.de <xypron.glpk@gmx.de>

efi_console: use EFIAPI for callback functions

The call to efi_create_event requires notification functions
flagged as EFIAPI.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 91be9a77 18-Jul-2017 xypron.glpk@gmx.de <xypron.glpk@gmx.de>

efi_console: set up events

Set up a timer event and the WaitForKey event.
In the notify function of the timer event check for console input
and signal the WaitForKey event accordingly.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 5be8b0a3 07-Nov-2016 Emmanuel Vadot <manu@bidouilliste.com>

efi_loader: console: Correctly report modes

Add support for EFI console modes.
Mode 0 is always 80x25 and present by EFI specification.
Mode 1 is always 80x50 and not mandatory.
Mode 2 and above is freely usable.

If the terminal can handle mode 1, we mark it as supported.
If the terminal size is greater than mode 0 and different than mode 1,
we install it as mode 2.

Modes can be switch with cout_set_mode.

Changes in V5:
Correctly detect mode before enabling mode 2.

Changes in V4:
Reset cursor positon on mode switch
Use local variables in console query code

Changes in V3:
Valid mode are 0 to EFIMode-1
Fix style

Changes in V2:
Add mode switch
Report only the modes that we support

Signed-off-by: Emmanuel Vadot <manu@bidouilliste.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# c1311ad4 03-Mar-2016 Alexander Graf <agraf@csgraf.de>

efi_loader: Add console interface

One of the basic EFI interfaces is the console interface. Using it an EFI
application can interface with the user. This patch implements an EFI console
interface using getc() and putc().

Today, we only implement text based consoles. We also convert the EFI Unicode
characters to UTF-8 on the fly, hoping that everyone managed to jump on the
train by now.

Signed-off-by: Alexander Graf <agraf@suse.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Tested-by: Simon Glass <sjg@chromium.org>

# b0ad9b5b 22-Dec-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: clear screen should move cursor to home

On a VT100 terminal <ESC>[2J should be enough to both clear the whole
screen and set the cursor to position (1, 1). But the Linux console does
not behave like this. So send an extra <ESC>[H. For reference see the
console_codes(4) man page.

Add a function description.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 1045315d 14-Nov-2019 Simon Glass <sjg@chromium.org>

common: Move get_ticks() function out of common.h

This function belongs in time.h so move it over and add a comment.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Tom Rini <trini@konsulko.com>

# fe1a81c1 05-Sep-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

doc: UEFI API documentation

Add some more files to the UEFI API documentation.

Correct some Sphinx comments.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 03446987 04-Sep-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: do not set invalid screen mode

EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.SetMode() should return EFI_UNDEFINED if a
screen mode is not available.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Alexander Graf <agraf@csgraf.de>

# 97ea0690 04-Sep-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: cursor positioning

When backspacing in column 0 do no set the column index to ULONG_MAX.
Ensure that the row number is not set to ULONG_MAX even if the row count is
advertised as 0.
Ignore control characters other the 0x08, 0x0a, 0x0d when updating the
column.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 7b51b576 01-Aug-2019 Simon Glass <sjg@chromium.org>

env: Move env_get() to env.h

Move env_get() over to the new header file.

Acked-by: Joe Hershberger <joe.hershberger@ni.com>
Signed-off-by: Simon Glass <sjg@chromium.org>

# 3b435c11 16-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: console incorrectly advertised left logo key

Avoid to signal that the left logo key is pressed, when it is not.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 3b2b2de8 16-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: alternative scan codes for F5, END, HOME

Depending on the key board alternative scan codes are used for F5, END, and
HOME.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 3950f0f8 13-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix SetAttribute()

The SetAttribute() service and the Reset() service of the simple text
output protocol must update the attribute value in the mode information.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 2ad238fc 13-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix SetMode()

Correct the check of the mode number in SetMode() service of the simple
text output protocol.

Clear the screen in SetMode().

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 7eaa900e 06-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: event signaling in ExitBootServices

ExitBootServices() has to stop timer related activity before calling the
events of the EFI_EVENT_GROUP_EXIT_BOOT_SERVICES event group. But our
current implementation was stopping all other events.

All events have to observe the task priority level.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 25e6fb5e 02-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix EnableCursor()

The EnableCursor() service of the simple text output protocol must update
the the CursorVisible field of the output mode.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# b31ca6bf 18-May-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: parameter check OutputString

Check the parameters against NULL.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# acee9652 18-May-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.SetState() correct parameter

KeyToggleState is a pointer according to UEFI spec 2.8.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# bfc2dd53 06-Apr-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: correct CTRL-A - CTRL-Z console input

In the extended text input protocol CTRL-A - CTRL-Z have to be signaled as
Unicode characters a-z or A-Z depending on the shift state and not as 0x01
to 0x1a.

Update Python unit test.

This patch is required for using the EFI shell `edit` command.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# dd1a1ec2 04-Mar-2019 Matthias Brugger <mbrugger@suse.com>

efi_loader: Fix serial console size detection

Function term_read_reply tries to read from the serial console until
the end_char was read. This can hang forever if we are, for some reason,
not able to read the full response (e.g. serial buffer too small,
frame error). This patch moves the timeout detection into
term_read_reply() to assure we will make progress.

Fixes: 6bb591f704 ("efi_loader: query serial console size reliably")
Signed-off-by: Matthias Brugger <mbrugger@suse.com>

Throw missing error when an incomplete reply for the cursor position is
received.

Change type of argument of term_get_char() *s32. This renders the function
reusable in efi_cin_read_key().

Reviewed-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# e1fec152 18-Oct-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix typos

Fix typos in EFI subsystem comments.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# fae0118e 25-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: eliminate handle member

A pointer to a struct efi_object is a handle. We do not need any handle
member in this structure. Let's eliminate it.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 6f566c23 01-Oct-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: return type efi_console_register()

Use a return type that can encompass the return value.

This fixes CoverityScan CID 184090.

Reported-by: Tom Rini <trini@konsulko.com>
Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 6bb591f7 15-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: query serial console size reliably

Not all terminals understand CSI [18t for querying the console size.
We should adhere to escape sequences documented in the console_codes
manpage and the ECMA-48 standard.

So here we follow a different approach. We position the cursor to the
bottom right and query its position. Before leaving the function we
restore the original cursor position.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# aaace4b0 14-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: terminal left upper corner is [1, 1]

For the VT102 and the Linux console the left upper corner is [1, 1].
EFI uses [0, 0] as left upper corner.

Check parameters of the SetCursorPosition() service.

Use CSI H like EDK2 for positioning.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 4fdcf066 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: implement key notify functions

Implement registering and unregistreing key notify functions in the
EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 55fbdf99 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: support modifiers for F1 - F4

Support modifiers for F1 - F4.
Add support for letters with ALT key.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 110c6280 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL

This patch implements the EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.

The implementation of notification functions is postponed to a later
patch.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 0dfd13a4 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: rework event handling for console

Preread the next key in the console timer event.

The EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL requires to trigger registered key
notification functions based on the prefetched key.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 35cbb796 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: support Unicode text input

Up to now the EFI_TEXT_INPUT_PROTOCOL only supported ASCII characters.
With the patch it can consume UTF-8 from the console.

Currently only the serial console and the console can deliver UTF-8.
Local consoles are restricted to ASCII.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 14d103bb 08-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: typos in efi_console.c

Fix a few typos.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 3e603ec7 08-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: struct efi_simple_text_input_protocol

%s/efi_simple_input_interface/efi_simple_text_input_protocol/

We should be consistent in the naming of the EFI protocol interface
structures. The protocol is called EFI_SIMPLE_TEXT_INPUT_PROTOCOL.

%s/ExtendedVerification/extended_verification/

Use consistent naming of function parameters. Do not use CamelCase.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 40e3e757 04-Sep-2018 Alexander Graf <agraf@csgraf.de>

efi_loader: Add console handle references to in/out/err

We register the console in/out protocol on the loaded image handle, but
never tell anyone about it in the systab.

Because of this Shell.efi can not find the input protocol. So let's add
a proper reference.

Signed-off-by: Alexander Graf <agraf@suse.de>
Reviewed-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# ba7bd5c2 31-Aug-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: don't use unlimited stack as buffer

The length of a string printed to the console by the
EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL is not limited by the UEFI spec.
Hence should not allocate a buffer for it on the stack.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 1dde0d57 31-Aug-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: rename utf16_strlen, utf16_strnlen

The function names utf16_strlen() and utf16_strnlen() are misnomers.
The functions do not count utf-16 characters but non-zero words.
So let's rename them to u16_strlen and u16_strnlen().

In utf16_dup() avoid assignment in if clause.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 9d12daff 05-Jul-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.Reset()

Implement the reset service of the EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.

This should resolve the error reported by the SCT in
Protocol/SimpleTextOut/BlackBoxTest/SimpleTextOutBBTestFunction_uefi.c:639

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# e67ff94d 05-Jul-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: clear screen has to reset cursor position

After clearing the screen the cursor position is row 0, column 0.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 4f187897 05-Jul-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_INPUT_PROTOCOL.Reset()

Implement the reset service of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL.

This should resolve the error reported by the SCT in
Protocol/SimpleTextIn/BlackBoxTest/SimpleTextInBBTestFunction.c:193

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 80483b2a 03-Jun-2018 Alexander Graf <agraf@csgraf.de>

efi_loader: Fix warning in raw/cols query

The code to determine rows / cols on the screen could potentially run
into a case where it doesn't know how big the screen is. In that case,
assume 80x25.

Signed-off-by: Alexander Graf <agraf@suse.de>

# 62217295 16-May-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: avoid using unitialized values as console size

If a request for the console size would be answered with a response
with less then three values, uninitialized stack memory would be
copied to the number of rows and columns of the terminal.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# a4aa7bef 29-Apr-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: always check parameters in efi_cout_query_mode()

If we cannot determine the size of the serial terminal we still have
to check the parameters of efi_cout_query_mode().

Querying the size of the serial terminal drains the keyboard buffer.
So make sure we do this during the initialization and not in the midst
of an EFI application.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 7ca7c3c0 29-Apr-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: updating the cursor position

The current coding advances the column by the number of UTF-8 bytes.
The column should be increased by one per unicode character.

The UEFI spec provides advance rules for U+0000, U+0008, U+000A,
and U000D. All other characters, including control characters
U+0007 (bel) and U+0009 (tab), have to increase the column by one.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# f739fcd8 07-May-2018 Tom Rini <trini@konsulko.com>

SPDX: Convert a few files that were missed before

As part of the main conversion a few files were missed. These files had
additional whitespace after the '*' and before the SPDX tag and my
previous regex was too strict. This time I did a grep for all SPDX tags
and then filtered out anything that matched the correct styles.

Fixes: 83d290c56fab ("SPDX: Convert all of our single license tags to Linux Kernel style")
Reported-by: Heinrich Schuchardt <xypron.debian@gmx.de>
Signed-off-by: Tom Rini <trini@konsulko.com>

# b095f3c8 18-Feb-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: implement event groups

If an event of a group event is signaled all other events of the same
group are signaled too.

Function efi_signal_event is renamed to efi_queue_event.
A new function efi_signal_event is introduced that checks if an event
belongs to a group and than signals all events of the group.
Event group notifciation is implemented for ExitBootServices,
InstallConfigurationTable, and ResetSystem.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 0fb4169e 19-Feb-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: correct input of special keys

Don't set unicode_char if scan_code is set.
Add support for page up, page down, and insert.
Correct input of function keys.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 8396e3fd 03-Mar-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: remove deprecated ConsoleControlProtocol

The console control protocol is not defined in the UEFI standard.

It exists in EDK2's EdkCompatiblityPkg package. But this package
is deprecated according to
https://github.com/tianocore/tianocore.github.io/wiki/Differences-between-EDK-and-EDK-II

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 9bc9664d 19-Jan-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: add check_tpl parameter to efi_signal_event

In ExitBootServices we need to signal events irrespective of the current
TPL level. A new parameter check_tpl is added to efi_signal_event().

Function efi_console_timer_notify() gets some comments.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 2074f700 11-Jan-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: consistently use efi_handle_t for handles

We should consistently use the efi_handle_t typedef when
referring to handles.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# ebb4dd5b 26-Oct-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: efi_console: use helper functions

Use helper functions efi_created_handle and efi_add_protocol
for creating the console handles and instaling the respective
protocols.

This change is needed if we want to move from an array of
protocols to a linked list of protocols.

Eliminate EFI_PROTOCOL_OBJECT which is not used anymore.

Currently we have not defined protocol interfaces to be const.
So efi_con_out and efi_console_control cannot be defined as const.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 2d5dc2a5 10-Oct-2017 Rob Clark <robdclark@gmail.com>

efi_loader: console support for color attributes

Shell.efi uses this, and supporting color attributes makes things look
nicer. Map the EFI fg/bg color attributes to ANSI escape sequences.
Not all colors have a perfect match, but spec just says "Devices
supporting a different number of text colors are required to emulate the
above colors to the best of the device’s capabilities".

Signed-off-by: Rob Clark <robdclark@gmail.com>
Tested-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Alexander Graf <agraf@suse.de>
[agraf: s/unsigned/unsigned int/]
Signed-off-by: Alexander Graf <agraf@suse.de>

# e190e897 04-Oct-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: use type bool for event states

Queued and signaled describe boolean states of events.
So let's use type bool and rename the structure members to is_queued
and is_signaled.

Update the comments for is_queued and is_signaled.

Reported-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Rob Clark <robdclark@gmail.com>
Reviewed-by: Rob Clark <robdclark@gmail.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 3a45bc7f 13-Sep-2017 Rob Clark <robdclark@gmail.com>

efi_loader: Some console improvements for vidconsole

1) use fputs() to reduce cache flushes from once-per-char to
once-per-string
2) handle \r, \t, and \b in addition to just \n for tracking
cursor position
3) cursor row/col are zero based, not one based

Signed-off-by: Rob Clark <robdclark@gmail.com>
[agraf: s/unsigned/unsigned int/]
Signed-off-by: Alexander Graf <agraf@suse.de>

# a18c5a83 13-Sep-2017 Rob Clark <robdclark@gmail.com>

efi_loader: Correctly figure out size for vidconsole

If stdout is vidconsole, we cannot rely on ANSI escape sequences to
query the size, as vidconsole cannot reply on stdin. Instead special-
case this if stdout is vidconsole.

Signed-off-by: Rob Clark <robdclark@gmail.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 71cc25c3 13-Sep-2017 Rob Clark <robdclark@gmail.com>

efi_loader: split out escape sequence based size query

We need to do something different for vidconsole, since it cannot
respond to the query on stdin. Prep work for next patch.

Signed-off-by: Rob Clark <robdclark@gmail.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# ca62a4f5 15-Sep-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: implement queueing of the notification function

For the correct implementation of the task priority level (TPL)
calling the notification function must be queued.

Add a status field 'queued' to events.

In function efi_signal_event set status queued if a notification
function exists and reset it after we have called the function.
A later patch will add a check of the TPL here.

In efi_create_event and efi_close_event unset the queued status.

In function efi_wait_for_event and efi_check_event
queue the notification function.

In efi_timer_check call the efi_notify_event
if the status queued is set.
For all timer events set status signaled.

In efi_console_timer_notify set the signaled state of the
WaitForKey event.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 78178bb0 09-Sep-2017 Rob Clark <robdclark@gmail.com>

lib: add some utf16 handling helpers

We'll eventually want these in a few places in efi_loader, and also
vsprintf.

Signed-off-by: Rob Clark <robdclark@gmail.com>

# a17e62cc 24-Jul-2017 Rob Clark <robdclark@gmail.com>

efi_loader: expose protocols via GUID

shim.efi (or rather gnu-efi's LibLocateProtocol() which shim.efi uses)
resolves protocols via efi_locate_handle() so the console protocols
need to be added to the efi object list.

Signed-off-by: Rob Clark <robdclark@gmail.com>
[agraf: whitespace fixes]
Signed-off-by: Alexander Graf <agraf@suse.de>

# ff925938 19-Jul-2017 xypron.glpk@gmx.de <xypron.glpk@gmx.de>

efi_console: use EFIAPI for callback functions

The call to efi_create_event requires notification functions
flagged as EFIAPI.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 91be9a77 18-Jul-2017 xypron.glpk@gmx.de <xypron.glpk@gmx.de>

efi_console: set up events

Set up a timer event and the WaitForKey event.
In the notify function of the timer event check for console input
and signal the WaitForKey event accordingly.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 5be8b0a3 07-Nov-2016 Emmanuel Vadot <manu@bidouilliste.com>

efi_loader: console: Correctly report modes

Add support for EFI console modes.
Mode 0 is always 80x25 and present by EFI specification.
Mode 1 is always 80x50 and not mandatory.
Mode 2 and above is freely usable.

If the terminal can handle mode 1, we mark it as supported.
If the terminal size is greater than mode 0 and different than mode 1,
we install it as mode 2.

Modes can be switch with cout_set_mode.

Changes in V5:
Correctly detect mode before enabling mode 2.

Changes in V4:
Reset cursor positon on mode switch
Use local variables in console query code

Changes in V3:
Valid mode are 0 to EFIMode-1
Fix style

Changes in V2:
Add mode switch
Report only the modes that we support

Signed-off-by: Emmanuel Vadot <manu@bidouilliste.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# c1311ad4 03-Mar-2016 Alexander Graf <agraf@csgraf.de>

efi_loader: Add console interface

One of the basic EFI interfaces is the console interface. Using it an EFI
application can interface with the user. This patch implements an EFI console
interface using getc() and putc().

Today, we only implement text based consoles. We also convert the EFI Unicode
characters to UTF-8 on the fly, hoping that everyone managed to jump on the
train by now.

Signed-off-by: Alexander Graf <agraf@suse.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Tested-by: Simon Glass <sjg@chromium.org>

# 1045315d 14-Nov-2019 Simon Glass <sjg@chromium.org>

common: Move get_ticks() function out of common.h

This function belongs in time.h so move it over and add a comment.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Tom Rini <trini@konsulko.com>

# fe1a81c1 05-Sep-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

doc: UEFI API documentation

Add some more files to the UEFI API documentation.

Correct some Sphinx comments.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 03446987 04-Sep-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: do not set invalid screen mode

EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.SetMode() should return EFI_UNDEFINED if a
screen mode is not available.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Alexander Graf <agraf@csgraf.de>

# 97ea0690 04-Sep-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: cursor positioning

When backspacing in column 0 do no set the column index to ULONG_MAX.
Ensure that the row number is not set to ULONG_MAX even if the row count is
advertised as 0.
Ignore control characters other the 0x08, 0x0a, 0x0d when updating the
column.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 7b51b576 01-Aug-2019 Simon Glass <sjg@chromium.org>

env: Move env_get() to env.h

Move env_get() over to the new header file.

Acked-by: Joe Hershberger <joe.hershberger@ni.com>
Signed-off-by: Simon Glass <sjg@chromium.org>

# 3b435c11 16-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: console incorrectly advertised left logo key

Avoid to signal that the left logo key is pressed, when it is not.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 3b2b2de8 16-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: alternative scan codes for F5, END, HOME

Depending on the key board alternative scan codes are used for F5, END, and
HOME.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 3950f0f8 13-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix SetAttribute()

The SetAttribute() service and the Reset() service of the simple text
output protocol must update the attribute value in the mode information.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 2ad238fc 13-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix SetMode()

Correct the check of the mode number in SetMode() service of the simple
text output protocol.

Clear the screen in SetMode().

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 7eaa900e 06-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: event signaling in ExitBootServices

ExitBootServices() has to stop timer related activity before calling the
events of the EFI_EVENT_GROUP_EXIT_BOOT_SERVICES event group. But our
current implementation was stopping all other events.

All events have to observe the task priority level.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 25e6fb5e 02-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix EnableCursor()

The EnableCursor() service of the simple text output protocol must update
the the CursorVisible field of the output mode.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# b31ca6bf 18-May-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: parameter check OutputString

Check the parameters against NULL.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# acee9652 18-May-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.SetState() correct parameter

KeyToggleState is a pointer according to UEFI spec 2.8.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# bfc2dd53 06-Apr-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: correct CTRL-A - CTRL-Z console input

In the extended text input protocol CTRL-A - CTRL-Z have to be signaled as
Unicode characters a-z or A-Z depending on the shift state and not as 0x01
to 0x1a.

Update Python unit test.

This patch is required for using the EFI shell `edit` command.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# dd1a1ec2 04-Mar-2019 Matthias Brugger <mbrugger@suse.com>

efi_loader: Fix serial console size detection

Function term_read_reply tries to read from the serial console until
the end_char was read. This can hang forever if we are, for some reason,
not able to read the full response (e.g. serial buffer too small,
frame error). This patch moves the timeout detection into
term_read_reply() to assure we will make progress.

Fixes: 6bb591f704 ("efi_loader: query serial console size reliably")
Signed-off-by: Matthias Brugger <mbrugger@suse.com>

Throw missing error when an incomplete reply for the cursor position is
received.

Change type of argument of term_get_char() *s32. This renders the function
reusable in efi_cin_read_key().

Reviewed-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# e1fec152 18-Oct-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix typos

Fix typos in EFI subsystem comments.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# fae0118e 25-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: eliminate handle member

A pointer to a struct efi_object is a handle. We do not need any handle
member in this structure. Let's eliminate it.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 6f566c23 01-Oct-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: return type efi_console_register()

Use a return type that can encompass the return value.

This fixes CoverityScan CID 184090.

Reported-by: Tom Rini <trini@konsulko.com>
Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 6bb591f7 15-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: query serial console size reliably

Not all terminals understand CSI [18t for querying the console size.
We should adhere to escape sequences documented in the console_codes
manpage and the ECMA-48 standard.

So here we follow a different approach. We position the cursor to the
bottom right and query its position. Before leaving the function we
restore the original cursor position.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# aaace4b0 14-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: terminal left upper corner is [1, 1]

For the VT102 and the Linux console the left upper corner is [1, 1].
EFI uses [0, 0] as left upper corner.

Check parameters of the SetCursorPosition() service.

Use CSI H like EDK2 for positioning.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 4fdcf066 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: implement key notify functions

Implement registering and unregistreing key notify functions in the
EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 55fbdf99 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: support modifiers for F1 - F4

Support modifiers for F1 - F4.
Add support for letters with ALT key.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 110c6280 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL

This patch implements the EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.

The implementation of notification functions is postponed to a later
patch.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 0dfd13a4 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: rework event handling for console

Preread the next key in the console timer event.

The EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL requires to trigger registered key
notification functions based on the prefetched key.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 35cbb796 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: support Unicode text input

Up to now the EFI_TEXT_INPUT_PROTOCOL only supported ASCII characters.
With the patch it can consume UTF-8 from the console.

Currently only the serial console and the console can deliver UTF-8.
Local consoles are restricted to ASCII.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 14d103bb 08-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: typos in efi_console.c

Fix a few typos.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 3e603ec7 08-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: struct efi_simple_text_input_protocol

%s/efi_simple_input_interface/efi_simple_text_input_protocol/

We should be consistent in the naming of the EFI protocol interface
structures. The protocol is called EFI_SIMPLE_TEXT_INPUT_PROTOCOL.

%s/ExtendedVerification/extended_verification/

Use consistent naming of function parameters. Do not use CamelCase.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 40e3e757 04-Sep-2018 Alexander Graf <agraf@suse.de>

efi_loader: Add console handle references to in/out/err

We register the console in/out protocol on the loaded image handle, but
never tell anyone about it in the systab.

Because of this Shell.efi can not find the input protocol. So let's add
a proper reference.

Signed-off-by: Alexander Graf <agraf@suse.de>
Reviewed-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# ba7bd5c2 31-Aug-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: don't use unlimited stack as buffer

The length of a string printed to the console by the
EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL is not limited by the UEFI spec.
Hence should not allocate a buffer for it on the stack.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 1dde0d57 31-Aug-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: rename utf16_strlen, utf16_strnlen

The function names utf16_strlen() and utf16_strnlen() are misnomers.
The functions do not count utf-16 characters but non-zero words.
So let's rename them to u16_strlen and u16_strnlen().

In utf16_dup() avoid assignment in if clause.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 9d12daff 05-Jul-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.Reset()

Implement the reset service of the EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.

This should resolve the error reported by the SCT in
Protocol/SimpleTextOut/BlackBoxTest/SimpleTextOutBBTestFunction_uefi.c:639

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# e67ff94d 05-Jul-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: clear screen has to reset cursor position

After clearing the screen the cursor position is row 0, column 0.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 4f187897 05-Jul-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_INPUT_PROTOCOL.Reset()

Implement the reset service of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL.

This should resolve the error reported by the SCT in
Protocol/SimpleTextIn/BlackBoxTest/SimpleTextInBBTestFunction.c:193

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 80483b2a 03-Jun-2018 Alexander Graf <agraf@suse.de>

efi_loader: Fix warning in raw/cols query

The code to determine rows / cols on the screen could potentially run
into a case where it doesn't know how big the screen is. In that case,
assume 80x25.

Signed-off-by: Alexander Graf <agraf@suse.de>

# 62217295 16-May-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: avoid using unitialized values as console size

If a request for the console size would be answered with a response
with less then three values, uninitialized stack memory would be
copied to the number of rows and columns of the terminal.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# a4aa7bef 29-Apr-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: always check parameters in efi_cout_query_mode()

If we cannot determine the size of the serial terminal we still have
to check the parameters of efi_cout_query_mode().

Querying the size of the serial terminal drains the keyboard buffer.
So make sure we do this during the initialization and not in the midst
of an EFI application.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 7ca7c3c0 29-Apr-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: updating the cursor position

The current coding advances the column by the number of UTF-8 bytes.
The column should be increased by one per unicode character.

The UEFI spec provides advance rules for U+0000, U+0008, U+000A,
and U000D. All other characters, including control characters
U+0007 (bel) and U+0009 (tab), have to increase the column by one.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# f739fcd8 07-May-2018 Tom Rini <trini@konsulko.com>

SPDX: Convert a few files that were missed before

As part of the main conversion a few files were missed. These files had
additional whitespace after the '*' and before the SPDX tag and my
previous regex was too strict. This time I did a grep for all SPDX tags
and then filtered out anything that matched the correct styles.

Fixes: 83d290c56fab ("SPDX: Convert all of our single license tags to Linux Kernel style")
Reported-by: Heinrich Schuchardt <xypron.debian@gmx.de>
Signed-off-by: Tom Rini <trini@konsulko.com>

# b095f3c8 18-Feb-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: implement event groups

If an event of a group event is signaled all other events of the same
group are signaled too.

Function efi_signal_event is renamed to efi_queue_event.
A new function efi_signal_event is introduced that checks if an event
belongs to a group and than signals all events of the group.
Event group notifciation is implemented for ExitBootServices,
InstallConfigurationTable, and ResetSystem.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 0fb4169e 19-Feb-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: correct input of special keys

Don't set unicode_char if scan_code is set.
Add support for page up, page down, and insert.
Correct input of function keys.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 8396e3fd 03-Mar-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: remove deprecated ConsoleControlProtocol

The console control protocol is not defined in the UEFI standard.

It exists in EDK2's EdkCompatiblityPkg package. But this package
is deprecated according to
https://github.com/tianocore/tianocore.github.io/wiki/Differences-between-EDK-and-EDK-II

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 9bc9664d 19-Jan-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: add check_tpl parameter to efi_signal_event

In ExitBootServices we need to signal events irrespective of the current
TPL level. A new parameter check_tpl is added to efi_signal_event().

Function efi_console_timer_notify() gets some comments.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 2074f700 11-Jan-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: consistently use efi_handle_t for handles

We should consistently use the efi_handle_t typedef when
referring to handles.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# ebb4dd5b 26-Oct-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: efi_console: use helper functions

Use helper functions efi_created_handle and efi_add_protocol
for creating the console handles and instaling the respective
protocols.

This change is needed if we want to move from an array of
protocols to a linked list of protocols.

Eliminate EFI_PROTOCOL_OBJECT which is not used anymore.

Currently we have not defined protocol interfaces to be const.
So efi_con_out and efi_console_control cannot be defined as const.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 2d5dc2a5 10-Oct-2017 Rob Clark <robdclark@gmail.com>

efi_loader: console support for color attributes

Shell.efi uses this, and supporting color attributes makes things look
nicer. Map the EFI fg/bg color attributes to ANSI escape sequences.
Not all colors have a perfect match, but spec just says "Devices
supporting a different number of text colors are required to emulate the
above colors to the best of the device’s capabilities".

Signed-off-by: Rob Clark <robdclark@gmail.com>
Tested-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Alexander Graf <agraf@suse.de>
[agraf: s/unsigned/unsigned int/]
Signed-off-by: Alexander Graf <agraf@suse.de>

# e190e897 04-Oct-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: use type bool for event states

Queued and signaled describe boolean states of events.
So let's use type bool and rename the structure members to is_queued
and is_signaled.

Update the comments for is_queued and is_signaled.

Reported-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Rob Clark <robdclark@gmail.com>
Reviewed-by: Rob Clark <robdclark@gmail.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 3a45bc7f 13-Sep-2017 Rob Clark <robdclark@gmail.com>

efi_loader: Some console improvements for vidconsole

1) use fputs() to reduce cache flushes from once-per-char to
once-per-string
2) handle \r, \t, and \b in addition to just \n for tracking
cursor position
3) cursor row/col are zero based, not one based

Signed-off-by: Rob Clark <robdclark@gmail.com>
[agraf: s/unsigned/unsigned int/]
Signed-off-by: Alexander Graf <agraf@suse.de>

# a18c5a83 13-Sep-2017 Rob Clark <robdclark@gmail.com>

efi_loader: Correctly figure out size for vidconsole

If stdout is vidconsole, we cannot rely on ANSI escape sequences to
query the size, as vidconsole cannot reply on stdin. Instead special-
case this if stdout is vidconsole.

Signed-off-by: Rob Clark <robdclark@gmail.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 71cc25c3 13-Sep-2017 Rob Clark <robdclark@gmail.com>

efi_loader: split out escape sequence based size query

We need to do something different for vidconsole, since it cannot
respond to the query on stdin. Prep work for next patch.

Signed-off-by: Rob Clark <robdclark@gmail.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# ca62a4f5 15-Sep-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: implement queueing of the notification function

For the correct implementation of the task priority level (TPL)
calling the notification function must be queued.

Add a status field 'queued' to events.

In function efi_signal_event set status queued if a notification
function exists and reset it after we have called the function.
A later patch will add a check of the TPL here.

In efi_create_event and efi_close_event unset the queued status.

In function efi_wait_for_event and efi_check_event
queue the notification function.

In efi_timer_check call the efi_notify_event
if the status queued is set.
For all timer events set status signaled.

In efi_console_timer_notify set the signaled state of the
WaitForKey event.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 78178bb0 09-Sep-2017 Rob Clark <robdclark@gmail.com>

lib: add some utf16 handling helpers

We'll eventually want these in a few places in efi_loader, and also
vsprintf.

Signed-off-by: Rob Clark <robdclark@gmail.com>

# a17e62cc 24-Jul-2017 Rob Clark <robdclark@gmail.com>

efi_loader: expose protocols via GUID

shim.efi (or rather gnu-efi's LibLocateProtocol() which shim.efi uses)
resolves protocols via efi_locate_handle() so the console protocols
need to be added to the efi object list.

Signed-off-by: Rob Clark <robdclark@gmail.com>
[agraf: whitespace fixes]
Signed-off-by: Alexander Graf <agraf@suse.de>

# ff925938 19-Jul-2017 xypron.glpk@gmx.de <xypron.glpk@gmx.de>

efi_console: use EFIAPI for callback functions

The call to efi_create_event requires notification functions
flagged as EFIAPI.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 91be9a77 18-Jul-2017 xypron.glpk@gmx.de <xypron.glpk@gmx.de>

efi_console: set up events

Set up a timer event and the WaitForKey event.
In the notify function of the timer event check for console input
and signal the WaitForKey event accordingly.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 5be8b0a3 07-Nov-2016 Emmanuel Vadot <manu@bidouilliste.com>

efi_loader: console: Correctly report modes

Add support for EFI console modes.
Mode 0 is always 80x25 and present by EFI specification.
Mode 1 is always 80x50 and not mandatory.
Mode 2 and above is freely usable.

If the terminal can handle mode 1, we mark it as supported.
If the terminal size is greater than mode 0 and different than mode 1,
we install it as mode 2.

Modes can be switch with cout_set_mode.

Changes in V5:
Correctly detect mode before enabling mode 2.

Changes in V4:
Reset cursor positon on mode switch
Use local variables in console query code

Changes in V3:
Valid mode are 0 to EFIMode-1
Fix style

Changes in V2:
Add mode switch
Report only the modes that we support

Signed-off-by: Emmanuel Vadot <manu@bidouilliste.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# c1311ad4 03-Mar-2016 Alexander Graf <agraf@suse.de>

efi_loader: Add console interface

One of the basic EFI interfaces is the console interface. Using it an EFI
application can interface with the user. This patch implements an EFI console
interface using getc() and putc().

Today, we only implement text based consoles. We also convert the EFI Unicode
characters to UTF-8 on the fly, hoping that everyone managed to jump on the
train by now.

Signed-off-by: Alexander Graf <agraf@suse.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Tested-by: Simon Glass <sjg@chromium.org>

# fe1a81c1 05-Sep-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

doc: UEFI API documentation

Add some more files to the UEFI API documentation.

Correct some Sphinx comments.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 03446987 04-Sep-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: do not set invalid screen mode

EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.SetMode() should return EFI_UNDEFINED if a
screen mode is not available.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Alexander Graf <agraf@csgraf.de>

# 97ea0690 04-Sep-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: cursor positioning

When backspacing in column 0 do no set the column index to ULONG_MAX.
Ensure that the row number is not set to ULONG_MAX even if the row count is
advertised as 0.
Ignore control characters other the 0x08, 0x0a, 0x0d when updating the
column.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 7b51b576 01-Aug-2019 Simon Glass <sjg@chromium.org>

env: Move env_get() to env.h

Move env_get() over to the new header file.

Acked-by: Joe Hershberger <joe.hershberger@ni.com>
Signed-off-by: Simon Glass <sjg@chromium.org>

# 3b435c11 16-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: console incorrectly advertised left logo key

Avoid to signal that the left logo key is pressed, when it is not.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 3b2b2de8 16-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: alternative scan codes for F5, END, HOME

Depending on the key board alternative scan codes are used for F5, END, and
HOME.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 3950f0f8 13-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix SetAttribute()

The SetAttribute() service and the Reset() service of the simple text
output protocol must update the attribute value in the mode information.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 2ad238fc 13-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix SetMode()

Correct the check of the mode number in SetMode() service of the simple
text output protocol.

Clear the screen in SetMode().

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 7eaa900e 06-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: event signaling in ExitBootServices

ExitBootServices() has to stop timer related activity before calling the
events of the EFI_EVENT_GROUP_EXIT_BOOT_SERVICES event group. But our
current implementation was stopping all other events.

All events have to observe the task priority level.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 25e6fb5e 02-Jun-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix EnableCursor()

The EnableCursor() service of the simple text output protocol must update
the the CursorVisible field of the output mode.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# b31ca6bf 18-May-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: parameter check OutputString

Check the parameters against NULL.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# acee9652 18-May-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.SetState() correct parameter

KeyToggleState is a pointer according to UEFI spec 2.8.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# bfc2dd53 06-Apr-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: correct CTRL-A - CTRL-Z console input

In the extended text input protocol CTRL-A - CTRL-Z have to be signaled as
Unicode characters a-z or A-Z depending on the shift state and not as 0x01
to 0x1a.

Update Python unit test.

This patch is required for using the EFI shell `edit` command.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# dd1a1ec2 04-Mar-2019 Matthias Brugger <mbrugger@suse.com>

efi_loader: Fix serial console size detection

Function term_read_reply tries to read from the serial console until
the end_char was read. This can hang forever if we are, for some reason,
not able to read the full response (e.g. serial buffer too small,
frame error). This patch moves the timeout detection into
term_read_reply() to assure we will make progress.

Fixes: 6bb591f704 ("efi_loader: query serial console size reliably")
Signed-off-by: Matthias Brugger <mbrugger@suse.com>

Throw missing error when an incomplete reply for the cursor position is
received.

Change type of argument of term_get_char() *s32. This renders the function
reusable in efi_cin_read_key().

Reviewed-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# e1fec152 18-Oct-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: fix typos

Fix typos in EFI subsystem comments.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# fae0118e 25-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: eliminate handle member

A pointer to a struct efi_object is a handle. We do not need any handle
member in this structure. Let's eliminate it.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 6f566c23 01-Oct-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: return type efi_console_register()

Use a return type that can encompass the return value.

This fixes CoverityScan CID 184090.

Reported-by: Tom Rini <trini@konsulko.com>
Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 6bb591f7 15-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: query serial console size reliably

Not all terminals understand CSI [18t for querying the console size.
We should adhere to escape sequences documented in the console_codes
manpage and the ECMA-48 standard.

So here we follow a different approach. We position the cursor to the
bottom right and query its position. Before leaving the function we
restore the original cursor position.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# aaace4b0 14-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: terminal left upper corner is [1, 1]

For the VT102 and the Linux console the left upper corner is [1, 1].
EFI uses [0, 0] as left upper corner.

Check parameters of the SetCursorPosition() service.

Use CSI H like EDK2 for positioning.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 4fdcf066 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: implement key notify functions

Implement registering and unregistreing key notify functions in the
EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 55fbdf99 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: support modifiers for F1 - F4

Support modifiers for F1 - F4.
Add support for letters with ALT key.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 110c6280 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL

This patch implements the EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.

The implementation of notification functions is postponed to a later
patch.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 0dfd13a4 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: rework event handling for console

Preread the next key in the console timer event.

The EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL requires to trigger registered key
notification functions based on the prefetched key.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 35cbb796 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: support Unicode text input

Up to now the EFI_TEXT_INPUT_PROTOCOL only supported ASCII characters.
With the patch it can consume UTF-8 from the console.

Currently only the serial console and the console can deliver UTF-8.
Local consoles are restricted to ASCII.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 14d103bb 08-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: typos in efi_console.c

Fix a few typos.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 3e603ec7 08-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: struct efi_simple_text_input_protocol

%s/efi_simple_input_interface/efi_simple_text_input_protocol/

We should be consistent in the naming of the EFI protocol interface
structures. The protocol is called EFI_SIMPLE_TEXT_INPUT_PROTOCOL.

%s/ExtendedVerification/extended_verification/

Use consistent naming of function parameters. Do not use CamelCase.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 40e3e757 04-Sep-2018 Alexander Graf <agraf@suse.de>

efi_loader: Add console handle references to in/out/err

We register the console in/out protocol on the loaded image handle, but
never tell anyone about it in the systab.

Because of this Shell.efi can not find the input protocol. So let's add
a proper reference.

Signed-off-by: Alexander Graf <agraf@suse.de>
Reviewed-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# ba7bd5c2 31-Aug-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: don't use unlimited stack as buffer

The length of a string printed to the console by the
EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL is not limited by the UEFI spec.
Hence should not allocate a buffer for it on the stack.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 1dde0d57 31-Aug-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: rename utf16_strlen, utf16_strnlen

The function names utf16_strlen() and utf16_strnlen() are misnomers.
The functions do not count utf-16 characters but non-zero words.
So let's rename them to u16_strlen and u16_strnlen().

In utf16_dup() avoid assignment in if clause.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 9d12daff 05-Jul-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.Reset()

Implement the reset service of the EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.

This should resolve the error reported by the SCT in
Protocol/SimpleTextOut/BlackBoxTest/SimpleTextOutBBTestFunction_uefi.c:639

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# e67ff94d 05-Jul-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: clear screen has to reset cursor position

After clearing the screen the cursor position is row 0, column 0.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 4f187897 05-Jul-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_INPUT_PROTOCOL.Reset()

Implement the reset service of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL.

This should resolve the error reported by the SCT in
Protocol/SimpleTextIn/BlackBoxTest/SimpleTextInBBTestFunction.c:193

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 80483b2a 03-Jun-2018 Alexander Graf <agraf@suse.de>

efi_loader: Fix warning in raw/cols query

The code to determine rows / cols on the screen could potentially run
into a case where it doesn't know how big the screen is. In that case,
assume 80x25.

Signed-off-by: Alexander Graf <agraf@suse.de>

# 62217295 16-May-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: avoid using unitialized values as console size

If a request for the console size would be answered with a response
with less then three values, uninitialized stack memory would be
copied to the number of rows and columns of the terminal.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# a4aa7bef 29-Apr-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: always check parameters in efi_cout_query_mode()

If we cannot determine the size of the serial terminal we still have
to check the parameters of efi_cout_query_mode().

Querying the size of the serial terminal drains the keyboard buffer.
So make sure we do this during the initialization and not in the midst
of an EFI application.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 7ca7c3c0 29-Apr-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: updating the cursor position

The current coding advances the column by the number of UTF-8 bytes.
The column should be increased by one per unicode character.

The UEFI spec provides advance rules for U+0000, U+0008, U+000A,
and U000D. All other characters, including control characters
U+0007 (bel) and U+0009 (tab), have to increase the column by one.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# f739fcd8 07-May-2018 Tom Rini <trini@konsulko.com>

SPDX: Convert a few files that were missed before

As part of the main conversion a few files were missed. These files had
additional whitespace after the '*' and before the SPDX tag and my
previous regex was too strict. This time I did a grep for all SPDX tags
and then filtered out anything that matched the correct styles.

Fixes: 83d290c56fab ("SPDX: Convert all of our single license tags to Linux Kernel style")
Reported-by: Heinrich Schuchardt <xypron.debian@gmx.de>
Signed-off-by: Tom Rini <trini@konsulko.com>

# b095f3c8 18-Feb-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: implement event groups

If an event of a group event is signaled all other events of the same
group are signaled too.

Function efi_signal_event is renamed to efi_queue_event.
A new function efi_signal_event is introduced that checks if an event
belongs to a group and than signals all events of the group.
Event group notifciation is implemented for ExitBootServices,
InstallConfigurationTable, and ResetSystem.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 0fb4169e 19-Feb-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: correct input of special keys

Don't set unicode_char if scan_code is set.
Add support for page up, page down, and insert.
Correct input of function keys.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 8396e3fd 03-Mar-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: remove deprecated ConsoleControlProtocol

The console control protocol is not defined in the UEFI standard.

It exists in EDK2's EdkCompatiblityPkg package. But this package
is deprecated according to
https://github.com/tianocore/tianocore.github.io/wiki/Differences-between-EDK-and-EDK-II

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 9bc9664d 19-Jan-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: add check_tpl parameter to efi_signal_event

In ExitBootServices we need to signal events irrespective of the current
TPL level. A new parameter check_tpl is added to efi_signal_event().

Function efi_console_timer_notify() gets some comments.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 2074f700 11-Jan-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: consistently use efi_handle_t for handles

We should consistently use the efi_handle_t typedef when
referring to handles.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# ebb4dd5b 26-Oct-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: efi_console: use helper functions

Use helper functions efi_created_handle and efi_add_protocol
for creating the console handles and instaling the respective
protocols.

This change is needed if we want to move from an array of
protocols to a linked list of protocols.

Eliminate EFI_PROTOCOL_OBJECT which is not used anymore.

Currently we have not defined protocol interfaces to be const.
So efi_con_out and efi_console_control cannot be defined as const.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 2d5dc2a5 10-Oct-2017 Rob Clark <robdclark@gmail.com>

efi_loader: console support for color attributes

Shell.efi uses this, and supporting color attributes makes things look
nicer. Map the EFI fg/bg color attributes to ANSI escape sequences.
Not all colors have a perfect match, but spec just says "Devices
supporting a different number of text colors are required to emulate the
above colors to the best of the device’s capabilities".

Signed-off-by: Rob Clark <robdclark@gmail.com>
Tested-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Alexander Graf <agraf@suse.de>
[agraf: s/unsigned/unsigned int/]
Signed-off-by: Alexander Graf <agraf@suse.de>

# e190e897 04-Oct-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: use type bool for event states

Queued and signaled describe boolean states of events.
So let's use type bool and rename the structure members to is_queued
and is_signaled.

Update the comments for is_queued and is_signaled.

Reported-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Rob Clark <robdclark@gmail.com>
Reviewed-by: Rob Clark <robdclark@gmail.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 3a45bc7f 13-Sep-2017 Rob Clark <robdclark@gmail.com>

efi_loader: Some console improvements for vidconsole

1) use fputs() to reduce cache flushes from once-per-char to
once-per-string
2) handle \r, \t, and \b in addition to just \n for tracking
cursor position
3) cursor row/col are zero based, not one based

Signed-off-by: Rob Clark <robdclark@gmail.com>
[agraf: s/unsigned/unsigned int/]
Signed-off-by: Alexander Graf <agraf@suse.de>

# a18c5a83 13-Sep-2017 Rob Clark <robdclark@gmail.com>

efi_loader: Correctly figure out size for vidconsole

If stdout is vidconsole, we cannot rely on ANSI escape sequences to
query the size, as vidconsole cannot reply on stdin. Instead special-
case this if stdout is vidconsole.

Signed-off-by: Rob Clark <robdclark@gmail.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 71cc25c3 13-Sep-2017 Rob Clark <robdclark@gmail.com>

efi_loader: split out escape sequence based size query

We need to do something different for vidconsole, since it cannot
respond to the query on stdin. Prep work for next patch.

Signed-off-by: Rob Clark <robdclark@gmail.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# ca62a4f5 15-Sep-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: implement queueing of the notification function

For the correct implementation of the task priority level (TPL)
calling the notification function must be queued.

Add a status field 'queued' to events.

In function efi_signal_event set status queued if a notification
function exists and reset it after we have called the function.
A later patch will add a check of the TPL here.

In efi_create_event and efi_close_event unset the queued status.

In function efi_wait_for_event and efi_check_event
queue the notification function.

In efi_timer_check call the efi_notify_event
if the status queued is set.
For all timer events set status signaled.

In efi_console_timer_notify set the signaled state of the
WaitForKey event.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 78178bb0 09-Sep-2017 Rob Clark <robdclark@gmail.com>

lib: add some utf16 handling helpers

We'll eventually want these in a few places in efi_loader, and also
vsprintf.

Signed-off-by: Rob Clark <robdclark@gmail.com>

# a17e62cc 24-Jul-2017 Rob Clark <robdclark@gmail.com>

efi_loader: expose protocols via GUID

shim.efi (or rather gnu-efi's LibLocateProtocol() which shim.efi uses)
resolves protocols via efi_locate_handle() so the console protocols
need to be added to the efi object list.

Signed-off-by: Rob Clark <robdclark@gmail.com>
[agraf: whitespace fixes]
Signed-off-by: Alexander Graf <agraf@suse.de>

# ff925938 19-Jul-2017 xypron.glpk@gmx.de <xypron.glpk@gmx.de>

efi_console: use EFIAPI for callback functions

The call to efi_create_event requires notification functions
flagged as EFIAPI.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 91be9a77 18-Jul-2017 xypron.glpk@gmx.de <xypron.glpk@gmx.de>

efi_console: set up events

Set up a timer event and the WaitForKey event.
In the notify function of the timer event check for console input
and signal the WaitForKey event accordingly.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 5be8b0a3 07-Nov-2016 Emmanuel Vadot <manu@bidouilliste.com>

efi_loader: console: Correctly report modes

Add support for EFI console modes.
Mode 0 is always 80x25 and present by EFI specification.
Mode 1 is always 80x50 and not mandatory.
Mode 2 and above is freely usable.

If the terminal can handle mode 1, we mark it as supported.
If the terminal size is greater than mode 0 and different than mode 1,
we install it as mode 2.

Modes can be switch with cout_set_mode.

Changes in V5:
Correctly detect mode before enabling mode 2.

Changes in V4:
Reset cursor positon on mode switch
Use local variables in console query code

Changes in V3:
Valid mode are 0 to EFIMode-1
Fix style

Changes in V2:
Add mode switch
Report only the modes that we support

Signed-off-by: Emmanuel Vadot <manu@bidouilliste.com>
Signed-off-by: Alexander Graf <agraf@suse.de>

# c1311ad4 03-Mar-2016 Alexander Graf <agraf@suse.de>

efi_loader: Add console interface

One of the basic EFI interfaces is the console interface. Using it an EFI
application can interface with the user. This patch implements an EFI console
interface using getc() and putc().

Today, we only implement text based consoles. We also convert the EFI Unicode
characters to UTF-8 on the fly, hoping that everyone managed to jump on the
train by now.

Signed-off-by: Alexander Graf <agraf@suse.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Tested-by: Simon Glass <sjg@chromium.org>

# 6f566c23 01-Oct-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: return type efi_console_register()

Use a return type that can encompass the return value.

This fixes CoverityScan CID 184090.

Reported-by: Tom Rini <trini@konsulko.com>
Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>


# 6bb591f7 15-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: query serial console size reliably

Not all terminals understand CSI [18t for querying the console size.
We should adhere to escape sequences documented in the console_codes
manpage and the ECMA-48 standard.

So here we follow a different approach. We position the cursor to the
bottom right and query its position. Before leaving the function we
restore the original cursor position.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>


# aaace4b0 14-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: terminal left upper corner is [1, 1]

For the VT102 and the Linux console the left upper corner is [1, 1].
EFI uses [0, 0] as left upper corner.

Check parameters of the SetCursorPosition() service.

Use CSI H like EDK2 for positioning.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>


# 4fdcf066 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: implement key notify functions

Implement registering and unregistreing key notify functions in the
EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>


# 55fbdf99 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: support modifiers for F1 - F4

Support modifiers for F1 - F4.
Add support for letters with ALT key.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>


# 110c6280 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL

This patch implements the EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.

The implementation of notification functions is postponed to a later
patch.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>


# 0dfd13a4 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: rework event handling for console

Preread the next key in the console timer event.

The EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL requires to trigger registered key
notification functions based on the prefetched key.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>


# 35cbb796 11-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: support Unicode text input

Up to now the EFI_TEXT_INPUT_PROTOCOL only supported ASCII characters.
With the patch it can consume UTF-8 from the console.

Currently only the serial console and the console can deliver UTF-8.
Local consoles are restricted to ASCII.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>


# 14d103bb 08-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: typos in efi_console.c

Fix a few typos.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>


# 3e603ec7 08-Sep-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: struct efi_simple_text_input_protocol

%s/efi_simple_input_interface/efi_simple_text_input_protocol/

We should be consistent in the naming of the EFI protocol interface
structures. The protocol is called EFI_SIMPLE_TEXT_INPUT_PROTOCOL.

%s/ExtendedVerification/extended_verification/

Use consistent naming of function parameters. Do not use CamelCase.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>


# 40e3e757 04-Sep-2018 Alexander Graf <agraf@suse.de>

efi_loader: Add console handle references to in/out/err

We register the console in/out protocol on the loaded image handle, but
never tell anyone about it in the systab.

Because of this Shell.efi can not find the input protocol. So let's add
a proper reference.

Signed-off-by: Alexander Graf <agraf@suse.de>
Reviewed-by: Heinrich Schuchardt <xypron.glpk@gmx.de>


# ba7bd5c2 31-Aug-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: don't use unlimited stack as buffer

The length of a string printed to the console by the
EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL is not limited by the UEFI spec.
Hence should not allocate a buffer for it on the stack.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>


# 1dde0d57 31-Aug-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: rename utf16_strlen, utf16_strnlen

The function names utf16_strlen() and utf16_strnlen() are misnomers.
The functions do not count utf-16 characters but non-zero words.
So let's rename them to u16_strlen and u16_strnlen().

In utf16_dup() avoid assignment in if clause.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>


# 9d12daff 05-Jul-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.Reset()

Implement the reset service of the EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.

This should resolve the error reported by the SCT in
Protocol/SimpleTextOut/BlackBoxTest/SimpleTextOutBBTestFunction_uefi.c:639

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>


# e67ff94d 05-Jul-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: clear screen has to reset cursor position

After clearing the screen the cursor position is row 0, column 0.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>


# 4f187897 05-Jul-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: EFI_SIMPLE_TEXT_INPUT_PROTOCOL.Reset()

Implement the reset service of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL.

This should resolve the error reported by the SCT in
Protocol/SimpleTextIn/BlackBoxTest/SimpleTextInBBTestFunction.c:193

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>


# 80483b2a 03-Jun-2018 Alexander Graf <agraf@suse.de>

efi_loader: Fix warning in raw/cols query

The code to determine rows / cols on the screen could potentially run
into a case where it doesn't know how big the screen is. In that case,
assume 80x25.

Signed-off-by: Alexander Graf <agraf@suse.de>


# 62217295 16-May-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: avoid using unitialized values as console size

If a request for the console size would be answered with a response
with less then three values, uninitialized stack memory would be
copied to the number of rows and columns of the terminal.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>


# a4aa7bef 29-Apr-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: always check parameters in efi_cout_query_mode()

If we cannot determine the size of the serial terminal we still have
to check the parameters of efi_cout_query_mode().

Querying the size of the serial terminal drains the keyboard buffer.
So make sure we do this during the initialization and not in the midst
of an EFI application.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>


# 7ca7c3c0 29-Apr-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: updating the cursor position

The current coding advances the column by the number of UTF-8 bytes.
The column should be increased by one per unicode character.

The UEFI spec provides advance rules for U+0000, U+0008, U+000A,
and U000D. All other characters, including control characters
U+0007 (bel) and U+0009 (tab), have to increase the column by one.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>


# f739fcd8 07-May-2018 Tom Rini <trini@konsulko.com>

SPDX: Convert a few files that were missed before

As part of the main conversion a few files were missed. These files had
additional whitespace after the '*' and before the SPDX tag and my
previous regex was too strict. This time I did a grep for all SPDX tags
and then filtered out anything that matched the correct styles.

Fixes: 83d290c56fab ("SPDX: Convert all of our single license tags to Linux Kernel style")
Reported-by: Heinrich Schuchardt <xypron.debian@gmx.de>
Signed-off-by: Tom Rini <trini@konsulko.com>


# b095f3c8 18-Feb-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: implement event groups

If an event of a group event is signaled all other events of the same
group are signaled too.

Function efi_signal_event is renamed to efi_queue_event.
A new function efi_signal_event is introduced that checks if an event
belongs to a group and than signals all events of the group.
Event group notifciation is implemented for ExitBootServices,
InstallConfigurationTable, and ResetSystem.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>


# 0fb4169e 19-Feb-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: correct input of special keys

Don't set unicode_char if scan_code is set.
Add support for page up, page down, and insert.
Correct input of function keys.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>


# 8396e3fd 03-Mar-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: remove deprecated ConsoleControlProtocol

The console control protocol is not defined in the UEFI standard.

It exists in EDK2's EdkCompatiblityPkg package. But this package
is deprecated according to
https://github.com/tianocore/tianocore.github.io/wiki/Differences-between-EDK-and-EDK-II

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>


# 9bc9664d 19-Jan-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: add check_tpl parameter to efi_signal_event

In ExitBootServices we need to signal events irrespective of the current
TPL level. A new parameter check_tpl is added to efi_signal_event().

Function efi_console_timer_notify() gets some comments.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>


# 2074f700 11-Jan-2018 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: consistently use efi_handle_t for handles

We should consistently use the efi_handle_t typedef when
referring to handles.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>


# ebb4dd5b 26-Oct-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: efi_console: use helper functions

Use helper functions efi_created_handle and efi_add_protocol
for creating the console handles and instaling the respective
protocols.

This change is needed if we want to move from an array of
protocols to a linked list of protocols.

Eliminate EFI_PROTOCOL_OBJECT which is not used anymore.

Currently we have not defined protocol interfaces to be const.
So efi_con_out and efi_console_control cannot be defined as const.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>


# 2d5dc2a5 10-Oct-2017 Rob Clark <robdclark@gmail.com>

efi_loader: console support for color attributes

Shell.efi uses this, and supporting color attributes makes things look
nicer. Map the EFI fg/bg color attributes to ANSI escape sequences.
Not all colors have a perfect match, but spec just says "Devices
supporting a different number of text colors are required to emulate the
above colors to the best of the device’s capabilities".

Signed-off-by: Rob Clark <robdclark@gmail.com>
Tested-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Alexander Graf <agraf@suse.de>
[agraf: s/unsigned/unsigned int/]
Signed-off-by: Alexander Graf <agraf@suse.de>


# e190e897 04-Oct-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: use type bool for event states

Queued and signaled describe boolean states of events.
So let's use type bool and rename the structure members to is_queued
and is_signaled.

Update the comments for is_queued and is_signaled.

Reported-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Rob Clark <robdclark@gmail.com>
Reviewed-by: Rob Clark <robdclark@gmail.com>
Signed-off-by: Alexander Graf <agraf@suse.de>


# 3a45bc7f 13-Sep-2017 Rob Clark <robdclark@gmail.com>

efi_loader: Some console improvements for vidconsole

1) use fputs() to reduce cache flushes from once-per-char to
once-per-string
2) handle \r, \t, and \b in addition to just \n for tracking
cursor position
3) cursor row/col are zero based, not one based

Signed-off-by: Rob Clark <robdclark@gmail.com>
[agraf: s/unsigned/unsigned int/]
Signed-off-by: Alexander Graf <agraf@suse.de>


# a18c5a83 13-Sep-2017 Rob Clark <robdclark@gmail.com>

efi_loader: Correctly figure out size for vidconsole

If stdout is vidconsole, we cannot rely on ANSI escape sequences to
query the size, as vidconsole cannot reply on stdin. Instead special-
case this if stdout is vidconsole.

Signed-off-by: Rob Clark <robdclark@gmail.com>
Signed-off-by: Alexander Graf <agraf@suse.de>


# 71cc25c3 13-Sep-2017 Rob Clark <robdclark@gmail.com>

efi_loader: split out escape sequence based size query

We need to do something different for vidconsole, since it cannot
respond to the query on stdin. Prep work for next patch.

Signed-off-by: Rob Clark <robdclark@gmail.com>
Signed-off-by: Alexander Graf <agraf@suse.de>


# ca62a4f5 15-Sep-2017 Heinrich Schuchardt <xypron.glpk@gmx.de>

efi_loader: implement queueing of the notification function

For the correct implementation of the task priority level (TPL)
calling the notification function must be queued.

Add a status field 'queued' to events.

In function efi_signal_event set status queued if a notification
function exists and reset it after we have called the function.
A later patch will add a check of the TPL here.

In efi_create_event and efi_close_event unset the queued status.

In function efi_wait_for_event and efi_check_event
queue the notification function.

In efi_timer_check call the efi_notify_event
if the status queued is set.
For all timer events set status signaled.

In efi_console_timer_notify set the signaled state of the
WaitForKey event.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>


# 78178bb0 09-Sep-2017 Rob Clark <robdclark@gmail.com>

lib: add some utf16 handling helpers

We'll eventually want these in a few places in efi_loader, and also
vsprintf.

Signed-off-by: Rob Clark <robdclark@gmail.com>


# a17e62cc 24-Jul-2017 Rob Clark <robdclark@gmail.com>

efi_loader: expose protocols via GUID

shim.efi (or rather gnu-efi's LibLocateProtocol() which shim.efi uses)
resolves protocols via efi_locate_handle() so the console protocols
need to be added to the efi object list.

Signed-off-by: Rob Clark <robdclark@gmail.com>
[agraf: whitespace fixes]
Signed-off-by: Alexander Graf <agraf@suse.de>


# ff925938 19-Jul-2017 xypron.glpk@gmx.de <xypron.glpk@gmx.de>

efi_console: use EFIAPI for callback functions

The call to efi_create_event requires notification functions
flagged as EFIAPI.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>


# 91be9a77 18-Jul-2017 xypron.glpk@gmx.de <xypron.glpk@gmx.de>

efi_console: set up events

Set up a timer event and the WaitForKey event.
In the notify function of the timer event check for console input
and signal the WaitForKey event accordingly.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Signed-off-by: Alexander Graf <agraf@suse.de>


# 5be8b0a3 07-Nov-2016 Emmanuel Vadot <manu@bidouilliste.com>

efi_loader: console: Correctly report modes

Add support for EFI console modes.
Mode 0 is always 80x25 and present by EFI specification.
Mode 1 is always 80x50 and not mandatory.
Mode 2 and above is freely usable.

If the terminal can handle mode 1, we mark it as supported.
If the terminal size is greater than mode 0 and different than mode 1,
we install it as mode 2.

Modes can be switch with cout_set_mode.

Changes in V5:
Correctly detect mode before enabling mode 2.

Changes in V4:
Reset cursor positon on mode switch
Use local variables in console query code

Changes in V3:
Valid mode are 0 to EFIMode-1
Fix style

Changes in V2:
Add mode switch
Report only the modes that we support

Signed-off-by: Emmanuel Vadot <manu@bidouilliste.com>
Signed-off-by: Alexander Graf <agraf@suse.de>


# c1311ad4 03-Mar-2016 Alexander Graf <agraf@suse.de>

efi_loader: Add console interface

One of the basic EFI interfaces is the console interface. Using it an EFI
application can interface with the user. This patch implements an EFI console
interface using getc() and putc().

Today, we only implement text based consoles. We also convert the EFI Unicode
characters to UTF-8 on the fly, hoping that everyone managed to jump on the
train by now.

Signed-off-by: Alexander Graf <agraf@suse.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Tested-by: Simon Glass <sjg@chromium.org>