]> git.ipfire.org Git - people/ms/u-boot.git/blame - common/cmd_log.c
Adapt log driver to latest POST changes (POST_SLOWTEST)
[people/ms/u-boot.git] / common / cmd_log.c
CommitLineData
56f94be3
WD
1/*
2 * (C) Copyright 2002
3 * Detlev Zundel, DENX Software Engineering, dzu@denx.de.
4 *
228f29ac
WD
5 * Code used from linux/kernel/printk.c
6 * Copyright (C) 1991, 1992 Linus Torvalds
7 *
56f94be3
WD
8 * 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
228f29ac
WD
25 *
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.
56f94be3
WD
37 */
38
39/*
40 * Logbuffer handling routines
41 */
42
43#include <common.h>
44#include <command.h>
45#include <devices.h>
228f29ac 46#include <post.h>
56f94be3
WD
47#include <logbuff.h>
48
49#if defined(CONFIG_LOGBUFFER)
50
56f94be3
WD
51/* 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
228f29ac 58/* This combination will not print messages with the default loglevel */
56f94be3
WD
59static unsigned console_loglevel = 3;
60static unsigned default_message_loglevel = 4;
228f29ac
WD
61static 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)
56f94be3
WD
66#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
228f29ac
WD
72/* 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;
b37c7e5e 77 unsigned long *ext_tag;
2960b65a 78 unsigned long post_word;
228f29ac
WD
79 char *s;
80
81 log_buf = (unsigned char *)(gd->bd->bi_memsize-LOGBUFF_LEN);
b37c7e5e
WD
82 ext_tag = (unsigned long *)(log_buf)-4;
83 ext_log_start = (unsigned long *)(log_buf)-3;
228f29ac
WD
84 ext_log_size = (unsigned long *)(log_buf)-2;
85 ext_logged_chars = (unsigned long *)(log_buf)-1;
2960b65a 86 post_word = post_word_load();
d1cbe85b
WD
87#ifdef CONFIG_POST
88 /* The post routines have setup the word so we can simply test it */
2960b65a
WD
89 if (((post_word & 0xffff) == POST_POWERON) ||
90 ((post_word & 0xffff) == POST_SLOWTEST)) {
228f29ac 91 logged_chars = log_size = log_start = 0;
b37c7e5e 92 *ext_tag = LOGBUFF_MAGIC;
228f29ac 93 }
d1cbe85b
WD
94#else
95 /* No post routines, so we do our own checking */
96 if (post_word_load () != LOGBUFF_MAGIC) {
97 logged_chars = log_size = log_start = 0;
98 post_word_store (LOGBUFF_MAGIC);
b37c7e5e 99 *ext_tag = LOGBUFF_MAGIC;
d1cbe85b
WD
100 }
101#endif
228f29ac
WD
102 /* 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
56f94be3
WD
109int 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];
228f29ac
WD
131 buf[0] = c;
132 buf[1] = '\0';
133 logbuff_printk (buf);
56f94be3
WD
134}
135
136static void logbuff_puts (const char *s)
137{
228f29ac 138 logbuff_printk (s);
56f94be3
WD
139}
140
141void logbuff_log(char *msg)
142{
143 DECLARE_GLOBAL_DATA_PTR;
144
228f29ac
WD
145 if ((gd->post_log_word & LOGBUFF_INITIALIZED)) {
146 logbuff_printk (msg);
56f94be3 147 } else {
228f29ac
WD
148 /* Can happen only for pre-relocated errors as logging */
149 /* at that stage should be disabled */
150 puts (msg);
56f94be3
WD
151 }
152}
153
154/*
155 * Subroutine: do_log
156 *
157 * Description: Handler for 'log' command..
158 *
159 * Inputs: argv[1] contains the subcommand
160 *
161 * Return: None
162 *
163 */
164int do_log (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
165{
166 char *s;
167 unsigned long i;
168
228f29ac
WD
169 if (strcmp(argv[1],"append") == 0) {
170 /* Log concatenation of all arguments separated by spaces */
171 for (i=2; i<argc; i++) {
b37c7e5e
WD
172 logbuff_printk (argv[i]);
173 logbuff_putc ((i<argc-1) ? ' ' : '\n');
228f29ac
WD
174 }
175 return 0;
56f94be3
WD
176 }
177
178 switch (argc) {
179
180 case 2:
181 if (strcmp(argv[1],"show") == 0) {
228f29ac
WD
182 for (i=0; i < (log_size&LOGBUFF_MASK); i++) {
183 s = log_buf+((log_start+i)&LOGBUFF_MASK);
184 putc (*s);
56f94be3
WD
185 }
186 return 0;
187 } else if (strcmp(argv[1],"reset") == 0) {
228f29ac
WD
188 log_start = 0;
189 log_size = 0;
190 logged_chars = 0;
56f94be3 191 return 0;
228f29ac
WD
192 } else if (strcmp(argv[1],"info") == 0) {
193 printf ("Logbuffer at %08lx\n", (unsigned long)log_buf);
194 printf ("log_start = %08lx\n", log_start);
195 printf ("log_size = %08lx\n", log_size);
196 printf ("logged_chars = %08lx\n", logged_chars);
56f94be3 197 return 0;
56f94be3
WD
198 }
199 printf ("Usage:\n%s\n", cmdtp->usage);
200 return 1;
201
202 default:
203 printf ("Usage:\n%s\n", cmdtp->usage);
204 return 1;
205 }
206}
8bde7f77 207#if defined(CONFIG_LOGBUFFER)
0d498393
WD
208U_BOOT_CMD(
209 log, 255, 1, do_log,
8bde7f77 210 "log - manipulate logbuffer\n",
b37c7e5e 211 "info - show pointer details\n"
8bde7f77
WD
212 "log reset - clear contents\n"
213 "log show - show contents\n"
214 "log append <msg> - append <msg> to the logbuffer\n"
215);
216#endif /* CONFIG_LOGBUFFER */
56f94be3
WD
217static int logbuff_printk(const char *line)
218{
219 int i;
220 char *msg, *p, *buf_end;
221 int line_feed;
222 static signed char msg_level = -1;
223
228f29ac
WD
224 strcpy (buf + 3, line);
225 i = strlen (line);
56f94be3
WD
226 buf_end = buf + 3 + i;
227 for (p = buf + 3; p < buf_end; p++) {
228 msg = p;
229 if (msg_level < 0) {
230 if (
231 p[0] != '<' ||
232 p[1] < '0' ||
233 p[1] > '7' ||
234 p[2] != '>'
235 ) {
236 p -= 3;
237 p[0] = '<';
238 p[1] = default_message_loglevel + '0';
239 p[2] = '>';
240 } else
241 msg += 3;
242 msg_level = p[1] - '0';
243 }
244 line_feed = 0;
245 for (; p < buf_end; p++) {
228f29ac
WD
246 log_buf[(log_start+log_size) & LOGBUFF_MASK] = *p;
247 if (log_size < LOGBUFF_LEN)
56f94be3
WD
248 log_size++;
249 else
250 log_start++;
251
252 logged_chars++;
253 if (*p == '\n') {
254 line_feed = 1;
255 break;
256 }
257 }
258 if (msg_level < console_loglevel) {
259 printf("%s", msg);
260 }
261 if (line_feed)
262 msg_level = -1;
263 }
264 return i;
265}
266
267#endif /* (CONFIG_LOGBUFFER) */