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 X-Spam-Level: X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 70CFBC4338F for ; Wed, 25 Aug 2021 13:51:40 +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 E6F2E60EBC for ; Wed, 25 Aug 2021 13:51:39 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org E6F2E60EBC Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=nic.cz 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 17FE48320F; Wed, 25 Aug 2021 15:49:42 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=nic.cz Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (1024-bit key; secure) header.d=nic.cz header.i=@nic.cz header.b="Mvb1mbvO"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 0C55182EDA; Wed, 25 Aug 2021 15:47:51 +0200 (CEST) Received: from mail.nic.cz (lists.nic.cz [IPv6:2001:1488:800:400::400]) (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 170488315C for ; Wed, 25 Aug 2021 15:46:39 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=nic.cz Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=marek.behun@nic.cz Received: from dellmb.labs.office.nic.cz (unknown [IPv6:2001:1488:fffe:6:8747:7254:5571:3010]) by mail.nic.cz (Postfix) with ESMTPSA id 93887140A7F; Wed, 25 Aug 2021 15:46:38 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=nic.cz; s=default; t=1629899198; bh=AqPg8UzvSAnYezeQE33zhnv4B4/t3LMR5xA5BeTyoYM=; h=From:To:Date; b=Mvb1mbvOaaC/O2/VXAd/hircQJ50ySFyZpqjWk+Bewj1EdaDiMgaoUaSE/axXVN/b +Qgqgu01I2gp331+4SJxcjJSVvFSd5Lv3ro8l1zZSxmHyToyWUGvrbZVHMZ5RlLpTK Iap8AcANRCWmhZVr7/5Gh0h8lbLRi9euknwKaqXw= From: =?UTF-8?q?Marek=20Beh=C3=BAn?= To: Stefan Roese Cc: u-boot@lists.denx.de, pali@kernel.org, Chris Packham , Baruch Siach , Dennis Gilmore , Mario Six , Jon Nettleton , =?UTF-8?q?Marek=20Beh=C3=BAn?= Subject: [PATCH u-boot-marvell 24/29] tools: kwboot: Round up header size to 128 B when patching Date: Wed, 25 Aug 2021 15:46:29 +0200 Message-Id: <20210825134634.3959-25-marek.behun@nic.cz> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210825134634.3959-1-marek.behun@nic.cz> References: <20210825134634.3959-1-marek.behun@nic.cz> 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 The beginning of image data must be sent in a separate xmodem block; the block must not contain end of header with the beginning of data. Therefore we need to ensure that the image header size is a multiple of xmodem block size (which is 128 B). Read the file into a malloc()ed buffer of enough size instead of mmap()ing it. (If we are going to move the data, most of the pages will be dirty anyway.) Then move the payload if header size needs to be increased. Signed-off-by: Pali Rohár [ refactored ] Signed-off-by: Marek Behún --- tools/kwboot.c | 91 +++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 79 insertions(+), 12 deletions(-) diff --git a/tools/kwboot.c b/tools/kwboot.c index 1481735346..6d7d812416 100644 --- a/tools/kwboot.c +++ b/tools/kwboot.c @@ -25,7 +25,6 @@ #include #include #include -#include #include /* @@ -689,11 +688,12 @@ out: } static void * -kwboot_mmap_image(const char *path, size_t *size) +kwboot_read_image(const char *path, size_t *size, size_t reserve) { int rc, fd; struct stat st; void *img; + off_t tot; rc = -1; img = NULL; @@ -706,17 +706,30 @@ kwboot_mmap_image(const char *path, size_t *size) if (rc) goto out; - img = mmap(NULL, st.st_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0); - if (img == MAP_FAILED) { - img = NULL; + img = malloc(st.st_size + reserve); + if (!img) goto out; + + tot = 0; + while (tot < st.st_size) { + ssize_t rd = read(fd, img + tot, st.st_size - tot); + + if (rd < 0) + goto out; + + tot += rd; + + if (!rd && tot < st.st_size) { + errno = EIO; + goto out; + } } rc = 0; *size = st.st_size; out: if (rc && img) { - munmap(img, st.st_size); + free(img); img = NULL; } if (fd >= 0) @@ -752,8 +765,41 @@ kwboot_img_is_secure(void *img) return 0; } +static void +kwboot_img_grow_hdr(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); + + hdr->srcaddr = cpu_to_le32(srcaddr + need); + *size += need; + } + + if (kwbimage_version(img) == 1) { + struct main_hdr_v1 *hdr = img; + + hdrsz += grow; + hdr->headersz_msb = hdrsz >> 16; + hdr->headersz_lsb = cpu_to_le16(hdrsz & 0xffff); + } +} + static int -kwboot_img_patch_hdr(void *img, size_t size) +kwboot_img_patch_hdr(void *img, size_t *size) { int rc; struct main_hdr_v1 *hdr; @@ -767,7 +813,7 @@ kwboot_img_patch_hdr(void *img, size_t size) rc = -1; hdr = img; - if (size < hdrsz) { + if (*size < hdrsz) { errno = EINVAL; goto out; } @@ -781,7 +827,7 @@ kwboot_img_patch_hdr(void *img, size_t size) hdrsz = kwbheader_size(hdr); - if (size < hdrsz) { + if (*size < hdrsz) { errno = EINVAL; goto out; } @@ -841,10 +887,31 @@ kwboot_img_patch_hdr(void *img, size_t size) } } + if (hdrsz > le32_to_cpu(hdr->srcaddr) || + *size < le32_to_cpu(hdr->srcaddr) + le32_to_cpu(hdr->blocksize)) { + errno = EINVAL; + goto out; + } + hdr->blockid = IBR_HDR_UART_ID; + if (hdrsz % KWBOOT_XM_BLKSZ) { + size_t offset = (KWBOOT_XM_BLKSZ - hdrsz % KWBOOT_XM_BLKSZ) % + KWBOOT_XM_BLKSZ; + + if (is_secure) { + fprintf(stderr, "Cannot align image with secure header\n"); + errno = EINVAL; + goto out; + } + + kwboot_printv("Aligning image header to Xmodem block size\n"); + kwboot_img_grow_hdr(img, size, offset); + } + hdr->checksum = kwboot_hdr_csum8(hdr) - csum; + *size = le32_to_cpu(hdr->srcaddr) + le32_to_cpu(hdr->blocksize); rc = 0; out: return rc; @@ -971,13 +1038,13 @@ main(int argc, char **argv) } if (imgpath) { - img = kwboot_mmap_image(imgpath, &size); + img = kwboot_read_image(imgpath, &size, KWBOOT_XM_BLKSZ); if (!img) { perror(imgpath); goto out; } - rc = kwboot_img_patch_hdr(img, size); + rc = kwboot_img_patch_hdr(img, &size); if (rc) { fprintf(stderr, "%s: Invalid image.\n", imgpath); goto out; @@ -1020,7 +1087,7 @@ out: close(tty); if (img) - munmap(img, size); + free(img); return rv; -- 2.31.1