From ae14abb38de9460bb0e5cd2da3f302eab58d5e82 Mon Sep 17 00:00:00 2001 From: Anduin1109 Date: Thu, 11 Sep 2025 14:43:22 +0800 Subject: [PATCH 1/2] set default authorize for enterprise device Signed-off-by: Anduin1109 --- usb/hdi_service/include/usb_device_impl.h | 2 + usb/hdi_service/src/usb_device_impl.cpp | 45 +++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/usb/hdi_service/include/usb_device_impl.h b/usb/hdi_service/include/usb_device_impl.h index 34e89afa0c..a2c5e29bd7 100644 --- a/usb/hdi_service/include/usb_device_impl.h +++ b/usb/hdi_service/include/usb_device_impl.h @@ -74,6 +74,8 @@ private: std::string GetDeviceDirName(uint8_t devNum, uint8_t devAddr); std::string GetInterfaceDirName(uint8_t devNum, uint8_t devAddr, uint8_t configId, uint8_t interfaceId); int32_t SetAuthorize(const std::string &filePath, bool authorized); + int32_t SetDefaultAuthorize(bool authorized); + int32_t SetGlobalDefaultAuthorize(bool authorized); static UsbdSubscriber subscribers_[MAX_SUBSCRIBER]; static bool isGadgetConnected_; static bool isEdmExist_; diff --git a/usb/hdi_service/src/usb_device_impl.cpp b/usb/hdi_service/src/usb_device_impl.cpp index b66b9c2bef..349e682dca 100644 --- a/usb/hdi_service/src/usb_device_impl.cpp +++ b/usb/hdi_service/src/usb_device_impl.cpp @@ -359,6 +359,10 @@ int32_t UsbDeviceImpl::UsbdEventHandleRelease(void) int32_t UsbDeviceImpl::UsbDeviceAuthorize(uint8_t busNum, uint8_t devAddr, bool authorized) { HDF_LOGI("%{public}s: enter", __func__); + if (busNum == 0 && devAddr == 0) { + // set default value + return SetDefaultAuthorize(authorized); + } std::string dev_dirname = GetDeviceDirName(busNum, devAddr); if (dev_dirname.length() == 0) { HDF_LOGE("%{public}s: failed to reach busNum: %{public}d, devAddr: %{public}d", __func__, busNum, devAddr); @@ -540,6 +544,47 @@ int32_t UsbDeviceImpl::SetAuthorize(const std::string &filePath, bool authorized return HDF_SUCCESS; } +int32_t UsbDeviceImpl::SetDefaultAuthorize(bool authorized) +{ + int32_t ret = SetGlobalDefaultAuthorize(authorized); + std::string hubDir; + struct dirent *entry; + DIR *dir = opendir(SYSFS_DEVICES_DIR); + if (dir == nullptr) { + HDF_LOGE("%{public}s: dir is empty", __func__); + return HDF_FAILURE; + } + while ((entry = readdir(dir)) != nullptr) { + if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) { + continue; + } + hubDir = std::string(entry->d_name); + (void)SetAuthorize(SYSFS_DEVICES_DIR + hubDir + "/authorized_default", authorized); + } + closedir(dir); + return ret; +} + +int32_t UsbDeviceImpl::SetGlobalDefaultAuthorize(bool authorized) +{ + int32_t fd; + int32_t ret; + std::string content = (authorized)? "-1" : "0"; + fd = open("/sys/module/usbcore/parameters/authorized_default", O_WRONLY | O_TRUNC); + if (fd < 0) { + HDF_LOGE("%{public}s: failed to reach authorized_default, errno = %{public}d", __func__, errno); + return HDF_FAILURE; + } + ret = write(fd, content.c_str(), content.length()); + close(fd); + if (ret < 0) { + HDF_LOGE("%{public}s: failed to set usb default authorize, errno = %{public}d", __func__, errno); + return HDF_FAILURE; + } + HDF_LOGI("%{public}s: set usb global default authorize %{public}s finished", __func__, content.c_str()); + return HDF_SUCCESS; +} + } // namespace v2_0 } // namespace Usb } // namespace HDI -- Gitee From 1bf729c2795fb805acbde97dc9d276cc7baba689 Mon Sep 17 00:00:00 2001 From: Anduin1109 Date: Thu, 11 Sep 2025 16:24:08 +0800 Subject: [PATCH 2/2] set default authorize for enterprise device Signed-off-by: Anduin1109 --- usb/hdi_service/src/usb_device_impl.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/usb/hdi_service/src/usb_device_impl.cpp b/usb/hdi_service/src/usb_device_impl.cpp index 349e682dca..e8081da330 100644 --- a/usb/hdi_service/src/usb_device_impl.cpp +++ b/usb/hdi_service/src/usb_device_impl.cpp @@ -559,6 +559,8 @@ int32_t UsbDeviceImpl::SetDefaultAuthorize(bool authorized) continue; } hubDir = std::string(entry->d_name); + // set all authorized values (hubs have "authorized_default" attribute), and just ignore any failure here + (void)SetAuthorize(SYSFS_DEVICES_DIR + hubDir + "/authorized_default", authorized); (void)SetAuthorize(SYSFS_DEVICES_DIR + hubDir + "/authorized_default", authorized); } closedir(dir); -- Gitee