2 * (C) Copyright 2002-2007
3 * Detlev Zundel, DENX Software Engineering, dzu@denx.de.
5 * Code used from linux/kernel/printk.c
6 * Copyright (C) 1991, 1992 Linus Torvalds
8 * SPDX-License-Identifier: GPL-2.0+
12 * After relocating the code, the environment variable "loglevel" is
13 * copied to console_loglevel. The functionality is similar to the
14 * handling in the Linux kernel, i.e. messages logged with a priority
15 * less than console_loglevel are also output to stdout.
17 * If you want messages with the default level (e.g. POST messages) to
18 * appear on stdout also, make sure the environment variable
19 * "loglevel" is set at boot time to a number higher than
20 * default_message_loglevel below.
24 * Logbuffer handling routines
29 #include <stdio_dev.h>
33 DECLARE_GLOBAL_DATA_PTR;
35 /* Local prototypes */
36 static void logbuff_putc(struct stdio_dev *dev, const char c);
37 static void logbuff_puts(struct stdio_dev *dev, const char *s);
38 static int logbuff_printk(const char *line);
40 static char buf[1024];
42 /* This combination will not print messages with the default loglevel */
43 static unsigned console_loglevel = 3;
44 static unsigned default_message_loglevel = 4;
45 static unsigned log_version = 1;
46 #ifdef CONFIG_ALT_LB_ADDR
47 static volatile logbuff_t *log;
49 static logbuff_t *log;
53 unsigned long __logbuffer_base(void)
55 return CONFIG_SYS_SDRAM_BASE + get_effective_memsize() - LOGBUFF_LEN;
57 unsigned long logbuffer_base(void)
58 __attribute__((weak, alias("__logbuffer_base")));
60 void logbuff_init_ptrs(void)
62 unsigned long tag, post_word;
65 #ifdef CONFIG_ALT_LB_ADDR
66 log = (logbuff_t *)CONFIG_ALT_LH_ADDR;
67 lbuf = (char *)CONFIG_ALT_LB_ADDR;
69 log = (logbuff_t *)(logbuffer_base()) - 1;
70 lbuf = (char *)log->buf;
73 /* Set up log version */
74 if ((s = getenv ("logversion")) != NULL)
75 log_version = (int)simple_strtoul(s, NULL, 10);
81 post_word = post_word_load();
83 /* The post routines have setup the word so we can simply test it */
84 if (tag != LOGBUFF_MAGIC || (post_word & POST_COLDBOOT))
87 /* No post routines, so we do our own checking */
88 if (tag != LOGBUFF_MAGIC || post_word != LOGBUFF_MAGIC) {
90 post_word_store (LOGBUFF_MAGIC);
93 if (log_version == 2 && (long)log->v2.start > (long)log->v2.con)
94 log->v2.start = log->v2.con;
96 /* Initialize default loglevel if present */
97 if ((s = getenv ("loglevel")) != NULL)
98 console_loglevel = (int)simple_strtoul(s, NULL, 10);
100 gd->flags |= GD_FLG_LOGINIT;
103 void logbuff_reset(void)
105 #ifndef CONFIG_ALT_LB_ADDR
106 memset(log, 0, sizeof(logbuff_t));
108 if (log_version == 2) {
109 log->v2.tag = LOGBUFF_MAGIC;
110 #ifdef CONFIG_ALT_LB_ADDR
117 log->v1.tag = LOGBUFF_MAGIC;
118 #ifdef CONFIG_ALT_LB_ADDR
127 int drv_logbuff_init(void)
129 struct stdio_dev logdev;
132 /* Device initialization */
133 memset (&logdev, 0, sizeof (logdev));
135 strcpy (logdev.name, "logbuff");
136 logdev.ext = 0; /* No extensions */
137 logdev.flags = DEV_FLAGS_OUTPUT; /* Output only */
138 logdev.putc = logbuff_putc; /* 'putc' function */
139 logdev.puts = logbuff_puts; /* 'puts' function */
141 rc = stdio_register(&logdev);
143 return (rc == 0) ? 1 : rc;
146 static void logbuff_putc(struct stdio_dev *dev, const char c)
154 static void logbuff_puts(struct stdio_dev *dev, const char *s)
159 void logbuff_log(char *msg)
161 if ((gd->flags & GD_FLG_LOGINIT)) {
165 * Can happen only for pre-relocated errors as logging
166 * at that stage should be disabled
175 * Description: Handler for 'log' command..
177 * Inputs: argv[1] contains the subcommand
182 int do_log(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
184 struct stdio_dev *sdev = NULL;
186 unsigned long i, start, size;
188 if (strcmp(argv[1], "append") == 0) {
189 /* Log concatenation of all arguments separated by spaces */
190 for (i = 2; i < argc; i++) {
191 logbuff_printk(argv[i]);
192 logbuff_putc(sdev, (i < argc - 1) ? ' ' : '\n');
200 if (strcmp(argv[1], "show") == 0) {
201 if (log_version == 2) {
202 start = log->v2.start;
203 size = log->v2.end - log->v2.start;
205 start = log->v1.start;
208 if (size > LOGBUFF_LEN)
210 for (i = 0; i < size; i++) {
211 s = lbuf + ((start + i) & LOGBUFF_MASK);
215 } else if (strcmp(argv[1], "reset") == 0) {
218 } else if (strcmp(argv[1], "info") == 0) {
219 printf("Logbuffer at %08lx\n", (unsigned long)lbuf);
220 if (log_version == 2) {
221 printf("log_start = %08lx\n",
223 printf("log_end = %08lx\n", log->v2.end);
224 printf("log_con = %08lx\n", log->v2.con);
225 printf("logged_chars = %08lx\n",
229 printf("log_start = %08lx\n",
231 printf("log_size = %08lx\n",
233 printf("logged_chars = %08lx\n",
238 return CMD_RET_USAGE;
241 return CMD_RET_USAGE;
247 "manipulate logbuffer",
248 "info - show pointer details\n"
249 "log reset - clear contents\n"
250 "log show - show contents\n"
251 "log append <msg> - append <msg> to the logbuffer"
254 static int logbuff_printk(const char *line)
257 char *msg, *p, *buf_end;
259 static signed char msg_level = -1;
261 strcpy(buf + 3, line);
263 buf_end = buf + 3 + i;
264 for (p = buf + 3; p < buf_end; p++) {
275 p[1] = default_message_loglevel + '0';
280 msg_level = p[1] - '0';
283 for (; p < buf_end; p++) {
284 if (log_version == 2) {
285 lbuf[log->v2.end & LOGBUFF_MASK] = *p;
287 if (log->v2.end - log->v2.start > LOGBUFF_LEN)
291 lbuf[(log->v1.start + log->v1.size) &
293 if (log->v1.size < LOGBUFF_LEN)
304 if (msg_level < console_loglevel) {