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
49 DECLARE_GLOBAL_DATA_PTR;
51 #if defined(CONFIG_LOGBUFFER)
53 /* Local prototypes */
54 static void logbuff_putc (const char c);
55 static void logbuff_puts (const char *s);
56 static int logbuff_printk(const char *line);
58 static char buf[1024];
60 /* This combination will not print messages with the default loglevel */
61 static unsigned console_loglevel = 3;
62 static unsigned default_message_loglevel = 4;
63 static unsigned log_version = 1;
64 static logbuff_t *log;
66 void logbuff_init_ptrs (void)
68 unsigned long tag, post_word;
71 log = (logbuff_t *)(gd->bd->bi_memsize-LOGBUFF_LEN) - 1;
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)) {
88 /* No post routines, so we do our own checking */
89 if (tag != LOGBUFF_MAGIC || post_word != LOGBUFF_MAGIC) {
91 post_word_store (LOGBUFF_MAGIC);
94 if (log_version == 2 && (long)log->v2.start > (long)log->v2.con)
95 log->v2.start = log->v2.con;
97 /* Initialize default loglevel if present */
98 if ((s = getenv ("loglevel")) != NULL)
99 console_loglevel = (int)simple_strtoul (s, NULL, 10);
101 gd->post_log_word |= LOGBUFF_INITIALIZED;
104 void logbuff_reset (void)
106 memset (log, 0, sizeof (logbuff_t));
107 if (log_version == 2)
108 log->v2.tag = LOGBUFF_MAGIC;
110 log->v1.tag = LOGBUFF_MAGIC;
113 int drv_logbuff_init (void)
118 /* Device initialization */
119 memset (&logdev, 0, sizeof (logdev));
121 strcpy (logdev.name, "logbuff");
122 logdev.ext = 0; /* No extensions */
123 logdev.flags = DEV_FLAGS_OUTPUT; /* Output only */
124 logdev.putc = logbuff_putc; /* 'putc' function */
125 logdev.puts = logbuff_puts; /* 'puts' function */
127 rc = device_register (&logdev);
129 return (rc == 0) ? 1 : rc;
132 static void logbuff_putc (const char c)
137 logbuff_printk (buf);
140 static void logbuff_puts (const char *s)
145 void logbuff_log(char *msg)
147 if ((gd->post_log_word & LOGBUFF_INITIALIZED)) {
148 logbuff_printk (msg);
150 /* Can happen only for pre-relocated errors as logging */
151 /* at that stage should be disabled */
159 * Description: Handler for 'log' command..
161 * Inputs: argv[1] contains the subcommand
166 int do_log (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
169 unsigned long i, start, size;
171 if (strcmp(argv[1],"append") == 0) {
172 /* Log concatenation of all arguments separated by spaces */
173 for (i=2; i<argc; i++) {
174 logbuff_printk (argv[i]);
175 logbuff_putc ((i<argc-1) ? ' ' : '\n');
183 if (strcmp(argv[1],"show") == 0) {
184 if (log_version == 2) {
185 start = log->v2.start;
186 size = log->v2.end - log->v2.start;
189 start = log->v1.start;
192 for (i=0; i < (size&LOGBUFF_MASK); i++) {
193 s = (char *)log->buf+((start+i)&LOGBUFF_MASK);
197 } else if (strcmp(argv[1],"reset") == 0) {
200 } else if (strcmp(argv[1],"info") == 0) {
201 printf ("Logbuffer at %08lx\n", (unsigned long)log->buf);
202 if (log_version == 2) {
203 printf ("log_start = %08lx\n", log->v2.start);
204 printf ("log_end = %08lx\n", log->v2.end);
205 printf ("logged_chars = %08lx\n", log->v2.chars);
208 printf ("log_start = %08lx\n", log->v1.start);
209 printf ("log_size = %08lx\n", log->v1.size);
210 printf ("logged_chars = %08lx\n", log->v1.chars);
214 printf ("Usage:\n%s\n", cmdtp->usage);
218 printf ("Usage:\n%s\n", cmdtp->usage);
225 "log - manipulate logbuffer\n",
226 "info - show pointer details\n"
227 "log reset - clear contents\n"
228 "log show - show contents\n"
229 "log append <msg> - append <msg> to the logbuffer\n"
232 static int logbuff_printk(const char *line)
235 char *msg, *p, *buf_end;
237 static signed char msg_level = -1;
239 strcpy (buf + 3, line);
241 buf_end = buf + 3 + i;
242 for (p = buf + 3; p < buf_end; p++) {
253 p[1] = default_message_loglevel + '0';
257 msg_level = p[1] - '0';
260 for (; p < buf_end; p++) {
261 if (log_version == 2) {
262 log->buf[log->v2.end & LOGBUFF_MASK] = *p;
264 if (log->v2.end - log->v2.start > LOGBUFF_LEN)
269 log->buf[(log->v1.start + log->v1.size) &
271 if (log->v1.size < LOGBUFF_LEN)
282 if (msg_level < console_loglevel) {
291 #endif /* (CONFIG_LOGBUFFER) */