Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7841FC433F5 for ; Fri, 22 Oct 2021 06:47:37 +0000 (UTC) Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id D82616112D for ; Fri, 22 Oct 2021 06:47:36 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org D82616112D Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=denx.de Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=lists.denx.de Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 18CB583459; Fri, 22 Oct 2021 08:47:35 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=denx.de Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=denx.de; s=phobos-20191101; t=1634885255; bh=uuFDPHB9r7Z7/awUaJWVS1GVotQcub9dFQlaultPZn0=; h=Subject:To:Cc:References:From:Date:In-Reply-To:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From; b=Jmmpod3Y0zZLmD4yojlO4HQX3VksHGOnhugrk7+LTry7ImwELNnFZJtP9kHvdk4jr xcdCyqAah99fLLVbr4/YSKmtl3sBVN0X7hYlWXKiFsvtBsaToTrsK2VabmUmciy9yR lvGt074E4EyTJ6H1wWC3DKbau3WziFZIyIVoqser2Dt+AMDu/afx9TNuahL2UP0xf5 HWazNwfStznOrAcqkR34iNcJJmtyFC9e6gqlg3nybMrQc4E6Wrn24vtxAK6OAcuYIl vfxRow1pQP1UvfLhORQYUMWzZYe+hfVEUW8Z1VtCWJ1gRwJzOCWiy7anClJTj0QQT/ Lg/LlnI+32BkQ== Received: by phobos.denx.de (Postfix, from userid 109) id 23C2783459; Fri, 22 Oct 2021 08:47:33 +0200 (CEST) Received: from mout-u-107.mailbox.org (mout-u-107.mailbox.org [IPv6:2001:67c:2050:1::465:107]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 05B6683461 for ; Fri, 22 Oct 2021 08:47:30 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=denx.de Authentication-Results: phobos.denx.de; spf=fail smtp.mailfrom=sr@denx.de Received: from smtp202.mailbox.org (smtp202.mailbox.org [IPv6:2001:67c:2050:105:465:1:4:0]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-384) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-u-107.mailbox.org (Postfix) with ESMTPS id 4HbFJx6xsLzQjCy; Fri, 22 Oct 2021 08:47:29 +0200 (CEST) Subject: Re: [PATCH 2/4] tools: kwbimage: Align BIN header executable code to 128-bit boundary To: =?UTF-8?Q?Pali_Roh=c3=a1r?= Cc: =?UTF-8?Q?Marek_Beh=c3=ban?= , u-boot@lists.denx.de References: <20211021144609.9319-1-pali@kernel.org> <20211021144609.9319-3-pali@kernel.org> From: Stefan Roese Message-ID: Date: Fri, 22 Oct 2021 08:47:26 +0200 MIME-Version: 1.0 In-Reply-To: <20211021144609.9319-3-pali@kernel.org> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: de-DE Content-Transfer-Encoding: 8bit X-Rspamd-Queue-Id: EA33E1318 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 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.2 at phobos.denx.de X-Virus-Status: Clean On 21.10.21 16:46, Pali Rohár wrote: > ARM executable code inside the BIN header on some mvebu platforms > (e.g. A370, AXP) must always be aligned with the 128-bit boundary. This > requirement can be met by inserting dummy arguments into BIN header. > > Signed-off-by: Pali Rohár Reviewed-by: Stefan Roese Thanks, Stefan > --- > tools/kwbimage.c | 51 ++++++++++++++++++++++++++++++++---------------- > 1 file changed, 34 insertions(+), 17 deletions(-) > > diff --git a/tools/kwbimage.c b/tools/kwbimage.c > index 77bf4dd8865e..abc88d01b9d8 100644 > --- a/tools/kwbimage.c > +++ b/tools/kwbimage.c > @@ -932,6 +932,12 @@ static size_t image_headersz_v1(int *hasext) > */ > headersz = sizeof(struct main_hdr_v1); > > + if (image_get_csk_index() >= 0) { > + headersz += sizeof(struct secure_hdr_v1); > + if (hasext) > + *hasext = 1; > + } > + > count = image_count_options(IMAGE_CFG_DATA); > if (count > 0) > headersz += sizeof(struct register_set_hdr_v1) + 8 * count + 4; > @@ -963,15 +969,10 @@ static size_t image_headersz_v1(int *hasext) > return 0; > } > > - headersz += sizeof(struct opt_hdr_v1) + > - ALIGN(s.st_size, 4) + > - (binarye->binary.nargs + 2) * sizeof(uint32_t); > - if (hasext) > - *hasext = 1; > - } > - > - if (image_get_csk_index() >= 0) { > - headersz += sizeof(struct secure_hdr_v1); > + headersz += sizeof(struct opt_hdr_v1) + sizeof(uint32_t) + > + (binarye->binary.nargs) * sizeof(uint32_t); > + headersz = ALIGN(headersz, 16); > + headersz += ALIGN(s.st_size, 4) + sizeof(uint32_t); > if (hasext) > *hasext = 1; > } > @@ -984,9 +985,12 @@ static size_t image_headersz_v1(int *hasext) > } > > int add_binary_header_v1(uint8_t **cur, uint8_t **next_ext, > - struct image_cfg_element *binarye) > + struct image_cfg_element *binarye, > + struct main_hdr_v1 *main_hdr) > { > struct opt_hdr_v1 *hdr = (struct opt_hdr_v1 *)*cur; > + uint32_t add_args; > + uint32_t offset; > uint32_t *args; > size_t binhdrsz; > struct stat s; > @@ -1009,12 +1013,6 @@ int add_binary_header_v1(uint8_t **cur, uint8_t **next_ext, > goto err_close; > } > > - binhdrsz = sizeof(struct opt_hdr_v1) + > - (binarye->binary.nargs + 2) * sizeof(uint32_t) + > - ALIGN(s.st_size, 4); > - hdr->headersz_lsb = cpu_to_le16(binhdrsz & 0xFFFF); > - hdr->headersz_msb = (binhdrsz & 0xFFFF0000) >> 16; > - > *cur += sizeof(struct opt_hdr_v1); > > args = (uint32_t *)*cur; > @@ -1025,6 +1023,19 @@ int add_binary_header_v1(uint8_t **cur, uint8_t **next_ext, > > *cur += (binarye->binary.nargs + 1) * sizeof(uint32_t); > > + /* > + * ARM executable code inside the BIN header on some mvebu platforms > + * (e.g. A370, AXP) must always be aligned with the 128-bit boundary. > + * This requirement can be met by inserting dummy arguments into > + * BIN header, if needed. > + */ > + offset = *cur - (uint8_t *)main_hdr; > + add_args = ((16 - offset % 16) % 16) / sizeof(uint32_t); > + if (add_args) { > + *(args - 1) = cpu_to_le32(binarye->binary.nargs + add_args); > + *cur += add_args * sizeof(uint32_t); > + } > + > ret = fread(*cur, s.st_size, 1, bin); > if (ret != 1) { > fprintf(stderr, > @@ -1043,6 +1054,12 @@ int add_binary_header_v1(uint8_t **cur, uint8_t **next_ext, > > *cur += sizeof(uint32_t); > > + binhdrsz = sizeof(struct opt_hdr_v1) + > + (binarye->binary.nargs + add_args + 2) * sizeof(uint32_t) + > + ALIGN(s.st_size, 4); > + hdr->headersz_lsb = cpu_to_le16(binhdrsz & 0xFFFF); > + hdr->headersz_msb = (binhdrsz & 0xFFFF0000) >> 16; > + > return 0; > > err_close: > @@ -1299,7 +1316,7 @@ static void *image_create_v1(size_t *imagesz, struct image_tool_params *params, > if (e->type != IMAGE_CFG_BINARY) > continue; > > - if (add_binary_header_v1(&cur, &next_ext, e)) > + if (add_binary_header_v1(&cur, &next_ext, e, main_hdr)) > return NULL; > } > > Viele Grüße, Stefan -- DENX Software Engineering GmbH, Managing Director: Wolfgang Denk HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany Phone: (+49)-8142-66989-51 Fax: (+49)-8142-66989-80 Email: sr@denx.de