Merge mainline into keylayouts

This commit is contained in:
Vladimir 'phcoder' Serbinenko 2010-08-31 14:03:29 +02:00
commit 5aaf2c18bd
616 changed files with 13408 additions and 7359 deletions

View file

@ -0,0 +1,622 @@
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 2007,2008,2009 Free Software Foundation, Inc.
*
* GRUB 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.
*
* GRUB 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 GRUB. If not, see <http://www.gnu.org/licenses/>.
*/
#include <grub/dl.h>
#include <grub/at_keyboard.h>
#include <grub/cpu/at_keyboard.h>
#include <grub/cpu/io.h>
#include <grub/misc.h>
#include <grub/term.h>
#include <grub/keyboard_layouts.h>
#include <grub/time.h>
#include <grub/loader.h>
static short at_keyboard_status = 0;
static int e0_received = 0;
static int f0_received = 0;
static grub_uint8_t led_status;
#define KEYBOARD_LED_SCROLL (1 << 0)
#define KEYBOARD_LED_NUM (1 << 1)
#define KEYBOARD_LED_CAPS (1 << 2)
static grub_uint8_t grub_keyboard_controller_orig;
static grub_uint8_t grub_keyboard_orig_set;
static grub_uint8_t current_set;
static const grub_uint8_t set1_mapping[128] =
{
/* 0x00 */ 0 /* Unused */, GRUB_KEYBOARD_KEY_ESCAPE,
/* 0x02 */ GRUB_KEYBOARD_KEY_1, GRUB_KEYBOARD_KEY_2,
/* 0x04 */ GRUB_KEYBOARD_KEY_3, GRUB_KEYBOARD_KEY_4,
/* 0x06 */ GRUB_KEYBOARD_KEY_5, GRUB_KEYBOARD_KEY_6,
/* 0x08 */ GRUB_KEYBOARD_KEY_7, GRUB_KEYBOARD_KEY_8,
/* 0x0a */ GRUB_KEYBOARD_KEY_9, GRUB_KEYBOARD_KEY_0,
/* 0x0c */ GRUB_KEYBOARD_KEY_DASH, GRUB_KEYBOARD_KEY_EQUAL,
/* 0x0e */ GRUB_KEYBOARD_KEY_BACKSPACE, GRUB_KEYBOARD_KEY_TAB,
/* 0x10 */ GRUB_KEYBOARD_KEY_Q, GRUB_KEYBOARD_KEY_W,
/* 0x12 */ GRUB_KEYBOARD_KEY_E, GRUB_KEYBOARD_KEY_R,
/* 0x14 */ GRUB_KEYBOARD_KEY_T, GRUB_KEYBOARD_KEY_Y,
/* 0x16 */ GRUB_KEYBOARD_KEY_U, GRUB_KEYBOARD_KEY_I,
/* 0x18 */ GRUB_KEYBOARD_KEY_O, GRUB_KEYBOARD_KEY_P,
/* 0x1a */ GRUB_KEYBOARD_KEY_LBRACKET, GRUB_KEYBOARD_KEY_RBRACKET,
/* 0x1c */ GRUB_KEYBOARD_KEY_ENTER, GRUB_KEYBOARD_KEY_LEFT_CTRL,
/* 0x1e */ GRUB_KEYBOARD_KEY_A, GRUB_KEYBOARD_KEY_S,
/* 0x20 */ GRUB_KEYBOARD_KEY_D, GRUB_KEYBOARD_KEY_F,
/* 0x22 */ GRUB_KEYBOARD_KEY_G, GRUB_KEYBOARD_KEY_H,
/* 0x24 */ GRUB_KEYBOARD_KEY_J, GRUB_KEYBOARD_KEY_K,
/* 0x26 */ GRUB_KEYBOARD_KEY_L, GRUB_KEYBOARD_KEY_SEMICOLON,
/* 0x28 */ GRUB_KEYBOARD_KEY_DQUOTE, GRUB_KEYBOARD_KEY_RQUOTE,
/* 0x2a */ GRUB_KEYBOARD_KEY_LEFT_SHIFT, GRUB_KEYBOARD_KEY_BACKSLASH,
/* 0x2c */ GRUB_KEYBOARD_KEY_Z, GRUB_KEYBOARD_KEY_X,
/* 0x2e */ GRUB_KEYBOARD_KEY_C, GRUB_KEYBOARD_KEY_V,
/* 0x30 */ GRUB_KEYBOARD_KEY_B, GRUB_KEYBOARD_KEY_N,
/* 0x32 */ GRUB_KEYBOARD_KEY_M, GRUB_KEYBOARD_KEY_COMMA,
/* 0x34 */ GRUB_KEYBOARD_KEY_DOT, GRUB_KEYBOARD_KEY_SLASH,
/* 0x36 */ GRUB_KEYBOARD_KEY_RIGHT_SHIFT, GRUB_KEYBOARD_KEY_NUMMUL,
/* 0x38 */ GRUB_KEYBOARD_KEY_LEFT_ALT, GRUB_KEYBOARD_KEY_SPACE,
/* 0x3a */ GRUB_KEYBOARD_KEY_CAPS_LOCK, GRUB_KEYBOARD_KEY_F1,
/* 0x3c */ GRUB_KEYBOARD_KEY_F2, GRUB_KEYBOARD_KEY_F3,
/* 0x3e */ GRUB_KEYBOARD_KEY_F4, GRUB_KEYBOARD_KEY_F5,
/* 0x40 */ GRUB_KEYBOARD_KEY_F6, GRUB_KEYBOARD_KEY_F7,
/* 0x42 */ GRUB_KEYBOARD_KEY_F8, GRUB_KEYBOARD_KEY_F9,
/* 0x44 */ GRUB_KEYBOARD_KEY_F10, GRUB_KEYBOARD_KEY_NUM_LOCK,
/* 0x46 */ GRUB_KEYBOARD_KEY_SCROLL_LOCK, GRUB_KEYBOARD_KEY_NUM7,
/* 0x48 */ GRUB_KEYBOARD_KEY_NUM8, GRUB_KEYBOARD_KEY_NUM9,
/* 0x4a */ GRUB_KEYBOARD_KEY_NUMMINUS, GRUB_KEYBOARD_KEY_NUM4,
/* 0x4c */ GRUB_KEYBOARD_KEY_NUM5, GRUB_KEYBOARD_KEY_NUM6,
/* 0x4e */ GRUB_KEYBOARD_KEY_NUMPLUS, GRUB_KEYBOARD_KEY_NUM1,
/* 0x50 */ GRUB_KEYBOARD_KEY_NUM2, GRUB_KEYBOARD_KEY_NUM3,
/* 0x52 */ GRUB_KEYBOARD_KEY_NUMDOT, GRUB_KEYBOARD_KEY_NUMDOT,
/* 0x54 */ 0, 0,
/* 0x56 */ GRUB_KEYBOARD_KEY_102ND, GRUB_KEYBOARD_KEY_F11,
/* 0x58 */ GRUB_KEYBOARD_KEY_F12, 0,
/* 0x5a */ 0, 0,
/* 0x5c */ 0, 0,
/* 0x5e */ 0, 0,
/* 0x60 */ 0, 0,
/* 0x62 */ 0, 0,
/* OLPC keys. Just mapped to normal keys. */
/* 0x64 */ 0, GRUB_KEYBOARD_KEY_UP,
/* 0x66 */ GRUB_KEYBOARD_KEY_DOWN, GRUB_KEYBOARD_KEY_LEFT,
/* 0x68 */ GRUB_KEYBOARD_KEY_RIGHT
};
static const struct
{
grub_uint8_t from, to;
} set1_e0_mapping[] =
{
{0x1c, GRUB_KEYBOARD_KEY_NUMENTER},
{0x1d, GRUB_KEYBOARD_KEY_RIGHT_CTRL},
{0x35, GRUB_KEYBOARD_KEY_NUMSLASH },
{0x38, GRUB_KEYBOARD_KEY_RIGHT_ALT},
{0x47, GRUB_KEYBOARD_KEY_HOME},
{0x48, GRUB_KEYBOARD_KEY_UP},
{0x49, GRUB_KEYBOARD_KEY_NPAGE},
{0x4b, GRUB_KEYBOARD_KEY_LEFT},
{0x4d, GRUB_KEYBOARD_KEY_RIGHT},
{0x4f, GRUB_KEYBOARD_KEY_END},
{0x50, GRUB_KEYBOARD_KEY_DOWN},
{0x51, GRUB_KEYBOARD_KEY_PPAGE},
{0x52, GRUB_KEYBOARD_KEY_INSERT},
{0x53, GRUB_KEYBOARD_KEY_DELETE},
};
static const grub_uint8_t set2_mapping[256] =
{
/* 0x00 */ 0, GRUB_KEYBOARD_KEY_F9,
/* 0x02 */ 0, GRUB_KEYBOARD_KEY_F5,
/* 0x04 */ GRUB_KEYBOARD_KEY_F3, GRUB_KEYBOARD_KEY_F1,
/* 0x06 */ GRUB_KEYBOARD_KEY_F2, GRUB_KEYBOARD_KEY_F12,
/* 0x08 */ 0, GRUB_KEYBOARD_KEY_F10,
/* 0x0a */ GRUB_KEYBOARD_KEY_F8, GRUB_KEYBOARD_KEY_F6,
/* 0x0c */ GRUB_KEYBOARD_KEY_F4, GRUB_KEYBOARD_KEY_TAB,
/* 0x0e */ GRUB_KEYBOARD_KEY_RQUOTE, 0,
/* 0x10 */ 0, GRUB_KEYBOARD_KEY_LEFT_ALT,
/* 0x12 */ GRUB_KEYBOARD_KEY_LEFT_SHIFT, 0,
/* 0x14 */ GRUB_KEYBOARD_KEY_LEFT_CTRL, GRUB_KEYBOARD_KEY_Q,
/* 0x16 */ GRUB_KEYBOARD_KEY_1, 0,
/* 0x18 */ 0, 0,
/* 0x1a */ GRUB_KEYBOARD_KEY_Z, GRUB_KEYBOARD_KEY_S,
/* 0x1c */ GRUB_KEYBOARD_KEY_A, GRUB_KEYBOARD_KEY_W,
/* 0x1e */ GRUB_KEYBOARD_KEY_2, 0,
/* 0x20 */ 0, GRUB_KEYBOARD_KEY_C,
/* 0x22 */ GRUB_KEYBOARD_KEY_X, GRUB_KEYBOARD_KEY_D,
/* 0x24 */ GRUB_KEYBOARD_KEY_E, GRUB_KEYBOARD_KEY_4,
/* 0x26 */ GRUB_KEYBOARD_KEY_3, 0,
/* 0x28 */ 0, GRUB_KEYBOARD_KEY_SPACE,
/* 0x2a */ GRUB_KEYBOARD_KEY_V, GRUB_KEYBOARD_KEY_F,
/* 0x2c */ GRUB_KEYBOARD_KEY_T, GRUB_KEYBOARD_KEY_R,
/* 0x2e */ GRUB_KEYBOARD_KEY_5, 0,
/* 0x30 */ 0, GRUB_KEYBOARD_KEY_N,
/* 0x32 */ GRUB_KEYBOARD_KEY_B, GRUB_KEYBOARD_KEY_H,
/* 0x34 */ GRUB_KEYBOARD_KEY_G, GRUB_KEYBOARD_KEY_Y,
/* 0x36 */ GRUB_KEYBOARD_KEY_6, 0,
/* 0x38 */ 0, 0,
/* 0x3a */ GRUB_KEYBOARD_KEY_M, GRUB_KEYBOARD_KEY_J,
/* 0x3c */ GRUB_KEYBOARD_KEY_U, GRUB_KEYBOARD_KEY_7,
/* 0x3e */ GRUB_KEYBOARD_KEY_8, 0,
/* 0x40 */ 0, GRUB_KEYBOARD_KEY_COMMA,
/* 0x42 */ GRUB_KEYBOARD_KEY_K, GRUB_KEYBOARD_KEY_I,
/* 0x44 */ GRUB_KEYBOARD_KEY_O, GRUB_KEYBOARD_KEY_0,
/* 0x46 */ GRUB_KEYBOARD_KEY_9, 0,
/* 0x48 */ 0, GRUB_KEYBOARD_KEY_DOT,
/* 0x4a */ GRUB_KEYBOARD_KEY_SLASH, GRUB_KEYBOARD_KEY_L,
/* 0x4c */ GRUB_KEYBOARD_KEY_SEMICOLON, GRUB_KEYBOARD_KEY_P,
/* 0x4e */ GRUB_KEYBOARD_KEY_DASH, 0,
/* 0x50 */ 0, 0,
/* 0x52 */ GRUB_KEYBOARD_KEY_DQUOTE, 0,
/* 0x54 */ GRUB_KEYBOARD_KEY_LBRACKET, GRUB_KEYBOARD_KEY_EQUAL,
/* 0x56 */ 0, 0,
/* 0x58 */ GRUB_KEYBOARD_KEY_CAPS_LOCK, GRUB_KEYBOARD_KEY_RIGHT_SHIFT,
/* 0x5a */ GRUB_KEYBOARD_KEY_ENTER, GRUB_KEYBOARD_KEY_RBRACKET,
/* 0x5c */ 0, GRUB_KEYBOARD_KEY_BACKSLASH,
/* 0x5e */ 0, 0,
/* 0x60 */ 0, GRUB_KEYBOARD_KEY_102ND,
/* 0x62 */ 0, 0,
/* 0x64 */ 0, 0,
/* 0x66 */ GRUB_KEYBOARD_KEY_BACKSPACE, 0,
/* 0x68 */ 0, GRUB_KEYBOARD_KEY_NUM1,
/* 0x6a */ 0, GRUB_KEYBOARD_KEY_NUM4,
/* 0x6c */ GRUB_KEYBOARD_KEY_NUM7, 0,
/* 0x6e */ 0, 0,
/* 0x70 */ GRUB_KEYBOARD_KEY_NUMDOT, GRUB_KEYBOARD_KEY_NUM0,
/* 0x72 */ GRUB_KEYBOARD_KEY_NUM2, GRUB_KEYBOARD_KEY_NUM5,
/* 0x74 */ GRUB_KEYBOARD_KEY_NUM6, GRUB_KEYBOARD_KEY_NUM8,
/* 0x76 */ GRUB_KEYBOARD_KEY_ESCAPE, GRUB_KEYBOARD_KEY_NUM_LOCK,
/* 0x78 */ GRUB_KEYBOARD_KEY_F11, GRUB_KEYBOARD_KEY_NUMPLUS,
/* 0x7a */ GRUB_KEYBOARD_KEY_NUM3, GRUB_KEYBOARD_KEY_NUMMINUS,
/* 0x7c */ GRUB_KEYBOARD_KEY_NUMMUL, GRUB_KEYBOARD_KEY_NUM9,
/* 0x7e */ GRUB_KEYBOARD_KEY_SCROLL_LOCK, 0,
/* 0x80 */ 0, 0,
/* 0x82 */ 0, GRUB_KEYBOARD_KEY_F7,
};
static const struct
{
grub_uint8_t from, to;
} set2_e0_mapping[] =
{
{0x11, GRUB_KEYBOARD_KEY_RIGHT_ALT},
{0x14, GRUB_KEYBOARD_KEY_RIGHT_CTRL},
{0x4a, GRUB_KEYBOARD_KEY_NUMSLASH},
{0x5a, GRUB_KEYBOARD_KEY_NUMENTER},
{0x69, GRUB_KEYBOARD_KEY_END},
{0x6b, GRUB_KEYBOARD_KEY_LEFT},
{0x6c, GRUB_KEYBOARD_KEY_HOME},
{0x70, GRUB_KEYBOARD_KEY_INSERT},
{0x71, GRUB_KEYBOARD_KEY_DELETE},
{0x72, GRUB_KEYBOARD_KEY_DOWN},
{0x74, GRUB_KEYBOARD_KEY_RIGHT},
{0x75, GRUB_KEYBOARD_KEY_UP},
{0x7a, GRUB_KEYBOARD_KEY_NPAGE},
{0x7d, GRUB_KEYBOARD_KEY_PPAGE},
};
static void
keyboard_controller_wait_until_ready (void)
{
while (! KEYBOARD_COMMAND_ISREADY (grub_inb (KEYBOARD_REG_STATUS)));
}
static grub_uint8_t
wait_ack (void)
{
grub_uint64_t endtime;
grub_uint8_t ack;
endtime = grub_get_time_ms () + 20;
do
ack = grub_inb (KEYBOARD_REG_DATA);
while (ack != GRUB_AT_ACK && ack != GRUB_AT_NACK
&& grub_get_time_ms () < endtime);
return ack;
}
static int
at_command (grub_uint8_t data)
{
unsigned i;
for (i = 0; i < GRUB_AT_TRIES; i++)
{
grub_uint8_t ack;
keyboard_controller_wait_until_ready ();
grub_outb (data, KEYBOARD_REG_STATUS);
ack = wait_ack ();
if (ack == GRUB_AT_NACK)
continue;
if (ack == GRUB_AT_ACK)
break;
return 0;
}
return (i != GRUB_AT_TRIES);
}
static void
grub_keyboard_controller_write (grub_uint8_t c)
{
at_command (KEYBOARD_COMMAND_WRITE);
keyboard_controller_wait_until_ready ();
grub_outb (c, KEYBOARD_REG_DATA);
}
static grub_uint8_t
grub_keyboard_controller_read (void)
{
at_command (KEYBOARD_COMMAND_READ);
keyboard_controller_wait_until_ready ();
return grub_inb (KEYBOARD_REG_DATA);
}
static int
write_mode (int mode)
{
unsigned i;
for (i = 0; i < GRUB_AT_TRIES; i++)
{
grub_uint8_t ack;
keyboard_controller_wait_until_ready ();
grub_outb (0xf0, KEYBOARD_REG_DATA);
keyboard_controller_wait_until_ready ();
grub_outb (mode, KEYBOARD_REG_DATA);
keyboard_controller_wait_until_ready ();
ack = wait_ack ();
if (ack == GRUB_AT_NACK)
continue;
if (ack == GRUB_AT_ACK)
break;
return 0;
}
return (i != GRUB_AT_TRIES);
}
static int
query_mode (void)
{
grub_uint8_t ret;
int e;
e = write_mode (0);
if (!e)
return 0;
keyboard_controller_wait_until_ready ();
do
ret = grub_inb (KEYBOARD_REG_DATA);
while (ret == GRUB_AT_ACK);
/* QEMU translates the set even in no-translate mode. */
if (ret == 0x43 || ret == 1)
return 1;
if (ret == 0x41 || ret == 2)
return 2;
if (ret == 0x3f || ret == 3)
return 3;
return 0;
}
static void
set_scancodes (void)
{
/* You must have visited computer museum. Keyboard without scancode set
knowledge. Assume XT. */
if (!grub_keyboard_orig_set)
{
grub_dprintf ("atkeyb", "No sets support assumed\n");
current_set = 1;
return;
}
grub_keyboard_controller_write (grub_keyboard_controller_orig
& ~KEYBOARD_AT_TRANSLATE);
write_mode (2);
current_set = query_mode ();
grub_dprintf ("atkeyb", "returned set %d\n", current_set);
if (current_set == 2)
return;
write_mode (1);
current_set = query_mode ();
grub_dprintf ("atkeyb", "returned set %d\n", current_set);
if (current_set == 1)
return;
grub_printf ("No supported scancode set found\n");
}
static void
keyboard_controller_led (grub_uint8_t leds)
{
keyboard_controller_wait_until_ready ();
grub_outb (0xed, KEYBOARD_REG_DATA);
keyboard_controller_wait_until_ready ();
grub_outb (leds & 0x7, KEYBOARD_REG_DATA);
}
static int
fetch_key (int *is_break)
{
int was_ext = 0;
grub_uint8_t at_key;
int ret = 0;
if (! KEYBOARD_ISREADY (grub_inb (KEYBOARD_REG_STATUS)))
return -1;
at_key = grub_inb (KEYBOARD_REG_DATA);
if (at_key == 0xe0)
{
e0_received = 1;
return -1;
}
if ((current_set == 2 || current_set == 3) && at_key == 0xf0)
{
f0_received = 1;
return -1;
}
/* Setting LEDs may generate ACKs. */
if (at_key == GRUB_AT_ACK)
return -1;
was_ext = e0_received;
e0_received = 0;
switch (current_set)
{
case 1:
*is_break = !!(at_key & 0x80);
if (!was_ext)
ret = set1_mapping[at_key & 0x7f];
else
{
unsigned i;
for (i = 0; i < ARRAY_SIZE (set1_e0_mapping); i++)
if (set1_e0_mapping[i].from == (at_key & 0x7f))
{
ret = set1_e0_mapping[i].to;
break;
}
}
break;
case 2:
*is_break = f0_received;
f0_received = 0;
if (!was_ext)
ret = set2_mapping[at_key];
else
{
unsigned i;
for (i = 0; i < ARRAY_SIZE (set2_e0_mapping); i++)
if (set2_e0_mapping[i].from == at_key)
{
ret = set2_e0_mapping[i].to;
break;
}
}
break;
default:
return -1;
}
if (!ret)
{
if (was_ext)
grub_printf ("Unknown key 0xe0+0x%02x from set %d\n",
at_key, current_set);
else
grub_printf ("Unknown key 0x%02x from set %d\n",
at_key, current_set);
return -1;
}
return ret;
}
/* FIXME: This should become an interrupt service routine. For now
it's just used to catch events from control keys. */
static int
grub_keyboard_isr (grub_keyboard_key_t key, int is_break)
{
if (!is_break)
switch (key)
{
case GRUB_KEYBOARD_KEY_LEFT_SHIFT:
at_keyboard_status |= GRUB_TERM_STATUS_LSHIFT;
return 1;
case GRUB_KEYBOARD_KEY_RIGHT_SHIFT:
at_keyboard_status |= GRUB_TERM_STATUS_RSHIFT;
return 1;
case GRUB_KEYBOARD_KEY_LEFT_CTRL:
at_keyboard_status |= GRUB_TERM_STATUS_LCTRL;
return 1;
case GRUB_KEYBOARD_KEY_RIGHT_CTRL:
at_keyboard_status |= GRUB_TERM_STATUS_RCTRL;
return 1;
case GRUB_KEYBOARD_KEY_RIGHT_ALT:
at_keyboard_status |= GRUB_TERM_STATUS_RALT;
return 1;
case GRUB_KEYBOARD_KEY_LEFT_ALT:
at_keyboard_status |= GRUB_TERM_STATUS_LALT;
return 1;
default:
return 0;
}
else
switch (key)
{
case GRUB_KEYBOARD_KEY_LEFT_SHIFT:
at_keyboard_status &= ~GRUB_TERM_STATUS_LSHIFT;
return 1;
case GRUB_KEYBOARD_KEY_RIGHT_SHIFT:
at_keyboard_status &= ~GRUB_TERM_STATUS_RSHIFT;
return 1;
case GRUB_KEYBOARD_KEY_LEFT_CTRL:
at_keyboard_status &= ~GRUB_TERM_STATUS_LCTRL;
return 1;
case GRUB_KEYBOARD_KEY_RIGHT_CTRL:
at_keyboard_status &= ~GRUB_TERM_STATUS_RCTRL;
return 1;
case GRUB_KEYBOARD_KEY_RIGHT_ALT:
at_keyboard_status &= ~GRUB_TERM_STATUS_RALT;
return 1;
case GRUB_KEYBOARD_KEY_LEFT_ALT:
at_keyboard_status &= ~GRUB_TERM_STATUS_LALT;
return 1;
default:
return 0;
}
}
/* If there is a raw key pending, return it; otherwise return -1. */
static int
grub_keyboard_getkey (void)
{
int key;
int is_break;
key = fetch_key (&is_break);
if (key == -1)
return -1;
if (grub_keyboard_isr (key, is_break))
return -1;
if (is_break)
return -1;
return key;
}
/* If there is a character pending, return it;
otherwise return GRUB_TERM_NO_KEY. */
static int
grub_at_keyboard_getkey (struct grub_term_input *term __attribute__ ((unused)))
{
int code;
code = grub_keyboard_getkey ();
if (code == -1)
return GRUB_TERM_NO_KEY;
#ifdef DEBUG_AT_KEYBOARD
grub_dprintf ("atkeyb", "Detected key 0x%x\n", key);
#endif
switch (code)
{
case GRUB_KEYBOARD_KEY_CAPS_LOCK:
at_keyboard_status ^= GRUB_TERM_STATUS_CAPS;
led_status ^= KEYBOARD_LED_CAPS;
keyboard_controller_led (led_status);
#ifdef DEBUG_AT_KEYBOARD
grub_dprintf ("atkeyb", "caps_lock = %d\n", !!(at_keyboard_status & KEYBOARD_STATUS_CAPS_LOCK));
#endif
return GRUB_TERM_NO_KEY;
case GRUB_KEYBOARD_KEY_NUM_LOCK:
at_keyboard_status ^= GRUB_TERM_STATUS_NUM;
led_status ^= KEYBOARD_LED_NUM;
keyboard_controller_led (led_status);
#ifdef DEBUG_AT_KEYBOARD
grub_dprintf ("atkeyb", "num_lock = %d\n", !!(at_keyboard_status & KEYBOARD_STATUS_NUM_LOCK));
#endif
return GRUB_TERM_NO_KEY;
case GRUB_KEYBOARD_KEY_SCROLL_LOCK:
at_keyboard_status ^= GRUB_TERM_STATUS_SCROLL;
led_status ^= KEYBOARD_LED_SCROLL;
keyboard_controller_led (led_status);
return GRUB_TERM_NO_KEY;
default:
return grub_term_map_key (code, at_keyboard_status);
}
}
static grub_err_t
grub_keyboard_controller_init (struct grub_term_input *term __attribute__ ((unused)))
{
at_keyboard_status = 0;
/* Drain input buffer. */
while (1)
{
keyboard_controller_wait_until_ready ();
if (! KEYBOARD_ISREADY (grub_inb (KEYBOARD_REG_STATUS)))
break;
keyboard_controller_wait_until_ready ();
grub_inb (KEYBOARD_REG_DATA);
}
grub_keyboard_controller_orig = grub_keyboard_controller_read ();
grub_keyboard_orig_set = query_mode ();
set_scancodes ();
keyboard_controller_led (led_status);
return GRUB_ERR_NONE;
}
static grub_err_t
grub_keyboard_controller_fini (struct grub_term_input *term __attribute__ ((unused)))
{
if (grub_keyboard_orig_set)
write_mode (grub_keyboard_orig_set);
grub_keyboard_controller_write (grub_keyboard_controller_orig);
return GRUB_ERR_NONE;
}
static grub_err_t
grub_at_fini_hw (int noreturn __attribute__ ((unused)))
{
return grub_keyboard_controller_fini (NULL);
}
static grub_err_t
grub_at_restore_hw (void)
{
/* Drain input buffer. */
while (1)
{
keyboard_controller_wait_until_ready ();
if (! KEYBOARD_ISREADY (grub_inb (KEYBOARD_REG_STATUS)))
break;
keyboard_controller_wait_until_ready ();
grub_inb (KEYBOARD_REG_DATA);
}
set_scancodes ();
keyboard_controller_led (led_status);
return GRUB_ERR_NONE;
}
static struct grub_term_input grub_at_keyboard_term =
{
.name = "at_keyboard",
.init = grub_keyboard_controller_init,
.fini = grub_keyboard_controller_fini,
.getkey = grub_at_keyboard_getkey
};
GRUB_MOD_INIT(at_keyboard)
{
grub_term_register_input ("at_keyboard", &grub_at_keyboard_term);
grub_loader_register_preboot_hook (grub_at_fini_hw, grub_at_restore_hw,
GRUB_LOADER_PREBOOT_HOOK_PRIO_CONSOLE);
}
GRUB_MOD_FINI(at_keyboard)
{
grub_keyboard_controller_fini (NULL);
grub_term_unregister_input (&grub_at_keyboard_term);
}

