wdenk | 56f94be | 2002-11-05 16:35:14 +0000 | [diff] [blame] | 1 | /* |
Igor Lisitsin | 93470f2 | 2007-04-18 14:55:19 +0400 | [diff] [blame] | 2 | * (C) Copyright 2002-2007 |
wdenk | 56f94be | 2002-11-05 16:35:14 +0000 | [diff] [blame] | 3 | * Detlev Zundel, DENX Software Engineering, dzu@denx.de. |
| 4 | * |
wdenk | 9dfa8d1 | 2002-12-08 09:53:23 +0000 | [diff] [blame] | 5 | * Code used from linux/kernel/printk.c |
| 6 | * Copyright (C) 1991, 1992 Linus Torvalds |
| 7 | * |
wdenk | 56f94be | 2002-11-05 16:35:14 +0000 | [diff] [blame] | 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 |
wdenk | 9dfa8d1 | 2002-12-08 09:53:23 +0000 | [diff] [blame] | 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. |
wdenk | 56f94be | 2002-11-05 16:35:14 +0000 | [diff] [blame] | 37 | */ |
| 38 | |
| 39 | /* |
| 40 | * Logbuffer handling routines |
| 41 | */ |
| 42 | |
| 43 | #include <common.h> |
| 44 | #include <command.h> |
| 45 | #include <devices.h> |
wdenk | 9dfa8d1 | 2002-12-08 09:53:23 +0000 | [diff] [blame] | 46 | #include <post.h> |
wdenk | 56f94be | 2002-11-05 16:35:14 +0000 | [diff] [blame] | 47 | #include <logbuff.h> |
| 48 | |
Wolfgang Denk | 6405a15 | 2006-03-31 18:32:53 +0200 | [diff] [blame] | 49 | DECLARE_GLOBAL_DATA_PTR; |
| 50 | |
wdenk | 56f94be | 2002-11-05 16:35:14 +0000 | [diff] [blame] | 51 | /* Local prototypes */ |
| 52 | static void logbuff_putc (const char c); |
| 53 | static void logbuff_puts (const char *s); |
| 54 | static int logbuff_printk(const char *line); |
| 55 | |
| 56 | static char buf[1024]; |
| 57 | |
wdenk | 9dfa8d1 | 2002-12-08 09:53:23 +0000 | [diff] [blame] | 58 | /* This combination will not print messages with the default loglevel */ |
wdenk | 56f94be | 2002-11-05 16:35:14 +0000 | [diff] [blame] | 59 | static unsigned console_loglevel = 3; |
| 60 | static unsigned default_message_loglevel = 4; |
Igor Lisitsin | 93470f2 | 2007-04-18 14:55:19 +0400 | [diff] [blame] | 61 | static unsigned log_version = 1; |
Yuri Tikhonov | fe1d91b | 2008-02-06 18:48:36 +0100 | [diff] [blame] | 62 | #ifdef CONFIG_ALT_LB_ADDR |
| 63 | static volatile logbuff_t *log; |
| 64 | #else |
Igor Lisitsin | 93470f2 | 2007-04-18 14:55:19 +0400 | [diff] [blame] | 65 | static logbuff_t *log; |
Yuri Tikhonov | fe1d91b | 2008-02-06 18:48:36 +0100 | [diff] [blame] | 66 | #endif |
| 67 | static char *lbuf; |
wdenk | 56f94be | 2002-11-05 16:35:14 +0000 | [diff] [blame] | 68 | |
wdenk | 9dfa8d1 | 2002-12-08 09:53:23 +0000 | [diff] [blame] | 69 | void logbuff_init_ptrs (void) |
| 70 | { |
Igor Lisitsin | 93470f2 | 2007-04-18 14:55:19 +0400 | [diff] [blame] | 71 | unsigned long tag, post_word; |
wdenk | 9dfa8d1 | 2002-12-08 09:53:23 +0000 | [diff] [blame] | 72 | char *s; |
| 73 | |
Yuri Tikhonov | fe1d91b | 2008-02-06 18:48:36 +0100 | [diff] [blame] | 74 | #ifdef CONFIG_ALT_LB_ADDR |
| 75 | log = (logbuff_t *)CONFIG_ALT_LH_ADDR; |
| 76 | lbuf = (char *)CONFIG_ALT_LB_ADDR; |
| 77 | #else |
Igor Lisitsin | 93470f2 | 2007-04-18 14:55:19 +0400 | [diff] [blame] | 78 | log = (logbuff_t *)(gd->bd->bi_memsize-LOGBUFF_LEN) - 1; |
Yuri Tikhonov | fe1d91b | 2008-02-06 18:48:36 +0100 | [diff] [blame] | 79 | lbuf = log->buf; |
| 80 | #endif |
Igor Lisitsin | 93470f2 | 2007-04-18 14:55:19 +0400 | [diff] [blame] | 81 | |
| 82 | /* Set up log version */ |
| 83 | if ((s = getenv ("logversion")) != NULL) |
| 84 | log_version = (int)simple_strtoul (s, NULL, 10); |
| 85 | |
| 86 | if (log_version == 2) |
| 87 | tag = log->v2.tag; |
| 88 | else |
| 89 | tag = log->v1.tag; |
wdenk | 41bea11 | 2003-07-15 21:08:26 +0000 | [diff] [blame] | 90 | post_word = post_word_load(); |
wdenk | d9fce81 | 2003-06-28 17:24:46 +0000 | [diff] [blame] | 91 | #ifdef CONFIG_POST |
| 92 | /* The post routines have setup the word so we can simply test it */ |
Igor Lisitsin | 93470f2 | 2007-04-18 14:55:19 +0400 | [diff] [blame] | 93 | if (tag != LOGBUFF_MAGIC || (post_word & POST_COLDBOOT)) { |
| 94 | logbuff_reset (); |
Wolfgang Denk | 70df7bc | 2007-06-22 23:59:00 +0200 | [diff] [blame] | 95 | } |
wdenk | d9fce81 | 2003-06-28 17:24:46 +0000 | [diff] [blame] | 96 | #else |
| 97 | /* No post routines, so we do our own checking */ |
Igor Lisitsin | 93470f2 | 2007-04-18 14:55:19 +0400 | [diff] [blame] | 98 | if (tag != LOGBUFF_MAGIC || post_word != LOGBUFF_MAGIC) { |
| 99 | logbuff_reset (); |
wdenk | d9fce81 | 2003-06-28 17:24:46 +0000 | [diff] [blame] | 100 | post_word_store (LOGBUFF_MAGIC); |
Wolfgang Denk | 70df7bc | 2007-06-22 23:59:00 +0200 | [diff] [blame] | 101 | } |
wdenk | d9fce81 | 2003-06-28 17:24:46 +0000 | [diff] [blame] | 102 | #endif |
Igor Lisitsin | 93470f2 | 2007-04-18 14:55:19 +0400 | [diff] [blame] | 103 | if (log_version == 2 && (long)log->v2.start > (long)log->v2.con) |
| 104 | log->v2.start = log->v2.con; |
| 105 | |
wdenk | 9dfa8d1 | 2002-12-08 09:53:23 +0000 | [diff] [blame] | 106 | /* Initialize default loglevel if present */ |
| 107 | if ((s = getenv ("loglevel")) != NULL) |
| 108 | console_loglevel = (int)simple_strtoul (s, NULL, 10); |
| 109 | |
| 110 | gd->post_log_word |= LOGBUFF_INITIALIZED; |
| 111 | } |
| 112 | |
Igor Lisitsin | 93470f2 | 2007-04-18 14:55:19 +0400 | [diff] [blame] | 113 | void logbuff_reset (void) |
| 114 | { |
Yuri Tikhonov | fe1d91b | 2008-02-06 18:48:36 +0100 | [diff] [blame] | 115 | #ifndef CONFIG_ALT_LB_ADDR |
Igor Lisitsin | 93470f2 | 2007-04-18 14:55:19 +0400 | [diff] [blame] | 116 | memset (log, 0, sizeof (logbuff_t)); |
Yuri Tikhonov | fe1d91b | 2008-02-06 18:48:36 +0100 | [diff] [blame] | 117 | #endif |
| 118 | if (log_version == 2) { |
Igor Lisitsin | 93470f2 | 2007-04-18 14:55:19 +0400 | [diff] [blame] | 119 | log->v2.tag = LOGBUFF_MAGIC; |
Yuri Tikhonov | fe1d91b | 2008-02-06 18:48:36 +0100 | [diff] [blame] | 120 | #ifdef CONFIG_ALT_LB_ADDR |
| 121 | log->v2.start = 0; |
| 122 | log->v2.con = 0; |
| 123 | log->v2.end = 0; |
| 124 | log->v2.chars = 0; |
| 125 | #endif |
| 126 | } else { |
Igor Lisitsin | 93470f2 | 2007-04-18 14:55:19 +0400 | [diff] [blame] | 127 | log->v1.tag = LOGBUFF_MAGIC; |
Yuri Tikhonov | fe1d91b | 2008-02-06 18:48:36 +0100 | [diff] [blame] | 128 | #ifdef CONFIG_ALT_LB_ADDR |
| 129 | log->v1.dummy = 0; |
| 130 | log->v1.start = 0; |
| 131 | log->v1.size = 0; |
| 132 | log->v1.chars = 0; |
| 133 | #endif |
| 134 | } |
Igor Lisitsin | 93470f2 | 2007-04-18 14:55:19 +0400 | [diff] [blame] | 135 | } |
| 136 | |
wdenk | 56f94be | 2002-11-05 16:35:14 +0000 | [diff] [blame] | 137 | int drv_logbuff_init (void) |
| 138 | { |
| 139 | device_t logdev; |
| 140 | int rc; |
| 141 | |
| 142 | /* Device initialization */ |
| 143 | memset (&logdev, 0, sizeof (logdev)); |
| 144 | |
| 145 | strcpy (logdev.name, "logbuff"); |
| 146 | logdev.ext = 0; /* No extensions */ |
| 147 | logdev.flags = DEV_FLAGS_OUTPUT; /* Output only */ |
| 148 | logdev.putc = logbuff_putc; /* 'putc' function */ |
| 149 | logdev.puts = logbuff_puts; /* 'puts' function */ |
| 150 | |
| 151 | rc = device_register (&logdev); |
| 152 | |
| 153 | return (rc == 0) ? 1 : rc; |
| 154 | } |
| 155 | |
| 156 | static void logbuff_putc (const char c) |
| 157 | { |
| 158 | char buf[2]; |
wdenk | 9dfa8d1 | 2002-12-08 09:53:23 +0000 | [diff] [blame] | 159 | buf[0] = c; |
| 160 | buf[1] = '\0'; |
| 161 | logbuff_printk (buf); |
wdenk | 56f94be | 2002-11-05 16:35:14 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | static void logbuff_puts (const char *s) |
| 165 | { |
wdenk | 9dfa8d1 | 2002-12-08 09:53:23 +0000 | [diff] [blame] | 166 | logbuff_printk (s); |
wdenk | 56f94be | 2002-11-05 16:35:14 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | void logbuff_log(char *msg) |
| 170 | { |
wdenk | 9dfa8d1 | 2002-12-08 09:53:23 +0000 | [diff] [blame] | 171 | if ((gd->post_log_word & LOGBUFF_INITIALIZED)) { |
| 172 | logbuff_printk (msg); |
wdenk | 56f94be | 2002-11-05 16:35:14 +0000 | [diff] [blame] | 173 | } else { |
wdenk | 9dfa8d1 | 2002-12-08 09:53:23 +0000 | [diff] [blame] | 174 | /* Can happen only for pre-relocated errors as logging */ |
| 175 | /* at that stage should be disabled */ |
| 176 | puts (msg); |
wdenk | 56f94be | 2002-11-05 16:35:14 +0000 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | |
wdenk | 56f94be | 2002-11-05 16:35:14 +0000 | [diff] [blame] | 180 | /* |
| 181 | * Subroutine: do_log |
| 182 | * |
| 183 | * Description: Handler for 'log' command.. |
| 184 | * |
| 185 | * Inputs: argv[1] contains the subcommand |
| 186 | * |
| 187 | * Return: None |
| 188 | * |
| 189 | */ |
| 190 | int do_log (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) |
| 191 | { |
| 192 | char *s; |
Igor Lisitsin | 93470f2 | 2007-04-18 14:55:19 +0400 | [diff] [blame] | 193 | unsigned long i, start, size; |
wdenk | 56f94be | 2002-11-05 16:35:14 +0000 | [diff] [blame] | 194 | |
wdenk | 9dfa8d1 | 2002-12-08 09:53:23 +0000 | [diff] [blame] | 195 | if (strcmp(argv[1],"append") == 0) { |
| 196 | /* Log concatenation of all arguments separated by spaces */ |
| 197 | for (i=2; i<argc; i++) { |
wdenk | b9bbd24 | 2003-06-30 16:24:52 +0000 | [diff] [blame] | 198 | logbuff_printk (argv[i]); |
| 199 | logbuff_putc ((i<argc-1) ? ' ' : '\n'); |
wdenk | 9dfa8d1 | 2002-12-08 09:53:23 +0000 | [diff] [blame] | 200 | } |
| 201 | return 0; |
wdenk | 56f94be | 2002-11-05 16:35:14 +0000 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | switch (argc) { |
| 205 | |
| 206 | case 2: |
| 207 | if (strcmp(argv[1],"show") == 0) { |
Igor Lisitsin | 93470f2 | 2007-04-18 14:55:19 +0400 | [diff] [blame] | 208 | if (log_version == 2) { |
| 209 | start = log->v2.start; |
| 210 | size = log->v2.end - log->v2.start; |
| 211 | } |
| 212 | else { |
| 213 | start = log->v1.start; |
| 214 | size = log->v1.size; |
| 215 | } |
| 216 | for (i=0; i < (size&LOGBUFF_MASK); i++) { |
Yuri Tikhonov | fe1d91b | 2008-02-06 18:48:36 +0100 | [diff] [blame] | 217 | s = lbuf+((start+i)&LOGBUFF_MASK); |
wdenk | 9dfa8d1 | 2002-12-08 09:53:23 +0000 | [diff] [blame] | 218 | putc (*s); |
wdenk | 56f94be | 2002-11-05 16:35:14 +0000 | [diff] [blame] | 219 | } |
| 220 | return 0; |
| 221 | } else if (strcmp(argv[1],"reset") == 0) { |
Igor Lisitsin | 93470f2 | 2007-04-18 14:55:19 +0400 | [diff] [blame] | 222 | logbuff_reset (); |
wdenk | 56f94be | 2002-11-05 16:35:14 +0000 | [diff] [blame] | 223 | return 0; |
wdenk | 9dfa8d1 | 2002-12-08 09:53:23 +0000 | [diff] [blame] | 224 | } else if (strcmp(argv[1],"info") == 0) { |
Yuri Tikhonov | fe1d91b | 2008-02-06 18:48:36 +0100 | [diff] [blame] | 225 | printf ("Logbuffer at %08lx\n", (unsigned long)lbuf); |
Igor Lisitsin | 93470f2 | 2007-04-18 14:55:19 +0400 | [diff] [blame] | 226 | if (log_version == 2) { |
| 227 | printf ("log_start = %08lx\n", log->v2.start); |
| 228 | printf ("log_end = %08lx\n", log->v2.end); |
| 229 | printf ("logged_chars = %08lx\n", log->v2.chars); |
| 230 | } |
| 231 | else { |
| 232 | printf ("log_start = %08lx\n", log->v1.start); |
| 233 | printf ("log_size = %08lx\n", log->v1.size); |
| 234 | printf ("logged_chars = %08lx\n", log->v1.chars); |
| 235 | } |
wdenk | 56f94be | 2002-11-05 16:35:14 +0000 | [diff] [blame] | 236 | return 0; |
wdenk | 56f94be | 2002-11-05 16:35:14 +0000 | [diff] [blame] | 237 | } |
| 238 | printf ("Usage:\n%s\n", cmdtp->usage); |
| 239 | return 1; |
| 240 | |
| 241 | default: |
| 242 | printf ("Usage:\n%s\n", cmdtp->usage); |
| 243 | return 1; |
| 244 | } |
| 245 | } |
Igor Lisitsin | 93470f2 | 2007-04-18 14:55:19 +0400 | [diff] [blame] | 246 | |
wdenk | f287a24 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 247 | U_BOOT_CMD( |
| 248 | log, 255, 1, do_log, |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 249 | "log - manipulate logbuffer\n", |
wdenk | b9bbd24 | 2003-06-30 16:24:52 +0000 | [diff] [blame] | 250 | "info - show pointer details\n" |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 251 | "log reset - clear contents\n" |
| 252 | "log show - show contents\n" |
| 253 | "log append <msg> - append <msg> to the logbuffer\n" |
| 254 | ); |
Igor Lisitsin | 93470f2 | 2007-04-18 14:55:19 +0400 | [diff] [blame] | 255 | |
wdenk | 56f94be | 2002-11-05 16:35:14 +0000 | [diff] [blame] | 256 | static int logbuff_printk(const char *line) |
| 257 | { |
| 258 | int i; |
| 259 | char *msg, *p, *buf_end; |
| 260 | int line_feed; |
| 261 | static signed char msg_level = -1; |
| 262 | |
wdenk | 9dfa8d1 | 2002-12-08 09:53:23 +0000 | [diff] [blame] | 263 | strcpy (buf + 3, line); |
| 264 | i = strlen (line); |
wdenk | 56f94be | 2002-11-05 16:35:14 +0000 | [diff] [blame] | 265 | buf_end = buf + 3 + i; |
| 266 | for (p = buf + 3; p < buf_end; p++) { |
| 267 | msg = p; |
| 268 | if (msg_level < 0) { |
| 269 | if ( |
| 270 | p[0] != '<' || |
| 271 | p[1] < '0' || |
| 272 | p[1] > '7' || |
| 273 | p[2] != '>' |
| 274 | ) { |
| 275 | p -= 3; |
| 276 | p[0] = '<'; |
| 277 | p[1] = default_message_loglevel + '0'; |
| 278 | p[2] = '>'; |
| 279 | } else |
| 280 | msg += 3; |
| 281 | msg_level = p[1] - '0'; |
| 282 | } |
| 283 | line_feed = 0; |
| 284 | for (; p < buf_end; p++) { |
Igor Lisitsin | 93470f2 | 2007-04-18 14:55:19 +0400 | [diff] [blame] | 285 | if (log_version == 2) { |
Yuri Tikhonov | fe1d91b | 2008-02-06 18:48:36 +0100 | [diff] [blame] | 286 | lbuf[log->v2.end & LOGBUFF_MASK] = *p; |
Igor Lisitsin | 93470f2 | 2007-04-18 14:55:19 +0400 | [diff] [blame] | 287 | log->v2.end++; |
| 288 | if (log->v2.end - log->v2.start > LOGBUFF_LEN) |
| 289 | log->v2.start++; |
| 290 | log->v2.chars++; |
| 291 | } |
| 292 | else { |
Yuri Tikhonov | fe1d91b | 2008-02-06 18:48:36 +0100 | [diff] [blame] | 293 | lbuf[(log->v1.start + log->v1.size) & |
Igor Lisitsin | 93470f2 | 2007-04-18 14:55:19 +0400 | [diff] [blame] | 294 | LOGBUFF_MASK] = *p; |
| 295 | if (log->v1.size < LOGBUFF_LEN) |
| 296 | log->v1.size++; |
| 297 | else |
| 298 | log->v1.start++; |
| 299 | log->v1.chars++; |
| 300 | } |
wdenk | 56f94be | 2002-11-05 16:35:14 +0000 | [diff] [blame] | 301 | if (*p == '\n') { |
| 302 | line_feed = 1; |
| 303 | break; |
| 304 | } |
| 305 | } |
| 306 | if (msg_level < console_loglevel) { |
| 307 | printf("%s", msg); |
| 308 | } |
| 309 | if (line_feed) |
| 310 | msg_level = -1; |
| 311 | } |
| 312 | return i; |
| 313 | } |