Skip to content

Conversation

@orangecms
Copy link

Signed-off-by: Daniel Maslowski [email protected]

@orangecms orangecms marked this pull request as ready for review October 11, 2022 18:11
@orangecms
Copy link
Author

With a bunch of rework and dependency bumps, I could get the code to compile and run again.

Now I'm seeing if I can get it to execute an actual payload, such as the example app from https://github.com/rust-osdev/uefi-rs.

Note that the QEMU option for a VirtIO SCSI block device has been deprecated:
https://www.qemu.org/docs/master/about/deprecated.html?highlight=virtio%20scsi%20blk#device-virtio-blk-scsi-on-off-since-5-0
With just virtio-scsi, I get a different PCI device ID, 1004. I'm seeing the following then:

RUST_FIRMWARE_FSP_FD_FILE=/home/dama/Projects/Fiedka/uefi-pi-rs/QemuFsp/BuildFsp/QEMU_FSP_RELEASE.fd \
EDK2_PATH=/home/dama/Projects/Fiedka/uefi-pi-rs/QemuFsp \
cargo xbuild --target x86_64-unknown-uefi --release
RUST_FIRMWARE_FSP_FD_FILE=/home/dama/Projects/Fiedka/uefi-pi-rs/QemuFsp/BuildFsp/QEMU_FSP_RELEASE.fd \
cargo run -p rust-firmware-tool -- \
	target/x86_64-unknown-uefi/release/ResetVector.bin \
	target/x86_64-unknown-uefi/release/rust_ipl.efi \
	target/x86_64-unknown-uefi/release/rust-uefi-payload.efi \
	target/x86_64-unknown-uefi/release/final.bin
2022-10-11 20:15:46,122 INFO  [rust_firmware_tool] environment variable: RUST_FIRMWARE_TOOL_FSP_T_FILE not set, use default
2022-10-11 20:15:46,122 INFO  [rust_firmware_tool] environment variable: RUST_FIRMWARE_TOOL_FSP_M_FILE not set, use default
2022-10-11 20:15:46,123 INFO  [rust_firmware_tool] environment variable: RUST_FIRMWARE_TOOL_FSP_S_FILE not set, use default

rust-firmware-tool target/x86_64-unknown-uefi/release/ResetVector.bin target/x86_64-unknown-uefi/release/rust_ipl.efi target/x86_64-unknown-uefi/release/rust-uefi-payload.efi target/x86_64-unknown-uefi/release/final.bin

2022-10-11 20:15:46,124 INFO  [pe_loader::pe] start relocate...
qemu-system-x86_64 -m 4G -machine q35 \
	-drive if=pflash,format=raw,unit=0,file=target/x86_64-unknown-uefi/release/final.bin \
	-drive format=raw,file=../uefi-rs//boot.fat,if=none,id=drive0 \
	-device virtio-scsi-pci,id=scsi \
    -device scsi-hd,drive=boot \
	-drive format=raw,file=fat:rw:boot,if=none,id=boot \
	-serial mon:stdio -nographic -vga none -nic none \
	-kernel ~/Projects/Fiedka/minilb
Starting RUST Based IPL:
    Boot_fv - 0xFFF00000
    Top of stack - 0x79000
    Temp ram base - 0x0
    Temp ram top - 0x7FF00
    Temp page table base - 0x79000
    Initial eax value - 0xFFFFFE04
Call FspMemoryInit
Switch to stack - 0x7DF00000
Continue function - Hob address - 0x7EF20000
Migrate pagetable @ 0x7E700000
Call TempRamExit
Call FspSiliconInit
Payload is pe image
Migrate hobs @ 0x7E800000
Call payload entry - 0x7DF17970
Starting UEFI hob - 0x7e800000
PhitHob:
  version                - 0x9
  boot_mode              - 0x0
  efi_memory_top         - 0x000000007f000000
  efi_memory_bottom      - 0x000000007ef00000
  efi_free_memory_top    - 0x000000007efe4000
  efi_free_memory_bottom - 0x000000007ef20dc0
  efi_end_of_hob_list    - 0x000000007e800e60
Hob:
  header.type            - 0x7
  header.length          - 0x28
Hob:
  header.type            - 0x7
  header.length          - 0x108
Hob:
  header.type            - 0x7
  header.length          - 0x108
Hob:
  header.type            - 0x7
  header.length          - 0x88
Hob:
  header.type            - 0x7
  header.length          - 0x88
Hob:
  header.type            - 0x7
  header.length          - 0x208
Hob:
  header.type            - 0x7
  header.length          - 0x10
Hob:
  header.type            - 0x7
  header.length          - 0x10
Hob:
  header.type            - 0x4
  header.length          - 0xe0
