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