Integrate clang-format

This commit is contained in:
Furkan Mudanyali 2023-12-27 00:02:34 +03:00
parent 1bff9d0f7c
commit 722d59a3e0
48 changed files with 718 additions and 694 deletions

63
.clang-format Normal file
View File

@ -0,0 +1,63 @@
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: AcrossComments
AlignConsecutiveDeclarations: AcrossComments
AlignConsecutiveBitFields: AcrossComments
AlignConsecutiveMacros: AcrossComments
AlignArrayOfStructures: Right
AlignEscapedNewlines: Left
AlignOperands: AlignAfterOperator
AlignTrailingComments:
Kind: Always
OverEmptyLines: 2
AllowAllParametersOfDeclarationOnNextLine: false
AllowAllArgumentsOnNextLine: false
AllowShortBlocksOnASingleLine: Never
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: false
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
BinPackArguments: false
BinPackParameters: false
BreakBeforeBraces: Attach
BreakBeforeBinaryOperators: NonAssignment
BreakBeforeTernaryOperators: false
BreakAfterJavaFieldAnnotations: true
BreakStringLiterals: false
ColumnLimit: 120
ContinuationIndentWidth: 4
IncludeBlocks: Regroup
IndentCaseLabels: false
IndentGotoLabels: false
IndentPPDirectives: None
IndentWidth: 4
IndentWrappedFunctionNames: false
KeepEmptyLinesAtTheStartOfBlocks: false
MaxEmptyLinesToKeep: 1
PointerAlignment: Right
ReflowComments: true
SortIncludes: true
SpaceAfterCStyleCast: false
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: ControlStatementsExceptForEachMacros
SpaceBeforeRangeBasedForLoopColon: true
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 4
SpacesInContainerLiterals: false
SpacesInSquareBrackets: false
TabWidth: 4
UseTab: Never

View File

@ -6,7 +6,7 @@
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
@ -22,22 +22,22 @@ uint32_t MMIO_BASE = 0;
uint32_t GPIO_BASE = 0;
uint32_t UART_BASE = 0;
uint32_t GPPUD = 0;
uint32_t GPPUD = 0;
uint32_t GPPUDCLK0 = 0;
uint32_t UART_DR = 0;
uint32_t UART_FR = 0;
uint32_t UART_DR = 0;
uint32_t UART_FR = 0;
uint32_t UART_IBRD = 0;
uint32_t UART_FBRD = 0;
uint32_t UART_LCRH = 0;
uint32_t UART_CR = 0;
uint32_t UART_CR = 0;
uint32_t UART_IMSC = 0;
uint32_t UART_ICR = 0;
uint32_t UART_ICR = 0;
uint32_t MBOX_BASE = 0;
uint32_t MBOX_READ = 0;
uint32_t MBOX_POLL = 0;
uint32_t MBOX_BASE = 0;
uint32_t MBOX_READ = 0;
uint32_t MBOX_POLL = 0;
uint32_t MBOX_SENDER = 0;
uint32_t MBOX_STATUS = 0;
uint32_t MBOX_CONFIG = 0;
uint32_t MBOX_WRITE = 0;
uint32_t MBOX_WRITE = 0;

View File

