Skip to content

Commit da07643

Browse files
committed
lib/vsprintf: Add support for generic FOURCCs by extending %p4cc
%p4cc is designed for DRM/V4L2 FOURCCs with their specific quirks, but it's useful to be able to print generic 4-character codes formatted as an integer. Extend it to add format specifiers for printing generic 32-bit FOURCCs with various endian semantics: %p4ch Host-endian %p4cl Little-endian %p4cb Big-endian %p4cr Reverse-endian The endianness determines how bytes are interpreted as a u32, and the FOURCC is then always printed MSByte-first (this is the opposite of V4L/DRM FOURCCs). This covers most practical cases, e.g. %p4cr would allow printing LSByte-first FOURCCs stored in host endian order (other than the hex form being in character order, not the integer value). Signed-off-by: Hector Martin <[email protected]>
1 parent 457391b commit da07643

2 files changed

Lines changed: 61 additions & 6 deletions

File tree

Documentation/core-api/printk-formats.rst

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,38 @@ Examples::
625625
%p4cc Y10 little-endian (0x20303159)
626626
%p4cc NV12 big-endian (0xb231564e)
627627

628+
Generic FourCC code
629+
-------------------
630+
631+
::
632+
%p4c[hnbl] gP00 (0x67503030)
633+
634+
Print a generic FourCC code, as both ASCII characters and its numerical
635+
value as hexadecimal.
636+
637+
The additional ``h``, ``r``, ``b``, and ``l`` specifiers are used to specify
638+
host, reversed, big or little endian order data respectively. Host endian
639+
order means the data is interpreted as a 32-bit integer and the most
640+
significant byte is printed first; that is, the character code as printed
641+
matches the byte order stored in memory on big-endian systems, and is reversed
642+
on little-endian systems.
643+
644+
Passed by reference.
645+
646+
Examples for a little-endian machine, given &(u32)0x67503030::
647+
648+
%p4ch gP00 (0x67503030)
649+
%p4cl gP00 (0x67503030)
650+
%p4cb 00Pg (0x30305067)
651+
%p4cr 00Pg (0x30305067)
652+
653+
Examples for a big-endian machine, given &(u32)0x67503030::
654+
655+
%p4ch gP00 (0x67503030)
656+
%p4cl 00Pg (0x30305067)
657+
%p4cb gP00 (0x67503030)
658+
%p4cr 00Pg (0x30305067)
659+
628660
Rust
629661
----
630662

lib/vsprintf.c

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1758,27 +1758,50 @@ char *fourcc_string(char *buf, char *end, const u32 *fourcc,
17581758
char output[sizeof("0123 little-endian (0x01234567)")];
17591759
char *p = output;
17601760
unsigned int i;
1761+
bool pix_fmt = false;
17611762
u32 orig, val;
17621763

1763-
if (fmt[1] != 'c' || fmt[2] != 'c')
1764+
if (fmt[1] != 'c')
17641765
return error_string(buf, end, "(%p4?)", spec);
17651766

17661767
if (check_pointer(&buf, end, fourcc, spec))
17671768
return buf;
17681769

17691770
orig = get_unaligned(fourcc);
1770-
val = orig & ~BIT(31);
1771+
switch (fmt[2]) {
1772+
case 'h':
1773+
val = orig;
1774+
break;
1775+
case 'r':
1776+
val = orig = swab32(orig);
1777+
break;
1778+
case 'l':
1779+
val = orig = le32_to_cpu(orig);
1780+
break;
1781+
case 'b':
1782+
val = orig = be32_to_cpu(orig);
1783+
break;
1784+
case 'c':
1785+
/* Pixel formats are printed LSB-first */
1786+
val = swab32(orig & ~BIT(31));
1787+
pix_fmt = true;
1788+
break;
1789+
default:
1790+
return error_string(buf, end, "(%p4?)", spec);
1791+
}
17711792

17721793
for (i = 0; i < sizeof(u32); i++) {
1773-
unsigned char c = val >> (i * 8);
1794+
unsigned char c = val >> ((3 - i) * 8);
17741795

17751796
/* Print non-control ASCII characters as-is, dot otherwise */
17761797
*p++ = isascii(c) && isprint(c) ? c : '.';
17771798
}
17781799

1779-
*p++ = ' ';
1780-
strcpy(p, orig & BIT(31) ? "big-endian" : "little-endian");
1781-
p += strlen(p);
1800+
if (pix_fmt) {
1801+
*p++ = ' ';
1802+
strcpy(p, orig & BIT(31) ? "big-endian" : "little-endian");
1803+
p += strlen(p);
1804+
}
17821805

17831806
*p++ = ' ';
17841807
*p++ = '(';

0 commit comments

Comments
 (0)