Skip to content

Commit e52c6ed

Browse files
author
Felix "xq" Queißner
committed
starts to refactor into tree of dynamic content
1 parent fd05fff commit e52c6ed

13 files changed

+841
-285
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@ The *Fill* type will fill the remaining size in its space with the given `<byte>
3232
fill <byte>
3333
```
3434

35-
### Raw Binary Content (`raw`)
35+
### Paste File Contents (`paste-file`)
3636

3737
The *Raw* type will include the file at `<path>` verbatim and will error, if not enough space is available.
3838

3939
`<path>` is relative to the current file.
4040

4141
```plain
42-
raw <path>
42+
paste-file <path>
4343
```
4444

4545
### MBR Partition Table (`mbr-part`)

build.zig.zon

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
.dependencies = .{
66
.zfat = .{
77
.url = "https://github.com/ZigEmbeddedGroup/zfat/archive/3ce06d43a4e04d387034dcae2f486b050701f321.tar.gz",
8-
.hash = "12205d874e8c9fd08d93c09ccbfddb045809afcc28e232b36b5abe3d288278ce458f",
8+
.hash = "zfat-0.0.0-AAAAAMYlcABdh06Mn9CNk8Ccy_3bBFgJr8wo4jKza1q-",
99
},
1010
.args = .{
1111
.url = "git+https://github.com/ikskuh/zig-args.git#9425b94c103a031777fdd272c555ce93a7dea581",

concept/script.dis

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
mbr-part
2-
bootloader $PATH1
2+
bootloader paste-file $PATH1
33
part # partition 1
44
type fat32-lba
55
size 500M

justfile

+2-4
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,9 @@ behaviour-tests: \
1919
(behaviour-test "tests/basic/raw.dis") \
2020
(behaviour-test "tests/part/mbr/minimal.dis")
2121

22-
behaviour-test script:
23-
{{zig}} build install
24-
22+
behaviour-test script: install
2523
./zig-out/bin/dim --output .zig-cache/disk.img --script "{{script}}"
26-
./zig-out/bin/dim --output .zig-cache/disk.img --size 30M --script "{{script}}"
24+
./zig-out/bin/dim --output .zig-cache/disk.img --script "{{script}}" --size 30M
2725

2826

2927
fuzz:

src/build.old.zig

-204
Original file line numberDiff line numberDiff line change
@@ -712,210 +712,6 @@ pub const Content = union(enum) {
712712
}
713713
};
714714

715-
pub const mbr = struct {
716-
pub const Table = struct {
717-
bootloader: [440]u8 = .{0} ** 440,
718-
disk_id: ?u32 = null,
719-
partitions: [4]?*const Partition,
720-
};
721-
722-
pub const Partition = struct {
723-
offset: ?u64 = null,
724-
size: u64,
725-
726-
bootable: bool,
727-
type: PartitionType,
728-
729-
data: Content,
730-
};
731-
732-
/// https://en.wikipedia.org/wiki/Partition_type
733-
pub const PartitionType = enum(u8) {
734-
empty = 0x00,
735-
736-
fat12 = 0x01,
737-
ntfs = 0x07,
738-
739-
fat32_chs = 0x0B,
740-
fat32_lba = 0x0C,
741-
742-
fat16_lba = 0x0E,
743-
744-
linux_swap = 0x82,
745-
linux_fs = 0x83,
746-
linux_lvm = 0x8E,
747-
748-
// Output from fdisk (util-linux 2.38.1)
749-
// 00 Leer 27 Verst. NTFS Win 82 Linux Swap / So c1 DRDOS/sec (FAT-
750-
// 01 FAT12 39 Plan 9 83 Linux c4 DRDOS/sec (FAT-
751-
// 02 XENIX root 3c PartitionMagic 84 versteckte OS/2 c6 DRDOS/sec (FAT-
752-
// 03 XENIX usr 40 Venix 80286 85 Linux erweitert c7 Syrinx
753-
// 04 FAT16 <32M 41 PPC PReP Boot 86 NTFS Datenträge da Keine Dateisyst
754-
// 05 Erweiterte 42 SFS 87 NTFS Datenträge db CP/M / CTOS / .
755-
// 06 FAT16 4d QNX4.x 88 Linux Klartext de Dell Dienstprog
756-
// 07 HPFS/NTFS/exFAT 4e QNX4.x 2. Teil 8e Linux LVM df BootIt
757-
// 08 AIX 4f QNX4.x 3. Teil 93 Amoeba e1 DOS-Zugriff
758-
// 09 AIX bootfähig 50 OnTrack DM 94 Amoeba BBT e3 DOS R/O
759-
// 0a OS/2-Bootmanage 51 OnTrack DM6 Aux 9f BSD/OS e4 SpeedStor
760-
// 0b W95 FAT32 52 CP/M a0 IBM Thinkpad Ru ea Linux erweitert
761-
// 0c W95 FAT32 (LBA) 53 OnTrack DM6 Aux a5 FreeBSD eb BeOS Dateisyste
762-
// 0e W95 FAT16 (LBA) 54 OnTrackDM6 a6 OpenBSD ee GPT
763-
// 0f W95 Erw. (LBA) 55 EZ-Drive a7 NeXTSTEP ef EFI (FAT-12/16/
764-
// 10 OPUS 56 Golden Bow a8 Darwin UFS f0 Linux/PA-RISC B
765-
// 11 Verst. FAT12 5c Priam Edisk a9 NetBSD f1 SpeedStor
766-
// 12 Compaq Diagnost 61 SpeedStor ab Darwin Boot f4 SpeedStor
767-
// 14 Verst. FAT16 <3 63 GNU HURD oder S af HFS / HFS+ f2 DOS sekundär
768-
// 16 Verst. FAT16 64 Novell Netware b7 BSDi Dateisyste f8 EBBR geschützt
769-
// 17 Verst. HPFS/NTF 65 Novell Netware b8 BSDI Swap fb VMware VMFS
770-
// 18 AST SmartSleep 70 DiskSecure Mult bb Boot-Assistent fc VMware VMKCORE
771-
// 1b Verst. W95 FAT3 75 PC/IX bc Acronis FAT32 L fd Linux RAID-Auto
772-
// 1c Verst. W95 FAT3 80 Altes Minix be Solaris Boot fe LANstep
773-
// 1e Verst. W95 FAT1 81 Minix / altes L bf Solaris ff BBT
774-
// 24 NEC DOS
775-
776-
_,
777-
};
778-
779-
pub fn encodeMbrChsEntry(lba: u32) [3]u8 {
780-
var chs = lbaToChs(lba);
781-
782-
if (chs.cylinder >= 1024) {
783-
chs = .{
784-
.cylinder = 1023,
785-
.head = 255,
786-
.sector = 63,
787-
};
788-
}
789-
790-
const cyl: u10 = @intCast(chs.cylinder);
791-
const head: u8 = @intCast(chs.head);
792-
const sect: u6 = @intCast(chs.sector);
793-
794-
const sect_cyl: u8 = @as(u8, 0xC0) & @as(u8, @truncate(cyl >> 2)) + sect;
795-
const sect_8: u8 = @truncate(cyl);
796-
797-
return .{ head, sect_cyl, sect_8 };
798-
}
799-
800-
const CHS = struct {
801-
cylinder: u32,
802-
head: u8, // limit: 256
803-
sector: u6, // limit: 64
804-
805-
pub fn init(c: u32, h: u8, s: u6) CHS {
806-
return .{ .cylinder = c, .head = h, .sector = s };
807-
}
808-
};
809-
810-
pub fn lbaToChs(lba: u32) CHS {
811-
const hpc = 255;
812-
const spt = 63;
813-
814-
// C, H and S are the cylinder number, the head number, and the sector number
815-
// LBA is the logical block address
816-
// HPC is the maximum number of heads per cylinder (reported by disk drive, typically 16 for 28-bit LBA)
817-
// SPT is the maximum number of sectors per track (reported by disk drive, typically 63 for 28-bit LBA)
818-
// LBA = (C * HPC + H) * SPT + (S - 1)
819-
820-
const sector = (lba % spt);
821-
const cyl_head = (lba / spt);
822-
823-
const head = (cyl_head % hpc);
824-
const cyl = (cyl_head / hpc);
825-
826-
return CHS{
827-
.sector = @intCast(sector + 1),
828-
.head = @intCast(head),
829-
.cylinder = cyl,
830-
};
831-
}
832-
};
833-
834-
// test "lba to chs" {
835-
// // table from https://en.wikipedia.org/wiki/Logical_block_addressing#CHS_conversion
836-
// try std.testing.expectEqual(mbr.CHS.init(0, 0, 1), mbr.lbaToChs(0));
837-
// try std.testing.expectEqual(mbr.CHS.init(0, 0, 2), mbr.lbaToChs(1));
838-
// try std.testing.expectEqual(mbr.CHS.init(0, 0, 3), mbr.lbaToChs(2));
839-
// try std.testing.expectEqual(mbr.CHS.init(0, 0, 63), mbr.lbaToChs(62));
840-
// try std.testing.expectEqual(mbr.CHS.init(0, 1, 1), mbr.lbaToChs(63));
841-
// try std.testing.expectEqual(mbr.CHS.init(0, 15, 1), mbr.lbaToChs(945));
842-
// try std.testing.expectEqual(mbr.CHS.init(0, 15, 63), mbr.lbaToChs(1007));
843-
// try std.testing.expectEqual(mbr.CHS.init(1, 0, 1), mbr.lbaToChs(1008));
844-
// try std.testing.expectEqual(mbr.CHS.init(1, 0, 63), mbr.lbaToChs(1070));
845-
// try std.testing.expectEqual(mbr.CHS.init(1, 1, 1), mbr.lbaToChs(1071));
846-
// try std.testing.expectEqual(mbr.CHS.init(1, 1, 63), mbr.lbaToChs(1133));
847-
// try std.testing.expectEqual(mbr.CHS.init(1, 2, 1), mbr.lbaToChs(1134));
848-
// try std.testing.expectEqual(mbr.CHS.init(1, 15, 63), mbr.lbaToChs(2015));
849-
// try std.testing.expectEqual(mbr.CHS.init(2, 0, 1), mbr.lbaToChs(2016));
850-
// try std.testing.expectEqual(mbr.CHS.init(15, 15, 63), mbr.lbaToChs(16127));
851-
// try std.testing.expectEqual(mbr.CHS.init(16, 0, 1), mbr.lbaToChs(16128));
852-
// try std.testing.expectEqual(mbr.CHS.init(31, 15, 63), mbr.lbaToChs(32255));
853-
// try std.testing.expectEqual(mbr.CHS.init(32, 0, 1), mbr.lbaToChs(32256));
854-
// try std.testing.expectEqual(mbr.CHS.init(16319, 15, 63), mbr.lbaToChs(16450559));
855-
// try std.testing.expectEqual(mbr.CHS.init(16382, 15, 63), mbr.lbaToChs(16514063));
856-
// }
857-
858-
pub const gpt = struct {
859-
pub const Guid = [16]u8;
860-
861-
pub const Table = struct {
862-
disk_id: Guid,
863-
864-
partitions: []const Partition,
865-
};
866-
867-
pub const Partition = struct {
868-
type: Guid,
869-
part_id: Guid,
870-
871-
offset: ?u64 = null,
872-
size: u64,
873-
874-
name: [36]u16,
875-
876-
attributes: Attributes,
877-
878-
data: Content,
879-
880-
pub const Attributes = packed struct(u32) {
881-
system: bool,
882-
efi_hidden: bool,
883-
legacy: bool,
884-
read_only: bool,
885-
hidden: bool,
886-
no_automount: bool,
887-
888-
padding: u26 = 0,
889-
};
890-
};
891-
892-
/// https://en.wikipedia.org/wiki/GUID_Partition_Table#Partition_type_GUIDs
893-
pub const PartitionType = struct {
894-
pub const unused: Guid = .{};
895-
896-
pub const microsoft_basic_data: Guid = .{};
897-
pub const microsoft_reserved: Guid = .{};
898-
899-
pub const windows_recovery: Guid = .{};
900-
901-
pub const plan9: Guid = .{};
902-
903-
pub const linux_swap: Guid = .{};
904-
pub const linux_fs: Guid = .{};
905-
pub const linux_reserved: Guid = .{};
906-
pub const linux_lvm: Guid = .{};
907-
};
908-
909-
pub fn nameLiteral(comptime name: []const u8) [36]u16 {
910-
return comptime blk: {
911-
var buf: [36]u16 = undefined;
912-
const len = std.unicode.utf8ToUtf16Le(&buf, name) catch |err| @compileError(@tagName(err));
913-
@memset(buf[len..], 0);
914-
break :blk &buf;
915-
};
916-
}
917-
};
918-
919715
pub const FileSystem = struct {
920716
pub const Format = union(enum) {
921717
pub const Tag = std.meta.Tag(@This());

src/components/EmptyData.zig

+18-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,22 @@
66
const std = @import("std");
77
const dim = @import("../dim.zig");
88

9-
pub fn execute(ctx: dim.Context) !void {
10-
_ = ctx;
9+
const EmptyData = @This();
10+
11+
pub fn parse(ctx: dim.Context) !dim.Content {
12+
const pf = try ctx.alloc_object(EmptyData);
13+
pf.* = .{};
14+
return .create_handle(pf, .create(@This(), .{
15+
.guess_size_fn = guess_size,
16+
.render_fn = render,
17+
}));
18+
}
19+
20+
fn guess_size(_: *EmptyData) dim.Content.GuessError!dim.SizeGuess {
21+
return .{ .at_least = 0 };
22+
}
23+
24+
fn render(self: *EmptyData, stream: *dim.BinaryStream) dim.Content.RenderError!void {
25+
_ = self;
26+
_ = stream;
1127
}

src/components/FillData.zig

+23-5
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,28 @@
55
const std = @import("std");
66
const dim = @import("../dim.zig");
77

8-
pub fn execute(ctx: dim.Context) !void {
9-
const fill_value: u8 = try ctx.get_integer(u8, 0);
8+
const FillData = @This();
109

11-
if (ctx.get_remaining_size()) |size| {
12-
try ctx.writer().writeByteNTimes(fill_value, size);
13-
}
10+
fill_value: u8,
11+
12+
pub fn parse(ctx: dim.Context) !dim.Content {
13+
const pf = try ctx.alloc_object(FillData);
14+
pf.* = .{
15+
.fill_value = try ctx.parse_integer(u8, 0),
16+
};
17+
return .create_handle(pf, .create(@This(), .{
18+
.guess_size_fn = guess_size,
19+
.render_fn = render,
20+
}));
21+
}
22+
23+
fn guess_size(_: *FillData) dim.Content.GuessError!dim.SizeGuess {
24+
return .{ .at_least = 0 };
25+
}
26+
27+
fn render(self: *FillData, stream: *dim.BinaryStream) dim.Content.RenderError!void {
28+
try stream.writer().writeByteNTimes(
29+
self.fill_value,
30+
stream.capacity,
31+
);
1432
}

src/components/PasteFile.zig

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const std = @import("std");
2+
const dim = @import("../dim.zig");
3+
4+
const PasteFile = @This();
5+
6+
file_handle: dim.FileName,
7+
8+
pub fn parse(ctx: dim.Context) !dim.Content {
9+
const pf = try ctx.alloc_object(PasteFile);
10+
pf.* = .{
11+
.file_handle = try ctx.parse_file_name(),
12+
};
13+
return .create_handle(pf, .create(@This(), .{
14+
.guess_size_fn = guess_size,
15+
.render_fn = render,
16+
}));
17+
}
18+
19+
fn guess_size(self: *PasteFile) dim.Content.GuessError!dim.SizeGuess {
20+
const size = try self.file_handle.get_size();
21+
22+
return .{ .exact = size };
23+
}
24+
25+
fn render(self: *PasteFile, stream: *dim.BinaryStream) dim.Content.RenderError!void {
26+
try self.file_handle.copy_to(stream);
27+
}

src/components/RawData.zig

-20
This file was deleted.

0 commit comments

Comments
 (0)