View file

@ -0,0 +1,296 @@
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 2006,2007,2008 Free Software Foundation, Inc.
*
* GRUB 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.
*
* GRUB 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 GRUB. If not, see <http://www.gnu.org/licenses/>.
*/
#include <grub/term.h>
#include <grub/misc.h>
#include <grub/types.h>
#include <grub/err.h>
#include <grub/efi/efi.h>
#include <grub/efi/api.h>
#include <grub/efi/console.h>
static const grub_uint8_t
grub_console_standard_color = GRUB_EFI_TEXT_ATTR (GRUB_EFI_YELLOW,
GRUB_EFI_BACKGROUND_BLACK);
static grub_uint32_t
map_char (grub_uint32_t c)
{
/* Map some unicode characters to the EFI character. */
switch (c)
{
case GRUB_UNICODE_LEFTARROW:
c = GRUB_UNICODE_BLACK_LEFT_TRIANGLE;
break;
case GRUB_UNICODE_UPARROW:
c = GRUB_UNICODE_BLACK_UP_TRIANGLE;
break;
case GRUB_UNICODE_RIGHTARROW:
c = GRUB_UNICODE_BLACK_RIGHT_TRIANGLE;
break;
case GRUB_UNICODE_DOWNARROW:
c = GRUB_UNICODE_BLACK_DOWN_TRIANGLE;
break;
case GRUB_UNICODE_HLINE:
c = GRUB_UNICODE_LIGHT_HLINE;
break;
case GRUB_UNICODE_VLINE:
c = GRUB_UNICODE_LIGHT_VLINE;
break;
case GRUB_UNICODE_CORNER_UL:
c = GRUB_UNICODE_LIGHT_CORNER_UL;
break;
case GRUB_UNICODE_CORNER_UR:
c = GRUB_UNICODE_LIGHT_CORNER_UR;
break;
case GRUB_UNICODE_CORNER_LL:
c = GRUB_UNICODE_LIGHT_CORNER_LL;
break;
case GRUB_UNICODE_CORNER_LR:
c = GRUB_UNICODE_LIGHT_CORNER_LR;
break;
}
return c;
}
static void
grub_console_putchar (struct grub_term_output *term __attribute__ ((unused)),
const struct grub_unicode_glyph *c)
{
grub_efi_char16_t str[2 + c->ncomb];
grub_efi_simple_text_output_interface_t *o;
unsigned i, j;
if (grub_efi_is_finished)
return;
o = grub_efi_system_table->con_out;
/* For now, do not try to use a surrogate pair. */
if (c->base > 0xffff)
str[0] = '?';
else
str[0] = (grub_efi_char16_t) map_char (c->base & 0xffff);
j = 1;
for (i = 0; i < c->ncomb; i++)
if (c->base < 0xffff)
str[j++] = c->combining[i].code;
str[j] = 0;
/* Should this test be cached? */
if ((c->base > 0x7f || c->ncomb)
&& efi_call_2 (o->test_string, o, str) != GRUB_EFI_SUCCESS)
return;
efi_call_2 (o->output_string, o, str);
}
const unsigned efi_codes[] =
{
0, GRUB_TERM_UP, GRUB_TERM_DOWN, GRUB_TERM_RIGHT,
GRUB_TERM_LEFT, GRUB_TERM_HOME, GRUB_TERM_END, GRUB_TERM_KEY_INSERT,
GRUB_TERM_DC, GRUB_TERM_KEY_PPAGE, GRUB_TERM_KEY_NPAGE, GRUB_TERM_KEY_F1,
GRUB_TERM_KEY_F2, GRUB_TERM_KEY_F3, GRUB_TERM_KEY_F4, GRUB_TERM_KEY_F5,
GRUB_TERM_KEY_F6, GRUB_TERM_KEY_F7, GRUB_TERM_KEY_F8, GRUB_TERM_KEY_F9,
GRUB_TERM_KEY_F10, 0, 0, '\e'
};
static int
grub_console_getkey (struct grub_term_input *term __attribute__ ((unused)))
{
grub_efi_simple_input_interface_t *i;
grub_efi_input_key_t key;
grub_efi_status_t status;
if (grub_efi_is_finished)
return 0;
i = grub_efi_system_table->con_in;
status = efi_call_2 (i->read_key_stroke, i, &key);
if (status != GRUB_EFI_SUCCESS)
return GRUB_TERM_NO_KEY;
if (key.scan_code == 0)
return key.unicode_char;
else if (key.scan_code < ARRAY_SIZE (efi_codes))
return efi_codes[key.scan_code];
return GRUB_TERM_NO_KEY;
}
static grub_uint16_t
grub_console_getwh (struct grub_term_output *term __attribute__ ((unused)))
{
grub_efi_simple_text_output_interface_t *o;
grub_efi_uintn_t columns, rows;
o = grub_efi_system_table->con_out;
if (grub_efi_is_finished || efi_call_4 (o->query_mode, o, o->mode->mode,
&columns, &rows) != GRUB_EFI_SUCCESS)
{
/* Why does this fail? */
columns = 80;
rows = 25;
}
return ((columns << 8) | rows);
}
static grub_uint16_t
grub_console_getxy (struct grub_term_output *term __attribute__ ((unused)))
{
grub_efi_simple_text_output_interface_t *o;
if (grub_efi_is_finished)
return 0;
o = grub_efi_system_table->con_out;
return ((o->mode->cursor_column << 8) | o->mode->cursor_row);
}
static void
grub_console_gotoxy (struct grub_term_output *term __attribute__ ((unused)),
grub_uint8_t x, grub_uint8_t y)
{
grub_efi_simple_text_output_interface_t *o;
if (grub_efi_is_finished)
return;
o = grub_efi_system_table->con_out;
efi_call_3 (o->set_cursor_position, o, x, y);
}
static void
grub_console_cls (struct grub_term_output *term __attribute__ ((unused)))
{
grub_efi_simple_text_output_interface_t *o;
grub_efi_int32_t orig_attr;
if (grub_efi_is_finished)
return;
o = grub_efi_system_table->con_out;
orig_attr = o->mode->attribute;
efi_call_2 (o->set_attributes, o, GRUB_EFI_BACKGROUND_BLACK);
efi_call_1 (o->clear_screen, o);
efi_call_2 (o->set_attributes, o, orig_attr);
}
static void
grub_console_setcolorstate (struct grub_term_output *term,
grub_term_color_state state)
{
grub_efi_simple_text_output_interface_t *o;
if (grub_efi_is_finished)
return;
o = grub_efi_system_table->con_out;
switch (state) {
case GRUB_TERM_COLOR_STANDARD:
efi_call_2 (o->set_attributes, o, grub_console_standard_color);
break;
case GRUB_TERM_COLOR_NORMAL:
efi_call_2 (o->set_attributes, o, term->normal_color);
break;
case GRUB_TERM_COLOR_HIGHLIGHT:
efi_call_2 (o->set_attributes, o, term->highlight_color);
break;
default:
break;
}
}
static void
grub_console_setcursor (struct grub_term_output *term __attribute__ ((unused)),
int on)
{
grub_efi_simple_text_output_interface_t *o;
if (grub_efi_is_finished)
return;
o = grub_efi_system_table->con_out;
efi_call_2 (o->enable_cursor, o, on);
}
static grub_err_t
grub_efi_console_init (struct grub_term_output *term)
{
grub_console_setcursor (term, 1);
return 0;
}
static grub_err_t
grub_efi_console_fini (struct grub_term_output *term)
{
grub_console_setcursor (term, 0);
return 0;
}
static struct grub_term_input grub_console_term_input =
{
.name = "console",
.getkey = grub_console_getkey,
};
static struct grub_term_output grub_console_term_output =
{
.name = "console",
.init = grub_efi_console_init,
.fini = grub_efi_console_fini,
.putchar = grub_console_putchar,
.getwh = grub_console_getwh,
.getxy = grub_console_getxy,
.gotoxy = grub_console_gotoxy,
.cls = grub_console_cls,
.setcolorstate = grub_console_setcolorstate,
.setcursor = grub_console_setcursor,
.normal_color = GRUB_EFI_TEXT_ATTR (GRUB_EFI_LIGHTGRAY,
GRUB_EFI_BACKGROUND_BLACK),
.highlight_color = GRUB_EFI_TEXT_ATTR (GRUB_EFI_BLACK,
GRUB_EFI_BACKGROUND_LIGHTGRAY),
.flags = GRUB_TERM_CODE_TYPE_VISUAL_GLYPHS
};
void
grub_console_init (void)
{
/* FIXME: it is necessary to consider the case where no console control
is present but the default is already in text mode. */
if (! grub_efi_set_text_mode (1))
{
grub_error (GRUB_ERR_BAD_DEVICE, "cannot set text mode");
return;
}
grub_term_register_input ("console", &grub_console_term_input);
grub_term_register_output ("console", &grub_console_term_output);
}
void
grub_console_fini (void)
{
grub_term_unregister_input (&grub_console_term_input);
grub_term_unregister_output (&grub_console_term_output);
}

