Skip to content

Commit

Permalink
✨ 176 高速缓冲优化 - 内核校验和
Browse files Browse the repository at this point in the history
  • Loading branch information
StevenBaby committed Aug 16, 2023
1 parent dff0a3c commit 8d50c90
Showing 8 changed files with 106 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/boot/loader.asm
Original file line number Diff line number Diff line change
@@ -109,7 +109,7 @@ protect_mode:
sub esp, 4 * 3; 三个变量
mov dword [esp], 0; 读出的数量
mov dword [esp + 4], 10 ; ecx 初始扇区位置
mov dword [esp + 8], 0x10000; edi 目标内存位置
mov dword [esp + 8], 0x18000; edi 目标内存位置
BLOCK_SIZE equ 200 ; 一次读取的扇区数量

.read_block:
@@ -132,7 +132,7 @@ protect_mode:
mov eax, 0x20220205; 内核魔数
mov ebx, ards_count; ards 数量指针

jmp dword code_selector:0x10040
jmp dword code_selector:0x20000

ud2; 表示出错

2 changes: 2 additions & 0 deletions src/kernel/e1000.c
Original file line number Diff line number Diff line change
@@ -488,6 +488,7 @@ static void e1000_reset(e1000_t *e1000)

// 接收初始化
e1000->rx_desc = (rx_desc_t *)alloc_kpage(1); // TODO: free
memset(e1000->rx_desc, 0, PAGE_SIZE);
e1000->rx_cur = 0;

e1000->rx_pbuf = (pbuf_t **)&e1000->rx_desc[RX_DESC_NR];
@@ -519,6 +520,7 @@ static void e1000_reset(e1000_t *e1000)

// 传输初始化
e1000->tx_desc = (tx_desc_t *)alloc_kpage(1); // TODO:free
memset(e1000->tx_desc, 0, PAGE_SIZE);
e1000->tx_cur = 0;

e1000->tx_pbuf = (pbuf_t **)&e1000->tx_desc[TX_DESC_NR];
46 changes: 46 additions & 0 deletions src/kernel/onix.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include <onix/types.h>
#include <onix/net/chksum.h>
#include <onix/string.h>
#include <onix/debug.h>

static u32 __attribute__((section(".onix.addr"))) kernel_addr = 0x20000;
static u32 __attribute__((section(".onix.size"))) kernel_size = 0;
static u32 __attribute__((section(".onix.chksum"))) kernel_chksum = 0;
static u32 __attribute__((section(".onix.magic"))) kernel_magic = ONIX_MAGIC;

static u8 magic_msg[] = "kernel magic check failure!!!";
static u8 crc32_msg[] = "kernel crc32 check failure!!!";
static char *video = (char *)0xb8000;

extern void hang();

static void show_message(char *msg, int len)
{
for (int i = 0; i < len; i++)
video[i * 2] = msg[i];
}

err_t onix_init()
{
if (kernel_magic != ONIX_MAGIC)
{
show_message(magic_msg, sizeof(magic_msg));
goto failure;
}

u32 size = kernel_size;
u32 chksum = kernel_chksum;

kernel_chksum = 0;
kernel_size = 0;

u32 result = eth_fcs((void *)kernel_addr, size);
if (result != chksum)
{
show_message(crc32_msg, sizeof(crc32_msg));
goto failure;
}
return EOK;
failure:
hang();
}
3 changes: 2 additions & 1 deletion src/kernel/start.asm
Original file line number Diff line number Diff line change
@@ -17,6 +17,7 @@ header_start:
dd 8 ; size
header_end:

extern onix_init
extern device_init
extern console_init
extern gdt_init
@@ -32,7 +33,7 @@ global _start
_start:
push ebx; ards_count
push eax; magic

call onix_init ; 检测内核完整性
call device_init ; 虚拟设备初始化
call console_init ; 控制台初始化

7 changes: 5 additions & 2 deletions src/makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
BUILD:=../build
SRC:=.

