#! /usr/bin/python3
"""CP/M disk image utility supporting multiple disk formats.

Supported formats:
  - sssd:  8" SSSD floppy (ibm-3740 compatible, 250KB)
  - hd1k:  Standard RomWBW hd1k format (8MB single slice)
  - combo: Combo disk with 1MB MBR prefix + 6x8MB slices (51MB total)
           Every subcommand takes --slice N to pick one; slice N behaves
           exactly like a plain hd1k image.  There is no 8MB reach limit, so
           slices 1-5 are as accessible as slice 0.

Usage:
  cpm_disk.py create <disk.img>                    # Create 8MB hd1k disk
  cpm_disk.py create --sssd <disk.img>             # Create 250KB SSSD floppy
  cpm_disk.py create --combo <disk.img>            # Create 51MB combo disk
  cpm_disk.py add <disk.img> <file1.com> [...]     # Add files to disk
  cpm_disk.py list <disk.img>                      # List files, and any of
                                                   # F1-F8 R/O SYS ARC set
  cpm_disk.py delete <disk.img> <file1.com> [...]  # Delete files from disk
  cpm_disk.py extract <disk.img> <file1.com> [...] # Extract files from disk
  cpm_disk.py list --slice 3 <combo.img>           # A combo slice (0-5)
  cpm_disk.py add  --slice 3 <combo.img> <f.com>   # ...any subcommand takes it
  cpm_disk.py read-boot <disk.img> <output.bin>       # Read boot area to file
  cpm_disk.py write-boot <disk.img> <input.bin>       # Write to sector 0
  cpm_disk.py write-boot <disk.img> <input.bin> 4     # Write starting at sector 4
  cpm_disk.py write-boot <disk.img> <input.bin> 4 2   # Write at sector 4, pad to 2 sectors

Boot area sizes:
  - SSSD:  6656 bytes  (2 tracks x 26 sectors x 128 bytes, no skew)
  - hd1k:  16384 bytes (2 tracks x 16 sectors x 512 bytes)
  - combo: 16384 bytes (after 1MB prefix)

Format is auto-detected for existing disks based on file size.

CP/M Extent Handling
--------------------
CP/M directory entries are 32 bytes and can only track a limited amount of file
data. For files larger than this limit, multiple directory entries called
"extents" are used. Key concepts:

1. RECORDS: CP/M measures file size in 128-byte records (not bytes).

2. EXTENT CAPACITY: Each directory entry can track up to 128 records (16KB).
   The actual capacity depends on block size and the EXM (Extent Mask) value
   from the Disk Parameter Block:
   - EXM=0: Each entry = 1 logical extent = 128 records = 16KB max
   - EXM=1: Each entry = 2 logical extents = 256 records = 32KB max

3. EXTENT NUMBERING: The EX field (byte 12, bits 0-4) and S2 field (byte 14,
   bits 0-5) combine to form an 11-bit extent number:
     extent_num = (S2 << 5) | EX
   For EXM=1, the extent number is the LAST logical extent in that physical
   directory entry (e.g., extent 1 covers logical extents 0-1).

4. RECORD COUNT (RC): Byte 15 contains the number of records used in the
   LAST logical extent of this directory entry (1-128).

5. BLOCK POINTERS: Bytes 16-31 contain pointers to disk blocks:
   - 8-bit pointers (16 per entry): Used when DSM < 256 (small disks)
   - 16-bit pointers (8 per entry): Used when DSM >= 256 (large disks)

Example: A 50KB file on an hd1k disk (EXM=1, 16-bit pointers):
  - 50KB = 400 records = 2 physical extents
  - Extent 0: directory entry with extent_num=1, RC=128 (covers records 0-255)
  - Extent 1: directory entry with extent_num=3, RC=16 (covers records 256-399)

When reading files, extents must be processed in order by extent number.
When writing files, new directory entries are allocated for each extent.
"""

import sys
import os
import struct
import argparse

# Common CP/M constants
SECTOR_SIZE_HD = 512    # hd1k sector size
SECTOR_SIZE_SSSD = 128  # SSSD sector size

# ibm-3740 (8" SSSD) format constants
SSSD_SECTOR_SIZE = 128
SSSD_SECTORS_PER_TRACK = 26
SSSD_TRACKS = 77
SSSD_BLOCK_SIZE = 1024   # 1KB blocks
SSSD_DIR_ENTRIES = 64
SSSD_BOOT_TRACKS = 2
SSSD_SIZE = SSSD_TRACKS * SSSD_SECTORS_PER_TRACK * SSSD_SECTOR_SIZE  # 256,256 bytes
SSSD_DIR_START = SSSD_BOOT_TRACKS * SSSD_SECTORS_PER_TRACK * SSSD_SECTOR_SIZE  # 6656 bytes
SSSD_SKEW = 6  # Standard ibm-3740 sector skew (boot tracks have no skew)


def generate_skew_table(sectors, skew):
    """Generate sector translation table for given skew factor.

    Returns a list where table[logical_sector] = physical_sector (0-indexed).
    Uses the standard CP/M algorithm to avoid sector collisions.
    """
    if skew == 0:
        return list(range(sectors))

    # table[physical] = logical during construction
    table = [None] * sectors
    physical = 0
    for logical in range(sectors):
        # Find next free physical sector
        while table[physical] is not None:
            physical = (physical + 1) % sectors
        table[physical] = logical
        physical = (physical + skew) % sectors

    # Invert: we want logical_to_physical
    logical_to_physical = [0] * sectors
    for phys, log in enumerate(table):
        logical_to_physical[log] = phys
    return logical_to_physical


# Pre-computed skew table for ibm-3740
SSSD_SKEW_TABLE = generate_skew_table(SSSD_SECTORS_PER_TRACK, SSSD_SKEW)

# hd1k format constants
BLOCK_SIZE = 4096  # 4KB blocks for hd1k


def cpm_pattern_to_83(pattern):
    """Convert a CP/M wildcard pattern to 8.3 format with ? expansion.

    In CP/M, * fills the rest of the field with ?, and ? matches any char.
    E.g., "*.COM" -> "????????COM", "A*.*" -> "A???????????"
    """
    pattern = pattern.upper()
    if '.' in pattern:
        name, ext = pattern.rsplit('.', 1)
    else:
        name, ext = pattern, ''

    # Expand * to fill rest of field with ?
    if '*' in name:
        idx = name.index('*')
        name = name[:idx] + '?' * (8 - idx)
    name = name[:8].ljust(8, ' ')

    if '*' in ext:
        idx = ext.index('*')
        ext = ext[:idx] + '?' * (3 - idx)
    ext = ext[:3].ljust(3, ' ')

    return name + ext


def cpm_match(pattern_83, filename_83):
    """Match a filename against a CP/M pattern (both in 8.3 format).

    ? matches any character, other characters must match exactly.
    """
    if len(pattern_83) != 11 or len(filename_83) != 11:
        return False
    for p, f in zip(pattern_83, filename_83):
        if p != '?' and p != f:
            return False
    return True


# A directory entry's name is eleven bytes, and bit 7 of each is an attribute,
# not part of the name: f1'-f4' are the user attributes MP/M's and CP/M 3's
# SET [F1=ON] sets, f5'-f8' are reserved, and t1', t2' and t3' are read-only,
# SYS and archive.  The BDOS compares a name with all eleven stripped, and so
# does everything here.  Nothing decodes a raw name byte: delete and add used
# to, and died with UnicodeDecodeError on the first entry with a bit set in
# its first eight bytes, whichever file they had been asked about.
ATTRIBUTE_NAMES = ('F1', 'F2', 'F3', 'F4', 'F5', 'F6', 'F7', 'F8',
                   'R/O', 'SYS', 'ARC')


def entry_name83(entry):
    """A directory entry's eleven name bytes with the attribute bits off."""
    return bytes(b & 0x7F for b in entry[1:12])


def entry_attribute_bits(entry):
    """Bit i set when name byte i+1 of the entry carries its attribute."""
    bits = 0
    for i in range(11):
        if entry[1 + i] & 0x80:
            bits |= 1 << i
    return bits


def attribute_names(bits):
    """['F1', 'SYS', ...] for a mask from entry_attribute_bits()."""
    return [ATTRIBUTE_NAMES[i] for i in range(11) if bits & (1 << i)]


def entry_filename(entry):
    """'NAME.EXT' for a directory entry, or None when its name is not printable
    ASCII even with the attribute bits off - an unformatted or damaged entry."""
    n = entry_name83(entry)
    if not all(0x20 <= b <= 0x7E for b in n):
        return None
    name = n[:8].decode('ascii').rstrip()
    ext = n[8:].decode('ascii').rstrip()
    return name + '.' + ext if ext else name


def filename_to_name83(filename, fold=True):
    """The eleven bytes a filename is compared with an entry's as: upper case,
    both fields space-padded.  A character with no ASCII form becomes '?',
    which no name on a disk has, so it matches nothing rather than raising.
    fold=False keeps the case, for a name taken from the directory itself."""
    name, ext = os.path.splitext(filename.upper() if fold else filename)
    name = name[:8].ljust(8)
    ext = ext[1:4].ljust(3) if ext else '   '
    return (name + ext).encode('ascii', 'replace')


def lookup_name83(disk, filename, user):
    """The eleven name bytes the entries `filename` means carry.

    The name upper-cased, as the CCP upper-cases everything typed at it - and,
    when no entry of this user has that name, the one name that differs from
    it only in case.  The BDOS does not fold case, so a program that makes a
    file with a lower-case FCB leaves a name no CCP command can reach; this
    upper-cased the request and compared, so extract and delete could not
    reach it either, although list showed it.  Two names that fold alike and
    neither exact: nothing matches, rather than a guess."""
    want = filename_to_name83(filename)
    folded = []
    for entry in disk.dir_entries():
        if entry[0] != user:
            continue
        name = entry_name83(entry)
        if name == want:
            return want
        if name.upper() == want and name not in folded:
            folded.append(name)
    return folded[0] if len(folded) == 1 else want


