print_ut.c 4.2 KB

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