Hob:
  header.type            - 0x4
  header.length          - 0xb8
ResourceDescription 0x00000000 : 0x0000000000000000 - 0x000000000009ffff (0x00003c07)
ResourceDescription 0x00000005 : 0x00000000000a0000 - 0x00000000000fffff (0x00003c07)
ResourceDescription 0x00000000 : 0x0000000000100000 - 0x000000007eefffff (0x00003c07)
ResourceDescription 0x00000005 : 0x000000007ef00000 - 0x000000007effffff (0x00003c07)
ResourceDescription 0x00000000 : 0x0000000100000000 - 0x000000017fffffff (0x00003c03)
ResourceDescription 0x00000005 : 0x000000007f000000 - 0x000000007fffffff (0x00003c07)
MemoryAllocation 0x00000004 : 0x000000007ef00000 - 0x000000007ef1ffff
MemoryAllocation 0x00000003 : 0x000000007effa000 - 0x000000007effffff
Cpu : mem size 40 , io size 16
MemoryAllocation 0x00000004 : 0x000000007eff9000 - 0x000000007eff9fff
Hob:
  header.type            - 0x7
  header.length          - 0x28
Hob:
  header.type            - 0x7
  header.length          - 0x18
Hob:
  header.type            - 0x7
  header.length          - 0x10
Hob:
  header.type            - 0x7
  header.length          - 0x18
MemoryAllocation 0x00000003 : 0x000000007eff6000 - 0x000000007eff8fff
Hob:
  header.type            - 0x4
  header.length          - 0x1b0
MemoryAllocation 0x00000004 : 0x000000007eff2000 - 0x000000007eff5fff
MemoryAllocation 0x00000004 : 0x000000007eff1000 - 0x000000007eff1fff
MemoryAllocation 0x00000003 : 0x000000007eff0000 - 0x000000007eff0fff
MemoryAllocation 0x00000004 : 0x000000007efec000 - 0x000000007efeffff
MemoryAllocation 0x00000004 : 0x000000007efeb000 - 0x000000007efebfff
MemoryAllocation 0x00000003 : 0x000000007efea000 - 0x000000007efeafff
MemoryAllocation 0x00000004 : 0x000000007efe6000 - 0x000000007efe9fff
MemoryAllocation 0x00000004 : 0x000000007efe5000 - 0x000000007efe5fff
MemoryAllocation 0x00000003 : 0x000000007efe4000 - 0x000000007efe4fff
Hob:
  header.type            - 0x4
  header.length          - 0xc0
