Anastasia Klimchuk submitted this change.

View Change

Approvals: Dmitry Zhadinets: Looks good to me, but someone else must approve Peter Marheine: Looks good to me, approved build bot (Jenkins): Verified Anastasia Klimchuk: Looks good to me, approved
libflashrom: Add API to get the list of supported programmers

There were no options available to obtain the list of programmers.
The implementation is based on flashrom_supported_flash_chips.
Arrays of constant strings are returned, and the array must be
freed using flashrom_data_free.

Testing: Both unit tests and CLI tools serve as libflashrom clients.
All unit tests run successfully.

Change-Id: Ib5275b742b849183b1fe701900040fee369a1d78
Signed-off-by: Dmitry Zhadinets <dzhadinets@gmail.com>
Reviewed-on: https://review.coreboot.org/c/flashrom/+/86921
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Peter Marheine <pmarheine@chromium.org>
Reviewed-by: Anastasia Klimchuk <aklm@chromium.org>
---
M include/libflashrom.h
M libflashrom.c
M libflashrom.map
M tests/libflashrom.c
M tests/tests.c
M tests/tests.h
6 files changed, 42 insertions(+), 2 deletions(-)

diff --git a/include/libflashrom.h b/include/libflashrom.h
index b9b0d17..f30ad6b 100644
--- a/include/libflashrom.h
+++ b/include/libflashrom.h
@@ -194,6 +194,14 @@
*/
const char *flashrom_version_info(void);
/**
+ * @brief Returns list of supported programmers
+ * The last entry in the returned list is followed by a NULL.
+ *
+ * @return List of supported programmers, or NULL if an error occurred.
+ * The pointer must be freed after using flashrom_data_free
+ */
+const char **flashrom_supported_programmers(void);
+/**
* @brief Returns list of supported flash chips
* @return List of supported flash chips, or NULL if an error occurred
*/
diff --git a/libflashrom.c b/libflashrom.c
index 888ca05..d6bd098 100644
--- a/libflashrom.c
+++ b/libflashrom.c
@@ -194,6 +194,18 @@
return flashrom_version;
}

+const char **flashrom_supported_programmers(void)
+{
+ const char **result = malloc(sizeof(char*) * (programmer_table_size + 1));
+ if (!result)
+ return NULL;
+ for (unsigned int i = 0; i < programmer_table_size; ++i) {
+ result[i] = programmer_table[i]->name;
+ }
+ result[programmer_table_size] = 0;
+ return result;
+}
+
struct flashrom_flashchip_info *flashrom_supported_flash_chips(void)
{
struct flashrom_flashchip_info *supported_flashchips =
diff --git a/libflashrom.map b/libflashrom.map
index a12a95c..fc6724a 100644
--- a/libflashrom.map
+++ b/libflashrom.map
@@ -32,6 +32,7 @@
flashrom_supported_boards;
flashrom_supported_chipsets;
flashrom_supported_flash_chips;
+ flashrom_supported_programmers;
flashrom_version_info;
flashrom_wp_cfg_new;
flashrom_wp_cfg_release;
diff --git a/tests/libflashrom.c b/tests/libflashrom.c
index 3320597..59ffeaf 100644
--- a/tests/libflashrom.c
+++ b/tests/libflashrom.c
@@ -1,7 +1,7 @@
/*
* This file is part of the flashrom project.
*
- * Copyright 2025 Dmitry Zhadinets (dzhadinets@gmail.com)
+ * Copyright 2025 Dmitry Zhadinets <dzhadinets@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -96,4 +96,21 @@
/* check that callback is called after the change*/
assert_int_equal(print(FLASHROM_MSG_INFO, "1%s", "\n"), 0x666 + (int)FLASHROM_MSG_INFO);
flashrom_set_log_callback(NULL);
-}
\ No newline at end of file
+}
+
+void flashrom_supported_programmers_test_success(void **state)
+{
+ (void) state; /* unused */
+ const char **array = flashrom_supported_programmers();
+ const char **ptr = array;
+
+ assert_non_null(array);
+
+ do {
+ assert_non_null(*ptr);
+ }while (*(++ptr));
+
+ flashrom_data_free(array);
+
+ assert_int_not_equal(ptr - array, 0);
+}
diff --git a/tests/tests.c b/tests/tests.c
index ef71967..2a79d32 100644
--- a/tests/tests.c
+++ b/tests/tests.c
@@ -482,6 +482,7 @@
cmocka_unit_test(flashrom_set_log_callback_test_success),
cmocka_unit_test(flashrom_set_log_callback_v2_test_success),
cmocka_unit_test(flashrom_set_log_level_test_success),
+ cmocka_unit_test(flashrom_supported_programmers_test_success),
};
ret |= cmocka_run_group_tests_name("libflashrom.c tests", libflashrom_tests, NULL, NULL);

diff --git a/tests/tests.h b/tests/tests.h
index e358e86..e87c00c 100644
--- a/tests/tests.h
+++ b/tests/tests.h
@@ -35,6 +35,7 @@
void flashrom_set_log_callback_test_success(void **state);
void flashrom_set_log_callback_v2_test_success(void **state);
void flashrom_set_log_level_test_success(void **state);
+void flashrom_supported_programmers_test_success(void **state);

/* spi25.c */
void spi_write_enable_test_success(void **state);

To view, visit change 86921. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-MessageType: merged
Gerrit-Project: flashrom
Gerrit-Branch: main
Gerrit-Change-Id: Ib5275b742b849183b1fe701900040fee369a1d78
Gerrit-Change-Number: 86921
Gerrit-PatchSet: 13
Gerrit-Owner: Dmitry Zhadinets <dzhadinets@gmail.com>
Gerrit-Reviewer: Anastasia Klimchuk <aklm@chromium.org>
Gerrit-Reviewer: Dmitry Zhadinets <dzhadinets@gmail.com>
Gerrit-Reviewer: Peter Marheine <pmarheine@chromium.org>
Gerrit-Reviewer: build bot (Jenkins) <no-reply@coreboot.org>