1222
grub-core/term/gfxterm.c Normal file

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,68 @@
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 2002,2003,2005,2007,2008,2009 Free Software Foundation, Inc.
*
* GRUB 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.
*
* GRUB 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 GRUB. If not, see <http://www.gnu.org/licenses/>.
*/
#include <grub/machine/memory.h>
#include <grub/machine/console.h>
#include <grub/term.h>
#include <grub/types.h>
static const struct grub_machine_bios_data_area *bios_data_area =
(struct grub_machine_bios_data_area *) GRUB_MEMORY_MACHINE_BIOS_DATA_AREA_ADDR;
static int
grub_console_getkeystatus (struct grub_term_input *term __attribute__ ((unused)))
{
/* conveniently GRUB keystatus is modelled after BIOS one. */
return bios_data_area->keyboard_flag_lower & ~0x80;
}
static struct grub_term_input grub_console_term_input =
{
.name = "console",
.getkey = grub_console_getkey,
.getkeystatus = grub_console_getkeystatus
};
static struct grub_term_output grub_console_term_output =
{
.name = "console",
.putchar = grub_console_putchar,
.getwh = grub_console_getwh,
.getxy = grub_console_getxy,
.gotoxy = grub_console_gotoxy,
.cls = grub_console_cls,
.setcolorstate = grub_console_setcolorstate,
.setcursor = grub_console_setcursor,
.flags = GRUB_TERM_CODE_TYPE_CP437,
.normal_color = GRUB_TERM_DEFAULT_NORMAL_COLOR,
.highlight_color = GRUB_TERM_DEFAULT_HIGHLIGHT_COLOR,
};
void
grub_console_init (void)
{
grub_term_register_output ("console", &grub_console_term_output);
grub_term_register_input ("console", &grub_console_term_input);
}
void
grub_console_fini (void)
{
grub_term_unregister_input (&grub_console_term_input);
grub_term_unregister_output (&grub_console_term_output);
}

View file

@ -0,0 +1,173 @@
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 2007, 2008 Free Software Foundation, Inc.
*
* GRUB 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.
*
* GRUB 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 GRUB. If not, see <http://www.gnu.org/licenses/>.
*/
#include <grub/dl.h>
#include <grub/i386/vga_common.h>
#include <grub/i386/io.h>
#include <grub/types.h>
#include <grub/vga.h>
#define COLS 80
#define ROWS 25
static int grub_curr_x, grub_curr_y;
#define VGA_TEXT_SCREEN 0xb8000
static void
screen_write_char (int x, int y, short c)
{
((short *) VGA_TEXT_SCREEN)[y * COLS + x] = c;
}
static short
screen_read_char (int x, int y)
{
return ((short *) VGA_TEXT_SCREEN)[y * COLS + x];
}
static void
update_cursor (void)
{
unsigned int pos = grub_curr_y * COLS + grub_curr_x;
grub_vga_cr_write (pos >> 8, GRUB_VGA_CR_CURSOR_ADDR_HIGH);
grub_vga_cr_write (pos & 0xFF, GRUB_VGA_CR_CURSOR_ADDR_LOW);
}
static void
inc_y (void)
{
grub_curr_x = 0;
if (grub_curr_y < ROWS - 1)
grub_curr_y++;
else
{
int x, y;
for (y = 0; y < ROWS - 1; y++)
for (x = 0; x < COLS; x++)
screen_write_char (x, y, screen_read_char (x, y + 1));
for (x = 0; x < COLS; x++)
screen_write_char (x, ROWS - 1, ' ' | (grub_console_cur_color << 8));
}
}
static void
inc_x (void)
{
if (grub_curr_x >= COLS - 1)
inc_y ();
else
grub_curr_x++;
}
static void
grub_vga_text_putchar (struct grub_term_output *term __attribute__ ((unused)),
const struct grub_unicode_glyph *c)
{
switch (c->base)
{
case '\b':
if (grub_curr_x != 0)
screen_write_char (grub_curr_x--, grub_curr_y, ' ');
break;
case '\n':
inc_y ();
break;
case '\r':
grub_curr_x = 0;
break;
default:
screen_write_char (grub_curr_x, grub_curr_y,
c->base | (grub_console_cur_color << 8));
inc_x ();
}
update_cursor ();
}
static grub_uint16_t
grub_vga_text_getxy (struct grub_term_output *term __attribute__ ((unused)))
{
return (grub_curr_x << 8) | grub_curr_y;
}
static void
grub_vga_text_gotoxy (struct grub_term_output *term __attribute__ ((unused)),
grub_uint8_t x, grub_uint8_t y)
{
grub_curr_x = x;
grub_curr_y = y;
update_cursor ();
}
static void
grub_vga_text_cls (struct grub_term_output *term)
{
int i;
for (i = 0; i < ROWS * COLS; i++)
((short *) VGA_TEXT_SCREEN)[i] = ' ' | (grub_console_cur_color << 8);
grub_vga_text_gotoxy (term, 0, 0);
}
static void
grub_vga_text_setcursor (struct grub_term_output *term __attribute__ ((unused)),
int on)
{
grub_uint8_t old;
old = grub_vga_cr_read (GRUB_VGA_CR_CURSOR_START);
if (on)
grub_vga_cr_write (old & ~GRUB_VGA_CR_CURSOR_START_DISABLE,
GRUB_VGA_CR_CURSOR_START);
else
grub_vga_cr_write (old | GRUB_VGA_CR_CURSOR_START_DISABLE,
GRUB_VGA_CR_CURSOR_START);
}
static grub_err_t
grub_vga_text_init_fini (struct grub_term_output *term)
{
grub_vga_text_cls (term);
return 0;
}
static struct grub_term_output grub_vga_text_term =
{
.name = "vga_text",
.init = grub_vga_text_init_fini,
.fini = grub_vga_text_init_fini,
.putchar = grub_vga_text_putchar,
.getwh = grub_console_getwh,
.getxy = grub_vga_text_getxy,
.gotoxy = grub_vga_text_gotoxy,
.cls = grub_vga_text_cls,
.setcolorstate = grub_console_setcolorstate,
.setcursor = grub_vga_text_setcursor,
.flags = GRUB_TERM_CODE_TYPE_CP437,
.normal_color = GRUB_TERM_DEFAULT_NORMAL_COLOR,
.highlight_color = GRUB_TERM_DEFAULT_HIGHLIGHT_COLOR,
};
GRUB_MOD_INIT(vga_text)
{
grub_term_register_output ("vga_text", &grub_vga_text_term);
}
GRUB_MOD_FINI(vga_text)
{
grub_term_unregister_output (&grub_vga_text_term);
}

