Commit 682826b332fdaffaf814c2c6b3fa3dd1df39d139

Authored by Luo Ji
Committed by Ji Luo
1 parent 026360c2db

MA-14370 [coverity] Buffer not null terminated

Fix coverity issue:
  CID 43787: Buffer not null terminated (BUFFER_SIZE_WARNING)
  buffer_size_warning: Calling strncpy with a maximum size argument
  of 32 bytes on destination array sdev.name of size 32 bytes might
  leave the destination string unterminated.

Test: Coverity scan pass.

Change-Id: Ib10e631bab893cb9cd1484082229f806b02849ba
Signed-off-by: Luo Ji <ji.luo@nxp.com>

Showing 1 changed file with 1 additions and 1 deletions Inline Diff

drivers/serial/serial-uclass.c
1 /* 1 /*
2 * Copyright (c) 2014 The Chromium OS Authors. 2 * Copyright (c) 2014 The Chromium OS Authors.
3 * 3 *
4 * SPDX-License-Identifier: GPL-2.0+ 4 * SPDX-License-Identifier: GPL-2.0+
5 */ 5 */
6 6
7 #include <common.h> 7 #include <common.h>
8 #include <dm.h> 8 #include <dm.h>
9 #include <environment.h> 9 #include <environment.h>
10 #include <errno.h> 10 #include <errno.h>
11 #include <os.h> 11 #include <os.h>
12 #include <serial.h> 12 #include <serial.h>
13 #include <stdio_dev.h> 13 #include <stdio_dev.h>
14 #include <watchdog.h> 14 #include <watchdog.h>
15 #include <dm/lists.h> 15 #include <dm/lists.h>
16 #include <dm/device-internal.h> 16 #include <dm/device-internal.h>
17 #include <dm/of_access.h> 17 #include <dm/of_access.h>
18 18
19 DECLARE_GLOBAL_DATA_PTR; 19 DECLARE_GLOBAL_DATA_PTR;
20 20
21 /* 21 /*
22 * Table with supported baudrates (defined in config_xyz.h) 22 * Table with supported baudrates (defined in config_xyz.h)
23 */ 23 */
24 static const unsigned long baudrate_table[] = CONFIG_SYS_BAUDRATE_TABLE; 24 static const unsigned long baudrate_table[] = CONFIG_SYS_BAUDRATE_TABLE;
25 25
26 #if !CONFIG_VAL(SYS_MALLOC_F_LEN) 26 #if !CONFIG_VAL(SYS_MALLOC_F_LEN)
27 #error "Serial is required before relocation - define CONFIG_$(SPL_)SYS_MALLOC_F_LEN to make this work" 27 #error "Serial is required before relocation - define CONFIG_$(SPL_)SYS_MALLOC_F_LEN to make this work"
28 #endif 28 #endif
29 29
30 static int serial_check_stdout(const void *blob, struct udevice **devp) 30 static int serial_check_stdout(const void *blob, struct udevice **devp)
31 { 31 {
32 int node; 32 int node;
33 33
34 /* Check for a chosen console */ 34 /* Check for a chosen console */
35 node = fdtdec_get_chosen_node(blob, "stdout-path"); 35 node = fdtdec_get_chosen_node(blob, "stdout-path");
36 if (node < 0) { 36 if (node < 0) {
37 const char *str, *p, *name; 37 const char *str, *p, *name;
38 38
39 /* 39 /*
40 * Deal with things like 40 * Deal with things like
41 * stdout-path = "serial0:115200n8"; 41 * stdout-path = "serial0:115200n8";
42 * 42 *
43 * We need to look up the alias and then follow it to the 43 * We need to look up the alias and then follow it to the
44 * correct node. 44 * correct node.
45 */ 45 */
46 str = fdtdec_get_chosen_prop(blob, "stdout-path"); 46 str = fdtdec_get_chosen_prop(blob, "stdout-path");
47 if (str) { 47 if (str) {
48 p = strchr(str, ':'); 48 p = strchr(str, ':');
49 name = fdt_get_alias_namelen(blob, str, 49 name = fdt_get_alias_namelen(blob, str,
50 p ? p - str : strlen(str)); 50 p ? p - str : strlen(str));
51 if (name) 51 if (name)
52 node = fdt_path_offset(blob, name); 52 node = fdt_path_offset(blob, name);
53 } 53 }
54 } 54 }
55 if (node < 0) 55 if (node < 0)
56 node = fdt_path_offset(blob, "console"); 56 node = fdt_path_offset(blob, "console");
57 if (!uclass_get_device_by_of_offset(UCLASS_SERIAL, node, devp)) 57 if (!uclass_get_device_by_of_offset(UCLASS_SERIAL, node, devp))
58 return 0; 58 return 0;
59 59
60 /* 60 /*
61 * If the console is not marked to be bound before relocation, bind it 61 * If the console is not marked to be bound before relocation, bind it
62 * anyway. 62 * anyway.
63 */ 63 */
64 if (node > 0 && !lists_bind_fdt(gd->dm_root, offset_to_ofnode(node), 64 if (node > 0 && !lists_bind_fdt(gd->dm_root, offset_to_ofnode(node),
65 devp)) { 65 devp)) {
66 if (!device_probe(*devp)) 66 if (!device_probe(*devp))
67 return 0; 67 return 0;
68 } 68 }
69 69
70 return -ENODEV; 70 return -ENODEV;
71 } 71 }
72 72
73 static void serial_find_console_or_panic(void) 73 static void serial_find_console_or_panic(void)
74 { 74 {
75 const void *blob = gd->fdt_blob; 75 const void *blob = gd->fdt_blob;
76 struct udevice *dev; 76 struct udevice *dev;
77 #ifdef CONFIG_SERIAL_SEARCH_ALL 77 #ifdef CONFIG_SERIAL_SEARCH_ALL
78 int ret; 78 int ret;
79 #endif 79 #endif
80 80
81 if (CONFIG_IS_ENABLED(OF_PLATDATA)) { 81 if (CONFIG_IS_ENABLED(OF_PLATDATA)) {
82 uclass_first_device(UCLASS_SERIAL, &dev); 82 uclass_first_device(UCLASS_SERIAL, &dev);
83 if (dev) { 83 if (dev) {
84 gd->cur_serial_dev = dev; 84 gd->cur_serial_dev = dev;
85 return; 85 return;
86 } 86 }
87 } else if (CONFIG_IS_ENABLED(OF_CONTROL) && blob) { 87 } else if (CONFIG_IS_ENABLED(OF_CONTROL) && blob) {
88 /* Live tree has support for stdout */ 88 /* Live tree has support for stdout */
89 if (of_live_active()) { 89 if (of_live_active()) {
90 struct device_node *np = of_get_stdout(); 90 struct device_node *np = of_get_stdout();
91 91
92 if (np && !uclass_get_device_by_ofnode(UCLASS_SERIAL, 92 if (np && !uclass_get_device_by_ofnode(UCLASS_SERIAL,
93 np_to_ofnode(np), &dev)) { 93 np_to_ofnode(np), &dev)) {
94 gd->cur_serial_dev = dev; 94 gd->cur_serial_dev = dev;
95 return; 95 return;
96 } 96 }
97 } else { 97 } else {
98 if (!serial_check_stdout(blob, &dev)) { 98 if (!serial_check_stdout(blob, &dev)) {
99 gd->cur_serial_dev = dev; 99 gd->cur_serial_dev = dev;
100 return; 100 return;
101 } 101 }
102 } 102 }
103 } 103 }
104 if (!SPL_BUILD || !CONFIG_IS_ENABLED(OF_CONTROL) || !blob) { 104 if (!SPL_BUILD || !CONFIG_IS_ENABLED(OF_CONTROL) || !blob) {
105 /* 105 /*
106 * Try to use CONFIG_CONS_INDEX if available (it is numbered 106 * Try to use CONFIG_CONS_INDEX if available (it is numbered
107 * from 1!). 107 * from 1!).
108 * 108 *
109 * Failing that, get the device with sequence number 0, or in 109 * Failing that, get the device with sequence number 0, or in
110 * extremis just the first working serial device we can find. 110 * extremis just the first working serial device we can find.
111 * But we insist on having a console (even if it is silent). 111 * But we insist on having a console (even if it is silent).
112 */ 112 */
113 #ifdef CONFIG_CONS_INDEX 113 #ifdef CONFIG_CONS_INDEX
114 #define INDEX (CONFIG_CONS_INDEX - 1) 114 #define INDEX (CONFIG_CONS_INDEX - 1)
115 #else 115 #else
116 #define INDEX 0 116 #define INDEX 0
117 #endif 117 #endif
118 118
119 #ifdef CONFIG_SERIAL_SEARCH_ALL 119 #ifdef CONFIG_SERIAL_SEARCH_ALL
120 if (!uclass_get_device_by_seq(UCLASS_SERIAL, INDEX, &dev) || 120 if (!uclass_get_device_by_seq(UCLASS_SERIAL, INDEX, &dev) ||
121 !uclass_get_device(UCLASS_SERIAL, INDEX, &dev)) { 121 !uclass_get_device(UCLASS_SERIAL, INDEX, &dev)) {
122 if (dev->flags & DM_FLAG_ACTIVATED) { 122 if (dev->flags & DM_FLAG_ACTIVATED) {
123 gd->cur_serial_dev = dev; 123 gd->cur_serial_dev = dev;
124 return; 124 return;
125 } 125 }
126 } 126 }
127 127
128 /* Search for any working device */ 128 /* Search for any working device */
129 for (ret = uclass_first_device_check(UCLASS_SERIAL, &dev); 129 for (ret = uclass_first_device_check(UCLASS_SERIAL, &dev);
130 dev; 130 dev;
131 ret = uclass_next_device_check(&dev)) { 131 ret = uclass_next_device_check(&dev)) {
132 if (!ret) { 132 if (!ret) {
133 /* Device did succeed probing */ 133 /* Device did succeed probing */
134 gd->cur_serial_dev = dev; 134 gd->cur_serial_dev = dev;
135 return; 135 return;
136 } 136 }
137 } 137 }
138 #else 138 #else
139 if (!uclass_get_device_by_seq(UCLASS_SERIAL, INDEX, &dev) || 139 if (!uclass_get_device_by_seq(UCLASS_SERIAL, INDEX, &dev) ||
140 !uclass_get_device(UCLASS_SERIAL, INDEX, &dev) || 140 !uclass_get_device(UCLASS_SERIAL, INDEX, &dev) ||
141 (!uclass_first_device(UCLASS_SERIAL, &dev) && dev)) { 141 (!uclass_first_device(UCLASS_SERIAL, &dev) && dev)) {
142 gd->cur_serial_dev = dev; 142 gd->cur_serial_dev = dev;
143 return; 143 return;
144 } 144 }
145 #endif 145 #endif
146 146
147 #undef INDEX 147 #undef INDEX
148 } 148 }
149 149
150 #ifdef CONFIG_REQUIRE_SERIAL_CONSOLE 150 #ifdef CONFIG_REQUIRE_SERIAL_CONSOLE
151 panic_str("No serial driver found"); 151 panic_str("No serial driver found");
152 #endif 152 #endif
153 } 153 }
154 154
155 /* Called prior to relocation */ 155 /* Called prior to relocation */
156 int serial_init(void) 156 int serial_init(void)
157 { 157 {
158 serial_find_console_or_panic(); 158 serial_find_console_or_panic();
159 gd->flags |= GD_FLG_SERIAL_READY; 159 gd->flags |= GD_FLG_SERIAL_READY;
160 160
161 return 0; 161 return 0;
162 } 162 }
163 163
164 /* Called after relocation */ 164 /* Called after relocation */
165 void serial_initialize(void) 165 void serial_initialize(void)
166 { 166 {
167 serial_init(); 167 serial_init();
168 } 168 }
169 169
170 static void _serial_putc(struct udevice *dev, char ch) 170 static void _serial_putc(struct udevice *dev, char ch)
171 { 171 {
172 struct dm_serial_ops *ops = serial_get_ops(dev); 172 struct dm_serial_ops *ops = serial_get_ops(dev);
173 int err; 173 int err;
174 174
175 if (ch == '\n') 175 if (ch == '\n')
176 _serial_putc(dev, '\r'); 176 _serial_putc(dev, '\r');
177 177
178 do { 178 do {
179 err = ops->putc(dev, ch); 179 err = ops->putc(dev, ch);
180 } while (err == -EAGAIN); 180 } while (err == -EAGAIN);
181 } 181 }
182 182
183 static void _serial_puts(struct udevice *dev, const char *str) 183 static void _serial_puts(struct udevice *dev, const char *str)
184 { 184 {
185 struct dm_serial_ops *ops = serial_get_ops(dev); 185 struct dm_serial_ops *ops = serial_get_ops(dev);
186 int err; 186 int err;
187 187
188 if (ops->puts) { 188 if (ops->puts) {
189 do { 189 do {
190 err = ops->puts(dev, str); 190 err = ops->puts(dev, str);
191 } while (err == -EAGAIN); 191 } while (err == -EAGAIN);
192 } else { 192 } else {
193 while (*str) 193 while (*str)
194 _serial_putc(dev, *str++); 194 _serial_putc(dev, *str++);
195 } 195 }
196 } 196 }
197 197
198 static int __serial_getc(struct udevice *dev) 198 static int __serial_getc(struct udevice *dev)
199 { 199 {
200 struct dm_serial_ops *ops = serial_get_ops(dev); 200 struct dm_serial_ops *ops = serial_get_ops(dev);
201 int err; 201 int err;
202 202
203 do { 203 do {
204 err = ops->getc(dev); 204 err = ops->getc(dev);
205 if (err == -EAGAIN) 205 if (err == -EAGAIN)
206 WATCHDOG_RESET(); 206 WATCHDOG_RESET();
207 } while (err == -EAGAIN); 207 } while (err == -EAGAIN);
208 208
209 return err >= 0 ? err : 0; 209 return err >= 0 ? err : 0;
210 } 210 }
211 211
212 static int __serial_tstc(struct udevice *dev) 212 static int __serial_tstc(struct udevice *dev)
213 { 213 {
214 struct dm_serial_ops *ops = serial_get_ops(dev); 214 struct dm_serial_ops *ops = serial_get_ops(dev);
215 215
216 if (ops->pending) 216 if (ops->pending)
217 return ops->pending(dev, true); 217 return ops->pending(dev, true);
218 218
219 return 1; 219 return 1;
220 } 220 }
221 221
222 #if CONFIG_IS_ENABLED(SERIAL_RX_BUFFER) 222 #if CONFIG_IS_ENABLED(SERIAL_RX_BUFFER)
223 static int _serial_tstc(struct udevice *dev) 223 static int _serial_tstc(struct udevice *dev)
224 { 224 {
225 struct serial_dev_priv *upriv = dev_get_uclass_priv(dev); 225 struct serial_dev_priv *upriv = dev_get_uclass_priv(dev);
226 226
227 /* Read all available chars into the RX buffer */ 227 /* Read all available chars into the RX buffer */
228 while (__serial_tstc(dev)) { 228 while (__serial_tstc(dev)) {
229 upriv->buf[upriv->wr_ptr++] = __serial_getc(dev); 229 upriv->buf[upriv->wr_ptr++] = __serial_getc(dev);
230 upriv->wr_ptr %= CONFIG_SERIAL_RX_BUFFER_SIZE; 230 upriv->wr_ptr %= CONFIG_SERIAL_RX_BUFFER_SIZE;
231 } 231 }
232 232
233 return upriv->rd_ptr != upriv->wr_ptr ? 1 : 0; 233 return upriv->rd_ptr != upriv->wr_ptr ? 1 : 0;
234 } 234 }
235 235
236 static int _serial_getc(struct udevice *dev) 236 static int _serial_getc(struct udevice *dev)
237 { 237 {
238 struct serial_dev_priv *upriv = dev_get_uclass_priv(dev); 238 struct serial_dev_priv *upriv = dev_get_uclass_priv(dev);
239 char val; 239 char val;
240 240
241 val = upriv->buf[upriv->rd_ptr++]; 241 val = upriv->buf[upriv->rd_ptr++];
242 upriv->rd_ptr %= CONFIG_SERIAL_RX_BUFFER_SIZE; 242 upriv->rd_ptr %= CONFIG_SERIAL_RX_BUFFER_SIZE;
243 243
244 return val; 244 return val;
245 } 245 }
246 246
247 #else /* CONFIG_IS_ENABLED(SERIAL_RX_BUFFER) */ 247 #else /* CONFIG_IS_ENABLED(SERIAL_RX_BUFFER) */
248 248
249 static int _serial_getc(struct udevice *dev) 249 static int _serial_getc(struct udevice *dev)
250 { 250 {
251 return __serial_getc(dev); 251 return __serial_getc(dev);
252 } 252 }
253 253
254 static int _serial_tstc(struct udevice *dev) 254 static int _serial_tstc(struct udevice *dev)
255 { 255 {
256 return __serial_tstc(dev); 256 return __serial_tstc(dev);
257 } 257 }
258 #endif /* CONFIG_IS_ENABLED(SERIAL_RX_BUFFER) */ 258 #endif /* CONFIG_IS_ENABLED(SERIAL_RX_BUFFER) */
259 259
260 void serial_putc(char ch) 260 void serial_putc(char ch)
261 { 261 {
262 if (gd->cur_serial_dev) 262 if (gd->cur_serial_dev)
263 _serial_putc(gd->cur_serial_dev, ch); 263 _serial_putc(gd->cur_serial_dev, ch);
264 } 264 }
265 265
266 void serial_puts(const char *str) 266 void serial_puts(const char *str)
267 { 267 {
268 if (gd->cur_serial_dev) 268 if (gd->cur_serial_dev)
269 _serial_puts(gd->cur_serial_dev, str); 269 _serial_puts(gd->cur_serial_dev, str);
270 } 270 }
271 271
272 int serial_getc(void) 272 int serial_getc(void)
273 { 273 {
274 if (!gd->cur_serial_dev) 274 if (!gd->cur_serial_dev)
275 return 0; 275 return 0;
276 276
277 return _serial_getc(gd->cur_serial_dev); 277 return _serial_getc(gd->cur_serial_dev);
278 } 278 }
279 279
280 int serial_tstc(void) 280 int serial_tstc(void)
281 { 281 {
282 if (!gd->cur_serial_dev) 282 if (!gd->cur_serial_dev)
283 return 0; 283 return 0;
284 284
285 return _serial_tstc(gd->cur_serial_dev); 285 return _serial_tstc(gd->cur_serial_dev);
286 } 286 }
287 287
288 void serial_setbrg(void) 288 void serial_setbrg(void)
289 { 289 {
290 struct dm_serial_ops *ops; 290 struct dm_serial_ops *ops;
291 291
292 if (!gd->cur_serial_dev) 292 if (!gd->cur_serial_dev)
293 return; 293 return;
294 294
295 ops = serial_get_ops(gd->cur_serial_dev); 295 ops = serial_get_ops(gd->cur_serial_dev);
296 if (ops->setbrg) 296 if (ops->setbrg)
297 ops->setbrg(gd->cur_serial_dev, gd->baudrate); 297 ops->setbrg(gd->cur_serial_dev, gd->baudrate);
298 } 298 }
299 299
300 void serial_stdio_init(void) 300 void serial_stdio_init(void)
301 { 301 {
302 } 302 }
303 303
304 #if defined(CONFIG_DM_STDIO) 304 #if defined(CONFIG_DM_STDIO)
305 305
306 #if CONFIG_IS_ENABLED(SERIAL_PRESENT) 306 #if CONFIG_IS_ENABLED(SERIAL_PRESENT)
307 static void serial_stub_putc(struct stdio_dev *sdev, const char ch) 307 static void serial_stub_putc(struct stdio_dev *sdev, const char ch)
308 { 308 {
309 _serial_putc(sdev->priv, ch); 309 _serial_putc(sdev->priv, ch);
310 } 310 }
311 #endif 311 #endif
312 312
313 static void serial_stub_puts(struct stdio_dev *sdev, const char *str) 313 static void serial_stub_puts(struct stdio_dev *sdev, const char *str)
314 { 314 {
315 _serial_puts(sdev->priv, str); 315 _serial_puts(sdev->priv, str);
316 } 316 }
317 317
318 static int serial_stub_getc(struct stdio_dev *sdev) 318 static int serial_stub_getc(struct stdio_dev *sdev)
319 { 319 {
320 return _serial_getc(sdev->priv); 320 return _serial_getc(sdev->priv);
321 } 321 }
322 322
323 static int serial_stub_tstc(struct stdio_dev *sdev) 323 static int serial_stub_tstc(struct stdio_dev *sdev)
324 { 324 {
325 return _serial_tstc(sdev->priv); 325 return _serial_tstc(sdev->priv);
326 } 326 }
327 #endif 327 #endif
328 328
329 /** 329 /**
330 * on_baudrate() - Update the actual baudrate when the env var changes 330 * on_baudrate() - Update the actual baudrate when the env var changes
331 * 331 *
332 * This will check for a valid baudrate and only apply it if valid. 332 * This will check for a valid baudrate and only apply it if valid.
333 */ 333 */
334 static int on_baudrate(const char *name, const char *value, enum env_op op, 334 static int on_baudrate(const char *name, const char *value, enum env_op op,
335 int flags) 335 int flags)
336 { 336 {
337 int i; 337 int i;
338 int baudrate; 338 int baudrate;
339 339
340 switch (op) { 340 switch (op) {
341 case env_op_create: 341 case env_op_create:
342 case env_op_overwrite: 342 case env_op_overwrite:
343 /* 343 /*
344 * Switch to new baudrate if new baudrate is supported 344 * Switch to new baudrate if new baudrate is supported
345 */ 345 */
346 baudrate = simple_strtoul(value, NULL, 10); 346 baudrate = simple_strtoul(value, NULL, 10);
347 347
348 /* Not actually changing */ 348 /* Not actually changing */
349 if (gd->baudrate == baudrate) 349 if (gd->baudrate == baudrate)
350 return 0; 350 return 0;
351 351
352 for (i = 0; i < ARRAY_SIZE(baudrate_table); ++i) { 352 for (i = 0; i < ARRAY_SIZE(baudrate_table); ++i) {
353 if (baudrate == baudrate_table[i]) 353 if (baudrate == baudrate_table[i])
354 break; 354 break;
355 } 355 }
356 if (i == ARRAY_SIZE(baudrate_table)) { 356 if (i == ARRAY_SIZE(baudrate_table)) {
357 if ((flags & H_FORCE) == 0) 357 if ((flags & H_FORCE) == 0)
358 printf("## Baudrate %d bps not supported\n", 358 printf("## Baudrate %d bps not supported\n",
359 baudrate); 359 baudrate);
360 return 1; 360 return 1;
361 } 361 }
362 if ((flags & H_INTERACTIVE) != 0) { 362 if ((flags & H_INTERACTIVE) != 0) {
363 printf("## Switch baudrate to %d bps and press ENTER ...\n", 363 printf("## Switch baudrate to %d bps and press ENTER ...\n",
364 baudrate); 364 baudrate);
365 udelay(50000); 365 udelay(50000);
366 } 366 }
367 367
368 gd->baudrate = baudrate; 368 gd->baudrate = baudrate;
369 369
370 serial_setbrg(); 370 serial_setbrg();
371 371
372 udelay(50000); 372 udelay(50000);
373 373
374 if ((flags & H_INTERACTIVE) != 0) 374 if ((flags & H_INTERACTIVE) != 0)
375 while (1) { 375 while (1) {
376 if (getc() == '\r') 376 if (getc() == '\r')
377 break; 377 break;
378 } 378 }
379 379
380 return 0; 380 return 0;
381 case env_op_delete: 381 case env_op_delete:
382 printf("## Baudrate may not be deleted\n"); 382 printf("## Baudrate may not be deleted\n");
383 return 1; 383 return 1;
384 default: 384 default:
385 return 0; 385 return 0;
386 } 386 }
387 } 387 }
388 U_BOOT_ENV_CALLBACK(baudrate, on_baudrate); 388 U_BOOT_ENV_CALLBACK(baudrate, on_baudrate);
389 389
390 #if CONFIG_IS_ENABLED(SERIAL_PRESENT) 390 #if CONFIG_IS_ENABLED(SERIAL_PRESENT)
391 static int serial_post_probe(struct udevice *dev) 391 static int serial_post_probe(struct udevice *dev)
392 { 392 {
393 struct dm_serial_ops *ops = serial_get_ops(dev); 393 struct dm_serial_ops *ops = serial_get_ops(dev);
394 #ifdef CONFIG_DM_STDIO 394 #ifdef CONFIG_DM_STDIO
395 struct serial_dev_priv *upriv = dev_get_uclass_priv(dev); 395 struct serial_dev_priv *upriv = dev_get_uclass_priv(dev);
396 struct stdio_dev sdev; 396 struct stdio_dev sdev;
397 #endif 397 #endif
398 int ret; 398 int ret;
399 399
400 #if defined(CONFIG_NEEDS_MANUAL_RELOC) 400 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
401 if (ops->setbrg) 401 if (ops->setbrg)
402 ops->setbrg += gd->reloc_off; 402 ops->setbrg += gd->reloc_off;
403 if (ops->getc) 403 if (ops->getc)
404 ops->getc += gd->reloc_off; 404 ops->getc += gd->reloc_off;
405 if (ops->putc) 405 if (ops->putc)
406 ops->putc += gd->reloc_off; 406 ops->putc += gd->reloc_off;
407 if (ops->pending) 407 if (ops->pending)
408 ops->pending += gd->reloc_off; 408 ops->pending += gd->reloc_off;
409 if (ops->clear) 409 if (ops->clear)
410 ops->clear += gd->reloc_off; 410 ops->clear += gd->reloc_off;
411 #if CONFIG_POST & CONFIG_SYS_POST_UART 411 #if CONFIG_POST & CONFIG_SYS_POST_UART
412 if (ops->loop) 412 if (ops->loop)
413 ops->loop += gd->reloc_off 413 ops->loop += gd->reloc_off
414 #endif 414 #endif
415 #endif 415 #endif
416 /* Set the baud rate */ 416 /* Set the baud rate */
417 if (ops->setbrg) { 417 if (ops->setbrg) {
418 ret = ops->setbrg(dev, gd->baudrate); 418 ret = ops->setbrg(dev, gd->baudrate);
419 if (ret) 419 if (ret)
420 return ret; 420 return ret;
421 } 421 }
422 422
423 #ifdef CONFIG_DM_STDIO 423 #ifdef CONFIG_DM_STDIO
424 if (!(gd->flags & GD_FLG_RELOC)) 424 if (!(gd->flags & GD_FLG_RELOC))
425 return 0; 425 return 0;
426 memset(&sdev, '\0', sizeof(sdev)); 426 memset(&sdev, '\0', sizeof(sdev));
427 427
428 strncpy(sdev.name, dev->name, sizeof(sdev.name)); 428 strncpy(sdev.name, dev->name, sizeof(sdev.name) - 1);
429 sdev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT | DEV_FLAGS_DM; 429 sdev.flags = DEV_FLAGS_OUTPUT | DEV_FLAGS_INPUT | DEV_FLAGS_DM;
430 sdev.priv = dev; 430 sdev.priv = dev;
431 sdev.putc = serial_stub_putc; 431 sdev.putc = serial_stub_putc;
432 sdev.puts = serial_stub_puts; 432 sdev.puts = serial_stub_puts;
433 sdev.getc = serial_stub_getc; 433 sdev.getc = serial_stub_getc;
434 sdev.tstc = serial_stub_tstc; 434 sdev.tstc = serial_stub_tstc;
435 435
436 #if CONFIG_IS_ENABLED(SERIAL_RX_BUFFER) 436 #if CONFIG_IS_ENABLED(SERIAL_RX_BUFFER)
437 /* Allocate the RX buffer */ 437 /* Allocate the RX buffer */
438 upriv->buf = malloc(CONFIG_SERIAL_RX_BUFFER_SIZE); 438 upriv->buf = malloc(CONFIG_SERIAL_RX_BUFFER_SIZE);
439 #endif 439 #endif
440 440
441 stdio_register_dev(&sdev, &upriv->sdev); 441 stdio_register_dev(&sdev, &upriv->sdev);
442 #endif 442 #endif
443 return 0; 443 return 0;
444 } 444 }
445 445
446 static int serial_pre_remove(struct udevice *dev) 446 static int serial_pre_remove(struct udevice *dev)
447 { 447 {
448 #if CONFIG_IS_ENABLED(SYS_STDIO_DEREGISTER) 448 #if CONFIG_IS_ENABLED(SYS_STDIO_DEREGISTER)
449 struct serial_dev_priv *upriv = dev_get_uclass_priv(dev); 449 struct serial_dev_priv *upriv = dev_get_uclass_priv(dev);
450 450
451 if (stdio_deregister_dev(upriv->sdev, true)) 451 if (stdio_deregister_dev(upriv->sdev, true))
452 return -EPERM; 452 return -EPERM;
453 #endif 453 #endif
454 454
455 return 0; 455 return 0;
456 } 456 }
457 457
458 UCLASS_DRIVER(serial) = { 458 UCLASS_DRIVER(serial) = {
459 .id = UCLASS_SERIAL, 459 .id = UCLASS_SERIAL,
460 .name = "serial", 460 .name = "serial",
461 .flags = DM_UC_FLAG_SEQ_ALIAS, 461 .flags = DM_UC_FLAG_SEQ_ALIAS,
462 .post_probe = serial_post_probe, 462 .post_probe = serial_post_probe,
463 .pre_remove = serial_pre_remove, 463 .pre_remove = serial_pre_remove,
464 .per_device_auto_alloc_size = sizeof(struct serial_dev_priv), 464 .per_device_auto_alloc_size = sizeof(struct serial_dev_priv),
465 }; 465 };
466 #endif 466 #endif
467 467