Compare commits

..

4 Commits

6 changed files with 90 additions and 12 deletions

View File

@ -1,8 +1,58 @@
#include "arch/Timer.h"
#include "arch/Serial.h"
#include "boot/bootboot.h"
#include <Result.h>
static u64 timer_ticks = 0;
static u64 boot_timestamp;
static int isleap(int year)
{
return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
}
static int make_yday(int year, int month)
{
static const short int upto[12] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
int yd;
yd = upto[month - 1];
if (month > 2 && isleap(year)) yd++;
return yd;
}
// https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_16
static u64 broken_down_to_unix(u64 year, u64 yday, u64 hour, u64 min, u64 sec)
{
return sec + min * 60 + hour * 3600 + yday * 86400 + (year - 70) * 31536000 + ((year - 69) / 4) * 86400 -
((year - 1) / 100) * 86400 + ((year + 299) / 400) * 86400;
}
// The bootloader encodes the date and time in Binary-Coded Decimal (BCD), which represents decimal digits using
// hexadecimal digits. For example, BCD 0x22 is 22 in decimal.
// https://gitlab.com/bztsrc/bootboot/-/blob/master/bootboot_spec_1st_ed.pdf, page 15.
static int bcd_number_to_decimal(int num)
{
return ((num >> 4) * 10) + (num & 0xf);
}
static u64 bootloader_time_to_unix(u8 boottime[8])
{
int year = bcd_number_to_decimal(boottime[0]) * 100 + bcd_number_to_decimal(boottime[1]);
int month = bcd_number_to_decimal(boottime[2]);
int day = bcd_number_to_decimal(boottime[3]);
int hour = bcd_number_to_decimal(boottime[4]);
int minute = bcd_number_to_decimal(boottime[5]);
int second = bcd_number_to_decimal(boottime[6]);
// "The last byte can store 1/100th second precision, but in lack of support on most platforms, it is 0x00".
// Therefore, let's not rely on it.
Serial::printf("Current time: %.2d/%.2d/%d %.2d:%.2d:%.2d UTC\n", day, month, year, hour, minute, second)
.release_value();
return broken_down_to_unix(year - 1900, make_yday(year, month) + (day - 1), hour, minute, second);
}
extern BOOTBOOT bootboot;
namespace Timer
{
void tick()
@ -75,6 +125,7 @@ namespace Timer
void init()
{
boot_timestamp = bootloader_time_to_unix(bootboot.datetime);
arch_init();
}
}

View File

@ -2,19 +2,19 @@
extern "C"
{
void* memcpy(void* dest, const void* src, size_t n)
void* memcpy(void* dest, const void* src, usize n)
{
for (size_t i = 0; i < n; ++i) { *((u8*)dest + i) = *((const u8*)src + i); }
for (usize i = 0; i < n; ++i) { *((u8*)dest + i) = *((const u8*)src + i); }
return dest;
}
void* memset(void* buf, int c, size_t n)
void* memset(void* buf, int c, usize n)
{
for (size_t i = 0; i < n; ++i) { *((u8*)buf + i) = (u8)c; }
for (usize i = 0; i < n; ++i) { *((u8*)buf + i) = (u8)c; }
return buf;
}
int memcmp(const void* a, const void* b, size_t n)
int memcmp(const void* a, const void* b, usize n)
{
if (!n) return 0;
const u8* ap = (const u8*)a;
@ -27,7 +27,7 @@ extern "C"
return *ap - *bp;
}
void* memmove(void* dest, const void* src, size_t n)
void* memmove(void* dest, const void* src, usize n)
{
if (dest == src) return dest;
if (dest > src)
@ -37,7 +37,7 @@ extern "C"
return dest;
}
size_t strlen(const char* str)
usize strlen(const char* str)
{
const char* i = str;
for (; *i; ++i)

View File

@ -3,9 +3,9 @@
extern "C"
{
void* memcpy(void* dest, const void* src, size_t n);
void* memset(void* buf, int c, size_t n);
int memcmp(const void* a, const void* b, size_t n);
void* memmove(void* dest, const void* src, size_t n);
size_t strlen(const char* str);
void* memcpy(void* dest, const void* src, usize n);
void* memset(void* buf, int c, usize n);
int memcmp(const void* a, const void* b, usize n);
void* memmove(void* dest, const void* src, usize n);
usize strlen(const char* str);
}

View File

@ -5,6 +5,7 @@ source $(dirname $0)/env.sh
cd $LUNA_ROOT
SOURCES=($(find kernel/src -type f | grep -v "\.asm"))
SOURCES+=($(find luna -type f | grep -v "CMakeLists.txt"))
ALL_OK=1

25
tools/replace-stdint.sh Executable file
View File

@ -0,0 +1,25 @@
#!/usr/bin/env bash
set -e
source $(dirname $0)/env.sh
cd $LUNA_ROOT
SOURCES=($(find kernel/src -type f | grep -v "\.asm" | grep -v "bootboot.h"))
SOURCES+=($(find luna -type f | grep -v "CMakeLists.txt" | grep -v "Types.h" | grep -v "PlacementNew.h"))
for f in ${SOURCES[@]}
do
sed -i 's/uint8_t/u8/g' $f
sed -i 's/uint16_t/u16/g' $f
sed -i 's/uint32_t/u32/g' $f
sed -i 's/uint64_t/u64/g' $f
sed -i 's/int8_t/i8/g' $f
sed -i 's/int16_t/i16/g' $f
sed -i 's/int32_t/i32/g' $f
sed -i 's/int64_t/i64/g' $f
sed -i 's/size_t/usize/g' $f
sed -i 's/ssize_t/isize/g' $f
done

View File

@ -6,6 +6,7 @@ source $(dirname $0)/env.sh
cd $LUNA_ROOT
SOURCES=($(find kernel/src -type f | grep -v "\.asm"))
SOURCES+=($(find luna -type f | grep -v "CMakeLists.txt"))
for f in ${SOURCES[@]}
do