1
0
mirror of https://review.coreboot.org/flashrom.git synced 2025-07-01 14:11:15 +02:00

libflashrom: Update the API for progress callback

The initial version of API for progress callback would require the
callback function to make a second call to get the needed data about
progress state (current, total etc).

This patch changes the callback API, so that callback function gets
all needed data straight away as parameters, and with this,
callback has all the data to do its job.

Since the initial version was submitted and it was in the tree for a
while, the change needs to add a _v2 suffix for new thing and
deprecated attribute for old thing.

Testing: both unit tests and cli are libflashrom clients.
All unit tests run successfully, for the cli all scenarios from
commit 75dc0655b9 run successfully.

Change-Id: Ia8cc0461c449b7e65888a64cdc594c55b81eae7a
Signed-off-by: Anastasia Klimchuk <aklm@flashrom.org>
Reviewed-on: https://review.coreboot.org/c/flashrom/+/86031
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Sergii Dmytruk <sergii.dmytruk@3mdeb.com>
Reviewed-by: Peter Marheine <pmarheine@chromium.org>
This commit is contained in:
Anastasia Klimchuk
2025-01-16 17:40:15 +11:00
parent f5a3c7a35b
commit e2df58a379
11 changed files with 122 additions and 72 deletions

View File

@ -81,20 +81,20 @@ 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);
static void progress_callback(enum flashrom_progress_stage stage, size_t current, size_t total, void* user_data) {
struct progress_user_data *progress_user_data = user_data;
if (current == 0) {
printf("Progress started for stage %d, initial callback call\n", stage);
} else {
/* Progress cannot go backwards. */
assert_true(flash->progress_state->current >= progress_user_data->last_seen[flash->progress_state->stage]);
assert_true(current >= progress_user_data->last_seen[stage]);
}
if (flash->progress_state->current >= flash->progress_state->total - 1)
printf("Progress almost complete for stage %d, current %ld, total %ld\n",
flash->progress_state->stage, flash->progress_state->current, flash->progress_state->total);
if (current >= total - 1)
printf("Progress almost complete for stage %d, current %ld, total %ld\n", stage, current, total);
progress_user_data->last_seen[flash->progress_state->stage] = flash->progress_state->current;
progress_user_data->last_seen[stage] = current;
}
static void setup_chip(struct flashrom_flashctx *flashctx, struct flashrom_layout **layout,
@ -255,10 +255,7 @@ void erase_chip_with_progress(void **state)
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);
flashrom_set_progress_callback_v2(&flashctx, progress_callback, &progress_user_data);
printf("Erase chip operation started.\n");
assert_int_equal(0, flashrom_flash_erase(&flashctx));
@ -357,10 +354,7 @@ void read_chip_with_progress(void **state)
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);
flashrom_set_progress_callback_v2(&flashctx, progress_callback, &progress_user_data);
const char *const filename = "read_chip.test";
unsigned long size = mock_chip.total_size * 1024;
@ -488,10 +482,7 @@ void write_chip_with_progress(void **state)
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);
flashrom_set_progress_callback_v2(&flashctx, progress_callback, &progress_user_data);
const char *const filename = "-";
unsigned long size = mock_chip.total_size * 1024;
@ -621,10 +612,7 @@ void write_chip_feature_no_erase_with_progress(void **state)
assert_non_null(newcontents);
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);
flashrom_set_progress_callback_v2(&flashctx, progress_callback, &progress_user_data);
printf("Write chip operation started.\n");
assert_int_equal(0, read_buf_from_file(newcontents, size, filename));

View File

@ -69,26 +69,25 @@ int __wrap_spi_send_command(const struct flashctx *flash,
return 0;
}
static void spi_read_progress_cb(struct flashrom_flashctx *flashctx)
static void spi_read_progress_cb(enum flashrom_progress_stage stage, size_t current, size_t total, void* user_data)
{
struct flashrom_progress *progress_state = flashctx->progress_state;
uint32_t *cnt = (uint32_t *) progress_state->user_data;
assert_int_equal(0x400, progress_state->total);
uint32_t *cnt = (uint32_t *) user_data;
assert_int_equal(0x400, total);
switch (*cnt) {
case 0:
assert_int_equal(0x0, progress_state->current);
assert_int_equal(0x0, current);
break;
case 1:
assert_int_equal(0x100, progress_state->current);
assert_int_equal(0x100, current);
break;
case 2:
assert_int_equal(0x200, progress_state->current);
assert_int_equal(0x200, current);
break;
case 3:
assert_int_equal(0x300, progress_state->current);
assert_int_equal(0x300, current);
break;
case 4:
assert_int_equal(0x400, progress_state->current);
assert_int_equal(0x400, current);
break;
default:
fail();
@ -113,10 +112,7 @@ void default_spi_read_test_success(void **state)
.chip = &mock_chip,
.mst = &mst
};
struct flashrom_progress progress_state = {
.user_data = (void *) &cnt,
};
flashrom_set_progress_callback(&flashctx, spi_read_progress_cb, &progress_state);
flashrom_set_progress_callback_v2(&flashctx, spi_read_progress_cb, (void *) &cnt);
init_progress(&flashctx, FLASHROM_PROGRESS_READ, 0x400);
for (int i = 0; i < 4; i++) {
will_return(__wrap_spi_send_command, JEDEC_WRDI);
@ -126,7 +122,7 @@ void default_spi_read_test_success(void **state)
assert_int_equal(0, default_spi_read(&flashctx, buf, offset, sizeof(buf)));
assert_int_equal(5, cnt);
flashrom_set_progress_callback(&flashctx, NULL, NULL);
flashrom_set_progress_callback_v2(&flashctx, NULL, NULL);
}
void spi_write_enable_test_success(void **state)