1
0
mirror of https://review.coreboot.org/flashrom.git synced 2025-07-02 22:43:17 +02:00

Complete and fix progress feature implementation for all operations

Original progress reporting implemented in CB:49643 and it has some
issues, for example:

    size_t start_address = start;
    size_t end_address = len - start;

End address is anything but length minus start address.

    update_progress(flash,
                    FLASHROM_PROGRESS_READ,
                    /*current*/ start - start_address + to_read,
                    /*total*/ end_address);

Total should just be length if that's how current value is computed.

---

libflashrom needs to know total size ahead of time.
That's init_progress() and changed update_progress().

It also needs to store the last current value to be able to update it.
That's stage_progress in flashrom_flashctx.

Measuring accurately amount of data which will be read/erased/written
isn't easy because things can be skipped as optimizations. The next
patch in the chain aims to address this, there are TODO/FIXME
comments there.

---

CLI shares terminal with the rest of the code and has to maintain more
state to handle that reasonably well.

Similar to CB:64668, an effort is made to keep the progress on a
single line. Non-progress output is kept track of to know when
moving to a new line cannot be avoided.

---

A script to test the CLI:

\#!/bin/bash
t=${1:-rewW}
shift

if [[ $t =~ r ]]; then
    echo ">>> READ"
    ./flashrom -p dummy:emulate=W25Q128FV,freq=64mhz -r dump.rom --progress "$@"
    echo
fi

if [[ $t =~ e ]]; then
    echo ">>> ERASE"
    ./flashrom -p dummy:emulate=W25Q128FV,freq=64mhz -E --progress "$@"
    echo
fi

if [[ $t =~ w ]]; then
    echo ">>> WRITE (without erase)"
    dd if=/dev/zero of=zero.rom bs=1M count=16 2> /dev/null
    ./flashrom -p dummy:emulate=W25Q128FV,freq=64mhz -w zero.rom --progress "$@"
    echo
fi

if [[ $t =~ W ]]; then
    echo ">>> WRITE (with erase)"
    dd if=/dev/zero of=zero.rom bs=1M count=16 2> /dev/null
    dd if=/dev/random of=random.rom bs=1M count=16 2> /dev/null
    ./flashrom -p dummy:emulate=W25Q128FV,freq=64mhz,image=random.rom -w zero.rom --progress "$@"
    echo
fi

Co-developed-by: Anastasia Klimchuk <aklm@flashrom.org>
Co-developed-by: Sergii Dmytruk <sergii.dmytruk@3mdeb.com>
Change-Id: If1e40fc97f443c4f0c0501cef11cff1f3f84c051
Signed-off-by: Sergii Dmytruk <sergii.dmytruk@3mdeb.com>
Signed-off-by: Anastasia Klimchuk <aklm@flashrom.org>
Reviewed-on: https://review.coreboot.org/c/flashrom/+/84102
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Anastasia Klimchuk <aklm@chromium.org>
This commit is contained in:
Anastasia Klimchuk
2024-09-20 23:29:53 +10:00
parent 83ba5e93c1
commit 75dc0655b9
23 changed files with 333 additions and 51 deletions

View File

@ -24,6 +24,14 @@
enum flashrom_log_level verbose_screen = FLASHROM_MSG_INFO;
enum flashrom_log_level verbose_logfile = FLASHROM_MSG_DEBUG2;
/* Enum to indicate what was the latest printed char prior to a progress indicator. */
enum line_state {
NEWLINE,
MIDLINE,
PROGRESS
};
static enum line_state line_state = NEWLINE;
static FILE *logfile = NULL;
int close_logfile(void)
@ -75,18 +83,75 @@ static const char *flashrom_progress_stage_to_string(enum flashrom_progress_stag
return "UNKNOWN";
}
static void print_progress(const struct cli_progress *cli_progress, enum flashrom_progress_stage stage)
{
if (!(cli_progress->visible_stages & (1 << stage)))
return;
msg_ginfo("[%s: %2u%%]", flashrom_progress_stage_to_string(stage), cli_progress->stage_pc[stage]);
}
void flashrom_progress_cb(struct flashrom_flashctx *flashctx)
{
struct flashrom_progress *progress_state = flashctx->progress_state;
unsigned int pc = 0;
unsigned int *percentages = progress_state->user_data;
if (progress_state->current > 0 && progress_state->total > 0)
pc = ((unsigned long long) progress_state->current * 10000llu) /
((unsigned long long) progress_state->total * 100llu);
if (percentages[progress_state->stage] != pc) {
percentages[progress_state->stage] = pc;
msg_ginfo("[%s] %u%% complete... ", flashrom_progress_stage_to_string(progress_state->stage), pc);
struct cli_progress *cli_progress = progress_state->user_data;
/* The expectation is that initial progress of zero is reported before doing anything. */
if (progress_state->current == 0) {
if (!cli_progress->stage_setup) {
cli_progress->stage_setup = true;
/* Initialization of some stage doesn't imply that it will make any progress,
* only show stages which have progressed. */
cli_progress->visible_stages = 0;
if (line_state != NEWLINE) {
/* We're going to clear and replace ongoing progress output, so make a new line. */
msg_ginfo("\n");
}
}
cli_progress->stage_pc[progress_state->stage] = 0;
} else {
cli_progress->stage_setup = false;
cli_progress->visible_stages |= 1 << progress_state->stage;
}
if (progress_state->current > 0 && progress_state->total > 0)
pc = ((unsigned long long) progress_state->current * 100llu) /
((unsigned long long) progress_state->total);
if (cli_progress->stage_pc[progress_state->stage] != pc) {
cli_progress->stage_pc[progress_state->stage] = pc;
if (line_state == PROGRESS) {
/* Erase previous output, because it was previous progress step. */
int i;
for (i = 0; i < 16 * FLASHROM_PROGRESS_NR; ++i)
msg_ginfo("\b \b");
} else if (line_state == MIDLINE) {
/* Start with new line, to preserve some other previous message */
msg_ginfo("\n");
} // Remaining option is NEWLINE, which means nothing to do: newline has been printed already.
/* The order is deliberate, the operations typically follow this sequence. */
print_progress(cli_progress, FLASHROM_PROGRESS_READ);
print_progress(cli_progress, FLASHROM_PROGRESS_ERASE);
print_progress(cli_progress, FLASHROM_PROGRESS_WRITE);
/* There can be output right after the progress, this acts as a separator. */
msg_ginfo("...");
/* Reset the flag, because now the latest message is a progress one. */
line_state = PROGRESS;
}
}
static void update_line_state(const char *fmt)
{
size_t len = strlen(fmt);
if (len > 0)
line_state = (fmt[len - 1] == '\n' ? NEWLINE : MIDLINE);
}
/* Please note that level is the verbosity, not the importance of the message. */
@ -103,6 +168,7 @@ int flashrom_print_cb(enum flashrom_log_level level, const char *fmt, va_list ap
if (level <= verbose_screen) {
ret = vfprintf(output_type, fmt, ap);
update_line_state(fmt);
/* msg_*spew often happens inside chip accessors in possibly
* time-critical operations. Don't slow them down by flushing. */
if (level != FLASHROM_MSG_SPEW)
@ -111,6 +177,7 @@ int flashrom_print_cb(enum flashrom_log_level level, const char *fmt, va_list ap
if ((level <= verbose_logfile) && logfile) {
ret = vfprintf(logfile, fmt, logfile_args);
update_line_state(fmt);
if (level != FLASHROM_MSG_SPEW)
fflush(logfile);
}