From patchwork Tue Aug 14 14:25:09 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Geoff Levand X-Patchwork-Id: 957545 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) (using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 41qZdM2dWNz9sBZ for ; Wed, 15 Aug 2018 00:25:43 +1000 (AEST) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=infradead.org Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=infradead.org header.i=@infradead.org header.b="cPRZ0EFB"; dkim-atps=neutral Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) by lists.ozlabs.org (Postfix) with ESMTP id 41qZdL6gFbzF1Rm for ; Wed, 15 Aug 2018 00:25:42 +1000 (AEST) Authentication-Results: lists.ozlabs.org; dmarc=none (p=none dis=none) header.from=infradead.org Authentication-Results: lists.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=infradead.org header.i=@infradead.org header.b="cPRZ0EFB"; dkim-atps=neutral X-Original-To: Petitboot@lists.ozlabs.org Delivered-To: Petitboot@lists.ozlabs.org Authentication-Results: lists.ozlabs.org; spf=none (mailfrom) smtp.mailfrom=infradead.org (client-ip=2607:7c80:54:e::133; helo=bombadil.infradead.org; envelope-from=geoff@infradead.org; receiver=) Authentication-Results: lists.ozlabs.org; dmarc=none (p=none dis=none) header.from=infradead.org Authentication-Results: lists.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=infradead.org header.i=@infradead.org header.b="cPRZ0EFB"; dkim-atps=neutral Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 41qZd134qNzF15Q for ; Wed, 15 Aug 2018 00:25:21 +1000 (AEST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20170209; h=Content-Transfer-Encoding: Content-Type:In-Reply-To:MIME-Version:Date:Message-ID:References:Cc:To:From: Subject:Sender:Reply-To: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=QI9ZKhim7JiqSjm1RLu8H9VIzo2rQEGtaRXW1p3DBA4=; b=cPRZ0EFBt/A74bvI5LqRYCi4V 1rjLAzzgpb4VPeSyrP7l7YaUxTEWfd5V3I1atU2MTQpRuaDPnj2q9IlsHEON9VmA8zc/B2hyhPs/l y3xCVu1f1jzxz9R9IlFw8WFXzE+eDAUi7tmB2H1n4z7+odH4NqTa9zgRVP2v53OCxHThCUZtju2aG DEqwCWTqeWNoHXBJODkSC2k+p93rlNrbS5LABAk/AECqx0HG+0/ZbBDh7kTW637RVCjyk8YSWDFY/ fmmGOqULXoVoyqF/ALA09QLeZ7wpyIt7ZAyqrfabnQcxK6vM7BU0GpW3w4D22fqd5r1CfcXbT8FPz GB1yd1rIA==; Received: from [2602:306:37b0:7840:8c97:c0db:6025:133e] by bombadil.infradead.org with esmtpsa (Exim 4.90_1 #2 (Red Hat Linux)) id 1fpaG4-0006RD-Cq; Tue, 14 Aug 2018 14:25:16 +0000 Subject: [PATCH v2] test/efivar: Rework for efi_mount From: Geoff Levand To: Samuel Mendoza-Jonas References: Message-ID: <555e6a04-70ec-cd44-2834-efc204a935e9@infradead.org> Date: Tue, 14 Aug 2018 07:25:09 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.9.1 MIME-Version: 1.0 In-Reply-To: Content-Language: en-US X-BeenThere: petitboot@lists.ozlabs.org X-Mailman-Version: 2.1.27 Precedence: list List-Id: Petitboot bootloader development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Petitboot@lists.ozlabs.org Errors-To: petitboot-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org Sender: "Petitboot" Signed-off-by: Geoff Levand --- v2: Add SOB. test/lib/test-efivar.c | 128 +++++++++++++++++++------------------------------ 1 file changed, 49 insertions(+), 79 deletions(-) diff --git a/test/lib/test-efivar.c b/test/lib/test-efivar.c index a85b73c..1be95fd 100644 --- a/test/lib/test-efivar.c +++ b/test/lib/test-efivar.c @@ -16,117 +16,87 @@ * reserved. * Author: Ge Song */ + #include -#include #include -#include -#include #include #include #include #include + #include #include -#include +#include #include "efi/efivar.h" +#include "log/log.h" #include "talloc/talloc.h" -#define DEF_ATTR (EFI_VARIABLE_NON_VOLATILE | \ - EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS) - -static const char *test_efivar_guid = "c9c07add-256e-4452-b911-f8d0d35a1ac7"; -static const char *test_varname = "efivartest"; -static const char *test_data = "petitboot"; +static const struct efi_mount efi_mount = { + .path = "/tmp/pb-test-efivar", + .guid = "c9c07add-256e-4452-b911-f8d0d35a1ac7", +}; +static const char v_name[] = "petitboot-test-one"; +static const char v_data[] = "petitboot-efi-tester"; -static char* find_efitest_path(void) +void finish(int code) { - static char dir[PATH_MAX] = {0}; - static bool run = false; - char *rest_path = "/efivarfs_data/"; - char *pos = NULL; - - if (run) - return dir; - - readlink("/proc/self/exe", dir, PATH_MAX); - - pos = strrchr(dir, '/'); - *pos = '\0'; - - strcat(dir, rest_path); - run = true; - - return dir; + efi_del_variable(&efi_mount, v_name); + rmdir(efi_mount.path); + exit(code); } -static bool probe(void) +int main(void) { - char *path; + struct efi_data *efi_data; int rc; - path = find_efitest_path(); + __pb_log_init(stderr, true); - rc = access(path, F_OK); + rc = mkdir(efi_mount.path, 0755); if (rc) { - if (errno == ENOENT) { - rc = mkdir(path, 0755); - if(rc) - return false; - } else { - return false; + if (errno == EEXIST) + pb_log("mkdir exists\n"); + else { + pb_log("mkdir failed: (%d) %s\n", errno, + strerror(errno)); + finish(__LINE__); } } - set_efivarfs_path(path); + if (!efi_check_mount_magic(&efi_mount, false)) + finish(__LINE__); - return true; -} + efi_data = talloc_zero(NULL, struct efi_data); + efi_data->attributes = EFI_DEFALT_ATTRIBUTES; + efi_data->data = talloc_strdup(efi_data, v_data); + efi_data->data_size = sizeof(v_data); -int main(void) -{ - void *ctx = NULL; - int rc, errno_value; - uint32_t attr = DEF_ATTR; - char *path = NULL; - struct efi_data *efi_data; - - if(!probe()) - return ENOENT; - - talloc_new(ctx); - - efi_data = talloc_zero(ctx, struct efi_data); - efi_data->attributes = attr; - efi_data->data = talloc_strdup(efi_data, test_data); - efi_data->data_size = strlen(test_data) + 1; - - rc = efi_set_variable(test_efivar_guid, test_varname, - efi_data); + if (efi_set_variable(&efi_mount, v_name, efi_data)) + finish(__LINE__); talloc_free(efi_data); - rc = efi_get_variable(ctx, test_efivar_guid, test_varname, - &efi_data); - assert(efi_data->data != NULL); - rc = strcmp((char *)efi_data->data, test_data); - if (rc) { - talloc_free(ctx); - assert(0); - } - - rc = efi_del_variable(test_efivar_guid, test_varname); + if (efi_get_variable(NULL, &efi_mount, v_name, &efi_data)) + finish(__LINE__); - rc = efi_get_variable(ctx, test_efivar_guid, test_varname, - &efi_data); + if (!efi_data->data) { + pb_log("No efi_data->data\n"); + finish(__LINE__); + } - errno_value = errno; - talloc_free(ctx); + if (strcmp((char *)efi_data->data, v_data)) { + pb_log("Bad efi_data->data: '%s' != '%s'\n", + (char *)efi_data->data, v_data); + finish(__LINE__); + } - assert(errno_value == ENOENT); + if (efi_del_variable(&efi_mount, v_name)) + finish(__LINE__); - path = find_efitest_path(); - rmdir(path); + /* Get after delete should fail. */ + if (!efi_get_variable(NULL, &efi_mount, v_name, &efi_data)) + finish(__LINE__); - return EXIT_SUCCESS; + finish(EXIT_SUCCESS); }