Skip to content

Commit

Permalink
Implemented a custom executable called, ForstWing Deployed Executable
Browse files Browse the repository at this point in the history
  • Loading branch information
pradosh-arduino committed Jan 12, 2024
1 parent e7a6a82 commit 30e6a78
Show file tree
Hide file tree
Showing 9 changed files with 148 additions and 38 deletions.
5 changes: 4 additions & 1 deletion source/desktop-manager/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,15 @@ override HEADER_DEPS := $(addprefix obj/,$(CFILES:.c=.c.d))
.PHONY: all

all:
@make clean desktop-manager.bin
@make clean desktop-manager.bin finalize

clean:
@rm -rf obj
@rm -rf desktop-manager.bin

finalize:
@python ./tools/signature.py

desktop-manager.bin: x86_64-dm-linker.ld $(OBJ)
@mkdir -p "$$(dirname $@)"
@ld $(OBJ) $(LDFLAGS) -o $@
Expand Down
5 changes: 3 additions & 2 deletions source/desktop-manager/dm_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,15 @@ Replace [SYSCALL ID] to appropriate syscall numbers.
void send_alive_msg(){
asm volatile("movq %0, %%rax" :: "r"((int64)0x3));
asm volatile("int $0x80");
asm volatile("movq %0, %%rax" :: "r"((int64)0x0)); // ! Always revert the RAX register after an syscall
}

/**
* @attention Don't rename this function, if you wanted to rename it, u must change the linker also.
*
*/
void dw_main(){
int dw_main(){
send_alive_msg();

return; // Safe to do.
return 0; // status code
}
53 changes: 53 additions & 0 deletions source/desktop-manager/tools/signature.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import struct
import os

def check_endian(file_path):
with open(file_path, 'rb') as file:
data = file.read(4)

if len(data) == 4:
little_endian_value = int.from_bytes(data, byteorder='little')
big_endian_value = int.from_bytes(data, byteorder='big')

if little_endian_value == big_endian_value:
print(f"The file '{file_path}' has an indeterminate endian.")
exit(1)
elif little_endian_value < big_endian_value:
print(f"The file '{file_path}' is little-endian.")
return 1
else:
print(f"The file '{file_path}' is big-endian.")
return 2
else:
print(f"The file '{file_path}' does not contain enough data to determine its endian.")
exit(1)

def append_header(file_path, architecture, file_size):
"""
Appends a custom header to a binary file.
Args:
file_path (str): Path to the binary file.
architecture (int): Architecture information (32 or 64 bits).
file_size (int): Size of the file in bytes.
"""
signature = b'FWDE' # stands for Frost Wing Deployed Executable
architecture_byte = struct.pack('B', architecture) # 'B' format code packs an unsigned char (1 byte)
size_byte = struct.pack('B', file_size) # 'B' format code packs an unsigned char (1 byte)

endian = check_endian(file_path) # 0 = error; 1 = little; 2 = big

endian_byte = struct.pack('B', endian) # 'B' format code packs an unsigned char (1 byte)

with open(file_path, 'rb') as file:
existing_content = file.read()

with open(file_path, 'wb') as file:
file.write(signature + architecture_byte + size_byte + endian_byte)
file.write(existing_content)

file_path = 'desktop-manager.bin'
architecture = 1 # 1 = 64 bits; 2 = 32 bits; 3 = 16 bits; 4 = 8 bits
file_size = os.path.getsize(file_path)

append_header(file_path, architecture, file_size)
13 changes: 0 additions & 13 deletions source/includes/executables/binary.h

This file was deleted.

21 changes: 21 additions & 0 deletions source/includes/executables/fwde.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* @file fwde.h
* @author Pradosh ([email protected])
* @brief The executor header for FrostWing Deployed Executable - 64 bits
* @version 0.1
* @date 2024-01-07
*
* @copyright Copyright (c) Pradosh 2024
*
*/
#include <basics.h>
#include <stdbool.h>

typedef struct {
char signature[4]; // FWDE
int8 architecture; // 1 = 64 bits; 2 = 32 bits; 3 = 16 bits; 4 = 8 bits
int8 raw_size; // size of just the executable part and not the header
int8 endian; // 0 = error; 1 = little; 2 = big
} fwde_header;

void execute_fwde(int64* addr);
2 changes: 1 addition & 1 deletion source/includes/kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
#include <linkedlist.h>
#include <ps2-mouse.h>
#include <image/targa.h>
#include <executables/binary.h>
#include <executables/fwde.h>

/**
* @brief The memory address pointer where the kernel ends.
Expand Down
20 changes: 0 additions & 20 deletions source/kernel/C/executables/binary.c

This file was deleted.

65 changes: 65 additions & 0 deletions source/kernel/C/executables/fwde.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* @file fwde.c
* @author Pradosh ([email protected])
* @brief The executor code for FrostWing Deployed Executable - 64 bits
* @version 0.1
* @date 2024-01-07
*
* @copyright Copyright (c) Pradosh 2024
*
*/

#include <executables/fwde.h>

bool verify_signature(char* signature){
if(signature[0] == 'F' &&
signature[1] == 'W' &&
signature[2] == 'D' &&
signature[3] == 'E'){
return true;
}
else
return false;
}

void execute_fwde(int64* addr){
info("Verifying the FrostWing deployed executable!", __FILE__);

int8* local = (int8*)addr;

fwde_header* header = (fwde_header*)local;

if(verify_signature(header->signature)){
info("Signature is correct!", __FILE__);
}else{
error("Wrong signature! Abort.", __FILE__);
return;
}

if(header->endian != 1 && header->endian != 2){
error("Undetermined endian executable! Abort.", __FILE__);
return;
}

if(header->architecture == 1){ // 64 bits
local += sizeof(fwde_header);
if(*(local + header->raw_size) == 0){
info("Valid executable! and ready for execution!", __FILE__);
}
info("Starting executing the FrostWing deployed executable...", __FILE__);
int (*execute_binary)() = (int (*)())local;
info("Function is ready!", __FILE__);
int status_code = execute_binary();
if(status_code != 0){
error("Executable returned an non zero status code! (something went wrong on the executable side)", __FILE__);
printf("Status code : 0x%x (%d)", status_code, status_code);
return;
}
}else{
error("Unsupported architecture!", __FILE__);
return;
}


done("Completed successfully!", __FILE__);
}
2 changes: 1 addition & 1 deletion source/kernel/C/kernel.c
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ void main(void) {

font_address = module_request.response->modules[1]->address;

execute_bin(module_request.response->modules[2]->address);
execute_fwde(module_request.response->modules[2]->address);

print("press F10 for (ACPI) Shutdown.\n");
print("press F9 for (ACPI/Hard) Reboot/Reset.\n");
Expand Down

0 comments on commit 30e6a78

Please sign in to comment.