xref: /optee_os/core/kernel/console.c (revision 5b25c76ac40f830867e3d60800120ffd7874e8dc)
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 				    char **path_out, char **params_out)
61 {
62 	const struct fdt_property *prop;
63 	const char *uart;
64 	const char *parms = NULL;
65 	int offs;
66 	char *stdout_data;
67 	char *p;
68 	TEE_Result rc = TEE_ERROR_GENERIC;
69 
70 	/* Probe console from secure DT and fallback to non-secure DT */
71 	offs = find_chosen_node(fdt);
72 	if (offs < 0) {
73 		DMSG("No console directive from DTB");
74 		return TEE_ERROR_ITEM_NOT_FOUND;
75 	}
76 
77 	prop = fdt_get_property(fdt, offs, "stdout-path", NULL);
78 	if (!prop) {
79 		/*
80 		 * A secure-chosen or chosen node is present but defined
81 		 * no stdout-path property: no console expected
82 		 */
83 		IMSG("Switching off console");
84 		register_serial_console(NULL);
85 		return TEE_ERROR_ITEM_NOT_FOUND;
86 	}
87 
88 	stdout_data = nex_strdup(prop->data);
89 	if (!stdout_data)
90 		panic();
91 	p = strchr(stdout_data, ':');
92 	if (p) {
93 		*p = '\0';
94 		parms = p + 1;
95 	}
96 
97 	/* stdout-path may refer to an alias */
98 	uart = fdt_get_alias(fdt, stdout_data);
99 	if (!uart) {
100 		/* Not an alias, assume we have a node path */
101 		uart = stdout_data;
102 	}
103 	offs = fdt_path_offset(fdt, uart);
104 	if (offs >= 0) {
105 		if (offs_out)
106 			*offs_out = offs;
107 		if (params_out)
108 			*params_out = parms ? nex_strdup(parms) : NULL;
109 		if (path_out)
110 			*path_out = uart ? nex_strdup(uart) : NULL;
111 
112 		rc = TEE_SUCCESS;
113 	}
114 
115 	nex_free(stdout_data);
116 
117 	return rc;
118 }
119 
120 void configure_console_from_dt(void)
121 {
122 	const struct dt_driver *dt_drv;
123 	const struct serial_driver *sdrv;
124 	struct serial_chip *dev;
125 	char *uart = NULL;
126 	char *parms = NULL;
127 	void *fdt;
128 	int offs;
129 
130 	fdt = get_external_dt();
131 	if (get_console_node_from_dt(fdt, &offs, &uart, &parms))
132 		return;
133 
134 	dt_drv = dt_find_compatible_driver(fdt, offs);
135 	if (!dt_drv)
136 		goto out;
137 
138 	sdrv = (const struct serial_driver *)dt_drv->driver;
139 	if (!sdrv)
140 		goto out;
141 
142 	dev = sdrv->dev_alloc();
143 	if (!dev)
144 		goto out;
145 
146 	/*
147 	 * If the console is the same as the early console, dev_init() might
148 	 * clear pending data. Flush to avoid that.
149 	 */
150 	console_flush();
151 	if (sdrv->dev_init(dev, fdt, offs, parms) < 0) {
152 		sdrv->dev_free(dev);
153 		goto out;
154 	}
155 
156 	IMSG("Switching console to device: %s", uart);
157 	register_serial_console(dev);
158 out:
159 	nex_free(uart);
160 	nex_free(parms);
161 }
162 
163 #endif /* CFG_DT */
164