Skip to content

Commit 2409760

Browse files
author
Felix "xq" Queißner
committed
Makes MBR partition types use name lookup + numeric option instead of fixed enum.
1 parent 6036cd4 commit 2409760

File tree

3 files changed

+63
-60
lines changed

3 files changed

+63
-60
lines changed

README.md

+28-8
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,28 @@ This tool is incredibly valuable when implementing your own operating system, em
88

99
## Example
1010

11-
```plain
12-
11+
```rb
12+
mbr-part
13+
bootloader paste-file "./syslinux.bin"
14+
part # partition 1
15+
type fat16-lba
16+
size 25M
17+
contains vfat fat16
18+
label "BOOT"
19+
copy-dir "/syslinux" "./bootfs/syslinux"
20+
endfat
21+
endpart
22+
part # partition 2
23+
type fat32-lba
24+
contains vfat fat32
25+
label "OS"
26+
mkdir "/home/dimmer"
27+
copy-file "/home/dimmer/.config/dimmer.cfg" "./dimmer.cfg"
28+
!include "./rootfs/files.dis"
29+
endfat
30+
endpart
31+
ignore # partition 3
32+
ignore # partition 4
1333
```
1434

1535
## Available Content Types
@@ -77,12 +97,12 @@ The `mbr-part` component will end after all 4 partitions are specified.
7797
| `empty` | 0x00 | No content |
7898
| `fat12` | 0x01 | [FAT12](https://en.wikipedia.org/wiki/FAT12) |
7999
| `ntfs` | 0x07 | [NTFS](https://en.wikipedia.org/wiki/NTFS) |
80-
| `fat32_chs` | 0x0B | [FAT32](https://en.wikipedia.org/wiki/FAT32) with [CHS](https://en.wikipedia.org/wiki/Cylinder-head-sector) addressing |
81-
| `fat32_lba` | 0x0C | [FAT32](https://en.wikipedia.org/wiki/FAT32) with [LBA](https://en.wikipedia.org/wiki/Logical_block_addressing) addressing |
82-
| `fat16_lba` | 0x0E | [FAT16B](https://en.wikipedia.org/wiki/File_Allocation_Table#FAT16B) with [LBA](https://en.wikipedia.org/wiki/Logical_block_addressing) addressing |
83-
| `linux_swap` | 0x82 | [Linux swap space](https://en.wikipedia.org/wiki/Swap_space#Linux) |
84-
| `linux_fs` | 0x83 | Any [Linux file system](https://en.wikipedia.org/wiki/File_system#Linux) |
85-
| `linux_lvm` | 0x8E | [Linux LVM](https://en.wikipedia.org/wiki/Logical_Volume_Manager_(Linux)) |
100+
| `fat32-chs` | 0x0B | [FAT32](https://en.wikipedia.org/wiki/FAT32) with [CHS](https://en.wikipedia.org/wiki/Cylinder-head-sector) addressing |
101+
| `fat32-lba` | 0x0C | [FAT32](https://en.wikipedia.org/wiki/FAT32) with [LBA](https://en.wikipedia.org/wiki/Logical_block_addressing) addressing |
102+
| `fat16-lba` | 0x0E | [FAT16B](https://en.wikipedia.org/wiki/File_Allocation_Table#FAT16B) with [LBA](https://en.wikipedia.org/wiki/Logical_block_addressing) addressing |
103+
| `linux-swap` | 0x82 | [Linux swap space](https://en.wikipedia.org/wiki/Swap_space#Linux) |
104+
| `linux-fs` | 0x83 | Any [Linux file system](https://en.wikipedia.org/wiki/File_system#Linux) |
105+
| `linux-lvm` | 0x8E | [Linux LVM](https://en.wikipedia.org/wiki/Logical_Volume_Manager_(Linux)) |
86106

87107
A complete list can be [found on Wikipedia](https://en.wikipedia.org/wiki/Partition_type), but [we do not support that yet](https://github.com/zig-osdev/disk-image-step/issues/8).
88108

src/components/part/MbrPartitionTable.zig

+33-50
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ fn parse_partition(ctx: dim.Context) !Partition {
8888
.offset = null,
8989
.size = null,
9090
.bootable = false,
91-
.type = .empty,
91+
.type = 0x00,
9292
.contains = .empty,
9393
};
9494

@@ -108,7 +108,19 @@ fn parse_partition(ctx: dim.Context) !Partition {
108108
endpart,
109109
});
110110
try switch (kw) {
111-
.type => updater.set(.type, try ctx.parse_enum(PartitionType)),
111+
.type => {
112+
const part_name = try ctx.parse_string();
113+
114+
const encoded = if (std.fmt.parseInt(u8, part_name, 0)) |value|
115+
value
116+
else |_|
117+
known_partition_types.get(part_name) orelse blk: {
118+
try ctx.report_nonfatal_error("unknown partition type '{}'", .{std.zig.fmtEscapes(part_name)});
119+
break :blk 0x00;
120+
};
121+
122+
try updater.set(.type, encoded);
123+
},
112124
.bootable => updater.set(.bootable, true),
113125
.size => updater.set(.size, try ctx.parse_mem_size()),
114126
.offset => updater.set(.offset, try ctx.parse_mem_size()),
@@ -212,7 +224,7 @@ fn render(table: *PartTable, stream: *dim.BinaryStream) dim.Content.RenderError!
212224
desc[0] = if (part.bootable) 0x80 else 0x00;
213225

214226
desc[1..4].* = encodeMbrChsEntry(lba); // chs_start
215-
desc[4] = @intFromEnum(part.type);
227+
desc[4] = part.type;
216228
desc[5..8].* = encodeMbrChsEntry(lba + size - 1); // chs_end
217229
std.mem.writeInt(u32, desc[8..12], lba, .little); // lba_start
218230
std.mem.writeInt(u32, desc[12..16], size, .little); // block_count
@@ -241,57 +253,28 @@ pub const Partition = struct {
241253
size: ?u64,
242254

243255
bootable: bool,
244-
type: PartitionType,
256+
type: u8,
245257

246258
contains: dim.Content,
247259
};
248260

249-
/// https://en.wikipedia.org/wiki/Partition_type
250-
pub const PartitionType = enum(u8) {
251-
empty = 0x00,
252-
253-
fat12 = 0x01,
254-
ntfs = 0x07,
255-
256-
fat32_chs = 0x0B,
257-
fat32_lba = 0x0C,
258-
259-
fat16_lba = 0x0E,
260-
261-
linux_swap = 0x82,
262-
linux_fs = 0x83,
263-
linux_lvm = 0x8E,
264-
265-
// Output from fdisk (util-linux 2.38.1)
266-
// 00 Leer 27 Verst. NTFS Win 82 Linux Swap / So c1 DRDOS/sec (FAT-
267-
// 01 FAT12 39 Plan 9 83 Linux c4 DRDOS/sec (FAT-
268-
// 02 XENIX root 3c PartitionMagic 84 versteckte OS/2 c6 DRDOS/sec (FAT-
269-
// 03 XENIX usr 40 Venix 80286 85 Linux erweitert c7 Syrinx
270-
// 04 FAT16 <32M 41 PPC PReP Boot 86 NTFS Datenträge da Keine Dateisyst
271-
// 05 Erweiterte 42 SFS 87 NTFS Datenträge db CP/M / CTOS / .
272-
// 06 FAT16 4d QNX4.x 88 Linux Klartext de Dell Dienstprog
273-
// 07 HPFS/NTFS/exFAT 4e QNX4.x 2. Teil 8e Linux LVM df BootIt
274-
// 08 AIX 4f QNX4.x 3. Teil 93 Amoeba e1 DOS-Zugriff
275-
// 09 AIX bootfähig 50 OnTrack DM 94 Amoeba BBT e3 DOS R/O
276-
// 0a OS/2-Bootmanage 51 OnTrack DM6 Aux 9f BSD/OS e4 SpeedStor
277-
// 0b W95 FAT32 52 CP/M a0 IBM Thinkpad Ru ea Linux erweitert
278-
// 0c W95 FAT32 (LBA) 53 OnTrack DM6 Aux a5 FreeBSD eb BeOS Dateisyste
279-
// 0e W95 FAT16 (LBA) 54 OnTrackDM6 a6 OpenBSD ee GPT
280-
// 0f W95 Erw. (LBA) 55 EZ-Drive a7 NeXTSTEP ef EFI (FAT-12/16/
281-
// 10 OPUS 56 Golden Bow a8 Darwin UFS f0 Linux/PA-RISC B
282-
// 11 Verst. FAT12 5c Priam Edisk a9 NetBSD f1 SpeedStor
283-
// 12 Compaq Diagnost 61 SpeedStor ab Darwin Boot f4 SpeedStor
284-
// 14 Verst. FAT16 <3 63 GNU HURD oder S af HFS / HFS+ f2 DOS sekundär
285-
// 16 Verst. FAT16 64 Novell Netware b7 BSDi Dateisyste f8 EBBR geschützt
286-
// 17 Verst. HPFS/NTF 65 Novell Netware b8 BSDI Swap fb VMware VMFS
287-
// 18 AST SmartSleep 70 DiskSecure Mult bb Boot-Assistent fc VMware VMKCORE
288-
// 1b Verst. W95 FAT3 75 PC/IX bc Acronis FAT32 L fd Linux RAID-Auto
289-
// 1c Verst. W95 FAT3 80 Altes Minix be Solaris Boot fe LANstep
290-
// 1e Verst. W95 FAT1 81 Minix / altes L bf Solaris ff BBT
291-
// 24 NEC DOS
292-
293-
_,
294-
};
261+
// TODO: Fill from https://en.wikipedia.org/wiki/Partition_type
262+
const known_partition_types = std.StaticStringMap(u8).initComptime(.{
263+
.{ "empty", 0x00 },
264+
265+
.{ "fat12", 0x01 },
266+
267+
.{ "ntfs", 0x07 },
268+
269+
.{ "fat32-chs", 0x0B },
270+
.{ "fat32-lba", 0x0C },
271+
272+
.{ "fat16-lba", 0x0E },
273+
274+
.{ "linux-swap", 0x82 },
275+
.{ "linux-fs", 0x83 },
276+
.{ "linux-lvm", 0x8E },
277+
});
295278

296279
pub fn encodeMbrChsEntry(lba: u32) [3]u8 {
297280
var chs = lbaToChs(lba);

tests/compound/mbr-boot.dis

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
mbr-part
22
bootloader empty
33
part # partition 1
4-
type fat16_lba
4+
type fat16-lba
55
contains vfat fat16
66
label "BOOT"
77
endfat
88
size 10M
99
endpart
1010
part # partition 2
11-
type fat16_lba
11+
type fat16-lba
1212
contains vfat fat16
1313
label "OS"
1414
!include "../../data/rootfs.dis"

0 commit comments

Comments
 (0)