cmd_log.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. * (C) Copyright 2002
  3. * Detlev Zundel, DENX Software Engineering, dzu@denx.de.
  4. *
  5. * Code used from linux/kernel/printk.c
  6. * Copyright (C) 1991, 1992 Linus Torvalds
  7. *
  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
  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.
  37. */
  38. /*
  39. * Logbuffer handling routines
  40. */
  41. #include <common.h>
  42. #include <command.h>
  43. #include <devices.h>
  44. #include <post.h>
  45. #include <logbuff.h>
  46. #if defined(CONFIG_LOGBUFFER)
  47. /* Local prototypes */
  48. static void logbuff_putc (const char c);
  49. static void logbuff_puts (const char *s);
  50. static int logbuff_printk(const char *line);
  51. static char buf[1024];
  52. /* This combination will not print messages with the default loglevel */
  53. static unsigned console_loglevel = 3;
  54. static unsigned default_message_loglevel = 4;
  55. static unsigned char *log_buf = NULL;
  56. static unsigned long *ext_log_size;
  57. static unsigned long *ext_log_start;
  58. static unsigned long *ext_logged_chars;
  59. #define log_size (*ext_log_size)
  60. #define log_start (*ext_log_start)
  61. #define logged_chars (*ext_logged_chars)
  62. /* Forced by code, eh! */
  63. #define LOGBUFF_MAGIC 0xc0de4ced
  64. /* The mapping used here has to be the same as in setup_ext_logbuff ()
  65. in linux/kernel/printk */
  66. void logbuff_init_ptrs (void)
  67. {
  68. DECLARE_GLOBAL_DATA_PTR;
  69. unsigned long *ext_tag;
  70. char *s;
  71. log_buf = (unsigned char *)(gd->bd->bi_memsize-LOGBUFF_LEN);
  72. ext_tag = (unsigned long *)(log_buf)-4;
  73. ext_log_start = (unsigned long *)(log_buf)-3;
  74. ext_log_size = (unsigned long *)(log_buf)-2;
  75. ext_logged_chars = (unsigned long *)(log_buf)-1;
  76. #ifdef CONFIG_POST
  77. /* The post routines have setup the word so we can simply test it */
  78. if ((post_word_load () & 0xffff) == POST_POWERON) {
  79. logged_chars = log_size = log_start = 0;
  80. *ext_tag = LOGBUFF_MAGIC;
  81. }
  82. #else
  83. /* No post routines, so we do our own checking */
  84. if (post_word_load () != LOGBUFF_MAGIC) {
  85. logged_chars = log_size = log_start = 0;
  86. post_word_store (LOGBUFF_MAGIC);
  87. *ext_tag = LOGBUFF_MAGIC;
  88. }
  89. #endif
  90. /* Initialize default loglevel if present */
  91. if ((s = getenv ("loglevel")) != NULL)
  92. console_loglevel = (int)simple_strtoul (s, NULL, 10);
  93. gd->post_log_word |= LOGBUFF_INITIALIZED;
  94. }
  95. int drv_logbuff_init (void)
  96. {
  97. device_t logdev;
  98. int rc;
  99. /* Device initialization */
  100. memset (&logdev, 0, sizeof (logdev));
  101. strcpy (logdev.name, "logbuff");
  102. logdev.ext = 0; /* No extensions */
  103. logdev.flags = DEV_FLAGS_OUTPUT; /* Output only */
  104. logdev.putc = logbuff_putc; /* 'putc' function */
  105. logdev.puts = logbuff_puts; /* 'puts' function */
  106. rc = device_register (&logdev);
  107. return (rc == 0) ? 1 : rc;
  108. }
  109. static void logbuff_putc (const char c)
  110. {
  111. char buf[2];
  112. buf[0] = c;
  113. buf[1] = '\0';
  114. logbuff_printk (buf);
  115. }
  116. static void logbuff_puts (const char *s)
  117. {
  118. logbuff_printk (s);
  119. }
  120. void logbuff_log(char *msg)
  121. {
  122. DECLARE_GLOBAL_DATA_PTR;
  123. if ((gd->post_log_word & LOGBUFF_INITIALIZED)) {
  124. logbuff_printk (msg);
  125. } else {
  126. /* Can happen only for pre-relocated errors as logging */
  127. /* at that stage should be disabled */
  128. puts (msg);
  129. }
  130. }
  131. /*
  132. * Subroutine: do_log
  133. *
  134. * Description: Handler for 'log' command..
  135. *
  136. * Inputs: argv[1] contains the subcommand
  137. *
  138. * Return: None
  139. *
  140. */
  141. int do_log (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  142. {
  143. char *s;
  144. unsigned long i;
  145. if (strcmp(argv[1],"append") == 0) {
  146. /* Log concatenation of all arguments separated by spaces */
  147. for (i=2; i<argc; i++) {
  148. logbuff_printk (argv[i]);
  149. logbuff_putc ((i<argc-1) ? ' ' : '\n');
  150. }
  151. return 0;
  152. }
  153. switch (argc) {
  154. case 2:
  155. if (strcmp(argv[1],"show") == 0) {
  156. for (i=0; i < (log_size&LOGBUFF_MASK); i++) {
  157. s = log_buf+((log_start+i)&LOGBUFF_MASK);
  158. putc (*s);
  159. }
  160. return 0;
  161. } else if (strcmp(argv[1],"reset") == 0) {
  162. log_start = 0;
  163. log_size = 0;
  164. logged_chars = 0;
  165. return 0;
  166. } else if (strcmp(argv[1],"info") == 0) {
  167. printf ("Logbuffer at %08lx\n", (unsigned long)log_buf);
  168. printf ("log_start = %08lx\n", log_start);
  169. printf ("log_size = %08lx\n", log_size);
  170. printf ("logged_chars = %08lx\n", logged_chars);
  171. return 0;
  172. }
  173. printf ("Usage:\n%s\n", cmdtp->usage);
  174. return 1;
  175. default:
  176. printf ("Usage:\n%s\n", cmdtp->usage);
  177. return 1;
  178. }
  179. }
  180. #if defined(CONFIG_LOGBUFFER)
  181. cmd_tbl_t U_BOOT_CMD(LOG) = MK_CMD_ENTRY(
  182. "log", 255, 1, do_log,
  183. "log - manipulate logbuffer\n",
  184. "info - show pointer details\n"
  185. "log reset - clear contents\n"
  186. "log show - show contents\n"
  187. "log append <msg> - append <msg> to the logbuffer\n"
  188. );
  189. #endif /* CONFIG_LOGBUFFER */
  190. static int logbuff_printk(const char *line)
  191. {
  192. int i;
  193. char *msg, *p, *buf_end;
  194. int line_feed;
  195. static signed char msg_level = -1;
  196. strcpy (buf + 3, line);
  197. i = strlen (line);
  198. buf_end = buf + 3 + i;
  199. for (p = buf + 3; p < buf_end; p++) {
  200. msg = p;
  201. if (msg_level < 0) {
  202. if (
  203. p[0] != '<' ||
  204. p[1] < '0' ||
  205. p[1] > '7' ||
  206. p[2] != '>'
  207. ) {
  208. p -= 3;
  209. p[0] = '<';
  210. p[1] = default_message_loglevel + '0';
  211. p[2] = '>';
  212. } else
  213. msg += 3;
  214. msg_level = p[1] - '0';
  215. }
  216. line_feed = 0;
  217. for (; p < buf_end; p++) {
  218. log_buf[(log_start+log_size) & LOGBUFF_MASK] = *p;
  219. if (log_size < LOGBUFF_LEN)
  220. log_size++;
  221. else
  222. log_start++;
  223. logged_chars++;
  224. if (*p == '\n') {
  225. line_feed = 1;
  226. break;
  227. }
  228. }
  229. if (msg_level < console_loglevel) {
  230. printf("%s", msg);
  231. }
  232. if (line_feed)
  233. msg_level = -1;
  234. }
  235. return i;
  236. }
  237. #endif /* (CONFIG_LOGBUFFER) */