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 | |
Heinrich Schuchardt | 2db9516 | 2020-02-26 21:48:16 +0100 | [diff] [blame] | 8 | #include <log.h> |
Simon Glass | 274e0b0 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 9 | #include <net.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 10 | #include <asm/global_data.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; |
Heinrich Schuchardt | 2db9516 | 2020-02-26 21:48:16 +0100 | [diff] [blame] | 38 | unsigned int log_level; |
| 39 | char *log_hostname; |
| 40 | |
Heinrich Schuchardt | 2db9516 | 2020-02-26 21:48:16 +0100 | [diff] [blame] | 41 | /* Setup packet buffers */ |
Sean Anderson | 032e49d | 2020-09-12 17:45:44 -0400 | [diff] [blame] | 42 | ret = net_init(); |
| 43 | if (ret) |
| 44 | return ret; |
Heinrich Schuchardt | 2db9516 | 2020-02-26 21:48:16 +0100 | [diff] [blame] | 45 | /* Disable hardware and put it into the reset state */ |
| 46 | eth_halt(); |
| 47 | /* Set current device according to environment variables */ |
| 48 | eth_set_current(); |
| 49 | /* Get hardware ready for send and receive operations */ |
| 50 | ret = eth_init(); |
| 51 | if (ret < 0) { |
| 52 | eth_halt(); |
| 53 | goto out; |
| 54 | } |
| 55 | |
| 56 | memset(msg, 0, BUFFER_SIZE); |
| 57 | |
| 58 | /* Set ethernet header */ |
| 59 | eth_hdr_size = net_set_ether((uchar *)ptr, net_bcast_ethaddr, PROT_IP); |
| 60 | ptr += eth_hdr_size; |
| 61 | iphdr = ptr; |
| 62 | ptr += IP_UDP_HDR_SIZE; |
| 63 | log_msg = ptr; |
| 64 | |
| 65 | /* |
| 66 | * The syslog log levels defined in RFC 5424 match the U-Boot ones up to |
| 67 | * level 7 (debug). |
| 68 | */ |
| 69 | log_level = rec->level; |
| 70 | if (log_level > 7) |
| 71 | log_level = 7; |
| 72 | /* Leave high bits as 0 to write a 'kernel message' */ |
| 73 | |
| 74 | /* Write log message to buffer */ |
| 75 | append(&ptr, msg_end, "<%u>", log_level); |
| 76 | log_hostname = env_get("log_hostname"); |
| 77 | if (log_hostname) |
| 78 | append(&ptr, msg_end, "%s ", log_hostname); |
| 79 | append(&ptr, msg_end, "uboot: "); |
Heinrich Schuchardt | add83a3 | 2020-06-17 21:52:45 +0200 | [diff] [blame] | 80 | if (fmt & BIT(LOGF_LEVEL)) |
Heinrich Schuchardt | 2db9516 | 2020-02-26 21:48:16 +0100 | [diff] [blame] | 81 | append(&ptr, msg_end, "%s.", |
| 82 | log_get_level_name(rec->level)); |
Heinrich Schuchardt | add83a3 | 2020-06-17 21:52:45 +0200 | [diff] [blame] | 83 | if (fmt & BIT(LOGF_CAT)) |
Heinrich Schuchardt | 2db9516 | 2020-02-26 21:48:16 +0100 | [diff] [blame] | 84 | append(&ptr, msg_end, "%s,", |
| 85 | log_get_cat_name(rec->cat)); |
Heinrich Schuchardt | add83a3 | 2020-06-17 21:52:45 +0200 | [diff] [blame] | 86 | if (fmt & BIT(LOGF_FILE)) |
Heinrich Schuchardt | 2db9516 | 2020-02-26 21:48:16 +0100 | [diff] [blame] | 87 | append(&ptr, msg_end, "%s:", rec->file); |
Heinrich Schuchardt | add83a3 | 2020-06-17 21:52:45 +0200 | [diff] [blame] | 88 | if (fmt & BIT(LOGF_LINE)) |
Heinrich Schuchardt | 2db9516 | 2020-02-26 21:48:16 +0100 | [diff] [blame] | 89 | append(&ptr, msg_end, "%d-", rec->line); |
Heinrich Schuchardt | add83a3 | 2020-06-17 21:52:45 +0200 | [diff] [blame] | 90 | if (fmt & BIT(LOGF_FUNC)) |
Heinrich Schuchardt | 2db9516 | 2020-02-26 21:48:16 +0100 | [diff] [blame] | 91 | append(&ptr, msg_end, "%s()", rec->func); |
Heinrich Schuchardt | add83a3 | 2020-06-17 21:52:45 +0200 | [diff] [blame] | 92 | if (fmt & BIT(LOGF_MSG)) |
Heinrich Schuchardt | 2db9516 | 2020-02-26 21:48:16 +0100 | [diff] [blame] | 93 | append(&ptr, msg_end, "%s%s", |
Heinrich Schuchardt | add83a3 | 2020-06-17 21:52:45 +0200 | [diff] [blame] | 94 | fmt != BIT(LOGF_MSG) ? " " : "", rec->msg); |
Heinrich Schuchardt | 2db9516 | 2020-02-26 21:48:16 +0100 | [diff] [blame] | 95 | /* Consider trailing 0x00 */ |
| 96 | ptr++; |
| 97 | |
| 98 | debug("log message: '%s'\n", log_msg); |
| 99 | |
| 100 | /* Broadcast message */ |
| 101 | bcast_ip.s_addr = 0xFFFFFFFFL; |
| 102 | net_set_udp_header((uchar *)iphdr, bcast_ip, 514, 514, ptr - log_msg); |
| 103 | net_send_packet((uchar *)msg, ptr - msg); |
| 104 | |
| 105 | out: |
Heinrich Schuchardt | 2db9516 | 2020-02-26 21:48:16 +0100 | [diff] [blame] | 106 | return ret; |
| 107 | } |
| 108 | |
| 109 | LOG_DRIVER(syslog) = { |
| 110 | .name = "syslog", |
| 111 | .emit = log_syslog_emit, |
Heinrich Schuchardt | 2db9516 | 2020-02-26 21:48:16 +0100 | [diff] [blame] | 112 | }; |