1
0
mirror of https://review.coreboot.org/flashrom.git synced 2025-07-01 14:11:15 +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 */

View File

@ -55,7 +55,9 @@ int __wrap_spi_send_command(const struct flashctx *flash,
*/
return __real_spi_send_command(flash, writecnt, readcnt, writearr, readarr);
check_expected_ptr(flash);
if (!flash->progress_callback)
check_expected_ptr(flash);
assert_int_equal(writecnt, mock_type(int));
assert_int_equal(writearr[0], mock_type(int));
@ -71,22 +73,22 @@ static void spi_read_progress_cb(struct flashrom_flashctx *flashctx)
{
struct flashrom_progress *progress_state = flashctx->progress_state;
uint32_t *cnt = (uint32_t *) progress_state->user_data;
assert_int_equal(0x300, progress_state->total);
assert_int_equal(0x400, progress_state->total);
switch (*cnt) {
case 0:
assert_int_equal(0x100, progress_state->current);
assert_int_equal(0x0, progress_state->current);
break;
case 1:
assert_int_equal(0x200, progress_state->current);
assert_int_equal(0x100, progress_state->current);
break;
case 2:
assert_int_equal(0x300, progress_state->current);
assert_int_equal(0x200, progress_state->current);
break;
case 3:
assert_int_equal(0x300, progress_state->current);
break;
case 4:
assert_int_equal(0x300, progress_state->current);
assert_int_equal(0x400, progress_state->current);
break;
default:
fail();
@ -94,7 +96,7 @@ static void spi_read_progress_cb(struct flashrom_flashctx *flashctx)
(*cnt)++;
}
void spi_read_chunked_test_success(void **state)
void default_spi_read_test_success(void **state)
{
(void) state; /* unused */
uint8_t buf[0x400] = { 0x0 };
@ -115,15 +117,16 @@ void spi_read_chunked_test_success(void **state)
.user_data = (void *) &cnt,
};
flashrom_set_progress_callback(&flashctx, spi_read_progress_cb, &progress_state);
init_progress(&flashctx, FLASHROM_PROGRESS_READ, 0x400);
for (int i = 0; i < 4; i++) {
expect_memory(__wrap_spi_send_command, flash,
&flashctx, sizeof(flashctx));
will_return(__wrap_spi_send_command, JEDEC_WRDI);
will_return(__wrap_spi_send_command, JEDEC_READ);
will_return(__wrap_spi_send_command, max_data_read);
}
assert_int_equal(0, spi_chip_read(&flashctx, buf, offset, sizeof(buf)));
assert_int_equal(0, default_spi_read(&flashctx, buf, offset, sizeof(buf)));
assert_int_equal(5, cnt);
flashrom_set_progress_callback(&flashctx, NULL, NULL);
}
void spi_write_enable_test_success(void **state)

View File

@ -441,7 +441,7 @@ int main(int argc, char *argv[])
const struct CMUnitTest spi25_tests[] = {
cmocka_unit_test(spi_write_enable_test_success),
cmocka_unit_test(spi_write_disable_test_success),
cmocka_unit_test(spi_read_chunked_test_success),
cmocka_unit_test(default_spi_read_test_success),
cmocka_unit_test(probe_spi_rdid_test_success),
cmocka_unit_test(probe_spi_rdid4_test_success),
cmocka_unit_test(probe_spi_rems_test_success),
@ -495,10 +495,13 @@ int main(int argc, char *argv[])
const struct CMUnitTest chip_tests[] = {
cmocka_unit_test(erase_chip_test_success),
cmocka_unit_test(erase_chip_with_progress),
cmocka_unit_test(erase_chip_with_dummyflasher_test_success),
cmocka_unit_test(read_chip_test_success),
cmocka_unit_test(read_chip_with_progress),
cmocka_unit_test(read_chip_with_dummyflasher_test_success),
cmocka_unit_test(write_chip_test_success),
cmocka_unit_test(write_chip_with_progress),
cmocka_unit_test(write_chip_with_dummyflasher_test_success),
cmocka_unit_test(write_chip_feature_no_erase),
cmocka_unit_test(write_nonaligned_region_with_dummyflasher_test_success),

View File

@ -34,7 +34,7 @@ void flashbuses_to_text_test_success(void **state);
/* spi25.c */
void spi_write_enable_test_success(void **state);
void spi_write_disable_test_success(void **state);
void spi_read_chunked_test_success(void **state);
void default_spi_read_test_success(void **state);
void probe_spi_rdid_test_success(void **state);
void probe_spi_rdid4_test_success(void **state);
void probe_spi_rems_test_success(void **state);
@ -82,10 +82,13 @@ void layout_region_invalid_range_test_success(void **state);
/* chip.c */
void erase_chip_test_success(void **state);
void erase_chip_with_progress(void **state);
void erase_chip_with_dummyflasher_test_success(void **state);
void read_chip_test_success(void **state);
void read_chip_with_progress(void **state);
void read_chip_with_dummyflasher_test_success(void **state);
void write_chip_test_success(void **state);
void write_chip_with_progress(void **state);
void write_chip_with_dummyflasher_test_success(void **state);
void write_chip_feature_no_erase(void **state);
void write_nonaligned_region_with_dummyflasher_test_success(void **state);