From patchwork Wed Mar 6 18:59:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dmitrii Merkurev X-Patchwork-Id: 1908994 X-Patchwork-Delegate: sjg@chromium.org Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20230601 header.b=P6zcHD9M; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=patchwork.ozlabs.org) Received: from phobos.denx.de (phobos.denx.de [IPv6:2a01:238:438b:c500:173d:9f52:ddab:ee01]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4TqhZ34XSvz1yX0 for ; Thu, 7 Mar 2024 05:59:39 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id EBD2987BFD; Wed, 6 Mar 2024 19:59:32 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=reject dis=none) header.from=google.com 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=google.com header.i=@google.com header.b="P6zcHD9M"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 9647D87BFD; Wed, 6 Mar 2024 19:59:31 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-ed1-x549.google.com (mail-ed1-x549.google.com [IPv6:2a00:1450:4864:20::549]) (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 0CA708785E for ; Wed, 6 Mar 2024 19:59:29 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=3EL3oZQkKBmMEJNPSJOOZHPPHMF.DPNV-CPPUMJTUT.EFOY.EF@flex--dimorinny.bounces.google.com Received: by mail-ed1-x549.google.com with SMTP id 4fb4d7f45d1cf-564901924f4so24931a12.0 for ; Wed, 06 Mar 2024 10:59:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1709751568; x=1710356368; darn=lists.denx.de; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=ym7hzQHK/paNdElTro3XbWneGVWU5eG2Qxyxp/0JJ9c=; b=P6zcHD9MF0xwFjkjQiHhhT2LZvvS+0M9ethuaAkROepuirRrO/pIEmsC6xpkPgyLq1 qDD/Gc2vgGGrSxOilFmVCw5I5kUgnr/LG1ab54X82FvkDNpRPGzVfRgKDZHUmYq8KnIy PxpINoKyR6oF5i430WvN6+xExcrdZYIpI6KH/DlD2VvQTR97i8iZclsqReMe7OoxSDNE /aoK4Qc0LH/hvJwd99wS6agSfhWVKdLcj9tsYQgDap1zs6Xb7b7/UR3vLFoMv8vnMPEJ wxpxrnxwQLOGj1NivxOmh1c++fOMocMnEiK5e5WqCQKKcCe5GwsPKuDs71W0gzjFMAcQ cNHg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709751568; x=1710356368; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=ym7hzQHK/paNdElTro3XbWneGVWU5eG2Qxyxp/0JJ9c=; b=MQ2Rc2cPyLnSZZ/2Hz65xoNXTi1+i+zRw17mxGqUDM2hBvjAcnlkaKfy7MlK1EbXF8 uYgFnQVCEq24sZnkDUNckGkxfAOelUdjCopoa12sFAFuZDJJjK2m+p/bn76EDMBtcIvC lfdhMtzwpNADRekZOoYlYr6zxblcUHkTyFn4Mk3lO5W9L17+wTH0nz4hpgen7rlgJ8rQ 1U3XKUQJcJxcHXvOuXBam4hwHybrwNHPSxWf5Th0jBa2hhBNv/82czfmMcyN6+bdXsOk puz9jq/DFdrG7CigtYX4oqQN9iCZaBddj9QtUFWJlCoFmC/y/YC6EdV3a0Ow301OSoFK wAhQ== X-Gm-Message-State: AOJu0Yxs5RfrMIug35fAlRD2o8ErFD44h8SI6pJZNW425DfZpJWqB367 K7ia0elaa5VA2GdbnPJXIzn92eCYFHC8X1SjQSA4b2g9Kcgq6fxroGLpCcU26Bdz61jPLFtJtvx 67wKgWpB6RwSaR+4M5Q2KIuJ7N5sn3fr+KHbx0tm7lJOc4RmIDg2Zs7QywZ6Rx3JkyuUBZyLkdp Aw81x31yzCoPgVn6fnbk2Pj6qXX9E/72sNBFXcuXI5QmWuiw== X-Google-Smtp-Source: AGHT+IGKDSxzlY5IENTAdyINy58VySQcvWgAl+DYqtPrzH2RXc3SK7qZWXcODdZkI5i671A6HFQZpQ0GU50Gmmg= X-Received: from dimorinny0.lon.corp.google.com ([2a00:79e0:d:209:bf09:8cbc:464b:6c87]) (user=dimorinny job=sendgmr) by 2002:aa7:d34b:0:b0:567:a72f:f274 with SMTP id m11-20020aa7d34b000000b00567a72ff274mr16268edr.1.1709751568020; Wed, 06 Mar 2024 10:59:28 -0800 (PST) Date: Wed, 6 Mar 2024 18:59:18 +0000 Mime-Version: 1.0 X-Mailer: git-send-email 2.44.0.278.ge034bb2e1d-goog Message-ID: <20240306185921.1854109-1-dimorinny@google.com> Subject: [PATCH 1/4] virtio: blk: introduce virtio-block erase support From: Dmitrii Merkurev To: u-boot@lists.denx.de Cc: rammuthiah@google.com, Dmitrii Merkurev , Cody Schuffelen , Tuomas Tynkkynen , Simon Glass , Mattijs Korpershoek , Ying-Chun Liu 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 Co-developed-by: Cody Schuffelen Signed-off-by: Cody Schuffelen Signed-off-by: Dmitrii Merkurev Cc: Tuomas Tynkkynen Cc: Simon Glass Cc: Mattijs Korpershoek Cc: Ying-Chun Liu (PaulLiu) Tested-by: Mattijs Korpershoek # sandbox Tested-by: Mattijs Korpershoek # sandbox --- drivers/virtio/virtio_blk.c | 91 +++++++++++++++++++++++++++++++------ drivers/virtio/virtio_blk.h | 47 +++++++++++++++++++ 2 files changed, 124 insertions(+), 14 deletions(-) diff --git a/drivers/virtio/virtio_blk.c b/drivers/virtio/virtio_blk.c index 9581058286..225b65c4d1 100644 --- a/drivers/virtio/virtio_blk.c +++ b/drivers/virtio/virtio_blk.c @@ -19,30 +19,82 @@ struct virtio_blk_priv { struct virtqueue *vq; }; +static const u32 feature[] = { + VIRTIO_BLK_F_WRITE_ZEROES +}; + +static void virtio_blk_init_header_sg(struct udevice *dev, u64 sector, u32 type, + struct virtio_blk_outhdr *out_hdr, struct virtio_sg *sg) +{ + const bool sector_is_needed = type == VIRTIO_BLK_T_IN || + type == VIRTIO_BLK_T_OUT; + + out_hdr->type = cpu_to_virtio32(dev, type); + out_hdr->sector = cpu_to_virtio64(dev, sector_is_needed ? sector : 0); + + sg->addr = out_hdr; + sg->length = sizeof(*out_hdr); +} + +static void virtio_blk_init_write_zeroes_sg(struct udevice *dev, u64 sector, lbaint_t blkcnt, + struct virtio_blk_discard_write_zeroes *wz, + struct virtio_sg *sg) +{ + wz->sector = cpu_to_virtio64(dev, sector); + wz->num_sectors = cpu_to_virtio32(dev, blkcnt); + wz->flags = cpu_to_virtio32(dev, 0); + + sg->addr = wz; + sg->length = sizeof(*wz); +} + +static void virtio_blk_init_status_sg(u8 *status, struct virtio_sg *sg) +{ + sg->addr = status; + sg->length = sizeof(*status); +} + +static void virtio_blk_init_data_sg(void *buffer, lbaint_t blkcnt, struct virtio_sg *sg) +{ + sg->addr = buffer; + sg->length = blkcnt * 512; +} + static ulong virtio_blk_do_req(struct udevice *dev, u64 sector, lbaint_t blkcnt, void *buffer, u32 type) { struct virtio_blk_priv *priv = dev_get_priv(dev); + struct virtio_blk_outhdr out_hdr; + struct virtio_blk_discard_write_zeroes wz_hdr; unsigned int num_out = 0, num_in = 0; + struct virtio_sg hdr_sg, wz_sg, data_sg, status_sg; struct virtio_sg *sgs[3]; u8 status; int ret; - struct virtio_blk_outhdr out_hdr = { - .type = cpu_to_virtio32(dev, type), - .sector = cpu_to_virtio64(dev, sector), - }; - struct virtio_sg hdr_sg = { &out_hdr, sizeof(out_hdr) }; - struct virtio_sg data_sg = { buffer, blkcnt * 512 }; - struct virtio_sg status_sg = { &status, sizeof(status) }; - + virtio_blk_init_header_sg(dev, sector, type, &out_hdr, &hdr_sg); sgs[num_out++] = &hdr_sg; - if (type & VIRTIO_BLK_T_OUT) - sgs[num_out++] = &data_sg; - else - sgs[num_out + num_in++] = &data_sg; - + switch (type) { + case VIRTIO_BLK_T_IN: + case VIRTIO_BLK_T_OUT: + virtio_blk_init_data_sg(buffer, blkcnt, &data_sg); + if (type & VIRTIO_BLK_T_OUT) + sgs[num_out++] = &data_sg; + else + sgs[num_out + num_in++] = &data_sg; + break; + + case VIRTIO_BLK_T_WRITE_ZEROES: + virtio_blk_init_write_zeroes_sg(dev, sector, blkcnt, &wz_hdr, &wz_sg); + sgs[num_out++] = &wz_sg; + break; + + default: + return -EINVAL; + } + + virtio_blk_init_status_sg(&status, &status_sg); sgs[num_out + num_in++] = &status_sg; log_debug("dev=%s, active=%d, priv=%p, priv->vq=%p\n", dev->name, device_active(dev), priv, priv->vq); @@ -76,6 +128,15 @@ static ulong virtio_blk_write(struct udevice *dev, lbaint_t start, VIRTIO_BLK_T_OUT); } +static ulong virtio_blk_erase(struct udevice *dev, lbaint_t start, + lbaint_t blkcnt) +{ + if (!virtio_has_feature(dev, VIRTIO_BLK_F_WRITE_ZEROES)) + return -EOPNOTSUPP; + + return virtio_blk_do_req(dev, start, blkcnt, NULL, VIRTIO_BLK_T_WRITE_ZEROES); +} + static int virtio_blk_bind(struct udevice *dev) { struct virtio_dev_priv *uc_priv = dev_get_uclass_priv(dev->parent); @@ -105,7 +166,8 @@ static int virtio_blk_bind(struct udevice *dev) desc->bdev = dev; /* Indicate what driver features we support */ - virtio_driver_features_init(uc_priv, NULL, 0, NULL, 0); + virtio_driver_features_init(uc_priv, feature, ARRAY_SIZE(feature), + NULL, 0); return 0; } @@ -132,6 +194,7 @@ static int virtio_blk_probe(struct udevice *dev) static const struct blk_ops virtio_blk_ops = { .read = virtio_blk_read, .write = virtio_blk_write, + .erase = virtio_blk_erase, }; U_BOOT_DRIVER(virtio_blk) = { diff --git a/drivers/virtio/virtio_blk.h b/drivers/virtio/virtio_blk.h index 8d8e02fa2e..b37ba264df 100644 --- a/drivers/virtio/virtio_blk.h +++ b/drivers/virtio/virtio_blk.h @@ -17,6 +17,8 @@ #define VIRTIO_BLK_F_BLK_SIZE 6 /* Block size of disk is available */ #define VIRTIO_BLK_F_TOPOLOGY 10 /* Topology information is available */ #define VIRTIO_BLK_F_MQ 12 /* Support more than one vq */ +#define VIRTIO_BLK_F_DISCARD 13 /* Discard is supported */ +#define VIRTIO_BLK_F_WRITE_ZEROES 14 /* Write zeroes is supported */ /* Legacy feature bits */ #ifndef VIRTIO_BLK_NO_LEGACY @@ -65,6 +67,39 @@ struct __packed virtio_blk_config { /* number of vqs, only available when VIRTIO_BLK_F_MQ is set */ __u16 num_queues; + + /* the next 3 entries are guarded by VIRTIO_BLK_F_DISCARD */ + /* + * The maximum discard sectors (in 512-byte sectors) for + * one segment. + */ + __u32 max_discard_sectors; + /* + * The maximum number of discard segments in a + * discard command. + */ + __u32 max_discard_seg; + /* Discard commands must be aligned to this number of sectors. */ + __u32 discard_sector_alignment; + + /* the next 3 entries are guarded by VIRTIO_BLK_F_WRITE_ZEROES */ + /* + * The maximum number of write zeroes sectors (in 512-byte sectors) in + * one segment. + */ + __u32 max_write_zeroes_sectors; + /* + * The maximum number of segments in a write zeroes + * command. + */ + __u32 max_write_zeroes_seg; + /* + * Set if a VIRTIO_BLK_T_WRITE_ZEROES request may result in the + * deallocation of one or more of the sectors. + */ + __u8 write_zeroes_may_unmap; + + __u8 unused1[3]; }; /* @@ -93,6 +128,9 @@ struct __packed virtio_blk_config { /* Get device ID command */ #define VIRTIO_BLK_T_GET_ID 8 +/* Write zeroes command */ +#define VIRTIO_BLK_T_WRITE_ZEROES 13 + #ifndef VIRTIO_BLK_NO_LEGACY /* Barrier before this op */ #define VIRTIO_BLK_T_BARRIER 0x80000000 @@ -112,6 +150,15 @@ struct virtio_blk_outhdr { __virtio64 sector; }; +struct virtio_blk_discard_write_zeroes { + /* discard/write zeroes start sector */ + __virtio64 sector; + /* number of discard/write zeroes sectors */ + __virtio32 num_sectors; + /* flags for this range */ + __virtio32 flags; +}; + #ifndef VIRTIO_BLK_NO_LEGACY struct virtio_scsi_inhdr { __virtio32 errors; From patchwork Wed Mar 6 18:59:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dmitrii Merkurev X-Patchwork-Id: 1908995 X-Patchwork-Delegate: mkorpershoek@baylibre.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20230601 header.b=QcaO8Xpd; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=85.214.62.61; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=patchwork.ozlabs.org) Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4TqhZR1Jwdz1yX0 for ; Thu, 7 Mar 2024 05:59:59 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 6859B87DE6; Wed, 6 Mar 2024 19:59:42 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=reject dis=none) header.from=google.com 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=google.com header.i=@google.com header.b="QcaO8Xpd"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id C1DF38792D; Wed, 6 Mar 2024 19:59:39 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-ej1-x649.google.com (mail-ej1-x649.google.com [IPv6:2a00:1450:4864:20::649]) (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 8E9FA87C0A for ; Wed, 6 Mar 2024 19:59:31 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=3Er3oZQkKBmUGLPRULQQbJRRJOH.FRPX-ERRWOLVWV.GHQa.GH@flex--dimorinny.bounces.google.com Received: by mail-ej1-x649.google.com with SMTP id a640c23a62f3a-a4531df8727so3737166b.2 for ; Wed, 06 Mar 2024 10:59:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1709751571; x=1710356371; darn=lists.denx.de; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=IkJBlRHr1MnBJ8BFY/COQb8zlbQgTu1+SK+kF9BShbo=; b=QcaO8Xpdwez2Z7me9i/eUrrJQxhiMejNSTU4r8QnOAhs8zDhL2NCpAMeJ2OA5v2vFx mjHn8gB/beomfbuDoNtui3meniFjOwefm+s7FaTm94lotAzX74FQWCECrxkC5G+UYtTH ZdURIiFgdEcgelSpqe1aJfSNUQQJ3nWrbItewBFQ9/FhjsWVEPmTcnSJfbmwTNMLmJT2 wvH/iRa4AzE3k+tIIOiMnkj/2d5aIRz5K4r6vOAkUoDMy0sb3U9U+PBfuxOraMCtZjiS 32oarRxpLhoOmtYr5qvWZOhfjzho4FhoLhqM5fmhUx5p+sKjqsLEOt0vp1DNIn7NTySm OoIQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709751571; x=1710356371; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=IkJBlRHr1MnBJ8BFY/COQb8zlbQgTu1+SK+kF9BShbo=; b=WD1MbUiu079TDnx5YHflY5o96YzPEDDfgHtK82u2CNosMvIbpuwC1Xj39VPrWhuyxf ztG7xUSMDWV3AoO5CJpCxYkfFke4LmW+1IeQvyjZ2Z44DBDyJi0mostZnUB19oVzYfGx vuqPVIgaLXG9n2I4uXs2gnq69drakQp/l/nAfu54MYpHOFOicwLSh8WCEMuAaWY1hmHC fGav5mOFWzKcvOelyBvplM/myRR11mqU0Xd5eHkVnWmFxu5NQET2ZktuwfMVIFd7LUAK giTOPkuUvrj+YDcEed7LM9ZEjuf3Pol0bQDon7QwNgxOHPmS/8V+NptsKuwftxm1Nh7a vvbg== X-Gm-Message-State: AOJu0Yw+qsUIZIdbm7GjE3WXSs9TqcDQYayVYi7iDMexhymU3DxdbNRw C6sgNZ4XsHBXxfDwCa4WO5ViyVULBJp8asqV0/i3CKAY57X8h1L2CzcOL+Mzict01jYkB96LgRF y2PjXzNj8BXptKqt001Tf1hz89foAtdZ2yV48/HXSbijl4JDfraFYd+0e9ZxmYSLMmUA7J2Y7gJ x3iuMWVvMVtCYBXEXxzq1fwDSTs9CepV7oeR9aU8SES8xXZw== X-Google-Smtp-Source: AGHT+IE07GdR+B/oq34H4obqdjlJkegH3KW0wqAnBohFm/9HCX/dzrQH3TsEs2bxX3cw/05+81OkdVrIdCY4vT4= X-Received: from dimorinny0.lon.corp.google.com ([2a00:79e0:d:209:bf09:8cbc:464b:6c87]) (user=dimorinny job=sendgmr) by 2002:a17:906:6a0c:b0:a3f:be13:e2c8 with SMTP id qw12-20020a1709066a0c00b00a3fbe13e2c8mr49409ejc.11.1709751570604; Wed, 06 Mar 2024 10:59:30 -0800 (PST) Date: Wed, 6 Mar 2024 18:59:19 +0000 In-Reply-To: <20240306185921.1854109-1-dimorinny@google.com> Mime-Version: 1.0 References: <20240306185921.1854109-1-dimorinny@google.com> X-Mailer: git-send-email 2.44.0.278.ge034bb2e1d-goog Message-ID: <20240306185921.1854109-2-dimorinny@google.com> Subject: [PATCH 2/4] fastboot: blk: add block device flashing configuration From: Dmitrii Merkurev To: u-boot@lists.denx.de Cc: rammuthiah@google.com, Dmitrii Merkurev , Patrick Delaunay , Simon Glass , Mattijs Korpershoek , Ying-Chun Liu 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 Signed-off-by: Dmitrii Merkurev Cc: Patrick Delaunay Cc: Simon Glass Cc: Mattijs Korpershoek Cc: Ying-Chun Liu (PaulLiu) --- drivers/fastboot/Kconfig | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/drivers/fastboot/Kconfig b/drivers/fastboot/Kconfig index 5e5855a76c..460c5e98d7 100644 --- a/drivers/fastboot/Kconfig +++ b/drivers/fastboot/Kconfig @@ -87,7 +87,7 @@ config FASTBOOT_USB_DEV config FASTBOOT_FLASH bool "Enable FASTBOOT FLASH command" default y if ARCH_SUNXI || ARCH_ROCKCHIP - depends on MMC || (MTD_RAW_NAND && CMD_MTDPARTS) + depends on MMC || (MTD_RAW_NAND && CMD_MTDPARTS) || BLK select IMAGE_SPARSE help The fastboot protocol includes a "flash" command for writing @@ -109,12 +109,16 @@ choice config FASTBOOT_FLASH_MMC bool "FASTBOOT on MMC" - depends on MMC + depends on MMC && BLK config FASTBOOT_FLASH_NAND bool "FASTBOOT on NAND" depends on MTD_RAW_NAND && CMD_MTDPARTS +config FASTBOOT_FLASH_BLOCK + bool "FASTBOOT on block device" + depends on BLK + endchoice config FASTBOOT_FLASH_MMC_DEV @@ -189,6 +193,18 @@ config FASTBOOT_MMC_USER_NAME defined here. The default target name for erasing EMMC_USER is "mmc0". +config FASTBOOT_FLASH_BLOCK_INTERFACE_NAME + string "Define FASTBOOT block interface name" + depends on FASTBOOT_FLASH_BLOCK + help + "Fastboot block interface name (mmc, virtio, etc)" + +config FASTBOOT_FLASH_BLOCK_DEVICE_ID + int "Define FASTBOOT block device id" + depends on FASTBOOT_FLASH_BLOCK + help + "Fastboot block device id" + config FASTBOOT_GPT_NAME string "Target name for updating GPT" depends on FASTBOOT_FLASH_MMC && EFI_PARTITION From patchwork Wed Mar 6 18:59:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dmitrii Merkurev X-Patchwork-Id: 1908996 X-Patchwork-Delegate: mkorpershoek@baylibre.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20230601 header.b=Cg327y4z; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=patchwork.ozlabs.org) Received: from phobos.denx.de (phobos.denx.de [IPv6:2a01:238:438b:c500:173d:9f52:ddab:ee01]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4TqhZt0lVnz1yWw for ; Thu, 7 Mar 2024 06:00:22 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 6219387BA8; Wed, 6 Mar 2024 19:59:54 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=reject dis=none) header.from=google.com 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=google.com header.i=@google.com header.b="Cg327y4z"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 2CC1F87DA1; Wed, 6 Mar 2024 19:59:44 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) (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 C848587BA8 for ; Wed, 6 Mar 2024 19:59:34 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=3Fb3oZQkKBmgJOSUXOTTeMUUMRK.IUSa-HUUZROYZY.JKTd.JK@flex--dimorinny.bounces.google.com Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-dd0ae66422fso273365276.0 for ; Wed, 06 Mar 2024 10:59:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1709751573; x=1710356373; darn=lists.denx.de; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=RIfaiBm3WO+2nkue07djDti7gI+pUNt6ILryeQaBBxo=; b=Cg327y4zqj102C0REHJyyCC3pYciIzDZj9MOB6553QJBB0ghJ2EJEjRENa9TGfY1P9 a/jGsSt0wJlphfoPes5VBiVgBaOYODohckp6hw9jrRKO5hsmvTSB5/XLfd8kGiZ/nCc3 Ap9WogdIx9Ed+fRDNvdFzIsexDmWvCG2CLkKEC7HU5+vV0qF7Vr2b2Qiv1RD050iOh// zXqUU/7EYGSBw7qPumHTt85jSszDy0fzNIkoXjSaJQRBoJigbTuAZrLJ1LdL+g74I0sk 4gCpVC/jzq90piV9OS72x20VVZp2XwlugQj+iTYRZXHYPRH1QhVyCHCRt+IJwnM+x6Kn sg4A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709751573; x=1710356373; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=RIfaiBm3WO+2nkue07djDti7gI+pUNt6ILryeQaBBxo=; b=rT1v3+zAsHJ6okCPCbaLX3eH9Z9WvRYN+2TxloH25clFWnmgFcLUyGz7vA+7G2o7l7 YwlKANrbP2SfKPklZ2OXgtfHuhlXi6vdb8bQD+h1/97/+5nvoUCSyfPnkgFrWAPwbb7G wnjjIdxR8nQXo6R4wA3yXidLSqK+b2saOjnVSFakiuu1NkbDZYhfC0/r7pjbRegqNphL LrbF/oEOLoGxIFxQPGA1SEm1Bfvo0ux8Iidv+a9bnW6aj8j85wD8bVBkEv+Of8R4RVa1 4m9du46Meks2318wlTN6fYZW/VV5RHLA+g6wL/zFr/aTiYCY+npuHEgWCcUjbVuO62Hk KigA== X-Gm-Message-State: AOJu0YwxEwLWvyrCLi4x0UjnX5qKENWB9nnAaVjnkuzRcmQtnFaOWTv2 5VBROJP/wWFe6VQAT7QYBJ52XjrDnHHcHlHJ4e3BtOKL30QuvxZL2VXGuTeCeMLW3fybJapJAHw dWYIHKVTabWQKcQk9sUcVd3JhUgXyVUw044Mn55dKFRzr0Lkpyklnwz7okVNg1uUgbej7fyJerZ J+Z6Sl7yl9gOJo/jWF9tMyH1GO4wfxfgKbIyGnF9AaxkXSwQ== X-Google-Smtp-Source: AGHT+IGRmR15C14ZqfIqY/0ecrgi98j/F9bv9KwQWrj0P/fylaTlwg1gv7I53OmzW+iXJ6sTVCeHBIuinAojeA0= X-Received: from dimorinny0.lon.corp.google.com ([2a00:79e0:d:209:bf09:8cbc:464b:6c87]) (user=dimorinny job=sendgmr) by 2002:a25:690d:0:b0:dcc:5463:49a8 with SMTP id e13-20020a25690d000000b00dcc546349a8mr1263012ybc.6.1709751573462; Wed, 06 Mar 2024 10:59:33 -0800 (PST) Date: Wed, 6 Mar 2024 18:59:20 +0000 In-Reply-To: <20240306185921.1854109-1-dimorinny@google.com> Mime-Version: 1.0 References: <20240306185921.1854109-1-dimorinny@google.com> X-Mailer: git-send-email 2.44.0.278.ge034bb2e1d-goog Message-ID: <20240306185921.1854109-3-dimorinny@google.com> Subject: [PATCH 3/4] fastboot: blk: introduce fastboot block flashing support From: Dmitrii Merkurev To: u-boot@lists.denx.de Cc: rammuthiah@google.com, Dmitrii Merkurev , Alex Kiernan , Patrick Delaunay , Simon Glass , Mattijs Korpershoek , Ying-Chun Liu 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 Reuse common logic between existing mmc and new introduced block implementation Signed-off-by: Dmitrii Merkurev Cc: Alex Kiernan Cc: Patrick Delaunay Cc: Simon Glass Cc: Mattijs Korpershoek Cc: Ying-Chun Liu (PaulLiu) --- drivers/fastboot/Makefile | 4 +- drivers/fastboot/fb_block.c | 200 ++++++++++++++++++++++++++++++++++++ drivers/fastboot/fb_mmc.c | 120 ++-------------------- include/fb_block.h | 70 +++++++++++++ 4 files changed, 281 insertions(+), 113 deletions(-) create mode 100644 drivers/fastboot/fb_block.c create mode 100644 include/fb_block.h diff --git a/drivers/fastboot/Makefile b/drivers/fastboot/Makefile index 048af5aa82..91e98763e8 100644 --- a/drivers/fastboot/Makefile +++ b/drivers/fastboot/Makefile @@ -3,5 +3,7 @@ obj-y += fb_common.o obj-y += fb_getvar.o obj-y += fb_command.o -obj-$(CONFIG_FASTBOOT_FLASH_MMC) += fb_mmc.o +obj-$(CONFIG_FASTBOOT_FLASH_BLOCK) += fb_block.o +# MMC reuses block implementation +obj-$(CONFIG_FASTBOOT_FLASH_MMC) += fb_block.o fb_mmc.o obj-$(CONFIG_FASTBOOT_FLASH_NAND) += fb_nand.o diff --git a/drivers/fastboot/fb_block.c b/drivers/fastboot/fb_block.c new file mode 100644 index 0000000000..908decd544 --- /dev/null +++ b/drivers/fastboot/fb_block.c @@ -0,0 +1,200 @@ +// SPDX-License-Identifier: BSD-2-Clause +/* + * Copyright (C) 2024 The Android Open Source Project + */ + +#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_WRITE - maximum blocks to write per dwrite call + */ +#define FASTBOOT_MAX_BLOCKS_WRITE 65536 + +struct fb_block_sparse { + struct blk_desc *dev_desc; +}; + +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; + 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"); + blks_written = blk_derase(block_dev, blk, cur_blkcnt); + } + blk += blks_written; + blks += blks_written; + } + 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_erase(const char *part_name, char *response) +{ + struct blk_desc *dev_desc; + struct disk_partition part_info; + lbaint_t written; + + if (fastboot_block_get_part_info(part_name, &dev_desc, &part_info, response) < 0) + return; + + written = fb_block_write(dev_desc, part_info.start, part_info.size, NULL); + if (written != part_info.size) { + fastboot_fail("failed to erase partition", response); + return; + } + + 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; + } + + puts("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/drivers/fastboot/fb_mmc.c b/drivers/fastboot/fb_mmc.c index 060918e491..7894f01f63 100644 --- a/drivers/fastboot/fb_mmc.c +++ b/drivers/fastboot/fb_mmc.c @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -21,10 +22,6 @@ #define BOOT_PARTITION_NAME "boot" -struct fb_mmc_sparse { - struct blk_desc *dev_desc; -}; - static int raw_part_get_info_by_name(struct blk_desc *dev_desc, const char *name, struct disk_partition *info) @@ -115,88 +112,6 @@ static int part_get_info_by_name_or_alias(struct blk_desc **dev_desc, return do_get_part_info(dev_desc, name, info); } -/** - * fb_mmc_blk_write() - Write/erase MMC in chunks of FASTBOOT_MAX_BLK_WRITE - * - * @block_dev: Pointer to block device - * @start: First block to write/erase - * @blkcnt: Count of blocks - * @buffer: Pointer to data buffer for write or NULL for erase - */ -static lbaint_t fb_mmc_blk_write(struct blk_desc *block_dev, lbaint_t start, - lbaint_t blkcnt, const void *buffer) -{ - lbaint_t blk = start; - lbaint_t blks_written; - lbaint_t cur_blkcnt; - lbaint_t blks = 0; - int i; - - for (i = 0; i < blkcnt; i += FASTBOOT_MAX_BLK_WRITE) { - cur_blkcnt = min((int)blkcnt - i, FASTBOOT_MAX_BLK_WRITE); - 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"); - blks_written = blk_derase(block_dev, blk, cur_blkcnt); - } - blk += blks_written; - blks += blks_written; - } - return blks; -} - -static lbaint_t fb_mmc_sparse_write(struct sparse_storage *info, - lbaint_t blk, lbaint_t blkcnt, const void *buffer) -{ - struct fb_mmc_sparse *sparse = info->priv; - struct blk_desc *dev_desc = sparse->dev_desc; - - return fb_mmc_blk_write(dev_desc, blk, blkcnt, buffer); -} - -static lbaint_t fb_mmc_sparse_reserve(struct sparse_storage *info, - lbaint_t blk, lbaint_t blkcnt) -{ - return blkcnt; -} - -static void 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; - } - - puts("Flashing Raw Image\n"); - - blks = fb_mmc_blk_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); -} - #if defined(CONFIG_FASTBOOT_MMC_BOOT_SUPPORT) || \ defined(CONFIG_FASTBOOT_MMC_USER_SUPPORT) static int fb_mmc_erase_mmc_hwpart(struct blk_desc *dev_desc) @@ -205,7 +120,7 @@ static int fb_mmc_erase_mmc_hwpart(struct blk_desc *dev_desc) debug("Start Erasing mmc hwpart[%u]...\n", dev_desc->hwpart); - blks = fb_mmc_blk_write(dev_desc, 0, dev_desc->lba, NULL); + blks = fb_block_write(dev_desc, 0, dev_desc->lba, NULL); if (blks != dev_desc->lba) { pr_err("Failed to erase mmc hwpart[%u]\n", dev_desc->hwpart); @@ -249,7 +164,7 @@ static void fb_mmc_boot_ops(struct blk_desc *dev_desc, void *buffer, debug("Start Flashing Image to EMMC_BOOT%d...\n", hwpart); - blks = fb_mmc_blk_write(dev_desc, 0, blkcnt, buffer); + blks = fb_block_write(dev_desc, 0, blkcnt, buffer); if (blks != blkcnt) { pr_err("Failed to write EMMC_BOOT%d\n", hwpart); @@ -610,30 +525,11 @@ void fastboot_mmc_flash_write(const char *cmd, void *download_buffer, return; if (is_sparse_image(download_buffer)) { - struct fb_mmc_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_mmc_sparse_write; - sparse.reserve = fb_mmc_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, cmd, download_buffer, - response); - if (!err) - fastboot_okay(NULL, response); + fastboot_block_write_sparse_image(dev_desc, &info, cmd, + download_buffer, response); } else { - write_raw_image(dev_desc, &info, cmd, download_buffer, - download_bytes, response); + fastboot_block_write_raw_image(dev_desc, &info, cmd, download_buffer, + download_bytes, response); } } @@ -697,7 +593,7 @@ void fastboot_mmc_erase(const char *cmd, char *response) printf("Erasing blocks " LBAFU " to " LBAFU " due to alignment\n", blks_start, blks_start + blks_size); - blks = fb_mmc_blk_write(dev_desc, blks_start, blks_size, NULL); + blks = fb_block_write(dev_desc, blks_start, blks_size, NULL); if (blks != blks_size) { pr_err("failed erasing from device %d\n", dev_desc->devnum); diff --git a/include/fb_block.h b/include/fb_block.h new file mode 100644 index 0000000000..90208e21a7 --- /dev/null +++ b/include/fb_block.h @@ -0,0 +1,70 @@ +/* SPDX-License-Identifier: BSD-2-Clause */ +/* + * 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 + */ +int fastboot_block_get_part_info(const char *part_name, + struct blk_desc **dev_desc, + struct disk_partition *part_info, + 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_image() - Write raw image to block device + * + * @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 + * + * @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_ From patchwork Wed Mar 6 18:59:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dmitrii Merkurev X-Patchwork-Id: 1908997 X-Patchwork-Delegate: mkorpershoek@baylibre.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20230601 header.b=bMkbc5mG; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=patchwork.ozlabs.org) Received: from phobos.denx.de (phobos.denx.de [IPv6:2a01:238:438b:c500:173d:9f52:ddab:ee01]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4TqhbC2qXxz1yWw for ; Thu, 7 Mar 2024 06:00:39 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id E4D5587D9D; Wed, 6 Mar 2024 19:59:56 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=reject dis=none) header.from=google.com 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=google.com header.i=@google.com header.b="bMkbc5mG"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id DB2D686D38; Wed, 6 Mar 2024 19:59:50 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-ed1-x54a.google.com (mail-ed1-x54a.google.com [IPv6:2a00:1450:4864:20::54a]) (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 3275487DA7 for ; Wed, 6 Mar 2024 19:59:38 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=3F73oZQkKBmoLQUWZQVVgOWWOTM.KWUc-JWWbTQaba.LMVf.LM@flex--dimorinny.bounces.google.com Received: by mail-ed1-x54a.google.com with SMTP id 4fb4d7f45d1cf-564901924f4so24952a12.0 for ; Wed, 06 Mar 2024 10:59:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1709751576; x=1710356376; darn=lists.denx.de; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=JLg0wWtrW/tIqSJgysEmUyHuXR8cZzTRkKM96ER484s=; b=bMkbc5mGCkKEJf5/iWWcULQOqNvYe3f4EmurHEddX1SKHummLSESNc9H+ndQgOKUy/ BkfIWOJ81QvuG9CP3o0wxSOf5pmECazNSxSo1kntPLbjaR8SbyVPIHdZwOiJ2sxG9O+6 Cn7yBKQmTiEOkqhMkmK0k/+E/BEyyFdPINtWIh+zcQGKKcyjSrODB5m1TICrxo24Dev2 2V5zLHz1y8jC+WOsURoCeCKh9IeKxOBZr9ckqLQRMRpxy3Vu2lZEesFm8wvmclPjNm1X +f/SBEUTmXnBssTVh9ADUaAs9VgsBxjC+Y71PjdVDSv0z1fpAYSuCpxehWApT7e2TC8h TwaQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709751576; x=1710356376; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=JLg0wWtrW/tIqSJgysEmUyHuXR8cZzTRkKM96ER484s=; b=QLnp/7dCM8VhsoCDHD/DFVwTIqCxFiD/Oxa+/6I/5Rd3xV3eJhfMtzap2N9/wmDkEZ BkDTUQGWk0fnPkoXFcStR8sHy2zuj93TXCG+nPHRjBySzoIv19wagFgTX9n5FAfdInZh ducIX0HFLOQHwt1522OXzNcgQ+GPuK002nWPrHfMArRF9OUHfxDhxaloFxa3ovWmo7ca bYf0zkPxr9YKU90J6OMwWr2pBbZUmMz6hgD1fvj5vgAaVGvWJQ5vEmCSrL3hO0tfW54M 6wM6dTsaHmQWDVCyhbOLmVnuAfJGsLccZJM9YuPUOCY/7BYyan4ZbrehR1SoIo2YpcnF 9+jg== X-Gm-Message-State: AOJu0YwADsUs7R8Fg2gseY1bn9gVcqIwUsnScVWy9QCFraC3zf7t/Rb9 ATt0lM3EUSGKdloQlgyZz6XUfEp6DKMNhUPpKRULdVd67KGKVOF9+HVQaYT3A394vPSffTUCxnd bNWcCyQ24XLFYU9VuYG2lA9hbyemHqIStX+goDZmiqs8Bowz67zfXkQbPmt5BK7y25xTaD0e9Rc GZKzTgtvWXXC+NCzWWSxd3swcoieY128cdoCL5HXuzx+F9qw== X-Google-Smtp-Source: AGHT+IEyMz8cUs1Ot7+3BR58qqZJZ0gguKQ8TG45zyyQiE+L7DjToJBWoVzMih4d2xOXPDvrObEVCiSSC8yWW5I= X-Received: from dimorinny0.lon.corp.google.com ([2a00:79e0:d:209:bf09:8cbc:464b:6c87]) (user=dimorinny job=sendgmr) by 2002:a05:6402:121a:b0:566:77b7:ed46 with SMTP id c26-20020a056402121a00b0056677b7ed46mr54807edw.5.1709751575883; Wed, 06 Mar 2024 10:59:35 -0800 (PST) Date: Wed, 6 Mar 2024 18:59:21 +0000 In-Reply-To: <20240306185921.1854109-1-dimorinny@google.com> Mime-Version: 1.0 References: <20240306185921.1854109-1-dimorinny@google.com> X-Mailer: git-send-email 2.44.0.278.ge034bb2e1d-goog Message-ID: <20240306185921.1854109-4-dimorinny@google.com> Subject: [PATCH 4/4] fastboot: integrate block flashing back-end From: Dmitrii Merkurev To: u-boot@lists.denx.de Cc: rammuthiah@google.com, Dmitrii Merkurev , Alex Kiernan , Patrick Delaunay , Simon Glass , Mattijs Korpershoek , Ying-Chun Liu 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 1. Get partition info/size 2. Erase partition 3. Flash partition 4. BCB Signed-off-by: Dmitrii Merkurev Cc: Alex Kiernan Cc: Patrick Delaunay Cc: Simon Glass Cc: Mattijs Korpershoek Cc: Ying-Chun Liu (PaulLiu) Reviewed-by: Mattijs Korpershoek --- drivers/fastboot/fb_command.c | 8 ++++++++ drivers/fastboot/fb_common.c | 15 +++++++++++---- drivers/fastboot/fb_getvar.c | 8 +++++++- 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/drivers/fastboot/fb_command.c b/drivers/fastboot/fb_command.c index f95f4e4ae1..67ebe02efa 100644 --- a/drivers/fastboot/fb_command.c +++ b/drivers/fastboot/fb_command.c @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -331,6 +332,10 @@ void fastboot_data_complete(char *response) */ static void __maybe_unused flash(char *cmd_parameter, char *response) { + if (IS_ENABLED(CONFIG_FASTBOOT_FLASH_BLOCK)) + fastboot_block_flash_write(cmd_parameter, fastboot_buf_addr, + image_size, response); + if (IS_ENABLED(CONFIG_FASTBOOT_FLASH_MMC)) fastboot_mmc_flash_write(cmd_parameter, fastboot_buf_addr, image_size, response); @@ -351,6 +356,9 @@ static void __maybe_unused flash(char *cmd_parameter, char *response) */ static void __maybe_unused erase(char *cmd_parameter, char *response) { + if (IS_ENABLED(CONFIG_FASTBOOT_FLASH_BLOCK)) + fastboot_block_erase(cmd_parameter, response); + if (IS_ENABLED(CONFIG_FASTBOOT_FLASH_MMC)) fastboot_mmc_erase(cmd_parameter, response); diff --git a/drivers/fastboot/fb_common.c b/drivers/fastboot/fb_common.c index 3576b06772..7602653c0b 100644 --- a/drivers/fastboot/fb_common.c +++ b/drivers/fastboot/fb_common.c @@ -97,16 +97,23 @@ int __weak fastboot_set_reboot_flag(enum fastboot_reboot_reason reason) [FASTBOOT_REBOOT_REASON_FASTBOOTD] = "boot-fastboot", [FASTBOOT_REBOOT_REASON_RECOVERY] = "boot-recovery" }; - const int mmc_dev = config_opt_enabled(CONFIG_FASTBOOT_FLASH_MMC, - CONFIG_FASTBOOT_FLASH_MMC_DEV, -1); - if (!IS_ENABLED(CONFIG_FASTBOOT_FLASH_MMC)) + int device = config_opt_enabled(CONFIG_FASTBOOT_FLASH_BLOCK, + CONFIG_FASTBOOT_FLASH_BLOCK_DEVICE_ID, -1); + if (device == -1) { + device = config_opt_enabled(CONFIG_FASTBOOT_FLASH_MMC, + CONFIG_FASTBOOT_FLASH_MMC_DEV, -1); + } + char *bcb_iface = config_opt_enabled(CONFIG_FASTBOOT_FLASH_BLOCK, + CONFIG_FASTBOOT_FLASH_BLOCK_INTERFACE_NAME, "mmc"); + + if (device == -1) return -EINVAL; if (reason >= FASTBOOT_REBOOT_REASONS_COUNT) return -EINVAL; - ret = bcb_find_partition_and_load("mmc", mmc_dev, "misc"); + ret = bcb_find_partition_and_load(bcb_iface, device, "misc"); if (ret) goto out; diff --git a/drivers/fastboot/fb_getvar.c b/drivers/fastboot/fb_getvar.c index f65519c57b..71507009ab 100644 --- a/drivers/fastboot/fb_getvar.c +++ b/drivers/fastboot/fb_getvar.c @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -114,7 +115,12 @@ static int getvar_get_part_info(const char *part_name, char *response, struct disk_partition disk_part; struct part_info *part_info; - if (IS_ENABLED(CONFIG_FASTBOOT_FLASH_MMC)) { + if (IS_ENABLED(CONFIG_FASTBOOT_FLASH_BLOCK)) { + r = fastboot_block_get_part_info(part_name, &dev_desc, &disk_part, + response); + if (r >= 0 && size) + *size = disk_part.size * disk_part.blksz; + } else if (IS_ENABLED(CONFIG_FASTBOOT_FLASH_MMC)) { r = fastboot_mmc_get_part_info(part_name, &dev_desc, &disk_part, response); if (r >= 0 && size)