blob: 57ef48461a537b12228ab830fe27db794d8c51c7 [file] [log] [blame]
wdenk56f94be2002-11-05 16:35:14 +00001/*
2 * (C) Copyright 2002
3 * 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
49#if defined(CONFIG_LOGBUFFER)
50
wdenk56f94be2002-11-05 16:35:14 +000051/* Local prototypes */
52static void logbuff_putc (const char c);
53static void logbuff_puts (const char *s);
54static int logbuff_printk(const char *line);
55
56static char buf[1024];
57
wdenk228f29a2002-12-08 09:53:23 +000058/* This combination will not print messages with the default loglevel */
wdenk56f94be2002-11-05 16:35:14 +000059static unsigned console_loglevel = 3;
60static unsigned default_message_loglevel = 4;
wdenk228f29a2002-12-08 09:53:23 +000061static unsigned char *log_buf = NULL;
62static unsigned long *ext_log_size;
63static unsigned long *ext_log_start;
64static unsigned long *ext_logged_chars;
65#define log_size (*ext_log_size)
wdenk56f94be2002-11-05 16:35:14 +000066#define log_start (*ext_log_start)
67#define logged_chars (*ext_logged_chars)
68
69/* Forced by code, eh! */
70#define LOGBUFF_MAGIC 0xc0de4ced
71
wdenk228f29a2002-12-08 09:53:23 +000072/* The mapping used here has to be the same as in setup_ext_logbuff ()
73 in linux/kernel/printk */
74void logbuff_init_ptrs (void)
75{
76 DECLARE_GLOBAL_DATA_PTR;
wdenkb37c7e52003-06-30 16:24:52 +000077 unsigned long *ext_tag;
wdenk2960b652003-07-15 21:08:26 +000078 unsigned long post_word;
wdenk228f29a2002-12-08 09:53:23 +000079 char *s;
80
81 log_buf = (unsigned char *)(gd->bd->bi_memsize-LOGBUFF_LEN);
wdenkb37c7e52003-06-30 16:24:52 +000082 ext_tag = (unsigned long *)(log_buf)-4;
83 ext_log_start = (unsigned long *)(log_buf)-3;
wdenk228f29a2002-12-08 09:53:23 +000084 ext_log_size = (unsigned long *)(log_buf)-2;
85 ext_logged_chars = (unsigned long *)(log_buf)-1;
wdenk2960b652003-07-15 21:08:26 +000086 post_word = post_word_load();
wdenkd1cbe852003-06-28 17:24:46 +000087#ifdef CONFIG_POST
88 /* The post routines have setup the word so we can simply test it */
wdenk27b207f2003-07-24 23:38:38 +000089 if (post_word_load () & POST_COLDBOOT) {
wdenk228f29a2002-12-08 09:53:23 +000090 logged_chars = log_size = log_start = 0;
wdenkb37c7e52003-06-30 16:24:52 +000091 *ext_tag = LOGBUFF_MAGIC;
wdenk228f29a2002-12-08 09:53:23 +000092 }
wdenkd1cbe852003-06-28 17:24:46 +000093#else
94 /* No post routines, so we do our own checking */
wdenk667122a2003-07-15 21:50:34 +000095 if (post_word != LOGBUFF_MAGIC) {
wdenkd1cbe852003-06-28 17:24:46 +000096 logged_chars = log_size = log_start = 0;
97 post_word_store (LOGBUFF_MAGIC);
wdenkb37c7e52003-06-30 16:24:52 +000098 *ext_tag = LOGBUFF_MAGIC;
wdenkd1cbe852003-06-28 17:24:46 +000099 }
100#endif
wdenk228f29a2002-12-08 09:53:23 +0000101 /* Initialize default loglevel if present */
102 if ((s = getenv ("loglevel")) != NULL)
103 console_loglevel = (int)simple_strtoul (s, NULL, 10);
104
105 gd->post_log_word |= LOGBUFF_INITIALIZED;
106}
107
wdenk56f94be2002-11-05 16:35:14 +0000108int drv_logbuff_init (void)
109{
110 device_t logdev;
111 int rc;
112
113 /* Device initialization */
114 memset (&logdev, 0, sizeof (logdev));
115
116 strcpy (logdev.name, "logbuff");
117 logdev.ext = 0; /* No extensions */
118 logdev.flags = DEV_FLAGS_OUTPUT; /* Output only */
119 logdev.putc = logbuff_putc; /* 'putc' function */
120 logdev.puts = logbuff_puts; /* 'puts' function */
121
122 rc = device_register (&logdev);
123
124 return (rc == 0) ? 1 : rc;
125}
126
127static void logbuff_putc (const char c)
128{
129 char buf[2];
wdenk228f29a2002-12-08 09:53:23 +0000130 buf[0] = c;
131 buf[1] = '\0';
132 logbuff_printk (buf);
wdenk56f94be2002-11-05 16:35:14 +0000133}
134
135static void logbuff_puts (const char *s)
136{
wdenk228f29a2002-12-08 09:53:23 +0000137 logbuff_printk (s);
wdenk56f94be2002-11-05 16:35:14 +0000138}
139
140void logbuff_log(char *msg)
141{
142 DECLARE_GLOBAL_DATA_PTR;
143
wdenk228f29a2002-12-08 09:53:23 +0000144 if ((gd->post_log_word & LOGBUFF_INITIALIZED)) {
145 logbuff_printk (msg);
wdenk56f94be2002-11-05 16:35:14 +0000146 } else {
wdenk228f29a2002-12-08 09:53:23 +0000147 /* Can happen only for pre-relocated errors as logging */
148 /* at that stage should be disabled */
149 puts (msg);
wdenk56f94be2002-11-05 16:35:14 +0000150 }
151}
152
153/*
154 * Subroutine: do_log
155 *
156 * Description: Handler for 'log' command..
157 *
158 * Inputs: argv[1] contains the subcommand
159 *
160 * Return: None
161 *
162 */
163int do_log (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
164{
165 char *s;
166 unsigned long i;
167
wdenk228f29a2002-12-08 09:53:23 +0000168 if (strcmp(argv[1],"append") == 0) {
169 /* Log concatenation of all arguments separated by spaces */
170 for (i=2; i<argc; i++) {
wdenkb37c7e52003-06-30 16:24:52 +0000171 logbuff_printk (argv[i]);
172 logbuff_putc ((i<argc-1) ? ' ' : '\n');
wdenk228f29a2002-12-08 09:53:23 +0000173 }
174 return 0;
wdenk56f94be2002-11-05 16:35:14 +0000175 }
176
177 switch (argc) {
178
179 case 2:
180 if (strcmp(argv[1],"show") == 0) {
wdenk228f29a2002-12-08 09:53:23 +0000181 for (i=0; i < (log_size&LOGBUFF_MASK); i++) {
182 s = log_buf+((log_start+i)&LOGBUFF_MASK);
183 putc (*s);
wdenk56f94be2002-11-05 16:35:14 +0000184 }
185 return 0;
186 } else if (strcmp(argv[1],"reset") == 0) {
wdenk228f29a2002-12-08 09:53:23 +0000187 log_start = 0;
188 log_size = 0;
189 logged_chars = 0;
wdenk56f94be2002-11-05 16:35:14 +0000190 return 0;
wdenk228f29a2002-12-08 09:53:23 +0000191 } else if (strcmp(argv[1],"info") == 0) {
192 printf ("Logbuffer at %08lx\n", (unsigned long)log_buf);
193 printf ("log_start = %08lx\n", log_start);
194 printf ("log_size = %08lx\n", log_size);
195 printf ("logged_chars = %08lx\n", logged_chars);
wdenk56f94be2002-11-05 16:35:14 +0000196 return 0;
wdenk56f94be2002-11-05 16:35:14 +0000197 }
198 printf ("Usage:\n%s\n", cmdtp->usage);
199 return 1;
200
201 default:
202 printf ("Usage:\n%s\n", cmdtp->usage);
203 return 1;
204 }
205}
wdenk8bde7f72003-06-27 21:31:46 +0000206#if defined(CONFIG_LOGBUFFER)
wdenk0d498392003-07-01 21:06:45 +0000207U_BOOT_CMD(
208 log, 255, 1, do_log,
wdenk8bde7f72003-06-27 21:31:46 +0000209 "log - manipulate logbuffer\n",
wdenkb37c7e52003-06-30 16:24:52 +0000210 "info - show pointer details\n"
wdenk8bde7f72003-06-27 21:31:46 +0000211 "log reset - clear contents\n"
212 "log show - show contents\n"
213 "log append <msg> - append <msg> to the logbuffer\n"
214);
215#endif /* CONFIG_LOGBUFFER */
wdenk56f94be2002-11-05 16:35:14 +0000216static int logbuff_printk(const char *line)
217{
218 int i;
219 char *msg, *p, *buf_end;
220 int line_feed;
221 static signed char msg_level = -1;
222
wdenk228f29a2002-12-08 09:53:23 +0000223 strcpy (buf + 3, line);
224 i = strlen (line);
wdenk56f94be2002-11-05 16:35:14 +0000225 buf_end = buf + 3 + i;
226 for (p = buf + 3; p < buf_end; p++) {
227 msg = p;
228 if (msg_level < 0) {
229 if (
230 p[0] != '<' ||
231 p[1] < '0' ||
232 p[1] > '7' ||
233 p[2] != '>'
234 ) {
235 p -= 3;
236 p[0] = '<';
237 p[1] = default_message_loglevel + '0';
238 p[2] = '>';
239 } else
240 msg += 3;
241 msg_level = p[1] - '0';
242 }
243 line_feed = 0;
244 for (; p < buf_end; p++) {
wdenk228f29a2002-12-08 09:53:23 +0000245 log_buf[(log_start+log_size) & LOGBUFF_MASK] = *p;
246 if (log_size < LOGBUFF_LEN)
wdenk56f94be2002-11-05 16:35:14 +0000247 log_size++;
248 else
249 log_start++;
250
251 logged_chars++;
252 if (*p == '\n') {
253 line_feed = 1;
254 break;
255 }
256 }
257 if (msg_level < console_loglevel) {
258 printf("%s", msg);
259 }
260 if (line_feed)
261 msg_level = -1;
262 }
263 return i;
264}
265
266#endif /* (CONFIG_LOGBUFFER) */