Coverage Report

Created: 2026-11-13 06:22

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/barebox/common/console_common.c
Line
Count
Source
1
// SPDX-License-Identifier: GPL-2.0-or-later
2
/*
3
 * based on code:
4
 *
5
 * (C) Copyright 2000 Paolo Scaffardi, AIRVENT SAM s.p.a -
6
 * RIMINI(ITALY), arsenio@tin.it
7
 */
8
#include "stdio.h"
9
#include <common.h>
10
#include <fs.h>
11
#include <errno.h>
12
#include <console.h>
13
#include <init.h>
14
#include <string.h>
15
#include <environment.h>
16
#include <globalvar.h>
17
#include <magicvar.h>
18
#include <memory.h>
19
#include <of.h>
20
#include <password.h>
21
#include <clock.h>
22
#include <malloc.h>
23
#include <linux/pstore.h>
24
#include <linux/math64.h>
25
#include <linux/sizes.h>
26
#include <linux/overflow.h>
27
#include <security/config.h>
28
29
#ifndef CONFIG_CONSOLE_NONE
30
31
static const char *colored_log_level[] = {
32
  [MSG_EMERG] = "\033[1;31mEMERG:\033[0m ", /* red */
33
  [MSG_ALERT] = "\033[1;31mALERT:\033[0m ", /* red */
34
  [MSG_CRIT] = "\033[1;31mCRITICAL:\033[0m ", /* red */
35
  [MSG_ERR] = "\033[1;31mERROR:\033[0m ",   /* red */
36
  [MSG_WARNING] = "\033[1;33mWARNING:\033[0m ", /* yellow */
37
  [MSG_NOTICE] = "\033[1;34mNOTICE:\033[0m ", /* blue */
38
};
39
40
int barebox_loglevel = CONFIG_DEFAULT_LOGLEVEL;
41
42
LIST_HEAD(barebox_logbuf);
43
static int barebox_logbuf_num_messages;
44
static int barebox_log_max_messages;
45
46
static void log_del(struct log_entry *log)
47
270
{
48
270
  list_del(&log->list);
49
270
  free(log);
50
270
  barebox_logbuf_num_messages--;
51
270
}
52
53
/**
54
 * log_clean - delete log messages from buffer
55
 *
56
 * @limit:  The maximum messages left in the buffer after
57
 *    calling this function.
58
 *
59
 * This function deletes all messages in the logbuf exceeding
60
 * the limit.
61
 */
62
void log_clean(unsigned int limit)
63
300
{
64
300
  struct log_entry *log, *tmp;
65
66
300
  if (list_empty(&barebox_logbuf))
67
30
    return;
68
69
510
  list_for_each_entry_safe(log, tmp, &barebox_logbuf, list) {
70
510
    if (barebox_logbuf_num_messages <= limit)
71
240
      break;
72
270
    log_del(log);
73
270
  }
74
270
}
75
76
/**
77
 * log_set_max_messages - limit the number of buffered log messages
78
 *
79
 * @max:  The maximum number of messages to keep in the log buffer.
80
 *    Zero means buffering without any limit, a negative value
81
 *    disables buffering altogether.
82
 *
83
 * Messages already exceeding the new limit are dropped.
84
 */
