Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Stefan Agner | ffbd5d0 | 2017-08-16 11:00:51 -0700 | [diff] [blame] | 2 | /* |
| 3 | * f_sdp.c -- USB HID Serial Download Protocol |
| 4 | * |
| 5 | * Copyright (C) 2017 Toradex |
| 6 | * Author: Stefan Agner <stefan.agner@toradex.com> |
| 7 | * |
| 8 | * This file implements the Serial Download Protocol (SDP) as specified in |
| 9 | * the i.MX 6 Reference Manual. The SDP is a USB HID based protocol and |
| 10 | * allows to download images directly to memory. The implementation |
| 11 | * works with the imx_loader (imx_usb) USB client software on host side. |
| 12 | * |
| 13 | * Not all commands are implemented, e.g. WRITE_REGISTER, DCD_WRITE and |
| 14 | * SKIP_DCD_HEADER are only stubs. |
| 15 | * |
| 16 | * Parts of the implementation are based on f_dfu and f_thor. |
Stefan Agner | ffbd5d0 | 2017-08-16 11:00:51 -0700 | [diff] [blame] | 17 | */ |
| 18 | |
| 19 | #include <errno.h> |
Stefan Agner | ffbd5d0 | 2017-08-16 11:00:51 -0700 | [diff] [blame] | 20 | #include <console.h> |
Simon Glass | 313112a | 2019-08-01 09:46:46 -0600 | [diff] [blame] | 21 | #include <env.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 22 | #include <log.h> |
Stefan Agner | ffbd5d0 | 2017-08-16 11:00:51 -0700 | [diff] [blame] | 23 | #include <malloc.h> |
Simon Glass | bdd5f81 | 2023-09-14 18:21:46 -0600 | [diff] [blame] | 24 | #include <linux/printk.h> |
Stefan Agner | ffbd5d0 | 2017-08-16 11:00:51 -0700 | [diff] [blame] | 25 | |
| 26 | #include <linux/usb/ch9.h> |
| 27 | #include <linux/usb/gadget.h> |
| 28 | #include <linux/usb/composite.h> |
| 29 | |
| 30 | #include <asm/io.h> |
| 31 | #include <g_dnl.h> |
| 32 | #include <sdp.h> |
Stefan Agner | 3888da7 | 2017-08-16 11:00:52 -0700 | [diff] [blame] | 33 | #include <spl.h> |
| 34 | #include <image.h> |
Stefan Agner | ffbd5d0 | 2017-08-16 11:00:51 -0700 | [diff] [blame] | 35 | #include <imximage.h> |
Sean Anderson | 952ed67 | 2023-10-14 16:47:44 -0400 | [diff] [blame] | 36 | #include <imx_container.h> |
Vincent Prince | f26cf93 | 2017-10-23 11:16:35 +0200 | [diff] [blame] | 37 | #include <watchdog.h> |
Stefan Agner | ffbd5d0 | 2017-08-16 11:00:51 -0700 | [diff] [blame] | 38 | |
| 39 | #define HID_REPORT_ID_MASK 0x000000ff |
| 40 | |
| 41 | /* |
| 42 | * HID class requests |
| 43 | */ |
| 44 | #define HID_REQ_GET_REPORT 0x01 |
| 45 | #define HID_REQ_GET_IDLE 0x02 |
| 46 | #define HID_REQ_GET_PROTOCOL 0x03 |
| 47 | #define HID_REQ_SET_REPORT 0x09 |
| 48 | #define HID_REQ_SET_IDLE 0x0A |
| 49 | #define HID_REQ_SET_PROTOCOL 0x0B |
| 50 | |
| 51 | #define HID_USAGE_PAGE_LEN 76 |
| 52 | |
| 53 | struct hid_report { |
| 54 | u8 usage_page[HID_USAGE_PAGE_LEN]; |
| 55 | } __packed; |
| 56 | |
| 57 | #define SDP_READ_REGISTER 0x0101 |
| 58 | #define SDP_WRITE_REGISTER 0x0202 |
| 59 | #define SDP_WRITE_FILE 0x0404 |
| 60 | #define SDP_ERROR_STATUS 0x0505 |
| 61 | #define SDP_DCD_WRITE 0x0a0a |
| 62 | #define SDP_JUMP_ADDRESS 0x0b0b |
| 63 | #define SDP_SKIP_DCD_HEADER 0x0c0c |
| 64 | |
| 65 | #define SDP_SECURITY_CLOSED 0x12343412 |
| 66 | #define SDP_SECURITY_OPEN 0x56787856 |
| 67 | |
| 68 | #define SDP_WRITE_FILE_COMPLETE 0x88888888 |
| 69 | #define SDP_WRITE_REGISTER_COMPLETE 0x128A8A12 |
| 70 | #define SDP_SKIP_DCD_HEADER_COMPLETE 0x900DD009 |
| 71 | #define SDP_ERROR_IMXHEADER 0x000a0533 |
| 72 | |
| 73 | #define SDP_COMMAND_LEN 16 |
| 74 | |
Sherry Sun | 5226298 | 2020-08-18 18:16:48 +0800 | [diff] [blame] | 75 | #define SDP_HID_PACKET_SIZE_EP1 1024 |
| 76 | |
Peng Fan | f49d470 | 2020-08-18 18:16:46 +0800 | [diff] [blame] | 77 | #define SDP_EXIT 1 |
| 78 | |
Stefan Agner | ffbd5d0 | 2017-08-16 11:00:51 -0700 | [diff] [blame] | 79 | struct sdp_command { |
| 80 | u16 cmd; |
| 81 | u32 addr; |
| 82 | u8 format; |
| 83 | u32 cnt; |
| 84 | u32 data; |
| 85 | u8 rsvd; |
| 86 | } __packed; |
| 87 | |
| 88 | enum sdp_state { |
| 89 | SDP_STATE_IDLE, |
Sherry Sun | 5226298 | 2020-08-18 18:16:48 +0800 | [diff] [blame] | 90 | SDP_STATE_RX_CMD, |
Stefan Agner | ffbd5d0 | 2017-08-16 11:00:51 -0700 | [diff] [blame] | 91 | SDP_STATE_RX_DCD_DATA, |
| 92 | SDP_STATE_RX_FILE_DATA, |
Sherry Sun | 5226298 | 2020-08-18 18:16:48 +0800 | [diff] [blame] | 93 | SDP_STATE_RX_FILE_DATA_BUSY, |
Stefan Agner | ffbd5d0 | 2017-08-16 11:00:51 -0700 | [diff] [blame] | 94 | SDP_STATE_TX_SEC_CONF, |
| 95 | SDP_STATE_TX_SEC_CONF_BUSY, |
| 96 | SDP_STATE_TX_REGISTER, |
| 97 | SDP_STATE_TX_REGISTER_BUSY, |
| 98 | SDP_STATE_TX_STATUS, |
| 99 | SDP_STATE_TX_STATUS_BUSY, |
| 100 | SDP_STATE_JUMP, |
| 101 | }; |
| 102 | |
| 103 | struct f_sdp { |
| 104 | struct usb_function usb_function; |
| 105 | |
| 106 | struct usb_descriptor_header **function; |
| 107 | |
| 108 | u8 altsetting; |
| 109 | enum sdp_state state; |
| 110 | enum sdp_state next_state; |
| 111 | u32 dnl_address; |
Petr Štetiar | d991cc3 | 2018-11-23 14:37:52 +0100 | [diff] [blame] | 112 | u32 dnl_bytes; |
Stefan Agner | ffbd5d0 | 2017-08-16 11:00:51 -0700 | [diff] [blame] | 113 | u32 dnl_bytes_remaining; |
| 114 | u32 jmp_address; |
| 115 | bool always_send_status; |
| 116 | u32 error_status; |
| 117 | |
| 118 | /* EP0 request */ |
| 119 | struct usb_request *req; |
| 120 | |
| 121 | /* EP1 IN */ |
| 122 | struct usb_ep *in_ep; |
| 123 | struct usb_request *in_req; |
Sherry Sun | 5226298 | 2020-08-18 18:16:48 +0800 | [diff] [blame] | 124 | /* EP1 OUT */ |
| 125 | struct usb_ep *out_ep; |
| 126 | struct usb_request *out_req; |
Stefan Agner | ffbd5d0 | 2017-08-16 11:00:51 -0700 | [diff] [blame] | 127 | |
| 128 | bool configuration_done; |
Sherry Sun | 5226298 | 2020-08-18 18:16:48 +0800 | [diff] [blame] | 129 | bool ep_int_enable; |
Stefan Agner | ffbd5d0 | 2017-08-16 11:00:51 -0700 | [diff] [blame] | 130 | }; |
| 131 | |
| 132 | static struct f_sdp *sdp_func; |
| 133 | |
| 134 | static inline struct f_sdp *func_to_sdp(struct usb_function *f) |
| 135 | { |
| 136 | return container_of(f, struct f_sdp, usb_function); |
| 137 | } |
| 138 | |
| 139 | static struct usb_interface_descriptor sdp_intf_runtime = { |
| 140 | .bLength = sizeof(sdp_intf_runtime), |
| 141 | .bDescriptorType = USB_DT_INTERFACE, |
| 142 | .bAlternateSetting = 0, |
Sherry Sun | 5226298 | 2020-08-18 18:16:48 +0800 | [diff] [blame] | 143 | .bNumEndpoints = 2, |
Stefan Agner | ffbd5d0 | 2017-08-16 11:00:51 -0700 | [diff] [blame] | 144 | .bInterfaceClass = USB_CLASS_HID, |
| 145 | .bInterfaceSubClass = 0, |
| 146 | .bInterfaceProtocol = 0, |
| 147 | /* .iInterface = DYNAMIC */ |
| 148 | }; |
| 149 | |
| 150 | /* HID configuration */ |
| 151 | static struct usb_class_hid_descriptor sdp_hid_desc = { |
| 152 | .bLength = sizeof(sdp_hid_desc), |
| 153 | .bDescriptorType = USB_DT_CS_DEVICE, |
| 154 | |
| 155 | .bcdCDC = __constant_cpu_to_le16(0x0110), |
| 156 | .bCountryCode = 0, |
| 157 | .bNumDescriptors = 1, |
| 158 | |
| 159 | .bDescriptorType0 = USB_DT_HID_REPORT, |
| 160 | .wDescriptorLength0 = HID_USAGE_PAGE_LEN, |
| 161 | }; |
| 162 | |
| 163 | static struct usb_endpoint_descriptor in_desc = { |
| 164 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 165 | .bDescriptorType = USB_DT_ENDPOINT, /*USB_DT_CS_ENDPOINT*/ |
| 166 | |
| 167 | .bEndpointAddress = 1 | USB_DIR_IN, |
| 168 | .bmAttributes = USB_ENDPOINT_XFER_INT, |
| 169 | .wMaxPacketSize = 64, |
| 170 | .bInterval = 1, |
| 171 | }; |
| 172 | |
Sherry Sun | 5226298 | 2020-08-18 18:16:48 +0800 | [diff] [blame] | 173 | static struct usb_endpoint_descriptor out_desc = { |
| 174 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 175 | .bDescriptorType = USB_DT_ENDPOINT, /*USB_DT_CS_ENDPOINT*/ |
| 176 | |
| 177 | .bEndpointAddress = 1 | USB_DIR_OUT, |
| 178 | .bmAttributes = USB_ENDPOINT_XFER_INT, |
| 179 | .wMaxPacketSize = 64, |
| 180 | .bInterval = 1, |
| 181 | }; |
| 182 | |
Ye Li | a9e8fe0 | 2020-08-18 18:16:44 +0800 | [diff] [blame] | 183 | static struct usb_endpoint_descriptor in_hs_desc = { |
| 184 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 185 | .bDescriptorType = USB_DT_ENDPOINT, /*USB_DT_CS_ENDPOINT*/ |
| 186 | |
| 187 | .bEndpointAddress = 1 | USB_DIR_IN, |
| 188 | .bmAttributes = USB_ENDPOINT_XFER_INT, |
| 189 | .wMaxPacketSize = 512, |
Sherry Sun | 44f26b1 | 2020-08-18 18:16:49 +0800 | [diff] [blame] | 190 | .bInterval = 3, |
Ye Li | a9e8fe0 | 2020-08-18 18:16:44 +0800 | [diff] [blame] | 191 | }; |
| 192 | |
Sherry Sun | 5226298 | 2020-08-18 18:16:48 +0800 | [diff] [blame] | 193 | static struct usb_endpoint_descriptor out_hs_desc = { |
| 194 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 195 | .bDescriptorType = USB_DT_ENDPOINT, /*USB_DT_CS_ENDPOINT*/ |
| 196 | |
| 197 | .bEndpointAddress = 1 | USB_DIR_OUT, |
| 198 | .bmAttributes = USB_ENDPOINT_XFER_INT, |
| 199 | .wMaxPacketSize = SDP_HID_PACKET_SIZE_EP1, |
Sherry Sun | 44f26b1 | 2020-08-18 18:16:49 +0800 | [diff] [blame] | 200 | .bInterval = 3, |
Sherry Sun | 5226298 | 2020-08-18 18:16:48 +0800 | [diff] [blame] | 201 | }; |
| 202 | |
Stefan Agner | ffbd5d0 | 2017-08-16 11:00:51 -0700 | [diff] [blame] | 203 | static struct usb_descriptor_header *sdp_runtime_descs[] = { |
| 204 | (struct usb_descriptor_header *)&sdp_intf_runtime, |
| 205 | (struct usb_descriptor_header *)&sdp_hid_desc, |
| 206 | (struct usb_descriptor_header *)&in_desc, |
Sherry Sun | 5226298 | 2020-08-18 18:16:48 +0800 | [diff] [blame] | 207 | (struct usb_descriptor_header *)&out_desc, |
Stefan Agner | ffbd5d0 | 2017-08-16 11:00:51 -0700 | [diff] [blame] | 208 | NULL, |
| 209 | }; |
| 210 | |
Ye Li | a9e8fe0 | 2020-08-18 18:16:44 +0800 | [diff] [blame] | 211 | static struct usb_descriptor_header *sdp_runtime_hs_descs[] = { |
| 212 | (struct usb_descriptor_header *)&sdp_intf_runtime, |
| 213 | (struct usb_descriptor_header *)&sdp_hid_desc, |
| 214 | (struct usb_descriptor_header *)&in_hs_desc, |
Sherry Sun | 5226298 | 2020-08-18 18:16:48 +0800 | [diff] [blame] | 215 | (struct usb_descriptor_header *)&out_hs_desc, |
Ye Li | a9e8fe0 | 2020-08-18 18:16:44 +0800 | [diff] [blame] | 216 | NULL, |
| 217 | }; |
| 218 | |
Stefan Agner | ffbd5d0 | 2017-08-16 11:00:51 -0700 | [diff] [blame] | 219 | /* This is synchronized with what the SoC implementation reports */ |
| 220 | static struct hid_report sdp_hid_report = { |
| 221 | .usage_page = { |
| 222 | 0x06, 0x00, 0xff, /* Usage Page */ |
| 223 | 0x09, 0x01, /* Usage (Pointer?) */ |
| 224 | 0xa1, 0x01, /* Collection */ |
| 225 | |
| 226 | 0x85, 0x01, /* Report ID */ |
| 227 | 0x19, 0x01, /* Usage Minimum */ |
| 228 | 0x29, 0x01, /* Usage Maximum */ |
| 229 | 0x15, 0x00, /* Local Minimum */ |
| 230 | 0x26, 0xFF, 0x00, /* Local Maximum? */ |
| 231 | 0x75, 0x08, /* Report Size */ |
| 232 | 0x95, 0x10, /* Report Count */ |
| 233 | 0x91, 0x02, /* Output Data */ |
| 234 | |
| 235 | 0x85, 0x02, /* Report ID */ |
| 236 | 0x19, 0x01, /* Usage Minimum */ |
| 237 | 0x29, 0x01, /* Usage Maximum */ |
| 238 | 0x15, 0x00, /* Local Minimum */ |
| 239 | 0x26, 0xFF, 0x00, /* Local Maximum? */ |
| 240 | 0x75, 0x80, /* Report Size 128 */ |
| 241 | 0x95, 0x40, /* Report Count */ |
| 242 | 0x91, 0x02, /* Output Data */ |
| 243 | |
| 244 | 0x85, 0x03, /* Report ID */ |
| 245 | 0x19, 0x01, /* Usage Minimum */ |
| 246 | 0x29, 0x01, /* Usage Maximum */ |
| 247 | 0x15, 0x00, /* Local Minimum */ |
| 248 | 0x26, 0xFF, 0x00, /* Local Maximum? */ |
| 249 | 0x75, 0x08, /* Report Size 8 */ |
| 250 | 0x95, 0x04, /* Report Count */ |
| 251 | 0x81, 0x02, /* Input Data */ |
| 252 | |
| 253 | 0x85, 0x04, /* Report ID */ |
| 254 | 0x19, 0x01, /* Usage Minimum */ |
| 255 | 0x29, 0x01, /* Usage Maximum */ |
| 256 | 0x15, 0x00, /* Local Minimum */ |
| 257 | 0x26, 0xFF, 0x00, /* Local Maximum? */ |
| 258 | 0x75, 0x08, /* Report Size 8 */ |
| 259 | 0x95, 0x40, /* Report Count */ |
| 260 | 0x81, 0x02, /* Input Data */ |
| 261 | 0xc0 |
| 262 | }, |
| 263 | }; |
| 264 | |
| 265 | static const char sdp_name[] = "Serial Downloader Protocol"; |
| 266 | |
| 267 | /* |
| 268 | * static strings, in UTF-8 |
| 269 | */ |
| 270 | static struct usb_string strings_sdp_generic[] = { |
| 271 | [0].s = sdp_name, |
| 272 | { } /* end of list */ |
| 273 | }; |
| 274 | |
| 275 | static struct usb_gadget_strings stringtab_sdp_generic = { |
| 276 | .language = 0x0409, /* en-us */ |
| 277 | .strings = strings_sdp_generic, |
| 278 | }; |
| 279 | |
| 280 | static struct usb_gadget_strings *sdp_generic_strings[] = { |
| 281 | &stringtab_sdp_generic, |
| 282 | NULL, |
| 283 | }; |
| 284 | |
Andre Heider | e1d461b | 2018-02-15 10:17:29 +0100 | [diff] [blame] | 285 | static inline void *sdp_ptr(u32 val) |
| 286 | { |
| 287 | return (void *)(uintptr_t)val; |
| 288 | } |
| 289 | |
Stefan Agner | ffbd5d0 | 2017-08-16 11:00:51 -0700 | [diff] [blame] | 290 | static void sdp_rx_command_complete(struct usb_ep *ep, struct usb_request *req) |
| 291 | { |
| 292 | struct f_sdp *sdp = req->context; |
| 293 | int status = req->status; |
| 294 | u8 *data = req->buf; |
| 295 | u8 report = data[0]; |
| 296 | |
| 297 | if (status != 0) { |
Andre Heider | b841e97 | 2018-02-15 07:08:55 +0100 | [diff] [blame] | 298 | pr_err("Status: %d\n", status); |
Stefan Agner | ffbd5d0 | 2017-08-16 11:00:51 -0700 | [diff] [blame] | 299 | return; |
| 300 | } |
| 301 | |
| 302 | if (report != 1) { |
Andre Heider | b841e97 | 2018-02-15 07:08:55 +0100 | [diff] [blame] | 303 | pr_err("Unexpected report %d\n", report); |
Stefan Agner | ffbd5d0 | 2017-08-16 11:00:51 -0700 | [diff] [blame] | 304 | return; |
| 305 | } |
| 306 | |
| 307 | struct sdp_command *cmd = req->buf + 1; |
| 308 | |
| 309 | debug("%s: command: %04x, addr: %08x, cnt: %u\n", |
| 310 | __func__, be16_to_cpu(cmd->cmd), |
| 311 | be32_to_cpu(cmd->addr), be32_to_cpu(cmd->cnt)); |
| 312 | |
| 313 | switch (be16_to_cpu(cmd->cmd)) { |
| 314 | case SDP_READ_REGISTER: |
| 315 | sdp->always_send_status = false; |
| 316 | sdp->error_status = 0x0; |
| 317 | |
| 318 | sdp->state = SDP_STATE_TX_SEC_CONF; |
| 319 | sdp->dnl_address = be32_to_cpu(cmd->addr); |
| 320 | sdp->dnl_bytes_remaining = be32_to_cpu(cmd->cnt); |
| 321 | sdp->next_state = SDP_STATE_TX_REGISTER; |
| 322 | printf("Reading %d registers at 0x%08x... ", |
| 323 | sdp->dnl_bytes_remaining, sdp->dnl_address); |
| 324 | break; |
| 325 | case SDP_WRITE_FILE: |
| 326 | sdp->always_send_status = true; |
| 327 | sdp->error_status = SDP_WRITE_FILE_COMPLETE; |
| 328 | |
| 329 | sdp->state = SDP_STATE_RX_FILE_DATA; |
Frank Li | c2bdb58 | 2020-04-29 10:35:11 +0800 | [diff] [blame] | 330 | sdp->dnl_address = cmd->addr ? be32_to_cpu(cmd->addr) : CONFIG_SDP_LOADADDR; |
Stefan Agner | ffbd5d0 | 2017-08-16 11:00:51 -0700 | [diff] [blame] | 331 | sdp->dnl_bytes_remaining = be32_to_cpu(cmd->cnt); |
Petr Štetiar | d991cc3 | 2018-11-23 14:37:52 +0100 | [diff] [blame] | 332 | sdp->dnl_bytes = sdp->dnl_bytes_remaining; |
Stefan Agner | ffbd5d0 | 2017-08-16 11:00:51 -0700 | [diff] [blame] | 333 | sdp->next_state = SDP_STATE_IDLE; |
| 334 | |
| 335 | printf("Downloading file of size %d to 0x%08x... ", |
| 336 | sdp->dnl_bytes_remaining, sdp->dnl_address); |
| 337 | |
| 338 | break; |
| 339 | case SDP_ERROR_STATUS: |
| 340 | sdp->always_send_status = true; |
| 341 | sdp->error_status = 0; |
| 342 | |
| 343 | sdp->state = SDP_STATE_TX_SEC_CONF; |
| 344 | sdp->next_state = SDP_STATE_IDLE; |
| 345 | break; |
| 346 | case SDP_DCD_WRITE: |
| 347 | sdp->always_send_status = true; |
| 348 | sdp->error_status = SDP_WRITE_REGISTER_COMPLETE; |
| 349 | |
| 350 | sdp->state = SDP_STATE_RX_DCD_DATA; |
| 351 | sdp->dnl_bytes_remaining = be32_to_cpu(cmd->cnt); |
| 352 | sdp->next_state = SDP_STATE_IDLE; |
| 353 | break; |
| 354 | case SDP_JUMP_ADDRESS: |
| 355 | sdp->always_send_status = false; |
| 356 | sdp->error_status = 0; |
| 357 | |
Frank Li | c2bdb58 | 2020-04-29 10:35:11 +0800 | [diff] [blame] | 358 | sdp->jmp_address = cmd->addr ? be32_to_cpu(cmd->addr) : CONFIG_SDP_LOADADDR; |
Stefan Agner | ffbd5d0 | 2017-08-16 11:00:51 -0700 | [diff] [blame] | 359 | sdp->state = SDP_STATE_TX_SEC_CONF; |
| 360 | sdp->next_state = SDP_STATE_JUMP; |
| 361 | break; |
| 362 | case SDP_SKIP_DCD_HEADER: |
| 363 | sdp->always_send_status = true; |
| 364 | sdp->error_status = SDP_SKIP_DCD_HEADER_COMPLETE; |
| 365 | |
| 366 | /* Ignore command, DCD not supported anyway */ |
| 367 | sdp->state = SDP_STATE_TX_SEC_CONF; |
| 368 | sdp->next_state = SDP_STATE_IDLE; |
| 369 | break; |
| 370 | default: |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 371 | pr_err("Unknown command: %04x\n", be16_to_cpu(cmd->cmd)); |
Stefan Agner | ffbd5d0 | 2017-08-16 11:00:51 -0700 | [diff] [blame] | 372 | } |
| 373 | } |
| 374 | |
| 375 | static void sdp_rx_data_complete(struct usb_ep *ep, struct usb_request *req) |
| 376 | { |
| 377 | struct f_sdp *sdp = req->context; |
| 378 | int status = req->status; |
| 379 | u8 *data = req->buf; |
| 380 | u8 report = data[0]; |
Sherry Sun | 5226298 | 2020-08-18 18:16:48 +0800 | [diff] [blame] | 381 | int datalen = req->actual - 1; |
Stefan Agner | ffbd5d0 | 2017-08-16 11:00:51 -0700 | [diff] [blame] | 382 | |
| 383 | if (status != 0) { |
Andre Heider | b841e97 | 2018-02-15 07:08:55 +0100 | [diff] [blame] | 384 | pr_err("Status: %d\n", status); |
Stefan Agner | ffbd5d0 | 2017-08-16 11:00:51 -0700 | [diff] [blame] | 385 | return; |
| 386 | } |
| 387 | |
| 388 | if (report != 2) { |
Andre Heider | b841e97 | 2018-02-15 07:08:55 +0100 | [diff] [blame] | 389 | pr_err("Unexpected report %d\n", report); |
Stefan Agner | ffbd5d0 | 2017-08-16 11:00:51 -0700 | [diff] [blame] | 390 | return; |
| 391 | } |
| 392 | |
| 393 | if (sdp->dnl_bytes_remaining < datalen) { |
| 394 | /* |
| 395 | * Some USB stacks require to send a complete buffer as |
| 396 | * specified in the HID descriptor. This leads to longer |
| 397 | * transfers than the file length, no problem for us. |
| 398 | */ |
| 399 | sdp->dnl_bytes_remaining = 0; |
| 400 | } else { |
| 401 | sdp->dnl_bytes_remaining -= datalen; |
| 402 | } |
| 403 | |
Sherry Sun | 5226298 | 2020-08-18 18:16:48 +0800 | [diff] [blame] | 404 | if (sdp->state == SDP_STATE_RX_FILE_DATA_BUSY) { |
Andre Heider | e1d461b | 2018-02-15 10:17:29 +0100 | [diff] [blame] | 405 | memcpy(sdp_ptr(sdp->dnl_address), req->buf + 1, datalen); |
Stefan Agner | ffbd5d0 | 2017-08-16 11:00:51 -0700 | [diff] [blame] | 406 | sdp->dnl_address += datalen; |
| 407 | } |
| 408 | |
Sherry Sun | 5226298 | 2020-08-18 18:16:48 +0800 | [diff] [blame] | 409 | if (sdp->dnl_bytes_remaining) { |
| 410 | sdp->state = SDP_STATE_RX_FILE_DATA; |
Stefan Agner | ffbd5d0 | 2017-08-16 11:00:51 -0700 | [diff] [blame] | 411 | return; |
Sherry Sun | 5226298 | 2020-08-18 18:16:48 +0800 | [diff] [blame] | 412 | } |
Stefan Agner | ffbd5d0 | 2017-08-16 11:00:51 -0700 | [diff] [blame] | 413 | |
Petr Štetiar | d991cc3 | 2018-11-23 14:37:52 +0100 | [diff] [blame] | 414 | #ifndef CONFIG_SPL_BUILD |
| 415 | env_set_hex("filesize", sdp->dnl_bytes); |
| 416 | #endif |
Stefan Agner | ffbd5d0 | 2017-08-16 11:00:51 -0700 | [diff] [blame] | 417 | printf("done\n"); |
| 418 | |
| 419 | switch (sdp->state) { |
Sherry Sun | 5226298 | 2020-08-18 18:16:48 +0800 | [diff] [blame] | 420 | case SDP_STATE_RX_FILE_DATA_BUSY: |
Stefan Agner | ffbd5d0 | 2017-08-16 11:00:51 -0700 | [diff] [blame] | 421 | sdp->state = SDP_STATE_TX_SEC_CONF; |
| 422 | break; |
| 423 | case SDP_STATE_RX_DCD_DATA: |
| 424 | sdp->state = SDP_STATE_TX_SEC_CONF; |
| 425 | break; |
| 426 | default: |
Andre Heider | b841e97 | 2018-02-15 07:08:55 +0100 | [diff] [blame] | 427 | pr_err("Invalid state: %d\n", sdp->state); |
Stefan Agner | ffbd5d0 | 2017-08-16 11:00:51 -0700 | [diff] [blame] | 428 | } |
| 429 | } |
| 430 | |
| 431 | static void sdp_tx_complete(struct usb_ep *ep, struct usb_request *req) |
| 432 | { |
| 433 | struct f_sdp *sdp = req->context; |
| 434 | int status = req->status; |
| 435 | |
| 436 | if (status != 0) { |
Andre Heider | b841e97 | 2018-02-15 07:08:55 +0100 | [diff] [blame] | 437 | pr_err("Status: %d\n", status); |
Stefan Agner | ffbd5d0 | 2017-08-16 11:00:51 -0700 | [diff] [blame] | 438 | return; |
| 439 | } |
| 440 | |
| 441 | switch (sdp->state) { |
| 442 | case SDP_STATE_TX_SEC_CONF_BUSY: |
| 443 | /* Not all commands require status report */ |
| 444 | if (sdp->always_send_status || sdp->error_status) |
| 445 | sdp->state = SDP_STATE_TX_STATUS; |
| 446 | else |
| 447 | sdp->state = sdp->next_state; |
| 448 | |
| 449 | break; |
| 450 | case SDP_STATE_TX_STATUS_BUSY: |
| 451 | sdp->state = sdp->next_state; |
| 452 | break; |
| 453 | case SDP_STATE_TX_REGISTER_BUSY: |
| 454 | if (sdp->dnl_bytes_remaining) |
| 455 | sdp->state = SDP_STATE_TX_REGISTER; |
| 456 | else |
| 457 | sdp->state = SDP_STATE_IDLE; |
| 458 | break; |
| 459 | default: |
Andre Heider | b841e97 | 2018-02-15 07:08:55 +0100 | [diff] [blame] | 460 | pr_err("Wrong State: %d\n", sdp->state); |
Stefan Agner | ffbd5d0 | 2017-08-16 11:00:51 -0700 | [diff] [blame] | 461 | sdp->state = SDP_STATE_IDLE; |
| 462 | break; |
| 463 | } |
| 464 | debug("%s complete --> %d, %d/%d\n", ep->name, |
| 465 | status, req->actual, req->length); |
| 466 | } |
| 467 | |
| 468 | static int sdp_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl) |
| 469 | { |
| 470 | struct usb_gadget *gadget = f->config->cdev->gadget; |
| 471 | struct usb_request *req = f->config->cdev->req; |
| 472 | struct f_sdp *sdp = f->config->cdev->req->context; |
| 473 | u16 len = le16_to_cpu(ctrl->wLength); |
| 474 | u16 w_value = le16_to_cpu(ctrl->wValue); |
| 475 | int value = 0; |
| 476 | u8 req_type = ctrl->bRequestType & USB_TYPE_MASK; |
| 477 | |
| 478 | debug("w_value: 0x%04x len: 0x%04x\n", w_value, len); |
| 479 | debug("req_type: 0x%02x ctrl->bRequest: 0x%02x sdp->state: %d\n", |
| 480 | req_type, ctrl->bRequest, sdp->state); |
| 481 | |
| 482 | if (req_type == USB_TYPE_STANDARD) { |
| 483 | if (ctrl->bRequest == USB_REQ_GET_DESCRIPTOR) { |
| 484 | /* Send HID report descriptor */ |
| 485 | value = min(len, (u16) sizeof(sdp_hid_report)); |
| 486 | memcpy(req->buf, &sdp_hid_report, value); |
| 487 | sdp->configuration_done = true; |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | if (req_type == USB_TYPE_CLASS) { |
| 492 | int report = w_value & HID_REPORT_ID_MASK; |
| 493 | |
| 494 | /* HID (SDP) request */ |
| 495 | switch (ctrl->bRequest) { |
| 496 | case HID_REQ_SET_REPORT: |
| 497 | switch (report) { |
| 498 | case 1: |
| 499 | value = SDP_COMMAND_LEN + 1; |
| 500 | req->complete = sdp_rx_command_complete; |
Sherry Sun | 5226298 | 2020-08-18 18:16:48 +0800 | [diff] [blame] | 501 | sdp_func->ep_int_enable = false; |
Stefan Agner | ffbd5d0 | 2017-08-16 11:00:51 -0700 | [diff] [blame] | 502 | break; |
| 503 | case 2: |
| 504 | value = len; |
| 505 | req->complete = sdp_rx_data_complete; |
Sherry Sun | 5226298 | 2020-08-18 18:16:48 +0800 | [diff] [blame] | 506 | sdp_func->state = SDP_STATE_RX_FILE_DATA_BUSY; |
Stefan Agner | ffbd5d0 | 2017-08-16 11:00:51 -0700 | [diff] [blame] | 507 | break; |
| 508 | } |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | if (value >= 0) { |
| 513 | req->length = value; |
| 514 | req->zero = value < len; |
| 515 | value = usb_ep_queue(gadget->ep0, req, 0); |
| 516 | if (value < 0) { |
| 517 | debug("ep_queue --> %d\n", value); |
| 518 | req->status = 0; |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | return value; |
| 523 | } |
| 524 | |
| 525 | static int sdp_bind(struct usb_configuration *c, struct usb_function *f) |
| 526 | { |
| 527 | struct usb_gadget *gadget = c->cdev->gadget; |
| 528 | struct usb_composite_dev *cdev = c->cdev; |
| 529 | struct f_sdp *sdp = func_to_sdp(f); |
| 530 | int rv = 0, id; |
| 531 | |
| 532 | id = usb_interface_id(c, f); |
| 533 | if (id < 0) |
| 534 | return id; |
| 535 | sdp_intf_runtime.bInterfaceNumber = id; |
| 536 | |
Sherry Sun | 5226298 | 2020-08-18 18:16:48 +0800 | [diff] [blame] | 537 | struct usb_ep *ep_in, *ep_out; |
Stefan Agner | ffbd5d0 | 2017-08-16 11:00:51 -0700 | [diff] [blame] | 538 | |
| 539 | /* allocate instance-specific endpoints */ |
Sherry Sun | 5226298 | 2020-08-18 18:16:48 +0800 | [diff] [blame] | 540 | ep_in = usb_ep_autoconfig(gadget, &in_desc); |
| 541 | if (!ep_in) { |
| 542 | rv = -ENODEV; |
| 543 | goto error; |
| 544 | } |
| 545 | |
| 546 | ep_out = usb_ep_autoconfig(gadget, &out_desc); |
| 547 | if (!ep_out) { |
Stefan Agner | ffbd5d0 | 2017-08-16 11:00:51 -0700 | [diff] [blame] | 548 | rv = -ENODEV; |
| 549 | goto error; |
| 550 | } |
| 551 | |
Ye Li | a9e8fe0 | 2020-08-18 18:16:44 +0800 | [diff] [blame] | 552 | if (gadget_is_dualspeed(gadget)) { |
| 553 | /* Assume endpoint addresses are the same for both speeds */ |
| 554 | in_hs_desc.bEndpointAddress = in_desc.bEndpointAddress; |
Sherry Sun | 5226298 | 2020-08-18 18:16:48 +0800 | [diff] [blame] | 555 | out_hs_desc.bEndpointAddress = out_desc.bEndpointAddress; |
Ye Li | a9e8fe0 | 2020-08-18 18:16:44 +0800 | [diff] [blame] | 556 | } |
| 557 | |
Sherry Sun | 5226298 | 2020-08-18 18:16:48 +0800 | [diff] [blame] | 558 | sdp->in_ep = ep_in; /* Store IN EP for enabling @ setup */ |
| 559 | sdp->out_ep = ep_out; |
Stefan Agner | ffbd5d0 | 2017-08-16 11:00:51 -0700 | [diff] [blame] | 560 | |
| 561 | cdev->req->context = sdp; |
| 562 | |
| 563 | error: |
| 564 | return rv; |
| 565 | } |
| 566 | |
| 567 | static void sdp_unbind(struct usb_configuration *c, struct usb_function *f) |
| 568 | { |
| 569 | free(sdp_func); |
| 570 | sdp_func = NULL; |
| 571 | } |
| 572 | |
| 573 | static struct usb_request *alloc_ep_req(struct usb_ep *ep, unsigned length) |
| 574 | { |
| 575 | struct usb_request *req; |
| 576 | |
| 577 | req = usb_ep_alloc_request(ep, 0); |
| 578 | if (!req) |
| 579 | return req; |
| 580 | |
| 581 | req->length = length; |
| 582 | req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, length); |
| 583 | if (!req->buf) { |
| 584 | usb_ep_free_request(ep, req); |
| 585 | req = NULL; |
| 586 | } |
| 587 | |
| 588 | return req; |
| 589 | } |
| 590 | |
| 591 | |
Sherry Sun | 5226298 | 2020-08-18 18:16:48 +0800 | [diff] [blame] | 592 | static struct usb_request *sdp_start_ep(struct usb_ep *ep, bool in) |
Stefan Agner | ffbd5d0 | 2017-08-16 11:00:51 -0700 | [diff] [blame] | 593 | { |
| 594 | struct usb_request *req; |
| 595 | |
Sherry Sun | 5226298 | 2020-08-18 18:16:48 +0800 | [diff] [blame] | 596 | if (in) |
| 597 | req = alloc_ep_req(ep, 65); |
| 598 | else |
| 599 | req = alloc_ep_req(ep, 2048); |
| 600 | /* |
| 601 | * OUT endpoint request length should be an integral multiple of |
| 602 | * maxpacket size 1024, else we break on certain controllers like |
| 603 | * DWC3 that expect bulk OUT requests to be divisible by maxpacket size. |
| 604 | */ |
Stefan Agner | ffbd5d0 | 2017-08-16 11:00:51 -0700 | [diff] [blame] | 605 | debug("%s: ep:%p req:%p\n", __func__, ep, req); |
| 606 | |
| 607 | if (!req) |
| 608 | return NULL; |
| 609 | |
| 610 | memset(req->buf, 0, req->length); |
Sherry Sun | 5226298 | 2020-08-18 18:16:48 +0800 | [diff] [blame] | 611 | if (in) |
| 612 | req->complete = sdp_tx_complete; |
| 613 | else |
| 614 | req->complete = sdp_rx_command_complete; |
Stefan Agner | ffbd5d0 | 2017-08-16 11:00:51 -0700 | [diff] [blame] | 615 | |
| 616 | return req; |
| 617 | } |
| 618 | static int sdp_set_alt(struct usb_function *f, unsigned intf, unsigned alt) |
| 619 | { |
| 620 | struct f_sdp *sdp = func_to_sdp(f); |
| 621 | struct usb_composite_dev *cdev = f->config->cdev; |
Ye Li | a9e8fe0 | 2020-08-18 18:16:44 +0800 | [diff] [blame] | 622 | struct usb_gadget *gadget = cdev->gadget; |
Stefan Agner | ffbd5d0 | 2017-08-16 11:00:51 -0700 | [diff] [blame] | 623 | int result; |
| 624 | |
| 625 | debug("%s: intf: %d alt: %d\n", __func__, intf, alt); |
| 626 | |
Sherry Sun | 5226298 | 2020-08-18 18:16:48 +0800 | [diff] [blame] | 627 | if (gadget_is_dualspeed(gadget) && gadget->speed == USB_SPEED_HIGH) { |
Ye Li | a9e8fe0 | 2020-08-18 18:16:44 +0800 | [diff] [blame] | 628 | result = usb_ep_enable(sdp->in_ep, &in_hs_desc); |
Sherry Sun | 5226298 | 2020-08-18 18:16:48 +0800 | [diff] [blame] | 629 | result |= usb_ep_enable(sdp->out_ep, &out_hs_desc); |
| 630 | } else { |
Ye Li | a9e8fe0 | 2020-08-18 18:16:44 +0800 | [diff] [blame] | 631 | result = usb_ep_enable(sdp->in_ep, &in_desc); |
Sherry Sun | 5226298 | 2020-08-18 18:16:48 +0800 | [diff] [blame] | 632 | result |= usb_ep_enable(sdp->out_ep, &out_desc); |
| 633 | } |
Stefan Agner | ffbd5d0 | 2017-08-16 11:00:51 -0700 | [diff] [blame] | 634 | if (result) |
| 635 | return result; |
Sherry Sun | 5226298 | 2020-08-18 18:16:48 +0800 | [diff] [blame] | 636 | |
| 637 | sdp->in_req = sdp_start_ep(sdp->in_ep, true); |
Stefan Agner | ffbd5d0 | 2017-08-16 11:00:51 -0700 | [diff] [blame] | 638 | sdp->in_req->context = sdp; |
Sherry Sun | 5226298 | 2020-08-18 18:16:48 +0800 | [diff] [blame] | 639 | sdp->out_req = sdp_start_ep(sdp->out_ep, false); |
| 640 | sdp->out_req->context = sdp; |
Stefan Agner | ffbd5d0 | 2017-08-16 11:00:51 -0700 | [diff] [blame] | 641 | |
| 642 | sdp->in_ep->driver_data = cdev; /* claim */ |
Sherry Sun | 5226298 | 2020-08-18 18:16:48 +0800 | [diff] [blame] | 643 | sdp->out_ep->driver_data = cdev; /* claim */ |
Stefan Agner | ffbd5d0 | 2017-08-16 11:00:51 -0700 | [diff] [blame] | 644 | |
| 645 | sdp->altsetting = alt; |
| 646 | sdp->state = SDP_STATE_IDLE; |
Sherry Sun | 5226298 | 2020-08-18 18:16:48 +0800 | [diff] [blame] | 647 | sdp->ep_int_enable = true; |
Stefan Agner | ffbd5d0 | 2017-08-16 11:00:51 -0700 | [diff] [blame] | 648 | |
| 649 | return 0; |
| 650 | } |
| 651 | |
| 652 | static int sdp_get_alt(struct usb_function *f, unsigned intf) |
| 653 | { |
| 654 | struct f_sdp *sdp = func_to_sdp(f); |
| 655 | |
| 656 | return sdp->altsetting; |
| 657 | } |
| 658 | |
| 659 | static void sdp_disable(struct usb_function *f) |
| 660 | { |
| 661 | struct f_sdp *sdp = func_to_sdp(f); |
| 662 | |
| 663 | usb_ep_disable(sdp->in_ep); |
Sherry Sun | 5226298 | 2020-08-18 18:16:48 +0800 | [diff] [blame] | 664 | usb_ep_disable(sdp->out_ep); |
Stefan Agner | ffbd5d0 | 2017-08-16 11:00:51 -0700 | [diff] [blame] | 665 | |
| 666 | if (sdp->in_req) { |
Sherry Sun | 5226298 | 2020-08-18 18:16:48 +0800 | [diff] [blame] | 667 | free(sdp->in_req->buf); |
| 668 | usb_ep_free_request(sdp->in_ep, sdp->in_req); |
Stefan Agner | ffbd5d0 | 2017-08-16 11:00:51 -0700 | [diff] [blame] | 669 | sdp->in_req = NULL; |
| 670 | } |
Sherry Sun | 5226298 | 2020-08-18 18:16:48 +0800 | [diff] [blame] | 671 | if (sdp->out_req) { |
| 672 | free(sdp->out_req->buf); |
| 673 | usb_ep_free_request(sdp->out_ep, sdp->out_req); |
| 674 | sdp->out_req = NULL; |
| 675 | } |
Stefan Agner | ffbd5d0 | 2017-08-16 11:00:51 -0700 | [diff] [blame] | 676 | } |
| 677 | |
| 678 | static int sdp_bind_config(struct usb_configuration *c) |
| 679 | { |
| 680 | int status; |
| 681 | |
| 682 | if (!sdp_func) { |
| 683 | sdp_func = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*sdp_func)); |
| 684 | if (!sdp_func) |
| 685 | return -ENOMEM; |
| 686 | } |
| 687 | |
| 688 | memset(sdp_func, 0, sizeof(*sdp_func)); |
| 689 | |
| 690 | sdp_func->usb_function.name = "sdp"; |
Ye Li | a9e8fe0 | 2020-08-18 18:16:44 +0800 | [diff] [blame] | 691 | sdp_func->usb_function.hs_descriptors = sdp_runtime_hs_descs; |
Stefan Agner | ffbd5d0 | 2017-08-16 11:00:51 -0700 | [diff] [blame] | 692 | sdp_func->usb_function.descriptors = sdp_runtime_descs; |
| 693 | sdp_func->usb_function.bind = sdp_bind; |
| 694 | sdp_func->usb_function.unbind = sdp_unbind; |
| 695 | sdp_func->usb_function.set_alt = sdp_set_alt; |
| 696 | sdp_func->usb_function.get_alt = sdp_get_alt; |
| 697 | sdp_func->usb_function.disable = sdp_disable; |
| 698 | sdp_func->usb_function.strings = sdp_generic_strings; |
| 699 | sdp_func->usb_function.setup = sdp_setup; |
| 700 | |
| 701 | status = usb_add_function(c, &sdp_func->usb_function); |
| 702 | |
| 703 | return status; |
| 704 | } |
| 705 | |
Marek Vasut | 2bd299d | 2023-09-01 11:49:58 +0200 | [diff] [blame] | 706 | int sdp_init(struct udevice *udc) |
Stefan Agner | ffbd5d0 | 2017-08-16 11:00:51 -0700 | [diff] [blame] | 707 | { |
| 708 | printf("SDP: initialize...\n"); |
| 709 | while (!sdp_func->configuration_done) { |
| 710 | if (ctrlc()) { |
| 711 | puts("\rCTRL+C - Operation aborted.\n"); |
| 712 | return 1; |
| 713 | } |
Vincent Prince | f26cf93 | 2017-10-23 11:16:35 +0200 | [diff] [blame] | 714 | |
Stefan Roese | 80877fa | 2022-09-02 14:10:46 +0200 | [diff] [blame] | 715 | schedule(); |
Marek Vasut | 2bd299d | 2023-09-01 11:49:58 +0200 | [diff] [blame] | 716 | dm_usb_gadget_handle_interrupts(udc); |
Stefan Agner | ffbd5d0 | 2017-08-16 11:00:51 -0700 | [diff] [blame] | 717 | } |
| 718 | |
| 719 | return 0; |
| 720 | } |
| 721 | |
| 722 | static u32 sdp_jump_imxheader(void *address) |
| 723 | { |
| 724 | flash_header_v2_t *headerv2 = address; |
| 725 | ulong (*entry)(void); |
| 726 | |
| 727 | if (headerv2->header.tag != IVT_HEADER_TAG) { |
| 728 | printf("Header Tag is not an IMX image\n"); |
| 729 | return SDP_ERROR_IMXHEADER; |
| 730 | } |
| 731 | |
| 732 | printf("Jumping to 0x%08x\n", headerv2->entry); |
Andre Heider | e1d461b | 2018-02-15 10:17:29 +0100 | [diff] [blame] | 733 | entry = sdp_ptr(headerv2->entry); |
Stefan Agner | ffbd5d0 | 2017-08-16 11:00:51 -0700 | [diff] [blame] | 734 | entry(); |
| 735 | |
| 736 | /* The image probably never returns hence we won't reach that point */ |
| 737 | return 0; |
| 738 | } |
| 739 | |
Frieder Schrempf | 09580e2 | 2019-06-04 21:56:29 +0200 | [diff] [blame] | 740 | #ifdef CONFIG_SPL_BUILD |
Peng Fan | f49d470 | 2020-08-18 18:16:46 +0800 | [diff] [blame] | 741 | static ulong sdp_load_read(struct spl_load_info *load, ulong sector, |
| 742 | ulong count, void *buf) |
Frieder Schrempf | 09580e2 | 2019-06-04 21:56:29 +0200 | [diff] [blame] | 743 | { |
| 744 | debug("%s: sector %lx, count %lx, buf %lx\n", |
| 745 | __func__, sector, count, (ulong)buf); |
Sean Anderson | 2b5f9c8 | 2023-11-08 11:48:38 -0500 | [diff] [blame] | 746 | memcpy(buf, (void *)(load->priv + sector), count); |
Frieder Schrempf | 09580e2 | 2019-06-04 21:56:29 +0200 | [diff] [blame] | 747 | return count; |
| 748 | } |
Peng Fan | f49d470 | 2020-08-18 18:16:46 +0800 | [diff] [blame] | 749 | |
| 750 | static ulong search_fit_header(ulong p, int size) |
| 751 | { |
| 752 | int i; |
| 753 | |
| 754 | for (i = 0; i < size; i += 4) { |
| 755 | if (genimg_get_format((const void *)(p + i)) == IMAGE_FORMAT_FIT) |
| 756 | return p + i; |
| 757 | } |
| 758 | |
| 759 | return 0; |
| 760 | } |
| 761 | |
| 762 | static ulong search_container_header(ulong p, int size) |
| 763 | { |
| 764 | int i; |
| 765 | u8 *hdr; |
| 766 | |
| 767 | for (i = 0; i < size; i += 4) { |
| 768 | hdr = (u8 *)(p + i); |
| 769 | if (*(hdr + 3) == 0x87 && *hdr == 0) |
| 770 | if (*(hdr + 1) != 0 || *(hdr + 2) != 0) |
| 771 | return p + i; |
| 772 | } |
| 773 | return 0; |
| 774 | } |
Frieder Schrempf | 09580e2 | 2019-06-04 21:56:29 +0200 | [diff] [blame] | 775 | #endif |
| 776 | |
Pali Rohár | dda8f88 | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 777 | static int sdp_handle_in_ep(struct spl_image_info *spl_image, |
| 778 | struct spl_boot_device *bootdev) |
Stefan Agner | ffbd5d0 | 2017-08-16 11:00:51 -0700 | [diff] [blame] | 779 | { |
| 780 | u8 *data = sdp_func->in_req->buf; |
| 781 | u32 status; |
| 782 | int datalen; |
| 783 | |
| 784 | switch (sdp_func->state) { |
| 785 | case SDP_STATE_TX_SEC_CONF: |
| 786 | debug("Report 3: HAB security\n"); |
| 787 | data[0] = 3; |
| 788 | |
| 789 | status = SDP_SECURITY_OPEN; |
| 790 | memcpy(&data[1], &status, 4); |
| 791 | sdp_func->in_req->length = 5; |
| 792 | usb_ep_queue(sdp_func->in_ep, sdp_func->in_req, 0); |
| 793 | sdp_func->state = SDP_STATE_TX_SEC_CONF_BUSY; |
| 794 | break; |
| 795 | |
| 796 | case SDP_STATE_TX_STATUS: |
| 797 | debug("Report 4: Status\n"); |
| 798 | data[0] = 4; |
| 799 | |
| 800 | memcpy(&data[1], &sdp_func->error_status, 4); |
| 801 | sdp_func->in_req->length = 65; |
| 802 | usb_ep_queue(sdp_func->in_ep, sdp_func->in_req, 0); |
| 803 | sdp_func->state = SDP_STATE_TX_STATUS_BUSY; |
| 804 | break; |
| 805 | case SDP_STATE_TX_REGISTER: |
| 806 | debug("Report 4: Register Values\n"); |
| 807 | data[0] = 4; |
| 808 | |
| 809 | datalen = sdp_func->dnl_bytes_remaining; |
| 810 | |
| 811 | if (datalen > 64) |
| 812 | datalen = 64; |
| 813 | |
Andre Heider | e1d461b | 2018-02-15 10:17:29 +0100 | [diff] [blame] | 814 | memcpy(&data[1], sdp_ptr(sdp_func->dnl_address), datalen); |
Stefan Agner | ffbd5d0 | 2017-08-16 11:00:51 -0700 | [diff] [blame] | 815 | sdp_func->in_req->length = 65; |
| 816 | |
| 817 | sdp_func->dnl_bytes_remaining -= datalen; |
| 818 | sdp_func->dnl_address += datalen; |
| 819 | |
| 820 | usb_ep_queue(sdp_func->in_ep, sdp_func->in_req, 0); |
| 821 | sdp_func->state = SDP_STATE_TX_REGISTER_BUSY; |
| 822 | break; |
| 823 | case SDP_STATE_JUMP: |
Stefan Agner | 3888da7 | 2017-08-16 11:00:52 -0700 | [diff] [blame] | 824 | printf("Jumping to header at 0x%08x\n", sdp_func->jmp_address); |
Andre Heider | e1d461b | 2018-02-15 10:17:29 +0100 | [diff] [blame] | 825 | status = sdp_jump_imxheader(sdp_ptr(sdp_func->jmp_address)); |
Stefan Agner | 3888da7 | 2017-08-16 11:00:52 -0700 | [diff] [blame] | 826 | |
| 827 | /* If imx header fails, try some U-Boot specific headers */ |
| 828 | if (status) { |
| 829 | #ifdef CONFIG_SPL_BUILD |
Peng Fan | f49d470 | 2020-08-18 18:16:46 +0800 | [diff] [blame] | 830 | if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER)) |
| 831 | sdp_func->jmp_address = (u32)search_container_header((ulong)sdp_func->jmp_address, sdp_func->dnl_bytes); |
| 832 | else if (IS_ENABLED(CONFIG_SPL_LOAD_FIT)) |
| 833 | sdp_func->jmp_address = (u32)search_fit_header((ulong)sdp_func->jmp_address, sdp_func->dnl_bytes); |
| 834 | if (sdp_func->jmp_address == 0) |
| 835 | panic("Error in search header, failed to jump\n"); |
| 836 | |
| 837 | printf("Found header at 0x%08x\n", sdp_func->jmp_address); |
| 838 | |
Simon Glass | bb7d3bb | 2022-09-06 20:26:52 -0600 | [diff] [blame] | 839 | struct legacy_img_hdr *header = |
Frieder Schrempf | 09580e2 | 2019-06-04 21:56:29 +0200 | [diff] [blame] | 840 | sdp_ptr(sdp_func->jmp_address); |
| 841 | #ifdef CONFIG_SPL_LOAD_FIT |
| 842 | if (image_get_magic(header) == FDT_MAGIC) { |
| 843 | struct spl_load_info load; |
| 844 | |
| 845 | debug("Found FIT\n"); |
Sean Anderson | 2b5f9c8 | 2023-11-08 11:48:38 -0500 | [diff] [blame] | 846 | load.priv = header; |
Sean Anderson | 35f15fe | 2023-11-08 11:48:43 -0500 | [diff] [blame] | 847 | spl_set_bl_len(&load, 1); |
Peng Fan | f49d470 | 2020-08-18 18:16:46 +0800 | [diff] [blame] | 848 | load.read = sdp_load_read; |
Frieder Schrempf | 09580e2 | 2019-06-04 21:56:29 +0200 | [diff] [blame] | 849 | spl_load_simple_fit(spl_image, &load, 0, |
| 850 | header); |
| 851 | |
Peng Fan | f49d470 | 2020-08-18 18:16:46 +0800 | [diff] [blame] | 852 | return SDP_EXIT; |
Frieder Schrempf | 09580e2 | 2019-06-04 21:56:29 +0200 | [diff] [blame] | 853 | } |
| 854 | #endif |
Sean Anderson | 952ed67 | 2023-10-14 16:47:44 -0400 | [diff] [blame] | 855 | if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER) && |
| 856 | valid_container_hdr((void *)header)) { |
Peng Fan | f49d470 | 2020-08-18 18:16:46 +0800 | [diff] [blame] | 857 | struct spl_load_info load; |
| 858 | |
Sean Anderson | 2b5f9c8 | 2023-11-08 11:48:38 -0500 | [diff] [blame] | 859 | load.priv = header; |
Sean Anderson | 35f15fe | 2023-11-08 11:48:43 -0500 | [diff] [blame] | 860 | spl_set_bl_len(&load, 1); |
Peng Fan | f49d470 | 2020-08-18 18:16:46 +0800 | [diff] [blame] | 861 | load.read = sdp_load_read; |
| 862 | spl_load_imx_container(spl_image, &load, 0); |
| 863 | return SDP_EXIT; |
| 864 | } |
| 865 | |
Stefan Agner | 3888da7 | 2017-08-16 11:00:52 -0700 | [diff] [blame] | 866 | /* In SPL, allow jumps to U-Boot images */ |
| 867 | struct spl_image_info spl_image = {}; |
Pali Rohár | dda8f88 | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 868 | struct spl_boot_device bootdev = {}; |
| 869 | spl_parse_image_header(&spl_image, &bootdev, header); |
Marek Vasut | a0a4ce8 | 2023-03-07 08:42:33 +0100 | [diff] [blame] | 870 | spl_board_prepare_for_boot(); |
Stefan Agner | 3888da7 | 2017-08-16 11:00:52 -0700 | [diff] [blame] | 871 | jump_to_image_no_args(&spl_image); |
| 872 | #else |
| 873 | /* In U-Boot, allow jumps to scripts */ |
Simon Glass | daee3ba | 2023-01-06 08:52:28 -0600 | [diff] [blame] | 874 | cmd_source_script(sdp_func->jmp_address, NULL, NULL); |
Stefan Agner | 3888da7 | 2017-08-16 11:00:52 -0700 | [diff] [blame] | 875 | #endif |
| 876 | } |
Stefan Agner | ffbd5d0 | 2017-08-16 11:00:51 -0700 | [diff] [blame] | 877 | |
| 878 | sdp_func->next_state = SDP_STATE_IDLE; |
| 879 | sdp_func->error_status = status; |
| 880 | |
| 881 | /* Only send Report 4 if there was an error */ |
| 882 | if (status) |
| 883 | sdp_func->state = SDP_STATE_TX_STATUS; |
| 884 | else |
| 885 | sdp_func->state = SDP_STATE_IDLE; |
| 886 | break; |
| 887 | default: |
| 888 | break; |
| 889 | }; |
Peng Fan | f49d470 | 2020-08-18 18:16:46 +0800 | [diff] [blame] | 890 | |
| 891 | return 0; |
Stefan Agner | ffbd5d0 | 2017-08-16 11:00:51 -0700 | [diff] [blame] | 892 | } |
| 893 | |
Sherry Sun | 5226298 | 2020-08-18 18:16:48 +0800 | [diff] [blame] | 894 | static void sdp_handle_out_ep(void) |
| 895 | { |
| 896 | int rc; |
| 897 | |
| 898 | if (sdp_func->state == SDP_STATE_IDLE) { |
| 899 | sdp_func->out_req->complete = sdp_rx_command_complete; |
| 900 | rc = usb_ep_queue(sdp_func->out_ep, sdp_func->out_req, 0); |
| 901 | if (rc) |
| 902 | printf("error in submission: %s\n", |
| 903 | sdp_func->out_ep->name); |
| 904 | sdp_func->state = SDP_STATE_RX_CMD; |
| 905 | } else if (sdp_func->state == SDP_STATE_RX_FILE_DATA) { |
| 906 | sdp_func->out_req->complete = sdp_rx_data_complete; |
| 907 | rc = usb_ep_queue(sdp_func->out_ep, sdp_func->out_req, 0); |
| 908 | if (rc) |
| 909 | printf("error in submission: %s\n", |
| 910 | sdp_func->out_ep->name); |
| 911 | sdp_func->state = SDP_STATE_RX_FILE_DATA_BUSY; |
| 912 | } |
| 913 | } |
| 914 | |
Frieder Schrempf | 09580e2 | 2019-06-04 21:56:29 +0200 | [diff] [blame] | 915 | #ifndef CONFIG_SPL_BUILD |
Marek Vasut | 2bd299d | 2023-09-01 11:49:58 +0200 | [diff] [blame] | 916 | int sdp_handle(struct udevice *udc) |
Frieder Schrempf | 09580e2 | 2019-06-04 21:56:29 +0200 | [diff] [blame] | 917 | #else |
Marek Vasut | 2bd299d | 2023-09-01 11:49:58 +0200 | [diff] [blame] | 918 | int spl_sdp_handle(struct udevice *udc, struct spl_image_info *spl_image, |
Pali Rohár | dda8f88 | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 919 | struct spl_boot_device *bootdev) |
Frieder Schrempf | 09580e2 | 2019-06-04 21:56:29 +0200 | [diff] [blame] | 920 | #endif |
Stefan Agner | ffbd5d0 | 2017-08-16 11:00:51 -0700 | [diff] [blame] | 921 | { |
Peng Fan | f49d470 | 2020-08-18 18:16:46 +0800 | [diff] [blame] | 922 | int flag = 0; |
Stefan Agner | ffbd5d0 | 2017-08-16 11:00:51 -0700 | [diff] [blame] | 923 | printf("SDP: handle requests...\n"); |
| 924 | while (1) { |
| 925 | if (ctrlc()) { |
| 926 | puts("\rCTRL+C - Operation aborted.\n"); |
Frieder Schrempf | 09580e2 | 2019-06-04 21:56:29 +0200 | [diff] [blame] | 927 | return -EINVAL; |
Stefan Agner | ffbd5d0 | 2017-08-16 11:00:51 -0700 | [diff] [blame] | 928 | } |
| 929 | |
Peng Fan | f49d470 | 2020-08-18 18:16:46 +0800 | [diff] [blame] | 930 | if (flag == SDP_EXIT) |
Frieder Schrempf | 09580e2 | 2019-06-04 21:56:29 +0200 | [diff] [blame] | 931 | return 0; |
Frieder Schrempf | 09580e2 | 2019-06-04 21:56:29 +0200 | [diff] [blame] | 932 | |
Stefan Roese | 80877fa | 2022-09-02 14:10:46 +0200 | [diff] [blame] | 933 | schedule(); |
Marek Vasut | 2bd299d | 2023-09-01 11:49:58 +0200 | [diff] [blame] | 934 | dm_usb_gadget_handle_interrupts(udc); |
Stefan Agner | ffbd5d0 | 2017-08-16 11:00:51 -0700 | [diff] [blame] | 935 | |
Frieder Schrempf | 09580e2 | 2019-06-04 21:56:29 +0200 | [diff] [blame] | 936 | #ifdef CONFIG_SPL_BUILD |
Pali Rohár | dda8f88 | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 937 | flag = sdp_handle_in_ep(spl_image, bootdev); |
Frieder Schrempf | 09580e2 | 2019-06-04 21:56:29 +0200 | [diff] [blame] | 938 | #else |
Pali Rohár | dda8f88 | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 939 | flag = sdp_handle_in_ep(NULL, NULL); |
Frieder Schrempf | 09580e2 | 2019-06-04 21:56:29 +0200 | [diff] [blame] | 940 | #endif |
Sherry Sun | 5226298 | 2020-08-18 18:16:48 +0800 | [diff] [blame] | 941 | if (sdp_func->ep_int_enable) |
| 942 | sdp_handle_out_ep(); |
Stefan Agner | ffbd5d0 | 2017-08-16 11:00:51 -0700 | [diff] [blame] | 943 | } |
| 944 | } |
| 945 | |
| 946 | int sdp_add(struct usb_configuration *c) |
| 947 | { |
| 948 | int id; |
| 949 | |
| 950 | id = usb_string_id(c->cdev); |
| 951 | if (id < 0) |
| 952 | return id; |
| 953 | strings_sdp_generic[0].id = id; |
| 954 | sdp_intf_runtime.iInterface = id; |
| 955 | |
| 956 | debug("%s: cdev: %p gadget: %p gadget->ep0: %p\n", __func__, |
| 957 | c->cdev, c->cdev->gadget, c->cdev->gadget->ep0); |
| 958 | |
| 959 | return sdp_bind_config(c); |
| 960 | } |
| 961 | |
| 962 | DECLARE_GADGET_BIND_CALLBACK(usb_dnl_sdp, sdp_add); |