Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

s390/boot: Add field width and padding handling to boot_printk()

Enhance boot_printk() to support field width and padding across all
argument types for better formatting.

Signed-off-by: Vasily Gorbik <gor@linux.ibm.com>
Acked-by: Heiko Carstens <hca@linux.ibm.com>
Signed-off-by: Alexander Gordeev <agordeev@linux.ibm.com>

authored by

Vasily Gorbik and committed by
Alexander Gordeev
3d846daa 9291d572

+50 -18
+50 -18
arch/s390/boot/printk.c
··· 13 13 14 14 const char hex_asc[] = "0123456789abcdef"; 15 15 16 + #define MAX_NUMLEN 17 16 17 static char *as_hex(char *dst, unsigned long val, int pad) 17 18 { 18 - char *p, *end = p = dst + max(pad, (int)__fls(val | 1) / 4 + 1); 19 + char *p = dst + max(pad, (int)__fls(val | 1) / 4 + 1); 19 20 20 - for (*p-- = 0; p >= dst; val >>= 4) 21 + for (*p-- = '\0'; p >= dst; val >>= 4) 21 22 *p-- = hex_asc[val & 0x0f]; 22 - return end; 23 + return dst; 24 + } 25 + 26 + static ssize_t strpad(char *dst, size_t dst_size, const char *src, 27 + int _pad, bool zero_pad) 28 + { 29 + ssize_t len = strlen(src), pad = _pad; 30 + char *p = dst; 31 + 32 + if (max(len, abs(pad)) >= dst_size) 33 + return -E2BIG; 34 + 35 + if (pad > len) { 36 + memset(p, zero_pad ? '0' : ' ', pad - len); 37 + p += pad - len; 38 + } 39 + memcpy(p, src, len); 40 + p += len; 41 + if (pad < 0 && -pad > len) { 42 + memset(p, ' ', -pad - len); 43 + p += -pad - len; 44 + } 45 + *p = '\0'; 46 + return p - dst; 23 47 } 24 48 25 49 static char *symstart(char *p) ··· 82 58 return NULL; 83 59 } 84 60 85 - static noinline char *strsym(void *ip) 61 + #define MAX_SYMLEN 64 62 + static noinline char *strsym(char *buf, void *ip) 86 63 { 87 - static char buf[64]; 88 64 unsigned short off; 89 65 unsigned short len; 90 66 char *p; 91 67 92 68 p = findsym((unsigned long)ip, &off, &len); 93 69 if (p) { 94 - strncpy(buf, p, sizeof(buf)); 70 + strncpy(buf, p, MAX_SYMLEN); 95 71 /* reserve 15 bytes for offset/len in symbol+0x1234/0x1234 */ 96 - p = buf + strnlen(buf, sizeof(buf) - 15); 72 + p = buf + strnlen(buf, MAX_SYMLEN - 15); 97 73 strcpy(p, "+0x"); 98 - p = as_hex(p + 3, off, 0); 99 - strcpy(p, "/0x"); 100 - as_hex(p + 3, len, 0); 74 + as_hex(p + 3, off, 0); 75 + strcat(p, "/0x"); 76 + as_hex(p + strlen(p), len, 0); 101 77 } else { 102 78 as_hex(buf, (unsigned long)ip, 16); 103 79 } ··· 115 91 { 116 92 char buf[1024] = { 0 }; 117 93 char *end = buf + sizeof(buf) - 1; /* make sure buf is 0 terminated */ 118 - unsigned long pad; 119 - char *p = buf; 94 + bool zero_pad; 95 + char *strval, *p = buf; 96 + char valbuf[MAX(MAX_SYMLEN, MAX_NUMLEN)]; 120 97 va_list args; 121 98 char lenmod; 99 + ssize_t len; 100 + int pad; 122 101 123 102 va_start(args, fmt); 124 103 for (; p < end && *fmt; fmt++) { ··· 133 106 *p++ = '%'; 134 107 continue; 135 108 } 136 - pad = isdigit(*fmt) ? simple_strtol(fmt, (char **)&fmt, 10) : 0; 109 + zero_pad = (*fmt == '0'); 110 + pad = simple_strtol(fmt, (char **)&fmt, 10); 137 111 lenmod = (*fmt == 'h' || *fmt == 'l' || *fmt == 'z') ? *fmt++ : 0; 138 112 if (lenmod == 'h' && *fmt == 'h') { 139 113 lenmod = 'H'; ··· 144 116 case 's': 145 117 if (lenmod) 146 118 goto out; 147 - p = buf + strlcat(buf, va_arg(args, char *), sizeof(buf)); 119 + strval = va_arg(args, char *); 120 + zero_pad = false; 148 121 break; 149 122 case 'p': 150 123 if (*++fmt != 'S' || lenmod) 151 124 goto out; 152 - p = buf + strlcat(buf, strsym(va_arg(args, void *)), sizeof(buf)); 125 + strval = strsym(valbuf, va_arg(args, void *)); 126 + zero_pad = false; 153 127 break; 154 128 case 'x': 155 - if (end - p <= max(sizeof(long) * 2, pad)) 156 - goto out; 157 - p = as_hex(p, va_arg_len_type(args, lenmod, unsigned), pad); 129 + strval = as_hex(valbuf, va_arg_len_type(args, lenmod, unsigned), 0); 158 130 break; 159 131 default: 160 132 goto out; 161 133 } 134 + len = strpad(p, end - p, strval, pad, zero_pad); 135 + if (len == -E2BIG) 136 + break; 137 + p += len; 162 138 } 163 139 out: 164 140 va_end(args);