85
void log_set_max_messages(int max)
86
30
{
87
30
  barebox_log_max_messages = max;
88
89
30
  if (max > 0)
90
0
    log_clean(max);
91
30
  else if (max < 0)
92
30
    log_clean(0);
93
30
}
94
95
static void print_colored_log_level(struct console_device *con, const int level)
96
0
{
97
0
  if (!console_allow_color())
98
0
    return;
99
0
  if (level >= ARRAY_SIZE(colored_log_level))
100
0
    return;
101
0
  if (!colored_log_level[level])
102
0
    return;
103
104
0
  console_puts(con, colored_log_level[level]);
105
0
}
106
107
static void pr_puts(int level, const char *str)
108
4.59M
{
109
4.59M
  struct log_entry *log;
110
111
4.59M
  if (IS_ENABLED(CONFIG_LOGBUF) && mem_malloc_is_initialized()) {
112
4.59M
    if (barebox_log_max_messages > 0)
113
270
      log_clean(barebox_log_max_messages - 1);
114
115
4.59M
    if (barebox_log_max_messages >= 0) {
116
270
      int msglen;
117
118
270
      msglen = strlen(str);
119
270
      log = malloc(struct_size(log, msg, msglen + 1));
120
270
      if (!log)
121
0
        goto nolog;
122
123
270
      memcpy(log->msg, str, msglen + 1);
124
125
270
      log->timestamp = get_time_ns();
126
270
      log->level = level;
127
270
      list_add_tail(&log->list, &barebox_logbuf);
128
270
      barebox_logbuf_num_messages++;
129
270
    }
130
4.59M
  }
131
132
4.59M
  pstore_log(str);
133
4.59M
nolog:
134
135
4.59M
  if (level > barebox_loglevel)
136
4.59M
    return;
137
138
0
  print_colored_log_level(CONSOLE_DEV_STDERR, level);
139
0
  console_puts(CONSOLE_DEV_STDERR, str);
140
0
}
141
142
int pr_print(int level, const char *fmt, ...)
143
4.51M
{
144
4.51M
  va_list args;
145
4.51M
  int i;
146
4.51M
  char printbuffer[CFG_PBSIZE];
147
148
4.51M
  if (!IS_ENABLED(CONFIG_LOGBUF) && level > barebox_loglevel)
149
0
    return 0;
150
151
4.51M
  va_start(args, fmt);
152
4.51M
  i = vsnprintf(printbuffer, sizeof(printbuffer), fmt, args);
153
4.51M
  va_end(args);
154
155
4.51M
  pr_puts(level, printbuffer);
156
157
4.51M
  return i;
158
4.51M
}
159
EXPORT_SYMBOL(pr_print);
160
161
int dev_printf(int level, const struct device *dev, const char *format, ...)
162
78.0k
{
163
78.0k
  va_list args;
164
78.0k
  int ret = 0;
165
78.0k
  char printbuffer[CFG_PBSIZE];
166
78.0k
  size_t size = sizeof(printbuffer);
167
168
78.0k
  if (!IS_ENABLED(CONFIG_LOGBUF) && level > barebox_loglevel)
169
0
    return 0;
170
171
78.0k
  if (dev && dev->driver && dev->driver->name)
172
53.5k
    ret += snprintf(printbuffer, size, "%s ", dev->driver->name);
173
174
78.0k
  ret += snprintf(printbuffer + ret, size - ret, "%s: ", dev_name(dev));
175
176
78.0k
  va_start(args, format);
177
178
78.0k
  ret += vsnprintf(printbuffer + ret, size - ret, format, args);
179
180
78.0k
  va_end(args);
181
182
78.0k
  pr_puts(level, printbuffer);
183
184
78.0k
  return ret;
185
78.0k
}
186
EXPORT_SYMBOL(dev_printf);
187
188
#ifdef CONFIG_CONSOLE_ALLOW_COLOR
189
static unsigned int __console_allow_color = 1;
190
#else
191
static unsigned int __console_allow_color = 0;
192
#endif
193
194
bool console_allow_color(void)
195
0
{
196
0
  return __console_allow_color;
197
0
}
198
199
static int console_common_init(void)
200
30
{
201
30
  if (IS_ENABLED(CONFIG_LOGBUF)) {
202
30
    barebox_log_max_messages
203
30
      = clamp(mem_malloc_size() / SZ_32K, 1000UL, 100000UL);
204
30
    globalvar_add_simple_int("log_max_messages",
205
30
        &barebox_log_max_messages, "%d");
206
30
  }
207
208
30
  globalvar_add_simple_bool("allow_color", &__console_allow_color);
209
210
30
  return globalvar_add_simple_int("loglevel", &barebox_loglevel, "%d");
211
30
}
212
core_initcall(console_common_init);
213
214
BAREBOX_MAGICVAR(global.loglevel,
215
     "Console log level: only messages up to this level are printed");
216
BAREBOX_MAGICVAR(global.log_max_messages,
217
     "Maximum number of messages kept in the log buffer");
218
BAREBOX_MAGICVAR(global.allow_color, "Allow colored console output");
219
220
int log_writefile(const char *filepath)
221
0
{
222
0
  int ret = 0, nbytes = 0, fd = -1;
223
0
  struct log_entry *log;
224
225
0
  fd = open(filepath, O_WRONLY | O_CREAT | O_TRUNC, 0644);
226
0
  if (fd < 0)
227
0
    return -errno;
228
229
0
  list_for_each_entry(log, &barebox_logbuf, list) {
230
0
    ret = dputs(fd, log->msg);
231
0
    if (ret < 0)
232
0
      break;
233
0
    nbytes += ret;
234
0
  }
235
236
0
  close(fd);
237
0
  return ret < 0 ? ret : nbytes;
238
0
}
239
240
/**
241
 * log_print - print the log buffer
242
 *
243
 * @con:  Console device to print to
244
 * @flags:  Flags selecting output formatting
245
 * @levels: bitmask of loglevels to print, 0 for all
246
 *
247
 * This function prints the messages of the selected levels; optionally with
248
 * additional information and formatting.
249
 */
250
int log_print(struct console_device *con, unsigned flags, unsigned levels)
251
0
{
252
0
  struct log_entry *log;
253
0
  unsigned long last = 0;
254
255
0
  list_for_each_entry(log, &barebox_logbuf, list) {
256
0
    uint64_t time_ns = log->timestamp;
257
0
    unsigned long time;
258
259
0
    if (levels && !(levels & (1 << log->level)))
260
0
      continue;
261
0
    if (ctrlc_non_interruptible())
262
0
      return -EINTR;
263
264
0
    if (!(flags & (BAREBOX_LOG_PRINT_RAW | BAREBOX_LOG_PRINT_TIME
265
0
             | BAREBOX_LOG_DIFF_TIME)))
266
0
      print_colored_log_level(con, log->level);
267
268
0
    if (flags & BAREBOX_LOG_PRINT_RAW)
269
0
      console_printf(con, "<%i>", log->level);
270
271
    /* convert ns to us */
272
0
    do_div(time_ns, 1000);
273
0
    time = time_ns;
274
275
0
    if (flags & (BAREBOX_LOG_PRINT_TIME | BAREBOX_LOG_DIFF_TIME))
276
0
      console_printf(con, "[");
277
278
0
    if (flags & BAREBOX_LOG_PRINT_TIME)
279
0
      console_printf(con, "%10luus", time);
280
281
0
    if (flags & BAREBOX_LOG_DIFF_TIME) {
282
0
      console_printf(con, " < %10luus", time - last);
283
0
      last = time;
284
0
    }
285
286
0
    if (flags & (BAREBOX_LOG_PRINT_TIME | BAREBOX_LOG_DIFF_TIME))
287
0
      console_printf(con, "] ");
288
289
0
    console_printf(con, "%s", log->msg);
290
0
  }
291
292
0
  return 0;
293
0
}
294
295
int console_printf(struct console_device *con, const char *fmt, ...)
296
0
{
297
0
  va_list args;
298
0
  int i;
299
0
  char printbuffer[CFG_PBSIZE];
300
301
0
  va_start(args, fmt);
302
303
  /*
304
   * For this to work, printbuffer must be larger than
305
   * anything we ever want to print.
306
   */
307
0
  i = vsnprintf(printbuffer, sizeof(printbuffer), fmt, args);
308
0
  va_end(args);
309
310
  /* Print the string */
311
0
  console_puts(con, printbuffer);
312
313
0
  return i;
314
0
}
315
EXPORT_SYMBOL(console_printf);
316
317
int console_vprintf(struct console_device *con, const char *fmt, va_list args)
318
60
{
319
60
  int i;
320
60
  char printbuffer[CFG_PBSIZE];
321
322
  /*
323
   * For this to work, printbuffer must be larger than
324
   * anything we ever want to print.
325
   */
326
60
  i = vsnprintf(printbuffer, sizeof(printbuffer), fmt, args);
327
328
  /* Print the string */
329
60
  console_puts(con, printbuffer);
330
331
60
  return i;
332
60
}
333
EXPORT_SYMBOL(console_vprintf);
334
335
int printf(const char *fmt, ...)
336
60
{
337
60
  va_list args;
338
60
  int i;
339
340
60
  va_start(args, fmt);
341
60
  i = console_vprintf(CONSOLE_DEV_STDOUT, fmt, args);
342
60
  va_end(args);
343
344
60
  return i;
345
60
}
346
EXPORT_SYMBOL(printf);
347
348
int vprintf(const char *fmt, va_list args)
349
0
{
350
0
  return console_vprintf(CONSOLE_DEV_STDOUT, fmt, args);
351
0
}
352
EXPORT_SYMBOL(vprintf);
353
354
struct console_device *console_get_by_dev(struct device *dev)
355
0
{
356
0
  for_each_console(cdev) {
357
0
    if (cdev->dev == dev)
358
0
      return cdev;
359
0
  }
360
361
0
  return NULL;
362
0
}
363
EXPORT_SYMBOL(console_get_by_dev);
364
365
struct console_device *console_get_by_name(const char *name)
366
0
{
367
0
  for_each_console(cdev) {
368
0
    if (cdev->devname && !strcmp(cdev->devname, name))
369
0
      return cdev;
370
0
  }
371
372
0
  return NULL;
373
0
}
374
EXPORT_SYMBOL(console_get_by_name);
375
376
/*
377
 * @brief returns current used console device
378
 *
379
 * @return console device which is registered with CONSOLE_STDIN and
380
 * CONSOLE_STDOUT
381
 */
