KOYAMA Yoshiaki のブログ

プログラミングについての試行錯誤をつらつら書き溜めていきます。

HelloKernel.kext、HelloIOKit.kext

Framework Programming Guide
http://developer.apple.com/documentation/MacOSX/Conceptual/BPFrameworks/index.html

以前[ id:hatenadiary:20070205 ] にも書いた Framework Programming Guide を読み終えました。まだ完全には理解していませんが、今度は無謀にもデバイスドライバのドキュメントに挑戦してみました。


まず最初に

Getting Started with Hardware and Drivers: Getting Started with Hardware & Drivers
http://developer.apple.com/referencelibrary/GettingStarted/GS_HardwareDrivers/index.html

を読みました。ここで紹介されていた

I/O Kit Fundamentals
http://developer.apple.com/documentation/DeviceDrivers/Conceptual/IOKitFundamentals/index.html#//apple_ref/doc/uid/TP0000011

を読み始めたのですが、理解しづらいので Getting Started with Hardware and Drivers で紹介されていた下記 URL を参考にデバイスドライバのサンプル Hello Kernel、Hello IOKit を作成してみました。

Kernel Extension Concepts
http://developer.apple.com/documentation/Darwin/Conceptual/KEXTConcept/index.html

ターミナルでコマンド操作を理解していて、Xcode でプログラム経験があれば、何の問題もなく Hello Kernel、Hello IOKit を作成できると思います。


下記に表されているのが、実際にドライバをそれぞれ読み込んで kextstat を使って表示した状態です。

~ yoshiaki$kextstat
  Index Refs Address    Size       Wired      Name (Version) <Linked Against>
   99    1 0xa58000   0x42000    0x41000    com.apple.driver.AppleOnboardAudio (2.5.6b5) <98 71 29 11>
  101    0 0xb39000   0x6000     0x5000     com.apple.driver.AppleOnyxAudio (2.5.6b5) <99 71 11>
  103    0 0x9b8000   0xb000     0xa000     com.apple.iokit.IOBluetoothSerialManager (1.7.8f2) <93 11>
  104    0 0x302b9000 0x2000     0x1000     com.MyTutorial.kext.HelloKernel (1.0.0d1) <11>
  105    0 0x302c3000 0x3000     0x2000     com.MyTutorial.driver.HelloIOKit (1.0.0d1) <11>
Kernel Extension Concepts: Hello Debugger: Debugging a Device Driver With GDB
http://developer.apple.com/documentation/Darwin/Conceptual/KEXTConcept/KEXTConceptDebugger/hello_debugger.html#//apple_ref/doc/uid/20002367

Kernel Extension Concepts: Hello Debugger: Debugging a Device Driver With GDB を試してみたかったのですが、デバイスドライバGDB で Debug する環境がありませんでした。

Kernel Extension Concepts: Packaging Your KEXT for Distribution and Installation
http://developer.apple.com/documentation/Darwin/Conceptual/KEXTConcept/KEXTConceptPackaging/packaging_kext.html#//apple_ref/doc/uid/20002368

Kernel Extension Concepts: Packaging Your KEXT for Distribution and Installation を参考に Hello IOKit を PackageMaker を使って、パッケージ (HelloIOKit.pkg) にしてみました。

PackageMaker の利用は難しいものとばかり思っていたのですが、以外と簡単で驚きました。xshogi.app に利用してみたいですね!


HelloIOKit.pkg をインストールし再起動した後(起動に少し時間がかかりました。)、 kextstat で表示した結果が次の通りです。

~ yoshiaki$kextstat
Index Refs Address    Size       Wired      Name (Version) <Linked Against>
 96    0 0x30d61000 0xa000     0x9000     com.apple.iokit.IOFireWireIP (1.3.4) <37 25 6 5 4 3 2>
   97    0 0x30d6b000 0x5000     0x4000     com.apple.driver.InternalModemSupport (2.3.6) <89 29 11>
   98    0 0x30d70000 0x3000     0x2000     com.MyTutorial.driver.HelloIOKit (1.0.0d1) <11>
   99    0 0x30d73000 0xd000     0xc000     com.apple.driver.AppleTexasAudio (2.5.6b5) <87 86 85 84 11>
  101    1 0x30d8c000 0x2b000    0x2a000    com.apple.driver.AppleDBDMAAudio (2.5.6b5) <84 11>
  102    1 0x32559000 0x42000    0x41000    com.apple.driver.AppleOnboardAudio (2.5.6b5) <101 84 29 11>
  103    0 0x30db7000 0x6000     0x5000     com.apple.driver.AppleOnyxAudio (2.5.6b5) <102 84 11>
  104    0 0x3270a000 0xb000     0xa000     com.apple.iokit.IOBluetoothSerialManager (1.7.8f2) <89 11>


Hello IOKit のアンインストールするシェルスクリプトも作成しました。基本的には

kextunload HelloIOKit.kext
rm -R /System/Library/Extensions/HelloIOKit.kext
rm -R /Library/Receipts/HelloIOKit.pkg

を実行しているだけです。この辺はあまり詳しくないので注意して実行してください。