1
0
mirror of https://review.coreboot.org/flashrom.git synced 2025-06-30 21:52:36 +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

@ -1225,6 +1225,24 @@ notfound:
return chip - flashchips;
}
static void setup_progress_from_layout(struct flashctx *flashctx,
enum flashrom_progress_stage stage)
{
if (!flashctx->progress_callback)
return;
const struct flashrom_layout *const flash_layout = get_layout(flashctx);
size_t total = 0;
const struct romentry *entry = NULL;
while ((entry = layout_next_included(flash_layout, entry))) {
const struct flash_region *region = &entry->region;
total += region->end - region->start + 1;
}
init_progress(flashctx, stage, total);
}
/**
* @brief Reads the included layout regions into a buffer.
*
@ -1241,6 +1259,8 @@ static int read_by_layout(struct flashctx *const flashctx, uint8_t *const buffer
const struct flashrom_layout *const layout = get_layout(flashctx);
const struct romentry *entry = NULL;
setup_progress_from_layout(flashctx, FLASHROM_PROGRESS_READ);
while ((entry = layout_next_included(layout, entry))) {
const struct flash_region *region = &entry->region;
const chipoff_t region_start = region->start;
@ -1353,6 +1373,9 @@ static int erase_by_layout(struct flashctx *const flashctx)
memset(curcontents, ~ERASED_VALUE(flashctx), flash_size);
memset(newcontents, ERASED_VALUE(flashctx), flash_size);
setup_progress_from_layout(flashctx, FLASHROM_PROGRESS_READ);
setup_progress_from_layout(flashctx, FLASHROM_PROGRESS_ERASE);
const struct flashrom_layout *const flash_layout = get_layout(flashctx);
const struct romentry *entry = NULL;
while ((entry = layout_next_included(flash_layout, entry))) {
@ -1389,6 +1412,10 @@ static int write_by_layout(struct flashctx *const flashctx,
goto _ret;
}
setup_progress_from_layout(flashctx, FLASHROM_PROGRESS_READ);
setup_progress_from_layout(flashctx, FLASHROM_PROGRESS_WRITE);
setup_progress_from_layout(flashctx, FLASHROM_PROGRESS_ERASE);
const struct romentry *entry = NULL;
while ((entry = layout_next_included(flash_layout, entry))) {
ret = erase_write(flashctx, entry->region.start,
@ -1427,6 +1454,8 @@ static int verify_by_layout(
{
const struct romentry *entry = NULL;
setup_progress_from_layout(flashctx, FLASHROM_PROGRESS_READ);
while ((entry = layout_next_included(layout, entry))) {
const struct flash_region *region = &entry->region;
const chipoff_t region_start = region->start;
@ -1924,6 +1953,7 @@ int flashrom_image_write(struct flashctx *const flashctx, void *const buffer, co
*/
msg_cinfo("Reading old flash chip contents... ");
if (verify_all) {
init_progress(flashctx, FLASHROM_PROGRESS_READ, flash_size);
if (read_flash(flashctx, oldcontents, 0, flash_size)) {
msg_cinfo("FAILED.\n");
goto _finalize_ret;
@ -1940,12 +1970,14 @@ int flashrom_image_write(struct flashctx *const flashctx, void *const buffer, co
bool all_skipped = true;
msg_cinfo("Updating flash chip contents... ");
if (write_by_layout(flashctx, curcontents, newcontents, &all_skipped)) {
msg_cerr("Uh oh. Erase/write failed. ");
ret = 2;
if (verify_all) {
msg_cerr("Checking if anything has changed.\n");
msg_cinfo("Reading current flash chip contents... ");
init_progress(flashctx, FLASHROM_PROGRESS_READ, flash_size);
if (!read_flash(flashctx, curcontents, 0, flash_size)) {
msg_cinfo("done.\n");
if (!memcmp(oldcontents, curcontents, flash_size)) {