post.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /*
  2. * (C) Copyright 2002
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. #include <console.h>
  25. #include <watchdog.h>
  26. #include <post.h>
  27. #ifdef CONFIG_LOGBUFFER
  28. #include <logbuff.h>
  29. #endif
  30. #ifdef CONFIG_POST
  31. #define POST_MAX_NUMBER 32
  32. #define BOOTMODE_MAGIC 0xDEAD0000
  33. void post_bootmode_init (void)
  34. {
  35. int bootmode = post_bootmode_get (0);
  36. if (bootmode == 0) {
  37. bootmode = POST_POWERON;
  38. } else if (bootmode == POST_POWERON) {
  39. bootmode = POST_POWERNORMAL;
  40. } else {
  41. return;
  42. }
  43. post_word_store (BOOTMODE_MAGIC | bootmode);
  44. }
  45. int post_bootmode_get (unsigned int *last_test)
  46. {
  47. unsigned long word = post_word_load ();
  48. int bootmode;
  49. if ((word & 0xFFFF0000) != BOOTMODE_MAGIC) {
  50. return 0;
  51. }
  52. bootmode = word & 0xFF;
  53. if (last_test && (bootmode & POST_POWERTEST)) {
  54. *last_test = (word >> 8) & 0xFF;
  55. }
  56. return bootmode;
  57. }
  58. void post_bootmode_clear (void)
  59. {
  60. post_word_store (0);
  61. }
  62. static void post_bootmode_test_on (unsigned int last_test)
  63. {
  64. unsigned long word = post_word_load ();
  65. word |= POST_POWERTEST;
  66. word |= (last_test & 0xFF) << 8;
  67. post_word_store (word);
  68. }
  69. static void post_bootmode_test_off (void)
  70. {
  71. unsigned long word = post_word_load ();
  72. word &= ~POST_POWERTEST;
  73. post_word_store (word);
  74. }
  75. static void post_get_flags (int *test_flags)
  76. {
  77. int flag[] = { POST_POWERON, POST_POWERNORMAL, POST_POWERFAIL };
  78. char *var[] = { "post_poweron", "post_normal", "post_shutdown" };
  79. int varnum = sizeof (var) / sizeof (var[0]);
  80. char list[128]; /* long enough for POST list */
  81. char *name;
  82. char *s;
  83. int last;
  84. int i, j;
  85. for (j = 0; j < post_list_size; j++) {
  86. test_flags[j] = post_list[j].flags;
  87. }
  88. for (i = 0; i < varnum; i++) {
  89. if (getenv_r (var[i], list, sizeof (list)) <= 0)
  90. continue;
  91. for (j = 0; j < post_list_size; j++) {
  92. test_flags[j] &= ~flag[i];
  93. }
  94. last = 0;
  95. name = list;
  96. while (!last) {
  97. while (*name && *name == ' ')
  98. name++;
  99. if (*name == 0)
  100. break;
  101. s = name + 1;
  102. while (*s && *s != ' ')
  103. s++;
  104. if (*s == 0)
  105. last = 1;
  106. else
  107. *s = 0;
  108. for (j = 0; j < post_list_size; j++) {
  109. if (strcmp (post_list[j].cmd, name) == 0) {
  110. test_flags[j] |= flag[i];
  111. break;
  112. }
  113. }
  114. if (j == post_list_size) {
  115. printf ("No such test: %s\n", name);
  116. }
  117. name = s + 1;
  118. }
  119. }
  120. }
  121. static int post_run_single (struct post_test *test,
  122. int test_flags, int flags, unsigned int i)
  123. {
  124. if ((flags & test_flags & POST_ALWAYS) &&
  125. (flags & test_flags & POST_MEM)) {
  126. WATCHDOG_RESET ();
  127. if (!(flags & POST_REBOOT)) {
  128. if ((test_flags & POST_REBOOT) && !(flags & POST_MANUAL)) {
  129. post_bootmode_test_on (i);
  130. }
  131. post_log ("POST %s ", test->cmd);
  132. }
  133. if ((*test->test) (flags) != 0)
  134. post_log ("FAILED\n");
  135. else
  136. post_log ("PASSED\n");
  137. if ((test_flags & POST_REBOOT) && !(flags & POST_MANUAL)) {
  138. post_bootmode_test_off ();
  139. }
  140. return 0;
  141. } else {
  142. return -1;
  143. }
  144. }
  145. int post_run (char *name, int flags)
  146. {
  147. unsigned int i;
  148. int test_flags[POST_MAX_NUMBER];
  149. post_get_flags (test_flags);
  150. if (name == NULL) {
  151. unsigned int last;
  152. if (post_bootmode_get (&last) & POST_POWERTEST) {
  153. if (last < post_list_size &&
  154. (flags & test_flags[last] & POST_ALWAYS) &&
  155. (flags & test_flags[last] & POST_MEM)) {
  156. post_run_single (post_list + last,
  157. test_flags[last],
  158. flags | POST_REBOOT, last);
  159. for (i = last + 1; i < post_list_size; i++) {
  160. post_run_single (post_list + i,
  161. test_flags[i],
  162. flags, i);
  163. }
  164. }
  165. } else {
  166. for (i = 0; i < post_list_size; i++) {
  167. post_run_single (post_list + i,
  168. test_flags[i],
  169. flags, i);
  170. }
  171. }
  172. return 0;
  173. } else {
  174. for (i = 0; i < post_list_size; i++) {
  175. if (strcmp (post_list[i].cmd, name) == 0)
  176. break;
  177. }
  178. if (i < post_list_size) {
  179. return post_run_single (post_list + i,
  180. test_flags[i],
  181. flags, i);
  182. } else {
  183. return -1;
  184. }
  185. }
  186. }
  187. static int post_info_single (struct post_test *test, int full)
  188. {
  189. if (test->flags & POST_MANUAL) {
  190. if (full)
  191. printf ("%s - %s\n"
  192. " %s\n", test->cmd, test->name, test->desc);
  193. else
  194. printf (" %-15s - %s\n", test->cmd, test->name);
  195. return 0;
  196. } else {
  197. return -1;
  198. }
  199. }
  200. int post_info (char *name)
  201. {
  202. unsigned int i;
  203. if (name == NULL) {
  204. for (i = 0; i < post_list_size; i++) {
  205. post_info_single (post_list + i, 0);
  206. }
  207. return 0;
  208. } else {
  209. for (i = 0; i < post_list_size; i++) {
  210. if (strcmp (post_list[i].cmd, name) == 0)
  211. break;
  212. }
  213. if (i < post_list_size) {
  214. return post_info_single (post_list + i, 1);
  215. } else {
  216. return -1;
  217. }
  218. }
  219. }
  220. int post_log (char *format, ...)
  221. {
  222. va_list args;
  223. uint i;
  224. char printbuffer[CFG_PBSIZE];
  225. va_start (args, format);
  226. /* For this to work, printbuffer must be larger than
  227. * anything we ever want to print.
  228. */
  229. i = vsprintf (printbuffer, format, args);
  230. va_end (args);
  231. #ifdef CONFIG_LOGBUFFER
  232. logbuff_log (printbuffer);
  233. #else
  234. /* Send to the stdout file */
  235. puts (printbuffer);
  236. #endif
  237. return 0;
  238. }
  239. void post_reloc (void)
  240. {
  241. DECLARE_GLOBAL_DATA_PTR;
  242. unsigned int i;
  243. /*
  244. * We have to relocate the test table manually
  245. */
  246. for (i = 0; i < post_list_size; i++) {
  247. ulong addr;
  248. struct post_test *test = post_list + i;
  249. if (test->name) {
  250. addr = (ulong) (test->name) + gd->reloc_off;
  251. test->name = (char *) addr;
  252. }
  253. if (test->cmd) {
  254. addr = (ulong) (test->cmd) + gd->reloc_off;
  255. test->cmd = (char *) addr;
  256. }
  257. if (test->desc) {
  258. addr = (ulong) (test->desc) + gd->reloc_off;
  259. test->desc = (char *) addr;
  260. }
  261. if (test->test) {
  262. addr = (ulong) (test->test) + gd->reloc_off;
  263. test->test = (int (*)(int flags)) addr;
  264. }
  265. }
  266. }
  267. #endif /* CONFIG_POST */