Ninad Palsule | e258e51 | 2023-04-26 22:17:57 -0500 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2023 IBM Corporation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Andrew Jeffery | 30ea638 | 2023-05-03 16:56:10 +0930 | [diff] [blame] | 17 | #include <assert.h> |
Ninad Palsule | e258e51 | 2023-04-26 22:17:57 -0500 | [diff] [blame] | 18 | #include <errno.h> |
| 19 | #include <err.h> |
Ninad Palsule | bd992c9 | 2023-05-09 18:30:01 -0500 | [diff] [blame] | 20 | #include <string.h> |
| 21 | #include <sys/socket.h> |
Ninad Palsule | e258e51 | 2023-04-26 22:17:57 -0500 | [diff] [blame] | 22 | |
| 23 | #include "console-server.h" |
Alexander Hansen | 1e04f44 | 2024-06-12 16:35:58 +0200 | [diff] [blame] | 24 | #include "config.h" |
Ninad Palsule | e258e51 | 2023-04-26 22:17:57 -0500 | [diff] [blame] | 25 | |
| 26 | /* size of the dbus object path length */ |
| 27 | const size_t dbus_obj_path_len = 1024; |
| 28 | |
Ninad Palsule | b14ca19 | 2023-03-31 09:49:00 -0500 | [diff] [blame] | 29 | #define DBUS_ERR "org.openbmc.error" |
| 30 | #define DBUS_NAME "xyz.openbmc_project.Console.%s" |
| 31 | #define OBJ_NAME "/xyz/openbmc_project/console/%s" |
Jonathan Doman | 9598b86 | 2023-06-01 10:21:25 -0700 | [diff] [blame] | 32 | #define UART_INTF "xyz.openbmc_project.Console.UART" |
Ninad Palsule | b14ca19 | 2023-03-31 09:49:00 -0500 | [diff] [blame] | 33 | #define ACCESS_INTF "xyz.openbmc_project.Console.Access" |
Ninad Palsule | e258e51 | 2023-04-26 22:17:57 -0500 | [diff] [blame] | 34 | |
| 35 | static void tty_change_baudrate(struct console *console) |
| 36 | { |
Ninad Palsule | e258e51 | 2023-04-26 22:17:57 -0500 | [diff] [blame] | 37 | int i; |
| 38 | int rc; |
| 39 | |
| 40 | tty_init_termios(console); |
| 41 | |
| 42 | for (i = 0; i < console->n_handlers; i++) { |
Jeremy Kerr | e2826c7 | 2024-07-05 10:54:21 +0800 | [diff] [blame] | 43 | const struct handler_type *type; |
| 44 | struct handler *handler; |
| 45 | |
Ninad Palsule | e258e51 | 2023-04-26 22:17:57 -0500 | [diff] [blame] | 46 | handler = console->handlers[i]; |
Jeremy Kerr | e2826c7 | 2024-07-05 10:54:21 +0800 | [diff] [blame] | 47 | type = handler->type; |
| 48 | if (!type->baudrate) { |
Ninad Palsule | e258e51 | 2023-04-26 22:17:57 -0500 | [diff] [blame] | 49 | continue; |
| 50 | } |
| 51 | |
Jeremy Kerr | e2826c7 | 2024-07-05 10:54:21 +0800 | [diff] [blame] | 52 | rc = type->baudrate(handler, console->tty.uart.baud); |
Ninad Palsule | e258e51 | 2023-04-26 22:17:57 -0500 | [diff] [blame] | 53 | if (rc) { |
| 54 | warnx("Can't set terminal baudrate for handler %s", |
Jeremy Kerr | e2826c7 | 2024-07-05 10:54:21 +0800 | [diff] [blame] | 55 | type->name); |
Ninad Palsule | e258e51 | 2023-04-26 22:17:57 -0500 | [diff] [blame] | 56 | } |
| 57 | } |
| 58 | } |
| 59 | |
Jonathan Doman | 9598b86 | 2023-06-01 10:21:25 -0700 | [diff] [blame] | 60 | static int set_baud_handler(sd_bus *bus, const char *path, |
| 61 | const char *interface, const char *property, |
| 62 | sd_bus_message *msg, void *userdata, |
| 63 | sd_bus_error *err __attribute__((unused))) |
| 64 | { |
| 65 | struct console *console = userdata; |
| 66 | uint64_t baudrate; |
| 67 | speed_t speed; |
| 68 | int r; |
| 69 | |
| 70 | if (!console) { |
| 71 | return -ENOENT; |
| 72 | } |
| 73 | |
| 74 | r = sd_bus_message_read(msg, "t", &baudrate); |
| 75 | if (r < 0 || baudrate > UINT32_MAX) { |
| 76 | return -EINVAL; |
| 77 | } |
| 78 | |
| 79 | speed = parse_int_to_baud((uint32_t)baudrate); |
| 80 | if (!speed) { |
| 81 | warnx("Invalid baud rate: '%" PRIu64 "'", baudrate); |
| 82 | return -EINVAL; |
| 83 | } |
| 84 | |
| 85 | assert(console->tty.type == TTY_DEVICE_UART); |
| 86 | console->tty.uart.baud = speed; |
| 87 | tty_change_baudrate(console); |
| 88 | |
| 89 | sd_bus_emit_properties_changed(bus, path, interface, property, NULL); |
| 90 | |
| 91 | return 1; |
| 92 | } |
| 93 | |
Jonathan Doman | 9598b86 | 2023-06-01 10:21:25 -0700 | [diff] [blame] | 94 | static int get_baud_handler(sd_bus *bus __attribute__((unused)), |
| 95 | const char *path __attribute__((unused)), |
| 96 | const char *interface __attribute__((unused)), |
| 97 | const char *property __attribute__((unused)), |
| 98 | sd_bus_message *reply, void *userdata, |
| 99 | sd_bus_error *error __attribute__((unused))) |
| 100 | { |
| 101 | struct console *console = userdata; |
| 102 | uint64_t baudrate; |
| 103 | int r; |
| 104 | |
| 105 | assert(console->tty.type == TTY_DEVICE_UART); |
| 106 | baudrate = parse_baud_to_int(console->tty.uart.baud); |
| 107 | if (!baudrate) { |
| 108 | warnx("Invalid baud rate: '%d'", console->tty.uart.baud); |
| 109 | } |
| 110 | |
| 111 | r = sd_bus_message_append(reply, "t", baudrate); |
| 112 | |
| 113 | return r; |
| 114 | } |
| 115 | |
Ninad Palsule | bd992c9 | 2023-05-09 18:30:01 -0500 | [diff] [blame] | 116 | static int method_connect(sd_bus_message *msg, void *userdata, |
| 117 | sd_bus_error *err) |
Ninad Palsule | b14ca19 | 2023-03-31 09:49:00 -0500 | [diff] [blame] | 118 | { |
| 119 | struct console *console = userdata; |
Ninad Palsule | bd992c9 | 2023-05-09 18:30:01 -0500 | [diff] [blame] | 120 | int rc; |
| 121 | int socket_fd = -1; |
Ninad Palsule | b14ca19 | 2023-03-31 09:49:00 -0500 | [diff] [blame] | 122 | |
Ninad Palsule | bd992c9 | 2023-05-09 18:30:01 -0500 | [diff] [blame] | 123 | if (!console) { |
| 124 | warnx("Internal error: Console pointer is null"); |
| 125 | sd_bus_error_set_const(err, DBUS_ERR, "Internal error"); |
| 126 | return sd_bus_reply_method_error(msg, err); |
| 127 | } |
| 128 | |
| 129 | /* Register the consumer. */ |
| 130 | socket_fd = dbus_create_socket_consumer(console); |
| 131 | if (socket_fd < 0) { |
| 132 | rc = socket_fd; |
| 133 | warnx("Failed to create socket consumer: %s", strerror(rc)); |
| 134 | sd_bus_error_set_const(err, DBUS_ERR, |
| 135 | "Failed to create socket consumer"); |
| 136 | return sd_bus_reply_method_error(msg, err); |
| 137 | } |
| 138 | |
| 139 | rc = sd_bus_reply_method_return(msg, "h", socket_fd); |
| 140 | |
| 141 | /* Close the our end */ |
| 142 | close(socket_fd); |
| 143 | |
| 144 | return rc; |
Ninad Palsule | b14ca19 | 2023-03-31 09:49:00 -0500 | [diff] [blame] | 145 | } |
| 146 | |
Jonathan Doman | 9598b86 | 2023-06-01 10:21:25 -0700 | [diff] [blame] | 147 | static const sd_bus_vtable console_uart_vtable[] = { |
| 148 | SD_BUS_VTABLE_START(0), |
| 149 | SD_BUS_WRITABLE_PROPERTY("Baud", "t", get_baud_handler, |
| 150 | set_baud_handler, 0, |
| 151 | SD_BUS_VTABLE_UNPRIVILEGED | |
| 152 | SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE), |
| 153 | SD_BUS_VTABLE_END, |
| 154 | }; |
| 155 | |
Ninad Palsule | b14ca19 | 2023-03-31 09:49:00 -0500 | [diff] [blame] | 156 | static const sd_bus_vtable console_access_vtable[] = { |
| 157 | SD_BUS_VTABLE_START(0), |
Ninad Palsule | bd992c9 | 2023-05-09 18:30:01 -0500 | [diff] [blame] | 158 | SD_BUS_METHOD("Connect", SD_BUS_NO_ARGS, "h", method_connect, |
| 159 | SD_BUS_VTABLE_UNPRIVILEGED), |
Ninad Palsule | b14ca19 | 2023-03-31 09:49:00 -0500 | [diff] [blame] | 160 | SD_BUS_VTABLE_END, |
| 161 | }; |
| 162 | |
Andrew Jeffery | 498a4a8 | 2024-07-09 15:07:05 +0930 | [diff] [blame] | 163 | int dbus_init(struct console *console, |
| 164 | struct config *config __attribute__((unused))) |
Ninad Palsule | e258e51 | 2023-04-26 22:17:57 -0500 | [diff] [blame] | 165 | { |
| 166 | char obj_name[dbus_obj_path_len]; |
| 167 | char dbus_name[dbus_obj_path_len]; |
| 168 | int dbus_poller = 0; |
| 169 | int fd; |
| 170 | int r; |
| 171 | size_t bytes; |
| 172 | |
| 173 | if (!console) { |
| 174 | warnx("Couldn't get valid console"); |
Andrew Jeffery | 498a4a8 | 2024-07-09 15:07:05 +0930 | [diff] [blame] | 175 | return -1; |
Ninad Palsule | e258e51 | 2023-04-26 22:17:57 -0500 | [diff] [blame] | 176 | } |
| 177 | |
Andrew Jeffery | 12398cd | 2024-07-09 14:59:33 +0930 | [diff] [blame] | 178 | r = sd_bus_default(&console->bus); |
Ninad Palsule | e258e51 | 2023-04-26 22:17:57 -0500 | [diff] [blame] | 179 | if (r < 0) { |
Andrew Jeffery | 12398cd | 2024-07-09 14:59:33 +0930 | [diff] [blame] | 180 | warnx("Failed to connect to bus: %s", strerror(-r)); |
Andrew Jeffery | 498a4a8 | 2024-07-09 15:07:05 +0930 | [diff] [blame] | 181 | return -1; |
Ninad Palsule | e258e51 | 2023-04-26 22:17:57 -0500 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | /* Register support console interface */ |
| 185 | bytes = snprintf(obj_name, dbus_obj_path_len, OBJ_NAME, |
| 186 | console->console_id); |
| 187 | if (bytes >= dbus_obj_path_len) { |
| 188 | warnx("Console id '%s' is too long. There is no enough space in the buffer.", |
| 189 | console->console_id); |
Andrew Jeffery | 498a4a8 | 2024-07-09 15:07:05 +0930 | [diff] [blame] | 190 | return -1; |
Ninad Palsule | e258e51 | 2023-04-26 22:17:57 -0500 | [diff] [blame] | 191 | } |
| 192 | |
Andrew Jeffery | 30ea638 | 2023-05-03 16:56:10 +0930 | [diff] [blame] | 193 | if (console->tty.type == TTY_DEVICE_UART) { |
Jonathan Doman | 9598b86 | 2023-06-01 10:21:25 -0700 | [diff] [blame] | 194 | /* Register UART interface */ |
| 195 | r = sd_bus_add_object_vtable(console->bus, NULL, obj_name, |
| 196 | UART_INTF, console_uart_vtable, |
| 197 | console); |
| 198 | if (r < 0) { |
| 199 | warnx("Failed to register UART interface: %s", |
| 200 | strerror(-r)); |
Andrew Jeffery | 498a4a8 | 2024-07-09 15:07:05 +0930 | [diff] [blame] | 201 | return -1; |
Jonathan Doman | 9598b86 | 2023-06-01 10:21:25 -0700 | [diff] [blame] | 202 | } |
Ninad Palsule | b14ca19 | 2023-03-31 09:49:00 -0500 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | /* Register access interface */ |
| 206 | r = sd_bus_add_object_vtable(console->bus, NULL, obj_name, ACCESS_INTF, |
| 207 | console_access_vtable, console); |
Ninad Palsule | e258e51 | 2023-04-26 22:17:57 -0500 | [diff] [blame] | 208 | if (r < 0) { |
| 209 | warnx("Failed to issue method call: %s", strerror(-r)); |
Andrew Jeffery | 498a4a8 | 2024-07-09 15:07:05 +0930 | [diff] [blame] | 210 | return -1; |
Ninad Palsule | e258e51 | 2023-04-26 22:17:57 -0500 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | bytes = snprintf(dbus_name, dbus_obj_path_len, DBUS_NAME, |
| 214 | console->console_id); |
| 215 | if (bytes >= dbus_obj_path_len) { |
| 216 | warnx("Console id '%s' is too long. There is no enough space in the buffer.", |
| 217 | console->console_id); |
Andrew Jeffery | 498a4a8 | 2024-07-09 15:07:05 +0930 | [diff] [blame] | 218 | return -1; |
Ninad Palsule | e258e51 | 2023-04-26 22:17:57 -0500 | [diff] [blame] | 219 | } |
| 220 | |
Ninad Palsule | b14ca19 | 2023-03-31 09:49:00 -0500 | [diff] [blame] | 221 | /* Finally register the bus name */ |
Ninad Palsule | e258e51 | 2023-04-26 22:17:57 -0500 | [diff] [blame] | 222 | r = sd_bus_request_name(console->bus, dbus_name, |
| 223 | SD_BUS_NAME_ALLOW_REPLACEMENT | |
| 224 | SD_BUS_NAME_REPLACE_EXISTING); |
| 225 | if (r < 0) { |
| 226 | warnx("Failed to acquire service name: %s", strerror(-r)); |
Andrew Jeffery | 498a4a8 | 2024-07-09 15:07:05 +0930 | [diff] [blame] | 227 | return -1; |
Ninad Palsule | e258e51 | 2023-04-26 22:17:57 -0500 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | fd = sd_bus_get_fd(console->bus); |
| 231 | if (fd < 0) { |
| 232 | warnx("Couldn't get the bus file descriptor"); |
Andrew Jeffery | 498a4a8 | 2024-07-09 15:07:05 +0930 | [diff] [blame] | 233 | return -1; |
Ninad Palsule | e258e51 | 2023-04-26 22:17:57 -0500 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | dbus_poller = POLLFD_DBUS; |
| 237 | |
| 238 | console->pollfds[dbus_poller].fd = fd; |
| 239 | console->pollfds[dbus_poller].events = POLLIN; |
Andrew Jeffery | 498a4a8 | 2024-07-09 15:07:05 +0930 | [diff] [blame] | 240 | |
| 241 | return 0; |
Ninad Palsule | e258e51 | 2023-04-26 22:17:57 -0500 | [diff] [blame] | 242 | } |