summaryrefslogtreecommitdiffstats
path: root/bsps/i386/pc386/console/console_select.c
blob: 1c064fb271c64e2e6f167c234190e9e1cfb847b4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/**
 * @file
 *
 * @ingroup Console
 *
 * @brief pc386 console select
 *
 * This file contains a routine to select the console based upon a number
 * of criteria.
 */

/*
 *  COPYRIGHT (c) 2011-2012, 2016.
 *  On-Line Applications Research Corporation (OAR).
 *
 *  The license and distribution terms for this file may be
 *  found in the file LICENSE in this distribution or at
 *  http://www.rtems.org/license/LICENSE.
 */

#include <limits.h>
#include <stdlib.h>
#include <termios.h>

#include <bsp.h>
#include <libchip/serial.h>
#include <rtems/libio.h>
#include <rtems/console.h>
#include <rtems/termiostypes.h>
#include <bsp/bspimpl.h>

#include "../../shared/dev/serial/legacy-console.h"
#ifdef RTEMS_RUNTIME_CONSOLE_SELECT
  #include <crt.h>
#endif

/*
 * Method to return true if the device associated with the
 * minor number probs available.
 */
static bool bsp_Is_Available( rtems_device_minor_number minor )
{
  console_tbl  *cptr = Console_Port_Tbl[minor];

  /*
   * First perform the configuration dependent probe, then the
   * device dependent probe
   */
  if ((!cptr->deviceProbe || cptr->deviceProbe(minor)) &&
       cptr->pDeviceFns->deviceProbe(minor)) {
    return true;
  }
  return false;
}

/*
 * Method to return the first available device.
 */
static rtems_device_minor_number bsp_First_Available_Device( void )
{
  rtems_device_minor_number minor;

  for (minor=0; minor < Console_Port_Count ; minor++) {
    console_tbl  *cptr = Console_Port_Tbl[minor];

    /*
     * First perform the configuration dependent probe, then the
     * device dependent probe
     */

    if ((!cptr->deviceProbe || cptr->deviceProbe(minor)) &&
         cptr->pDeviceFns->deviceProbe(minor)) {
      return minor;
    }
  }

  /*
   *  Error No devices were found.  We will want to bail here.
   */
  rtems_fatal_error_occurred(RTEMS_IO_ERROR);
}

static bool parse_printk_or_console(
  const char                *param,
  rtems_device_minor_number *minor_out
)
{
  static const char         *opt;
  const char                *option;
  const char                *comma;
  size_t                     length;
  size_t                     index;
  rtems_device_minor_number  minor;
  console_tbl               *conscfg;

  /*
   * Check the command line for the type of mode the console is.
   */
  opt = bsp_cmdline_arg(param);
  if ( !opt ) {
    return false;
  }

  /*
   * Fine the length, there can be more command line visible.
   */
  length = 0;
  while ((opt[length] != ' ') && (opt[length] != '\0')) {
    ++length;
    if (length > NAME_MAX) {
      printk("invalid option (%s): too long\n", param);
      return false;
    }
  }

  /*
   * Only match up to a comma or NULL
   */
  index = 0;
  while ((opt[index] != '=') && (index < length)) {
    ++index;
  }

  if (opt[index] != '=') {
    printk("invalid option (%s): no equals\n", param);
    return false;
  }

  ++index;
  option = &opt[index];

  while ((opt[index] != ',') && (index < length)) {
    ++index;
  }

  if (opt[index] == ',')
    comma = &opt[index];
  else
    comma = NULL;

  length = &opt[index] - option;

  conscfg = console_find_console_entry( option, length, &minor );
  if ( conscfg == NULL ) {
    return false;
  }

  *minor_out = minor;
  if (comma) {
    option = comma + 1;
    if (strncmp (option, "115200", sizeof ("115200") - 1) == 0)
      conscfg->pDeviceParams = (void *)115200;
    else if (strncmp (option, "57600", sizeof ("57600") - 1) == 0)
      conscfg->pDeviceParams = (void *)57600;
    else if (strncmp (option, "38400", sizeof ("38400") - 1) == 0)
      conscfg->pDeviceParams = (void *)38400;
    else if (strncmp (option, "19200", sizeof ("19200") - 1) == 0)
      conscfg->pDeviceParams = (void *)19200;
    else if (strncmp (option, "9600", sizeof ("9600") - 1) == 0)
      conscfg->pDeviceParams = (void *)9600;
    else if (strncmp (option, "4800", sizeof ("4800") - 1) == 0)
      conscfg->pDeviceParams = (void *)4800;
  }

  return true;
}

/*
 * Helper to retrieve device name
 */
static inline const char *get_name(
  rtems_device_minor_number  minor
)
{
  return Console_Port_Tbl[minor]->sDeviceName;
}

/*
 * Parse the arguments early so the printk and console ports are
 * set appropriately.
 */
void pc386_parse_console_arguments(void)
{
  rtems_device_minor_number minor;
  rtems_device_minor_number minor_console = 0;
  rtems_device_minor_number minor_printk = 0;

  /*
   * Assume that if only --console is specified, that printk() should
   * follow that selection by default.
   */
  if ( parse_printk_or_console( "--console=", &minor ) ) {
    minor_console = minor;
    minor_printk = minor;
  }

  /*
   * But if explicitly specified, attempt to honor it.
   */
  if ( parse_printk_or_console( "--printk=",  &minor ) ) {
    minor_printk = minor;
  }

  printk( "Console: %s printk: %s\n",
          get_name(minor_console),get_name(minor_printk) );

  /*
   * Any output after this can cause problems until termios is initialised.
   */
  Console_Port_Minor = minor_console;
  BSPPrintkPort = minor_printk;
}

/*
 *  This handles the selection of the console after the devices are
 *  initialized.
 */
void bsp_console_select(void)
{
  #ifdef RTEMS_RUNTIME_CONSOLE_SELECT
    /*
     * WARNING: This code is really needed any more and should be removed.
     *          references to COM1 and COM2 like they are wrong.
     */
    if ( BSP_runtime_console_select )
      BSP_runtime_console_select(&BSPPrintkPort, &Console_Port_Minor);

    /*
     * If no video card, fall back to serial port console
     */
    if((Console_Port_Minor == BSP_CONSOLE_VGA)
     && (*(unsigned char*) NB_MAX_ROW_ADDR == 0)
     && (*(unsigned short*)NB_MAX_COL_ADDR == 0)) {
      Console_Port_Minor = BSP_CONSOLE_COM2;
      BSPPrintkPort      = BSP_CONSOLE_COM1;
    }
  #endif

  /*
   * If the device that was selected isn't available then
   * let the user know and select the first available device.
   */
  if ( !bsp_Is_Available( Console_Port_Minor ) ) {
    printk(
      "Error finding %s setting console to first available\n",
      get_name(Console_Port_Minor)
    );
    Console_Port_Minor = bsp_First_Available_Device();
  }
}