View file

@ -0,0 +1,48 @@
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 2002,2003,2005,2007,2008 Free Software Foundation, Inc.
*
* GRUB 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.
*
* GRUB 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 GRUB. If not, see <http://www.gnu.org/licenses/>.
*/
#include <grub/i386/vga_common.h>
#include <grub/term.h>
#include <grub/types.h>
grub_uint8_t grub_console_cur_color = 0x7;
grub_uint16_t
grub_console_getwh (struct grub_term_output *term __attribute__ ((unused)))
{
return (80 << 8) | 25;
}
void
grub_console_setcolorstate (struct grub_term_output *term,
grub_term_color_state state)
{
switch (state) {
case GRUB_TERM_COLOR_STANDARD:
grub_console_cur_color = GRUB_TERM_DEFAULT_STANDARD_COLOR & 0x7f;
break;
case GRUB_TERM_COLOR_NORMAL:
grub_console_cur_color = term->normal_color & 0x7f;
break;
case GRUB_TERM_COLOR_HIGHLIGHT:
grub_console_cur_color = term->highlight_color & 0x7f;
break;
default:
break;
}
}

View file

@ -0,0 +1,245 @@
/* ofconsole.c -- Open Firmware console for GRUB. */
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 2003,2004,2005,2007,2008,2009 Free Software Foundation, Inc.
*
* GRUB 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.
*
* GRUB 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 GRUB. If not, see <http://www.gnu.org/licenses/>.
*/
#include <grub/term.h>
#include <grub/types.h>
#include <grub/misc.h>
#include <grub/mm.h>
#include <grub/time.h>
#include <grub/terminfo.h>
#include <grub/ieee1275/console.h>
#include <grub/ieee1275/ieee1275.h>
static grub_ieee1275_ihandle_t stdout_ihandle;
static grub_ieee1275_ihandle_t stdin_ihandle;
static grub_uint8_t grub_ofconsole_width;
static grub_uint8_t grub_ofconsole_height;
struct color
{
int red;
int green;
int blue;
};
/* Use serial colors as they are default on most firmwares and some firmwares
ignore set-color!. Additionally output may be redirected to serial. */
static struct color colors[] =
{
// {R, G, B}
{0x00, 0x00, 0x00}, // 0 = black
{0xA8, 0x00, 0x00}, // 1 = red
{0x00, 0xA8, 0x00}, // 2 = green
{0xFE, 0xFE, 0x54}, // 3 = yellow
{0x00, 0x00, 0xA8}, // 4 = blue
{0xA8, 0x00, 0xA8}, // 5 = magenta
{0x00, 0xA8, 0xA8}, // 6 = cyan
{0xFE, 0xFE, 0xFE} // 7 = white
};
static void
put (struct grub_term_output *term __attribute__ ((unused)), const int c)
{
char chr = c;
grub_ieee1275_write (stdout_ihandle, &chr, 1, 0);
}
static int
readkey (struct grub_term_input *term __attribute__ ((unused)))
{
grub_uint8_t c;
grub_ssize_t actual = 0;
grub_ieee1275_read (stdin_ihandle, &c, 1, &actual);
if (actual > 0)
return c;
return -1;
}
static void
grub_ofconsole_dimensions (void)
{
grub_ieee1275_ihandle_t options;
grub_ssize_t lval;
if (! grub_ieee1275_finddevice ("/options", &options)
&& options != (grub_ieee1275_ihandle_t) -1)
{
if (! grub_ieee1275_get_property_length (options, "screen-#columns",
&lval)
&& lval >= 0 && lval < 1024)
{
char val[lval];
if (! grub_ieee1275_get_property (options, "screen-#columns",
val, lval, 0))
grub_ofconsole_width = (grub_uint8_t) grub_strtoul (val, 0, 10);
}
if (! grub_ieee1275_get_property_length (options, "screen-#rows", &lval)
&& lval >= 0 && lval < 1024)
{
char val[lval];
if (! grub_ieee1275_get_property (options, "screen-#rows",
val, lval, 0))
grub_ofconsole_height = (grub_uint8_t) grub_strtoul (val, 0, 10);
}
}
/* Use a small console by default. */
if (! grub_ofconsole_width)
grub_ofconsole_width = 80;
if (! grub_ofconsole_height)
grub_ofconsole_height = 24;
}
static grub_uint16_t
grub_ofconsole_getwh (struct grub_term_output *term __attribute__ ((unused)))
{
return (grub_ofconsole_width << 8) | grub_ofconsole_height;
}
static void
grub_ofconsole_setcursor (struct grub_term_output *term __attribute__ ((unused)),
int on)
{
/* Understood by the Open Firmware flavour in OLPC. */
if (on)
grub_ieee1275_interpret ("cursor-on", 0);
else
grub_ieee1275_interpret ("cursor-off", 0);
}
static grub_err_t
grub_ofconsole_init_input (struct grub_term_input *term)
{
grub_ssize_t actual;
if (grub_ieee1275_get_integer_property (grub_ieee1275_chosen, "stdin", &stdin_ihandle,
sizeof stdin_ihandle, &actual)
|| actual != sizeof stdin_ihandle)
return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "cannot find stdin");
return grub_terminfo_input_init (term);
}
static grub_err_t
grub_ofconsole_init_output (struct grub_term_output *term)
{
grub_ssize_t actual;
/* The latest PowerMacs don't actually initialize the screen for us, so we
* use this trick to re-open the output device (but we avoid doing this on
* platforms where it's known to be broken). */
if (! grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_BROKEN_OUTPUT))
grub_ieee1275_interpret ("output-device output", 0);
if (grub_ieee1275_get_integer_property (grub_ieee1275_chosen, "stdout", &stdout_ihandle,
sizeof stdout_ihandle, &actual)
|| actual != sizeof stdout_ihandle)
return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "cannot find stdout");
/* Initialize colors. */
if (! grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_CANNOT_SET_COLORS))
{
unsigned col;
for (col = 0; col < ARRAY_SIZE (colors); col++)
grub_ieee1275_set_color (stdout_ihandle, col, colors[col].red,
colors[col].green, colors[col].blue);
/* Set the right fg and bg colors. */
grub_terminfo_setcolorstate (term, GRUB_TERM_COLOR_NORMAL);
}
grub_ofconsole_dimensions ();
return 0;
}
struct grub_terminfo_input_state grub_ofconsole_terminfo_input =
{
.readkey = readkey
};
struct grub_terminfo_output_state grub_ofconsole_terminfo_output =
{
.put = put
};
static struct grub_term_input grub_ofconsole_term_input =
{
.name = "ofconsole",
.init = grub_ofconsole_init_input,
.getkey = grub_terminfo_getkey,
.data = &grub_ofconsole_terminfo_input
};
static struct grub_term_output grub_ofconsole_term_output =
{
.name = "ofconsole",
.init = grub_ofconsole_init_output,
.putchar = grub_terminfo_putchar,
.getxy = grub_terminfo_getxy,
.getwh = grub_ofconsole_getwh,
.gotoxy = grub_terminfo_gotoxy,
.cls = grub_terminfo_cls,
.setcolorstate = grub_terminfo_setcolorstate,
.setcursor = grub_ofconsole_setcursor,
.flags = GRUB_TERM_CODE_TYPE_ASCII,
.data = &grub_ofconsole_terminfo_output,
.normal_color = GRUB_TERM_DEFAULT_NORMAL_COLOR,
.highlight_color = GRUB_TERM_DEFAULT_HIGHLIGHT_COLOR,
};
void grub_terminfo_fini (void);
void grub_terminfo_init (void);
void
grub_console_init_early (void)
{
grub_term_register_input ("ofconsole", &grub_ofconsole_term_input);
grub_term_register_output ("ofconsole", &grub_ofconsole_term_output);
}
void
grub_console_init_lately (void)
{
const char *type;
if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_NO_ANSI))
type = "dumb";
else
type = "ieee1275";
grub_terminfo_init ();
grub_terminfo_output_register (&grub_ofconsole_term_output, type);
}
void
grub_console_fini (void)
{
grub_term_unregister_input (&grub_ofconsole_term_input);
grub_term_unregister_output (&grub_ofconsole_term_output);
grub_terminfo_output_unregister (&grub_ofconsole_term_output);
grub_terminfo_fini ();
}

263
grub-core/term/ns8250.c Normal file
View file

@ -0,0 +1,263 @@
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 2000,2001,2002,2003,2004,2005,2007,2008,2009,2010 Free Software Foundation, Inc.
*
* GRUB 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.
*
* GRUB 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 GRUB. If not, see <http://www.gnu.org/licenses/>.
*/
#include <grub/machine/memory.h>
#include <grub/serial.h>
#include <grub/ns8250.h>
#include <grub/types.h>
#include <grub/dl.h>
#include <grub/misc.h>
#include <grub/cpu/io.h>
#include <grub/mm.h>
#ifdef GRUB_MACHINE_PCBIOS
static const unsigned short *serial_hw_io_addr = (const unsigned short *) GRUB_MEMORY_MACHINE_BIOS_DATA_AREA_ADDR;
#define GRUB_SERIAL_PORT_NUM 4
#else
#include <grub/machine/serial.h>
static const grub_port_t serial_hw_io_addr[] = GRUB_MACHINE_SERIAL_PORTS;
#define GRUB_SERIAL_PORT_NUM (ARRAY_SIZE(serial_hw_io_addr))
#endif
/* Convert speed to divisor. */
static unsigned short
serial_get_divisor (unsigned int speed)
{
unsigned int i;
/* The structure for speed vs. divisor. */
struct divisor
{
unsigned int speed;
unsigned short div;
};
/* The table which lists common configurations. */
/* 1843200 / (speed * 16) */
static struct divisor divisor_tab[] =
{
{ 2400, 0x0030 },
{ 4800, 0x0018 },
{ 9600, 0x000C },
{ 19200, 0x0006 },
{ 38400, 0x0003 },
{ 57600, 0x0002 },
{ 115200, 0x0001 }
};
/* Set the baud rate. */
for (i = 0; i < ARRAY_SIZE (divisor_tab); i++)
if (divisor_tab[i].speed == speed)
/* UART in Yeeloong runs twice the usual rate. */
#ifdef GRUB_MACHINE_MIPS_YEELOONG
return 2 * divisor_tab[i].div;
#else
return divisor_tab[i].div;
#endif
return 0;
}
static void
do_real_config (struct grub_serial_port *port)
{
int divisor;
unsigned char status = 0;
const unsigned char parities[] = {
[GRUB_SERIAL_PARITY_NONE] = UART_NO_PARITY,
[GRUB_SERIAL_PARITY_ODD] = UART_ODD_PARITY,
[GRUB_SERIAL_PARITY_EVEN] = UART_EVEN_PARITY
};
const unsigned char stop_bits[] = {
[GRUB_SERIAL_STOP_BITS_1] = UART_1_STOP_BIT,
[GRUB_SERIAL_STOP_BITS_2] = UART_2_STOP_BITS,
};
if (port->configured)
return;
divisor = serial_get_divisor (port->config.speed);
/* Turn off the interrupt. */
grub_outb (0, port->port + UART_IER);
/* Set DLAB. */
grub_outb (UART_DLAB, port->port + UART_LCR);
/* Set the baud rate. */
grub_outb (divisor & 0xFF, port->port + UART_DLL);
grub_outb (divisor >> 8, port->port + UART_DLH);
/* Set the line status. */
status |= (parities[port->config.parity]
| (port->config.word_len - 5)
| stop_bits[port->config.stop_bits]);
grub_outb (status, port->port + UART_LCR);
/* In Yeeloong serial port has only 3 wires. */
#ifndef GRUB_MACHINE_MIPS_YEELOONG
/* Enable the FIFO. */
grub_outb (UART_ENABLE_FIFO_TRIGGER1, port->port + UART_FCR);
/* Turn on DTR and RTS. */
grub_outb (UART_ENABLE_DTRRTS, port->port + UART_MCR);
#else
/* Enable the FIFO. */
grub_outb (UART_ENABLE_FIFO_TRIGGER14, port->port + UART_FCR);
/* Turn on DTR, RTS, and OUT2. */
grub_outb (UART_ENABLE_DTRRTS | UART_ENABLE_OUT2, port->port + UART_MCR);
#endif
/* Drain the input buffer. */
while (grub_inb (port->port + UART_LSR) & UART_DATA_READY)
grub_inb (port->port + UART_RX);
port->configured = 1;
}
/* Fetch a key. */
static int
serial_hw_fetch (struct grub_serial_port *port)
{
do_real_config (port);
if (grub_inb (port->port + UART_LSR) & UART_DATA_READY)
return grub_inb (port->port + UART_RX);
return -1;
}
/* Put a character. */
static void
serial_hw_put (struct grub_serial_port *port, const int c)
{
unsigned int timeout = 100000;
do_real_config (port);
/* Wait until the transmitter holding register is empty. */
while ((grub_inb (port->port + UART_LSR) & UART_EMPTY_TRANSMITTER) == 0)
{
if (--timeout == 0)
/* There is something wrong. But what can I do? */
return;
}
grub_outb (c, port->port + UART_TX);
}
/* 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 grub_err_t
serial_hw_configure (struct grub_serial_port *port,
struct grub_serial_config *config)
{
unsigned short divisor;
divisor = serial_get_divisor (config->speed);
if (divisor == 0)
return grub_error (GRUB_ERR_BAD_ARGUMENT, "bad speed");
if (config->parity != GRUB_SERIAL_PARITY_NONE
&& config->parity != GRUB_SERIAL_PARITY_ODD
&& config->parity != GRUB_SERIAL_PARITY_EVEN)
return grub_error (GRUB_ERR_BAD_ARGUMENT, "unsupported parity");
if (config->stop_bits != GRUB_SERIAL_STOP_BITS_1
&& config->stop_bits != GRUB_SERIAL_STOP_BITS_2)
return grub_error (GRUB_ERR_BAD_ARGUMENT, "unsupported stop bits");
if (config->word_len < 5 || config->word_len > 8)
return grub_error (GRUB_ERR_BAD_ARGUMENT, "unsupported word length");
port->config = *config;
port->configured = 0;
/* FIXME: should check if the serial terminal was found. */
return GRUB_ERR_NONE;
}
struct grub_serial_driver grub_ns8250_driver =
{
.configure = serial_hw_configure,
.fetch = serial_hw_fetch,
.put = serial_hw_put
};
static char com_names[GRUB_SERIAL_PORT_NUM][20];
static struct grub_serial_port com_ports[GRUB_SERIAL_PORT_NUM];
void
grub_ns8250_init (void)
{
unsigned i;
for (i = 0; i < GRUB_SERIAL_PORT_NUM; i++)
if (serial_hw_io_addr[i])
{
grub_err_t err;
grub_snprintf (com_names[i], sizeof (com_names[i]), "com%d", i);
com_ports[i].name = com_names[i];
com_ports[i].driver = &grub_ns8250_driver;
com_ports[i].port = serial_hw_io_addr[i];
err = grub_serial_config_defaults (&com_ports[i]);
if (err)
grub_print_error ();
grub_serial_register (&com_ports[i]);
}
}
/* Return the port number for the UNITth serial device. */
grub_port_t
grub_ns8250_hw_get_port (const unsigned int unit)
{
if (unit < GRUB_SERIAL_PORT_NUM)
return serial_hw_io_addr[unit];
else
return 0;
}
char *
grub_serial_ns8250_add_port (grub_port_t port)
{
struct grub_serial_port *p;
unsigned i;
for (i = 0; i < GRUB_SERIAL_PORT_NUM; i++)
if (com_ports[i].port == port)
return com_names[i];
p = grub_malloc (sizeof (*p));
if (!p)
return NULL;
p->name = grub_xasprintf ("port%lx", (unsigned long) port);
if (!p->name)
{
grub_free (p);
return NULL;
}
p->driver = &grub_ns8250_driver;
grub_serial_config_defaults (p);
p->port = port;
grub_serial_register (p);
return p->name;
}

