usb: gadget: f_sdp: honour a closed i.MX part, bound ep0 data
Subsystems (4)
cover usb: gadget: f_sdp: honour a closed i.MX part, bound ep0 data
The SDP gadget lets SPL, and the sdp command, act as the i.MX boot ROM's serial download mode: a host on the USB port writes files to memory and jumps to them. On a part whose secure boot is fused closed, the ROM only runs an image it has authenticated. The gadget does not
ask: SDP_JUMP_ADDRESS first calls the entry of any IVT it finds at the given address, before the FIT, container and legacy paths that authenticate, and SDP_WRITE_FILE and SDP_READ_REGISTER take any address. A closed part that boots SPL with SPL_USB_SDP_SUPPORT will therefore run unsigned code from the USB host, past the chain of trust that HAB or AHAB set up. Separately, two ep0 requests take the length of their data stage from the host's wLength alone, against a 4 KiB buffer.
Patch 1 bounds every control request's data stage to the ep0 buffer in composite_setup(), as Linux commit 36dfdf11af49 ("USB: gadget: detect too-big endpoint 0 requests") and barebox do: an OUT request that is too long is stalled, an IN request is shortened. This covers the SDP report 2 SET_REPORT and the OS descriptor vendor request, which clears and returns wLength bytes of the 4 KiB buffer.
Patches 2 and 3 fix the SDP data path on every part: a zero-length report 2 packet made the copy length -1, a report 2 sent outside a download was copied to wherever the last one ended, and the padding of the last packet was copied past the announced length.
Patch 4 converts the command's address and count once instead of in each command, with no change in behaviour. It makes room in SPL for the checks of patch 6 on imx6dl_mamoj, whose SPL is close to its size limit.
Patch 5 adds sdp_is_closed(), a weak hook (false by default) with implementations for HAB parts (imx_hab_is_enabled()) and ELE AHAB parts (OEM closed or locked lifecycle), and reports SDP_SECURITY_CLOSED as the ROM does. Patch 6 then, on a closed part only, skips the raw IVT jump, takes an image only by a path that authenticates it, refuses READ_REGISTER, and receives and drops a WRITE_FILE that lies below DRAM or touches the loader's own BSS, heaps, device tree, page tables or stack. A legacy image is taken only when its header starts the range that jump_to_image() authenticates, since its entry point comes from that header. Refusals are answered with a failure in report 4 and the gadget waits for the next command; where SPL searches for a FIT or container header and finds none, the jump ends in the existing panic. On an open part, and on every other platform, the SDP flows of imx_usb_loader and uuu are unchanged: every new check sits behind sdp_is_closed().
There is no sandbox UDC, and the closed paths need fused hardware, so the series was checked by building the SPL SDP boards before and after it with the toolchain CI uses (gcc 14.2), with and without the part's secure boot option:
defconfig secure boot SPL bytes before -> after imx6dl_mamoj IMX_HAB (*) 58800 -> 59088 apalis_imx6 - 46928 -> 47224 apalis_imx6 IMX_HAB 49864 -> 50208 colibri_imx6 - 43280 -> 43576 colibri_imx6 IMX_HAB 46248 -> 46560 imx8mm_evk - 152448 -> 152400 imx8mm_evk IMX_HAB 155224 -> 155704 verdin-imx8mm - 156336 -> 156288 verdin-imx8mm IMX_HAB 159112 -> 159576 imx952_evk - 125248 -> 125696 imx952_evk AHAB_BOOT 129088 -> 129520 qcom_sdm845_spl - 242032 -> 242192
(*) in its defconfig
All build without new warnings, and imx6dl_mamoj, apalis_imx6, imx8mm_evk and imx952_evk build at patches 4 and 5 too. The SPL links the weak sdp_is_closed() without the option and the i.MX one with it; on the imx8mm boards, where LTO inlines it, the SDP code calls imx_hab_is_enabled() only with IMX_HAB. The tightest are imx6dl_mamoj, whose u-boot-spl.bin grows from 61017 to 61305 bytes against its limit of 61344, and verdin-imx8mm with IMX_HAB, which keeps 168 bytes of its SPL's on-chip RAM, from 632; this is also why the refusals print nothing.
checkpatch flags the two #if in patch 6: they guard gd members that only exist under those options, so IS_ENABLED() cannot replace them.
CI: the series passes all 91 jobs of U-Boot's Azure pipeline, in build 13725 of my CI-only pull request, on main e43c38e0021.
PR: https://github.com/u-boot/u-boot/pull/1064 Build: https://dev.azure.com/u-boot/u-boot/_build/results?buildId=13725
Ana Bobillo (6):
usb: gadget: composite: bound ep0 requests to the request buffer
usb: gadget: f_sdp: drop an empty data packet
usb: gadget: f_sdp: write report 2 data only within a download
usb: gadget: f_sdp: decode the command's address and count once
usb: gadget: f_sdp: report a closed part as the boot ROM does
usb: gadget: f_sdp: refuse unauthenticated loads on a closed part
arch/arm/mach-imx/ele_ahab.c | 13 +++ arch/arm/mach-imx/hab.c | 7 ++ drivers/usb/gadget/composite.c | 16 +++ drivers/usb/gadget/f_sdp.c | 175 +++++++++++++++++++++++++++++---- include/sdp.h | 19 ++++ 5 files changed, 212 insertions(+), 18 deletions(-)
base-commit: e43c38e00218eae1a7b4a683c0fef8973d838fa3
PATCH 1/6 usb: gadget: composite: bound ep0 requests to the request buffer
From: Ana Bobillo <[email protected]>
composite_setup() hands every control request's wLength on, unchecked, as the data stage length, while the ep0 request it queues, cdev->req, has a buffer of USB_BUFSIZ (4096) bytes. Two handlers take that length as it comes: the OS descriptor vendor request clears and returns wLength bytes of that buffer, and the SDP function's report 2 SET_REPORT queues wLength bytes into it. A host sending a longer wLength has the data stage run past the end of the buffer.
Linux closed this for all of its composite functions in one place, commit 36dfdf11af49 ("USB: gadget: detect too-big endpoint 0 requests"), and barebox carries the same check: stall an OUT request longer than the buffer, and shorten an IN request to it. Do the same here, so no function behind the composite layer can be handed a data stage the buffer does not hold.
No request a gadget function handles needs more: the largest legitimate data stages are DFU's DNLOAD/UPLOAD at DFU_USB_BUFSIZ (4096) and the SDP report 2 of 1025 bytes.
--- drivers/usb/gadget/composite.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)
drivers/usb/gadget/composite.c +16 -0
@@ -1019,6 +1019,22 @@ composite_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)u8 endp;struct usb_configuration *c; +/* +* The data stage of every request handled here, or by a function +* through cdev->req, goes through the USB_BUFSIZ ep0 buffer. +*/ +if (w_length >USB_BUFSIZ ) { +if (ctrl->bRequestType &USB_DIR_IN ) { +/* Cast away the const, we are going to overwrite on purpose. */ +__le16 *temp = (__le16 *)&ctrl->wLength; + + *temp =cpu_to_le16 (USB_BUFSIZ ); + w_length =USB_BUFSIZ ; + }else { + goto done; + } + } +/* * partial re-init of the response message; the function or the * gadget might need to intercept e.g. a control-OUT completion
Re: [PATCH 1/6] usb: gadget: composite: bound ep0 requests to the request buffer
Hi Ana,
Thank you for the patch.
Quoted 11 lines
composite_setup()hands every control request's wLength on, unchecked, as the data stage length, while the ep0 request it queues,cdev->req, has a buffer ofUSB_BUFSIZ(4096) bytes. Two handlers take that length as it comes: the OS descriptor vendor request clears and returns wLength bytes of that buffer, and the SDP function's report 2SET_REPORTqueues wLength bytes into it. A host sending a longer wLength has the data stage run past the end of the buffer.Linux closed this for all of its composite functions in one place, commit
36dfdf11af49("USB: gadget: detect too-big endpoint 0 requests"), and barebox carries the same check: stall an OUT request
I've searched for the mentioned commit sha in the linux (36dfdf11af49) git history:
$ git show 36dfdf11af49 fatal: ambiguous argument '36dfdf11af49': unknown revision or path not in the working tree. Use '--' to separate paths from revisions, like this: 'git <command> [<revision>...] -- [<file>...]'
This is on master (commit 666539eecdf4 ("Merge tag 'assoc-array-fixes-v2'"))
Looking a bit more, I've found the mentioned one but it has a different sha:
commit 153a2d7e3350 ("USB: gadget: detect too-big endpoint 0 requests")
Can we please update the commit message?
The change looks good.
Signature · 1 line
Mattijs Korpershoek <[email protected]>
Re: [PATCH 1/6] usb: gadget: composite: bound ep0 requests to the request buffer
Hi Mattijs,
Quoted 6 lines · Mattijs Korpershoek
Looking a bit more, I've found the mentioned one but it has a different sha:
commit
153a2d7e3350("USB: gadget: detect too-big endpoint 0 requests")Can we please update the commit message?
You're right, sorry: 36dfdf11af49 is a stable backport of it, and 153a2d7e3350 is the mainline commit. The paragraph should read:
Linux closed this for all of its composite functions in one place, commit 153a2d7e3350 ("USB: gadget: detect too-big endpoint 0 requests"), and barebox carries the same check: stall an OUT request
As I wrote to Tom on 10 November [1], I am not sending new revisions of my series, so if you apply this one, could you make that change while applying, as with the Fixes tags on 6/6?
Thank you for the review.
[1] https://lists.u-boot-project.org/pipermail/u-boot/2026-November/639858.html
Ana
Re: [PATCH 1/6] usb: gadget: composite: bound ep0 requests to the request buffer
Hi Ana,
Quoted 18 lines · Ana Bobillo
Hi Mattijs,
Quoted 6 lines · Mattijs Korpershoek
Looking a bit more, I've found the mentioned one but it has a different sha:
commit
153a2d7e3350("USB: gadget: detect too-big endpoint 0 requests")Can we please update the commit message?
You're right, sorry:
36dfdf11af49is a stable backport of it, and153a2d7e3350is the mainline commit. The paragraph should read:Linux closed this for all of its composite functions in one place, commit
153a2d7e3350("USB: gadget: detect too-big endpoint 0 requests"), and barebox carries the same check: stall an OUT request
Quoted 7 lines
As I wrote to Tom on 10 November [1], I am not sending new revisions of my series, so if you apply this one, could you make that change while applying, as with the Fixes tags on 6/6?
Thank you for the review.
[1] https://lists.u-boot-project.org/pipermail/u-boot/2026-November/639858.html
Now that I read the above thread, I am quite unhappy. It's disrepectful of my (and other maintainers) time to have not at least done a human-review before submitting this.
If anyone else has any interest in getting this series merged, please help me review/test this. It won't get merged if I don't hear from anyone else on this.
Thanks
Mattijs
Quoted 2 lines
Ana
PATCH 2/6 usb: gadget: f_sdp: drop an empty data packet
In reply to Ana BobilloFrom: Ana Bobillo <[email protected]>
sdp_rx_data_complete() takes the payload length of a report 2 packet as req->actual - 1, one byte for the report ID. A completed transfer of zero bytes makes that -1, which passes the dnl_bytes_remaining check as a huge unsigned value and is then given to memcpy() as the copy length, reading and writing far past both buffers. Whether it gets there depends only on the stale report ID byte left in the request buffer by the previous packet.
Drop a packet too short to carry its report ID before acting on that byte, and wait for the next one.
--- drivers/usb/gadget/f_sdp.c | 7 +++++++ 1 file changed, 7 insertions(+)
drivers/usb/gadget/f_sdp.c +7 -0
@@ -386,6 +386,13 @@ static void sdp_rx_data_complete(struct usb_ep *ep, struct usb_request *req)return ; } +/* A report 2 packet carries at least its report ID: drop it */ +if (req->actual <1 ) { +if (sdp->state ==SDP_STATE_RX_FILE_DATA_BUSY ) + sdp->state =SDP_STATE_RX_FILE_DATA ; +return ; + } +if (report !=2 ) {pr_err ("Unexpected report %d\n" , report);return ;
Re: [PATCH 2/6] usb: gadget: f_sdp: drop an empty data packet
Quoted 9 lines · Ana Bobillo
sdp_rx_data_complete()takes the payload length of a report 2 packet asreq->actual- 1, one byte for the report ID. A completed transfer of zero bytes makes that -1, which passes thednl_bytes_remainingcheck as a huge unsigned value and is then given tomemcpy()as the copy length, reading and writing far past both buffers. Whether it gets there depends only on the stale report ID byte left in the request buffer by the previous packet.[...]
Signature · 1 line
Mattijs Korpershoek <[email protected]>
PATCH 3/6 usb: gadget: f_sdp: write report 2 data only within a download
In reply to Ana BobilloFrom: Ana Bobillo <[email protected]>
sdp_setup() switches to SDP_STATE_RX_FILE_DATA_BUSY on every report 2 SET_REPORT, whatever the state, and sdp_rx_data_complete() then copies the packet to dnl_address. Two things follow:
- a report 2 sent outside a download (after the previous WRITE_FILE completed, or during a DCD_WRITE) is still copied to dnl_address, where the last download ended, or wherever an earlier command left it;
- the last packet of a download is copied whole, padding included, although dnl_bytes_remaining is clamped to zero: up to a packet past the length the host announced in WRITE_FILE.
Accept report 2 only while a WRITE_FILE or DCD_WRITE expects data and stall it otherwise, and copy no more than the bytes remaining. DCD data is still consumed without being written, as the DCD commands are stubs. The host tools (imx_usb_loader, uuu) send data only after the command and pad only the last packet, so their flows are unchanged.
--- drivers/usb/gadget/f_sdp.c | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-)
drivers/usb/gadget/f_sdp.c +13 -6
@@ -402,12 +402,11 @@ static void sdp_rx_data_complete(struct usb_ep *ep, struct usb_request *req)/* * Some USB stacks require to send a complete buffer as * specified in the HID descriptor. This leads to longer -* transfers than the file length, no problem for us. +* transfers than the file length: drop the padding. */ - sdp->dnl_bytes_remaining =0 ; - }else { - sdp->dnl_bytes_remaining -= datalen; + datalen = sdp->dnl_bytes_remaining; } + sdp->dnl_bytes_remaining -= datalen;if (sdp->state ==SDP_STATE_RX_FILE_DATA_BUSY ) {memcpy (sdp_ptr (sdp->dnl_address), req->buf +1 , datalen);
@@ -415,7 +414,9 @@ static void sdp_rx_data_complete(struct usb_ep *ep, struct usb_request *req) }if (sdp->dnl_bytes_remaining) { - sdp->state =SDP_STATE_RX_FILE_DATA ; +/* DCD data is consumed, never written to dnl_address */ +if (sdp->state ==SDP_STATE_RX_FILE_DATA_BUSY ) + sdp->state =SDP_STATE_RX_FILE_DATA ;return ; }
@@ -509,9 +510,15 @@ static int sdp_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl) sdp_func->ep_int_enable = false;break ;case 2 : +/* Data only follows a WRITE_FILE or DCD_WRITE */ +if (sdp_func->state ==SDP_STATE_RX_FILE_DATA || + sdp_func->state ==SDP_STATE_RX_FILE_DATA_BUSY ) + sdp_func->state =SDP_STATE_RX_FILE_DATA_BUSY ; +else if (sdp_func->state !=SDP_STATE_RX_DCD_DATA ) +return -EOPNOTSUPP ; + value = len; req->complete = sdp_rx_data_complete; - sdp_func->state =SDP_STATE_RX_FILE_DATA_BUSY ;break ; } }
Re: [PATCH 3/6] usb: gadget: f_sdp: write report 2 data only within a download
Quoted 12 lines · Ana Bobillo
sdp_setup()switches toSDP_STATE_RX_FILE_DATA_BUSYon every report 2SET_REPORT, whatever the state, andsdp_rx_data_complete()then copies the packet todnl_address. Two things follow:- a report 2 sent outside a download (after the previous
WRITE_FILEcompleted, or during aDCD_WRITE) is still copied todnl_address, where the last download ended, or wherever an earlier command left it;
- the last packet of a download is copied whole, padding included, althoughdnl_bytes_remainingis clamped to zero: up to a packet past the length the host announced inWRITE_FILE.
Quoted 1 line
[...]
Signature · 1 line
Mattijs Korpershoek <[email protected]>
PATCH 4/6 usb: gadget: f_sdp: decode the command's address and count once
In reply to Ana BobilloFrom: Ana Bobillo <[email protected]>
sdp_rx_command_complete() converts the big-endian addr and cnt fields of the command in each case that uses them. struct sdp_command is packed and starts one byte into the report, so every conversion is a run of byte loads: six of them across four commands.
Convert both fields once, before the switch. No functional change: an addr of zero still selects CONFIG_SDP_LOADADDR. This takes 96 bytes off the SPL of imx6dl_mamoj, room the closed-part checks later in this series need to stay within that board's SPL size limit.
--- drivers/usb/gadget/f_sdp.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-)
drivers/usb/gadget/f_sdp.c +9 -8
@@ -306,10 +306,11 @@ static void sdp_rx_command_complete(struct usb_ep *ep, struct usb_request *req) }struct sdp_command *cmd = req->buf +1 ; +u32 addr =be32_to_cpu (cmd->addr); +u32 cnt =be32_to_cpu (cmd->cnt);debug ("%s: command: %04x, addr: %08x, cnt: %u\n" , - __func__,be16_to_cpu (cmd->cmd), -be32_to_cpu (cmd->addr),be32_to_cpu (cmd->cnt)); + __func__,be16_to_cpu (cmd->cmd), addr, cnt);switch (be16_to_cpu (cmd->cmd)) {case SDP_READ_REGISTER :
@@ -317,8 +318,8 @@ static void sdp_rx_command_complete(struct usb_ep *ep, struct usb_request *req) sdp->error_status =0x0 ; sdp->state =SDP_STATE_TX_SEC_CONF ; - sdp->dnl_address =be32_to_cpu (cmd->addr); - sdp->dnl_bytes_remaining =be32_to_cpu (cmd->cnt); + sdp->dnl_address = addr; + sdp->dnl_bytes_remaining = cnt; sdp->next_state =SDP_STATE_TX_REGISTER ;printf ("Reading %d registers at 0x%08x... " , sdp->dnl_bytes_remaining, sdp->dnl_address);
@@ -328,8 +329,8 @@ static void sdp_rx_command_complete(struct usb_ep *ep, struct usb_request *req) sdp->error_status =SDP_WRITE_FILE_COMPLETE ; sdp->state =SDP_STATE_RX_FILE_DATA ; - sdp->dnl_address = cmd->addr ?be32_to_cpu (cmd->addr) :CONFIG_SDP_LOADADDR ; - sdp->dnl_bytes_remaining =be32_to_cpu (cmd->cnt); + sdp->dnl_address = addr ? addr :CONFIG_SDP_LOADADDR ; + sdp->dnl_bytes_remaining = cnt; sdp->dnl_bytes = sdp->dnl_bytes_remaining; sdp->next_state =SDP_STATE_IDLE ;
@@ -349,14 +350,14 @@ static void sdp_rx_command_complete(struct usb_ep *ep, struct usb_request *req) sdp->error_status =SDP_WRITE_REGISTER_COMPLETE ; sdp->state =SDP_STATE_RX_DCD_DATA ; - sdp->dnl_bytes_remaining =be32_to_cpu (cmd->cnt); + sdp->dnl_bytes_remaining = cnt; sdp->next_state =SDP_STATE_IDLE ;break ;case SDP_JUMP_ADDRESS : sdp->always_send_status = false; sdp->error_status =0 ; - sdp->jmp_address = cmd->addr ?be32_to_cpu (cmd->addr) :CONFIG_SDP_LOADADDR ; + sdp->jmp_address = addr ? addr :CONFIG_SDP_LOADADDR ; sdp->state =SDP_STATE_TX_SEC_CONF ; sdp->next_state =SDP_STATE_JUMP ;break ;
Re: [PATCH 4/6] usb: gadget: f_sdp: decode the command's address and count once
Quoted 11 lines · Ana Bobillo
sdp_rx_command_complete()converts the big-endian addr and cnt fields of the command in each case that uses them. structsdp_commandis packed and starts one byte into the report, so every conversion is a run of byte loads: six of them across four commands.Convert both fields once, before the switch. No functional change: an addr of zero still selects
CONFIG_SDP_LOADADDR. This takes 96 bytes off the SPL ofimx6dl_mamoj, room the closed-part checks later in this series need to stay within that board's SPL size limit.[...]
Signature · 1 line
Mattijs Korpershoek <[email protected]>
PATCH 5/6 usb: gadget: f_sdp: report a closed part as the boot ROM does
In reply to Ana BobilloFrom: Ana Bobillo <[email protected]>
The SDP gadget implements the serial download protocol of the i.MX boot ROM, but always reports SDP_SECURITY_OPEN in its HAB security report (report 3), also on a part whose secure boot is fused closed, where the ROM reports SDP_SECURITY_CLOSED.
Add sdp_is_closed(), a weak hook that says whether secure boot is enforced, false by default so that other platforms are unchanged, and implement it for i.MX:
- HAB parts (i.MX6/7/7ULP/8M): imx_hab_is_enabled(), the secure boot fuse minus a field return; a fuse read error counts as closed;
- ELE AHAB parts (i.MX8ULP/9): the OEM closed and OEM locked lifecycles, and on i.MX94/95/952 also OEM secure world closed, read from the same FSB register as ahab_status.
The SECO AHAB parts (i.MX8QM/QXP) keep the weak default; none of their in-tree defconfigs enables SDP.
Use it for the security report. The next patch makes the gadget refuse what the ROM refuses on a closed part.
--- arch/arm/mach-imx/ele_ahab.c | 13 +++++++++++++ arch/arm/mach-imx/hab.c | 7 +++++++ drivers/usb/gadget/f_sdp.c | 7 ++++++- include/sdp.h | 19 +++++++++++++++++++ 4 files changed, 45 insertions(+), 1 deletion(-)
arch/arm/mach-imx/ele_ahab.c +13 -0
@@ -14,6 +14,7 @@#include <asm/arch/sys_proto.h> #include <console.h> #include <cpu_func.h> +#include <sdp.h> #include <asm/global_data.h> DECLARE_GLOBAL_DATA_PTR ;
@@ -414,6 +415,8 @@ static int do_authenticate(struct cmd_tbl *cmdtp, int flag, int argc,#if IS_ENABLED (CONFIG_IMX95 ) ||IS_ENABLED (CONFIG_IMX94 ) ||IS_ENABLED (CONFIG_IMX952 )#define FSB_LC_OFFSET 0x414#define LC_OEM_OPEN 0x10 +/* OEM secure world closed, OEM closed, OEM locked */ +#define LC_OEM_CLOSED (0x20 | 0x40 | 0x80)static void display_life_cycle (u32 lc) {printf ("Lifecycle: 0x%08X, " , lc);
@@ -459,6 +462,8 @@ static void display_life_cycle(u32 lc)#else #define FSB_LC_OFFSET 0x41c#define LC_OEM_OPEN 0x8 +/* OEM closed, OEM locked */ +#define LC_OEM_CLOSED (0x20 | 0x100)static void display_life_cycle (u32 lc) {printf ("Lifecycle: 0x%08X, " , lc);
@@ -497,6 +502,14 @@ static void display_life_cycle(u32 lc) }#endif +/* Secure boot is enforced: the SDP gadget then behaves as the ROM's does */ +bool sdp_is_closed (void ) +{ +u32 lc =readl (FSB_BASE_ADDR +FSB_LC_OFFSET ) &0x3ff ; + +return lc &LC_OEM_CLOSED ; +} +static int confirm_close (void ) { puts("Warning: Please ensure your sample is in NXP closed state, "
arch/arm/mach-imx/hab.c +7 -0
@@ -16,6 +16,7 @@#include <asm/arch/sys_proto.h> #include <asm/mach-imx/hab.h> #include <linux/arm-smccc.h> +#include <sdp.h> DECLARE_GLOBAL_DATA_PTR ;
@@ -932,6 +933,12 @@ bool imx_hab_is_enabled(void)return is_enabled; } +/* Secure boot is enforced: the SDP gadget then behaves as the ROM's does */ +bool sdp_is_closed (void ) +{ +return imx_hab_is_enabled (); +} +int imx_hab_authenticate_image (uint32_t ddr_start,uint32_t image_size,uint32_t ivt_offset) {
drivers/usb/gadget/f_sdp.c +6 -1
@@ -288,6 +288,11 @@ static inline void *sdp_ptr(u32 val)return (void *)(uintptr_t )val; } +__weak bool sdp_is_closed (void ) +{ +return false; +} +static void sdp_rx_command_complete (struct usb_ep *ep,struct usb_request *req) {struct f_sdp *sdp = req->context;
@@ -801,7 +806,7 @@ static int sdp_handle_in_ep(struct spl_image_info *spl_image,debug ("Report 3: HAB security\n" ); data[0 ] =3 ; - status =SDP_SECURITY_OPEN ; + status =sdp_is_closed () ?SDP_SECURITY_CLOSED :SDP_SECURITY_OPEN ;memcpy (&data[1 ], &status,4 ); sdp_func->in_req->length =5 ;usb_ep_queue (sdp_func->in_ep, sdp_func->in_req,0 );
include/sdp.h +19 -0
@@ -9,8 +9,27 @@#ifndef __SDP_H_#define __SDP_H_ +#include <linux/types.h> + +struct udevice ; +int sdp_init (struct udevice *udc); +/** +* sdp_is_closed() - Tell whether secure boot is enforced on this part +* +* The SDP gadget emulates the i.MX boot ROM's serial download mode. On a +* part with secure boot fused (HAB closed, AHAB OEM closed), the ROM refuses +* the operations that would bypass the signature chain, and SDP has to do the +* same, or it is a way around secure boot on an otherwise locked device. +* +* The weak default says open; i.MX code overrides it per secure boot +* mechanism. +* +* Return: true if the part is closed, false otherwise +*/ +bool sdp_is_closed (void ); +#ifdef CONFIG_XPL_BUILD #include <spl.h>
Re: [PATCH 5/6] usb: gadget: f_sdp: report a closed part as the boot ROM does
Quoted 10 lines · Ana Bobillo
The SDP gadget implements the serial download protocol of the i.MX boot ROM, but always reports
SDP_SECURITY_OPENin its HAB security report (report 3), also on a part whose secure boot is fused closed, where the ROM reportsSDP_SECURITY_CLOSED.Add
sdp_is_closed(), a weak hook that says whether secure boot is enforced, false by default so that other platforms are unchanged, and implement it for i.MX:[...]
Signature · 1 line
Mattijs Korpershoek <[email protected]>
PATCH 6/6 usb: gadget: f_sdp: refuse unauthenticated loads on a closed part
In reply to Ana BobilloFrom: Ana Bobillo <[email protected]>
On a part with secure boot fused closed, the i.MX boot ROM's serial download mode only runs an image it has authenticated, and NXP treats a ROM that reads out memory there as a vulnerability (CVE-2022-45163). The SDP gadget in SPL takes the ROM's place in that chain but does none of this, so a host on the USB port of a closed part can run any code it sends:
- SDP_JUMP_ADDRESS first calls sdp_jump_imxheader(), which calls the entry of any IVT it finds, unchecked, before the FIT, container and legacy image paths that authenticate;
- SDP_WRITE_FILE writes anywhere, the running loader's own BSS, heap, stack and page tables included, so that what it wrote runs before any image is checked;
- SDP_READ_REGISTER reads any address.
When sdp_is_closed() says the part is closed:
- skip the raw IVT jump, and take an image only by a path that authenticates it: a FIT with IMX_HAB (spl_load_simple_fit_fix_load()), a container with AHAB_BOOT (spl_load_imx_container()), a legacy image with IMX_HAB whose header starts the range the i.MX jump_to_image() authenticates (else the entry point comes from an unsigned header). A container on a HAB part, a FIT on an AHAB part and a script in U-Boot proper are refused. Where SPL searches for a FIT or container header and finds none, an IVT image on a closed part included, the jump ends in the existing panic;
- receive and drop a WRITE_FILE that wraps, starts below DRAM (CFG_SYS_SDRAM_BASE: the ROM, the peripherals, the on-chip RAM where SPL runs) or touches this phase's BSS, malloc areas, device tree, page tables or stack (once relocated: the stack up to ram_top);
- refuse READ_REGISTER.
Each refusal is answered with a failure in report 4, and the gadget waits for the next command. Read-back of a download through READ_REGISTER, as imx_usb_loader's verify option does, no longer works on a closed part. An open part, and every other platform, behaves as
before: these checks only apply when sdp_is_closed() returns true.
--- drivers/usb/gadget/f_sdp.c | 125 ++++++++++++++++++++++++++++++++++++- 1 file changed, 122 insertions(+), 3 deletions(-)
drivers/usb/gadget/f_sdp.c +122 -3
@@ -22,12 +22,16 @@#include <log.h> #include <malloc.h> #include <linux/printk.h> +#include <linux/sizes.h> #include <linux/usb/ch9.h> #include <linux/usb/gadget.h> #include <linux/usb/composite.h> +#include <config.h> #include <asm/io.h> +#include <asm/global_data.h> +#include <asm/sections.h> #include <g_dnl.h> #include <sdp.h> #include <spl.h>
@@ -36,6 +40,8 @@#include <imx_container.h> #include <watchdog.h> +DECLARE_GLOBAL_DATA_PTR ; +#define HID_REPORT_ID_MASK 0x000000ff/*
@@ -69,6 +75,8 @@ struct hid_report {#define SDP_WRITE_REGISTER_COMPLETE 0x128A8A12#define SDP_SKIP_DCD_HEADER_COMPLETE 0x900DD009#define SDP_ERROR_IMXHEADER 0x000a0533 +/* Status of a command refused on a closed part (HAB_FAILURE is 0x33) */ +#define SDP_ERROR_CLOSED 0x33333333#define SDP_COMMAND_LEN 16
@@ -113,6 +121,7 @@ struct f_sdp {u32 dnl_bytes;u32 dnl_bytes_remaining;u32 jmp_address; +bool dnl_discard;bool always_send_status;u32 error_status;
@@ -293,6 +302,61 @@ __weak bool sdp_is_closed(void)return false; } +/* +* On a closed part the image is authenticated when the host jumps to it, so +* its download must not change what runs before then: it has to land in +* DRAM, clear of this phase's BSS, heaps, stack, device tree and page tables. +* SPL runs from on-chip RAM below DRAM; U-Boot proper runs from the top of +* DRAM, which the stack range covers there. +*/ +static bool sdp_write_allowed (u32 addr,u32 cnt) +{ +const gd_t *g = gd; +ulong start = addr, end = start + cnt; +ulong sp = (ulong)&start; +ulong top = (ulong )g +sizeof (*g); +const void *fdt = g->fdt_blob; +int i; + +if (!sdp_is_closed ()) +return true; + +#ifdef CFG_SYS_SDRAM_BASE +if (start <CFG_SYS_SDRAM_BASE || cnt >U32_MAX - addr) +return false; +#else +return false; +#endif + +if (!IS_ENABLED (CONFIG_XPL_BUILD )) { +/* ram_top may be 4 GiB, which a 32-bit ulong holds as 0 */ + top = (ulong )g->ram_top; +if (!top) + top =ULONG_MAX ; + } + +/* Stack (up to ram_top once relocated), BSS, heaps, DT, page tables */ +ulong busy[][2 ] = { + { sp >SZ_64K ? sp -SZ_64K :0 , top }, + { (ulong )__bss_start, (ulong )__bss_end }, + { mem_malloc_start, mem_malloc_end }, + { (ulong )fdt, fdt ? (ulong )fdt +fdt_totalsize (fdt) :0 }, +#if CONFIG_IS_ENABLED(SYS_MALLOC_F) + { g->malloc_base, g->malloc_base + g->malloc_limit }, +#endif +#if defined(CONFIG_ARM) && \ + !(CONFIG_IS_ENABLED(SYS_ICACHE_OFF) && CONFIG_IS_ENABLED(SYS_DCACHE_OFF)) + { g->arch.tlb_addr, g->arch.tlb_addr + g->arch.tlb_size }, +#endif + }; + +for (i =0 ; i <ARRAY_SIZE (busy); i++) +if (start < busy[i][1 ] && busy[i][0 ] < end) +return false; + +return true; +} +static void sdp_rx_command_complete (struct usb_ep *ep,struct usb_request *req) {struct f_sdp *sdp = req->context;
@@ -326,6 +390,14 @@ static void sdp_rx_command_complete(struct usb_ep *ep, struct usb_request *req) sdp->dnl_address = addr; sdp->dnl_bytes_remaining = cnt; sdp->next_state =SDP_STATE_TX_REGISTER ; + +/* Nothing is read out of a closed part */ +if (sdp_is_closed ()) { + sdp->error_status =SDP_ERROR_CLOSED ; + sdp->next_state =SDP_STATE_IDLE ; +break ; + } +printf ("Reading %d registers at 0x%08x... " , sdp->dnl_bytes_remaining, sdp->dnl_address);break ;
@@ -339,6 +411,12 @@ static void sdp_rx_command_complete(struct usb_ep *ep, struct usb_request *req) sdp->dnl_bytes = sdp->dnl_bytes_remaining; sdp->next_state =SDP_STATE_IDLE ; +/* A refused download is received and dropped, then reported */ + sdp->dnl_discard = !sdp_write_allowed (sdp->dnl_address, + sdp->dnl_bytes); +if (sdp->dnl_discard) + sdp->error_status =SDP_ERROR_CLOSED ; +printf ("Downloading file of size %d to 0x%08x... " , sdp->dnl_bytes_remaining, sdp->dnl_address);
@@ -414,7 +492,7 @@ static void sdp_rx_data_complete(struct usb_ep *ep, struct usb_request *req) } sdp->dnl_bytes_remaining -= datalen; -if (sdp->state ==SDP_STATE_RX_FILE_DATA_BUSY ) { +if (sdp->state ==SDP_STATE_RX_FILE_DATA_BUSY && !sdp->dnl_discard) {memcpy (sdp_ptr (sdp->dnl_address), req->buf +1 , datalen); sdp->dnl_address += datalen; }
@@ -757,6 +835,23 @@ static u32 sdp_jump_imxheader(void *address)return 0 ; } +/* +* On a closed part the ROM only runs an image it has authenticated: take an +* image only by a load path that authenticates it, else report the failure. +*/ +static bool sdp_jump_refused (bool authenticated) +{ +if (authenticated || !sdp_is_closed ()) +return false; + +debug ("%s: refused on a closed part\n" , __func__); + sdp_func->error_status =SDP_ERROR_CLOSED ; + sdp_func->next_state =SDP_STATE_IDLE ; + sdp_func->state =SDP_STATE_TX_STATUS ; + +return true; +} +#ifdef CONFIG_XPL_BUILD static ulong sdp_load_read (struct spl_load_info *load,ulong sector,ulong count,void *buf)
@@ -842,7 +937,10 @@ static int sdp_handle_in_ep(struct spl_image_info *spl_image,break ;case SDP_STATE_JUMP :printf ("Jumping to header at 0x%08x\n" , sdp_func->jmp_address); - status =sdp_jump_imxheader (sdp_ptr (sdp_func->jmp_address)); +/* An IVT entry is called as it is, unauthenticated */ + status =SDP_ERROR_IMXHEADER ; +if (!sdp_is_closed ()) + status =sdp_jump_imxheader (sdp_ptr (sdp_func->jmp_address));/* If imx header fails, try some U-Boot specific headers */ if (status) {
@@ -864,6 +962,10 @@ static int sdp_handle_in_ep(struct spl_image_info *spl_image,int ret;debug ("Found FIT\n" ); +/* spl_load_simple_fit_fix_load() authenticates it */ +if (sdp_jump_refused (IS_ENABLED (CONFIG_IMX_HAB ))) +return 0 ; +spl_load_init (&load, sdp_load_read, header,1 ); ret =spl_load_simple_fit (spl_image, &load,0 , header);
@@ -878,6 +980,10 @@ static int sdp_handle_in_ep(struct spl_image_info *spl_image,struct spl_load_info load;int ret; +/* spl_load_imx_container() authenticates it */ +if (sdp_jump_refused (IS_ENABLED (CONFIG_AHAB_BOOT ))) +return 0 ; +spl_load_init (&load, sdp_load_read, header,1 ); ret =spl_load_imx_container (spl_image, &load,0 );if (ret)
@@ -889,10 +995,23 @@ static int sdp_handle_in_ep(struct spl_image_info *spl_image,/* In SPL, allow jumps to U-Boot images */ struct spl_image_info spl_image = {};struct spl_boot_device bootdev = {}; -spl_parse_image_header (&spl_image, &bootdev, header); +int err =spl_parse_image_header (&spl_image, &bootdev, + header); + +/* +* The i.MX jump_to_image() authenticates from load_addr +* on, so the header is signed only if it lies there +*/ +if (sdp_jump_refused (IS_ENABLED (CONFIG_IMX_HAB ) && !err && + spl_image.load_addr == sdp_func->jmp_address)) +return 0 ;spl_board_prepare_for_boot ();jump_to_image (&spl_image);#else +/* Nothing authenticates a script */ +if (sdp_jump_refused (false)) +return 0 ; +/* In U-Boot, allow jumps to scripts */ cmd_source_script (sdp_func->jmp_address,NULL ,NULL );#endif
Re: [PATCH 6/6] usb: gadget: f_sdp: refuse unauthenticated loads on a closed part
Hi,
On my video series, Tom noted that a Fixes tag isn't appropriate for populating newly added fields, and asked me to check my other series as well:
https://lists.u-boot-project.org/pipermail/u-boot/2026-November/639710.html
This patch acts only through sdp_is_closed(), the hook that 5/6 adds, so its two Fixes tags should go. Could they be dropped when the series is applied? If a v2 is needed, I will drop them there. Patches 1/6 to 3/6 keep theirs, as they fix the named commits' own code.
Thanks,
Ana
Re: [PATCH 6/6] usb: gadget: f_sdp: refuse unauthenticated loads on a closed part
Hi Ana,
Quoted 10 lines · Ana Bobillo
Hi,
On my video series, Tom noted that a Fixes tag isn't appropriate for populating newly added fields, and asked me to check my other series as well:
https://lists.u-boot-project.org/pipermail/u-boot/2026-November/639710.htmlThis patch acts only through
sdp_is_closed(), the hook that 5/6 adds, so its two Fixes tags should go. Could they be dropped when the series is applied? If a v2 is needed, I will drop them there. Patches 1/6 to
If I don't have any other review comments, I can take care of dropping the tags when applying.
Thanks
Mattijs
Quoted 4 lines
3/6 keep theirs, as they fix the named commits' own code.
Thanks,
Ana
Re: [PATCH 6/6] usb: gadget: f_sdp: refuse unauthenticated loads on a closed part
In reply to Ana BobilloQuoted 8 lines · Ana Bobillo
On a part with secure boot fused closed, the i.MX boot ROM's serial download mode only runs an image it has authenticated, and NXP treats a ROM that reads out memory there as a vulnerability (CVE-2022-45163). The SDP gadget in SPL takes the ROM's place in that chain but does none of this, so a host on the USB port of a closed part can run any code it sends:
[...]
Signature · 1 line
Mattijs Korpershoek <[email protected]>