From patchwork Thu Apr 4 07:48:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ilias Apalodimas X-Patchwork-Id: 785816 Delivered-To: patch@linaro.org Received: by 2002:adf:f001:0:b0:33e:7753:30bd with SMTP id j1csp702465wro; Thu, 4 Apr 2024 00:48:40 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCXE+MTh1LTJpGa3CvqhxV+xteeZrEmNCUaDMtt0rZVu3oJHDZPYFJieCBDplnfLmofBti5SO8FDaXhgO6/3hlGm X-Google-Smtp-Source: AGHT+IGJbabsYQlu+rB00JvtybjxIcdJ0LgfJkiWhi4gqoTswoVcW0AU+tdApPpYPEs52SwqByFg X-Received: by 2002:a17:907:b9cc:b0:a47:34b2:ca4b with SMTP id xa12-20020a170907b9cc00b00a4734b2ca4bmr1328277ejc.50.1712216920358; Thu, 04 Apr 2024 00:48:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1712216920; cv=none; d=google.com; s=arc-20160816; b=u6wqKpgZ2jASbYkJZ8uk+QWmZ4S590GXsIxCzoBbvI9t5ds40Lxo13uceD6N5S4E9P rBNXf95Y4UoCtsl9c1UWL5aR17dmaYA4LVL/YilLYcZqcBiwgMDQIrKJTVeTk6qgnf// ES50dbfxkjapVJo/wXlEgvMEu6G7iFdrFTlT5YHwKC0NGCWNvx6MeP4kYDtiGwcYlQWq i0/COy1L8wk7mvziWRUnb4Zu7zaePwADM4XxwgJcgvw+RoAh7tcC57TWL4F8nj8yhTr6 NlyD7LcR5eLpRqk/8oPIY2ivCPt9eThrMan1O2doq0QjvtWDHd1G3pLCoTgNRSXBApje PixA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:cc:to:from:dkim-signature; bh=5Werfi7/+IS32ruyIg6vPW/180DaksboXEb/vUAP/jk=; fh=FF9i86CQX+N3rkF7xd+R9kMwElxlBO/Oml8u5xxRtfI=; b=iGWxHh/Z21irNDJQCg4sjTa9xr89ueSkVlpEPByohy9+SGkvbKcHUpItIi+++ST2ou snt/5X89CyzuUsRWHJxIOu/q+ernLXrNB7FrZJS9b46TStuLNYrJtpFL3aLGejF/ss5U qDzxv1E5a3j+GXFdVHu8eNV+8b7+5hwHZTfwrliVc+cYnxUosRWZvrtQrzP/YaDsJXDi pSFk0oXkgh1e1oQJqJTvOOA83pADJds11EzO3/Y4GWogmedvrSrnURHrp4FC60Ukkj3a 12UpqvtHeu3j1nQH/WA0ryOQM7vVM8OZGe2KXou1Ksv4A3hnAabMDqccEWYf8Z8QDmQc xPSg==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=COh1ZKL5; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from phobos.denx.de (phobos.denx.de. [2a01:238:438b:c500:173d:9f52:ddab:ee01]) by mx.google.com with ESMTPS id a6-20020a1709065f8600b00a4e99622e04si1829729eju.353.2024.04.04.00.48.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 04 Apr 2024 00:48:40 -0700 (PDT) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=COh1ZKL5; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 22973881E6; Thu, 4 Apr 2024 09:48:39 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b="COh1ZKL5"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 5A9168827F; Thu, 4 Apr 2024 09:48:38 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-ej1-x630.google.com (mail-ej1-x630.google.com [IPv6:2a00:1450:4864:20::630]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 48691880BE for ; Thu, 4 Apr 2024 09:48:36 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=ilias.apalodimas@linaro.org Received: by mail-ej1-x630.google.com with SMTP id a640c23a62f3a-a4a3a5e47baso88071066b.2 for ; Thu, 04 Apr 2024 00:48:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1712216916; x=1712821716; darn=lists.denx.de; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=5Werfi7/+IS32ruyIg6vPW/180DaksboXEb/vUAP/jk=; b=COh1ZKL5VQVYWCVK2SwpqKBt2NgKe17DCMFxnAiEAPZoqpagYPvAyYkjOMN63eOuwD 6kUvvCUS1nM9HOjlrrwoEAEQpsFH6Td8bYMwYgDZfkM+Vp113/Loq6rcpXy9VCxf4f4U DWfp2k0ihzVTNd8JQ9R7LjW5GFWW8CGqdOsve0nC0+qPGvyP/Dm56hXAbBCLjn2R32X2 uw0879s2BEr4XiyxNYkON0+oj8JrRrEA4YhmSsSVSyBxbaKi/9AEqlV2SPDohaMh/0HA FJonAG4R32+FgyRpf2ezP0L1Iam6cLv+0IebtHx/YrTtljXooywzl8yqt1h7s5Ug3LJJ aFJw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712216916; x=1712821716; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=5Werfi7/+IS32ruyIg6vPW/180DaksboXEb/vUAP/jk=; b=Go0jFBQdmPdVy8Nz/jrllBId9gZu/7tSeaR6vNREbyfnG4pMzApl9p+KvuG6sODUXR W5tcB3t/sL8M2y1QVHm92WKTW0iM9A6Vm9OrDWETO8qo5wCcoXg2O30Q78JUwl8cdh44 VD3uMF9Jk79OSUDadKzWIl+lXkdg7+McPIMKgR6LmLdBucaBwhX4a1wto0KfjhI/np9x rekO6lWurRM2DAb/wyWLbJH5JuemzXxzFugvH5E0tmnZP8oBk966JnHhhI9DTV7kV3zP AJrxDZquP4AOgdEHNsjnpfCRzfPpc32qUarZGI+duT6t+bK+as+VhmJBgm5aUTvSJU7R h9aw== X-Forwarded-Encrypted: i=1; AJvYcCUE67NM+IldKldB5pOjI6CoggbjBGZOJvk21yJL2762bNJjksNz8TNrwF4YfJqiduMHqKRf45u6AWIfZtQxiLtOn65uNA== X-Gm-Message-State: AOJu0Yyn8l1HyexwC44BMmOVG9r3hK0n6R4HInZjzjmLMb0cUMjsy+j9 7D60MqUfyrCIQy79FVdKbkBci4gmw7axHvmSOAejAMHUZDq5nv4PX24Tte2zO3c= X-Received: by 2002:a17:906:1b4a:b0:a51:8d19:3b07 with SMTP id p10-20020a1709061b4a00b00a518d193b07mr403903ejg.4.1712216915724; Thu, 04 Apr 2024 00:48:35 -0700 (PDT) Received: from hades.. (ppp089210071137.access.hol.gr. [89.210.71.137]) by smtp.gmail.com with ESMTPSA id z19-20020a170906669300b00a4e299dea48sm8144093ejo.199.2024.04.04.00.48.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 04 Apr 2024 00:48:35 -0700 (PDT) From: Ilias Apalodimas To: xypron.glpk@gmx.de Cc: Ilias Apalodimas , Ilias Apalodimas , Tom Rini , Simon Glass , Weizhao Ouyang , u-boot@lists.denx.de Subject: [PATCH] efi_loader: move efi_var_collect to common functions Date: Thu, 4 Apr 2024 10:48:18 +0300 Message-Id: <20240404074819.52156-1-ilias.apalodimas@linaro.org> X-Mailer: git-send-email 2.37.2 MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.8 at phobos.denx.de X-Virus-Status: Clean From: Ilias Apalodimas efi_var_collect() was initially placed in efi_var_file.c, since back then we only supported efi variables stored in a file. Since then we support variables stored in an RPMB as well and use that function to collect variables that should be present at runtime. So let's move it around in efi_var_common.c which makes more sense Suggested-by: Heinrich Schuchardt Signed-off-by: Ilias Apalodimas --- lib/efi_loader/efi_var_common.c | 76 +++++++++++++++++++++++++++++++++ lib/efi_loader/efi_var_file.c | 64 --------------------------- 2 files changed, 76 insertions(+), 64 deletions(-) diff --git a/lib/efi_loader/efi_var_common.c b/lib/efi_loader/efi_var_common.c index 16b2c3d48828..07b9603d49f3 100644 --- a/lib/efi_loader/efi_var_common.c +++ b/lib/efi_loader/efi_var_common.c @@ -9,6 +9,7 @@ #include #include #include +#include enum efi_secure_mode { EFI_MODE_SETUP, @@ -416,3 +417,78 @@ void *efi_get_var(const u16 *name, const efi_guid_t *vendor, efi_uintn_t *size) return buf; } + +/** + * efi_var_collect() - Copy EFI variables mstching attributes mask + * + * @bufp: buffer containing variable collection + * @lenp: buffer length + * @attr_mask: mask of matched attributes + * + * Return: Status code + */ +efi_status_t __maybe_unused efi_var_collect(struct efi_var_file **bufp, loff_t *lenp, + u32 check_attr_mask) +{ + size_t len = EFI_VAR_BUF_SIZE; + struct efi_var_file *buf; + struct efi_var_entry *var, *old_var; + size_t old_var_name_length = 2; + + *bufp = NULL; /* Avoid double free() */ + buf = calloc(1, len); + if (!buf) + return EFI_OUT_OF_RESOURCES; + var = buf->var; + old_var = var; + for (;;) { + efi_uintn_t data_length, var_name_length; + u8 *data; + efi_status_t ret; + + if ((uintptr_t)buf + len <= + (uintptr_t)var->name + old_var_name_length) + return EFI_BUFFER_TOO_SMALL; + + var_name_length = (uintptr_t)buf + len - (uintptr_t)var->name; + memcpy(var->name, old_var->name, old_var_name_length); + guidcpy(&var->guid, &old_var->guid); + ret = efi_get_next_variable_name_int( + &var_name_length, var->name, &var->guid); + if (ret == EFI_NOT_FOUND) + break; + if (ret != EFI_SUCCESS) { + free(buf); + return ret; + } + old_var_name_length = var_name_length; + old_var = var; + + data = (u8 *)var->name + old_var_name_length; + data_length = (uintptr_t)buf + len - (uintptr_t)data; + ret = efi_get_variable_int(var->name, &var->guid, + &var->attr, &data_length, data, + &var->time); + if (ret != EFI_SUCCESS) { + free(buf); + return ret; + } + if ((var->attr & check_attr_mask) == check_attr_mask) { + var->length = data_length; + var = (struct efi_var_entry *)ALIGN((uintptr_t)data + data_length, 8); + } + } + + buf->reserved = 0; + buf->magic = EFI_VAR_FILE_MAGIC; + len = (uintptr_t)var - (uintptr_t)buf; + buf->crc32 = crc32(0, (u8 *)buf->var, + len - sizeof(struct efi_var_file)); + buf->length = len; + *bufp = buf; + *lenp = len; + + return EFI_SUCCESS; +} + + diff --git a/lib/efi_loader/efi_var_file.c b/lib/efi_loader/efi_var_file.c index 532b6b40eefe..413e1794e88c 100644 --- a/lib/efi_loader/efi_var_file.c +++ b/lib/efi_loader/efi_var_file.c @@ -52,70 +52,6 @@ static efi_status_t __maybe_unused efi_set_blk_dev_to_system_partition(void) return EFI_SUCCESS; } -efi_status_t __maybe_unused efi_var_collect(struct efi_var_file **bufp, loff_t *lenp, - u32 check_attr_mask) -{ - size_t len = EFI_VAR_BUF_SIZE; - struct efi_var_file *buf; - struct efi_var_entry *var, *old_var; - size_t old_var_name_length = 2; - - *bufp = NULL; /* Avoid double free() */ - buf = calloc(1, len); - if (!buf) - return EFI_OUT_OF_RESOURCES; - var = buf->var; - old_var = var; - for (;;) { - efi_uintn_t data_length, var_name_length; - u8 *data; - efi_status_t ret; - - if ((uintptr_t)buf + len <= - (uintptr_t)var->name + old_var_name_length) - return EFI_BUFFER_TOO_SMALL; - - var_name_length = (uintptr_t)buf + len - (uintptr_t)var->name; - memcpy(var->name, old_var->name, old_var_name_length); - guidcpy(&var->guid, &old_var->guid); - ret = efi_get_next_variable_name_int( - &var_name_length, var->name, &var->guid); - if (ret == EFI_NOT_FOUND) - break; - if (ret != EFI_SUCCESS) { - free(buf); - return ret; - } - old_var_name_length = var_name_length; - old_var = var; - - data = (u8 *)var->name + old_var_name_length; - data_length = (uintptr_t)buf + len - (uintptr_t)data; - ret = efi_get_variable_int(var->name, &var->guid, - &var->attr, &data_length, data, - &var->time); - if (ret != EFI_SUCCESS) { - free(buf); - return ret; - } - if ((var->attr & check_attr_mask) == check_attr_mask) { - var->length = data_length; - var = (struct efi_var_entry *)ALIGN((uintptr_t)data + data_length, 8); - } - } - - buf->reserved = 0; - buf->magic = EFI_VAR_FILE_MAGIC; - len = (uintptr_t)var - (uintptr_t)buf; - buf->crc32 = crc32(0, (u8 *)buf->var, - len - sizeof(struct efi_var_file)); - buf->length = len; - *bufp = buf; - *lenp = len; - - return EFI_SUCCESS; -} - /** * efi_var_to_file() - save non-volatile variables as file *