Skip to content

Partition Tables

opencode-agent[bot] edited this page May 10, 2026 · 1 revision

Partition Tables (IBM, GPT, APM)

Partition table parsing and creation for different disk formats.

Overview

JNode supports multiple partition table formats, enabling compatibility with disks partitioned by various operating systems. The partition table system provides a unified interface for detecting, reading, and traversing partitions regardless of the underlying format.

The three main partition table formats supported are:

  1. IBM MBR (Master Boot Record) - The traditional PC partition scheme, supporting up to 4 primary partitions or 3 primary + 1 extended partition
  2. GPT (GUID Partition Table) - The modern standard for UEFI-based systems, supporting practically unlimited partitions and larger disks
  3. APM (Apple Partition Map) - Used by older Mac systems

Partition tables are discovered and parsed through the PartitionTableType interface, which provides a plugin-based mechanism for supporting new formats.

Key Components

Class / File Role
fs/src/partitions/org/jnode/partitions/PartitionTable.java Generic interface for all partition tables
fs/src/partitions/org/jnode/partitions/PartitionTableEntry.java Interface for partition entries
fs/src/partitions/org/jnode/partitions/PartitionTableType.java Interface for partition table type detection/creation
fs/src/partitions/org/jnode/partitions/ibm/IBMPartitionTable.java IBM MBR partition table implementation
fs/src/partitions/org/jnode/partitions/ibm/IBMPartitionTableEntry.java IBM MBR partition entry
fs/src/partitions/org/jnode/partitions/ibm/IBMPartitionTableType.java IBM MBR type detection
fs/src/partitions/org/jnode/partitions/ibm/IBMPartitionTypes.java MBR partition type constants
fs/src/partitions/org/jnode/partitions/gpt/GptPartitionTable.java GPT partition table implementation
fs/src/partitions/org/jnode/partitions/gpt/GptPartitionTableEntry.java GPT partition entry
fs/src/partitions/org/jnode/partitions/gpt/GptPartitionTableType.java GPT type detection
fs/src/partitions/org/jnode/partitions/apm/ApmPartitionTable.java Apple Partition Map implementation
fs/src/partitions/org/jnode/partitions/apm/ApmPartitionTableEntry.java APM partition entry
fs/src/partitions/org/jnode/partitions/service/def/PartitionTablePlugin.java Plugin exposing partition services

How It Works

Architecture

The partition table system follows a plugin-based architecture:

PartitionTableType (interface)
    ├── IBMPartitionTableType
    ├── GptPartitionTableType
    └── ApmPartitionTableType

Each PartitionTableType implementation provides:

  • getName() - Unique identifier for the partition type
  • supports(byte[] firstSectors, BlockDeviceAPI) - Detection whether a disk uses this format
  • create(byte[] firstSectors, Device) - Creates a PartitionTable instance

IBM MBR Detection and Parsing

The MBR is stored in the first sector (512 bytes) of a disk. Detection checks for multiple signatures:

  1. Magic number: 0xAA55 at offset 510
  2. Extended signatures: Various vendor-specific signatures (AST, Disk Manager, LILO, HP, etc.)
  3. Error message strings: DOS/Windows/BSD/GRUB boot code error messages
  4. Fallback: If no signature matches, validates that partition entries don't overlap and at least one is valid
// From IBMPartitionTable.containsPartitionTable()
if (LittleEndian.getUInt16(bootSector, 510) != 0xaa55) {
    return false;
}
// ... check extended signatures ...
// ... check boot code strings ...
// ... validate partition entries ...

MBR supports:

  • 4 primary partition entries (16 bytes each at offsets 446-509)
  • Extended partitions via linked EBR (Extended Boot Record) chains
  • Partition types identified by 1-byte system indicator

GPT Detection and Parsing

GPT uses a protective MBR in sector 0 followed by the GPT header:

  1. Protective MBR: Contains a single partition of type 0xEE (EFI GPT)
  2. GPT Header: Located at LBA 1, starts with "EFI PART" signature
  3. Partition Array: Follows header, contains 128-byte entry blocks
// From GptPartitionTable.detectBlockSize()
String signature = new String(signatureBytes, Charset.forName("US-ASCII"));
if ("EFI PART".equals(signature)) {
    return blockSize;
}

GPT requires reading the first 16KB of disk to detect and parse partitions.

Partition Entry Interface

All partition entries implement PartitionTableEntry:

public interface PartitionTableEntry {
    boolean isValid();
    boolean hasChildPartitionTable();
    PartitionTable<?> getChildPartitionTable();
}

This enables recursive partition traversal - useful for extended MBR partitions and LVM-style nested partition tables.

Gotchas & Non-Obvious Behavior

  1. GPT requires larger initial read: Unlike MBR (512 bytes), GPT detection requires reading at least 16KB to access the partition array.

  2. Protective MBR: GPT disks still contain a protective MBR for backward compatibility with legacy BIOS. The GptPartitionTable.containsPartitionTable() method can optionally require this protective MBR.

  3. Extended partition chain: MBR extended partitions form a linked chain where each EBR points to the next. The IBMPartitionTable recursively traverses this chain in handleExtended().

  4. Multiple detection signatures: The MBR detector checks for ~20 different signatures. A disk with boot code can still have a valid partition table.

  5. Partition type vs filesystem: MBR partition type (system indicator) identifies the partition scheme, not the filesystem. A type of 0x83 (Linux) doesn't guarantee ext2/ext3 - it's just a convention.

  6. Overlapping partition detection: The MBR validator explicitly checks for overlapping entries and rejects the table if found.

  7. APM structure: APM stores partition entries starting at offset 0x200, with a counter at 0x204 indicating the number of entries.

Related Pages

Clone this wiki locally