MemoryAllocation 0x00000004 : 0x000000007e700000 - 0x000000007e7fffff
MemoryAllocation 0x00000003 : 0x000000007df00000 - 0x000000007e17ffff
MemoryAllocation 0x00000004 : 0x000000007d700000 - 0x000000007defffff
FirmwareVolume : 0x00000000ffc00000 - 0x00000000ffffffff
add_initial_allocation 7 : 0x0000000000000000-0x000000000009ffff
add_initial_allocation 7 : 0x0000000000100000-0x000000007eefffff
add_initial_allocation 7 : 0x0000000100000000-0x000000017fffffff
==== find free memory:    32 pages @ 0x7ef00000
rust-uefi-payload/src/efi/alloc.rs:240 out of resource
==== find free memory:     6 pages @ 0x7effa000
rust-uefi-payload/src/efi/alloc.rs:240 out of resource
==== find free memory:     1 pages @ 0x7eff9000
rust-uefi-payload/src/efi/alloc.rs:240 out of resource
==== find free memory:     3 pages @ 0x7eff6000
rust-uefi-payload/src/efi/alloc.rs:240 out of resource
==== find free memory:     4 pages @ 0x7eff2000
rust-uefi-payload/src/efi/alloc.rs:240 out of resource
==== find free memory:     1 pages @ 0x7eff1000
rust-uefi-payload/src/efi/alloc.rs:240 out of resource
==== find free memory:     1 pages @ 0x7eff0000
rust-uefi-payload/src/efi/alloc.rs:240 out of resource
==== find free memory:     4 pages @ 0x7efec000
rust-uefi-payload/src/efi/alloc.rs:240 out of resource
==== find free memory:     1 pages @ 0x7efeb000
rust-uefi-payload/src/efi/alloc.rs:240 out of resource
==== find free memory:     1 pages @ 0x7efea000
rust-uefi-payload/src/efi/alloc.rs:240 out of resource
==== find free memory:     4 pages @ 0x7efe6000
rust-uefi-payload/src/efi/alloc.rs:240 out of resource
==== find free memory:     1 pages @ 0x7efe5000
rust-uefi-payload/src/efi/alloc.rs:240 out of resource
==== find free memory:     1 pages @ 0x7efe4000
rust-uefi-payload/src/efi/alloc.rs:240 out of resource
==== find free memory:   256 pages @ 0x7e700000
==== find free memory:   640 pages @ 0x7df00000
==== find free memory:  2048 pages @ 0x7d700000
==== find free memory:     1 pages @ 0x0
EFI_STUB: install_protocol_interface: Guid { time_low: [193, 119, 116, 56], time_mid: [199, 105], time_hi_and_version: [210, 17], clk_seq_hi_res: 142, clk_seq_low: 57, node: [0, 160, 201, 105, 114, 59] }, handle: 0x0, interface: 0x7df1f000 - new_handle: 0x7e11f3f8 status: Status(0)
EFI_STUB: install_protocol_interface: Guid { time_low: [52, 117, 158, 221], time_mid: [98, 119], time_hi_and_version: [152, 70], clk_seq_hi_res: 140, clk_seq_low: 20, node: [245, 133, 23, 166, 37, 170] }, handle: 0x7e11f3f8, interface: 0x7df1f018 - new_handle: 0x7e11f3f8 status: Status(0)
EFI_STUB: install_protocol_interface: Guid { time_low: [194, 119, 116, 56], time_mid: [199, 105], time_hi_and_version: [210, 17], clk_seq_hi_res: 142, clk_seq_low: 57, node: [0, 160, 201, 105, 114, 59] }, handle: 0x0, interface: 0x7df1f048 - new_handle: 0x7e11f588 status: Status(0)
EFI_STUB: install_protocol_interface: Guid { time_low: [194, 119, 116, 56], time_mid: [199, 105], time_hi_and_version: [210, 17], clk_seq_hi_res: 142, clk_seq_low: 57, node: [0, 160, 201, 105, 114, 59] }, handle: 0x0, interface: 0x7df1f048 - new_handle: 0x7e11f718 status: Status(0)
Found PCI device vendor=8086 device=29c0 in slot=0
Found PCI device vendor=1af4 device=1004 in slot=1
Found PCI device vendor=8086 device=2918 in slot=31
Found PCI device vendor=8086 device=29c0 in slot=0
Found PCI device vendor=1af4 device=1004 in slot=1
Found PCI device vendor=8086 device=2918 in slot=31
PCI Device: 0:1.0 1af4:1004
Bar: type=IoSpace address=0
Bar: type=MemorySpace32 address=0
Bar: type=MemorySpace32 address=0
Bar: type=MemorySpace32 address=0
Bar: type=MemorySpace64 address=0
Bar: type=Unused address=0
io_write 0 4096 20
io_write 0 4096 20
io_write 0 4096 20
io_write 3 4096 0
io_write 3 4096 0
io_write 3 4096 8
io_write 3 4096 12
io_write 3 4096 8
io_write 3 4096 12
io_write 0 4096 20
io_write 1 4096 22
io_write 1 4096 24
io_write 7 4096 32
io_write 7 4096 40
io_write 7 4096 48
io_write 1 4096 28
io_write 0 4096 20
Virtio block device configured. Capacity: 6531726500807662245 sectors
io_write 3 4096 92820
panic ... PanicInfo { payload: Any { .. }, message: Some(assertion failed: (offset + (core::mem::size_of::<T>() - 1) as u64) < self.length), location: Location { file: "rust-uefi-payload/src/mem.rs", line: 144, col: 9 }, can_unwind: true }
QEMU: Terminated

I have added some debug prints for io_write, because it fails at some point. I haven't yet looked any deeper what it means though, but it's cool to see this whole thing here basically working again with a 2022 toolchain. :-)

@orangecms orangecms changed the title WIP: get ready for 2022 Rework to compile with a 2022 toolchain Oct 11, 2022
@jyao1
Copy link
Owner

jyao1 commented Jun 11, 2023

@orangecms, thank you very much for the contribution. It is great to see it works in 2022 toolchain.

Would you please let me know the high level plan on your side?

@orangecms
Copy link
Author

The main goal from my side is to get this to a quality level where it is useful. It may serve as a new basis for simplification.
On the other hand, I do have some ideas, for which a simpler setup would really help:

  • a malware analysis environment; I want to have a setup where I could drop in malicious binaries and run them in QEMU, see what they do, if they fiddle with protocols and tables, leave runtime services or similar
  • monitoring: to test the behavior and correctness of a module, a modified PEI core / DXE core that allows for inserting stubs would be decent, asserting on MMIO, port write, MSR access, etc
  • reverse engineering to fix issues vendors are not taking care of: many consumers are asking me for custom firmware for their devices, preferably not UEFI, so dumping IO access etc would help with porting

For most of this, I have ideas for Fiedka, noted down in the following issues:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants