blob: 5e3e20e8a456230980519e1529e5183fe4f1a939 [file] [log] [blame]
Heinrich Schuchardtbefadde2020-02-26 21:48:16 +01001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Log to syslog.
4 *
5 * Copyright (c) 2020, Heinrich Schuchardt <xypron.glpk@gmx.de>
6 */
7
8#include <common.h>
9#include <log.h>
10
11DECLARE_GLOBAL_DATA_PTR;
12
13#define BUFFER_SIZE 480
14
15static void append(char **buf, char *buf_end, const char *fmt, ...)
16{
17 va_list args;
18 size_t size = buf_end - *buf;
19
20 va_start(args, fmt);
21 vsnprintf(*buf, size, fmt, args);
22 va_end(args);
23 *buf += strlen(*buf);
24}
25
26static int log_syslog_emit(struct log_device *ldev, struct log_rec *rec)
27{
28 int ret;
29 int fmt = gd->log_fmt;
30 char msg[BUFFER_SIZE];
31 char *msg_end = msg + BUFFER_SIZE;
32 char *ptr = msg;
33 char *iphdr;
34 char *log_msg;
35 int eth_hdr_size;
36 struct in_addr bcast_ip;
37 static int processing_msg;
38 unsigned int log_level;
39 char *log_hostname;
40
41 /* Fend off messages from the network stack while writing a message */
42 if (processing_msg)
43 return 0;
44
45 processing_msg = 1;
46
47 /* Setup packet buffers */
48 net_init();
49 /* Disable hardware and put it into the reset state */
50 eth_halt();
51 /* Set current device according to environment variables */
52 eth_set_current();
53 /* Get hardware ready for send and receive operations */
54 ret = eth_init();
55 if (ret < 0) {
56 eth_halt();
57 goto out;
58 }
59
60 memset(msg, 0, BUFFER_SIZE);
61
62 /* Set ethernet header */
63 eth_hdr_size = net_set_ether((uchar *)ptr, net_bcast_ethaddr, PROT_IP);
64 ptr += eth_hdr_size;
65 iphdr = ptr;
66 ptr += IP_UDP_HDR_SIZE;
67 log_msg = ptr;
68
69 /*
70 * The syslog log levels defined in RFC 5424 match the U-Boot ones up to
71 * level 7 (debug).
72 */
73 log_level = rec->level;
74 if (log_level > 7)
75 log_level = 7;
76 /* Leave high bits as 0 to write a 'kernel message' */
77
78 /* Write log message to buffer */
79 append(&ptr, msg_end, "<%u>", log_level);
80 log_hostname = env_get("log_hostname");
81 if (log_hostname)
82 append(&ptr, msg_end, "%s ", log_hostname);
83 append(&ptr, msg_end, "uboot: ");
84 if (fmt & (1 << LOGF_LEVEL))
85 append(&ptr, msg_end, "%s.",
86 log_get_level_name(rec->level));
87 if (fmt & (1 << LOGF_CAT))
88 append(&ptr, msg_end, "%s,",
89 log_get_cat_name(rec->cat));
90 if (fmt & (1 << LOGF_FILE))
91 append(&ptr, msg_end, "%s:", rec->file);
92 if (fmt & (1 << LOGF_LINE))
93 append(&ptr, msg_end, "%d-", rec->line);
94 if (fmt & (1 << LOGF_FUNC))
95 append(&ptr, msg_end, "%s()", rec->func);
96 if (fmt & (1 << LOGF_MSG))
97 append(&ptr, msg_end, "%s%s",
98 fmt != (1 << LOGF_MSG) ? " " : "", rec->msg);
99 /* Consider trailing 0x00 */
100 ptr++;
101
102 debug("log message: '%s'\n", log_msg);
103
104 /* Broadcast message */
105 bcast_ip.s_addr = 0xFFFFFFFFL;
106 net_set_udp_header((uchar *)iphdr, bcast_ip, 514, 514, ptr - log_msg);
107 net_send_packet((uchar *)msg, ptr - msg);
108
109out:
110 processing_msg = 0;
111 return ret;
112}
113
114LOG_DRIVER(syslog) = {
115 .name = "syslog",
116 .emit = log_syslog_emit,
117};