miniximg 1.1.0

A library for creating, inspecting, and editing Minix v1 filesystem images
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
//! On-disk Minix layout types and byte encoding helpers.

use std::mem::size_of;

use crate::error::{MinixError, Result};

/// The Minix filesystem logical block size used by the current kernel.
pub const BLOCK_SIZE: usize = 1024;

/// The maximum file-name length stored in one directory entry.
pub const MINIX_NAME_LENGTH: usize = 14;

/// The Minix v1 magic number expected by the current kernel.
pub const MINIX_SUPER_MAGIC: u16 = 0x137F;

/// The root inode number.
pub const ROOT_INODE_NUMBER: u16 = 1;

/// The number of direct data-zone pointers stored in one inode.
pub const DIRECT_ZONE_COUNT: usize = 7;

/// The number of 16-bit entries that fit in one indirect block.
pub const INDIRECT_ENTRY_COUNT: usize = BLOCK_SIZE / size_of::<u16>();

/// The number of bytes occupied by one serialized inode.
pub const DISK_INODE_SIZE: usize = 32;

/// The number of bytes occupied by one serialized super block.
pub const DISK_SUPER_BLOCK_SIZE: usize = 20;

/// The number of bytes occupied by one serialized directory entry.
pub const DIRECTORY_ENTRY_SIZE: usize = 16;

/// The number of inodes that fit in one filesystem block.
pub const INODES_PER_BLOCK: usize = BLOCK_SIZE / DISK_INODE_SIZE;

/// The maximum logical block count reachable through direct and indirect zones.
pub const MAX_LOGICAL_BLOCKS: usize =
    DIRECT_ZONE_COUNT + INDIRECT_ENTRY_COUNT + INDIRECT_ENTRY_COUNT * INDIRECT_ENTRY_COUNT;

/// The maximum representable file size under the current addressing scheme.
pub const MAX_FILE_SIZE: u32 = (MAX_LOGICAL_BLOCKS * BLOCK_SIZE) as u32;

/// The semantically decoded inode type.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum InodeType {
    /// Ordinary file data stored in data zones.
    Regular,
    /// A directory containing name-to-inode mappings.
    Directory,
    /// A FIFO special file.
    Fifo,
    /// A block-device special file.
    BlockDevice,
    /// A character-device special file.
    CharacterDevice,
}

/// A small wrapper around the low permission bits stored in one inode mode.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct InodeModeFlags(pub u16);

/// A semantically typed inode mode wrapper.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[repr(transparent)]
pub struct InodeMode(pub u16);

/// The on-disk Minix super block.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[repr(C)]
pub struct DiskSuperBlock {
    /// The number of usable inode slots.
    pub inode_count: u16,
    /// The total zone count recorded by the image.
    pub zone_count: u16,
    /// The number of inode bitmap blocks.
    pub inode_bitmap_block_count: u16,
    /// The number of zone bitmap blocks.
    pub zone_bitmap_block_count: u16,
    /// The first data zone number.
    pub first_data_zone: u16,
    /// The log2 multiplier for zone size. The current kernel requires zero.
    pub log_zone_size: u16,
    /// The maximum representable file size.
    pub max_file_size: u32,
    /// The filesystem magic number.
    pub magic: u16,
}

/// The on-disk Minix inode.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[repr(C)]
pub struct DiskInode {
    /// The type and permission bits.
    pub mode: InodeMode,
    /// The owning user ID.
    pub user_id: u16,
    /// The file size in bytes.
    pub size: u32,
    /// The modification timestamp.
    pub modification_time: u32,
    /// The owning group ID.
    pub group_id: u8,
    /// The link count.
    pub link_count: u8,
    /// The direct data zones.
    pub direct_zones: [u16; DIRECT_ZONE_COUNT],
    /// The single-indirect zone pointer.
    pub single_indirect_zone: u16,
    /// The double-indirect zone pointer.
    pub double_indirect_zone: u16,
}

/// The on-disk Minix directory entry.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[repr(C)]
pub struct DiskDirectoryEntry {
    /// The referenced inode number.
    pub inode_number: u16,
    /// The fixed-size file name field.
    pub name: [u8; MINIX_NAME_LENGTH],
}

impl InodeModeFlags {
    /// The mask that selects permission and special bits.
    pub const MASK: u16 = 0o007777;
}

impl InodeMode {
    /// The mask that selects the inode type field.
    pub const TYPE_MASK: u16 = 0o170000;

    /// The mask that selects the permission and special bits.
    pub const FLAGS_MASK: u16 = InodeModeFlags::MASK;

    /// Build a regular-file mode word from permission bits.
    pub const fn regular(permissions: u16) -> Self {
        Self(0o100000 | (permissions & Self::FLAGS_MASK))
    }

    /// Build a directory mode word from permission bits.
    pub const fn directory(permissions: u16) -> Self {
        Self(0o040000 | (permissions & Self::FLAGS_MASK))
    }