362
grub-core/term/serial.c Normal file
View file

@ -0,0 +1,362 @@
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 2000,2001,2002,2003,2004,2005,2007,2008,2009,2010 Free Software Foundation, Inc.
*
* GRUB 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.
*
* GRUB 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 GRUB. If not, see <http://www.gnu.org/licenses/>.
*/
#include <grub/serial.h>
#include <grub/term.h>
#include <grub/types.h>
#include <grub/dl.h>
#include <grub/misc.h>
#include <grub/terminfo.h>
#include <grub/cpu/io.h>
#include <grub/extcmd.h>
#include <grub/i18n.h>
#include <grub/list.h>
#define FOR_SERIAL_PORTS(var) FOR_LIST_ELEMENTS((var), (grub_serial_ports))
/* Argument options. */
static const struct grub_arg_option options[] =
{
{"unit", 'u', 0, N_("Set the serial unit."), 0, ARG_TYPE_INT},
{"port", 'p', 0, N_("Set the serial port address."), 0, ARG_TYPE_STRING},
{"speed", 's', 0, N_("Set the serial port speed."), 0, ARG_TYPE_INT},
{"word", 'w', 0, N_("Set the serial port word length."), 0, ARG_TYPE_INT},
{"parity", 'r', 0, N_("Set the serial port parity."), 0, ARG_TYPE_STRING},
{"stop", 't', 0, N_("Set the serial port stop bits."), 0, ARG_TYPE_INT},
{0, 0, 0, 0, 0, 0}
};
struct grub_serial_port *grub_serial_ports;
struct grub_serial_output_state
{
struct grub_terminfo_output_state tinfo;
struct grub_serial_port *port;
};
struct grub_serial_input_state
{
struct grub_terminfo_input_state tinfo;
struct grub_serial_port *port;
};
static grub_uint16_t
grub_serial_getwh (struct grub_term_output *term __attribute__ ((unused)))
{
const grub_uint8_t TEXT_WIDTH = 80;
const grub_uint8_t TEXT_HEIGHT = 24;
return (TEXT_WIDTH << 8) | TEXT_HEIGHT;
}
static void
serial_put (grub_term_output_t term, const int c)
{
struct grub_serial_output_state *data = term->data;
data->port->driver->put (data->port, c);
}
static int
serial_fetch (grub_term_input_t term)
{
struct grub_serial_input_state *data = term->data;
return data->port->driver->fetch (data->port);
}
struct grub_serial_input_state grub_serial_terminfo_input =
{
.tinfo =
{
.readkey = serial_fetch
}
};
struct grub_serial_output_state grub_serial_terminfo_output =
{
.tinfo =
{
.put = serial_put
}
};
int registered = 0;
static struct grub_term_input grub_serial_term_input =
{
.name = "serial",
.init = grub_terminfo_input_init,
.getkey = grub_terminfo_getkey,
.data = &grub_serial_terminfo_input
};
static struct grub_term_output grub_serial_term_output =
{
.name = "serial",
.putchar = grub_terminfo_putchar,
.getwh = grub_serial_getwh,
.getxy = grub_terminfo_getxy,
.gotoxy = grub_terminfo_gotoxy,
.cls = grub_terminfo_cls,
.setcolorstate = grub_terminfo_setcolorstate,
.setcursor = grub_terminfo_setcursor,
.flags = GRUB_TERM_CODE_TYPE_ASCII,
.data = &grub_serial_terminfo_output,
.normal_color = GRUB_TERM_DEFAULT_NORMAL_COLOR,
.highlight_color = GRUB_TERM_DEFAULT_HIGHLIGHT_COLOR,
};
static struct grub_serial_port *
grub_serial_find (char *name)
{
struct grub_serial_port *port;
FOR_SERIAL_PORTS (port)
if (grub_strcmp (port->name, name) == 0)
break;
#ifndef GRUB_MACHINE_EMU
if (!port && grub_memcmp (name, "port", sizeof ("port") - 1) == 0
&& grub_isdigit (name [sizeof ("port") - 1]))
{
name = grub_serial_ns8250_add_port (grub_strtoul (&name[sizeof ("port") - 1],
0, 16));
if (!name)
return NULL;
FOR_SERIAL_PORTS (port)
if (grub_strcmp (port->name, name) == 0)
break;
}
#endif
return port;
}
static grub_err_t
grub_cmd_serial (grub_extcmd_t cmd, int argc, char **args)
{
struct grub_arg_list *state = cmd->state;
char pname[40];
char *name = NULL;
struct grub_serial_port *port;
struct grub_serial_config config;
grub_err_t err;
if (state[0].set)
{
grub_snprintf (pname, sizeof (pname), "com%ld",
grub_strtoul (state[0].arg, 0, 0));
name = pname;
}
if (state[1].set)
{
grub_snprintf (pname, sizeof (pname), "port%lx",
grub_strtoul (state[1].arg, 0, 0));
name = pname;
}
if (argc >= 1)
name = args[0];
if (!name)
name = "com0";
port = grub_serial_find (name);
if (!port)
return grub_error (GRUB_ERR_BAD_ARGUMENT, "unknown serial port");
config = port->config;
if (state[2].set)
config.speed = grub_strtoul (state[2].arg, 0, 0);
if (state[3].set)
config.word_len = grub_strtoul (state[3].arg, 0, 0);
if (state[4].set)
{
if (! grub_strcmp (state[4].arg, "no"))
config.parity = GRUB_SERIAL_PARITY_NONE;
else if (! grub_strcmp (state[4].arg, "odd"))
config.parity = GRUB_SERIAL_PARITY_ODD;
else if (! grub_strcmp (state[4].arg, "even"))
config.parity = GRUB_SERIAL_PARITY_EVEN;
else
return grub_error (GRUB_ERR_BAD_ARGUMENT, "bad parity");
}
if (state[5].set)
{
if (! grub_strcmp (state[5].arg, "1"))
config.stop_bits = GRUB_SERIAL_STOP_BITS_1;
else if (! grub_strcmp (state[5].arg, "2"))
config.stop_bits = GRUB_SERIAL_STOP_BITS_2;
else
return grub_error (GRUB_ERR_BAD_ARGUMENT, "bad number of stop bits");
}
/* Initialize with new settings. */
err = port->driver->configure (port, &config);
if (err)
return err;
#ifndef GRUB_MACHINE_EMU
/* Compatibility kludge. */
if (port->driver == &grub_ns8250_driver)
{
if (!registered)
{
grub_term_register_input ("serial", &grub_serial_term_input);
grub_term_register_output ("serial", &grub_serial_term_output);
}
grub_serial_terminfo_output.port = port;
grub_serial_terminfo_input.port = port;
registered = 1;
}
#endif
return GRUB_ERR_NONE;
}
grub_err_t
grub_serial_register (struct grub_serial_port *port)
{
struct grub_term_input *in;
struct grub_term_output *out;
struct grub_serial_input_state *indata;
struct grub_serial_output_state *outdata;
in = grub_malloc (sizeof (*in));
if (!in)
return grub_errno;
indata = grub_malloc (sizeof (*indata));
if (!indata)
{
grub_free (in);
return grub_errno;
}
grub_memcpy (in, &grub_serial_term_input, sizeof (*in));
in->data = indata;
in->name = grub_xasprintf ("serial_%s", port->name);
grub_memcpy (indata, &grub_serial_terminfo_input, sizeof (*indata));
if (!in->name)
{
grub_free (in);
grub_free (indata);
return grub_errno;
}
out = grub_malloc (sizeof (*out));
if (!out)
{
grub_free (in);
grub_free (indata);
grub_free ((char *) in->name);
return grub_errno;
}
outdata = grub_malloc (sizeof (*outdata));
if (!outdata)
{
grub_free (in);
grub_free (indata);
grub_free ((char *) in->name);
grub_free (out);
return grub_errno;
}
grub_memcpy (out, &grub_serial_term_output, sizeof (*out));
out->data = outdata;
out->name = in->name;
grub_memcpy (outdata, &grub_serial_terminfo_output, sizeof (*outdata));
grub_list_push (GRUB_AS_LIST_P (&grub_serial_ports), GRUB_AS_LIST (port));
((struct grub_serial_input_state *) in->data)->port = port;
((struct grub_serial_output_state *) out->data)->port = port;
port->term_in = in;
port->term_out = out;
grub_terminfo_output_register (out, "vt100");
#ifdef GRUB_MACHINE_MIPS_YEELOONG
if (grub_strcmp (port->name, "com0") == 0)
{
grub_term_register_input_active ("serial_*", in);
grub_term_register_output_active ("serial_*", out);
}
else
#endif
{
grub_term_register_input ("serial_*", in);
grub_term_register_output ("serial_*", out);
}
return GRUB_ERR_NONE;
}
void
grub_serial_unregister (struct grub_serial_port *port)
{
if (port->driver->fini)
port->driver->fini (port);
if (port->term_in)
grub_term_unregister_input (port->term_in);
if (port->term_out)
grub_term_unregister_output (port->term_out);
grub_list_remove (GRUB_AS_LIST_P (&grub_serial_ports), GRUB_AS_LIST (port));
}
void
grub_serial_unregister_driver (struct grub_serial_driver *driver)
{
struct grub_serial_port *port, *next;
for (port = grub_serial_ports; port; port = next)
{
next = port->next;
if (port->driver == driver)
grub_serial_unregister (port);
}
}
static grub_extcmd_t cmd;
GRUB_MOD_INIT(serial)
{
cmd = grub_register_extcmd ("serial", grub_cmd_serial,
GRUB_COMMAND_FLAG_BOTH,
N_("[OPTIONS...]"),
N_("Configure serial port."), options);
#ifndef GRUB_MACHINE_EMU
grub_ns8250_init ();
#endif
}
GRUB_MOD_FINI(serial)
{
while (grub_serial_ports)
grub_serial_unregister (grub_serial_ports);
if (registered)
{
grub_term_unregister_input (&grub_serial_term_input);
grub_term_unregister_output (&grub_serial_term_output);
}
grub_unregister_extcmd (cmd);
}

621
grub-core/term/terminfo.c Normal file
View file

