blob: 042a403026f00fa04b5cadd571ee3246b301d522 [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
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;
wdenk228f29a2002-12-08 09:53:23 +000063static unsigned char *log_buf = NULL;
64static unsigned long *ext_log_size;
65static unsigned long *ext_log_start;
66static unsigned long *ext_logged_chars;
67#define log_size (*ext_log_size)
wdenk56f94be2002-11-05 16:35:14 +000068#define log_start (*ext_log_start)
69#define logged_chars (*ext_logged_chars)
70
71/* Forced by code, eh! */
72#define LOGBUFF_MAGIC 0xc0de4ced
73
wdenk228f29a2002-12-08 09:53:23 +000074/* The mapping used here has to be the same as in setup_ext_logbuff ()
75 in linux/kernel/printk */
76void logbuff_init_ptrs (void)
77{
wdenkb37c7e52003-06-30 16:24:52 +000078 unsigned long *ext_tag;
wdenk2960b652003-07-15 21:08:26 +000079 unsigned long post_word;
wdenk228f29a2002-12-08 09:53:23 +000080 char *s;
81
82 log_buf = (unsigned char *)(gd->bd->bi_memsize-LOGBUFF_LEN);
wdenkb37c7e52003-06-30 16:24:52 +000083 ext_tag = (unsigned long *)(log_buf)-4;
84 ext_log_start = (unsigned long *)(log_buf)-3;
wdenk228f29a2002-12-08 09:53:23 +000085 ext_log_size = (unsigned long *)(log_buf)-2;
86 ext_logged_chars = (unsigned long *)(log_buf)-1;
wdenk2960b652003-07-15 21:08:26 +000087 post_word = post_word_load();
wdenkd1cbe852003-06-28 17:24:46 +000088#ifdef CONFIG_POST
89 /* The post routines have setup the word so we can simply test it */
wdenk27b207f2003-07-24 23:38:38 +000090 if (post_word_load () & POST_COLDBOOT) {
wdenk228f29a2002-12-08 09:53:23 +000091 logged_chars = log_size = log_start = 0;
wdenkb37c7e52003-06-30 16:24:52 +000092 *ext_tag = LOGBUFF_MAGIC;
wdenk228f29a2002-12-08 09:53:23 +000093 }
wdenkd1cbe852003-06-28 17:24:46 +000094#else
95 /* No post routines, so we do our own checking */
wdenk667122a2003-07-15 21:50:34 +000096 if (post_word != LOGBUFF_MAGIC) {
wdenkd1cbe852003-06-28 17:24:46 +000097 logged_chars = log_size = log_start = 0;
98 post_word_store (LOGBUFF_MAGIC);
wdenkb37c7e52003-06-30 16:24:52 +000099 *ext_tag = LOGBUFF_MAGIC;
wdenkd1cbe852003-06-28 17:24:46 +0000100 }
101#endif
wdenk228f29a2002-12-08 09:53:23 +0000102 /* Initialize default loglevel if present */
103 if ((s = getenv ("loglevel")) != NULL)
104 console_loglevel = (int)simple_strtoul (s, NULL, 10);
105
106 gd->post_log_word |= LOGBUFF_INITIALIZED;
107}
108
wdenk56f94be2002-11-05 16:35:14 +0000109int drv_logbuff_init (void)
110{
111 device_t logdev;
112 int rc;
113
114 /* Device initialization */
115 memset (&logdev, 0, sizeof (logdev));
116
117 strcpy (logdev.name, "logbuff");
118 logdev.ext = 0; /* No extensions */
119 logdev.flags = DEV_FLAGS_OUTPUT; /* Output only */
120 logdev.putc = logbuff_putc; /* 'putc' function */
121 logdev.puts = logbuff_puts; /* 'puts' function */
122
123 rc = device_register (&logdev);
124
125 return (rc == 0) ? 1 : rc;
126}
127
128static void logbuff_putc (const char c)
129{
130 char buf[2];
wdenk228f29a2002-12-08 09:53:23 +0000131 buf[0] = c;
132 buf[1] = '\0';
133 logbuff_printk (buf);
wdenk56f94be2002-11-05 16:35:14 +0000134}
135
136static void logbuff_puts (const char *s)
137{
wdenk228f29a2002-12-08 09:53:23 +0000138 logbuff_printk (s);
wdenk56f94be2002-11-05 16:35:14 +0000139}
140
141void logbuff_log(char *msg)
142{
wdenk228f29a2002-12-08 09:53:23 +0000143 if ((gd->post_log_word & LOGBUFF_INITIALIZED)) {
144 logbuff_printk (msg);
wdenk56f94be2002-11-05 16:35:14 +0000145 } else {
wdenk228f29a2002-12-08 09:53:23 +0000146 /* Can happen only for pre-relocated errors as logging */
147 /* at that stage should be disabled */
148 puts (msg);
wdenk56f94be2002-11-05 16:35:14 +0000149 }
150}
151
152/*
153 * Subroutine: do_log
154 *
155 * Description: Handler for 'log' command..
156 *
157 * Inputs: argv[1] contains the subcommand
158 *
159 * Return: None
160 *
161 */
162int do_log (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
163{
164 char *s;
165 unsigned long i;
166
wdenk228f29a2002-12-08 09:53:23 +0000167 if (strcmp(argv[1],"append") == 0) {
168 /* Log concatenation of all arguments separated by spaces */
169 for (i=2; i<argc; i++) {
wdenkb37c7e52003-06-30 16:24:52 +0000170 logbuff_printk (argv[i]);
171 logbuff_putc ((i<argc-1) ? ' ' : '\n');
wdenk228f29a2002-12-08 09:53:23 +0000172 }
173 return 0;
wdenk56f94be2002-11-05 16:35:14 +0000174 }
175
176 switch (argc) {
177
178 case 2:
179 if (strcmp(argv[1],"show") == 0) {
wdenk228f29a2002-12-08 09:53:23 +0000180 for (i=0; i < (log_size&LOGBUFF_MASK); i++) {
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200181 s = (char *)log_buf+((log_start+i)&LOGBUFF_MASK);
wdenk228f29a2002-12-08 09:53:23 +0000182 putc (*s);
wdenk56f94be2002-11-05 16:35:14 +0000183 }
184 return 0;
185 } else if (strcmp(argv[1],"reset") == 0) {
wdenk228f29a2002-12-08 09:53:23 +0000186 log_start = 0;
187 log_size = 0;
188 logged_chars = 0;
wdenk56f94be2002-11-05 16:35:14 +0000189 return 0;
wdenk228f29a2002-12-08 09:53:23 +0000190 } else if (strcmp(argv[1],"info") == 0) {
191 printf ("Logbuffer at %08lx\n", (unsigned long)log_buf);
192 printf ("log_start = %08lx\n", log_start);
193 printf ("log_size = %08lx\n", log_size);
194 printf ("logged_chars = %08lx\n", logged_chars);
wdenk56f94be2002-11-05 16:35:14 +0000195 return 0;
wdenk56f94be2002-11-05 16:35:14 +0000196 }
197 printf ("Usage:\n%s\n", cmdtp->usage);
198 return 1;
199
200 default:
201 printf ("Usage:\n%s\n", cmdtp->usage);
202 return 1;
203 }
204}
wdenk8bde7f72003-06-27 21:31:46 +0000205#if defined(CONFIG_LOGBUFFER)
wdenk0d498392003-07-01 21:06:45 +0000206U_BOOT_CMD(
207 log, 255, 1, do_log,
wdenk8bde7f72003-06-27 21:31:46 +0000208 "log - manipulate logbuffer\n",
wdenkb37c7e52003-06-30 16:24:52 +0000209 "info - show pointer details\n"
wdenk8bde7f72003-06-27 21:31:46 +0000210 "log reset - clear contents\n"
211 "log show - show contents\n"
212 "log append <msg> - append <msg> to the logbuffer\n"
213);
214#endif /* CONFIG_LOGBUFFER */
wdenk56f94be2002-11-05 16:35:14 +0000215static int logbuff_printk(const char *line)
216{
217 int i;
218 char *msg, *p, *buf_end;
219 int line_feed;
220 static signed char msg_level = -1;
221
wdenk228f29a2002-12-08 09:53:23 +0000222 strcpy (buf + 3, line);
223 i = strlen (line);
wdenk56f94be2002-11-05 16:35:14 +0000224 buf_end = buf + 3 + i;
225 for (p = buf + 3; p < buf_end; p++) {
226 msg = p;
227 if (msg_level < 0) {
228 if (
229 p[0] != '<' ||
230 p[1] < '0' ||
231 p[1] > '7' ||
232 p[2] != '>'
233 ) {
234 p -= 3;
235 p[0] = '<';
236 p[1] = default_message_loglevel + '0';
237 p[2] = '>';
238 } else
239 msg += 3;
240 msg_level = p[1] - '0';
241 }
242 line_feed = 0;
243 for (; p < buf_end; p++) {
wdenk228f29a2002-12-08 09:53:23 +0000244 log_buf[(log_start+log_size) & LOGBUFF_MASK] = *p;
245 if (log_size < LOGBUFF_LEN)
wdenk56f94be2002-11-05 16:35:14 +0000246 log_size++;
247 else
248 log_start++;
249
250 logged_chars++;
251 if (*p == '\n') {
252 line_feed = 1;
253 break;
254 }
255 }
256 if (msg_level < console_loglevel) {
257 printf("%s", msg);
258 }
259 if (line_feed)
260 msg_level = -1;
261 }
262 return i;
263}
264
265#endif /* (CONFIG_LOGBUFFER) */