@ -6,7 +6,7 @@
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
@ -18,66 +18,73 @@
#include <bits/alltypes.h>
#include <device.h>
#include <fb.h>
#include <fdt.h>
#include <hal.h>
#include <stdio.h>
#include <string.h>
#include <fb.h>
/* The buffer must be 16-byte aligned as only the upper 28 bits of the address can be passed via the mailbox */
/* The buffer must be 16-byte aligned as only the upper 28 bits of the address
* can be passed via the mailbox */
volatile unsigned int __attribute__((aligned(16))) mbox[36];
unsigned int width, height, pitch, bpp, rgb;
unsigned int width, height, pitch, bpp, rgb;
unsigned char *fb;
void pixel_draw(int x, int y, int r, int g, int b) {
fb[(pitch * y) + (x*bpp) + 0] = r;
fb[(pitch * y) + (x*bpp) + 1] = g;
fb[(pitch * y) + (x*bpp) + 2] = b;
fb[(pitch * y) + (x * bpp) + 0] = r;
fb[(pitch * y) + (x * bpp) + 1] = g;
fb[(pitch * y) + (x * bpp) + 2] = b;
}
unsigned int mbox_call(unsigned char ch) {
unsigned int m = ((unsigned int)((long) &mbox) &~ 0xF) | (ch & 0xF); /* 28-bit address (MSB), 4-bit value (LSB) */
unsigned int m = ((unsigned int)((long)&mbox) & ~0xF) | (ch & 0xF); /* 28-bit address (MSB), 4-bit value (LSB) */
while (1)
if (!(mmio_read(MBOX_STATUS) & MBOX_FULL))
break;
while (1) if(!(mmio_read(MBOX_STATUS) & MBOX_FULL)) break;
mmio_write(MBOX_WRITE, m);
while (1) {
while (1) if(!(mmio_read(MBOX_STATUS) & MBOX_EMPTY)) break; /* Wait for a reply */
while (1)
if (!(mmio_read(MBOX_STATUS) & MBOX_EMPTY))
break; /* Wait for a reply */
if (m == mmio_read(MBOX_READ)) { /* Look for a message on MBOX_READ */
if(mbox[1]==MBOX_RESPONSE) return 1; /* Success! */
else return 0;
}
if (mbox[1] == MBOX_RESPONSE)
return 1; /* Success! */
else
return 0;
}
}
return 0;
}
void fb_init() {
fdt_prop mailbox_path = fdt_get_prop(fdt_get_addr("/__symbols__/mailbox"));
fdt_prop mailbox_reg = fdt_get_prop(fdt_get_addr(strcat((char*)mailbox_path.data, "/reg")));
MBOX_BASE = SWAP_UINT32(*mailbox_reg.data) - MMIO_BASE;
MBOX_READ = (MBOX_BASE + 0x0);
MBOX_POLL = (MBOX_BASE + 0x10);
fdt_prop mailbox_path = fdt_get_prop(fdt_get_addr("/__symbols__/mailbox"));
fdt_prop mailbox_reg = fdt_get_prop(fdt_get_addr(strcat((char *)mailbox_path.data, "/reg")));
MBOX_BASE = SWAP_UINT32(*mailbox_reg.data) - MMIO_BASE;
MBOX_READ = (MBOX_BASE + 0x0);
MBOX_POLL = (MBOX_BASE + 0x10);
MBOX_SENDER = (MBOX_BASE + 0x14);
MBOX_STATUS = (MBOX_BASE + 0x18);
MBOX_CONFIG = (MBOX_BASE + 0x1C);
MBOX_WRITE = (MBOX_BASE + 0x20);
MBOX_WRITE = (MBOX_BASE + 0x20);
mbox[0] = 35 * 4; /* Length of message in bytes */
mbox[1] = MBOX_REQUEST;
mbox[2] = MBOX_TAG_SETPHYWH;
mbox[3] = 8; /* Value size in bytes */
mbox[3] = 8; /* Value size in bytes */
mbox[4] = 0;
mbox[5] = 640; /* Width, hardcoded for now */
mbox[6] = 480; /* Height, hardcoded for now */
mbox[5] = 640; /* Width, hardcoded for now */
mbox[6] = 480; /* Height, hardcoded for now */
mbox[7] = MBOX_TAG_SETVIRTWH;
mbox[8] = 8;
mbox[9] = 8;
mbox[7] = MBOX_TAG_SETVIRTWH;
mbox[8] = 8;
mbox[9] = 8;
mbox[10] = 640;
mbox[11] = 480;
@ -109,16 +116,17 @@ void fb_init() {
mbox[33] = 0; /* Bytes per line */
mbox[34] = MBOX_TAG_LAST;
/* Check if mailbox says we're good and that the depth is what we asked for and that it gave us a pointer */
/* Check if mailbox says we're good and that the depth is what we asked for
* and that it gave us a pointer */
if (mbox_call(MBOX_CH_PROP) && mbox[20] == 32 && mbox[28] != 0) {
mbox[28] &= 0x3FFFFFFF; /* Convert GPU address to ARM address */
width = mbox[10]; /* Actual physical width */
width = mbox[10]; /* Actual physical width */
height = mbox[11]; /* Actual physical height */
pitch = mbox[33]; /* Number of bytes per line */
rgb = mbox[24]; /* Pixel order */
fb = (unsigned char *)((long)mbox[28]);
bpp = pitch / width;
pitch = mbox[33]; /* Number of bytes per line */
rgb = mbox[24]; /* Pixel order */
fb = (unsigned char *)((long)mbox[28]);
bpp = pitch / width;
printf("Initialized framebuffer.\n");
}
memset(fb, 0x00, pitch * height);

View File

@ -6,7 +6,7 @@
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
@ -17,83 +17,79 @@
*/
#include <fdt.h>
#include <stdlib.h>
#include <string.h>
#include <uart.h>
#include <stdlib.h>
fdt_header* fdt;
fdt_header *fdt;
void fdt_init() {
fdt = *(fdt_header**)0x1337;
fdt = *(fdt_header **)0x1337;
}
fdt_prop fdt_get_prop(uint32_t* addr) {
return (fdt_prop) {
SWAP_UINT32(*addr),
SWAP_UINT32(*(addr+1)),
addr + 2
};
fdt_prop fdt_get_prop(uint32_t *addr) {
return (fdt_prop){SWAP_UINT32(*addr), SWAP_UINT32(*(addr + 1)), addr + 2};
}
uint32_t* fdt_get_addr(const char* path) {
const char* fdt_strings = (char*)fdt + SWAP_UINT32(fdt->off_dt_strings);
uint32_t* fdt_struct = (uint32_t*)fdt + SWAP_UINT32(fdt->off_dt_struct);
uint32_t *fdt_get_addr(const char *path) {
const char *fdt_strings = (char *)fdt + SWAP_UINT32(fdt->off_dt_strings);
uint32_t *fdt_struct = (uint32_t *)fdt + SWAP_UINT32(fdt->off_dt_struct);
const uint32_t struct_size = SWAP_UINT32(fdt->size_dt_struct);
uint32_t* struct_ptr = fdt_struct;
uint32_t name_length = 0;
uint32_t prop_length = 0;
uint32_t prop_name_off = 0;
uint32_t* lastaddr = 0;
uint32_t *struct_ptr = fdt_struct;
uint32_t name_length = 0;
uint32_t prop_length = 0;
uint32_t prop_name_off = 0;
uint32_t *lastaddr = 0;
char path_copy[strlen(path)+1];
char path_copy[strlen(path) + 1];
strcpy(path_copy, path);
char* name = strtok(path_copy, "/");
char *name = strtok(path_copy, "/");
while(name && struct_ptr < fdt_struct + struct_size) {
while (name && struct_ptr < fdt_struct + struct_size) {
/* Read token */
const uint32_t token = SWAP_UINT32(*struct_ptr);
switch(token) {
/* The beginning node is followed immediately by its name */
case FDT_BEGIN_NODE:
/* Advance pointer */
struct_ptr++;
switch (token) {
/* The beginning node is followed immediately by its name */
case FDT_BEGIN_NODE:
/* Advance pointer */
struct_ptr++;
/* check if we are in the token we want */
if(!strcmp((char*)struct_ptr, name)) {
name = strtok(0, "/");
lastaddr = struct_ptr;
}
/* check if we are in the token we want */
if (!strcmp((char *)struct_ptr, name)) {
name = strtok(0, "/");
lastaddr = struct_ptr;
}
/* Calculate the size of the node name */
name_length = strlen((char*)struct_ptr);
/* Advance pointer by size of node name aligned to 4 bytes */
struct_ptr += name_length / 4;
break;
/* Property */
case FDT_PROP:
/* Advance pointer */
prop_length = *++struct_ptr;
prop_name_off = *++struct_ptr;
/* Calculate the size of the node name */
name_length = strlen((char *)struct_ptr);
/* Advance pointer by size of node name aligned to 4 bytes */
struct_ptr += name_length / 4;
break;
/* check if we are in the token we want */
if(!strcmp(fdt_strings + SWAP_UINT32(prop_name_off), name)) {
return struct_ptr-1;
}
/* Advance pointer */
struct_ptr += SWAP_UINT32(prop_length) / 4;
break;
/* Property */
case FDT_PROP:
/* Advance pointer */
prop_length = *++struct_ptr;
prop_name_off = *++struct_ptr;
/* End is followed immediately by next token */
case FDT_END_NODE:
case FDT_NOP:
break;
/* check if we are in the token we want */
if (!strcmp(fdt_strings + SWAP_UINT32(prop_name_off), name)) {
return struct_ptr - 1;
}
/* Advance pointer */
struct_ptr += SWAP_UINT32(prop_length) / 4;
break;
/* There can be only one FDT_END which marks the
end of FDT */
case FDT_END:
return 0;
/* End is followed immediately by next token */
case FDT_END_NODE:
case FDT_NOP:
break;
/* There can be only one FDT_END which marks the
* end of FDT */
case FDT_END:
return 0;
}
/* Advance pointer */
struct_ptr++;
@ -102,96 +98,101 @@ uint32_t* fdt_get_addr(const char* path) {
}
void print_fdt() {
const char* fdt_strings = (char*)fdt + SWAP_UINT32(fdt->off_dt_strings);
uint32_t* fdt_struct = (uint32_t*)fdt + SWAP_UINT32(fdt->off_dt_struct);
const char *fdt_strings = (char *)fdt + SWAP_UINT32(fdt->off_dt_strings);
uint32_t *fdt_struct = (uint32_t *)fdt + SWAP_UINT32(fdt->off_dt_struct);
const uint32_t struct_size = SWAP_UINT32(fdt->size_dt_struct);
uint32_t* struct_ptr = fdt_struct;
uint32_t name_length = 0;
uint32_t prop_length = 0;
uint32_t prop_name_off = 0;
char intstr[10];
uint8_t tab_count = 0;
uint8_t first_elem = 0;
uint32_t *struct_ptr = fdt_struct;
uint32_t name_length = 0;
uint32_t prop_length = 0;
uint32_t prop_name_off = 0;
char intstr[10];
uint8_t tab_count = 0;
uint8_t first_elem = 0;
while(struct_ptr < fdt_struct + struct_size) {
while (struct_ptr < fdt_struct + struct_size) {
/* Read token */
const uint32_t token = SWAP_UINT32(*struct_ptr);
switch(token) {
/* The beginning node is followed immediately by its name */
case FDT_BEGIN_NODE:
/* Advance pointer */
struct_ptr++;
switch (token) {
/* The beginning node is followed immediately by its name */
case FDT_BEGIN_NODE:
/* Advance pointer */
struct_ptr++;
/* print tabs */
for(int i = 0; i < tab_count; ++i) uart_puts(" ");
/* increment tab count */
tab_count++;
/* print tabs */
for (int i = 0; i < tab_count; ++i) {
uart_puts(" ");
}
/* increment tab count */
tab_count++;
/* Calculate the size of the node name */
name_length = strlen((char*)struct_ptr);
/* Calculate the size of the node name */
name_length = strlen((char *)struct_ptr);
/* Print node name */
if(!name_length) {
uart_puts("/:");
} else {
uart_puts((char*)struct_ptr);
uart_puts(":");
}
uart_putc('\n');
/* Print node name */
if (!name_length) {
uart_puts("/:");
} else {
uart_puts((char *)struct_ptr);
uart_puts(":");
}
uart_putc('\n');
/* Advance pointer by size of node name aligned to 4 bytes */
struct_ptr += name_length / 4;
break;
/* Property */
case FDT_PROP:
/* Advance pointer */
prop_length = *++struct_ptr;
prop_name_off = *++struct_ptr;
/* Advance pointer by size of node name aligned to 4 bytes */
struct_ptr += name_length / 4;
break;
/* print tabs */
for(int i = 0; i < tab_count+1; ++i) uart_puts(" ");
/* Property */
case FDT_PROP:
/* Advance pointer */
prop_length = *++struct_ptr;
prop_name_off = *++struct_ptr;
/* Print prop name and value */
uart_puts(fdt_strings + SWAP_UINT32(prop_name_off));
/* Value can be empty */
if(prop_length) {
uart_puts(": ");
/* Dirty hack to see if the value is a string or a number(s)
If the first byte of the 4 byte chunk does not fall
under acceptable ASCII character values, print its
digits one by one, else just call puts on it since
it is ***probably*** a string. */
first_elem = (uint8_t)(SWAP_UINT32(*(struct_ptr+1)));
/* It is probably a number or an array of numbers
Divide length of the property by 4 to get
the amount of numbers and iterate over them */
if(first_elem < 0x20 || first_elem > 0x7A || !strcmp("reg", fdt_strings + SWAP_UINT32(prop_name_off))) {
for(uint32_t i = 0; i < SWAP_UINT32(prop_length) >> 2; ++i) {
uint32_t hex = SWAP_UINT32(*(struct_ptr + 1 + i));
uart_puts("0x");
uart_puts(uitoa(hex, intstr, 16));
uart_putc(' ');
}
} else {
/* Its a good old string */
uart_puts((char*)(struct_ptr + 1));
/* print tabs */
for (int i = 0; i < tab_count + 1; ++i) {
uart_puts(" ");
}
/* Print prop name and value */
uart_puts(fdt_strings + SWAP_UINT32(prop_name_off));
/* Value can be empty */
if (prop_length) {
uart_puts(": ");
/* Dirty hack to see if the value is a string or a number(s)
* If the first byte of the 4 byte chunk does not fall
* under acceptable ASCII character values, print its
* digits one by one, else just call puts on it since
* it is ***probably*** a string. */
first_elem = (uint8_t)(SWAP_UINT32(*(struct_ptr + 1)));
/* It is probably a number or an array of numbers
* Divide length of the property by 4 to get
* the amount of numbers and iterate over them */
if (first_elem < 0x20 || first_elem > 0x7A
|| !strcmp("reg", fdt_strings + SWAP_UINT32(prop_name_off))) {
for (uint32_t i = 0; i < SWAP_UINT32(prop_length) >> 2; ++i) {
uint32_t hex = SWAP_UINT32(*(struct_ptr + 1 + i));
uart_puts("0x");
uart_puts(uitoa(hex, intstr, 16));
uart_putc(' ');
}
} else {
/* Its a good old string */
uart_puts((char *)(struct_ptr + 1));
}
uart_putc('\n');
struct_ptr += SWAP_UINT32(prop_length) >> 2;
break;
}
uart_putc('\n');
struct_ptr += SWAP_UINT32(prop_length) >> 2;
break;
/* End is followed immediately by next token */
case FDT_END_NODE:
--tab_count;
case FDT_NOP:
break;
/* End is followed immediately by next token */
case FDT_END_NODE:
--tab_count;
case FDT_NOP:
break;
/* There can be only one FDT_END which marks the end of FDT */
case FDT_END:
return;
/* There can be only one FDT_END which marks the end of FDT */
case FDT_END:
return;
}
/* Advance pointer */
struct_ptr++;

View File

@ -6,7 +6,7 @@
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
@ -23,18 +23,15 @@ void hcf() {
}
void delay(int32_t count) {
asm("__delay_%=: subs %[count], %[count], #1; bne __delay_%=\n"
: "=r"(count): [count]"0"(count) : "cc");
asm("__delay_%=: subs %[count], %[count], #1; bne __delay_%=\n" : "=r"(count) : [count] "0"(count) : "cc");
}
uint32_t mmio_read(uint64_t reg) {
uint64_t *p_reg = (uint64_t*)reg;
uint64_t *p_reg = (uint64_t *)reg;
return *p_reg;
}
void mmio_write(uint64_t reg, uint32_t data) {
uint32_t *p_reg = (uint32_t*)reg;
*p_reg = data;
uint32_t *p_reg = (uint32_t *)reg;
*p_reg = data;
}

View File

@ -6,7 +6,7 @@
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
@ -19,37 +19,37 @@
#ifndef _ASAGIRI_ALLTYPES_H
#define _ASAGIRI_ALLTYPES_H
#define _Addr long
#define _Addr long
#define _Int64 long
#define _Reg long
#define _Reg long
#define __CHAR_MAX 0x7f
#define __UCHAR_MAX 0xff
#define __SHRT_MAX 0x7fff
#define __USHRT_MAX 0xffff
#define __INT_MAX 0x7fffffff
#define __UINT_MAX 0xffffffff
#define __LONG_MAX 0x7fffffffffffffffL
#define __ULONG_MAX 0xffffffffffffffffUL
#define __LLONG_MAX 0x7fffffffffffffffLL
#define __CHAR_MAX 0x7f
#define __UCHAR_MAX 0xff
#define __SHRT_MAX 0x7fff
#define __USHRT_MAX 0xffff
#define __INT_MAX 0x7fffffff
#define __UINT_MAX 0xffffffff
#define __LONG_MAX 0x7fffffffffffffffL
#define __ULONG_MAX 0xffffffffffffffffUL
#define __LLONG_MAX 0x7fffffffffffffffLL
#define __ULLONG_MAX 0xffffffffffffffffULL
typedef unsigned wint_t;
typedef int blksize_t;
typedef unsigned wint_t;
typedef int blksize_t;
typedef unsigned int nlink_t;
typedef float float_t;
typedef double double_t;
typedef int size_t;
typedef float float_t;
typedef double double_t;
typedef int size_t;
typedef char int8_t;
typedef unsigned char uint8_t;
typedef short int16_t;
typedef char int8_t;
typedef unsigned char uint8_t;
typedef short int16_t;
typedef unsigned short uint16_t;
typedef int int32_t;
typedef unsigned int uint32_t;
typedef long int64_t;
typedef unsigned long uint64_t;
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef int int32_t;
typedef unsigned int uint32_t;
typedef long int64_t;
typedef unsigned long uint64_t;
typedef long intptr_t;
typedef unsigned long uintptr_t;
#endif

View File

@ -6,7 +6,7 @@
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
@ -16,10 +16,10 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#define va_start(v,l) __builtin_va_start(v,l)
#define va_end(v) __builtin_va_end(v)
#define va_arg(v,l) __builtin_va_arg(v,l)
#define va_copy(d,s) __builtin_va_copy(d,s)
#define va_start(v, l) __builtin_va_start(v, l)
#define va_end(v) __builtin_va_end(v)
#define va_arg(v, l) __builtin_va_arg(v, l)
#define va_copy(d, s) __builtin_va_copy(d, s)
typedef __builtin_va_list va_list;
typedef __builtin_va_list __isoc_va_list;

View File

@ -6,7 +6,7 @@
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

View File

@ -6,7 +6,7 @@
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
@ -20,20 +20,20 @@
#define _ASAGIRI_FB_H
#define MBOX_RESPONSE 0x80000000
#define MBOX_FULL 0x80000000
#define MBOX_EMPTY 0x40000000
#define MBOX_REQUEST 0x0
#define MBOX_FULL 0x80000000
#define MBOX_EMPTY 0x40000000
#define MBOX_REQUEST 0x0
#define MBOX_TAG_SETPOWER 0x28001
#define MBOX_TAG_SETPOWER 0x28001
#define MBOX_TAG_SETCLKRATE 0x38002
#define MBOX_TAG_SETPHYWH 0x48003
#define MBOX_TAG_SETVIRTWH 0x48004
#define MBOX_TAG_SETPHYWH 0x48003
#define MBOX_TAG_SETVIRTWH 0x48004
#define MBOX_TAG_SETVIRTOFF 0x48009
#define MBOX_TAG_SETDEPTH 0x48005
#define MBOX_TAG_SETDEPTH 0x48005
#define MBOX_TAG_SETPXLORDR 0x48006
#define MBOX_TAG_GETFB 0x40001
#define MBOX_TAG_GETPITCH 0x40008
#define MBOX_TAG_LAST 0x0
#define MBOX_TAG_GETFB 0x40001
#define MBOX_TAG_GETPITCH 0x40008
#define MBOX_TAG_LAST 0x0
#define MBOX_CH_PROP 0x8

View File

@ -6,7 +6,7 @@
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
@ -19,15 +19,12 @@
#ifndef _ASAGIRI_FDT_H
#define _ASAGIRI_FDT_H
#define SWAP_UINT32(x) (((x) >> 24) | \
(((x) & 0x00FF0000) >> 8) | \
(((x) & 0x0000FF00) << 8) | \
((x) << 24))
#define SWAP_UINT32(x) (((x) >> 24) | (((x)&0x00FF0000) >> 8) | (((x)&0x0000FF00) << 8) | ((x) << 24))
#include <bits/alltypes.h>
typedef struct fdt_header {
uint32_t magic; /* Magic value identifying FDT */
uint32_t magic; /* Magic value identifying FDT */
uint32_t totalsize; /* Total size of FDT including the header */
uint32_t off_dt_struct; /* Offset from beginning of the FDT to the structure block */
uint32_t off_dt_strings; /* Offset from beginning of the FDT to the strings block */
@ -40,22 +37,16 @@ typedef struct fdt_header {
} fdt_header;
typedef struct fdt_prop {
uint32_t nameoff;
uint32_t len;
uint32_t* data;
uint32_t nameoff;
uint32_t len;
uint32_t *data;
} fdt_prop;
enum {
FDT_BEGIN_NODE = 0x1,
FDT_END_NODE = 0x2,
FDT_PROP = 0x3,
FDT_NOP = 0x4,
FDT_END = 0x9
};
enum { FDT_BEGIN_NODE = 0x1, FDT_END_NODE = 0x2, FDT_PROP = 0x3, FDT_NOP = 0x4, FDT_END = 0x9 };
fdt_prop fdt_get_prop(uint32_t*);
uint32_t* fdt_get_addr(const char*);
void fdt_init();
void print_fdt();
fdt_prop fdt_get_prop(uint32_t *);
uint32_t *fdt_get_addr(const char *);
void fdt_init();
void print_fdt();
#endif

View File

@ -6,7 +6,7 @@
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
@ -23,9 +23,9 @@
#define asm __asm__ __volatile__
void hcf();
void delay(int32_t);
void hcf();
void delay(int32_t);
uint32_t mmio_read(uint64_t);
void mmio_write(uint64_t, uint32_t);
void mmio_write(uint64_t, uint32_t);
#endif

View File

@ -6,7 +6,7 @@
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
@ -19,9 +19,9 @@
#ifndef _ASAGIRI_UART_H
#define _ASAGIRI_UART_H
void uart_init();
void uart_putc(unsigned char);
void uart_init();
void uart_putc(unsigned char);
unsigned char uart_getc();
void uart_puts(const char*);
void uart_puts(const char *);
#endif

View File

@ -6,7 +6,7 @@
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
@ -17,78 +17,79 @@
*/
#include <bits/alltypes.h>
#include <device.h>
#include <fdt.h>
#include <hal.h>
#include <string.h>
#include <device.h>
#include <uart.h>
void uart_init() {
/* Get MMIO, UART and MBOX addresses from FDT */
fdt_prop ranges = fdt_get_prop(fdt_get_addr("/soc/ranges"));
MMIO_BASE = SWAP_UINT32(*ranges.data) - SWAP_UINT32(*(ranges.data+1));
/* Get MMIO, UART and MBOX addresses from FDT */
fdt_prop ranges = fdt_get_prop(fdt_get_addr("/soc/ranges"));
MMIO_BASE = SWAP_UINT32(*ranges.data) - SWAP_UINT32(*(ranges.data + 1));
fdt_prop gpio_path = fdt_get_prop(fdt_get_addr("/__symbols__/gpio"));
fdt_prop gpio_reg = fdt_get_prop(fdt_get_addr(strcat((char*)gpio_path.data, "/reg")));
GPIO_BASE = SWAP_UINT32(*gpio_reg.data) - MMIO_BASE;
fdt_prop gpio_path = fdt_get_prop(fdt_get_addr("/__symbols__/gpio"));
fdt_prop gpio_reg = fdt_get_prop(fdt_get_addr(strcat((char *)gpio_path.data, "/reg")));
GPIO_BASE = SWAP_UINT32(*gpio_reg.data) - MMIO_BASE;
fdt_prop uart_path = fdt_get_prop(fdt_get_addr("/__symbols__/uart1"));
fdt_prop uart_reg = fdt_get_prop(fdt_get_addr(strcat((char*)uart_path.data, "/reg")));
UART_BASE = SWAP_UINT32(*uart_reg.data) - MMIO_BASE;
fdt_prop uart_path = fdt_get_prop(fdt_get_addr("/__symbols__/uart1"));
fdt_prop uart_reg = fdt_get_prop(fdt_get_addr(strcat((char *)uart_path.data, "/reg")));
UART_BASE = SWAP_UINT32(*uart_reg.data) - MMIO_BASE;
GPPUD = GPIO_BASE + 0x94;
GPPUDCLK0 = GPIO_BASE + 0x98;
GPPUD = GPIO_BASE + 0x94;
GPPUDCLK0 = GPIO_BASE + 0x98;
UART_DR = UART_BASE + 0x00;
UART_FR = UART_BASE + 0x18;
UART_IBRD = UART_BASE + 0x24;
UART_FBRD = UART_BASE + 0x28;
UART_LCRH = UART_BASE + 0x2C;
UART_CR = UART_BASE + 0x30;
UART_IMSC = UART_BASE + 0x38;
UART_ICR = UART_BASE + 0x44;
UART_DR = UART_BASE + 0x00;
UART_FR = UART_BASE + 0x18;
UART_IBRD = UART_BASE + 0x24;
UART_FBRD = UART_BASE + 0x28;
UART_LCRH = UART_BASE + 0x2C;
UART_CR = UART_BASE + 0x30;
UART_IMSC = UART_BASE + 0x38;
UART_ICR = UART_BASE + 0x44;
/* Disable UART0. */
mmio_write(UART_CR, 0);
/* Setup the GPIO pin 14 && 15.
Disable pull up/down for all GPIO pins & delay for 150 cycles. */
mmio_write(GPPUD, 0);
delay(150);
/* Disable pull up/down for pin 14,15 & delay for 150 cycles. */
mmio_write(GPPUDCLK0, (1<<14)|(1<<15));
delay(150);
/* Write 0 to GPPUDCLK0 to make it take effect. */
mmio_write(GPPUDCLK0, 0);
/* Clear pending interrupts. */
mmio_write(UART_ICR, 0x7FF);
/* Set integer & fractional part of baud rate.
Divider = UART_CLOCK/(16 * Baud) */
mmio_write(UART_IBRD, 1);
/* Fractional part register = (.627 * 64) + 0.5 = 40.6 = ~40. */
mmio_write(UART_FBRD, 40);
/* Enable FIFO & 8 bit data transmission (1 stop bit, no parity). */
mmio_write(UART_LCRH, (1<<4)|(1<<5)|(1<<6));
/* Mask all interrupts. */
mmio_write(UART_IMSC, (1<<1)|(1<<4)|(1<<5)|(1<<6)
|(1<<7)|(1<<8)|(1<<9)|(1<<10));
/* Enable UART0, receive & transfer part of UART. */
mmio_write(UART_CR, 1|(1<<8)|(1<<9));
/* Disable UART0. */
mmio_write(UART_CR, 0);
/* Setup the GPIO pin 14 && 15.
* Disable pull up/down for all GPIO pins & delay for 150 cycles. */
mmio_write(GPPUD, 0);
delay(150);
/* Disable pull up/down for pin 14,15 & delay for 150 cycles. */
mmio_write(GPPUDCLK0, (1 << 14) | (1 << 15));
delay(150);
/* Write 0 to GPPUDCLK0 to make it take effect. */
mmio_write(GPPUDCLK0, 0);
/* Clear pending interrupts. */
mmio_write(UART_ICR, 0x7FF);
/* Set integer & fractional part of baud rate.
* Divider = UART_CLOCK/(16 * Baud) */
mmio_write(UART_IBRD, 1);
/* Fractional part register = (.627 * 64) + 0.5 = 40.6 = ~40. */
mmio_write(UART_FBRD, 40);
/* Enable FIFO & 8 bit data transmission (1 stop bit, no parity). */
mmio_write(UART_LCRH, (1 << 4) | (1 << 5) | (1 << 6));
/* Mask all interrupts. */
mmio_write(UART_IMSC, (1 << 1) | (1 << 4) | (1 << 5) | (1 << 6) | (1 << 7) | (1 << 8) | (1 << 9) | (1 << 10));
/* Enable UART0, receive & transfer part of UART. */
mmio_write(UART_CR, 1 | (1 << 8) | (1 << 9));
}
void uart_putc(unsigned char c) {
/* Wait for UART to become ready to transmit. */
while(mmio_read(UART_FR) & (1<<5));
mmio_write(UART_DR, c);
/* Wait for UART to become ready to transmit. */
while (mmio_read(UART_FR) & (1 << 5))
;
mmio_write(UART_DR, c);
}
unsigned char uart_getc() {
/* Wait for UART to have received something. */
while (mmio_read(UART_FR) & (1<<4));
while (mmio_read(UART_FR) & (1 << 4))
;
return mmio_read(UART_DR);
}
void uart_puts(const char* str) {
for (size_t i = 0; str[i] != '\0'; i ++) {
uart_putc((unsigned char)str[i]);
}
void uart_puts(const char *str) {
for (size_t i = 0; str[i] != '\0'; i++) {
uart_putc((unsigned char)str[i]);
}
}

View File

@ -6,7 +6,7 @@
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

View File

@ -6,7 +6,7 @@
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
@ -23,8 +23,8 @@
#define asm __asm__ __volatile__
void hcf();
void outb(uint16_t, uint8_t);
void hcf();
void outb(uint16_t, uint8_t);
uint8_t inb(uint16_t);
#endif

View File

@ -6,7 +6,7 @@
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
@ -17,5 +17,5 @@
*/
int isdigit(int c) {
return (unsigned)c-'0' < 10;
return (unsigned)c - '0' < 10;
}

View File

@ -6,7 +6,7 @@
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
@ -21,6 +21,6 @@
#include <stdlib.h>
#define assert(val) ((val) ? (void)0: (printf("ASSERTION FAILED, " __FILE__ ":%d: " #val "\n", __LINE__), abort()))
#define assert(val) ((val) ? (void)0 : (printf("ASSERTION FAILED, " __FILE__ ":%d: " #val "\n", __LINE__), abort()))
#endif

View File

@ -6,7 +6,7 @@
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
@ -19,10 +19,10 @@
#ifndef _MATH_H
#define _MATH_H
#include <sys/cdefs.h>
#include <stddef.h>
#include <sys/cdefs.h>
double pow(double x, double y);
int abs(int x);
int abs(int x);
#endif

View File

@ -6,7 +6,7 @@
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
@ -19,10 +19,10 @@
#ifndef _STDDEF_H
#define _STDDEF_H
#define NULL ((void*)0)
#define NULL ((void *)0)
#include <bits/alltypes.h>
#define offsetof(type, member) ((size_t)( (char *)&(((type *)0)->member) - (char *)0 ))
#define offsetof(type, member) ((size_t)((char *)&(((type *)0)->member) - (char *)0))
#endif

View File

@ -6,7 +6,7 @@
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
@ -21,15 +21,15 @@
#include <bits/alltypes.h>
#define INT8_MIN (-1-0x7f)
#define INT16_MIN (-1-0x7fff)
#define INT32_MIN (-1-0x7fffffff)
#define INT64_MIN (-1-0x7fffffffffffffff)
#define INT8_MIN (-1 - 0x7f)
#define INT16_MIN (-1 - 0x7fff)
#define INT32_MIN (-1 - 0x7fffffff)
#define INT64_MIN (-1 - 0x7fffffffffffffff)
#define INT8_MAX (0x7f)
#define INT16_MAX (0x7fff)
#define INT32_MAX (0x7fffffff)
#define INT64_MAX (0x7fffffffffffffff)
#define INT8_MAX (0x7f)
#define INT16_MAX (0x7fff)
#define INT32_MAX (0x7fffffff)
#define INT64_MAX (0x7fffffffffffffff)
#define UINT8_MAX (0xff)
#define UINT16_MAX (0xffff)

View File

@ -6,7 +6,7 @@
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
@ -21,8 +21,8 @@
#define EOF -1
int printf(const char* restrict, ...);
int printf(const char *restrict, ...);
int putchar(int);
int puts(const char*);
int puts(const char *);
#endif

View File

@ -6,7 +6,7 @@
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
@ -19,11 +19,11 @@
#ifndef _STDLIB_H
#define _STDLIB_H
#include <sys/cdefs.h>
#include <stddef.h>
#include <sys/cdefs.h>
char* itoa(int value, char* buffer, int base);
char* uitoa(uint32_t value, char* buffer, int base);
void abort();
char *itoa(int value, char *buffer, int base);
char *uitoa(uint32_t value, char *buffer, int base);
void abort();
#endif

View File

@ -6,7 +6,7 @@
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
@ -19,20 +19,22 @@
#ifndef _STRING_H
#define _STRING_H
#include <sys/cdefs.h>
#include <stddef.h>
#include <sys/cdefs.h>
char* stpcpy(char* restrict, const char*);
char* strcpy(char* restrict, const char*);
char* strcat(char* restrict, const char*);
char* strtok(char* restrict, const char*);
int strcmp(const char* restrict, const char*);
char* strchr(const char* restrict, int c);
char *stpcpy(char *restrict, const char *);
char *strcpy(char *restrict, const char *);
char *strcat(char *restrict, const char *);
char *strtok(char *restrict, const char *);
char *strchr(const char *restrict, int c);
int memcmp(const void*, const void*, size_t);
void* memcpy(void* restrict, const void*, size_t);
void* memmove(void*, const void*, size_t);
void* memset(void*, int, size_t);
size_t strlen(const char*);
int memcmp(const void *, const void *, size_t);
int strcmp(const char *restrict, const char *);
void *memcpy(void *restrict, const void *, size_t);
void *memmove(void *, const void *, size_t);
void *memset(void *, int, size_t);
size_t strlen(const char *);
#endif

View File

@ -6,7 +6,7 @@
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

View File

@ -6,7 +6,7 @@
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
@ -15,7 +15,7 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
int abs(int x) {
return x >= 0 ? x : -x;
return x >= 0 ? x : -x;
}

View File

@ -6,7 +6,7 @@
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
@ -15,16 +15,18 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
double pow(double x, double y) {
if(y == 0) return 1;
int count = 0;
double out = 1;
while(count < y) {
out = out * x;
count++;
}
return out;
double pow(double x, double y) {
if (y == 0) {
return 1;
}
int count = 0;
double out = 1;
while (count < y) {
out = out * x;
count++;
}
return out;
}

View File

@ -6,7 +6,7 @@
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
@ -16,23 +16,23 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <bits/stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <bits/stdarg.h>
#include <string.h>
#include <stdlib.h>
#include <string.h>
static uint8_t print(const char* data, size_t len) {
const unsigned char* bytes = (const unsigned char*)data;
for(size_t i = 0; i < len; ++i) {
if(putchar(bytes[i]) == EOF) {
static uint8_t print(const char *data, size_t len) {
const unsigned char *bytes = (const unsigned char *)data;
for (size_t i = 0; i < len; ++i) {
if (putchar(bytes[i]) == EOF) {
return 0;
}
}
return 1;
}
int printf(const char* restrict format, ...) {
int printf(const char *restrict format, ...) {
va_list params;
va_start(params, format);
@ -61,81 +61,81 @@ int printf(const char* restrict format, ...) {
continue;
}
const char* format_begun_at = format++;
const char *format_begun_at = format++;
size_t len = 0;
char buf[11];
char buf[11];
switch(*format) {
case 'c':
format++;
char c = (char)va_arg(params, int);
if (!maxrem) {
/* EOVERFLOW */
return -1;
}
if (!print(&c, sizeof(c))) {
return -1;
}
written++;
break;
switch (*format) {
case 'c':
format++;
char c = (char)va_arg(params, int);
if (!maxrem) {
/* EOVERFLOW */
return -1;
}
if (!print(&c, sizeof(c))) {
return -1;
}
written++;
break;
case 's':
format++;
const char* str = va_arg(params, const char*);
len = strlen(str);
if (maxrem < len) {
/* TODO: EOVERFLOW */
return -1;
}
if (!print(str, len)) {
return -1;
}
written += len;
break;
case 's':
format++;
const char *str = va_arg(params, const char *);
len = strlen(str);
if (maxrem < len) {
/* TODO: EOVERFLOW */
return -1;
}
if (!print(str, len)) {
return -1;
}
written += len;
break;
case 'd':
format++;
int num = va_arg(params, int);
itoa(num, buf, 10);
len = strlen(buf);
if(maxrem < len) {
/* EOVERFLOW */
return -1;
}
if(!print(buf, len)) {
return -1;
}
written += len;
break;
case 'd':
format++;
int num = va_arg(params, int);
itoa(num, buf, 10);
len = strlen(buf);
if (maxrem < len) {
/* EOVERFLOW */
return -1;
}
if (!print(buf, len)) {
return -1;
}
written += len;
break;
case 'x':
format++;
uint32_t unum = va_arg(params, uint32_t);
uitoa(unum, buf, 16);
len = strlen(buf);
if(maxrem < len) {
/* EOVERFLOW */
return -1;
}
if(!print(buf, len)) {
return -1;
}
written += len;
break;
case 'x':
format++;
uint32_t unum = va_arg(params, uint32_t);
uitoa(unum, buf, 16);
len = strlen(buf);
if (maxrem < len) {
/* EOVERFLOW */
return -1;
}
if (!print(buf, len)) {
return -1;
}
written += len;
break;
default:
format = format_begun_at;
len = strlen(format);
if (maxrem < len) {
/* EOVERFLOW */
return -1;
}
if (!print(format, len)) {
return -1;
}
written += len;
format += len;
default:
format = format_begun_at;
len = strlen(format);
if (maxrem < len) {
/* EOVERFLOW */
return -1;
}
if (!print(format, len)) {
return -1;
}
written += len;
format += len;
}
}

View File

@ -6,7 +6,7 @@
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

View File

@ -6,7 +6,7 @@
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
@ -18,6 +18,6 @@
#include <stdio.h>
int puts(const char* str){
int puts(const char *str) {
return printf("%s", str);
}

View File

@ -6,7 +6,7 @@
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
@ -25,12 +25,14 @@
#include <stdio.h>
void abort() {
#ifdef _IS_YUKARI
#ifdef _IS_YUKARI
printf("kernel panic!!\n");
hcf();
#else
#else
/* TODO: Abnormally terminate the process as if by SIGABRT. */
printf("abort!!\n");
#endif
for(;;);
#endif
for (;;) {
;
}
}

View File

@ -6,7 +6,7 @@
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
@ -15,51 +15,51 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <stdint.h>
#include <math.h>
#include <stdint.h>
static const char* digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
static const char *digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char* itoa(int value, char* buffer, int base) {
char *itoa(int value, char *buffer, int base) {
if (base < 2 || base > 36) {
return buffer;
}
uint8_t index = 0;
uint8_t index = 0;
uint8_t negative = 0;
uint8_t digit = 0;
uint8_t digit = 0;
/* Handle negative numbers */
/* Handle negative numbers */
if (value < 0) {
negative = 1;
value = abs(value);
value = abs(value);
}
/* Put base remainder of value into the buffer
while dividing value by base */
do {
digit = value % base;
buffer[index] = digits[digit];
++index;
value /= base;
} while (value);
/* Put base remainder of value into the buffer
* while dividing value by base */
do {
digit = value % base;
buffer[index] = digits[digit];
++index;
value /= base;
} while (value);
/* Put negative character in the end */
if(negative) {
buffer[index++] = '-';
}
/* Null terminate */
buffer[index] = 0;
/* Put negative character in the end */
if (negative) {
buffer[index++] = '-';
}
/* Null terminate */
buffer[index] = 0;
/* Reverse the buffer */
uint8_t len = index;
char temp = 0;
for(uint8_t i = 0; i < len >> 1; ++i) {
temp = buffer[i];
buffer[i] = buffer[len-i-1];
buffer[len-i-1] = temp;
}
/* Reverse the buffer */
uint8_t len = index;
char temp = 0;
for (uint8_t i = 0; i < len >> 1; ++i) {
temp = buffer[i];
buffer[i] = buffer[len - i - 1];
buffer[len - i - 1] = temp;
}
return buffer;
}

View File

@ -6,7 +6,7 @@
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
@ -15,39 +15,39 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <stdint.h>
static const char* digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
static const char *digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char* uitoa(uint32_t value, char* buffer, int base) {
char *uitoa(uint32_t value, char *buffer, int base) {
if (base < 2 || base > 36) {
return buffer;
}
uint8_t index = 0;
uint8_t digit = 0;
uint8_t digit = 0;
/* Put base remainder of value into the buffer
while dividing value by base */
do {
digit = value % base;
buffer[index] = digits[digit];
++index;
value /= base;
} while (value);
/* Put base remainder of value into the buffer
* while dividing value by base */
do {
digit = value % base;
buffer[index] = digits[digit];
++index;
value /= base;
} while (value);
/* Null terminate */
buffer[index] = 0;
/* Null terminate */
buffer[index] = 0;
/* Reverse the buffer */
uint8_t len = index;
char temp = 0;
for(uint8_t i = 0; i < len >> 1; ++i) {
temp = buffer[i];
buffer[i] = buffer[len-i-1];
buffer[len-i-1] = temp;
}
/* Reverse the buffer */
uint8_t len = index;
char temp = 0;
for (uint8_t i = 0; i < len >> 1; ++i) {
temp = buffer[i];
buffer[i] = buffer[len - i - 1];
buffer[len - i - 1] = temp;
}
return buffer;
}

View File

@ -6,7 +6,7 @@
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
@ -18,9 +18,9 @@
#include <string.h>
int memcmp(const void* aptr, const void* bptr, size_t size) {
const unsigned char* a = (const unsigned char*) aptr;
const unsigned char* b = (const unsigned char*) bptr;
int memcmp(const void *aptr, const void *bptr, size_t size) {
const unsigned char *a = (const unsigned char *)aptr;
const unsigned char *b = (const unsigned char *)bptr;
for (size_t i = 0; i < size; i++) {
if (a[i] < b[i]) {
return -1;

View File

@ -6,7 +6,7 @@
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
@ -18,9 +18,9 @@
#include <string.h>
void* memcpy(void* restrict dstptr, const void* srcptr, size_t size) {
unsigned char* dst = (unsigned char*) dstptr;
const unsigned char* src = (const unsigned char*) srcptr;
void *memcpy(void *restrict dstptr, const void *srcptr, size_t size) {
unsigned char *dst = (unsigned char *)dstptr;
const unsigned char *src = (const unsigned char *)srcptr;
for (size_t i = 0; i < size; i++) {
dst[i] = src[i];
}

View File

@ -6,7 +6,7 @@
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
@ -18,9 +18,9 @@
#include <string.h>
void* memmove(void* dstptr, const void* srcptr, size_t size) {
unsigned char* dst = (unsigned char*) dstptr;
const unsigned char* src = (const unsigned char*) srcptr;
void *memmove(void *dstptr, const void *srcptr, size_t size) {
unsigned char *dst = (unsigned char *)dstptr;
const unsigned char *src = (const unsigned char *)srcptr;
if (dst < src) {
for (size_t i = 0; i < size; i++) {
@ -28,7 +28,7 @@ void* memmove(void* dstptr, const void* srcptr, size_t size) {
}
} else {
for (size_t i = size; i != 0; i--) {
dst[i-1] = src[i-1];
dst[i - 1] = src[i - 1];
}
}
return dstptr;

View File

@ -6,7 +6,7 @@
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
@ -18,10 +18,10 @@
#include <string.h>
void* memset(void* bufptr, int value, size_t size) {
unsigned char* buf = (unsigned char*) bufptr;
void *memset(void *bufptr, int value, size_t size) {
unsigned char *buf = (unsigned char *)bufptr;
for (size_t i = 0; i < size; i++) {
buf[i] = (unsigned char) value;
buf[i] = (unsigned char)value;
}
return bufptr;
}

View File

@ -6,7 +6,7 @@
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
@ -16,7 +16,8 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
char* stpcpy(char* restrict d, const char* s){
for(;(*d=*s); s++, d++);
char *stpcpy(char *restrict d, const char *s) {
for (; (*d = *s); s++, d++)
;
return d;
}

View File

@ -6,7 +6,7 @@
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
@ -18,7 +18,7 @@
#include <string.h>
char* strcat(char* restrict dest, const char* src) {
char *strcat(char *restrict dest, const char *src) {
strcpy(dest + strlen(dest), src);
return dest;
}

View File

@ -6,7 +6,7 @@
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
@ -16,10 +16,10 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
char* strchr(const char* restrict str, int c) {
char *strchr(const char *restrict str, int c) {
while (*str) {
if (*str == c) {
return (char*)str;
return (char *)str;
}
str++;
}

View File

@ -6,7 +6,7 @@
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
@ -16,9 +16,10 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
int strcmp(const char* restrict str1, const char* str2) {
while(*str1 || *str2) {
if(*str1 != *str2) return *str1 - *str2;
int strcmp(const char *restrict str1, const char *str2) {
while (*str1 || *str2) {
if (*str1 != *str2)
return *str1 - *str2;
str1++;
str2++;
}

View File

@ -6,7 +6,7 @@
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
@ -16,7 +16,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
char* strcpy(char* restrict dest, const char* src) {
char *strcpy(char *restrict dest, const char *src) {
stpcpy(dest, src);
return dest;
}

View File

@ -6,7 +6,7 @@
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
@ -18,7 +18,7 @@
#include <string.h>
size_t strlen(const char* str) {
size_t strlen(const char *str) {
size_t len = 0;
while (str[len]) {
len++;

View File

@ -6,7 +6,7 @@
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
@ -18,33 +18,33 @@
#include <string.h>
char* strtok(char* restrict str, const char* delim) {
static char* lastToken = 0;
if(str) {
char *strtok(char *restrict str, const char *delim) {
static char *lastToken = 0;
if (str) {
lastToken = str;
} else if (!lastToken) {
return 0;
}
while (*lastToken && strchr(delim, *lastToken)) {
lastToken++;
}
if (!*lastToken) {
lastToken = 0;
return 0;
}
char* tokenStart = lastToken;
char *tokenStart = lastToken;
while (*lastToken && !strchr(delim, *lastToken)) {
lastToken++;
}
if (*lastToken) {
*lastToken = 0;
lastToken++;
}
return tokenStart;
}

View File

@ -6,7 +6,7 @@
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
@ -18,67 +18,44 @@
#include <shinobu.h>
EFI_FILE_PROTOCOL* EFIAPI
OpenFile(CHAR16 *path) {
EFI_FILE_PROTOCOL *EFIAPI OpenFile(CHAR16 *path) {
EFI_STATUS status;
/* Locate handle supporting SFSP */
EFI_HANDLE *handles;
UINTN handleCnt;
EFI_GUID sfspGuid = EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID;
UINTN handleCnt;
EFI_GUID sfspGuid = EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID;
status = uefi_call_wrapper(*(void**)&BS->LocateHandleBuffer, 5,
ByProtocol,
&sfspGuid,
NULL,
&handleCnt,
&handles
);
status = uefi_call_wrapper(*(void **)&BS->LocateHandleBuffer, 5, ByProtocol, &sfspGuid, NULL, &handleCnt, &handles);
PRINT_ERR(status);
/* TODO: Might need to iterate on all handles if
multiple FAT32 images are connected
Open Filesystem */
* multiple FAT32 images are connected
* Open Filesystem */
EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *fs = NULL;
status = uefi_call_wrapper(*(void**)&BS->HandleProtocol, 3,
handles[0],
&sfspGuid,
(VOID**)&fs
);
status = uefi_call_wrapper(*(void **)&BS->HandleProtocol, 3, handles[0], &sfspGuid, (VOID **)&fs);
PRINT_ERR(status);
/* Open ROOTDIR */
EFI_FILE_PROTOCOL *root = NULL;
status = uefi_call_wrapper(*(void**)&fs->OpenVolume, 2,
fs,
&root
);
status = uefi_call_wrapper(*(void **)&fs->OpenVolume, 2, fs, &root);
PRINT_ERR(status);
/* Open file */
status = uefi_call_wrapper(*(void**)&root->Open, 5,
root,
&root,
path,
EFI_FILE_MODE_READ,
EFI_FILE_READ_ONLY
);
status = uefi_call_wrapper(*(void **)&root->Open, 5, root, &root, path, EFI_FILE_MODE_READ, EFI_FILE_READ_ONLY);
PRINT_ERR(status);
return root;
}
EFI_STATUS EFIAPI
SetPos(EFI_FILE_PROTOCOL *file, UINTN pos) {
return uefi_call_wrapper(*(void**)&file->SetPosition, 2, file, pos);
EFI_STATUS EFIAPI SetPos(EFI_FILE_PROTOCOL *file, UINTN pos) {
return uefi_call_wrapper(*(void **)&file->SetPosition, 2, file, pos);
}
EFI_STATUS EFIAPI
Read(EFI_FILE_PROTOCOL *file, UINTN *size, VOID *dest) {
return uefi_call_wrapper(*(void**)&file->Read, 3, file, size, dest);
EFI_STATUS EFIAPI Read(EFI_FILE_PROTOCOL *file, UINTN *size, VOID *dest) {
return uefi_call_wrapper(*(void **)&file->Read, 3, file, size, dest);
}
EFI_STATUS EFIAPI
Close(EFI_FILE_PROTOCOL *file) {
return uefi_call_wrapper(*(void**)&file->Close, 1, file);
EFI_STATUS EFIAPI Close(EFI_FILE_PROTOCOL *file) {
return uefi_call_wrapper(*(void **)&file->Close, 1, file);
}

View File

@ -6,7 +6,7 @@
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
@ -22,28 +22,23 @@
#include <efi/efi.h>
#include <efi/efilib.h>
#define PRINT_ERR(status) \
if(EFI_ERROR(status)) \
Print( \
L"%a:%d error: %r\n", \
__FILE__, \
__LINE__, \
status \
);
#define PRINT_ERR(status) \
if (EFI_ERROR(status)) \
Print(L"%a:%d error: %r\n", __FILE__, __LINE__, status);
typedef struct {
UINTN memoryMapSize;
EFI_MEMORY_DESCRIPTOR* memoryMap;
UINTN mapKey;
UINTN descriptorSize;
UINT32 descriptorVersion;
UINTN memoryMapSize;
EFI_MEMORY_DESCRIPTOR *memoryMap;
UINTN mapKey;
UINTN descriptorSize;
UINT32 descriptorVersion;
} UEFIMemoryMap;
EFI_STATUS EFIAPI SetMemoryMap(UEFIMemoryMap*);
EFI_FILE_PROTOCOL* EFIAPI OpenFile(CHAR16 *path);
EFI_STATUS EFIAPI SetPos(EFI_FILE_PROTOCOL*, UINTN);
EFI_STATUS EFIAPI Read(EFI_FILE_PROTOCOL*, UINTN*, VOID*);
EFI_STATUS EFIAPI Close(EFI_FILE_PROTOCOL*);
EFI_STATUS EFIAPI SetMemoryMap(UEFIMemoryMap *);
EFI_FILE_PROTOCOL *EFIAPI OpenFile(CHAR16 *path);
EFI_STATUS EFIAPI SetPos(EFI_FILE_PROTOCOL *, UINTN);
EFI_STATUS EFIAPI Read(EFI_FILE_PROTOCOL *, UINTN *, VOID *);
EFI_STATUS EFIAPI Close(EFI_FILE_PROTOCOL *);
/* elf.h */
#define EI_NIDENT (16)
@ -54,31 +49,31 @@ typedef uint64_t Elf64_Off;
typedef uint64_t Elf64_Xword;
typedef struct {
unsigned char e_ident[EI_NIDENT]; /* Magic number and other info */
Elf64_Half e_type; /* Object file type */
Elf64_Half e_machine; /* Architecture */
Elf64_Word e_version; /* Object file version */
Elf64_Addr e_entry; /* Entry point virtual address */
Elf64_Off e_phoff; /* Program header table file offset */
Elf64_Off e_shoff; /* Section header table file offset */
Elf64_Word e_flags; /* Processor-specific flags */
Elf64_Half e_ehsize; /* ELF header size in bytes */
Elf64_Half e_phentsize; /* Program header table entry size */
Elf64_Half e_phnum; /* Program header table entry count */
Elf64_Half e_shentsize; /* Section header table entry size */
Elf64_Half e_shnum; /* Section header table entry count */
Elf64_Half e_shstrndx; /* Section header string table index */
unsigned char e_ident[EI_NIDENT]; /* Magic number and other info */
Elf64_Half e_type; /* Object file type */
Elf64_Half e_machine; /* Architecture */
Elf64_Word e_version; /* Object file version */
Elf64_Addr e_entry; /* Entry point virtual address */
Elf64_Off e_phoff; /* Program header table file offset */
Elf64_Off e_shoff; /* Section header table file offset */
Elf64_Word e_flags; /* Processor-specific flags */
Elf64_Half e_ehsize; /* ELF header size in bytes */
Elf64_Half e_phentsize; /* Program header table entry size */
Elf64_Half e_phnum; /* Program header table entry count */
Elf64_Half e_shentsize; /* Section header table entry size */
Elf64_Half e_shnum; /* Section header table entry count */
Elf64_Half e_shstrndx; /* Section header string table index */
} Elf64_Ehdr;
typedef struct {
Elf64_Word p_type; /* Segment type */
Elf64_Word p_flags; /* Segment flags */
Elf64_Off p_offset; /* Segment file offset */
Elf64_Addr p_vaddr; /* Segment virtual address */
Elf64_Addr p_paddr; /* Segment physical address */
Elf64_Xword p_filesz; /* Segment size in file */
Elf64_Xword p_memsz; /* Segment size in memory */
Elf64_Xword p_align; /* Segment alignment */
Elf64_Word p_type; /* Segment type */
Elf64_Word p_flags; /* Segment flags */
Elf64_Off p_offset; /* Segment file offset */
Elf64_Addr p_vaddr; /* Segment virtual address */
Elf64_Addr p_paddr; /* Segment physical address */
Elf64_Xword p_filesz; /* Segment size in file */
Elf64_Xword p_memsz; /* Segment size in memory */
Elf64_Xword p_align; /* Segment alignment */
} Elf64_Phdr;
#endif

View File

@ -6,7 +6,7 @@
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
@ -18,27 +18,26 @@
#include <shinobu.h>
EFI_STATUS EFIAPI
SetMemoryMap(UEFIMemoryMap *map) {
EFI_STATUS EFIAPI SetMemoryMap(UEFIMemoryMap *map) {
/* Set a size for memory map */
EFI_STATUS status;
map->memoryMapSize = sizeof(EFI_MEMORY_DESCRIPTOR) * 64;
/* Loop until we correctly get a memory map or come across
an error */
for(;;) {
* an error */
for (;;) {
/* Try allocating and getting memory map */
map->memoryMap = AllocatePool(map->memoryMapSize);
status = uefi_call_wrapper(*(void**)&BS->GetMemoryMap, 5,
&map->memoryMapSize,
map->memoryMap,
&map->mapKey,
&map->descriptorSize,
&map->descriptorVersion
);
status = uefi_call_wrapper(*(void **)&BS->GetMemoryMap,
5,
&map->memoryMapSize,
map->memoryMap,
&map->mapKey,
&map->descriptorSize,
&map->descriptorVersion);
/* If allocated buffer is too small, free buffer and
increase its size */
* increase its size */
if (status == EFI_BUFFER_TOO_SMALL) {
FreePool(map->memoryMap);
map->memoryMapSize += sizeof(EFI_MEMORY_DESCRIPTOR) * 16;

View File

@ -6,7 +6,7 @@
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
@ -18,10 +18,9 @@
#include <shinobu.h>
EFI_STATUS EFIAPI
efi_main(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable) {
EFI_STATUS EFIAPI efi_main(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable) {
EFI_STATUS status;
/* Initialize UEFI library */
InitializeLib(ImageHandle, SystemTable);
@ -30,14 +29,14 @@ efi_main(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable) {
BS = ST->BootServices;
/* Clear Screen */
uefi_call_wrapper(*(void**)&ST->ConOut->ClearScreen, 1, ST->ConOut);
uefi_call_wrapper(*(void **)&ST->ConOut->ClearScreen, 1, ST->ConOut);
Print(L"Shinobu Bootloader Alpha\n");
/* Open ELF64 Kernel */
EFI_FILE_PROTOCOL *kernelFile = OpenFile(L"yukari");
/* Read ELF Header */
UINTN EHeaderSize = sizeof(Elf64_Ehdr);
UINTN EHeaderSize = sizeof(Elf64_Ehdr);
Elf64_Ehdr *kernelElfHeader = AllocatePool(EHeaderSize);
SetPos(kernelFile, 0);
@ -46,8 +45,8 @@ efi_main(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable) {
Print(L"ELF Header Loc: 0x%x\n", kernelElfHeader);
/* Read ELF Prog Header */
UINTN PHeaderSize = kernelElfHeader->e_phnum * kernelElfHeader->e_phentsize;
Elf64_Phdr* kernelProgHeader = AllocatePool(PHeaderSize);
UINTN PHeaderSize = kernelElfHeader->e_phnum * kernelElfHeader->e_phentsize;
Elf64_Phdr *kernelProgHeader = AllocatePool(PHeaderSize);
SetPos(kernelFile, kernelElfHeader->e_phoff);
status = Read(kernelFile, &PHeaderSize, kernelProgHeader);
@ -55,24 +54,20 @@ efi_main(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable) {
Print(L"ELF Prog Header Loc: 0x%x\n", kernelProgHeader);
/* Load Program Segments */
for(UINT16 i = 0; i < kernelElfHeader->e_phnum; ++i) {
if(kernelProgHeader[i].p_type == 1) {
for (UINT16 i = 0; i < kernelElfHeader->e_phnum; ++i) {
if (kernelProgHeader[i].p_type == 1) {
/* Get count of pages needed */
int pages = (kernelProgHeader[i].p_memsz + EFI_PAGE_SIZE - 1) / EFI_PAGE_SIZE;
int pages = (kernelProgHeader[i].p_memsz + EFI_PAGE_SIZE - 1) / EFI_PAGE_SIZE;
/* Get segment address */
Elf64_Addr segment = kernelProgHeader[i].p_paddr;
/* Allocate pages to the segment address */
status = uefi_call_wrapper(*(void**)&BS->AllocatePages, 4,
AllocateAddress,
EfiLoaderData,
pages,
&segment
);
status =
uefi_call_wrapper(*(void **)&BS->AllocatePages, 4, AllocateAddress, EfiLoaderData, pages, &segment);
PRINT_ERR(status);
/* Write program segment to the allocated address */
UINTN fileSize = kernelProgHeader[i].p_filesz;
SetPos(kernelFile, kernelProgHeader[i].p_offset);
status = Read(kernelFile, &fileSize, (VOID*)segment);
status = Read(kernelFile, &fileSize, (VOID *)segment);
PRINT_ERR(status);
}
}
@ -84,13 +79,10 @@ efi_main(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable) {
/* Set Memory Map and exit boot services */
UEFIMemoryMap map;
status = SetMemoryMap(&map);
uefi_call_wrapper(*(void**)&BS->ExitBootServices, 2,
ImageHandle,
map.mapKey
);
uefi_call_wrapper(*(void **)&BS->ExitBootServices, 2, ImageHandle, map.mapKey);
/* Jump to kernel */
VOID (*_start)(VOID) = (VOID (*)(VOID))kernelElfHeader->e_entry;
VOID (*_start)(VOID) = (VOID(*)(VOID))kernelElfHeader->e_entry;
_start();
/* Should be unreachable */

View File

@ -6,7 +6,7 @@
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
@ -18,44 +18,36 @@
#if _ARCH_AARCH64
#include <fb.h>
#include <fdt.h>
#include <uart.h>
#include <fb.h>
#else
#endif
#include <hal.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <hal.h>
#include <stdio.h>
#include <stdlib.h>
__attribute__((section (".text.boot")))
void _start() {
__attribute__((section(".text.boot"))) void _start() {
#if _ARCH_AARCH64
fdt_init();
uart_init();
fdt_init();
uart_init();
#endif
printf(
"Selamunaleykum.\n"
"Asagiri version pre-alpha\n"
"UART initialized.\n"
);
printf("Selamunaleykum.\n"
"Asagiri version pre-alpha\n"
"UART initialized.\n");
#if _ARCH_AARCH64
print_fdt();
fb_init();
print_fdt();
fb_init();
#else
uint64_t val = 0xDEADBEEF;
asm(
"mov %0, %%rax"
:
: "r"(val)
: "%rax"
);
asm("1: hlt\njmp 1b\n");
uint64_t val = 0xDEADBEEF;
asm("mov %0, %%rax" : : "r"(val) : "%rax");
asm("1: hlt\njmp 1b\n");
#endif
hcf();
hcf();
}