@ -0,0 +1,621 @@
/* terminfo.c - simple terminfo module */
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 2003,2004,2005,2007 Free Software Foundation, Inc.
*
* GRUB 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.
*
* GRUB 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 GRUB. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* This file contains various functions dealing with different
* terminal capabilities. For example, vt52 and vt100.
*/
#include <grub/types.h>
#include <grub/misc.h>
#include <grub/mm.h>
#include <grub/err.h>
#include <grub/dl.h>
#include <grub/term.h>
#include <grub/terminfo.h>
#include <grub/tparm.h>
#include <grub/command.h>
#include <grub/i18n.h>
#include <grub/time.h>
static struct grub_term_output *terminfo_outputs;
/* Get current terminfo name. */
char *
grub_terminfo_get_current (struct grub_term_output *term)
{
struct grub_terminfo_output_state *data
= (struct grub_terminfo_output_state *) term->data;
return data->name;
}
/* Free *PTR and set *PTR to NULL, to prevent double-free. */
static void
grub_terminfo_free (char **ptr)
{
grub_free (*ptr);
*ptr = 0;
}
static void
grub_terminfo_all_free (struct grub_term_output *term)
{
struct grub_terminfo_output_state *data
= (struct grub_terminfo_output_state *) term->data;
/* Free previously allocated memory. */
grub_terminfo_free (&data->name);
grub_terminfo_free (&data->gotoxy);
grub_terminfo_free (&data->cls);
grub_terminfo_free (&data->reverse_video_on);
grub_terminfo_free (&data->reverse_video_off);
grub_terminfo_free (&data->cursor_on);
grub_terminfo_free (&data->cursor_off);
}
/* Set current terminfo type. */
grub_err_t
grub_terminfo_set_current (struct grub_term_output *term,
const char *str)
{
struct grub_terminfo_output_state *data
= (struct grub_terminfo_output_state *) term->data;
/* TODO
* Lookup user specified terminfo type. If found, set term variables
* as appropriate. Otherwise return an error.
*
* How should this be done?
* a. A static table included in this module.
* - I do not like this idea.
* b. A table stored in the configuration directory.
* - Users must convert their terminfo settings if we have not already.
* c. Look for terminfo files in the configuration directory.
* - /usr/share/terminfo is 6.3M on my system.
* - /usr/share/terminfo is not on most users boot partition.
* + Copying the terminfo files you want to use to the grub
* configuration directory is easier then (b).
* d. Your idea here.
*/
grub_terminfo_all_free (term);
if (grub_strcmp ("vt100", str) == 0)
{
data->name = grub_strdup ("vt100");
data->gotoxy = grub_strdup ("\e[%i%p1%d;%p2%dH");
data->cls = grub_strdup ("\e[H\e[J");
data->reverse_video_on = grub_strdup ("\e[7m");
data->reverse_video_off = grub_strdup ("\e[m");
data->cursor_on = grub_strdup ("\e[?25h");
data->cursor_off = grub_strdup ("\e[?25l");
data->setcolor = NULL;
return grub_errno;
}
if (grub_strcmp ("vt100-color", str) == 0)
{
data->name = grub_strdup ("vt100-color");
data->gotoxy = grub_strdup ("\e[%i%p1%d;%p2%dH");
data->cls = grub_strdup ("\e[H\e[J");
data->reverse_video_on = grub_strdup ("\e[7m");
data->reverse_video_off = grub_strdup ("\e[m");
data->cursor_on = grub_strdup ("\e[?25h");
data->cursor_off = grub_strdup ("\e[?25l");
data->setcolor = grub_strdup ("\e[3%p1%dm\e[4%p2%dm");
return grub_errno;
}
if (grub_strcmp ("ieee1275", str) == 0)
{
data->name = grub_strdup ("ieee1275");
data->gotoxy = grub_strdup ("\e[%i%p1%d;%p2%dH");
/* Clear the screen. Using serial console, screen(1) only recognizes the
* ANSI escape sequence. Using video console, Apple Open Firmware
* (version 3.1.1) only recognizes the literal ^L. So use both. */
data->cls = grub_strdup (" \e[2J");
data->reverse_video_on = grub_strdup ("\e[7m");
data->reverse_video_off = grub_strdup ("\e[m");
data->cursor_on = grub_strdup ("\e[?25h");
data->cursor_off = grub_strdup ("\e[?25l");
data->setcolor = grub_strdup ("\e[3%p1%dm\e[4%p2%dm");
return grub_errno;
}
if (grub_strcmp ("dumb", str) == 0)
{
data->name = grub_strdup ("dumb");
data->gotoxy = NULL;
data->cls = NULL;
data->reverse_video_on = NULL;
data->reverse_video_off = NULL;
data->cursor_on = NULL;
data->cursor_off = NULL;
data->setcolor = NULL;
return grub_errno;
}
return grub_error (GRUB_ERR_BAD_ARGUMENT, "unknown terminfo type");
}
grub_err_t
grub_terminfo_output_register (struct grub_term_output *term,
const char *type)
{
grub_err_t err;
struct grub_terminfo_output_state *data;
err = grub_terminfo_set_current (term, type);
if (err)
return err;
data = (struct grub_terminfo_output_state *) term->data;
data->next = terminfo_outputs;
terminfo_outputs = term;
return GRUB_ERR_NONE;
}
grub_err_t
grub_terminfo_output_unregister (struct grub_term_output *term)
{
struct grub_term_output **ptr;
for (ptr = &terminfo_outputs; *ptr;
ptr = &((struct grub_terminfo_output_state *) (*ptr)->data)->next)
if (*ptr == term)
{
grub_terminfo_all_free (term);
*ptr = ((struct grub_terminfo_output_state *) (*ptr)->data)->next;
return GRUB_ERR_NONE;
}
return grub_error (GRUB_ERR_BAD_ARGUMENT, "terminal not found");
}
/* Wrapper for grub_putchar to write strings. */
static void
putstr (struct grub_term_output *term, const char *str)
{
struct grub_terminfo_output_state *data
= (struct grub_terminfo_output_state *) term->data;
while (*str)
data->put (term, *str++);
}
grub_uint16_t
grub_terminfo_getxy (struct grub_term_output *term)
{
struct grub_terminfo_output_state *data
= (struct grub_terminfo_output_state *) term->data;
return ((data->xpos << 8) | data->ypos);
}
void
grub_terminfo_gotoxy (struct grub_term_output *term,
grub_uint8_t x, grub_uint8_t y)
{
struct grub_terminfo_output_state *data
= (struct grub_terminfo_output_state *) term->data;
if (x > grub_term_width (term) || y > grub_term_height (term))
{
grub_error (GRUB_ERR_OUT_OF_RANGE, "invalid point (%u,%u)", x, y);
return;
}
if (data->gotoxy)
putstr (term, grub_terminfo_tparm (data->gotoxy, y, x));
else
{
if ((y == data->ypos) && (x == data->xpos - 1))
data->put (term, '\b');
}
data->xpos = x;
data->ypos = y;
}
/* Clear the screen. */
void
grub_terminfo_cls (struct grub_term_output *term)
{
struct grub_terminfo_output_state *data
= (struct grub_terminfo_output_state *) term->data;
putstr (term, grub_terminfo_tparm (data->cls));
data->xpos = data->ypos = 0;
}
void
grub_terminfo_setcolorstate (struct grub_term_output *term,
const grub_term_color_state state)
{
struct grub_terminfo_output_state *data
= (struct grub_terminfo_output_state *) term->data;
if (data->setcolor)
{
int fg;
int bg;
/* Map from VGA to terminal colors. */
const int colormap[8]
= { 0, /* Black. */
4, /* Blue. */
2, /* Green. */
6, /* Cyan. */
1, /* Red. */
5, /* Magenta. */
3, /* Yellow. */
7, /* White. */
};
switch (state)
{
case GRUB_TERM_COLOR_STANDARD:
case GRUB_TERM_COLOR_NORMAL:
fg = term->normal_color & 0x0f;
bg = term->normal_color >> 4;
break;
case GRUB_TERM_COLOR_HIGHLIGHT:
fg = term->highlight_color & 0x0f;
bg = term->highlight_color >> 4;
break;
default:
return;
}
putstr (term, grub_terminfo_tparm (data->setcolor, colormap[fg & 7],
colormap[bg & 7]));
return;
}
switch (state)
{
case GRUB_TERM_COLOR_STANDARD:
case GRUB_TERM_COLOR_NORMAL:
putstr (term, grub_terminfo_tparm (data->reverse_video_off));
break;
case GRUB_TERM_COLOR_HIGHLIGHT:
putstr (term, grub_terminfo_tparm (data->reverse_video_on));
break;
default:
break;
}
}
void
grub_terminfo_setcursor (struct grub_term_output *term, const int on)
{
struct grub_terminfo_output_state *data
= (struct grub_terminfo_output_state *) term->data;
if (on)
putstr (term, grub_terminfo_tparm (data->cursor_on));
else
putstr (term, grub_terminfo_tparm (data->cursor_off));
}
/* The terminfo version of putchar. */
void
grub_terminfo_putchar (struct grub_term_output *term,
const struct grub_unicode_glyph *c)
{
struct grub_terminfo_output_state *data
= (struct grub_terminfo_output_state *) term->data;
/* Keep track of the cursor. */
switch (c->base)
{
case '\a':
break;
case '\b':
case 127:
if (data->xpos > 0)
data->xpos--;
break;
case '\n':
if (data->ypos < grub_term_height (term) - 1)
data->ypos++;
break;
case '\r':
data->xpos = 0;
break;
default:
if (data->xpos + c->estimated_width >= grub_term_width (term) + 1)
{
data->xpos = 0;
if (data->ypos < grub_term_height (term) - 1)
data->ypos++;
data->put (term, '\r');
data->put (term, '\n');
}
data->xpos += c->estimated_width;
break;
}
data->put (term, c->base);
}
#define ANSI_C0 0x9b
static void
grub_terminfo_readkey (struct grub_term_input *term, int *keys, int *len,
int (*readkey) (struct grub_term_input *term))
{
int c;
#define CONTINUE_READ \
{ \
grub_uint64_t start; \
/* On 9600 we have to wait up to 12 milliseconds. */ \
start = grub_get_time_ms (); \
do \
c = readkey (term); \
while (c == -1 && grub_get_time_ms () - start < 12); \
if (c == -1) \
return; \
\
keys[*len] = c; \
(*len)++; \
}
c = readkey (term);
if (c < 0)
{
*len = 0;
return;
}
*len = 1;
keys[0] = c;
if (c != ANSI_C0 && c != '\e')
{
/* Backspace: Ctrl-h. */
if (c == 0x7f)
c = '\b';
*len = 1;
keys[0] = c;
return;
}
{
static struct
{
char key;
unsigned ascii;
}
three_code_table[] =
{
{'4', GRUB_TERM_KEY_DC},
{'A', GRUB_TERM_KEY_UP},
{'B', GRUB_TERM_KEY_DOWN},
{'C', GRUB_TERM_KEY_RIGHT},
{'D', GRUB_TERM_KEY_LEFT},
{'F', GRUB_TERM_KEY_END},
{'H', GRUB_TERM_KEY_HOME},
{'K', GRUB_TERM_KEY_END},
{'P', GRUB_TERM_KEY_DC},
{'?', GRUB_TERM_KEY_PPAGE},
{'/', GRUB_TERM_KEY_NPAGE}
};
static struct
{
char key;
unsigned ascii;
}
four_code_table[] =
{
{'1', GRUB_TERM_KEY_HOME},
{'3', GRUB_TERM_KEY_DC},
{'5', GRUB_TERM_KEY_PPAGE},
{'6', GRUB_TERM_KEY_NPAGE}
};
unsigned i;
if (c == '\e')
{
CONTINUE_READ;
if (c != '[')
return;
}
CONTINUE_READ;
for (i = 0; i < ARRAY_SIZE (three_code_table); i++)
if (three_code_table[i].key == c)
{
keys[0] = three_code_table[i].ascii;
*len = 1;
return;
}
for (i = 0; i < ARRAY_SIZE (four_code_table); i++)
if (four_code_table[i].key == c)
{
CONTINUE_READ;
if (c != '~')
return;
keys[0] = three_code_table[i].ascii;
*len = 1;
return;
}
return;
}
#undef CONTINUE_READ
}
/* The terminfo version of getkey. */
int
grub_terminfo_getkey (struct grub_term_input *termi)
{
struct grub_terminfo_input_state *data
= (struct grub_terminfo_input_state *) (termi->data);
if (data->npending)
{
data->npending--;
grub_memmove (data->input_buf, data->input_buf + 1, data->npending);
return data->input_buf[0];
}
grub_terminfo_readkey (termi, data->input_buf,
&data->npending, data->readkey);
if (data->npending)
{
data->npending--;
grub_memmove (data->input_buf, data->input_buf + 1, data->npending);
return data->input_buf[0];
}
return GRUB_TERM_NO_KEY;
}
grub_err_t
grub_terminfo_input_init (struct grub_term_input *termi)
{
struct grub_terminfo_input_state *data
= (struct grub_terminfo_input_state *) (termi->data);
data->npending = 0;
return GRUB_ERR_NONE;
}
/* GRUB Command. */
static grub_err_t
print_terminfo (void)
{
const char *encoding_names[(GRUB_TERM_CODE_TYPE_MASK
>> GRUB_TERM_CODE_TYPE_SHIFT) + 1]
= {
/* VGA and glyph descriptor types are just for completeness,
they are not used on terminfo terminals.
*/
[GRUB_TERM_CODE_TYPE_ASCII >> GRUB_TERM_CODE_TYPE_SHIFT] = _("ASCII"),
[GRUB_TERM_CODE_TYPE_CP437 >> GRUB_TERM_CODE_TYPE_SHIFT] = "CP-437",
[GRUB_TERM_CODE_TYPE_UTF8_LOGICAL >> GRUB_TERM_CODE_TYPE_SHIFT]
= _("UTF-8"),
[GRUB_TERM_CODE_TYPE_UTF8_VISUAL >> GRUB_TERM_CODE_TYPE_SHIFT]
= _("UTF-8 visual"),
[GRUB_TERM_CODE_TYPE_VISUAL_GLYPHS >> GRUB_TERM_CODE_TYPE_SHIFT]
= "Glyph descriptors",
_("Unknown"), _("Unknown"), _("Unknown")
};
struct grub_term_output *cur;
grub_printf ("Current terminfo types: \n");
for (cur = terminfo_outputs; cur;
cur = ((struct grub_terminfo_output_state *) cur->data)->next)
grub_printf ("%s: %s\t%s\n", cur->name,
grub_terminfo_get_current(cur),
encoding_names[(cur->flags & GRUB_TERM_CODE_TYPE_MASK)
>> GRUB_TERM_CODE_TYPE_SHIFT]);
return GRUB_ERR_NONE;
}
static grub_err_t
grub_cmd_terminfo (grub_command_t cmd __attribute__ ((unused)),
int argc, char **args)
{
struct grub_term_output *cur;
int encoding = GRUB_TERM_CODE_TYPE_ASCII;
int i;
char *name = NULL, *type = NULL;
if (argc == 0)
return print_terminfo ();
for (i = 0; i < argc; i++)
{
if (grub_strcmp (args[i], "-a") == 0
|| grub_strcmp (args[i], "--ascii") == 0)
{
encoding = GRUB_TERM_CODE_TYPE_ASCII;
continue;
}
if (grub_strcmp (args[i], "-u") == 0
|| grub_strcmp (args[i], "--utf8") == 0)
{
encoding = GRUB_TERM_CODE_TYPE_UTF8_LOGICAL;
continue;
}
if (grub_strcmp (args[i], "-v") == 0
|| grub_strcmp (args[i], "--visual-utf8") == 0)
{
encoding = GRUB_TERM_CODE_TYPE_UTF8_VISUAL;
continue;
}
if (!name)
{
name = args[i];
continue;
}
if (!type)
{
type = args[i];
continue;
}
return grub_error (GRUB_ERR_BAD_ARGUMENT, "too many parameters");
}
if (name == NULL)
return grub_error (GRUB_ERR_BAD_ARGUMENT, "too few parameters");
for (cur = terminfo_outputs; cur;
cur = ((struct grub_terminfo_output_state *) cur->data)->next)
if (grub_strcmp (name, cur->name) == 0)
{
cur->flags = (cur->flags & ~GRUB_TERM_CODE_TYPE_MASK) | encoding;
if (!type)
return GRUB_ERR_NONE;
return grub_terminfo_set_current (cur, type);
}
return grub_error (GRUB_ERR_BAD_ARGUMENT,
"no terminal %s found or it's not handled by terminfo",
name);
}
static grub_command_t cmd;
GRUB_MOD_INIT(terminfo)
{
cmd = grub_register_command ("terminfo", grub_cmd_terminfo,
N_("[[-a|-u|-v] TERM [TYPE]]"),
N_("Set terminfo type of TERM to TYPE.\n"
"-a, --ascii Terminal is ASCII-only [default].\n"
"-u, --utf8 Terminal is logical-ordered UTF-8.\n"
"-v, --visual-utf8 Terminal is visually-ordered UTF-8.")
);
}
GRUB_MOD_FINI(terminfo)
{
grub_unregister_command (cmd);
}

