Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1puCt8-0000gQ-DJ for mharc-grub-devel@gnu.org; Wed, 03 May 2023 09:51:22 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1puCt2-0000eU-Qp for grub-devel@gnu.org; Wed, 03 May 2023 09:51:17 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1puCt0-0007dV-Vl for grub-devel@gnu.org; Wed, 03 May 2023 09:51:16 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1683121873; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=pZo3GOzp3BhOKaRahI24z7GSGvxhK4GXd66snMRLFCk=; b=CZVU3krZyoRiqg2nEoGQYxSnbFWSVH9cn8gmBcQ1eZmaNMKKoxr79Hyhh/iLlPWVYTOpxL FIXPwfXmePQDiQFY+MwYD0zfRji+tHMFCWgAY8hrl8UWZwp+1DeU5Mjx3lDNYXcSAMOXIs gw2XK5uUIj5QmsSVT2SOMbQ6gT+Yizk= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-256-tb6vulTQNbWgV_a9nKwQRA-1; Wed, 03 May 2023 09:51:10 -0400 X-MC-Unique: tb6vulTQNbWgV_a9nKwQRA-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 725F03C0F37A; Wed, 3 May 2023 13:51:10 +0000 (UTC) Received: from osteffen-laptop.redhat.com (unknown [10.39.193.168]) by smtp.corp.redhat.com (Postfix) with ESMTP id AF1882166B29; Wed, 3 May 2023 13:51:09 +0000 (UTC) From: Oliver Steffen To: grub-devel@gnu.org Cc: Daniel Kiper , Gerd Hoffmann , Oliver Steffen Subject: [PATCH v7 03/10] kern/misc: Add a format specifier GUIDs Date: Wed, 3 May 2023 15:50:12 +0200 Message-Id: <20230503135019.101181-4-osteffen@redhat.com> In-Reply-To: <20230503135019.101181-1-osteffen@redhat.com> References: <20230503135019.101181-1-osteffen@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.6 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true Received-SPF: pass client-ip=170.10.133.124; envelope-from=osteffen@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -22 X-Spam_score: -2.3 X-Spam_bar: -- X-Spam_report: (-2.3 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.161, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 03 May 2023 13:51:17 -0000 Extend the printf format specifier for pointers (%p) to accept a suffix specifier G to print GUIDs: %pG can be used to print grub_guid structs. This does not interfere with the -Wformat checking of gcc. Note that the data type is not checked though (%p accepts void*). Signed-off-by: Oliver Steffen --- grub-core/kern/misc.c | 81 ++++++++++++++++++++++++++++++++----------- 1 file changed, 60 insertions(+), 21 deletions(-) diff --git a/grub-core/kern/misc.c b/grub-core/kern/misc.c index dfae4f9d7..eadb46988 100644 --- a/grub-core/kern/misc.c +++ b/grub-core/kern/misc.c @@ -24,6 +24,7 @@ #include #include #include +#include union printf_arg { @@ -34,7 +35,8 @@ union printf_arg { INT, LONG, LONGLONG, UNSIGNED_INT = 3, UNSIGNED_LONG, UNSIGNED_LONGLONG, - STRING + STRING, + GUID } type; long long ll; }; @@ -772,6 +774,9 @@ parse_printf_arg_fmt (const char *fmt0, struct printf_args *args, switch (c) { case 'p': + if (*(fmt) == 'G') + ++fmt; + /* Fall through. */ case 'x': case 'X': case 'u': @@ -885,6 +890,10 @@ parse_printf_arg_fmt (const char *fmt0, struct printf_args *args, args->ptr[curn].type = UNSIGNED_LONGLONG; else args->ptr[curn].type = UNSIGNED_INT; + if (*(fmt) == 'G') { + args->ptr[curn].type = GUID; + ++fmt; + } break; case 's': args->ptr[curn].type = STRING; @@ -926,6 +935,7 @@ parse_printf_args (const char *fmt0, struct printf_args *args, va_list args_in) args->ptr[n].ll = va_arg (args_in, long long); break; case STRING: + case GUID: if (sizeof (void *) == sizeof (long long)) args->ptr[n].ll = va_arg (args_in, long long); else @@ -943,6 +953,27 @@ write_char (char *str, grub_size_t *count, grub_size_t max_len, unsigned char ch (*count)++; } +static void +write_number (char *str, grub_size_t *count, grub_size_t max_len, grub_size_t format1, + char rightfill, char zerofill, char c, long long value) +{ + char tmp[32]; + const char *p = tmp; + grub_size_t len; + grub_size_t fill; + + len = grub_lltoa (tmp, c, value) - tmp; + fill = len < format1 ? format1 - len : 0; + if (! rightfill) + while (fill--) + write_char (str, count, max_len, zerofill); + while (*p) + write_char (str, count, max_len, *p++); + if (rightfill) + while (fill--) + write_char (str, count, max_len, zerofill); +} + static int grub_vsnprintf_real (char *str, grub_size_t max_len, const char *fmt0, struct printf_args *args) @@ -1025,31 +1056,39 @@ grub_vsnprintf_real (char *str, grub_size_t max_len, const char *fmt0, switch (c) { case 'p': - write_char (str, &count, max_len, '0'); - write_char (str, &count, max_len, 'x'); - c = 'x'; + if (*(fmt) == 'G') + { + ++fmt; + grub_guid_t *guid = (grub_guid_t *) curarg; + write_number (str, &count, max_len, 8, 0, '0', 'x', grub_le_to_cpu32 (guid->data1)); + write_char (str, &count, max_len, '-'); + write_number (str, &count, max_len, 4, 0, '0', 'x', grub_le_to_cpu16 (guid->data2)); + write_char (str, &count, max_len, '-'); + write_number (str, &count, max_len, 4, 0, '0', 'x', grub_le_to_cpu16 (guid->data3)); + write_char (str, &count, max_len, '-'); + write_number (str, &count, max_len, 2, 0, '0', 'x', guid->data4[0]); + write_number (str, &count, max_len, 2, 0, '0', 'x', guid->data4[1]); + write_char (str, &count, max_len, '-'); + write_number (str, &count, max_len, 2, 0, '0', 'x', guid->data4[2]); + write_number (str, &count, max_len, 2, 0, '0', 'x', guid->data4[3]); + write_number (str, &count, max_len, 2, 0, '0', 'x', guid->data4[4]); + write_number (str, &count, max_len, 2, 0, '0', 'x', guid->data4[5]); + write_number (str, &count, max_len, 2, 0, '0', 'x', guid->data4[6]); + write_number (str, &count, max_len, 2, 0, '0', 'x', guid->data4[7]); + break; + } + else + { + write_char (str, &count, max_len, '0'); + write_char (str, &count, max_len, 'x'); + c = 'x'; + } /* Fall through. */ case 'x': case 'X': case 'u': case 'd': - { - char tmp[32]; - const char *p = tmp; - grub_size_t len; - grub_size_t fill; - - len = grub_lltoa (tmp, c, curarg) - tmp; - fill = len < format1 ? format1 - len : 0; - if (! rightfill) - while (fill--) - write_char (str, &count, max_len, zerofill); - while (*p) - write_char (str, &count, max_len, *p++); - if (rightfill) - while (fill--) - write_char (str, &count, max_len, zerofill); - } + write_number (str, &count, max_len, format1, rightfill, zerofill, c, curarg); break; case 'c': -- 2.40.1