post.c 7.3 KB

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