761
grub-core/term/tparm.c Normal file
View file

@ -0,0 +1,761 @@
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 1998-2003,2004,2005 Free Software Foundation, Inc.
*
* GRUB 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.
*
* GRUB 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 GRUB. If not, see <http://www.gnu.org/licenses/>.
*/
/**********************************************************************
* This code is a modification of lib_tparm.c found in ncurses-5.2. The
* modification are for use in grub by replacing all libc function through
* special grub functions. This also meant to delete all dynamic memory
* allocation and replace it by a number of fixed buffers.
*
* Modifications by Tilmann Bubeck <t.bubeck@reinform.de> 2002
*
* Resync with ncurses-5.4 by Omniflux <omniflux+devel@omniflux.com> 2005
**********************************************************************/
/****************************************************************************
* Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
* and: Eric S. Raymond <esr@snark.thyrsus.com> *
* and: Thomas E. Dickey, 1996 on *
****************************************************************************/
/*
* tparm.c
*
*/
#include <grub/misc.h>
#include <grub/mm.h>
#include <grub/types.h>
#include <grub/tparm.h>
/*
* Common/troublesome character definitions
*/
typedef char grub_bool_t;
#ifndef FALSE
# define FALSE (0)
#endif
#ifndef TRUE
# define TRUE (!FALSE)
#endif
#define NUM_PARM 9
#define NUM_VARS 26
#define STACKSIZE 20
#define MAX_FORMAT_LEN 256
#define max(a,b) ((a) > (b) ? (a) : (b))
#define isdigit(c) ((c) >= '0' && (c) <= '9')
#define isUPPER(c) ((c) >= 'A' && (c) <= 'Z')
#define isLOWER(c) ((c) >= 'a' && (c) <= 'z')
#define UChar(c) ((unsigned char)(c))
//MODULE_ID("$Id$")
/*
* char *
* tparm(string, ...)
*
* Substitute the given parameters into the given string by the following
* rules (taken from terminfo(5)):
*
* Cursor addressing and other strings requiring parame-
* ters in the terminal are described by a parameterized string
* capability, with like escapes %x in it. For example, to
* address the cursor, the cup capability is given, using two
* parameters: the row and column to address to. (Rows and
* columns are numbered from zero and refer to the physical
* screen visible to the user, not to any unseen memory.) If
* the terminal has memory relative cursor addressing, that can
* be indicated by
*
* The parameter mechanism uses a stack and special %
* codes to manipulate it. Typically a sequence will push one
* of the parameters onto the stack and then print it in some
* format. Often more complex operations are necessary.
*
* The % encodings have the following meanings:
*
* %% outputs `%'
* %c print pop() like %c in printf()
* %s print pop() like %s in printf()
* %[[:]flags][width[.precision]][doxXs]
* as in printf, flags are [-+#] and space
* The ':' is used to avoid making %+ or %-
* patterns (see below).
*
* %p[1-9] push ith parm
* %P[a-z] set dynamic variable [a-z] to pop()
* %g[a-z] get dynamic variable [a-z] and push it
* %P[A-Z] set static variable [A-Z] to pop()
* %g[A-Z] get static variable [A-Z] and push it
* %l push strlen(pop)
* %'c' push char constant c
* %{nn} push integer constant nn
*
* %+ %- %* %/ %m
* arithmetic (%m is mod): push(pop() op pop())
* %& %| %^ bit operations: push(pop() op pop())
* %= %> %< logical operations: push(pop() op pop())
* %A %O logical and & or operations for conditionals
* %! %~ unary operations push(op pop())
* %i add 1 to first two parms (for ANSI terminals)
*
* %? expr %t thenpart %e elsepart %;
* if-then-else, %e elsepart is optional.
* else-if's are possible ala Algol 68:
* %? c1 %t b1 %e c2 %t b2 %e c3 %t b3 %e c4 %t b4 %e b5 %;
*
* For those of the above operators which are binary and not commutative,
* the stack works in the usual way, with
* %gx %gy %m
* resulting in x mod y, not the reverse.
*/
typedef struct {
union {
int num;
char *str;
} data;
grub_bool_t num_type;
} stack_frame;
static stack_frame stack[STACKSIZE];
static int stack_ptr;
static const char *tparam_base = "";
static char *out_buff;
static grub_size_t out_size;
static grub_size_t out_used;
static char *fmt_buff;
static grub_size_t fmt_size;
static inline void
get_space(grub_size_t need)
{
need += out_used;
if (need > out_size) {
out_size = need * 2;
out_buff = grub_realloc(out_buff, out_size*sizeof(char));
/* FIX ME! handle out_buff == 0. */
}
}
static inline void
save_text(const char *fmt, const char *s, int len)
{
grub_size_t s_len = grub_strlen(s);
if (len > (int) s_len)
s_len = len;
get_space(s_len + 1);
(void) grub_snprintf(out_buff + out_used, s_len + 1, fmt, s);
out_used += grub_strlen(out_buff + out_used);
}
static inline void
save_number(const char *fmt, int number, int len)
{
if (len < 30)
len = 30; /* actually log10(MAX_INT)+1 */
get_space((unsigned) len + 1);
(void) grub_snprintf(out_buff + out_used, len + 1, fmt, number);
out_used += grub_strlen(out_buff + out_used);
}
static inline void
save_char(int c)
{
if (c == 0)
c = 0200;
get_space(1);
out_buff[out_used++] = c;
}
static inline void
npush(int x)
{
if (stack_ptr < STACKSIZE) {
stack[stack_ptr].num_type = TRUE;
stack[stack_ptr].data.num = x;
stack_ptr++;
}
}
static inline int
npop(void)
{
int result = 0;
if (stack_ptr > 0) {
stack_ptr--;
if (stack[stack_ptr].num_type)
result = stack[stack_ptr].data.num;
}
return result;
}
static inline void
spush(char *x)
{
if (stack_ptr < STACKSIZE) {
stack[stack_ptr].num_type = FALSE;
stack[stack_ptr].data.str = x;
stack_ptr++;
}
}
static inline char *
spop(void)
{
static char dummy[] = ""; /* avoid const-cast */
char *result = dummy;
if (stack_ptr > 0) {
stack_ptr--;
if (!stack[stack_ptr].num_type && stack[stack_ptr].data.str != 0)
result = stack[stack_ptr].data.str;
}
return result;
}
static inline const char *
parse_format(const char *s, char *format, int *len)
{
*len = 0;
if (format != 0) {
grub_bool_t done = FALSE;
grub_bool_t allowminus = FALSE;
grub_bool_t dot = FALSE;
grub_bool_t err = FALSE;
char *fmt = format;
int my_width = 0;
int my_prec = 0;
int value = 0;
*len = 0;
*format++ = '%';
while (*s != '\0' && !done) {
switch (*s) {
case 'c': /* FALLTHRU */
case 'd': /* FALLTHRU */
case 'o': /* FALLTHRU */
case 'x': /* FALLTHRU */
case 'X': /* FALLTHRU */
case 's':
*format++ = *s;
done = TRUE;
break;
case '.':
*format++ = *s++;
if (dot) {
err = TRUE;
} else { /* value before '.' is the width */
dot = TRUE;
my_width = value;
}
value = 0;
break;
case '#':
*format++ = *s++;
break;
case ' ':
*format++ = *s++;
break;
case ':':
s++;
allowminus = TRUE;
break;
case '-':
if (allowminus) {
*format++ = *s++;
} else {
done = TRUE;
}
break;
default:
if (isdigit(UChar(*s))) {
value = (value * 10) + (*s - '0');
if (value > 10000)
err = TRUE;
*format++ = *s++;
} else {
done = TRUE;
}
}
}
/*
* If we found an error, ignore (and remove) the flags.
*/
if (err) {
my_width = my_prec = value = 0;
format = fmt;
*format++ = '%';
*format++ = *s;
}
/*
* Any value after '.' is the precision. If we did not see '.', then
* the value is the width.
*/
if (dot)
my_prec = value;
else
my_width = value;
*format = '\0';
/* return maximum string length in print */
*len = (my_width > my_prec) ? my_width : my_prec;
}
return s;
}
/*
* Analyze the string to see how many parameters we need from the varargs list,
* and what their types are. We will only accept string parameters if they
* appear as a %l or %s format following an explicit parameter reference (e.g.,
* %p2%s). All other parameters are numbers.
*
* 'number' counts coarsely the number of pop's we see in the string, and
* 'popcount' shows the highest parameter number in the string. We would like
* to simply use the latter count, but if we are reading termcap strings, there
* may be cases that we cannot see the explicit parameter numbers.
*/
static inline int
analyze(const char *string, char *p_is_s[NUM_PARM], int *popcount)
{
grub_size_t len2;
int i;
int lastpop = -1;
int len;
int number = 0;
const char *cp = string;
static char dummy[] = "";
*popcount = 0;
if (cp == 0)
return 0;
if ((len2 = grub_strlen(cp)) > fmt_size) {
fmt_size = len2 + fmt_size + 2;
if ((fmt_buff = grub_realloc(fmt_buff, fmt_size*sizeof(char))) == 0)
return 0;
}
grub_memset(p_is_s, 0, sizeof(p_is_s[0]) * NUM_PARM);
while ((cp - string) < (int) len2) {
if (*cp == '%') {
cp++;
cp = parse_format(cp, fmt_buff, &len);
switch (*cp) {
default:
break;
case 'd': /* FALLTHRU */
case 'o': /* FALLTHRU */
case 'x': /* FALLTHRU */
case 'X': /* FALLTHRU */
case 'c': /* FALLTHRU */
if (lastpop <= 0)
number++;
lastpop = -1;
break;
case 'l':
case 's':
if (lastpop > 0)
p_is_s[lastpop - 1] = dummy;
++number;
break;
case 'p':
cp++;
i = (UChar(*cp) - '0');
if (i >= 0 && i <= NUM_PARM) {
lastpop = i;
if (lastpop > *popcount)
*popcount = lastpop;
}
break;
case 'P':
++number;
++cp;
break;
case 'g':
cp++;
break;
case '\'':
cp += 2;
lastpop = -1;
break;
case '{':
cp++;
while (isdigit(UChar(*cp))) {
cp++;
}
break;
case '+':
case '-':
case '*':
case '/':
case 'm':
case 'A':
case 'O':
case '&':
case '|':
case '^':
case '=':
case '<':
case '>':
lastpop = -1;
number += 2;
break;
case '!':
case '~':
lastpop = -1;
++number;
break;
case 'i':
/* will add 1 to first (usually two) parameters */
break;
}
}
if (*cp != '\0')
cp++;
}
if (number > NUM_PARM)
number = NUM_PARM;
return number;
}
static inline char *
tparam_internal(const char *string, va_list ap)
{
char *p_is_s[NUM_PARM];
long param[NUM_PARM];
int popcount;
int number;
int len;
int level;
int x, y;
int i;
const char *cp = string;
grub_size_t len2;
static int dynamic_var[NUM_VARS];
static int static_vars[NUM_VARS];
if (cp == 0)
return 0;
out_used = out_size = fmt_size = 0;
len2 = (int) grub_strlen(cp);
/*
* Find the highest parameter-number referred to in the format string.
* Use this value to limit the number of arguments copied from the
* variable-length argument list.
*/
number = analyze(cp, p_is_s, &popcount);
if (fmt_buff == 0)
return 0;
for (i = 0; i < max(popcount, number); i++) {
/*
* A few caps (such as plab_norm) have string-valued parms.
* We'll have to assume that the caller knows the difference, since
* a char* and an int may not be the same size on the stack.
*/
if (p_is_s[i] != 0) {
p_is_s[i] = va_arg(ap, char *);
} else {
param[i] = va_arg(ap, long int);
}
}
/*
* This is a termcap compatibility hack. If there are no explicit pop
* operations in the string, load the stack in such a way that
* successive pops will grab successive parameters. That will make
* the expansion of (for example) \E[%d;%dH work correctly in termcap
* style, which means tparam() will expand termcap strings OK.
*/
stack_ptr = 0;
if (popcount == 0) {
popcount = number;
for (i = number - 1; i >= 0; i--)
npush(param[i]);
}
while ((cp - string) < (int) len2) {
if (*cp != '%') {
save_char(UChar(*cp));
} else {
tparam_base = cp++;
cp = parse_format(cp, fmt_buff, &len);
switch (*cp) {
default:
break;
case '%':
save_char('%');
break;
case 'd': /* FALLTHRU */
case 'o': /* FALLTHRU */
case 'x': /* FALLTHRU */
case 'X': /* FALLTHRU */
save_number(fmt_buff, npop(), len);
break;
case 'c': /* FALLTHRU */
save_char(npop());
break;
case 'l':
save_number("%d", (int) grub_strlen(spop()), 0);
break;
case 's':
save_text(fmt_buff, spop(), len);
break;
case 'p':
cp++;
i = (UChar(*cp) - '1');
if (i >= 0 && i < NUM_PARM) {
if (p_is_s[i])
spush(p_is_s[i]);
else
npush(param[i]);
}
break;
case 'P':
cp++;
if (isUPPER(*cp)) {
i = (UChar(*cp) - 'A');
static_vars[i] = npop();
} else if (isLOWER(*cp)) {
i = (UChar(*cp) - 'a');
dynamic_var[i] = npop();
}
break;
case 'g':
cp++;
if (isUPPER(*cp)) {
i = (UChar(*cp) - 'A');
npush(static_vars[i]);
} else if (isLOWER(*cp)) {
i = (UChar(*cp) - 'a');
npush(dynamic_var[i]);
}
break;
case '\'':
cp++;
npush(UChar(*cp));
cp++;
break;
case '{':
number = 0;
cp++;
while (isdigit(UChar(*cp))) {
number = (number * 10) + (UChar(*cp) - '0');
cp++;
}
npush(number);
break;
case '+':
npush(npop() + npop());
break;
case '-':
y = npop();
x = npop();
npush(x - y);
break;
case '*':
npush(npop() * npop());
break;
case '/':
y = npop();
x = npop();
npush(y ? (x / y) : 0);
break;
case 'm':
y = npop();
x = npop();
npush(y ? (x % y) : 0);
break;
case 'A':
npush(npop() && npop());
break;
case 'O':
npush(npop() || npop());
break;
case '&':
npush(npop() & npop());
break;
case '|':
npush(npop() | npop());
break;
case '^':
npush(npop() ^ npop());
break;
case '=':
y = npop();
x = npop();
npush(x == y);
break;
case '<':
y = npop();
x = npop();
npush(x < y);
break;
case '>':
y = npop();
x = npop();
npush(x > y);
break;
case '!':
npush(!npop());
break;
case '~':
npush(~npop());
break;
case 'i':
if (p_is_s[0] == 0)
param[0]++;
if (p_is_s[1] == 0)
param[1]++;
break;
case '?':
break;
case 't':
x = npop();
if (!x) {
/* scan forward for %e or %; at level zero */
cp++;
level = 0;
while (*cp) {
if (*cp == '%') {
cp++;
if (*cp == '?')
level++;
else if (*cp == ';') {
if (level > 0)
level--;
else
break;
} else if (*cp == 'e' && level == 0)
break;
}
if (*cp)
cp++;
}
}
break;
case 'e':
/* scan forward for a %; at level zero */
cp++;
level = 0;
while (*cp) {
if (*cp == '%') {
cp++;
if (*cp == '?')
level++;
else if (*cp == ';') {
if (level > 0)
level--;
else
break;
}
}
if (*cp)
cp++;
}
break;
case ';':
break;
} /* endswitch (*cp) */
} /* endelse (*cp == '%') */
if (*cp == '\0')
break;
cp++;
} /* endwhile (*cp) */
get_space(1);
out_buff[out_used] = '\0';
return (out_buff);
}
char *
grub_terminfo_tparm (const char *string, ...)
{
va_list ap;
char *result;
if (!string)
return "";
va_start (ap, string);
result = tparam_internal (string, ap);
va_end (ap);
return result;
}

