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> |
Alexander Hansen | 1e04f44 | 2024-06-12 16:35:58 +0200 | [diff] [blame] | 32 | |
| 33 | #include <iniparser/iniparser.h> |
| 34 | |
| 35 | #include "config.h" |
| 36 | #include "config-internal.h" |
| 37 | |
| 38 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 39 | |
Jeremy Kerr | e440a40 | 2016-03-17 16:34:14 +0800 | [diff] [blame] | 40 | static const char *config_default_filename = SYSCONFDIR "/obmc-console.conf"; |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 41 | |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 42 | const char *config_get_value(struct config *config, const char *name) |
| 43 | { |
Alexander Hansen | 1e04f44 | 2024-06-12 16:35:58 +0200 | [diff] [blame] | 44 | int rc; |
| 45 | char buf[CONFIG_MAX_KEY_LENGTH]; |
| 46 | rc = snprintf(buf, CONFIG_MAX_KEY_LENGTH, ":%s", name); |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 47 | |
Alexander Hansen | 1e04f44 | 2024-06-12 16:35:58 +0200 | [diff] [blame] | 48 | if (rc < 0) { |
Andrew Jeffery | ba2af96 | 2023-05-02 15:02:56 +0930 | [diff] [blame] | 49 | return NULL; |
| 50 | } |
| 51 | |
Alexander Hansen | 1e04f44 | 2024-06-12 16:35:58 +0200 | [diff] [blame] | 52 | if ((size_t)rc >= sizeof(buf)) { |
| 53 | return NULL; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 54 | } |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 55 | |
Alexander Hansen | 1e04f44 | 2024-06-12 16:35:58 +0200 | [diff] [blame] | 56 | const char *value = iniparser_getstring(config->dict, buf, NULL); |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 57 | |
Alexander Hansen | 1e04f44 | 2024-06-12 16:35:58 +0200 | [diff] [blame] | 58 | if (value && strlen(value) == 0) { |
| 59 | return NULL; |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 60 | } |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 61 | |
Alexander Hansen | 1e04f44 | 2024-06-12 16:35:58 +0200 | [diff] [blame] | 62 | return value; |
Jeremy Kerr | 6c8d781 | 2016-03-17 15:05:45 +0800 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | struct config *config_init(const char *filename) |
| 66 | { |
| 67 | struct config *config; |
| 68 | int fd; |
| 69 | |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 70 | if (!filename) { |
Jeremy Kerr | 6c8d781 | 2016-03-17 15:05:45 +0800 | [diff] [blame] | 71 | filename = config_default_filename; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 72 | } |
Jeremy Kerr | 6c8d781 | 2016-03-17 15:05:45 +0800 | [diff] [blame] | 73 | |
| 74 | fd = open(filename, O_RDONLY); |
| 75 | if (fd < 0) { |
| 76 | warn("Can't open configuration file %s", filename); |
| 77 | return NULL; |
| 78 | } |
Jeremy Kerr | 6c8d781 | 2016-03-17 15:05:45 +0800 | [diff] [blame] | 79 | close(fd); |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 80 | |
Alexander Hansen | 1e04f44 | 2024-06-12 16:35:58 +0200 | [diff] [blame] | 81 | dictionary *dict = iniparser_load(filename); |
| 82 | |
| 83 | if (dict == NULL) { |
| 84 | return NULL; |
| 85 | } |
| 86 | |
| 87 | config = malloc(sizeof(*config)); |
| 88 | if (config) { |
| 89 | config->dict = dict; |
| 90 | } |
| 91 | |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 92 | return config; |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | void config_fini(struct config *config) |
| 96 | { |
Andrew Jeffery | ba2af96 | 2023-05-02 15:02:56 +0930 | [diff] [blame] | 97 | if (!config) { |
| 98 | return; |
| 99 | } |
| 100 | |
Alexander Hansen | 1e04f44 | 2024-06-12 16:35:58 +0200 | [diff] [blame] | 101 | if (config->dict) { |
| 102 | iniparser_freedict(config->dict); |
Jeremy Kerr | d66195c | 2016-03-16 17:24:51 +0800 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | free(config); |
| 106 | } |
Jeremy Kerr | 6c8d781 | 2016-03-17 15:05:45 +0800 | [diff] [blame] | 107 | |
Benjamin Fair | fcbdea9 | 2018-06-04 14:19:25 -0700 | [diff] [blame] | 108 | struct terminal_speed_name { |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 109 | speed_t speed; |
| 110 | uint32_t baud; |
| 111 | const char *name; |
Benjamin Fair | fcbdea9 | 2018-06-04 14:19:25 -0700 | [diff] [blame] | 112 | }; |
| 113 | |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 114 | #define TERM_SPEED(x) \ |
| 115 | { \ |
| 116 | B##x, x, #x \ |
| 117 | } |
Cheng C Yang | f9c8f6c | 2019-03-04 18:39:52 +0800 | [diff] [blame] | 118 | |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 119 | // clang-format off |
Cheng C Yang | f9c8f6c | 2019-03-04 18:39:52 +0800 | [diff] [blame] | 120 | static const struct terminal_speed_name terminal_speeds[] = { |
| 121 | TERM_SPEED(50), |
| 122 | TERM_SPEED(75), |
| 123 | TERM_SPEED(110), |
| 124 | TERM_SPEED(134), |
| 125 | TERM_SPEED(150), |
| 126 | TERM_SPEED(200), |
| 127 | TERM_SPEED(300), |
| 128 | TERM_SPEED(600), |
| 129 | TERM_SPEED(1200), |
| 130 | TERM_SPEED(1800), |
| 131 | TERM_SPEED(2400), |
| 132 | TERM_SPEED(4800), |
| 133 | TERM_SPEED(9600), |
| 134 | TERM_SPEED(19200), |
| 135 | TERM_SPEED(38400), |
| 136 | TERM_SPEED(57600), |
| 137 | TERM_SPEED(115200), |
| 138 | TERM_SPEED(230400), |
| 139 | TERM_SPEED(460800), |
| 140 | TERM_SPEED(500000), |
| 141 | TERM_SPEED(576000), |
| 142 | TERM_SPEED(921600), |
| 143 | TERM_SPEED(1000000), |
| 144 | TERM_SPEED(1152000), |
| 145 | TERM_SPEED(1500000), |
| 146 | TERM_SPEED(2000000), |
| 147 | TERM_SPEED(2500000), |
| 148 | TERM_SPEED(3000000), |
| 149 | TERM_SPEED(3500000), |
| 150 | TERM_SPEED(4000000), |
| 151 | }; |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 152 | // clang-format on |
Cheng C Yang | f9c8f6c | 2019-03-04 18:39:52 +0800 | [diff] [blame] | 153 | |
| 154 | int config_parse_baud(speed_t *speed, const char *baud_string) |
| 155 | { |
Benjamin Fair | fcbdea9 | 2018-06-04 14:19:25 -0700 | [diff] [blame] | 156 | size_t i; |
| 157 | |
Cheng C Yang | f9c8f6c | 2019-03-04 18:39:52 +0800 | [diff] [blame] | 158 | for (i = 0; i < ARRAY_SIZE(terminal_speeds); i++) { |
Benjamin Fair | fcbdea9 | 2018-06-04 14:19:25 -0700 | [diff] [blame] | 159 | if (strcmp(baud_string, terminal_speeds[i].name) == 0) { |
| 160 | *speed = terminal_speeds[i].speed; |
| 161 | return 0; |
| 162 | } |
| 163 | } |
| 164 | return -1; |
| 165 | } |
| 166 | |
Cheng C Yang | f9c8f6c | 2019-03-04 18:39:52 +0800 | [diff] [blame] | 167 | uint32_t parse_baud_to_int(speed_t speed) |
| 168 | { |
| 169 | size_t i; |
| 170 | |
| 171 | for (i = 0; i < ARRAY_SIZE(terminal_speeds); i++) { |
| 172 | if (terminal_speeds[i].speed == speed) { |
| 173 | return terminal_speeds[i].baud; |
| 174 | } |
| 175 | } |
| 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | speed_t parse_int_to_baud(uint32_t baud) |
| 180 | { |
| 181 | size_t i; |
| 182 | |
| 183 | for (i = 0; i < ARRAY_SIZE(terminal_speeds); i++) { |
| 184 | if (terminal_speeds[i].baud == baud) { |
| 185 | return terminal_speeds[i].speed; |
| 186 | } |
| 187 | } |
| 188 | return 0; |
| 189 | } |
| 190 | |
Medicine Yeh | d6e8b64 | 2024-03-18 01:49:17 -0700 | [diff] [blame] | 191 | int config_parse_bytesize(const char *size_str, size_t *size) |
Kun Yi | 6424cc3 | 2018-06-14 14:09:28 -0700 | [diff] [blame] | 192 | { |
| 193 | struct size_suffix_shift { |
| 194 | /* Left shiftwidth corresponding to the suffix. */ |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 195 | size_t shiftwidth; |
| 196 | int unit; |
Kun Yi | 6424cc3 | 2018-06-14 14:09:28 -0700 | [diff] [blame] | 197 | }; |
| 198 | |
| 199 | const struct size_suffix_shift suffixes[] = { |
| 200 | { 10, 'k' }, |
| 201 | { 20, 'M' }, |
| 202 | { 30, 'G' }, |
| 203 | }; |
Andrew Jeffery | a72711a | 2023-04-18 18:19:41 +0930 | [diff] [blame] | 204 | const size_t num_suffixes = |
| 205 | sizeof(suffixes) / sizeof(struct size_suffix_shift); |
Kun Yi | 6424cc3 | 2018-06-14 14:09:28 -0700 | [diff] [blame] | 206 | size_t logsize; |
| 207 | char *suffix; |
| 208 | size_t i; |
| 209 | |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 210 | if (!size_str) { |
Kun Yi | 6424cc3 | 2018-06-14 14:09:28 -0700 | [diff] [blame] | 211 | return -1; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 212 | } |
Kun Yi | 6424cc3 | 2018-06-14 14:09:28 -0700 | [diff] [blame] | 213 | |
| 214 | logsize = strtoul(size_str, &suffix, 0); |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 215 | if (logsize == 0 || logsize >= UINT32_MAX || suffix == size_str) { |
Kun Yi | 6424cc3 | 2018-06-14 14:09:28 -0700 | [diff] [blame] | 216 | return -1; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 217 | } |
Kun Yi | 6424cc3 | 2018-06-14 14:09:28 -0700 | [diff] [blame] | 218 | |
| 219 | /* Ignore spaces between number and suffix */ |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 220 | while (*suffix && isspace(*suffix)) { |
Kun Yi | 6424cc3 | 2018-06-14 14:09:28 -0700 | [diff] [blame] | 221 | suffix++; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 222 | } |
Kun Yi | 6424cc3 | 2018-06-14 14:09:28 -0700 | [diff] [blame] | 223 | |
| 224 | for (i = 0; i < num_suffixes; i++) { |
| 225 | if (*suffix == suffixes[i].unit) { |
| 226 | /* |
| 227 | * If logsize overflows, probably something was wrong. |
| 228 | * Return instead of clamping to an arbitrary value. |
| 229 | */ |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 230 | if (logsize > (UINT32_MAX >> suffixes[i].shiftwidth)) { |
Kun Yi | 6424cc3 | 2018-06-14 14:09:28 -0700 | [diff] [blame] | 231 | return -1; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 232 | } |
Kun Yi | 6424cc3 | 2018-06-14 14:09:28 -0700 | [diff] [blame] | 233 | |
| 234 | logsize <<= suffixes[i].shiftwidth; |
| 235 | suffix++; |
| 236 | break; |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | /* Allow suffix like 'kB' */ |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 241 | while (*suffix && (tolower(*suffix) == 'b' || isspace(*suffix))) { |
Kun Yi | 6424cc3 | 2018-06-14 14:09:28 -0700 | [diff] [blame] | 242 | suffix++; |
Andrew Jeffery | 2834c5b | 2023-04-19 12:47:09 +0930 | [diff] [blame] | 243 | } |
Kun Yi | 6424cc3 | 2018-06-14 14:09:28 -0700 | [diff] [blame] | 244 | |
| 245 | if (*suffix) { |
| 246 | warn("Invalid suffix!"); |
| 247 | return -1; |
| 248 | } |
| 249 | |
| 250 | *size = logsize; |
| 251 | return 0; |
| 252 | } |
| 253 | |
Ninad Palsule | 5ba20b5 | 2023-05-12 14:03:15 -0500 | [diff] [blame] | 254 | /* Default console id if not specified on command line or in config */ |
| 255 | #define DEFAULT_CONSOLE_ID "default" |
| 256 | |
| 257 | /* Get the console id */ |
| 258 | const char *config_resolve_console_id(struct config *config, const char *id_arg) |
| 259 | { |
| 260 | const char *configured; |
| 261 | |
| 262 | if (id_arg) { |
| 263 | return id_arg; |
| 264 | } |
| 265 | |
| 266 | if ((configured = config_get_value(config, "console-id"))) { |
| 267 | return configured; |
| 268 | } |
| 269 | |
Ninad Palsule | 5ba20b5 | 2023-05-12 14:03:15 -0500 | [diff] [blame] | 270 | return DEFAULT_CONSOLE_ID; |
| 271 | } |