From patchwork Tue Nov 6 22:57:42 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 993973 X-Patchwork-Delegate: agraf@suse.de Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.denx.de (client-ip=81.169.180.215; helo=lists.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=chromium.org Received: from lists.denx.de (dione.denx.de [81.169.180.215]) by ozlabs.org (Postfix) with ESMTP id 42qQ5Y01sJz9sCm for ; Wed, 7 Nov 2018 10:01:20 +1100 (AEDT) Received: by lists.denx.de (Postfix, from userid 105) id 84516C22946; Tue, 6 Nov 2018 22:59:11 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on lists.denx.de X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=RCVD_IN_DNSWL_BLOCKED, RCVD_IN_MSPIKE_H2 autolearn=unavailable autolearn_force=no version=3.4.0 Received: from lists.denx.de (localhost [IPv6:::1]) by lists.denx.de (Postfix) with ESMTP id 3C055C22951; Tue, 6 Nov 2018 22:58:02 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id C8D38C22940; Tue, 6 Nov 2018 22:57:58 +0000 (UTC) Received: from mail-vs1-f74.google.com (mail-vs1-f74.google.com [209.85.217.74]) by lists.denx.de (Postfix) with ESMTPS id 05005C22851 for ; Tue, 6 Nov 2018 22:57:55 +0000 (UTC) Received: by mail-vs1-f74.google.com with SMTP id e124so566102vsc.7 for ; Tue, 06 Nov 2018 14:57:54 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=itaqCPenC1VxocxlUjPAUMe5XzZQASZN0KXa06SEs3E=; b=UAPbcp7COlpehUKcOzDoEyI2ZPRMlXoJrUR18MqdHaeQPooH2kqIhlOsavh33TG26U HsF3RiQ5tmZOexuVaFOeHDMKuYxqSip+NtDztprQQv/iwP4Fi9swI7h6tBBOic3W0SK4 HU5WORCP1iMR0uBTkTsmN7dcv+mKeQy+01zVBw2RE52/6Wq+mVhZY0pQ39fFCro31Tef 5quvm6HdcGCXsL2F9aotV5QeR8utgygFdFaoOHCjrXwHoQdz6lAttbleX81MxMFyF7By BqHmWkrk43f+ZZRQNDs9a4lv+9mbB+nego8Y9CRAmLjiWamz4ei5podGcxK1kz1cUR7l siQg== X-Gm-Message-State: AGRZ1gLd/3dznDbLCh/7UWNjRnEvLc9kBTbhT4B772Lx1YAGe2ay0aUH uxBu0lPy2OSi1AWzlnkvxIBQrwg= X-Google-Smtp-Source: AJdET5eyusRKQPm2p0KzePj8uaZImqZpy6a+rAGsQFhsZ4ssZrz9RdJFdntd7exVa7Z/0GHG//TSgGA= X-Received: by 2002:a1f:cc2:: with SMTP id 185mr25447752vkm.5.1541545073850; Tue, 06 Nov 2018 14:57:53 -0800 (PST) Date: Tue, 6 Nov 2018 15:57:42 -0700 In-Reply-To: <20181106225744.139945-1-sjg@chromium.org> Message-Id: <20181106225744.139945-5-sjg@chromium.org> Mime-Version: 1.0 References: <20181106225744.139945-1-sjg@chromium.org> X-Mailer: git-send-email 2.19.1.930.g4563a0d9d0-goog From: Simon Glass To: U-Boot Mailing List Cc: Alexander Graf Subject: [U-Boot] [PATCH v12 4/6] efi: Split out test init/uninit into functions X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.18 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" The functions in bootefi are very long because they mix high-level code and control with the low-level implementation. To help with this, create functions which handle preparing for running the test and cleaning up afterwards. Also shorten the awfully long variable names here. Signed-off-by: Simon Glass --- Changes in v12: - Rename image to image_prot Changes in v11: None Changes in v9: - Add comments to bootefi_test_prepare() about the memset()s Changes in v7: None Changes in v5: - Drop call to efi_init_obj_list() which is now done in do_bootefi() Changes in v4: None Changes in v3: - Add new patch to split out test init/uninit into functions cmd/bootefi.c | 85 +++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 65 insertions(+), 20 deletions(-) diff --git a/cmd/bootefi.c b/cmd/bootefi.c index 4d68d807480..0dd18d594d5 100644 --- a/cmd/bootefi.c +++ b/cmd/bootefi.c @@ -455,6 +455,64 @@ exit: return ret; } +#ifdef CONFIG_CMD_BOOTEFI_SELFTEST +/** + * bootefi_test_prepare() - prepare to run an EFI test + * + * This sets things up so we can call EFI functions. This involves preparing + * the 'gd' pointer and setting up the load ed image data structures. + * + * @image: Pointer to a struct which will hold the loaded image info. + * This struct will be inited by this function before use. + * @obj: Pointer to a struct which will hold the loaded image object + * This struct will be inited by this function before use. + * @path: File path to the test being run (often just the test name with a + * backslash before it + * @test_func: Address of the test function that is being run + * @return 0 if OK, -ve on error + */ +static efi_status_t bootefi_test_prepare(struct efi_loaded_image **imagep, + struct efi_loaded_image_obj **objp, + const char *path, ulong test_func) +{ + efi_status_t r; + + /* Construct a dummy device path */ + bootefi_device_path = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE, + (uintptr_t)test_func, + (uintptr_t)test_func); + bootefi_image_path = efi_dp_from_file(NULL, 0, path); + r = efi_setup_loaded_image(bootefi_device_path, bootefi_image_path, + objp, imagep); + if (r) + return r; + /* + * gd lives in a fixed register which may get clobbered while we execute + * the payload. So save it here and restore it on every callback entry + */ + efi_save_gd(); + + /* Transfer environment variable efi_selftest as load options */ + set_load_options(*imagep, "efi_selftest"); + + return 0; +} + +/** + * bootefi_test_finish() - finish up after running an EFI test + * + * @image: Pointer to a struct which holds the loaded image info + * @obj: Pointer to a struct which holds the loaded image object + */ +static void bootefi_test_finish(struct efi_loaded_image *image, + struct efi_loaded_image_obj *obj) +{ + efi_restore_gd(); + free(image->load_options); + efi_delete_handle(&obj->parent); +} +#endif /* CONFIG_CMD_BOOTEFI_SELFTEST */ + static int do_bootefi_bootmgr_exec(void) { struct efi_device_path *device_path, *file_path; @@ -527,29 +585,16 @@ static int do_bootefi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) #endif #ifdef CONFIG_CMD_BOOTEFI_SELFTEST if (!strcmp(argv[1], "selftest")) { - struct efi_loaded_image_obj *image_handle; - struct efi_loaded_image *loaded_image_info; - - /* Construct a dummy device path. */ - bootefi_device_path = efi_dp_from_mem(EFI_RESERVED_MEMORY_TYPE, - (uintptr_t)&efi_selftest, - (uintptr_t)&efi_selftest); - bootefi_image_path = efi_dp_from_file(NULL, 0, "\\selftest"); - - r = efi_setup_loaded_image(bootefi_device_path, - bootefi_image_path, &image_handle, - &loaded_image_info); - if (r != EFI_SUCCESS) + struct efi_loaded_image_obj *obj; + struct efi_loaded_image *image_prot; + + if (bootefi_test_prepare(&image_prot, &obj, "\\selftest", + (uintptr_t)&efi_selftest)) return CMD_RET_FAILURE; - efi_save_gd(); - /* Transfer environment variable efi_selftest as load options */ - set_load_options(loaded_image_info, "efi_selftest"); /* Execute the test */ - r = efi_selftest(image_handle, &systab); - efi_restore_gd(); - free(loaded_image_info->load_options); - efi_delete_handle(&image_handle->parent); + r = efi_selftest(obj, &systab); + bootefi_test_finish(image_prot, obj); return r != EFI_SUCCESS; } else #endif