[USB] Introduction to macOS usb kernel driver development

1. Environmental preparation

Don't want me to be long-winded and jump directly to point 3 to start execution! ! !

1. Background description

  • First, let's define what we do. In fact, it is very simple to write a simple USB driver that runs in the kernel mode on macOS .
  • So how simple is it? That is to say, when a USB device is plugged into the macOS system, it can sense and print something . That's it. Of course, we must ensure that the inserted USB device does not have a matching driver on macOS . Otherwise, it will end up looking for another driver, and it will not be our driver's turn to match.
  • I will not translate the following one, it must be satisfied

To run the sample code project in Xcode, make sure to use macOS 10.15.5 or later and Xcode 11.5 or later.

2. What is SIP? Why do you want to turn off SIP?

  • SIP (System Integrity Protection), what is system integrity protection ? Why is it closed?
  • SIP has done a thing. For the safety of your system, SIP will not allow programs that have not been reviewed by Apple to run on macOS systems.
  • Now I understand, but what kind of program has been reviewed by Apple? This person said that a program downloaded from the app store or a program developed by a developer notarized by Apple is considered a reviewed program. Everything else is not.
  • Then why do you want to turn off SIP? This makes it very clear that the driver we want to run is not downloaded from the app store, and we have not been notarized by Apple. So our program has to turn off SIP to run normally.

3. Turn off SIP (intel processor)

  • Restart the machine and hold down the Command (⌘)-R key combination. Enter recovery mode. as follows:
    insert image description here
  • Open a terminal, and type csrutil disable.
  • Restart the system, you can.

4. Sample code download

Download address sample-code

2. Compile and run

  • Use Xcode to open the above sample code project.
  • Click the button on the upper left of xcode to compile. During this period, the following error may be reported. According to the error message, log in to your own appleId and select the team:

Signing for “SimpleUSBInterfaceDriver” requires a development team. Select a development team in the Signing & Capabilities editor.

  • A terminal will pop up after compiling and running successfully. We execute the following command in the terminal:
sudo cp -R SimpleUSBInterfaceDriver.kext ~
cd ~
sudo chown -R root:wheel SimpleUSBInterfaceDriver.kext
sudo chmod -R 755 SimpleUSBInterfaceDriver.kext
sudo kextload SimpleUSBInterfaceDriver.kext
  • The above operation will also report an error:

Extension with identifiers com.example.apple-samplecode.SimpleUSBInterfaceDriverVP92JH77XU not approved to load. Please approve using System Preferences.

  • Follow the prompts 系统设置>隐私与安全性to allow the program to run inside, as follows:
    insert image description here
  • After allowing, you will be prompted to restart the computer, just do it.
  • Go ahead and execute the above command. Then we 控制台can see the following print in the program, indicating that the kernel loads the driver successfully:
    insert image description here
  • But unfortunately, I haven't found a usb device that can match this driver.

3. References

Guess you like

Origin blog.csdn.net/C2681595858/article/details/127938586
usb
usb