Skip to content

Commit a67d73b

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 98f7e32 commit a67d73b

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
@@ -1760,27 +1760,50 @@ char *fourcc_string(char *buf, char *end, const u32 *fourcc,
17601760
char output[sizeof("0123 little-endian (0x01234567)")];
17611761
char *p = output;
17621762
unsigned int i;
1763+
bool pix_fmt = false;
17631764
u32 orig, val;
17641765

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

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

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

17741795
for (i = 0; i < sizeof(u32); i++) {
1775-
unsigned char c = val >> (i * 8);
1796+
unsigned char c = val >> ((3 - i) * 8);
17761797

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

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

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

0 commit comments

Comments
 (0)