diff --git a/src/boot/loader.asm b/src/boot/loader.asm index 24453053..11cd7d87 100644 --- a/src/boot/loader.asm +++ b/src/boot/loader.asm @@ -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; 表示出错 diff --git a/src/kernel/e1000.c b/src/kernel/e1000.c index 906525bd..b4ecdda1 100644 --- a/src/kernel/e1000.c +++ b/src/kernel/e1000.c @@ -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]; diff --git a/src/kernel/onix.c b/src/kernel/onix.c new file mode 100644 index 00000000..7bbf5f0f --- /dev/null +++ b/src/kernel/onix.c @@ -0,0 +1,46 @@ +#include +#include +#include +#include + +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(); +} diff --git a/src/kernel/start.asm b/src/kernel/start.asm index b8daa4ba..d1ff15a8 100644 --- a/src/kernel/start.asm +++ b/src/kernel/start.asm @@ -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 ; 控制台初始化 diff --git a/src/makefile b/src/makefile index 1abb698b..bc59c75c 100644 --- a/src/makefile +++ b/src/makefile @@ -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 > $@ diff --git a/src/net/netif.c b/src/net/netif.c index 5c559ed2..3a7d04cf 100644 --- a/src/net/netif.c +++ b/src/net/netif.c @@ -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; } diff --git a/src/utils/generate.py b/src/utils/generate.py new file mode 100644 index 00000000..9728492e --- /dev/null +++ b/src/utils/generate.py @@ -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() diff --git a/src/utils/image.mk b/src/utils/image.mk index a8365ef9..ae1616ed 100644 --- a/src/utils/image.mk +++ b/src/utils/image.mk @@ -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