blob: c99783704fb87576622c83b6b36b32eb743e5fa3 [file] [log] [blame]
wdenk56f94be2002-11-05 16:35:14 +00001/*
Igor Lisitsin93470f22007-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 *
wdenk9dfa8d12002-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
wdenk9dfa8d12002-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>
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +020045#include <stdio_dev.h>
wdenk9dfa8d12002-12-08 09:53:23 +000046#include <post.h>
wdenk56f94be2002-11-05 16:35:14 +000047#include <logbuff.h>
48
Wolfgang Denk6405a152006-03-31 18:32:53 +020049DECLARE_GLOBAL_DATA_PTR;
50
wdenk56f94be2002-11-05 16:35:14 +000051/* Local prototypes */
Heiko Schocher31020122012-02-14 22:21:06 +000052static void logbuff_putc(const char c);
53static void logbuff_puts(const char *s);
wdenk56f94be2002-11-05 16:35:14 +000054static int logbuff_printk(const char *line);
55
56static char buf[1024];
57
wdenk9dfa8d12002-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;
Igor Lisitsin93470f22007-04-18 14:55:19 +040061static unsigned log_version = 1;
Yuri Tikhonovfe1d91b2008-02-06 18:48:36 +010062#ifdef CONFIG_ALT_LB_ADDR
63static volatile logbuff_t *log;
64#else
Igor Lisitsin93470f22007-04-18 14:55:19 +040065static logbuff_t *log;
Yuri Tikhonovfe1d91b2008-02-06 18:48:36 +010066#endif
67static char *lbuf;
wdenk56f94be2002-11-05 16:35:14 +000068
Marian Balakowicz902ce702008-05-13 15:53:29 +020069unsigned long __logbuffer_base(void)
70{
Heiko Schocher9026d682011-10-12 01:13:38 +000071 return CONFIG_SYS_SDRAM_BASE + gd->ram_size - LOGBUFF_LEN;
Marian Balakowicz902ce702008-05-13 15:53:29 +020072}
Heiko Schocher31020122012-02-14 22:21:06 +000073unsigned long logbuffer_base(void)
74__attribute__((weak, alias("__logbuffer_base")));
Marian Balakowicz902ce702008-05-13 15:53:29 +020075
Heiko Schocher31020122012-02-14 22:21:06 +000076void logbuff_init_ptrs(void)
wdenk9dfa8d12002-12-08 09:53:23 +000077{
Igor Lisitsin93470f22007-04-18 14:55:19 +040078 unsigned long tag, post_word;
wdenk9dfa8d12002-12-08 09:53:23 +000079 char *s;
80
Yuri Tikhonovfe1d91b2008-02-06 18:48:36 +010081#ifdef CONFIG_ALT_LB_ADDR
82 log = (logbuff_t *)CONFIG_ALT_LH_ADDR;
83 lbuf = (char *)CONFIG_ALT_LB_ADDR;
84#else
Heiko Schocher31020122012-02-14 22:21:06 +000085 log = (logbuff_t *)(logbuffer_base()) - 1;
Jean-Christophe PLAGNIOL-VILLARD531ad562008-04-02 08:03:57 +020086 lbuf = (char *)log->buf;
Yuri Tikhonovfe1d91b2008-02-06 18:48:36 +010087#endif
Igor Lisitsin93470f22007-04-18 14:55:19 +040088
89 /* Set up log version */
90 if ((s = getenv ("logversion")) != NULL)
Heiko Schocher31020122012-02-14 22:21:06 +000091 log_version = (int)simple_strtoul(s, NULL, 10);
Igor Lisitsin93470f22007-04-18 14:55:19 +040092
93 if (log_version == 2)
94 tag = log->v2.tag;
95 else
96 tag = log->v1.tag;
wdenk41bea112003-07-15 21:08:26 +000097 post_word = post_word_load();
wdenkd9fce812003-06-28 17:24:46 +000098#ifdef CONFIG_POST
99 /* The post routines have setup the word so we can simply test it */
Heiko Schocher31020122012-02-14 22:21:06 +0000100 if (tag != LOGBUFF_MAGIC || (post_word & POST_COLDBOOT))
101 logbuff_reset();
wdenkd9fce812003-06-28 17:24:46 +0000102#else
103 /* No post routines, so we do our own checking */
Igor Lisitsin93470f22007-04-18 14:55:19 +0400104 if (tag != LOGBUFF_MAGIC || post_word != LOGBUFF_MAGIC) {
105 logbuff_reset ();
wdenkd9fce812003-06-28 17:24:46 +0000106 post_word_store (LOGBUFF_MAGIC);
Wolfgang Denk70df7bc2007-06-22 23:59:00 +0200107 }
wdenkd9fce812003-06-28 17:24:46 +0000108#endif
Igor Lisitsin93470f22007-04-18 14:55:19 +0400109 if (log_version == 2 && (long)log->v2.start > (long)log->v2.con)
110 log->v2.start = log->v2.con;
111
wdenk9dfa8d12002-12-08 09:53:23 +0000112 /* Initialize default loglevel if present */
113 if ((s = getenv ("loglevel")) != NULL)
Heiko Schocher31020122012-02-14 22:21:06 +0000114 console_loglevel = (int)simple_strtoul(s, NULL, 10);
wdenk9dfa8d12002-12-08 09:53:23 +0000115
Yuri Tikhonov35d81ce2008-05-08 15:46:42 +0200116 gd->flags |= GD_FLG_LOGINIT;
wdenk9dfa8d12002-12-08 09:53:23 +0000117}
118
Heiko Schocher31020122012-02-14 22:21:06 +0000119void logbuff_reset(void)
Igor Lisitsin93470f22007-04-18 14:55:19 +0400120{
Yuri Tikhonovfe1d91b2008-02-06 18:48:36 +0100121#ifndef CONFIG_ALT_LB_ADDR
Heiko Schocher31020122012-02-14 22:21:06 +0000122 memset(log, 0, sizeof(logbuff_t));
Yuri Tikhonovfe1d91b2008-02-06 18:48:36 +0100123#endif
124 if (log_version == 2) {
Igor Lisitsin93470f22007-04-18 14:55:19 +0400125 log->v2.tag = LOGBUFF_MAGIC;
Yuri Tikhonovfe1d91b2008-02-06 18:48:36 +0100126#ifdef CONFIG_ALT_LB_ADDR
127 log->v2.start = 0;
128 log->v2.con = 0;
129 log->v2.end = 0;
130 log->v2.chars = 0;
131#endif
132 } else {
Igor Lisitsin93470f22007-04-18 14:55:19 +0400133 log->v1.tag = LOGBUFF_MAGIC;
Yuri Tikhonovfe1d91b2008-02-06 18:48:36 +0100134#ifdef CONFIG_ALT_LB_ADDR
135 log->v1.dummy = 0;
136 log->v1.start = 0;
137 log->v1.size = 0;
138 log->v1.chars = 0;
139#endif
140 }
Igor Lisitsin93470f22007-04-18 14:55:19 +0400141}
142
Heiko Schocher31020122012-02-14 22:21:06 +0000143int drv_logbuff_init(void)
wdenk56f94be2002-11-05 16:35:14 +0000144{
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +0200145 struct stdio_dev logdev;
wdenk56f94be2002-11-05 16:35:14 +0000146 int rc;
147
148 /* Device initialization */
149 memset (&logdev, 0, sizeof (logdev));
150
151 strcpy (logdev.name, "logbuff");
152 logdev.ext = 0; /* No extensions */
153 logdev.flags = DEV_FLAGS_OUTPUT; /* Output only */
154 logdev.putc = logbuff_putc; /* 'putc' function */
155 logdev.puts = logbuff_puts; /* 'puts' function */
156
Heiko Schocher31020122012-02-14 22:21:06 +0000157 rc = stdio_register(&logdev);
wdenk56f94be2002-11-05 16:35:14 +0000158
159 return (rc == 0) ? 1 : rc;
160}
161
Heiko Schocher31020122012-02-14 22:21:06 +0000162static void logbuff_putc(const char c)
wdenk56f94be2002-11-05 16:35:14 +0000163{
164 char buf[2];
wdenk9dfa8d12002-12-08 09:53:23 +0000165 buf[0] = c;
166 buf[1] = '\0';
Heiko Schocher31020122012-02-14 22:21:06 +0000167 logbuff_printk(buf);
wdenk56f94be2002-11-05 16:35:14 +0000168}
169
Heiko Schocher31020122012-02-14 22:21:06 +0000170static void logbuff_puts(const char *s)
wdenk56f94be2002-11-05 16:35:14 +0000171{
wdenk9dfa8d12002-12-08 09:53:23 +0000172 logbuff_printk (s);
wdenk56f94be2002-11-05 16:35:14 +0000173}
174
175void logbuff_log(char *msg)
176{
Yuri Tikhonov35d81ce2008-05-08 15:46:42 +0200177 if ((gd->flags & GD_FLG_LOGINIT)) {
Heiko Schocher31020122012-02-14 22:21:06 +0000178 logbuff_printk(msg);
wdenk56f94be2002-11-05 16:35:14 +0000179 } else {
Heiko Schocher31020122012-02-14 22:21:06 +0000180 /*
181 * Can happen only for pre-relocated errors as logging
182 * at that stage should be disabled
183 */
wdenk9dfa8d12002-12-08 09:53:23 +0000184 puts (msg);
wdenk56f94be2002-11-05 16:35:14 +0000185 }
186}
187
wdenk56f94be2002-11-05 16:35:14 +0000188/*
189 * Subroutine: do_log
190 *
191 * Description: Handler for 'log' command..
192 *
193 * Inputs: argv[1] contains the subcommand
194 *
195 * Return: None
196 *
197 */
Heiko Schocher31020122012-02-14 22:21:06 +0000198int do_log(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk56f94be2002-11-05 16:35:14 +0000199{
200 char *s;
Igor Lisitsin93470f22007-04-18 14:55:19 +0400201 unsigned long i, start, size;
wdenk56f94be2002-11-05 16:35:14 +0000202
Heiko Schocher31020122012-02-14 22:21:06 +0000203 if (strcmp(argv[1], "append") == 0) {
wdenk9dfa8d12002-12-08 09:53:23 +0000204 /* Log concatenation of all arguments separated by spaces */
Heiko Schocher31020122012-02-14 22:21:06 +0000205 for (i = 2; i < argc; i++) {
206 logbuff_printk(argv[i]);
207 logbuff_putc((i < argc - 1) ? ' ' : '\n');
wdenk9dfa8d12002-12-08 09:53:23 +0000208 }
209 return 0;
wdenk56f94be2002-11-05 16:35:14 +0000210 }
211
212 switch (argc) {
213
214 case 2:
Heiko Schocher31020122012-02-14 22:21:06 +0000215 if (strcmp(argv[1], "show") == 0) {
Igor Lisitsin93470f22007-04-18 14:55:19 +0400216 if (log_version == 2) {
217 start = log->v2.start;
218 size = log->v2.end - log->v2.start;
Heiko Schocher31020122012-02-14 22:21:06 +0000219 } else {
Igor Lisitsin93470f22007-04-18 14:55:19 +0400220 start = log->v1.start;
221 size = log->v1.size;
222 }
Heiko Schochere5f78692012-02-16 01:02:21 +0000223 if (size > LOGBUFF_LEN)
224 size = LOGBUFF_LEN;
225 for (i = 0; i < size; i++) {
Heiko Schocher31020122012-02-14 22:21:06 +0000226 s = lbuf + ((start + i) & LOGBUFF_MASK);
227 putc(*s);
wdenk56f94be2002-11-05 16:35:14 +0000228 }
229 return 0;
Heiko Schocher31020122012-02-14 22:21:06 +0000230 } else if (strcmp(argv[1], "reset") == 0) {
231 logbuff_reset();
wdenk56f94be2002-11-05 16:35:14 +0000232 return 0;
Heiko Schocher31020122012-02-14 22:21:06 +0000233 } else if (strcmp(argv[1], "info") == 0) {
234 printf("Logbuffer at %08lx\n", (unsigned long)lbuf);
Igor Lisitsin93470f22007-04-18 14:55:19 +0400235 if (log_version == 2) {
Heiko Schocher31020122012-02-14 22:21:06 +0000236 printf("log_start = %08lx\n",
237 log->v2.start);
238 printf("log_end = %08lx\n", log->v2.end);
239 printf("logged_chars = %08lx\n",
240 log->v2.chars);
Igor Lisitsin93470f22007-04-18 14:55:19 +0400241 }
242 else {
Heiko Schocher31020122012-02-14 22:21:06 +0000243 printf("log_start = %08lx\n",
244 log->v1.start);
245 printf("log_size = %08lx\n",
246 log->v1.size);
247 printf("logged_chars = %08lx\n",
248 log->v1.chars);
Igor Lisitsin93470f22007-04-18 14:55:19 +0400249 }
wdenk56f94be2002-11-05 16:35:14 +0000250 return 0;
wdenk56f94be2002-11-05 16:35:14 +0000251 }
Simon Glassa06dfc72011-12-10 08:44:01 +0000252 return CMD_RET_USAGE;
wdenk56f94be2002-11-05 16:35:14 +0000253
254 default:
Simon Glassa06dfc72011-12-10 08:44:01 +0000255 return CMD_RET_USAGE;
wdenk56f94be2002-11-05 16:35:14 +0000256 }
257}
Igor Lisitsin93470f22007-04-18 14:55:19 +0400258
wdenkf287a242003-07-01 21:06:45 +0000259U_BOOT_CMD(
260 log, 255, 1, do_log,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600261 "manipulate logbuffer",
wdenkb9bbd242003-06-30 16:24:52 +0000262 "info - show pointer details\n"
wdenk57b2d802003-06-27 21:31:46 +0000263 "log reset - clear contents\n"
264 "log show - show contents\n"
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200265 "log append <msg> - append <msg> to the logbuffer"
wdenk57b2d802003-06-27 21:31:46 +0000266);
Igor Lisitsin93470f22007-04-18 14:55:19 +0400267
wdenk56f94be2002-11-05 16:35:14 +0000268static int logbuff_printk(const char *line)
269{
270 int i;
271 char *msg, *p, *buf_end;
272 int line_feed;
273 static signed char msg_level = -1;
274
Heiko Schocher31020122012-02-14 22:21:06 +0000275 strcpy(buf + 3, line);
276 i = strlen(line);
wdenk56f94be2002-11-05 16:35:14 +0000277 buf_end = buf + 3 + i;
278 for (p = buf + 3; p < buf_end; p++) {
279 msg = p;
280 if (msg_level < 0) {
281 if (
282 p[0] != '<' ||
283 p[1] < '0' ||
284 p[1] > '7' ||
285 p[2] != '>'
286 ) {
287 p -= 3;
288 p[0] = '<';
289 p[1] = default_message_loglevel + '0';
290 p[2] = '>';
Heiko Schocher31020122012-02-14 22:21:06 +0000291 } else {
wdenk56f94be2002-11-05 16:35:14 +0000292 msg += 3;
Heiko Schocher31020122012-02-14 22:21:06 +0000293 }
wdenk56f94be2002-11-05 16:35:14 +0000294 msg_level = p[1] - '0';
295 }
296 line_feed = 0;
297 for (; p < buf_end; p++) {
Igor Lisitsin93470f22007-04-18 14:55:19 +0400298 if (log_version == 2) {
Yuri Tikhonovfe1d91b2008-02-06 18:48:36 +0100299 lbuf[log->v2.end & LOGBUFF_MASK] = *p;
Igor Lisitsin93470f22007-04-18 14:55:19 +0400300 log->v2.end++;
301 if (log->v2.end - log->v2.start > LOGBUFF_LEN)
302 log->v2.start++;
303 log->v2.chars++;
Heiko Schocher31020122012-02-14 22:21:06 +0000304 } else {
Yuri Tikhonovfe1d91b2008-02-06 18:48:36 +0100305 lbuf[(log->v1.start + log->v1.size) &
Igor Lisitsin93470f22007-04-18 14:55:19 +0400306 LOGBUFF_MASK] = *p;
307 if (log->v1.size < LOGBUFF_LEN)
308 log->v1.size++;
309 else
310 log->v1.start++;
311 log->v1.chars++;
312 }
wdenk56f94be2002-11-05 16:35:14 +0000313 if (*p == '\n') {
314 line_feed = 1;
315 break;
316 }
317 }
318 if (msg_level < console_loglevel) {
319 printf("%s", msg);
320 }
321 if (line_feed)
322 msg_level = -1;
323 }
324 return i;
325}