cmd_log.c 6.4 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. char *s;
  70. log_buf = (unsigned char *)(gd->bd->bi_memsize-LOGBUFF_LEN);
  71. ext_log_start = (unsigned long *)(log_buf)-3;
  72. ext_log_size = (unsigned long *)(log_buf)-2;
  73. ext_logged_chars = (unsigned long *)(log_buf)-1;
  74. #ifdef CONFIG_POST
  75. /* The post routines have setup the word so we can simply test it */
  76. if (post_word_load () & POST_POWERON) {
  77. logged_chars = log_size = log_start = 0;
  78. }
  79. #else
  80. /* No post routines, so we do our own checking */
  81. if (post_word_load () != LOGBUFF_MAGIC) {
  82. logged_chars = log_size = log_start = 0;
  83. post_word_store (LOGBUFF_MAGIC);
  84. }
  85. #endif
  86. /* Initialize default loglevel if present */
  87. if ((s = getenv ("loglevel")) != NULL)
  88. console_loglevel = (int)simple_strtoul (s, NULL, 10);
  89. gd->post_log_word |= LOGBUFF_INITIALIZED;
  90. }
  91. int drv_logbuff_init (void)
  92. {
  93. device_t logdev;
  94. int rc;
  95. /* Device initialization */
  96. memset (&logdev, 0, sizeof (logdev));
  97. strcpy (logdev.name, "logbuff");
  98. logdev.ext = 0; /* No extensions */
  99. logdev.flags = DEV_FLAGS_OUTPUT; /* Output only */
  100. logdev.putc = logbuff_putc; /* 'putc' function */
  101. logdev.puts = logbuff_puts; /* 'puts' function */
  102. rc = device_register (&logdev);
  103. return (rc == 0) ? 1 : rc;
  104. }
  105. static void logbuff_putc (const char c)
  106. {
  107. char buf[2];
  108. buf[0] = c;
  109. buf[1] = '\0';
  110. logbuff_printk (buf);
  111. }
  112. static void logbuff_puts (const char *s)
  113. {
  114. logbuff_printk (s);
  115. }
  116. void logbuff_log(char *msg)
  117. {
  118. DECLARE_GLOBAL_DATA_PTR;
  119. if ((gd->post_log_word & LOGBUFF_INITIALIZED)) {
  120. logbuff_printk (msg);
  121. } else {
  122. /* Can happen only for pre-relocated errors as logging */
  123. /* at that stage should be disabled */
  124. puts (msg);
  125. }
  126. }
  127. /*
  128. * Subroutine: do_log
  129. *
  130. * Description: Handler for 'log' command..
  131. *
  132. * Inputs: argv[1] contains the subcommand
  133. *
  134. * Return: None
  135. *
  136. */
  137. int do_log (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  138. {
  139. char *s;
  140. unsigned long i;
  141. if (strcmp(argv[1],"append") == 0) {
  142. /* Log concatenation of all arguments separated by spaces */
  143. for (i=2; i<argc; i++) {
  144. if (i<argc-1) {
  145. logbuff_printk (argv[i]);
  146. logbuff_putc (' ');
  147. } else {
  148. logbuff_puts (argv[i]);
  149. }
  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. "log 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) */