Skip to content

std.os.uefi.protocol.file

Standard libraryZig 0.17.1Official documentation
Sourcestd/os/uefi/protocol/file.zig

Source line 1

std.os.uefi.protocol.file.cc

alias

Source line 6

Alias of std.os.uefi.cc

std.os.uefi.protocol.file.File

container

Source line 8

Fields

revision: u64
_open: *const fn (*const File, **File, [*:0]const u16, OpenMode, Attributes) callconv(cc) Status
_close: *const fn (*File) callconv(cc) Status
_delete: *const fn (*File) callconv(cc) Status
_read: *const fn (*File, *usize, [*]u8) callconv(cc) Status
_write: *const fn (*File, *usize, [*]const u8) callconv(cc) Status
_get_position: *const fn (*const File, *u64) callconv(cc) Status
_set_position: *const fn (*File, u64) callconv(cc) Status
_get_info: *const fn (*const File, *align(8) const Guid, *usize, ?[*]u8) callconv(cc) Status
_set_info: *const fn (*File, *align(8) const Guid, usize, [*]const u8) callconv(cc) Status
_flush: *const fn (*File) callconv(cc) Status

std.os.uefi.protocol.file.File.Attributes

container

Source line 302

Fields

read_only: bool = false
hidden: bool = false
system: bool = false
reserved: bool = false
directory: bool = false
archive: bool = false
_pad: u58 = 0

std.os.uefi.protocol.file.File.close

function

Source line 109

pub fn close(self: *File) CloseError!void

Parameters

self: *File

Errors

Unexpected UnexpectedError

std.os.uefi.protocol.file.File.CloseError

alias

Source line 33

Alias of std.os.uefi.UnexpectedError

std.os.uefi.protocol.file.File.delete

function

Source line 120

Delete the file.

Returns true if the file was deleted, false if the file was not deleted, which is a warning according to the UEFI specification.

pub fn delete(self: *File) uefi.UnexpectedError!bool

Parameters

self: *File

Errors

Unexpected

std.os.uefi.protocol.file.File.end_of_file

constant

Source line 387

u64

std.os.uefi.protocol.file.File.flush

function

Source line 274

pub fn flush(self: *File) FlushError!void

Parameters

self: *File

Errors

AccessDenied
DeviceError
Unexpected UnexpectedError
VolumeCorrupted
VolumeFull
WriteProtected

std.os.uefi.protocol.file.File.FlushError

error-set

Source line 72

Errors

AccessDenied
DeviceError
Unexpected UnexpectedError
VolumeCorrupted
VolumeFull
WriteProtected

std.os.uefi.protocol.file.File.getInfo

function

Source line 219

If buffer is too small to contain all of the info, this function returns Error.BufferTooSmall. You should call getInfoSize first to determine how big the buffer should be to safely call this function.

pub fn getInfo( self: *const File, comptime info: std.meta.Tag(Info), buffer: []align(@alignOf(@FieldType(Info, @tagName(info)))) u8, ) GetInfoError!*@FieldType(Info, @tagName(info))

Parameters

self: *const File
info: std.meta.Tag(Info)
buffer: []align(@alignOf(@FieldType(Info, @tagName(info)))) u8

Errors

BufferTooSmall
DeviceError GetInfoSizeError
NoMedia GetInfoSizeError
Unexpected UnexpectedError
Unsupported GetInfoSizeError
VolumeCorrupted GetInfoSizeError

std.os.uefi.protocol.file.File.GetInfoError

error-set

Source line 59

Errors

BufferTooSmall
DeviceError GetInfoSizeError
NoMedia GetInfoSizeError
Unexpected UnexpectedError
Unsupported GetInfoSizeError
VolumeCorrupted GetInfoSizeError

std.os.uefi.protocol.file.File.getInfoSize

function

Source line 202

pub fn getInfoSize(self: *const File, comptime info: std.meta.Tag(Info)) GetInfoError!usize

Parameters

self: *const File
info: std.meta.Tag(Info)

Errors