View file

@ -0,0 +1,395 @@
/* Support for the HID Boot Protocol. */
/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 2008, 2009 Free Software Foundation, Inc.
*
* GRUB 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.
*
* GRUB 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 GRUB. If not, see <http://www.gnu.org/licenses/>.
*/
#include <grub/term.h>
#include <grub/time.h>
#include <grub/cpu/io.h>
#include <grub/misc.h>
#include <grub/term.h>
#include <grub/usb.h>
#include <grub/dl.h>
#include <grub/time.h>
#include <grub/keyboard_layouts.h>
enum
{
KEY_NO_KEY = 0x00,
KEY_ERR_BUFFER = 0x01,
KEY_ERR_POST = 0x02,
KEY_ERR_UNDEF = 0x03,
KEY_CAPS_LOCK = 0x39,
KEY_NUM_LOCK = 0x53,
};
enum
{
LED_NUM_LOCK = 0x01,
LED_CAPS_LOCK = 0x02
};
/* Valid values for bRequest. See HID definition version 1.11 section 7.2. */
#define USB_HID_GET_REPORT 0x01
#define USB_HID_GET_IDLE 0x02
#define USB_HID_GET_PROTOCOL 0x03
#define USB_HID_SET_REPORT 0x09
#define USB_HID_SET_IDLE 0x0A
#define USB_HID_SET_PROTOCOL 0x0B
#define USB_HID_BOOT_SUBCLASS 0x01
#define USB_HID_KBD_PROTOCOL 0x01
#define GRUB_USB_KEYBOARD_LEFT_CTRL 0x01
#define GRUB_USB_KEYBOARD_LEFT_SHIFT 0x02
#define GRUB_USB_KEYBOARD_LEFT_ALT 0x04
#define GRUB_USB_KEYBOARD_RIGHT_CTRL 0x10
#define GRUB_USB_KEYBOARD_RIGHT_SHIFT 0x20
#define GRUB_USB_KEYBOARD_RIGHT_ALT 0x40
struct grub_usb_keyboard_data
{
grub_usb_device_t usbdev;
grub_uint8_t status;
grub_uint16_t mods;
int interfno;
struct grub_usb_desc_endp *endp;
grub_usb_transfer_t transfer;
grub_uint8_t report[8];
int dead;
int last_key;
grub_uint64_t repeat_time;
};
static int grub_usb_keyboard_getkey (struct grub_term_input *term);
static int grub_usb_keyboard_getkeystatus (struct grub_term_input *term);
static struct grub_term_input grub_usb_keyboard_term =
{
.getkey = grub_usb_keyboard_getkey,
.getkeystatus = grub_usb_keyboard_getkeystatus,
.next = 0
};
static struct grub_term_input grub_usb_keyboards[16];
static int
interpret_status (grub_uint8_t data0)
{
int mods = 0;
/* Check Shift, Control, and Alt status. */
if (data0 & GRUB_USB_KEYBOARD_LEFT_SHIFT)
mods |= GRUB_TERM_STATUS_LSHIFT;
if (data0 & GRUB_USB_KEYBOARD_RIGHT_SHIFT)
mods |= GRUB_TERM_STATUS_RSHIFT;
if (data0 & GRUB_USB_KEYBOARD_LEFT_CTRL)
mods |= GRUB_TERM_STATUS_LCTRL;
if (data0 & GRUB_USB_KEYBOARD_RIGHT_CTRL)
mods |= GRUB_TERM_STATUS_RCTRL;
if (data0 & GRUB_USB_KEYBOARD_LEFT_ALT)
mods |= GRUB_TERM_STATUS_LALT;
if (data0 & GRUB_USB_KEYBOARD_RIGHT_ALT)
mods |= GRUB_TERM_STATUS_RALT;
return mods;
}
static void
grub_usb_keyboard_detach (grub_usb_device_t usbdev,
int config __attribute__ ((unused)),
int interface __attribute__ ((unused)))
{
unsigned i;
for (i = 0; i < ARRAY_SIZE (grub_usb_keyboards); i++)
{
struct grub_usb_keyboard_data *data = grub_usb_keyboards[i].data;
if (!data)
continue;
if (data->usbdev != usbdev)
continue;
if (data->transfer)
grub_usb_cancel_transfer (data->transfer);
grub_term_unregister_input (&grub_usb_keyboards[i]);
grub_free ((char *) grub_usb_keyboards[i].name);
grub_usb_keyboards[i].name = NULL;
grub_free (grub_usb_keyboards[i].data);
grub_usb_keyboards[i].data = 0;
}
}
static int
grub_usb_keyboard_attach (grub_usb_device_t usbdev, int configno, int interfno)
{
unsigned curnum;
struct grub_usb_keyboard_data *data;
struct grub_usb_desc_endp *endp = NULL;
int j;
grub_dprintf ("usb_keyboard", "%x %x %x %d %d\n",
usbdev->descdev.class, usbdev->descdev.subclass,
usbdev->descdev.protocol, configno, interfno);
for (curnum = 0; curnum < ARRAY_SIZE (grub_usb_keyboards); curnum++)
if (!grub_usb_keyboards[curnum].data)
break;
if (curnum == ARRAY_SIZE (grub_usb_keyboards))
return 0;
if (usbdev->descdev.class != 0
|| usbdev->descdev.subclass != 0 || usbdev->descdev.protocol != 0)
return 0;
if (usbdev->config[configno].interf[interfno].descif->subclass
!= USB_HID_BOOT_SUBCLASS
|| usbdev->config[configno].interf[interfno].descif->protocol
!= USB_HID_KBD_PROTOCOL)
return 0;
for (j = 0; j < usbdev->config[configno].interf[interfno].descif->endpointcnt;
j++)
{
endp = &usbdev->config[configno].interf[interfno].descendp[j];
if ((endp->endp_addr & 128) && grub_usb_get_ep_type(endp)
== GRUB_USB_EP_INTERRUPT)
break;
}
if (j == usbdev->config[configno].interf[interfno].descif->endpointcnt)
return 0;
grub_dprintf ("usb_keyboard", "HID found!\n");
data = grub_malloc (sizeof (*data));
if (!data)
{
grub_print_error ();
return 0;
}
data->usbdev = usbdev;
data->interfno = interfno;
data->endp = endp;
/* Place the device in boot mode. */
grub_usb_control_msg (usbdev, GRUB_USB_REQTYPE_CLASS_INTERFACE_OUT,
USB_HID_SET_PROTOCOL, 0, interfno, 0, 0);
/* Reports every time an event occurs and not more often than that. */
grub_usb_control_msg (usbdev, GRUB_USB_REQTYPE_CLASS_INTERFACE_OUT,
USB_HID_SET_IDLE, 0<<8, interfno, 0, 0);
grub_memcpy (&grub_usb_keyboards[curnum], &grub_usb_keyboard_term,
sizeof (grub_usb_keyboards[curnum]));
grub_usb_keyboards[curnum].data = data;
usbdev->config[configno].interf[interfno].detach_hook
= grub_usb_keyboard_detach;
grub_usb_keyboards[curnum].name = grub_xasprintf ("usb_keyboard%d", curnum);
if (!grub_usb_keyboards[curnum].name)
{
grub_print_error ();
return 0;
}
/* Test showed that getting report may make the keyboard go nuts.
Moreover since we're reattaching keyboard it usually sends
an initial message on interrupt pipe and so we retrieve
the same keystatus.
*/
#if 0
{
grub_uint8_t report[8];
grub_usb_err_t err;
grub_memset (report, 0, sizeof (report));
err = grub_usb_control_msg (usbdev, GRUB_USB_REQTYPE_CLASS_INTERFACE_IN,
USB_HID_GET_REPORT, 0x0100, interfno,
sizeof (report), (char *) report);
if (err)
data->status = 0;
else
data->status = report[0];
}
#else
data->status = 0;
#endif
data->transfer = grub_usb_bulk_read_background (usbdev,
data->endp->endp_addr,
sizeof (data->report),
(char *) data->report);
if (!data->transfer)
{
grub_print_error ();
return 0;
}
data->last_key = -1;
data->mods = 0;
data->dead = 0;
grub_term_register_input_active ("usb_keyboard", &grub_usb_keyboards[curnum]);
return 1;
}
static void
send_leds (struct grub_usb_keyboard_data *termdata)
{
char report[1];
report[0] = 0;
if (termdata->mods & GRUB_TERM_STATUS_CAPS)
report[0] |= LED_CAPS_LOCK;
if (termdata->mods & GRUB_TERM_STATUS_NUM)
report[0] |= LED_NUM_LOCK;
grub_usb_control_msg (termdata->usbdev, GRUB_USB_REQTYPE_CLASS_INTERFACE_OUT,
USB_HID_SET_REPORT, 0x0200, termdata->interfno,
sizeof (report), (char *) report);
grub_errno = GRUB_ERR_NONE;
}
static int
grub_usb_keyboard_getkey (struct grub_term_input *term)
{
grub_usb_err_t err;
struct grub_usb_keyboard_data *termdata = term->data;
grub_uint8_t data[sizeof (termdata->report)];
grub_size_t actual;
if (termdata->dead)
return GRUB_TERM_NO_KEY;
/* Poll interrupt pipe. */
err = grub_usb_check_transfer (termdata->transfer, &actual);
if (err == GRUB_USB_ERR_WAIT)
{
if (termdata->last_key != -1
&& grub_get_time_ms () > termdata->repeat_time)
{
termdata->repeat_time = grub_get_time_ms ()
+ GRUB_TERM_REPEAT_INTERVAL;
return termdata->last_key;
}
return GRUB_TERM_NO_KEY;
}
grub_memcpy (data, termdata->report, sizeof (data));
termdata->transfer = grub_usb_bulk_read_background (termdata->usbdev,
termdata->endp->endp_addr,
sizeof (termdata->report),
(char *) termdata->report);
if (!termdata->transfer)
{
grub_printf ("%s failed. Stopped\n", term->name);
termdata->dead = 1;
}
termdata->last_key = -1;
grub_dprintf ("usb_keyboard",
"err = %d, actual = %d report: 0x%02x 0x%02x 0x%02x 0x%02x"
" 0x%02x 0x%02x 0x%02x 0x%02x\n",
err, actual,
data[0], data[1], data[2], data[3],
data[4], data[5], data[6], data[7]);
if (err || actual < 1)
return GRUB_TERM_NO_KEY;
termdata->status = data[0];
if (actual < 3)
return GRUB_TERM_NO_KEY;
if (data[2] == KEY_NO_KEY || data[2] == KEY_ERR_BUFFER
|| data[2] == KEY_ERR_POST || data[2] == KEY_ERR_UNDEF)
return GRUB_TERM_NO_KEY;
if (data[2] == KEY_CAPS_LOCK)
{
termdata->mods ^= GRUB_TERM_STATUS_CAPS;
send_leds (termdata);
return GRUB_TERM_NO_KEY;
}
if (data[2] == KEY_NUM_LOCK)
{
termdata->mods ^= GRUB_TERM_STATUS_NUM;
send_leds (termdata);
return GRUB_TERM_NO_KEY;
}
termdata->last_key = grub_term_map_key (data[2], interpret_status (data[0])
| termdata->mods);
termdata->repeat_time = grub_get_time_ms () + GRUB_TERM_REPEAT_PRE_INTERVAL;
grub_errno = GRUB_ERR_NONE;
return termdata->last_key;
}
static int
grub_usb_keyboard_getkeystatus (struct grub_term_input *term)
{
struct grub_usb_keyboard_data *termdata = term->data;
return interpret_status (termdata->status) | termdata->mods;
}
struct grub_usb_attach_desc attach_hook =
{
.class = GRUB_USB_CLASS_HID,
.hook = grub_usb_keyboard_attach
};
GRUB_MOD_INIT(usb_keyboard)
{
grub_usb_register_attach_hook_class (&attach_hook);
}
GRUB_MOD_FINI(usb_keyboard)
{
unsigned i;
for (i = 0; i < ARRAY_SIZE (grub_usb_keyboards); i++)
if (grub_usb_keyboards[i].data)
{
struct grub_usb_keyboard_data *data = grub_usb_keyboards[i].data;
if (!data)
continue;
if (data->transfer)
grub_usb_cancel_transfer (data->transfer);
grub_term_unregister_input (&grub_usb_keyboards[i]);
grub_free ((char *) grub_usb_keyboards[i].name);
grub_usb_keyboards[i].name = NULL;
grub_free (grub_usb_keyboards[i].data);
grub_usb_keyboards[i].data = 0;
}
grub_usb_unregister_attach_hook_class (&attach_hook);
}