1 /* 2 * Copyright 2017 Rockchip Electronics Co., Ltd 3 * Frank Wang <frank.wang@rock-chips.com> 4 * 5 * SPDX-License-Identifier: GPL-2.0+ 6 */ 7 8 #include <asm/io.h> 9 #include <android_avb/avb_ops_user.h> 10 #include <android_avb/rk_avb_ops_user.h> 11 #include <asm/arch/boot_mode.h> 12 #include <asm/arch/chip_info.h> 13 #include <asm/arch/rk_atags.h> 14 #include <write_keybox.h> 15 #include <linux/mtd/mtd.h> 16 #include <optee_include/OpteeClientInterface.h> 17 18 #ifdef CONFIG_ROCKCHIP_VENDOR_PARTITION 19 #include <asm/arch/vendor.h> 20 #endif 21 #include <rockusb.h> 22 23 #define ROCKUSB_INTERFACE_CLASS 0xff 24 #define ROCKUSB_INTERFACE_SUB_CLASS 0x06 25 #define ROCKUSB_INTERFACE_PROTOCOL 0x05 26 27 #define ROCKCHIP_FLASH_BLOCK_SIZE 1024 28 #define ROCKCHIP_FLASH_PAGE_SIZE 4 29 30 static struct usb_interface_descriptor rkusb_intf_desc = { 31 .bLength = USB_DT_INTERFACE_SIZE, 32 .bDescriptorType = USB_DT_INTERFACE, 33 .bInterfaceNumber = 0x00, 34 .bAlternateSetting = 0x00, 35 .bNumEndpoints = 0x02, 36 .bInterfaceClass = ROCKUSB_INTERFACE_CLASS, 37 .bInterfaceSubClass = ROCKUSB_INTERFACE_SUB_CLASS, 38 .bInterfaceProtocol = ROCKUSB_INTERFACE_PROTOCOL, 39 }; 40 41 static struct usb_descriptor_header *rkusb_fs_function[] = { 42 (struct usb_descriptor_header *)&rkusb_intf_desc, 43 (struct usb_descriptor_header *)&fsg_fs_bulk_in_desc, 44 (struct usb_descriptor_header *)&fsg_fs_bulk_out_desc, 45 NULL, 46 }; 47 48 static struct usb_descriptor_header *rkusb_hs_function[] = { 49 (struct usb_descriptor_header *)&rkusb_intf_desc, 50 (struct usb_descriptor_header *)&fsg_hs_bulk_in_desc, 51 (struct usb_descriptor_header *)&fsg_hs_bulk_out_desc, 52 NULL, 53 }; 54 55 static struct usb_descriptor_header *rkusb_ss_function[] = { 56 (struct usb_descriptor_header *)&rkusb_intf_desc, 57 (struct usb_descriptor_header *)&fsg_ss_bulk_in_desc, 58 (struct usb_descriptor_header *)&fsg_ss_bulk_in_comp_desc, 59 (struct usb_descriptor_header *)&fsg_ss_bulk_out_desc, 60 (struct usb_descriptor_header *)&fsg_ss_bulk_out_comp_desc, 61 NULL, 62 }; 63 64 struct rk_flash_info { 65 u32 flash_size; 66 u16 block_size; 67 u8 page_size; 68 u8 ecc_bits; 69 u8 access_time; 70 u8 manufacturer; 71 u8 flash_mask; 72 } __packed; 73 74 static int rkusb_rst_code; /* The subcode in reset command (0xFF) */ 75 76 int g_dnl_bind_fixup(struct usb_device_descriptor *dev, const char *name) 77 { 78 if (IS_RKUSB_UMS_DNL(name)) { 79 /* Fix to Rockchip's VID and PID */ 80 dev->idVendor = __constant_cpu_to_le16(0x2207); 81 dev->idProduct = __constant_cpu_to_le16(CONFIG_ROCKUSB_G_DNL_PID); 82 83 /* Enumerate as a loader device */ 84 #if defined(CONFIG_SUPPORT_USBPLUG) 85 dev->bcdUSB = cpu_to_le16(0x0200); 86 #else 87 dev->bcdUSB = cpu_to_le16(0x0201); 88 #endif 89 } else if (!strncmp(name, "usb_dnl_fastboot", 16)) { 90 /* Fix to Google's VID and PID */ 91 dev->idVendor = __constant_cpu_to_le16(0x18d1); 92 dev->idProduct = __constant_cpu_to_le16(0xd00d); 93 } else if (!strncmp(name, "usb_dnl_dfu", 11)) { 94 /* Fix to Rockchip's VID and PID for DFU */ 95 dev->idVendor = cpu_to_le16(0x2207); 96 dev->idProduct = cpu_to_le16(0x0107); 97 } else if (!strncmp(name, "usb_dnl_ums", 11)) { 98 dev->idVendor = cpu_to_le16(0x2207); 99 dev->idProduct = cpu_to_le16(0x0010); 100 } 101 102 return 0; 103 } 104 105 __maybe_unused 106 static inline void dump_cbw(struct fsg_bulk_cb_wrap *cbw) 107 { 108 assert(!cbw); 109 110 debug("%s:\n", __func__); 111 debug("Signature %x\n", cbw->Signature); 112 debug("Tag %x\n", cbw->Tag); 113 debug("DataTransferLength %x\n", cbw->DataTransferLength); 114 debug("Flags %x\n", cbw->Flags); 115 debug("LUN %x\n", cbw->Lun); 116 debug("Length %x\n", cbw->Length); 117 debug("OptionCode %x\n", cbw->CDB[0]); 118 debug("SubCode %x\n", cbw->CDB[1]); 119 debug("SectorAddr %x\n", get_unaligned_be32(&cbw->CDB[2])); 120 debug("BlkSectors %x\n\n", get_unaligned_be16(&cbw->CDB[7])); 121 } 122 123 static int rkusb_check_lun(struct fsg_common *common) 124 { 125 struct fsg_lun *curlun; 126 127 /* Check the LUN */ 128 if (common->lun >= 0 && common->lun < common->nluns) { 129 curlun = &common->luns[common->lun]; 130 if (common->cmnd[0] != SC_REQUEST_SENSE) { 131 curlun->sense_data = SS_NO_SENSE; 132 curlun->info_valid = 0; 133 } 134 } else { 135 curlun = NULL; 136 common->bad_lun_okay = 0; 137 138 /* 139 * INQUIRY and REQUEST SENSE commands are explicitly allowed 140 * to use unsupported LUNs; all others may not. 141 */ 142 if (common->cmnd[0] != SC_INQUIRY && 143 common->cmnd[0] != SC_REQUEST_SENSE) { 144 debug("unsupported LUN %d\n", common->lun); 145 return -EINVAL; 146 } 147 } 148 149 return 0; 150 } 151 152 static void __do_reset(struct usb_ep *ep, struct usb_request *req) 153 { 154 u32 boot_flag = BOOT_NORMAL; 155 156 if (rkusb_rst_code == 0x03) 157 boot_flag = BOOT_BROM_DOWNLOAD; 158 159 rkusb_rst_code = 0; /* restore to default */ 160 writel(boot_flag, (void *)CONFIG_ROCKCHIP_BOOT_MODE_REG); 161 162 do_reset(NULL, 0, 0, NULL); 163 } 164 165 static int rkusb_do_reset(struct fsg_common *common, 166 struct fsg_buffhd *bh) 167 { 168 common->data_size_from_cmnd = common->cmnd[4]; 169 common->residue = 0; 170 bh->inreq->complete = __do_reset; 171 bh->state = BUF_STATE_EMPTY; 172 173 rkusb_rst_code = !common->cmnd[1] ? 0xff : common->cmnd[1]; 174 return 0; 175 } 176 177 static int rkusb_do_test_unit_ready(struct fsg_common *common, 178 struct fsg_buffhd *bh) 179 { 180 common->residue = 0x06 << 24; /* Max block xfer support from host */ 181 common->data_dir = DATA_DIR_NONE; 182 bh->state = BUF_STATE_EMPTY; 183 184 return 0; 185 } 186 187 static int rkusb_do_read_flash_id(struct fsg_common *common, 188 struct fsg_buffhd *bh) 189 { 190 u8 *buf = (u8 *)bh->buf; 191 u32 len = 5; 192 enum if_type type = ums[common->lun].block_dev.if_type; 193 194 if (type == IF_TYPE_MMC) 195 memcpy((void *)&buf[0], "EMMC ", 5); 196 else if (type == IF_TYPE_RKNAND) 197 memcpy((void *)&buf[0], "NAND ", 5); 198 else 199 memcpy((void *)&buf[0], "UNKN ", 5); /* unknown */ 200 201 /* Set data xfer size */ 202 common->residue = common->data_size_from_cmnd = len; 203 common->data_size = len; 204 205 return len; 206 } 207 208 static int rkusb_do_test_bad_block(struct fsg_common *common, 209 struct fsg_buffhd *bh) 210 { 211 u8 *buf = (u8 *)bh->buf; 212 u32 len = 64; 213 214 memset((void *)&buf[0], 0, len); 215 216 /* Set data xfer size */ 217 common->residue = common->data_size_from_cmnd = len; 218 common->data_size = len; 219 220 return len; 221 } 222 223 static int rkusb_do_read_flash_info(struct fsg_common *common, 224 struct fsg_buffhd *bh) 225 { 226 struct blk_desc *desc = &ums[common->lun].block_dev; 227 u8 *buf = (u8 *)bh->buf; 228 u32 len = sizeof(struct rk_flash_info); 229 struct rk_flash_info finfo = { 230 .block_size = ROCKCHIP_FLASH_BLOCK_SIZE, 231 .ecc_bits = 0, 232 .page_size = ROCKCHIP_FLASH_PAGE_SIZE, 233 .access_time = 40, 234 .manufacturer = 0, 235 .flash_mask = 0 236 }; 237 238 finfo.flash_size = (u32)desc->lba; 239 240 if (desc->if_type == IF_TYPE_MTD && 241 (desc->devnum == BLK_MTD_NAND || 242 desc->devnum == BLK_MTD_SPI_NAND)) { 243 struct mtd_info *mtd = (struct mtd_info *)desc->bdev->priv; 244 245 if (mtd) { 246 finfo.block_size = mtd->erasesize >> 9; 247 finfo.page_size = mtd->writesize >> 9; 248 } 249 } 250 251 if (desc->if_type == IF_TYPE_MTD && desc->devnum == BLK_MTD_SPI_NOR) { 252 /* RV1126/RK3308 mtd spinor keep the former upgrade mode */ 253 #if !defined(CONFIG_ROCKCHIP_RV1126) && !defined(CONFIG_ROCKCHIP_RK3308) 254 finfo.block_size = 0x80; /* Aligned to 64KB */ 255 #else 256 finfo.block_size = ROCKCHIP_FLASH_BLOCK_SIZE; 257 #endif 258 } 259 260 debug("Flash info: block_size= %x page_size= %x\n", finfo.block_size, 261 finfo.page_size); 262 263 if (finfo.flash_size) 264 finfo.flash_mask = 1; 265 266 memset((void *)&buf[0], 0, len); 267 memcpy((void *)&buf[0], (void *)&finfo, len); 268 269 /* Set data xfer size */ 270 common->residue = common->data_size_from_cmnd = len; 271 /* legacy upgrade_tool does not set correct transfer size */ 272 common->data_size = len; 273 274 return len; 275 } 276 277 static int rkusb_do_get_chip_info(struct fsg_common *common, 278 struct fsg_buffhd *bh) 279 { 280 u8 *buf = (u8 *)bh->buf; 281 u32 len = common->data_size; 282 u32 chip_info[4]; 283 284 memset((void *)chip_info, 0, sizeof(chip_info)); 285 rockchip_rockusb_get_chip_info(chip_info); 286 287 memset((void *)&buf[0], 0, len); 288 memcpy((void *)&buf[0], (void *)chip_info, len); 289 290 /* Set data xfer size */ 291 common->residue = common->data_size_from_cmnd = len; 292 293 return len; 294 } 295 296 static int rkusb_do_lba_erase(struct fsg_common *common, 297 struct fsg_buffhd *bh) 298 { 299 struct fsg_lun *curlun = &common->luns[common->lun]; 300 u32 lba, amount; 301 loff_t file_offset; 302 int rc; 303 304 lba = get_unaligned_be32(&common->cmnd[2]); 305 if (lba >= curlun->num_sectors) { 306 curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE; 307 rc = -EINVAL; 308 goto out; 309 } 310 311 file_offset = ((loff_t) lba) << 9; 312 amount = get_unaligned_be16(&common->cmnd[7]) << 9; 313 if (unlikely(amount == 0)) { 314 curlun->sense_data = SS_INVALID_FIELD_IN_CDB; 315 rc = -EIO; 316 goto out; 317 } 318 319 /* Perform the erase */ 320 rc = ums[common->lun].erase_sector(&ums[common->lun], 321 file_offset / SECTOR_SIZE, 322 amount / SECTOR_SIZE); 323 if (!rc) { 324 curlun->sense_data = SS_MEDIUM_NOT_PRESENT; 325 rc = -EIO; 326 } 327 328 out: 329 common->data_dir = DATA_DIR_NONE; 330 bh->state = BUF_STATE_EMPTY; 331 332 return rc; 333 } 334 335 static int rkusb_do_erase_force(struct fsg_common *common, 336 struct fsg_buffhd *bh) 337 { 338 struct blk_desc *desc = &ums[common->lun].block_dev; 339 struct fsg_lun *curlun = &common->luns[common->lun]; 340 u16 block_size = ROCKCHIP_FLASH_BLOCK_SIZE; 341 u32 lba, amount; 342 loff_t file_offset; 343 int rc; 344 345 lba = get_unaligned_be32(&common->cmnd[2]); 346 if (lba >= curlun->num_sectors) { 347 curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE; 348 rc = -EINVAL; 349 goto out; 350 } 351 352 if (desc->if_type == IF_TYPE_MTD && 353 (desc->devnum == BLK_MTD_NAND || 354 desc->devnum == BLK_MTD_SPI_NAND)) { 355 struct mtd_info *mtd = (struct mtd_info *)desc->bdev->priv; 356 357 if (mtd) 358 block_size = mtd->erasesize >> 9; 359 } 360 361 file_offset = ((loff_t)lba) * block_size; 362 amount = get_unaligned_be16(&common->cmnd[7]) * block_size; 363 364 debug("%s lba= %x, nsec= %x\n", __func__, lba, 365 (u32)get_unaligned_be16(&common->cmnd[7])); 366 367 if (unlikely(amount == 0)) { 368 curlun->sense_data = SS_INVALID_FIELD_IN_CDB; 369 rc = -EIO; 370 goto out; 371 } 372 373 /* Perform the erase */ 374 rc = ums[common->lun].erase_sector(&ums[common->lun], 375 file_offset, 376 amount); 377 if (!rc) { 378 curlun->sense_data = SS_MEDIUM_NOT_PRESENT; 379 rc = -EIO; 380 } 381 382 out: 383 common->data_dir = DATA_DIR_NONE; 384 bh->state = BUF_STATE_EMPTY; 385 386 return rc; 387 } 388 389 #ifdef CONFIG_ROCKCHIP_VENDOR_PARTITION 390 static int rkusb_do_vs_write(struct fsg_common *common) 391 { 392 struct fsg_lun *curlun = &common->luns[common->lun]; 393 u16 type = get_unaligned_be16(&common->cmnd[4]); 394 struct vendor_item *vhead; 395 struct fsg_buffhd *bh; 396 void *data; 397 int rc; 398 399 if (common->data_size >= (u32)65536) { 400 /* _MUST_ small than 64K */ 401 curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE; 402 return -EINVAL; 403 } 404 405 common->residue = common->data_size; 406 common->usb_amount_left = common->data_size; 407 408 /* Carry out the file writes */ 409 if (unlikely(common->data_size == 0)) 410 return -EIO; /* No data to write */ 411 412 for (;;) { 413 if (common->usb_amount_left > 0) { 414 /* Wait for the next buffer to become available */ 415 bh = common->next_buffhd_to_fill; 416 if (bh->state != BUF_STATE_EMPTY) 417 goto wait; 418 419 /* Request the next buffer */ 420 common->usb_amount_left -= common->data_size; 421 bh->outreq->length = common->data_size; 422 bh->bulk_out_intended_length = common->data_size; 423 bh->outreq->short_not_ok = 1; 424 425 START_TRANSFER_OR(common, bulk_out, bh->outreq, 426 &bh->outreq_busy, &bh->state) 427 /* 428 * Don't know what to do if 429 * common->fsg is NULL 430 */ 431 return -EIO; 432 common->next_buffhd_to_fill = bh->next; 433 } else { 434 /* Then, wait for the data to become available */ 435 bh = common->next_buffhd_to_drain; 436 if (bh->state != BUF_STATE_FULL) 437 goto wait; 438 439 common->next_buffhd_to_drain = bh->next; 440 bh->state = BUF_STATE_EMPTY; 441 442 /* Did something go wrong with the transfer? */ 443 if (bh->outreq->status != 0) { 444 curlun->sense_data = SS_COMMUNICATION_FAILURE; 445 curlun->info_valid = 1; 446 break; 447 } 448 449 /* Perform the write */ 450 vhead = (struct vendor_item *)bh->buf; 451 data = bh->buf + sizeof(struct vendor_item); 452 453 if (!type) { 454 if (vhead->id == HDCP_14_HDMI_ID || 455 vhead->id == HDCP_14_HDMIRX_ID || 456 vhead->id == HDCP_14_DP_ID) { 457 rc = vendor_handle_hdcp(vhead); 458 if (rc < 0) { 459 curlun->sense_data = SS_WRITE_ERROR; 460 return -EIO; 461 } 462 } 463 464 /* Vendor storage */ 465 rc = vendor_storage_write(vhead->id, 466 (char __user *)data, 467 vhead->size); 468 if (rc < 0) { 469 curlun->sense_data = SS_WRITE_ERROR; 470 return -EIO; 471 } 472 } else if (type == 1) { 473 /* RPMB */ 474 rc = 475 write_keybox_to_secure_storage((u8 *)data, 476 vhead->size); 477 if (rc < 0) { 478 curlun->sense_data = SS_WRITE_ERROR; 479 return -EIO; 480 } 481 } else if (type == 2) { 482 /* security storage */ 483 #ifdef CONFIG_RK_AVB_LIBAVB_USER 484 debug("%s call rk_avb_write_perm_attr %d, %d\n", 485 __func__, vhead->id, vhead->size); 486 rc = rk_avb_write_perm_attr(vhead->id, 487 (char __user *)data, 488 vhead->size); 489 if (rc < 0) { 490 curlun->sense_data = SS_WRITE_ERROR; 491 return -EIO; 492 } 493 #else 494 printf("Please enable CONFIG_RK_AVB_LIBAVB_USER\n"); 495 #endif 496 } else if (type == 3) { 497 /* efuse or otp*/ 498 #ifdef CONFIG_OPTEE_CLIENT 499 if (memcmp(data, "TAEK", 4) == 0) { 500 if (vhead->size - 8 != 32) { 501 printf("check ta encryption key size fail!\n"); 502 curlun->sense_data = SS_WRITE_ERROR; 503 return -EIO; 504 } 505 if (trusty_write_ta_encryption_key((uint32_t *)(data + 8), 8) != 0) { 506 printf("trusty_write_ta_encryption_key error!"); 507 curlun->sense_data = SS_WRITE_ERROR; 508 return -EIO; 509 } 510 } else if (memcmp(data, "EHUK", 4) == 0) { 511 if (vhead->size - 8 != 32) { 512 printf("check oem huk size fail!\n"); 513 curlun->sense_data = SS_WRITE_ERROR; 514 return -EIO; 515 } 516 if (trusty_write_oem_huk((uint32_t *)(data + 8), 8) != 0) { 517 printf("trusty_write_oem_huk error!"); 518 curlun->sense_data = SS_WRITE_ERROR; 519 return -EIO; 520 } 521 } else { 522 printf("Unknown tag\n"); 523 curlun->sense_data = SS_WRITE_ERROR; 524 return -EIO; 525 } 526 #else 527 printf("Please enable CONFIG_OPTEE_CLIENT\n"); 528 #endif 529 } else { 530 return -EINVAL; 531 } 532 533 common->residue -= common->data_size; 534 535 /* Did the host decide to stop early? */ 536 if (bh->outreq->actual != bh->outreq->length) 537 common->short_packet_received = 1; 538 break; /* Command done */ 539 } 540 wait: 541 /* Wait for something to happen */ 542 rc = sleep_thread(common); 543 if (rc) 544 return rc; 545 } 546 547 return -EIO; /* No default reply */ 548 } 549 550 static int rkusb_do_vs_read(struct fsg_common *common) 551 { 552 struct fsg_lun *curlun = &common->luns[common->lun]; 553 u16 type = get_unaligned_be16(&common->cmnd[4]); 554 struct vendor_item *vhead; 555 struct fsg_buffhd *bh; 556 void *data; 557 int rc; 558 559 if (common->data_size >= (u32)65536) { 560 /* _MUST_ small than 64K */ 561 curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE; 562 return -EINVAL; 563 } 564 565 common->residue = common->data_size; 566 common->usb_amount_left = common->data_size; 567 568 /* Carry out the file reads */ 569 if (unlikely(common->data_size == 0)) 570 return -EIO; /* No default reply */ 571 572 for (;;) { 573 /* Wait for the next buffer to become available */ 574 bh = common->next_buffhd_to_fill; 575 while (bh->state != BUF_STATE_EMPTY) { 576 rc = sleep_thread(common); 577 if (rc) 578 return rc; 579 } 580 581 memset(bh->buf, 0, FSG_BUFLEN); 582 vhead = (struct vendor_item *)bh->buf; 583 data = bh->buf + sizeof(struct vendor_item); 584 vhead->id = get_unaligned_be16(&common->cmnd[2]); 585 586 if (!type) { 587 /* Vendor storage */ 588 rc = vendor_storage_read(vhead->id, 589 (char __user *)data, 590 common->data_size); 591 if (!rc) { 592 curlun->sense_data = SS_UNRECOVERED_READ_ERROR; 593 return -EIO; 594 } 595 vhead->size = rc; 596 } else if (type == 1) { 597 /* RPMB */ 598 rc = 599 read_raw_data_from_secure_storage((u8 *)data, 600 common->data_size); 601 if (!rc) { 602 curlun->sense_data = SS_UNRECOVERED_READ_ERROR; 603 return -EIO; 604 } 605 vhead->size = rc; 606 } else if (type == 2) { 607 /* security storage */ 608 #ifdef CONFIG_RK_AVB_LIBAVB_USER 609 rc = rk_avb_read_perm_attr(vhead->id, 610 (char __user *)data, 611 vhead->size); 612 if (rc < 0) 613 return -EIO; 614 vhead->size = rc; 615 #else 616 printf("Please enable CONFIG_RK_AVB_LIBAVB_USER!\n"); 617 #endif 618 } else { 619 return -EINVAL; 620 } 621 622 common->residue -= common->data_size; 623 bh->inreq->length = common->data_size; 624 bh->state = BUF_STATE_FULL; 625 626 break; /* No more left to read */ 627 } 628 629 return -EIO; /* No default reply */ 630 } 631 #endif 632 633 static int rkusb_do_get_storage_info(struct fsg_common *common, 634 struct fsg_buffhd *bh) 635 { 636 enum if_type type = ums[common->lun].block_dev.if_type; 637 int devnum = ums[common->lun].block_dev.devnum; 638 u32 media = BOOT_TYPE_UNKNOWN; 639 u32 len = common->data_size; 640 u8 *buf = (u8 *)bh->buf; 641 642 if (len > 4) 643 len = 4; 644 645 switch (type) { 646 case IF_TYPE_MMC: 647 media = BOOT_TYPE_EMMC; 648 break; 649 650 case IF_TYPE_SD: 651 media = BOOT_TYPE_SD0; 652 break; 653 654 case IF_TYPE_MTD: 655 if (devnum == BLK_MTD_SPI_NAND) 656 media = BOOT_TYPE_MTD_BLK_SPI_NAND; 657 else if (devnum == BLK_MTD_NAND) 658 media = BOOT_TYPE_NAND; 659 else 660 media = BOOT_TYPE_MTD_BLK_SPI_NOR; 661 break; 662 663 case IF_TYPE_SCSI: 664 media = BOOT_TYPE_SATA; 665 break; 666 667 case IF_TYPE_RKNAND: 668 media = BOOT_TYPE_NAND; 669 break; 670 671 case IF_TYPE_NVME: 672 media = BOOT_TYPE_PCIE; 673 break; 674 675 default: 676 break; 677 } 678 679 memcpy((void *)&buf[0], (void *)&media, len); 680 common->residue = len; 681 common->data_size_from_cmnd = len; 682 683 return len; 684 } 685 686 static int rkusb_do_read_capacity(struct fsg_common *common, 687 struct fsg_buffhd *bh) 688 { 689 u8 *buf = (u8 *)bh->buf; 690 u32 len = common->data_size; 691 enum if_type type = ums[common->lun].block_dev.if_type; 692 int devnum = ums[common->lun].block_dev.devnum; 693 694 /* 695 * bit[0]: Direct LBA, 0: Disabled; 696 * bit[1]: Vendor Storage API, 0: Disabed (default); 697 * bit[2]: First 4M Access, 0: Disabled; 698 * bit[3]: Read LBA On, 0: Disabed (default); 699 * bit[4]: New Vendor Storage API, 0: Disabed; 700 * bit[5]: Read uart data from ram 701 * bit[6]: Read IDB config 702 * bit[7]: Read SecureMode 703 * bit[8]: New IDB feature 704 * bit[9]: Get storage media info 705 * bit[10:63}: Reserved. 706 */ 707 memset((void *)&buf[0], 0, len); 708 if (type == IF_TYPE_MMC || type == IF_TYPE_SD || type == IF_TYPE_NVME) 709 buf[0] = BIT(0) | BIT(2) | BIT(4); 710 else 711 buf[0] = BIT(0) | BIT(4); 712 713 if (type == IF_TYPE_MTD && 714 (devnum == BLK_MTD_NAND || 715 devnum == BLK_MTD_SPI_NAND)) 716 buf[0] |= (1 << 6); 717 718 #if !defined(CONFIG_ROCKCHIP_RV1126) && !defined(CONFIG_ROCKCHIP_RK3308) 719 if (type == IF_TYPE_MTD && devnum == BLK_MTD_SPI_NOR) 720 buf[0] |= (1 << 6); 721 #endif 722 723 #if defined(CONFIG_ROCKCHIP_NEW_IDB) 724 buf[1] = BIT(0); 725 #endif 726 buf[1] |= BIT(1); /* Switch Storage */ 727 buf[1] |= BIT(2); /* LBAwrite Parity */ 728 729 /* Set data xfer size */ 730 common->residue = len; 731 common->data_size_from_cmnd = len; 732 733 return len; 734 } 735 736 static void rkusb_fixup_cbwcb(struct fsg_common *common, 737 struct fsg_buffhd *bh) 738 { 739 struct usb_request *req = bh->outreq; 740 struct fsg_bulk_cb_wrap *cbw = req->buf; 741 742 /* FIXME cbw.DataTransferLength was not set by Upgrade Tool */ 743 common->data_size = le32_to_cpu(cbw->DataTransferLength); 744 if (common->data_size == 0) { 745 common->data_size = 746 get_unaligned_be16(&common->cmnd[7]) << 9; 747 printf("Trasfer Length NOT set, please use new version tool\n"); 748 debug("%s %d, cmnd1 %x\n", __func__, 749 get_unaligned_be16(&common->cmnd[7]), 750 get_unaligned_be16(&common->cmnd[1])); 751 } 752 if (cbw->Flags & USB_BULK_IN_FLAG) 753 common->data_dir = DATA_DIR_TO_HOST; 754 else 755 common->data_dir = DATA_DIR_FROM_HOST; 756 757 /* Not support */ 758 common->cmnd[1] = 0; 759 } 760 761 static int rkusb_cmd_process(struct fsg_common *common, 762 struct fsg_buffhd *bh, int *reply) 763 { 764 struct usb_request *req = bh->outreq; 765 struct fsg_bulk_cb_wrap *cbw = req->buf; 766 int rc; 767 768 dump_cbw(cbw); 769 770 if (rkusb_check_lun(common)) { 771 *reply = -EINVAL; 772 return RKUSB_RC_ERROR; 773 } 774 775 switch (common->cmnd[0]) { 776 case RKUSB_TEST_UNIT_READY: 777 *reply = rkusb_do_test_unit_ready(common, bh); 778 rc = RKUSB_RC_FINISHED; 779 break; 780 781 case RKUSB_READ_FLASH_ID: 782 *reply = rkusb_do_read_flash_id(common, bh); 783 rc = RKUSB_RC_FINISHED; 784 break; 785 786 case RKUSB_TEST_BAD_BLOCK: 787 *reply = rkusb_do_test_bad_block(common, bh); 788 rc = RKUSB_RC_FINISHED; 789 break; 790 791 case RKUSB_ERASE_10_FORCE: 792 *reply = rkusb_do_erase_force(common, bh); 793 rc = RKUSB_RC_FINISHED; 794 break; 795 796 case RKUSB_LBA_READ_10: 797 rkusb_fixup_cbwcb(common, bh); 798 common->cmnd[0] = SC_READ_10; 799 common->cmnd[1] = 0; /* Not support */ 800 rc = RKUSB_RC_CONTINUE; 801 break; 802 803 case RKUSB_LBA_WRITE_10: 804 rkusb_fixup_cbwcb(common, bh); 805 common->cmnd[0] = SC_WRITE_10; 806 common->cmnd[1] = 0; /* Not support */ 807 rc = RKUSB_RC_CONTINUE; 808 break; 809 810 case RKUSB_READ_FLASH_INFO: 811 *reply = rkusb_do_read_flash_info(common, bh); 812 rc = RKUSB_RC_FINISHED; 813 break; 814 815 case RKUSB_GET_CHIP_VER: 816 *reply = rkusb_do_get_chip_info(common, bh); 817 rc = RKUSB_RC_FINISHED; 818 break; 819 820 case RKUSB_LBA_ERASE: 821 *reply = rkusb_do_lba_erase(common, bh); 822 rc = RKUSB_RC_FINISHED; 823 break; 824 825 #ifdef CONFIG_ROCKCHIP_VENDOR_PARTITION 826 case RKUSB_VS_WRITE: 827 *reply = rkusb_do_vs_write(common); 828 rc = RKUSB_RC_FINISHED; 829 break; 830 831 case RKUSB_VS_READ: 832 *reply = rkusb_do_vs_read(common); 833 rc = RKUSB_RC_FINISHED; 834 break; 835 #endif 836 case RKUSB_GET_STORAGE_MEDIA: 837 *reply = rkusb_do_get_storage_info(common, bh); 838 rc = RKUSB_RC_FINISHED; 839 break; 840 841 case RKUSB_READ_CAPACITY: 842 *reply = rkusb_do_read_capacity(common, bh); 843 rc = RKUSB_RC_FINISHED; 844 break; 845 846 case RKUSB_RESET: 847 *reply = rkusb_do_reset(common, bh); 848 rc = RKUSB_RC_FINISHED; 849 break; 850 851 case RKUSB_READ_10: 852 case RKUSB_WRITE_10: 853 printf("CMD Not support, pls use new version Tool\n"); 854 case RKUSB_SET_DEVICE_ID: 855 case RKUSB_ERASE_10: 856 case RKUSB_WRITE_SPARE: 857 case RKUSB_READ_SPARE: 858 case RKUSB_GET_VERSION: 859 case RKUSB_ERASE_SYS_DISK: 860 case RKUSB_SDRAM_READ_10: 861 case RKUSB_SDRAM_WRITE_10: 862 case RKUSB_SDRAM_EXECUTE: 863 case RKUSB_LOW_FORMAT: 864 case RKUSB_SET_RESET_FLAG: 865 case RKUSB_SPI_READ_10: 866 case RKUSB_SPI_WRITE_10: 867 case RKUSB_SESSION: 868 /* Fall through */ 869 default: 870 rc = RKUSB_RC_UNKNOWN_CMND; 871 break; 872 } 873 874 return rc; 875 } 876 877 int rkusb_do_check_parity(struct fsg_common *common) 878 { 879 int ret = 0, rc; 880 u32 parity, i, usb_parity, lba, len; 881 static u32 usb_check_buffer[1024 * 256]; 882 883 usb_parity = common->cmnd[9] | (common->cmnd[10] << 8) | 884 (common->cmnd[11] << 16) | (common->cmnd[12] << 24); 885 886 if (common->cmnd[0] == SC_WRITE_10 && (usb_parity)) { 887 lba = get_unaligned_be32(&common->cmnd[2]); 888 len = common->data_size_from_cmnd >> 9; 889 rc = blk_dread(&ums[common->lun].block_dev, lba, len, usb_check_buffer); 890 parity = 0x000055aa; 891 for (i = 0; i < len * 128; i++) 892 parity += usb_check_buffer[i]; 893 if (!rc || parity != usb_parity) 894 common->phase_error = 1; 895 } 896 897 return ret; 898 } 899 900 DECLARE_GADGET_BIND_CALLBACK(rkusb_ums_dnl, fsg_add); 901