print_ut.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 efi_loader specific printing */
  15. static void efi_ut_print(void)
  16. {
  17. #if CONFIG_IS_ENABLED(EFI_LOADER) && !defined(API_BUILD)
  18. char str[10];
  19. u8 buf[sizeof(struct efi_device_path_sd_mmc_path) +
  20. sizeof(struct efi_device_path)];
  21. u8 *pos = buf;
  22. struct efi_device_path *dp_end;
  23. struct efi_device_path_sd_mmc_path *dp_sd =
  24. (struct efi_device_path_sd_mmc_path *)pos;
  25. /* Create a device path for an SD card */
  26. dp_sd->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
  27. dp_sd->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_SD;
  28. dp_sd->dp.length = sizeof(struct efi_device_path_sd_mmc_path);
  29. dp_sd->slot_number = 3;
  30. pos += sizeof(struct efi_device_path_sd_mmc_path);
  31. /* Append end node */
  32. dp_end = (struct efi_device_path *)pos;
  33. dp_end->type = DEVICE_PATH_TYPE_END;
  34. dp_end->sub_type = DEVICE_PATH_SUB_TYPE_END;
  35. dp_end->length = sizeof(struct efi_device_path);
  36. snprintf(str, sizeof(str), "_%pD_", buf);
  37. assert(!strcmp("_/SD(3)_", str));
  38. /* NULL device path */
  39. snprintf(str, sizeof(str), "_%pD_", NULL);
  40. assert(!strcmp("_<NULL>_", str));
  41. #endif
  42. }
  43. static int do_ut_print(cmd_tbl_t *cmdtp, int flag, int argc,
  44. char *const argv[])
  45. {
  46. char big_str[400];
  47. int big_str_len;
  48. char str[10], *s;
  49. int len;
  50. printf("%s: Testing print\n", __func__);
  51. snprintf(str, sizeof(str), "testing");
  52. assert(!strcmp("testing", str));
  53. snprintf(str, sizeof(str), "testing but too long");
  54. assert(!strcmp("testing b", str));
  55. snprintf(str, 1, "testing none");
  56. assert(!strcmp("", str));
  57. *str = 'x';
  58. snprintf(str, 0, "testing none");
  59. assert(*str == 'x');
  60. sprintf(big_str, "_%ls_", L"foo");
  61. assert(!strcmp("_foo_", big_str));
  62. /* Test the banner function */
  63. s = display_options_get_banner(true, str, sizeof(str));
  64. assert(s == str);
  65. assert(!strcmp("\n\nU-Boo\n\n", s));
  66. s = display_options_get_banner(true, str, 1);
  67. assert(s == str);
  68. assert(!strcmp("", s));
  69. s = display_options_get_banner(true, str, 2);
  70. assert(s == str);
  71. assert(!strcmp("\n", s));
  72. s = display_options_get_banner(false, str, sizeof(str));
  73. assert(s == str);
  74. assert(!strcmp("U-Boot \n\n", s));
  75. /* Give it enough space for some of the version */
  76. big_str_len = strlen(version_string) - 5;
  77. s = display_options_get_banner_priv(false, FAKE_BUILD_TAG, big_str,
  78. big_str_len);
  79. assert(s == big_str);
  80. assert(!strncmp(version_string, s, big_str_len - 3));
  81. assert(!strcmp("\n\n", s + big_str_len - 3));
  82. /* Give it enough space for the version and some of the build tag */
  83. big_str_len = strlen(version_string) + 9 + 20;
  84. s = display_options_get_banner_priv(false, FAKE_BUILD_TAG, big_str,
  85. big_str_len);
  86. assert(s == big_str);
  87. len = strlen(version_string);
  88. assert(!strncmp(version_string, s, len));
  89. assert(!strncmp(", Build: ", s + len, 9));
  90. assert(!strncmp(FAKE_BUILD_TAG, s + 9 + len, 12));
  91. assert(!strcmp("\n\n", s + big_str_len - 3));
  92. /* Test efi_loader specific printing */
  93. efi_ut_print();
  94. printf("%s: Everything went swimmingly\n", __func__);
  95. return 0;
  96. }
  97. U_BOOT_CMD(
  98. ut_print, 1, 1, do_ut_print,
  99. "Very basic test of printf(), etc.",
  100. ""
  101. );