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> |
| 20 | #include <common.h> |
| 21 | #include <console.h> |
| 22 | #include <malloc.h> |
| 23 | |
| 24 | #include <linux/usb/ch9.h> |
| 25 | #include <linux/usb/gadget.h> |
| 26 | #include <linux/usb/composite.h> |
| 27 | |
| 28 | #include <asm/io.h> |
| 29 | #include <g_dnl.h> |
| 30 | #include <sdp.h> |
Stefan Agner | 3888da7 | 2017-08-16 11:00:52 -0700 | [diff] [blame] | 31 | #include <spl.h> |
| 32 | #include <image.h> |
Stefan Agner | ffbd5d0 | 2017-08-16 11:00:51 -0700 | [diff] [blame] | 33 | #include <imximage.h> |
Vincent Prince | f26cf93 | 2017-10-23 11:16:35 +0200 | [diff] [blame] | 34 | #include <watchdog.h> |
Stefan Agner | ffbd5d0 | 2017-08-16 11:00:51 -0700 | [diff] [blame] | 35 | |
| 36 | #define HID_REPORT_ID_MASK 0x000000ff |
| 37 | |
| 38 | /* |
| 39 | * HID class requests |
| 40 | */ |
| 41 | #define HID_REQ_GET_REPORT 0x01 |
| 42 | #define HID_REQ_GET_IDLE 0x02 |
| 43 | #define HID_REQ_GET_PROTOCOL 0x03 |
| 44 | #define HID_REQ_SET_REPORT 0x09 |
| 45 | #define HID_REQ_SET_IDLE 0x0A |
| 46 | #define HID_REQ_SET_PROTOCOL 0x0B |
| 47 | |
| 48 | #define HID_USAGE_PAGE_LEN 76 |
| 49 | |
| 50 | struct hid_report { |
| 51 | u8 usage_page[HID_USAGE_PAGE_LEN]; |
| 52 | } __packed; |
| 53 | |
| 54 | #define SDP_READ_REGISTER 0x0101 |
| 55 | #define SDP_WRITE_REGISTER 0x0202 |
| 56 | #define SDP_WRITE_FILE 0x0404 |
| 57 | #define SDP_ERROR_STATUS 0x0505 |
| 58 | #define SDP_DCD_WRITE 0x0a0a |
| 59 | #define SDP_JUMP_ADDRESS 0x0b0b |
| 60 | #define SDP_SKIP_DCD_HEADER 0x0c0c |
| 61 | |
| 62 | #define SDP_SECURITY_CLOSED 0x12343412 |
| 63 | #define SDP_SECURITY_OPEN 0x56787856 |
| 64 | |
| 65 | #define SDP_WRITE_FILE_COMPLETE 0x88888888 |
| 66 | #define SDP_WRITE_REGISTER_COMPLETE 0x128A8A12 |
| 67 | #define SDP_SKIP_DCD_HEADER_COMPLETE 0x900DD009 |
| 68 | #define SDP_ERROR_IMXHEADER 0x000a0533 |
| 69 | |
| 70 | #define SDP_COMMAND_LEN 16 |
| 71 | |
| 72 | struct sdp_command { |
| 73 | u16 cmd; |
| 74 | u32 addr; |
| 75 | u8 format; |
| 76 | u32 cnt; |
| 77 | u32 data; |
| 78 | u8 rsvd; |
| 79 | } __packed; |
| 80 | |
| 81 | enum sdp_state { |
| 82 | SDP_STATE_IDLE, |
| 83 | SDP_STATE_RX_DCD_DATA, |
| 84 | SDP_STATE_RX_FILE_DATA, |
| 85 | SDP_STATE_TX_SEC_CONF, |
| 86 | SDP_STATE_TX_SEC_CONF_BUSY, |
| 87 | SDP_STATE_TX_REGISTER, |
| 88 | SDP_STATE_TX_REGISTER_BUSY, |
| 89 | SDP_STATE_TX_STATUS, |
| 90 | SDP_STATE_TX_STATUS_BUSY, |
| 91 | SDP_STATE_JUMP, |
| 92 | }; |
| 93 | |
| 94 | struct f_sdp { |
| 95 | struct usb_function usb_function; |
| 96 | |
| 97 | struct usb_descriptor_header **function; |
| 98 | |
| 99 | u8 altsetting; |
| 100 | enum sdp_state state; |
| 101 | enum sdp_state next_state; |
| 102 | u32 dnl_address; |
Petr Štetiar | d991cc3 | 2018-11-23 14:37:52 +0100 | [diff] [blame] | 103 | u32 dnl_bytes; |
Stefan Agner | ffbd5d0 | 2017-08-16 11:00:51 -0700 | [diff] [blame] | 104 | u32 dnl_bytes_remaining; |
| 105 | u32 jmp_address; |
| 106 | bool always_send_status; |
| 107 | u32 error_status; |
| 108 | |
| 109 | /* EP0 request */ |
| 110 | struct usb_request *req; |
| 111 | |
| 112 | /* EP1 IN */ |
| 113 | struct usb_ep *in_ep; |
| 114 | struct usb_request *in_req; |
| 115 | |
| 116 | bool configuration_done; |
| 117 | }; |
| 118 | |
| 119 | static struct f_sdp *sdp_func; |
| 120 | |
| 121 | static inline struct f_sdp *func_to_sdp(struct usb_function *f) |
| 122 | { |
| 123 | return container_of(f, struct f_sdp, usb_function); |
| 124 | } |
| 125 | |
| 126 | static struct usb_interface_descriptor sdp_intf_runtime = { |
| 127 | .bLength = sizeof(sdp_intf_runtime), |
| 128 | .bDescriptorType = USB_DT_INTERFACE, |
| 129 | .bAlternateSetting = 0, |
| 130 | .bNumEndpoints = 1, |
| 131 | .bInterfaceClass = USB_CLASS_HID, |
| 132 | .bInterfaceSubClass = 0, |
| 133 | .bInterfaceProtocol = 0, |
| 134 | /* .iInterface = DYNAMIC */ |
| 135 | }; |
| 136 | |
| 137 | /* HID configuration */ |
| 138 | static struct usb_class_hid_descriptor sdp_hid_desc = { |
| 139 | .bLength = sizeof(sdp_hid_desc), |
| 140 | .bDescriptorType = USB_DT_CS_DEVICE, |
| 141 | |
| 142 | .bcdCDC = __constant_cpu_to_le16(0x0110), |
| 143 | .bCountryCode = 0, |
| 144 | .bNumDescriptors = 1, |
| 145 | |
| 146 | .bDescriptorType0 = USB_DT_HID_REPORT, |
| 147 | .wDescriptorLength0 = HID_USAGE_PAGE_LEN, |
| 148 | }; |
| 149 | |
| 150 | static struct usb_endpoint_descriptor in_desc = { |
| 151 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 152 | .bDescriptorType = USB_DT_ENDPOINT, /*USB_DT_CS_ENDPOINT*/ |
| 153 | |
| 154 | .bEndpointAddress = 1 | USB_DIR_IN, |
| 155 | .bmAttributes = USB_ENDPOINT_XFER_INT, |
| 156 | .wMaxPacketSize = 64, |
| 157 | .bInterval = 1, |
| 158 | }; |
| 159 | |
| 160 | static struct usb_descriptor_header *sdp_runtime_descs[] = { |
| 161 | (struct usb_descriptor_header *)&sdp_intf_runtime, |
| 162 | (struct usb_descriptor_header *)&sdp_hid_desc, |
| 163 | (struct usb_descriptor_header *)&in_desc, |
| 164 | NULL, |
| 165 | }; |
| 166 | |
| 167 | /* This is synchronized with what the SoC implementation reports */ |
| 168 | static struct hid_report sdp_hid_report = { |
| 169 | .usage_page = { |
| 170 | 0x06, 0x00, 0xff, /* Usage Page */ |
| 171 | 0x09, 0x01, /* Usage (Pointer?) */ |
| 172 | 0xa1, 0x01, /* Collection */ |
| 173 | |
| 174 | 0x85, 0x01, /* Report ID */ |
| 175 | 0x19, 0x01, /* Usage Minimum */ |
| 176 | 0x29, 0x01, /* Usage Maximum */ |
| 177 | 0x15, 0x00, /* Local Minimum */ |
| 178 | 0x26, 0xFF, 0x00, /* Local Maximum? */ |
| 179 | 0x75, 0x08, /* Report Size */ |
| 180 | 0x95, 0x10, /* Report Count */ |
| 181 | 0x91, 0x02, /* Output Data */ |
| 182 | |
| 183 | 0x85, 0x02, /* Report ID */ |
| 184 | 0x19, 0x01, /* Usage Minimum */ |
| 185 | 0x29, 0x01, /* Usage Maximum */ |
| 186 | 0x15, 0x00, /* Local Minimum */ |
| 187 | 0x26, 0xFF, 0x00, /* Local Maximum? */ |
| 188 | 0x75, 0x80, /* Report Size 128 */ |
| 189 | 0x95, 0x40, /* Report Count */ |
| 190 | 0x91, 0x02, /* Output Data */ |
| 191 | |
| 192 | 0x85, 0x03, /* Report ID */ |
| 193 | 0x19, 0x01, /* Usage Minimum */ |
| 194 | 0x29, 0x01, /* Usage Maximum */ |
| 195 | 0x15, 0x00, /* Local Minimum */ |
| 196 | 0x26, 0xFF, 0x00, /* Local Maximum? */ |
| 197 | 0x75, 0x08, /* Report Size 8 */ |
| 198 | 0x95, 0x04, /* Report Count */ |
| 199 | 0x81, 0x02, /* Input Data */ |
| 200 | |
| 201 | 0x85, 0x04, /* Report ID */ |
| 202 | 0x19, 0x01, /* Usage Minimum */ |
| 203 | 0x29, 0x01, /* Usage Maximum */ |
| 204 | 0x15, 0x00, /* Local Minimum */ |
| 205 | 0x26, 0xFF, 0x00, /* Local Maximum? */ |
| 206 | 0x75, 0x08, /* Report Size 8 */ |
| 207 | 0x95, 0x40, /* Report Count */ |
| 208 | 0x81, 0x02, /* Input Data */ |
| 209 | 0xc0 |
| 210 | }, |
| 211 | }; |
| 212 | |
| 213 | static const char sdp_name[] = "Serial Downloader Protocol"; |
| 214 | |
| 215 | /* |
| 216 | * static strings, in UTF-8 |
| 217 | */ |
| 218 | static struct usb_string strings_sdp_generic[] = { |
| 219 | [0].s = sdp_name, |
| 220 | { } /* end of list */ |
| 221 | }; |
| 222 | |
| 223 | static struct usb_gadget_strings stringtab_sdp_generic = { |
| 224 | .language = 0x0409, /* en-us */ |
| 225 | .strings = strings_sdp_generic, |
| 226 | }; |
| 227 | |
| 228 | static struct usb_gadget_strings *sdp_generic_strings[] = { |
| 229 | &stringtab_sdp_generic, |
| 230 | NULL, |
| 231 | }; |
| 232 | |
Andre Heider | e1d461b | 2018-02-15 10:17:29 +0100 | [diff] [blame] | 233 | static inline void *sdp_ptr(u32 val) |
| 234 | { |
| 235 | return (void *)(uintptr_t)val; |
| 236 | } |
| 237 | |
Stefan Agner | ffbd5d0 | 2017-08-16 11:00:51 -0700 | [diff] [blame] | 238 | static void sdp_rx_command_complete(struct usb_ep *ep, struct usb_request *req) |
| 239 | { |
| 240 | struct f_sdp *sdp = req->context; |
| 241 | int status = req->status; |
| 242 | u8 *data = req->buf; |
| 243 | u8 report = data[0]; |
| 244 | |
| 245 | if (status != 0) { |
Andre Heider | b841e97 | 2018-02-15 07:08:55 +0100 | [diff] [blame] | 246 | pr_err("Status: %d\n", status); |
Stefan Agner | ffbd5d0 | 2017-08-16 11:00:51 -0700 | [diff] [blame] | 247 | return; |
| 248 | } |
| 249 | |
| 250 | if (report != 1) { |
Andre Heider | b841e97 | 2018-02-15 07:08:55 +0100 | [diff] [blame] | 251 | pr_err("Unexpected report %d\n", report); |
Stefan Agner | ffbd5d0 | 2017-08-16 11:00:51 -0700 | [diff] [blame] | 252 | return; |
| 253 | } |
| 254 | |
| 255 | struct sdp_command *cmd = req->buf + 1; |
| 256 | |
| 257 | debug("%s: command: %04x, addr: %08x, cnt: %u\n", |
| 258 | __func__, be16_to_cpu(cmd->cmd), |
| 259 | be32_to_cpu(cmd->addr), be32_to_cpu(cmd->cnt)); |
| 260 | |
| 261 | switch (be16_to_cpu(cmd->cmd)) { |
| 262 | case SDP_READ_REGISTER: |
| 263 | sdp->always_send_status = false; |
| 264 | sdp->error_status = 0x0; |
| 265 | |
| 266 | sdp->state = SDP_STATE_TX_SEC_CONF; |
| 267 | sdp->dnl_address = be32_to_cpu(cmd->addr); |
| 268 | sdp->dnl_bytes_remaining = be32_to_cpu(cmd->cnt); |
| 269 | sdp->next_state = SDP_STATE_TX_REGISTER; |
| 270 | printf("Reading %d registers at 0x%08x... ", |
| 271 | sdp->dnl_bytes_remaining, sdp->dnl_address); |
| 272 | break; |
| 273 | case SDP_WRITE_FILE: |
| 274 | sdp->always_send_status = true; |
| 275 | sdp->error_status = SDP_WRITE_FILE_COMPLETE; |
| 276 | |
| 277 | sdp->state = SDP_STATE_RX_FILE_DATA; |
| 278 | sdp->dnl_address = be32_to_cpu(cmd->addr); |
| 279 | sdp->dnl_bytes_remaining = be32_to_cpu(cmd->cnt); |
Petr Štetiar | d991cc3 | 2018-11-23 14:37:52 +0100 | [diff] [blame] | 280 | sdp->dnl_bytes = sdp->dnl_bytes_remaining; |
Stefan Agner | ffbd5d0 | 2017-08-16 11:00:51 -0700 | [diff] [blame] | 281 | sdp->next_state = SDP_STATE_IDLE; |
| 282 | |
| 283 | printf("Downloading file of size %d to 0x%08x... ", |
| 284 | sdp->dnl_bytes_remaining, sdp->dnl_address); |
| 285 | |
| 286 | break; |
| 287 | case SDP_ERROR_STATUS: |
| 288 | sdp->always_send_status = true; |
| 289 | sdp->error_status = 0; |
| 290 | |
| 291 | sdp->state = SDP_STATE_TX_SEC_CONF; |
| 292 | sdp->next_state = SDP_STATE_IDLE; |
| 293 | break; |
| 294 | case SDP_DCD_WRITE: |
| 295 | sdp->always_send_status = true; |
| 296 | sdp->error_status = SDP_WRITE_REGISTER_COMPLETE; |
| 297 | |
| 298 | sdp->state = SDP_STATE_RX_DCD_DATA; |
| 299 | sdp->dnl_bytes_remaining = be32_to_cpu(cmd->cnt); |
| 300 | sdp->next_state = SDP_STATE_IDLE; |
| 301 | break; |
| 302 | case SDP_JUMP_ADDRESS: |
| 303 | sdp->always_send_status = false; |
| 304 | sdp->error_status = 0; |
| 305 | |
| 306 | sdp->jmp_address = be32_to_cpu(cmd->addr); |
| 307 | sdp->state = SDP_STATE_TX_SEC_CONF; |
| 308 | sdp->next_state = SDP_STATE_JUMP; |
| 309 | break; |
| 310 | case SDP_SKIP_DCD_HEADER: |
| 311 | sdp->always_send_status = true; |
| 312 | sdp->error_status = SDP_SKIP_DCD_HEADER_COMPLETE; |
| 313 | |
| 314 | /* Ignore command, DCD not supported anyway */ |
| 315 | sdp->state = SDP_STATE_TX_SEC_CONF; |
| 316 | sdp->next_state = SDP_STATE_IDLE; |
| 317 | break; |
| 318 | default: |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 319 | pr_err("Unknown command: %04x\n", be16_to_cpu(cmd->cmd)); |
Stefan Agner | ffbd5d0 | 2017-08-16 11:00:51 -0700 | [diff] [blame] | 320 | } |
| 321 | } |
| 322 | |
| 323 | static void sdp_rx_data_complete(struct usb_ep *ep, struct usb_request *req) |
| 324 | { |
| 325 | struct f_sdp *sdp = req->context; |
| 326 | int status = req->status; |
| 327 | u8 *data = req->buf; |
| 328 | u8 report = data[0]; |
| 329 | int datalen = req->length - 1; |
| 330 | |
| 331 | if (status != 0) { |
Andre Heider | b841e97 | 2018-02-15 07:08:55 +0100 | [diff] [blame] | 332 | pr_err("Status: %d\n", status); |
Stefan Agner | ffbd5d0 | 2017-08-16 11:00:51 -0700 | [diff] [blame] | 333 | return; |
| 334 | } |
| 335 | |
| 336 | if (report != 2) { |
Andre Heider | b841e97 | 2018-02-15 07:08:55 +0100 | [diff] [blame] | 337 | pr_err("Unexpected report %d\n", report); |
Stefan Agner | ffbd5d0 | 2017-08-16 11:00:51 -0700 | [diff] [blame] | 338 | return; |
| 339 | } |
| 340 | |
| 341 | if (sdp->dnl_bytes_remaining < datalen) { |
| 342 | /* |
| 343 | * Some USB stacks require to send a complete buffer as |
| 344 | * specified in the HID descriptor. This leads to longer |
| 345 | * transfers than the file length, no problem for us. |
| 346 | */ |
| 347 | sdp->dnl_bytes_remaining = 0; |
| 348 | } else { |
| 349 | sdp->dnl_bytes_remaining -= datalen; |
| 350 | } |
| 351 | |
| 352 | if (sdp->state == SDP_STATE_RX_FILE_DATA) { |
Andre Heider | e1d461b | 2018-02-15 10:17:29 +0100 | [diff] [blame] | 353 | memcpy(sdp_ptr(sdp->dnl_address), req->buf + 1, datalen); |
Stefan Agner | ffbd5d0 | 2017-08-16 11:00:51 -0700 | [diff] [blame] | 354 | sdp->dnl_address += datalen; |
| 355 | } |
| 356 | |
| 357 | if (sdp->dnl_bytes_remaining) |
| 358 | return; |
| 359 | |
Petr Štetiar | d991cc3 | 2018-11-23 14:37:52 +0100 | [diff] [blame] | 360 | #ifndef CONFIG_SPL_BUILD |
| 361 | env_set_hex("filesize", sdp->dnl_bytes); |
| 362 | #endif |
Stefan Agner | ffbd5d0 | 2017-08-16 11:00:51 -0700 | [diff] [blame] | 363 | printf("done\n"); |
| 364 | |
| 365 | switch (sdp->state) { |
| 366 | case SDP_STATE_RX_FILE_DATA: |
| 367 | sdp->state = SDP_STATE_TX_SEC_CONF; |
| 368 | break; |
| 369 | case SDP_STATE_RX_DCD_DATA: |
| 370 | sdp->state = SDP_STATE_TX_SEC_CONF; |
| 371 | break; |
| 372 | default: |
Andre Heider | b841e97 | 2018-02-15 07:08:55 +0100 | [diff] [blame] | 373 | pr_err("Invalid state: %d\n", sdp->state); |
Stefan Agner | ffbd5d0 | 2017-08-16 11:00:51 -0700 | [diff] [blame] | 374 | } |
| 375 | } |
| 376 | |
| 377 | static void sdp_tx_complete(struct usb_ep *ep, struct usb_request *req) |
| 378 | { |
| 379 | struct f_sdp *sdp = req->context; |
| 380 | int status = req->status; |
| 381 | |
| 382 | if (status != 0) { |
Andre Heider | b841e97 | 2018-02-15 07:08:55 +0100 | [diff] [blame] | 383 | pr_err("Status: %d\n", status); |
Stefan Agner | ffbd5d0 | 2017-08-16 11:00:51 -0700 | [diff] [blame] | 384 | return; |
| 385 | } |
| 386 | |
| 387 | switch (sdp->state) { |
| 388 | case SDP_STATE_TX_SEC_CONF_BUSY: |
| 389 | /* Not all commands require status report */ |
| 390 | if (sdp->always_send_status || sdp->error_status) |
| 391 | sdp->state = SDP_STATE_TX_STATUS; |
| 392 | else |
| 393 | sdp->state = sdp->next_state; |
| 394 | |
| 395 | break; |
| 396 | case SDP_STATE_TX_STATUS_BUSY: |
| 397 | sdp->state = sdp->next_state; |
| 398 | break; |
| 399 | case SDP_STATE_TX_REGISTER_BUSY: |
| 400 | if (sdp->dnl_bytes_remaining) |
| 401 | sdp->state = SDP_STATE_TX_REGISTER; |
| 402 | else |
| 403 | sdp->state = SDP_STATE_IDLE; |
| 404 | break; |
| 405 | default: |
Andre Heider | b841e97 | 2018-02-15 07:08:55 +0100 | [diff] [blame] | 406 | pr_err("Wrong State: %d\n", sdp->state); |
Stefan Agner | ffbd5d0 | 2017-08-16 11:00:51 -0700 | [diff] [blame] | 407 | sdp->state = SDP_STATE_IDLE; |
| 408 | break; |
| 409 | } |
| 410 | debug("%s complete --> %d, %d/%d\n", ep->name, |
| 411 | status, req->actual, req->length); |
| 412 | } |
| 413 | |
| 414 | static int sdp_setup(struct usb_function *f, const struct usb_ctrlrequest *ctrl) |
| 415 | { |
| 416 | struct usb_gadget *gadget = f->config->cdev->gadget; |
| 417 | struct usb_request *req = f->config->cdev->req; |
| 418 | struct f_sdp *sdp = f->config->cdev->req->context; |
| 419 | u16 len = le16_to_cpu(ctrl->wLength); |
| 420 | u16 w_value = le16_to_cpu(ctrl->wValue); |
| 421 | int value = 0; |
| 422 | u8 req_type = ctrl->bRequestType & USB_TYPE_MASK; |
| 423 | |
| 424 | debug("w_value: 0x%04x len: 0x%04x\n", w_value, len); |
| 425 | debug("req_type: 0x%02x ctrl->bRequest: 0x%02x sdp->state: %d\n", |
| 426 | req_type, ctrl->bRequest, sdp->state); |
| 427 | |
| 428 | if (req_type == USB_TYPE_STANDARD) { |
| 429 | if (ctrl->bRequest == USB_REQ_GET_DESCRIPTOR) { |
| 430 | /* Send HID report descriptor */ |
| 431 | value = min(len, (u16) sizeof(sdp_hid_report)); |
| 432 | memcpy(req->buf, &sdp_hid_report, value); |
| 433 | sdp->configuration_done = true; |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | if (req_type == USB_TYPE_CLASS) { |
| 438 | int report = w_value & HID_REPORT_ID_MASK; |
| 439 | |
| 440 | /* HID (SDP) request */ |
| 441 | switch (ctrl->bRequest) { |
| 442 | case HID_REQ_SET_REPORT: |
| 443 | switch (report) { |
| 444 | case 1: |
| 445 | value = SDP_COMMAND_LEN + 1; |
| 446 | req->complete = sdp_rx_command_complete; |
| 447 | break; |
| 448 | case 2: |
| 449 | value = len; |
| 450 | req->complete = sdp_rx_data_complete; |
| 451 | break; |
| 452 | } |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | if (value >= 0) { |
| 457 | req->length = value; |
| 458 | req->zero = value < len; |
| 459 | value = usb_ep_queue(gadget->ep0, req, 0); |
| 460 | if (value < 0) { |
| 461 | debug("ep_queue --> %d\n", value); |
| 462 | req->status = 0; |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | return value; |
| 467 | } |
| 468 | |
| 469 | static int sdp_bind(struct usb_configuration *c, struct usb_function *f) |
| 470 | { |
| 471 | struct usb_gadget *gadget = c->cdev->gadget; |
| 472 | struct usb_composite_dev *cdev = c->cdev; |
| 473 | struct f_sdp *sdp = func_to_sdp(f); |
| 474 | int rv = 0, id; |
| 475 | |
| 476 | id = usb_interface_id(c, f); |
| 477 | if (id < 0) |
| 478 | return id; |
| 479 | sdp_intf_runtime.bInterfaceNumber = id; |
| 480 | |
| 481 | struct usb_ep *ep; |
| 482 | |
| 483 | /* allocate instance-specific endpoints */ |
| 484 | ep = usb_ep_autoconfig(gadget, &in_desc); |
| 485 | if (!ep) { |
| 486 | rv = -ENODEV; |
| 487 | goto error; |
| 488 | } |
| 489 | |
| 490 | sdp->in_ep = ep; /* Store IN EP for enabling @ setup */ |
| 491 | |
| 492 | cdev->req->context = sdp; |
| 493 | |
| 494 | error: |
| 495 | return rv; |
| 496 | } |
| 497 | |
| 498 | static void sdp_unbind(struct usb_configuration *c, struct usb_function *f) |
| 499 | { |
| 500 | free(sdp_func); |
| 501 | sdp_func = NULL; |
| 502 | } |
| 503 | |
| 504 | static struct usb_request *alloc_ep_req(struct usb_ep *ep, unsigned length) |
| 505 | { |
| 506 | struct usb_request *req; |
| 507 | |
| 508 | req = usb_ep_alloc_request(ep, 0); |
| 509 | if (!req) |
| 510 | return req; |
| 511 | |
| 512 | req->length = length; |
| 513 | req->buf = memalign(CONFIG_SYS_CACHELINE_SIZE, length); |
| 514 | if (!req->buf) { |
| 515 | usb_ep_free_request(ep, req); |
| 516 | req = NULL; |
| 517 | } |
| 518 | |
| 519 | return req; |
| 520 | } |
| 521 | |
| 522 | |
| 523 | static struct usb_request *sdp_start_ep(struct usb_ep *ep) |
| 524 | { |
| 525 | struct usb_request *req; |
| 526 | |
| 527 | req = alloc_ep_req(ep, 64); |
| 528 | debug("%s: ep:%p req:%p\n", __func__, ep, req); |
| 529 | |
| 530 | if (!req) |
| 531 | return NULL; |
| 532 | |
| 533 | memset(req->buf, 0, req->length); |
| 534 | req->complete = sdp_tx_complete; |
| 535 | |
| 536 | return req; |
| 537 | } |
| 538 | static int sdp_set_alt(struct usb_function *f, unsigned intf, unsigned alt) |
| 539 | { |
| 540 | struct f_sdp *sdp = func_to_sdp(f); |
| 541 | struct usb_composite_dev *cdev = f->config->cdev; |
| 542 | int result; |
| 543 | |
| 544 | debug("%s: intf: %d alt: %d\n", __func__, intf, alt); |
| 545 | |
| 546 | result = usb_ep_enable(sdp->in_ep, &in_desc); |
| 547 | if (result) |
| 548 | return result; |
| 549 | sdp->in_req = sdp_start_ep(sdp->in_ep); |
| 550 | sdp->in_req->context = sdp; |
| 551 | |
| 552 | sdp->in_ep->driver_data = cdev; /* claim */ |
| 553 | |
| 554 | sdp->altsetting = alt; |
| 555 | sdp->state = SDP_STATE_IDLE; |
| 556 | |
| 557 | return 0; |
| 558 | } |
| 559 | |
| 560 | static int sdp_get_alt(struct usb_function *f, unsigned intf) |
| 561 | { |
| 562 | struct f_sdp *sdp = func_to_sdp(f); |
| 563 | |
| 564 | return sdp->altsetting; |
| 565 | } |
| 566 | |
| 567 | static void sdp_disable(struct usb_function *f) |
| 568 | { |
| 569 | struct f_sdp *sdp = func_to_sdp(f); |
| 570 | |
| 571 | usb_ep_disable(sdp->in_ep); |
| 572 | |
| 573 | if (sdp->in_req) { |
| 574 | free(sdp->in_req); |
| 575 | sdp->in_req = NULL; |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | static int sdp_bind_config(struct usb_configuration *c) |
| 580 | { |
| 581 | int status; |
| 582 | |
| 583 | if (!sdp_func) { |
| 584 | sdp_func = memalign(CONFIG_SYS_CACHELINE_SIZE, sizeof(*sdp_func)); |
| 585 | if (!sdp_func) |
| 586 | return -ENOMEM; |
| 587 | } |
| 588 | |
| 589 | memset(sdp_func, 0, sizeof(*sdp_func)); |
| 590 | |
| 591 | sdp_func->usb_function.name = "sdp"; |
| 592 | sdp_func->usb_function.hs_descriptors = sdp_runtime_descs; |
| 593 | sdp_func->usb_function.descriptors = sdp_runtime_descs; |
| 594 | sdp_func->usb_function.bind = sdp_bind; |
| 595 | sdp_func->usb_function.unbind = sdp_unbind; |
| 596 | sdp_func->usb_function.set_alt = sdp_set_alt; |
| 597 | sdp_func->usb_function.get_alt = sdp_get_alt; |
| 598 | sdp_func->usb_function.disable = sdp_disable; |
| 599 | sdp_func->usb_function.strings = sdp_generic_strings; |
| 600 | sdp_func->usb_function.setup = sdp_setup; |
| 601 | |
| 602 | status = usb_add_function(c, &sdp_func->usb_function); |
| 603 | |
| 604 | return status; |
| 605 | } |
| 606 | |
| 607 | int sdp_init(int controller_index) |
| 608 | { |
| 609 | printf("SDP: initialize...\n"); |
| 610 | while (!sdp_func->configuration_done) { |
| 611 | if (ctrlc()) { |
| 612 | puts("\rCTRL+C - Operation aborted.\n"); |
| 613 | return 1; |
| 614 | } |
Vincent Prince | f26cf93 | 2017-10-23 11:16:35 +0200 | [diff] [blame] | 615 | |
| 616 | WATCHDOG_RESET(); |
Stefan Agner | ffbd5d0 | 2017-08-16 11:00:51 -0700 | [diff] [blame] | 617 | usb_gadget_handle_interrupts(controller_index); |
| 618 | } |
| 619 | |
| 620 | return 0; |
| 621 | } |
| 622 | |
| 623 | static u32 sdp_jump_imxheader(void *address) |
| 624 | { |
| 625 | flash_header_v2_t *headerv2 = address; |
| 626 | ulong (*entry)(void); |
| 627 | |
| 628 | if (headerv2->header.tag != IVT_HEADER_TAG) { |
| 629 | printf("Header Tag is not an IMX image\n"); |
| 630 | return SDP_ERROR_IMXHEADER; |
| 631 | } |
| 632 | |
| 633 | printf("Jumping to 0x%08x\n", headerv2->entry); |
Andre Heider | e1d461b | 2018-02-15 10:17:29 +0100 | [diff] [blame] | 634 | entry = sdp_ptr(headerv2->entry); |
Stefan Agner | ffbd5d0 | 2017-08-16 11:00:51 -0700 | [diff] [blame] | 635 | entry(); |
| 636 | |
| 637 | /* The image probably never returns hence we won't reach that point */ |
| 638 | return 0; |
| 639 | } |
| 640 | |
Frieder Schrempf | 09580e2 | 2019-06-04 21:56:29 +0200 | [diff] [blame^] | 641 | #ifdef CONFIG_SPL_BUILD |
| 642 | #ifdef CONFIG_SPL_LOAD_FIT |
| 643 | static ulong sdp_fit_read(struct spl_load_info *load, ulong sector, |
| 644 | ulong count, void *buf) |
| 645 | { |
| 646 | debug("%s: sector %lx, count %lx, buf %lx\n", |
| 647 | __func__, sector, count, (ulong)buf); |
| 648 | memcpy(buf, (void *)(load->dev + sector), count); |
| 649 | return count; |
| 650 | } |
| 651 | #endif |
| 652 | #endif |
| 653 | |
| 654 | static void sdp_handle_in_ep(struct spl_image_info *spl_image) |
Stefan Agner | ffbd5d0 | 2017-08-16 11:00:51 -0700 | [diff] [blame] | 655 | { |
| 656 | u8 *data = sdp_func->in_req->buf; |
| 657 | u32 status; |
| 658 | int datalen; |
| 659 | |
| 660 | switch (sdp_func->state) { |
| 661 | case SDP_STATE_TX_SEC_CONF: |
| 662 | debug("Report 3: HAB security\n"); |
| 663 | data[0] = 3; |
| 664 | |
| 665 | status = SDP_SECURITY_OPEN; |
| 666 | memcpy(&data[1], &status, 4); |
| 667 | sdp_func->in_req->length = 5; |
| 668 | usb_ep_queue(sdp_func->in_ep, sdp_func->in_req, 0); |
| 669 | sdp_func->state = SDP_STATE_TX_SEC_CONF_BUSY; |
| 670 | break; |
| 671 | |
| 672 | case SDP_STATE_TX_STATUS: |
| 673 | debug("Report 4: Status\n"); |
| 674 | data[0] = 4; |
| 675 | |
| 676 | memcpy(&data[1], &sdp_func->error_status, 4); |
| 677 | sdp_func->in_req->length = 65; |
| 678 | usb_ep_queue(sdp_func->in_ep, sdp_func->in_req, 0); |
| 679 | sdp_func->state = SDP_STATE_TX_STATUS_BUSY; |
| 680 | break; |
| 681 | case SDP_STATE_TX_REGISTER: |
| 682 | debug("Report 4: Register Values\n"); |
| 683 | data[0] = 4; |
| 684 | |
| 685 | datalen = sdp_func->dnl_bytes_remaining; |
| 686 | |
| 687 | if (datalen > 64) |
| 688 | datalen = 64; |
| 689 | |
Andre Heider | e1d461b | 2018-02-15 10:17:29 +0100 | [diff] [blame] | 690 | memcpy(&data[1], sdp_ptr(sdp_func->dnl_address), datalen); |
Stefan Agner | ffbd5d0 | 2017-08-16 11:00:51 -0700 | [diff] [blame] | 691 | sdp_func->in_req->length = 65; |
| 692 | |
| 693 | sdp_func->dnl_bytes_remaining -= datalen; |
| 694 | sdp_func->dnl_address += datalen; |
| 695 | |
| 696 | usb_ep_queue(sdp_func->in_ep, sdp_func->in_req, 0); |
| 697 | sdp_func->state = SDP_STATE_TX_REGISTER_BUSY; |
| 698 | break; |
| 699 | case SDP_STATE_JUMP: |
Stefan Agner | 3888da7 | 2017-08-16 11:00:52 -0700 | [diff] [blame] | 700 | printf("Jumping to header at 0x%08x\n", sdp_func->jmp_address); |
Andre Heider | e1d461b | 2018-02-15 10:17:29 +0100 | [diff] [blame] | 701 | status = sdp_jump_imxheader(sdp_ptr(sdp_func->jmp_address)); |
Stefan Agner | 3888da7 | 2017-08-16 11:00:52 -0700 | [diff] [blame] | 702 | |
| 703 | /* If imx header fails, try some U-Boot specific headers */ |
| 704 | if (status) { |
| 705 | #ifdef CONFIG_SPL_BUILD |
Frieder Schrempf | 09580e2 | 2019-06-04 21:56:29 +0200 | [diff] [blame^] | 706 | image_header_t *header = |
| 707 | sdp_ptr(sdp_func->jmp_address); |
| 708 | #ifdef CONFIG_SPL_LOAD_FIT |
| 709 | if (image_get_magic(header) == FDT_MAGIC) { |
| 710 | struct spl_load_info load; |
| 711 | |
| 712 | debug("Found FIT\n"); |
| 713 | load.dev = header; |
| 714 | load.bl_len = 1; |
| 715 | load.read = sdp_fit_read; |
| 716 | spl_load_simple_fit(spl_image, &load, 0, |
| 717 | header); |
| 718 | |
| 719 | return; |
| 720 | } |
| 721 | #endif |
Stefan Agner | 3888da7 | 2017-08-16 11:00:52 -0700 | [diff] [blame] | 722 | /* In SPL, allow jumps to U-Boot images */ |
| 723 | struct spl_image_info spl_image = {}; |
Frieder Schrempf | 09580e2 | 2019-06-04 21:56:29 +0200 | [diff] [blame^] | 724 | spl_parse_image_header(&spl_image, header); |
Stefan Agner | 3888da7 | 2017-08-16 11:00:52 -0700 | [diff] [blame] | 725 | jump_to_image_no_args(&spl_image); |
| 726 | #else |
| 727 | /* In U-Boot, allow jumps to scripts */ |
| 728 | source(sdp_func->jmp_address, "script@1"); |
| 729 | #endif |
| 730 | } |
Stefan Agner | ffbd5d0 | 2017-08-16 11:00:51 -0700 | [diff] [blame] | 731 | |
| 732 | sdp_func->next_state = SDP_STATE_IDLE; |
| 733 | sdp_func->error_status = status; |
| 734 | |
| 735 | /* Only send Report 4 if there was an error */ |
| 736 | if (status) |
| 737 | sdp_func->state = SDP_STATE_TX_STATUS; |
| 738 | else |
| 739 | sdp_func->state = SDP_STATE_IDLE; |
| 740 | break; |
| 741 | default: |
| 742 | break; |
| 743 | }; |
| 744 | } |
| 745 | |
Frieder Schrempf | 09580e2 | 2019-06-04 21:56:29 +0200 | [diff] [blame^] | 746 | #ifndef CONFIG_SPL_BUILD |
| 747 | int sdp_handle(int controller_index) |
| 748 | #else |
| 749 | int spl_sdp_handle(int controller_index, struct spl_image_info *spl_image) |
| 750 | #endif |
Stefan Agner | ffbd5d0 | 2017-08-16 11:00:51 -0700 | [diff] [blame] | 751 | { |
| 752 | printf("SDP: handle requests...\n"); |
| 753 | while (1) { |
| 754 | if (ctrlc()) { |
| 755 | puts("\rCTRL+C - Operation aborted.\n"); |
Frieder Schrempf | 09580e2 | 2019-06-04 21:56:29 +0200 | [diff] [blame^] | 756 | return -EINVAL; |
Stefan Agner | ffbd5d0 | 2017-08-16 11:00:51 -0700 | [diff] [blame] | 757 | } |
| 758 | |
Frieder Schrempf | 09580e2 | 2019-06-04 21:56:29 +0200 | [diff] [blame^] | 759 | #ifdef CONFIG_SPL_BUILD |
| 760 | if (spl_image->flags & SPL_FIT_FOUND) |
| 761 | return 0; |
| 762 | #endif |
| 763 | |
Vincent Prince | f26cf93 | 2017-10-23 11:16:35 +0200 | [diff] [blame] | 764 | WATCHDOG_RESET(); |
Stefan Agner | ffbd5d0 | 2017-08-16 11:00:51 -0700 | [diff] [blame] | 765 | usb_gadget_handle_interrupts(controller_index); |
| 766 | |
Frieder Schrempf | 09580e2 | 2019-06-04 21:56:29 +0200 | [diff] [blame^] | 767 | #ifdef CONFIG_SPL_BUILD |
| 768 | sdp_handle_in_ep(spl_image); |
| 769 | #else |
| 770 | sdp_handle_in_ep(NULL); |
| 771 | #endif |
Stefan Agner | ffbd5d0 | 2017-08-16 11:00:51 -0700 | [diff] [blame] | 772 | } |
| 773 | } |
| 774 | |
| 775 | int sdp_add(struct usb_configuration *c) |
| 776 | { |
| 777 | int id; |
| 778 | |
| 779 | id = usb_string_id(c->cdev); |
| 780 | if (id < 0) |
| 781 | return id; |
| 782 | strings_sdp_generic[0].id = id; |
| 783 | sdp_intf_runtime.iInterface = id; |
| 784 | |
| 785 | debug("%s: cdev: %p gadget: %p gadget->ep0: %p\n", __func__, |
| 786 | c->cdev, c->cdev->gadget, c->cdev->gadget->ep0); |
| 787 | |
| 788 | return sdp_bind_config(c); |
| 789 | } |
| 790 | |
| 791 | DECLARE_GADGET_BIND_CALLBACK(usb_dnl_sdp, sdp_add); |