382
struct console_device *console_get_first_interactive(void)
383
0
{
384
0
  const unsigned char active = CONSOLE_STDIN | CONSOLE_STDOUT;
385
386
  /* if no console input is allows, then we can't have STDIN on any. */
387
0
  if (!IS_ALLOWED(SCONFIG_CONSOLE_INPUT))
388
0
    return NULL;
389
390
  /*
391
   * Assumption to have BOTH CONSOLE_STDIN AND STDOUT in the
392
   * same output console
393
   */
394
0
  for_each_console(cdev) {
395
0
    if ((cdev->f_active & active) == active)
396
0
      return cdev;
397
0
  }
398
399
0
  return NULL;
400
0
}
401
EXPORT_SYMBOL(console_get_first_interactive);
402
403
struct console_device *of_console_get_by_alias(const char *alias)
404
0
{
405
0
  struct device_node *node;
406
0
  struct device *dev;
407
408
0
  node = of_find_node_by_alias(NULL, alias);
409
0
  if (!node)
410
0
    return NULL;
411
412
0
  dev = of_find_device_by_node(node);
413
0
  if (!dev)
414
0
    return NULL;
415
416
0
  return console_get_by_dev(dev);
417
0
}
418
EXPORT_SYMBOL(of_console_get_by_alias);
419
420
#endif /* !CONFIG_CONSOLE_NONE */
421
422
int vdprintf(int file, const char *fmt, va_list args)
423
0
{
424
0
  char printbuffer[CFG_PBSIZE];
425
426
  /*
427
   * For this to work, printbuffer must be larger than
428
   * anything we ever want to print.
429
   */
430
0
  vsnprintf(printbuffer, sizeof(printbuffer), fmt, args);
431
432
  /* Print the string */
433
0
  return dputs(file, printbuffer);
434
0
}
435
EXPORT_SYMBOL(vdprintf);
436
437
int dprintf(int file, const char *fmt, ...)
438
0
{
439
0
  va_list args;
440
0
  int i;
441
442
0
  va_start(args, fmt);
443
0
  i = vdprintf(file, fmt, args);
444
0
  va_end(args);
445
446
0
  return i;
447
0
}
448
EXPORT_SYMBOL(dprintf);
449
450
int dputs(int fd, const char *s)
451
0
{
452
0
  if (fd == 1)
453
0
    return puts(s);
454
0
  else if (fd == 2)
455
0
    return console_puts(CONSOLE_DEV_STDERR, s);
456
0
  else
457
0
    return write(fd, s, strlen(s));
458
0
}
459
EXPORT_SYMBOL(dputs);
460
461
int dputc(int fd, char c)
462
0
{
463
0
  if (fd == 1)
464
0
    putchar(c);
465
0
  else if (fd == 2)
466
0
    return console_putc(CONSOLE_DEV_STDERR, c);
467
0
  else
468
0
    return write(fd, &c, 1);
469
470
0
  return 0;
471
0
}
472
EXPORT_SYMBOL(dputc);