1 /*
2 * Copyright (c) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "scsicommonfunction_fuzzer.h"
17 #include <cinttypes>
18 #include "UsbSubscriberTest.h"
19 #include "hdf_log.h"
20 #define HDF_LOG_TAG scsi_ddk_fuzzer
21
22 using namespace OHOS::HDI::Usb::ScsiDdk::V1_0;
23
24 namespace OHOS {
25 namespace SCSI {
26
27 constexpr uint32_t SHIFT_32 = 32;
28 uint64_t g_interfaceIndex = 0x00;
29 constexpr uint32_t DEVICE_MEM_MAP_SIZE = 1024;
30
ToDdkDeviceId(const uint8_t busNum,const uint8_t devAddr)31 static uint64_t ToDdkDeviceId(const uint8_t busNum, const uint8_t devAddr)
32 {
33 return (static_cast<uint64_t>(busNum) << SHIFT_32) + devAddr;
34 }
35
ScsiFuzzTestHostModeInit(const sptr<IScsiPeripheralDdk> & scsiPeripheralDdk,ScsiPeripheralDevice & device)36 int32_t ScsiFuzzTestHostModeInit(const sptr<IScsiPeripheralDdk> &scsiPeripheralDdk, ScsiPeripheralDevice &device)
37 {
38 sptr<OHOS::HDI::Usb::V1_0::IUsbInterface> usbInterface = OHOS::HDI::Usb::V1_0::IUsbInterface::Get();
39 if (usbInterface == nullptr) {
40 return HDF_FAILURE;
41 }
42 sleep(SLEEP_TIME);
43
44 sptr<OHOS::USB::UsbSubscriberTest> subscriber = new OHOS::USB::UsbSubscriberTest();
45 int32_t ret = usbInterface->BindUsbdSubscriber(subscriber);
46 if (ret != HDF_SUCCESS) {
47 HDF_LOGE("%{public}s: bind usbd subscriber failed", __func__);
48 return ret;
49 }
50
51 uint8_t busNum = subscriber->busNum_;
52 uint8_t devAddr = subscriber->devAddr_;
53 uint64_t deviceId = ToDdkDeviceId(busNum, devAddr);
54 HDF_LOGI("%{public}s: busNum:%{public}d, devAddr:%{public}d, deviceID:%{public}" PRIu64 "", __func__,
55 busNum, devAddr, deviceId);
56 usbInterface->UnbindUsbdSubscriber(subscriber);
57
58 int memMapFd = -1;
59 ret = scsiPeripheralDdk->Open(deviceId, g_interfaceIndex, device, memMapFd);
60 if (ret != HDF_SUCCESS) {
61 HDF_LOGE("%{public}s: open device failed, deviceId:%{public}" PRIu64 ", g_interfaceIndex:%{public}" PRIu64 ","
62 "ret:%{public}d", __func__, deviceId, g_interfaceIndex, ret);
63 }
64 ftruncate(memMapFd, DEVICE_MEM_MAP_SIZE);
65
66 return ret;
67 }
68 } // namespace SCSI
69 } // namespace OHOS
70