BufferTooSmall
DeviceError GetInfoSizeError
NoMedia GetInfoSizeError
Unexpected UnexpectedError
Unsupported GetInfoSizeError
VolumeCorrupted GetInfoSizeError

std.os.uefi.protocol.file.File.GetInfoSizeError

error-set

Source line 53

Errors

DeviceError
NoMedia
Unexpected UnexpectedError
Unsupported
VolumeCorrupted

std.os.uefi.protocol.file.File.getPosition

function

Source line 171

pub fn getPosition(self: *const File) SeekError!u64

Parameters

self: *const File

Errors

DeviceError
Unexpected UnexpectedError
Unsupported

std.os.uefi.protocol.file.File.Info

container

Source line 318

Fields

file: Info.File
file_system: FileSystem
volume_label: VolumeLabel

std.os.uefi.protocol.file.File.Info.File

container

Source line 323

Fields

size: u64
file_size: u64
physical_size: u64
create_time: Time
last_access_time: Time
modification_time: Time
attribute: Attributes
_file_name: u16

std.os.uefi.protocol.file.File.Info.File.getFileName

function

Source line 333

pub fn getFileName(self: *const Info.File) [*:0]const u16

Parameters

self: *const Info.File

std.os.uefi.protocol.file.File.Info.File.guid

constant

Source line 337

std.os.uefi.protocol.file.File.Info.FileSystem

container

Source line 347

Fields

size: u64
read_only: bool
volume_size: u64
free_space: u64
block_size: u32
_volume_label: u16

std.os.uefi.protocol.file.File.Info.FileSystem.getVolumeLabel

function

Source line 355

pub fn getVolumeLabel(self: *const FileSystem) [*:0]const u16

Parameters

self: *const FileSystem

std.os.uefi.protocol.file.File.Info.FileSystem.guid

constant

Source line 359

std.os.uefi.protocol.file.File.Info.VolumeLabel

container

Source line 369

Fields

_volume_label: u16

std.os.uefi.protocol.file.File.Info.VolumeLabel.getVolumeLabel

function

Source line 372

pub fn getVolumeLabel(self: *const VolumeLabel) [*:0]const u16

Parameters

self: *const VolumeLabel

std.os.uefi.protocol.file.File.Info.VolumeLabel.guid

constant

Source line 376

std.os.uefi.protocol.file.File.open

function

Source line 80

pub fn open( self: *const File, file_name: [*:0]const u16, mode: OpenMode, create_attributes: Attributes, ) OpenError!*File

Parameters

self: *const File
file_name: [*:0]const u16
mode: OpenMode
create_attributes: Attributes

Errors

AccessDenied
DeviceError
InvalidParameter
MediaChanged
NoMedia
NotFound
OutOfResources
Unexpected UnexpectedError
VolumeCorrupted
VolumeFull
WriteProtected

std.os.uefi.protocol.file.File.OpenError

error-set

Source line 21

Errors

AccessDenied
DeviceError
InvalidParameter
MediaChanged
NoMedia
NotFound
OutOfResources
Unexpected UnexpectedError
VolumeCorrupted
VolumeFull
WriteProtected

std.os.uefi.protocol.file.File.OpenMode

container

Source line 286

Fields

read = @bitCast(Bits{ .read = true })
read_write = @bitCast(Bits{ .read = true, .write = true })
read_write_create = @bitCast(Bits{ .read = true, .write = true, .create = true })

std.os.uefi.protocol.file.File.OpenMode.Bits

container

Source line 287

Fields

read: bool = false
write: bool = false
_pad: u61 = 0
create: bool = false

std.os.uefi.protocol.file.File.read

function

Source line 144

If self is a directory entry and buffer is too small to contain the next entry, this function returns Error.BufferTooSmall. You can call readSize before or after to determine how big the buffer should be to call this function.

pub fn read(self: *File, buffer: []u8) ReadError!usize

Parameters

self: *File
buffer: []u8

Errors

BufferTooSmall
DeviceError
NoMedia
Unexpected UnexpectedError
VolumeCorrupted

std.os.uefi.protocol.file.File.ReadError

error-set

Source line 38