MULTIBOOT2:=0x10000
ENTRYPOINT:=$(shell python -c "print(f'0x{$(MULTIBOOT2) + 64:x}')")
MULTIBOOT2:=0x18000
ENTRYPOINT:=$(shell python -c "print(f'0x{$(MULTIBOOT2) + 0x8000:x}')")

CFLAGS:= -m32 # 32 位的程序
CFLAGS+= -march=pentium # pentium 处理器
@@ -91,6 +91,7 @@ LDFLAGS:=$(strip ${LDFLAGS})

$(BUILD)/kernel.bin: \
$(BUILD)/kernel/start.o \
$(BUILD)/kernel/onix.o \
$(BUILD)/kernel/main.o \
$(BUILD)/kernel/io.o \
$(BUILD)/kernel/mio.o \
@@ -181,6 +182,8 @@ $(BUILD)/kernel.bin: \

$(BUILD)/system.bin: $(BUILD)/kernel.bin
objcopy -O binary $< $@
python utils/generate.py
objcopy -O binary $< $@

$(BUILD)/system.map: $(BUILD)/kernel.bin
nm $< | sort > $@
3 changes: 2 additions & 1 deletion src/net/netif.c
Original file line number Diff line number Diff line change
@@ -151,7 +151,8 @@ netif_t *netif_route(ip_addr_t addr)
for (list_node_t *ptr = list->head.next; ptr != &list->tail; ptr = ptr->next)
{
netif_t *netif = element_entry(netif_t, node, ptr);
if (ip_addr_maskcmp(addr, netif->ipaddr, netif->netmask))
if (ip_addr_maskcmp(addr, netif->ipaddr, netif->netmask) &&
!ip_addr_isany(netif->ipaddr))
{
return netif;
}
46 changes: 46 additions & 0 deletions src/utils/generate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# coding=utf-8

import os
import binascii
from elftools.elf.elffile import ELFFile

DIRNAME = os.path.dirname(os.path.abspath(__file__))
BUILD = os.path.abspath(os.path.join(DIRNAME, "../../build"))
KERNELBIN = os.path.join(BUILD, 'kernel.bin')
SYSTEMBIN = os.path.join(BUILD, 'system.bin')


def main():
start = 0x8000
size = os.path.getsize(SYSTEMBIN) - start

with open(SYSTEMBIN, 'rb') as file:
data = file.read()
chksum = binascii.crc32(data[start:])
print(f"system.bin chksum: {chksum} size {size}")

with open(KERNELBIN, 'rb+') as file:
elf = ELFFile(file)
section = elf.get_section_by_name(".onix.magic")
offset = section["sh_offset"]
file.seek(offset)
magic = int.from_bytes(file.read(4), "little")
if magic != 0x20220205:
print("magic check error")
return

section = elf.get_section_by_name(".onix.size")
offset = section["sh_offset"]
file.seek(offset)
file.write(size.to_bytes(4, "little"))
print(f"write size {size} offset {offset:x} success")

section = elf.get_section_by_name(".onix.chksum")
offset = section["sh_offset"]
file.seek(offset)
file.write(chksum.to_bytes(4, "little"))
print(f"write size {chksum:#x} offset {offset:x} success:")


if __name__ == '__main__':
main()
2 changes: 1 addition & 1 deletion src/utils/image.mk
Original file line number Diff line number Diff line change
@@ -27,7 +27,7 @@ $(BUILD)/master.img: $(BUILD)/boot/boot.bin \
dd if=$(BUILD)/boot/loader.bin of=$@ bs=512 count=4 seek=2 conv=notrunc

# 测试 system.bin 容量,否则需要修改下面的 count
test -n "$$(find $(BUILD)/system.bin -size -500k)"
test -n "$$(find $(BUILD)/system.bin -size -384k)"

# 将 system.bin 写入硬盘
dd if=$(BUILD)/system.bin of=$@ bs=512 count=1000 seek=10 conv=notrunc

0 comments on commit 8d50c90

Please sign in to comment.