def allocate_blocks(used, first_block, max_block, blocks_needed):
    """The blocks a file of `blocks_needed` blocks is written to, or None.

    One run after the highest block in use, when the file fits there: that is
    where this tool has always put a file, and romwbw_disks rebuilds published
    images byte for byte on it.  When it does not fit, free blocks from the
    lowest up - an allocation map lists each block, and nothing needs them
    adjacent.  This stopped at the run, so the blocks a delete or a replace
    freed below the highest file were never used again: on SSSD, a 100 KB
    file replaced twice beside a 10 KB one filled the disk with 110 of its
    241 blocks in use."""
    top = max(used) if used else first_block - 1
    start = max(top + 1, first_block)
    if start + blocks_needed - 1 <= max_block:
        return list(range(start, start + blocks_needed))
    free = [b for b in range(first_block, max_block + 1) if b not in used]
    if len(free) < blocks_needed:
        return None
    return free[:blocks_needed]


def free_block_count(used, first_block, max_block):
    return sum(1 for b in range(first_block, max_block + 1) if b not in used)


def refusal(filename, cpm_filename, replacing, blocks, blocks_needed, free_blocks,
            entries_needed, free_entries):
    """Why add_file cannot write this file, or None when it can.

    Both refusals are decided before anything is written or reported, with a
    file of the same name already counted out, so a replace that fails leaves
    that file as it was and does not say it was replaced.  add printed
    "(replaced existing A.BIN)" and then "needs 150 blocks and only 141 are
    free", and A.BIN was still there: the delete was on the image in memory,
    which add does not write back after an error.  The directory check was
    made while the entries were being written, after the data blocks.
    """
    def also(what):
        if not replacing:
            return ""
        return (f", counting the {what} of the {cpm_filename} it would replace, "
                f"which is left as it was")
    if blocks is None:
        return (f"Error: {filename} needs {blocks_needed} blocks and only "
                f"{free_blocks} are free{also('blocks')}")
    if entries_needed > free_entries:
        return (f"Error: {filename} needs {entries_needed} directory entries and only "
                f"{free_entries} are free{also('directory entries')}")
    return None


# Disk format sizes
HD1K_SINGLE_SIZE = 8388608      # 8 MB
HD1K_SLICE_SIZE = 8388608       # 8 MB per slice
HD1K_MBR_PREFIX = 1048576       # 1 MB MBR prefix for combo
HD1K_COMBO_SLICES = 6           # 6 slices in combo disk
HD1K_COMBO_SIZE = HD1K_MBR_PREFIX + (HD1K_COMBO_SLICES * HD1K_SLICE_SIZE)  # ~51 MB


def format_sssd_disk(data):
    """Format an SSSD (ibm-3740) disk with empty CP/M directory.

    ibm-3740 format:
    - 128 bytes/sector, 26 sectors/track, 77 tracks
    - Block size: 1024 bytes (8 sectors)
    - Boot tracks: 2 (reserved)
    - Directory: 64 entries x 32 bytes = 2KB = 2 blocks

    Directory starts at track 2, sector 0
    """
    # Every sector after the boot tracks is 0xE5, the CP/M empty directory
    # marker, as a real FORMAT leaves it.  This used to fill the first 2 KB
    # of track 2 by physical offset, which is the directory's size but not
    # where it is: the directory is read through the sector skew, so its
    # sixteen logical sectors are spread over the whole track, and the half of
    # them outside those 2 KB read back as zeros - user 0, a name of NULs.
    # `create --sssd` failed its own verify on those and never wrote a file.
    data[SSSD_DIR_START:SSSD_SIZE] = bytes([0xE5] * (SSSD_SIZE - SSSD_DIR_START))


def create_sssd_disk():
    """Create a new formatted SSSD (ibm-3740) disk image in memory.

    Returns:
        bytearray containing the formatted disk image
    """
    data = bytearray(SSSD_SIZE)
    format_sssd_disk(data)
    return data


def format_hd1k_slice(data, offset):
    """Format a single hd1k slice with empty CP/M directory.

    hd1k format:
    - 512 bytes/sector, 16 sectors/track, 1024 tracks
    - Block size: 4096 bytes (8 sectors)
    - Boot tracks: 2 (reserved)
    - Directory: 1024 entries x 32 bytes = 32KB = 8 blocks

    Directory starts at track 2, sector 0
    """
    BOOT_TRACKS = 2
    SECTORS_PER_TRACK = 16
    DIR_ENTRIES = 1024
    DIR_ENTRY_SIZE = 32

    dir_offset = offset + (BOOT_TRACKS * SECTORS_PER_TRACK * SECTOR_SIZE_HD)
    dir_size = DIR_ENTRIES * DIR_ENTRY_SIZE

    # Initialize directory with 0xE5 (CP/M empty directory marker)
    if dir_offset + dir_size <= len(data):
        data[dir_offset:dir_offset + dir_size] = bytes([0xE5] * dir_size)


def create_combo_mbr(data):
    """Create MBR for combo disk with RomWBW partition type.

    MBR structure:
    - Offset 0x1BE: First partition entry (16 bytes)
    - Offset 0x1FE: Signature 0x55AA
    """
    PARTITION_START_LBA = 2048  # 1MB / 512 = 2048 sectors
    PARTITION_SIZE_LBA = (HD1K_COMBO_SIZE - HD1K_MBR_PREFIX) // 512

    # Partition entry at offset 0x1BE
    part_offset = 0x1BE
    data[part_offset + 0] = 0x00   # Not bootable
    data[part_offset + 1] = 0x01   # CHS start head
    data[part_offset + 2] = 0x01   # CHS start sector
    data[part_offset + 3] = 0x00   # CHS start cylinder
    data[part_offset + 4] = 0x2E   # Partition type: RomWBW hd1k
    data[part_offset + 5] = 0xFE   # CHS end head
    data[part_offset + 6] = 0xFF   # CHS end sector
    data[part_offset + 7] = 0xFF   # CHS end cylinder

    # LBA start (little-endian)
    struct.pack_into('<I', data, part_offset + 8, PARTITION_START_LBA)
    # LBA size (little-endian)
    struct.pack_into('<I', data, part_offset + 12, PARTITION_SIZE_LBA)

    # MBR signature
    data[0x1FE] = 0x55
    data[0x1FF] = 0xAA


def create_hd1k_disk(combo=False):
    """Create a new formatted hd1k disk image in memory.

    Args:
        combo: If True, create combo disk (51MB), otherwise single slice (8MB)

    Returns:
        bytearray containing the formatted disk image
    """
    if combo:
        size = HD1K_COMBO_SIZE
    else:
        size = HD1K_SINGLE_SIZE

    # Allocate and zero-fill
    data = bytearray(size)

    if not combo:
        # Single slice: format the entire disk
        format_hd1k_slice(data, 0)
    else:
        # Combo disk: create MBR and format each slice
        create_combo_mbr(data)

        # Format each slice (starts after 1MB MBR prefix)
        for slice_num in range(HD1K_COMBO_SLICES):
            slice_offset = HD1K_MBR_PREFIX + (slice_num * HD1K_SLICE_SIZE)
            format_hd1k_slice(data, slice_offset)

    return data