Errors

BufferTooSmall
DeviceError
NoMedia
Unexpected UnexpectedError
VolumeCorrupted

std.os.uefi.protocol.file.File.readSize

function

Source line 128

pub fn readSize(self: *File) ReadError!usize

Parameters

self: *File

Errors

BufferTooSmall
DeviceError
NoMedia
Unexpected UnexpectedError
VolumeCorrupted

std.os.uefi.protocol.file.File.seekBy

function

Source line 190

fn seekBy(self: *File, offset: i64) SeekError!void

Parameters

self: *File
offset: i64

Errors

DeviceError
Unexpected UnexpectedError
Unsupported

std.os.uefi.protocol.file.File.SeekError

error-set

Source line 34

Errors

DeviceError
Unexpected UnexpectedError
Unsupported

std.os.uefi.protocol.file.File.setInfo

function

Source line 243

pub fn setInfo( self: *File, comptime info: std.meta.Tag(Info), data: *const @FieldType(Info, @tagName(info)), ) SetInfoError!void

Parameters

self: *File
info: std.meta.Tag(Info)
data: *const @FieldType(Info, @tagName(info))

Errors

AccessDenied
BadBufferSize
DeviceError
NoMedia
Unexpected UnexpectedError
Unsupported
VolumeCorrupted
VolumeFull
WriteProtected

std.os.uefi.protocol.file.File.SetInfoError

error-set

Source line 62

Errors

AccessDenied
BadBufferSize
DeviceError
NoMedia
Unexpected UnexpectedError
Unsupported
VolumeCorrupted
VolumeFull
WriteProtected

std.os.uefi.protocol.file.File.setPosition

function

Source line 181

pub fn setPosition(self: *File, position: u64) SeekError!void

Parameters

self: *File
position: u64

Errors

DeviceError
Unexpected UnexpectedError
Unsupported

std.os.uefi.protocol.file.File.write

function

Source line 156

pub fn write(self: *File, buffer: []const u8) WriteError!usize

Parameters

self: *File
buffer: []const u8

Errors

AccessDenied
DeviceError
NoMedia
Unexpected UnexpectedError
Unsupported
VolumeCorrupted
VolumeFull
WriteProtected

std.os.uefi.protocol.file.File.WriteError

error-set

Source line 44

Errors

AccessDenied
DeviceError
NoMedia
Unexpected UnexpectedError
Unsupported
VolumeCorrupted
VolumeFull
WriteProtected

std.os.uefi.protocol.file.Guid

alias

Source line 3

Alias of std.os.uefi.Guid

std.os.uefi.protocol.file.Status

alias

Source line 5

Alias of std.os.uefi.Status

std.os.uefi.protocol.file.std

alias

Source line 1

Alias of std

std.os.uefi.protocol.file.Time

alias

Source line 4

Alias of std.os.uefi.Time

std.os.uefi.protocol.file.uefi

alias

Source line 2

Alias of std.os.uefi

Source code

std/os/uefi/protocol/file.zig

const std = @import("std");
const uefi = std.os.uefi;
const Guid = uefi.Guid;
const Time = uefi.Time;
const Status = uefi.Status;
const cc = uefi.cc;

