wdenk | 56f94be | 2002-11-05 16:35:14 +0000 | [diff] [blame] | 1 | /* |
Igor Lisitsin | 2dc6445 | 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 | 228f29a | 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 | 228f29a | 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 | 228f29a | 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 | d87080b | 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 | 228f29a | 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 | 2dc6445 | 2007-04-18 14:55:19 +0400 | [diff] [blame] | 61 | static unsigned log_version = 1; |
| 62 | static logbuff_t *log; |
wdenk | 56f94be | 2002-11-05 16:35:14 +0000 | [diff] [blame] | 63 | |
wdenk | 228f29a | 2002-12-08 09:53:23 +0000 | [diff] [blame] | 64 | void logbuff_init_ptrs (void) |
| 65 | { |
Igor Lisitsin | 2dc6445 | 2007-04-18 14:55:19 +0400 | [diff] [blame] | 66 | unsigned long tag, post_word; |
wdenk | 228f29a | 2002-12-08 09:53:23 +0000 | [diff] [blame] | 67 | char *s; |
| 68 | |
Igor Lisitsin | 2dc6445 | 2007-04-18 14:55:19 +0400 | [diff] [blame] | 69 | log = (logbuff_t *)(gd->bd->bi_memsize-LOGBUFF_LEN) - 1; |
| 70 | |
| 71 | /* Set up log version */ |
| 72 | if ((s = getenv ("logversion")) != NULL) |
| 73 | log_version = (int)simple_strtoul (s, NULL, 10); |
| 74 | |
| 75 | if (log_version == 2) |
| 76 | tag = log->v2.tag; |
| 77 | else |
| 78 | tag = log->v1.tag; |
wdenk | 2960b65 | 2003-07-15 21:08:26 +0000 | [diff] [blame] | 79 | post_word = post_word_load(); |
wdenk | d1cbe85 | 2003-06-28 17:24:46 +0000 | [diff] [blame] | 80 | #ifdef CONFIG_POST |
| 81 | /* The post routines have setup the word so we can simply test it */ |
Igor Lisitsin | 2dc6445 | 2007-04-18 14:55:19 +0400 | [diff] [blame] | 82 | if (tag != LOGBUFF_MAGIC || (post_word & POST_COLDBOOT)) { |
| 83 | logbuff_reset (); |
Wolfgang Denk | 1636d1c | 2007-06-22 23:59:00 +0200 | [diff] [blame] | 84 | } |
wdenk | d1cbe85 | 2003-06-28 17:24:46 +0000 | [diff] [blame] | 85 | #else |
| 86 | /* No post routines, so we do our own checking */ |
Igor Lisitsin | 2dc6445 | 2007-04-18 14:55:19 +0400 | [diff] [blame] | 87 | if (tag != LOGBUFF_MAGIC || post_word != LOGBUFF_MAGIC) { |
| 88 | logbuff_reset (); |
wdenk | d1cbe85 | 2003-06-28 17:24:46 +0000 | [diff] [blame] | 89 | post_word_store (LOGBUFF_MAGIC); |
Wolfgang Denk | 1636d1c | 2007-06-22 23:59:00 +0200 | [diff] [blame] | 90 | } |
wdenk | d1cbe85 | 2003-06-28 17:24:46 +0000 | [diff] [blame] | 91 | #endif |
Igor Lisitsin | 2dc6445 | 2007-04-18 14:55:19 +0400 | [diff] [blame] | 92 | if (log_version == 2 && (long)log->v2.start > (long)log->v2.con) |
| 93 | log->v2.start = log->v2.con; |
| 94 | |
wdenk | 228f29a | 2002-12-08 09:53:23 +0000 | [diff] [blame] | 95 | /* Initialize default loglevel if present */ |
| 96 | if ((s = getenv ("loglevel")) != NULL) |
| 97 | console_loglevel = (int)simple_strtoul (s, NULL, 10); |
| 98 | |
| 99 | gd->post_log_word |= LOGBUFF_INITIALIZED; |
| 100 | } |
| 101 | |
Igor Lisitsin | 2dc6445 | 2007-04-18 14:55:19 +0400 | [diff] [blame] | 102 | void logbuff_reset (void) |
| 103 | { |
| 104 | memset (log, 0, sizeof (logbuff_t)); |
| 105 | if (log_version == 2) |
| 106 | log->v2.tag = LOGBUFF_MAGIC; |
| 107 | else |
| 108 | log->v1.tag = LOGBUFF_MAGIC; |
| 109 | } |
| 110 | |
wdenk | 56f94be | 2002-11-05 16:35:14 +0000 | [diff] [blame] | 111 | int drv_logbuff_init (void) |
| 112 | { |
| 113 | device_t logdev; |
| 114 | int rc; |
| 115 | |
| 116 | /* Device initialization */ |
| 117 | memset (&logdev, 0, sizeof (logdev)); |
| 118 | |
| 119 | strcpy (logdev.name, "logbuff"); |
| 120 | logdev.ext = 0; /* No extensions */ |
| 121 | logdev.flags = DEV_FLAGS_OUTPUT; /* Output only */ |
| 122 | logdev.putc = logbuff_putc; /* 'putc' function */ |
| 123 | logdev.puts = logbuff_puts; /* 'puts' function */ |
| 124 | |
| 125 | rc = device_register (&logdev); |
| 126 | |
| 127 | return (rc == 0) ? 1 : rc; |
| 128 | } |
| 129 | |
| 130 | static void logbuff_putc (const char c) |
| 131 | { |
| 132 | char buf[2]; |
wdenk | 228f29a | 2002-12-08 09:53:23 +0000 | [diff] [blame] | 133 | buf[0] = c; |
| 134 | buf[1] = '\0'; |
| 135 | logbuff_printk (buf); |
wdenk | 56f94be | 2002-11-05 16:35:14 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | static void logbuff_puts (const char *s) |
| 139 | { |
wdenk | 228f29a | 2002-12-08 09:53:23 +0000 | [diff] [blame] | 140 | logbuff_printk (s); |
wdenk | 56f94be | 2002-11-05 16:35:14 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | void logbuff_log(char *msg) |
| 144 | { |
wdenk | 228f29a | 2002-12-08 09:53:23 +0000 | [diff] [blame] | 145 | if ((gd->post_log_word & LOGBUFF_INITIALIZED)) { |
| 146 | logbuff_printk (msg); |
wdenk | 56f94be | 2002-11-05 16:35:14 +0000 | [diff] [blame] | 147 | } else { |
wdenk | 228f29a | 2002-12-08 09:53:23 +0000 | [diff] [blame] | 148 | /* Can happen only for pre-relocated errors as logging */ |
| 149 | /* at that stage should be disabled */ |
| 150 | puts (msg); |
wdenk | 56f94be | 2002-11-05 16:35:14 +0000 | [diff] [blame] | 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 | */ |
| 164 | int do_log (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) |
| 165 | { |
| 166 | char *s; |
Igor Lisitsin | 2dc6445 | 2007-04-18 14:55:19 +0400 | [diff] [blame] | 167 | unsigned long i, start, size; |
wdenk | 56f94be | 2002-11-05 16:35:14 +0000 | [diff] [blame] | 168 | |
wdenk | 228f29a | 2002-12-08 09:53:23 +0000 | [diff] [blame] | 169 | if (strcmp(argv[1],"append") == 0) { |
| 170 | /* Log concatenation of all arguments separated by spaces */ |
| 171 | for (i=2; i<argc; i++) { |
wdenk | b37c7e5 | 2003-06-30 16:24:52 +0000 | [diff] [blame] | 172 | logbuff_printk (argv[i]); |
| 173 | logbuff_putc ((i<argc-1) ? ' ' : '\n'); |
wdenk | 228f29a | 2002-12-08 09:53:23 +0000 | [diff] [blame] | 174 | } |
| 175 | return 0; |
wdenk | 56f94be | 2002-11-05 16:35:14 +0000 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | switch (argc) { |
| 179 | |
| 180 | case 2: |
| 181 | if (strcmp(argv[1],"show") == 0) { |
Igor Lisitsin | 2dc6445 | 2007-04-18 14:55:19 +0400 | [diff] [blame] | 182 | if (log_version == 2) { |
| 183 | start = log->v2.start; |
| 184 | size = log->v2.end - log->v2.start; |
| 185 | } |
| 186 | else { |
| 187 | start = log->v1.start; |
| 188 | size = log->v1.size; |
| 189 | } |
| 190 | for (i=0; i < (size&LOGBUFF_MASK); i++) { |
| 191 | s = (char *)log->buf+((start+i)&LOGBUFF_MASK); |
wdenk | 228f29a | 2002-12-08 09:53:23 +0000 | [diff] [blame] | 192 | putc (*s); |
wdenk | 56f94be | 2002-11-05 16:35:14 +0000 | [diff] [blame] | 193 | } |
| 194 | return 0; |
| 195 | } else if (strcmp(argv[1],"reset") == 0) { |
Igor Lisitsin | 2dc6445 | 2007-04-18 14:55:19 +0400 | [diff] [blame] | 196 | logbuff_reset (); |
wdenk | 56f94be | 2002-11-05 16:35:14 +0000 | [diff] [blame] | 197 | return 0; |
wdenk | 228f29a | 2002-12-08 09:53:23 +0000 | [diff] [blame] | 198 | } else if (strcmp(argv[1],"info") == 0) { |
Igor Lisitsin | 2dc6445 | 2007-04-18 14:55:19 +0400 | [diff] [blame] | 199 | printf ("Logbuffer at %08lx\n", (unsigned long)log->buf); |
| 200 | if (log_version == 2) { |
| 201 | printf ("log_start = %08lx\n", log->v2.start); |
| 202 | printf ("log_end = %08lx\n", log->v2.end); |
| 203 | printf ("logged_chars = %08lx\n", log->v2.chars); |
| 204 | } |
| 205 | else { |
| 206 | printf ("log_start = %08lx\n", log->v1.start); |
| 207 | printf ("log_size = %08lx\n", log->v1.size); |
| 208 | printf ("logged_chars = %08lx\n", log->v1.chars); |
| 209 | } |
wdenk | 56f94be | 2002-11-05 16:35:14 +0000 | [diff] [blame] | 210 | return 0; |
wdenk | 56f94be | 2002-11-05 16:35:14 +0000 | [diff] [blame] | 211 | } |
| 212 | printf ("Usage:\n%s\n", cmdtp->usage); |
| 213 | return 1; |
| 214 | |
| 215 | default: |
| 216 | printf ("Usage:\n%s\n", cmdtp->usage); |
| 217 | return 1; |
| 218 | } |
| 219 | } |
Igor Lisitsin | 2dc6445 | 2007-04-18 14:55:19 +0400 | [diff] [blame] | 220 | |
wdenk | 0d49839 | 2003-07-01 21:06:45 +0000 | [diff] [blame] | 221 | U_BOOT_CMD( |
| 222 | log, 255, 1, do_log, |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 223 | "log - manipulate logbuffer\n", |
wdenk | b37c7e5 | 2003-06-30 16:24:52 +0000 | [diff] [blame] | 224 | "info - show pointer details\n" |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 225 | "log reset - clear contents\n" |
| 226 | "log show - show contents\n" |
| 227 | "log append <msg> - append <msg> to the logbuffer\n" |
| 228 | ); |
Igor Lisitsin | 2dc6445 | 2007-04-18 14:55:19 +0400 | [diff] [blame] | 229 | |
wdenk | 56f94be | 2002-11-05 16:35:14 +0000 | [diff] [blame] | 230 | static int logbuff_printk(const char *line) |
| 231 | { |
| 232 | int i; |
| 233 | char *msg, *p, *buf_end; |
| 234 | int line_feed; |
| 235 | static signed char msg_level = -1; |
| 236 | |
wdenk | 228f29a | 2002-12-08 09:53:23 +0000 | [diff] [blame] | 237 | strcpy (buf + 3, line); |
| 238 | i = strlen (line); |
wdenk | 56f94be | 2002-11-05 16:35:14 +0000 | [diff] [blame] | 239 | buf_end = buf + 3 + i; |
| 240 | for (p = buf + 3; p < buf_end; p++) { |
| 241 | msg = p; |
| 242 | if (msg_level < 0) { |
| 243 | if ( |
| 244 | p[0] != '<' || |
| 245 | p[1] < '0' || |
| 246 | p[1] > '7' || |
| 247 | p[2] != '>' |
| 248 | ) { |
| 249 | p -= 3; |
| 250 | p[0] = '<'; |
| 251 | p[1] = default_message_loglevel + '0'; |
| 252 | p[2] = '>'; |
| 253 | } else |
| 254 | msg += 3; |
| 255 | msg_level = p[1] - '0'; |
| 256 | } |
| 257 | line_feed = 0; |
| 258 | for (; p < buf_end; p++) { |
Igor Lisitsin | 2dc6445 | 2007-04-18 14:55:19 +0400 | [diff] [blame] | 259 | if (log_version == 2) { |
| 260 | log->buf[log->v2.end & LOGBUFF_MASK] = *p; |
| 261 | log->v2.end++; |
| 262 | if (log->v2.end - log->v2.start > LOGBUFF_LEN) |
| 263 | log->v2.start++; |
| 264 | log->v2.chars++; |
| 265 | } |
| 266 | else { |
| 267 | log->buf[(log->v1.start + log->v1.size) & |
| 268 | LOGBUFF_MASK] = *p; |
| 269 | if (log->v1.size < LOGBUFF_LEN) |
| 270 | log->v1.size++; |
| 271 | else |
| 272 | log->v1.start++; |
| 273 | log->v1.chars++; |
| 274 | } |
wdenk | 56f94be | 2002-11-05 16:35:14 +0000 | [diff] [blame] | 275 | if (*p == '\n') { |
| 276 | line_feed = 1; |
| 277 | break; |
| 278 | } |
| 279 | } |
| 280 | if (msg_level < console_loglevel) { |
| 281 | printf("%s", msg); |
| 282 | } |
| 283 | if (line_feed) |
| 284 | msg_level = -1; |
| 285 | } |
| 286 | return i; |
| 287 | } |