    /// Return the stored inode type if it is recognized.
    pub const fn file_type(self) -> Option<InodeType> {
        match self.0 & Self::TYPE_MASK {
            0o100000 => Some(InodeType::Regular),
            0o040000 => Some(InodeType::Directory),
            0o010000 => Some(InodeType::Fifo),
            0o060000 => Some(InodeType::BlockDevice),
            0o020000 => Some(InodeType::CharacterDevice),
            _ => None,
        }
    }

    /// Return the permission and special bits without the type field.
    pub const fn flags(self) -> InodeModeFlags {
        InodeModeFlags(self.0 & Self::FLAGS_MASK)
    }
}

impl DiskSuperBlock {
    /// Decode one super block from the beginning of a block buffer.
    pub fn decode(block: &[u8; BLOCK_SIZE]) -> Result<Self> {
        let bytes = &block[..DISK_SUPER_BLOCK_SIZE];

        Ok(Self {
            inode_count: read_u16(bytes, 0)?,
            zone_count: read_u16(bytes, 2)?,
            inode_bitmap_block_count: read_u16(bytes, 4)?,
            zone_bitmap_block_count: read_u16(bytes, 6)?,
            first_data_zone: read_u16(bytes, 8)?,
            log_zone_size: read_u16(bytes, 10)?,
            max_file_size: read_u32(bytes, 12)?,
            magic: read_u16(bytes, 16)?,
        })
    }

    /// Encode the super block into the beginning of a block buffer.
    pub fn encode(self, block: &mut [u8; BLOCK_SIZE]) {
        write_u16(block, 0, self.inode_count);
        write_u16(block, 2, self.zone_count);
        write_u16(block, 4, self.inode_bitmap_block_count);
        write_u16(block, 6, self.zone_bitmap_block_count);
        write_u16(block, 8, self.first_data_zone);
        write_u16(block, 10, self.log_zone_size);
        write_u32(block, 12, self.max_file_size);
        write_u16(block, 16, self.magic);
    }
}

impl DiskInode {
    /// Return an all-zero inode suitable for a fresh inode slot.
    pub const fn zeroed() -> Self {
        Self {
            mode: InodeMode(0),
            user_id: 0,
            size: 0,
            modification_time: 0,
            group_id: 0,
            link_count: 0,
            direct_zones: [0; DIRECT_ZONE_COUNT],
            single_indirect_zone: 0,
            double_indirect_zone: 0,
        }
    }

    /// Return whether the inode slot is fully zero-filled.
    pub const fn is_zeroed(&self) -> bool {
        self.mode.0 == 0
            && self.user_id == 0
            && self.size == 0
            && self.modification_time == 0
            && self.group_id == 0
            && self.link_count == 0
            && self.direct_zones[0] == 0
            && self.direct_zones[1] == 0
            && self.direct_zones[2] == 0
            && self.direct_zones[3] == 0
            && self.direct_zones[4] == 0
            && self.direct_zones[5] == 0
            && self.direct_zones[6] == 0
            && self.single_indirect_zone == 0
            && self.double_indirect_zone == 0
    }

    /// Decode one inode from a 32-byte inode slot.
    pub fn decode(bytes: &[u8]) -> Result<Self> {
        if bytes.len() < DISK_INODE_SIZE {
            return Err(MinixError::Corrupt(
                "inode slot is shorter than the Minix inode size".into(),
            ));
        }

        Ok(Self {
            mode: InodeMode(read_u16(bytes, 0)?),
            user_id: read_u16(bytes, 2)?,
            size: read_u32(bytes, 4)?,
            modification_time: read_u32(bytes, 8)?,
            group_id: bytes[12],
            link_count: bytes[13],
            direct_zones: [
                read_u16(bytes, 14)?,
                read_u16(bytes, 16)?,
                read_u16(bytes, 18)?,
                read_u16(bytes, 20)?,
                read_u16(bytes, 22)?,
                read_u16(bytes, 24)?,
                read_u16(bytes, 26)?,
            ],
            single_indirect_zone: read_u16(bytes, 28)?,
            double_indirect_zone: read_u16(bytes, 30)?,
        })
    }

    /// Encode one inode into a 32-byte inode slot.
    pub fn encode(self, bytes: &mut [u8]) {
        write_u16(bytes, 0, self.mode.0);
        write_u16(bytes, 2, self.user_id);
        write_u32(bytes, 4, self.size);
        write_u32(bytes, 8, self.modification_time);
        bytes[12] = self.group_id;
        bytes[13] = self.link_count;
        write_u16(bytes, 14, self.direct_zones[0]);
        write_u16(bytes, 16, self.direct_zones[1]);
        write_u16(bytes, 18, self.direct_zones[2]);
        write_u16(bytes, 20, self.direct_zones[3]);
        write_u16(bytes, 22, self.direct_zones[4]);
        write_u16(bytes, 24, self.direct_zones[5]);
        write_u16(bytes, 26, self.direct_zones[6]);
        write_u16(bytes, 28, self.single_indirect_zone);
        write_u16(bytes, 30, self.double_indirect_zone);
    }
}

