Skip to content

Commit 38a0fbc

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 037dde4 commit 38a0fbc

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
@@ -1795,27 +1795,50 @@ char *fourcc_string(char *buf, char *end, const u32 *fourcc,
17951795
char output[sizeof("0123 little-endian (0x01234567)")];
17961796
char *p = output;
17971797
unsigned int i;
1798+
bool pix_fmt = false;
17981799
u32 orig, val;
17991800

1800-
if (fmt[1] != 'c' || fmt[2] != 'c')
1801+
if (fmt[1] != 'c')
18011802
return error_string(buf, end, "(%p4?)", spec);
18021803

18031804
if (check_pointer(&buf, end, fourcc, spec))
18041805
return buf;
18051806

18061807
orig = get_unaligned(fourcc);
1807-
val = orig & ~BIT(31);
1808+
switch (fmt[2]) {
1809+
case 'h':
1810+
val = orig;
1811+
break;
1812+
case 'r':
1813+
val = orig = swab32(orig);
1814+
break;
1815+
case 'l':
1816+
val = orig = le32_to_cpu(orig);
1817+
break;
1818+
case 'b':
1819+
val = orig = be32_to_cpu(orig);
1820+
break;
1821+
case 'c':
1822+
/* Pixel formats are printed LSB-first */
1823+
val = swab32(orig & ~BIT(31));
1824+
pix_fmt = true;
1825+
break;
1826+
default:
1827+
return error_string(buf, end, "(%p4?)", spec);
1828+
}
18081829

18091830
for (i = 0; i < sizeof(u32); i++) {
1810-
unsigned char c = val >> (i * 8);
1831+
unsigned char c = val >> ((3 - i) * 8);
18111832

18121833
/* Print non-control ASCII characters as-is, dot otherwise */
18131834
*p++ = isascii(c) && isprint(c) ? c : '.';
18141835
}
18151836

1816-
*p++ = ' ';
1817-
strcpy(p, orig & BIT(31) ? "big-endian" : "little-endian");
1818-
p += strlen(p);
1837+
if (pix_fmt) {
1838+
*p++ = ' ';
1839+
strcpy(p, orig & BIT(31) ? "big-endian" : "little-endian");
1840+
p += strlen(p);
1841+
}
18191842

18201843
*p++ = ' ';
18211844
*p++ = '(';

0 commit comments

Comments
 (0)