Received: by 2002:a05:6602:18e:0:0:0:0 with SMTP id m14csp1661720ioo; Sun, 22 May 2022 23:27:54 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzrhgpHajx3QAGT1CjtliiJGlXOdDU3yGbtf8qgq2xtBZQC5V8xNfDM5HUKRs+R70V+ZX61 X-Received: by 2002:a63:8ac8:0:b0:3f7:ecb7:9261 with SMTP id y191-20020a638ac8000000b003f7ecb79261mr12960181pgd.38.1653287274520; Sun, 22 May 2022 23:27:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1653287274; cv=none; d=google.com; s=arc-20160816; b=PUMzgeXiWn4dXEVg6mLGovQFg2FF8vks1t1N3UYmmMoJ5xeGAu0y0aoUeW1pOZ9ozn aHmHUPE1WANuvjY2mUNVg8pJvsi5U2Q1i/Rvp8h9v82l92klcWPdIn1D00QxQ8h1T+MQ kOkg/j6W7rpTt00y4SJ3JOlFggcUi2vZ3UAOJUzrrZB0q9e4/FDnA7wf79cj3Snl2ol2 jfa6nKgkgbprSiNVudWBS6Bnq+5voJK82JwcrqlMRafl14e0SqeM5MYdyVKCRj+Wpvuj gVGRoNMboXjMN7y4PU4s0798cNpf9dIL79ioggvN4uYuoj1QALBzlGLEgDFVz2o7M0s9 Xf7g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=i1SabHWBF9yjaawRYqmI4JtnrLJvNYnK3oYqAVGf7Ao=; b=GGtt8HQ3N8TvtCqcjdCLZeQ//RDDw+yV15eszk663O5HqC0kfW90OPEivecEGuZVea +OajbBzodqlBS3QoXcWiPmFxsLL55tXRcpoW2qzzfPaP2L2vBrbUf/YV1MCIjjRYa3je QgfJAyMl92Cvz6d7BzwDkGVK9M73YSaSTg4lRQyRVpyLm97f+/Hqt4LkNZhof1wFAvdf P3QGlsuotgjlQ/jwvDoxSqekCpBSe58HCaDb41HkpbcUOp+gB+D0mG16hPsb6iZSNBH6 4koOtIr9er4L3T0dsZ8PRz4iOOKSKxIt7KebYzsglePKpVHRkWfSk6aJyHu18oRbs7D6 WxvQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@igalia.com header.s=20170329 header.b=gesr4pHQ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id z13-20020a170902cccd00b00161f0c3df9esi8464793ple.593.2022.05.22.23.27.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 22 May 2022 23:27:54 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=fail header.i=@igalia.com header.s=20170329 header.b=gesr4pHQ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id A9D5DE0C6; Sun, 22 May 2022 23:06:23 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1353319AbiETTvb (ORCPT + 99 others); Fri, 20 May 2022 15:51:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45816 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1353318AbiETTva (ORCPT ); Fri, 20 May 2022 15:51:30 -0400 Received: from fanzine2.igalia.com (fanzine.igalia.com [178.60.130.6]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B84BF5A2E3; Fri, 20 May 2022 12:51:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=i1SabHWBF9yjaawRYqmI4JtnrLJvNYnK3oYqAVGf7Ao=; b=gesr4pHQ0zxgkl4V4UHZOm/9At KVQwK8sl9PhTHOAgnypdTxNXRdkr4bWPbUK6Li6GY/6LRRY/OTndzqrYB+wh5lLbyiv5wCVLQIm5P fYyOUVReou6EYoVA/0t/qOjs6hxpyWFPQzJ1htTgcxM9t8DHtec9N7KXN03AGiaE51w09F/xGYSJl vdlSJkoWe+NXaNOgRtbx77ASGNpf6PugBB5YcKh7/kYy7lCooFnkz/NFBEHxEehRXtMbjbc+CI0Ba 142I0nIDZEhkGopOp5PAE/n9Yb35rOttg5fc7SE2WE0avYKZjdTqEW72WUd51ielFusXPxF6mesHd PcrvrEcQ==; Received: from 200-161-159-120.dsl.telesp.net.br ([200.161.159.120] helo=localhost) by fanzine2.igalia.com with esmtpsa (Cipher TLS1.3:ECDHE_SECP256R1__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim) id 1ns8ei-00D3mx-Qz; Fri, 20 May 2022 21:51:25 +0200 From: "Guilherme G. Piccoli" To: ardb@kernel.org, linux-efi@vger.kernel.org Cc: linux-kernel@vger.kernel.org, kernel-dev@igalia.com, kernel@gpiccoli.net, anton@enomsg.org, ccross@android.com, keescook@chromium.org, matt@codeblueprint.co.uk, matthew.garrett@nebula.com, tony.luck@intel.com, "Guilherme G. Piccoli" Subject: [PATCH 1/2] efi: Add a generic helper to convert strings to unicode Date: Fri, 20 May 2022 16:50:27 -0300 Message-Id: <20220520195028.1347426-2-gpiccoli@igalia.com> X-Mailer: git-send-email 2.36.0 In-Reply-To: <20220520195028.1347426-1-gpiccoli@igalia.com> References: <20220520195028.1347426-1-gpiccoli@igalia.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.7 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RDNS_NONE, SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Currently both efi-pstore and efibc rely in simple for-loops to convert from regular char pointers to u16/unicode strings; efibc has even a nice helper to perform such work. So, let's export this helper to common EFI code to prevent code duplication (like in efi-pstore); this helper will also be used in a subsequent patch (adding a new module). Notice that efi-pstore didn't write the end NULL char in the unicode string before this patch, but this should not change anything. No functional change is expected here. Signed-off-by: Guilherme G. Piccoli --- drivers/firmware/efi/efi-pstore.c | 5 ++--- drivers/firmware/efi/efibc.c | 16 ++++------------ include/linux/efi.h | 15 +++++++++++++++ 3 files changed, 21 insertions(+), 15 deletions(-) diff --git a/drivers/firmware/efi/efi-pstore.c b/drivers/firmware/efi/efi-pstore.c index 7e771c56c13c..299116ecfb4e 100644 --- a/drivers/firmware/efi/efi-pstore.c +++ b/drivers/firmware/efi/efi-pstore.c @@ -249,7 +249,7 @@ static int efi_pstore_write(struct pstore_record *record) char name[DUMP_NAME_LEN]; efi_char16_t efi_name[DUMP_NAME_LEN]; efi_guid_t vendor = LINUX_EFI_CRASH_GUID; - int i, ret = 0; + int ret = 0; record->id = generic_id(record->time.tv_sec, record->part, record->count); @@ -262,8 +262,7 @@ static int efi_pstore_write(struct pstore_record *record) (long long)record->time.tv_sec, record->compressed ? 'C' : 'D'); - for (i = 0; i < DUMP_NAME_LEN; i++) - efi_name[i] = name[i]; + efi_str8_to_str16(efi_name, name, DUMP_NAME_LEN - 1); ret = efivar_entry_set_safe(efi_name, vendor, PSTORE_EFI_ATTRIBUTES, false, record->size, record->psi->buf); diff --git a/drivers/firmware/efi/efibc.c b/drivers/firmware/efi/efibc.c index 15a47539dc56..63fe2bf753cb 100644 --- a/drivers/firmware/efi/efibc.c +++ b/drivers/firmware/efi/efibc.c @@ -11,16 +11,6 @@ #include #include -static void efibc_str_to_str16(const char *str, efi_char16_t *str16) -{ - size_t i; - - for (i = 0; i < strlen(str); i++) - str16[i] = str[i]; - - str16[i] = '\0'; -} - static int efibc_set_variable(const char *name, const char *value) { int ret; @@ -39,8 +29,10 @@ static int efibc_set_variable(const char *name, const char *value) return -ENOMEM; } - efibc_str_to_str16(name, entry->var.VariableName); - efibc_str_to_str16(value, (efi_char16_t *)entry->var.Data); + efi_str8_to_str16(entry->var.VariableName, name, strlen(name)); + efi_str8_to_str16((efi_char16_t *)entry->var.Data, value, + strlen(value)); + memcpy(&entry->var.VendorGuid, &guid, sizeof(guid)); ret = efivar_entry_set_safe(entry->var.VariableName, diff --git a/include/linux/efi.h b/include/linux/efi.h index ccd4d3f91c98..066ebc5bcb2a 100644 --- a/include/linux/efi.h +++ b/include/linux/efi.h @@ -1030,6 +1030,21 @@ efivar_unregister(struct efivar_entry *var) kobject_put(&var->kobj); } +/* + * Helper that converts regular char buffer to u16 unicode-like strings; + * notice that the unicode buffer requires to be at least len+1 in size. + */ +static inline void +efi_str8_to_str16(efi_char16_t *str16, const char *str8, size_t len) +{ + size_t i; + + for (i = 0; i < len; i++) + str16[i] = str8[i]; + + str16[i] = '\0'; +} + int efivars_register(struct efivars *efivars, const struct efivar_operations *ops, struct kobject *kobject); -- 2.36.0