source: rtems/bsps/i386/pc386/console/console_select.c

Last change on this file was a52aa5b4, checked in by Joel Sherrill <joel@…>, on 07/08/22 at 13:45:31

bsps/i386/pc386: Change license to BSD-2

Updates #3053.

  • Property mode set to 100644
File size: 7.2 KB
Line 
1/* SPDX-License-Identifier: BSD-2-Clause */
2
3/**
4 * @file
5 *
6 * @ingroup Console
7 *
8 * @brief pc386 console select
9 *
10 * This file contains a routine to select the console based upon a number
11 * of criteria.
12 */
13
14/*
15 *  COPYRIGHT (c) 2011-2012, 2016.
16 *  On-Line Applications Research Corporation (OAR).
17 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
20 * are met:
21 * 1. Redistributions of source code must retain the above copyright
22 *    notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 *    notice, this list of conditions and the following disclaimer in the
25 *    documentation and/or other materials provided with the distribution.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
28 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
31 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40#include <limits.h>
41#include <stdlib.h>
42#include <termios.h>
43
44#include <bsp.h>
45#include <libchip/serial.h>
46#include <rtems/libio.h>
47#include <rtems/console.h>
48#include <rtems/termiostypes.h>
49#include <bsp/bspimpl.h>
50
51#include "../../shared/dev/serial/legacy-console.h"
52#ifdef RTEMS_RUNTIME_CONSOLE_SELECT
53  #include <crt.h>
54#endif
55
56/*
57 * Method to return true if the device associated with the
58 * minor number probs available.
59 */
60static bool bsp_Is_Available( rtems_device_minor_number minor )
61{
62  console_tbl  *cptr = Console_Port_Tbl[minor];
63
64  /*
65   * First perform the configuration dependent probe, then the
66   * device dependent probe
67   */
68  if ((!cptr->deviceProbe || cptr->deviceProbe(minor)) &&
69       cptr->pDeviceFns->deviceProbe(minor)) {
70    return true;
71  }
72  return false;
73}
74
75/*
76 * Method to return the first available device.
77 */
78static rtems_device_minor_number bsp_First_Available_Device( void )
79{
80  rtems_device_minor_number minor;
81
82  for (minor=0; minor < Console_Port_Count ; minor++) {
83    console_tbl  *cptr = Console_Port_Tbl[minor];
84
85    /*
86     * First perform the configuration dependent probe, then the
87     * device dependent probe
88     */
89
90    if ((!cptr->deviceProbe || cptr->deviceProbe(minor)) &&
91         cptr->pDeviceFns->deviceProbe(minor)) {
92      return minor;
93    }
94  }
95
96  /*
97   *  Error No devices were found.  We will want to bail here.
98   */
99  rtems_fatal_error_occurred(RTEMS_IO_ERROR);
100}
101
102static bool parse_printk_or_console(
103  const char                *param,
104  rtems_device_minor_number *minor_out
105)
106{
107  static const char         *opt;
108  const char                *option;
109  const char                *comma;
110  size_t                     length;
111  size_t                     index;
112  rtems_device_minor_number  minor;
113  console_tbl               *conscfg;
114
115  /*
116   * Check the command line for the type of mode the console is.
117   */
118  opt = bsp_cmdline_arg(param);
119  if ( !opt ) {
120    return false;
121  }
122
123  /*
124   * Fine the length, there can be more command line visible.
125   */
126  length = 0;
127  while ((opt[length] != ' ') && (opt[length] != '\0')) {
128    ++length;
129    if (length > NAME_MAX) {
130      printk("invalid option (%s): too long\n", param);
131      return false;
132    }
133  }
134
135  /*
136   * Only match up to a comma or NULL
137   */
138  index = 0;
139  while ((opt[index] != '=') && (index < length)) {
140    ++index;
141  }
142
143  if (opt[index] != '=') {
144    printk("invalid option (%s): no equals\n", param);
145    return false;
146  }
147
148  ++index;
149  option = &opt[index];
150
151  while ((opt[index] != ',') && (index < length)) {
152    ++index;
153  }
154
155  if (opt[index] == ',')
156    comma = &opt[index];
157  else
158    comma = NULL;
159
160  length = &opt[index] - option;
161
162  conscfg = console_find_console_entry( option, length, &minor );
163  if ( conscfg == NULL ) {
164    return false;
165  }
166
167  *minor_out = minor;
168  if (comma) {
169    option = comma + 1;
170    if (strncmp (option, "115200", sizeof ("115200") - 1) == 0)
171      conscfg->pDeviceParams = (void *)115200;
172    else if (strncmp (option, "57600", sizeof ("57600") - 1) == 0)
173      conscfg->pDeviceParams = (void *)57600;
174    else if (strncmp (option, "38400", sizeof ("38400") - 1) == 0)
175      conscfg->pDeviceParams = (void *)38400;
176    else if (strncmp (option, "19200", sizeof ("19200") - 1) == 0)
177      conscfg->pDeviceParams = (void *)19200;
178    else if (strncmp (option, "9600", sizeof ("9600") - 1) == 0)
179      conscfg->pDeviceParams = (void *)9600;
180    else if (strncmp (option, "4800", sizeof ("4800") - 1) == 0)
181      conscfg->pDeviceParams = (void *)4800;
182  }
183
184  return true;
185}
186
187/*
188 * Helper to retrieve device name
189 */
190static inline const char *get_name(
191  rtems_device_minor_number  minor
192)
193{
194  return Console_Port_Tbl[minor]->sDeviceName;
195}
196
197/*
198 * Parse the arguments early so the printk and console ports are
199 * set appropriately.
200 */
201void pc386_parse_console_arguments(void)
202{
203  rtems_device_minor_number minor;
204  rtems_device_minor_number minor_console = 0;
205  rtems_device_minor_number minor_printk = 0;
206
207  /*
208   * Assume that if only --console is specified, that printk() should
209   * follow that selection by default.
210   */
211  if ( parse_printk_or_console( "--console=", &minor ) ) {
212    minor_console = minor;
213    minor_printk = minor;
214  }
215
216  /*
217   * But if explicitly specified, attempt to honor it.
218   */
219  if ( parse_printk_or_console( "--printk=",  &minor ) ) {
220    minor_printk = minor;
221  }
222
223  printk( "Console: %s printk: %s\n",
224          get_name(minor_console),get_name(minor_printk) );
225
226  /*
227   * Any output after this can cause problems until termios is initialised.
228   */
229  Console_Port_Minor = minor_console;
230  BSPPrintkPort = minor_printk;
231}
232
233/*
234 *  This handles the selection of the console after the devices are
235 *  initialized.
236 */
237void bsp_console_select(void)
238{
239  #ifdef RTEMS_RUNTIME_CONSOLE_SELECT
240    /*
241     * WARNING: This code is really needed any more and should be removed.
242     *          references to COM1 and COM2 like they are wrong.
243     */
244    if ( BSP_runtime_console_select )
245      BSP_runtime_console_select(&BSPPrintkPort, &Console_Port_Minor);
246
247    /*
248     * If no video card, fall back to serial port console
249     */
250    if((Console_Port_Minor == BSP_CONSOLE_VGA)
251     && (*(unsigned char*) NB_MAX_ROW_ADDR == 0)
252     && (*(unsigned short*)NB_MAX_COL_ADDR == 0)) {
253      Console_Port_Minor = BSP_CONSOLE_COM2;
254      BSPPrintkPort      = BSP_CONSOLE_COM1;
255    }
256  #endif
257
258  /*
259   * If the device that was selected isn't available then
260   * let the user know and select the first available device.
261   */
262  if ( !bsp_Is_Available( Console_Port_Minor ) ) {
263    printk(
264      "Error finding %s setting console to first available\n",
265      get_name(Console_Port_Minor)
266    );
267    Console_Port_Minor = bsp_First_Available_Device();
268  }
269}
Note: See TracBrowser for help on using the repository browser.