impl DiskDirectoryEntry {
    /// Return an empty directory entry.
    pub const fn empty() -> Self {
        Self {
            inode_number: 0,
            name: [0; MINIX_NAME_LENGTH],
        }
    }

    /// Build one directory entry from a validated file name.
    pub fn new(name: &str, inode_number: u16) -> Result<Self> {
        if name.len() > MINIX_NAME_LENGTH {
            return Err(MinixError::NameTooLong {
                name: name.into(),
                max_bytes: MINIX_NAME_LENGTH,
            });
        }

        let mut bytes = [0_u8; MINIX_NAME_LENGTH];
        bytes[..name.len()].copy_from_slice(name.as_bytes());

        Ok(Self {
            inode_number,
            name: bytes,
        })
    }

    /// Decode one directory entry from a 16-byte slot.
    pub fn decode(bytes: &[u8]) -> Result<Self> {
        if bytes.len() < DIRECTORY_ENTRY_SIZE {
            return Err(MinixError::Corrupt(
                "directory entry slot is shorter than the Minix directory-entry size".into(),
            ));
        }

        let mut name = [0_u8; MINIX_NAME_LENGTH];
        name.copy_from_slice(&bytes[2..DIRECTORY_ENTRY_SIZE]);

        Ok(Self {
            inode_number: read_u16(bytes, 0)?,
            name,
        })
    }

    /// Encode one directory entry into a 16-byte slot.
    pub fn encode(self, bytes: &mut [u8]) {
        write_u16(bytes, 0, self.inode_number);
        bytes[2..DIRECTORY_ENTRY_SIZE].copy_from_slice(&self.name);
    }

    /// Return the file name without trailing NUL bytes.
    pub fn name(&self) -> Result<String> {
        let len = self
            .name
            .iter()
            .position(|byte| *byte == 0)
            .unwrap_or(MINIX_NAME_LENGTH);

        let raw = &self.name[..len];
        let value = std::str::from_utf8(raw).map_err(|_| {
            MinixError::Corrupt("directory entry contains non-UTF-8 file-name bytes".into())
        })?;

        Ok(value.into())
    }
}

/// Read one little-endian `u16` from a byte slice.
fn read_u16(bytes: &[u8], offset: usize) -> Result<u16> {
    let slice = bytes.get(offset..offset + 2).ok_or_else(|| {
        MinixError::Corrupt("truncated little-endian u16 field in on-disk structure".into())
    })?;
    Ok(u16::from_le_bytes([slice[0], slice[1]]))
}

/// Read one little-endian `u32` from a byte slice.
fn read_u32(bytes: &[u8], offset: usize) -> Result<u32> {
    let slice = bytes.get(offset..offset + 4).ok_or_else(|| {
        MinixError::Corrupt("truncated little-endian u32 field in on-disk structure".into())
    })?;
    Ok(u32::from_le_bytes([slice[0], slice[1], slice[2], slice[3]]))
}

/// Write one little-endian `u16` into a byte slice.
fn write_u16(bytes: &mut [u8], offset: usize, value: u16) {
    bytes[offset..offset + 2].copy_from_slice(&value.to_le_bytes());
}

/// Write one little-endian `u32` into a byte slice.
fn write_u32(bytes: &mut [u8], offset: usize, value: u32) {
    bytes[offset..offset + 4].copy_from_slice(&value.to_le_bytes());
}

#[cfg(test)]
mod tests {
    //! Layout-focused unit tests.

    use std::mem::size_of;

    use super::*;

    /// Confirm the `repr(C)` layout matches the serialized Minix inode width.
    #[test]
    fn disk_inode_size_matches_minix_layout() {
        assert_eq!(size_of::<DiskInode>(), DISK_INODE_SIZE);
    }

    /// Confirm the `repr(C)` layout matches the serialized Minix super block width.
    #[test]
    fn disk_super_block_size_matches_minix_layout() {
        assert_eq!(size_of::<DiskSuperBlock>(), DISK_SUPER_BLOCK_SIZE);
    }

    /// Confirm the `repr(C)` layout matches the serialized Minix directory-entry width.
    #[test]
    fn directory_entry_size_matches_minix_layout() {
        assert_eq!(size_of::<DiskDirectoryEntry>(), DIRECTORY_ENTRY_SIZE);
    }

    /// Confirm directory entries reject names longer than fourteen bytes.
    #[test]
    fn directory_entry_rejects_long_names() {
        let error = DiskDirectoryEntry::new("123456789012345", 1).unwrap_err();
        assert!(matches!(error, MinixError::NameTooLong { .. }));
    }
}