cmd_log.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. unsigned long post_word;
  71. char *s;
  72. log_buf = (unsigned char *)(gd->bd->bi_memsize-LOGBUFF_LEN);
  73. ext_tag = (unsigned long *)(log_buf)-4;
  74. ext_log_start = (unsigned long *)(log_buf)-3;
  75. ext_log_size = (unsigned long *)(log_buf)-2;
  76. ext_logged_chars = (unsigned long *)(log_buf)-1;
  77. post_word = post_word_load();
  78. #ifdef CONFIG_POST
  79. /* The post routines have setup the word so we can simply test it */
  80. if (post_word_load () & POST_COLDBOOT) {
  81. logged_chars = log_size = log_start = 0;
  82. *ext_tag = LOGBUFF_MAGIC;
  83. }
  84. #else
  85. /* No post routines, so we do our own checking */
  86. if (post_word != LOGBUFF_MAGIC) {
  87. logged_chars = log_size = log_start = 0;
  88. post_word_store (LOGBUFF_MAGIC);
  89. *ext_tag = LOGBUFF_MAGIC;
  90. }
  91. #endif
  92. /* Initialize default loglevel if present */
  93. if ((s = getenv ("loglevel")) != NULL)
  94. console_loglevel = (int)simple_strtoul (s, NULL, 10);
  95. gd->post_log_word |= LOGBUFF_INITIALIZED;
  96. }
  97. int drv_logbuff_init (void)
  98. {
  99. device_t logdev;
  100. int rc;
  101. /* Device initialization */
  102. memset (&logdev, 0, sizeof (logdev));
  103. strcpy (logdev.name, "logbuff");
  104. logdev.ext = 0; /* No extensions */
  105. logdev.flags = DEV_FLAGS_OUTPUT; /* Output only */
  106. logdev.putc = logbuff_putc; /* 'putc' function */
  107. logdev.puts = logbuff_puts; /* 'puts' function */
  108. rc = device_register (&logdev);
  109. return (rc == 0) ? 1 : rc;
  110. }
  111. static void logbuff_putc (const char c)
  112. {
  113. char buf[2];
  114. buf[0] = c;
  115. buf[1] = '\0';
  116. logbuff_printk (buf);
  117. }
  118. static void logbuff_puts (const char *s)
  119. {
  120. logbuff_printk (s);
  121. }
  122. void logbuff_log(char *msg)
  123. {
  124. DECLARE_GLOBAL_DATA_PTR;
  125. if ((gd->post_log_word & LOGBUFF_INITIALIZED)) {
  126. logbuff_printk (msg);
  127. } else {
  128. /* Can happen only for pre-relocated errors as logging */
  129. /* at that stage should be disabled */
  130. puts (msg);
  131. }
  132. }
  133. /*
  134. * Subroutine: do_log
  135. *
  136. * Description: Handler for 'log' command..
  137. *
  138. * Inputs: argv[1] contains the subcommand
  139. *
  140. * Return: None
  141. *
  142. */
  143. int do_log (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  144. {
  145. char *s;
  146. unsigned long i;
  147. if (strcmp(argv[1],"append") == 0) {
  148. /* Log concatenation of all arguments separated by spaces */
  149. for (i=2; i<argc; i++) {
  150. logbuff_printk (argv[i]);
  151. logbuff_putc ((i<argc-1) ? ' ' : '\n');
  152. }
  153. return 0;
  154. }
  155. switch (argc) {
  156. case 2:
  157. if (strcmp(argv[1],"show") == 0) {
  158. for (i=0; i < (log_size&LOGBUFF_MASK); i++) {
  159. s = log_buf+((log_start+i)&LOGBUFF_MASK);
  160. putc (*s);
  161. }
  162. return 0;
  163. } else if (strcmp(argv[1],"reset") == 0) {
  164. log_start = 0;
  165. log_size = 0;
  166. logged_chars = 0;
  167. return 0;
  168. } else if (strcmp(argv[1],"info") == 0) {
  169. printf ("Logbuffer at %08lx\n", (unsigned long)log_buf);
  170. printf ("log_start = %08lx\n", log_start);
  171. printf ("log_size = %08lx\n", log_size);
  172. printf ("logged_chars = %08lx\n", logged_chars);
  173. return 0;
  174. }
  175. printf ("Usage:\n%s\n", cmdtp->usage);
  176. return 1;
  177. default:
  178. printf ("Usage:\n%s\n", cmdtp->usage);
  179. return 1;
  180. }
  181. }
  182. #if defined(CONFIG_LOGBUFFER)
  183. U_BOOT_CMD(
  184. log, 255, 1, do_log,
  185. "log - manipulate logbuffer\n",
  186. "info - show pointer details\n"
  187. "log reset - clear contents\n"
  188. "log show - show contents\n"
  189. "log append <msg> - append <msg> to the logbuffer\n"
  190. );
  191. #endif /* CONFIG_LOGBUFFER */
  192. static int logbuff_printk(const char *line)
  193. {
  194. int i;
  195. char *msg, *p, *buf_end;
  196. int line_feed;
  197. static signed char msg_level = -1;
  198. strcpy (buf + 3, line);
  199. i = strlen (line);
  200. buf_end = buf + 3 + i;
  201. for (p = buf + 3; p < buf_end; p++) {
  202. msg = p;
  203. if (msg_level < 0) {
  204. if (
  205. p[0] != '<' ||
  206. p[1] < '0' ||
  207. p[1] > '7' ||
  208. p[2] != '>'
  209. ) {
  210. p -= 3;
  211. p[0] = '<';
  212. p[1] = default_message_loglevel + '0';
  213. p[2] = '>';
  214. } else
  215. msg += 3;
  216. msg_level = p[1] - '0';
  217. }
  218. line_feed = 0;
  219. for (; p < buf_end; p++) {
  220. log_buf[(log_start+log_size) & LOGBUFF_MASK] = *p;
  221. if (log_size < LOGBUFF_LEN)
  222. log_size++;
  223. else
  224. log_start++;
  225. logged_chars++;
  226. if (*p == '\n') {
  227. line_feed = 1;
  228. break;
  229. }
  230. }
  231. if (msg_level < console_loglevel) {
  232. printf("%s", msg);
  233. }
  234. if (line_feed)
  235. msg_level = -1;
  236. }
  237. return i;
  238. }
  239. #endif /* (CONFIG_LOGBUFFER) */