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