From: Ard Biesheuvel <ard.biesheuvel@linaro.org>
To: linux-efi@vger.kernel.org
Cc: mark.rutland@arm.com,
Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>,
Ard Biesheuvel <ard.biesheuvel@linaro.org>,
Geoff Levand <geoff@infradead.org>,
catalin.marinas@arm.com, Riku Voipio <riku.voipio@linaro.org>,
Sudeep Holla <sudeep.holla@arm.com>,
will.deacon@arm.com, leif.lindholm@linaro.org,
linux-acpi@vger.kernel.org, James Morse <james.morse@arm.com>,
Hanjun Guo <hanjun.guo@linaro.org>,
Mark Salter <msalter@redhat.com>,
linux-arm-kernel@lists.infradead.org,
Ian Campbell <ijc@debian.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 [thread overview]
Message-ID: <20180704154919.18564-2-ard.biesheuvel@linaro.org> (raw)
In-Reply-To: <20180704154919.18564-1-ard.biesheuvel@linaro.org>
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 <ard.biesheuvel@linaro.org>
---
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;
}
--
2.17.1
next prev parent reply other threads:[~2018-07-04 15:49 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-07-04 15:49 [RFC PATCH 0/2] efi: add contents of LinuxExtraArgs EFI var to command line Ard Biesheuvel
2018-07-04 15:49 ` Ard Biesheuvel [this message]
2018-07-04 15:49 ` [RFC PATCH 2/2] efi/libstub: taken contents of LinuxExtraArgs UEFI variable into account Ard Biesheuvel
2018-07-12 17:01 ` [RFC PATCH 0/2] efi: add contents of LinuxExtraArgs EFI var to command line Will Deacon
2018-07-12 17:39 ` Ard Biesheuvel
2018-07-12 19:24 ` Ian Campbell
2018-07-12 22:22 ` Geoff Levand
2018-07-13 6:15 ` Ard Biesheuvel
2018-07-13 9:56 ` Will Deacon
2018-07-13 15:59 ` Geoff Levand
2018-07-31 15:54 ` Geoff Levand
2018-08-01 10:07 ` James Morse
2018-08-02 15:47 ` Graeme Gregory
2018-08-06 21:23 ` Geoff Levand
2018-07-13 10:02 ` Ian Campbell
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20180704154919.18564-2-ard.biesheuvel@linaro.org \
--to=ard.biesheuvel@linaro.org \
--cc=catalin.marinas@arm.com \
--cc=geoff@infradead.org \
--cc=hanjun.guo@linaro.org \
--cc=ijc@debian.org \
--cc=james.morse@arm.com \
--cc=leif.lindholm@linaro.org \
--cc=linux-acpi@vger.kernel.org \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-efi@vger.kernel.org \
--cc=lorenzo.pieralisi@arm.com \
--cc=mark.rutland@arm.com \
--cc=msalter@redhat.com \
--cc=riku.voipio@linaro.org \
--cc=sudeep.holla@arm.com \
--cc=will.deacon@arm.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).