Anastasia Klimchuk has uploaded this change for review. ( https://review.coreboot.org/c/flashrom/+/87174?usp=email )
Change subject: cli: Set maximum log level for log callback
......................................................................
cli: Set maximum log level for log callback
Follow up on
commit 6571f263b52710579f27b3c53ade52d46acbc8e3
which adds ability to set maximum log level to log callback API.
And INFO as the default.
cli at the moment processes all the messages in the callback,
so log level should be maximum possible to get all the messages.
Alternative to this could be setting the default max log level
for callback as SPEW.
Change-Id: I70a02ea1a1d692267fd6d92cdb5273786a913777
Signed-off-by: Anastasia Klimchuk <aklm(a)flashrom.org>
---
M cli_classic.c
1 file changed, 3 insertions(+), 0 deletions(-)
git pull ssh://review.coreboot.org:29418/flashrom refs/changes/74/87174/1
diff --git a/cli_classic.c b/cli_classic.c
index d0c7114..3e5dab9 100644
--- a/cli_classic.c
+++ b/cli_classic.c
@@ -1110,6 +1110,9 @@
* chip when a flash device gets opened with fd 1 or 2.
*/
if (check_file(stdout) && check_file(stderr)) {
+ /* This is maximum log level for callback to be invoked,
+ * and cli wants callback to be always invoked. */
+ flashrom_set_log_level(FLASHROM_MSG_SPEW);
flashrom_set_log_callback(&flashrom_print_cb);
}
--
To view, visit https://review.coreboot.org/c/flashrom/+/87174?usp=email
To unsubscribe, or for help writing mail filters, visit https://review.coreboot.org/settings?usp=email
Gerrit-MessageType: newchange
Gerrit-Project: flashrom
Gerrit-Branch: main
Gerrit-Change-Id: I70a02ea1a1d692267fd6d92cdb5273786a913777
Gerrit-Change-Number: 87174
Gerrit-PatchSet: 1
Gerrit-Owner: Anastasia Klimchuk <aklm(a)chromium.org>
Attention is currently required from: Dmitry Zhadinets.
Anastasia Klimchuk has posted comments on this change by Dmitry Zhadinets. ( https://review.coreboot.org/c/flashrom/+/86921?usp=email )
Change subject: libflashrom: Add API to return the list of supported programmers
......................................................................
Patch Set 3:
(5 comments)
Patchset:
PS3:
> I will wait the merge of log API merge to rebase it because I will have conflicts in test/libflashro […]
I realised that by now I already added comments to all three patches in the chain, hope it's fine. If you re-arrange the code between commits, comment can be "replied" in the other commit from where it was asked, it's fine.
I am mostly thinking / worried about a comment in CB:86946 because it seems like it should be v2 probing doing this.
File libflashrom.c:
https://review.coreboot.org/c/flashrom/+/86921/comment/ff1707a8_7d6246bf?us… :
PS3, Line 147:
no space here
https://review.coreboot.org/c/flashrom/+/86921/comment/886fd2df_5d72935b?us… :
PS3, Line 159: const char *flashrom_get_programmer_name(int n)
: {
: if (n < 0 || (size_t)n >= programmer_table_size)
: return NULL;
: return programmer_table[n]->name;
: }
Why this is needed? Client can get the n-th name using the function just above `flashrom_supported_programmers` and then loop on top of it?
File tests/libflashrom.c:
https://review.coreboot.org/c/flashrom/+/86921/comment/c4f3c060_74462418?us… :
PS3, Line 40: #if CONFIG_DUMMY == 1
For tests, dummy is always included, you can remove this check
https://review.coreboot.org/c/flashrom/+/86921/comment/0aa4163f_c7fd656f?us… :
PS3, Line 62:
This needs to be tabs (instead of spaces).
--
To view, visit https://review.coreboot.org/c/flashrom/+/86921?usp=email
To unsubscribe, or for help writing mail filters, visit https://review.coreboot.org/settings?usp=email
Gerrit-MessageType: comment
Gerrit-Project: flashrom
Gerrit-Branch: main
Gerrit-Change-Id: Ib5275b742b849183b1fe701900040fee369a1d78
Gerrit-Change-Number: 86921
Gerrit-PatchSet: 3
Gerrit-Owner: Dmitry Zhadinets <dzhadinets(a)gmail.com>
Gerrit-Reviewer: Anastasia Klimchuk <aklm(a)chromium.org>
Gerrit-Reviewer: Peter Marheine <pmarheine(a)chromium.org>
Gerrit-Reviewer: build bot (Jenkins) <no-reply(a)coreboot.org>
Gerrit-Attention: Dmitry Zhadinets <dzhadinets(a)gmail.com>
Gerrit-Comment-Date: Fri, 04 Apr 2025 11:46:28 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Comment-In-Reply-To: Dmitry Zhadinets <dzhadinets(a)gmail.com>
Anastasia Klimchuk has submitted this change. ( https://review.coreboot.org/c/flashrom/+/86875?usp=email )
Change subject: libflashrom: Update the API for Logger Callback
......................................................................
libflashrom: Update the API for Logger Callback
The initial implementation does not account for user_data, requiring
the calling application to use a global scope. This may lead to issues
related to object lifecycle management and other architectural
concerns.
This patch adds user_data to the user’s log callback. Moreover, it
performs message formatting, so the application only needs to pass
the formatted string to the selected output.
The change does not break the existing logging API but extends it.
A new API version is introduced with the v2 suffix.
Testing: Both unit tests and CLI tools serve as libflashrom clients.
All unit tests run successfully.
Change-Id: Iea738bd371fa3d69b9cf222c89ee67490d30af39
Signed-off-by: Dmitry Zhadinets <dzhadinets(a)gmail.com>
Reviewed-on: https://review.coreboot.org/c/flashrom/+/86875
Reviewed-by: Anastasia Klimchuk <aklm(a)chromium.org>
Reviewed-by: Peter Marheine <pmarheine(a)chromium.org>
Tested-by: build bot (Jenkins) <no-reply(a)coreboot.org>
---
M include/flash.h
M include/libflashrom.h
M libflashrom.c
M libflashrom.map
M meson.build
M meson_options.txt
A tests/libflashrom.c
M tests/meson.build
M tests/tests.c
M tests/tests.h
10 files changed, 153 insertions(+), 1 deletion(-)
Approvals:
build bot (Jenkins): Verified
Peter Marheine: Looks good to me, approved
Anastasia Klimchuk: Looks good to me, approved
Dmitry Zhadinets: Looks good to me, but someone else must approve
diff --git a/include/flash.h b/include/flash.h
index 3293980..b5ea2b5 100644
--- a/include/flash.h
+++ b/include/flash.h
@@ -730,7 +730,14 @@
void print_chip_support_status(const struct flashchip *chip);
/* libflashrom.c */
-/* Let gcc and clang check for correct printf-style format strings. */
+/*
+ * Let gcc and clang check for correct printf-style format strings.
+ * Avoid using formatted messages longer than 256 characters.
+ * The new public logging API formats messages and passes a ready-to-print
+ * string to the user callback for performance reasons.
+ * Therefore, any message exceeding 256 characters will be truncated,
+ * and an ERANGE error code will be returned.
+ */
int print(enum flashrom_log_level level, const char *fmt, ...)
#ifdef __MINGW32__
# ifndef __MINGW_PRINTF_FORMAT
diff --git a/include/libflashrom.h b/include/libflashrom.h
index 41c7424..96479ed 100644
--- a/include/libflashrom.h
+++ b/include/libflashrom.h
@@ -69,6 +69,24 @@
*/
void flashrom_set_log_callback(flashrom_log_callback *log_callback);
+typedef void (flashrom_log_callback_v2)(
+ enum flashrom_log_level,
+ const char* message,
+ void* user_data);
+/**
+ * @brief Set the log callback function.
+ *
+ * Set a callback function which will be invoked whenever libflashrom wants
+ * to output messages. This allows frontends to do whatever they see fit with
+ * such messages, e.g. write them to syslog, or to a file, or print them in a
+ * GUI window, etc.
+ * The message is formatted using vsnprintf
+ *
+ * @param log_callback Pointer to the new log callback function.
+ * @param user_data A pointer to data managed by the caller.
+ */
+void flashrom_set_log_callback_v2(flashrom_log_callback_v2 *log_callback, void* user_data);
+
enum flashrom_progress_stage {
FLASHROM_PROGRESS_READ,
FLASHROM_PROGRESS_WRITE,
diff --git a/libflashrom.c b/libflashrom.c
index 8cc38c4..5cc33b3 100644
--- a/libflashrom.c
+++ b/libflashrom.c
@@ -31,6 +31,8 @@
/** Pointer to log callback function. */
static flashrom_log_callback *global_log_callback = NULL;
+static flashrom_log_callback_v2 *global_log_callback_v2 = NULL;
+static void *global_log_user_data = NULL;
int flashrom_init(const int perform_selfcheck)
{
@@ -50,6 +52,49 @@
{
global_log_callback = log_callback;
}
+
+/** @private */
+static int format_message_and_invoke_log_callback_v2(enum flashrom_log_level level,
+ const char *format, va_list args)
+{
+ char message[LOG_MESSAGE_LENGTH_LIMIT] = {0};
+ int actual_len;
+
+ /* sanity check */
+ if (!global_log_callback_v2)
+ return -EINVAL;
+
+ actual_len = vsnprintf(message, sizeof(message), format, args);
+
+ if (actual_len < 0)
+ return actual_len;
+
+ global_log_callback_v2(level, message, global_log_user_data);
+
+ if ((size_t)actual_len >= sizeof(message)) {
+ snprintf(message, sizeof(message),
+ "%zu characters were truncated from the previous log message",
+ (size_t)actual_len - sizeof(message) + 1);
+ global_log_callback_v2(FLASHROM_MSG_WARN, message, global_log_user_data);
+ return -ERANGE;
+ }
+ return 0;
+}
+
+void flashrom_set_log_callback_v2(flashrom_log_callback_v2 *const log_callback, void* user_data)
+{
+ if (!log_callback) {
+ /* Reset v1 callback only if it was installed by flashrom_set_log_callback_v2 op */
+ if (global_log_callback == format_message_and_invoke_log_callback_v2) {
+ global_log_callback = NULL;
+ }
+ }
+ else
+ global_log_callback = format_message_and_invoke_log_callback_v2;
+ global_log_callback_v2 = log_callback;
+ global_log_user_data = user_data;
+}
+
/** @private */
int print(const enum flashrom_log_level level, const char *const fmt, ...)
{
diff --git a/libflashrom.map b/libflashrom.map
index 8e4635d..4ab9ac9 100644
--- a/libflashrom.map
+++ b/libflashrom.map
@@ -24,6 +24,7 @@
flashrom_programmer_init;
flashrom_programmer_shutdown;
flashrom_set_log_callback;
+ flashrom_set_log_callback_v2;
flashrom_set_progress_callback;
flashrom_set_progress_callback_v2;
flashrom_shutdown;
diff --git a/meson.build b/meson.build
index ba17fef..734afb6 100644
--- a/meson.build
+++ b/meson.build
@@ -652,6 +652,8 @@
cargs += '-DCONFIG_DEFAULT_PROGRAMMER_ARGS="' + config_default_programmer_args + '"'
+cargs += '-DLOG_MESSAGE_LENGTH_LIMIT=' + get_option('log_message_length_limit').to_string()
+
if get_option('llvm_cov').enabled()
cargs += ['-fprofile-instr-generate', '-fcoverage-mapping']
link_args += ['-fprofile-instr-generate', '-fcoverage-mapping']
diff --git a/meson_options.txt b/meson_options.txt
index 43b5117..2829cd1 100644
--- a/meson_options.txt
+++ b/meson_options.txt
@@ -27,3 +27,5 @@
description : 'Minimum time in microseconds to suspend execution for (rather than polling) when a delay is required.'
+ ' Larger values may perform better on machines with low timer resolution, at the cost of increased power.')
option('rpmc', type : 'feature', value : 'auto', description : 'Support for Replay Protected Monotonic Counter (RPMC) commands as specified by JESD260')
+option('log_message_length_limit', type : 'integer', min : 64, max : 1024, value : 256,
+ description : 'Log message length limit for v2 logging API')
diff --git a/tests/libflashrom.c b/tests/libflashrom.c
new file mode 100644
index 0000000..42a2da0
--- /dev/null
+++ b/tests/libflashrom.c
@@ -0,0 +1,66 @@
+/*
+ * This file is part of the flashrom project.
+ *
+ * Copyright 2025 Dmitry Zhadinets (dzhadinets(a)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
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+
+#include <stdlib.h>
+
+#include <include/test.h>
+#include "tests.h"
+#include "libflashrom.h"
+#include "flash.h"
+
+static int test_log_callback(enum flashrom_log_level level, const char *format,
+ va_list vargs)
+{
+ /* check that loglevel has passed corectly */
+ assert_int_equal(level, FLASHROM_MSG_INFO);
+ char message[3] = {0};
+ vsnprintf(message, 3, format, vargs);
+ assert_string_equal(message, "1\n");
+ return 0x666 + 1;
+}
+
+static void test_log_callback_v2(enum flashrom_log_level level,
+ const char *message, void *user_data)
+{
+ /* check that loglevel has passed corectly */
+ assert_int_equal(level, FLASHROM_MSG_ERROR);
+ /* check that user dta has passed */
+ assert_ptr_not_equal(user_data, 0);
+ /* check that user_data is correct */
+ assert_int_equal(*(int *)(user_data), 100500);
+ /* check that format is working correctly */
+ assert_string_equal(message, "2\n");
+ *(int*)user_data = 0x666 + 2;
+}
+
+void flashrom_set_log_callback_test_success(void **state)
+{
+ (void)state; /* unused */
+ flashrom_set_log_callback(test_log_callback);
+ /* check that callback is called */
+ assert_int_equal(print(FLASHROM_MSG_INFO, "1%s", "\n"), 0x666 + 1);
+ flashrom_set_log_callback(NULL);
+}
+
+void flashrom_set_log_callback_v2_test_success(void **state)
+{
+ (void)state; /* unused */
+ int user_data = 100500;
+ flashrom_set_log_callback_v2(test_log_callback_v2, &user_data);
+ print(FLASHROM_MSG_ERROR, "2%s", "\n");
+ /* check that callback is called */
+ assert_int_equal(user_data, 0x666 + 2);
+ flashrom_set_log_callback_v2(NULL, NULL);
+}
diff --git a/tests/meson.build b/tests/meson.build
index 66c9e10..a49278d 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -17,6 +17,7 @@
'libusb_wraps.c',
'helpers.c',
'flashrom.c',
+ 'libflashrom.c',
'spi25.c',
'lifecycle.c',
'layout.c',
diff --git a/tests/tests.c b/tests/tests.c
index bf5903d..1c72853 100644
--- a/tests/tests.c
+++ b/tests/tests.c
@@ -478,6 +478,12 @@
};
ret |= cmocka_run_group_tests_name("flashrom.c tests", flashrom_tests, NULL, NULL);
+ const struct CMUnitTest libflashrom_tests[] = {
+ cmocka_unit_test(flashrom_set_log_callback_test_success),
+ cmocka_unit_test(flashrom_set_log_callback_v2_test_success),
+ };
+ ret |= cmocka_run_group_tests_name("libflashrom.c tests", libflashrom_tests, NULL, NULL);
+
const struct CMUnitTest spi25_tests[] = {
cmocka_unit_test(spi_write_enable_test_success),
cmocka_unit_test(spi_write_disable_test_success),
diff --git a/tests/tests.h b/tests/tests.h
index 7cf3271..a71eded 100644
--- a/tests/tests.h
+++ b/tests/tests.h
@@ -31,6 +31,10 @@
/* flashrom.c */
void flashbuses_to_text_test_success(void **state);
+/* libflashrom.c */
+void flashrom_set_log_callback_test_success(void **state);
+void flashrom_set_log_callback_v2_test_success(void **state);
+
/* spi25.c */
void spi_write_enable_test_success(void **state);
void spi_write_disable_test_success(void **state);
--
To view, visit https://review.coreboot.org/c/flashrom/+/86875?usp=email
To unsubscribe, or for help writing mail filters, visit https://review.coreboot.org/settings?usp=email
Gerrit-MessageType: merged
Gerrit-Project: flashrom
Gerrit-Branch: main
Gerrit-Change-Id: Iea738bd371fa3d69b9cf222c89ee67490d30af39
Gerrit-Change-Number: 86875
Gerrit-PatchSet: 15
Gerrit-Owner: Dmitry Zhadinets <dzhadinets(a)gmail.com>
Gerrit-Reviewer: Anastasia Klimchuk <aklm(a)chromium.org>
Gerrit-Reviewer: Dmitry Zhadinets <dzhadinets(a)gmail.com>
Gerrit-Reviewer: Peter Marheine <pmarheine(a)chromium.org>
Gerrit-Reviewer: build bot (Jenkins) <no-reply(a)coreboot.org>
Attention is currently required from: Dmitry Zhadinets.
Anastasia Klimchuk has posted comments on this change by Dmitry Zhadinets. ( https://review.coreboot.org/c/flashrom/+/86875?usp=email )
Change subject: libflashrom: Update the API for Logger Callback
......................................................................
Patch Set 14:
(1 comment)
Patchset:
PS14:
> Do I need something else to do here?
Nothing for you to do, all good! this one and log level will be merged tomorrow
We usually have 3 days between full approve and merge: https://flashrom.org/dev_guide/development_guide.html#merge-checklist
--
To view, visit https://review.coreboot.org/c/flashrom/+/86875?usp=email
To unsubscribe, or for help writing mail filters, visit https://review.coreboot.org/settings?usp=email
Gerrit-MessageType: comment
Gerrit-Project: flashrom
Gerrit-Branch: main
Gerrit-Change-Id: Iea738bd371fa3d69b9cf222c89ee67490d30af39
Gerrit-Change-Number: 86875
Gerrit-PatchSet: 14
Gerrit-Owner: Dmitry Zhadinets <dzhadinets(a)gmail.com>
Gerrit-Reviewer: Anastasia Klimchuk <aklm(a)chromium.org>
Gerrit-Reviewer: Dmitry Zhadinets <dzhadinets(a)gmail.com>
Gerrit-Reviewer: Peter Marheine <pmarheine(a)chromium.org>
Gerrit-Reviewer: build bot (Jenkins) <no-reply(a)coreboot.org>
Gerrit-Attention: Dmitry Zhadinets <dzhadinets(a)gmail.com>
Gerrit-Comment-Date: Thu, 03 Apr 2025 03:42:07 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Comment-In-Reply-To: Dmitry Zhadinets <dzhadinets(a)gmail.com>
Attention is currently required from: Anastasia Klimchuk.
Stefan Reinauer has posted comments on this change by Anastasia Klimchuk. ( https://review.coreboot.org/c/flashrom/+/86953?usp=email )
Change subject: doc: Update supported flashchips page because we split the large file
......................................................................
Patch Set 2: Code-Review+2
(2 comments)
File doc/supported_hw/supported_flashchips.rst:
https://review.coreboot.org/c/flashrom/+/86953/comment/f520e43a_50253a51?us… :
PS2, Line 5: is in
is in the
https://review.coreboot.org/c/flashrom/+/86953/comment/862af5bf_f2deb7e6?us… :
PS2, Line 10: in ``/flashchips`` directory
All the files in the ``/flashchips`` directory are included in the parent file ``flashchips.c``. You can inspect the source
`here <https://github.com/flashrom/flashrom/blob/main/flashchips.c>`_.
--
To view, visit https://review.coreboot.org/c/flashrom/+/86953?usp=email
To unsubscribe, or for help writing mail filters, visit https://review.coreboot.org/settings?usp=email
Gerrit-MessageType: comment
Gerrit-Project: flashrom
Gerrit-Branch: main
Gerrit-Change-Id: Ic6179517d0f951a32c0c4e0baf32677398224542
Gerrit-Change-Number: 86953
Gerrit-PatchSet: 2
Gerrit-Owner: Anastasia Klimchuk <aklm(a)chromium.org>
Gerrit-Reviewer: Stefan Reinauer <stefan.reinauer(a)coreboot.org>
Gerrit-Reviewer: build bot (Jenkins) <no-reply(a)coreboot.org>
Gerrit-Attention: Anastasia Klimchuk <aklm(a)chromium.org>
Gerrit-Comment-Date: Thu, 03 Apr 2025 03:07:50 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: Yes
Dmitry Zhadinets has posted comments on this change by Dmitry Zhadinets. ( https://review.coreboot.org/c/flashrom/+/86875?usp=email )
Change subject: libflashrom: Update the API for Logger Callback
......................................................................
Patch Set 14: Code-Review+1
(1 comment)
Patchset:
PS14:
Do I need something else to do here?
--
To view, visit https://review.coreboot.org/c/flashrom/+/86875?usp=email
To unsubscribe, or for help writing mail filters, visit https://review.coreboot.org/settings?usp=email
Gerrit-MessageType: comment
Gerrit-Project: flashrom
Gerrit-Branch: main
Gerrit-Change-Id: Iea738bd371fa3d69b9cf222c89ee67490d30af39
Gerrit-Change-Number: 86875
Gerrit-PatchSet: 14
Gerrit-Owner: Dmitry Zhadinets <dzhadinets(a)gmail.com>
Gerrit-Reviewer: Anastasia Klimchuk <aklm(a)chromium.org>
Gerrit-Reviewer: Dmitry Zhadinets <dzhadinets(a)gmail.com>
Gerrit-Reviewer: Peter Marheine <pmarheine(a)chromium.org>
Gerrit-Reviewer: build bot (Jenkins) <no-reply(a)coreboot.org>
Gerrit-Comment-Date: Thu, 03 Apr 2025 02:53:43 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: Yes
Attention is currently required from: Dmitry Zhadinets.
Peter Marheine has posted comments on this change by Dmitry Zhadinets. ( https://review.coreboot.org/c/flashrom/+/86875?usp=email )
Change subject: libflashrom: Update the API for Logger Callback
......................................................................
Patch Set 14: Code-Review+2
--
To view, visit https://review.coreboot.org/c/flashrom/+/86875?usp=email
To unsubscribe, or for help writing mail filters, visit https://review.coreboot.org/settings?usp=email
Gerrit-MessageType: comment
Gerrit-Project: flashrom
Gerrit-Branch: main
Gerrit-Change-Id: Iea738bd371fa3d69b9cf222c89ee67490d30af39
Gerrit-Change-Number: 86875
Gerrit-PatchSet: 14
Gerrit-Owner: Dmitry Zhadinets <dzhadinets(a)gmail.com>
Gerrit-Reviewer: Anastasia Klimchuk <aklm(a)chromium.org>
Gerrit-Reviewer: Peter Marheine <pmarheine(a)chromium.org>
Gerrit-Reviewer: build bot (Jenkins) <no-reply(a)coreboot.org>
Gerrit-Attention: Dmitry Zhadinets <dzhadinets(a)gmail.com>
Gerrit-Comment-Date: Wed, 02 Apr 2025 02:59:49 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes