| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- const std = @import("std");
- const uefi = std.os.uefi;
- const Status = uefi.Status;
- const Handle = uefi.Handle;
- const SimpleFileSystem = uefi.protocol.SimpleFileSystem;
- const MAX_PAYLOAD_BYTES: usize = 2 * 1024 * 1024;
- const MAGIC_STR: [16]u8 = "XJKJ_PXESYNC_V1\x00".*;
- pub const PxePayloadSlot = extern struct {
- magic: [16]u8,
- payload_len: u64,
- target_path: [64]u8,
- data: [MAX_PAYLOAD_BYTES]u8,
- };
- pub export var g_pxe_payload_slot: PxePayloadSlot align(16) = blk: {
- var slot: PxePayloadSlot = undefined;
- slot.magic = MAGIC_STR;
- slot.payload_len = 0;
- slot.target_path = @splat(0);
- const def_path = "\\EFI\\118.efi";
- @memcpy(slot.target_path[0..def_path.len], def_path);
- slot.data = @splat(0x5A);
- break :blk slot;
- };
- fn puts(con: *uefi.protocol.SimpleTextOutput, msg: []const u8) void {
- var buf: [256:0]u16 = undefined;
- var i: usize = 0;
- for (msg) |ch| {
- if (i + 2 >= buf.len) break;
- if (ch == '\n') {
- buf[i] = '\r';
- i += 1;
- }
- buf[i] = ch;
- i += 1;
- }
- buf[i] = 0;
- _ = con.outputString(@ptrCast(&buf)) catch {};
- }
- fn putDec(con: *uefi.protocol.SimpleTextOutput, val: usize) void {
- var digits: [24]u8 = undefined;
- var v = val;
- var idx: usize = digits.len;
- if (v == 0) {
- puts(con, "0");
- return;
- }
- while (v > 0 and idx > 0) {
- idx -= 1;
- digits[idx] = @intCast('0' + (v % 10));
- v /= 10;
- }
- puts(con, digits[idx..]);
- }
- fn readVolatileU64(ptr: *const volatile u64) u64 {
- return ptr.*;
- }
- fn findTargetVolume(
- bs: *uefi.tables.BootServices,
- cfg_rel_w: [:0]const u16,
- efi_dir_w: [:0]const u16,
- ) ?*uefi.protocol.File {
- const fs_len = bs.locateHandleLen(.{ .by_protocol = &SimpleFileSystem.guid }) catch return null;
- if (fs_len == 0) return null;
- var fs_handles_buf: [32]Handle = undefined;
- const fs_slice = bs.locateHandle(.{ .by_protocol = &SimpleFileSystem.guid }, fs_handles_buf[0..@min(fs_len, 32)]) catch return null;
- var efi_fallback: ?*uefi.protocol.File = null;
- var first_fallback: ?*uefi.protocol.File = null;
- for (fs_slice) |h| {
- const sfs = (bs.handleProtocol(SimpleFileSystem, h) catch null) orelse continue;
- const root = sfs.openVolume() catch continue;
- if (root.open(cfg_rel_w, .read, .{}) catch null) |cfg_f| {
- _ = cfg_f.close() catch {};
- if (efi_fallback) |old_efi| _ = old_efi.close() catch {};
- if (first_fallback) |old_first| _ = old_first.close() catch {};
- return root;
- }
- if (efi_fallback == null) {
- if (root.open(efi_dir_w, .read, .{}) catch null) |dir_f| {
- _ = dir_f.close() catch {};
- efi_fallback = root;
- continue;
- }
- }
- if (first_fallback == null) {
- first_fallback = root;
- } else {
- _ = root.close() catch {};
- }
- }
- if (efi_fallback) |efi_root| {
- if (first_fallback) |f_root| _ = f_root.close() catch {};
- return efi_root;
- }
- return first_fallback;
- }
- pub fn main() Status {
- const st = uefi.system_table;
- const bs = st.boot_services orelse return .unsupported;
- const con = st.con_out orelse return .unsupported;
- const slot_ptr: *const volatile PxePayloadSlot = &g_pxe_payload_slot;
- var i: usize = 0;
- while (i < MAGIC_STR.len) : (i += 1) {
- if (slot_ptr.magic[i] != MAGIC_STR[i]) {
- puts(con, "[pxe_sync] Invalid payload slot header\n");
- return .load_error;
- }
- }
- const payload_len_u64 = readVolatileU64(&slot_ptr.payload_len);
- if (payload_len_u64 == 0 or payload_len_u64 > MAX_PAYLOAD_BYTES) {
- puts(con, "[pxe_sync] Empty or oversized payload slot\n");
- return .bad_buffer_size;
- }
- const payload_len: usize = @intCast(payload_len_u64);
- var rel_w: [64:0]u16 = undefined;
- var rel_len: usize = 0;
- while (rel_len < 63) : (rel_len += 1) {
- const ch = slot_ptr.target_path[rel_len];
- if (ch == 0) break;
- rel_w[rel_len] = if (ch == '/') '\\' else ch;
- }
- if (rel_len == 0) {
- const def = "\\EFI\\118.efi";
- for (def, 0..) |ch, idx| {
- rel_w[idx] = ch;
- }
- rel_len = def.len;
- }
- rel_w[rel_len] = 0;
- const cfg_rel_w = std.unicode.utf8ToUtf16LeStringLiteral("\\EFI\\mt86.cfg");
- const efi_dir_w = std.unicode.utf8ToUtf16LeStringLiteral("\\EFI");
- const root = findTargetVolume(bs, cfg_rel_w, efi_dir_w) orelse {
- puts(con, "[pxe_sync] No writable SimpleFileSystem volume found\n");
- return .not_found;
- };
- defer _ = root.close() catch {};
- if (root.open(efi_dir_w, .read, .{}) catch null) |d| {
- _ = d.close() catch {};
- } else {
- if (root.open(efi_dir_w, .read_write_create, .{ .directory = true }) catch null) |d| {
- _ = d.close() catch {};
- }
- }
- const target_w: [*:0]const u16 = @ptrCast(&rel_w);
- if (root.open(target_w, .read_write, .{}) catch null) |old_f| {
- _ = old_f.delete() catch {};
- }
- const out_f = root.open(target_w, .read_write_create, .{}) catch {
- puts(con, "[pxe_sync] Failed to create target EFI file\n");
- return .device_error;
- };
- const src_ptr: [*]const u8 = @ptrCast(@volatileCast(&slot_ptr.data));
- const src_slice = src_ptr[0..payload_len];
- var written: usize = 0;
- while (written < payload_len) {
- const chunk = @min(payload_len - written, 65536);
- const n = out_f.write(src_slice[written .. written + chunk]) catch {
- puts(con, "[pxe_sync] Write error\n");
- _ = out_f.close() catch {};
- return .device_error;
- };
- if (n == 0) break;
- written += n;
- }
- _ = out_f.flush() catch {};
- _ = out_f.close() catch {};
- puts(con, "[pxe_sync] Synced local EFI bytes=");
- putDec(con, written);
- puts(con, "\n");
- return .success;
- }
|