xref: /optee_os/core/kernel/console.c (revision 8bbd9b374a51a1b8617796aae8a70c271543357f)
1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright (c) 2017, Linaro Limited
4  */
5 
6 #include <console.h>
7 #include <compiler.h>
8 #include <drivers/serial.h>
9 #include <kernel/generic_boot.h>
10 #include <kernel/panic.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <string_ext.h>
14 
15 #ifdef CFG_DT
16 #include <kernel/dt.h>
17 #include <libfdt.h>
18 #endif
19 
20 static struct serial_chip *serial_console __nex_bss;
21 
22 void __weak console_putc(int ch)
23 {
24 	if (!serial_console)
25 		return;
26 
27 	if (ch == '\n')
28 		serial_console->ops->putc(serial_console, '\r');
29 	serial_console->ops->putc(serial_console, ch);
30 }
31 
32 void __weak console_flush(void)
33 {
34 	if (!serial_console)
35 		return;
36 
37 	serial_console->ops->flush(serial_console);
38 }
39 
40 void register_serial_console(struct serial_chip *chip)
41 {
42 	serial_console = chip;
43 }
44 
45 #ifdef CFG_DT
46 static int find_chosen_node(void *fdt)
47 {
48 	if (!fdt)
49 		return -1;
50 
51 	int offset = fdt_path_offset(fdt, "/secure-chosen");
52 
53 	if (offset < 0)
54 		offset = fdt_path_offset(fdt, "/chosen");
55 
56 	return offset;
57 }
58 
59 TEE_Result get_console_node_from_dt(void *fdt, int *offs_out,
60 				    const char **path_out,
61 				    const char **params_out)
62 {
63 	const struct fdt_property *prop;
64 	const char *uart;
65 	const char *parms = NULL;
66 	int offs;
67 	char *stdout_data;
68 	char *p;
69 	TEE_Result rc = TEE_ERROR_GENERIC;
70 
71 	/* Probe console from secure DT and fallback to non-secure DT */
72 	offs = find_chosen_node(fdt);
73 	if (offs < 0) {
74 		DMSG("No console directive from DTB");
75 		return TEE_ERROR_ITEM_NOT_FOUND;
76 	}
77 
78 	prop = fdt_get_property(fdt, offs, "stdout-path", NULL);
79 	if (!prop) {
80 		/*
81 		 * A secure-chosen or chosen node is present but defined
82 		 * no stdout-path property: no console expected
83 		 */
84 		IMSG("Switching off console");
85 		register_serial_console(NULL);
86 		return TEE_ERROR_ITEM_NOT_FOUND;
87 	}
88 
89 	stdout_data = nex_strdup(prop->data);
90 	if (!stdout_data)
91 		panic();
92 	p = strchr(stdout_data, ':');
93 	if (p) {
94 		*p = '\0';
95 		parms = p + 1;
96 	}
97 
98 	/* stdout-path may refer to an alias */
99 	uart = fdt_get_alias(fdt, stdout_data);
100 	if (!uart) {
101 		/* Not an alias, assume we have a node path */
102 		uart = stdout_data;
103 	}
104 	offs = fdt_path_offset(fdt, uart);
105 	if (offs >= 0) {
106 		if (offs_out)
107 			*offs_out = offs;
108 		if (params_out)
109 			*params_out = parms;
110 		if (path_out)
111 			*path_out = uart;
112 
113 		rc = TEE_SUCCESS;
114 	}
115 
116 	nex_free(stdout_data);
117 
118 	return rc;
119 }
120 
121 void configure_console_from_dt(void)
122 {
123 	const struct dt_driver *dt_drv;
124 	const struct serial_driver *sdrv;
125 	struct serial_chip *dev;
126 	const char *uart;
127 	const char *parms;
128 	void *fdt;
129 	int offs;
130 
131 	fdt = get_external_dt();
132 	if (get_console_node_from_dt(fdt, &offs, &uart, &parms))
133 		return;
134 
135 	dt_drv = dt_find_compatible_driver(fdt, offs);
136 	if (!dt_drv)
137 		return;
138 
139 	sdrv = (const struct serial_driver *)dt_drv->driver;
140 	if (!sdrv)
141 		return;
142 
143 	dev = sdrv->dev_alloc();
144 	if (!dev)
145 		return;
146 
147 	/*
148 	 * If the console is the same as the early console, dev_init() might
149 	 * clear pending data. Flush to avoid that.
150 	 */
151 	console_flush();
152 	if (sdrv->dev_init(dev, fdt, offs, parms) < 0) {
153 		sdrv->dev_free(dev);
154 		return;
155 	}
156 
157 	IMSG("Switching console to device: %s", uart);
158 	register_serial_console(dev);
159 }
160 
161 #endif /* CFG_DT */
162