print_ut.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2012, The Chromium Authors
  4. */
  5. #define DEBUG
  6. #include <common.h>
  7. #if CONFIG_IS_ENABLED(EFI_LOADER) && !defined(API_BUILD)
  8. #include <efi_api.h>
  9. #endif
  10. #include <display_options.h>
  11. #include <version.h>
  12. #define FAKE_BUILD_TAG "jenkins-u-boot-denx_uboot_dm-master-build-aarch64" \
  13. "and a lot more text to come"
  14. /* Test printing GUIDs */
  15. static void guid_ut_print(void)
  16. {
  17. #if CONFIG_IS_ENABLED(LIB_UUID)
  18. unsigned char guid[16] = {
  19. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16
  20. };
  21. char str[40];
  22. sprintf(str, "%pUb", guid);
  23. assert(!strcmp("01020304-0506-0708-090a-0b0c0d0e0f10", str));
  24. sprintf(str, "%pUB", guid);
  25. assert(!strcmp("01020304-0506-0708-090A-0B0C0D0E0F10", str));
  26. sprintf(str, "%pUl", guid);
  27. assert(!strcmp("04030201-0605-0807-090a-0b0c0d0e0f10", str));
  28. sprintf(str, "%pUL", guid);
  29. assert(!strcmp("04030201-0605-0807-090A-0B0C0D0E0F10", str));
  30. #endif
  31. }
  32. /* Test efi_loader specific printing */
  33. static void efi_ut_print(void)
  34. {
  35. #if CONFIG_IS_ENABLED(EFI_LOADER) && !defined(API_BUILD)
  36. char str[10];
  37. u8 buf[sizeof(struct efi_device_path_sd_mmc_path) +
  38. sizeof(struct efi_device_path)];
  39. u8 *pos = buf;
  40. struct efi_device_path *dp_end;
  41. struct efi_device_path_sd_mmc_path *dp_sd =
  42. (struct efi_device_path_sd_mmc_path *)pos;
  43. /* Create a device path for an SD card */
  44. dp_sd->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
  45. dp_sd->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_SD;
  46. dp_sd->dp.length = sizeof(struct efi_device_path_sd_mmc_path);
  47. dp_sd->slot_number = 3;
  48. pos += sizeof(struct efi_device_path_sd_mmc_path);
  49. /* Append end node */
  50. dp_end = (struct efi_device_path *)pos;
  51. dp_end->type = DEVICE_PATH_TYPE_END;
  52. dp_end->sub_type = DEVICE_PATH_SUB_TYPE_END;
  53. dp_end->length = sizeof(struct efi_device_path);
  54. snprintf(str, sizeof(str), "_%pD_", buf);
  55. assert(!strcmp("_/SD(3)_", str));
  56. /* NULL device path */
  57. snprintf(str, sizeof(str), "_%pD_", NULL);
  58. assert(!strcmp("_<NULL>_", str));
  59. #endif
  60. }
  61. static int do_ut_print(cmd_tbl_t *cmdtp, int flag, int argc,
  62. char *const argv[])
  63. {
  64. char big_str[400];
  65. int big_str_len;
  66. char str[10], *s;
  67. int len;
  68. printf("%s: Testing print\n", __func__);
  69. snprintf(str, sizeof(str), "testing");
  70. assert(!strcmp("testing", str));
  71. snprintf(str, sizeof(str), "testing but too long");
  72. assert(!strcmp("testing b", str));
  73. snprintf(str, 1, "testing none");
  74. assert(!strcmp("", str));
  75. *str = 'x';
  76. snprintf(str, 0, "testing none");
  77. assert(*str == 'x');
  78. sprintf(big_str, "_%ls_", L"foo");
  79. assert(!strcmp("_foo_", big_str));
  80. /* Test the banner function */
  81. s = display_options_get_banner(true, str, sizeof(str));
  82. assert(s == str);
  83. assert(!strcmp("\n\nU-Boo\n\n", s));
  84. /* Assert that we do not overwrite memory before the buffer */
  85. str[0] = '`';
  86. s = display_options_get_banner(true, str + 1, 1);
  87. assert(s == str + 1);
  88. assert(!strcmp("`", str));
  89. str[0] = '~';
  90. s = display_options_get_banner(true, str + 1, 2);
  91. assert(s == str + 1);
  92. assert(!strcmp("~\n", str));
  93. /* The last two characters are set to \n\n for all buffer sizes > 2 */
  94. s = display_options_get_banner(false, str, sizeof(str));
  95. assert(s == str);
  96. assert(!strcmp("U-Boot \n\n", s));
  97. /* Give it enough space for some of the version */
  98. big_str_len = strlen(version_string) - 5;
  99. s = display_options_get_banner_priv(false, FAKE_BUILD_TAG, big_str,
  100. big_str_len);
  101. assert(s == big_str);
  102. assert(!strncmp(version_string, s, big_str_len - 3));
  103. assert(!strcmp("\n\n", s + big_str_len - 3));
  104. /* Give it enough space for the version and some of the build tag */
  105. big_str_len = strlen(version_string) + 9 + 20;
  106. s = display_options_get_banner_priv(false, FAKE_BUILD_TAG, big_str,
  107. big_str_len);
  108. assert(s == big_str);
  109. len = strlen(version_string);
  110. assert(!strncmp(version_string, s, len));
  111. assert(!strncmp(", Build: ", s + len, 9));
  112. assert(!strncmp(FAKE_BUILD_TAG, s + 9 + len, 12));
  113. assert(!strcmp("\n\n", s + big_str_len - 3));
  114. /* Test efi_loader specific printing */
  115. efi_ut_print();
  116. /* Test printing GUIDs */
  117. guid_ut_print();
  118. printf("%s: Everything went swimmingly\n", __func__);
  119. return 0;
  120. }
  121. U_BOOT_CMD(
  122. ut_print, 1, 1, do_ut_print,
  123. "Very basic test of printf(), etc.",
  124. ""
  125. );