From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f52.google.com (mail-wm1-f52.google.com [209.85.128.52]) by mx.groups.io with SMTP id smtpd.web12.15252.1621230605073825791 for ; Sun, 16 May 2021 22:50:05 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@linaro.org header.s=google header.b=JH1QMwrL; spf=pass (domain: linaro.org, ip: 209.85.128.52, mailfrom: etienne.carriere@linaro.org) Received: by mail-wm1-f52.google.com with SMTP id u133so2837419wmg.1 for ; Sun, 16 May 2021 22:50:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=7Z4UW4i3ZRZ+AM0lK3Y61ygqQ1Jsg9DkMF7DbqeuNk0=; b=JH1QMwrLIoQFeYdldsB1Y047UkKKzdVEoZqcTcfpM+0NSdrXeMootndcREjHTbBVJ9 yzfGNZf9J0lInnhNWgrkAV2AGJAeOny99tnWSC2c9QPMpI8K8fjx2vpy6w6oG2uRTAhP c7XP2zkPRgHIo6Dh+7kuPkhb4a2OidedpoFxQrF8u+ltHwSJyZf9cZxL3CeIg3fshc4p NTsv1qAytbYxt/FwH5QFSjVMueLIGWrOUdND8bYEmYltI6yhfqAMeurn2ahqis9odgci ch54d6DKjD/1bdzydSw9xKeHhiPQI6l8ypnu9OPMc1fn9QbvD5DmF9y2sqKgTDyCI6U6 tv2w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=7Z4UW4i3ZRZ+AM0lK3Y61ygqQ1Jsg9DkMF7DbqeuNk0=; b=uWO6G/IR0EQoDHE9cQO5YbpvFj/6HzYZhlOkPVzETTNegbmSFsuviyra0vxbpVQBT4 VGMwilmS46ezYD/giSCyLruvvFuMQtx/dH5CV5pRK83vq8uj3LYDpmYKJM58QRHWbNtt N9rynj2UxroAmkDTpPQHYUCdZQ30ieN9UBFxVppMXRLzSJeSZMFUjLGLOBAWXTzIMZ/x 0LJoNVaDianUgYB23P73TpgwxCTn4gmYnlDwAL/gd5QI/nj8/l5U/5euquETyZL8HDIU zY2zN9tXmOvCdjbNZ4iTqumQZYAwFjpp+X2QX2Sho598wKpbPNG12mC/ZTQ6MXZqId0g cfxg== X-Gm-Message-State: AOAM530E2/tmeEDyVp6BPL6gWJ3Z/OG4E+CE7BAPuPGejyvLiSpngndB CCGaD5YcNIJhDPs51lRw+bWWZh8ArLMtbGlu X-Google-Smtp-Source: ABdhPJzrhFBfOu9EjHECz4Fv80qJhIkO7ZGZMRjYMMBWZ3xad9l5m3in0N0BxBIgu2FtMSB8yJ5H/Q== X-Received: by 2002:a1c:988f:: with SMTP id a137mr6931035wme.110.1621230603482; Sun, 16 May 2021 22:50:03 -0700 (PDT) Return-Path: Received: from lmecxl0524.lme.st.com (2a01cb058b850800452ff0effb46d5f4.ipv6.abo.wanadoo.fr. [2a01:cb05:8b85:800:452f:f0ef:fb46:d5f4]) by smtp.gmail.com with ESMTPSA id c194sm17488851wme.46.2021.05.16.22.50.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 16 May 2021 22:50:03 -0700 (PDT) From: "Etienne Carriere" To: devel@edk2.groups.io Cc: Achin Gupta , Ard Biesheuvel , Jiewen Yao , Leif Lindholm , Sami Mujawar , Sughosh Ganu , Etienne Carriere , Bob Feng , Liming Gao Subject: [PATCH v2 3/5] GenFv: Arm: support images entered in Thumb mode Date: Mon, 17 May 2021 07:49:09 +0200 Message-Id: <20210517054911.30665-3-etienne.carriere@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210517054911.30665-1-etienne.carriere@linaro.org> References: <20210517054911.30665-1-etienne.carriere@linaro.org> Change GenFv for Arm architecture to generate a specific jump instruction as image entry instruction, when the target entry label is assembled with Thumb instruction set. This is possible since SecCoreEntryAddress value fetched from the PE32 has its LSBit set when the entry instruction executes in Thumb mode. Cc: Bob Feng Cc: Liming Gao Cc: Achin Gupta Cc: Ard Biesheuvel Cc: Leif Lindholm Cc: Sughosh Ganu Signed-off-by: Etienne Carriere --- Changes since v1: - Fix typos in commit log and inline comments - Change if() test operand to be an explicit boolean --- BaseTools/Source/C/GenFv/GenFvInternalLib.c | 38 +++++++++++++++----- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/BaseTools/Source/C/GenFv/GenFvInternalLib.c b/BaseTools/Source/C/GenFv/GenFvInternalLib.c index 6e296b8ad6..5f3fd4f808 100644 --- a/BaseTools/Source/C/GenFv/GenFvInternalLib.c +++ b/BaseTools/Source/C/GenFv/GenFvInternalLib.c @@ -34,9 +34,27 @@ SPDX-License-Identifier: BSD-2-Clause-Patent #include "FvLib.h" #include "PeCoffLib.h" -#define ARMT_UNCONDITIONAL_JUMP_INSTRUCTION 0xEB000000 #define ARM64_UNCONDITIONAL_JUMP_INSTRUCTION 0x14000000 +/* + * Arm instruction to jump to Fv entry instruction in Arm or Thumb mode. + * From ARM Arch Ref Manual versions b/c/d, section A8.8.25 BL, BLX (immediate) + * BLX (encoding A2) branches to offset in Thumb instruction set mode. + * BL (encoding A1) branches to offset in Arm instruction set mode. + */ +#define ARM_JUMP_OFFSET_MAX 0xffffff +#define ARM_JUMP_TO_ARM(Offset) (0xeb000000 | ((Offset - 8) >> 2)) + +#define _ARM_JUMP_TO_THUMB(Imm32) (0xfa000000 | \ + (((Imm32) & (1 << 1)) << (24 - 1)) | \ + (((Imm32) >> 2) & 0x7fffff)) +#define ARM_JUMP_TO_THUMB(Offset) _ARM_JUMP_TO_THUMB((Offset) - 8) + +/* + * Arm instruction to retrun from exception (MOVS PC, LR) + */ +#define ARM_RETURN_FROM_EXCEPTION 0xE1B0F07E + BOOLEAN mArm = FALSE; BOOLEAN mRiscV = FALSE; STATIC UINT32 MaxFfsAlignment = 0; @@ -2203,23 +2221,25 @@ Returns: // if we found an SEC core entry point then generate a branch instruction // to it and populate a debugger SWI entry as well if (UpdateVectorSec) { + UINT32 EntryOffset; VerboseMsg("UpdateArmResetVectorIfNeeded updating ARM SEC vector"); - // B SecEntryPoint - signed_immed_24 part +/-32MB offset - // on ARM, the PC is always 8 ahead, so we're not really jumping from the base address, but from base address + 8 - ResetVector[0] = (INT32)(SecCoreEntryAddress - FvInfo->BaseAddress - 8) >> 2; + EntryOffset = (INT32)(SecCoreEntryAddress - FvInfo->BaseAddress); - if (ResetVector[0] > 0x00FFFFFF) { - Error(NULL, 0, 3000, "Invalid", "SEC Entry point must be within 32MB of the start of the FV"); + if (EntryOffset > ARM_JUMP_OFFSET_MAX) { + Error(NULL, 0, 3000, "Invalid", "SEC Entry point offset above 1MB of the start of the FV"); return EFI_ABORTED; } - // Add opcode for an unconditional branch with no link. i.e.: " B SecEntryPoint" - ResetVector[0] |= ARMT_UNCONDITIONAL_JUMP_INSTRUCTION; + if (SecCoreEntryAddress & 1 != 0) { + ResetVector[0] = ARM_JUMP_TO_THUMB(EntryOffset); + } else { + ResetVector[0] = ARM_JUMP_TO_ARM(EntryOffset); + } // SWI handler movs pc,lr. Just in case a debugger uses SWI - ResetVector[2] = 0xE1B0F07E; + ResetVector[2] = ARM_RETURN_FROM_EXCEPTION; // Place holder to support a common interrupt handler from ROM. // Currently not supported. For this to be used the reset vector would not be in this FV -- 2.17.1