blob: 8132570773fc77e4946362497f74e09178330ddc [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;
wdenk228f29a2002-12-08 09:53:23 +000077 char *s;
78
79 log_buf = (unsigned char *)(gd->bd->bi_memsize-LOGBUFF_LEN);
wdenk228f29a2002-12-08 09:53:23 +000080 ext_log_start = (unsigned long *)(log_buf)-3;
81 ext_log_size = (unsigned long *)(log_buf)-2;
82 ext_logged_chars = (unsigned long *)(log_buf)-1;
wdenkd1cbe852003-06-28 17:24:46 +000083#ifdef CONFIG_POST
84 /* The post routines have setup the word so we can simply test it */
85 if (post_word_load () & POST_POWERON) {
wdenk228f29a2002-12-08 09:53:23 +000086 logged_chars = log_size = log_start = 0;
wdenk228f29a2002-12-08 09:53:23 +000087 }
wdenkd1cbe852003-06-28 17:24:46 +000088#else
89 /* No post routines, so we do our own checking */
90 if (post_word_load () != LOGBUFF_MAGIC) {
91 logged_chars = log_size = log_start = 0;
92 post_word_store (LOGBUFF_MAGIC);
93 }
94#endif
wdenk228f29a2002-12-08 09:53:23 +000095 /* Initialize default loglevel if present */
96 if ((s = getenv ("loglevel")) != NULL)
97 console_loglevel = (int)simple_strtoul (s, NULL, 10);
98
99 gd->post_log_word |= LOGBUFF_INITIALIZED;
100}
101
wdenk56f94be2002-11-05 16:35:14 +0000102int drv_logbuff_init (void)
103{
104 device_t logdev;
105 int rc;
106
107 /* Device initialization */
108 memset (&logdev, 0, sizeof (logdev));
109
110 strcpy (logdev.name, "logbuff");
111 logdev.ext = 0; /* No extensions */
112 logdev.flags = DEV_FLAGS_OUTPUT; /* Output only */
113 logdev.putc = logbuff_putc; /* 'putc' function */
114 logdev.puts = logbuff_puts; /* 'puts' function */
115
116 rc = device_register (&logdev);
117
118 return (rc == 0) ? 1 : rc;
119}
120
121static void logbuff_putc (const char c)
122{
123 char buf[2];
wdenk228f29a2002-12-08 09:53:23 +0000124 buf[0] = c;
125 buf[1] = '\0';
126 logbuff_printk (buf);
wdenk56f94be2002-11-05 16:35:14 +0000127}
128
129static void logbuff_puts (const char *s)
130{
wdenk228f29a2002-12-08 09:53:23 +0000131 logbuff_printk (s);
wdenk56f94be2002-11-05 16:35:14 +0000132}
133
134void logbuff_log(char *msg)
135{
136 DECLARE_GLOBAL_DATA_PTR;
137
wdenk228f29a2002-12-08 09:53:23 +0000138 if ((gd->post_log_word & LOGBUFF_INITIALIZED)) {
139 logbuff_printk (msg);
wdenk56f94be2002-11-05 16:35:14 +0000140 } else {
wdenk228f29a2002-12-08 09:53:23 +0000141 /* Can happen only for pre-relocated errors as logging */
142 /* at that stage should be disabled */
143 puts (msg);
wdenk56f94be2002-11-05 16:35:14 +0000144 }
145}
146
147/*
148 * Subroutine: do_log
149 *
150 * Description: Handler for 'log' command..
151 *
152 * Inputs: argv[1] contains the subcommand
153 *
154 * Return: None
155 *
156 */
157int do_log (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
158{
159 char *s;
160 unsigned long i;
161
wdenk228f29a2002-12-08 09:53:23 +0000162 if (strcmp(argv[1],"append") == 0) {
163 /* Log concatenation of all arguments separated by spaces */
164 for (i=2; i<argc; i++) {
165 if (i<argc-1) {
166 logbuff_printk (argv[i]);
167 logbuff_putc (' ');
168 } else {
169 logbuff_puts (argv[i]);
170 }
171 }
172 return 0;
wdenk56f94be2002-11-05 16:35:14 +0000173 }
174
175 switch (argc) {
176
177 case 2:
178 if (strcmp(argv[1],"show") == 0) {
wdenk228f29a2002-12-08 09:53:23 +0000179 for (i=0; i < (log_size&LOGBUFF_MASK); i++) {
180 s = log_buf+((log_start+i)&LOGBUFF_MASK);
181 putc (*s);
wdenk56f94be2002-11-05 16:35:14 +0000182 }
183 return 0;
184 } else if (strcmp(argv[1],"reset") == 0) {
wdenk228f29a2002-12-08 09:53:23 +0000185 log_start = 0;
186 log_size = 0;
187 logged_chars = 0;
wdenk56f94be2002-11-05 16:35:14 +0000188 return 0;
wdenk228f29a2002-12-08 09:53:23 +0000189 } else if (strcmp(argv[1],"info") == 0) {
190 printf ("Logbuffer at %08lx\n", (unsigned long)log_buf);
191 printf ("log_start = %08lx\n", log_start);
192 printf ("log_size = %08lx\n", log_size);
193 printf ("logged_chars = %08lx\n", logged_chars);
wdenk56f94be2002-11-05 16:35:14 +0000194 return 0;
wdenk56f94be2002-11-05 16:35:14 +0000195 }
196 printf ("Usage:\n%s\n", cmdtp->usage);
197 return 1;
198
199 default:
200 printf ("Usage:\n%s\n", cmdtp->usage);
201 return 1;
202 }
203}
wdenk8bde7f72003-06-27 21:31:46 +0000204#if defined(CONFIG_LOGBUFFER)
205cmd_tbl_t U_BOOT_CMD(LOG) = MK_CMD_ENTRY(
206 "log", 255, 1, do_log,
207 "log - manipulate logbuffer\n",
208 "log info - show pointer details\n"
209 "log reset - clear contents\n"
210 "log show - show contents\n"
211 "log append <msg> - append <msg> to the logbuffer\n"
212);
213#endif /* CONFIG_LOGBUFFER */
wdenk56f94be2002-11-05 16:35:14 +0000214static int logbuff_printk(const char *line)
215{
216 int i;
217 char *msg, *p, *buf_end;
218 int line_feed;
219 static signed char msg_level = -1;
220
wdenk228f29a2002-12-08 09:53:23 +0000221 strcpy (buf + 3, line);
222 i = strlen (line);
wdenk56f94be2002-11-05 16:35:14 +0000223 buf_end = buf + 3 + i;
224 for (p = buf + 3; p < buf_end; p++) {
225 msg = p;
226 if (msg_level < 0) {
227 if (
228 p[0] != '<' ||
229 p[1] < '0' ||
230 p[1] > '7' ||
231 p[2] != '>'
232 ) {
233 p -= 3;
234 p[0] = '<';
235 p[1] = default_message_loglevel + '0';
236 p[2] = '>';
237 } else
238 msg += 3;
239 msg_level = p[1] - '0';
240 }
241 line_feed = 0;
242 for (; p < buf_end; p++) {
wdenk228f29a2002-12-08 09:53:23 +0000243 log_buf[(log_start+log_size) & LOGBUFF_MASK] = *p;
244 if (log_size < LOGBUFF_LEN)
wdenk56f94be2002-11-05 16:35:14 +0000245 log_size++;
246 else
247 log_start++;
248
249 logged_chars++;
250 if (*p == '\n') {
251 line_feed = 1;
252 break;
253 }
254 }
255 if (msg_level < console_loglevel) {
256 printf("%s", msg);
257 }
258 if (line_feed)
259 msg_level = -1;
260 }
261 return i;
262}
263
264#endif /* (CONFIG_LOGBUFFER) */