Jeremy Kerr | 9326d77 | 2016-03-17 17:15:02 +0800 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2016 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 | */ |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 16 | |
Kun Yi | 6424cc3 | 2018-06-14 14:09:28 -0700 | [diff] [blame] | 17 | #include <ctype.h> |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 18 | #include <err.h> |
Andrew Jeffery | d30d757 | 2023-04-18 14:51:51 +0930 | [diff] [blame] | 19 | #include <errno.h> |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 20 | #include <fcntl.h> |
Kun Yi | 6424cc3 | 2018-06-14 14:09:28 -0700 | [diff] [blame] | 21 | #include <limits.h> |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 22 | #include <stdint.h> |
| 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | #include <strings.h> |
Benjamin Fair | fcbdea9 | 2018-06-04 14:19:25 -0700 | [diff] [blame] | 27 | #include <termios.h> /* for speed_t */ |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 28 | #include <unistd.h> |
| 29 | |
| 30 | #include <sys/mman.h> |
| 31 | #include <sys/stat.h> |
Cheng C Yang | f9c8f6c | 2019-03-04 18:39:52 +0800 | [diff] [blame] | 32 | #include "console-server.h" |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 33 | |
Jeremy Kerr | e440a40 | 2016-03-17 16:34:14 +0800 | [diff] [blame] | 34 | static const char *config_default_filename = SYSCONFDIR "/obmc-console.conf"; |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 35 | |
| 36 | struct config_item { |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 37 | char *name; |
| 38 | char *value; |
| 39 | struct config_item *next; |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 40 | }; |
| 41 | |
| 42 | struct config { |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 43 | struct config_item *items; |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | const char *config_get_value(struct config *config, const char *name) |
| 47 | { |
| 48 | struct config_item *item; |
| 49 | |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 50 | for (item = config->items; item; item = item->next) { |
| 51 | if (!strcasecmp(item->name, name)) { |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 52 | return item->value; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 53 | } |
| 54 | } |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 55 | |
| 56 | return NULL; |
| 57 | } |
| 58 | |
| 59 | static void config_parse(struct config *config, char *buf) |
| 60 | { |
| 61 | struct config_item *item; |
Andrew Jeffery | b70f871 | 2023-04-19 12:53:34 +0930 | [diff] [blame] | 62 | char *name; |
| 63 | char *value; |
| 64 | char *p; |
| 65 | char *line; |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 66 | |
| 67 | for (p = NULL, line = strtok_r(buf, "\n", &p); line; |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 68 | line = strtok_r(NULL, "\n", &p)) { |
Andrew Jeffery | d30d757 | 2023-04-18 14:51:51 +0930 | [diff] [blame] | 69 | int rc; |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 70 | |
| 71 | /* trim leading space */ |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 72 | for (; *line == ' ' || *line == '\t'; line++) { |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 73 | ; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 74 | } |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 75 | |
| 76 | /* skip comments */ |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 77 | if (*line == '#') { |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 78 | continue; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 79 | } |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 80 | |
Andrew Jeffery | d30d757 | 2023-04-18 14:51:51 +0930 | [diff] [blame] | 81 | name = malloc(strlen(line)); |
| 82 | value = malloc(strlen(line)); |
| 83 | if (name && value) { |
| 84 | rc = sscanf(line, "%[^ =] = %s ", name, value); |
| 85 | } else { |
| 86 | rc = -ENOMEM; |
| 87 | } |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 88 | |
Andrew Jeffery | d30d757 | 2023-04-18 14:51:51 +0930 | [diff] [blame] | 89 | if (rc != 2) { |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 90 | free(name); |
| 91 | free(value); |
| 92 | continue; |
| 93 | } |
| 94 | |
| 95 | /* create a new item and add to our list */ |
| 96 | item = malloc(sizeof(*item)); |
| 97 | item->name = name; |
| 98 | item->value = value; |
| 99 | item->next = config->items; |
| 100 | config->items = item; |
| 101 | } |
| 102 | } |
| 103 | |
Jeremy Kerr | 6c8d781 | 2016-03-17 15:05:45 +0800 | [diff] [blame] | 104 | static struct config *config_init_fd(int fd, const char *filename) |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 105 | { |
| 106 | struct config *config; |
Andrew Jeffery | b70f871 | 2023-04-19 12:53:34 +0930 | [diff] [blame] | 107 | size_t size; |
| 108 | size_t len; |
Andrew Jeffery | 5c359cc | 2023-04-18 22:50:07 +0930 | [diff] [blame] | 109 | ssize_t rc; |
Jeremy Kerr | 2eb56f5 | 2016-03-17 14:55:14 +0800 | [diff] [blame] | 110 | char *buf; |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 111 | |
Jeremy Kerr | 2eb56f5 | 2016-03-17 14:55:14 +0800 | [diff] [blame] | 112 | size = 4096; |
| 113 | len = 0; |
| 114 | buf = malloc(size + 1); |
Jeremy Kerr | 6c8d781 | 2016-03-17 15:05:45 +0800 | [diff] [blame] | 115 | config = NULL; |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 116 | |
Jeremy Kerr | 2eb56f5 | 2016-03-17 14:55:14 +0800 | [diff] [blame] | 117 | for (;;) { |
| 118 | rc = read(fd, buf + len, size - len); |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 119 | if (rc < 0) { |
Jeremy Kerr | 6c8d781 | 2016-03-17 15:05:45 +0800 | [diff] [blame] | 120 | warn("Can't read from configuration file %s", filename); |
| 121 | goto out_free; |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 122 | |
| 123 | } else if (!rc) { |
| 124 | break; |
| 125 | } |
Jeremy Kerr | 2eb56f5 | 2016-03-17 14:55:14 +0800 | [diff] [blame] | 126 | len += rc; |
| 127 | if (len == size) { |
| 128 | size <<= 1; |
| 129 | buf = realloc(buf, size + 1); |
| 130 | } |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 131 | } |
| 132 | buf[len] = '\0'; |
| 133 | |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 134 | config = malloc(sizeof(*config)); |
| 135 | config->items = NULL; |
| 136 | |
| 137 | config_parse(config, buf); |
| 138 | |
Jeremy Kerr | 6c8d781 | 2016-03-17 15:05:45 +0800 | [diff] [blame] | 139 | out_free: |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 140 | free(buf); |
Jeremy Kerr | 6c8d781 | 2016-03-17 15:05:45 +0800 | [diff] [blame] | 141 | return config; |
| 142 | } |
| 143 | |
| 144 | struct config *config_init(const char *filename) |
| 145 | { |
| 146 | struct config *config; |
| 147 | int fd; |
| 148 | |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 149 | if (!filename) { |
Jeremy Kerr | 6c8d781 | 2016-03-17 15:05:45 +0800 | [diff] [blame] | 150 | filename = config_default_filename; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 151 | } |
Jeremy Kerr | 6c8d781 | 2016-03-17 15:05:45 +0800 | [diff] [blame] | 152 | |
| 153 | fd = open(filename, O_RDONLY); |
| 154 | if (fd < 0) { |
| 155 | warn("Can't open configuration file %s", filename); |
| 156 | return NULL; |
| 157 | } |
| 158 | |
| 159 | config = config_init_fd(fd, filename); |
| 160 | |
| 161 | close(fd); |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 162 | |
| 163 | return config; |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | void config_fini(struct config *config) |
| 167 | { |
Andrew Jeffery | b70f871 | 2023-04-19 12:53:34 +0930 | [diff] [blame] | 168 | struct config_item *item; |
| 169 | struct config_item *next; |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 170 | |
| 171 | for (item = config->items; item; item = next) { |
| 172 | next = item->next; |
| 173 | free(item->name); |
| 174 | free(item->value); |
| 175 | free(item); |
| 176 | } |
| 177 | |
| 178 | free(config); |
| 179 | } |
Jeremy Kerr | 6c8d781 | 2016-03-17 15:05:45 +0800 | [diff] [blame] | 180 | |
Benjamin Fair | fcbdea9 | 2018-06-04 14:19:25 -0700 | [diff] [blame] | 181 | struct terminal_speed_name { |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 182 | speed_t speed; |
| 183 | uint32_t baud; |
| 184 | const char *name; |
Benjamin Fair | fcbdea9 | 2018-06-04 14:19:25 -0700 | [diff] [blame] | 185 | }; |
| 186 | |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 187 | #define TERM_SPEED(x) \ |
| 188 | { \ |
| 189 | B##x, x, #x \ |
| 190 | } |
Cheng C Yang | f9c8f6c | 2019-03-04 18:39:52 +0800 | [diff] [blame] | 191 | |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 192 | // clang-format off |
Cheng C Yang | f9c8f6c | 2019-03-04 18:39:52 +0800 | [diff] [blame] | 193 | static const struct terminal_speed_name terminal_speeds[] = { |
| 194 | TERM_SPEED(50), |
| 195 | TERM_SPEED(75), |
| 196 | TERM_SPEED(110), |
| 197 | TERM_SPEED(134), |
| 198 | TERM_SPEED(150), |
| 199 | TERM_SPEED(200), |
| 200 | TERM_SPEED(300), |
| 201 | TERM_SPEED(600), |
| 202 | TERM_SPEED(1200), |
| 203 | TERM_SPEED(1800), |
| 204 | TERM_SPEED(2400), |
| 205 | TERM_SPEED(4800), |
| 206 | TERM_SPEED(9600), |
| 207 | TERM_SPEED(19200), |
| 208 | TERM_SPEED(38400), |
| 209 | TERM_SPEED(57600), |
| 210 | TERM_SPEED(115200), |
| 211 | TERM_SPEED(230400), |
| 212 | TERM_SPEED(460800), |
| 213 | TERM_SPEED(500000), |
| 214 | TERM_SPEED(576000), |
| 215 | TERM_SPEED(921600), |
| 216 | TERM_SPEED(1000000), |
| 217 | TERM_SPEED(1152000), |
| 218 | TERM_SPEED(1500000), |
| 219 | TERM_SPEED(2000000), |
| 220 | TERM_SPEED(2500000), |
| 221 | TERM_SPEED(3000000), |
| 222 | TERM_SPEED(3500000), |
| 223 | TERM_SPEED(4000000), |
| 224 | }; |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 225 | // clang-format on |
Cheng C Yang | f9c8f6c | 2019-03-04 18:39:52 +0800 | [diff] [blame] | 226 | |
| 227 | int config_parse_baud(speed_t *speed, const char *baud_string) |
| 228 | { |
Benjamin Fair | fcbdea9 | 2018-06-04 14:19:25 -0700 | [diff] [blame] | 229 | size_t i; |
| 230 | |
Cheng C Yang | f9c8f6c | 2019-03-04 18:39:52 +0800 | [diff] [blame] | 231 | for (i = 0; i < ARRAY_SIZE(terminal_speeds); i++) { |
Benjamin Fair | fcbdea9 | 2018-06-04 14:19:25 -0700 | [diff] [blame] | 232 | if (strcmp(baud_string, terminal_speeds[i].name) == 0) { |
| 233 | *speed = terminal_speeds[i].speed; |
| 234 | return 0; |
| 235 | } |
| 236 | } |
| 237 | return -1; |
| 238 | } |
| 239 | |
Cheng C Yang | f9c8f6c | 2019-03-04 18:39:52 +0800 | [diff] [blame] | 240 | uint32_t parse_baud_to_int(speed_t speed) |
| 241 | { |
| 242 | size_t i; |
| 243 | |
| 244 | for (i = 0; i < ARRAY_SIZE(terminal_speeds); i++) { |
| 245 | if (terminal_speeds[i].speed == speed) { |
| 246 | return terminal_speeds[i].baud; |
| 247 | } |
| 248 | } |
| 249 | return 0; |
| 250 | } |
| 251 | |
| 252 | speed_t parse_int_to_baud(uint32_t baud) |
| 253 | { |
| 254 | size_t i; |
| 255 | |
| 256 | for (i = 0; i < ARRAY_SIZE(terminal_speeds); i++) { |
| 257 | if (terminal_speeds[i].baud == baud) { |
| 258 | return terminal_speeds[i].speed; |
| 259 | } |
| 260 | } |
| 261 | return 0; |
| 262 | } |
| 263 | |
Kun Yi | 6424cc3 | 2018-06-14 14:09:28 -0700 | [diff] [blame] | 264 | int config_parse_logsize(const char *size_str, size_t *size) |
| 265 | { |
| 266 | struct size_suffix_shift { |
| 267 | /* Left shiftwidth corresponding to the suffix. */ |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 268 | size_t shiftwidth; |
| 269 | int unit; |
Kun Yi | 6424cc3 | 2018-06-14 14:09:28 -0700 | [diff] [blame] | 270 | }; |
| 271 | |
| 272 | const struct size_suffix_shift suffixes[] = { |
| 273 | { 10, 'k' }, |
| 274 | { 20, 'M' }, |
| 275 | { 30, 'G' }, |
| 276 | }; |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 277 | const size_t num_suffixes = |
| 278 | sizeof(suffixes) / sizeof(struct size_suffix_shift); |
Kun Yi | 6424cc3 | 2018-06-14 14:09:28 -0700 | [diff] [blame] | 279 | size_t logsize; |
| 280 | char *suffix; |
| 281 | size_t i; |
| 282 | |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 283 | if (!size_str) { |
Kun Yi | 6424cc3 | 2018-06-14 14:09:28 -0700 | [diff] [blame] | 284 | return -1; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 285 | } |
Kun Yi | 6424cc3 | 2018-06-14 14:09:28 -0700 | [diff] [blame] | 286 | |
| 287 | logsize = strtoul(size_str, &suffix, 0); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 288 | if (logsize == 0 || logsize >= UINT32_MAX || suffix == size_str) { |
Kun Yi | 6424cc3 | 2018-06-14 14:09:28 -0700 | [diff] [blame] | 289 | return -1; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 290 | } |
Kun Yi | 6424cc3 | 2018-06-14 14:09:28 -0700 | [diff] [blame] | 291 | |
| 292 | /* Ignore spaces between number and suffix */ |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 293 | while (*suffix && isspace(*suffix)) { |
Kun Yi | 6424cc3 | 2018-06-14 14:09:28 -0700 | [diff] [blame] | 294 | suffix++; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 295 | } |
Kun Yi | 6424cc3 | 2018-06-14 14:09:28 -0700 | [diff] [blame] | 296 | |
| 297 | for (i = 0; i < num_suffixes; i++) { |
| 298 | if (*suffix == suffixes[i].unit) { |
| 299 | /* |
| 300 | * If logsize overflows, probably something was wrong. |
| 301 | * Return instead of clamping to an arbitrary value. |
| 302 | */ |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 303 | if (logsize > (UINT32_MAX >> suffixes[i].shiftwidth)) { |
Kun Yi | 6424cc3 | 2018-06-14 14:09:28 -0700 | [diff] [blame] | 304 | return -1; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 305 | } |
Kun Yi | 6424cc3 | 2018-06-14 14:09:28 -0700 | [diff] [blame] | 306 | |
| 307 | logsize <<= suffixes[i].shiftwidth; |
| 308 | suffix++; |
| 309 | break; |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | /* Allow suffix like 'kB' */ |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 314 | while (*suffix && (tolower(*suffix) == 'b' || isspace(*suffix))) { |
Kun Yi | 6424cc3 | 2018-06-14 14:09:28 -0700 | [diff] [blame] | 315 | suffix++; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 316 | } |
Kun Yi | 6424cc3 | 2018-06-14 14:09:28 -0700 | [diff] [blame] | 317 | |
| 318 | if (*suffix) { |
| 319 | warn("Invalid suffix!"); |
| 320 | return -1; |
| 321 | } |
| 322 | |
| 323 | *size = logsize; |
| 324 | return 0; |
| 325 | } |
| 326 | |
Jeremy Kerr | 6c8d781 | 2016-03-17 15:05:45 +0800 | [diff] [blame] | 327 | #ifdef CONFIG_TEST |
| 328 | int main(void) |
| 329 | { |
| 330 | struct config_item *item; |
| 331 | struct config *config; |
| 332 | |
| 333 | config = config_init_fd(STDIN_FILENO, "<stdin>"); |
| 334 | if (!config) |
| 335 | return EXIT_FAILURE; |
| 336 | |
| 337 | for (item = config->items; item; item = item->next) |
| 338 | printf("%s: %s\n", item->name, item->value); |
| 339 | |
| 340 | config_fini(config); |
| 341 | |
| 342 | return EXIT_SUCCESS; |
Jeremy Kerr | 6c8d781 | 2016-03-17 15:05:45 +0800 | [diff] [blame] | 343 | } |
| 344 | #endif |