class SssdDisk:
    """SSSD (ibm-3740) 8" floppy disk format."""

    SECTOR_SIZE = SSSD_SECTOR_SIZE  # 128 bytes
    SECTORS_PER_TRACK = SSSD_SECTORS_PER_TRACK  # 26
    BLOCK_SIZE = SSSD_BLOCK_SIZE  # 1024 bytes
    DIR_ENTRIES = SSSD_DIR_ENTRIES  # 64
    BOOT_TRACKS = SSSD_BOOT_TRACKS  # 2
    DIR_START = SSSD_DIR_START  # 6656 bytes (2 tracks * 26 sectors * 128 bytes)
    # With 1KB blocks and DSM < 256, block pointers are 8-bit
    # EXM = 0 for 1KB blocks, so each extent = 128 records = 16KB = 16 blocks
    BLOCKS_PER_EXTENT = 16
    RECORDS_PER_BLOCK = BLOCK_SIZE // 128  # 8 records per 1KB block

    # Blocks that fit in the data area: 0..MAX_BLOCK.  Same reason Hd1kDisk has
    # one - without it add_file walks off the end of the image and grows the
    # file rather than reporting a full disk.
    MAX_BLOCK = (SSSD_SIZE - SSSD_DIR_START) // SSSD_BLOCK_SIZE - 1

    def __init__(self, disk_data, use_skew=True):
        self.data = disk_data
        self.use_skew = use_skew
        self.skew_table = SSSD_SKEW_TABLE if use_skew else list(range(self.SECTORS_PER_TRACK))

    def logical_sector_to_offset(self, track, logical_sector):
        """Convert (track, logical_sector) to byte offset in disk image.

        Boot tracks (0-1) have no skew; data tracks use skew table.
        logical_sector is 0-indexed.
        """
        if track < self.BOOT_TRACKS:
            # Boot tracks: no skew
            physical_sector = logical_sector
        else:
            # Data tracks: apply skew
            physical_sector = self.skew_table[logical_sector]
        return (track * self.SECTORS_PER_TRACK + physical_sector) * self.SECTOR_SIZE

    def read_sector(self, track, logical_sector):
        """Read a single logical sector from disk."""
        offset = self.logical_sector_to_offset(track, logical_sector)
        return bytes(self.data[offset:offset + self.SECTOR_SIZE])

    def write_sector(self, track, logical_sector, data):
        """Write a single logical sector to disk."""
        offset = self.logical_sector_to_offset(track, logical_sector)
        self.data[offset:offset + self.SECTOR_SIZE] = data[:self.SECTOR_SIZE]

    def read_block(self, block_num):
        """Read a block (8 consecutive logical sectors) from the data area.

        Block 0 starts at track BOOT_TRACKS (directory), blocks are numbered
        sequentially across tracks.
        """
        # Calculate starting track and sector for this block
        sectors_per_block = self.BLOCK_SIZE // self.SECTOR_SIZE  # 8
        logical_sector_num = block_num * sectors_per_block
        track = self.BOOT_TRACKS + (logical_sector_num // self.SECTORS_PER_TRACK)
        sector_in_track = logical_sector_num % self.SECTORS_PER_TRACK

        result = bytearray()
        for i in range(sectors_per_block):
            current_sector = sector_in_track + i
            current_track = track
            # Handle track crossing
            while current_sector >= self.SECTORS_PER_TRACK:
                current_sector -= self.SECTORS_PER_TRACK
                current_track += 1
            result.extend(self.read_sector(current_track, current_sector))
        return bytes(result)

    def write_block(self, block_num, data):
        """Write a block (8 consecutive logical sectors) to the data area."""
        sectors_per_block = self.BLOCK_SIZE // self.SECTOR_SIZE  # 8
        logical_sector_num = block_num * sectors_per_block
        track = self.BOOT_TRACKS + (logical_sector_num // self.SECTORS_PER_TRACK)
        sector_in_track = logical_sector_num % self.SECTORS_PER_TRACK

        # Pad data to block size if needed
        if len(data) < self.BLOCK_SIZE:
            data = data + bytes([0x1A] * (self.BLOCK_SIZE - len(data)))

        for i in range(sectors_per_block):
            current_sector = sector_in_track + i
            current_track = track
            while current_sector >= self.SECTORS_PER_TRACK:
                current_sector -= self.SECTORS_PER_TRACK
                current_track += 1
            sector_data = data[i * self.SECTOR_SIZE:(i + 1) * self.SECTOR_SIZE]
            self.write_sector(current_track, current_sector, sector_data)

    def read_dir_entry(self, entry_num):
        """Read a directory entry (32 bytes) by entry number."""
        # Directory entries are in blocks 0 and 1
        # Each block = 1024 bytes = 32 entries
        block_num = entry_num // 32
        offset_in_block = (entry_num % 32) * 32
        block_data = self.read_block(block_num)
        return bytes(block_data[offset_in_block:offset_in_block + 32])

    def write_dir_entry(self, entry_num, entry_data):
        """Write a directory entry (32 bytes) by entry number."""
        block_num = entry_num // 32
        offset_in_block = (entry_num % 32) * 32
        block_data = bytearray(self.read_block(block_num))
        block_data[offset_in_block:offset_in_block + 32] = entry_data[:32]
        self.write_block(block_num, bytes(block_data))

    def find_free_dir_entry(self):
        """Find first free directory entry (starts with 0xE5).

        Returns entry number (not byte offset).
        """
        for i in range(self.DIR_ENTRIES):
            entry = self.read_dir_entry(i)
            if entry[0] == 0xE5:
                return i
        return None

    def dir_entries(self):
        """Every directory entry, 32 bytes each, in order."""
        for i in range(self.DIR_ENTRIES):
            yield self.read_dir_entry(i)

    def get_used_blocks(self):
        """Every block number the directory points at, and 0-1, which are the
        directory itself."""
        used = {0, 1}
        for entry in self.dir_entries():
            if entry[0] != 0xE5:
                for j in range(16):
                    if entry[16 + j]:
                        used.add(entry[16 + j])
        return used

    def find_max_block(self):
        """Find highest used block number in directory.

        Returns the highest block number in use, or 1 if no files exist
        (blocks 0-1 are reserved for directory with 1KB blocks, 64 entries).
        """
        # Directory = 64 entries * 32 bytes = 2048 bytes = 2 blocks
        max_block = 1
        for i in range(self.DIR_ENTRIES):
            entry = self.read_dir_entry(i)
            if entry[0] != 0xE5:
                # 8-bit block pointers for SSSD (16 pointers per entry)
                for j in range(16):
                    block = entry[16 + j]
                    if block > max_block and block != 0:
                        max_block = block
        return max_block

    def add_file(self, filename, file_data, sys_attr=False, user=0):
        """Add a file to the disk image.

        Args:
            filename: Name of the file to add
            file_data: File contents as bytes
            sys_attr: If True, set the SYS attribute
            user: User number (0-15)
        """
        # Parse filename (8.3 format)
        name, ext = os.path.splitext(filename.upper())
        name = name[:8].ljust(8)
        ext = ext[1:4].ljust(3) if ext else '   '

        # Replace a file of the same name, as Hd1kDisk.add_file does.  This
        # did not, so a second add wrote a second extent 0 beside the first,
        # which `add` then refused in its verify step and wrote nothing.
        cpm_filename = (name.rstrip() + '.' + ext.rstrip()).rstrip('.')
        replacing = self._entries_named(cpm_filename, user)
        self.delete_file(cpm_filename, user, exact=True)

        num_records = (len(file_data) + 127) // 128
        blocks_needed = (num_records + self.RECORDS_PER_BLOCK - 1) // self.RECORDS_PER_BLOCK
        entries_needed = max(1, -(-blocks_needed // self.BLOCKS_PER_EXTENT))

        used = self.get_used_blocks()
        blocks = allocate_blocks(used, 2, self.MAX_BLOCK, blocks_needed)
        why = refusal(filename, cpm_filename, replacing, blocks, blocks_needed,
                      free_block_count(used, 2, self.MAX_BLOCK), entries_needed,
                      sum(1 for e in self.dir_entries() if e[0] == 0xE5))
        if why:
            for i in replacing:
                entry = bytearray(self.read_dir_entry(i))
                entry[0] = user
                self.write_dir_entry(i, entry)
            print(why)
            return False
        if replacing:
            print(f"  (replaced existing {cpm_filename})")
        next_block = blocks[0] if blocks else self.find_max_block() + 1

        sys_flag = " [SYS]" if sys_attr else ""
        user_flag = f" [U{user}]" if user != 0 else ""
        print(f"Adding {filename}{sys_flag}{user_flag}: {len(file_data)} bytes, {num_records} records, {blocks_needed} blocks starting at {next_block}")

        # Prepare extension bytes with optional SYS attribute
        ext_bytes = ext.encode('ascii')
        if sys_attr:
            ext_bytes = bytes([ext_bytes[0], ext_bytes[1] | 0x80, ext_bytes[2]])

        # Write file data to blocks first
        for i in range(blocks_needed):
            block_num = blocks[i]
            data_offset = i * self.BLOCK_SIZE
            chunk = file_data[data_offset:data_offset + self.BLOCK_SIZE]
            self.write_block(block_num, chunk)

        # Create directory entries
        # For SSSD: EXM=0, each extent = 128 records, 16 blocks max per entry.
        # An empty file still has its extent 0, RC 0 and no blocks, as the
        # BDOS's make leaves it.  It had none, so the file did not exist, and
        # adding an empty file over an existing one deleted it and said
        # "Successfully updated".
        extent_num = 0
        block_idx = 0

        while block_idx < blocks_needed or extent_num == 0:
            dir_entry_num = self.find_free_dir_entry()
            if dir_entry_num is None:
                print(f"No free directory entry for {filename} extent {extent_num}")
                return False

            entry = bytearray(32)
            entry[0] = user  # User number
            entry[1:9] = name.encode('ascii')
            entry[9:12] = ext_bytes

            # Get blocks for this extent (up to 16 for 8-bit pointers)
            extent_blocks = blocks[block_idx:block_idx + self.BLOCKS_PER_EXTENT]

            # Calculate record count for this extent
            if block_idx + len(extent_blocks) >= blocks_needed:
                # Last extent - calculate remaining records
                remaining_bytes = len(file_data) - (block_idx * self.BLOCK_SIZE)
                extent_records = (remaining_bytes + 127) // 128
            else:
                # Full extent
                extent_records = 128

            entry[12] = extent_num & 0x1F  # Extent low
            entry[13] = 0  # S1
            entry[14] = (extent_num >> 5) & 0x3F  # S2/EH
            entry[15] = min(extent_records, 128)

            # Store 8-bit block pointers
            for i, block in enumerate(extent_blocks):
                entry[16 + i] = block & 0xFF

            self.write_dir_entry(dir_entry_num, entry)

            block_idx += len(extent_blocks)
            extent_num += 1

        return True

    def list_files(self):
        """List all files in the directory."""
        files = {}
        for i in range(self.DIR_ENTRIES):
            entry = self.read_dir_entry(i)
            user = entry[0]
            if user != 0xE5 and user < 32:
                # Attribute bits off, from the name as well as the type: a
                # file with F1 set was skipped here as unprintable.
                fullname = entry_filename(entry)
                if fullname is None:
                    continue
                extent_lo = entry[12]
                extent_hi = entry[14]
                extent = extent_lo + (extent_hi << 5)
                records = entry[15]

                key = (user, fullname)

                if key not in files:
                    files[key] = {'extents': 0, 'records': 0, 'blocks': [],
                                  'attr_bits': 0}
                files[key]['attr_bits'] |= entry_attribute_bits(entry)

                files[key]['extents'] = max(files[key]['extents'], extent + 1)
                if extent == files[key]['extents'] - 1:
                    files[key]['records'] = extent * 128 + records

                # 8-bit block pointers
                for j in range(16):
                    block = entry[16 + j]
                    if block > 0:
                        files[key]['blocks'].append(block)

        for info in files.values():
            info['attrs'] = attribute_names(info.pop('attr_bits'))
        return files

    def _entries_named(self, filename, user):
        """The directory entries delete_file(filename, user, exact=True) would
        erase, by number."""
        want = filename_to_name83(filename, fold=False)
        return [i for i, e in enumerate(self.dir_entries())
                if e[0] == user and entry_name83(e) == want]

    def delete_file(self, filename, user=0, exact=False):
        """Delete a file from the disk image.

        Only byte 0 of each entry changes, so its attribute bits stay on disk
        as a real ERA leaves them.  exact=True takes the name's case as given
        (see lookup_name83 for the other).
        """
        want = (filename_to_name83(filename, fold=False) if exact
                else lookup_name83(self, filename, user))

        deleted_count = 0
        for i in range(self.DIR_ENTRIES):
            entry = self.read_dir_entry(i)
            entry_user = entry[0]
            if entry_user == user and entry_name83(entry) == want:
                # Mark entry as deleted
                deleted_entry = bytearray(entry)
                deleted_entry[0] = 0xE5
                self.write_dir_entry(i, deleted_entry)
                deleted_count += 1

        return deleted_count

    def extract_file(self, filename, user=0):
        """Extract a file from the disk image.

        Returns the file data as bytes, or None if not found.
        """
        want = lookup_name83(self, filename, user)

        # Collect all extents for this file
        extents = {}
        for i in range(self.DIR_ENTRIES):
            entry = self.read_dir_entry(i)
            entry_user = entry[0]
            if entry_user == user:
                if entry_name83(entry) == want:
                    extent_lo = entry[12]
                    extent_hi = entry[14]
                    extent_num = extent_lo + (extent_hi << 5)
                    records = entry[15]
                    blocks = []
                    # 8-bit block pointers
                    for j in range(16):
                        block = entry[16 + j]
                        if block > 0:
                            blocks.append(block)
                    extents[extent_num] = (records, blocks)

        if not extents:
            return None

        # Read data from blocks in extent order
        file_data = bytearray()
        for ext_num in sorted(extents.keys()):
            records, blocks = extents[ext_num]
            for block in blocks:
                file_data.extend(self.read_block(block))

        # Trim to actual size
        last_ext = max(extents.keys())
        total_records = last_ext * 128 + extents[last_ext][0]
        actual_size = total_records * 128
        return bytes(file_data[:actual_size])

    def read_boot_area(self):
        """Read the boot area (first 2 tracks) of this image or slice.

        Boot tracks have no sector skew applied - they are read sequentially.
        Returns 6656 bytes (2 tracks × 26 sectors × 128 bytes).
        """
        boot_size = self.BOOT_TRACKS * self.SECTORS_PER_TRACK * self.SECTOR_SIZE
        result = bytearray()
        for track in range(self.BOOT_TRACKS):
            for sector in range(self.SECTORS_PER_TRACK):
                result.extend(self.read_sector(track, sector))
        return bytes(result)

    def write_boot_area(self, data):
        """Write the boot area (first 2 tracks) of this image or slice.

        Boot tracks have no sector skew applied - they are written sequentially.
        Data is padded or truncated to exactly 6656 bytes.
        """
        boot_size = self.BOOT_TRACKS * self.SECTORS_PER_TRACK * self.SECTOR_SIZE
        # Pad or truncate to boot area size
        if len(data) < boot_size:
            data = data + bytes(boot_size - len(data))
        elif len(data) > boot_size:
            data = data[:boot_size]

        offset = 0
        for track in range(self.BOOT_TRACKS):
            for sector in range(self.SECTORS_PER_TRACK):
                sector_data = data[offset:offset + self.SECTOR_SIZE]
                self.write_sector(track, sector, sector_data)
                offset += self.SECTOR_SIZE


class Hd1kDisk:
    """Standard hd1k disk format (RomWBW compatible).

    Addressed from `base`, which is 0 for a plain image and the top of a slice
    for a combo (see ComboDisk).  A combo slice IS a plain hd1k image that
    happens to start further into the file, so one implementation serves both:
    every offset below is relative to `base` and nothing else differs.
    """

    SECTOR_SIZE = SECTOR_SIZE_HD  # 512 bytes
    SECTORS_PER_TRACK = 16
    TRACK_SIZE = SECTORS_PER_TRACK * SECTOR_SIZE_HD          # 8 KB
    DIR_ENTRIES = 1024
    BOOT_TRACKS = 2
    BOOT_SIZE = BOOT_TRACKS * TRACK_SIZE                     # 0x4000 (16 KB)
    SLICE_SIZE = HD1K_SINGLE_SIZE                            # 8 MB

    # Blocks that fit in the data area: 0..MAX_BLOCK.  Block MAX_BLOCK ends
    # exactly on the slice boundary, so nothing addressable here runs past the
    # end of a plain image or into the next slice of a combo.
    MAX_BLOCK = (SLICE_SIZE - BOOT_SIZE) // BLOCK_SIZE - 1    # 2043

    # The directory of a plain image, for callers that want the constant
    # without an instance.  An instance shadows it with its own base.
    DIR_START = BOOT_SIZE

    def __init__(self, disk_data, base=0):
        self.data = disk_data
        self.base = base
        self.DIR_START = base + self.BOOT_SIZE
        # Same value under the name ComboDisk used, so callers of either keep
        # working.
        self.dir_offset = self.DIR_START

    def find_free_dir_entry(self):
        """Find first free directory entry (starts with 0xE5)."""
        for i in range(self.DIR_ENTRIES):
            offset = self.DIR_START + (i * 32)
            if self.data[offset] == 0xE5:
                return offset
        return None

    def dir_entries(self):
        """Every directory entry, 32 bytes each, in order."""
        for i in range(self.DIR_ENTRIES):
            offset = self.DIR_START + (i * 32)
            yield bytes(self.data[offset:offset + 32])

    def get_used_blocks(self):
        """Every block number the directory currently points at."""
        used = set(range(8))          # 0-7 are the directory itself
        for i in range(self.DIR_ENTRIES):
            entry_offset = self.DIR_START + (i * 32)
            if self.data[entry_offset] == 0xE5:
                continue
            for j in range(8):
                ptr_offset = entry_offset + 16 + (j * 2)
                block = struct.unpack('<H', self.data[ptr_offset:ptr_offset + 2])[0]
                if block:
                    used.add(block)
        return used

    def find_free_block(self, used_blocks):
        """First free block, or -1.  Bounded by the disk, not by a constant."""
        for block in range(8, self.MAX_BLOCK + 1):
            if block not in used_blocks:
                return block
        return -1

    def find_max_block(self):
        """Find highest used block number in directory.

        Returns the highest block number in use, or 7 if no files exist
        (blocks 0-7 are reserved for directory).
        """
        # Blocks 0-7 are reserved for directory (32KB = 8 blocks at 4KB each)
        max_block = 7
        for i in range(self.DIR_ENTRIES):
            offset = self.DIR_START + (i * 32)
            if self.data[offset] != 0xE5:
                for j in range(8):
                    block = struct.unpack('<H', self.data[offset+16+j*2:offset+18+j*2])[0]
                    if block > max_block and block < 0xFFFF:
                        max_block = block
        return max_block

    def add_file(self, filename, file_data, sys_attr=False, user=0):
        """Add a file to the disk image.

        Args:
            filename: Name of the file to add
            file_data: File contents as bytes
            sys_attr: If True, set the SYS attribute (makes file visible from any user area)
            user: User number (0-15)
        """
        # Parse filename (8.3 format) - uppercase for CP/M compatibility
        name, ext = os.path.splitext(filename.upper())
        name = name[:8].ljust(8)
        ext = ext[1:4].ljust(3) if ext else '   '

        # Delete existing file with same name to avoid duplicates
        cpm_filename = (name.rstrip() + '.' + ext.rstrip()).rstrip('.')
        replacing = self._entries_named(cpm_filename, user)
        self.delete_file(cpm_filename, user, exact=True)

        num_records = (len(file_data) + 127) // 128
        records_per_block = BLOCK_SIZE // 128  # 32 records per 4KB block
        blocks_needed = (num_records + records_per_block - 1) // records_per_block
        entries_needed = max(1, -(-blocks_needed // 8))  # 8 block pointers an entry

        # Refuse rather than run off the end.  There was no bound here at all:
        # `cpm_disk.py add hd.img <9MB file>` on an 8 MB image printed
        # "Successfully updated" and left the file 9,486,336 bytes - a grown
        # image whose directory points past where the geometry says the disk
        # ends.  Slice-aware, so on a combo it stops at the slice boundary
        # rather than writing into the next slice.
        used = self.get_used_blocks()
        blocks = allocate_blocks(used, 8, self.MAX_BLOCK, blocks_needed)
        why = refusal(filename, cpm_filename, replacing, blocks, blocks_needed,
                      free_block_count(used, 8, self.MAX_BLOCK), entries_needed,
                      sum(1 for e in self.dir_entries() if e[0] == 0xE5))
        if why:
            for i in replacing:
                self.data[self.DIR_START + i * 32] = user
            print(why)
            return False
        if replacing:
            print(f"  (replaced existing {cpm_filename})")
        next_block = blocks[0] if blocks else self.find_max_block() + 1

        sys_flag = " [SYS]" if sys_attr else ""
        user_flag = f" [U{user}]" if user != 0 else ""
        print(f"Adding {filename}{sys_flag}{user_flag}: {len(file_data)} bytes, {num_records} records, {blocks_needed} blocks starting at {next_block}")

        # Prepare extension bytes with optional SYS attribute
        ext_bytes = ext.encode('ascii')
        if sys_attr:
            ext_bytes = bytes([ext_bytes[0], ext_bytes[1] | 0x80, ext_bytes[2]])

        # Write file data to blocks first
        for i in range(blocks_needed):
            block_num = blocks[i]
            block_offset = self.DIR_START + (block_num * BLOCK_SIZE)
            data_offset = i * BLOCK_SIZE
            chunk = file_data[data_offset:data_offset + BLOCK_SIZE]
            if len(chunk) < BLOCK_SIZE:
                # PAD WITH NUL, NOT ^Z.  The bytes past the last record are
                # undefined in CP/M - the directory entry's record count is
                # what bounds the file - so both are legal and neither is
                # visible to a guest. They are not equally useful to US:
                # cpmtools' cpmcp pads with 0x00, and every hd1k image RomWBW
                # and romwbw_disks have ever published was written with it.
                # Padding with 0x1A made a rebuilt image differ from its
                # published self in exactly those tail bytes - 4608 of them in
                # hd1k_combo - which turns a byte-for-byte reproducibility
                # check into a diff nobody can act on, and would have forced a
                # re-cut of two immutable release tags to adopt this tool.
                chunk = chunk + bytes([0x00] * (BLOCK_SIZE - len(chunk)))
            self.data[block_offset:block_offset + BLOCK_SIZE] = chunk

        # Create directory entries
        # For hd1k with 4KB blocks and DSM > 255: EXM=1
        # Each physical directory entry covers 2 logical extents = 256 records = 8 blocks
        # EL field contains the last logical extent number within this physical extent
        # RC field contains the record count in that last logical extent
        exm = 1  # EXM=1 for hd1k format (4KB blocks, DSM > 255)
        records_per_physical_extent = 128 * (exm + 1)  # 256 records
        blocks_per_physical_extent = 8  # 8 block pointers per directory entry
        physical_extent_num = 0
        block_idx = 0

        # At least one entry: an empty file is extent 0 with RC 0 and no
        # blocks (see SssdDisk.add_file).
        while block_idx < blocks_needed or physical_extent_num == 0:
            dir_offset = self.find_free_dir_entry()
            if dir_offset is None:
                print(f"No free directory entry for {filename} extent {physical_extent_num}")
                return False

            entry = bytearray(32)
            entry[0] = user  # User number
            entry[1:9] = name.encode('ascii')
            entry[9:12] = ext_bytes

            # Get blocks for this physical extent
            extent_blocks = blocks[block_idx:block_idx + blocks_per_physical_extent]

            # Calculate which logical extent this ends on and the record count
            records_before = block_idx * records_per_block
            records_in_extent = len(extent_blocks) * records_per_block
            records_covered = min(records_before + records_in_extent, num_records)
            records_in_last_logical = ((records_covered - 1) % 128) + 1 if records_covered > 0 else 0

            # Logical extent number within this physical extent (0 or 1 for EXM=1)
            if records_covered > records_before + 128:
                last_logical_extent = 1  # Extends into second logical extent
            else:
                last_logical_extent = 0

            # Full extent number = physical_extent * (exm+1) + last_logical_extent
            full_extent_num = physical_extent_num * (exm + 1) + last_logical_extent

            entry[12] = full_extent_num & 0x1F  # Extent low (bits 0-4)
            entry[13] = 0  # S1
            entry[14] = (full_extent_num >> 5) & 0x3F  # S2/EH (bits 5-10)
            entry[15] = records_in_last_logical  # RC = records in last logical extent

            # Store block pointers
            for i, block in enumerate(extent_blocks):
                struct.pack_into('<H', entry, 16 + i*2, block)

            self.data[dir_offset:dir_offset+32] = entry

            block_idx += len(extent_blocks)
            physical_extent_num += 1

        return True

    def list_files(self):
        """List all files in the directory."""
        files = {}
        for i in range(self.DIR_ENTRIES):
            offset = self.DIR_START + (i * 32)
            user = self.data[offset]
            if user != 0xE5 and user < 32:
                # Validate filename - printable ASCII once the attribute bits
                # are off, from the name as well as the type: a file with F1
                # set was skipped here as unprintable.
                entry = self.data[offset:offset + 32]
                fullname = entry_filename(entry)
                if fullname is None:
                    continue
                extent_lo = self.data[offset+12]
                extent_hi = self.data[offset+14]
                extent = extent_lo + (extent_hi << 5)
                records = self.data[offset+15]

                key = (user, fullname)

                if key not in files:
                    files[key] = {'extents': 0, 'records': 0, 'blocks': [],
                                  'attr_bits': 0}
                files[key]['attr_bits'] |= entry_attribute_bits(entry)

                files[key]['extents'] = max(files[key]['extents'], extent + 1)
                if extent == files[key]['extents'] - 1:
                    files[key]['records'] = extent * 128 + records

                for j in range(8):
                    block = struct.unpack('<H', self.data[offset+16+j*2:offset+18+j*2])[0]
                    if block > 0:
                        files[key]['blocks'].append(block)

        for info in files.values():
            info['attrs'] = attribute_names(info.pop('attr_bits'))
        return files

    def _entries_named(self, filename, user):
        """The directory entries delete_file(filename, user, exact=True) would
        erase, by number."""
        want = filename_to_name83(filename, fold=False)
        return [i for i, e in enumerate(self.dir_entries())
                if e[0] == user and entry_name83(e) == want]

    def delete_file(self, filename, user=0, exact=False):
        """Delete a file from the disk image by marking its directory entries as empty.

        Only byte 0 of each entry changes, so its attribute bits stay on disk
        as a real ERA leaves them.  exact=True takes the name's case as given
        (see lookup_name83 for the other).
        """
        want = (filename_to_name83(filename, fold=False) if exact
                else lookup_name83(self, filename, user))

        deleted_count = 0
        for i in range(self.DIR_ENTRIES):
            offset = self.DIR_START + (i * 32)
            entry_user = self.data[offset]
            if entry_user == user and entry_name83(self.data[offset:offset + 32]) == want:
                # Mark entry as deleted
                self.data[offset] = 0xE5
                deleted_count += 1

        return deleted_count

    def extract_file(self, filename, user=0):
        """Extract a file from the disk image.

        Returns the file data as bytes, or None if not found.
        """
        want = lookup_name83(self, filename, user)

        # Collect all extents for this file
        extents = {}  # extent_num -> (records, blocks)
        for i in range(self.DIR_ENTRIES):
            offset = self.DIR_START + (i * 32)
            entry_user = self.data[offset]
            if entry_user == user:
                if entry_name83(self.data[offset:offset + 32]) == want:
                    extent_lo = self.data[offset+12]
                    extent_hi = self.data[offset+14]
                    extent_num = extent_lo + (extent_hi << 5)
                    records = self.data[offset+15]
                    blocks = []
                    for j in range(8):
                        block = struct.unpack('<H', self.data[offset+16+j*2:offset+18+j*2])[0]
                        if block > 0:
                            blocks.append(block)
                    extents[extent_num] = (records, blocks)

        if not extents:
            return None

        # Read data from blocks in extent order
        file_data = bytearray()
        for ext_num in sorted(extents.keys()):
            records, blocks = extents[ext_num]
            for block in blocks:
                block_offset = self.DIR_START + (block * BLOCK_SIZE)
                file_data.extend(self.data[block_offset:block_offset + BLOCK_SIZE])

        # Trim to actual size based on last extent's record count
        last_ext = max(extents.keys())
        total_records = last_ext * 128 + extents[last_ext][0]
        actual_size = total_records * 128
        return bytes(file_data[:actual_size])

    def read_boot_area(self):
        """Read the boot area (first 2 tracks) from the disk.

        Returns 16384 bytes (2 tracks × 16 sectors × 512 bytes).
        No skew is applied to hard disk formats.
        """
        boot_size = self.BOOT_SIZE
        return bytes(self.data[self.base:self.base + boot_size])

    def write_boot_area(self, data):
        """Write data to the boot area (first 2 tracks) of the disk.

        Data is padded or truncated to exactly 16384 bytes.
        No skew is applied to hard disk formats.
        """
        boot_size = self.BOOT_SIZE
        # Pad or truncate to boot area size
        if len(data) < boot_size:
            data = data + bytes(boot_size - len(data))
        elif len(data) > boot_size:
            data = data[:boot_size]
        self.data[self.base:self.base + boot_size] = data


class SliceError(ValueError):
    """A bad --slice argument.

    Its own class because UnicodeDecodeError is a subclass of ValueError, and
    a blanket `except ValueError` in main() would have dressed a corrupt
    directory up as a bad flag.
    """


class ComboDisk(Hd1kDisk):
    """One slice of a combo image.

    A combo is a 1 MB MBR prefix followed by six 8 MB hd1k slices, so slice N
    IS a plain hd1k image that starts at PREFIX_SIZE + N*SLICE_SIZE.  That is
    the whole of the difference, and it is now the whole of this class.

    It used to be a second implementation of Hd1kDisk - directory scan, block
    allocation, extents, boot area, all written again - and the copy was worse
    than the original in three ways that took a while to find:

      - It addressed file data from PREFIX_SIZE and the directory from
        PREFIX_SIZE + the boot area, so every block was read and written 16384
        bytes early.  A listing looked perfect and `extract` returned a
        neighbouring file's bytes.
      - Its add_file wrote a single logical extent with RC capped at 128
        records, so any file over 16384 bytes was silently truncated.  The
        original handles multi-extent files correctly.
      - It exposed dir_offset but not DIR_START, so verify_disk() - which reads
        DIR_START by name off whatever it is handed - died with AttributeError
        on every combo.

    None of those were possible to have in one implementation, which is the
    argument for not having two.
    """

    PREFIX_SIZE = 1048576                 # 1 MB MBR prefix
    SLICES = HD1K_COMBO_SLICES            # 6

    # Slice 0's directory, for callers that want the constant without an
    # instance.  Every instance shadows it with its own slice.
    DIR_START = PREFIX_SIZE + Hd1kDisk.BOOT_SIZE

    def __init__(self, disk_data, slice_num=0):
        if not isinstance(slice_num, int) or isinstance(slice_num, bool):
            raise SliceError("slice must be an integer")
        if not 0 <= slice_num < self.SLICES:
            raise SliceError(
                "slice %d is out of range: a combo image has slices 0-%d"
                % (slice_num, self.SLICES - 1))
        need = self.PREFIX_SIZE + (slice_num + 1) * self.SLICE_SIZE
        if len(disk_data) < need:
            raise SliceError(
                "slice %d needs a %d byte image; this one is %d bytes"
                % (slice_num, need, len(disk_data)))

        super().__init__(disk_data,
                         base=self.PREFIX_SIZE + (slice_num * self.SLICE_SIZE))
        self.slice_num = slice_num
        # An alias for `base` under the name that says what it is here.
        self.slice_start = self.base


def cmd_create(args):
    """Create a new empty formatted disk image."""
    if os.path.exists(args.disk) and not args.force:
        print(f"Error: {args.disk} already exists (use --force to overwrite)")
        return 1

    # Determine format to create
    if getattr(args, 'sssd', False):
        disk_data = create_sssd_disk()
        size_desc = f"{len(disk_data) // 1024}KB SSSD (ibm-3740)"
        fmt = 'sssd'
    elif getattr(args, 'combo', False):
        disk_data = create_hd1k_disk(combo=True)
        size_desc = f"{len(disk_data) // 1048576}MB combo (6 slices)"
        fmt = 'combo'
    else:
        disk_data = create_hd1k_disk(combo=False)
        size_desc = f"{len(disk_data) // 1048576}MB hd1k"
        fmt = 'hd1k'

    # Verify the newly created disk
    disk = get_disk_object(disk_data)
    errors, warnings = verify_disk(disk, disk_data, fmt)
    if errors:
        print(f"VERIFY FAILED after create:")
        for e in errors:
            print(f"  {e}")
        return 1

    with open(args.disk, 'wb') as f:
        f.write(disk_data)

    print(f"Created {size_desc} disk: {args.disk}")
    return 0


def detect_disk_format(disk_data):
    """Auto-detect disk format based on size and signatures.

    Returns:
        'sssd' for ibm-3740 (8" SSSD floppy)
        'combo' for combo disk with MBR
        'hd1k' for standard hd1k
    """
    size = len(disk_data)

    # Check for SSSD (ibm-3740) format - ~256KB
    if size == SSSD_SIZE or (243000 < size < 260000):
        return 'sssd'

    # Check for combo disk - MBR signature and partition type
    if size >= HD1K_COMBO_SIZE:
        if disk_data[0x1FE] == 0x55 and disk_data[0x1FF] == 0xAA:
            if disk_data[0x1BE + 4] == 0x2E:  # RomWBW hd1k partition type
                return 'combo'

    # Default to hd1k for 8MB disks
    if size == HD1K_SINGLE_SIZE:
        return 'hd1k'

    # Fallback - assume hd1k for larger disks, sssd for smaller
    if size > 1000000:
        return 'hd1k'
    else:
        return 'sssd'


def get_disk_object(disk_data, format_hint=None, slice_num=0):
    """Get appropriate disk object for the format.

    Args:
        disk_data: The disk image data
        format_hint: Optional format override. Values:
            'sssd' - ibm-3740 with skew (default for 256KB disks)
            'sssd-noskew' - ibm-3740 without skew
            'hd1k' - standard hd1k format
            'combo' - combo disk with MBR
        slice_num: Which slice of a combo image to address (0-5).  A non-zero
            slice on any single-slice format is an error, not a no-op.

    Returns:
        Disk object (SssdDisk, Hd1kDisk, or ComboDisk)
    """
    if format_hint:
        fmt = format_hint
    else:
        fmt = detect_disk_format(disk_data)

    if slice_num and fmt != 'combo':
        # Above the dispatch, so it covers the SSSD branches as well - they
        # used to return before ever reaching the check.
        raise SliceError(
            "--slice %d: only a combo image has slices; this one is %s"
            % (slice_num, fmt))

    if fmt == 'sssd':
        return SssdDisk(disk_data, use_skew=True)
    elif fmt == 'sssd-noskew':
        return SssdDisk(disk_data, use_skew=False)
    elif fmt == 'combo':
        return ComboDisk(disk_data, slice_num=slice_num)
    else:
        return Hd1kDisk(disk_data)


def is_combo_disk(disk_data):
    """Auto-detect if disk is combo format by checking MBR signature and size."""
    return detect_disk_format(disk_data) == 'combo'


def get_format_hint(args, disk_data):
    """Determine format hint from args and disk data."""
    if getattr(args, 'sssd', False):
        return 'sssd-noskew' if getattr(args, 'no_skew', False) else 'sssd'
    elif getattr(args, 'combo', False):
        return 'combo'
    elif getattr(args, 'no_skew', False):
        # Auto-detect but force no-skew for SSSD
        if detect_disk_format(disk_data) == 'sssd':
            return 'sssd-noskew'
    return None


def get_slice(args):
    """Which slice of a combo image the command should act on (default 0)."""
    return getattr(args, 'slice', 0) or 0


def cmd_add(args):
    """Add files to a disk image."""
    with open(args.disk, 'rb') as f:
        disk_data = bytearray(f.read())

    fmt = detect_disk_format(disk_data)
    disk = get_disk_object(disk_data, get_format_hint(args, disk_data),
                            slice_num=get_slice(args))

    sys_attr = getattr(args, 'sys', False)
    user = getattr(args, 'user', 0)

    for filepath in args.files:
        filename = os.path.basename(filepath)
        with open(filepath, 'rb') as f:
            file_data = f.read()
        if not disk.add_file(filename, file_data, sys_attr=sys_attr, user=user):
            return 1

        # Verify after each add
        errors, warnings = verify_disk(disk, disk_data, fmt)
        if errors:
            print(f"VERIFY FAILED after adding {filename}:")
            for e in errors:
                print(f"  {e}")
            return 1

    with open(args.disk, 'wb') as f:
        f.write(disk_data)

    print(f"Successfully updated {args.disk}")
    return 0


def cmd_list(args):
    """List files in a disk image."""
    with open(args.disk, 'rb') as f:
        disk_data = bytearray(f.read())

    disk = get_disk_object(disk_data, get_format_hint(args, disk_data),
                            slice_num=get_slice(args))
    files = disk.list_files()

    if not files:
        print("No files found")
        return 0

    # Attributes go last, after the four columns this always printed:
    # romwbw_emu and romwbw_disks read the name as the second field of each
    # line after the first two, and that must not move.
    print(f"{'User':<5} {'Filename':<12} {'Size':>8} {'Blocks':>6}  Attributes")
    print("-" * 47)

    for (user, name), info in sorted(files.items()):
        size = info['records'] * 128
        blocks = len(info['blocks'])
        line = f"{user:<5} {name:<12} {size:>8} {blocks:>6}  {' '.join(info['attrs'])}"
        print(line.rstrip())

    return 0


def cmd_delete(args):
    """Delete files from a disk image."""
    with open(args.disk, 'rb') as f:
        disk_data = bytearray(f.read())

    fmt = detect_disk_format(disk_data)
    disk = get_disk_object(disk_data, get_format_hint(args, disk_data),
                            slice_num=get_slice(args))

    # Get list of all files
    files = disk.list_files()

    any_deleted = False
    def name_83(fullname):
        if '.' in fullname:
            name, ext = fullname.rsplit('.', 1)
        else:
            name, ext = fullname, ''
        return name.ljust(8) + ext.ljust(3)

    for pattern in args.files:
        pattern_83 = cpm_pattern_to_83(pattern)
        matched = False

        # The pattern is upper-cased.  Names that match it as they are come
        # first; only when none does are names compared with their case
        # folded, so `delete low.txt` reaches a lower-case name a program
        # made, and never takes it along with an upper-case LOW.TXT.  Each
        # is then deleted by its own name, case and all: this upper-cased the
        # listed name again, so `delete *.*` matched low.TXT and deleted
        # nothing.
        candidates = [k for k in files if cpm_match(pattern_83, name_83(k[1]))]
        if not candidates:
            candidates = [k for k in files if cpm_match(pattern_83, name_83(k[1]).upper())]

        for (user, fullname) in candidates:
            info = files[(user, fullname)]
            deleted = disk.delete_file(fullname, user, exact=True)
            if deleted > 0:
                attrs = f" [{' '.join(info['attrs'])}]" if info['attrs'] else ""
                print(f"Deleted {fullname}{attrs} ({deleted} extent(s))")
                any_deleted = True
                matched = True
                del files[(user, fullname)]

                # Verify after each delete
                errors, warnings = verify_disk(disk, disk_data, fmt)
                if errors:
                    print(f"VERIFY FAILED after deleting {fullname}:")
                    for e in errors:
                        print(f"  {e}")
                    return 1

        if not matched:
            print(f"No files matching: {pattern}")

    if any_deleted:
        with open(args.disk, 'wb') as f:
            f.write(disk_data)
        print(f"Successfully updated {args.disk}")

    return 0


def cmd_extract(args):
    """Extract files from a disk image."""
    with open(args.disk, 'rb') as f:
        disk_data = bytearray(f.read())

    disk = get_disk_object(disk_data, get_format_hint(args, disk_data),
                            slice_num=get_slice(args))

    user = getattr(args, 'user', 0)
    output_dir = getattr(args, 'output', '.')

    for filename in args.files:
        file_data = disk.extract_file(filename, user=user)
        if file_data is None:
            print(f"File not found: {filename}")
            return 1

        # Determine output path
        out_name = os.path.basename(filename).lower()
        out_path = os.path.join(output_dir, out_name)

        with open(out_path, 'wb') as f:
            f.write(file_data)
        print(f"Extracted {filename} -> {out_path} ({len(file_data)} bytes)")

    return 0


def cmd_read_boot(args):
    """Read boot area from disk image to a file."""
    with open(args.disk, 'rb') as f:
        disk_data = bytearray(f.read())

    disk = get_disk_object(disk_data, get_format_hint(args, disk_data),
                            slice_num=get_slice(args))
    boot_data = disk.read_boot_area()

    with open(args.output, 'wb') as f:
        f.write(boot_data)

    fmt = detect_disk_format(disk_data)
    print(f"Read {len(boot_data)} bytes boot area from {args.disk} ({fmt}) -> {args.output}")
    return 0


def cmd_write_boot(args):
    """Write boot area to disk image from a file."""
    with open(args.disk, 'rb') as f:
        disk_data = bytearray(f.read())

    disk = get_disk_object(disk_data, get_format_hint(args, disk_data),
                            slice_num=get_slice(args))
    fmt = detect_disk_format(disk_data)

    with open(args.input, 'rb') as f:
        file_data = f.read()

    sector_size = disk.SECTOR_SIZE
    total_boot_sectors = disk.BOOT_TRACKS * disk.SECTORS_PER_TRACK
    start_sector = getattr(args, 'sector', 0) or 0
    length_sectors = getattr(args, 'length', None)

    # Validate start sector
    if start_sector < 0 or start_sector >= total_boot_sectors:
        print(f"Error: sector {start_sector} out of range (0-{total_boot_sectors - 1})")
        return 1

    # Calculate how many sectors the file needs
    file_sectors = (len(file_data) + sector_size - 1) // sector_size

    # If length specified, pad or check bounds
    if length_sectors is not None:
        if length_sectors < file_sectors:
            print(f"Error: file needs {file_sectors} sectors but length is only {length_sectors}")
            return 1
        # Pad file to specified length
        target_size = length_sectors * sector_size
        file_data = file_data + bytes(target_size - len(file_data))
        file_sectors = length_sectors

    # Check if it fits in boot area
    if start_sector + file_sectors > total_boot_sectors:
        print(f"Error: {file_sectors} sector(s) at sector {start_sector} exceeds boot area ({total_boot_sectors} sectors)")
        return 1

    # Read current boot area, overlay file data, write back
    boot_area = bytearray(disk.read_boot_area())
    offset = start_sector * sector_size
    # Pad file_data to sector boundary
    if len(file_data) % sector_size != 0:
        file_data = file_data + bytes(sector_size - (len(file_data) % sector_size))
    boot_area[offset:offset + len(file_data)] = file_data
    disk.write_boot_area(bytes(boot_area))

    with open(args.disk, 'wb') as f:
        f.write(disk_data)

    if length_sectors is not None:
        print(f"Wrote {len(file_data)} bytes ({file_sectors} sectors) to sector {start_sector} of {args.disk} ({fmt})")
    else:
        print(f"Wrote {len(file_data)} bytes to sector {start_sector} of {args.disk} ({fmt})")
    return 0


def verify_disk(disk, disk_data, fmt):
    """Verify disk image consistency.

    Checks:
    1. Extent numbering: if extent N exists, extents 0..N-1 must exist
    2. Block allocation: no block used by more than one file
    3. Block range: all blocks within valid disk bounds

    Note: CP/M random I/O can create sparse files with holes (block ptr = 0),
    so zero block pointers in the middle of an extent are allowed.

    Returns:
        (errors, warnings) - lists of error/warning messages
    """
    errors = []
    warnings = []

    # Determine format-specific parameters
    if fmt == 'sssd':
        dir_entries = SSSD_DIR_ENTRIES
        block_size = SSSD_BLOCK_SIZE
        blocks_per_extent = 16  # 8-bit pointers
        pointer_size = 1
        # Calculate max block: (disk_size - boot_tracks) / block_size
        data_start = SSSD_BOOT_TRACKS * SSSD_SECTORS_PER_TRACK * SSSD_SECTOR_SIZE
        max_block = (len(disk_data) - data_start) // block_size
        dir_blocks = 2  # 64 entries * 32 bytes = 2KB = 2 blocks
    else:
        dir_entries = 1024
        block_size = BLOCK_SIZE
        blocks_per_extent = 8  # 16-bit pointers
        pointer_size = 2
        if fmt == 'combo':
            # A combo is ~51 MB but a block pointer only ever addresses one
            # 8 MB slice, so bounding by the whole file would let a pointer
            # that runs off the end of the slice pass as in range.
            #
            # +1 because max_block is used as a COUNT below - the test is
            # `block >= max_block` - while MAX_BLOCK is the last valid index.
            # The hd1k arm computes a count too.  Without this the last block
            # of every slice, which is exactly the one find_free_block hands
            # out on a full slice, is condemned by the tool's own verifier.
            max_block = ComboDisk.MAX_BLOCK + 1
        else:
            max_block = (len(disk_data) - 16384) // block_size  # After boot tracks
        dir_blocks = 8  # 1024 entries * 32 bytes = 32KB = 8 blocks

    # Collect all directory entries by file
    files = {}  # (user, name) -> {extent_num: (dir_entry_idx, blocks)}
    block_usage = {}  # block_num -> [(user, name, extent, entry_idx), ...]

    for i in range(dir_entries):
        if fmt == 'sssd':
            entry = disk.read_dir_entry(i)
        else:
            offset = disk.DIR_START + (i * 32)
            entry = bytes(disk_data[offset:offset + 32])

        user = entry[0]
        if user == 0xE5:
            continue  # Deleted entry - OK

        # Check for garbage in user byte
        if user >= 32:
            # Not deleted and not valid user - garbage
            errors.append(f"Entry {i}: Invalid user byte 0x{user:02X} (expected 0-31 or 0xE5)")
            continue

        # Parse filename (mask attribute bits)
        name_bytes = bytes([b & 0x7F for b in entry[1:9]])
        ext_bytes = bytes([b & 0x7F for b in entry[9:12]])

        # Check for non-printable characters in filename
        has_garbage = False
        for j, b in enumerate(name_bytes):
            if b != 0x20 and (b < 0x21 or b > 0x7E):
                errors.append(f"Entry {i}: Garbage char 0x{b:02X} at name position {j}")
                has_garbage = True
        for j, b in enumerate(ext_bytes):
            if b != 0x20 and (b < 0x21 or b > 0x7E):
                errors.append(f"Entry {i}: Garbage char 0x{b:02X} at ext position {j}")
                has_garbage = True

        if has_garbage:
            continue

        try:
            name = name_bytes.decode('ascii').rstrip()
            ext = ext_bytes.decode('ascii').rstrip()
        except UnicodeDecodeError:
            errors.append(f"Entry {i}: Invalid filename bytes")
            continue

        fullname = f"{name}.{ext}" if ext else name
        key = (user, fullname)

        # Parse extent number
        extent_lo = entry[12] & 0x1F
        extent_hi = entry[14] & 0x3F
        extent_num = extent_lo + (extent_hi << 5)

        # Collect blocks from this entry
        blocks = []
        for j in range(blocks_per_extent):
            if pointer_size == 1:
                block = entry[16 + j]
            else:
                block = struct.unpack('<H', entry[16 + j*2:18 + j*2])[0]

            if block > 0:
                blocks.append(block)

                # Track block usage
                if block not in block_usage:
                    block_usage[block] = []
                block_usage[block].append((user, fullname, extent_num, i))

                # Check block range
                if block >= max_block:
                    errors.append(f"U{user}:{fullname} extent {extent_num}: "
                                  f"block {block} exceeds max ({max_block})")

        # Store extent info
        if key not in files:
            files[key] = {}
        if extent_num in files[key]:
            errors.append(f"U{user}:{fullname}: duplicate extent {extent_num} "
                          f"(entries {files[key][extent_num][0]} and {i})")
        files[key][extent_num] = (i, blocks)

    # Check extent numbering
    # For EXM=0: extents are 0, 1, 2, 3, ... (each entry = 1 logical extent)
    # For EXM=1: extents are 0 or 1, 2 or 3, 4 or 5, ... (each entry = 2 logical extents)
    # The extent number is the LAST logical extent in that physical extent
    # So with EXM=1, extent 1 covers logical extents 0-1, extent 3 covers 2-3, etc.
    exm = 1 if fmt != 'sssd' else 0  # hd1k has EXM=1, SSSD has EXM=0

    for (user, fullname), extents in files.items():
        if not extents:
            continue

        # Group extents by physical extent
        # Physical extent N covers logical extents N*(exm+1) to N*(exm+1)+exm
        physical_extents = set()
        for ext_num in extents.keys():
            phys_ext = ext_num // (exm + 1)
            physical_extents.add(phys_ext)

        # Check that physical extents are contiguous from 0
        if physical_extents:
            max_phys = max(physical_extents)
            for n in range(max_phys):
                if n not in physical_extents:
                    errors.append(f"U{user}:{fullname}: missing physical extent {n} "
                                  f"(has physical extent {max_phys})")

    # Check for blocks used by multiple files
    for block, usages in block_usage.items():
        if len(usages) > 1:
            usage_strs = [f"U{u}:{f} ext{e}" for u, f, e, _ in usages]
            errors.append(f"Block {block} used by multiple files: {', '.join(usage_strs)}")

        # Check if block is in directory area (reserved)
        if block < dir_blocks:
            for u, f, e, entry_idx in usages:
                errors.append(f"U{u}:{f} extent {e}: block {block} overlaps directory area")

    return errors, warnings


def cmd_verify(args):
    """Verify disk image consistency."""
    with open(args.disk, 'rb') as f:
        disk_data = bytearray(f.read())

    fmt = detect_disk_format(disk_data)
    disk = get_disk_object(disk_data, get_format_hint(args, disk_data),
                            slice_num=get_slice(args))

    print(f"Verifying {args.disk} ({fmt} format, {len(disk_data)} bytes)")

    errors, warnings = verify_disk(disk, disk_data, fmt)

    # Also list file count and block usage
    files = disk.list_files()
    total_files = len(files)
    total_blocks = sum(len(info['blocks']) for info in files.values())

    print(f"Files: {total_files}, Blocks used: {total_blocks}")

    if warnings:
        print(f"\nWarnings ({len(warnings)}):")
        for w in warnings:
            print(f"  {w}")

    if errors:
        print(f"\nErrors ({len(errors)}):")
        for e in errors:
            print(f"  {e}")
        return 1

    print("\nDisk image OK")
    return 0


def main():
    parser = argparse.ArgumentParser(
        description='CP/M disk image utility',
        formatter_class=argparse.RawDescriptionHelpFormatter,
        epilog=__doc__
    )

    subparsers = parser.add_subparsers(dest='command', required=True)

    # Create command
    create_parser = subparsers.add_parser('create', help='Create new empty disk image')
    create_group = create_parser.add_mutually_exclusive_group()
    create_group.add_argument('--sssd', action='store_true',
                              help='Create SSSD (ibm-3740) format (250KB)')
    create_group.add_argument('--combo', action='store_true',
                              help='Create combo format (51MB) instead of single hd1k (8MB)')
    create_parser.add_argument('--no-skew', action='store_true',
                               help='SSSD: create without sector interleave (for emulators)')
    create_parser.add_argument('--force', '-f', action='store_true',
                               help='Overwrite existing file')
    create_parser.add_argument('disk', help='Disk image file to create')
    create_parser.set_defaults(func=cmd_create)

    # Add command
    add_parser = subparsers.add_parser('add', help='Add files to disk image')
    add_format = add_parser.add_mutually_exclusive_group()
    add_format.add_argument('--sssd', action='store_true',
                            help='Disk is SSSD (ibm-3740) format')
    add_format.add_argument('--combo', action='store_true',
                            help='Disk is combo format (1MB prefix)')
    add_parser.add_argument('--no-skew', action='store_true',
                           help='Disable sector skew (SSSD only)')
    add_parser.add_argument('--slice', type=int, default=0, metavar='N',
                            help='Slice of a combo image to act on (0-5, default 0)')
    add_parser.add_argument('--sys', '-s', action='store_true',
                           help='Set SYS attribute on files (makes visible from any user area)')
    add_parser.add_argument('--user', '-u', type=int, default=0,
                           help='User number for files (0-15, default 0)')
    add_parser.add_argument('disk', help='Disk image file')
    add_parser.add_argument('files', nargs='+', help='Files to add')
    add_parser.set_defaults(func=cmd_add)

    # List command
    list_parser = subparsers.add_parser('list', help='List files in disk image')
    list_format = list_parser.add_mutually_exclusive_group()
    list_format.add_argument('--sssd', action='store_true',
                             help='Disk is SSSD (ibm-3740) format')
    list_format.add_argument('--combo', action='store_true',
                             help='Disk is combo format (1MB prefix)')
    list_parser.add_argument('--no-skew', action='store_true',
                             help='Disable sector skew (SSSD only)')
    list_parser.add_argument('--slice', type=int, default=0, metavar='N',
                             help='Slice of a combo image to act on (0-5, default 0)')
    list_parser.add_argument('disk', help='Disk image file')
    list_parser.set_defaults(func=cmd_list)

    # Delete command
    delete_parser = subparsers.add_parser('delete', help='Delete files from disk image')
    delete_format = delete_parser.add_mutually_exclusive_group()
    delete_format.add_argument('--sssd', action='store_true',
                               help='Disk is SSSD (ibm-3740) format')
    delete_format.add_argument('--combo', action='store_true',
                               help='Disk is combo format (1MB prefix)')
    delete_parser.add_argument('--no-skew', action='store_true',
                               help='Disable sector skew (SSSD only)')
    delete_parser.add_argument('--slice', type=int, default=0, metavar='N',
                               help='Slice of a combo image to act on (0-5, default 0)')
    delete_parser.add_argument('disk', help='Disk image file')
    delete_parser.add_argument('files', nargs='+', help='Files to delete')
    delete_parser.set_defaults(func=cmd_delete)

    # Extract command
    extract_parser = subparsers.add_parser('extract', help='Extract files from disk image')
    extract_format = extract_parser.add_mutually_exclusive_group()
    extract_format.add_argument('--sssd', action='store_true',
                                help='Disk is SSSD (ibm-3740) format')
    extract_format.add_argument('--combo', action='store_true',
                                help='Disk is combo format (1MB prefix)')
    extract_parser.add_argument('--no-skew', action='store_true',
                                help='Disable sector skew (SSSD only)')
    extract_parser.add_argument('--slice', type=int, default=0, metavar='N',
                                help='Slice of a combo image to act on (0-5, default 0)')
    extract_parser.add_argument('--user', '-u', type=int, default=0,
                                help='User number to extract from (0-15, default 0)')
    extract_parser.add_argument('--output', '-o', default='.',
                                help='Output directory (default: current directory)')
    extract_parser.add_argument('disk', help='Disk image file')
    extract_parser.add_argument('files', nargs='+', help='Files to extract')
    extract_parser.set_defaults(func=cmd_extract)

    # Read-boot command
    read_boot_parser = subparsers.add_parser('read-boot', help='Read boot area from disk image')
    read_boot_format = read_boot_parser.add_mutually_exclusive_group()
    read_boot_format.add_argument('--sssd', action='store_true',
                                  help='Disk is SSSD (ibm-3740) format')
    read_boot_format.add_argument('--combo', action='store_true',
                                  help='Disk is combo format (1MB prefix)')
    read_boot_parser.add_argument('--no-skew', action='store_true',
                                  help='Disable sector skew (SSSD only)')
    read_boot_parser.add_argument('--slice', type=int, default=0, metavar='N',
                                  help='Slice of a combo image to act on (0-5, default 0)')
    read_boot_parser.add_argument('disk', help='Disk image file')
    read_boot_parser.add_argument('output', help='Output file for boot area')
    read_boot_parser.set_defaults(func=cmd_read_boot)

    # Write-boot command
    write_boot_parser = subparsers.add_parser('write-boot', help='Write boot area to disk image')
    write_boot_format = write_boot_parser.add_mutually_exclusive_group()
    write_boot_format.add_argument('--sssd', action='store_true',
                                   help='Disk is SSSD (ibm-3740) format')
    write_boot_format.add_argument('--combo', action='store_true',
                                   help='Disk is combo format (1MB prefix)')
    write_boot_parser.add_argument('--no-skew', action='store_true',
                                   help='Disable sector skew (SSSD only)')
    write_boot_parser.add_argument('--slice', type=int, default=0, metavar='N',
                                   help='Slice of a combo image to act on (0-5, default 0)')
    write_boot_parser.add_argument('disk', help='Disk image file')
    write_boot_parser.add_argument('input', help='Input file containing boot area data')
    write_boot_parser.add_argument('sector', nargs='?', type=int, default=0,
                                   help='Starting sector in boot area (default: 0)')
    write_boot_parser.add_argument('length', nargs='?', type=int, default=None,
                                   help='Length in sectors (pads with zeros if file is shorter)')
    write_boot_parser.set_defaults(func=cmd_write_boot)

    # Verify command
    verify_parser = subparsers.add_parser('verify', help='Verify disk image consistency')
    verify_format = verify_parser.add_mutually_exclusive_group()
    verify_format.add_argument('--sssd', action='store_true',
                               help='Disk is SSSD (ibm-3740) format')
    verify_format.add_argument('--combo', action='store_true',
                               help='Disk is combo format (1MB prefix)')
    verify_parser.add_argument('--no-skew', action='store_true',
                               help='Disable sector skew (SSSD only)')
    verify_parser.add_argument('--slice', type=int, default=0, metavar='N',
                               help='Slice of a combo image to act on (0-5, default 0)')
    verify_parser.add_argument('disk', help='Disk image file')
    verify_parser.set_defaults(func=cmd_verify)

    if len(sys.argv) == 1:
        parser.print_help()
        return 0

    args = parser.parse_args()
    try:
        return args.func(args)
    except SliceError as e:
        # Narrow on purpose: UnicodeDecodeError is a ValueError, so catching
        # ValueError here would report a corrupt directory as a bad flag.
        print("cpm_disk.py: error: %s" % e, file=sys.stderr)
        return 2


if __name__ == '__main__':
    sys.exit(main())
