From patchwork Wed Jul 4 15:49:18 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ard Biesheuvel X-Patchwork-Id: 10507287 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 45186603D7 for ; Wed, 4 Jul 2018 15:50:11 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2FD3F28884 for ; Wed, 4 Jul 2018 15:50:11 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 21840288DA; Wed, 4 Jul 2018 15:50:11 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI autolearn=unavailable version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id A367928884 for ; Wed, 4 Jul 2018 15:50:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:Cc:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:References: In-Reply-To:Message-Id:Date:Subject:To:From:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=zFRzd+ZcWNvBlgIhiwKRyQjpyH+Z/C44BIUxLtDZ0uU=; b=OM8s00prbyHKhCFcZTmkZNLozt EumGJIDP8zcccQ61WX4B5LnwAfoYng1adZDTQ7krMU4WYgntnJLy9Y51fMBaRzUFFuLNk/4OA9yCJ B+jsGg6EV26gY4fwDfSFAl8HFgnV6gMk/QVcrDv/jXXhqoWQTF6WBtT8Ty92F+xxyjrm3q1xwDQYB zQkNutuBSSIAQf9NHbLbTXPVal6Fj+HRgNfmrgPxxWp8la9fH/EaAoDYNk/3BDPSUAWvWnVfuOxKg 4Pz5HykyzEOmt6bH8zeNkErX+0tXbo/K6fwTUJQ2TB8R+FRNuAIq/tp19GiQz/aBbBPrCq+Yhw5Yl r0TT0OOQ==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1fak2b-0006p6-6L; Wed, 04 Jul 2018 15:50:01 +0000 Received: from mail-ed1-x541.google.com ([2a00:1450:4864:20::541]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1fak2C-0006GX-0a for linux-arm-kernel@lists.infradead.org; Wed, 04 Jul 2018 15:49:38 +0000 Received: by mail-ed1-x541.google.com with SMTP id g15-v6so4344536edr.12 for ; Wed, 04 Jul 2018 08:49:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=nh3Vqe3ZbN0PBJNN8uz18IspA2Go6Mdwe33u4rdyQo4=; b=gtZ0OV3Bg3X8hXrvxiqXjeyI1KC6wCL0uCwLajP90sEt+yu5EIAqC2moRqQvjJGOXg VSj94hcE/6VSFP2HP2bYM4wD6SM7wOaHgMYe+DR8F1MCGHKnJodRas2o2c2z0+chQdv4 KskP3ncD096JlyANaE5/nQr0Trddno8DuGqV0= 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; bh=nh3Vqe3ZbN0PBJNN8uz18IspA2Go6Mdwe33u4rdyQo4=; b=DeIaYHZS05K4ynk8iQNIU4hmbFCFs5zBUJLJTnnpdnHvTMWMYcrD/pQ5pI+nUsVyqQ wwY7jdO/Wh18A5iLrmapFlBsqkfwd55CzS3N6hk/1ng4YBLZ7vy8wO5ch983ey3ywSM/ QpY4vTHkao7eN9oAJi4Xo8a87njLzOHQGOFBQBZROuJ5Ia2MsaaGcQcok/FmWkloVZKo zv0gP+diGG2emvB1JlLqzZV6c6tN2e6q7cjE4kzlqfDgzX7rGeUGvfy0isbhqjA+RBfO 7fZYECwLQLHGFwG7Mpyo5R/CrEQEZBkq5eeF274EqNbqCbqhu56N0IdO7nSlmV+Izkuy qo2Q== X-Gm-Message-State: APt69E0NW1jrtNCpR0dy0bgL2rsrWI4G2ZnYmzdZrHX5AOi9xBth2e4j 2KZNCaqGC/CL/Xa5lkVTIP+aGA== X-Google-Smtp-Source: AAOMgpdgAljrqJpDDdS8UshJPRMjBI9K8u1cjim0GyCxTp0udPAxS6diO/fllkhk6ORX5B2pmUoJgQ== X-Received: by 2002:aa7:d9d7:: with SMTP id v23-v6mr3301784eds.300.1530719364312; Wed, 04 Jul 2018 08:49:24 -0700 (PDT) Received: from ards-mac-mini.arnhem.chello.nl (dhcp-077-251-017-237.chello.nl. [77.251.17.237]) by smtp.gmail.com with ESMTPSA id l61-v6sm1966954edl.96.2018.07.04.08.49.22 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 04 Jul 2018 08:49:23 -0700 (PDT) From: Ard Biesheuvel To: linux-efi@vger.kernel.org Subject: [RFC PATCH 1/2] efi/libstub: refactor load option command line processing for reuse Date: Wed, 4 Jul 2018 17:49:18 +0200 Message-Id: <20180704154919.18564-2-ard.biesheuvel@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180704154919.18564-1-ard.biesheuvel@linaro.org> References: <20180704154919.18564-1-ard.biesheuvel@linaro.org> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20180704_084936_197790_1C04BD38 X-CRM114-Status: GOOD ( 16.53 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: mark.rutland@arm.com, Lorenzo Pieralisi , Ard Biesheuvel , Geoff Levand , catalin.marinas@arm.com, Riku Voipio , Sudeep Holla , will.deacon@arm.com, leif.lindholm@linaro.org, linux-acpi@vger.kernel.org, James Morse , Hanjun Guo , Mark Salter , linux-arm-kernel@lists.infradead.org, Ian Campbell MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP As a preparatory step towards adding support for extra kernel command line arguments passed via a UEFI variable, do a slight refactor of the existing code so we can extend it more cleanly in a subsequent patch. No change is functionality is intended. Signed-off-by: Ard Biesheuvel --- drivers/firmware/efi/libstub/efi-stub-helper.c | 43 ++++++++++++-------- 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/drivers/firmware/efi/libstub/efi-stub-helper.c b/drivers/firmware/efi/libstub/efi-stub-helper.c index 50a9cab5a834..97a2423782af 100644 --- a/drivers/firmware/efi/libstub/efi-stub-helper.c +++ b/drivers/firmware/efi/libstub/efi-stub-helper.c @@ -726,6 +726,22 @@ static int efi_utf8_bytes(u16 c) return 1 + (c >= 0x80) + (c >= 0x800); } +/* + * Get an upper bound for the number of UTF-8 bytes corresponding + * to a \n or \0 terminated UTF-16 string. + */ +static int count_utf8_bytes(const u16 *s, int max_chars, int *num_chars) +{ + int ret = 0; + + while (*s != '\0' && *s != '\n' && --max_chars >= 0) { + ret += efi_utf8_bytes(*s++); + ++*num_chars; + } + + return ret; +} + /* * Convert an UTF-16 string, not necessarily null terminated, to UTF-8. */ @@ -779,44 +795,35 @@ char *efi_convert_cmdline(efi_system_table_t *sys_table_arg, efi_loaded_image_t *image, int *cmd_line_len) { - const u16 *s2; - u8 *s1 = NULL; + u8 *s1; unsigned long cmdline_addr = 0; int load_options_chars = image->load_options_size / 2; /* UTF-16 */ const u16 *options = image->load_options; - int options_bytes = 0; /* UTF-8 bytes */ + int cmd_line_bytes = 0; /* UTF-8 bytes */ int options_chars = 0; /* UTF-16 chars */ efi_status_t status; u16 zero = 0; - if (options) { - s2 = options; - while (*s2 && *s2 != '\n' - && options_chars < load_options_chars) { - options_bytes += efi_utf8_bytes(*s2++); - options_chars++; - } - } + if (options) + cmd_line_bytes = count_utf8_bytes(options, load_options_chars, + &options_chars); if (!options_chars) { /* No command line options, so return empty string*/ options = &zero; } - options_bytes++; /* NUL termination */ + cmd_line_bytes++; /* NUL termination */ - status = efi_high_alloc(sys_table_arg, options_bytes, 0, + status = efi_high_alloc(sys_table_arg, cmd_line_bytes, 0, &cmdline_addr, MAX_CMDLINE_ADDRESS); if (status != EFI_SUCCESS) return NULL; - s1 = (u8 *)cmdline_addr; - s2 = (const u16 *)options; - - s1 = efi_utf16_to_utf8(s1, s2, options_chars); + s1 = efi_utf16_to_utf8((u8 *)cmdline_addr, options, options_chars); *s1 = '\0'; - *cmd_line_len = options_bytes; + *cmd_line_len = cmd_line_bytes; return (char *)cmdline_addr; }