Chapter 08:File System === :::info 檔案系統的實現是整個作業系統裡面最像是軟體開發的部分,實際的硬體操作不算複雜,但最困難的點在於軟體開發的小細節,可以說是多如牛毛,甚至可能因人而異,每個人的設計可能大相徑庭,或是說設計本身是個人 Coding 哲學的體現,這部分也很鼓勵大家自行重新設計出自己版本的檔案系統,你就可以體會這個中的奧妙,過程很辛苦,但一定可以讓你收穫滿滿。   1. IDE   2. File System   3. inode   4. Directory   5. File ::: >[time=Mon, Nov 3, 2025 4:46 PM] --- https://youtu.be/bsPITXZIEmo <iframe width="560" height="315" src="https://www.youtube.com/embed/bsPITXZIEmo?si=Iq5hGnAwoVs52OpV" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe> https://youtu.be/vo21xzQT6h4 <iframe width="560" height="315" src="https://www.youtube.com/embed/vo21xzQT6h4?si=aiO1PCKo_aD-3R9c" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>  # IDE (Integrated Drive Electronics)   >[!Note] SATA? PATA? > > > >[!Caution] Bochs IDE >在 Bochs 這個 x86 模擬器裡,預設的 IDE 硬碟控制器是 PATA(也就是 Parallel ATA / 俗稱 IDE),不是 SATA。 >[!Note] Hard Disk > >[!Note] Disk Partition Table (DPT) > > > > > ==The starting LBA is the offset from the beginning of the partition.== > > >[!Caution] EBR 的相對位置會不同! >[!Note] Partition Type | 類型 | 代碼 (Hex) | 說明 | | ----------- | -------------------------------------- | ------------- | | 0x00 | 空分割區 | 未使用 | | 0x01 | FAT12 | 早期 DOS | | 0x04 / 0x06 | FAT16 | 小/大容量 FAT16 | | 0x07 | **NTFS / exFAT / HPFS** | Windows 使用 | | 0x0B / 0x0C | **FAT32 / FAT32 LBA** | Windows 使用 | | 0x0E | FAT16 LBA | 支援 LBA | | 0x0F | **Extended LBA** | 延伸分割區 | | 0x05 | **Extended CHS** | 延伸分割區(舊式 CHS) | | 0x82 | **Linux swap** | | | 0x83 | **Linux native(ext2/ext3/ext4 等)** | | | 0x8E | **Linux LVM (Logical Volume Manager)** | | | 0xA5 | BSD / FreeBSD | | | 0xA6 | OpenBSD | | | 0xA8 | macOS UFS | | | 0xAB | macOS Boot | | | 0xAF | macOS HFS / APFS | | | 0xEE | **GPT 保護分割區(Protective MBR)** | | | 0xEF | **EFI System Partition (ESP)** | | | 0xFD | Linux RAID autodetect | | ## Disk Formatting | Item | Value | | --------------- | ------- | | Capacity | 80 MB | | Sector size | 512 B | | Sector number | 163296 | | Cylinder number | ==162== | | Head number | ==16== | | Sector/Track | 63 | >[!Tip] 一個 track 固定是 63 sectors 建立新的硬碟 ``` ./bximage ```  設定 bochsrc ``` ata0-slave: type=disk, mode=flat, path="80mb.img" ```  確定 bochs 有認到硬碟 ``` xp /b 0x475 ```  格式化硬碟 ``` fdisk ./80mb.img ``` 指令描述  extra functionality 指令描述  print info  Setting Dos CHS  Primary Partition  Extended partition  Logical partition  Setting Partition Type  Write table to disk  Check the result ``` fdisk -l ./80mb.img ```  ## Enable all ATA channel >[!Note] ATA channel | 控制器 | 通常名稱 | Base I/O Port | Control Port | IRQ | 裝置代號 | |:-------- |:---------- |:------------- |:------------ |:------ |:------------ | | **ATA0** | Primary | `0x1F0` | `0x3F6` | IRQ 14 | Master/Slave | | **ATA1** | Secondary | `0x170` | `0x376` | IRQ 15 | Master/Slave | | **ATA2** | Tertiary | `0x1E8` | `0x3E6` | IRQ 11 | Master/Slave | | **ATA3** | Quaternary | `0x168` | `0x366` | IRQ 10 | Master/Slave |   disable pci   ## Programming >Reference: >[Bochs' map of I/O ports to functions](https://bochs.sourceforge.io/techspec/PORTS.LST) >[OSDev wiki](https://wiki.osdev.org/ATA_PIO_Mode#Addressing_Modes) > > >==會使用到的 command: 0xEC(identify), 0x20(read), 0x30(write)== > > > >主要分成以下五大步驟: >* 設定 sector count,預計要讀(寫)幾個 sector。 >* 設定 hard disk LBA 地址。 >* 寫入讀(寫)指令。 >* Polling 等待完成。 >* 讀取資料到 RAM。 >[!Note] identify drive 指令(0xEC) >這個指令會回傳一個 512 位元組(256 個 16-bit words) 的資料區塊,用來描述這顆磁碟的屬性、能力、幾何資訊(柱面、磁頭、磁區數)等。每個欄位都是以 16 位元(1 word) 為單位排列。 | Word 編號 | 說明 | Bochs 常見數值或行為 | | ------- | ---------------------------- | -------------------------- | | 0 | 一般設定(general configuration) | `0x0040` → 表示非可移除磁碟、磁性儲存裝置 | | 1 | 磁柱數(cylinders) | 由 `.bochsrc` 設定(例如 16383) | | 3 | 磁頭數(heads) | 由設定檔決定(例如 16) | | 6 | 每磁道磁區數(sectors per track) | 由設定檔決定(例如 63) | | 10–19 | 序號(Serial number,ASCII 格式) | `"BOCHS HARDDISK"` 或自訂 | | 23–26 | 韌體版本(Firmware revision) | 例如 `"BXHD001"` | | 27–46 | 型號名稱(Model number) | `"Bochs Virtual HD"` | | 47 | 每次中斷最大可傳輸磁區數 | 通常 `0x8010` | | 49 | 能力位元(Capabilities bit field) | 表示支援 DMA、LBA (`0x0F00`) | | 60–61 | 可使用之 LBA28 磁區總數 | 依磁碟映像檔大小決定 | | 80 | 支援的 ATA 版本 | 通常 `0x0006`(ATA-6) | | 82–84 | 支援的功能位元(Feature bits) | 顯示支援 LBA、DMA 等功能 | | 88 | Ultra DMA 模式支援 | 常見值 `0x007F` | | 100–103 | LBA48 總磁區數 | 若磁碟容量超過 128GiB 時會使用 | | 255 | 校驗碼(Checksum) | 通常為有效的 `0xA5` |   ## Source Code https://github.com/srhuang/a-os/commit/bfd4d57a781c40db1f0c11fda59cc61fcbfaf6f8 ## Compile ``` make all ``` ## Put on hard disk ``` sh gen.sh ``` ## Checkpoint  # File System  >[!Note] Volume Boot Record (VBR) / OS Boot Record (OBR) >位於 每個分割區(partition)開頭的第一個 sector,包含啟動程式碼與該分割區的檔案系統資訊。 | 名稱 | 全名 | 位置 | 功能 | | ------- | -------------------- | ------------------------------ | ---------------------- | | **MBR** | Master Boot Record | 磁碟最前面的 sector <br>(LBA 0) | 決定要載入哪個分割區開機(通常載入 VBR) | | **EBR** | Extended Boot Record | 每個邏輯分割區的開頭 <br>(Extended Partition) | 描述該邏輯分割區的位置與下一個 EBR | | **VBR** | Volume Boot Record | 每個(主或邏輯)分割區的開頭 | 該分割區的開機程式 | >[!Note] Super Block > >[!Note] inode 可以表示的檔案大小 >Block size = 4KB, LBA = 4 bytes >* Direct blocks: $12 \times 4KB=48KB$ >* Single indirect blocks: $(4KB\div4)\times4KB=4MB$ >* Double indirect blocks: $1KB\times1KB\times4KB=4GB$ >* Triple indirect blocks: $1KB\times1KB\times1KB\times4KB=4TB$ >[!Note] inode table > >[!Note] Data Structure > >[!Note] Root dir > >[!Note] block bitmap reset > ## Source Code https://github.com/srhuang/a-os/commit/d058c336af08f44a34601d8ba4ea8f05c1c9c024 ## Compile ``` make all ``` ## Put on hard disk ``` sh gen.sh ``` ## Checkpoint  # inode  >[!Note] i_block > >[!Note] inode read and write > ## Source Code https://github.com/srhuang/a-os/commit/315026263aaef6c8f1a3278500a39eed7f7fa5b0 ## Compile ``` make all ``` ## Put on hard disk ``` sh gen.sh ``` ## Checkpoint  # Directory   ## Source Code https://github.com/srhuang/a-os/commit/aad933eaa9de5e11353345b111d4f14a9efd3f4a ## Compile ``` make all ``` ## Put on hard disk ``` sh gen.sh ``` ## Checkpoint  # File   ## Source Code https://github.com/srhuang/a-os/commit/039a10b298cea86a42c7705df57fbeea8b2505b9 ## Compile ``` make all ``` ## Put on hard disk ``` sh gen.sh ``` ## Checkpoint  # More about File System  ## Source Code https://github.com/srhuang/a-os/commit/d879e79f029aa0fa553e4adb760765cc821f8c21 >[!Caution] stat bug fix >https://github.com/srhuang/a-os/commit/3cb7cd16446e49258daf0ce772f81d933f6dd0c4 ## Compile ``` make all ``` ## Put on hard disk ``` sh gen.sh ``` ## Checkpoint 
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up