From patchwork Fri Jan 15 09:57:18 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Hughes X-Patchwork-Id: 1426849 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.helo=coreboot.org (client-ip=78.46.105.101; helo=coreboot.org; envelope-from=flashrom-bounces@flashrom.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20161025 header.b=MI/lrvPr; dkim-atps=neutral Received: from coreboot.org (coreboot.org [78.46.105.101]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4DHGp024Ctz9sSC for ; Fri, 15 Jan 2021 20:57:58 +1100 (AEDT) Received: from authenticated-user (PRIMARY_HOSTNAME [PUBLIC_IP]) by coreboot.org (Postfix) with ESMTPA id 6005110C0C5E; Fri, 15 Jan 2021 09:57:47 +0000 (UTC) Received: from authenticated-user (PRIMARY_HOSTNAME [PUBLIC_IP]) by coreboot.org (Postfix) with ESMTP id 451AD10C0C46 for ; Fri, 15 Jan 2021 09:57:30 +0000 (UTC) Received: from authenticated-user (PRIMARY_HOSTNAME [PUBLIC_IP]) for ; Fri, 15 Jan 2021 01:57:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=u+9hihlvOPaod4XScsSIwVPh9zP0VrJfcMJNvsViTuc=; b=MI/lrvPrWFkmIEXmxBhNxNaoPuCoQObdQR9t7vT22hSprBhJgyE2FDkC6TxbCeKLtm 9tlpRCyIV0xXfzcUij+2b4pLGQb7IT2IBZ4BJ7NFLwYRi/TUM+X2NtKrkX0gPaZH5+YD R5IrI2HHhoDYXMJnVxZk8tnO6vwyBjuHdxAb3FyyoXfJLId9rAVFVPrW9Ri+543iJfL8 Y2pZWex08CAwCKq8Y5ORrrX3sr6U2LQxbPUaJj8asbkJJRHHxiFGT/QwtnbVZKD0zWnc /cMEA/j/66n8IMR0yeDpcn5W0BSWuBuO4LseyGkf9InC+pzOf31YD049uA6HXYOkX+Dc A/kw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=u+9hihlvOPaod4XScsSIwVPh9zP0VrJfcMJNvsViTuc=; b=SjS4/q2dYnPBbmtCg8xRkL6UCWmmGqA8yDJcT+Jdgxqd6C2pOTWwd9YxGzlSRURgk0 In7i8VS+Y+z++7styKsw6gOY8GTjlLOUIWzFNTjJIm2gTmMedFzBQDTis3MizM4zgBG2 mpcygKwnzzrqTJTjBQfYoU/nUluDTm6T1pOvmIMReudkrkhBJK5sByyfjeVy74bAcnlM 7cLU5qFx+6n2+AjZ/I5ktPdK8T+iuhTUVZccPhYl5PrwwO73mmpdtAnoDViTqDmMozTL G+4Ov/IbUG3xrpu57Asov/KrqW2GGqgYdZcuU8nREtQDF8TrO+PPXDV1RRdRMq8laNPx N5Ww== X-Gm-Message-State: AOAM533NyXPsHHUYgVwaytvYrgEl0FzBZaTXu6PLc7JOvFy6u2P7hX+y ncftUJVrx263rJDHEv8avEOGgRlRv37YKgpVvGI= X-Google-Smtp-Source: ABdhPJzXJN0fNg4DUHlC+iw9kiU19ZiVBdU0HYyDe9ECCT3OoTLofATS3S24gv8Oi7fKnucpAy3xCuWSZb6jgwWF1LU= X-Received: by 2002:a2e:2284:: with SMTP id i126mr5206902lji.93.1610704649586; Fri, 15 Jan 2021 01:57:29 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: Richard Hughes Date: Fri, 15 Jan 2021 09:57:18 +0000 Message-ID: To: David Hendricks Message-ID-Hash: MFIESY75JYIPIGF424PIAWUM7RNLSTDT X-Message-ID-Hash: MFIESY75JYIPIGF424PIAWUM7RNLSTDT X-MailFrom: hughsient@gmail.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-flashrom.flashrom.org-0; header-match-flashrom.flashrom.org-1; header-match-flashrom.flashrom.org-2; header-match-flashrom.flashrom.org-3; header-match-flashrom.flashrom.org-4; header-match-flashrom.flashrom.org-5; header-match-flashrom.flashrom.org-6; header-match-flashrom.flashrom.org-7; header-match-flashrom.flashrom.org-8; header-match-flashrom.flashrom.org-9; header-match-flashrom.flashrom.org-10; header-match-flashrom.flashrom.org-11; header-match-flashrom.flashrom.org-12; header-match-flashrom.flashrom.org-13; header-match-flashrom.flashrom.org-14; header-match-flashrom.flashrom.org-15; header-match-flashrom.flashrom.org-16; header-match-flashrom.flashrom.org-17; header-match-flashrom.flashrom.org-18; header-match-flashrom.flashrom.org-19; header-match-flashrom.flashrom.org-20; header-match-flashrom.flashrom.org-21; header-match-flashrom.flashrom.org- 22; header-match-flashrom.flashrom.org-23; header-match-flashrom.flashrom.org-24; header-match-flashrom.flashrom.org-25; header-match-flashrom.flashrom.org-26; header-match-flashrom.flashrom.org-27; header-match-flashrom.flashrom.org-28; header-match-flashrom.flashrom.org-29; header-match-flashrom.flashrom.org-30; header-match-flashrom.flashrom.org-31; header-match-flashrom.flashrom.org-32; header-match-flashrom.flashrom.org-33; header-match-flashrom.flashrom.org-34; header-match-flashrom.flashrom.org-35; header-match-flashrom.flashrom.org-36; header-match-flashrom.flashrom.org-37; header-match-flashrom.flashrom.org-38; header-match-flashrom.flashrom.org-39; header-match-flashrom.flashrom.org-40; header-match-flashrom.flashrom.org-41; header-match-flashrom.flashrom.org-42; header-match-flashrom.flashrom.org-43; header-match-flashrom.flashrom.org-44; header-match-flashrom.flashrom.org-45; header-match-flashrom.flashrom.org-46; header-match-flashrom.flashrom.org-47; header-match-flash rom.flashrom.org-48; header-match-flashrom.flashrom.org-49; header-match-flashrom.flashrom.org-50; header-match-flashrom.flashrom.org-51; header-match-flashrom.flashrom.org-52; header-match-flashrom.flashrom.org-53; header-match-flashrom.flashrom.org-54; header-match-flashrom.flashrom.org-55; header-match-flashrom.flashrom.org-56; header-match-flashrom.flashrom.org-57; header-match-flashrom.flashrom.org-58; header-match-flashrom.flashrom.org-59; header-match-flashrom.flashrom.org-60; header-match-flashrom.flashrom.org-61; header-match-flashrom.flashrom.org-62; header-match-flashrom.flashrom.org-63; header-match-flashrom.flashrom.org-64; header-match-flashrom.flashrom.org-65; header-match-flashrom.flashrom.org-66; header-match-flashrom.flashrom.org-67; header-match-flashrom.flashrom.org-68; header-match-flashrom.flashrom.org-69; header-match-flashrom.flashrom.org-70; header-match-flashrom.flashrom.org-71; header-match-flashrom.flashrom.org-72; header-match-flashrom.flashrom.org-73; h eader-match-flashrom.flashrom.org-74; header-match-flashrom.flashrom.org-75; header-match-flashrom.flashrom.org-76; header-match-flashrom.flashrom.org-77; header-match-flashrom.flashrom.org-78; header-match-flashrom.flashrom.org-79; header-match-flashrom.flashrom.org-80; header-match-flashrom.flashrom.org-81; header-match-flashrom.flashrom.org-82; header-match-flashrom.flashrom.org-83; header-match-flashrom.flashrom.org-84; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; suspicious-header CC: "flashrom@flashrom.org" , Mario Limonciello X-Mailman-Version: 3.3.2b1 Precedence: list Subject: [flashrom] Re: Using libflashrom from fwupd List-Id: flashrom discussion and development mailing list Archived-At: List-Archive: List-Help: List-Post: List-Subscribe: List-Unsubscribe: Authentication-Results: coreboot.org; auth=pass smtp.auth=mailman@coreboot.org smtp.mailfrom=flashrom-bounces@flashrom.org X-Spamd-Bar: - On Wed, 16 Aug 2017 at 22:02, David Hendricks wrote: > The short answer is yes. Nico also briefly mentioned an idea on IRC to use a mechanism similar to flashrom_set_log_callback() to avoid complicating function signatures for the read, write, and verify functions. Dragging up an email thread from a long time ago, apologies. We're finally using libflashrom via fwupd "in production" so to speak. One glaring problem is that the UI "freezes" when we're doing operations with libflashrom, which we've worked around with just making the progress bar spin up and down so at least it looks like the program has not crashed. Of course, the correct thing to do is return the progress information because, as David says, some flash chips take a looong time to write. I've attached a patch for some initial comments, if this looks acceptable I can add some more callbacks in other flash implementations (as at the moment I've only covered SPI stuff) but I didn't want to spend time on a mega patch before I had some kind of initial thumbs-up. Comments welcome. Thanks. Richard. From fbb0ce04a8306525ed02d62ae1fb4b7c0bc998b4 Mon Sep 17 00:00:00 2001 From: Richard Hughes Date: Fri, 15 Jan 2021 09:48:12 +0000 Subject: [PATCH] libflashrom: Return progress state to the library user Change-Id: I7197572bb7f19e3bdb2bde855d70a0f50fd3854c Signed-off-by: Richard Hughes --- flash.h | 1 + libflashrom.c | 25 +++++++++++++++++++++++++ libflashrom.h | 11 +++++++++++ spi.c | 6 ++++++ spi25.c | 13 +++++++++++++ 5 files changed, 56 insertions(+) diff --git flash.h flash.h index 883b6f4b..827a46d0 100644 --- flash.h +++ flash.h @@ -415,6 +415,7 @@ __attribute__((format(printf, 2, 3))); #define msg_gspew(...) print(FLASHROM_MSG_SPEW, __VA_ARGS__) /* general debug spew */ #define msg_pspew(...) print(FLASHROM_MSG_SPEW, __VA_ARGS__) /* programmer debug spew */ #define msg_cspew(...) print(FLASHROM_MSG_SPEW, __VA_ARGS__) /* chip debug spew */ +void set_progress(const enum flashrom_progress_stage stage, size_t current, size_t total); /* layout.c */ int register_include_arg(struct layout_include_args **args, char *name); diff --git libflashrom.c libflashrom.c index ae2d33da..ed7704ba 100644 --- libflashrom.c +++ libflashrom.c @@ -40,6 +40,8 @@ /** Pointer to log callback function. */ static flashrom_log_callback *global_log_callback = NULL; +static flashrom_progress_callback *global_progress_callback = NULL; +static void *global_progress_userdata = NULL; /** * @brief Initialize libflashrom. @@ -95,6 +97,29 @@ int print(const enum flashrom_log_level level, const char *const fmt, ...) return 0; } +/** + * @brief Set the progress callback function. + * + * Set a callback function which will be invoked whenever libflashrom wants + * to indicate the progress has chaned. This allows frontends to do whatever + * they see fit with such values, e.g. update a progress bar in a GUI tool. + * + * @param progress_callback Pointer to the new progress callback function. + * @param userdata Userdata pointer to include with the progress callback. + */ +void flashrom_set_progress_callback(flashrom_progress_callback *const progress_callback, void *progress_userdata) +{ + global_progress_callback = progress_callback; + global_progress_userdata = progress_userdata; +} +/** @private */ +void set_progress(const enum flashrom_progress_stage stage, size_t current, size_t total) +{ + if (global_progress_callback == NULL) + return; + global_progress_callback(stage, current, total, global_progress_userdata); +} + /** @} */ /* end flashrom-general */ diff --git libflashrom.h libflashrom.h index d0d58261..8d1caf7d 100644 --- libflashrom.h +++ libflashrom.h @@ -35,10 +35,21 @@ enum flashrom_log_level { FLASHROM_MSG_DEBUG2 = 4, FLASHROM_MSG_SPEW = 5, }; + +/** @ingroup flashrom-general */ +enum flashrom_progress_stage { + FLASHROM_PROGRESS_READ, + FLASHROM_PROGRESS_WRITE, +}; + /** @ingroup flashrom-general */ typedef int(flashrom_log_callback)(enum flashrom_log_level, const char *format, va_list); void flashrom_set_log_callback(flashrom_log_callback *); +/** @ingroup flashrom-general */ +typedef void(flashrom_progress_callback)(enum flashrom_progress_stage stage, size_t current, size_t total, void *user_data); +void flashrom_set_progress_callback(flashrom_progress_callback *, void *); + /** @ingroup flashrom-query */ enum flashrom_test_state { FLASHROM_TESTED_OK = 0, diff --git spi.c spi.c index aed2a927..9e991fcb 100644 --- spi.c +++ spi.c @@ -101,6 +101,8 @@ int spi_chip_read(struct flashctx *flash, uint8_t *buf, unsigned int start, { int ret; size_t to_read; + size_t progress_current = 0; + size_t progress_total = len - start; for (; len; len -= to_read, buf += to_read, start += to_read) { /* Do not cross 16MiB boundaries in a single transfer. This helps with @@ -110,6 +112,10 @@ int spi_chip_read(struct flashctx *flash, uint8_t *buf, unsigned int start, ret = flash->mst->spi.read(flash, buf, start, to_read); if (ret) return ret; + + /* Emit progress to client */ + progress_current += to_read; + set_progress(FLASHROM_PROGRESS_READ, progress_current, progress_total); } return 0; } diff --git spi25.c spi25.c index 213273f2..b3b98c81 100644 --- spi25.c +++ spi25.c @@ -669,11 +669,17 @@ int spi_read_chunked(struct flashctx *flash, uint8_t *buf, unsigned int start, { int ret; size_t to_read; + size_t progress_current = 0; + size_t progress_total = len - start; for (; len; len -= to_read, buf += to_read, start += to_read) { to_read = min(chunksize, len); ret = spi_nbyte_read(flash, start, buf, to_read); if (ret) return ret; + + /* Emit progress to client */ + progress_current += to_read; + set_progress(FLASHROM_PROGRESS_READ, progress_current, progress_total); } return 0; } @@ -693,6 +699,8 @@ int spi_write_chunked(struct flashctx *flash, const uint8_t *buf, unsigned int s * we're OK for now. */ unsigned int page_size = flash->chip->page_size; + size_t progress_current = 0; + size_t progress_total = len - start; /* Warning: This loop has a very unusual condition and body. * The loop needs to go through each page with at least one affected @@ -717,6 +725,10 @@ int spi_write_chunked(struct flashctx *flash, const uint8_t *buf, unsigned int s if (rc) return rc; } + + /* Emit progress to client */ + progress_current += lenhere; + set_progress(FLASHROM_PROGRESS_WRITE, progress_current, progress_total); } return 0; @@ -736,6 +748,7 @@ int spi_chip_write_1(struct flashctx *flash, const uint8_t *buf, unsigned int st for (i = start; i < start + len; i++) { if (spi_nbyte_program(flash, i, buf + i - start, 1)) return 1; + set_progress(FLASHROM_PROGRESS_WRITE, i - start, len - start); } return 0; } -- 2.29.2