blob: fba8bd8bf5c51a3e39ecbaa0fd8ccb3d8f639eec [file] [log] [blame]
wdenk56f94be2002-11-05 16:35:14 +00001/*
Igor Lisitsin2dc64452007-04-18 14:55:19 +04002 * (C) Copyright 2002-2007
wdenk56f94be2002-11-05 16:35:14 +00003 * Detlev Zundel, DENX Software Engineering, dzu@denx.de.
4 *
wdenk228f29a2002-12-08 09:53:23 +00005 * Code used from linux/kernel/printk.c
6 * Copyright (C) 1991, 1992 Linus Torvalds
7 *
wdenk56f94be2002-11-05 16:35:14 +00008 * See file CREDITS for list of people who contributed to this
9 * project.
10 *
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.
15 *
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.
20 *
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,
24 * MA 02111-1307 USA
wdenk228f29a2002-12-08 09:53:23 +000025 *
26 * Comments:
27 *
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.
32 *
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.
wdenk56f94be2002-11-05 16:35:14 +000037 */
38
39/*
40 * Logbuffer handling routines
41 */
42
43#include <common.h>
44#include <command.h>
45#include <devices.h>
wdenk228f29a2002-12-08 09:53:23 +000046#include <post.h>
wdenk56f94be2002-11-05 16:35:14 +000047#include <logbuff.h>
48
Wolfgang Denkd87080b2006-03-31 18:32:53 +020049DECLARE_GLOBAL_DATA_PTR;
50
wdenk56f94be2002-11-05 16:35:14 +000051#if defined(CONFIG_LOGBUFFER)
52
wdenk56f94be2002-11-05 16:35:14 +000053/* Local prototypes */
54static void logbuff_putc (const char c);
55static void logbuff_puts (const char *s);
56static int logbuff_printk(const char *line);
57
58static char buf[1024];
59
wdenk228f29a2002-12-08 09:53:23 +000060/* This combination will not print messages with the default loglevel */
wdenk56f94be2002-11-05 16:35:14 +000061static unsigned console_loglevel = 3;
62static unsigned default_message_loglevel = 4;
Igor Lisitsin2dc64452007-04-18 14:55:19 +040063static unsigned log_version = 1;
64static logbuff_t *log;
wdenk56f94be2002-11-05 16:35:14 +000065
wdenk228f29a2002-12-08 09:53:23 +000066void logbuff_init_ptrs (void)
67{
Igor Lisitsin2dc64452007-04-18 14:55:19 +040068 unsigned long tag, post_word;
wdenk228f29a2002-12-08 09:53:23 +000069 char *s;
70
Igor Lisitsin2dc64452007-04-18 14:55:19 +040071 log = (logbuff_t *)(gd->bd->bi_memsize-LOGBUFF_LEN) - 1;
72
73 /* Set up log version */
74 if ((s = getenv ("logversion")) != NULL)
75 log_version = (int)simple_strtoul (s, NULL, 10);
76
77 if (log_version == 2)
78 tag = log->v2.tag;
79 else
80 tag = log->v1.tag;
wdenk2960b652003-07-15 21:08:26 +000081 post_word = post_word_load();
wdenkd1cbe852003-06-28 17:24:46 +000082#ifdef CONFIG_POST
83 /* The post routines have setup the word so we can simply test it */
Igor Lisitsin2dc64452007-04-18 14:55:19 +040084 if (tag != LOGBUFF_MAGIC || (post_word & POST_COLDBOOT)) {
85 logbuff_reset ();
Wolfgang Denk1636d1c2007-06-22 23:59:00 +020086 }
wdenkd1cbe852003-06-28 17:24:46 +000087#else
88 /* No post routines, so we do our own checking */
Igor Lisitsin2dc64452007-04-18 14:55:19 +040089 if (tag != LOGBUFF_MAGIC || post_word != LOGBUFF_MAGIC) {
90 logbuff_reset ();
wdenkd1cbe852003-06-28 17:24:46 +000091 post_word_store (LOGBUFF_MAGIC);
Wolfgang Denk1636d1c2007-06-22 23:59:00 +020092 }
wdenkd1cbe852003-06-28 17:24:46 +000093#endif
Igor Lisitsin2dc64452007-04-18 14:55:19 +040094 if (log_version == 2 && (long)log->v2.start > (long)log->v2.con)
95 log->v2.start = log->v2.con;
96
wdenk228f29a2002-12-08 09:53:23 +000097 /* Initialize default loglevel if present */
98 if ((s = getenv ("loglevel")) != NULL)
99 console_loglevel = (int)simple_strtoul (s, NULL, 10);
100
101 gd->post_log_word |= LOGBUFF_INITIALIZED;
102}
103
Igor Lisitsin2dc64452007-04-18 14:55:19 +0400104void logbuff_reset (void)
105{
106 memset (log, 0, sizeof (logbuff_t));
107 if (log_version == 2)
108 log->v2.tag = LOGBUFF_MAGIC;
109 else
110 log->v1.tag = LOGBUFF_MAGIC;
111}
112
wdenk56f94be2002-11-05 16:35:14 +0000113int drv_logbuff_init (void)
114{
115 device_t logdev;
116 int rc;
117
118 /* Device initialization */
119 memset (&logdev, 0, sizeof (logdev));
120
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 */
126
127 rc = device_register (&logdev);
128
129 return (rc == 0) ? 1 : rc;
130}
131
132static void logbuff_putc (const char c)
133{
134 char buf[2];
wdenk228f29a2002-12-08 09:53:23 +0000135 buf[0] = c;
136 buf[1] = '\0';
137 logbuff_printk (buf);
wdenk56f94be2002-11-05 16:35:14 +0000138}
139
140static void logbuff_puts (const char *s)
141{
wdenk228f29a2002-12-08 09:53:23 +0000142 logbuff_printk (s);
wdenk56f94be2002-11-05 16:35:14 +0000143}
144
145void logbuff_log(char *msg)
146{
wdenk228f29a2002-12-08 09:53:23 +0000147 if ((gd->post_log_word & LOGBUFF_INITIALIZED)) {
148 logbuff_printk (msg);
wdenk56f94be2002-11-05 16:35:14 +0000149 } else {
wdenk228f29a2002-12-08 09:53:23 +0000150 /* Can happen only for pre-relocated errors as logging */
151 /* at that stage should be disabled */
152 puts (msg);
wdenk56f94be2002-11-05 16:35:14 +0000153 }
154}
155
156/*
157 * Subroutine: do_log
158 *
159 * Description: Handler for 'log' command..
160 *
161 * Inputs: argv[1] contains the subcommand
162 *
163 * Return: None
164 *
165 */
166int do_log (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
167{
168 char *s;
Igor Lisitsin2dc64452007-04-18 14:55:19 +0400169 unsigned long i, start, size;
wdenk56f94be2002-11-05 16:35:14 +0000170
wdenk228f29a2002-12-08 09:53:23 +0000171 if (strcmp(argv[1],"append") == 0) {
172 /* Log concatenation of all arguments separated by spaces */
173 for (i=2; i<argc; i++) {
wdenkb37c7e52003-06-30 16:24:52 +0000174 logbuff_printk (argv[i]);
175 logbuff_putc ((i<argc-1) ? ' ' : '\n');
wdenk228f29a2002-12-08 09:53:23 +0000176 }
177 return 0;
wdenk56f94be2002-11-05 16:35:14 +0000178 }
179
180 switch (argc) {
181
182 case 2:
183 if (strcmp(argv[1],"show") == 0) {
Igor Lisitsin2dc64452007-04-18 14:55:19 +0400184 if (log_version == 2) {
185 start = log->v2.start;
186 size = log->v2.end - log->v2.start;
187 }
188 else {
189 start = log->v1.start;
190 size = log->v1.size;
191 }
192 for (i=0; i < (size&LOGBUFF_MASK); i++) {
193 s = (char *)log->buf+((start+i)&LOGBUFF_MASK);
wdenk228f29a2002-12-08 09:53:23 +0000194 putc (*s);
wdenk56f94be2002-11-05 16:35:14 +0000195 }
196 return 0;
197 } else if (strcmp(argv[1],"reset") == 0) {
Igor Lisitsin2dc64452007-04-18 14:55:19 +0400198 logbuff_reset ();
wdenk56f94be2002-11-05 16:35:14 +0000199 return 0;
wdenk228f29a2002-12-08 09:53:23 +0000200 } else if (strcmp(argv[1],"info") == 0) {
Igor Lisitsin2dc64452007-04-18 14:55:19 +0400201 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);
206 }
207 else {
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);
211 }
wdenk56f94be2002-11-05 16:35:14 +0000212 return 0;
wdenk56f94be2002-11-05 16:35:14 +0000213 }
214 printf ("Usage:\n%s\n", cmdtp->usage);
215 return 1;
216
217 default:
218 printf ("Usage:\n%s\n", cmdtp->usage);
219 return 1;
220 }
221}
Igor Lisitsin2dc64452007-04-18 14:55:19 +0400222
wdenk0d498392003-07-01 21:06:45 +0000223U_BOOT_CMD(
224 log, 255, 1, do_log,
wdenk8bde7f72003-06-27 21:31:46 +0000225 "log - manipulate logbuffer\n",
wdenkb37c7e52003-06-30 16:24:52 +0000226 "info - show pointer details\n"
wdenk8bde7f72003-06-27 21:31:46 +0000227 "log reset - clear contents\n"
228 "log show - show contents\n"
229 "log append <msg> - append <msg> to the logbuffer\n"
230);
Igor Lisitsin2dc64452007-04-18 14:55:19 +0400231
wdenk56f94be2002-11-05 16:35:14 +0000232static int logbuff_printk(const char *line)
233{
234 int i;
235 char *msg, *p, *buf_end;
236 int line_feed;
237 static signed char msg_level = -1;
238
wdenk228f29a2002-12-08 09:53:23 +0000239 strcpy (buf + 3, line);
240 i = strlen (line);
wdenk56f94be2002-11-05 16:35:14 +0000241 buf_end = buf + 3 + i;
242 for (p = buf + 3; p < buf_end; p++) {
243 msg = p;
244 if (msg_level < 0) {
245 if (
246 p[0] != '<' ||
247 p[1] < '0' ||
248 p[1] > '7' ||
249 p[2] != '>'
250 ) {
251 p -= 3;
252 p[0] = '<';
253 p[1] = default_message_loglevel + '0';
254 p[2] = '>';
255 } else
256 msg += 3;
257 msg_level = p[1] - '0';
258 }
259 line_feed = 0;
260 for (; p < buf_end; p++) {
Igor Lisitsin2dc64452007-04-18 14:55:19 +0400261 if (log_version == 2) {
262 log->buf[log->v2.end & LOGBUFF_MASK] = *p;
263 log->v2.end++;
264 if (log->v2.end - log->v2.start > LOGBUFF_LEN)
265 log->v2.start++;
266 log->v2.chars++;
267 }
268 else {
269 log->buf[(log->v1.start + log->v1.size) &
270 LOGBUFF_MASK] = *p;
271 if (log->v1.size < LOGBUFF_LEN)
272 log->v1.size++;
273 else
274 log->v1.start++;
275 log->v1.chars++;
276 }
wdenk56f94be2002-11-05 16:35:14 +0000277 if (*p == '\n') {
278 line_feed = 1;
279 break;
280 }
281 }
282 if (msg_level < console_loglevel) {
283 printf("%s", msg);
284 }
285 if (line_feed)
286 msg_level = -1;
287 }
288 return i;
289}
290
291#endif /* (CONFIG_LOGBUFFER) */