pub const File = extern struct {
    revision: u64,
    _open: *const fn (*const File, **File, [*:0]const u16, OpenMode, Attributes) callconv(cc) Status,
    _close: *const fn (*File) callconv(cc) Status,
    _delete: *const fn (*File) callconv(cc) Status,
    _read: *const fn (*File, *usize, [*]u8) callconv(cc) Status,
    _write: *const fn (*File, *usize, [*]const u8) callconv(cc) Status,
    _get_position: *const fn (*const File, *u64) callconv(cc) Status,
    _set_position: *const fn (*File, u64) callconv(cc) Status,
    _get_info: *const fn (*const File, *align(8) const Guid, *usize, ?[*]u8) callconv(cc) Status,
    _set_info: *const fn (*File, *align(8) const Guid, usize, [*]const u8) callconv(cc) Status,
    _flush: *const fn (*File) callconv(cc) Status,

    pub const OpenError = uefi.UnexpectedError || error{
        NotFound,
        NoMedia,
        MediaChanged,
        DeviceError,
        VolumeCorrupted,
        WriteProtected,
        AccessDenied,
        OutOfResources,
        VolumeFull,
        InvalidParameter,
    };
    pub const CloseError = uefi.UnexpectedError;
    pub const SeekError = uefi.UnexpectedError || error{
        Unsupported,
        DeviceError,
    };
    pub const ReadError = uefi.UnexpectedError || error{
        NoMedia,
        DeviceError,
        VolumeCorrupted,
        BufferTooSmall,
    };
    pub const WriteError = uefi.UnexpectedError || error{
        Unsupported,
        NoMedia,
        DeviceError,
        VolumeCorrupted,
        WriteProtected,
        AccessDenied,
        VolumeFull,
    };
    pub const GetInfoSizeError = uefi.UnexpectedError || error{
        Unsupported,
        NoMedia,
        DeviceError,
        VolumeCorrupted,
    };
    pub const GetInfoError = GetInfoSizeError || error{
        BufferTooSmall,
    };
    pub const SetInfoError = uefi.UnexpectedError || error{
        Unsupported,
        NoMedia,
        DeviceError,
        VolumeCorrupted,
        WriteProtected,
        AccessDenied,
        VolumeFull,
        BadBufferSize,
    };
    pub const FlushError = uefi.UnexpectedError || error{
        DeviceError,
        VolumeCorrupted,
        WriteProtected,
        AccessDenied,
        VolumeFull,
    };

    pub fn open(
        self: *const File,
        file_name: [*:0]const u16,
        mode: OpenMode,
        create_attributes: Attributes,
    ) OpenError!*File {
        var new: *File = undefined;
        switch (self._open(
            self,
            &new,
            file_name,
            mode,
            create_attributes,
        )) {
            .success => return new,
            .not_found => return error.NotFound,
            .no_media => return error.NoMedia,
            .media_changed => return error.MediaChanged,
            .device_error => return error.DeviceError,
            .volume_corrupted => return error.VolumeCorrupted,
            .write_protected => return error.WriteProtected,
            .access_denied => return error.AccessDenied,
            .out_of_resources => return error.OutOfResources,
            .volume_full => return error.VolumeFull,
            .invalid_parameter => return error.InvalidParameter,
            else => |status| return uefi.unexpectedStatus(status),
        }
    }

    pub fn close(self: *File) CloseError!void {
        switch (self._close(self)) {
            .success => {},
            else => |status| return uefi.unexpectedStatus(status),
        }
    }

    /// Delete the file.
    ///
    /// Returns true if the file was deleted, false if the file was not deleted, which is a warning
    /// according to the UEFI specification.
    pub fn delete(self: *File) uefi.UnexpectedError!bool {
        switch (self._delete(self)) {
            .success => return true,
            .warn_delete_failure => return false,
            else => |status| return uefi.unexpectedStatus(status),
        }
    }

    pub fn readSize(self: *File) ReadError!usize {
        const zerobuf: [0]u8 = undefined;
        var size: usize = 0;
        switch (self._read(self, &size, &zerobuf)) {
            .success, .buffer_too_small => return size,
            .no_media => return error.NoMedia,
            .device_error => return error.DeviceError,
            .volume_corrupted => return error.VolumeCorrupted,
            else => |status| return uefi.unexpectedStatus(status),
        }
    }

    /// If `self` is a directory entry and `buffer` is too small to contain the
    /// next entry, this function returns `Error.BufferTooSmall`. You can call
    /// `readSize` before or after to determine how big the buffer should be to
    /// call this function.
    pub fn read(self: *File, buffer: []u8) ReadError!usize {
        var size: usize = buffer.len;
        switch (self._read(self, &size, buffer.ptr)) {
            .success => return size,
            .no_media => return error.NoMedia,
            .device_error => return error.DeviceError,
            .volume_corrupted => return error.VolumeCorrupted,
            .buffer_too_small => return error.BufferTooSmall,
            else => |status| return uefi.unexpectedStatus(status),
        }
    }

    pub fn write(self: *File, buffer: []const u8) WriteError!usize {
        var size: usize = buffer.len;
        switch (self._write(self, &size, buffer.ptr)) {
            .success => return size,
            .unsupported => return error.Unsupported,
            .no_media => return error.NoMedia,
            .device_error => return error.DeviceError,
            .volume_corrupted => return error.VolumeCorrupted,
            .write_protected => return error.WriteProtected,
            .access_denied => return error.AccessDenied,
            .volume_full => return error.VolumeFull,
            else => |status| return uefi.unexpectedStatus(status),
        }
    }

    pub fn getPosition(self: *const File) SeekError!u64 {
        var position: u64 = undefined;
        switch (self._get_position(self, &position)) {
            .success => return position,
            .unsupported => return error.Unsupported,
            .device_error => return error.DeviceError,
            else => |status| return uefi.unexpectedStatus(status),
        }
    }

    pub fn setPosition(self: *File, position: u64) SeekError!void {
        switch (self._set_position(self, position)) {
            .success => {},
            .unsupported => return error.Unsupported,
            .device_error => return error.DeviceError,
            else => |status| return uefi.unexpectedStatus(status),
        }
    }

    fn seekBy(self: *File, offset: i64) SeekError!void {
        var pos = try self.getPosition();
        const seek_back = offset < 0;
        const amt = @abs(offset);
        if (seek_back) {
            pos += amt;
        } else {
            pos -= amt;
        }
        try self.setPosition(pos);
    }

    pub fn getInfoSize(self: *const File, comptime info: std.meta.Tag(Info)) GetInfoError!usize {
        const InfoType = @FieldType(Info, @tagName(info));

        var len: usize = 0;
        switch (self._get_info(self, &InfoType.guid, &len, null)) {
            .success, .buffer_too_small => return len,
            .unsupported => return error.Unsupported,
            .no_media => return error.NoMedia,
            .device_error => return error.DeviceError,
            .volume_corrupted => return error.VolumeCorrupted,
            else => |status| return uefi.unexpectedStatus(status),
        }
    }

    /// If `buffer` is too small to contain all of the info, this function returns
    /// `Error.BufferTooSmall`. You should call `getInfoSize` first to determine
    /// how big the buffer should be to safely call this function.
    pub fn getInfo(
        self: *const File,
        comptime info: std.meta.Tag(Info),
        buffer: []align(@alignOf(@FieldType(Info, @tagName(info)))) u8,
    ) GetInfoError!*@FieldType(Info, @tagName(info)) {
        const InfoType = @FieldType(Info, @tagName(info));

        var len = buffer.len;
        switch (self._get_info(
            self,
            &InfoType.guid,
            &len,
            buffer.ptr,
        )) {
            .success => return @as(*InfoType, @ptrCast(buffer.ptr)),
            .buffer_too_small => return error.BufferTooSmall,
            .unsupported => return error.Unsupported,
            .no_media => return error.NoMedia,
            .device_error => return error.DeviceError,
            .volume_corrupted => return error.VolumeCorrupted,
            else => |status| return uefi.unexpectedStatus(status),
        }
    }

    pub fn setInfo(
        self: *File,
        comptime info: std.meta.Tag(Info),
        data: *const @FieldType(Info, @tagName(info)),
    ) SetInfoError!void {
        const InfoType = @FieldType(Info, @tagName(info));

        const attached_str: [*:0]const u16 = switch (info) {
            .file => data.getFileName(),
            .file_system, .volume_label => data.getVolumeLabel(),
        };
        const attached_str_len = std.mem.sliceTo(attached_str, 0).len;

        // add the length (not +1 for sentinel) because `@sizeOf(InfoType)`
        // already contains the first utf16 char
        const len = @sizeOf(InfoType) + (attached_str_len * 2);

        switch (self._set_info(self, &InfoType.guid, len, @ptrCast(data))) {
            .success => {},
            .unsupported => return error.Unsupported,
            .no_media => return error.NoMedia,
            .device_error => return error.DeviceError,
            .volume_corrupted => return error.VolumeCorrupted,
            .write_protected => return error.WriteProtected,
            .access_denied => return error.AccessDenied,
            .volume_full => return error.VolumeFull,
            .bad_buffer_size => return error.BadBufferSize,
            else => |status| return uefi.unexpectedStatus(status),
        }
    }

    pub fn flush(self: *File) FlushError!void {
        switch (self._flush(self)) {
            .success => {},
            .device_error => return error.DeviceError,
            .volume_corrupted => return error.VolumeCorrupted,
            .write_protected => return error.WriteProtected,
            .access_denied => return error.AccessDenied,
            .volume_full => return error.VolumeFull,
            else => |status| return uefi.unexpectedStatus(status),
        }
    }

    pub const OpenMode = enum(u64) {
        pub const Bits = packed struct(u64) {
            // 0x0000000000000001
            read: bool = false,
            // 0x0000000000000002
            write: bool = false,
            _pad: u61 = 0,
            // 0x8000000000000000
            create: bool = false,
        };

        read = @bitCast(Bits{ .read = true }),
        read_write = @bitCast(Bits{ .read = true, .write = true }),
        read_write_create = @bitCast(Bits{ .read = true, .write = true, .create = true }),
    };

    pub const Attributes = packed struct(u64) {
        // 0x0000000000000001
        read_only: bool = false,
        // 0x0000000000000002
        hidden: bool = false,
        // 0x0000000000000004
        system: bool = false,
        // 0x0000000000000008
        reserved: bool = false,
        // 0x0000000000000010
        directory: bool = false,
        // 0x0000000000000020
        archive: bool = false,
        _pad: u58 = 0,
    };

    pub const Info = union(enum) {
        file: Info.File,
        file_system: FileSystem,
        volume_label: VolumeLabel,

        pub const File = extern struct {
            size: u64,
            file_size: u64,
            physical_size: u64,
            create_time: Time,
            last_access_time: Time,
            modification_time: Time,
            attribute: Attributes,
            _file_name: u16,

            pub fn getFileName(self: *const Info.File) [*:0]const u16 {
                return @as([*:0]const u16, @ptrCast(&self._file_name));
            }

            pub const guid align(8) = Guid{
                .time_low = 0x09576e92,
                .time_mid = 0x6d3f,
                .time_high_and_version = 0x11d2,
                .clock_seq_high_and_reserved = 0x8e,
                .clock_seq_low = 0x39,
                .node = [_]u8{ 0x00, 0xa0, 0xc9, 0x69, 0x72, 0x3b },
            };
        };

        pub const FileSystem = extern struct {
            size: u64,
            read_only: bool,
            volume_size: u64,
            free_space: u64,
            block_size: u32,
            _volume_label: u16,

            pub fn getVolumeLabel(self: *const FileSystem) [*:0]const u16 {
                return @as([*:0]const u16, @ptrCast(&self._volume_label));
            }

            pub const guid align(8) = Guid{
                .time_low = 0x09576e93,
                .time_mid = 0x6d3f,
                .time_high_and_version = 0x11d2,
                .clock_seq_high_and_reserved = 0x8e,
                .clock_seq_low = 0x39,
                .node = [_]u8{ 0x00, 0xa0, 0xc9, 0x69, 0x72, 0x3b },
            };
        };

        pub const VolumeLabel = extern struct {
            _volume_label: u16,

            pub fn getVolumeLabel(self: *const VolumeLabel) [*:0]const u16 {
                return @as([*:0]const u16, @ptrCast(&self._volume_label));
            }

            pub const guid align(8) = Guid{
                .time_low = 0xdb47d7d3,
                .time_mid = 0xfe81,
                .time_high_and_version = 0x11d3,
                .clock_seq_high_and_reserved = 0x9a,
                .clock_seq_low = 0x35,
                .node = [_]u8{ 0x00, 0x90, 0x27, 0x3f, 0xc1, 0x4d },
            };
        };
    };

    const end_of_file: u64 = 0xffffffffffffffff;
};