128 lines
2.3 KiB
C
128 lines
2.3 KiB
C
#include <stdint.h>
|
|
#include <stddef.h>
|
|
|
|
// minimal set of string routines, copied from KarlOS
|
|
|
|
void *memset(void *ptr, int value, size_t num)
|
|
{
|
|
__asm__ ("rep stosb"
|
|
: "+D"(ptr), "+c"(num)
|
|
: "a"(value)
|
|
: "memory");
|
|
return ptr;
|
|
}
|
|
|
|
void *memcpy(void *restrict dest, const void *restrict src, size_t num)
|
|
{
|
|
__asm__ ("rep movsb"
|
|
: "+D"(dest), "+S"(src), "+c"(num)
|
|
:
|
|
: "memory");
|
|
return dest;
|
|
}
|
|
|
|
int memcmp(const void *ptr, const void *ptr2, size_t num)
|
|
{
|
|
unsigned char *d = (unsigned char *)ptr;
|
|
unsigned char *b = (unsigned char *)ptr2;
|
|
for (size_t i = 0; i < num; i++) {
|
|
if (d[i] != b[i]) {
|
|
return (d[i] - b[i]);
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
size_t strlen(const char *s)
|
|
{
|
|
size_t len = 0;
|
|
while (*s != '\0') {
|
|
s++;
|
|
len++;
|
|
}
|
|
return len;
|
|
}
|
|
|
|
// Helpers from bztsrc's bootboot codebase
|
|
|
|
/**
|
|
* convert ascii octal number to binary number
|
|
*/
|
|
int octbin(unsigned char *str,int size)
|
|
{
|
|
int s=0;
|
|
unsigned char *c=str;
|
|
while(size-->0){
|
|
s*=8;
|
|
s+=*c-'0';
|
|
c++;
|
|
}
|
|
return s;
|
|
}
|
|
|
|
/**
|
|
* convert ascii hex number to binary number
|
|
*/
|
|
int hexbin(unsigned char *str, int size)
|
|
{
|
|
int v=0;
|
|
while(size-->0){
|
|
v <<= 4;
|
|
if(*str>='0' && *str<='9')
|
|
v += (int)((unsigned char)(*str)-'0');
|
|
else if(*str >= 'A' && *str <= 'F')
|
|
v += (int)((unsigned char)(*str)-'A'+10);
|
|
str++;
|
|
}
|
|
return v;
|
|
}
|
|
|
|
typedef struct {
|
|
uint8_t *ptr;
|
|
uint64_t size;
|
|
} file_t;
|
|
|
|
#include "bootboot.h"
|
|
#include "fs.h"
|
|
|
|
// ELF64
|
|
|
|
#define EI_NIDENT 16
|
|
#define EI_MAG0 0x7F
|
|
#define EI_MAG1 'E'
|
|
#define EI_MAG2 'L'
|
|
#define EI_MAG3 'F'
|
|
|
|
typedef struct {
|
|
unsigned char e_ident[EI_NIDENT];
|
|
uint16_t e_type;
|
|
uint16_t e_machine;
|
|
uint32_t e_version;
|
|
uint64_t e_entry;
|
|
uint64_t e_phoff;
|
|
uint64_t e_shoff;
|
|
uint32_t e_flags;
|
|
uint16_t e_ehsize;
|
|
uint16_t e_phentsize;
|
|
uint16_t e_phnum;
|
|
uint16_t e_shentsize;
|
|
uint16_t e_shnum;
|
|
uint16_t e_shstrndx;
|
|
} Elf64_Ehdr;
|
|
|
|
typedef struct {
|
|
uint32_t p_type;
|
|
uint32_t p_flags;
|
|
uint64_t p_offset;
|
|
uint64_t p_vaddr;
|
|
uint64_t p_paddr;
|
|
uint64_t p_filesz;
|
|
uint64_t p_memsz;
|
|
uint64_t p_align;
|
|
} Elf64_Phdr;
|
|
|
|
void
|
|
loader_main(void)
|
|
{
|
|
for (;;) {}
|
|
}
|