From patchwork Mon May 18 19:06:53 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arvind Sankar X-Patchwork-Id: 206331 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.8 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6DCBEC433E1 for ; Mon, 18 May 2020 19:07:20 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4708620829 for ; Mon, 18 May 2020 19:07:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726283AbgERTHU (ORCPT ); Mon, 18 May 2020 15:07:20 -0400 Received: from mail-qv1-f68.google.com ([209.85.219.68]:36726 "EHLO mail-qv1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726374AbgERTHT (ORCPT ); Mon, 18 May 2020 15:07:19 -0400 Received: by mail-qv1-f68.google.com with SMTP id a4so5282501qvj.3 for ; Mon, 18 May 2020 12:07:18 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=kTfX7FMnAEHXPLMZO7UqEZul4KGH8VsTd//SDkdkJUQ=; b=f1O+qjWj3xUZHMAdKY1l7N9f44iGaDCKCaCMyHqJAC3tiMamKuJxzY+EXq3uFajAve 4gBJrw83TohxIaJ/GzqtVjAU4+o3TNXjt8pzkxORv6rb20A2p47+3NFGSNqkGc3p1JHc 04ytxEV5ntVq6rpQU4j93EY64oR9kY7XDF3XgRiViISDMgHMDcDNIkQxiS0qMyXgmnW4 GV4Dk/cwxFuol4mRuBLX3LUr3mxKjKB+ppKdmi4ZCG6SsV2r4Lg3BTLxVYWlYR0jwGBh LEAQPKN2AbMLr+LFiU/C/mcQlx5bHyyuLddPM8VUTOHWM9qvxmT92XACFZPoFTdcqqJp m5AA== X-Gm-Message-State: AOAM531La0zrJEmf+lOHT10G4ozvr2FggKXr2bh/eOnTXPHF2BMQHlYV L5m0MdD+j0vBaPOrXEnTJmg= X-Google-Smtp-Source: ABdhPJwdbBQtjMPz5o0qL7OkMH78XMNvXVY8sYfMx4mhBF39R8yjgibBH38mLGhkcxzQ0/R13QdGTg== X-Received: by 2002:a05:6214:1c2:: with SMTP id c2mr17933734qvt.185.1589828838181; Mon, 18 May 2020 12:07:18 -0700 (PDT) Received: from rani.riverdale.lan ([2001:470:1f07:5f3::b55f]) by smtp.gmail.com with ESMTPSA id a1sm9862811qtj.65.2020.05.18.12.07.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 May 2020 12:07:17 -0700 (PDT) From: Arvind Sankar To: Ard Biesheuvel Cc: linux-efi@vger.kernel.org Subject: [PATCH 01/24] efi/libstub: Include dependencies of efistub.h Date: Mon, 18 May 2020 15:06:53 -0400 Message-Id: <20200518190716.751506-2-nivedita@alum.mit.edu> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200518190716.751506-1-nivedita@alum.mit.edu> References: <20200518190716.751506-1-nivedita@alum.mit.edu> MIME-Version: 1.0 Sender: linux-efi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-efi@vger.kernel.org Add #include directives for include files that efistub.h depends on, instead of relying on them having been included by the C source files prior to efistub.h. Signed-off-by: Arvind Sankar --- drivers/firmware/efi/libstub/efistub.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/drivers/firmware/efi/libstub/efistub.h b/drivers/firmware/efi/libstub/efistub.h index 15d0b6f3f6c6..998924916b03 100644 --- a/drivers/firmware/efi/libstub/efistub.h +++ b/drivers/firmware/efi/libstub/efistub.h @@ -3,6 +3,11 @@ #ifndef _DRIVERS_FIRMWARE_EFI_EFISTUB_H #define _DRIVERS_FIRMWARE_EFI_EFISTUB_H +#include +#include +#include +#include + /* error code which can't be mistaken for valid address */ #define EFI_ERROR (~0UL) From patchwork Mon May 18 19:06:55 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arvind Sankar X-Patchwork-Id: 206330 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.8 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id E28ECC433DF for ; Mon, 18 May 2020 19:07:21 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id BD33320674 for ; Mon, 18 May 2020 19:07:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726388AbgERTHV (ORCPT ); Mon, 18 May 2020 15:07:21 -0400 Received: from mail-qk1-f193.google.com ([209.85.222.193]:33854 "EHLO mail-qk1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726374AbgERTHV (ORCPT ); Mon, 18 May 2020 15:07:21 -0400 Received: by mail-qk1-f193.google.com with SMTP id 190so11493970qki.1 for ; Mon, 18 May 2020 12:07:20 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=KNt8xSFOeACzamGszyWkjjITHV6tNlwlTbwDDnXubWM=; b=HIhlyCs2KVkcfpfpR0sFv8ei05QxCRjWzicc+CzzLbT1GhRO1hCHMhdu+ZZazFFZ74 cPXtgElpY6YyRSctUdzNZ1j8lN7bbKENlgs7SQXFPi0S/JftGt1eKNVHV/Nec1ViUnke 1QvptfPYU3PXOXfUWrFUyepH/x1qT2YGEhMKZ2yg6jZI7H3sIp4UcqpA0s7HXavaSY8V s7zHLf4zFEg/yJ/a4o/gSlGYbyf0VkRAbpWRcciImQVgMoCIYujcKVxU9neigF/6Pyiw CKEG3GRLafhp9jTAn0LU7r1NJ/OBWz/w4OgepxuQsZ7Vdmlv5lp9dJhXVyYuh7KLh+Rl nvkA== X-Gm-Message-State: AOAM532ZVfGA2RMDpHiz9eCX4hDQSxPCvNYt6je+pyegPhqTAHOIKFXW RNRmu25MNDpQmBxXFygRliI= X-Google-Smtp-Source: ABdhPJzkFod1ZyEwDCU12Z3qIpMo2eJHUiV2r0lUZwSG41RaN1YuFZ4MXz3n7/6CSpf690W0RH1Aaw== X-Received: by 2002:a05:620a:1312:: with SMTP id o18mr17762193qkj.277.1589828839848; Mon, 18 May 2020 12:07:19 -0700 (PDT) Received: from rani.riverdale.lan ([2001:470:1f07:5f3::b55f]) by smtp.gmail.com with ESMTPSA id a1sm9862811qtj.65.2020.05.18.12.07.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 May 2020 12:07:19 -0700 (PDT) From: Arvind Sankar To: Ard Biesheuvel Cc: linux-efi@vger.kernel.org Subject: [PATCH 03/24] efi/libstub: Buffer output of efi_puts Date: Mon, 18 May 2020 15:06:55 -0400 Message-Id: <20200518190716.751506-4-nivedita@alum.mit.edu> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200518190716.751506-1-nivedita@alum.mit.edu> References: <20200518190716.751506-1-nivedita@alum.mit.edu> MIME-Version: 1.0 Sender: linux-efi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-efi@vger.kernel.org Use a buffer to convert the string to UTF-16. This will reduce the number of firmware calls required to print the string from one per character to one per string in most cases. Cast the input char to unsigned char before converting to efi_char16_t to avoid sign-extension in case there are any non-ASCII characters in the input. Signed-off-by: Arvind Sankar --- .../firmware/efi/libstub/efi-stub-helper.c | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/drivers/firmware/efi/libstub/efi-stub-helper.c b/drivers/firmware/efi/libstub/efi-stub-helper.c index c6d7ef35e9f7..3cf506ab9ead 100644 --- a/drivers/firmware/efi/libstub/efi-stub-helper.c +++ b/drivers/firmware/efi/libstub/efi-stub-helper.c @@ -8,6 +8,7 @@ */ #include +#include #include #include "efistub.h" @@ -34,13 +35,19 @@ void efi_char16_puts(efi_char16_t *str) void efi_puts(const char *str) { - while (*str) { - efi_char16_t ch[] = { *str++, L'\0' }; + efi_char16_t buf[128]; + size_t pos = 0, lim = ARRAY_SIZE(buf); - if (ch[0] == L'\n') - efi_char16_puts(L"\r\n"); - else - efi_char16_puts(ch); + while (*str) { + if (*str == '\n') + buf[pos++] = L'\r'; + /* Cast to unsigned char to avoid sign-extension */ + buf[pos++] = (unsigned char)(*str++); + if (*str == '\0' || pos >= lim - 2) { + buf[pos] = L'\0'; + efi_char16_puts(buf); + pos = 0; + } } } From patchwork Mon May 18 19:06:57 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arvind Sankar X-Patchwork-Id: 206328 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.8 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id CB05CC433E2 for ; Mon, 18 May 2020 19:07:25 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A69AF207FB for ; Mon, 18 May 2020 19:07:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726378AbgERTHY (ORCPT ); Mon, 18 May 2020 15:07:24 -0400 Received: from mail-qk1-f195.google.com ([209.85.222.195]:41406 "EHLO mail-qk1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726374AbgERTHW (ORCPT ); Mon, 18 May 2020 15:07:22 -0400 Received: by mail-qk1-f195.google.com with SMTP id n14so11437919qke.8 for ; Mon, 18 May 2020 12:07:21 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=J8O+Zu4CL6oayPok1as4pcKubIbMHa01zS1BoB+sV7g=; b=VkAsdo5svBpPcyN20674HUqFg69GWQdNbYNBP2aLBRq/+3RilCYFW6PGsOg/KRCUUC WRmvDxZdBZX5oRV8u+FVFMg4HIICCohdUygI41JzUWf7pirSq0Y5L4ddi9vjmo3PyyD9 vRRpYna/YWMOGGFSBrJjrFeT6rhYSBEhT/U7Xl/76+bBgXGDNgjdNKwa3FIpoYOUHvzY sk7g8ydbJlIs5dRyaJ0+nSnGerJkAHCtun3F5TR9Oklw7tBXONAmXO7wSyZyeon81NUL W5aE7xsMa/IQWURBpQeCXyN32uA633ljSs36QEmXSEOqTAJBSGBTsRSZ+ZRw0XkyBygA HFSQ== X-Gm-Message-State: AOAM530uXmPBB3uJbpzzbAzXM6mmDtd0ZSklHdq1U1ITSyQ1u6B8WrXo gyczt6uZrHKwoZquUn474/SUQJ9MAbs= X-Google-Smtp-Source: ABdhPJy3RwaBlxlJ4J4WBQvu5V+Hg5jY92Bn0yP20dZimfZkSmrXLyPDRBJiJC9cPJMhCF/1f6w0Ow== X-Received: by 2002:a37:67c6:: with SMTP id b189mr17927750qkc.285.1589828841410; Mon, 18 May 2020 12:07:21 -0700 (PDT) Received: from rani.riverdale.lan ([2001:470:1f07:5f3::b55f]) by smtp.gmail.com with ESMTPSA id a1sm9862811qtj.65.2020.05.18.12.07.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 May 2020 12:07:21 -0700 (PDT) From: Arvind Sankar To: Ard Biesheuvel Cc: linux-efi@vger.kernel.org Subject: [PATCH 05/24] efi/libstub: Optimize for size instead of speed Date: Mon, 18 May 2020 15:06:57 -0400 Message-Id: <20200518190716.751506-6-nivedita@alum.mit.edu> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200518190716.751506-1-nivedita@alum.mit.edu> References: <20200518190716.751506-1-nivedita@alum.mit.edu> MIME-Version: 1.0 Sender: linux-efi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-efi@vger.kernel.org Reclaim the bloat from the addition of printf by optimizing the stub for size. With gcc 9, the text size of the stub is: ARCH before +printf -Os arm 35197 37889 34638 arm64 34883 38159 34479 i386 18571 21657 17025 x86_64 25677 29328 22144 Signed-off-by: Arvind Sankar --- drivers/firmware/efi/libstub/Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/firmware/efi/libstub/Makefile b/drivers/firmware/efi/libstub/Makefile index fb34c9d14a3c..034d71663b1e 100644 --- a/drivers/firmware/efi/libstub/Makefile +++ b/drivers/firmware/efi/libstub/Makefile @@ -7,7 +7,7 @@ # cflags-$(CONFIG_X86_32) := -march=i386 cflags-$(CONFIG_X86_64) := -mcmodel=small -cflags-$(CONFIG_X86) += -m$(BITS) -D__KERNEL__ -O2 \ +cflags-$(CONFIG_X86) += -m$(BITS) -D__KERNEL__ \ -fPIC -fno-strict-aliasing -mno-red-zone \ -mno-mmx -mno-sse -fshort-wchar \ -Wno-pointer-sign \ @@ -25,7 +25,7 @@ cflags-$(CONFIG_ARM) := $(subst $(CC_FLAGS_FTRACE),,$(KBUILD_CFLAGS)) \ cflags-$(CONFIG_EFI_GENERIC_STUB) += -I$(srctree)/scripts/dtc/libfdt -KBUILD_CFLAGS := $(cflags-y) -DDISABLE_BRANCH_PROFILING \ +KBUILD_CFLAGS := $(cflags-y) -Os -DDISABLE_BRANCH_PROFILING \ -include $(srctree)/drivers/firmware/efi/libstub/hidden.h \ -D__NO_FORTIFY \ $(call cc-option,-ffreestanding) \ From patchwork Mon May 18 19:06:58 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arvind Sankar X-Patchwork-Id: 206329 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.8 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0810DC433E1 for ; Mon, 18 May 2020 19:07:26 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id D66F4207FB for ; Mon, 18 May 2020 19:07:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726508AbgERTHY (ORCPT ); Mon, 18 May 2020 15:07:24 -0400 Received: from mail-qk1-f195.google.com ([209.85.222.195]:38808 "EHLO mail-qk1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726502AbgERTHX (ORCPT ); Mon, 18 May 2020 15:07:23 -0400 Received: by mail-qk1-f195.google.com with SMTP id f189so11453404qkd.5 for ; Mon, 18 May 2020 12:07:22 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=pTPDOaRxKOCuKruamlS+/pcEj8EnVB4SkdQgfaS27nc=; b=LspY+EZjVBAYcPsW5aipRqUlH72zgN6SSQRyNmxIGv99CLOUbST5AEF42SUVtt7STX 24DB5GgI3gY+E9/67zBMqX0pJLz7jscsV4VOSU4AN7bqkSfD2TJNiicnv+BY8r1DYFef qujtsONeqXtPYXXp0Sx3UYKKrjmZEKY8hiumtRAx63J2FOSOXn6EoWqAnBGG3NBc1o3W xk7kVtwwcPKbH5YJPVxYtpKXpK3Yuxv7Yj0lx/TmOClc7bb3MUFSgCU7u7t0d3e3npTp gcel5aV1pPU/bfyA3zQBATFaXPQhZkngOjgwikublRooogpn+sSYvE4UWbIOQLq4Hfkp obLg== X-Gm-Message-State: AOAM532ytVaM5Cqpg3/zID3ql9ETE36DGWz94FU+Gw6TrNqo5/0mG//G 0bBYkTIVWZdFCdSCBgp+VhFcGVKFc2I= X-Google-Smtp-Source: ABdhPJw/RlU69ORrvk0PfXkiiSg/+y1Nzw5/mpjFjpwXSLlHysTaxyUr1pyO1iu4hMjojpIH0d44Jw== X-Received: by 2002:a05:620a:108e:: with SMTP id g14mr17286309qkk.337.1589828842158; Mon, 18 May 2020 12:07:22 -0700 (PDT) Received: from rani.riverdale.lan ([2001:470:1f07:5f3::b55f]) by smtp.gmail.com with ESMTPSA id a1sm9862811qtj.65.2020.05.18.12.07.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 May 2020 12:07:21 -0700 (PDT) From: Arvind Sankar To: Ard Biesheuvel Cc: linux-efi@vger.kernel.org Subject: [PATCH 06/24] efi/printf: Drop %n format and L qualifier Date: Mon, 18 May 2020 15:06:58 -0400 Message-Id: <20200518190716.751506-7-nivedita@alum.mit.edu> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200518190716.751506-1-nivedita@alum.mit.edu> References: <20200518190716.751506-1-nivedita@alum.mit.edu> MIME-Version: 1.0 Sender: linux-efi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-efi@vger.kernel.org %n is unused and deprecated. The L qualifer is parsed but not actually implemented. Signed-off-by: Arvind Sankar --- drivers/firmware/efi/libstub/vsprintf.c | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/drivers/firmware/efi/libstub/vsprintf.c b/drivers/firmware/efi/libstub/vsprintf.c index 88483a49bac4..2815196de40b 100644 --- a/drivers/firmware/efi/libstub/vsprintf.c +++ b/drivers/firmware/efi/libstub/vsprintf.c @@ -127,7 +127,7 @@ int vsprintf(char *buf, const char *fmt, va_list args) int field_width; /* width of output field */ int precision; /* min. # of digits for integers; max number of chars for from string */ - int qualifier; /* 'h', 'l', or 'L' for integer fields */ + int qualifier; /* 'h' or 'l' for integer fields */ for (str = buf; *fmt; ++fmt) { if (*fmt != '%') { @@ -188,7 +188,7 @@ int vsprintf(char *buf, const char *fmt, va_list args) /* get the conversion qualifier */ qualifier = -1; - if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L') { + if (*fmt == 'h' || *fmt == 'l') { qualifier = *fmt; ++fmt; } @@ -229,16 +229,6 @@ int vsprintf(char *buf, const char *fmt, va_list args) field_width, precision, flags); continue; - case 'n': - if (qualifier == 'l') { - long *ip = va_arg(args, long *); - *ip = (str - buf); - } else { - int *ip = va_arg(args, int *); - *ip = (str - buf); - } - continue; - case '%': *str++ = '%'; continue; From patchwork Mon May 18 19:07:01 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arvind Sankar X-Patchwork-Id: 206327 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.8 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C346DC433E0 for ; Mon, 18 May 2020 19:07:29 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A588020674 for ; Mon, 18 May 2020 19:07:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726505AbgERTH0 (ORCPT ); Mon, 18 May 2020 15:07:26 -0400 Received: from mail-qt1-f195.google.com ([209.85.160.195]:44591 "EHLO mail-qt1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726374AbgERTHZ (ORCPT ); Mon, 18 May 2020 15:07:25 -0400 Received: by mail-qt1-f195.google.com with SMTP id d7so9026176qtn.11 for ; Mon, 18 May 2020 12:07:24 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=rYpaku212VJjExSBlM2ZzYjE+6P3nlhBNBcdulIFhoU=; b=IzVyDNetVQgq9t49BsWRmwJJEZHOUy0iqAPEH0uU2vNkLy+/eX4M1A3QN16g3JQXnZ v3CFY/NkzpR+I/A3R5GjvM3scT8QmYkLaGnL0NlBAJq+Z3bEx1J1VXjulEWBWlaOgSoo qnwIiDBFN2PPuDcbbYkCF+uFj2Xif6yIhoHThXUyJiRnFBm3PkU1JOQr5xS9TjLAGHg0 QT+tGxveRgr3jxKqGEemLA3z338H/j7nMDSCwC+10KkE95WE91vhgZoftV5bNQcG9SRb HnZ0fUVZ9S5o3jOg65kAlWLRETQlWGF3EN/acqG803lkot8HYeQM2N2EfGPu4Onbc9R1 zTbA== X-Gm-Message-State: AOAM532OsFRtu8aPJ3QPd4YEkk40Yl1OUZEks4cZW9iJiIX1dplSuV+u WC7Jbo1MMUDsNhAnLO5QGaniZ8H0asw= X-Google-Smtp-Source: ABdhPJxtUgMDz7u+NgwooehJFh57dodV0S2cJdhogZbx3w6PfCwElUB2VHV1FKpOXdKrSNzPcjsZOg== X-Received: by 2002:ac8:1e16:: with SMTP id n22mr18152585qtl.78.1589828844378; Mon, 18 May 2020 12:07:24 -0700 (PDT) Received: from rani.riverdale.lan ([2001:470:1f07:5f3::b55f]) by smtp.gmail.com with ESMTPSA id a1sm9862811qtj.65.2020.05.18.12.07.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 May 2020 12:07:23 -0700 (PDT) From: Arvind Sankar To: Ard Biesheuvel Cc: linux-efi@vger.kernel.org Subject: [PATCH 09/24] efi/printf: Fix minor bug in precision handling Date: Mon, 18 May 2020 15:07:01 -0400 Message-Id: <20200518190716.751506-10-nivedita@alum.mit.edu> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200518190716.751506-1-nivedita@alum.mit.edu> References: <20200518190716.751506-1-nivedita@alum.mit.edu> MIME-Version: 1.0 Sender: linux-efi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-efi@vger.kernel.org A negative precision should be ignored completely, and the presence of a valid precision should turn off the 0 flag. Signed-off-by: Arvind Sankar --- drivers/firmware/efi/libstub/vsprintf.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/drivers/firmware/efi/libstub/vsprintf.c b/drivers/firmware/efi/libstub/vsprintf.c index b3ed4af77af5..d7938e44f067 100644 --- a/drivers/firmware/efi/libstub/vsprintf.c +++ b/drivers/firmware/efi/libstub/vsprintf.c @@ -278,9 +278,10 @@ int vsprintf(char *buf, const char *fmt, va_list args) ++fmt; /* it's the next argument */ precision = va_arg(args, int); - } - if (precision < 0) + } else precision = 0; + if (precision >= 0) + flags &= ~ZEROPAD; } /* get the conversion qualifier */ From patchwork Mon May 18 19:07:03 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arvind Sankar X-Patchwork-Id: 206325 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.8 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id F321EC433E5 for ; Mon, 18 May 2020 19:07:30 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id DE0CB2081A for ; Mon, 18 May 2020 19:07:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726535AbgERTHa (ORCPT ); Mon, 18 May 2020 15:07:30 -0400 Received: from mail-qt1-f195.google.com ([209.85.160.195]:46335 "EHLO mail-qt1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726528AbgERTH1 (ORCPT ); Mon, 18 May 2020 15:07:27 -0400 Received: by mail-qt1-f195.google.com with SMTP id p12so9022363qtn.13 for ; Mon, 18 May 2020 12:07:26 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=0x0nqDDMF4dHcnE6V7iQ+GVCAY1e0HZWp+v/+DLW0rU=; b=BgHZpkRe653+csAVUh8/XZIzPUPM4WOvWArA/zDxzwjz0une7vDJJIxmrZc+pJSVWK w2gZAvYwJxU9qIEohNis16JCufZLgk6rnuTSzSDuHByRB9p1dGxIOZVOXNZxrCv4MypQ HJCHe42C3SeqIzhbNlyyOh6KPra5Ntl0DeXPm5wdesjp2jxoHu84PNkaExc7I/76iQni EAlLAQYXEG5botOviPwHD3mCws54PgZE/s+s3qOwnL6uqCUn7nRG3pqGyQ8rbkJy5uD1 7+wfe2PTMYo+CLNmba8fNhPJfE27N574baQxB981woU5SFJ4Vs2M0TtpftgFMzdN6C6g ECkA== X-Gm-Message-State: AOAM530QSlLMhULeGJv7lTuh1NpSFOrDplm9gbM14REPKROzC8ibX9Dg 329yGPlz7nG/assvfyPE7DEewWUvTYw= X-Google-Smtp-Source: ABdhPJw04OQz/RB2aTGvOKSwk88p1M8cVq4LmbHL/sQi1Os7ZRp7T1TefvwRn0GPoYJsDMh9HJ7NEg== X-Received: by 2002:ac8:1c3d:: with SMTP id a58mr18618889qtk.52.1589828846296; Mon, 18 May 2020 12:07:26 -0700 (PDT) Received: from rani.riverdale.lan ([2001:470:1f07:5f3::b55f]) by smtp.gmail.com with ESMTPSA id a1sm9862811qtj.65.2020.05.18.12.07.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 May 2020 12:07:25 -0700 (PDT) From: Arvind Sankar To: Ard Biesheuvel Cc: linux-efi@vger.kernel.org Subject: [PATCH 11/24] efi/printf: Factor out width/precision parsing Date: Mon, 18 May 2020 15:07:03 -0400 Message-Id: <20200518190716.751506-12-nivedita@alum.mit.edu> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200518190716.751506-1-nivedita@alum.mit.edu> References: <20200518190716.751506-1-nivedita@alum.mit.edu> MIME-Version: 1.0 Sender: linux-efi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-efi@vger.kernel.org Factor out the width/precision parsing into a helper function. Signed-off-by: Arvind Sankar --- drivers/firmware/efi/libstub/vsprintf.c | 60 ++++++++++++++++--------- 1 file changed, 40 insertions(+), 20 deletions(-) diff --git a/drivers/firmware/efi/libstub/vsprintf.c b/drivers/firmware/efi/libstub/vsprintf.c index 88c503077b92..1b71651fe6bc 100644 --- a/drivers/firmware/efi/libstub/vsprintf.c +++ b/drivers/firmware/efi/libstub/vsprintf.c @@ -230,7 +230,20 @@ int get_flags(const char **fmt) } while (1); } -int vsprintf(char *buf, const char *fmt, va_list args) +static +int get_int(const char **fmt, va_list *ap) +{ + if (isdigit(**fmt)) + return skip_atoi(fmt); + if (**fmt == '*') { + ++(*fmt); + /* it's the next argument */ + return va_arg(*ap, int); + } + return 0; +} + +int vsprintf(char *buf, const char *fmt, va_list ap) { int len; unsigned long long num; @@ -245,6 +258,24 @@ int vsprintf(char *buf, const char *fmt, va_list args) number of chars for from string */ int qualifier; /* 'h', 'hh', 'l' or 'll' for integer fields */ + va_list args; + + /* + * We want to pass our input va_list to helper functions by reference, + * but there's an annoying edge case. If va_list was originally passed + * to us by value, we could just pass &ap down to the helpers. This is + * the case on, for example, X86_32. + * However, on X86_64 (and possibly others), va_list is actually a + * size-1 array containing a structure. Our function parameter ap has + * decayed from T[1] to T*, and &ap has type T** rather than T(*)[1], + * which is what will be expected by a function taking a va_list * + * parameter. + * One standard way to solve this mess is by creating a copy in a local + * variable of type va_list and then passing a pointer to that local + * copy instead, which is what we do here. + */ + va_copy(args, ap); + for (str = buf; *fmt; ++fmt) { if (*fmt != '%' || *++fmt == '%') { *str++ = *fmt; @@ -255,31 +286,17 @@ int vsprintf(char *buf, const char *fmt, va_list args) flags = get_flags(&fmt); /* get field width */ - field_width = -1; - if (isdigit(*fmt)) - field_width = skip_atoi(&fmt); - else if (*fmt == '*') { - ++fmt; - /* it's the next argument */ - field_width = va_arg(args, int); - if (field_width < 0) { - field_width = -field_width; - flags |= LEFT; - } + field_width = get_int(&fmt, &args); + if (field_width < 0) { + field_width = -field_width; + flags |= LEFT; } /* get the precision */ precision = -1; if (*fmt == '.') { ++fmt; - if (isdigit(*fmt)) - precision = skip_atoi(&fmt); - else if (*fmt == '*') { - ++fmt; - /* it's the next argument */ - precision = va_arg(args, int); - } else - precision = 0; + precision = get_int(&fmt, &args); if (precision >= 0) flags &= ~ZEROPAD; } @@ -390,6 +407,9 @@ int vsprintf(char *buf, const char *fmt, va_list args) str = number(str, num, base, field_width, precision, flags); } *str = '\0'; + + va_end(args); + return str - buf; } From patchwork Mon May 18 19:07:04 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arvind Sankar X-Patchwork-Id: 206326 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.8 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id B1B68C433E2 for ; Mon, 18 May 2020 19:07:30 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 9BBF72081A for ; Mon, 18 May 2020 19:07:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726504AbgERTH3 (ORCPT ); Mon, 18 May 2020 15:07:29 -0400 Received: from mail-qk1-f195.google.com ([209.85.222.195]:46398 "EHLO mail-qk1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726374AbgERTH2 (ORCPT ); Mon, 18 May 2020 15:07:28 -0400 Received: by mail-qk1-f195.google.com with SMTP id f83so11404771qke.13 for ; Mon, 18 May 2020 12:07:27 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=O8XrfycEIcko67P/+2vaSkuEOC/bC3iipVXq8ggqVEc=; b=RDpkv4E7ReflB6vE9PdmJQ9t1vlRkIRARyjasdZQ4AZWQRvp0UPnWlv8MOrcy6i0fN V725pusK5Hs/dia8zwz/Ns332uWrWU2cTmGp5Gvm30m7XOU/Bdc/JqumqCWv3UcubuuM 69qgE95BXNOa/6L4bcgf4er8LkQv9zCL2Pl/hTurGplf1AtkZAz3U6UPhFjj7eJjU0Gw oSbuukOIq9uGjBYQlwX6zYRfYIPTFmKquyObo0GErb3cFHs2c6MGeKAV3D/GYr3O4Yx6 kTjEHti7H3y3LLk5mmDPRRudXBPUyHKRyQahvsgGiZWuNs7e/RYnbuF4/7xTV163kdYt 2OVQ== X-Gm-Message-State: AOAM530m8j3OxfJHE2rPFqLqKn8xPweAPRsqJKPepTxdddKxA9KQRn36 6nzPMwVboJBrPu32mNKjMeU= X-Google-Smtp-Source: ABdhPJwYzl1cWw1wKtls3UiSopO/nWEFNr9D21bL/0X3OWHkQ0O73YSpw1BHn9hHICwxe7MYRfdJMQ== X-Received: by 2002:a37:66d5:: with SMTP id a204mr17002208qkc.188.1589828847179; Mon, 18 May 2020 12:07:27 -0700 (PDT) Received: from rani.riverdale.lan ([2001:470:1f07:5f3::b55f]) by smtp.gmail.com with ESMTPSA id a1sm9862811qtj.65.2020.05.18.12.07.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 May 2020 12:07:26 -0700 (PDT) From: Arvind Sankar To: Ard Biesheuvel Cc: linux-efi@vger.kernel.org Subject: [PATCH 12/24] efi/printf: Factor out integer argument retrieval Date: Mon, 18 May 2020 15:07:04 -0400 Message-Id: <20200518190716.751506-13-nivedita@alum.mit.edu> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200518190716.751506-1-nivedita@alum.mit.edu> References: <20200518190716.751506-1-nivedita@alum.mit.edu> MIME-Version: 1.0 Sender: linux-efi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-efi@vger.kernel.org Factor out the code to get the correct type of numeric argument into a helper function. Signed-off-by: Arvind Sankar --- drivers/firmware/efi/libstub/vsprintf.c | 69 ++++++++++++------------- 1 file changed, 34 insertions(+), 35 deletions(-) diff --git a/drivers/firmware/efi/libstub/vsprintf.c b/drivers/firmware/efi/libstub/vsprintf.c index 1b71651fe6bc..dee9bd068cb6 100644 --- a/drivers/firmware/efi/libstub/vsprintf.c +++ b/drivers/firmware/efi/libstub/vsprintf.c @@ -243,6 +243,38 @@ int get_int(const char **fmt, va_list *ap) return 0; } +static +unsigned long long get_number(int sign, int qualifier, va_list *ap) +{ + if (sign) { + switch (qualifier) { + case 'L': + return va_arg(*ap, long long); + case 'l': + return va_arg(*ap, long); + case 'h': + return (short)va_arg(*ap, int); + case 'H': + return (signed char)va_arg(*ap, int); + default: + return va_arg(*ap, int); + }; + } else { + switch (qualifier) { + case 'L': + return va_arg(*ap, unsigned long long); + case 'l': + return va_arg(*ap, unsigned long); + case 'h': + return (unsigned short)va_arg(*ap, int); + case 'H': + return (unsigned char)va_arg(*ap, int); + default: + return va_arg(*ap, unsigned int); + } + } +} + int vsprintf(char *buf, const char *fmt, va_list ap) { int len; @@ -369,41 +401,8 @@ int vsprintf(char *buf, const char *fmt, va_list ap) } if (*fmt == 'p') num = (unsigned long)va_arg(args, void *); - else if (flags & SIGN) { - switch (qualifier) { - case 'L': - num = va_arg(args, long long); - break; - case 'l': - num = va_arg(args, long); - break; - case 'h': - num = (short)va_arg(args, int); - break; - case 'H': - num = (signed char)va_arg(args, int); - break; - default: - num = va_arg(args, int); - } - } else { - switch (qualifier) { - case 'L': - num = va_arg(args, unsigned long long); - break; - case 'l': - num = va_arg(args, unsigned long); - break; - case 'h': - num = (unsigned short)va_arg(args, int); - break; - case 'H': - num = (unsigned char)va_arg(args, int); - break; - default: - num = va_arg(args, unsigned int); - } - } + else + num = get_number(flags & SIGN, qualifier, &args); str = number(str, num, base, field_width, precision, flags); } *str = '\0'; From patchwork Mon May 18 19:07:06 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arvind Sankar X-Patchwork-Id: 206324 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.8 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1B302C433DF for ; Mon, 18 May 2020 19:07:34 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 00CCB20674 for ; Mon, 18 May 2020 19:07:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726560AbgERTHc (ORCPT ); Mon, 18 May 2020 15:07:32 -0400 Received: from mail-qk1-f194.google.com ([209.85.222.194]:33917 "EHLO mail-qk1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726545AbgERTHb (ORCPT ); Mon, 18 May 2020 15:07:31 -0400 Received: by mail-qk1-f194.google.com with SMTP id 190so11494936qki.1 for ; Mon, 18 May 2020 12:07:29 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=oK/JS8JFefZGjFuuEOCa4qvugMfmPh8fl1DUkLqdGmA=; b=Pz+0kvYtHegdt5T9aj6BAok5XVxy03LRKpVILpWaXziWSA2mZPvfapEfgpjjL2GAgD ClSUBZmOYBHdekXd2HfnoZe9CnZUtUTZpMsctjGdlr0yAEYKpwudzKfVwVMkkzcdz6q4 vgy7h2R/n6hRSlgvUn5c96xWCHj9H2RXQnC1JC3RKojUK0ZwT1pBxGeUlV/RsWfp3y7s DEMaa+M59ovZaRITxa1V+/CT5TTSPKimZDHQzuY4JE5CKK1PtvqDu4cN3ceLTx5M9OAq 3LrAlLTRdfZE3jWE8kNgIXsfTYof5N5xPwh6pQx7e8iT9wy1DprOiyirHYmGgDs/IZKS ovtw== X-Gm-Message-State: AOAM5314joSykr77r85Ww9+DzFV7MRJk1PvpvYHIYo6KH3CRznL9Iu5C FDTI8JCpAM5ePHppdgxuQGs= X-Google-Smtp-Source: ABdhPJytmhHerfBCYOTxWn4d3Oc6gRe6w81+bxxnxrA25+Cr+HYHWZBjxiuz6XvRP8ORvirVfDhGDg== X-Received: by 2002:a37:6e42:: with SMTP id j63mr17201660qkc.495.1589828848808; Mon, 18 May 2020 12:07:28 -0700 (PDT) Received: from rani.riverdale.lan ([2001:470:1f07:5f3::b55f]) by smtp.gmail.com with ESMTPSA id a1sm9862811qtj.65.2020.05.18.12.07.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 May 2020 12:07:28 -0700 (PDT) From: Arvind Sankar To: Ard Biesheuvel Cc: linux-efi@vger.kernel.org Subject: [PATCH 14/24] efi/printf: Refactor code to consolidate padding and output Date: Mon, 18 May 2020 15:07:06 -0400 Message-Id: <20200518190716.751506-15-nivedita@alum.mit.edu> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200518190716.751506-1-nivedita@alum.mit.edu> References: <20200518190716.751506-1-nivedita@alum.mit.edu> MIME-Version: 1.0 Sender: linux-efi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-efi@vger.kernel.org Consolidate the actual output of the formatted text into one place. Fix a couple of edge cases: 1. If 0 is printed with a precision of 0, the printf specification says that nothing should be output, with one exception (2b). 2. The specification for octal alternate format (%#o) adds the leading zero not as a prefix as the 0x for hexadecimal is, but by increasing the precision if necessary to add the zero. This means that a. %#.2o turns 8 into "010", but 1 into "01" rather than "001". b. %#.o prints 0 as "0" rather than "", unlike the situation for decimal, hexadecimal and regular octal format, which all output an empty string. Reduce the space allocated for printing a number to the maximum actually required (22 bytes for a 64-bit number in octal), instead of the 66 bytes previously allocated. Signed-off-by: Arvind Sankar --- drivers/firmware/efi/libstub/vsprintf.c | 273 +++++++++++++----------- 1 file changed, 148 insertions(+), 125 deletions(-) diff --git a/drivers/firmware/efi/libstub/vsprintf.c b/drivers/firmware/efi/libstub/vsprintf.c index 048276d9c376..3352ba394797 100644 --- a/drivers/firmware/efi/libstub/vsprintf.c +++ b/drivers/firmware/efi/libstub/vsprintf.c @@ -14,10 +14,12 @@ #include #include +#include #include #include -static int skip_atoi(const char **s) +static +int skip_atoi(const char **s) { int i = 0; @@ -32,16 +34,16 @@ static int skip_atoi(const char **s) * r/10 == (r * 0xccd) >> 15 is exact for all r < 16389. */ static -void put_dec_full4(char *buf, unsigned int r) +void put_dec_full4(char *end, unsigned int r) { int i; for (i = 0; i < 3; i++) { unsigned int q = (r * 0xccd) >> 15; - *buf++ = '0' + (r - q * 10); + *--end = '0' + (r - q * 10); r = q; } - *buf++ = '0' + r; + *--end = '0' + r; } /* put_dec is copied from lib/vsprintf.c with small modifications */ @@ -54,11 +56,11 @@ void put_dec_full4(char *buf, unsigned int r) * (second call in the put_dec code, assuming n is all-ones). */ static -unsigned int put_dec_helper4(char *buf, unsigned int x) +unsigned int put_dec_helper4(char *end, unsigned int x) { unsigned int q = (x * 0x346DC5D7ULL) >> 43; - put_dec_full4(buf, x - q * 10000); + put_dec_full4(end, x - q * 10000); return q; } @@ -68,10 +70,10 @@ unsigned int put_dec_helper4(char *buf, unsigned int x) * Performs no 64-bit division and hence should be fast on 32-bit machines. */ static -int put_dec(char *buf, unsigned long long n) +char *put_dec(char *end, unsigned long long n) { unsigned int d3, d2, d1, q, h; - char *p = buf; + char *p = end; d1 = ((unsigned int)n >> 16); /* implicit "& 0xffff" */ h = (n >> 32); @@ -82,28 +84,59 @@ int put_dec(char *buf, unsigned long long n) = 281_4749_7671_0656 d3 + 42_9496_7296 d2 + 6_5536 d1 + d0 */ q = 656 * d3 + 7296 * d2 + 5536 * d1 + ((unsigned int)n & 0xffff); q = put_dec_helper4(p, q); - p += 4; + p -= 4; q += 7671 * d3 + 9496 * d2 + 6 * d1; q = put_dec_helper4(p, q); - p += 4; + p -= 4; q += 4749 * d3 + 42 * d2; q = put_dec_helper4(p, q); - p += 4; + p -= 4; q += 281 * d3; q = put_dec_helper4(p, q); - p += 4; + p -= 4; put_dec_full4(p, q); - p += 4; + p -= 4; /* strip off the extra 0's we printed */ - while (p > buf && p[-1] == '0') - --p; + while (p < end && *p == '0') + ++p; - return p - buf; + return p; +} + +static +char *number(char *end, unsigned long long num, int base, char locase) +{ + /* + * locase = 0 or 0x20. ORing digits or letters with 'locase' + * produces same digits or (maybe lowercased) letters + */ + + /* we are called with base 8, 10 or 16, only, thus don't need "G..." */ + static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */ + + switch (base) { + case 10: + if (num != 0) + end = put_dec(end, num); + break; + case 8: + for (; num != 0; num >>= 3) + *--end = '0' + (num & 07); + break; + case 16: + for (; num != 0; num >>= 4) + *--end = digits[num & 0xf] | locase; + break; + default: + unreachable(); + }; + + return end; } #define ZEROPAD 1 /* pad with zero */ @@ -114,94 +147,6 @@ int put_dec(char *buf, unsigned long long n) #define SMALL 32 /* Must be 32 == 0x20 */ #define SPECIAL 64 /* 0x */ -static char *number(char *str, long long num, int base, int size, int precision, - int type) -{ - /* we are called with base 8, 10 or 16, only, thus don't need "G..." */ - static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */ - - char tmp[66]; - char c, sign, locase; - int i; - - /* locase = 0 or 0x20. ORing digits or letters with 'locase' - * produces same digits or (maybe lowercased) letters */ - locase = (type & SMALL); - if (type & LEFT) - type &= ~ZEROPAD; - c = (type & ZEROPAD) ? '0' : ' '; - sign = 0; - if (type & SIGN) { - if (num < 0) { - sign = '-'; - num = -num; - size--; - } else if (type & PLUS) { - sign = '+'; - size--; - } else if (type & SPACE) { - sign = ' '; - size--; - } - } - if (type & SPECIAL) { - if (base == 16) - size -= 2; - else if (base == 8) - size--; - } - i = 0; - if (num == 0) - tmp[i++] = '0'; - else switch (base) { - case 10: - i += put_dec(&tmp[i], num); - break; - case 8: - while (num != 0) { - tmp[i++] = '0' + (num & 07); - num = (unsigned long long)num >> 3; - } - break; - case 16: - while (num != 0) { - tmp[i++] = digits[num & 0xf] | locase; - num = (unsigned long long)num >> 4; - } - break; - - default: - unreachable(); - } - - if (i > precision) - precision = i; - size -= precision; - if (!(type & (ZEROPAD + LEFT))) - while (size-- > 0) - *str++ = ' '; - if (sign) - *str++ = sign; - if (type & SPECIAL) { - if (base == 8) - *str++ = '0'; - else if (base == 16) { - *str++ = '0'; - *str++ = ('X' | locase); - } - } - if (!(type & LEFT)) - while (size-- > 0) - *str++ = c; - while (i < precision--) - *str++ = '0'; - while (i-- > 0) - *str++ = tmp[i]; - while (size-- > 0) - *str++ = ' '; - return str; -} - static int get_flags(const char **fmt) { @@ -276,13 +221,33 @@ unsigned long long get_number(int sign, int qualifier, va_list *ap) } } +static +char get_sign(long long *num, int flags) +{ + if (!(flags & SIGN)) + return 0; + if (*num < 0) { + *num = -(*num); + return '-'; + } + if (flags & PLUS) + return '+'; + if (flags & SPACE) + return ' '; + return 0; +} + int vsprintf(char *buf, const char *fmt, va_list ap) { - int len; - unsigned long long num; - int i, base; + /* The maximum space required is to print a 64-bit number in octal */ + char tmp[(sizeof(unsigned long long) * 8 + 2) / 3]; + char *tmp_end = &tmp[ARRAY_SIZE(tmp)]; + long long num; + int base; char *str; const char *s; + int len; + char sign; int flags; /* flags to number() */ @@ -325,6 +290,9 @@ int vsprintf(char *buf, const char *fmt, va_list ap) flags |= LEFT; } + if (flags & LEFT) + flags &= ~ZEROPAD; + /* get the precision */ precision = -1; if (*fmt == '.') { @@ -345,32 +313,25 @@ int vsprintf(char *buf, const char *fmt, va_list ap) } } + sign = 0; + switch (*fmt) { case 'c': - if (!(flags & LEFT)) - while (--field_width > 0) - *str++ = ' '; - *str++ = (unsigned char)va_arg(args, int); - while (--field_width > 0) - *str++ = ' '; - continue; + flags &= LEFT; + tmp[0] = (unsigned char)va_arg(args, int); + s = tmp; + precision = len = 1; + goto output; case 's': + flags &= LEFT; if (precision < 0) precision = INT_MAX; s = va_arg(args, char *); if (!s) s = precision < 6 ? "" : "(null)"; - len = strnlen(s, precision); - - if (!(flags & LEFT)) - while (len < field_width--) - *str++ = ' '; - for (i = 0; i < len; ++i) - *str++ = *s++; - while (len < field_width--) - *str++ = ' '; - continue; + precision = len = strnlen(s, precision); + goto output; /* integer number formats - set up the flags and "break" */ case 'o': @@ -393,6 +354,7 @@ int vsprintf(char *buf, const char *fmt, va_list ap) flags |= SIGN; fallthrough; case 'u': + flags &= ~SPECIAL; base = 10; break; @@ -408,7 +370,68 @@ int vsprintf(char *buf, const char *fmt, va_list ap) num = (unsigned long)va_arg(args, void *); else num = get_number(flags & SIGN, qualifier, &args); - str = number(str, num, base, field_width, precision, flags); + + sign = get_sign(&num, flags); + if (sign) + --field_width; + + s = number(tmp_end, num, base, flags & SMALL); + len = tmp_end - s; + /* default precision is 1 */ + if (precision < 0) + precision = 1; + /* precision is minimum number of digits to print */ + if (precision < len) + precision = len; + if (flags & SPECIAL) { + /* + * For octal, a leading 0 is printed only if necessary, + * i.e. if it's not already there because of the + * precision. + */ + if (base == 8 && precision == len) + ++precision; + /* + * For hexadecimal, the leading 0x is skipped if the + * output is empty, i.e. both the number and the + * precision are 0. + */ + if (base == 16 && precision > 0) + field_width -= 2; + else + flags &= ~SPECIAL; + } + /* + * For zero padding, increase the precision to fill the field + * width. + */ + if ((flags & ZEROPAD) && field_width > precision) + precision = field_width; + +output: + /* Calculate the padding necessary */ + field_width -= precision; + /* Leading padding with ' ' */ + if (!(flags & LEFT)) + while (field_width-- > 0) + *str++ = ' '; + /* sign */ + if (sign) + *str++ = sign; + /* 0x/0X for hexadecimal */ + if (flags & SPECIAL) { + *str++ = '0'; + *str++ = 'X' | (flags & SMALL); + } + /* Zero padding and excess precision */ + while (precision-- > len) + *str++ = '0'; + /* Actual output */ + while (len-- > 0) + *str++ = *s++; + /* Trailing padding with ' ' */ + while (field_width-- > 0) + *str++ = ' '; } *str = '\0'; From patchwork Mon May 18 19:07:08 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arvind Sankar X-Patchwork-Id: 206323 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.8 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id CD287C433E2 for ; Mon, 18 May 2020 19:07:35 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id B692D20674 for ; Mon, 18 May 2020 19:07:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726513AbgERTHe (ORCPT ); Mon, 18 May 2020 15:07:34 -0400 Received: from mail-qt1-f195.google.com ([209.85.160.195]:34133 "EHLO mail-qt1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726553AbgERTHc (ORCPT ); Mon, 18 May 2020 15:07:32 -0400 Received: by mail-qt1-f195.google.com with SMTP id a23so3344057qto.1 for ; Mon, 18 May 2020 12:07:31 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=dm0AWhZq933OVtCzy7CiAJHP7WTrC8LBHUbou1f2eHs=; b=SAVRkyK6f8yEfcHdMnO9Oy9T17yEDZWkIMUTLaY/SiEz8ihdd9G5Jb8RCPPq4zMb6M WYNqEVYgnqgrA/xHpnO8WlpcEVfOozvHM0XcO4ep3qjaVfX8MX7xxbh/0s3hmbgs/OiD v7gg5Z2SfZar2QBdDhaUtLX3+pTVEJGU+oK6a3Ro8OfG6464rUQ08QIXWr1DDeCztXFf wyuBcQsKreSt/+K+q51AW7CkYoyHq92M0H8kg8o0yerFUIE1J6mc8hpPKro2xbX26db3 qCmUfGCYG9qcT69CWpT8JTVHsVnix8kLeMLM5DJE4BBO7aIZ0viz5M0PtY1XOvdwdINv qJ5g== X-Gm-Message-State: AOAM5309Bd+v9by6bWBScU3H6zfp7KDWRfMar8664KatfdR6w5sZRRgs 3oqlwGKu/gBeQlrntE0/Kg0= X-Google-Smtp-Source: ABdhPJw3Sjojo555zD1ikEXymd8al7QJDECVe7hMym+WS8+1A+YSBiTsEBkerVQ3FVVSx4Rl8+9txA== X-Received: by 2002:ac8:3173:: with SMTP id h48mr18209305qtb.179.1589828850114; Mon, 18 May 2020 12:07:30 -0700 (PDT) Received: from rani.riverdale.lan ([2001:470:1f07:5f3::b55f]) by smtp.gmail.com with ESMTPSA id a1sm9862811qtj.65.2020.05.18.12.07.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 May 2020 12:07:29 -0700 (PDT) From: Arvind Sankar To: Ard Biesheuvel Cc: linux-efi@vger.kernel.org Subject: [PATCH 16/24] efi/printf: Turn vsprintf into vsnprintf Date: Mon, 18 May 2020 15:07:08 -0400 Message-Id: <20200518190716.751506-17-nivedita@alum.mit.edu> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200518190716.751506-1-nivedita@alum.mit.edu> References: <20200518190716.751506-1-nivedita@alum.mit.edu> MIME-Version: 1.0 Sender: linux-efi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-efi@vger.kernel.org Implement vsnprintf instead of vsprintf to avoid the possibility of a buffer overflow. Signed-off-by: Arvind Sankar --- .../firmware/efi/libstub/efi-stub-helper.c | 6 ++- drivers/firmware/efi/libstub/vsprintf.c | 42 +++++++++++-------- 2 files changed, 30 insertions(+), 18 deletions(-) diff --git a/drivers/firmware/efi/libstub/efi-stub-helper.c b/drivers/firmware/efi/libstub/efi-stub-helper.c index 56b3b84fd3bd..5ecafc57619a 100644 --- a/drivers/firmware/efi/libstub/efi-stub-helper.c +++ b/drivers/firmware/efi/libstub/efi-stub-helper.c @@ -60,10 +60,14 @@ int efi_printk(const char *fmt, ...) int printed; va_start(args, fmt); - printed = vsprintf(printf_buf, fmt, args); + printed = vsnprintf(printf_buf, sizeof(printf_buf), fmt, args); va_end(args); efi_puts(printf_buf); + if (printed >= sizeof(printf_buf)) { + efi_puts("[Message truncated]\n"); + return -1; + } return printed; } diff --git a/drivers/firmware/efi/libstub/vsprintf.c b/drivers/firmware/efi/libstub/vsprintf.c index 7dcbc04498e7..36f2f4cf7434 100644 --- a/drivers/firmware/efi/libstub/vsprintf.c +++ b/drivers/firmware/efi/libstub/vsprintf.c @@ -17,6 +17,7 @@ #include #include #include +#include static int skip_atoi(const char **s) @@ -237,16 +238,22 @@ char get_sign(long long *num, int flags) return 0; } -int vsprintf(char *buf, const char *fmt, va_list ap) +#define PUTC(c) \ +do { \ + if (pos < size) \ + buf[pos] = (c); \ + ++pos; \ +} while (0); + +int vsnprintf(char *buf, size_t size, const char *fmt, va_list ap) { /* The maximum space required is to print a 64-bit number in octal */ char tmp[(sizeof(unsigned long long) * 8 + 2) / 3]; char *tmp_end = &tmp[ARRAY_SIZE(tmp)]; long long num; int base; - char *str; const char *s; - int len; + size_t len, pos; char sign; int flags; /* flags to number() */ @@ -274,9 +281,9 @@ int vsprintf(char *buf, const char *fmt, va_list ap) */ va_copy(args, ap); - for (str = buf; *fmt; ++fmt) { + for (pos = 0; *fmt; ++fmt) { if (*fmt != '%' || *++fmt == '%') { - *str++ = *fmt; + PUTC(*fmt); continue; } @@ -415,40 +422,41 @@ int vsprintf(char *buf, const char *fmt, va_list ap) /* Leading padding with ' ' */ if (!(flags & LEFT)) while (field_width-- > 0) - *str++ = ' '; + PUTC(' '); /* sign */ if (sign) - *str++ = sign; + PUTC(sign); /* 0x/0X for hexadecimal */ if (flags & SPECIAL) { - *str++ = '0'; - *str++ = 'X' | (flags & SMALL); + PUTC('0'); + PUTC( 'X' | (flags & SMALL)); } /* Zero padding and excess precision */ while (precision-- > len) - *str++ = '0'; + PUTC('0'); /* Actual output */ while (len-- > 0) - *str++ = *s++; + PUTC(*s++); /* Trailing padding with ' ' */ while (field_width-- > 0) - *str++ = ' '; + PUTC(' '); } fail: - *str = '\0'; - va_end(args); - return str - buf; + if (size) + buf[min(pos, size-1)] = '\0'; + + return pos; } -int sprintf(char *buf, const char *fmt, ...) +int snprintf(char *buf, size_t size, const char *fmt, ...) { va_list args; int i; va_start(args, fmt); - i = vsprintf(buf, fmt, args); + i = vsnprintf(buf, size, fmt, args); va_end(args); return i; } From patchwork Mon May 18 19:07:11 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arvind Sankar X-Patchwork-Id: 206322 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.8 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0B0A7C433E0 for ; Mon, 18 May 2020 19:07:37 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id DAEDE207FB for ; Mon, 18 May 2020 19:07:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726623AbgERTHf (ORCPT ); Mon, 18 May 2020 15:07:35 -0400 Received: from mail-qk1-f195.google.com ([209.85.222.195]:41468 "EHLO mail-qk1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726567AbgERTHe (ORCPT ); Mon, 18 May 2020 15:07:34 -0400 Received: by mail-qk1-f195.google.com with SMTP id n14so11439016qke.8 for ; Mon, 18 May 2020 12:07:33 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=fOw8zlM/FkyQy9W4Ki4Y1hrnF1IMps9I2hdJopKcb14=; b=nnTd36/S55hpLKny+M2mkogT0wNmjsl1D0Y7ofF6FE8Ninzo7RLoUQQQXDbc/98ti/ LFEX7Mk9q+2kAWHIiyYhTfK5wFxpW/qHHdso123dJv9wjpBDoQiMiKde//FbNPxf/bI+ v0OM6aOR8jdjfE3a7xajT15oGxG+F7EOLDRe4S5Fd56uc6m3caBl9GZWBrbK0xQVBHr5 tDxufqkwkeo6XlZXi/PYrprAwmSSFSURuP4txcQMa4PO2hBMqAdlaQ/NtK+4J6Izc+xj mZDjRoY5lkvsTacYKSZzix1ajBCgbahCqYRILIACQy7zrTprHNQGLVV+TQ5YbBvc1L8m iTgQ== X-Gm-Message-State: AOAM533AUXqnqRWIsSJPr+4gHb63oY38yHxsVzTwlMiL2hqFJcXa3cJn y8KAQ6BHgR21dPn6cBu/wUY= X-Google-Smtp-Source: ABdhPJyTzX/cQRXDTCHIvpvUGmUwj4CmS8vKjq7iLpixjrbnbcw9BNqaSWPqrtfl+XCg9Of0T4Ypzw== X-Received: by 2002:a37:38e:: with SMTP id 136mr17465742qkd.247.1589828852522; Mon, 18 May 2020 12:07:32 -0700 (PDT) Received: from rani.riverdale.lan ([2001:470:1f07:5f3::b55f]) by smtp.gmail.com with ESMTPSA id a1sm9862811qtj.65.2020.05.18.12.07.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 May 2020 12:07:31 -0700 (PDT) From: Arvind Sankar To: Ard Biesheuvel Cc: linux-efi@vger.kernel.org Subject: [PATCH 19/24] efi/gop: Add an option to list out the available GOP modes Date: Mon, 18 May 2020 15:07:11 -0400 Message-Id: <20200518190716.751506-20-nivedita@alum.mit.edu> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200518190716.751506-1-nivedita@alum.mit.edu> References: <20200518190716.751506-1-nivedita@alum.mit.edu> MIME-Version: 1.0 Sender: linux-efi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-efi@vger.kernel.org Add video=efifb:list option to list the modes that are available. Signed-off-by: Arvind Sankar --- Documentation/fb/efifb.rst | 5 + .../firmware/efi/libstub/efi-stub-helper.c | 35 +++++++ drivers/firmware/efi/libstub/efistub.h | 2 + drivers/firmware/efi/libstub/gop.c | 97 ++++++++++++++++++- include/linux/efi.h | 1 + 5 files changed, 139 insertions(+), 1 deletion(-) diff --git a/Documentation/fb/efifb.rst b/Documentation/fb/efifb.rst index 519550517fd4..6badff64756f 100644 --- a/Documentation/fb/efifb.rst +++ b/Documentation/fb/efifb.rst @@ -63,4 +63,9 @@ auto with the highest resolution, it will choose one with the highest color depth. +list + The EFI stub will list out all the display modes that are available. A + specific mode can then be chosen using one of the above options for the + next boot. + Edgar Hucek diff --git a/drivers/firmware/efi/libstub/efi-stub-helper.c b/drivers/firmware/efi/libstub/efi-stub-helper.c index c0278a8063b7..a36f3af6e130 100644 --- a/drivers/firmware/efi/libstub/efi-stub-helper.c +++ b/drivers/firmware/efi/libstub/efi-stub-helper.c @@ -463,3 +463,38 @@ efi_status_t efi_load_initrd(efi_loaded_image_t *image, return status; } + +efi_status_t efi_wait_for_key(unsigned long usec, efi_input_key_t *key) +{ + efi_event_t events[2], timer; + unsigned long index; + efi_simple_text_input_protocol_t *con_in; + efi_status_t status; + + con_in = efi_table_attr(efi_system_table, con_in); + if (!con_in) + return EFI_UNSUPPORTED; + efi_set_event_at(events, 0, efi_table_attr(con_in, wait_for_key)); + + status = efi_bs_call(create_event, EFI_EVT_TIMER, 0, NULL, NULL, &timer); + if (status != EFI_SUCCESS) + return status; + + status = efi_bs_call(set_timer, timer, EfiTimerRelative, + EFI_100NSEC_PER_USEC * usec); + if (status != EFI_SUCCESS) + return status; + efi_set_event_at(events, 1, timer); + + status = efi_bs_call(wait_for_event, 2, events, &index); + if (status == EFI_SUCCESS) { + if (index == 0) + status = efi_call_proto(con_in, read_keystroke, key); + else + status = EFI_TIMEOUT; + } + + efi_bs_call(close_event, timer); + + return status; +} diff --git a/drivers/firmware/efi/libstub/efistub.h b/drivers/firmware/efi/libstub/efistub.h index c7c03099367f..ad7e0406d0ba 100644 --- a/drivers/firmware/efi/libstub/efistub.h +++ b/drivers/firmware/efi/libstub/efistub.h @@ -323,6 +323,8 @@ union efi_simple_text_input_protocol { } mixed_mode; }; +efi_status_t efi_wait_for_key(unsigned long usec, efi_input_key_t *key); + union efi_simple_text_output_protocol { struct { void *reset; diff --git a/drivers/firmware/efi/libstub/gop.c b/drivers/firmware/efi/libstub/gop.c index 34c0cba2c8bf..ea5da307d542 100644 --- a/drivers/firmware/efi/libstub/gop.c +++ b/drivers/firmware/efi/libstub/gop.c @@ -19,7 +19,8 @@ enum efi_cmdline_option { EFI_CMDLINE_NONE, EFI_CMDLINE_MODE_NUM, EFI_CMDLINE_RES, - EFI_CMDLINE_AUTO + EFI_CMDLINE_AUTO, + EFI_CMDLINE_LIST }; static struct { @@ -100,6 +101,19 @@ static bool parse_auto(char *option, char **next) return true; } +static bool parse_list(char *option, char **next) +{ + if (!strstarts(option, "list")) + return false; + option += strlen("list"); + if (*option && *option++ != ',') + return false; + cmdline.option = EFI_CMDLINE_LIST; + + *next = option; + return true; +} + void efi_parse_option_graphics(char *option) { while (*option) { @@ -109,6 +123,8 @@ void efi_parse_option_graphics(char *option) continue; if (parse_auto(option, &option)) continue; + if (parse_list(option, &option)) + continue; while (*option && *option++ != ',') ; @@ -290,6 +306,82 @@ static u32 choose_mode_auto(efi_graphics_output_protocol_t *gop) return best_mode; } +static u32 choose_mode_list(efi_graphics_output_protocol_t *gop) +{ + efi_status_t status; + + efi_graphics_output_protocol_mode_t *mode; + efi_graphics_output_mode_info_t *info; + unsigned long info_size; + + u32 max_mode, cur_mode; + int pf; + efi_pixel_bitmask_t pi; + u32 m, w, h; + u8 d; + const char *dstr; + bool valid; + efi_input_key_t key; + + mode = efi_table_attr(gop, mode); + + cur_mode = efi_table_attr(mode, mode); + max_mode = efi_table_attr(mode, max_mode); + + efi_printk("Available graphics modes are 0-%u\n", max_mode-1); + efi_puts(" * = current mode\n" + " - = unusable mode\n"); + for (m = 0; m < max_mode; m++) { + status = efi_call_proto(gop, query_mode, m, + &info_size, &info); + if (status != EFI_SUCCESS) + continue; + + pf = info->pixel_format; + pi = info->pixel_information; + w = info->horizontal_resolution; + h = info->vertical_resolution; + + efi_bs_call(free_pool, info); + + valid = !(pf == PIXEL_BLT_ONLY || pf >= PIXEL_FORMAT_MAX); + d = 0; + switch (pf) { + case PIXEL_RGB_RESERVED_8BIT_PER_COLOR: + dstr = "rgb"; + break; + case PIXEL_BGR_RESERVED_8BIT_PER_COLOR: + dstr = "bgr"; + break; + case PIXEL_BIT_MASK: + dstr = ""; + d = pixel_bpp(pf, pi); + break; + case PIXEL_BLT_ONLY: + dstr = "blt"; + break; + default: + dstr = "xxx"; + break; + } + + efi_printk("Mode %3u %c%c: Resolution %ux%u-%s%.0hhu\n", + m, + m == cur_mode ? '*' : ' ', + !valid ? '-' : ' ', + w, h, dstr, d); + } + + efi_puts("\nPress any key to continue (or wait 10 seconds)\n"); + status = efi_wait_for_key(10 * EFI_USEC_PER_SEC, &key); + if (status != EFI_SUCCESS && status != EFI_TIMEOUT) { + efi_err("Unable to read key, continuing in 10 seconds\n"); + efi_bs_call(stall, 10 * EFI_USEC_PER_SEC); + } + + return cur_mode; +} + static void set_mode(efi_graphics_output_protocol_t *gop) { efi_graphics_output_protocol_mode_t *mode; @@ -305,6 +397,9 @@ static void set_mode(efi_graphics_output_protocol_t *gop) case EFI_CMDLINE_AUTO: new_mode = choose_mode_auto(gop); break; + case EFI_CMDLINE_LIST: + new_mode = choose_mode_list(gop); + break; default: return; } diff --git a/include/linux/efi.h b/include/linux/efi.h index 974648db0c68..609201bd4682 100644 --- a/include/linux/efi.h +++ b/include/linux/efi.h @@ -39,6 +39,7 @@ #define EFI_WRITE_PROTECTED ( 8 | (1UL << (BITS_PER_LONG-1))) #define EFI_OUT_OF_RESOURCES ( 9 | (1UL << (BITS_PER_LONG-1))) #define EFI_NOT_FOUND (14 | (1UL << (BITS_PER_LONG-1))) +#define EFI_TIMEOUT (18 | (1UL << (BITS_PER_LONG-1))) #define EFI_ABORTED (21 | (1UL << (BITS_PER_LONG-1))) #define EFI_SECURITY_VIOLATION (26 | (1UL << (BITS_PER_LONG-1))) From patchwork Mon May 18 19:07:14 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arvind Sankar X-Patchwork-Id: 206321 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.8 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C3378C433E4 for ; Mon, 18 May 2020 19:07:37 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A511820674 for ; Mon, 18 May 2020 19:07:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726628AbgERTHh (ORCPT ); Mon, 18 May 2020 15:07:37 -0400 Received: from mail-qk1-f195.google.com ([209.85.222.195]:46422 "EHLO mail-qk1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726615AbgERTHf (ORCPT ); Mon, 18 May 2020 15:07:35 -0400 Received: by mail-qk1-f195.google.com with SMTP id f83so11405313qke.13 for ; Mon, 18 May 2020 12:07:34 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=uHBX4hPresxM5fC6Xsnz+DDSTanyDon83nVA6k6j82E=; b=l1hfLGcgxDwqEwhsJoK3F3IF6324pKbWPs8UWjuqOHLlOy5zZa+wxuNMVcwv62D6MR EPILtbkska2815hEdHZVcS/ZJ0ZOWVXZW1fmBAMxe/RhOk4oEYuXSNXAgUElCLuGyGHW biZq1MaITmj/OaExi7DHSZ3D7dZZARHChSJcRjN+BjHf9VoflMCjXJz8em11AgKAZERf rGF3S9off01QVByoiqCb3qbz3NWbDVYvec3R4mxpt2o9BKjQXJdBzGD82qVMACMJwlcx oxrKOHEzmaD7tSHkrww7hnLlPv+wR/BRCVmFS+w3UnOCR5qtenIAwVAQ8NNd1G9BWGId HIXw== X-Gm-Message-State: AOAM532JKAJcBAJrpv7PZ1LvC/Ph51oXBuZRjNBRlLPK8rvqB/6p/VwC 0daqukr3agr6mWTffcM3RcGhgIPD450= X-Google-Smtp-Source: ABdhPJwWMkqJhKyjKT3PKAcfQOCk6p3G0nyugZifCchg4PFkgCrs1aiZYrnJyLzRnCNYEJ8yafqB3A== X-Received: by 2002:a37:66d5:: with SMTP id a204mr17002799qkc.188.1589828854209; Mon, 18 May 2020 12:07:34 -0700 (PDT) Received: from rani.riverdale.lan ([2001:470:1f07:5f3::b55f]) by smtp.gmail.com with ESMTPSA id a1sm9862811qtj.65.2020.05.18.12.07.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 May 2020 12:07:33 -0700 (PDT) From: Arvind Sankar To: Ard Biesheuvel Cc: linux-efi@vger.kernel.org Subject: [PATCH 22/24] efi/libstub: Use %ls for filename Date: Mon, 18 May 2020 15:07:14 -0400 Message-Id: <20200518190716.751506-23-nivedita@alum.mit.edu> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200518190716.751506-1-nivedita@alum.mit.edu> References: <20200518190716.751506-1-nivedita@alum.mit.edu> MIME-Version: 1.0 Sender: linux-efi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-efi@vger.kernel.org efi_printk can now handle the UTF-16 filename, so print it using efi_err instead of a separate efi_char16_puts call. Signed-off-by: Arvind Sankar --- drivers/firmware/efi/libstub/file.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/drivers/firmware/efi/libstub/file.c b/drivers/firmware/efi/libstub/file.c index 933536c5236a..2005e33b33d5 100644 --- a/drivers/firmware/efi/libstub/file.c +++ b/drivers/firmware/efi/libstub/file.c @@ -46,9 +46,7 @@ static efi_status_t efi_open_file(efi_file_protocol_t *volume, status = volume->open(volume, &fh, fi->filename, EFI_FILE_MODE_READ, 0); if (status != EFI_SUCCESS) { - efi_err("Failed to open file: "); - efi_char16_puts(fi->filename); - efi_puts("\n"); + efi_err("Failed to open file: %ls\n", fi->filename); return status; } From patchwork Mon May 18 19:07:15 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arvind Sankar X-Patchwork-Id: 206320 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-9.8 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 00C69C433E2 for ; Mon, 18 May 2020 19:07:38 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id DD9EE20674 for ; Mon, 18 May 2020 19:07:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726615AbgERTHh (ORCPT ); Mon, 18 May 2020 15:07:37 -0400 Received: from mail-qt1-f196.google.com ([209.85.160.196]:34141 "EHLO mail-qt1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726553AbgERTHg (ORCPT ); Mon, 18 May 2020 15:07:36 -0400 Received: by mail-qt1-f196.google.com with SMTP id a23so3344236qto.1 for ; Mon, 18 May 2020 12:07:35 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=n2+mp2oioqnXLDd7lyopk0p21Q0yaRtmEUZkcAcyKHs=; b=L7O3Da3jCxJT/NBkQLcoU5m9Zy2OMqYfoMRiZgNejmyo+2MkKFokHiG5VrYhnTWCJB xX+JbCU+pmsLMGLCYixlOzBLqwNjcCKC9r7Mdam/EMpetQBpmS9DHUIct4ORd9GJWeem wP/2aLBsiw48iHiXZjcwzuWRssdlpzBBgHmc6pZpg878VPf/DM0kzBAPciGRN6G7z9YJ do5QxYEyRBKpuQk+rrazNz1Ee/4lJnE/WEGh+o4nF0J1wPmGZp+hal7AUsslpGacKoge ipGOSFPn9wvQr4WODG4nJ+kor9YX3T1ZoKyjl3f+IhUYHVRh7hjtUDlENnn9sbLnUjJM qCyA== X-Gm-Message-State: AOAM531gF9/zEOC6r1uGOA0yWtXTjRcNvdzxGZeeD5OQMq9uwmwDgv+4 Dv2ZBFLuwrMm/OnrGhUenpBkhHL6wA4= X-Google-Smtp-Source: ABdhPJyR9CAINp36UApb7bKPnaxMcVuASzginpQY9h3gjw0E7rXTlKG9GA8cYsZN1gLi37Qwf6tu+A== X-Received: by 2002:ac8:6615:: with SMTP id c21mr10374210qtp.185.1589828854960; Mon, 18 May 2020 12:07:34 -0700 (PDT) Received: from rani.riverdale.lan ([2001:470:1f07:5f3::b55f]) by smtp.gmail.com with ESMTPSA id a1sm9862811qtj.65.2020.05.18.12.07.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 May 2020 12:07:34 -0700 (PDT) From: Arvind Sankar To: Ard Biesheuvel Cc: linux-efi@vger.kernel.org Subject: [PATCH 23/24] efi/libstub: Get the exact UTF-8 length Date: Mon, 18 May 2020 15:07:15 -0400 Message-Id: <20200518190716.751506-24-nivedita@alum.mit.edu> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200518190716.751506-1-nivedita@alum.mit.edu> References: <20200518190716.751506-1-nivedita@alum.mit.edu> MIME-Version: 1.0 Sender: linux-efi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-efi@vger.kernel.org efi_convert_cmdline currently overestimates the length of the equivalent UTF-8 encoding. snprintf can now be used to do the conversion to UTF-8, however, it does not have a way to specify the size of the UTF-16 string, only the size of the resulting UTF-8 string. So in order to use it, we need to precalculate the exact UTF-8 size. Signed-off-by: Arvind Sankar --- .../firmware/efi/libstub/efi-stub-helper.c | 44 ++++++++++++++----- 1 file changed, 32 insertions(+), 12 deletions(-) diff --git a/drivers/firmware/efi/libstub/efi-stub-helper.c b/drivers/firmware/efi/libstub/efi-stub-helper.c index 48242bc982a3..01476d8f8edf 100644 --- a/drivers/firmware/efi/libstub/efi-stub-helper.c +++ b/drivers/firmware/efi/libstub/efi-stub-helper.c @@ -205,15 +205,6 @@ efi_status_t efi_parse_options(char const *cmdline) return EFI_SUCCESS; } -/* - * Get the number of UTF-8 bytes corresponding to an UTF-16 character. - * This overestimates for surrogates, but that is okay. - */ -static int efi_utf8_bytes(u16 c) -{ - return 1 + (c >= 0x80) + (c >= 0x800); -} - /* * Convert an UTF-16 string, not necessarily null terminated, to UTF-8. */ @@ -274,10 +265,39 @@ char *efi_convert_cmdline(efi_loaded_image_t *image, if (options) { s2 = options; - while (*s2 && *s2 != '\n' - && options_chars < load_options_chars) { - options_bytes += efi_utf8_bytes(*s2++); + while (options_chars < load_options_chars) { + u16 c = *s2++; + + if (c == L'\0' || c == L'\n') + break; + /* + * Get the number of UTF-8 bytes corresponding to a + * UTF-16 character. + * The first part handles everything in the BMP. + */ + options_bytes += 1 + (c >= 0x80) + (c >= 0x800); options_chars++; + /* + * Add one more byte for valid surrogate pairs. Invalid + * surrogates will be replaced with 0xfffd and take up + * only 3 bytes. + */ + if ((c & 0xfc00) == 0xd800) { + /* + * If the very last word is a high surrogate, + * we must ignore it since we can't access the + * low surrogate. + */ + if (options_chars == load_options_chars) { + options_bytes -= 3; + options_chars--; + break; + } else if ((*s2 & 0xfc00) == 0xdc00) { + options_bytes++; + options_chars++; + s2++; + } + } } }