pxe_sync.zig 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. const std = @import("std");
  2. const uefi = std.os.uefi;
  3. const Status = uefi.Status;
  4. const Handle = uefi.Handle;
  5. const SimpleFileSystem = uefi.protocol.SimpleFileSystem;
  6. const MAX_PAYLOAD_BYTES: usize = 2 * 1024 * 1024;
  7. const MAGIC_STR: [16]u8 = "XJKJ_PXESYNC_V1\x00".*;
  8. pub const PxePayloadSlot = extern struct {
  9. magic: [16]u8,
  10. payload_len: u64,
  11. target_path: [64]u8,
  12. data: [MAX_PAYLOAD_BYTES]u8,
  13. };
  14. pub export var g_pxe_payload_slot: PxePayloadSlot align(16) = blk: {
  15. var slot: PxePayloadSlot = undefined;
  16. slot.magic = MAGIC_STR;
  17. slot.payload_len = 0;
  18. slot.target_path = @splat(0);
  19. const def_path = "\\EFI\\118.efi";
  20. @memcpy(slot.target_path[0..def_path.len], def_path);
  21. slot.data = @splat(0x5A);
  22. break :blk slot;
  23. };
  24. fn puts(con: *uefi.protocol.SimpleTextOutput, msg: []const u8) void {
  25. var buf: [256:0]u16 = undefined;
  26. var i: usize = 0;
  27. for (msg) |ch| {
  28. if (i + 2 >= buf.len) break;
  29. if (ch == '\n') {
  30. buf[i] = '\r';
  31. i += 1;
  32. }
  33. buf[i] = ch;
  34. i += 1;
  35. }
  36. buf[i] = 0;
  37. _ = con.outputString(@ptrCast(&buf)) catch {};
  38. }
  39. fn putDec(con: *uefi.protocol.SimpleTextOutput, val: usize) void {
  40. var digits: [24]u8 = undefined;
  41. var v = val;
  42. var idx: usize = digits.len;
  43. if (v == 0) {
  44. puts(con, "0");
  45. return;
  46. }
  47. while (v > 0 and idx > 0) {
  48. idx -= 1;
  49. digits[idx] = @intCast('0' + (v % 10));
  50. v /= 10;
  51. }
  52. puts(con, digits[idx..]);
  53. }
  54. fn readVolatileU64(ptr: *const volatile u64) u64 {
  55. return ptr.*;
  56. }
  57. fn findTargetVolume(
  58. bs: *uefi.tables.BootServices,
  59. cfg_rel_w: [:0]const u16,
  60. efi_dir_w: [:0]const u16,
  61. ) ?*uefi.protocol.File {
  62. const fs_len = bs.locateHandleLen(.{ .by_protocol = &SimpleFileSystem.guid }) catch return null;
  63. if (fs_len == 0) return null;
  64. var fs_handles_buf: [32]Handle = undefined;
  65. const fs_slice = bs.locateHandle(.{ .by_protocol = &SimpleFileSystem.guid }, fs_handles_buf[0..@min(fs_len, 32)]) catch return null;
  66. var efi_fallback: ?*uefi.protocol.File = null;
  67. var first_fallback: ?*uefi.protocol.File = null;
  68. for (fs_slice) |h| {
  69. const sfs = (bs.handleProtocol(SimpleFileSystem, h) catch null) orelse continue;
  70. const root = sfs.openVolume() catch continue;
  71. if (root.open(cfg_rel_w, .read, .{}) catch null) |cfg_f| {
  72. _ = cfg_f.close() catch {};
  73. if (efi_fallback) |old_efi| _ = old_efi.close() catch {};
  74. if (first_fallback) |old_first| _ = old_first.close() catch {};
  75. return root;
  76. }
  77. if (efi_fallback == null) {
  78. if (root.open(efi_dir_w, .read, .{}) catch null) |dir_f| {
  79. _ = dir_f.close() catch {};
  80. efi_fallback = root;
  81. continue;
  82. }
  83. }
  84. if (first_fallback == null) {
  85. first_fallback = root;
  86. } else {
  87. _ = root.close() catch {};
  88. }
  89. }
  90. if (efi_fallback) |efi_root| {
  91. if (first_fallback) |f_root| _ = f_root.close() catch {};
  92. return efi_root;
  93. }
  94. return first_fallback;
  95. }
  96. pub fn main() Status {
  97. const st = uefi.system_table;
  98. const bs = st.boot_services orelse return .unsupported;
  99. const con = st.con_out orelse return .unsupported;
  100. const slot_ptr: *const volatile PxePayloadSlot = &g_pxe_payload_slot;
  101. var i: usize = 0;
  102. while (i < MAGIC_STR.len) : (i += 1) {
  103. if (slot_ptr.magic[i] != MAGIC_STR[i]) {
  104. puts(con, "[pxe_sync] Invalid payload slot header\n");
  105. return .load_error;
  106. }
  107. }
  108. const payload_len_u64 = readVolatileU64(&slot_ptr.payload_len);
  109. if (payload_len_u64 == 0 or payload_len_u64 > MAX_PAYLOAD_BYTES) {
  110. puts(con, "[pxe_sync] Empty or oversized payload slot\n");
  111. return .bad_buffer_size;
  112. }
  113. const payload_len: usize = @intCast(payload_len_u64);
  114. var rel_w: [64:0]u16 = undefined;
  115. var rel_len: usize = 0;
  116. while (rel_len < 63) : (rel_len += 1) {
  117. const ch = slot_ptr.target_path[rel_len];
  118. if (ch == 0) break;
  119. rel_w[rel_len] = if (ch == '/') '\\' else ch;
  120. }
  121. if (rel_len == 0) {
  122. const def = "\\EFI\\118.efi";
  123. for (def, 0..) |ch, idx| {
  124. rel_w[idx] = ch;
  125. }
  126. rel_len = def.len;
  127. }
  128. rel_w[rel_len] = 0;
  129. const cfg_rel_w = std.unicode.utf8ToUtf16LeStringLiteral("\\EFI\\mt86.cfg");
  130. const efi_dir_w = std.unicode.utf8ToUtf16LeStringLiteral("\\EFI");
  131. const root = findTargetVolume(bs, cfg_rel_w, efi_dir_w) orelse {
  132. puts(con, "[pxe_sync] No writable SimpleFileSystem volume found\n");
  133. return .not_found;
  134. };
  135. defer _ = root.close() catch {};
  136. if (root.open(efi_dir_w, .read, .{}) catch null) |d| {
  137. _ = d.close() catch {};
  138. } else {
  139. if (root.open(efi_dir_w, .read_write_create, .{ .directory = true }) catch null) |d| {
  140. _ = d.close() catch {};
  141. }
  142. }
  143. const target_w: [*:0]const u16 = @ptrCast(&rel_w);
  144. if (root.open(target_w, .read_write, .{}) catch null) |old_f| {
  145. _ = old_f.delete() catch {};
  146. }
  147. const out_f = root.open(target_w, .read_write_create, .{}) catch {
  148. puts(con, "[pxe_sync] Failed to create target EFI file\n");
  149. return .device_error;
  150. };
  151. const src_ptr: [*]const u8 = @ptrCast(@volatileCast(&slot_ptr.data));
  152. const src_slice = src_ptr[0..payload_len];
  153. var written: usize = 0;
  154. while (written < payload_len) {
  155. const chunk = @min(payload_len - written, 65536);
  156. const n = out_f.write(src_slice[written .. written + chunk]) catch {
  157. puts(con, "[pxe_sync] Write error\n");
  158. _ = out_f.close() catch {};
  159. return .device_error;
  160. };
  161. if (n == 0) break;
  162. written += n;
  163. }
  164. _ = out_f.flush() catch {};
  165. _ = out_f.close() catch {};
  166. puts(con, "[pxe_sync] Synced local EFI bytes=");
  167. putDec(con, written);
  168. puts(con, "\n");
  169. return .success;
  170. }