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 5EF80C433EF for ; Tue, 26 Oct 2021 05:44:07 +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 B3DC360E05 for ; Tue, 26 Oct 2021 05:44:06 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org B3DC360E05 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 779018355F; Tue, 26 Oct 2021 07:44:04 +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=1635227045; bh=J7faC/365l0CNTVN/IRKTXMgNkgnExGDivl58Da6R24=; h=Subject:To:Cc:References:From:Date:In-Reply-To:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From; b=f45UL1OBh4DfbRcCPQsb3o5zaAy/UPVHMowfYq+WboQ3UE4i5xLYNWQlDizeZsGLu 6245Ay94vhmemqHjhT/sKzsLSZu1RXr/RaiHneFmuspQt4z0Vdmyxnv+wp0Wn+losf fUAFc/GuYkKZ2QQc2oROQGi2YomwNuM2fqD/xd6Scsbq5hUL2k6e6XByTlhyXGlTvm X2CrfL+RRQEfYKA5F2Xfhf2psPgrNSFon70wwr93jQZIKhlHYxHPUoUKhVVTIMoWCv a9hzHTsSuix+t1CRhQLn2nwkX8WU7qINTKUwg2V4Yv2/0X8L4gsvMUJ76Y7acAr6tr iJ9G/f8xfB09Q== Received: by phobos.denx.de (Postfix, from userid 109) id 209A983521; Tue, 26 Oct 2021 07:44:02 +0200 (CEST) Received: from mout-u-204.mailbox.org (mout-u-204.mailbox.org [91.198.250.253]) (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 77F8D8357F for ; Tue, 26 Oct 2021 07:43:58 +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 smtp2.mailbox.org (unknown [91.198.250.124]) (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-204.mailbox.org (Postfix) with ESMTPS id 4Hdgjp2xmzzQjbf; Tue, 26 Oct 2021 07:43:58 +0200 (CEST) Subject: Re: [PATCH u-boot-marvell 05/13] tools: kwboot: Inject baudrate change back code after data part To: =?UTF-8?Q?Marek_Beh=c3=ban?= Cc: u-boot@lists.denx.de, =?UTF-8?Q?Pali_Roh=c3=a1r?= , =?UTF-8?Q?Marek_Beh=c3=ban?= References: <20211025131304.21310-1-kabel@kernel.org> <20211025131304.21310-6-kabel@kernel.org> From: Stefan Roese Message-ID: Date: Tue, 26 Oct 2021 07:43:54 +0200 MIME-Version: 1.0 In-Reply-To: <20211025131304.21310-6-kabel@kernel.org> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: de-DE Content-Transfer-Encoding: 8bit X-Rspamd-Queue-Id: 50959130B 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 25.10.21 15:12, Marek Behún wrote: > From: Pali Rohár > > Some vendor U-Boot kwbimage binaries (e.g. those for A375) have load > address set to zero. Therefore it is not possible to inject code which > changes baudrate back to 115200 Bd before the data part. > > So instead inject it after the data part and change kwbimage execution > address to that offset. Also store original execution address into > baudrate change code, so after it changes baudrate back to 115200 Bd, it > can jump to orignal address. > > Signed-off-by: Pali Rohár > [ refactored ] > Signed-off-by: Marek Behún Reviewed-by: Stefan Roese Thanks, Stefan > --- > tools/kwboot.c | 72 ++++++++++++++++++++++---------------------------- > 1 file changed, 31 insertions(+), 41 deletions(-) > > diff --git a/tools/kwboot.c b/tools/kwboot.c > index bc44301535..bf26a667b7 100644 > --- a/tools/kwboot.c > +++ b/tools/kwboot.c > @@ -1295,34 +1295,22 @@ kwboot_img_is_secure(void *img) > } > > static void * > -kwboot_img_grow_data_left(void *img, size_t *size, size_t grow) > +kwboot_img_grow_data_right(void *img, size_t *size, size_t grow) > { > - uint32_t hdrsz, datasz, srcaddr; > struct main_hdr_v1 *hdr = img; > - uint8_t *data; > - > - srcaddr = le32_to_cpu(hdr->srcaddr); > - > - hdrsz = kwbheader_size(hdr); > - data = (uint8_t *)img + srcaddr; > - datasz = *size - srcaddr; > - > - /* only move data if there is not enough space */ > - if (hdrsz + grow > srcaddr) { > - size_t need = hdrsz + grow - srcaddr; > - > - /* move data by enough bytes */ > - memmove(data + need, data, datasz); > - *size += need; > - srcaddr += need; > - } > + void *result; > > - srcaddr -= grow; > - hdr->srcaddr = cpu_to_le32(srcaddr); > - hdr->destaddr = cpu_to_le32(le32_to_cpu(hdr->destaddr) - grow); > + /* > + * 32-bit checksum comes after end of image code, so we will be putting > + * new code there. So we get this pointer and then increase data size > + * (since increasing data size changes kwboot_img_csum32_ptr() return > + * value). > + */ > + result = kwboot_img_csum32_ptr(img); > hdr->blocksize = cpu_to_le32(le32_to_cpu(hdr->blocksize) + grow); > + *size += grow; > > - return (uint8_t *)img + srcaddr; > + return result; > } > > static void > @@ -1400,14 +1388,20 @@ kwboot_add_bin_ohdr_v1(void *img, size_t *size, uint32_t binsz) > } > > static void > -_copy_baudrate_change_code(struct main_hdr_v1 *hdr, void *dst, int pre, > - int old_baud, int new_baud) > +_inject_baudrate_change_code(void *img, size_t *size, int pre, > + int old_baud, int new_baud) > { > - size_t codesz = sizeof(kwboot_baud_code); > - uint8_t *code = dst; > + uint32_t codesz = sizeof(kwboot_baud_code); > + struct main_hdr_v1 *hdr = img; > + uint8_t *code; > > if (pre) { > - size_t presz = sizeof(kwboot_pre_baud_code); > + uint32_t presz = sizeof(kwboot_pre_baud_code); > + uint32_t orig_datasz; > + > + orig_datasz = le32_to_cpu(hdr->blocksize) - sizeof(uint32_t); > + > + code = kwboot_img_grow_data_right(img, size, presz + codesz); > > /* > * We need to prepend code that loads lr register with original > @@ -1421,9 +1415,12 @@ _copy_baudrate_change_code(struct main_hdr_v1 *hdr, void *dst, int pre, > memcpy(code, kwboot_pre_baud_code, presz); > *(uint32_t *)code = hdr->execaddr; > > - hdr->execaddr = cpu_to_le32(le32_to_cpu(hdr->destaddr) + 4); > + hdr->execaddr = cpu_to_le32(le32_to_cpu(hdr->destaddr) + > + orig_datasz + 4); > > code += presz; > + } else { > + code = kwboot_add_bin_ohdr_v1(img, size, codesz); > } > > memcpy(code, kwboot_baud_code, codesz - 8); > @@ -1516,9 +1513,6 @@ kwboot_img_patch(void *img, size_t *size, int baudrate) > } > > if (baudrate) { > - uint32_t codesz = sizeof(kwboot_baud_code); > - void *code; > - > if (image_ver == 0) { > fprintf(stderr, > "Cannot inject code for changing baudrate into v0 image header\n"); > @@ -1539,20 +1533,16 @@ kwboot_img_patch(void *img, size_t *size, int baudrate) > */ > kwboot_printv("Injecting binary header code for changing baudrate to %d Bd\n", > baudrate); > - > - code = kwboot_add_bin_ohdr_v1(img, size, codesz); > - _copy_baudrate_change_code(hdr, code, 0, 115200, baudrate); > + _inject_baudrate_change_code(img, size, 0, 115200, baudrate); > > /* > * Now inject code that changes the baudrate back to 115200 Bd. > - * This code is prepended to the data part of the image, so it > - * is executed before U-Boot proper. > + * This code is appended after the data part of the image, and > + * execaddr is changed so that it is executed before U-Boot > + * proper. > */ > kwboot_printv("Injecting code for changing baudrate back\n"); > - > - codesz += sizeof(kwboot_pre_baud_code); > - code = kwboot_img_grow_data_left(img, size, codesz); > - _copy_baudrate_change_code(hdr, code, 1, baudrate, 115200); > + _inject_baudrate_change_code(img, size, 1, baudrate, 115200); > > /* recompute header size */ > hdrsz = kwbheader_size(hdr); > 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