print_ut.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2012, The Chromium Authors
  4. */
  5. #include <common.h>
  6. #include <command.h>
  7. #include <efi_api.h>
  8. #include <display_options.h>
  9. #include <log.h>
  10. #include <mapmem.h>
  11. #include <version.h>
  12. #include <test/suites.h>
  13. #include <test/test.h>
  14. #include <test/ut.h>
  15. #define BUF_SIZE 0x100
  16. #define FAKE_BUILD_TAG "jenkins-u-boot-denx_uboot_dm-master-build-aarch64" \
  17. "and a lot more text to come"
  18. /* Declare a new print test */
  19. #define PRINT_TEST(_name, _flags) UNIT_TEST(_name, _flags, print_test)
  20. #if CONFIG_IS_ENABLED(LIB_UUID)
  21. /* Test printing GUIDs */
  22. static int print_guid(struct unit_test_state *uts)
  23. {
  24. unsigned char guid[16] = {
  25. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16
  26. };
  27. char str[40];
  28. sprintf(str, "%pUb", guid);
  29. ut_assertok(strcmp("01020304-0506-0708-090a-0b0c0d0e0f10", str));
  30. sprintf(str, "%pUB", guid);
  31. ut_assertok(strcmp("01020304-0506-0708-090A-0B0C0D0E0F10", str));
  32. sprintf(str, "%pUl", guid);
  33. ut_assertok(strcmp("04030201-0605-0807-090a-0b0c0d0e0f10", str));
  34. sprintf(str, "%pUL", guid);
  35. ut_assertok(strcmp("04030201-0605-0807-090A-0B0C0D0E0F10", str));
  36. return 0;
  37. }
  38. PRINT_TEST(print_guid, 0);
  39. #endif
  40. #if CONFIG_IS_ENABLED(EFI_LOADER) && !defined(API_BUILD)
  41. /* Test efi_loader specific printing */
  42. static int print_efi_ut(struct unit_test_state *uts)
  43. {
  44. char str[10];
  45. u8 buf[sizeof(struct efi_device_path_sd_mmc_path) +
  46. sizeof(struct efi_device_path)];
  47. u8 *pos = buf;
  48. struct efi_device_path *dp_end;
  49. struct efi_device_path_sd_mmc_path *dp_sd =
  50. (struct efi_device_path_sd_mmc_path *)pos;
  51. /* Create a device path for an SD card */
  52. dp_sd->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
  53. dp_sd->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_SD;
  54. dp_sd->dp.length = sizeof(struct efi_device_path_sd_mmc_path);
  55. dp_sd->slot_number = 3;
  56. pos += sizeof(struct efi_device_path_sd_mmc_path);
  57. /* Append end node */
  58. dp_end = (struct efi_device_path *)pos;
  59. dp_end->type = DEVICE_PATH_TYPE_END;
  60. dp_end->sub_type = DEVICE_PATH_SUB_TYPE_END;
  61. dp_end->length = sizeof(struct efi_device_path);
  62. snprintf(str, sizeof(str), "_%pD_", buf);
  63. ut_assertok(strcmp("_/SD(3)_", str));
  64. /* NULL device path */
  65. snprintf(str, sizeof(str), "_%pD_", NULL);
  66. ut_assertok(strcmp("_<NULL>_", str));
  67. return 0;
  68. }
  69. PRINT_TEST(print_efi_ut, 0);
  70. #endif
  71. static int print_printf(struct unit_test_state *uts)
  72. {
  73. char big_str[400];
  74. int big_str_len;
  75. char str[10], *s;
  76. int len;
  77. snprintf(str, sizeof(str), "testing");
  78. ut_assertok(strcmp("testing", str));
  79. snprintf(str, sizeof(str), "testing but too long");
  80. ut_assertok(strcmp("testing b", str));
  81. snprintf(str, 1, "testing none");
  82. ut_assertok(strcmp("", str));
  83. *str = 'x';
  84. snprintf(str, 0, "testing none");
  85. ut_asserteq('x', *str);
  86. sprintf(big_str, "_%ls_", L"foo");
  87. ut_assertok(strcmp("_foo_", big_str));
  88. /* Test the banner function */
  89. s = display_options_get_banner(true, str, sizeof(str));
  90. ut_asserteq_ptr(str, s);
  91. ut_assertok(strcmp("\n\nU-Boo\n\n", s));
  92. /* Assert that we do not overwrite memory before the buffer */
  93. str[0] = '`';
  94. s = display_options_get_banner(true, str + 1, 1);
  95. ut_asserteq_ptr(str + 1, s);
  96. ut_assertok(strcmp("`", str));
  97. str[0] = '~';
  98. s = display_options_get_banner(true, str + 1, 2);
  99. ut_asserteq_ptr(str + 1, s);
  100. ut_assertok(strcmp("~\n", str));
  101. /* The last two characters are set to \n\n for all buffer sizes > 2 */
  102. s = display_options_get_banner(false, str, sizeof(str));
  103. ut_asserteq_ptr(str, s);
  104. ut_assertok(strcmp("U-Boot \n\n", s));
  105. /* Give it enough space for some of the version */
  106. big_str_len = strlen(version_string) - 5;
  107. s = display_options_get_banner_priv(false, FAKE_BUILD_TAG, big_str,
  108. big_str_len);
  109. ut_asserteq_ptr(big_str, s);
  110. ut_assertok(strncmp(version_string, s, big_str_len - 3));
  111. ut_assertok(strcmp("\n\n", s + big_str_len - 3));
  112. /* Give it enough space for the version and some of the build tag */
  113. big_str_len = strlen(version_string) + 9 + 20;
  114. s = display_options_get_banner_priv(false, FAKE_BUILD_TAG, big_str,
  115. big_str_len);
  116. ut_asserteq_ptr(big_str, s);
  117. len = strlen(version_string);
  118. ut_assertok(strncmp(version_string, s, len));
  119. ut_assertok(strncmp(", Build: ", s + len, 9));
  120. ut_assertok(strncmp(FAKE_BUILD_TAG, s + 9 + len, 12));
  121. ut_assertok(strcmp("\n\n", s + big_str_len - 3));
  122. return 0;
  123. }
  124. PRINT_TEST(print_printf, 0);
  125. static int print_display_buffer(struct unit_test_state *uts)
  126. {
  127. u8 *buf;
  128. int i;
  129. buf = map_sysmem(0, BUF_SIZE);
  130. memset(buf, '\0', BUF_SIZE);
  131. for (i = 0; i < 0x11; i++)
  132. buf[i] = i * 0x11;
  133. /* bytes */
  134. console_record_reset();
  135. print_buffer(0, buf, 1, 0x12, 0);
  136. ut_assert_nextline("00000000: 00 11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff ..\"3DUfw........");
  137. ut_assert_nextline("00000010: 10 00 ..");
  138. ut_assert_console_end();
  139. /* line length */
  140. console_record_reset();
  141. print_buffer(0, buf, 1, 0x12, 8);
  142. ut_assert_nextline("00000000: 00 11 22 33 44 55 66 77 ..\"3DUfw");
  143. ut_assert_nextline("00000008: 88 99 aa bb cc dd ee ff ........");
  144. ut_assert_nextline("00000010: 10 00 ..");
  145. ut_assert_console_end();
  146. /* long line */
  147. console_record_reset();
  148. buf[0x41] = 0x41;
  149. print_buffer(0, buf, 1, 0x42, 0x40);
  150. ut_assert_nextline("00000000: 00 11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff 10 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ..\"3DUfw........................................................");
  151. ut_assert_nextline("00000040: 00 41 .A");
  152. ut_assert_console_end();
  153. /* address */
  154. console_record_reset();
  155. print_buffer(0x12345678, buf, 1, 0x12, 0);
  156. ut_assert_nextline("12345678: 00 11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff ..\"3DUfw........");
  157. ut_assert_nextline("12345688: 10 00 ..");
  158. ut_assert_console_end();
  159. /* 16-bit */
  160. console_record_reset();
  161. print_buffer(0, buf, 2, 9, 0);
  162. ut_assert_nextline("00000000: 1100 3322 5544 7766 9988 bbaa ddcc ffee ..\"3DUfw........");
  163. ut_assert_nextline("00000010: 0010 ..");
  164. ut_assert_console_end();
  165. /* 32-bit */
  166. console_record_reset();
  167. print_buffer(0, buf, 4, 5, 0);
  168. ut_assert_nextline("00000000: 33221100 77665544 bbaa9988 ffeeddcc ..\"3DUfw........");
  169. ut_assert_nextline("00000010: 00000010 ....");
  170. ut_assert_console_end();
  171. /* 64-bit */
  172. console_record_reset();
  173. print_buffer(0, buf, 8, 3, 0);
  174. ut_assert_nextline("00000000: 7766554433221100 ffeeddccbbaa9988 ..\"3DUfw........");
  175. ut_assert_nextline("00000010: 0000000000000010 ........");
  176. ut_assert_console_end();
  177. /* ASCII */
  178. console_record_reset();
  179. buf[1] = 31;
  180. buf[2] = 32;
  181. buf[3] = 33;
  182. for (i = 0; i < 4; i++)
  183. buf[4 + i] = 126 + i;
  184. buf[8] = 255;
  185. print_buffer(0, buf, 1, 10, 0);
  186. ut_assert_nextline("00000000: 00 1f 20 21 7e 7f 80 81 ff 99 .. !~.....");
  187. ut_assert_console_end();
  188. unmap_sysmem(buf);
  189. return 0;
  190. }
  191. PRINT_TEST(print_display_buffer, UT_TESTF_CONSOLE_REC);
  192. static int print_hexdump_line(struct unit_test_state *uts)
  193. {
  194. char *linebuf;
  195. u8 *buf;
  196. int i;
  197. buf = map_sysmem(0, BUF_SIZE);
  198. memset(buf, '\0', BUF_SIZE);
  199. for (i = 0; i < 0x11; i++)
  200. buf[i] = i * 0x11;
  201. /* Check buffer size calculations */
  202. linebuf = map_sysmem(0x400, BUF_SIZE);
  203. memset(linebuf, '\xff', BUF_SIZE);
  204. ut_asserteq(-ENOSPC, hexdump_line(0, buf, 1, 0x10, 0, linebuf, 75));
  205. ut_asserteq(-1, linebuf[0]);
  206. ut_asserteq(0x10, hexdump_line(0, buf, 1, 0x10, 0, linebuf, 76));
  207. ut_asserteq(0, linebuf[75]);
  208. ut_asserteq(-1, linebuf[76]);
  209. unmap_sysmem(buf);
  210. return 0;
  211. }
  212. PRINT_TEST(print_hexdump_line, UT_TESTF_CONSOLE_REC);
  213. static int print_do_hex_dump(struct unit_test_state *uts)
  214. {
  215. u8 *buf;
  216. int i;
  217. buf = map_sysmem(0, BUF_SIZE);
  218. memset(buf, '\0', BUF_SIZE);
  219. for (i = 0; i < 0x11; i++)
  220. buf[i] = i * 0x11;
  221. /* bytes */
  222. console_record_reset();
  223. print_hex_dump_bytes("", DUMP_PREFIX_ADDRESS, buf, 0x12);
  224. ut_assert_nextline("00000000: 00 11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff ..\"3DUfw........");
  225. ut_assert_nextline("00000010: 10 00 ..");
  226. ut_assert_console_end();
  227. /* line length */
  228. console_record_reset();
  229. print_hex_dump("", DUMP_PREFIX_ADDRESS, 8, 1, buf, 0x12, true);
  230. ut_assert_nextline("00000000: 00 11 22 33 44 55 66 77 ..\"3DUfw");
  231. ut_assert_nextline("00000008: 88 99 aa bb cc dd ee ff ........");
  232. ut_assert_nextline("00000010: 10 00 ..");
  233. ut_assert_console_end();
  234. unmap_sysmem(buf);
  235. /* long line */
  236. console_record_reset();
  237. buf[0x41] = 0x41;
  238. print_hex_dump("", DUMP_PREFIX_ADDRESS, 0x40, 1, buf, 0x42, true);
  239. ut_assert_nextline("00000000: 00 11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff 10 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ..\"3DUfw........................................................");
  240. ut_assert_nextline("00000040: 00 41 .A");
  241. ut_assert_console_end();
  242. /* 16-bit */
  243. console_record_reset();
  244. print_hex_dump("", DUMP_PREFIX_ADDRESS, 0, 2, buf, 0x12, true);
  245. ut_assert_nextline("00000000: 1100 3322 5544 7766 9988 bbaa ddcc ffee ..\"3DUfw........");
  246. ut_assert_nextline("00000010: 0010 ..");
  247. ut_assert_console_end();
  248. unmap_sysmem(buf);
  249. /* 32-bit */
  250. console_record_reset();
  251. print_hex_dump("", DUMP_PREFIX_ADDRESS, 0, 4, buf, 0x14, true);
  252. ut_assert_nextline("00000000: 33221100 77665544 bbaa9988 ffeeddcc ..\"3DUfw........");
  253. ut_assert_nextline("00000010: 00000010 ....");
  254. ut_assert_console_end();
  255. unmap_sysmem(buf);
  256. /* 64-bit */
  257. console_record_reset();
  258. print_hex_dump("", DUMP_PREFIX_ADDRESS, 16, 8, buf, 0x18, true);
  259. ut_assert_nextline("00000000: 7766554433221100 ffeeddccbbaa9988 ..\"3DUfw........");
  260. ut_assert_nextline("00000010: 0000000000000010 ........");
  261. ut_assert_console_end();
  262. unmap_sysmem(buf);
  263. /* ASCII */
  264. console_record_reset();
  265. buf[1] = 31;
  266. buf[2] = 32;
  267. buf[3] = 33;
  268. for (i = 0; i < 4; i++)
  269. buf[4 + i] = 126 + i;
  270. buf[8] = 255;
  271. print_hex_dump("", DUMP_PREFIX_ADDRESS, 0, 1, buf, 10, true);
  272. ut_assert_nextline("00000000: 00 1f 20 21 7e 7f 80 81 ff 99 .. !~.....");
  273. ut_assert_console_end();
  274. unmap_sysmem(buf);
  275. return 0;
  276. }
  277. PRINT_TEST(print_do_hex_dump, UT_TESTF_CONSOLE_REC);
  278. int do_ut_print(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
  279. {
  280. struct unit_test *tests = UNIT_TEST_SUITE_START(print_test);
  281. const int n_ents = UNIT_TEST_SUITE_COUNT(print_test);
  282. return cmd_ut_category("print", "print_", tests, n_ents, argc, argv);
  283. }