From 91084b450226f22a8c6fee5a5762a4d0f44a18f4 Mon Sep 17 00:00:00 2001 From: Ahmad Fatoum Date: Mon, 28 Sep 2020 17:34:29 +0200 Subject: vsprintf: add %pe format specifier for printing symbolic error names MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Starting with v5.5, Linux has a format specifier for printing error pointers. We have had strerror in barebox before that, but lets wire it into vsprintf with the same format specifier that Linux now uses. This yields less verbose call sites and makes Linux drivers more portable to barebox in future. This also has the potential to reduce code size as the previously "inlined" strerror at callsites can now be replaced by a single vsprintf. Cc: Uwe Kleine-König Signed-off-by: Ahmad Fatoum Signed-off-by: Sascha Hauer --- lib/vsprintf.c | 39 +++++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 12 deletions(-) (limited to 'lib/vsprintf.c') diff --git a/lib/vsprintf.c b/lib/vsprintf.c index 3e5f4db75a..3fccfa7a56 100644 --- a/lib/vsprintf.c +++ b/lib/vsprintf.c @@ -177,6 +177,17 @@ static char *string(char *buf, const char *end, const char *s, int field_width, return buf; } +static char *raw_pointer(char *buf, const char *end, const void *ptr, int field_width, + int precision, int flags) +{ + flags |= SMALL; + if (field_width == -1) { + field_width = 2*sizeof(void *); + flags |= ZEROPAD; + } + return number(buf, end, (unsigned long) ptr, 16, field_width, precision, flags); +} + #ifndef __PBL__ static char *symbol_string(char *buf, const char *end, const void *ptr, int field_width, int precision, int flags) @@ -214,6 +225,16 @@ char *ip4_addr_string(char *buf, const char *end, const u8 *addr, int field_widt return string(buf, end, ip4_addr, field_width, precision, flags); } +static +char *error_string(char *buf, const char *end, const u8 *errptr, int field_width, + int precision, int flags, const char *fmt) +{ + if (!IS_ERR(errptr)) + return raw_pointer(buf, end, errptr, field_width, precision, flags); + + return string(buf, end, strerrorp(errptr), field_width, precision, flags); +} + static noinline_for_stack char *uuid_string(char *buf, const char *end, const u8 *addr, int field_width, int precision, int flags, const char *fmt) @@ -345,24 +366,18 @@ static char *pointer(const char *fmt, char *buf, const char *end, const void *pt return ip4_addr_string(buf, end, ptr, field_width, precision, flags, fmt); } break; + case 'e': + return error_string(buf, end, ptr, field_width, precision, flags, fmt); } - flags |= SMALL; - if (field_width == -1) { - field_width = 2*sizeof(void *); - flags |= ZEROPAD; - } - return number(buf, end, (unsigned long) ptr, 16, field_width, precision, flags); + + return raw_pointer(buf, end, ptr, field_width, precision, flags); } + #else static char *pointer(const char *fmt, char *buf, const char *end, const void *ptr, int field_width, int precision, int flags) { - flags |= SMALL; - if (field_width == -1) { - field_width = 2*sizeof(void *); - flags |= ZEROPAD; - } - return number(buf, end, (unsigned long) ptr, 16, field_width, precision, flags); + return raw_pointer(buf, end, ptr, field_width, precision, flags); } #endif -- cgit v1.2.3