post.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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_POST
  28. #define POST_MAX_NUMBER 32
  29. #define BOOTMODE_MAGIC 0xDEAD0000
  30. void post_bootmode_init (void)
  31. {
  32. int bootmode = post_bootmode_get (0);
  33. if (bootmode == 0) {
  34. bootmode = POST_POWERON;
  35. } else if (bootmode == POST_POWERON) {
  36. bootmode = POST_POWERNORMAL;
  37. } else {
  38. return;
  39. }
  40. post_word_store (BOOTMODE_MAGIC | bootmode);
  41. }
  42. int post_bootmode_get (unsigned int *last_test)
  43. {
  44. unsigned long word = post_word_load ();
  45. int bootmode;
  46. if ((word & 0xFFFF0000) != BOOTMODE_MAGIC) {
  47. return 0;
  48. }
  49. bootmode = word & 0xFF;
  50. if (last_test && (bootmode & POST_POWERTEST)) {
  51. *last_test = (word >> 8) & 0xFF;
  52. }
  53. return bootmode;
  54. }
  55. void post_bootmode_clear (void)
  56. {
  57. post_word_store (0);
  58. }
  59. static void post_bootmode_test_on (unsigned int last_test)
  60. {
  61. unsigned long word = post_word_load ();
  62. word |= POST_POWERTEST;
  63. word |= (last_test & 0xFF) << 8;
  64. post_word_store (word);
  65. }
  66. static void post_bootmode_test_off (void)
  67. {
  68. unsigned long word = post_word_load ();
  69. word &= ~POST_POWERTEST;
  70. post_word_store (word);
  71. }
  72. static void post_get_flags (int *test_flags)
  73. {
  74. int flag[] = { POST_POWERON, POST_POWERNORMAL, POST_POWERFAIL };
  75. char *var[] = { "post_poweron", "post_normal", "post_shutdown" };
  76. int varnum = sizeof (var) / sizeof (var[0]);
  77. char list[128]; /* long enough for POST list */
  78. char *name;
  79. char *s;
  80. int last;
  81. int i, j;
  82. for (j = 0; j < post_list_size; j++) {
  83. test_flags[j] = post_list[j].flags;
  84. }
  85. for (i = 0; i < varnum; i++) {
  86. if (getenv_r (var[i], list, sizeof (list)) <= 0)
  87. continue;
  88. for (j = 0; j < post_list_size; j++) {
  89. test_flags[j] &= ~flag[i];
  90. }
  91. last = 0;
  92. name = list;
  93. while (!last) {
  94. while (*name && *name == ' ')
  95. name++;
  96. if (*name == 0)
  97. break;
  98. s = name + 1;
  99. while (*s && *s != ' ')
  100. s++;
  101. if (*s == 0)
  102. last = 1;
  103. else
  104. *s = 0;
  105. for (j = 0; j < post_list_size; j++) {
  106. if (strcmp (post_list[j].cmd, name) == 0) {
  107. test_flags[j] |= flag[i];
  108. break;
  109. }
  110. }
  111. if (j == post_list_size) {
  112. printf ("No such test: %s\n", name);
  113. }
  114. name = s + 1;
  115. }
  116. }
  117. }
  118. static int post_run_single (struct post_test *test,
  119. int test_flags, int flags, unsigned int i)
  120. {
  121. if ((flags & test_flags & POST_ALWAYS) &&
  122. (flags & test_flags & POST_MEM)) {
  123. WATCHDOG_RESET ();
  124. if (!(flags & POST_REBOOT)) {
  125. if ((test_flags & POST_REBOOT) && !(flags & POST_MANUAL)) {
  126. post_bootmode_test_on (i);
  127. }
  128. post_log ("START %s\n", test->cmd);
  129. }
  130. if ((*test->test) (flags) != 0)
  131. post_log ("FAILED\n");
  132. else
  133. post_log ("PASSED\n");
  134. if ((test_flags & POST_REBOOT) && !(flags & POST_MANUAL)) {
  135. post_bootmode_test_off ();
  136. }
  137. return 0;
  138. } else {
  139. return -1;
  140. }
  141. }
  142. int post_run (char *name, int flags)
  143. {
  144. unsigned int i;
  145. int test_flags[POST_MAX_NUMBER];
  146. post_get_flags (test_flags);
  147. if (name == NULL) {
  148. unsigned int last;
  149. if (post_bootmode_get (&last) & POST_POWERTEST) {
  150. if (last < post_list_size &&
  151. (flags & test_flags[last] & POST_ALWAYS) &&
  152. (flags & test_flags[last] & POST_MEM)) {
  153. post_run_single (post_list + last, test_flags[last],
  154. flags | POST_REBOOT, last);
  155. for (i = last + 1; i < post_list_size; i++) {
  156. post_run_single (post_list + i, test_flags[i],
  157. flags, i);
  158. }
  159. }
  160. } else {
  161. for (i = 0; i < post_list_size; i++) {
  162. post_run_single (post_list + i, test_flags[i], flags,
  163. i);
  164. }
  165. }
  166. return 0;
  167. } else {
  168. for (i = 0; i < post_list_size; i++) {
  169. if (strcmp (post_list[i].cmd, name) == 0)
  170. break;
  171. }
  172. if (i < post_list_size) {
  173. return post_run_single (post_list + i,
  174. test_flags[i],
  175. flags, i);
  176. } else {
  177. return -1;
  178. }
  179. }
  180. }
  181. static int post_info_single (struct post_test *test, int full)
  182. {
  183. if (test->flags & POST_MANUAL) {
  184. if (full)
  185. printf ("%s - %s\n"
  186. " %s\n", test->cmd, test->name, test->desc);
  187. else
  188. printf (" %-15s - %s\n", test->cmd, test->name);
  189. return 0;
  190. } else {
  191. return -1;
  192. }
  193. }
  194. int post_info (char *name)
  195. {
  196. unsigned int i;
  197. if (name == NULL) {
  198. for (i = 0; i < post_list_size; i++) {
  199. post_info_single (post_list + i, 0);
  200. }
  201. return 0;
  202. } else {
  203. for (i = 0; i < post_list_size; i++) {
  204. if (strcmp (post_list[i].cmd, name) == 0)
  205. break;
  206. }
  207. if (i < post_list_size) {
  208. return post_info_single (post_list + i, 1);
  209. } else {
  210. return -1;
  211. }
  212. }
  213. }
  214. int post_log (char *format, ...)
  215. {
  216. va_list args;
  217. uint i;
  218. char printbuffer[CFG_PBSIZE];
  219. va_start (args, format);
  220. /* For this to work, printbuffer must be larger than
  221. * anything we ever want to print.
  222. */
  223. i = vsprintf (printbuffer, format, args);
  224. va_end (args);
  225. /* Send to the stdout file */
  226. puts (printbuffer);
  227. return 0;
  228. }
  229. void post_reloc (void)
  230. {
  231. DECLARE_GLOBAL_DATA_PTR;
  232. unsigned int i;
  233. /*
  234. * We have to relocate the test table manually
  235. */
  236. for (i = 0; i < post_list_size; i++) {
  237. ulong addr;
  238. struct post_test *test = post_list + i;
  239. if (test->name) {
  240. addr = (ulong) (test->name) + gd->reloc_off;
  241. test->name = (char *) addr;
  242. }
  243. if (test->cmd) {
  244. addr = (ulong) (test->cmd) + gd->reloc_off;
  245. test->cmd = (char *) addr;
  246. }
  247. if (test->desc) {
  248. addr = (ulong) (test->desc) + gd->reloc_off;
  249. test->desc = (char *) addr;
  250. }
  251. if (test->test) {
  252. addr = (ulong) (test->test) + gd->reloc_off;
  253. test->test = (int (*)(int flags)) addr;
  254. }
  255. }
  256. }
  257. #endif /* CONFIG_POST */