mirror of
https://github.com/ps5-linux/ps5-linux-loader.git
synced 2026-08-13 23:18:29 -07:00
Format code.
This commit is contained in:
+40
-40
@@ -1,40 +1,40 @@
|
||||
ifndef PS5_PAYLOAD_SDK
|
||||
PS5_PAYLOAD_SDK = /opt/ps5-payload-sdk/
|
||||
endif
|
||||
|
||||
|
||||
ifeq ($(shell uname -m),aarch64)
|
||||
CC = x86_64-linux-gnu-gcc
|
||||
LD = x86_64-linux-gnu-ld
|
||||
OBJCOPY = x86_64-linux-gnu-objcopy
|
||||
else
|
||||
CC = gcc
|
||||
LD = ld
|
||||
OBJCOPY = objcopy
|
||||
endif
|
||||
CFLAGS = -O2 -fno-stack-protector -ffreestanding -nostdlib -m64 -I$(PS5_PAYLOAD_SDK)/target/include
|
||||
LDFLAGS = -T linker.ld
|
||||
TARGET = shellcode_kernel.elf
|
||||
TEXT_BIN = shellcode_text.bin
|
||||
|
||||
SRC = main.c utils.c kernel_code.c
|
||||
OBJ = $(SRC:.c=.o)
|
||||
|
||||
dump = shellcode_kernel.h
|
||||
|
||||
all: $(dump)
|
||||
|
||||
$(TARGET): $(OBJ)
|
||||
$(CC) $(CFLAGS) $(LDFLAGS) $(OBJ) -o $(TARGET)
|
||||
|
||||
%.o: %.c
|
||||
$(CC) $(CFLAGS) -c $< -o $@
|
||||
|
||||
$(TEXT_BIN): $(TARGET)
|
||||
$(OBJCOPY) -O binary -j .text $(TARGET) $(TEXT_BIN)
|
||||
|
||||
clean:
|
||||
rm -f $(OBJ) $(TARGET) $(TEXT_BIN) $(dump)
|
||||
|
||||
$(dump): $(TEXT_BIN)
|
||||
python3 bin_to_c_kernel.py $(TEXT_BIN)
|
||||
ifndef PS5_PAYLOAD_SDK
|
||||
PS5_PAYLOAD_SDK = /opt/ps5-payload-sdk/
|
||||
endif
|
||||
|
||||
|
||||
ifeq ($(shell uname -m),aarch64)
|
||||
CC = x86_64-linux-gnu-gcc
|
||||
LD = x86_64-linux-gnu-ld
|
||||
OBJCOPY = x86_64-linux-gnu-objcopy
|
||||
else
|
||||
CC = gcc
|
||||
LD = ld
|
||||
OBJCOPY = objcopy
|
||||
endif
|
||||
CFLAGS = -O2 -fno-stack-protector -ffreestanding -nostdlib -m64 -I$(PS5_PAYLOAD_SDK)/target/include
|
||||
LDFLAGS = -T linker.ld
|
||||
TARGET = shellcode_kernel.elf
|
||||
TEXT_BIN = shellcode_text.bin
|
||||
|
||||
SRC = main.c utils.c kernel_code.c
|
||||
OBJ = $(SRC:.c=.o)
|
||||
|
||||
dump = shellcode_kernel.h
|
||||
|
||||
all: $(dump)
|
||||
|
||||
$(TARGET): $(OBJ)
|
||||
$(CC) $(CFLAGS) $(LDFLAGS) $(OBJ) -o $(TARGET)
|
||||
|
||||
%.o: %.c
|
||||
$(CC) $(CFLAGS) -c $< -o $@
|
||||
|
||||
$(TEXT_BIN): $(TARGET)
|
||||
$(OBJCOPY) -O binary -j .text $(TARGET) $(TEXT_BIN)
|
||||
|
||||
clean:
|
||||
rm -f $(OBJ) $(TARGET) $(TEXT_BIN) $(dump)
|
||||
|
||||
$(dump): $(TEXT_BIN)
|
||||
python3 bin_to_c_kernel.py $(TEXT_BIN)
|
||||
|
||||
@@ -1,48 +1,48 @@
|
||||
import sys
|
||||
import os
|
||||
|
||||
def create_shellcode_header(input_file_text):
|
||||
if not os.path.exists(input_file_text):
|
||||
print(f"Error: {input_file_text} not found.")
|
||||
return
|
||||
|
||||
# Read binary data_text
|
||||
with open(input_file_text, "rb") as f:
|
||||
data_text = f.read()
|
||||
|
||||
# Hardcoded output name
|
||||
output_name = "shellcode_kernel.h"
|
||||
array_name_text = "shellcode_kernel_text"
|
||||
|
||||
with open(output_name, "w") as f:
|
||||
f.write(f"// Generated from {input_file_text}\n")
|
||||
f.write(f"#ifndef SHELLCODE_KERNEL_H\n")
|
||||
f.write(f"#define SHELLCODE_KERNEL_H\n\n")
|
||||
f.write(f"#include <unistd.h>\n\n")
|
||||
|
||||
f.write(f"#include \"shellcode_kernel_args.h\"\n\n")
|
||||
|
||||
f.write(f"uint8_t {array_name_text}[] = {{\n ")
|
||||
|
||||
for i, byte in enumerate(data_text):
|
||||
f.write(f"0x{byte:02X}")
|
||||
|
||||
if i < len(data_text) - 1:
|
||||
f.write(", ")
|
||||
|
||||
# New line every 12 bytes
|
||||
if (i + 1) % 12 == 0:
|
||||
f.write("\n ")
|
||||
|
||||
f.write(f"\n}};\n\n")
|
||||
f.write(f"uint64_t {array_name_text}_len = {len(data_text)};\n\n")
|
||||
|
||||
f.write(f"#endif // SHELLCODE_KERNEL_H\n")
|
||||
|
||||
print(f"Done! Created {output_name} ({len(data_text)} bytes)")
|
||||
|
||||
if __name__ == "__main__":
|
||||
if len(sys.argv) < 2:
|
||||
print("Usage: python bin_to_c_kernel.py <text.bin>")
|
||||
else:
|
||||
create_shellcode_header(sys.argv[1])
|
||||
import sys
|
||||
import os
|
||||
|
||||
def create_shellcode_header(input_file_text):
|
||||
if not os.path.exists(input_file_text):
|
||||
print(f"Error: {input_file_text} not found.")
|
||||
return
|
||||
|
||||
# Read binary data_text
|
||||
with open(input_file_text, "rb") as f:
|
||||
data_text = f.read()
|
||||
|
||||
# Hardcoded output name
|
||||
output_name = "shellcode_kernel.h"
|
||||
array_name_text = "shellcode_kernel_text"
|
||||
|
||||
with open(output_name, "w") as f:
|
||||
f.write(f"// Generated from {input_file_text}\n")
|
||||
f.write(f"#ifndef SHELLCODE_KERNEL_H\n")
|
||||
f.write(f"#define SHELLCODE_KERNEL_H\n\n")
|
||||
f.write(f"#include <unistd.h>\n\n")
|
||||
|
||||
f.write(f"#include \"shellcode_kernel_args.h\"\n\n")
|
||||
|
||||
f.write(f"uint8_t {array_name_text}[] = {{\n ")
|
||||
|
||||
for i, byte in enumerate(data_text):
|
||||
f.write(f"0x{byte:02X}")
|
||||
|
||||
if i < len(data_text) - 1:
|
||||
f.write(", ")
|
||||
|
||||
# New line every 12 bytes
|
||||
if (i + 1) % 12 == 0:
|
||||
f.write("\n ")
|
||||
|
||||
f.write(f"\n}};\n\n")
|
||||
f.write(f"uint64_t {array_name_text}_len = {len(data_text)};\n\n")
|
||||
|
||||
f.write(f"#endif // SHELLCODE_KERNEL_H\n")
|
||||
|
||||
print(f"Done! Created {output_name} ({len(data_text)} bytes)")
|
||||
|
||||
if __name__ == "__main__":
|
||||
if len(sys.argv) < 2:
|
||||
print("Usage: python bin_to_c_kernel.py <text.bin>")
|
||||
else:
|
||||
create_shellcode_header(sys.argv[1])
|
||||
|
||||
@@ -1,18 +1,18 @@
|
||||
#ifndef KERNEL_CODE_H
|
||||
#define KERNEL_CODE_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#define ALIGN_UP(size, align) (((size) + (align) - 1) & ~((align) - 1))
|
||||
|
||||
static int dp_enable_link_phy(int lanenum, int linkrate);
|
||||
static void patch_hv(void);
|
||||
void boot_linux(void);
|
||||
|
||||
extern int (*transmitter_control)(int cmd, void *control);
|
||||
extern int (*mp3_initialize)(int vmid);
|
||||
extern int (*mp3_invoke)(int cmd_id, void *req, void *rsp);
|
||||
|
||||
extern uint64_t g_vbios; // for main.c
|
||||
|
||||
#endif
|
||||
#ifndef KERNEL_CODE_H
|
||||
#define KERNEL_CODE_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#define ALIGN_UP(size, align) (((size) + (align) - 1) & ~((align) - 1))
|
||||
|
||||
static int dp_enable_link_phy(int lanenum, int linkrate);
|
||||
static void patch_hv(void);
|
||||
void boot_linux(void);
|
||||
|
||||
extern int (*transmitter_control)(int cmd, void *control);
|
||||
extern int (*mp3_initialize)(int vmid);
|
||||
extern int (*mp3_invoke)(int cmd_id, void *req, void *rsp);
|
||||
|
||||
extern uint64_t g_vbios; // for main.c
|
||||
|
||||
#endif
|
||||
|
||||
+17
-17
@@ -1,18 +1,18 @@
|
||||
/* linker.ld */
|
||||
ENTRY(main)
|
||||
|
||||
SECTIONS
|
||||
{
|
||||
. = 0x1000; /* 0x1000 to avoid warnings from linker */
|
||||
.text :
|
||||
{
|
||||
*(.entry_point)
|
||||
*(.text)
|
||||
*(.text.*)
|
||||
*(.data)
|
||||
*(.data.*)
|
||||
*(.rodata*)
|
||||
*(.bss)
|
||||
*(.bss.*)
|
||||
}
|
||||
/* linker.ld */
|
||||
ENTRY(main)
|
||||
|
||||
SECTIONS
|
||||
{
|
||||
. = 0x1000; /* 0x1000 to avoid warnings from linker */
|
||||
.text :
|
||||
{
|
||||
*(.entry_point)
|
||||
*(.text)
|
||||
*(.text.*)
|
||||
*(.data)
|
||||
*(.data.*)
|
||||
*(.rodata*)
|
||||
*(.bss)
|
||||
*(.bss.*)
|
||||
}
|
||||
}
|
||||
+291
-290
File diff suppressed because it is too large
Load Diff
+65
-65
@@ -1,65 +1,65 @@
|
||||
#ifndef MAIN_H
|
||||
#define MAIN_H
|
||||
#include "shellcode_kernel_args.h"
|
||||
#include <stdint.h>
|
||||
|
||||
void (*printf)(const char *format, ...);
|
||||
uint32_t (*AcpiSetFirmwareWakingVector)(uint64_t PhysicalAddress,
|
||||
uint64_t PhysicalAddress64);
|
||||
uint64_t (*kernel_va_to_pa)(uint64_t va);
|
||||
uint32_t (*hv_iommu_set_buffers)(uint64_t cb2_pa, uint64_t cb3_pa,
|
||||
uint64_t eb_pa, uint64_t unk, int *n_devices);
|
||||
uint32_t (*hv_iommu_wait_completion)(void);
|
||||
void (*smp_rendezvous)(void (*setup_func)(void), void (*action_func)(void),
|
||||
void (*teardown_func)(void), void *arg);
|
||||
void (*smp_no_rendevous_barrier)(void);
|
||||
|
||||
// We are being called instead of AcpiSetFirmwareWakingVector from
|
||||
// acpi_wakeup_machdep
|
||||
uint32_t main(uint64_t add1, uint64_t add2);
|
||||
|
||||
uint64_t rdmsr(uint32_t msr);
|
||||
|
||||
// tmr via ecam b0d18f2
|
||||
#ifndef ECAM_B0D18F2
|
||||
#define ECAM_B0D18F2 (0xF0000000ULL + 0x18ULL * 0x8000 + 2 * 0x1000)
|
||||
#define TMR_INDEX_OFF 0x80
|
||||
#define TMR_DATA_OFF 0x84
|
||||
#endif
|
||||
|
||||
// tmr layout (hardware)
|
||||
#define TMR_BASE(n) ((n) * 0x10 + 0x00)
|
||||
#define TMR_LIMIT(n) ((n) * 0x10 + 0x04)
|
||||
#define TMR_CONFIG(n) ((n) * 0x10 + 0x08)
|
||||
#define TMR_REQUESTORS(n) ((n) * 0x10 + 0x0C)
|
||||
#define TMR_CFG_PERMISSIVE 0x3F07
|
||||
#define MAX_TMR 22
|
||||
#define MAX_SAVED_TMRS 8
|
||||
|
||||
uint32_t tmr_read(uint64_t dmap, uint32_t addr);
|
||||
void tmr_write(uint64_t dmap, uint32_t addr, uint32_t val);
|
||||
int tmr_disable(uint64_t dmap);
|
||||
|
||||
// Command buffer MMIO offsets
|
||||
#define IOMMU_MMIO_CB_HEAD 0xa000
|
||||
#define IOMMU_MMIO_CB_TAIL 0xa008
|
||||
|
||||
// Queue constants
|
||||
#define IOMMU_CB_SIZE 0x2000
|
||||
#define IOMMU_CB_MASK (IOMMU_CB_SIZE - 1)
|
||||
#define IOMMU_CMD_ENTRY_SIZE 0x10
|
||||
|
||||
// Submit a single 16-byte command and wait for completion
|
||||
void iommu_submit_cmd(volatile shellcode_kernel_args *args_ptr, uint64_t *cmd);
|
||||
// Write 8 bytes to a physical address using IOMMU completion wait store
|
||||
void iommu_write8_pa(volatile shellcode_kernel_args *args_ptr, uint64_t pa,
|
||||
uint64_t val);
|
||||
|
||||
void patch_vmcb(volatile shellcode_kernel_args *args_ptr);
|
||||
|
||||
#define NULL (void *)0
|
||||
void vmmcall_dummy(void);
|
||||
void halt(void);
|
||||
void init_global_pointers(volatile shellcode_kernel_args *args_ptr);
|
||||
|
||||
#endif
|
||||
#ifndef MAIN_H
|
||||
#define MAIN_H
|
||||
#include "shellcode_kernel_args.h"
|
||||
#include <stdint.h>
|
||||
|
||||
void (*printf)(const char *format, ...);
|
||||
uint32_t (*AcpiSetFirmwareWakingVector)(uint64_t PhysicalAddress,
|
||||
uint64_t PhysicalAddress64);
|
||||
uint64_t (*kernel_va_to_pa)(uint64_t va);
|
||||
uint32_t (*hv_iommu_set_buffers)(uint64_t cb2_pa, uint64_t cb3_pa,
|
||||
uint64_t eb_pa, uint64_t unk, int *n_devices);
|
||||
uint32_t (*hv_iommu_wait_completion)(void);
|
||||
void (*smp_rendezvous)(void (*setup_func)(void), void (*action_func)(void),
|
||||
void (*teardown_func)(void), void *arg);
|
||||
void (*smp_no_rendevous_barrier)(void);
|
||||
|
||||
// We are being called instead of AcpiSetFirmwareWakingVector from
|
||||
// acpi_wakeup_machdep
|
||||
uint32_t main(uint64_t add1, uint64_t add2);
|
||||
|
||||
uint64_t rdmsr(uint32_t msr);
|
||||
|
||||
// tmr via ecam b0d18f2
|
||||
#ifndef ECAM_B0D18F2
|
||||
#define ECAM_B0D18F2 (0xF0000000ULL + 0x18ULL * 0x8000 + 2 * 0x1000)
|
||||
#define TMR_INDEX_OFF 0x80
|
||||
#define TMR_DATA_OFF 0x84
|
||||
#endif
|
||||
|
||||
// tmr layout (hardware)
|
||||
#define TMR_BASE(n) ((n) * 0x10 + 0x00)
|
||||
#define TMR_LIMIT(n) ((n) * 0x10 + 0x04)
|
||||
#define TMR_CONFIG(n) ((n) * 0x10 + 0x08)
|
||||
#define TMR_REQUESTORS(n) ((n) * 0x10 + 0x0C)
|
||||
#define TMR_CFG_PERMISSIVE 0x3F07
|
||||
#define MAX_TMR 22
|
||||
#define MAX_SAVED_TMRS 8
|
||||
|
||||
uint32_t tmr_read(uint64_t dmap, uint32_t addr);
|
||||
void tmr_write(uint64_t dmap, uint32_t addr, uint32_t val);
|
||||
int tmr_disable(uint64_t dmap);
|
||||
|
||||
// Command buffer MMIO offsets
|
||||
#define IOMMU_MMIO_CB_HEAD 0xa000
|
||||
#define IOMMU_MMIO_CB_TAIL 0xa008
|
||||
|
||||
// Queue constants
|
||||
#define IOMMU_CB_SIZE 0x2000
|
||||
#define IOMMU_CB_MASK (IOMMU_CB_SIZE - 1)
|
||||
#define IOMMU_CMD_ENTRY_SIZE 0x10
|
||||
|
||||
// Submit a single 16-byte command and wait for completion
|
||||
void iommu_submit_cmd(volatile shellcode_kernel_args *args_ptr, uint64_t *cmd);
|
||||
// Write 8 bytes to a physical address using IOMMU completion wait store
|
||||
void iommu_write8_pa(volatile shellcode_kernel_args *args_ptr, uint64_t pa,
|
||||
uint64_t val);
|
||||
|
||||
void patch_vmcb(volatile shellcode_kernel_args *args_ptr);
|
||||
|
||||
#define NULL (void *)0
|
||||
void vmmcall_dummy(void);
|
||||
void halt(void);
|
||||
void init_global_pointers(volatile shellcode_kernel_args *args_ptr);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,37 +1,37 @@
|
||||
// This file is shared between main payload and kernel shellcode
|
||||
#ifndef SHELLCODE_KERNEL_ARGS_H
|
||||
#define SHELLCODE_KERNEL_ARGS_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
typedef struct {
|
||||
uint32_t fw_version;
|
||||
uint64_t ktext;
|
||||
uint64_t kdata;
|
||||
uint64_t dmap_base;
|
||||
uint64_t fun_printf;
|
||||
uint64_t fun_va_to_pa;
|
||||
uint64_t fun_hv_iommu_set_buffers;
|
||||
uint64_t fun_hv_iommu_wait_completion;
|
||||
uint64_t fun_acpi_set_fw_waking_vector;
|
||||
uint64_t fun_smp_rendezvous;
|
||||
uint64_t fun_smp_no_rendevous_barrier;
|
||||
uint64_t fun_transmitter_control;
|
||||
uint64_t fun_mp3_initialize;
|
||||
uint64_t fun_mp3_invoke;
|
||||
uint64_t g_vbios;
|
||||
uint64_t iommu_mmio_va;
|
||||
uint64_t iommu_cb2_va;
|
||||
uint64_t iommu_cb3_va;
|
||||
uint64_t iommu_eb_va;
|
||||
uint64_t vmcb[16];
|
||||
uint64_t kernel_uart_override;
|
||||
uint64_t hv_handle_vmexit_pa;
|
||||
uint64_t hv_code_cave_pa;
|
||||
uint64_t hv_uart_override_pa;
|
||||
uint64_t linux_info_va; // To relocate by kernel shellcode
|
||||
} shellcode_kernel_args;
|
||||
|
||||
extern shellcode_kernel_args args; // Declared on main.c
|
||||
|
||||
#endif
|
||||
// This file is shared between main payload and kernel shellcode
|
||||
#ifndef SHELLCODE_KERNEL_ARGS_H
|
||||
#define SHELLCODE_KERNEL_ARGS_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
typedef struct {
|
||||
uint32_t fw_version;
|
||||
uint64_t ktext;
|
||||
uint64_t kdata;
|
||||
uint64_t dmap_base;
|
||||
uint64_t fun_printf;
|
||||
uint64_t fun_va_to_pa;
|
||||
uint64_t fun_hv_iommu_set_buffers;
|
||||
uint64_t fun_hv_iommu_wait_completion;
|
||||
uint64_t fun_acpi_set_fw_waking_vector;
|
||||
uint64_t fun_smp_rendezvous;
|
||||
uint64_t fun_smp_no_rendevous_barrier;
|
||||
uint64_t fun_transmitter_control;
|
||||
uint64_t fun_mp3_initialize;
|
||||
uint64_t fun_mp3_invoke;
|
||||
uint64_t g_vbios;
|
||||
uint64_t iommu_mmio_va;
|
||||
uint64_t iommu_cb2_va;
|
||||
uint64_t iommu_cb3_va;
|
||||
uint64_t iommu_eb_va;
|
||||
uint64_t vmcb[16];
|
||||
uint64_t kernel_uart_override;
|
||||
uint64_t hv_handle_vmexit_pa;
|
||||
uint64_t hv_code_cave_pa;
|
||||
uint64_t hv_uart_override_pa;
|
||||
uint64_t linux_info_va; // To relocate by kernel shellcode
|
||||
} shellcode_kernel_args;
|
||||
|
||||
extern shellcode_kernel_args args; // Declared on main.c
|
||||
|
||||
#endif
|
||||
|
||||
+77
-77
@@ -1,77 +1,77 @@
|
||||
#include "utils.h"
|
||||
#include "shellcode_kernel_args.h"
|
||||
|
||||
extern shellcode_kernel_args args;
|
||||
|
||||
uint64_t PHYS_TO_DMAP(uint64_t pa) { return args.dmap_base + pa; }
|
||||
|
||||
void memcpy(void *dest, void *src, uint64_t len) {
|
||||
uint8_t *d = (uint8_t *)dest;
|
||||
const uint8_t *s = (const uint8_t *)src;
|
||||
for (uint64_t i = 0; i < len; i++) {
|
||||
d[i] = s[i];
|
||||
}
|
||||
}
|
||||
|
||||
uint64_t read_cr3(void) {
|
||||
uint64_t cr3;
|
||||
__asm__ volatile("mov %%cr3, %0"
|
||||
: "=r"(cr3) // Output: move CR3 into the variable 'cr3'
|
||||
: // No inputs
|
||||
: // No clobbered registers
|
||||
);
|
||||
return cr3;
|
||||
}
|
||||
|
||||
// for ring0
|
||||
uint64_t va_to_pa_kernel(uint64_t va) {
|
||||
uint64_t cr3 = read_cr3();
|
||||
return va_to_pa_custom(va, cr3);
|
||||
}
|
||||
|
||||
// Source: PS5_kldload
|
||||
uint64_t va_to_pa_custom(uint64_t va, uint64_t cr3_custom) {
|
||||
|
||||
uint64_t table_phys = cr3_custom & 0xFFFFFFFF;
|
||||
|
||||
for (int level = 0; level < 4; level++) {
|
||||
int shift = 39 - (level * 9);
|
||||
uint64_t idx = (va >> shift) & 0x1FF;
|
||||
uint64_t entry;
|
||||
uint64_t entry_va = PHYS_TO_DMAP(PAGE_PA(table_phys) + idx * 8);
|
||||
|
||||
entry = *(uint64_t *)entry_va;
|
||||
|
||||
if (!PAGE_P(entry))
|
||||
return 0;
|
||||
|
||||
if ((level == 1 || level == 2) && PAGE_PS(entry)) {
|
||||
uint64_t page_size = P_SIZE(level);
|
||||
return PAGE_PA(entry) | (va & (page_size - 1));
|
||||
}
|
||||
|
||||
if (level == 3)
|
||||
return PAGE_PA(entry) | (va & 0xFFF);
|
||||
|
||||
table_phys = PAGE_PA(entry);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
__attribute__((noinline, optimize("O0"))) uint32_t putc_uart(uint64_t dmap,
|
||||
uint8_t tx_byte) {
|
||||
volatile uint32_t *uart_tx = (uint32_t *) (dmap + 0xc1010104ULL);
|
||||
volatile uint32_t *uart_busy = (uint32_t *) (dmap + 0xc101010cULL);
|
||||
uint64_t timeout = 0xFFFFFFFF;
|
||||
do {
|
||||
timeout--;
|
||||
if (timeout == 0)
|
||||
break;
|
||||
} while (((*uart_busy) & 0x20) == 0);
|
||||
|
||||
if (timeout == 0)
|
||||
return -1;
|
||||
|
||||
*uart_tx = (uint32_t)tx_byte & 0xFF;
|
||||
return 0;
|
||||
}
|
||||
#include "utils.h"
|
||||
#include "shellcode_kernel_args.h"
|
||||
|
||||
extern shellcode_kernel_args args;
|
||||
|
||||
uint64_t PHYS_TO_DMAP(uint64_t pa) { return args.dmap_base + pa; }
|
||||
|
||||
void memcpy(void *dest, void *src, uint64_t len) {
|
||||
uint8_t *d = (uint8_t *)dest;
|
||||
const uint8_t *s = (const uint8_t *)src;
|
||||
for (uint64_t i = 0; i < len; i++) {
|
||||
d[i] = s[i];
|
||||
}
|
||||
}
|
||||
|
||||
uint64_t read_cr3(void) {
|
||||
uint64_t cr3;
|
||||
__asm__ volatile("mov %%cr3, %0"
|
||||
: "=r"(cr3) // Output: move CR3 into the variable 'cr3'
|
||||
: // No inputs
|
||||
: // No clobbered registers
|
||||
);
|
||||
return cr3;
|
||||
}
|
||||
|
||||
// for ring0
|
||||
uint64_t va_to_pa_kernel(uint64_t va) {
|
||||
uint64_t cr3 = read_cr3();
|
||||
return va_to_pa_custom(va, cr3);
|
||||
}
|
||||
|
||||
// Source: PS5_kldload
|
||||
uint64_t va_to_pa_custom(uint64_t va, uint64_t cr3_custom) {
|
||||
|
||||
uint64_t table_phys = cr3_custom & 0xFFFFFFFF;
|
||||
|
||||
for (int level = 0; level < 4; level++) {
|
||||
int shift = 39 - (level * 9);
|
||||
uint64_t idx = (va >> shift) & 0x1FF;
|
||||
uint64_t entry;
|
||||
uint64_t entry_va = PHYS_TO_DMAP(PAGE_PA(table_phys) + idx * 8);
|
||||
|
||||
entry = *(uint64_t *)entry_va;
|
||||
|
||||
if (!PAGE_P(entry))
|
||||
return 0;
|
||||
|
||||
if ((level == 1 || level == 2) && PAGE_PS(entry)) {
|
||||
uint64_t page_size = P_SIZE(level);
|
||||
return PAGE_PA(entry) | (va & (page_size - 1));
|
||||
}
|
||||
|
||||
if (level == 3)
|
||||
return PAGE_PA(entry) | (va & 0xFFF);
|
||||
|
||||
table_phys = PAGE_PA(entry);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
__attribute__((noinline, optimize("O0"))) uint32_t putc_uart(uint64_t dmap,
|
||||
uint8_t tx_byte) {
|
||||
volatile uint32_t *uart_tx = (uint32_t *)(dmap + 0xc1010104ULL);
|
||||
volatile uint32_t *uart_busy = (uint32_t *)(dmap + 0xc101010cULL);
|
||||
uint64_t timeout = 0xFFFFFFFF;
|
||||
do {
|
||||
timeout--;
|
||||
if (timeout == 0)
|
||||
break;
|
||||
} while (((*uart_busy) & 0x20) == 0);
|
||||
|
||||
if (timeout == 0)
|
||||
return -1;
|
||||
|
||||
*uart_tx = (uint32_t)tx_byte & 0xFF;
|
||||
return 0;
|
||||
}
|
||||
|
||||
+43
-43
@@ -1,43 +1,43 @@
|
||||
#ifndef UTILS_H
|
||||
#define UTILS_H
|
||||
#include "shellcode_kernel_args.h"
|
||||
#include <stdint.h>
|
||||
|
||||
extern void (*printf)(const char *format, ...);
|
||||
uint64_t PHYS_TO_DMAP(uint64_t pa);
|
||||
void memcpy(void *dest, void *src, uint64_t len);
|
||||
|
||||
// Defines for Page management
|
||||
enum page_bits {
|
||||
P = 0,
|
||||
RW,
|
||||
US,
|
||||
PWT,
|
||||
PCD,
|
||||
A,
|
||||
D,
|
||||
PS,
|
||||
G,
|
||||
XO = 58,
|
||||
PK = 59,
|
||||
NX = 63
|
||||
};
|
||||
|
||||
#define PG_B_P (1ULL << P)
|
||||
#define PG_B_RW (1ULL << RW)
|
||||
#define PAGE_P(x) (x & (1ULL << P))
|
||||
#define PAGE_RW(x) (x & (1ULL << RW))
|
||||
#define PAGE_PS(x) (x & (1ULL << PS))
|
||||
#define PAGE_XO(x) (x & (1ULL << XO))
|
||||
#define PAGE_CLEAR_XO(x) (x &= ~(1ULL << XO))
|
||||
#define PAGE_CLEAR_G(x) (x &= ~(1ULL << G))
|
||||
#define PAGE_SET_RW(x) (x |= (1ULL << RW))
|
||||
#define PAGE_PA(x) (x & 0x000FFFFFFFFFF000ULL)
|
||||
#define P_SIZE(l) ((l == 1) ? (1ULL << 30) : (1ULL << 21))
|
||||
|
||||
uint64_t read_cr3(void);
|
||||
uint64_t va_to_pa_kernel(uint64_t va);
|
||||
uint64_t va_to_pa_custom(uint64_t va, uint64_t cr3_custom);
|
||||
uint32_t putc_uart(uint64_t dmap, uint8_t tx_byte);
|
||||
|
||||
#endif
|
||||
#ifndef UTILS_H
|
||||
#define UTILS_H
|
||||
#include "shellcode_kernel_args.h"
|
||||
#include <stdint.h>
|
||||
|
||||
extern void (*printf)(const char *format, ...);
|
||||
uint64_t PHYS_TO_DMAP(uint64_t pa);
|
||||
void memcpy(void *dest, void *src, uint64_t len);
|
||||
|
||||
// Defines for Page management
|
||||
enum page_bits {
|
||||
P = 0,
|
||||
RW,
|
||||
US,
|
||||
PWT,
|
||||
PCD,
|
||||
A,
|
||||
D,
|
||||
PS,
|
||||
G,
|
||||
XO = 58,
|
||||
PK = 59,
|
||||
NX = 63
|
||||
};
|
||||
|
||||
#define PG_B_P (1ULL << P)
|
||||
#define PG_B_RW (1ULL << RW)
|
||||
#define PAGE_P(x) (x & (1ULL << P))
|
||||
#define PAGE_RW(x) (x & (1ULL << RW))
|
||||
#define PAGE_PS(x) (x & (1ULL << PS))
|
||||
#define PAGE_XO(x) (x & (1ULL << XO))
|
||||
#define PAGE_CLEAR_XO(x) (x &= ~(1ULL << XO))
|
||||
#define PAGE_CLEAR_G(x) (x &= ~(1ULL << G))
|
||||
#define PAGE_SET_RW(x) (x |= (1ULL << RW))
|
||||
#define PAGE_PA(x) (x & 0x000FFFFFFFFFF000ULL)
|
||||
#define P_SIZE(l) ((l == 1) ? (1ULL << 30) : (1ULL << 21))
|
||||
|
||||
uint64_t read_cr3(void);
|
||||
uint64_t va_to_pa_kernel(uint64_t va);
|
||||
uint64_t va_to_pa_custom(uint64_t va, uint64_t cr3_custom);
|
||||
uint32_t putc_uart(uint64_t dmap, uint8_t tx_byte);
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user