|
| 1 | +From 4870ca09f6de64bb971ba649fc5b65b116b1d851 Mon Sep 17 00:00:00 2001 |
| 2 | +From: Hector Martin < [email protected]> |
| 3 | +Date: Tue, 8 Apr 2025 12:17:57 +0530 |
| 4 | +Subject: [PATCH 1/2] lib/vsprintf: Add support for generic FourCCs by |
| 5 | + extending %p4cc |
| 6 | + |
| 7 | +%p4cc is designed for DRM/V4L2 FourCCs with their specific quirks, but |
| 8 | +it's useful to be able to print generic 4-character codes formatted as |
| 9 | +an integer. Extend it to add format specifiers for printing generic |
| 10 | +32-bit FourCCs with various endian semantics: |
| 11 | + |
| 12 | +%p4ch Host byte order |
| 13 | +%p4cn Network byte order |
| 14 | +%p4cl Little-endian |
| 15 | +%p4cb Big-endian |
| 16 | + |
| 17 | +The endianness determines how bytes are interpreted as a u32, and the |
| 18 | +FourCC is then always printed MSByte-first (this is the opposite of |
| 19 | +V4L/DRM FourCCs). This covers most practical cases, e.g. %p4cn would |
| 20 | +allow printing LSByte-first FourCCs stored in host endian order |
| 21 | +(other than the hex form being in character order, not the integer |
| 22 | +value). |
| 23 | + |
| 24 | +Acked-by: Rasmus Villemoes < [email protected]> |
| 25 | +Reviewed-by: Andy Shevchenko < [email protected]> |
| 26 | +Reviewed-by: Petr Mladek < [email protected]> |
| 27 | +Tested-by: Petr Mladek < [email protected]> |
| 28 | +Signed-off-by: Hector Martin < [email protected]> |
| 29 | +Signed-off-by: Aditya Garg < [email protected]> |
| 30 | +Reviewed-by: Kees Cook < [email protected]> |
| 31 | +--- |
| 32 | + Documentation/core-api/printk-formats.rst | 32 +++++++++++++++++++++ |
| 33 | + lib/vsprintf.c | 35 +++++++++++++++++++---- |
| 34 | + scripts/checkpatch.pl | 2 +- |
| 35 | + 3 files changed, 62 insertions(+), 7 deletions(-) |
| 36 | + |
| 37 | +diff --git a/Documentation/core-api/printk-formats.rst b/Documentation/core-api/printk-formats.rst |
| 38 | +index ecccc0473..bd420e8aa 100644 |
| 39 | +--- a/Documentation/core-api/printk-formats.rst |
| 40 | ++++ b/Documentation/core-api/printk-formats.rst |
| 41 | +@@ -648,6 +648,38 @@ Examples:: |
| 42 | + %p4cc Y10 little-endian (0x20303159) |
| 43 | + %p4cc NV12 big-endian (0xb231564e) |
| 44 | + |
| 45 | ++Generic FourCC code |
| 46 | ++------------------- |
| 47 | ++ |
| 48 | ++:: |
| 49 | ++ %p4c[hnlb] gP00 (0x67503030) |
| 50 | ++ |
| 51 | ++Print a generic FourCC code, as both ASCII characters and its numerical |
| 52 | ++value as hexadecimal. |
| 53 | ++ |
| 54 | ++The generic FourCC code is always printed in the big-endian format, |
| 55 | ++the most significant byte first. This is the opposite of V4L/DRM FourCCs. |
| 56 | ++ |
| 57 | ++The additional ``h``, ``n``, ``l``, and ``b`` specifiers define what |
| 58 | ++endianness is used to load the stored bytes. The data might be interpreted |
| 59 | ++using the host byte order, network byte order, little-endian, or big-endian. |
| 60 | ++ |
| 61 | ++Passed by reference. |
| 62 | ++ |
| 63 | ++Examples for a little-endian machine, given &(u32)0x67503030:: |
| 64 | ++ |
| 65 | ++ %p4ch gP00 (0x67503030) |
| 66 | ++ %p4cn 00Pg (0x30305067) |
| 67 | ++ %p4cl gP00 (0x67503030) |
| 68 | ++ %p4cb 00Pg (0x30305067) |
| 69 | ++ |
| 70 | ++Examples for a big-endian machine, given &(u32)0x67503030:: |
| 71 | ++ |
| 72 | ++ %p4ch gP00 (0x67503030) |
| 73 | ++ %p4cn 00Pg (0x30305067) |
| 74 | ++ %p4cl 00Pg (0x30305067) |
| 75 | ++ %p4cb gP00 (0x67503030) |
| 76 | ++ |
| 77 | + Rust |
| 78 | + ---- |
| 79 | + |
| 80 | +diff --git a/lib/vsprintf.c b/lib/vsprintf.c |
| 81 | +index a8ac4c4ff..a434b58e8 100644 |
| 82 | +--- a/lib/vsprintf.c |
| 83 | ++++ b/lib/vsprintf.c |
| 84 | +@@ -1781,27 +1781,50 @@ char *fourcc_string(char *buf, char *end, const u32 *fourcc, |
| 85 | + char output[sizeof("0123 little-endian (0x01234567)")]; |
| 86 | + char *p = output; |
| 87 | + unsigned int i; |
| 88 | ++ bool pixel_fmt = false; |
| 89 | + u32 orig, val; |
| 90 | + |
| 91 | +- if (fmt[1] != 'c' || fmt[2] != 'c') |
| 92 | ++ if (fmt[1] != 'c') |
| 93 | + return error_string(buf, end, "(%p4?)", spec); |
| 94 | + |
| 95 | + if (check_pointer(&buf, end, fourcc, spec)) |
| 96 | + return buf; |
| 97 | + |
| 98 | + orig = get_unaligned(fourcc); |
| 99 | +- val = orig & ~BIT(31); |
| 100 | ++ switch (fmt[2]) { |
| 101 | ++ case 'h': |
| 102 | ++ break; |
| 103 | ++ case 'n': |
| 104 | ++ orig = swab32(orig); |
| 105 | ++ break; |
| 106 | ++ case 'l': |
| 107 | ++ orig = (__force u32)cpu_to_le32(orig); |
| 108 | ++ break; |
| 109 | ++ case 'b': |
| 110 | ++ orig = (__force u32)cpu_to_be32(orig); |
| 111 | ++ break; |
| 112 | ++ case 'c': |
| 113 | ++ /* Pixel formats are printed LSB-first */ |
| 114 | ++ pixel_fmt = true; |
| 115 | ++ break; |
| 116 | ++ default: |
| 117 | ++ return error_string(buf, end, "(%p4?)", spec); |
| 118 | ++ } |
| 119 | ++ |
| 120 | ++ val = pixel_fmt ? swab32(orig & ~BIT(31)) : orig; |
| 121 | + |
| 122 | + for (i = 0; i < sizeof(u32); i++) { |
| 123 | +- unsigned char c = val >> (i * 8); |
| 124 | ++ unsigned char c = val >> ((3 - i) * 8); |
| 125 | + |
| 126 | + /* Print non-control ASCII characters as-is, dot otherwise */ |
| 127 | + *p++ = isascii(c) && isprint(c) ? c : '.'; |
| 128 | + } |
| 129 | + |
| 130 | +- *p++ = ' '; |
| 131 | +- strcpy(p, orig & BIT(31) ? "big-endian" : "little-endian"); |
| 132 | +- p += strlen(p); |
| 133 | ++ if (pixel_fmt) { |
| 134 | ++ *p++ = ' '; |
| 135 | ++ strcpy(p, orig & BIT(31) ? "big-endian" : "little-endian"); |
| 136 | ++ p += strlen(p); |
| 137 | ++ } |
| 138 | + |
| 139 | + *p++ = ' '; |
| 140 | + *p++ = '('; |
| 141 | +diff --git a/scripts/checkpatch.pl b/scripts/checkpatch.pl |
| 142 | +index 7b28ad331..5595a0898 100755 |
| 143 | +--- a/scripts/checkpatch.pl |
| 144 | ++++ b/scripts/checkpatch.pl |
| 145 | +@@ -6904,7 +6904,7 @@ sub process { |
| 146 | + ($extension eq "f" && |
| 147 | + defined $qualifier && $qualifier !~ /^w/) || |
| 148 | + ($extension eq "4" && |
| 149 | +- defined $qualifier && $qualifier !~ /^cc/)) { |
| 150 | ++ defined $qualifier && $qualifier !~ /^c[hnlbc]/)) { |
| 151 | + $bad_specifier = $specifier; |
| 152 | + last; |
| 153 | + } |
| 154 | +-- |
| 155 | +2.49.0 |
| 156 | + |
0 commit comments