Coverage Report

Created: 2026-03-11 06:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/u-boot/test/common/print.c
Line
Count
Source
1
// SPDX-License-Identifier: GPL-2.0+
2
/*
3
 * Copyright (c) 2012, The Chromium Authors
4
 */
5
6
#include <command.h>
7
#include <compiler.h>
8
#include <efi_api.h>
9
#include <display_options.h>
10
#include <log.h>
11
#include <mapmem.h>
12
#include <version_string.h>
13
#include <stdio.h>
14
#include <vsprintf.h>
15
#include <test/common.h>
16
#include <test/test.h>
17
#include <test/ut.h>
18
19
0
#define BUF_SIZE  0x100
20
21
0
#define FAKE_BUILD_TAG  "jenkins-u-boot-denx_uboot_dm-master-build-aarch64" \
22
0
      "and a lot more text to come"
23
24
#if CONFIG_IS_ENABLED(LIB_UUID)
25
/* Test printing GUIDs */
26
static int print_guid(struct unit_test_state *uts)
27
0
{
28
0
  unsigned char guid[16] = {
29
0
    1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16
30
0
  };
31
0
  unsigned char guid_esp[16] = {
32
0
    0x28, 0x73, 0x2a, 0xc1, 0x1f, 0xf8, 0xd2, 0x11,
33
0
    0xBA, 0x4B, 0x00, 0xA0, 0xC9, 0x3E, 0xC9, 0x3B
34
0
  };
35
0
  char str[40];
36
0
  int ret;
37
38
0
  sprintf(str, "%pUb", guid);
39
0
  ut_asserteq_str("01020304-0506-0708-090a-0b0c0d0e0f10", str);
40
0
  sprintf(str, "%pUB", guid);
41
0
  ut_asserteq_str("01020304-0506-0708-090A-0B0C0D0E0F10", str);
42
0
  sprintf(str, "%pUl", guid);
43
0
  ut_asserteq_str("04030201-0605-0807-090a-0b0c0d0e0f10", str);
44
0
  sprintf(str, "%pUs", guid);
45
0
  ut_asserteq_str("04030201-0605-0807-090a-0b0c0d0e0f10", str);
46
0
  sprintf(str, "%pUL", guid);
47
0
  ut_asserteq_str("04030201-0605-0807-090A-0B0C0D0E0F10", str);
48
0
  sprintf(str, "%pUs", guid_esp);
49
0
  if (IS_ENABLED(CONFIG_EFI_PARTITION))
50
0
    ut_asserteq_str("EFI System Partition", str);
51
0
  else
52
0
    ut_asserteq_str("c12a7328-f81f-11d2-ba4b-00a0c93ec93b", str);
53
0
  ret = snprintf(str, 4, "%pUL", guid);
54
0
  ut_asserteq(0, str[3]);
55
0
  ut_asserteq(36, ret);
56
57
0
  return 0;
58
0
}
59
COMMON_TEST(print_guid, 0);
60
#endif
61
62
#if CONFIG_IS_ENABLED(EFI_LOADER) && !defined(API_BUILD)
63
/* Test efi_loader specific printing */
64
static int print_efi_ut(struct unit_test_state *uts)
65
0
{
66
0
  char str[10];
67
0
  u8 buf[sizeof(struct efi_device_path_sd_mmc_path) +
68
0
         sizeof(struct efi_device_path)];
69
0
  u8 *pos = buf;
70
0
  struct efi_device_path *dp_end;
71
0
  struct efi_device_path_sd_mmc_path *dp_sd =
72
0
      (struct efi_device_path_sd_mmc_path *)pos;
73
74
  /* Create a device path for an SD card */
75
0
  dp_sd->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
76
0
  dp_sd->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_SD;
77
0
  dp_sd->dp.length = sizeof(struct efi_device_path_sd_mmc_path);
78
0
  dp_sd->slot_number = 3;
79
0
  pos += sizeof(struct efi_device_path_sd_mmc_path);
80
  /* Append end node */
81
0
  dp_end = (struct efi_device_path *)pos;
82
0
  dp_end->type = DEVICE_PATH_TYPE_END;
83
0
  dp_end->sub_type = DEVICE_PATH_SUB_TYPE_END;
84
0
  dp_end->length = sizeof(struct efi_device_path);
85
86
0
  snprintf(str, sizeof(str), "_%pD_", buf);
87
0
  ut_assertok(strcmp("_/SD(3)_", str));
88
89
  /* NULL device path */
90
0
  snprintf(str, sizeof(str), "_%pD_", NULL);
91
0
  ut_assertok(strcmp("_<NULL>_", str));
92
93
0
  return 0;
94
0
}
95
COMMON_TEST(print_efi_ut, 0);
96
#endif
97
98
static int print_printf(struct unit_test_state *uts)
99
0
{
100
0
  char big_str[400];
101
0
  int big_str_len;
102
0
  char str[10], *s;
103
0
  int len;
104
105
0
  snprintf(str, sizeof(str), "testing");
106
0
  ut_assertok(strcmp("testing", str));
107
108
0
  snprintf(str, sizeof(str), "testing but too long");
109
0
  ut_assertok(strcmp("testing b", str));
110
111
0
  snprintf(str, 1, "testing none");
112
0
  ut_assertok(strcmp("", str));
113
114
0
  *str = 'x';
115
0
  snprintf(str, 0, "testing none");
116
0
  ut_asserteq('x', *str);
117
118
0
  if (CONFIG_IS_ENABLED(EFI_LOADER) || IS_ENABLED(CONFIG_EFI_APP)) {
119
0
    sprintf(big_str, "_%ls_", u"foo");
120
0
    ut_assertok(strcmp("_foo_", big_str));
121
0
  }
122
123
  /* Test the banner function */
124
0
  s = display_options_get_banner(true, str, sizeof(str));
125
0
  ut_asserteq_ptr(str, s);
126
0
  ut_assertok(strcmp("\n\nU-Boo\n\n", s));
127
128
  /* Assert that we do not overwrite memory before the buffer */
129
0
  str[0] = '`';
130
0
  s = display_options_get_banner(true, str + 1, 1);
131
0
  ut_asserteq_ptr(str + 1, s);
132
0
  ut_assertok(strcmp("`", str));
133
134
0
  str[0] = '~';
135
0
  s = display_options_get_banner(true, str + 1, 2);
136
0
  ut_asserteq_ptr(str + 1, s);
137
0
  ut_assertok(strcmp("~\n", str));
138
139
  /* The last two characters are set to \n\n for all buffer sizes > 2 */
140
0
  s = display_options_get_banner(false, str, sizeof(str));
141
0
  ut_asserteq_ptr(str, s);
142
0
  ut_assertok(strcmp("U-Boot \n\n", s));
143
144
  /* Give it enough space for some of the version */
145
0
  big_str_len = strlen(version_string) - 5;
146
0
  s = display_options_get_banner_priv(false, FAKE_BUILD_TAG, big_str,
147
0
              big_str_len);
148
0
  ut_asserteq_ptr(big_str, s);
149
0
  ut_assertok(strncmp(version_string, s, big_str_len - 3));
150
0
  ut_assertok(strcmp("\n\n", s + big_str_len - 3));
151
152
  /* Give it enough space for the version and some of the build tag */
153
0
  big_str_len = strlen(version_string) + 9 + 20;
154
0
  s = display_options_get_banner_priv(false, FAKE_BUILD_TAG, big_str,
155
0
              big_str_len);
156
0
  ut_asserteq_ptr(big_str, s);
157
0
  len = strlen(version_string);
158
0
  ut_assertok(strncmp(version_string, s, len));
159
0
  ut_assertok(strncmp(", Build: ", s + len, 9));
160
0
  ut_assertok(strncmp(FAKE_BUILD_TAG, s + 9 + len, 12));
161
0
  ut_assertok(strcmp("\n\n", s + big_str_len - 3));
162
163
0
  return 0;
164
0
}
165
COMMON_TEST(print_printf, 0);
166
167
static int print_display_buffer(struct unit_test_state *uts)
168
0
{
169
0
  u8 *buf;
170
0
  int i;
171
172
0
  buf = calloc(1, BUF_SIZE);
173
0
  ut_assertnonnull(buf);
174
0
  for (i = 0; i < 0x11; i++)
175
0
    buf[i] = i * 0x11;
176
177
  /* bytes */
178
0
  print_buffer(0, buf, 1, 0x12, 0);
179
0
  ut_assert_nextline("00000000: 00 11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff  ..\"3DUfw........");
180
0
  ut_assert_nextline("00000010: 10 00                                            ..");
181
0
  ut_assert_console_end();
182
183
  /* line length */
184
0
  print_buffer(0, buf, 1, 0x12, 8);
185
0
  ut_assert_nextline("00000000: 00 11 22 33 44 55 66 77  ..\"3DUfw");
186
0
  ut_assert_nextline("00000008: 88 99 aa bb cc dd ee ff  ........");
187
0
  ut_assert_nextline("00000010: 10 00                    ..");
188
0
  ut_assert_console_end();
189
190
  /* long line */
191
0
  buf[0x41] = 0x41;
192
0
  print_buffer(0, buf, 1, 0x42, 0x40);
193
0
  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........................................................");
194
0
  ut_assert_nextline("00000040: 00 41                                                                                                                                                                                            .A");
195
0
  ut_assert_console_end();
196
197
  /* address */
198
0
  print_buffer(0x12345678, buf, 1, 0x12, 0);
199
0
  ut_assert_nextline("12345678: 00 11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff  ..\"3DUfw........");
200
0
  ut_assert_nextline("12345688: 10 00                                            ..");
201
0
  ut_assert_console_end();
202
203
  /* 16-bit */
204
0
  print_buffer(0, buf, 2, 9, 0);
205
#ifdef __BIG_ENDIAN
206
  ut_assert_nextline("00000000: 0011 2233 4455 6677 8899 aabb ccdd eeff  ..\"3DUfw........");
207
  ut_assert_nextline("00000010: 1000                                     ..");
208
#else
209
0
  ut_assert_nextline("00000000: 1100 3322 5544 7766 9988 bbaa ddcc ffee  ..\"3DUfw........");
210
0
  ut_assert_nextline("00000010: 0010                                     ..");
211
0
#endif
212
0
  ut_assert_console_end();
213
214
  /* 32-bit */
215
0
  print_buffer(0, buf, 4, 5, 0);
216
#ifdef __BIG_ENDIAN
217
  ut_assert_nextline("00000000: 00112233 44556677 8899aabb ccddeeff  ..\"3DUfw........");
218
  ut_assert_nextline("00000010: 10000000                             ....");
219
#else
220
0
  ut_assert_nextline("00000000: 33221100 77665544 bbaa9988 ffeeddcc  ..\"3DUfw........");
221
0
  ut_assert_nextline("00000010: 00000010                             ....");
222
0
#endif
223
0
  ut_assert_console_end();
224
225
  /* 64-bit */
226
0
  if (MEM_SUPPORT_64BIT_DATA) {
227
0
    print_buffer(0, buf, 8, 3, 0);
228
#ifdef __BIG_ENDIAN
229
    ut_assert_nextline("00000000: 0011223344556677 8899aabbccddeeff  ..\"3DUfw........");
230
    ut_assert_nextline("00000010: 1000000000000000                   ........");
231
#else
232
0
    ut_assert_nextline("00000000: 7766554433221100 ffeeddccbbaa9988  ..\"3DUfw........");
233
0
    ut_assert_nextline("00000010: 0000000000000010                   ........");
234
0
#endif
235
0
    ut_assert_console_end();
236
0
  }
237
238
  /* ASCII */
239
0
  buf[1] = 31;
240
0
  buf[2] = 32;
241
0
  buf[3] = 33;
242
0
  for (i = 0; i < 4; i++)
243
0
    buf[4 + i] = 126 + i;
244
0
  buf[8] = 255;
245
0
  print_buffer(0, buf, 1, 10, 0);
246
0
  ut_assert_nextline("00000000: 00 1f 20 21 7e 7f 80 81 ff 99                    .. !~.....");
247
0
  ut_assert_console_end();
248
249
0
  free(buf);
250
251
0
  return 0;
252
0
}
253
COMMON_TEST(print_display_buffer, UTF_CONSOLE);
254
255
static int print_hexdump_line(struct unit_test_state *uts)
256
0
{
257
0
  u8 *linebuf;
258
0
  u8 *buf;
259
0
  int i;
260
261
0
  buf = calloc(1, BUF_SIZE);
262
0
  ut_assertnonnull(buf);
263
0
  for (i = 0; i < 0x11; i++)
264
0
    buf[i] = i * 0x11;
265
266
  /* Check buffer size calculations */
267
0
  linebuf = calloc(1, BUF_SIZE);
268
0
  ut_assertnonnull(buf);
269
0
  memset(linebuf, '\xff', BUF_SIZE);
270
0
  ut_asserteq(-ENOSPC, hexdump_line(0, buf, 1, 0x10, 0, linebuf, 75));
271
0
  ut_asserteq(0xff, linebuf[0]);
272
0
  ut_asserteq(0x10, hexdump_line(0, buf, 1, 0x10, 0, linebuf, 76));
273
0
  ut_asserteq('\0', linebuf[75]);
274
0
  ut_asserteq(0xff, linebuf[76]);
275
276
0
  free(linebuf);
277
0
  free(buf);
278
279
0
  return 0;
280
0
}
281
COMMON_TEST(print_hexdump_line, UTF_CONSOLE);
282
283
static int print_do_hex_dump(struct unit_test_state *uts)
284
0
{
285
0
  u8 *buf;
286
0
  int i;
287
0
  ulong addr;
288
289
0
  if (!CONFIG_IS_ENABLED(HEXDUMP))
290
0
    return -EAGAIN;
291
292
0
  buf = calloc(1, BUF_SIZE);
293
0
  ut_assertnonnull(buf);
294
0
  addr = map_to_sysmem(buf);
295
0
  for (i = 0; i < 0x11; i++)
296
0
    buf[i] = i * 0x11;
297
298
  /* bytes */
299
0
  print_hex_dump_bytes("", DUMP_PREFIX_ADDRESS, buf, 0x12);
300
0
  ut_assert_nextline("%0*lx: 00 11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff  ..\"3DUfw........",
301
0
         IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, addr);
302
0
  ut_assert_nextline("%0*lx: 10 00                                            ..",
303
0
         IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8,
304
0
         addr + 0x10UL);
305
0
  ut_assert_console_end();
306
307
  /* line length */
308
0
  print_hex_dump("", DUMP_PREFIX_ADDRESS, 8, 1, buf, 0x12, true);
309
0
  ut_assert_nextline("%0*lx: 00 11 22 33 44 55 66 77  ..\"3DUfw",
310
0
         IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, addr);
311
0
  ut_assert_nextline("%0*lx: 88 99 aa bb cc dd ee ff  ........",
312
0
         IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8,
313
0
         addr + 0x8UL);
314
0
  ut_assert_nextline("%0*lx: 10 00                    ..",
315
0
         IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8,
316
0
         addr + 0x10UL);
317
0
  ut_assert_console_end();
318
319
  /* long line */
320
0
  buf[0x41] = 0x41;
321
0
  print_hex_dump("", DUMP_PREFIX_ADDRESS, 0x40, 1, buf, 0x42, true);
322
0
  ut_assert_nextline("%0*lx: 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........................................................",
323
0
         IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, addr);
324
0
  ut_assert_nextline("%0*lx: 00 41                                                                                                                                                                                            .A",
325
0
         IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8,
326
0
         addr + 0x40UL);
327
0
  ut_assert_console_end();
328
329
  /* 16-bit */
330
0
  print_hex_dump("", DUMP_PREFIX_ADDRESS, 0, 2, buf, 0x12, true);
331
#ifdef __BIG_ENDIAN
332
  ut_assert_nextline("%0*lx: 0011 2233 4455 6677 8899 aabb ccdd eeff  ..\"3DUfw........",
333
         IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, addr);
334
  ut_assert_nextline("%0*lx: 1000                                     ..",
335
         IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8,
336
         addr + 0x10UL);
337
#else
338
0
  ut_assert_nextline("%0*lx: 1100 3322 5544 7766 9988 bbaa ddcc ffee  ..\"3DUfw........",
339
0
         IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, addr);
340
0
  ut_assert_nextline("%0*lx: 0010                                     ..",
341
0
         IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8,
342
0
         addr + 0x10UL);
343
0
#endif
344
0
  ut_assert_console_end();
345
346
  /* 32-bit */
347
0
  print_hex_dump("", DUMP_PREFIX_ADDRESS, 0, 4, buf, 0x14, true);
348
#ifdef __BIG_ENDIAN
349
  ut_assert_nextline("%0*lx: 00112233 44556677 8899aabb ccddeeff  ..\"3DUfw........",
350
         IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, addr);
351
  ut_assert_nextline("%0*lx: 10000000                             ....",
352
         IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8,
353
         addr + 0x10UL);
354
#else
355
0
  ut_assert_nextline("%0*lx: 33221100 77665544 bbaa9988 ffeeddcc  ..\"3DUfw........",
356
0
         IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, addr);
357
0
  ut_assert_nextline("%0*lx: 00000010                             ....",
358
0
         IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8,
359
0
         addr + 0x10UL);
360
0
#endif
361
0
  ut_assert_console_end();
362
363
  /* 64-bit */
364
0
  print_hex_dump("", DUMP_PREFIX_ADDRESS, 16, 8, buf, 0x18, true);
365
#ifdef __BIG_ENDIAN
366
  ut_assert_nextline("%0*lx: 0011223344556677 8899aabbccddeeff  ..\"3DUfw........",
367
         IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, addr);
368
  ut_assert_nextline("%0*lx: 1000000000000000                   ........",
369
         IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8,
370
         addr + 0x10UL);
371
#else
372
0
  ut_assert_nextline("%0*lx: 7766554433221100 ffeeddccbbaa9988  ..\"3DUfw........",
373
0
         IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, addr);
374
0
  ut_assert_nextline("%0*lx: 0000000000000010                   ........",
375
0
         IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8,
376
0
         addr + 0x10UL);
377
0
#endif
378
0
  ut_assert_console_end();
379
380
  /* ASCII */
381
0
  buf[1] = 31;
382
0
  buf[2] = 32;
383
0
  buf[3] = 33;
384
0
  for (i = 0; i < 4; i++)
385
0
    buf[4 + i] = 126 + i;
386
0
  buf[8] = 255;
387
0
  print_hex_dump("", DUMP_PREFIX_ADDRESS, 0, 1, buf, 10, true);
388
0
  ut_assert_nextline("%0*lx: 00 1f 20 21 7e 7f 80 81 ff 99                    .. !~.....",
389
0
         IS_ENABLED(CONFIG_PHYS_64BIT) ? 16 : 8, addr);
390
0
  ut_assert_console_end();
391
0
  free(buf);
392
393
0
  return 0;
394
0
}
395
COMMON_TEST(print_do_hex_dump, UTF_CONSOLE);
396
397
static int snprint(struct unit_test_state *uts)
398
0
{
399
0
  char buf[10] = "xxxxxxxxx";
400
0
  int ret;
401
402
0
  ret = snprintf(buf, 5, "%d", 12345678);
403
0
  ut_asserteq_str("1234", buf);
404
0
  ut_asserteq(8, ret);
405
0
  ret = snprintf(buf, 5, "0x%x", 0x1234);
406
0
  ut_asserteq_str("0x12", buf);
407
0
  ut_asserteq(6, ret);
408
0
  ret = snprintf(buf, 5, "0x%08x", 0x1234);
409
0
  ut_asserteq_str("0x00", buf);
410
0
  ut_asserteq(10, ret);
411
0
  ret = snprintf(buf, 3, "%s", "abc");
412
0
  ut_asserteq_str("ab", buf);
413
0
  ut_asserteq(3, ret);
414
0
  ret = snprintf(buf, 4, "%s:%s", "abc", "def");
415
0
  ut_asserteq(0, buf[3]);
416
0
  ut_asserteq(7, ret);
417
0
  ret = snprintf(buf, 4, "%s:%d", "abc", 9999);
418
0
  ut_asserteq(8, ret);
419
0
  return 0;
420
0
}
421
COMMON_TEST(snprint, 0);