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 #include <dm.h> 18 #include <misc.h> 19 #include <mmc.h> 20 #include <scsi.h> 21 #include <stdlib.h> 22 #include <usbplug.h> 23 #include <asm/arch/vendor.h> 24 #include <rockusb.h> 25 26 #define ROCKUSB_INTERFACE_CLASS 0xff 27 #define ROCKUSB_INTERFACE_SUB_CLASS 0x06 28 #define ROCKUSB_INTERFACE_PROTOCOL 0x05 29 30 #define ROCKCHIP_FLASH_BLOCK_SIZE 1024 31 #define ROCKCHIP_FLASH_PAGE_SIZE 4 32 33 static struct usb_interface_descriptor rkusb_intf_desc = { 34 .bLength = USB_DT_INTERFACE_SIZE, 35 .bDescriptorType = USB_DT_INTERFACE, 36 .bInterfaceNumber = 0x00, 37 .bAlternateSetting = 0x00, 38 .bNumEndpoints = 0x02, 39 .bInterfaceClass = ROCKUSB_INTERFACE_CLASS, 40 .bInterfaceSubClass = ROCKUSB_INTERFACE_SUB_CLASS, 41 .bInterfaceProtocol = ROCKUSB_INTERFACE_PROTOCOL, 42 }; 43 44 static struct usb_descriptor_header *rkusb_fs_function[] = { 45 (struct usb_descriptor_header *)&rkusb_intf_desc, 46 (struct usb_descriptor_header *)&fsg_fs_bulk_in_desc, 47 (struct usb_descriptor_header *)&fsg_fs_bulk_out_desc, 48 NULL, 49 }; 50 51 static struct usb_descriptor_header *rkusb_hs_function[] = { 52 (struct usb_descriptor_header *)&rkusb_intf_desc, 53 (struct usb_descriptor_header *)&fsg_hs_bulk_in_desc, 54 (struct usb_descriptor_header *)&fsg_hs_bulk_out_desc, 55 NULL, 56 }; 57 58 static struct usb_descriptor_header *rkusb_ss_function[] = { 59 (struct usb_descriptor_header *)&rkusb_intf_desc, 60 (struct usb_descriptor_header *)&fsg_ss_bulk_in_desc, 61 (struct usb_descriptor_header *)&fsg_ss_bulk_in_comp_desc, 62 (struct usb_descriptor_header *)&fsg_ss_bulk_out_desc, 63 (struct usb_descriptor_header *)&fsg_ss_bulk_out_comp_desc, 64 NULL, 65 }; 66 67 struct rk_flash_info { 68 u32 flash_size; 69 u16 block_size; 70 u8 page_size; 71 u8 ecc_bits; 72 u8 access_time; 73 u8 manufacturer; 74 u8 flash_mask; 75 } __packed; 76 77 static int rkusb_rst_code; /* The subcode in reset command (0xFF) */ 78 79 int g_dnl_bind_fixup(struct usb_device_descriptor *dev, const char *name) 80 { 81 if (IS_RKUSB_UMS_DNL(name)) { 82 /* Fix to Rockchip's VID and PID */ 83 dev->idVendor = __constant_cpu_to_le16(0x2207); 84 dev->idProduct = __constant_cpu_to_le16(CONFIG_ROCKUSB_G_DNL_PID); 85 86 /* Enumerate as a loader device */ 87 #if defined(CONFIG_SUPPORT_USBPLUG) 88 dev->bcdUSB = cpu_to_le16(0x0200); 89 #else 90 dev->bcdUSB = cpu_to_le16(0x0201); 91 #endif 92 } else if (!strncmp(name, "usb_dnl_fastboot", 16)) { 93 /* Fix to Google's VID and PID */ 94 dev->idVendor = __constant_cpu_to_le16(0x18d1); 95 dev->idProduct = __constant_cpu_to_le16(0x4d00); 96 } else if (!strncmp(name, "usb_dnl_dfu", 11)) { 97 /* Fix to Rockchip's VID and PID for DFU */ 98 dev->idVendor = cpu_to_le16(0x2207); 99 dev->idProduct = cpu_to_le16(0x0107); 100 } else if (!strncmp(name, "usb_dnl_ums", 11)) { 101 dev->idVendor = cpu_to_le16(0x2207); 102 dev->idProduct = cpu_to_le16(0x0010); 103 } 104 105 return 0; 106 } 107 108 __maybe_unused 109 static inline void dump_cbw(struct fsg_bulk_cb_wrap *cbw) 110 { 111 assert(!cbw); 112 113 debug("%s:\n", __func__); 114 debug("Signature %x\n", cbw->Signature); 115 debug("Tag %x\n", cbw->Tag); 116 debug("DataTransferLength %x\n", cbw->DataTransferLength); 117 debug("Flags %x\n", cbw->Flags); 118 debug("LUN %x\n", cbw->Lun); 119 debug("Length %x\n", cbw->Length); 120 debug("OptionCode %x\n", cbw->CDB[0]); 121 debug("SubCode %x\n", cbw->CDB[1]); 122 debug("SectorAddr %x\n", get_unaligned_be32(&cbw->CDB[2])); 123 debug("BlkSectors %x\n\n", get_unaligned_be16(&cbw->CDB[7])); 124 } 125 126 static int rkusb_check_lun(struct fsg_common *common) 127 { 128 struct fsg_lun *curlun; 129 130 /* Check the LUN */ 131 if (common->lun >= 0 && common->lun < common->nluns) { 132 curlun = &common->luns[common->lun]; 133 if (common->cmnd[0] != SC_REQUEST_SENSE) { 134 curlun->sense_data = SS_NO_SENSE; 135 curlun->info_valid = 0; 136 } 137 } else { 138 curlun = NULL; 139 common->bad_lun_okay = 0; 140 141 /* 142 * INQUIRY and REQUEST SENSE commands are explicitly allowed 143 * to use unsupported LUNs; all others may not. 144 */ 145 if (common->cmnd[0] != SC_INQUIRY && 146 common->cmnd[0] != SC_REQUEST_SENSE) { 147 debug("unsupported LUN %d\n", common->lun); 148 return -EINVAL; 149 } 150 } 151 152 return 0; 153 } 154 155 static void __do_reset(struct usb_ep *ep, struct usb_request *req) 156 { 157 u32 boot_flag = BOOT_NORMAL; 158 159 if (rkusb_rst_code == 0x03) 160 boot_flag = BOOT_BROM_DOWNLOAD; 161 162 rkusb_rst_code = 0; /* restore to default */ 163 writel(boot_flag, (void *)CONFIG_ROCKCHIP_BOOT_MODE_REG); 164 165 do_reset(NULL, 0, 0, NULL); 166 } 167 168 static int rkusb_do_reset(struct fsg_common *common, 169 struct fsg_buffhd *bh) 170 { 171 common->data_size_from_cmnd = common->cmnd[4]; 172 common->residue = 0; 173 bh->inreq->complete = __do_reset; 174 bh->state = BUF_STATE_EMPTY; 175 176 rkusb_rst_code = !common->cmnd[1] ? 0xff : common->cmnd[1]; 177 return 0; 178 } 179 180 __weak bool rkusb_usb3_capable(void) 181 { 182 return false; 183 } 184 185 static int rkusb_do_switch_to_usb3(struct fsg_common *common, 186 struct fsg_buffhd *bh) 187 { 188 g_dnl_set_serialnumber((char *)&common->cmnd[1]); 189 rkusb_switch_to_usb3_enable(true); 190 bh->state = BUF_STATE_EMPTY; 191 192 return 0; 193 } 194 195 static int rkusb_do_test_unit_ready(struct fsg_common *common, 196 struct fsg_buffhd *bh) 197 { 198 struct blk_desc *desc = &ums[common->lun].block_dev; 199 u32 usb_trb_size; 200 u16 residue; 201 202 if ((desc->if_type == IF_TYPE_MTD && desc->devnum == BLK_MTD_SPI_NOR) || 203 desc->if_type == IF_TYPE_SPINOR) 204 residue = 0x03; /* 128KB Max block xfer for SPI Nor */ 205 else if (common->cmnd[1] == 0xf7 && FSG_BUFLEN >= 0x400000) 206 residue = 0x0a; /* Max block xfer for USB DWC3 */ 207 else 208 residue = 0x06; /* Max block xfer support from host */ 209 210 usb_trb_size = (1 << residue) * 4096; 211 common->usb_trb_size = min(usb_trb_size, FSG_BUFLEN); 212 common->residue = residue << 24; 213 common->data_dir = DATA_DIR_NONE; 214 bh->state = BUF_STATE_EMPTY; 215 216 return 0; 217 } 218 219 static int rkusb_do_read_flash_id(struct fsg_common *common, 220 struct fsg_buffhd *bh) 221 { 222 u8 *buf = (u8 *)bh->buf; 223 u32 len = 5; 224 enum if_type type = ums[common->lun].block_dev.if_type; 225 u32 devnum = ums[common->lun].block_dev.devnum; 226 const char *str; 227 228 switch (type) { 229 case IF_TYPE_MMC: 230 str = "EMMC "; 231 break; 232 case IF_TYPE_RKNAND: 233 str = "NAND "; 234 break; 235 case IF_TYPE_MTD: 236 if (devnum == BLK_MTD_SPI_NAND) 237 str ="SNAND"; 238 else if (devnum == BLK_MTD_NAND) 239 str = "NAND "; 240 else 241 str = "NOR "; 242 break; 243 case IF_TYPE_SCSI: 244 str = "SATA "; 245 break; 246 case IF_TYPE_NVME: 247 str = "PCIE "; 248 break; 249 default: 250 str = "UNKN "; /* unknown */ 251 break; 252 } 253 254 memcpy((void *)&buf[0], str, len); 255 256 /* Set data xfer size */ 257 common->residue = common->data_size_from_cmnd = len; 258 common->data_size = len; 259 260 return len; 261 } 262 263 static int rkusb_do_test_bad_block(struct fsg_common *common, 264 struct fsg_buffhd *bh) 265 { 266 u8 *buf = (u8 *)bh->buf; 267 u32 len = 64; 268 269 memset((void *)&buf[0], 0, len); 270 271 /* Set data xfer size */ 272 common->residue = common->data_size_from_cmnd = len; 273 common->data_size = len; 274 275 return len; 276 } 277 278 static int rkusb_do_read_flash_info(struct fsg_common *common, 279 struct fsg_buffhd *bh) 280 { 281 struct blk_desc *desc = &ums[common->lun].block_dev; 282 u8 *buf = (u8 *)bh->buf; 283 u32 len = sizeof(struct rk_flash_info); 284 struct rk_flash_info finfo = { 285 .block_size = ROCKCHIP_FLASH_BLOCK_SIZE, 286 .ecc_bits = 0, 287 .page_size = ROCKCHIP_FLASH_PAGE_SIZE, 288 .access_time = 40, 289 .manufacturer = 0, 290 .flash_mask = 0 291 }; 292 293 /* Set the raw block size for tools to creat GPT with 4K block size */ 294 if (desc->rawblksz == 0x1000) 295 finfo.manufacturer = 208; 296 297 finfo.flash_size = (u32)desc->lba; 298 299 if (desc->if_type == IF_TYPE_MTD && 300 (desc->devnum == BLK_MTD_NAND || 301 desc->devnum == BLK_MTD_SPI_NAND)) { 302 struct mtd_info *mtd = (struct mtd_info *)desc->bdev->priv; 303 304 if (mtd) { 305 finfo.block_size = mtd->erasesize >> 9; 306 finfo.page_size = mtd->writesize >> 9; 307 #ifdef CONFIG_SUPPORT_USBPLUG 308 /* Using 4KB pagesize as 2KB for idblock */ 309 if (finfo.page_size == 8 && desc->devnum == BLK_MTD_SPI_NAND) 310 finfo.page_size |= (4 << 4); 311 #endif 312 } 313 } 314 315 if (desc->if_type == IF_TYPE_MTD && desc->devnum == BLK_MTD_SPI_NOR) { 316 /* RV1126 mtd spinor keep the former upgrade mode */ 317 #if !defined(CONFIG_ROCKCHIP_RV1126) 318 finfo.block_size = 0x80; /* Aligned to 64KB */ 319 #else 320 finfo.block_size = ROCKCHIP_FLASH_BLOCK_SIZE; 321 #endif 322 #if defined(CONFIG_ROCKCHIP_RK3308) 323 } else if (desc->if_type == IF_TYPE_SPINOR) { 324 finfo.block_size = 0x80; /* Aligned to 64KB */ 325 #endif 326 } 327 328 debug("Flash info: block_size= %x page_size= %x\n", finfo.block_size, 329 finfo.page_size); 330 331 if (finfo.flash_size) 332 finfo.flash_mask = 1; 333 334 memset((void *)&buf[0], 0, len); 335 memcpy((void *)&buf[0], (void *)&finfo, len); 336 337 /* Set data xfer size */ 338 common->residue = common->data_size_from_cmnd = len; 339 /* legacy upgrade_tool does not set correct transfer size */ 340 common->data_size = len; 341 342 return len; 343 } 344 345 static int rkusb_do_get_chip_info(struct fsg_common *common, 346 struct fsg_buffhd *bh) 347 { 348 u8 *buf = (u8 *)bh->buf; 349 u32 len = common->data_size; 350 u32 chip_info[4]; 351 352 memset((void *)chip_info, 0, sizeof(chip_info)); 353 rockchip_rockusb_get_chip_info(chip_info); 354 355 memset((void *)&buf[0], 0, len); 356 memcpy((void *)&buf[0], (void *)chip_info, len); 357 358 /* Set data xfer size */ 359 common->residue = common->data_size_from_cmnd = len; 360 361 return len; 362 } 363 364 static int rkusb_do_lba_erase(struct fsg_common *common, 365 struct fsg_buffhd *bh) 366 { 367 struct fsg_lun *curlun = &common->luns[common->lun]; 368 u32 lba, amount; 369 loff_t file_offset; 370 int rc; 371 372 lba = get_unaligned_be32(&common->cmnd[2]); 373 if (lba >= curlun->num_sectors) { 374 curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE; 375 rc = -EINVAL; 376 goto out; 377 } 378 379 file_offset = ((loff_t) lba) << 9; 380 amount = get_unaligned_be16(&common->cmnd[7]) << 9; 381 if (unlikely(amount == 0)) { 382 curlun->sense_data = SS_INVALID_FIELD_IN_CDB; 383 rc = -EIO; 384 goto out; 385 } 386 387 /* Perform the erase */ 388 rc = ums[common->lun].erase_sector(&ums[common->lun], 389 file_offset / SECTOR_SIZE, 390 amount / SECTOR_SIZE); 391 if (!rc) { 392 curlun->sense_data = SS_MEDIUM_NOT_PRESENT; 393 rc = -EIO; 394 } 395 396 out: 397 common->data_dir = DATA_DIR_NONE; 398 bh->state = BUF_STATE_EMPTY; 399 400 return rc; 401 } 402 403 static int rkusb_do_erase_force(struct fsg_common *common, 404 struct fsg_buffhd *bh) 405 { 406 struct blk_desc *desc = &ums[common->lun].block_dev; 407 struct fsg_lun *curlun = &common->luns[common->lun]; 408 u16 block_size = ROCKCHIP_FLASH_BLOCK_SIZE; 409 u32 lba, amount; 410 loff_t file_offset; 411 int rc; 412 413 lba = get_unaligned_be32(&common->cmnd[2]); 414 if (lba >= curlun->num_sectors) { 415 curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE; 416 rc = -EINVAL; 417 goto out; 418 } 419 420 if (desc->if_type == IF_TYPE_MTD && 421 (desc->devnum == BLK_MTD_NAND || 422 desc->devnum == BLK_MTD_SPI_NAND)) { 423 struct mtd_info *mtd = (struct mtd_info *)desc->bdev->priv; 424 425 if (mtd) 426 block_size = mtd->erasesize >> 9; 427 } 428 429 file_offset = ((loff_t)lba) * block_size; 430 amount = get_unaligned_be16(&common->cmnd[7]) * block_size; 431 432 debug("%s lba= %x, nsec= %x\n", __func__, lba, 433 (u32)get_unaligned_be16(&common->cmnd[7])); 434 435 if (unlikely(amount == 0)) { 436 curlun->sense_data = SS_INVALID_FIELD_IN_CDB; 437 rc = -EIO; 438 goto out; 439 } 440 441 /* Perform the erase */ 442 rc = ums[common->lun].erase_sector(&ums[common->lun], 443 file_offset, 444 amount); 445 if (!rc) { 446 curlun->sense_data = SS_MEDIUM_NOT_PRESENT; 447 rc = -EIO; 448 } 449 450 out: 451 common->data_dir = DATA_DIR_NONE; 452 bh->state = BUF_STATE_EMPTY; 453 454 return rc; 455 } 456 457 static int rkusb_do_vs_write(struct fsg_common *common) 458 { 459 struct fsg_lun *curlun = &common->luns[common->lun]; 460 u16 type = get_unaligned_be16(&common->cmnd[4]); 461 struct vendor_item *vhead; 462 struct fsg_buffhd *bh; 463 void *data; 464 int rc; 465 466 if (common->data_size >= (u32)65536) { 467 /* _MUST_ small than 64K */ 468 curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE; 469 return -EINVAL; 470 } 471 472 common->residue = common->data_size; 473 common->usb_amount_left = common->data_size; 474 475 /* Carry out the file writes */ 476 if (unlikely(common->data_size == 0)) 477 return -EIO; /* No data to write */ 478 479 for (;;) { 480 if (common->usb_amount_left > 0) { 481 /* Wait for the next buffer to become available */ 482 bh = common->next_buffhd_to_fill; 483 if (bh->state != BUF_STATE_EMPTY) 484 goto wait; 485 486 /* Request the next buffer */ 487 common->usb_amount_left -= common->data_size; 488 bh->outreq->length = common->data_size; 489 bh->bulk_out_intended_length = common->data_size; 490 bh->outreq->short_not_ok = 1; 491 492 START_TRANSFER_OR(common, bulk_out, bh->outreq, 493 &bh->outreq_busy, &bh->state) 494 /* 495 * Don't know what to do if 496 * common->fsg is NULL 497 */ 498 return -EIO; 499 common->next_buffhd_to_fill = bh->next; 500 } else { 501 /* Then, wait for the data to become available */ 502 bh = common->next_buffhd_to_drain; 503 if (bh->state != BUF_STATE_FULL) 504 goto wait; 505 506 common->next_buffhd_to_drain = bh->next; 507 bh->state = BUF_STATE_EMPTY; 508 509 /* Did something go wrong with the transfer? */ 510 if (bh->outreq->status != 0) { 511 curlun->sense_data = SS_COMMUNICATION_FAILURE; 512 curlun->info_valid = 1; 513 break; 514 } 515 516 /* Perform the write */ 517 vhead = (struct vendor_item *)bh->buf; 518 data = bh->buf + sizeof(struct vendor_item); 519 if (CONFIG_IS_ENABLED(ROCKCHIP_VENDOR_PARTITION) && !type) { 520 #ifndef CONFIG_SUPPORT_USBPLUG 521 if (vhead->id == HDCP_14_HDMI_ID || 522 vhead->id == HDCP_14_HDMIRX_ID || 523 vhead->id == HDCP_14_DP_ID) { 524 rc = vendor_handle_hdcp(vhead); 525 if (rc < 0) { 526 curlun->sense_data = SS_WRITE_ERROR; 527 return -EIO; 528 } 529 } 530 #endif 531 532 /* Vendor storage */ 533 rc = vendor_storage_write(vhead->id, 534 (char __user *)data, 535 vhead->size); 536 if (rc < 0) { 537 curlun->sense_data = SS_WRITE_ERROR; 538 return -EIO; 539 } 540 } else if (type == 1) { 541 /* RPMB */ 542 rc = 543 write_keybox_to_secure_storage((u8 *)data, 544 vhead->size); 545 if (rc < 0) { 546 curlun->sense_data = SS_WRITE_ERROR; 547 return -EIO; 548 } 549 } else if (type == 2) { 550 /* security storage */ 551 #ifdef CONFIG_RK_AVB_LIBAVB_USER 552 debug("%s call rk_avb_write_perm_attr %d, %d\n", 553 __func__, vhead->id, vhead->size); 554 rc = rk_avb_write_perm_attr(vhead->id, 555 (char __user *)data, 556 vhead->size); 557 if (rc < 0) { 558 curlun->sense_data = SS_WRITE_ERROR; 559 return -EIO; 560 } 561 #else 562 printf("Please enable CONFIG_RK_AVB_LIBAVB_USER\n"); 563 #endif 564 } else if (type == 3) { 565 /* efuse or otp*/ 566 #ifdef CONFIG_OPTEE_CLIENT 567 if (memcmp(data, "TAEK", 4) == 0) { 568 if (vhead->size - 8 != 32) { 569 printf("check ta encryption key size fail!\n"); 570 curlun->sense_data = SS_WRITE_ERROR; 571 return -EIO; 572 } 573 if (trusty_write_ta_encryption_key((uint32_t *)(data + 8), 8) != 0) { 574 printf("trusty_write_ta_encryption_key error!"); 575 curlun->sense_data = SS_WRITE_ERROR; 576 return -EIO; 577 } 578 } else if (memcmp(data, "EHUK", 4) == 0) { 579 if (vhead->size - 8 != 32) { 580 printf("check oem huk size fail!\n"); 581 curlun->sense_data = SS_WRITE_ERROR; 582 return -EIO; 583 } 584 if (trusty_write_oem_huk((uint32_t *)(data + 8), 8) != 0) { 585 printf("trusty_write_oem_huk error!"); 586 curlun->sense_data = SS_WRITE_ERROR; 587 return -EIO; 588 } 589 } else if (memcmp(data, "ENDA", 4) == 0) { 590 if (vhead->size - 8 != 16) { 591 printf("check oem encrypt data size fail!\n"); 592 curlun->sense_data = SS_WRITE_ERROR; 593 return -EIO; 594 } 595 if (trusty_write_oem_encrypt_data((uint32_t *)(data + 8), 4) != 0) { 596 printf("trusty_write_oem_encrypt_data error!"); 597 curlun->sense_data = SS_WRITE_ERROR; 598 return -EIO; 599 } 600 } else if (memcmp(data, "OTPK", 4) == 0) { 601 uint32_t key_len = vhead->size - 9; 602 uint8_t key_id = *((uint8_t *)data + 8); 603 if (key_len != 16 && key_len != 24 && key_len != 32) { 604 printf("check oem otp key size fail!\n"); 605 curlun->sense_data = SS_WRITE_ERROR; 606 return -EIO; 607 } 608 if (trusty_write_oem_otp_key(key_id, (uint8_t *)(data + 9), key_len) != 0) { 609 printf("trusty_write_oem_huk error!"); 610 curlun->sense_data = SS_WRITE_ERROR; 611 return -EIO; 612 } 613 } else { 614 printf("Unknown tag\n"); 615 curlun->sense_data = SS_WRITE_ERROR; 616 return -EIO; 617 } 618 #else 619 printf("Please enable CONFIG_OPTEE_CLIENT\n"); 620 #endif 621 } else { 622 return -EINVAL; 623 } 624 625 common->residue -= common->data_size; 626 627 /* Did the host decide to stop early? */ 628 if (bh->outreq->actual != bh->outreq->length) 629 common->short_packet_received = 1; 630 break; /* Command done */ 631 } 632 wait: 633 /* Wait for something to happen */ 634 rc = sleep_thread(common); 635 if (rc) 636 return rc; 637 } 638 639 return -EIO; /* No default reply */ 640 } 641 642 static int rkusb_do_vs_read(struct fsg_common *common) 643 { 644 struct fsg_lun *curlun = &common->luns[common->lun]; 645 u16 type = get_unaligned_be16(&common->cmnd[4]); 646 struct vendor_item *vhead; 647 struct fsg_buffhd *bh; 648 void *data; 649 int rc; 650 651 if (common->data_size >= (u32)65536) { 652 /* _MUST_ small than 64K */ 653 curlun->sense_data = SS_LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE; 654 return -EINVAL; 655 } 656 657 common->residue = common->data_size; 658 common->usb_amount_left = common->data_size; 659 660 /* Carry out the file reads */ 661 if (unlikely(common->data_size == 0)) 662 return -EIO; /* No default reply */ 663 664 for (;;) { 665 /* Wait for the next buffer to become available */ 666 bh = common->next_buffhd_to_fill; 667 while (bh->state != BUF_STATE_EMPTY) { 668 rc = sleep_thread(common); 669 if (rc) 670 return rc; 671 } 672 673 memset(bh->buf, 0, FSG_BUFLEN); 674 vhead = (struct vendor_item *)bh->buf; 675 data = bh->buf + sizeof(struct vendor_item); 676 vhead->id = get_unaligned_be16(&common->cmnd[2]); 677 if (CONFIG_IS_ENABLED(ROCKCHIP_VENDOR_PARTITION) && !type) { 678 /* Vendor storage */ 679 rc = vendor_storage_read(vhead->id, 680 (char __user *)data, 681 common->data_size); 682 if (!rc) { 683 curlun->sense_data = SS_UNRECOVERED_READ_ERROR; 684 return -EIO; 685 } 686 vhead->size = rc; 687 } else if (type == 1) { 688 /* RPMB */ 689 rc = 690 read_raw_data_from_secure_storage((u8 *)data, 691 common->data_size); 692 if (!rc) { 693 curlun->sense_data = SS_UNRECOVERED_READ_ERROR; 694 return -EIO; 695 } 696 vhead->size = rc; 697 } else if (type == 2) { 698 /* security storage */ 699 #ifdef CONFIG_RK_AVB_LIBAVB_USER 700 rc = rk_avb_read_perm_attr(vhead->id, 701 (char __user *)data, 702 vhead->size); 703 if (rc < 0) 704 return -EIO; 705 vhead->size = rc; 706 #else 707 printf("Please enable CONFIG_RK_AVB_LIBAVB_USER!\n"); 708 #endif 709 } else if (type == 3) { 710 /* efuse or otp*/ 711 #ifdef CONFIG_OPTEE_CLIENT 712 if (vhead->id == 120) { 713 u8 value; 714 char *written_str = "key is written!"; 715 char *not_written_str = "key is not written!"; 716 if (trusty_ta_encryption_key_is_written(&value) != 0) { 717 printf("trusty_ta_encryption_key_is_written error!"); 718 return -EIO; 719 } 720 if (value) { 721 memcpy(data, written_str, strlen(written_str)); 722 vhead->size = strlen(written_str); 723 } else { 724 memcpy(data, not_written_str, strlen(not_written_str)); 725 vhead->size = strlen(not_written_str); 726 } 727 } else { 728 printf("Unknown tag\n"); 729 return -EIO; 730 } 731 #else 732 printf("Please enable CONFIG_OPTEE_CLIENT\n"); 733 #endif 734 } else { 735 return -EINVAL; 736 } 737 738 common->residue -= common->data_size; 739 bh->inreq->length = common->data_size; 740 bh->state = BUF_STATE_FULL; 741 742 break; /* No more left to read */ 743 } 744 745 return -EIO; /* No default reply */ 746 } 747 748 #if CONFIG_PSTORE 749 static int rkusb_do_uart_debug_read(struct fsg_common *common) 750 { 751 struct fsg_buffhd *bh; 752 int *debug_head; 753 int debug_data_size; 754 int rc; 755 756 if (common->data_size >= (u32)FSG_BUFLEN) 757 return -EINVAL; 758 759 common->residue = common->data_size; 760 common->usb_amount_left = common->data_size; 761 762 /* Carry out the file reads */ 763 if (unlikely(common->data_size == 0) || unlikely(!gd->pstore_addr)) 764 return -EIO; /* No default reply */ 765 766 /* Wait for the next buffer to become available */ 767 bh = common->next_buffhd_to_fill; 768 while (bh->state != BUF_STATE_EMPTY) { 769 rc = sleep_thread(common); 770 if (rc) 771 return rc; 772 } 773 774 debug_head = (int *)gd->pstore_addr; 775 debug_data_size = debug_head[2]; 776 if (debug_data_size > FSG_BUFLEN - 8) 777 debug_data_size = FSG_BUFLEN - 8; 778 if (debug_data_size > common->data_size - 8) 779 debug_data_size = common->data_size - 8; 780 781 debug_head = (int *)bh->buf; 782 debug_head[0] = 0x55424544; 783 debug_head[1] = debug_data_size + 8; 784 785 memcpy((void *)(bh->buf + 8), (void *)(gd->pstore_addr + 12), debug_data_size); 786 787 common->residue -= common->data_size; 788 bh->inreq->length = common->data_size; 789 bh->state = BUF_STATE_FULL; 790 791 return -EIO; /* No default reply */ 792 } 793 #endif 794 795 static int rkusb_do_switch_storage(struct fsg_common *common) 796 { 797 enum if_type type, cur_type = ums[common->lun].block_dev.if_type; 798 int devnum, cur_devnum = ums[common->lun].block_dev.devnum; 799 struct blk_desc *block_dev; 800 u32 media = BOOT_TYPE_UNKNOWN; 801 802 media = 1 << common->cmnd[1]; 803 804 switch (media) { 805 #ifdef CONFIG_MMC 806 case BOOT_TYPE_EMMC: 807 type = IF_TYPE_MMC; 808 devnum = 0; 809 mmc_initialize(gd->bd); 810 break; 811 #endif 812 case BOOT_TYPE_MTD_BLK_NAND: 813 type = IF_TYPE_MTD; 814 devnum = 0; 815 break; 816 case BOOT_TYPE_MTD_BLK_SPI_NAND: 817 type = IF_TYPE_MTD; 818 devnum = 1; 819 break; 820 case BOOT_TYPE_MTD_BLK_SPI_NOR: 821 type = IF_TYPE_MTD; 822 devnum = 2; 823 break; 824 #if defined(CONFIG_SCSI) && defined(CONFIG_CMD_SCSI) && (defined(CONFIG_AHCI) || defined(CONFIG_UFS)) 825 case BOOT_TYPE_SATA: 826 type = IF_TYPE_SCSI; 827 devnum = 0; 828 scsi_scan(true); 829 break; 830 #endif 831 default: 832 printf("Bootdev 0x%x is not support\n", media); 833 return -ENODEV; 834 } 835 836 if (cur_type == type && cur_devnum == devnum) 837 return 0; 838 839 #if CONFIG_IS_ENABLED(SUPPORT_USBPLUG) 840 block_dev = usbplug_blk_get_devnum_by_type(type, devnum); 841 #else 842 block_dev = blk_get_devnum_by_type(type, devnum); 843 #endif 844 if (!block_dev) { 845 printf("Bootdev if_type=%d num=%d toggle fail\n", type, devnum); 846 return -ENODEV; 847 } 848 849 common->luns[common->lun].num_sectors = block_dev->lba; 850 ums[common->lun].num_sectors = block_dev->lba; 851 ums[common->lun].block_dev = *block_dev; 852 853 printf("RKUSB: LUN %d, dev %d, hwpart %d, sector %#x, count %#x\n", 854 0, 855 ums[common->lun].block_dev.devnum, 856 ums[common->lun].block_dev.hwpart, 857 ums[common->lun].start_sector, 858 ums[common->lun].num_sectors); 859 860 return 0; 861 } 862 863 static int rkusb_do_get_storage_info(struct fsg_common *common, 864 struct fsg_buffhd *bh) 865 { 866 enum if_type type = ums[common->lun].block_dev.if_type; 867 int devnum = ums[common->lun].block_dev.devnum; 868 u32 media = BOOT_TYPE_UNKNOWN; 869 u32 len = common->data_size; 870 u8 *buf = (u8 *)bh->buf; 871 872 if (len > 4) 873 len = 4; 874 875 switch (type) { 876 case IF_TYPE_MMC: 877 media = BOOT_TYPE_EMMC; 878 break; 879 880 case IF_TYPE_SD: 881 media = BOOT_TYPE_SD0; 882 break; 883 884 case IF_TYPE_MTD: 885 if (devnum == BLK_MTD_SPI_NAND) 886 media = BOOT_TYPE_MTD_BLK_SPI_NAND; 887 else if (devnum == BLK_MTD_NAND) 888 media = BOOT_TYPE_NAND; 889 else 890 media = BOOT_TYPE_MTD_BLK_SPI_NOR; 891 break; 892 893 case IF_TYPE_SCSI: 894 media = BOOT_TYPE_SATA; 895 break; 896 897 case IF_TYPE_RKNAND: 898 media = BOOT_TYPE_NAND; 899 break; 900 901 case IF_TYPE_NVME: 902 media = BOOT_TYPE_PCIE; 903 break; 904 905 default: 906 break; 907 } 908 909 memcpy((void *)&buf[0], (void *)&media, len); 910 common->residue = len; 911 common->data_size_from_cmnd = len; 912 913 return len; 914 } 915 916 static int rkusb_do_read_capacity(struct fsg_common *common, 917 struct fsg_buffhd *bh) 918 { 919 u8 *buf = (u8 *)bh->buf; 920 u32 len = common->data_size; 921 enum if_type type = ums[common->lun].block_dev.if_type; 922 int devnum = ums[common->lun].block_dev.devnum; 923 924 /* 925 * bit[0]: Direct LBA, 0: Disabled; 926 * bit[1]: Vendor Storage API, 0: Disabed (default); 927 * bit[2]: First 4M Access, 0: Disabled; 928 * bit[3]: Read LBA On, 0: Disabed (default); 929 * bit[4]: New Vendor Storage API, 0: Disabed; 930 * bit[5]: Read uart data from ram 931 * bit[6]: Read IDB config 932 * bit[7]: Read SecureMode 933 * bit[8]: New IDB feature 934 * bit[9]: Get storage media info 935 * bit[10]: LBAwrite Parity 936 * bit[11]: Read Otp Data 937 * bit[12]: usb3 download 938 * bit[13]: Write OTP proof 939 * bit[14]: Write Cipher Key 940 * bit[15:63}: Reserved. 941 */ 942 memset((void *)&buf[0], 0, len); 943 if (type == IF_TYPE_MMC || type == IF_TYPE_SD || type == IF_TYPE_NVME) 944 buf[0] = BIT(0) | BIT(2) | BIT(4); 945 else 946 buf[0] = BIT(0) | BIT(4); 947 948 if (type == IF_TYPE_MTD && 949 (devnum == BLK_MTD_NAND || 950 devnum == BLK_MTD_SPI_NAND)) 951 buf[0] |= (1 << 6); 952 953 #ifdef CONFIG_PSTORE 954 buf[0] |= (1 << 5); 955 #endif 956 957 #if !defined(CONFIG_ROCKCHIP_RV1126) 958 if (type == IF_TYPE_MTD && devnum == BLK_MTD_SPI_NOR) 959 buf[0] |= (1 << 6); 960 #if defined(CONFIG_ROCKCHIP_RK3308) 961 else if (type == IF_TYPE_SPINOR) 962 buf[0] |= (1 << 6); 963 #endif 964 #endif 965 966 #if defined(CONFIG_ROCKCHIP_NEW_IDB) 967 buf[1] = BIT(0); 968 #endif 969 buf[1] |= BIT(1); /* Switch Storage */ 970 buf[1] |= BIT(2); /* LBAwrite Parity */ 971 972 if (rkusb_usb3_capable() && !rkusb_force_usb2_enabled()) 973 buf[1] |= BIT(4); 974 else 975 buf[1] &= ~BIT(4); 976 977 #ifdef CONFIG_ROCKCHIP_OTP 978 buf[1] |= BIT(3); /* Read Otp Data */ 979 buf[1] |= BIT(5); /* Write OTP proof */ 980 buf[1] |= BIT(6); /* Write Cipher Key */ 981 #endif 982 983 /* Set data xfer size */ 984 common->residue = len; 985 common->data_size_from_cmnd = len; 986 987 return len; 988 } 989 990 #ifdef CONFIG_ROCKCHIP_OTP 991 static int rkusb_do_read_otp(struct fsg_common *common, 992 struct fsg_buffhd *bh) 993 { 994 u32 len = common->data_size; 995 u32 type = common->cmnd[1]; 996 u8 *buf = (u8 *)bh->buf; 997 struct udevice *dev; 998 999 buf[0] = 0; 1000 if (type == 0) { /* soc uuid */ 1001 if (!uclass_get_device_by_driver(UCLASS_MISC, DM_GET_DRIVER(rockchip_otp), &dev)) { 1002 if (!misc_read(dev, CFG_CPUID_OFFSET, (void *)&buf[1], len)) 1003 buf[0] = len; 1004 } 1005 } 1006 1007 common->residue = len; 1008 common->data_size_from_cmnd = len; 1009 1010 return len; 1011 } 1012 #endif 1013 1014 static void rkusb_fixup_cbwcb(struct fsg_common *common, 1015 struct fsg_buffhd *bh) 1016 { 1017 struct usb_request *req = bh->outreq; 1018 struct fsg_bulk_cb_wrap *cbw = req->buf; 1019 1020 /* FIXME cbw.DataTransferLength was not set by Upgrade Tool */ 1021 common->data_size = le32_to_cpu(cbw->DataTransferLength); 1022 if (common->data_size == 0) { 1023 common->data_size = 1024 get_unaligned_be16(&common->cmnd[7]) << 9; 1025 printf("Trasfer Length NOT set, please use new version tool\n"); 1026 debug("%s %d, cmnd1 %x\n", __func__, 1027 get_unaligned_be16(&common->cmnd[7]), 1028 get_unaligned_be16(&common->cmnd[1])); 1029 } 1030 if (cbw->Flags & USB_BULK_IN_FLAG) 1031 common->data_dir = DATA_DIR_TO_HOST; 1032 else 1033 common->data_dir = DATA_DIR_FROM_HOST; 1034 1035 /* Not support */ 1036 common->cmnd[1] = 0; 1037 } 1038 1039 static int rkusb_cmd_process(struct fsg_common *common, 1040 struct fsg_buffhd *bh, int *reply) 1041 { 1042 struct usb_request *req = bh->outreq; 1043 struct fsg_bulk_cb_wrap *cbw = req->buf; 1044 int rc; 1045 1046 dump_cbw(cbw); 1047 1048 if (rkusb_check_lun(common)) { 1049 *reply = -EINVAL; 1050 return RKUSB_RC_ERROR; 1051 } 1052 1053 switch (common->cmnd[0]) { 1054 case RKUSB_TEST_UNIT_READY: 1055 *reply = rkusb_do_test_unit_ready(common, bh); 1056 rc = RKUSB_RC_FINISHED; 1057 break; 1058 1059 case RKUSB_READ_FLASH_ID: 1060 *reply = rkusb_do_read_flash_id(common, bh); 1061 rc = RKUSB_RC_FINISHED; 1062 break; 1063 1064 case RKUSB_TEST_BAD_BLOCK: 1065 *reply = rkusb_do_test_bad_block(common, bh); 1066 rc = RKUSB_RC_FINISHED; 1067 break; 1068 1069 case RKUSB_ERASE_10_FORCE: 1070 *reply = rkusb_do_erase_force(common, bh); 1071 rc = RKUSB_RC_FINISHED; 1072 break; 1073 1074 case RKUSB_LBA_READ_10: 1075 rkusb_fixup_cbwcb(common, bh); 1076 common->cmnd[0] = SC_READ_10; 1077 common->cmnd[1] = 0; /* Not support */ 1078 rc = RKUSB_RC_CONTINUE; 1079 break; 1080 1081 case RKUSB_LBA_WRITE_10: 1082 rkusb_fixup_cbwcb(common, bh); 1083 common->cmnd[0] = SC_WRITE_10; 1084 common->cmnd[1] = 0; /* Not support */ 1085 rc = RKUSB_RC_CONTINUE; 1086 break; 1087 1088 case RKUSB_READ_FLASH_INFO: 1089 *reply = rkusb_do_read_flash_info(common, bh); 1090 rc = RKUSB_RC_FINISHED; 1091 break; 1092 1093 case RKUSB_GET_CHIP_VER: 1094 *reply = rkusb_do_get_chip_info(common, bh); 1095 rc = RKUSB_RC_FINISHED; 1096 break; 1097 1098 case RKUSB_LBA_ERASE: 1099 *reply = rkusb_do_lba_erase(common, bh); 1100 rc = RKUSB_RC_FINISHED; 1101 break; 1102 1103 case RKUSB_VS_WRITE: 1104 *reply = rkusb_do_vs_write(common); 1105 rc = RKUSB_RC_FINISHED; 1106 break; 1107 1108 case RKUSB_VS_READ: 1109 *reply = rkusb_do_vs_read(common); 1110 rc = RKUSB_RC_FINISHED; 1111 break; 1112 1113 #ifdef CONFIG_PSTORE 1114 case RKUSB_UART_READ: 1115 rkusb_fixup_cbwcb(common, bh); 1116 *reply = rkusb_do_uart_debug_read(common); 1117 rc = RKUSB_RC_FINISHED; 1118 break; 1119 #endif 1120 1121 case RKUSB_SWITCH_STORAGE: 1122 *reply = rkusb_do_switch_storage(common); 1123 rc = RKUSB_RC_FINISHED; 1124 break; 1125 1126 case RKUSB_GET_STORAGE_MEDIA: 1127 *reply = rkusb_do_get_storage_info(common, bh); 1128 rc = RKUSB_RC_FINISHED; 1129 break; 1130 1131 case RKUSB_READ_CAPACITY: 1132 *reply = rkusb_do_read_capacity(common, bh); 1133 rc = RKUSB_RC_FINISHED; 1134 break; 1135 1136 case RKUSB_SWITCH_USB3: 1137 *reply = rkusb_do_switch_to_usb3(common, bh); 1138 rc = RKUSB_RC_FINISHED; 1139 break; 1140 1141 case RKUSB_RESET: 1142 *reply = rkusb_do_reset(common, bh); 1143 rc = RKUSB_RC_FINISHED; 1144 break; 1145 1146 #ifdef CONFIG_ROCKCHIP_OTP 1147 case RKUSB_READ_OTP_DATA: 1148 *reply = rkusb_do_read_otp(common, bh); 1149 rc = RKUSB_RC_FINISHED; 1150 break; 1151 #endif 1152 1153 case RKUSB_READ_10: 1154 case RKUSB_WRITE_10: 1155 printf("CMD Not support, pls use new version Tool\n"); 1156 case RKUSB_SET_DEVICE_ID: 1157 case RKUSB_ERASE_10: 1158 case RKUSB_WRITE_SPARE: 1159 case RKUSB_READ_SPARE: 1160 case RKUSB_GET_VERSION: 1161 case RKUSB_ERASE_SYS_DISK: 1162 case RKUSB_SDRAM_READ_10: 1163 case RKUSB_SDRAM_WRITE_10: 1164 case RKUSB_SDRAM_EXECUTE: 1165 case RKUSB_LOW_FORMAT: 1166 case RKUSB_SET_RESET_FLAG: 1167 case RKUSB_SPI_READ_10: 1168 case RKUSB_SPI_WRITE_10: 1169 /* Fall through */ 1170 default: 1171 rc = RKUSB_RC_UNKNOWN_CMND; 1172 break; 1173 } 1174 1175 return rc; 1176 } 1177 1178 int rkusb_do_check_parity(struct fsg_common *common) 1179 { 1180 int ret = 0, rc; 1181 u32 parity, i, usb_parity, lba, len; 1182 static u32 usb_check_buffer[1024 * 256]; 1183 1184 usb_parity = common->cmnd[9] | (common->cmnd[10] << 8) | 1185 (common->cmnd[11] << 16) | (common->cmnd[12] << 24); 1186 1187 if (common->cmnd[0] == SC_WRITE_10 && (usb_parity)) { 1188 lba = get_unaligned_be32(&common->cmnd[2]); 1189 len = common->data_size_from_cmnd >> 9; 1190 rc = blk_dread(&ums[common->lun].block_dev, lba, len, usb_check_buffer); 1191 parity = 0x000055aa; 1192 for (i = 0; i < len * 128; i++) 1193 parity += usb_check_buffer[i]; 1194 if (!rc || parity != usb_parity) 1195 common->phase_error = 1; 1196 } 1197 1198 return ret; 1199 } 1200 1201 DECLARE_GADGET_BIND_CALLBACK(rkusb_ums_dnl, fsg_add); 1202