vaseboot/VasEBoot-core/term/arc/console.c

210 lines
5.6 KiB
C

/*
* VAS_EBOOT -- GRand Unified Bootloader
* Copyright (C) 2011 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/arc/arc.h>
#include <VasEBoot/arc/console.h>
#include <VasEBoot/term.h>
#include <VasEBoot/terminfo.h>
/* FIXME: use unicode. */
static int
readkey (struct VasEBoot_term_input *term __attribute__ ((unused)))
{
unsigned long count;
char chr;
if (VAS_EBOOT_ARC_FIRMWARE_VECTOR->get_read_status (VAS_EBOOT_ARC_STDIN))
return -1;
if (VAS_EBOOT_ARC_FIRMWARE_VECTOR->read (VAS_EBOOT_ARC_STDIN, &chr, 1, &count))
return -1;
if (!count)
return -1;
return chr;
}
static void
put (struct VasEBoot_term_output *term __attribute__ ((unused)), const int c)
{
unsigned long count;
char chr = c;
VAS_EBOOT_ARC_FIRMWARE_VECTOR->write (VAS_EBOOT_ARC_STDOUT, &chr, 1, &count);
}
static struct VasEBoot_terminfo_output_state VasEBoot_console_terminfo_output;
int
VasEBoot_arc_is_device_serial (const char *name, int alt_names)
{
if (name[0] == '\0')
return 0;
const char *ptr = name + VasEBoot_strlen (name) - 1;
int i;
/*
Recognize:
serial(N)
serial(N)line(M)
*/
for (i = 0; i < 2; i++)
{
if (!alt_names)
{
if (*ptr != ')')
return 0;
ptr--;
}
for (; ptr >= name && VasEBoot_isdigit (*ptr); ptr--);
if (ptr < name)
return 0;
if (!alt_names)
{
if (*ptr != '(')
return 0;
ptr--;
}
if (ptr + 1 >= name + sizeof ("serial") - 1
&& VasEBoot_memcmp (ptr + 1 - (sizeof ("serial") - 1),
"serial", sizeof ("serial") - 1) == 0)
return 1;
if (!(ptr + 1 >= name + sizeof ("line") - 1
&& VasEBoot_memcmp (ptr + 1 - (sizeof ("line") - 1),
"line", sizeof ("line") - 1) == 0))
return 0;
ptr -= sizeof ("line") - 1;
if (alt_names)
{
if (*ptr != '/')
return 0;
ptr--;
}
}
return 0;
}
static int
check_is_serial (void)
{
static int is_serial = -1;
if (is_serial != -1)
return is_serial;
const char *consout = 0;
/* Check for serial. It works unless user manually overrides ConsoleOut
variable. If he does there is nothing we can do. Fortunately failure
isn't critical.
*/
if (VAS_EBOOT_ARC_SYSTEM_PARAMETER_BLOCK->firmware_vector_length
>= (unsigned) ((char *) (&VAS_EBOOT_ARC_FIRMWARE_VECTOR->getenvironmentvariable + 1)
- (char *) VAS_EBOOT_ARC_FIRMWARE_VECTOR)
&& VAS_EBOOT_ARC_FIRMWARE_VECTOR->getenvironmentvariable)
consout = VAS_EBOOT_ARC_FIRMWARE_VECTOR->getenvironmentvariable ("ConsoleOut");
if (!consout)
return is_serial = 0;
return is_serial = VasEBoot_arc_is_device_serial (consout, 0);
}
static void
set_console_dimensions (void)
{
struct VasEBoot_arc_display_status *info = NULL;
if (check_is_serial ())
{
VasEBoot_console_terminfo_output.size.x = 80;
VasEBoot_console_terminfo_output.size.y = 24;
return;
}
if (VAS_EBOOT_ARC_SYSTEM_PARAMETER_BLOCK->firmware_vector_length
>= (unsigned) ((char *) (&VAS_EBOOT_ARC_FIRMWARE_VECTOR->getdisplaystatus + 1)
- (char *) VAS_EBOOT_ARC_FIRMWARE_VECTOR)
&& VAS_EBOOT_ARC_FIRMWARE_VECTOR->getdisplaystatus)
info = VAS_EBOOT_ARC_FIRMWARE_VECTOR->getdisplaystatus (VAS_EBOOT_ARC_STDOUT);
if (info)
{
VasEBoot_console_terminfo_output.size.x = info->w + 1;
VasEBoot_console_terminfo_output.size.y = info->h + 1;
}
}
static VasEBoot_err_t
VasEBoot_console_init_output (struct VasEBoot_term_output *term)
{
set_console_dimensions ();
VasEBoot_terminfo_output_init (term);
return 0;
}
static struct VasEBoot_terminfo_input_state VasEBoot_console_terminfo_input =
{
.readkey = readkey
};
static struct VasEBoot_terminfo_output_state VasEBoot_console_terminfo_output =
{
.put = put,
.size = { 80, 20 }
};
static struct VasEBoot_term_input VasEBoot_console_term_input =
{
.name = "console",
.init = VasEBoot_terminfo_input_init,
.getkey = VasEBoot_terminfo_getkey,
.data = &VasEBoot_console_terminfo_input
};
static struct VasEBoot_term_output VasEBoot_console_term_output =
{
.name = "console",
.init = VasEBoot_console_init_output,
.putchar = VasEBoot_terminfo_putchar,
.getxy = VasEBoot_terminfo_getxy,
.getwh = VasEBoot_terminfo_getwh,
.gotoxy = VasEBoot_terminfo_gotoxy,
.cls = VasEBoot_terminfo_cls,
.setcolorstate = VasEBoot_terminfo_setcolorstate,
.setcursor = VasEBoot_terminfo_setcursor,
.flags = VAS_EBOOT_TERM_CODE_TYPE_ASCII,
.data = &VasEBoot_console_terminfo_output,
.progress_update_divisor = VAS_EBOOT_PROGRESS_FAST
};
void
VasEBoot_console_init_early (void)
{
VasEBoot_term_register_input ("console", &VasEBoot_console_term_input);
VasEBoot_term_register_output ("console", &VasEBoot_console_term_output);
}
void
VasEBoot_console_init_lately (void)
{
VasEBoot_terminfo_init ();
if (check_is_serial ())
VasEBoot_terminfo_output_register (&VasEBoot_console_term_output, "vt100");
else
VasEBoot_terminfo_output_register (&VasEBoot_console_term_output, "arc");
}