print_ut.c 12 KB

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