cmd_log.c 6.2 KB

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