From patchwork Wed Feb 5 17:04:11 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Leif Lindholm X-Patchwork-Id: 24210 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-oa0-f72.google.com (mail-oa0-f72.google.com [209.85.219.72]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 5276E20675 for ; Wed, 5 Feb 2014 17:06:10 +0000 (UTC) Received: by mail-oa0-f72.google.com with SMTP id i4sf3545051oah.7 for ; Wed, 05 Feb 2014 09:06:09 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:delivered-to:from:to:cc:subject :date:message-id:in-reply-to:references:x-original-sender :x-original-authentication-results:precedence:mailing-list:list-id :list-post:list-help:list-archive:list-unsubscribe; bh=fWOAyj3nu3k3mKMVfZEpnlX1g2cH17ic1tHQZc0gcZs=; b=KCEhnJxtPE7gewWStBXdzI02gbUAwCXIR5WThYFFhyU1SdQL0ipeAl8feF3Jp12XQl B4qvNJ0maBs3sVU3Mf9Ft8MnGj40WNTP1a3FJ3YpLPawcwDfPsjiqiEtgrW+z5GRivn2 fkgh7zI8zt6SW3dgEKaZ2ByLepk9/ZSC2KX7lVd6T4gw0PwHHt3c8balB0rDQcZCfxHc NRb5jBGqFtkqBpkvTXdsA+ezBhcPpwiYYuZifQhWo3B+vxQgKvMuQ/rMXfsokyUKG7cV 4Tdx0Cx50KMd1wVfFNZz5iaekMIUO5FqS0X4SwxFnZq1LMB8QeZlz7i8Mk+/IgHvzDze Komg== X-Gm-Message-State: ALoCoQmwGqWKH1fgqhkFipGPsg3mmJsLkRAaZIpchfOlt02z7rNSPF4JqtlqCHZjLNNWIrG1q0hB X-Received: by 10.182.28.36 with SMTP id y4mr990734obg.46.1391619969407; Wed, 05 Feb 2014 09:06:09 -0800 (PST) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.140.91.110 with SMTP id y101ls211342qgd.0.gmail; Wed, 05 Feb 2014 09:06:09 -0800 (PST) X-Received: by 10.58.90.202 with SMTP id by10mr1713391veb.6.1391619969243; Wed, 05 Feb 2014 09:06:09 -0800 (PST) Received: from mail-vb0-f49.google.com (mail-vb0-f49.google.com [209.85.212.49]) by mx.google.com with ESMTPS id fi9si9862869vcb.114.2014.02.05.09.06.09 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Wed, 05 Feb 2014 09:06:09 -0800 (PST) Received-SPF: neutral (google.com: 209.85.212.49 is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) client-ip=209.85.212.49; Received: by mail-vb0-f49.google.com with SMTP id x14so496793vbb.8 for ; Wed, 05 Feb 2014 09:06:09 -0800 (PST) X-Received: by 10.58.211.8 with SMTP id my8mr70050vec.80.1391619969144; Wed, 05 Feb 2014 09:06:09 -0800 (PST) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patches@linaro.org Received: by 10.220.174.196 with SMTP id u4csp64689vcz; Wed, 5 Feb 2014 09:06:08 -0800 (PST) X-Received: by 10.194.187.101 with SMTP id fr5mr455126wjc.76.1391619938303; Wed, 05 Feb 2014 09:05:38 -0800 (PST) Received: from mail-we0-f176.google.com (mail-we0-f176.google.com [74.125.82.176]) by mx.google.com with ESMTPS id lm2si15473302wjb.40.2014.02.05.09.05.37 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Wed, 05 Feb 2014 09:05:38 -0800 (PST) Received-SPF: neutral (google.com: 74.125.82.176 is neither permitted nor denied by best guess record for domain of leif.lindholm@linaro.org) client-ip=74.125.82.176; Received: by mail-we0-f176.google.com with SMTP id q58so496171wes.21 for ; Wed, 05 Feb 2014 09:05:37 -0800 (PST) X-Received: by 10.180.101.166 with SMTP id fh6mr3398131wib.2.1391619937477; Wed, 05 Feb 2014 09:05:37 -0800 (PST) Received: from mohikan.mushroom.smurfnet.nu (cpc4-cmbg17-2-0-cust71.5-4.cable.virginm.net. [86.14.224.72]) by mx.google.com with ESMTPSA id p1sm47427211wie.1.2014.02.05.09.05.36 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 05 Feb 2014 09:05:36 -0800 (PST) From: Leif Lindholm To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-efi@vger.kernel.org Cc: patches@linaro.org, "H. Peter Anvin" , Roy Franz , Leif Lindholm Subject: [PATCH 20/22] Improve cmdline conversion Date: Wed, 5 Feb 2014 17:04:11 +0000 Message-Id: <1391619853-10601-21-git-send-email-leif.lindholm@linaro.org> X-Mailer: git-send-email 1.7.10.4 In-Reply-To: <1391619853-10601-1-git-send-email-leif.lindholm@linaro.org> References: <1391619853-10601-1-git-send-email-leif.lindholm@linaro.org> X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: leif.lindholm@linaro.org X-Original-Authentication-Results: mx.google.com; spf=neutral (google.com: 209.85.212.49 is neither permitted nor denied by best guess record for domain of patch+caf_=patchwork-forward=linaro.org@linaro.org) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Precedence: list Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org List-ID: X-Google-Group-Id: 836684582541 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , From: "H. Peter Anvin" Improve the conversion of the UTF-16 EFI command line to UTF-8 for passing to the kernel. Signed-off-by: Roy Franz Signed-off-by: H. Peter Anvin Signed-off-by: Leif Lindholm --- arch/x86/boot/compressed/eboot.c | 3 +- drivers/firmware/efi/arm-stub.c | 3 +- drivers/firmware/efi/efi-stub-helper.c | 89 ++++++++++++++++++++++++-------- 3 files changed, 70 insertions(+), 25 deletions(-) diff --git a/arch/x86/boot/compressed/eboot.c b/arch/x86/boot/compressed/eboot.c index a7677ba..feca05f 100644 --- a/arch/x86/boot/compressed/eboot.c +++ b/arch/x86/boot/compressed/eboot.c @@ -488,8 +488,7 @@ struct boot_params *make_boot_params(void *handle, efi_system_table_t *_table) hdr->type_of_loader = 0x21; /* Convert unicode cmdline to ascii */ - cmdline_ptr = efi_convert_cmdline_to_ascii(sys_table, image, - &options_size); + cmdline_ptr = efi_convert_cmdline(sys_table, image, &options_size); if (!cmdline_ptr) goto fail; hdr->cmd_line_ptr = (unsigned long)cmdline_ptr; diff --git a/drivers/firmware/efi/arm-stub.c b/drivers/firmware/efi/arm-stub.c index aefe963..b505fde 100644 --- a/drivers/firmware/efi/arm-stub.c +++ b/drivers/firmware/efi/arm-stub.c @@ -87,8 +87,7 @@ unsigned long efi_entry(void *handle, efi_system_table_t *sys_table, * protocol. We are going to copy the command line into the * device tree, so this can be allocated anywhere. */ - cmdline_ptr = efi_convert_cmdline_to_ascii(sys_table, image, - &cmdline_size); + cmdline_ptr = efi_convert_cmdline(sys_table, image, &cmdline_size); if (!cmdline_ptr) { pr_efi_err(sys_table, "getting command line via LOADED_IMAGE_PROTOCOL\n"); goto fail_free_image; diff --git a/drivers/firmware/efi/efi-stub-helper.c b/drivers/firmware/efi/efi-stub-helper.c index 8477a72..2ee69ea 100644 --- a/drivers/firmware/efi/efi-stub-helper.c +++ b/drivers/firmware/efi/efi-stub-helper.c @@ -625,52 +625,99 @@ static efi_status_t efi_relocate_kernel(efi_system_table_t *sys_table_arg, } /* - * Convert the unicode UEFI command line to ASCII to pass to kernel. + * 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. + */ +static u8 *efi_utf16_to_utf8(u8 *dst, const u16 *src, int n) +{ + unsigned int c; + + while (n--) { + c = *src++; + if (n && c >= 0xd800 && c <= 0xdbff && + *src >= 0xdc00 && *src <= 0xdfff) { + c = 0x10000 + ((c & 0x3ff) << 10) + (*src & 0x3ff); + src++; + n--; + } + if (c >= 0xd800 && c <= 0xdfff) + c = 0xfffd; /* Unmatched surrogate */ + if (c < 0x80) { + *dst++ = c; + continue; + } + if (c < 0x800) { + *dst++ = 0xc0 + (c >> 6); + goto t1; + } + if (c < 0x10000) { + *dst++ = 0xe0 + (c >> 12); + goto t2; + } + *dst++ = 0xf0 + (c >> 18); + *dst++ = 0x80 + ((c >> 12) & 0x3f); +t2: + *dst++ = 0x80 + ((c >> 6) & 0x3f); +t1: + *dst++ = 0x80 + (c & 0x3f); + } + + return dst; +} + +/* + * Do proper conversion from UTF-16 to UTF-8 * Size of memory allocated return in *cmd_line_len. * Returns NULL on error. */ -static char *efi_convert_cmdline_to_ascii(efi_system_table_t *sys_table_arg, - efi_loaded_image_t *image, - int *cmd_line_len) +static char *efi_convert_cmdline(efi_system_table_t *sys_table_arg, + efi_loaded_image_t *image, + int *cmd_line_len) { - u16 *s2; + const u16 *s2; u8 *s1 = NULL; unsigned long cmdline_addr = 0; - int load_options_size = image->load_options_size / 2; /* ASCII */ - void *options = image->load_options; - int options_size = 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 options_chars = 0; /* UTF-16 chars */ efi_status_t status; - int i; u16 zero = 0; if (options) { s2 = options; - while (*s2 && *s2 != '\n' && options_size < load_options_size) { - s2++; - options_size++; + while (options_chars < load_options_chars + && *s2 && *s2 != '\n') { + options_bytes += efi_utf8_bytes(*s2++); + options_chars++; } } - if (options_size == 0) { - /* No command line options, so return empty string*/ - options_size = 1; + if (!options_chars) { + /* No command line options, so return empty string */ options = &zero; } - options_size++; /* NULL termination */ + options_bytes++; /* NUL termination */ status = efi_low_alloc(sys_table_arg, options_bytes, 0, &cmdline_addr); if (status != EFI_SUCCESS) return NULL; s1 = (u8 *)cmdline_addr; - s2 = (u16 *)options; - - for (i = 0; i < options_size - 1; i++) - *s1++ = *s2++; + s2 = (const u16 *)options; + s1 = efi_utf16_to_utf8(s1, s2, options_chars); *s1 = '\0'; - *cmd_line_len = options_size; + *cmd_line_len = options_bytes; return (char *)cmdline_addr; }