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