blob: 38d0f5edfd7872d50af20b5e38173b0871c39f19 [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 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02008 * SPDX-License-Identifier: GPL-2.0+
wdenk9dfa8d12002-12-08 09:53:23 +00009 *
10 * Comments:
11 *
12 * After relocating the code, the environment variable "loglevel" is
13 * copied to console_loglevel. The functionality is similar to the
14 * handling in the Linux kernel, i.e. messages logged with a priority
15 * less than console_loglevel are also output to stdout.
16 *
17 * If you want messages with the default level (e.g. POST messages) to
18 * appear on stdout also, make sure the environment variable
19 * "loglevel" is set at boot time to a number higher than
20 * default_message_loglevel below.
wdenk56f94be2002-11-05 16:35:14 +000021 */
22
23/*
24 * Logbuffer handling routines
25 */
26
27#include <common.h>
28#include <command.h>
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +020029#include <stdio_dev.h>
wdenk9dfa8d12002-12-08 09:53:23 +000030#include <post.h>
wdenk56f94be2002-11-05 16:35:14 +000031#include <logbuff.h>
32
Wolfgang Denk6405a152006-03-31 18:32:53 +020033DECLARE_GLOBAL_DATA_PTR;
34
wdenk56f94be2002-11-05 16:35:14 +000035/* Local prototypes */
Heiko Schocher31020122012-02-14 22:21:06 +000036static void logbuff_putc(const char c);
37static void logbuff_puts(const char *s);
wdenk56f94be2002-11-05 16:35:14 +000038static int logbuff_printk(const char *line);
39
40static char buf[1024];
41
wdenk9dfa8d12002-12-08 09:53:23 +000042/* This combination will not print messages with the default loglevel */
wdenk56f94be2002-11-05 16:35:14 +000043static unsigned console_loglevel = 3;
44static unsigned default_message_loglevel = 4;
Igor Lisitsin93470f22007-04-18 14:55:19 +040045static unsigned log_version = 1;
Yuri Tikhonovfe1d91b2008-02-06 18:48:36 +010046#ifdef CONFIG_ALT_LB_ADDR
47static volatile logbuff_t *log;
48#else
Igor Lisitsin93470f22007-04-18 14:55:19 +040049static logbuff_t *log;
Yuri Tikhonovfe1d91b2008-02-06 18:48:36 +010050#endif
51static char *lbuf;
wdenk56f94be2002-11-05 16:35:14 +000052
Marian Balakowicz902ce702008-05-13 15:53:29 +020053unsigned long __logbuffer_base(void)
54{
York Sun863e8d82014-02-11 11:57:26 -080055 return CONFIG_SYS_SDRAM_BASE + get_effective_memsize() - LOGBUFF_LEN;
Marian Balakowicz902ce702008-05-13 15:53:29 +020056}
Heiko Schocher31020122012-02-14 22:21:06 +000057unsigned long logbuffer_base(void)
58__attribute__((weak, alias("__logbuffer_base")));
Marian Balakowicz902ce702008-05-13 15:53:29 +020059
Heiko Schocher31020122012-02-14 22:21:06 +000060void logbuff_init_ptrs(void)
wdenk9dfa8d12002-12-08 09:53:23 +000061{
Igor Lisitsin93470f22007-04-18 14:55:19 +040062 unsigned long tag, post_word;
wdenk9dfa8d12002-12-08 09:53:23 +000063 char *s;
64
Yuri Tikhonovfe1d91b2008-02-06 18:48:36 +010065#ifdef CONFIG_ALT_LB_ADDR
66 log = (logbuff_t *)CONFIG_ALT_LH_ADDR;
67 lbuf = (char *)CONFIG_ALT_LB_ADDR;
68#else
Heiko Schocher31020122012-02-14 22:21:06 +000069 log = (logbuff_t *)(logbuffer_base()) - 1;
Jean-Christophe PLAGNIOL-VILLARD531ad562008-04-02 08:03:57 +020070 lbuf = (char *)log->buf;
Yuri Tikhonovfe1d91b2008-02-06 18:48:36 +010071#endif
Igor Lisitsin93470f22007-04-18 14:55:19 +040072
73 /* Set up log version */
74 if ((s = getenv ("logversion")) != NULL)
Heiko Schocher31020122012-02-14 22:21:06 +000075 log_version = (int)simple_strtoul(s, NULL, 10);
Igor Lisitsin93470f22007-04-18 14:55:19 +040076
77 if (log_version == 2)
78 tag = log->v2.tag;
79 else
80 tag = log->v1.tag;
wdenk41bea112003-07-15 21:08:26 +000081 post_word = post_word_load();
wdenkd9fce812003-06-28 17:24:46 +000082#ifdef CONFIG_POST
83 /* The post routines have setup the word so we can simply test it */
Heiko Schocher31020122012-02-14 22:21:06 +000084 if (tag != LOGBUFF_MAGIC || (post_word & POST_COLDBOOT))
85 logbuff_reset();
wdenkd9fce812003-06-28 17:24:46 +000086#else
87 /* No post routines, so we do our own checking */
Igor Lisitsin93470f22007-04-18 14:55:19 +040088 if (tag != LOGBUFF_MAGIC || post_word != LOGBUFF_MAGIC) {
89 logbuff_reset ();
wdenkd9fce812003-06-28 17:24:46 +000090 post_word_store (LOGBUFF_MAGIC);
Wolfgang Denk70df7bc2007-06-22 23:59:00 +020091 }
wdenkd9fce812003-06-28 17:24:46 +000092#endif
Igor Lisitsin93470f22007-04-18 14:55:19 +040093 if (log_version == 2 && (long)log->v2.start > (long)log->v2.con)
94 log->v2.start = log->v2.con;
95
wdenk9dfa8d12002-12-08 09:53:23 +000096 /* Initialize default loglevel if present */
97 if ((s = getenv ("loglevel")) != NULL)
Heiko Schocher31020122012-02-14 22:21:06 +000098 console_loglevel = (int)simple_strtoul(s, NULL, 10);
wdenk9dfa8d12002-12-08 09:53:23 +000099
Yuri Tikhonov35d81ce2008-05-08 15:46:42 +0200100 gd->flags |= GD_FLG_LOGINIT;
wdenk9dfa8d12002-12-08 09:53:23 +0000101}
102
Heiko Schocher31020122012-02-14 22:21:06 +0000103void logbuff_reset(void)
Igor Lisitsin93470f22007-04-18 14:55:19 +0400104{
Yuri Tikhonovfe1d91b2008-02-06 18:48:36 +0100105#ifndef CONFIG_ALT_LB_ADDR
Heiko Schocher31020122012-02-14 22:21:06 +0000106 memset(log, 0, sizeof(logbuff_t));
Yuri Tikhonovfe1d91b2008-02-06 18:48:36 +0100107#endif
108 if (log_version == 2) {
Igor Lisitsin93470f22007-04-18 14:55:19 +0400109 log->v2.tag = LOGBUFF_MAGIC;
Yuri Tikhonovfe1d91b2008-02-06 18:48:36 +0100110#ifdef CONFIG_ALT_LB_ADDR
111 log->v2.start = 0;
112 log->v2.con = 0;
113 log->v2.end = 0;
114 log->v2.chars = 0;
115#endif
116 } else {
Igor Lisitsin93470f22007-04-18 14:55:19 +0400117 log->v1.tag = LOGBUFF_MAGIC;
Yuri Tikhonovfe1d91b2008-02-06 18:48:36 +0100118#ifdef CONFIG_ALT_LB_ADDR
119 log->v1.dummy = 0;
120 log->v1.start = 0;
121 log->v1.size = 0;
122 log->v1.chars = 0;
123#endif
124 }
Igor Lisitsin93470f22007-04-18 14:55:19 +0400125}
126
Heiko Schocher31020122012-02-14 22:21:06 +0000127int drv_logbuff_init(void)
wdenk56f94be2002-11-05 16:35:14 +0000128{
Jean-Christophe PLAGNIOL-VILLARD2a7a0312009-05-16 12:14:54 +0200129 struct stdio_dev logdev;
wdenk56f94be2002-11-05 16:35:14 +0000130 int rc;
131
132 /* Device initialization */
133 memset (&logdev, 0, sizeof (logdev));
134
135 strcpy (logdev.name, "logbuff");
136 logdev.ext = 0; /* No extensions */
137 logdev.flags = DEV_FLAGS_OUTPUT; /* Output only */
138 logdev.putc = logbuff_putc; /* 'putc' function */
139 logdev.puts = logbuff_puts; /* 'puts' function */
140
Heiko Schocher31020122012-02-14 22:21:06 +0000141 rc = stdio_register(&logdev);
wdenk56f94be2002-11-05 16:35:14 +0000142
143 return (rc == 0) ? 1 : rc;
144}
145
Heiko Schocher31020122012-02-14 22:21:06 +0000146static void logbuff_putc(const char c)
wdenk56f94be2002-11-05 16:35:14 +0000147{
148 char buf[2];
wdenk9dfa8d12002-12-08 09:53:23 +0000149 buf[0] = c;
150 buf[1] = '\0';
Heiko Schocher31020122012-02-14 22:21:06 +0000151 logbuff_printk(buf);
wdenk56f94be2002-11-05 16:35:14 +0000152}
153
Heiko Schocher31020122012-02-14 22:21:06 +0000154static void logbuff_puts(const char *s)
wdenk56f94be2002-11-05 16:35:14 +0000155{
wdenk9dfa8d12002-12-08 09:53:23 +0000156 logbuff_printk (s);
wdenk56f94be2002-11-05 16:35:14 +0000157}
158
159void logbuff_log(char *msg)
160{
Yuri Tikhonov35d81ce2008-05-08 15:46:42 +0200161 if ((gd->flags & GD_FLG_LOGINIT)) {
Heiko Schocher31020122012-02-14 22:21:06 +0000162 logbuff_printk(msg);
wdenk56f94be2002-11-05 16:35:14 +0000163 } else {
Heiko Schocher31020122012-02-14 22:21:06 +0000164 /*
165 * Can happen only for pre-relocated errors as logging
166 * at that stage should be disabled
167 */
wdenk9dfa8d12002-12-08 09:53:23 +0000168 puts (msg);
wdenk56f94be2002-11-05 16:35:14 +0000169 }
170}
171
wdenk56f94be2002-11-05 16:35:14 +0000172/*
173 * Subroutine: do_log
174 *
175 * Description: Handler for 'log' command..
176 *
177 * Inputs: argv[1] contains the subcommand
178 *
179 * Return: None
180 *
181 */
Heiko Schocher31020122012-02-14 22:21:06 +0000182int do_log(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk56f94be2002-11-05 16:35:14 +0000183{
184 char *s;
Igor Lisitsin93470f22007-04-18 14:55:19 +0400185 unsigned long i, start, size;
wdenk56f94be2002-11-05 16:35:14 +0000186
Heiko Schocher31020122012-02-14 22:21:06 +0000187 if (strcmp(argv[1], "append") == 0) {
wdenk9dfa8d12002-12-08 09:53:23 +0000188 /* Log concatenation of all arguments separated by spaces */
Heiko Schocher31020122012-02-14 22:21:06 +0000189 for (i = 2; i < argc; i++) {
190 logbuff_printk(argv[i]);
191 logbuff_putc((i < argc - 1) ? ' ' : '\n');
wdenk9dfa8d12002-12-08 09:53:23 +0000192 }
193 return 0;
wdenk56f94be2002-11-05 16:35:14 +0000194 }
195
196 switch (argc) {
197
198 case 2:
Heiko Schocher31020122012-02-14 22:21:06 +0000199 if (strcmp(argv[1], "show") == 0) {
Igor Lisitsin93470f22007-04-18 14:55:19 +0400200 if (log_version == 2) {
201 start = log->v2.start;
202 size = log->v2.end - log->v2.start;
Heiko Schocher31020122012-02-14 22:21:06 +0000203 } else {
Igor Lisitsin93470f22007-04-18 14:55:19 +0400204 start = log->v1.start;
205 size = log->v1.size;
206 }
Heiko Schochere5f78692012-02-16 01:02:21 +0000207 if (size > LOGBUFF_LEN)
208 size = LOGBUFF_LEN;
209 for (i = 0; i < size; i++) {
Heiko Schocher31020122012-02-14 22:21:06 +0000210 s = lbuf + ((start + i) & LOGBUFF_MASK);
211 putc(*s);
wdenk56f94be2002-11-05 16:35:14 +0000212 }
213 return 0;
Heiko Schocher31020122012-02-14 22:21:06 +0000214 } else if (strcmp(argv[1], "reset") == 0) {
215 logbuff_reset();
wdenk56f94be2002-11-05 16:35:14 +0000216 return 0;
Heiko Schocher31020122012-02-14 22:21:06 +0000217 } else if (strcmp(argv[1], "info") == 0) {
218 printf("Logbuffer at %08lx\n", (unsigned long)lbuf);
Igor Lisitsin93470f22007-04-18 14:55:19 +0400219 if (log_version == 2) {
Heiko Schocher31020122012-02-14 22:21:06 +0000220 printf("log_start = %08lx\n",
221 log->v2.start);
222 printf("log_end = %08lx\n", log->v2.end);
Heiko Schocher650ebc02012-02-14 22:21:07 +0000223 printf("log_con = %08lx\n", log->v2.con);
Heiko Schocher31020122012-02-14 22:21:06 +0000224 printf("logged_chars = %08lx\n",
225 log->v2.chars);
Igor Lisitsin93470f22007-04-18 14:55:19 +0400226 }
227 else {
Heiko Schocher31020122012-02-14 22:21:06 +0000228 printf("log_start = %08lx\n",
229 log->v1.start);
230 printf("log_size = %08lx\n",
231 log->v1.size);
232 printf("logged_chars = %08lx\n",
233 log->v1.chars);
Igor Lisitsin93470f22007-04-18 14:55:19 +0400234 }
wdenk56f94be2002-11-05 16:35:14 +0000235 return 0;
wdenk56f94be2002-11-05 16:35:14 +0000236 }
Simon Glassa06dfc72011-12-10 08:44:01 +0000237 return CMD_RET_USAGE;
wdenk56f94be2002-11-05 16:35:14 +0000238
239 default:
Simon Glassa06dfc72011-12-10 08:44:01 +0000240 return CMD_RET_USAGE;
wdenk56f94be2002-11-05 16:35:14 +0000241 }
242}
Igor Lisitsin93470f22007-04-18 14:55:19 +0400243
wdenkf287a242003-07-01 21:06:45 +0000244U_BOOT_CMD(
245 log, 255, 1, do_log,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600246 "manipulate logbuffer",
wdenkb9bbd242003-06-30 16:24:52 +0000247 "info - show pointer details\n"
wdenk57b2d802003-06-27 21:31:46 +0000248 "log reset - clear contents\n"
249 "log show - show contents\n"
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200250 "log append <msg> - append <msg> to the logbuffer"
wdenk57b2d802003-06-27 21:31:46 +0000251);
Igor Lisitsin93470f22007-04-18 14:55:19 +0400252
wdenk56f94be2002-11-05 16:35:14 +0000253static int logbuff_printk(const char *line)
254{
255 int i;
256 char *msg, *p, *buf_end;
257 int line_feed;
258 static signed char msg_level = -1;
259
Heiko Schocher31020122012-02-14 22:21:06 +0000260 strcpy(buf + 3, line);
261 i = strlen(line);
wdenk56f94be2002-11-05 16:35:14 +0000262 buf_end = buf + 3 + i;
263 for (p = buf + 3; p < buf_end; p++) {
264 msg = p;
265 if (msg_level < 0) {
266 if (
267 p[0] != '<' ||
268 p[1] < '0' ||
269 p[1] > '7' ||
270 p[2] != '>'
271 ) {
272 p -= 3;
273 p[0] = '<';
274 p[1] = default_message_loglevel + '0';
275 p[2] = '>';
Heiko Schocher31020122012-02-14 22:21:06 +0000276 } else {
wdenk56f94be2002-11-05 16:35:14 +0000277 msg += 3;
Heiko Schocher31020122012-02-14 22:21:06 +0000278 }
wdenk56f94be2002-11-05 16:35:14 +0000279 msg_level = p[1] - '0';
280 }
281 line_feed = 0;
282 for (; p < buf_end; p++) {
Igor Lisitsin93470f22007-04-18 14:55:19 +0400283 if (log_version == 2) {
Yuri Tikhonovfe1d91b2008-02-06 18:48:36 +0100284 lbuf[log->v2.end & LOGBUFF_MASK] = *p;
Igor Lisitsin93470f22007-04-18 14:55:19 +0400285 log->v2.end++;
286 if (log->v2.end - log->v2.start > LOGBUFF_LEN)
287 log->v2.start++;
288 log->v2.chars++;
Heiko Schocher31020122012-02-14 22:21:06 +0000289 } else {
Yuri Tikhonovfe1d91b2008-02-06 18:48:36 +0100290 lbuf[(log->v1.start + log->v1.size) &
Igor Lisitsin93470f22007-04-18 14:55:19 +0400291 LOGBUFF_MASK] = *p;
292 if (log->v1.size < LOGBUFF_LEN)
293 log->v1.size++;
294 else
295 log->v1.start++;
296 log->v1.chars++;
297 }
wdenk56f94be2002-11-05 16:35:14 +0000298 if (*p == '\n') {
299 line_feed = 1;
300 break;
301 }
302 }
303 if (msg_level < console_loglevel) {
304 printf("%s", msg);
305 }
306 if (line_feed)
307 msg_level = -1;
308 }
309 return i;
310}