vaseboot/VasEBoot-core/term/efi/serial.c

196 lines
5.8 KiB
C

/*
* VAS_EBOOT -- GRand Unified Bootloader
* Copyright (C) 2006,2007,2008,2012 Free Software Foundation, Inc.
*
* VAS_EBOOT is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* VAS_EBOOT is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with VAS_EBOOT. If not, see <http://www.gnu.org/licenses/>.
*/
#include <VasEBoot/disk.h>
#include <VasEBoot/partition.h>
#include <VasEBoot/mm.h>
#include <VasEBoot/types.h>
#include <VasEBoot/misc.h>
#include <VasEBoot/err.h>
#include <VasEBoot/term.h>
#include <VasEBoot/efi/api.h>
#include <VasEBoot/efi/efi.h>
#include <VasEBoot/efi/disk.h>
#include <VasEBoot/serial.h>
#include <VasEBoot/types.h>
#include <VasEBoot/i18n.h>
/* GUID. */
static VasEBoot_guid_t serial_io_guid = VAS_EBOOT_EFI_SERIAL_IO_GUID;
static void
do_real_config (struct VasEBoot_serial_port *port)
{
VasEBoot_efi_status_t status = VAS_EBOOT_EFI_SUCCESS;
const VasEBoot_efi_parity_type_t parities[] = {
[VAS_EBOOT_SERIAL_PARITY_NONE] = VAS_EBOOT_EFI_SERIAL_NO_PARITY,
[VAS_EBOOT_SERIAL_PARITY_ODD] = VAS_EBOOT_EFI_SERIAL_ODD_PARITY,
[VAS_EBOOT_SERIAL_PARITY_EVEN] = VAS_EBOOT_EFI_SERIAL_EVEN_PARITY
};
const VasEBoot_efi_stop_bits_t stop_bits[] = {
[VAS_EBOOT_SERIAL_STOP_BITS_1] = VAS_EBOOT_EFI_SERIAL_1_STOP_BIT,
[VAS_EBOOT_SERIAL_STOP_BITS_1_5] = VAS_EBOOT_EFI_SERIAL_1_5_STOP_BITS,
[VAS_EBOOT_SERIAL_STOP_BITS_2] = VAS_EBOOT_EFI_SERIAL_2_STOP_BITS,
};
if (port->configured)
return;
status = port->interface->set_attributes (port->interface,
port->config.speed,
0, 0, parities[port->config.parity],
port->config.word_len,
stop_bits[port->config.stop_bits]);
if (status != VAS_EBOOT_EFI_SUCCESS)
port->broken = 1;
status = port->interface->set_control_bits (port->interface,
port->config.rtscts ? 0x4002 : 0x2);
port->configured = 1;
}
/* Fetch a key. */
static int
serial_hw_fetch (struct VasEBoot_serial_port *port)
{
VasEBoot_efi_uintn_t bufsize = 1;
char c;
VasEBoot_efi_status_t status = VAS_EBOOT_EFI_SUCCESS;
do_real_config (port);
if (port->broken)
return -1;
status = port->interface->read (port->interface, &bufsize, &c);
if (status != VAS_EBOOT_EFI_SUCCESS || bufsize == 0)
return -1;
return c;
}
/* Put a character. */
static void
serial_hw_put (struct VasEBoot_serial_port *port, const int c)
{
VasEBoot_efi_uintn_t bufsize = 1;
char c0 = c;
do_real_config (port);
if (port->broken)
return;
port->interface->write (port->interface, &bufsize, &c0);
}
/* Initialize a serial device. PORT is the port number for a serial device.
SPEED is a DTE-DTE speed which must be one of these: 2400, 4800, 9600,
19200, 38400, 57600 and 115200. WORD_LEN is the word length to be used
for the device. Likewise, PARITY is the type of the parity and
STOP_BIT_LEN is the length of the stop bit. The possible values for
WORD_LEN, PARITY and STOP_BIT_LEN are defined in the header file as
macros. */
static VasEBoot_err_t
serial_hw_configure (struct VasEBoot_serial_port *port,
struct VasEBoot_serial_config *config)
{
if (config->parity != VAS_EBOOT_SERIAL_PARITY_NONE
&& config->parity != VAS_EBOOT_SERIAL_PARITY_ODD
&& config->parity != VAS_EBOOT_SERIAL_PARITY_EVEN)
return VasEBoot_error (VAS_EBOOT_ERR_BAD_ARGUMENT,
N_("unsupported serial port parity"));
if (config->stop_bits != VAS_EBOOT_SERIAL_STOP_BITS_1
&& config->stop_bits != VAS_EBOOT_SERIAL_STOP_BITS_1_5
&& config->stop_bits != VAS_EBOOT_SERIAL_STOP_BITS_2)
return VasEBoot_error (VAS_EBOOT_ERR_BAD_ARGUMENT,
N_("unsupported serial port stop bits number"));
if (config->word_len < 5 || config->word_len > 8)
return VasEBoot_error (VAS_EBOOT_ERR_BAD_ARGUMENT,
N_("unsupported serial port word length"));
port->config = *config;
port->configured = 0;
/* FIXME: should check if the serial terminal was found. */
return VAS_EBOOT_ERR_NONE;
}
struct VasEBoot_serial_driver VasEBoot_efiserial_driver =
{
.configure = serial_hw_configure,
.fetch = serial_hw_fetch,
.put = serial_hw_put
};
void
VasEBoot_efiserial_init (void)
{
VasEBoot_efi_uintn_t num_handles;
VasEBoot_efi_handle_t *handles;
VasEBoot_efi_handle_t *handle;
int num_serial = 0;
VasEBoot_err_t err;
/* Find handles which support the disk io interface. */
handles = VasEBoot_efi_locate_handle (VAS_EBOOT_EFI_BY_PROTOCOL, &serial_io_guid,
0, &num_handles);
if (! handles)
return;
/* Make a linked list of devices. */
for (handle = handles; num_handles--; handle++)
{
struct VasEBoot_serial_port *port;
struct VasEBoot_efi_serial_io_interface *sio;
sio = VasEBoot_efi_open_protocol (*handle, &serial_io_guid,
VAS_EBOOT_EFI_OPEN_PROTOCOL_GET_PROTOCOL);
if (! sio)
/* This should not happen... Why? */
continue;
port = VasEBoot_zalloc (sizeof (*port));
if (!port)
break;
port->name = VasEBoot_malloc (sizeof ("efiXXXXXXXXXXXXXXXXXXXX"));
if (!port->name)
{
VasEBoot_free (port);
break;
}
VasEBoot_snprintf (port->name, sizeof ("efiXXXXXXXXXXXXXXXXXXXX"),
"efi%d", num_serial++);
port->driver = &VasEBoot_efiserial_driver;
port->interface = sio;
err = VasEBoot_serial_config_defaults (port);
if (err)
VasEBoot_print_error ();
VasEBoot_serial_register (port);
}
VasEBoot_free (handles);
return;
}