1
0
mirror of https://review.coreboot.org/flashrom.git synced 2025-07-03 06:53:18 +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

@ -46,6 +46,10 @@ static struct {
.buf = { 0 },
};
struct progress_user_data {
size_t last_seen; /* % of progress last reported, to be asserted in the progress callback. */
};
static int read_chip(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len)
{
printf("Read chip called with start=0x%x, len=0x%x\n", start, len);
@ -76,6 +80,21 @@ static int block_erase_chip(struct flashctx *flash, unsigned int blockaddr, unsi
return 0;
}
static void progress_callback(struct flashctx *flash) {
struct progress_user_data *progress_user_data = flash->progress_state->user_data;
if (flash->progress_state->current == 0) {
printf("Progress started for stage %d, initial callback call\n", flash->progress_state->stage);
} else {
/* Progress cannot go backwards. */
assert_true(flash->progress_state->current >= progress_user_data->last_seen);
}
if (flash->progress_state->current >= flash->progress_state->total)
printf("Progress complete for stage %d, final callback call\n", flash->progress_state->stage);
progress_user_data->last_seen = flash->progress_state->current;
}
static void setup_chip(struct flashrom_flashctx *flashctx, struct flashrom_layout **layout,
struct flashchip *chip, const char *programmer_param, const struct io_mock *io)
{
@ -210,6 +229,41 @@ void erase_chip_test_success(void **state)
teardown(&layout);
}
void erase_chip_with_progress(void **state)
{
(void) state; /* unused */
static struct io_mock_fallback_open_state data = {
.noc = 0,
.paths = { NULL },
};
const struct io_mock chip_io = {
.fallback_open_state = &data,
};
g_test_write_injector = write_chip;
g_test_read_injector = read_chip;
g_test_erase_injector[0] = block_erase_chip;
struct flashrom_flashctx flashctx = { 0 };
struct flashrom_layout *layout;
struct flashchip mock_chip = chip_8MiB;
const char *param = ""; /* Default values for all params. */
setup_chip(&flashctx, &layout, &mock_chip, param, &chip_io);
struct progress_user_data progress_user_data = {0};
struct flashrom_progress progress_state = {
.user_data = &progress_user_data
};
flashrom_set_progress_callback(&flashctx, progress_callback, &progress_state);
printf("Erase chip operation started.\n");
assert_int_equal(0, flashrom_flash_erase(&flashctx));
printf("Erase chip operation done.\n");
teardown(&layout);
}
void erase_chip_with_dummyflasher_test_success(void **state)
{
(void) state; /* unused */
@ -277,6 +331,49 @@ void read_chip_test_success(void **state)
free(buf);
}
void read_chip_with_progress(void **state)
{
(void) state; /* unused */
static struct io_mock_fallback_open_state data = {
.noc = 0,
.paths = { NULL },
};
const struct io_mock chip_io = {
.fallback_open_state = &data,
};
g_test_write_injector = write_chip;
g_test_read_injector = read_chip;
g_test_erase_injector[0] = block_erase_chip;
struct flashrom_flashctx flashctx = { 0 };
struct flashrom_layout *layout;
struct flashchip mock_chip = chip_8MiB;
const char *param = ""; /* Default values for all params. */
setup_chip(&flashctx, &layout, &mock_chip, param, &chip_io);
struct progress_user_data progress_user_data = {0};
struct flashrom_progress progress_state = {
.user_data = &progress_user_data
};
flashrom_set_progress_callback(&flashctx, progress_callback, &progress_state);
const char *const filename = "read_chip.test";
unsigned long size = mock_chip.total_size * 1024;
unsigned char *buf = calloc(size, sizeof(unsigned char));
assert_non_null(buf);
printf("Read chip operation started.\n");
assert_int_equal(0, flashrom_image_read(&flashctx, buf, size));
assert_int_equal(0, write_buf_to_file(buf, size, filename));
printf("Read chip operation done.\n");
teardown(&layout);
free(buf);
}
void read_chip_with_dummyflasher_test_success(void **state)
{
(void) state; /* unused */
@ -365,6 +462,49 @@ void write_chip_test_success(void **state)
free(newcontents);
}
void write_chip_with_progress(void **state)
{
(void) state; /* unused */
static struct io_mock_fallback_open_state data = {
.noc = 0,
.paths = { NULL },
};
const struct io_mock chip_io = {
.fallback_open_state = &data,
};
g_test_write_injector = write_chip;
g_test_read_injector = read_chip;
g_test_erase_injector[0] = block_erase_chip;
struct flashrom_flashctx flashctx = { 0 };
struct flashrom_layout *layout;
struct flashchip mock_chip = chip_8MiB;
const char *param = ""; /* Default values for all params. */
setup_chip(&flashctx, &layout, &mock_chip, param, &chip_io);
struct progress_user_data progress_user_data = {0};
struct flashrom_progress progress_state = {
.user_data = &progress_user_data
};
flashrom_set_progress_callback(&flashctx, progress_callback, &progress_state);
const char *const filename = "-";
unsigned long size = mock_chip.total_size * 1024;
uint8_t *const newcontents = malloc(size);
assert_non_null(newcontents);
printf("Write chip operation started.\n");
assert_int_equal(0, read_buf_from_file(newcontents, size, filename));
assert_int_equal(0, flashrom_image_write(&flashctx, newcontents, size, NULL));
printf("Write chip operation done.\n");
teardown(&layout);
free(newcontents);
}
void write_chip_with_dummyflasher_test_success(void **state)
{
(void) state; /* unused */