]> git.ipfire.org Git - people/ms/u-boot.git/blame - common/stdio.c
imx: hab: Check if CSF is valid before authenticating image
[people/ms/u-boot.git] / common / stdio.c
CommitLineData
91d3256c 1/*
3f4978c7
HS
2 * Copyright (C) 2009 Sergey Kubushyn <ksi@koi8.net>
3 *
4 * Changes for multibus/multiadapter I2C support.
5 *
91d3256c
WD
6 * (C) Copyright 2000
7 * Paolo Scaffardi, AIRVENT SAM s.p.a - RIMINI(ITALY), arsenio@tin.it
8 *
1a459660 9 * SPDX-License-Identifier: GPL-2.0+
91d3256c
WD
10 */
11
12#include <config.h>
13#include <common.h>
b206cd73 14#include <dm.h>
d97143a6 15#include <errno.h>
91d3256c
WD
16#include <stdarg.h>
17#include <malloc.h>
52cb4d4f 18#include <stdio_dev.h>
281e00a3 19#include <serial.h>
ea818dbb 20
69153988 21#if defined(CONFIG_SYS_I2C)
91d3256c 22#include <i2c.h>
7f6c2cbc 23#endif
91d3256c 24
b206cd73
SG
25#include <dm/device-internal.h>
26
d87080b7
WD
27DECLARE_GLOBAL_DATA_PTR;
28
52cb4d4f
JCPV
29static struct stdio_dev devs;
30struct stdio_dev *stdio_devices[] = { NULL, NULL, NULL };
91d3256c
WD
31char *stdio_names[MAX_FILES] = { "stdin", "stdout", "stderr" };
32
6d0f6bcf
JCPV
33#if defined(CONFIG_SPLASH_SCREEN) && !defined(CONFIG_SYS_DEVICE_NULLDEV)
34#define CONFIG_SYS_DEVICE_NULLDEV 1
d791b1dc
WD
35#endif
36
869588de 37#if CONFIG_IS_ENABLED(SYS_STDIO_DEREGISTER)
32d01926
HG
38#define CONFIG_SYS_DEVICE_NULLDEV 1
39#endif
d791b1dc 40
6d0f6bcf 41#ifdef CONFIG_SYS_DEVICE_NULLDEV
654f8d0f 42static void nulldev_putc(struct stdio_dev *dev, const char c)
91d3256c 43{
c1de7a6d 44 /* nulldev is empty! */
91d3256c
WD
45}
46
654f8d0f 47static void nulldev_puts(struct stdio_dev *dev, const char *s)
91d3256c 48{
c1de7a6d 49 /* nulldev is empty! */
91d3256c
WD
50}
51
654f8d0f 52static int nulldev_input(struct stdio_dev *dev)
91d3256c 53{
c1de7a6d
JCPV
54 /* nulldev is empty! */
55 return 0;
91d3256c
WD
56}
57#endif
58
654f8d0f 59static void stdio_serial_putc(struct stdio_dev *dev, const char c)
709ea543
SG
60{
61 serial_putc(c);
62}
63
654f8d0f 64static void stdio_serial_puts(struct stdio_dev *dev, const char *s)
709ea543
SG
65{
66 serial_puts(s);
67}
68
654f8d0f 69static int stdio_serial_getc(struct stdio_dev *dev)
709ea543
SG
70{
71 return serial_getc();
72}
73
654f8d0f 74static int stdio_serial_tstc(struct stdio_dev *dev)
709ea543
SG
75{
76 return serial_tstc();
77}
78
91d3256c
WD
79/**************************************************************************
80 * SYSTEM DRIVERS
81 **************************************************************************
82 */
83
84static void drv_system_init (void)
85{
52cb4d4f 86 struct stdio_dev dev;
91d3256c
WD
87
88 memset (&dev, 0, sizeof (dev));
89
90 strcpy (dev.name, "serial");
1caf934a 91 dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
709ea543
SG
92 dev.putc = stdio_serial_putc;
93 dev.puts = stdio_serial_puts;
94 dev.getc = stdio_serial_getc;
95 dev.tstc = stdio_serial_tstc;
52cb4d4f 96 stdio_register (&dev);
91d3256c 97
6d0f6bcf 98#ifdef CONFIG_SYS_DEVICE_NULLDEV
91d3256c
WD
99 memset (&dev, 0, sizeof (dev));
100
101 strcpy (dev.name, "nulldev");
1caf934a 102 dev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT;
91d3256c
WD
103 dev.putc = nulldev_putc;
104 dev.puts = nulldev_puts;
105 dev.getc = nulldev_input;
106 dev.tstc = nulldev_input;
107
52cb4d4f 108 stdio_register (&dev);
91d3256c
WD
109#endif
110}
111
112/**************************************************************************
113 * DEVICES
114 **************************************************************************
115 */
52cb4d4f 116struct list_head* stdio_get_list(void)
c1de7a6d
JCPV
117{
118 return &(devs.list);
119}
120
d8441ea2
SG
121#ifdef CONFIG_DM_VIDEO
122/**
123 * stdio_probe_device() - Find a device which provides the given stdio device
124 *
125 * This looks for a device of the given uclass which provides a particular
126 * stdio device. It is currently really only useful for UCLASS_VIDEO.
127 *
128 * Ultimately we want to be able to probe a device by its stdio name. At
129 * present devices register in their probe function (for video devices this
130 * is done in vidconsole_post_probe()) and we don't know what name they will
131 * use until they do so.
132 * TODO(sjg@chromium.org): We should be able to determine the name before
133 * probing, and probe the required device.
134 *
135 * @name: stdio device name (e.g. "vidconsole")
136 * id: Uclass ID of device to look for (e.g. UCLASS_VIDEO)
137 * @sdevp: Returns stdout device, if found, else NULL
138 * @return 0 if found, -ENOENT if no device found with that name, other -ve
139 * on other error
140 */
141static int stdio_probe_device(const char *name, enum uclass_id id,
142 struct stdio_dev **sdevp)
143{
144 struct stdio_dev *sdev;
145 struct udevice *dev;
146 int seq, ret;
147
148 *sdevp = NULL;
149 seq = trailing_strtoln(name, NULL);
150 if (seq == -1)
d844efec
SG
151 seq = 0;
152 ret = uclass_get_device_by_seq(id, seq, &dev);
153 if (ret == -ENODEV)
d8441ea2 154 ret = uclass_first_device_err(id, &dev);
d8441ea2
SG
155 if (ret) {
156 debug("No %s device for seq %d (%s)\n", uclass_get_name(id),
157 seq, name);
158 return ret;
159 }
160 /* The device should be be the last one registered */
161 sdev = list_empty(&devs.list) ? NULL :
162 list_last_entry(&devs.list, struct stdio_dev, list);
163 if (!sdev || strcmp(sdev->name, name)) {
164 debug("Device '%s' did not register with stdio as '%s'\n",
165 dev->name, name);
166 return -ENOENT;
167 }
168 *sdevp = sdev;
169
170 return 0;
171}
172#endif
173
ab29a34a 174struct stdio_dev *stdio_get_by_name(const char *name)
c1de7a6d
JCPV
175{
176 struct list_head *pos;
d8441ea2 177 struct stdio_dev *sdev;
c1de7a6d 178
ab29a34a 179 if (!name)
c1de7a6d
JCPV
180 return NULL;
181
182 list_for_each(pos, &(devs.list)) {
d8441ea2
SG
183 sdev = list_entry(pos, struct stdio_dev, list);
184 if (strcmp(sdev->name, name) == 0)
185 return sdev;
c1de7a6d 186 }
d8441ea2
SG
187#ifdef CONFIG_DM_VIDEO
188 /*
189 * We did not find a suitable stdio device. If there is a video
190 * driver with a name starting with 'vidconsole', we can try probing
191 * that in the hope that it will produce the required stdio device.
192 *
193 * This function is sometimes called with the entire value of
194 * 'stdout', which may include a list of devices separate by commas.
195 * Obviously this is not going to work, so we ignore that case. The
196 * call path in that case is console_init_r() -> search_device() ->
197 * stdio_get_by_name().
198 */
199 if (!strncmp(name, "vidconsole", 10) && !strchr(name, ',') &&
200 !stdio_probe_device(name, UCLASS_VIDEO, &sdev))
201 return sdev;
202#endif
c1de7a6d
JCPV
203
204 return NULL;
205}
206
52cb4d4f 207struct stdio_dev* stdio_clone(struct stdio_dev *dev)
628ffd73 208{
52cb4d4f 209 struct stdio_dev *_dev;
628ffd73
JCPV
210
211 if(!dev)
212 return NULL;
213
52cb4d4f 214 _dev = calloc(1, sizeof(struct stdio_dev));
628ffd73
JCPV
215
216 if(!_dev)
217 return NULL;
218
52cb4d4f 219 memcpy(_dev, dev, sizeof(struct stdio_dev));
628ffd73
JCPV
220
221 return _dev;
222}
91d3256c 223
d97143a6 224int stdio_register_dev(struct stdio_dev *dev, struct stdio_dev **devp)
91d3256c 225{
52cb4d4f 226 struct stdio_dev *_dev;
628ffd73 227
52cb4d4f 228 _dev = stdio_clone(dev);
628ffd73 229 if(!_dev)
d97143a6 230 return -ENODEV;
3e3c026e 231 list_add_tail(&(_dev->list), &(devs.list));
d97143a6
SG
232 if (devp)
233 *devp = _dev;
234
91d3256c
WD
235 return 0;
236}
237
d97143a6
SG
238int stdio_register(struct stdio_dev *dev)
239{
240 return stdio_register_dev(dev, NULL);
241}
242
91d3256c
WD
243/* deregister the device "devname".
244 * returns 0 if success, -1 if device is assigned and 1 if devname not found
245 */
869588de 246#if CONFIG_IS_ENABLED(SYS_STDIO_DEREGISTER)
32d01926 247int stdio_deregister_dev(struct stdio_dev *dev, int force)
91d3256c 248{
c1de7a6d
JCPV
249 int l;
250 struct list_head *pos;
03bf22f5 251 char temp_names[3][16];
91d3256c 252
91d3256c
WD
253 /* get stdio devices (ListRemoveItem changes the dev list) */
254 for (l=0 ; l< MAX_FILES; l++) {
255 if (stdio_devices[l] == dev) {
32d01926
HG
256 if (force) {
257 strcpy(temp_names[l], "nulldev");
258 continue;
259 }
91d3256c
WD
260 /* Device is assigned -> report error */
261 return -1;
262 }
263 memcpy (&temp_names[l][0],
264 stdio_devices[l]->name,
03bf22f5 265 sizeof(temp_names[l]));
91d3256c 266 }
c1de7a6d
JCPV
267
268 list_del(&(dev->list));
88274b6c 269 free(dev);
c1de7a6d 270
91d3256c 271 /* reassign Device list */
c1de7a6d 272 list_for_each(pos, &(devs.list)) {
52cb4d4f 273 dev = list_entry(pos, struct stdio_dev, list);
91d3256c 274 for (l=0 ; l< MAX_FILES; l++) {
c1de7a6d 275 if(strcmp(dev->name, temp_names[l]) == 0)
91d3256c 276 stdio_devices[l] = dev;
91d3256c
WD
277 }
278 }
279 return 0;
280}
d97143a6 281
32d01926 282int stdio_deregister(const char *devname, int force)
d97143a6
SG
283{
284 struct stdio_dev *dev;
285
286 dev = stdio_get_by_name(devname);
287
288 if (!dev) /* device not found */
289 return -ENODEV;
290
32d01926 291 return stdio_deregister_dev(dev, force);
d97143a6 292}
869588de 293#endif /* CONFIG_IS_ENABLED(SYS_STDIO_DEREGISTER) */
91d3256c 294
9fb02491 295int stdio_init_tables(void)
91d3256c 296{
2e5167cc 297#if defined(CONFIG_NEEDS_MANUAL_RELOC)
521af04d 298 /* already relocated for current ARM implementation */
91d3256c 299 ulong relocation_offset = gd->reloc_off;
3595ac49 300 int i;
91d3256c
WD
301
302 /* relocate device name pointers */
303 for (i = 0; i < (sizeof (stdio_names) / sizeof (char *)); ++i) {
304 stdio_names[i] = (char *) (((ulong) stdio_names[i]) +
305 relocation_offset);
306 }
2e5167cc 307#endif /* CONFIG_NEEDS_MANUAL_RELOC */
91d3256c
WD
308
309 /* Initialize the list */
c1de7a6d 310 INIT_LIST_HEAD(&(devs.list));
91d3256c 311
9fb02491
SG
312 return 0;
313}
314
315int stdio_add_devices(void)
316{
b206cd73
SG
317#ifdef CONFIG_DM_KEYBOARD
318 struct udevice *dev;
319 struct uclass *uc;
320 int ret;
321
322 /*
323 * For now we probe all the devices here. At some point this should be
324 * done only when the devices are required - e.g. we have a list of
325 * input devices to start up in the stdin environment variable. That
326 * work probably makes more sense when stdio itself is converted to
327 * driver model.
328 *
329 * TODO(sjg@chromium.org): Convert changing uclass_first_device() etc.
330 * to return the device even on error. Then we could use that here.
331 */
332 ret = uclass_get(UCLASS_KEYBOARD, &uc);
333 if (ret)
334 return ret;
335
336 /* Don't report errors to the caller - assume that they are non-fatal */
337 uclass_foreach_dev(dev, uc) {
338 ret = device_probe(dev);
339 if (ret)
340 printf("Failed to probe keyboard '%s'\n", dev->name);
341 }
342#endif
3f4978c7 343#ifdef CONFIG_SYS_I2C
3f4978c7 344 i2c_init_all();
3f4978c7 345#else
3f4978c7 346#endif
e3b81c1c 347#ifdef CONFIG_DM_VIDEO
d8441ea2
SG
348 /*
349 * If the console setting is not in environment variables then
350 * console_init_r() will not be calling iomux_doenv() (which calls
351 * search_device()). So we will not dynamically add devices by
352 * calling stdio_probe_device().
353 *
354 * So just probe all video devices now so that whichever one is
355 * required will be available.
356 */
357#ifndef CONFIG_SYS_CONSOLE_IS_IN_ENV
e3b81c1c 358 struct udevice *vdev;
35a1f0df
SG
359# ifndef CONFIG_DM_KEYBOARD
360 int ret;
361# endif
e3b81c1c
SG
362
363 for (ret = uclass_first_device(UCLASS_VIDEO, &vdev);
364 vdev;
365 ret = uclass_next_device(&vdev))
366 ;
367 if (ret)
368 printf("%s: Video device failed (ret=%d)\n", __func__, ret);
d8441ea2 369#endif /* !CONFIG_SYS_CONSOLE_IS_IN_ENV */
e3b81c1c
SG
370#else
371# if defined(CONFIG_LCD)
91d3256c 372 drv_lcd_init ();
e3b81c1c
SG
373# endif
374# if defined(CONFIG_VIDEO) || defined(CONFIG_CFB_CONSOLE)
91d3256c 375 drv_video_init ();
e3b81c1c
SG
376# endif
377#endif /* CONFIG_DM_VIDEO */
b206cd73 378#if defined(CONFIG_KEYBOARD) && !defined(CONFIG_DM_KEYBOARD)
682011ff 379 drv_keyboard_init ();
91d3256c
WD
380#endif
381 drv_system_init ();
52cb4d4f 382 serial_stdio_init ();
232c150a
WD
383#ifdef CONFIG_USB_TTY
384 drv_usbtty_init ();
385#endif
68ceb29e
WD
386#ifdef CONFIG_NETCONSOLE
387 drv_nc_init ();
388#endif
36ea8e9a
MF
389#ifdef CONFIG_JTAG_CONSOLE
390 drv_jtag_console_init ();
391#endif
98ab435f
VB
392#ifdef CONFIG_CBMEM_CONSOLE
393 cbmemc_init();
394#endif
9fb02491
SG
395
396 return 0;
397}
398
399int stdio_init(void)
400{
401 stdio_init_tables();
402 stdio_add_devices();
403
404 return 0;
91d3256c 405}