morpheus_network/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
//! MorpheusX Network Stack
//!
//! ASM-first bare-metal HTTP client for bootloader environments.
//! Designed for post-ExitBootServices execution with no firmware dependencies.
//!
//! # Architecture (ASM-First)
//!
//! ```text
//! ┌─────────────────────────────────────────────────────────────┐
//! │                    HTTP Client                              │
//! │            NativeHttpClient (bare metal TCP/IP)             │
//! └─────────────────────────────────────────────────────────────┘
//!                              │
//!                              ▼
//! ┌─────────────────────────────────────────────────────────────┐
//! │              NetInterface (smoltcp TCP/IP stack)            │
//! │  DHCP | TCP sockets | IP routing | ARP                      │
//! └─────────────────────────────────────────────────────────────┘
//!                              │
//!                              ▼
//! ┌─────────────────────────────────────────────────────────────┐
//! │              NetworkDevice trait                            │
//! │  Abstraction over hardware: transmit, receive, mac_address │
//! └─────────────────────────────────────────────────────────────┘
//!                              │
//!                              ▼
//! ┌─────────────────────────────────────────────────────────────┐
//! │              VirtioNetDriver (ASM-backed)                   │
//! │  Uses hand-written assembly for all hardware access         │
//! │  - MMIO with explicit barriers                              │
//! │  - Virtqueue descriptor ring management                     │
//! │  - Fire-and-forget TX, poll-based RX                        │
//! └─────────────────────────────────────────────────────────────┘
//!                              │
//!                              ▼
//! ┌─────────────────────────────────────────────────────────────┐
//! │              ASM Layer (network/asm/)                       │
//! │  Core: TSC, barriers, MMIO, PIO, cache                      │
//! │  VirtIO: init, queue, TX, RX, notify                        │
//! └─────────────────────────────────────────────────────────────┘
//! ```
//!
//! # Usage (Post-ExitBootServices)
//!
//! ```ignore
//! use morpheus_network::{VirtioNetDriver, VirtioConfig, NetworkDriver};
//! use morpheus_network::boot::BootHandoff;
//! use morpheus_network::mainloop::bare_metal_main;
//!
//! // After ExitBootServices, with BootHandoff from pre-EBS setup:
//! let result = unsafe { bare_metal_main(handoff, config) };
//! ```

#![no_std]
#![allow(dead_code)]
#![allow(unused_imports)]
// Allow never_loop - our poll-based state machines intentionally return
// from loops early (single-threaded cooperative polling pattern)
#![allow(clippy::never_loop)]

extern crate alloc;

// ═══════════════════════════════════════════════════════════════
// POST-EBS ALLOCATOR (must be first - provides global allocator)
// ═══════════════════════════════════════════════════════════════
pub mod alloc_heap;

// ═══════════════════════════════════════════════════════════════
// CORE MODULES
// ═══════════════════════════════════════════════════════════════
pub mod client;
pub mod device;
pub mod error;
pub mod http;
pub mod stack;
pub mod transfer;
pub mod url;

// ═══════════════════════════════════════════════════════════════
// ASM-FIRST BARE-METAL MODULES
// These provide post-ExitBootServices network support using
// hand-written assembly for all hardware access.
// ═══════════════════════════════════════════════════════════════
pub mod asm; // ASM bindings (TSC, MMIO, PIO, barriers)
pub mod boot; // Boot handoff and initialization
pub mod dma; // DMA buffer management with ownership tracking
pub mod driver; // Driver abstraction and implementations
pub mod mainloop; // 5-phase poll loop
pub mod pci;
pub mod state; // State machines (DHCP, TCP, HTTP, etc.)
pub mod time; // Timing utilities
pub mod types; // Shared types (#[repr(C)] structs) // PCI bus access

// ═══════════════════════════════════════════════════════════════
// RE-EXPORTS
// ═══════════════════════════════════════════════════════════════

// Core types
pub use device::NetworkDevice;
pub use error::{NetworkError, Result};
pub use types::{HttpMethod, ProgressCallback};

// Client
pub use client::HttpClient;
pub use client::NativeHttpClient;

// Stack (smoltcp integration)
pub use stack::{debug_log, debug_log_available, debug_log_clear, debug_log_pop, DebugLogEntry};
pub use stack::{debug_stage, set_debug_stage}; // Debug stage tracking
pub use stack::{ecam_bases, DeviceAdapter, NetConfig, NetInterface, NetState}; // Ring buffer logging

// ASM-backed VirtIO driver (primary driver for post-EBS execution)
pub use driver::{NetworkDriver as AsmNetworkDriver, VirtioConfig, VirtioNetDriver};

// Standalone assembly functions
#[cfg(target_arch = "x86_64")]
pub use device::pci::{pci_io_test, read_tsc, tsc_delay_us};

// ===================== Serial Debug Output =====================
// Write directly to COM1 (0x3f8) for QEMU -serial stdio debugging
// This works even when the main display is blocked

/// Write a single byte to COM1 serial port (non-blocking with bounded wait)
#[cfg(target_arch = "x86_64")]
#[inline(always)]
pub fn serial_byte(b: u8) {
    unsafe {
        // Bounded wait for TX buffer empty - max ~100 iterations
        // If serial port doesn't exist, we just skip the write
        let mut retries = 0u32;
        loop {
            let status: u8;
            core::arch::asm!(
                "in al, dx",
                in("dx") 0x3fdu16,  // COM1 + 5 = line status register
                out("al") status,
                options(nostack, preserves_flags)
            );
            if status & 0x20 != 0 {
                // TX buffer empty, safe to write
                core::arch::asm!(
                    "out dx, al",
                    in("dx") 0x3f8u16,  // COM1 data register
                    in("al") b,
                    options(nostack, preserves_flags)
                );
                return;
            }
            retries += 1;
            if retries > 100 {
                // Serial port not responding - abandon write
                return;
            }
            core::hint::spin_loop();
        }
    }
}

/// Write a string to COM1 serial port
#[cfg(target_arch = "x86_64")]
pub fn serial_str(s: &str) {
    for b in s.bytes() {
        serial_byte(b);
    }
}

/// Write a u32 as decimal to serial
#[cfg(target_arch = "x86_64")]
pub fn serial_u32(n: u32) {
    if n == 0 {
        serial_byte(b'0');
        return;
    }
    let mut buf = [0u8; 10];
    let mut i = 0;
    let mut val = n;
    while val > 0 {
        buf[i] = b'0' + (val % 10) as u8;
        val /= 10;
        i += 1;
    }
    while i > 0 {
        i -= 1;
        serial_byte(buf[i]);
    }
}

/// Log debug stage to serial - format: `[NET:XX] message`
#[cfg(target_arch = "x86_64")]
pub fn serial_stage(stage: u32, msg: &str) {
    serial_str("[NET:");
    serial_u32(stage);
    serial_str("] ");
    serial_str(msg);
    serial_byte(b'\n');
}