print_ut.c 13 KB

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