Skip to content

Commit 99925e4

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 <marcan@marcan.st>
1 parent ffc2532 commit 99925e4

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
@@ -632,6 +632,38 @@ Examples::
632632
%p4cc Y10 little-endian (0x20303159)
633633
%p4cc NV12 big-endian (0xb231564e)
634634

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

lib/vsprintf.c

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

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

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

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

17731794
for (i = 0; i < sizeof(u32); i++) {
1774-
unsigned char c = val >> (i * 8);
1795+
unsigned char c = val >> ((3 - i) * 8);
17751796

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

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

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

0 commit comments

Comments
 (0)