Skip to content

Commit 9a0b528

Browse files
marcanjannau
authored andcommitted
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 <marcan@marcan.st>
1 parent bb5e1aa commit 9a0b528

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
@@ -648,6 +648,38 @@ Examples::
648648
%p4cc Y10 little-endian (0x20303159)
649649
%p4cc NV12 big-endian (0xb231564e)
650650

651+
Generic FourCC code
652+
-------------------
653+
654+
::
655+
%p4c[hnbl] gP00 (0x67503030)
656+
657+
Print a generic FourCC code, as both ASCII characters and its numerical
658+
value as hexadecimal.
659+
660+
The additional ``h``, ``r``, ``b``, and ``l`` specifiers are used to specify
661+
host, reversed, big or little endian order data respectively. Host endian
662+
order means the data is interpreted as a 32-bit integer and the most
663+
significant byte is printed first; that is, the character code as printed
664+
matches the byte order stored in memory on big-endian systems, and is reversed
665+
on little-endian systems.
666+
667+
Passed by reference.
668+
669+
Examples for a little-endian machine, given &(u32)0x67503030::
670+
671+
%p4ch gP00 (0x67503030)
672+
%p4cl gP00 (0x67503030)
673+
%p4cb 00Pg (0x30305067)
674+
%p4cr 00Pg (0x30305067)
675+
676+
Examples for a big-endian machine, given &(u32)0x67503030::
677+
678+
%p4ch gP00 (0x67503030)
679+
%p4cl 00Pg (0x30305067)
680+
%p4cb gP00 (0x67503030)
681+
%p4cr 00Pg (0x30305067)
682+
651683
Rust
652684
----
653685

lib/vsprintf.c

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1781,27 +1781,50 @@ char *fourcc_string(char *buf, char *end, const u32 *fourcc,
17811781
char output[sizeof("0123 little-endian (0x01234567)")];
17821782
char *p = output;
17831783
unsigned int i;
1784+
bool pix_fmt = false;
17841785
u32 orig, val;
17851786

1786-
if (fmt[1] != 'c' || fmt[2] != 'c')
1787+
if (fmt[1] != 'c')
17871788
return error_string(buf, end, "(%p4?)", spec);
17881789

17891790
if (check_pointer(&buf, end, fourcc, spec))
17901791
return buf;
17911792

17921793
orig = get_unaligned(fourcc);
1793-
val = orig & ~BIT(31);
1794+
switch (fmt[2]) {
1795+
case 'h':
1796+
val = orig;
1797+
break;
1798+
case 'r':
1799+
val = orig = swab32(orig);
1800+
break;
1801+
case 'l':
1802+
val = orig = le32_to_cpu(orig);
1803+
break;
1804+
case 'b':
1805+
val = orig = be32_to_cpu(orig);
1806+
break;
1807+
case 'c':
1808+
/* Pixel formats are printed LSB-first */
1809+
val = swab32(orig & ~BIT(31));
1810+
pix_fmt = true;
1811+
break;
1812+
default:
1813+
return error_string(buf, end, "(%p4?)", spec);
1814+
}
17941815

17951816
for (i = 0; i < sizeof(u32); i++) {
1796-
unsigned char c = val >> (i * 8);
1817+
unsigned char c = val >> ((3 - i) * 8);
17971818

17981819
/* Print non-control ASCII characters as-is, dot otherwise */
17991820
*p++ = isascii(c) && isprint(c) ? c : '.';
18001821
}
18011822

1802-
*p++ = ' ';
1803-
strcpy(p, orig & BIT(31) ? "big-endian" : "little-endian");
1804-
p += strlen(p);
1823+
if (pix_fmt) {
1824+
*p++ = ' ';
1825+
strcpy(p, orig & BIT(31) ? "big-endian" : "little-endian");
1826+
p += strlen(p);
1827+
}
18051828

18061829
*p++ = ' ';
18071830
*p++ = '(';

0 commit comments

Comments
 (0)