From mboxrd@z Thu Jan 1 00:00:00 1970 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 5BBDEC433EF for ; Mon, 25 Oct 2021 13:14:39 +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 D1DB560F0F for ; Mon, 25 Oct 2021 13:14:38 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org D1DB560F0F Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=kernel.org 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 8AF4583535; Mon, 25 Oct 2021 15:13:54 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=kernel.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=kernel.org header.i=@kernel.org header.b="jsJ+WG1R"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 2FDA183461; Mon, 25 Oct 2021 15:13:30 +0200 (CEST) Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 2E39683557 for ; Mon, 25 Oct 2021 15:13:17 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=kernel.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=kabel@kernel.org Received: by mail.kernel.org (Postfix) with ESMTPSA id BD08060EFF; Mon, 25 Oct 2021 13:13:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1635167595; bh=OJ8GtjV5wBlpBR/32M+tufS9VSLKNS/zXOgDTrovazM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=jsJ+WG1R98w/krqjejXbJCjGgPaJYOgFtyN1RLAmgJ/AcZoe+71nCXbV+i6RHzZzu jo9Ssc/VHX4URQ2ps5M2cDJQ7dgR+QnWcW7IgkA9kSjlu7Kq2nMTOhU0Bf3a5fS40+ LotTqE0Lqb2eA8UXrynxxIYBQEbOCCPqV8b2TIRJUbZcuosARYiDf3oZkxs5Vvc1WY CZYbFOJ5/IIq6A+Q/BHwKB51tnsleq2BqaQTLjqHoFXamsc4w3uwkjM0MBnrEYJEic Q02/gboSE7etEwQs+Pl8dsd5nrZ0NN4ARPbOEh255Bm5U3aQnf/7f/lRTTS0NVDPMH DU4Ag3cNxlmsg== From: =?UTF-8?q?Marek=20Beh=C3=BAn?= To: Stefan Roese Cc: u-boot@lists.denx.de, =?UTF-8?q?Pali=20Roh=C3=A1r?= , =?UTF-8?q?Marek=20Beh=C3=BAn?= Subject: [PATCH u-boot-marvell 05/13] tools: kwboot: Inject baudrate change back code after data part Date: Mon, 25 Oct 2021 15:12:56 +0200 Message-Id: <20211025131304.21310-6-kabel@kernel.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20211025131304.21310-1-kabel@kernel.org> References: <20211025131304.21310-1-kabel@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 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 --- 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); -- 2.32.0