From patchwork Thu May 22 12:37:05 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Neil Armstrong X-Patchwork-Id: 891703 Delivered-To: patch@linaro.org Received: by 2002:adf:e88a:0:b0:3a3:61c9:c5d4 with SMTP id d10csp2610890wrm; Thu, 22 May 2025 05:37:23 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCXWKSbI7D1LprvT5irVUprMgRlkcUZeb0UIFqk7kWsJbz2gtFbmYQz5DJvMTJM0Qk7bzL7sQg==@linaro.org X-Google-Smtp-Source: AGHT+IHSWaXYcgMZWQfhfSdDoyYcaPpZoQJPDMixTGyd0UXEEyH4dVtsQSFGRNK7OmvH9W4U66ds X-Received: by 2002:a05:6000:4310:b0:3a3:699a:3108 with SMTP id ffacd0b85a97d-3a3699a347fmr17026668f8f.14.1747917443256; Thu, 22 May 2025 05:37:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1747917443; cv=none; d=google.com; s=arc-20240605; b=ZlsvTy+wlK2hbeTvIRokljMCbjBZHuoAX586JWMcwanOXfrMFhgEryWFBtcNVGa1+t L8YiONjwdJVsYl7gomHMP4FhJXPXGcDOcfCnnIJlPRgPiXmUnPRgSvAbgG09mFfODUwN lZfwewGgVsZng0SVpkPKy9Zlvej9ALs+6Vou0ZgDDX4QylLpan5y8ll35+3pDsKTXJof +KVFX5fuK32lde2cxw8lKHuyytmLwqyCPVttW6KHpptebn2KyQnvEKIOD2z61xB6wCIm uPfmvNfKMS10FKpAp3suIgmopnaa5wqs8aX4ul9CFqvzpv7CmSTPg5GNqBHLVNrUomw9 QdFA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:cc:to:in-reply-to:references :message-id:content-transfer-encoding:mime-version:subject:date:from :dkim-signature; bh=/YPHwMzz1kHeByJZyGaprIJA3EOq7dCpM2gKxh2o3z8=; fh=+i+RTkrPvc0CCcB6rXKbnTZaOKShRvWA2CazXh31SXM=; b=V5T8y5akAYBBY4xsNJZuI9KqJgXjI8398+oB6yyonO0lwkADpM6PGGVIKvur0rnrmX 6C30L6rPkGipwghR4FtPiRsrDLa9IRJD6M5MoAh1PCKVccQz4wg0YD41bZb1zYCwZB4c rxn5W/xOteARRt/ADUW9fDKAFkLb8hz9s485BUUeSKKtsy0r41Kj4ndzZwRrucPYitKx 1zaGJkjA3vxLytzf3IH2yfX63n6Y8pQSqFeTTbVFeCDU5gGClqOm+pvpUS8e1Yyy122I RgqEMjdInt/WgewNshdBaozvsLXsF+VB9cbKuzWrHrjC3A+pRrIooe9eqqG9lgQkYA2R C0Hg==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=V+OnE+Rs; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org; dara=neutral header.i=@linaro.org Return-Path: Received: from phobos.denx.de (phobos.denx.de. [2a01:238:438b:c500:173d:9f52:ddab:ee01]) by mx.google.com with ESMTPS id ffacd0b85a97d-3a362bfccaesi10480008f8f.38.2025.05.22.05.37.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 May 2025 05:37:23 -0700 (PDT) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=V+OnE+Rs; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org; dara=neutral header.i=@linaro.org Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id B0F058309A; Thu, 22 May 2025 14:37:15 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.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=linaro.org header.i=@linaro.org header.b="V+OnE+Rs"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 4C9F083043; Thu, 22 May 2025 14:37:12 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_NONE,T_SPF_TEMPERROR autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-wm1-x331.google.com (mail-wm1-x331.google.com [IPv6:2a00:1450:4864:20::331]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 705BD83096 for ; Thu, 22 May 2025 14:37:09 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=neil.armstrong@linaro.org Received: by mail-wm1-x331.google.com with SMTP id 5b1f17b1804b1-43cf680d351so54502995e9.0 for ; Thu, 22 May 2025 05:37:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1747917428; x=1748522228; darn=lists.denx.de; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=/YPHwMzz1kHeByJZyGaprIJA3EOq7dCpM2gKxh2o3z8=; b=V+OnE+RsAFL6sVCcb8q18qAX4C1PFYqyLJ8G5JwTc/tT7Yminx3nrkWTRHKPyVe0ZI 5PXMkP1sG8NPcd64pt0nxT9yUKK2fh/d1gRYBuPAueYmLKbdpM8Ln9LJYfBmtSMpxUaf kA7V2GxUzNaWg6Wb0BP0DqJoqcNrnXU5ggpw6xZcNEGHPquMMHtWA+Wjl/FIrQCZ4Og4 oua2d/DEGxAdjNt3rAqKFddl531VWc1LtF2GHZRA4xEvxVeF8faB5RbcoHr+Zyo0PNCz 1Njjgf0czo55Em4sEZ8ZPgILY6WIoBMTmEi1sQfTICrSA6/XL/wQcex8vrkKybqoJU0T Oj/g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747917428; x=1748522228; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=/YPHwMzz1kHeByJZyGaprIJA3EOq7dCpM2gKxh2o3z8=; b=Mdlq5Fz7rXnPVm9HkISZVov0K2vwFmtfw2fptvSTUcLl6vtJgenEza03hoHxAYuZzu GINkDA8lB42rT8ti73wIuSIkCn2+1Eq2rBp26FI8X7SlKL2euV/f13W4ujTj4JDtfzg/ zB9wkMOOVhPHs6ezh5MFaJGEHzFkhr0hZI1MbN7IkeK5rQ7pUa+eQV/vffhu5jK+6FGm SFzz5I/zQgznGtEUGHppH395koTNaNMg5h7qOEUd/WT0BmEKaOtopZinlo0COFiiDRg+ GBXj8HdPK9X1ovSJcKfdIZEj0hxjV5jZ/mveKBW5oK6zwJPYmp5bRMM4XYxwAbOLnYDb FQRw== X-Gm-Message-State: AOJu0YyeL3VF5CPeCga91gDl1z6S5ubc2lYAyMMuIDAXvqJCXCfl0cOd xk6uMMN6WBi+Cr2ngyYkTv/y+b40KwnfUa2nXoxv5DQACPVSVDBUJcO37qkHRt5B6hQ= X-Gm-Gg: ASbGncvcTJ7KFpNCshSBHc4emuW7kdAsqWz8l8E56LebRez9OBJd0BtQYfKFC3DHGLt SUVKk2vRokpGDwKR1lWBmKDE0mYWsBR6aefdo2kFfFa2GLlTidaqSaphP85AfLpwc9vG6UmLIIi yxVSXQ85WUET8tFVKzpbifnipafE1iIAbwuUaDY4Howgf3c7ldrTokblaZctjEV8HtHroO84NPE R39PHkmBbRSVwbB2dmt5RQNbKszgAsTIahyhaPaUmrGlXcltYyNgQv6xUHsnmXIgYK3mT4FFjj1 fKzk1FXGbXBMaB2ZVqtGVWUAJXiSeWiu8tRaMx1f8n13y7grqkG7xm+m7W9rdgF/HQva1Y09 X-Received: by 2002:a05:600c:1f82:b0:441:bbe5:f562 with SMTP id 5b1f17b1804b1-442f8534e53mr292082435e9.16.1747917428464; Thu, 22 May 2025 05:37:08 -0700 (PDT) Received: from arrakeen.starnux.net ([2a01:e0a:3d9:2080:8261:5fff:fe11:bdda]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-447f38145eesm102271045e9.26.2025.05.22.05.37.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 22 May 2025 05:37:08 -0700 (PDT) From: Neil Armstrong Date: Thu, 22 May 2025 14:37:05 +0200 Subject: [PATCH RFT v4 1/3] fastboot: blk: introduce fastboot block flashing support MIME-Version: 1.0 Message-Id: <20250522-topic-fastboot-blk-v4-1-af7f7f30564d@linaro.org> References: <20250522-topic-fastboot-blk-v4-0-af7f7f30564d@linaro.org> In-Reply-To: <20250522-topic-fastboot-blk-v4-0-af7f7f30564d@linaro.org> To: Tom Rini , Mattijs Korpershoek , Mattijs Korpershoek Cc: u-boot@lists.denx.de, Neil Armstrong , Dmitrii Merkurev X-Mailer: b4 0.14.2 X-Developer-Signature: v=1; a=openpgp-sha256; l=14744; i=neil.armstrong@linaro.org; h=from:subject:message-id; bh=u38gyfBbqcgweukdputMcJtzWtuesaIscq4GIrJIkOU=; b=owEBbQKS/ZANAwAKAXfc29rIyEnRAcsmYgBoLxpyV4N1CRJrmVunZnG3IqVXJ+9pZg+zlVpzsrgH 0l1Tm0KJAjMEAAEKAB0WIQQ9U8YmyFYF/h30LIt33NvayMhJ0QUCaC8acgAKCRB33NvayMhJ0U8VEA DE1fPJOKSmn7iBjla0Gndhrwc7bPit2PR3SH4mOKjS3ojAuSWYk5ArExAzag07uplmJwY0185hvHPk CwZ/7vOvCcBROlIpIhCEpQ/EzP8f4RQb9tKSBEGT5GYj+uEn56Rb0hY0LLwl6m1e4PZ0Vyoh/uIX4G A9bEAy9eJ37n2IDtkMRPcG6tm7yGCPcDrur0doiOgoHhFIW5g3CI/ry7/KXnLIHeRIFbKnxbBa6tFN 2xhWF9SooSOKPdPd+PHV3RxyA6LJ6iDJ/uofeLjl89HFd/d7DXgVgNDQ5OyPd7i9uOOsYgb8WrFNtg eSoYPDHjKLlxuk+HAXw/9P26RKXXEnBOELkDpaMIOfINXcu4R7Mn7x6YltTq5tdagCfyVgG1sYORP2 R84vJeDk29ukJpj0IesIVdPxO4v6oneAdoWCJNjVoGz6S7E3YY6y0Oxc+pQJjL4tcJZPPgCfnAfnRo SV/0OZXkCIIDwXXvkp7pkZGWfL5stmZwHEWVm5n+CU9DImeD1r7ns8xbMCRbXVSGWodrQoruvb1ZS5 UiFnE+Vsc4TDOd8gKDvvhSQtbwhZ9HEGbizdV5GT3uEhYjGjekuH7+xJ7N+amhIG/9gbeVm5oePzfd t0a6hvAhV88sqzGmkEh+eEvxY3dgiJBWNSnyH+MApnqyGI9MLFXn1lPA+MOw== X-Developer-Key: i=neil.armstrong@linaro.org; a=openpgp; fpr=89EC3D058446217450F22848169AB7B1A4CFF8AE X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 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.8 at phobos.denx.de X-Virus-Status: Clean From: Dmitrii Merkurev Introduce fastboot block flashing functions and helpers to be shared with the MMC implementation. The write logic comes from the mmc implementation, while the partition lookup is much simpler and could be extended. For the erase logic, allmost no block drivers exposes the erase operation, except mmc & virtio, so in order to allow erasiong any partition a soft-erase logic has been added to write zero-ed buffers in a loop. Signed-off-by: Dmitrii Merkurev Reviewed-by: Mattijs Korpershoek Tested-by: Mattijs Korpershoek Signed-off-by: Neil Armstrong --- drivers/fastboot/fb_block.c | 323 ++++++++++++++++++++++++++++++++++++++++++++ include/fb_block.h | 105 ++++++++++++++ 2 files changed, 428 insertions(+) diff --git a/drivers/fastboot/fb_block.c b/drivers/fastboot/fb_block.c new file mode 100644 index 0000000000000000000000000000000000000000..b725397c91af2717812e69e2b624076eb30f781d --- /dev/null +++ b/drivers/fastboot/fb_block.c @@ -0,0 +1,323 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Copyright (C) 2024 The Android Open Source Project + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +/** + * FASTBOOT_MAX_BLOCKS_ERASE - maximum blocks to erase per derase call + * + * in the ERASE case we can have much larger buffer size since + * we're not transferring an actual buffer + */ +#define FASTBOOT_MAX_BLOCKS_ERASE 1048576 +/** + * FASTBOOT_MAX_BLOCKS_SOFT_ERASE - maximum blocks to software erase at once + */ +#define FASTBOOT_MAX_BLOCKS_SOFT_ERASE 4096 +/** + * FASTBOOT_MAX_BLOCKS_WRITE - maximum blocks to write per dwrite call + */ +#define FASTBOOT_MAX_BLOCKS_WRITE 65536 + +struct fb_block_sparse { + struct blk_desc *dev_desc; +}; + +/* Write 0s instead of using erase operation, inefficient but functional */ +static lbaint_t fb_block_soft_erase(struct blk_desc *block_dev, lbaint_t blk, + lbaint_t cur_blkcnt, lbaint_t erase_buf_blks, + void *erase_buffer) +{ + lbaint_t blks_written = 0; + int j; + + memset(erase_buffer, 0, erase_buf_blks * block_dev->blksz); + + for (j = 0; j < cur_blkcnt; j += erase_buf_blks) { + lbaint_t remain = min_t(lbaint_t, cur_blkcnt - j, + erase_buf_blks); + + blks_written += blk_dwrite(block_dev, blk + j, + remain, erase_buffer); + printf("."); + } + + return blks_written; +} + +static lbaint_t fb_block_write(struct blk_desc *block_dev, lbaint_t start, + lbaint_t blkcnt, const void *buffer) +{ + lbaint_t blk = start; + lbaint_t blks_written = 0; + lbaint_t cur_blkcnt = 0; + lbaint_t blks = 0; + void *erase_buf = NULL; + int erase_buf_blks = 0; + int step = buffer ? FASTBOOT_MAX_BLOCKS_WRITE : FASTBOOT_MAX_BLOCKS_ERASE; + int i; + + for (i = 0; i < blkcnt; i += step) { + cur_blkcnt = min((int)blkcnt - i, step); + if (buffer) { + if (fastboot_progress_callback) + fastboot_progress_callback("writing"); + blks_written = blk_dwrite(block_dev, blk, cur_blkcnt, + buffer + (i * block_dev->blksz)); + } else { + if (fastboot_progress_callback) + fastboot_progress_callback("erasing"); + + if (!erase_buf) { + blks_written = blk_derase(block_dev, blk, cur_blkcnt); + + /* Allocate erase buffer if erase is not implemented */ + if ((long)blks_written == -ENOSYS) { + erase_buf_blks = min_t(long, blkcnt, + FASTBOOT_MAX_BLOCKS_SOFT_ERASE); + erase_buf = malloc(erase_buf_blks * block_dev->blksz); + + printf("Slowly writing empty buffers due to missing erase operation\n"); + } + } + + if (erase_buf) + blks_written = fb_block_soft_erase(block_dev, blk, cur_blkcnt, + erase_buf_blks, erase_buf); + } + blk += blks_written; + blks += blks_written; + } + + if (erase_buf) + free(erase_buf); + + return blks; +} + +static lbaint_t fb_block_sparse_write(struct sparse_storage *info, + lbaint_t blk, lbaint_t blkcnt, + const void *buffer) +{ + struct fb_block_sparse *sparse = info->priv; + struct blk_desc *dev_desc = sparse->dev_desc; + + return fb_block_write(dev_desc, blk, blkcnt, buffer); +} + +static lbaint_t fb_block_sparse_reserve(struct sparse_storage *info, + lbaint_t blk, lbaint_t blkcnt) +{ + return blkcnt; +} + +int fastboot_block_get_part_info(const char *part_name, + struct blk_desc **dev_desc, + struct disk_partition *part_info, + char *response) +{ + int ret; + const char *interface = config_opt_enabled(CONFIG_FASTBOOT_FLASH_BLOCK, + CONFIG_FASTBOOT_FLASH_BLOCK_INTERFACE_NAME, + NULL); + const int device = config_opt_enabled(CONFIG_FASTBOOT_FLASH_BLOCK, + CONFIG_FASTBOOT_FLASH_BLOCK_DEVICE_ID, -1); + + if (!part_name || !strcmp(part_name, "")) { + fastboot_fail("partition not given", response); + return -ENOENT; + } + if (!interface || !strcmp(interface, "")) { + fastboot_fail("block interface isn't provided", response); + return -EINVAL; + } + + *dev_desc = blk_get_dev(interface, device); + if (!dev_desc) { + fastboot_fail("no such device", response); + return -ENODEV; + } + + ret = part_get_info_by_name(*dev_desc, part_name, part_info); + if (ret < 0) + fastboot_fail("failed to get partition info", response); + + return ret; +} + +void fastboot_block_raw_erase_disk(struct blk_desc *dev_desc, const char *disk_name, + char *response) +{ + lbaint_t written; + + debug("Start Erasing %s...\n", disk_name); + + written = fb_block_write(dev_desc, 0, dev_desc->lba, NULL); + if (written != dev_desc->lba) { + pr_err("Failed to erase %s\n", disk_name); + fastboot_response("FAIL", response, "Failed to erase %s", disk_name); + return; + } + + printf("........ erased " LBAFU " bytes from '%s'\n", + dev_desc->lba * dev_desc->blksz, disk_name); + fastboot_okay(NULL, response); +} + +void fastboot_block_raw_erase(struct blk_desc *dev_desc, struct disk_partition *info, + const char *part_name, uint alignment, char *response) +{ + lbaint_t written, blks_start, blks_size; + + if (alignment) { + blks_start = (info->start + alignment - 1) & ~(alignment - 1); + if (info->size >= alignment) + blks_size = (info->size - (blks_start - info->start)) & + (~(alignment - 1)); + else + blks_size = 0; + + printf("Erasing blocks " LBAFU " to " LBAFU " due to alignment\n", + blks_start, blks_start + blks_size); + } else { + blks_start = info->start; + blks_size = info->size; + } + + written = fb_block_write(dev_desc, blks_start, blks_size, NULL); + if (written != blks_size) { + fastboot_fail("failed to erase partition", response); + return; + } + + printf("........ erased " LBAFU " bytes from '%s'\n", + blks_size * info->blksz, part_name); + fastboot_okay(NULL, response); +} + +void fastboot_block_erase(const char *part_name, char *response) +{ + struct blk_desc *dev_desc; + struct disk_partition part_info; + + if (fastboot_block_get_part_info(part_name, &dev_desc, &part_info, response) < 0) + return; + + fastboot_block_raw_erase(dev_desc, &part_info, part_name, 0, response); +} + +void fastboot_block_write_raw_disk(struct blk_desc *dev_desc, const char *disk_name, + void *buffer, u32 download_bytes, char *response) +{ + lbaint_t blkcnt; + lbaint_t blks; + + /* determine number of blocks to write */ + blkcnt = ((download_bytes + (dev_desc->blksz - 1)) & ~(dev_desc->blksz - 1)); + blkcnt = lldiv(blkcnt, dev_desc->blksz); + + if (blkcnt > dev_desc->lba) { + pr_err("too large for disk: '%s'\n", disk_name); + fastboot_fail("too large for disk", response); + return; + } + + printf("Flashing Raw Image\n"); + + blks = fb_block_write(dev_desc, 0, blkcnt, buffer); + + if (blks != blkcnt) { + pr_err("failed writing to %s\n", disk_name); + fastboot_fail("failed writing to device", response); + return; + } + + printf("........ wrote " LBAFU " bytes to '%s'\n", blkcnt * dev_desc->blksz, + disk_name); + fastboot_okay(NULL, response); +} + +void fastboot_block_write_raw_image(struct blk_desc *dev_desc, + struct disk_partition *info, const char *part_name, + void *buffer, u32 download_bytes, char *response) +{ + lbaint_t blkcnt; + lbaint_t blks; + + /* determine number of blocks to write */ + blkcnt = ((download_bytes + (info->blksz - 1)) & ~(info->blksz - 1)); + blkcnt = lldiv(blkcnt, info->blksz); + + if (blkcnt > info->size) { + pr_err("too large for partition: '%s'\n", part_name); + fastboot_fail("too large for partition", response); + return; + } + + printf("Flashing Raw Image\n"); + + blks = fb_block_write(dev_desc, info->start, blkcnt, buffer); + + if (blks != blkcnt) { + pr_err("failed writing to device %d\n", dev_desc->devnum); + fastboot_fail("failed writing to device", response); + return; + } + + printf("........ wrote " LBAFU " bytes to '%s'\n", blkcnt * info->blksz, + part_name); + fastboot_okay(NULL, response); +} + +void fastboot_block_write_sparse_image(struct blk_desc *dev_desc, struct disk_partition *info, + const char *part_name, void *buffer, char *response) +{ + struct fb_block_sparse sparse_priv; + struct sparse_storage sparse; + int err; + + sparse_priv.dev_desc = dev_desc; + + sparse.blksz = info->blksz; + sparse.start = info->start; + sparse.size = info->size; + sparse.write = fb_block_sparse_write; + sparse.reserve = fb_block_sparse_reserve; + sparse.mssg = fastboot_fail; + + printf("Flashing sparse image at offset " LBAFU "\n", + sparse.start); + + sparse.priv = &sparse_priv; + err = write_sparse_image(&sparse, part_name, buffer, + response); + if (!err) + fastboot_okay(NULL, response); +} + +void fastboot_block_flash_write(const char *part_name, void *download_buffer, + u32 download_bytes, char *response) +{ + struct blk_desc *dev_desc; + struct disk_partition part_info; + + if (fastboot_block_get_part_info(part_name, &dev_desc, &part_info, response) < 0) + return; + + if (is_sparse_image(download_buffer)) { + fastboot_block_write_sparse_image(dev_desc, &part_info, part_name, + download_buffer, response); + } else { + fastboot_block_write_raw_image(dev_desc, &part_info, part_name, + download_buffer, download_bytes, response); + } +} diff --git a/include/fb_block.h b/include/fb_block.h new file mode 100644 index 0000000000000000000000000000000000000000..189c708e2f0eaccf4871f68f93c8d38eabc4349a --- /dev/null +++ b/include/fb_block.h @@ -0,0 +1,105 @@ +/* SPDX-License-Identifier: GPL-2.0+ */ +/* + * Copyright (C) 2024 The Android Open Source Project + */ + +#ifndef _FB_BLOCK_H_ +#define _FB_BLOCK_H_ + +struct blk_desc; +struct disk_partition; + +/** + * fastboot_block_get_part_info() - Lookup block partition by name + * + * @part_name: Named partition to lookup + * @dev_desc: Pointer to returned blk_desc pointer + * @part_info: Pointer to returned struct disk_partition + * @response: Pointer to fastboot response buffer + * Return: 0 on success, -ve on error + */ +int fastboot_block_get_part_info(const char *part_name, + struct blk_desc **dev_desc, + struct disk_partition *part_info, + char *response); + +/** + * fastboot_block_raw_erase() - Erase raw block device partition + * + * @dev_desc: Block device we're going write to + * @info: Partition we're going write to + * @part_name: Name of partition we're going write to + * @alignment: erase start and size alignment, specify 0 to ignore + * @response: Pointer to fastboot response buffer + */ +void fastboot_block_raw_erase(struct blk_desc *dev_desc, struct disk_partition *info, + const char *part_name, uint alignment, char *response); + +/** + * fastboot_block_raw_erase_disk() - Erase raw block device + * + * @dev_desc: Block device we're going write to + * @disk_name: Name of disk we're going write to + * @response: Pointer to fastboot response buffer + */ +void fastboot_block_raw_erase_disk(struct blk_desc *dev_desc, const char *disk_name, + char *response); + +/** + * fastboot_block_erase() - Erase partition on block device for fastboot + * + * @part_name: Named partition to erase + * @response: Pointer to fastboot response buffer + */ +void fastboot_block_erase(const char *part_name, char *response); + +/** + * fastboot_block_write_raw_disk() - Write raw image to block device + * + * @dev_desc: Block device we're going write to + * @disk_name: Name of disk we're going write to + * @buffer: Downloaded buffer pointer + * @download_bytes: Size of content on downloaded buffer pointer + * @response: Pointer to fastboot response buffer + */ +void fastboot_block_write_raw_disk(struct blk_desc *dev_desc, const char *disk_name, + void *buffer, u32 download_bytes, char *response); + +/** + * fastboot_block_write_raw_image() - Write raw image to block device partition + * + * @dev_desc: Block device we're going write to + * @info: Partition we're going write to + * @part_name: Name of partition we're going write to + * @buffer: Downloaded buffer pointer + * @download_bytes: Size of content on downloaded buffer pointer + * @response: Pointer to fastboot response buffer + */ +void fastboot_block_write_raw_image(struct blk_desc *dev_desc, + struct disk_partition *info, const char *part_name, + void *buffer, u32 download_bytes, char *response); + +/** + * fastboot_block_write_sparse_image() - Write sparse image to block device partition + * + * @dev_desc: Block device we're going write to + * @info: Partition we're going write to + * @part_name: Name of partition we're going write to + * @buffer: Downloaded buffer pointer + * @response: Pointer to fastboot response buffer + */ +void fastboot_block_write_sparse_image(struct blk_desc *dev_desc, struct disk_partition *info, + const char *part_name, void *buffer, char *response); + +/** + * fastboot_block_flash_write() - Write image to block device for fastboot + * + * @part_name: Named partition to write image to + * @download_buffer: Pointer to image data + * @download_bytes: Size of image data + * @response: Pointer to fastboot response buffer + */ +void fastboot_block_flash_write(const char *part_name, void *download_buffer, + u32 download_bytes, char *response); + +#endif // _FB_BLOCK_H_