refactor(core/bootloader): extract PB helper macros into separate file
What changed, and why it matters
This change simply moves a set of helper macros from one file to a new shared header file. The macros themselves are unchanged, and no program behavior is modified. It is a routine code cleanup (refactoring) with no security relevance.
No action required; this is a non-functional refactor.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit extracts PB (nanopb/protobuf) helper macros such as MSG_SEND_INIT, MSG_SEND_ASSIGN_VALUE, MSG_SEND_ASSIGN_STRING, MSG_SEND_ASSIGN_BYTES, MSG_SEND, MSG_RECV_INIT, MSG_RECV_CALLBACK, and MSG_RECV from core/embed/projects/bootloader/protob/protob.c into a new header core/embed/projects/bootloader/protob/protob_common.h. The .c file now includes the new header. The macro definitions are byte-for-byte identical to the originals; no logic, bounds, or control flow changed.
Changed components
core/embed/projects/bootloader/protob/protob.ccore/embed/projects/bootloader/protob/protob_common.hInspect captured patch +63 / −42
diff --git a/core/embed/projects/bootloader/protob/protob.c b/core/embed/projects/bootloader/protob/protob.c
index 89d472c3..3c3db7d1 100644
--- a/core/embed/projects/bootloader/protob/protob.c
+++ b/core/embed/projects/bootloader/protob/protob.c
@@ -37,51 +37,10 @@
#include "memzero.h"
#include "pb/messages.pb.h"
#include "protob.h"
+#include "protob_common.h"
#include "version.h"
#include "wire/codec_v1.h"
-#define MSG_SEND_INIT(TYPE) TYPE msg_send = TYPE##_init_default
-#define MSG_SEND_ASSIGN_REQUIRED_VALUE(FIELD, VALUE) \
- { msg_send.FIELD = VALUE; }
-#define MSG_SEND_ASSIGN_VALUE(FIELD, VALUE) \
- { \
- msg_send.has_##FIELD = true; \
- msg_send.FIELD = VALUE; \
- }
-#define MSG_SEND_ASSIGN_STRING(FIELD, VALUE) \
- { \
- msg_send.has_##FIELD = true; \
- memzero(msg_send.FIELD, sizeof(msg_send.FIELD)); \
- strncpy(msg_send.FIELD, VALUE, sizeof(msg_send.FIELD) - 1); \
- }
-#define MSG_SEND_ASSIGN_STRING_LEN(FIELD, VALUE, LEN) \
- { \
- msg_send.has_##FIELD = true; \
- memzero(msg_send.FIELD, sizeof(msg_send.FIELD)); \
- strncpy(msg_send.FIELD, VALUE, MIN(LEN, sizeof(msg_send.FIELD) - 1)); \
- }
-#define MSG_SEND_ASSIGN_BYTES(FIELD, VALUE, LEN) \
- { \
- msg_send.has_##FIELD = true; \
- memzero(msg_send.FIELD.bytes, sizeof(msg_send.FIELD.bytes)); \
- memcpy(msg_send.FIELD.bytes, VALUE, \
- MIN(LEN, sizeof(msg_send.FIELD.bytes))); \
- msg_send.FIELD.size = MIN(LEN, sizeof(msg_send.FIELD.bytes)); \
- }
-#define MSG_SEND(TYPE) \
- codec_send_msg(iface->wire, MessageType_MessageType_##TYPE, TYPE##_fields, \
- &msg_send)
-
-#define MSG_RECV_INIT(TYPE) TYPE msg_recv = TYPE##_init_default
-#define MSG_RECV_CALLBACK(FIELD, CALLBACK, ARGUMENT) \
- { \
- msg_recv.FIELD.funcs.decode = &CALLBACK; \
- msg_recv.FIELD.arg = (void *)ARGUMENT; \
- }
-#define MSG_RECV(TYPE) \
- codec_recv_message(iface->wire, iface->msg_size, iface->buf, TYPE##_fields, \
- &msg_recv)
-
secbool send_user_abort(protob_io_t *iface, const char *msg) {
MSG_SEND_INIT(Failure);
MSG_SEND_ASSIGN_VALUE(code, FailureType_Failure_ActionCancelled);
diff --git a/core/embed/projects/bootloader/protob/protob_common.h b/core/embed/projects/bootloader/protob/protob_common.h
new file mode 100644
index 00000000..884ccdab
--- /dev/null
+++ b/core/embed/projects/bootloader/protob/protob_common.h
@@ -0,0 +1,62 @@
+/*
+ * This file is part of the Trezor project, https://trezor.io/
+ *
+ * Copyright (c) SatoshiLabs
+ *
+ * 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, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * 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.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#pragma once
+
+#define MSG_SEND_INIT(TYPE) TYPE msg_send = TYPE##_init_default
+#define MSG_SEND_ASSIGN_REQUIRED_VALUE(FIELD, VALUE) \
+ { msg_send.FIELD = VALUE; }
+#define MSG_SEND_ASSIGN_VALUE(FIELD, VALUE) \
+ { \
+ msg_send.has_##FIELD = true; \
+ msg_send.FIELD = VALUE; \
+ }
+#define MSG_SEND_ASSIGN_STRING(FIELD, VALUE) \
+ { \
+ msg_send.has_##FIELD = true; \
+ memzero(msg_send.FIELD, sizeof(msg_send.FIELD)); \
+ strncpy(msg_send.FIELD, VALUE, sizeof(msg_send.FIELD) - 1); \
+ }
+#define MSG_SEND_ASSIGN_STRING_LEN(FIELD, VALUE, LEN) \
+ { \
+ msg_send.has_##FIELD = true; \
+ memzero(msg_send.FIELD, sizeof(msg_send.FIELD)); \
+ strncpy(msg_send.FIELD, VALUE, MIN(LEN, sizeof(msg_send.FIELD) - 1)); \
+ }
+#define MSG_SEND_ASSIGN_BYTES(FIELD, VALUE, LEN) \
+ { \
+ msg_send.has_##FIELD = true; \
+ memzero(msg_send.FIELD.bytes, sizeof(msg_send.FIELD.bytes)); \
+ memcpy(msg_send.FIELD.bytes, VALUE, \
+ MIN(LEN, sizeof(msg_send.FIELD.bytes))); \
+ msg_send.FIELD.size = MIN(LEN, sizeof(msg_send.FIELD.bytes)); \
+ }
+#define MSG_SEND(TYPE) \
+ codec_send_msg(iface->wire, MessageType_MessageType_##TYPE, TYPE##_fields, \
+ &msg_send)
+
+#define MSG_RECV_INIT(TYPE) TYPE msg_recv = TYPE##_init_default
+#define MSG_RECV_CALLBACK(FIELD, CALLBACK, ARGUMENT) \
+ { \
+ msg_recv.FIELD.funcs.decode = &CALLBACK; \
+ msg_recv.FIELD.arg = (void *)ARGUMENT; \
+ }
+#define MSG_RECV(TYPE) \
+ codec_recv_message(iface->wire, iface->msg_size, iface->buf, TYPE##_fields, \
+ &msg_recv)
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.