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 * See file CREDITS for list of people who contributed to this
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; either version 2 of
14 * the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
28 * After relocating the code, the environment variable "loglevel" is
29 * copied to console_loglevel. The functionality is similar to the
30 * handling in the Linux kernel, i.e. messages logged with a priority
31 * less than console_loglevel are also output to stdout.
33 * If you want messages with the default level (e.g. POST messages) to
34 * appear on stdout also, make sure the environment variable
35 * "loglevel" is set at boot time to a number higher than
36 * default_message_loglevel below.
40 * Logbuffer handling routines
45 #include <stdio_dev.h>
49 DECLARE_GLOBAL_DATA_PTR;
51 /* Local prototypes */
52 static void logbuff_putc(const char c);
53 static void logbuff_puts(const char *s);
54 static int logbuff_printk(const char *line);
56 static char buf[1024];
58 /* This combination will not print messages with the default loglevel */
59 static unsigned console_loglevel = 3;
60 static unsigned default_message_loglevel = 4;
61 static unsigned log_version = 1;
62 #ifdef CONFIG_ALT_LB_ADDR
63 static volatile logbuff_t *log;
65 static logbuff_t *log;
69 unsigned long __logbuffer_base(void)
71 return CONFIG_SYS_SDRAM_BASE + gd->ram_size - LOGBUFF_LEN;
73 unsigned long logbuffer_base(void)
74 __attribute__((weak, alias("__logbuffer_base")));
76 void logbuff_init_ptrs(void)
78 unsigned long tag, post_word;
81 #ifdef CONFIG_ALT_LB_ADDR
82 log = (logbuff_t *)CONFIG_ALT_LH_ADDR;
83 lbuf = (char *)CONFIG_ALT_LB_ADDR;
85 log = (logbuff_t *)(logbuffer_base()) - 1;
86 lbuf = (char *)log->buf;
89 /* Set up log version */
90 if ((s = getenv ("logversion")) != NULL)
91 log_version = (int)simple_strtoul(s, NULL, 10);
97 post_word = post_word_load();
99 /* The post routines have setup the word so we can simply test it */
100 if (tag != LOGBUFF_MAGIC || (post_word & POST_COLDBOOT))
103 /* No post routines, so we do our own checking */
104 if (tag != LOGBUFF_MAGIC || post_word != LOGBUFF_MAGIC) {
106 post_word_store (LOGBUFF_MAGIC);
109 if (log_version == 2 && (long)log->v2.start > (long)log->v2.con)
110 log->v2.start = log->v2.con;
112 /* Initialize default loglevel if present */
113 if ((s = getenv ("loglevel")) != NULL)
114 console_loglevel = (int)simple_strtoul(s, NULL, 10);
116 gd->flags |= GD_FLG_LOGINIT;
119 void logbuff_reset(void)
121 #ifndef CONFIG_ALT_LB_ADDR
122 memset(log, 0, sizeof(logbuff_t));
124 if (log_version == 2) {
125 log->v2.tag = LOGBUFF_MAGIC;
126 #ifdef CONFIG_ALT_LB_ADDR
133 log->v1.tag = LOGBUFF_MAGIC;
134 #ifdef CONFIG_ALT_LB_ADDR
143 int drv_logbuff_init(void)
145 struct stdio_dev logdev;
148 /* Device initialization */
149 memset (&logdev, 0, sizeof (logdev));
151 strcpy (logdev.name, "logbuff");
152 logdev.ext = 0; /* No extensions */
153 logdev.flags = DEV_FLAGS_OUTPUT; /* Output only */
154 logdev.putc = logbuff_putc; /* 'putc' function */
155 logdev.puts = logbuff_puts; /* 'puts' function */
157 rc = stdio_register(&logdev);
159 return (rc == 0) ? 1 : rc;
162 static void logbuff_putc(const char c)
170 static void logbuff_puts(const char *s)
175 void logbuff_log(char *msg)
177 if ((gd->flags & GD_FLG_LOGINIT)) {
181 * Can happen only for pre-relocated errors as logging
182 * at that stage should be disabled
191 * Description: Handler for 'log' command..
193 * Inputs: argv[1] contains the subcommand
198 int do_log(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
201 unsigned long i, start, size;
203 if (strcmp(argv[1], "append") == 0) {
204 /* Log concatenation of all arguments separated by spaces */
205 for (i = 2; i < argc; i++) {
206 logbuff_printk(argv[i]);
207 logbuff_putc((i < argc - 1) ? ' ' : '\n');
215 if (strcmp(argv[1], "show") == 0) {
216 if (log_version == 2) {
217 start = log->v2.start;
218 size = log->v2.end - log->v2.start;
220 start = log->v1.start;
223 if (size > LOGBUFF_LEN)
225 for (i = 0; i < size; i++) {
226 s = lbuf + ((start + i) & LOGBUFF_MASK);
230 } else if (strcmp(argv[1], "reset") == 0) {
233 } else if (strcmp(argv[1], "info") == 0) {
234 printf("Logbuffer at %08lx\n", (unsigned long)lbuf);
235 if (log_version == 2) {
236 printf("log_start = %08lx\n",
238 printf("log_end = %08lx\n", log->v2.end);
239 printf("log_con = %08lx\n", log->v2.con);
240 printf("logged_chars = %08lx\n",
244 printf("log_start = %08lx\n",
246 printf("log_size = %08lx\n",
248 printf("logged_chars = %08lx\n",
253 return CMD_RET_USAGE;
256 return CMD_RET_USAGE;
262 "manipulate logbuffer",
263 "info - show pointer details\n"
264 "log reset - clear contents\n"
265 "log show - show contents\n"
266 "log append <msg> - append <msg> to the logbuffer"
269 static int logbuff_printk(const char *line)
272 char *msg, *p, *buf_end;
274 static signed char msg_level = -1;
276 strcpy(buf + 3, line);
278 buf_end = buf + 3 + i;
279 for (p = buf + 3; p < buf_end; p++) {
290 p[1] = default_message_loglevel + '0';
295 msg_level = p[1] - '0';
298 for (; p < buf_end; p++) {
299 if (log_version == 2) {
300 lbuf[log->v2.end & LOGBUFF_MASK] = *p;
302 if (log->v2.end - log->v2.start > LOGBUFF_LEN)
306 lbuf[(log->v1.start + log->v1.size) &
308 if (log->v1.size < LOGBUFF_LEN)
319 if (msg_level < console_loglevel) {