Commit 73e4571b4c

fifty-six <ybham6@gmail.com>
2022-01-11 07:29:35
std/os/uefi: Add methods next() and size() to DevicePathProtocol
These are used for more easily dealing with a series of Device Path nodes.
1 parent 608fcef
Changed files (1)
lib
std
os
lib/std/os/uefi/protocols/device_path_protocol.zig
@@ -15,6 +15,25 @@ pub const DevicePathProtocol = packed struct {
         .node = [_]u8{ 0x00, 0xa0, 0xc9, 0x69, 0x72, 0x3b },
     };
 
+    /// Returns the next DevicePathProtocol node in the sequence, if any.
+    pub fn next(self: *DevicePathProtocol) ?*DevicePathProtocol {
+        if (self.type == .End and @intToEnum(EndDevicePath.Subtype, self.subtype) == .EndEntire)
+            return null;
+
+        return @ptrCast(*DevicePathProtocol, @ptrCast([*]u8, self) + self.length);
+    }
+
+    /// Calculates the total length of the device path structure in bytes, including the end of device path node.
+    pub fn size(self: *DevicePathProtocol) usize {
+        var node = self;
+
+        while (node.next()) |next_node| {
+            node = next_node;
+        }
+
+        return (@ptrToInt(node) + node.length) - @ptrToInt(self);
+    }
+
     pub fn getDevicePath(self: *const DevicePathProtocol) ?DevicePath {
         return switch (self.type) {
             .Hardware => blk: {