├── day2.1 ├── hello.nas ├── makefile └── readme ├── day3.1 ├── hello.nas ├── makefile └── readme ├── day3.2 ├── hello.nas ├── makefile └── readme ├── day3.3 ├── hello.nas ├── makefile └── readme ├── day3.4 ├── hello.nas ├── makefile └── readme ├── day3.5 ├── haribote.nas ├── hello.nas ├── makefile └── readme ├── day3.7 ├── .hello.nas.bak.swp ├── haribote.nas ├── hello.nas ├── makefile └── readme ├── day3.8 ├── .hello.nas.bak.swp ├── haribote.nas ├── hello.nas ├── makefile └── readme ├── day3.9 ├── hello.nas ├── kernel │ ├── entry.S │ ├── header.h │ ├── main.c │ ├── makefile │ ├── mmu.h │ └── screen.c ├── makefile └── readme ├── day4.1 ├── hello.nas ├── kernel │ ├── entry.S │ ├── header.h │ ├── main.c │ ├── makefile │ ├── mmu.h │ ├── screen.c │ ├── types.h │ └── x86.h ├── makefile └── readme ├── day4.2 ├── hello.nas ├── kernel │ ├── entry.S │ ├── header.h │ ├── main.c │ ├── makefile │ ├── mmu.h │ ├── screen.c │ ├── types.h │ └── x86.h ├── makefile └── readme ├── day4.3 ├── hello.nas ├── kernel │ ├── entry.S │ ├── header.h │ ├── main.c │ ├── makefile │ ├── mmu.h │ ├── screen.c │ ├── types.h │ └── x86.h ├── makefile └── readme ├── day4.6 ├── hello.nas ├── kernel │ ├── entry.S │ ├── header.h │ ├── main.c │ ├── makefile │ ├── mmu.h │ ├── screen.c │ ├── types.h │ └── x86.h ├── makefile └── readme ├── day4.7 ├── hello.nas ├── kernel │ ├── entry.S │ ├── header.h │ ├── main.c │ ├── makefile │ ├── mmu.h │ ├── screen.c │ ├── types.h │ └── x86.h ├── makefile └── readme ├── day4.8 ├── hello.nas ├── kernel │ ├── entry.S │ ├── header.h │ ├── main.c │ ├── makefile │ ├── mmu.h │ ├── screen.c │ ├── types.h │ └── x86.h ├── makefile └── readme ├── day5.4 ├── hello.nas ├── kernel │ ├── entry.S │ ├── header.h │ ├── main.c │ ├── makefile │ ├── mmu.h │ ├── screen.c │ ├── types.h │ └── x86.h ├── makefile └── readme ├── day5.6 ├── hello.nas ├── kernel │ ├── entry.S │ ├── font.c │ ├── font.h │ ├── fontascii.h │ ├── header.h │ ├── main.c │ ├── makefile │ ├── mmu.h │ ├── screen.c │ ├── types.h │ └── x86.h ├── makefile └── readme ├── day5.7 ├── hello.nas ├── kernel │ ├── .print.c.swp │ ├── entry.S │ ├── font.c │ ├── font.h │ ├── fontascii.h │ ├── header.h │ ├── main.c │ ├── makefile │ ├── mmu.h │ ├── print.c │ ├── screen.c │ ├── types.h │ └── x86.h ├── makefile └── readme ├── day5.8 ├── hello.nas ├── kernel │ ├── .print.c.swp │ ├── entry.S │ ├── font.c │ ├── font.h │ ├── fontascii.h │ ├── header.h │ ├── main.c │ ├── makefile │ ├── mmu.h │ ├── print.c │ ├── print.o │ ├── screen.c │ ├── types.h │ └── x86.h ├── makefile └── readme ├── day5.9 ├── hello.nas ├── kernel │ ├── .print.c.swp │ ├── entry.S │ ├── entry.ld │ ├── font.c │ ├── font.h │ ├── fontascii.h │ ├── header.h │ ├── idtgdt.c │ ├── idtgdt.h │ ├── kernel.ld │ ├── main.c │ ├── makefile │ ├── makefile.bak │ ├── mmu.h │ ├── print.c │ ├── screen.c │ ├── types.h │ └── x86.h ├── makefile └── readme ├── day6 ├── hello.nas ├── kernel │ ├── asmint32.S │ ├── entry.S │ ├── entry.ld │ ├── font.c │ ├── font.h │ ├── fontascii.h │ ├── header.h │ ├── idtgdt.c │ ├── kernel.ld │ ├── main.c │ ├── makefile │ ├── mmu.h │ ├── print.c │ ├── run │ ├── screen.c │ ├── types.h │ └── x86.h ├── makefile └── readme ├── day7.1.1 ├── hello.nas ├── kernel │ ├── asmint32.S │ ├── entry.S │ ├── entry.ld │ ├── fifo.c │ ├── font.c │ ├── font.h │ ├── fontascii.h │ ├── header.h │ ├── idtgdt.c │ ├── int.c │ ├── kernel.ld │ ├── log │ ├── main.c │ ├── makefile │ ├── mmu.h │ ├── print.c │ ├── print.c.no │ ├── run │ ├── screen.c │ ├── types.h │ └── x86.h ├── makefile └── readme ├── day7.1 ├── hello.nas ├── kernel │ ├── asmint32.S │ ├── entry.S │ ├── entry.ld │ ├── font.c │ ├── font.h │ ├── fontascii.h │ ├── header.h │ ├── idtgdt.c │ ├── int.c │ ├── kernel.ld │ ├── main.c │ ├── makefile │ ├── mmu.h │ ├── print.c │ ├── run │ ├── screen.c │ ├── types.h │ └── x86.h └── makefile ├── day7.2 ├── hello.nas ├── kernel │ ├── asmint32.S │ ├── entry.S │ ├── entry.ld │ ├── fifo.c │ ├── font.c │ ├── font.h │ ├── fontascii.h │ ├── header.h │ ├── idtgdt.c │ ├── int.c │ ├── kernel.ld │ ├── log │ ├── main.c │ ├── makefile │ ├── mmu.h │ ├── print.c │ ├── print.c.no │ ├── run │ ├── screen.c │ ├── types.h │ └── x86.h ├── makefile └── readme ├── day7.3 ├── hello.nas ├── kernel │ ├── asmint32.S │ ├── entry.S │ ├── entry.ld │ ├── fifo.c │ ├── font.c │ ├── font.h │ ├── fontascii.h │ ├── header.h │ ├── idtgdt.c │ ├── int.c │ ├── kernel.ld │ ├── log │ ├── main.c │ ├── makefile │ ├── mmu.h │ ├── print.c │ ├── print.c.no │ ├── run │ ├── screen.c │ ├── types.h │ └── x86.h ├── log ├── log.txt └── makefile ├── day7.4 ├── hello.nas ├── kernel │ ├── asmint32.S │ ├── entry.S │ ├── entry.ld │ ├── fifo.c │ ├── font.c │ ├── font.h │ ├── fontascii.h │ ├── header.h │ ├── idtgdt.c │ ├── int.c │ ├── kernel.ld │ ├── log │ ├── main.c │ ├── makefile │ ├── mmu.h │ ├── print.c │ ├── print.c.no │ ├── run │ ├── screen.c │ ├── types.h │ └── x86.h ├── makefile └── readme ├── day7.5 ├── hello.nas ├── kernel │ ├── asmint32.S │ ├── entry.S │ ├── entry.ld │ ├── fifo.c │ ├── font.c │ ├── font.h │ ├── fontascii.h │ ├── header.h │ ├── idtgdt.c │ ├── int.c │ ├── log │ ├── main.c │ ├── makefile │ ├── mmu.h │ ├── print.c │ ├── print.c.no │ ├── run │ ├── screen.c │ ├── types.h │ └── x86.h ├── makefile └── readme ├── day7.7 ├── hello.nas ├── kernel │ ├── asmint32.S │ ├── entry.S │ ├── entry.ld │ ├── fifo.c │ ├── font.c │ ├── font.h │ ├── fontascii.h │ ├── header.h │ ├── idtgdt.c │ ├── int.c │ ├── kernel.ld │ ├── log │ ├── main.c │ ├── makefile │ ├── mmu.h │ ├── mouse.c │ ├── print.c │ ├── print.c.no │ ├── run │ ├── screen.c │ ├── types.h │ └── x86.h ├── makefile └── readme ├── day8.2 ├── hello.nas ├── kernel │ ├── asmint32.S │ ├── entry.S │ ├── entry.ld │ ├── fifo.c │ ├── font.c │ ├── font.h │ ├── fontascii.h │ ├── header.h │ ├── int.c │ ├── kernel.ld │ ├── log │ ├── main.c │ ├── makefile │ ├── mmu.h │ ├── mouse.c │ ├── print.c │ ├── print.c.no │ ├── run │ ├── screen.c │ ├── types.h │ └── x86.h ├── makefile └── readme ├── day8.3 ├── hello.nas ├── kernel │ ├── asmint32.S │ ├── entry.S │ ├── entry.ld │ ├── fifo.c │ ├── font.c │ ├── font.h │ ├── fontascii.h │ ├── header.h │ ├── idtgdt.c │ ├── int.c │ ├── kernel.ld │ ├── log │ ├── main.c │ ├── makefile │ ├── mmu.h │ ├── mouse.c │ ├── print.c │ ├── print.c.no │ ├── run │ ├── screen.c │ ├── types.h │ └── x86.h ├── makefile └── readme ├── day8.4 ├── hello.nas ├── kernel │ ├── asmint32.S │ ├── entry.S │ ├── entry.ld │ ├── fifo.c │ ├── font.c │ ├── font.h │ ├── fontascii.h │ ├── header.h │ ├── idtgdt.c │ ├── int.c │ ├── kernel.ld │ ├── log │ ├── main.c │ ├── makefile │ ├── mouse.c │ ├── print.c │ ├── print.c.no │ ├── run │ ├── screen.c │ ├── types.h │ └── x86.h ├── makefile └── readme ├── day9.2 ├── hello.nas ├── kernel │ ├── asmint32.S │ ├── entry.S │ ├── entry.ld │ ├── fifo.c │ ├── font.c │ ├── font.h │ ├── fontascii.h │ ├── header.h │ ├── idtgdt.c │ ├── int.c │ ├── kernel.ld │ ├── log │ ├── main.c │ ├── makefile │ ├── memory.c │ ├── mmu.h │ ├── mouse.c │ ├── print.c │ ├── print.c.no │ ├── run │ ├── screen.c │ ├── types.h │ └── x86.h ├── makefile └── readme ├── day9.3 ├── hello.nas ├── kernel │ ├── asmint32.S │ ├── entry.S │ ├── entry.ld │ ├── fifo.c │ ├── font.c │ ├── font.h │ ├── fontascii.h │ ├── header.h │ ├── idtgdt.c │ ├── int.c │ ├── kernel.ld │ ├── log │ ├── main.c │ ├── makefile │ ├── memory.c │ ├── mmu.h │ ├── mouse.c │ ├── print.c │ ├── print.c.no │ ├── run │ ├── screen.c │ ├── types.h │ └── x86.h ├── makefile └── readme ├── day9.4 ├── hello.nas ├── kernel │ ├── asmint32.S │ ├── entry.S │ ├── entry.ld │ ├── fifo.c │ ├── font.c │ ├── font.h │ ├── fontascii.h │ ├── header.h │ ├── idtgdt.c │ ├── int.c │ ├── kernel.ld │ ├── log │ ├── main.c │ ├── makefile │ ├── memory.c │ ├── mmu.h │ ├── mouse.c │ ├── print.c │ ├── print.c.no │ ├── run │ ├── screen.c │ ├── types.h │ └── x86.h ├── makefile └── readme └── readme.md /day2.1/hello.nas: -------------------------------------------------------------------------------- 1 | ;hello.nas 2 | org 0x7c00;跳转到内存地址0x7c00处 3 | 4 | fat12: 5 | ;设定fat12格式 6 | jmp entry 7 | db 0x90 8 | db "chris_zz"; 8bytes 9 | dw 512 10 | db 1 11 | dw 1 12 | db 2 13 | dw 224 14 | dw 2880 15 | db 0xf0 16 | dw 9 17 | dw 18 18 | dw 2 19 | dd 0 20 | dd 2880 21 | db 0,0,0x29 22 | dd 0xffffffff 23 | db "My First OS" 24 | db "FAT12 " 25 | 26 | entry: 27 | ;程序主要部分 28 | mov ax, 0 29 | mov ss, ax 30 | mov ds, ax 31 | mov es, ax 32 | 33 | mov sp, 0x7c00 34 | 35 | mov si, msg 36 | 37 | putloop: 38 | ;循环显示字符 39 | ;通过0x10中断,显示一个字符 40 | mov al, [si] 41 | add si, 1 42 | cmp al, 0 43 | je fin 44 | mov ah, 0x0e 45 | mov bx, 10 46 | int 0x10 47 | jmp putloop 48 | 49 | fin: 50 | ;当需要显示的信息都显示完毕,那么进入死循环 51 | hlt 52 | jmp fin 53 | 54 | msg: 55 | ;设定需要显示的字符 56 | db 0x0a, 0x0a 57 | db "=============" 58 | db 0x0a 59 | db 0x0d 60 | db "Nice day ~~~" 61 | db 0x0a 62 | db 0x0d 63 | db "This is my first 0S" 64 | db 0x0a 65 | db 0x0d 66 | db "copyright GPL" 67 | db 0x0a 68 | db 0x0d 69 | db "AUTHOR: ChrisZZ" 70 | db 0x0a 71 | db 0x0d 72 | db "blog:http://chriszz.sinaapp.com" 73 | db 0x0a 74 | db 0x0d 75 | db 0 76 | 77 | endpart: 78 | times 510-($-$$) db 0 79 | db 0x55, 0xaa 80 | -------------------------------------------------------------------------------- /day2.1/makefile: -------------------------------------------------------------------------------- 1 | subdir=./kernel 2 | all:img 3 | boot: 4 | nasm -o bootloader hello.nas 5 | img:boot 6 | dd if=bootloader of=myos.img count=1 bs=512 7 | dd if=/dev/zero of=myos.img bs=512 seek=1 skip=1 count=2879 8 | run: 9 | qemu-system-i386 -drive file=myos.img,if=floppy 10 | clean: 11 | rm bootloader myos.img 12 | -------------------------------------------------------------------------------- /day2.1/readme: -------------------------------------------------------------------------------- 1 | 根据P29和P24-25写成 2 | 代码的功能:读取软盘的第一个扇区(512byte)到内存0x7c00的地方 3 | 为什么是0x7c00,是因为当年PC机器出来的时候IBM的大叔们规定的。 4 | -------------------------------------------------------------------------------- /day3.1/hello.nas: -------------------------------------------------------------------------------- 1 | ;hello.nas 2 | org 0x7c00;跳转到内存地址0x7c00处 3 | 4 | fat12: 5 | ;设定fat12格式 6 | jmp entry 7 | db 0x90 8 | db "chris_zz"; 8bytes 9 | dw 512 10 | db 1 11 | dw 1 12 | db 2 13 | dw 224 14 | dw 2880 15 | db 0xf0 16 | dw 9 17 | dw 18 18 | dw 2 19 | dd 0 20 | dd 2880 21 | db 0,0,0x29 22 | dd 0xffffffff 23 | db "My First OS" 24 | db "FAT12 " 25 | 26 | entry: 27 | ;程序主要部分 28 | mov ax, 0 29 | mov ss, ax 30 | mov ds, ax 31 | mov es, ax 32 | 33 | mov sp, 0x7c00 34 | 35 | mov si, msg 36 | call putloop ;!!!本次添加部分 37 | ;这句本来写的是jmp putloop,后来到3.7天的时候发现和书上的现象对应不上,而且3.5天的0000:4400处的连续3个字符也不是F4 EB FD 38 | ;现在修改jmp为call,后面的这两个现象就能够对应上了! 39 | 40 | mov ax, 0x0820 ;!!!本次添加部分 41 | mov es, ax ;!!!本次添加部分 42 | mov ch, 0 ;!!!本次添加部分 43 | mov dh, 0 ;!!!本次添加部分 44 | mov cl, 2 ;!!!本次添加部分 45 | 46 | mov ah, 0x02 ;!!!本次添加部分 47 | mov al, 1 ;!!!本次添加部分 48 | mov bx, 0 ;!!!本次添加部分 49 | mov dl, 0x00 ;!!!本次添加部分 50 | int 0x13 ;!!!本次添加部分 51 | jc error ;!!!本次添加部分 52 | 53 | putloop: 54 | ;循环显示字符 55 | ;通过0x10中断,显示一个字符 56 | mov al, [si] 57 | add si, 1 58 | cmp al, 0 59 | je fin 60 | mov ah, 0x0e 61 | mov bx, 10 62 | int 0x10 63 | jmp putloop 64 | 65 | fin: 66 | ;当需要显示的信息都显示完毕,那么进入死循环 67 | hlt 68 | jmp fin 69 | 70 | msg: 71 | ;设定需要显示的字符 72 | db 0x0a, 0x0a 73 | db "=============" 74 | db 0x0a 75 | db 0x0d 76 | db "Nice day ~~~" 77 | db 0x0a 78 | db 0x0d 79 | db "This is my first 0S" 80 | db 0x0a 81 | db 0x0d 82 | db "copyright GPL" 83 | db 0x0a 84 | db 0x0d 85 | db "AUTHOR: ChrisZZ" 86 | db 0x0a 87 | db 0x0d 88 | db "blog:http://chriszz.sinaapp.com" 89 | db 0x0a 90 | db 0x0d 91 | db 0 92 | 93 | error: 94 | ;!!!本次添加部分 95 | db 0x0a, 0x0a 96 | db "XXXXXXXXXXX" 97 | db 0x0a, 0x0d 98 | db "error occured" 99 | db 0x0a, 0x0d 100 | 101 | endpart: 102 | times 510-($-$$) db 0 103 | db 0x55, 0xaa 104 | -------------------------------------------------------------------------------- /day3.1/makefile: -------------------------------------------------------------------------------- 1 | subdir=./kernel 2 | all:img 3 | boot: 4 | nasm -o bootloader hello.nas 5 | img:boot 6 | dd if=bootloader of=myos.img count=1 bs=512 7 | dd if=/dev/zero of=myos.img bs=512 seek=1 skip=1 count=2879 8 | run: 9 | qemu-system-i386 -drive file=myos.img,if=floppy 10 | clean: 11 | rm bootloader myos.img 12 | -------------------------------------------------------------------------------- /day3.1/readme: -------------------------------------------------------------------------------- 1 | 第3天第一部分,对应书上P46-P50 2 | 添加的代码,功能是:读取软盘的第二个扇区的内容,到内存0x8200开始的地方 3 | 为什么是加载到0x8200这个地方: 4 | 0x7c00-0x7dff:ipl(启动引导区) 5 | 0x7e00-0x7fff:ipl的数据和堆栈占用 6 | 0x8000-0x81ff:ipl的备份占用 7 | -------------------------------------------------------------------------------- /day3.2/hello.nas: -------------------------------------------------------------------------------- 1 | ;hello.nas 2 | org 0x7c00;跳转到内存地址0x7c00处 3 | 4 | fat12: 5 | ;设定fat12格式 6 | jmp entry 7 | db 0x90 8 | db "chris_zz"; 8bytes 9 | dw 512 10 | db 1 11 | dw 1 12 | db 2 13 | dw 224 14 | dw 2880 15 | db 0xf0 16 | dw 9 17 | dw 18 18 | dw 2 19 | dd 0 20 | dd 2880 21 | db 0,0,0x29 22 | dd 0xffffffff 23 | db "My First OS" 24 | db "FAT12 " 25 | 26 | entry: 27 | ;程序主要部分 28 | mov ax, 0 29 | mov ss, ax 30 | mov ds, ax 31 | mov es, ax 32 | 33 | mov sp, 0x7c00 34 | 35 | mov si, msg 36 | call putloop 37 | 38 | mov ax, 0x0820 39 | mov es, ax 40 | mov ch, 0 41 | mov dh, 0 42 | mov cl, 2 43 | 44 | mov si, 0 ;!!!本次添加部分 45 | 46 | retry: 47 | ;!!!本次修改部分 48 | mov ah, 0x02 49 | mov al, 1 50 | mov bx, 0 51 | mov dl, 0x00 52 | int 0x13 53 | jnc fin 54 | add si, 1 55 | cmp si, 5 56 | jae error 57 | mov ah, 0x00 ;BIOS系统复为 58 | mov dl, 0x00 59 | int 0x13 60 | jmp retry 61 | 62 | putloop: 63 | ;循环显示字符 64 | ;通过0x10中断,显示一个字符 65 | mov al, [si] 66 | add si, 1 67 | cmp al, 0 68 | je fin 69 | mov ah, 0x0e 70 | mov bx, 10 71 | int 0x10 72 | jmp putloop 73 | 74 | fin: 75 | ;当需要显示的信息都显示完毕,那么进入死循环 76 | hlt 77 | jmp fin 78 | 79 | msg: 80 | ;设定需要显示的字符 81 | db 0x0a, 0x0a 82 | db "=============" 83 | db 0x0a 84 | db 0x0d 85 | db "Nice day ~~~" 86 | db 0x0a 87 | db 0x0d 88 | db "This is my first 0S" 89 | db 0x0a 90 | db 0x0d 91 | db "copyright GPL" 92 | db 0x0a 93 | db 0x0d 94 | db "AUTHOR: ChrisZZ" 95 | db 0x0a 96 | db 0x0d 97 | db "blog:http://chriszz.sinaapp.com" 98 | db 0x0a 99 | db 0x0d 100 | db 0 101 | 102 | error: 103 | db 0x0a, 0x0a 104 | db "XXXXXXXXXXX" 105 | db 0x0a, 0x0d 106 | db "error occured" 107 | db 0x0a, 0x0d 108 | 109 | endpart: 110 | times 510-($-$$) db 0 111 | db 0x55, 0xaa 112 | -------------------------------------------------------------------------------- /day3.2/makefile: -------------------------------------------------------------------------------- 1 | subdir=./kernel 2 | all:img 3 | boot: 4 | nasm -o bootloader hello.nas 5 | img:boot 6 | dd if=bootloader of=myos.img count=1 bs=512 7 | dd if=/dev/zero of=myos.img bs=512 seek=1 skip=1 count=2879 8 | run: 9 | qemu-system-i386 -drive file=myos.img,if=floppy 10 | clean: 11 | rm bootloader myos.img 12 | -------------------------------------------------------------------------------- /day3.2/readme: -------------------------------------------------------------------------------- 1 | 为了防止软盘出错,尝试5次读取第二个扇区到内存。如果失败,那么重新读取。 2 | -------------------------------------------------------------------------------- /day3.3/hello.nas: -------------------------------------------------------------------------------- 1 | ;hello.nas 2 | org 0x7c00;跳转到内存地址0x7c00处 3 | 4 | fat12: 5 | ;设定fat12格式 6 | jmp entry 7 | db 0x90 8 | db "chris_zz"; 8bytes 9 | dw 512 10 | db 1 11 | dw 1 12 | db 2 13 | dw 224 14 | dw 2880 15 | db 0xf0 16 | dw 9 17 | dw 18 18 | dw 2 19 | dd 0 20 | dd 2880 21 | db 0,0,0x29 22 | dd 0xffffffff 23 | db "My First OS" 24 | db "FAT12 " 25 | 26 | entry: 27 | ;程序主要部分 28 | mov ax, 0 29 | mov ss, ax 30 | mov ds, ax 31 | mov es, ax 32 | 33 | mov sp, 0x7c00 34 | 35 | mov si, msg 36 | call putloop 37 | 38 | readsector: 39 | ;!!!为了方便,我把这段代码起名为readsector 40 | mov ax, 0x0820 41 | mov es, ax 42 | mov ch, 0 43 | mov dh, 0 44 | mov cl, 2 45 | readloop: ;!!!循环 46 | mov si, 0 47 | 48 | retry: 49 | mov ah, 0x02 50 | mov al, 1 51 | mov bx, 0 52 | mov dl, 0x00 53 | int 0x13 54 | jnc next ;!!!如果没有出错,那么跳转到next部分 55 | add si, 1 56 | cmp si, 5 57 | jae error 58 | mov ah, 0x00 ;BIOS系统复为 59 | mov dl, 0x00 60 | int 0x13 61 | jmp retry 62 | 63 | next: 64 | ;!!!读完18个扇区中剩余部分 65 | mov ax, es 66 | add ax, 0x0020 67 | mov es, ax 68 | add cl, 1 69 | cmp cl, 18 70 | jbe readloop 71 | 72 | putloop: 73 | ;循环显示字符 74 | ;通过0x10中断,显示一个字符 75 | mov al, [si] 76 | add si, 1 77 | cmp al, 0 78 | je fin 79 | mov ah, 0x0e 80 | mov bx, 10 81 | int 0x10 82 | jmp putloop 83 | 84 | fin: 85 | ;当需要显示的信息都显示完毕,那么进入死循环 86 | hlt 87 | jmp fin 88 | 89 | msg: 90 | ;设定需要显示的字符 91 | db 0x0a, 0x0a 92 | db "=============" 93 | db 0x0a 94 | db 0x0d 95 | db "Nice day ~~~" 96 | db 0x0a 97 | db 0x0d 98 | db "This is my first 0S" 99 | db 0x0a 100 | db 0x0d 101 | db "copyright GPL" 102 | db 0x0a 103 | db 0x0d 104 | db "AUTHOR: ChrisZZ" 105 | db 0x0a 106 | db 0x0d 107 | db "blog:http://chriszz.sinaapp.com" 108 | db 0x0a 109 | db 0x0d 110 | db 0 111 | 112 | error: 113 | db 0x0a, 0x0a 114 | db "XXXXXXXXXXX" 115 | db 0x0a, 0x0d 116 | db "error occured" 117 | db 0x0a, 0x0d 118 | 119 | endpart: 120 | times 510-($-$$) db 0 121 | db 0x55, 0xaa 122 | -------------------------------------------------------------------------------- /day3.3/makefile: -------------------------------------------------------------------------------- 1 | subdir=./kernel 2 | all:img 3 | boot: 4 | nasm -o bootloader hello.nas 5 | img:boot 6 | dd if=bootloader of=myos.img count=1 bs=512 7 | dd if=/dev/zero of=myos.img bs=512 seek=1 skip=1 count=2879 8 | run: 9 | qemu-system-i386 -drive file=myos.img,if=floppy 10 | clean: 11 | rm bootloader myos.img 12 | -------------------------------------------------------------------------------- /day3.3/readme: -------------------------------------------------------------------------------- 1 | 第3部分 2 | 读到第18个扇区 3 | -------------------------------------------------------------------------------- /day3.4/makefile: -------------------------------------------------------------------------------- 1 | subdir=./kernel 2 | all:img 3 | boot: 4 | nasm -o bootloader hello.nas 5 | img:boot 6 | dd if=bootloader of=myos.img count=1 bs=512 7 | dd if=/dev/zero of=myos.img bs=512 seek=1 skip=1 count=2879 8 | run: 9 | qemu-system-i386 -drive file=myos.img,if=floppy 10 | clean: 11 | rm bootloader myos.img 12 | -------------------------------------------------------------------------------- /day3.4/readme: -------------------------------------------------------------------------------- 1 | 读取10个柱面 2 | 注意在代码最开始部分设定CYLS常量值为10 3 | -------------------------------------------------------------------------------- /day3.5/haribote.nas: -------------------------------------------------------------------------------- 1 | fin: 2 | hlt 3 | jmp fin 4 | -------------------------------------------------------------------------------- /day3.5/makefile: -------------------------------------------------------------------------------- 1 | subdir=./kernel 2 | all:img 3 | boot: 4 | nasm -o bootloader hello.nas 5 | nasm -o haribote.bin haribote.nas 6 | img:boot 7 | dd if=bootloader of=myos.img count=1 bs=512 8 | dd if=/dev/zero of=myos.img bs=512 seek=1 skip=1 count=2879 9 | copy: 10 | mkdir -p /tmp/floppy 11 | mount -o loop myos.img /tmp/floppy -o fat=12 12 | sleep 1 13 | cp haribote.bin /tmp/floppy 14 | sleep 1 15 | umount /tmp/floppy 16 | run: 17 | qemu-system-i386 -drive file=myos.img,if=floppy 18 | clean: 19 | rm bootloader myos.img haribote.bin 20 | -------------------------------------------------------------------------------- /day3.5/readme: -------------------------------------------------------------------------------- 1 | 添加了haribote.nas文件,修改了makefile 2 | makefile的步骤: 3 | 1.创建myos.img(和前面几次一样) 4 | 2.将haribote.nas编译为haribote.bin 5 | 3.挂载myos.img 6 | 4.复制haribote.bin到myos.img上 7 | 5.卸载myos.img 8 | 9 | 运行步骤: 10 | make clean 11 | make 12 | sudo make copy 13 | make run 14 | 15 | 用16进制编辑器hexedit查看: 16 | hexedit myos.img 17 | 发现在地址为0000:2600的地方有数据了!成功! 18 | 19 | Attention! 20 | 1.hexedit需要下载并安装:sudo yum install hexedit -y 21 | 2.sudo make copy这一步是必须的,因为要拷贝,同时需要sudo权限 22 | 3.另一个有数据的地址为0000:4400,发现和书上不一样。网上查过别人的帖子,基本上都是4400.估计是《30天》使用的工具和我们不太相同,我们这个4400经过考证应该是正确的。原因如下: 23 | 24 | 查看了微软的硬件白皮书(fatgen.doc),发现这个0x4400是可以计算出来的: 25 | 第一个公式: 26 | RootDirSectors = ((BPB_RootEntCnt * 32) + (BPB_BytsPerSec – 1)) / BPB_BytsPerSec; 27 | 这里需要注意,要round up取整。 28 | 第二个公式: 29 | If(BPB_FATSz16 != 0) 30 | FATSz = BPB_FATSz16; 31 | Else 32 | FATSz = BPB_FATSz32; 33 | FirstDataSector = BPB_ResvdSecCnt + (BPB_NumFATs * FATSz) + RootDirSectors; 34 | 预先设定的值中,BPB_FATSx16等于9,BPB_ResvdSecCnt为1, BPB_NumFATs为2, RootDirSectors上面计算得到是15,那么: 35 | FirstData=34 36 | 那么,34*512,也就是4400H那里了(4400H/200H=22H=34, 200H就是512k) 37 | 38 | 39 | 这里还有一个问题,就是在计算RootDirSectors的时候,感觉分子上的(512-1)没有什么用处。网上有人这样解释: 40 | “"字节对齐作用",解释比较简单和牵强,不过你这句话提醒了我,我想微软的这个公式是同时用于计算FAT16和FAT12用的, 41 | 对于FAT16本身"(BPB_BytsPerSec – 1)/BPB_BytsPerSec"是没必要的,而对于FAT12由于它每个表项是1.5个字节,如果 42 | 应用不当的话有可能会造成"跨越扇区边界"的问题,故而保留了1个扇区,不知这样认识对不对,请朋友们继续发表看法. ”(ref:http://bbs.eeworld.com.cn/thread-131851-1-1.html) 43 | -------------------------------------------------------------------------------- /day3.7/.hello.nas.bak.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zchrissirhcz/osask-linux/a7afa7623205aaee125fc33676480d72c21e5323/day3.7/.hello.nas.bak.swp -------------------------------------------------------------------------------- /day3.7/haribote.nas: -------------------------------------------------------------------------------- 1 | ;haribote.nas 2 | org 0xc400 3 | 4 | mov al, 0x13 5 | mov ah, 0x00 6 | int 0x10 7 | 8 | fin: 9 | hlt 10 | jmp fin 11 | 12 | -------------------------------------------------------------------------------- /day3.7/makefile: -------------------------------------------------------------------------------- 1 | subdir=./kernel 2 | all:img 3 | boot: 4 | nasm -o bootloader hello.nas 5 | nasm -o haribote.bin haribote.nas 6 | img:boot 7 | dd if=bootloader of=myos.img count=1 bs=512 8 | dd if=/dev/zero of=myos.img bs=512 seek=1 skip=1 count=2879 9 | copy: 10 | mkdir -p /tmp/floppy 11 | mount -o loop myos.img /tmp/floppy -o fat=12 12 | sleep 1 13 | cp haribote.bin /tmp/floppy 14 | sleep 1 15 | umount /tmp/floppy 16 | run: 17 | qemu-system-i386 -drive file=myos.img,if=floppy 18 | clean: 19 | rm bootloader myos.img haribote.bin 20 | -------------------------------------------------------------------------------- /day3.7/readme: -------------------------------------------------------------------------------- 1 | day3.6和day3.7因为内容上是连贯的,所以就一起写在这里。 2 | 3 | 需要在hello.nas中添加跳转到0xc400的指令,以及将CYLS的值写入0x0ff0的地址处 4 | 5 | 为什么是0xc400? 6 | 我们前面分析过了,0x7c00-0x7dff是ipl,也就是第一个扇区,0x7e00-0x7fff是ipl的堆栈和数据区域,0x8000-0x81ff是ipl的备份区。 7 | 对于ipl备份区域的这个说法,我也是在百度贴吧找到的,个人感觉不是很靠谱。ipl明明就是写入到了前32k中的最后1k了嘛!也就是0x7c00-0x7fff 8 | 不过我们可以这么想:0x8000-0x81ff就相当于ipl,也就是软盘的第一个扇区的内容复制到的地址部分,那么0x8200开始就是第二个扇区,以此类推。 9 | 这样看来,软盘上地址为addr的内容会被复制到内存的addr+0x8000的地址处。 10 | 根据hexedit查看myos.img的结果,以及前面关于0x4400的公式推导,可以知道:软盘上地址为0x4400的数据区域(也就是操作系统的kernel处)对应的内存地址应该是0x4400+0x8000,也就是0xc400 11 | 12 | 因此,在hello.nas中应当跳转到0xc400处,而haribote.nas的作用,就相当于最初级的kernel。 13 | 因此,接下来的任务,就是写自己的kernel 14 | -------------------------------------------------------------------------------- /day3.8/.hello.nas.bak.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zchrissirhcz/osask-linux/a7afa7623205aaee125fc33676480d72c21e5323/day3.8/.hello.nas.bak.swp -------------------------------------------------------------------------------- /day3.8/haribote.nas: -------------------------------------------------------------------------------- 1 | ;haribote.nas 2 | CYLS EQU 0x0ff0 3 | LEDS EQU 0x0ff1 4 | VMODE EQU 0x0ff2 5 | SCRNX EQU 0x0ff4 6 | SCRNY EQU 0x0ff6 7 | VRAM EQU 0x0ff8 8 | 9 | org 0xc400 10 | 11 | mov al, 0x13 12 | mov ah, 0x00 13 | int 0x10 14 | 15 | mov byte [VMODE], 8 16 | mov word [SCRNX], 320 17 | mov word [SCRNY], 200 18 | mov dword [VRAM], 0x000a0000 19 | 20 | mov ah, 0x02 21 | int 0x16 22 | mov [LEDS], al 23 | 24 | mov si, mymsg ;!!!哦,让我们来显示自己的字符吧! 25 | call putchar 26 | 27 | fin: 28 | hlt 29 | jmp fin 30 | 31 | putchar: 32 | ;原作者真是无聊,3.8节竟然依然是黑框框。 33 | ;那么我们就来做点修改,显示一些字符吧! 34 | mov al, [si] 35 | add si, 1 36 | cmp al, 0 37 | je over 38 | mov ah, 0x0e 39 | mov bx, 15 40 | int 0x10 41 | jmp putchar 42 | over: 43 | ret 44 | 45 | mymsg: 46 | db 0x0a, 0x0d 47 | db "=========" 48 | db 0x0a, 0x0d 49 | db "Hey, man~~~I'm ChrisZZ" 50 | db 0x0a, 0x0d 51 | db "my kernel is running" 52 | db 0x00 53 | -------------------------------------------------------------------------------- /day3.8/makefile: -------------------------------------------------------------------------------- 1 | subdir=./kernel 2 | all:img 3 | boot: 4 | nasm -o bootloader hello.nas 5 | nasm -o haribote.bin haribote.nas 6 | img:boot 7 | dd if=bootloader of=myos.img count=1 bs=512 8 | dd if=/dev/zero of=myos.img bs=512 seek=1 skip=1 count=2879 9 | copy: 10 | mkdir -p /tmp/floppy 11 | mount -o loop myos.img /tmp/floppy -o fat=12 12 | sleep 1 13 | cp haribote.bin /tmp/floppy 14 | sleep 1 15 | umount /tmp/floppy 16 | run: 17 | qemu-system-i386 -drive file=myos.img,if=floppy 18 | clean: 19 | rm bootloader myos.img haribote.bin 20 | -------------------------------------------------------------------------------- /day3.8/readme: -------------------------------------------------------------------------------- 1 | day3.8 2 | 这一部分是在haribote.nas中修改代码。 3 | 原作者真是不够意思,书上的运行效果依然是黑框框。 4 | 5 | 那么,我们就自行添加一段代码,在kernel中打印自定义的字符串吧 6 | 7 | -------------------------------------------------------------------------------- /day3.9/kernel/header.h: -------------------------------------------------------------------------------- 1 | #ifndef header 2 | #define header 3 | 4 | 5 | #define io_halt() asm("hlt") 6 | #define write_mem8(addr,data8) (*(volatile char *)(addr))=(char)data8 7 | 8 | extern void clear_screen(char color) ; //color=15 pure white color=40 red 9 | extern void color_screen(char color) ; 10 | 11 | #endif -------------------------------------------------------------------------------- /day3.9/kernel/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ================================================== 3 | * 4 | * Filename: bootpack.c 5 | * 6 | * Description: 7 | * 8 | * Version: 0.01 9 | * Created: 2014年03月01日 星期六 22时03分34秒 10 | * Author: ChrisZZ, zchrissirhcz@163.com 11 | * Company: ZJUT 12 | * 13 | * ================================================== 14 | */ 15 | #include 16 | void bootmain(void){ 17 | //注意这里的函数名字为bootmain,因为在entry.S中设定的入口名字也是bootmain,两者要保持一致 18 | while(1); 19 | } 20 | -------------------------------------------------------------------------------- /day3.9/kernel/makefile: -------------------------------------------------------------------------------- 1 | addr=0x7c00 2 | OBJDIR=. 3 | 4 | CFLAGS := $(CFLAGS) -O1 -fno-builtin -I$(OBJDIR) -MD 5 | CFLAGS += -fno-omit-frame-pointer 6 | CFLAGS += -Wall -Wno-format -Wno-unused -Werror -gstabs -m32 7 | 8 | 9 | LDFLAGS=-m elf_i386 10 | 11 | all:kernel 12 | 13 | kernel:entry.o main.o screen.o 14 | ld $(LDFLAGS) -N -e start -Ttext $(addr) -o $@.out $^ 15 | objdump -S $@.out >$@.asm 16 | objcopy -S -O binary -j .text $@.out $@ 17 | 18 | entry.o:entry.S 19 | gcc -nostdinc $(CFLAGS) -c -o $@ $< 20 | main.o:main.c 21 | @#对应《30天》中的bootpack.c 22 | gcc -nostdinc $(CFLAGS) -Os -c -o $@ $< 23 | screen.o:screen.c 24 | gcc -nostdinc $(CFLAGS) -c -o $@ $< 25 | 26 | run: 27 | qemu-system-i386 -drive file=kernel,if=floppy 28 | 29 | clean: 30 | @rm -f entry.o main.o screen.o kernel.out kernel.asm kernel *.d 31 | -------------------------------------------------------------------------------- /day3.9/kernel/screen.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void clear_screen(char color) //15:pure white 4 | { 5 | int i; 6 | for(i=0xa0000;i<0xaffff;i++) 7 | { 8 | write_mem8(i,color); //if we write 15 ,all pixels color will be white,15 mens pure white ,so the screen changes into white 9 | 10 | } 11 | } 12 | 13 | void color_screen(char color) //15:pure white 14 | { 15 | int i; 16 | color=color; 17 | for(i=0xa0000;i<0xaffff;i++) 18 | { 19 | write_mem8(i,i&0x0f); //if we write 15 ,all pixels color will be white,15 mens pure white ,so the screen changes into white 20 | 21 | } 22 | } -------------------------------------------------------------------------------- /day3.9/makefile: -------------------------------------------------------------------------------- 1 | subdir=./kernel 2 | all: 3 | # nasm -o bootloader hello.nas 4 | nasm -o bootloader hello.nas 5 | @cd $(subdir);make clean;make addr=0xc400 6 | 7 | img: 8 | dd if=bootloader of=myos.img bs=512 count=1 9 | dd if=/dev/zero of=myos.img bs=512 seek=1 skip=1 count=2879 10 | 11 | copy:img 12 | mkdir -p /tmp/floppy 13 | mount -o loop myos.img /tmp/floppy -o fat=12 14 | sleep 1 15 | cp $(subdir)/kernel /tmp/floppy 16 | sleep 1 17 | umount /tmp/floppy 18 | run: 19 | qemu-system-i386 -drive file=myos.img,if=floppy 20 | clean: 21 | @rm -f bootloader myos.img haribote.bin 22 | -------------------------------------------------------------------------------- /day3.9/readme: -------------------------------------------------------------------------------- 1 | day3.9 2 | 这里是导入C语言了。原作者用了一堆工具:cc1.exe,gas2nask.exe等。 3 | 这些工具是从linux下改造到windows下的。 4 | 为什么不在linux下用linux下的工具呢? 5 | 这也正是我动手在fedora下实践《30天》的原因。 6 | 7 | 创建kernel目录,在里面添加entry.S,screen.c,mmu.h等代码。和原作者说的一样,这里我们不求理解,要到后面才能完全理解这里做了什么。 8 | 这里唯一的目标就是,能够导入C语言并使用。 9 | makefile对应做了修改:添加了kernel目录下的makefile;外层的makefile调用内层的makefile; 10 | 运行的时候也相应调整为(在外层运行): 11 | make 12 | sudo make copy 13 | sudo make run (这里现在也需要权限了) 14 | 15 | 注意里层的makefile编写的时候,gcc的参数-m32表示编译为32位,因为x86架构无法运行64位程序;ld的参数-m elf_i386也是为32位设定的。 16 | 如果你用来开发的电脑是32位的,那么不写这两个参数也是ok的。 17 | 18 | 原作者的C语言代码为bootpack.c,这里修改为main.c,并且函数名因为entry.S中的设定,main.c中修改为bootmain 19 | 20 | 21 | ====================== 22 | 在代码的调式过程中,遇到一个问题,已经解决了,分享一下: 23 | makefile中的run部分,用的是qemu命令: 24 | qemu-system-i386 file-kernel,if=floppy 25 | 但是当我写成: 26 | qemu-system-i386 file-kernel, if=floppy 27 | 就会报错说: 28 | drive with bus=0, unit=0 (index=0) exists 29 | 30 | 空格、缩进什么的真的很重要。。。 31 | 32 | 33 | -------------------------------------------------------------------------------- /day4.1/kernel/header.h: -------------------------------------------------------------------------------- 1 | #ifndef header 2 | #define header 3 | #include 4 | 5 | 6 | #define io_halt() asm("hlt") 7 | #define write_mem8(addr,data8) (*(volatile char *)(addr))=(char)data8 8 | 9 | extern void clear_screen(char color) ; //color=15 pure white color=40 red 10 | extern void color_screen(char color) ; 11 | 12 | #endif 13 | -------------------------------------------------------------------------------- /day4.1/kernel/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ================================================== 3 | * 4 | * Filename: bootpack.c 5 | * 6 | * Description: 7 | * 8 | * Version: 0.01 9 | * Created: 2014年03月01日 星期六 22时03分34秒 10 | * Author: ChrisZZ, zchrissirhcz@163.com 11 | * Company: ZJUT 12 | * 13 | * ================================================== 14 | */ 15 | #include 16 | void bootmain(void){ 17 | //注意这里的函数名字为bootmain,因为在entry.S中设定的入口名字也是bootmain,两者要保持一致 18 | int i; 19 | for(i=0xa0000; i<=0xaffff; i++){ 20 | write_mem8(i, 15); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /day4.1/kernel/makefile: -------------------------------------------------------------------------------- 1 | addr=0x7c00 2 | OBJDIR=. 3 | 4 | CFLAGS := $(CFLAGS) -O1 -fno-builtin -I$(OBJDIR) -MD 5 | CFLAGS += -fno-omit-frame-pointer 6 | CFLAGS += -Wall -Wno-format -Wno-unused -Werror -gstabs -m32 7 | 8 | 9 | LDFLAGS=-m elf_i386 10 | 11 | all:kernel 12 | 13 | kernel:entry.o main.o screen.o 14 | ld $(LDFLAGS) -N -e start -Ttext $(addr) -o $@.out $^ 15 | objdump -S $@.out >$@.asm 16 | objcopy -S -O binary -j .text $@.out $@ 17 | 18 | entry.o:entry.S 19 | gcc -nostdinc $(CFLAGS) -c -o $@ $< 20 | main.o:main.c 21 | @#对应《30天》中的bootpack.c 22 | gcc -nostdinc $(CFLAGS) -Os -c -o $@ $< 23 | screen.o:screen.c 24 | gcc -nostdinc $(CFLAGS) -c -o $@ $< 25 | 26 | run: 27 | qemu-system-i386 -drive file=kernel,if=floppy 28 | 29 | clean: 30 | @rm -f entry.o main.o screen.o kernel.out kernel.asm kernel *.d 31 | -------------------------------------------------------------------------------- /day4.1/kernel/screen.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void clear_screen(char color) //15:pure white 4 | { 5 | int i; 6 | for(i=0xa0000;i<0xaffff;i++) 7 | { 8 | write_mem8(i,color); //if we write 15 ,all pixels color will be white,15 mens pure white ,so the screen changes into white 9 | 10 | } 11 | } 12 | 13 | void color_screen(char color) //15:pure white 14 | { 15 | int i; 16 | color=color; 17 | for(i=0xa0000;i<0xaffff;i++) 18 | { 19 | write_mem8(i,i&0x0f); //if we write 15 ,all pixels color will be white,15 mens pure white ,so the screen changes into white 20 | 21 | } 22 | } -------------------------------------------------------------------------------- /day4.1/makefile: -------------------------------------------------------------------------------- 1 | subdir=./kernel 2 | all: 3 | # nasm -o bootloader hello.nas 4 | nasm -o bootloader hello.nas 5 | @cd $(subdir);make clean;make addr=0xc400 6 | 7 | img: 8 | dd if=bootloader of=myos.img bs=512 count=1 9 | dd if=/dev/zero of=myos.img bs=512 seek=1 skip=1 count=2879 10 | 11 | copy:img 12 | mkdir -p /tmp/floppy 13 | mount -o loop myos.img /tmp/floppy -o fat=12 14 | sleep 1 15 | cp $(subdir)/kernel /tmp/floppy 16 | sleep 1 17 | umount /tmp/floppy 18 | run: 19 | qemu-system-i386 -drive file=myos.img,if=floppy 20 | clean: 21 | @rm -f bootloader myos.img haribote.bin 22 | -------------------------------------------------------------------------------- /day4.1/readme: -------------------------------------------------------------------------------- 1 | day4.1 2 | 书上day3.10提到了c语言,写了一个naskfunc.nas。我们不这么做。 3 | 我们通过两个宏定义来代替naskfunc.nas中的write_mem8和io_hlt,在header.h中定义 4 | 注意这里我们把名字io_hlt替换了为io_halt 5 | 6 | 修改main.c,调用刚刚定义的write_mem8,通过显存部分的0xa0000-0xaffff都设定write_mem8(i,15),整个屏幕就变成了白色。 7 | -------------------------------------------------------------------------------- /day4.2/kernel/header.h: -------------------------------------------------------------------------------- 1 | #ifndef header 2 | #define header 3 | #include 4 | 5 | 6 | #define io_halt() asm("hlt") 7 | #define write_mem8(addr,data8) (*(volatile char *)(addr))=(char)data8 8 | 9 | extern void clear_screen(char color) ; //color=15 pure white color=40 red 10 | extern void color_screen(char color) ; 11 | 12 | #endif 13 | -------------------------------------------------------------------------------- /day4.2/kernel/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ================================================== 3 | * 4 | * Filename: bootpack.c 5 | * 6 | * Description: 7 | * 8 | * Version: 0.01 9 | * Created: 2014年03月01日 星期六 22时03分34秒 10 | * Author: ChrisZZ, zchrissirhcz@163.com 11 | * Company: ZJUT 12 | * 13 | * ================================================== 14 | */ 15 | #include 16 | void bootmain(void){ 17 | //注意这里的函数名字为bootmain,因为在entry.S中设定的入口名字也是bootmain,两者要保持一致 18 | int i; 19 | for(i=0xa0000; i<=0xaffff; i++){ 20 | write_mem8(i, i&0x0f); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /day4.2/kernel/makefile: -------------------------------------------------------------------------------- 1 | addr=0x7c00 2 | OBJDIR=. 3 | 4 | CFLAGS := $(CFLAGS) -O1 -fno-builtin -I$(OBJDIR) -MD 5 | CFLAGS += -fno-omit-frame-pointer 6 | CFLAGS += -Wall -Wno-format -Wno-unused -Werror -gstabs -m32 7 | 8 | 9 | LDFLAGS=-m elf_i386 10 | 11 | all:kernel 12 | 13 | kernel:entry.o main.o screen.o 14 | ld $(LDFLAGS) -N -e start -Ttext $(addr) -o $@.out $^ 15 | objdump -S $@.out >$@.asm 16 | objcopy -S -O binary -j .text $@.out $@ 17 | 18 | entry.o:entry.S 19 | gcc -nostdinc $(CFLAGS) -c -o $@ $< 20 | main.o:main.c 21 | @#对应《30天》中的bootpack.c 22 | gcc -nostdinc $(CFLAGS) -Os -c -o $@ $< 23 | screen.o:screen.c 24 | gcc -nostdinc $(CFLAGS) -c -o $@ $< 25 | 26 | run: 27 | qemu-system-i386 -drive file=kernel,if=floppy 28 | 29 | clean: 30 | @rm -f entry.o main.o screen.o kernel.out kernel.asm kernel *.d 31 | -------------------------------------------------------------------------------- /day4.2/kernel/screen.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void clear_screen(char color) //15:pure white 4 | { 5 | int i; 6 | for(i=0xa0000;i<0xaffff;i++) 7 | { 8 | write_mem8(i,color); //if we write 15 ,all pixels color will be white,15 mens pure white ,so the screen changes into white 9 | 10 | } 11 | } 12 | 13 | void color_screen(char color) //15:pure white 14 | { 15 | int i; 16 | color=color; 17 | for(i=0xa0000;i<0xaffff;i++) 18 | { 19 | write_mem8(i,i&0x0f); //if we write 15 ,all pixels color will be white,15 mens pure white ,so the screen changes into white 20 | 21 | } 22 | } -------------------------------------------------------------------------------- /day4.2/makefile: -------------------------------------------------------------------------------- 1 | subdir=./kernel 2 | all: 3 | # nasm -o bootloader hello.nas 4 | nasm -o bootloader hello.nas 5 | @cd $(subdir);make clean;make addr=0xc400 6 | 7 | img: 8 | dd if=bootloader of=myos.img bs=512 count=1 9 | dd if=/dev/zero of=myos.img bs=512 seek=1 skip=1 count=2879 10 | 11 | copy:img 12 | mkdir -p /tmp/floppy 13 | mount -o loop myos.img /tmp/floppy -o fat=12 14 | sleep 1 15 | cp $(subdir)/kernel /tmp/floppy 16 | sleep 1 17 | umount /tmp/floppy 18 | run: 19 | qemu-system-i386 -drive file=myos.img,if=floppy 20 | clean: 21 | @rm -f bootloader myos.img haribote.bin 22 | -------------------------------------------------------------------------------- /day4.2/readme: -------------------------------------------------------------------------------- 1 | day4.2 2 | 这里用&运算的技巧,显示条纹。 3 | -------------------------------------------------------------------------------- /day4.3/kernel/header.h: -------------------------------------------------------------------------------- 1 | #ifndef header 2 | #define header 3 | #include 4 | 5 | 6 | #define io_halt() asm("hlt") 7 | #define write_mem8(addr,data8) (*(volatile char *)(addr))=(char)data8 8 | 9 | extern void clear_screen(char color) ; //color=15 pure white color=40 red 10 | extern void color_screen(char color) ; 11 | 12 | #endif 13 | -------------------------------------------------------------------------------- /day4.3/kernel/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ================================================== 3 | * 4 | * Filename: bootpack.c 5 | * 6 | * Description: 7 | * 8 | * Version: 0.01 9 | * Created: 2014年03月01日 星期六 22时03分34秒 10 | * Author: ChrisZZ, zchrissirhcz@163.com 11 | * Company: ZJUT 12 | * 13 | * ================================================== 14 | */ 15 | #include 16 | void bootmain(void){ 17 | //注意这里的函数名字为bootmain,因为在entry.S中设定的入口名字也是bootmain,两者要保持一致 18 | int i; 19 | char *p; 20 | for(i=0xa0000; i<=0xaffff; i++){ 21 | //write_mem8(i, i&0x0f); 22 | p=(char *)i; 23 | *p=i&0x0f; 24 | } 25 | 26 | for(;;){ 27 | io_halt(); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /day4.3/kernel/makefile: -------------------------------------------------------------------------------- 1 | addr=0x7c00 2 | OBJDIR=. 3 | 4 | CFLAGS := $(CFLAGS) -O1 -fno-builtin -I$(OBJDIR) -MD 5 | CFLAGS += -fno-omit-frame-pointer 6 | CFLAGS += -Wall -Wno-format -Wno-unused -Werror -gstabs -m32 7 | 8 | 9 | LDFLAGS=-m elf_i386 10 | 11 | all:kernel 12 | 13 | kernel:entry.o main.o screen.o 14 | ld $(LDFLAGS) -N -e start -Ttext $(addr) -o $@.out $^ 15 | objdump -S $@.out >$@.asm 16 | objcopy -S -O binary -j .text $@.out $@ 17 | 18 | entry.o:entry.S 19 | gcc -nostdinc $(CFLAGS) -c -o $@ $< 20 | main.o:main.c 21 | @#对应《30天》中的bootpack.c 22 | gcc -nostdinc $(CFLAGS) -Os -c -o $@ $< 23 | screen.o:screen.c 24 | gcc -nostdinc $(CFLAGS) -c -o $@ $< 25 | 26 | run: 27 | qemu-system-i386 -drive file=kernel,if=floppy 28 | 29 | clean: 30 | @rm -f entry.o main.o screen.o kernel.out kernel.asm kernel *.d 31 | -------------------------------------------------------------------------------- /day4.3/kernel/screen.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void clear_screen(char color) //15:pure white 4 | { 5 | int i; 6 | for(i=0xa0000;i<0xaffff;i++) 7 | { 8 | write_mem8(i,color); //if we write 15 ,all pixels color will be white,15 mens pure white ,so the screen changes into white 9 | 10 | } 11 | } 12 | 13 | void color_screen(char color) //15:pure white 14 | { 15 | int i; 16 | color=color; 17 | for(i=0xa0000;i<0xaffff;i++) 18 | { 19 | write_mem8(i,i&0x0f); //if we write 15 ,all pixels color will be white,15 mens pure white ,so the screen changes into white 20 | 21 | } 22 | } -------------------------------------------------------------------------------- /day4.3/makefile: -------------------------------------------------------------------------------- 1 | subdir=./kernel 2 | all: 3 | # nasm -o bootloader hello.nas 4 | nasm -o bootloader hello.nas 5 | @cd $(subdir);make clean;make addr=0xc400 6 | 7 | img: 8 | dd if=bootloader of=myos.img bs=512 count=1 9 | dd if=/dev/zero of=myos.img bs=512 seek=1 skip=1 count=2879 10 | 11 | copy:img 12 | mkdir -p /tmp/floppy 13 | mount -o loop myos.img /tmp/floppy -o fat=12 14 | sleep 1 15 | cp $(subdir)/kernel /tmp/floppy 16 | sleep 1 17 | umount /tmp/floppy 18 | run: 19 | qemu-system-i386 -drive file=myos.img,if=floppy 20 | clean: 21 | @rm -f bootloader myos.img haribote.bin 22 | -------------------------------------------------------------------------------- /day4.3/readme: -------------------------------------------------------------------------------- 1 | day4.3 2 | 所谓挑战指针,这里就是把write_mem8()函数用指针代替,把颜色值写入对应的内存地址. 3 | 为了避免任何warning,注意使用类型转换. 4 | -------------------------------------------------------------------------------- /day4.6/kernel/header.h: -------------------------------------------------------------------------------- 1 | #ifndef header 2 | #define header 3 | #include 4 | 5 | 6 | #define io_halt() asm("hlt") 7 | #define write_mem8(addr,data8) (*(volatile char *)(addr))=(char)data8 8 | 9 | #define io_cli() asm("cli") //!!!本次添加部分 10 | #define io_sti() asm("sti")//!!!本次添加部分 11 | 12 | 13 | extern void clear_screen(char color) ; //color=15 pure white color=40 red 14 | extern void color_screen(char color) ; 15 | 16 | extern void init_palette(void); 17 | extern void set_palette(int start, int end, unsigned char *rgb); 18 | 19 | #endif 20 | -------------------------------------------------------------------------------- /day4.6/kernel/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ================================================== 3 | * 4 | * Filename: bootpack.c 5 | * 6 | * Description: 7 | * 8 | * Version: 0.01 9 | * Created: 2014年03月01日 星期六 22时03分34秒 10 | * Author: ChrisZZ, zchrissirhcz@163.com 11 | * Company: ZJUT 12 | * 13 | * ================================================== 14 | */ 15 | #include 16 | void bootmain(void){ 17 | //注意这里的函数名字为bootmain,因为在entry.S中设定的入口名字也是bootmain,两者要保持一致 18 | int i; 19 | char *p; 20 | init_palette(); 21 | for(i=0xa0000; i<=0xaffff; i++){ 22 | //write_mem8(i, i&0x0f); 23 | p=(char *)i; 24 | *p=i&0x0f; 25 | } 26 | 27 | for(;;){ 28 | io_halt(); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /day4.6/kernel/makefile: -------------------------------------------------------------------------------- 1 | addr=0x7c00 2 | OBJDIR=. 3 | 4 | CFLAGS := $(CFLAGS) -O1 -fno-builtin -I$(OBJDIR) -MD 5 | CFLAGS += -fno-omit-frame-pointer 6 | CFLAGS += -Wall -Wno-format -Wno-unused -Werror -gstabs -m32 7 | 8 | 9 | LDFLAGS=-m elf_i386 10 | 11 | all:kernel 12 | 13 | kernel:entry.o main.o screen.o 14 | ld $(LDFLAGS) -N -e start -Ttext $(addr) -o $@.out $^ 15 | objdump -S $@.out >$@.asm 16 | # objcopy -S -O binary -j .text $@.out $@ 17 | objcopy -S -O binary $@.out $@ 18 | 19 | entry.o:entry.S 20 | gcc -nostdinc $(CFLAGS) -c -o $@ $< 21 | main.o:main.c 22 | @#对应《30天》中的bootpack.c 23 | gcc -nostdinc $(CFLAGS) -Os -c -o $@ $< 24 | screen.o:screen.c 25 | gcc -nostdinc $(CFLAGS) -c -o $@ $< 26 | 27 | run: 28 | qemu-system-i386 -drive file=kernel,if=floppy 29 | 30 | clean: 31 | @rm -f entry.o main.o screen.o kernel.out kernel.asm kernel *.d 32 | -------------------------------------------------------------------------------- /day4.6/kernel/screen.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void clear_screen(char color) //15:pure white 4 | { 5 | int i; 6 | for(i=0xa0000;i<0xaffff;i++) 7 | { 8 | write_mem8(i,color); //if we write 15 ,all pixels color will be white,15 mens pure white ,so the screen changes into white 9 | 10 | } 11 | } 12 | 13 | void color_screen(char color) //15:pure white 14 | { 15 | int i; 16 | color=color; 17 | for(i=0xa0000;i<0xaffff;i++) 18 | { 19 | write_mem8(i,i&0x0f); //if we write 15 ,all pixels color will be white,15 mens pure white ,so the screen changes into white 20 | 21 | } 22 | } 23 | 24 | void set_palette(int start, int end, unsigned char* rgb){ 25 | int i, eflags; 26 | eflags = read_eflags(); //替代作者的io_load_eflags() 27 | io_cli(); 28 | outb(0x03c8, start); //替代作者的io_out8() 29 | for(i=start; i<=end; i++){ 30 | outb(0x03c9,rgb[0]/4); 31 | outb(0x03c9,rgb[1]/4); 32 | outb(0x03c9,rgb[2]/4); 33 | rgb+=3; 34 | } 35 | write_eflags(eflags); //替代作者的io_store_eflags(eflags) 36 | return; 37 | } 38 | 39 | 40 | void init_palette(void){ 41 | //16种color,每个color三个字节。 42 | static unsigned char table_rgb[16*3]= 43 | { 44 | 0x00,0x00,0x00, /*0:black*/ 45 | 0xff,0x00,0x00, /*1:light red*/ 46 | 0x00,0xff,0x00, /*2:light green*/ 47 | 0xff,0xff,0x00, /*3:light yellow*/ 48 | 49 | 0x00,0x00,0xff, /*4:light blue*/ 50 | 0xff,0x00,0xff, /*5:light purper*/ 51 | 0x00,0xff,0xff, /*6:light blue*/ 52 | 0xff,0xff,0xff, /*7:white*/ 53 | 54 | 0xc6,0xc6,0xc6, /*8:light gray*/ 55 | 0x84,0x00,0x00, /*9:dark red*/ 56 | 0x00,0x84,0x00, /*10:dark green*/ 57 | 0x84,0x84,0x00, /*11:dark yellow*/ 58 | 59 | 0x00,0x00,0x84, /*12:dark 青*/ 60 | 0x84,0x00,0x84, /*13:dark purper*/ 61 | 0x00,0x84,0x84, /*14:light blue*/ 62 | 0x84,0x84,0x84, /*15:dark gray*/ 63 | }; 64 | set_palette(0,15,table_rgb); 65 | return; 66 | } 67 | -------------------------------------------------------------------------------- /day4.6/makefile: -------------------------------------------------------------------------------- 1 | subdir=./kernel 2 | all: 3 | # nasm -o bootloader hello.nas 4 | nasm -o bootloader hello.nas 5 | @cd $(subdir);make clean;make addr=0xc400 6 | 7 | img: 8 | dd if=bootloader of=myos.img bs=512 count=1 9 | dd if=/dev/zero of=myos.img bs=512 seek=1 skip=1 count=2879 10 | 11 | copy:img 12 | mkdir -p /tmp/floppy 13 | mount -o loop myos.img /tmp/floppy -o fat=12 14 | sleep 1 15 | cp $(subdir)/kernel /tmp/floppy 16 | sleep 1 17 | umount /tmp/floppy 18 | run: 19 | qemu-system-i386 -drive file=myos.img,if=floppy 20 | clean: 21 | @rm -f bootloader myos.img haribote.bin 22 | -------------------------------------------------------------------------------- /day4.6/readme: -------------------------------------------------------------------------------- 1 | day4.6 2 | 这部分作者的naskfunc.c定义了好多函数,不过我们用C语言的几个函数取而代之吧 3 | 4 | 首先修改main.c函数,添加init_palette()的调用 5 | 其次在header.h中声明init_palette().用extern的形式,那么只需要在screen.c中#include并定义init_palette()即可 6 | 在screen.c中定义init_palette()和set_palette(). init_palette()会调用set_palette() 7 | -------------------------------------------------------------------------------- /day4.7/kernel/header.h: -------------------------------------------------------------------------------- 1 | #ifndef header 2 | #define header 3 | #include 4 | 5 | 6 | #define io_halt() asm("hlt") 7 | #define write_mem8(addr,data8) (*(volatile char *)(addr))=(char)data8 8 | 9 | #define io_cli() asm("cli") 10 | #define io_sti() asm("sti") 11 | 12 | 13 | extern void clear_screen(char color) ; //color=15 pure white color=40 red 14 | extern void color_screen(char color) ; 15 | 16 | extern void init_palette(void); 17 | extern void set_palette(int start, int end, unsigned char *rgb); 18 | 19 | extern void boxfill8(char* vram, int xsize, unsigned char c, int x0, int y0, int x1, int y1); 20 | 21 | #endif 22 | -------------------------------------------------------------------------------- /day4.7/kernel/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ================================================== 3 | * 4 | * Filename: bootpack.c 5 | * 6 | * Description: 7 | * 8 | * Version: 0.01 9 | * Created: 2014年03月01日 星期六 22时03分34秒 10 | * Author: ChrisZZ, zchrissirhcz@163.com 11 | * Company: ZJUT 12 | * 13 | * ================================================== 14 | */ 15 | #include 16 | void bootmain(void){ 17 | //注意这里的函数名字为bootmain,因为在entry.S中设定的入口名字也是bootmain,两者要保持一致 18 | int i; 19 | char *p; 20 | init_palette(); 21 | 22 | clear_screen(15); //由于一进来这里显示的是"my kernel is running"等字符串,这里刷屏为灰色,去除字符串 23 | 24 | p=(char*)0xa0000; 25 | boxfill8(p, 320, 1, 20, 20, 120, 120); 26 | boxfill8(p, 320, 2, 70, 50, 170, 150); 27 | boxfill8(p, 320, 4, 120, 80, 220, 180); 28 | 29 | for(;;){ 30 | io_halt(); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /day4.7/kernel/makefile: -------------------------------------------------------------------------------- 1 | addr=0x7c00 2 | OBJDIR=. 3 | 4 | CFLAGS := $(CFLAGS) -O1 -fno-builtin -I$(OBJDIR) -MD 5 | CFLAGS += -fno-omit-frame-pointer 6 | CFLAGS += -Wall -Wno-format -Wno-unused -Werror -gstabs -m32 7 | 8 | 9 | LDFLAGS=-m elf_i386 10 | 11 | all:kernel 12 | 13 | kernel:entry.o main.o screen.o 14 | ld $(LDFLAGS) -N -e start -Ttext $(addr) -o $@.out $^ 15 | objdump -S $@.out >$@.asm 16 | # objcopy -S -O binary -j .text $@.out $@ 17 | objcopy -S -O binary $@.out $@ 18 | 19 | entry.o:entry.S 20 | gcc -nostdinc $(CFLAGS) -c -o $@ $< 21 | main.o:main.c 22 | @#对应《30天》中的bootpack.c 23 | gcc -nostdinc $(CFLAGS) -Os -c -o $@ $< 24 | screen.o:screen.c 25 | gcc -nostdinc $(CFLAGS) -c -o $@ $< 26 | 27 | run: 28 | qemu-system-i386 -drive file=kernel,if=floppy 29 | 30 | clean: 31 | @rm -f entry.o main.o screen.o kernel.out kernel.asm kernel *.d 32 | -------------------------------------------------------------------------------- /day4.7/kernel/screen.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void clear_screen(char color) //15:pure white 4 | { 5 | int i; 6 | for(i=0xa0000;i<0xaffff;i++) 7 | { 8 | write_mem8(i,color); //if we write 15 ,all pixels color will be white,15 mens pure white ,so the screen changes into white 9 | 10 | } 11 | } 12 | 13 | void color_screen(char color) //15:pure white 14 | { 15 | int i; 16 | color=color; 17 | for(i=0xa0000;i<0xaffff;i++) 18 | { 19 | write_mem8(i,i&0x0f); //if we write 15 ,all pixels color will be white,15 mens pure white ,so the screen changes into white 20 | 21 | } 22 | } 23 | 24 | void set_palette(int start, int end, unsigned char* rgb){ 25 | int i, eflags; 26 | eflags = read_eflags(); //替代作者的io_load_eflags() 27 | io_cli(); 28 | outb(0x03c8, start); //替代作者的io_out8() 29 | for(i=start; i<=end; i++){ 30 | outb(0x03c9,rgb[0]/4); 31 | outb(0x03c9,rgb[1]/4); 32 | outb(0x03c9,rgb[2]/4); 33 | rgb+=3; 34 | } 35 | write_eflags(eflags); //替代作者的io_store_eflags(eflags) 36 | return; 37 | } 38 | 39 | 40 | void init_palette(void){ 41 | //16种color,每个color三个字节。 42 | static unsigned char table_rgb[16*3]= 43 | { 44 | 0x00,0x00,0x00, /*0:black*/ 45 | 0xff,0x00,0x00, /*1:light red*/ 46 | 0x00,0xff,0x00, /*2:light green*/ 47 | 0xff,0xff,0x00, /*3:light yellow*/ 48 | 49 | 0x00,0x00,0xff, /*4:light blue*/ 50 | 0xff,0x00,0xff, /*5:light purper*/ 51 | 0x00,0xff,0xff, /*6:light blue*/ 52 | 0xff,0xff,0xff, /*7:white*/ 53 | 54 | 0xc6,0xc6,0xc6, /*8:light gray*/ 55 | 0x84,0x00,0x00, /*9:dark red*/ 56 | 0x00,0x84,0x00, /*10:dark green*/ 57 | 0x84,0x84,0x00, /*11:dark yellow*/ 58 | 59 | 0x00,0x00,0x84, /*12:dark 青*/ 60 | 0x84,0x00,0x84, /*13:dark purper*/ 61 | 0x00,0x84,0x84, /*14:light blue*/ 62 | 0x84,0x84,0x84, /*15:dark gray*/ 63 | }; 64 | set_palette(0,15,table_rgb); 65 | return; 66 | } 67 | 68 | void boxfill8(char* vram, int xsize, unsigned char c, int x0, int y0, int x1, int y1){ 69 | int x, y; 70 | for(y=y0; y<=y1; y++){ 71 | for(x=x0; x<=x1; x++){ 72 | vram[y*xsize+x]=c; 73 | } 74 | } 75 | return; 76 | } 77 | -------------------------------------------------------------------------------- /day4.7/makefile: -------------------------------------------------------------------------------- 1 | subdir=./kernel 2 | all: 3 | # nasm -o bootloader hello.nas 4 | nasm -o bootloader hello.nas 5 | @cd $(subdir);make clean;make addr=0xc400 6 | 7 | img: 8 | dd if=bootloader of=myos.img bs=512 count=1 9 | dd if=/dev/zero of=myos.img bs=512 seek=1 skip=1 count=2879 10 | 11 | copy:img 12 | mkdir -p /tmp/floppy 13 | mount -o loop myos.img /tmp/floppy -o fat=12 14 | sleep 1 15 | cp $(subdir)/kernel /tmp/floppy 16 | sleep 1 17 | umount /tmp/floppy 18 | run: 19 | qemu-system-i386 -drive file=myos.img,if=floppy 20 | clean: 21 | @rm -f bootloader myos.img haribote.bin 22 | -------------------------------------------------------------------------------- /day4.7/readme: -------------------------------------------------------------------------------- 1 | day4.7 2 | 根据前面编写的代码,已经可以在指定坐标点设定颜色了。那么让x和y坐标制定一个范围,这样就可以绘制图形了。 3 | -------------------------------------------------------------------------------- /day4.8/kernel/header.h: -------------------------------------------------------------------------------- 1 | #ifndef header 2 | #define header 3 | #include 4 | 5 | 6 | #define io_halt() asm("hlt") 7 | #define write_mem8(addr,data8) (*(volatile char *)(addr))=(char)data8 8 | 9 | #define io_cli() asm("cli") 10 | #define io_sti() asm("sti") 11 | 12 | #define COL8_000000 0 13 | #define COL8_FF0000 1 14 | #define COL8_00FF00 2 15 | #define COL8_FFFF00 3 16 | #define COL8_0000FF 4 17 | #define COL8_FF00FF 5 18 | #define COL8_00FFFF 6 19 | #define COL8_FFFFFF 7 20 | #define COL8_C6C6C6 8 21 | #define COL8_840000 9 22 | #define COL8_008400 10 23 | #define COL8_848400 11 24 | #define COL8_000084 12 25 | #define COL8_840084 13 26 | #define COL8_008484 14 27 | #define COL8_848484 15 28 | 29 | 30 | 31 | extern void clear_screen(char color) ; //color=15 pure white color=40 red 32 | extern void color_screen(char color) ; 33 | 34 | extern void init_palette(void); 35 | extern void set_palette(int start, int end, unsigned char *rgb); 36 | 37 | extern void boxfill8(char* vram, int xsize, unsigned char c, int x0, int y0, int x1, int y1); 38 | 39 | #endif 40 | -------------------------------------------------------------------------------- /day4.8/kernel/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ================================================== 3 | * 4 | * Filename: bootpack.c 5 | * 6 | * Description: 7 | * 8 | * Version: 0.01 9 | * Created: 2014年03月01日 星期六 22时03分34秒 10 | * Author: ChrisZZ, zchrissirhcz@163.com 11 | * Company: ZJUT 12 | * 13 | * ================================================== 14 | */ 15 | #include 16 | void bootmain(void){ 17 | //注意这里的函数名字为bootmain,因为在entry.S中设定的入口名字也是bootmain,两者要保持一致 18 | int i, xsize, ysize; 19 | char *vram; 20 | init_palette(); 21 | 22 | clear_screen(15); //由于一进来这里显示的是"my kernel is running"等字符串,这里刷屏为灰色,去除字符串 23 | 24 | vram=(char*)0xa0000; 25 | xsize=320; 26 | ysize=200; 27 | 28 | boxfill8(vram, xsize, COL8_008484, 0, 0, xsize-1, ysize-29); 29 | boxfill8(vram, xsize, COL8_C6C6C6, 0, ysize-28, xsize-1, ysize-28); 30 | boxfill8(vram, xsize, COL8_FFFFFF, 0, ysize-27, xsize-1, ysize-27); 31 | boxfill8(vram, xsize, COL8_C6C6C6, 0, ysize-26, xsize-1, ysize-1); 32 | 33 | boxfill8(vram, xsize, COL8_FFFFFF, 3, ysize-24, 59, ysize-24); 34 | boxfill8(vram, xsize, COL8_FFFFFF, 2, ysize-24, 2, ysize-4); 35 | boxfill8(vram, xsize, COL8_848484, 3, ysize-4, 59, ysize-4); 36 | boxfill8(vram, xsize, COL8_848484, 59, ysize-23, 59, ysize-5); 37 | boxfill8(vram, xsize, COL8_000000, 2, ysize-3, 59, ysize-3); 38 | boxfill8(vram, xsize, COL8_000000, 60, ysize-24, 60, ysize-3); 39 | 40 | boxfill8(vram, xsize, COL8_848484, xsize-47, ysize-24, xsize-4, ysize-24); 41 | boxfill8(vram, xsize, COL8_848484, xsize-47, ysize-23, xsize-47, ysize-4); 42 | boxfill8(vram, xsize, COL8_FFFFFF, xsize-47, ysize-3, xsize-4, ysize-3); 43 | boxfill8(vram, xsize, COL8_FFFFFF, xsize-3, ysize-24, xsize-3, ysize-3); 44 | 45 | for(;;){ 46 | io_halt(); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /day4.8/kernel/makefile: -------------------------------------------------------------------------------- 1 | addr=0x7c00 2 | OBJDIR=. 3 | 4 | CFLAGS := $(CFLAGS) -O1 -fno-builtin -I$(OBJDIR) -MD 5 | CFLAGS += -fno-omit-frame-pointer 6 | CFLAGS += -Wall -Wno-format -Wno-unused -Werror -gstabs -m32 7 | 8 | 9 | LDFLAGS=-m elf_i386 10 | 11 | all:kernel 12 | 13 | kernel:entry.o main.o screen.o 14 | ld $(LDFLAGS) -N -e start -Ttext $(addr) -o $@.out $^ 15 | objdump -S $@.out >$@.asm 16 | # objcopy -S -O binary -j .text $@.out $@ 17 | objcopy -S -O binary $@.out $@ 18 | 19 | entry.o:entry.S 20 | gcc -nostdinc $(CFLAGS) -c -o $@ $< 21 | main.o:main.c 22 | @#对应《30天》中的bootpack.c 23 | gcc -nostdinc $(CFLAGS) -Os -c -o $@ $< 24 | screen.o:screen.c 25 | gcc -nostdinc $(CFLAGS) -c -o $@ $< 26 | 27 | run: 28 | qemu-system-i386 -drive file=kernel,if=floppy 29 | 30 | clean: 31 | @rm -f entry.o main.o screen.o kernel.out kernel.asm kernel *.d 32 | -------------------------------------------------------------------------------- /day4.8/kernel/screen.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | void clear_screen(char color) //15:pure white 5 | { 6 | int i; 7 | for(i=0xa0000;i<0xaffff;i++) 8 | { 9 | write_mem8(i,color); //if we write 15 ,all pixels color will be white,15 mens pure white ,so the screen changes into white 10 | 11 | } 12 | } 13 | 14 | void color_screen(char color) //15:pure white 15 | { 16 | int i; 17 | color=color; 18 | for(i=0xa0000;i<0xaffff;i++) 19 | { 20 | write_mem8(i,i&0x0f); //if we write 15 ,all pixels color will be white,15 mens pure white ,so the screen changes into white 21 | 22 | } 23 | } 24 | 25 | void set_palette(int start, int end, unsigned char* rgb){ 26 | int i, eflags; 27 | eflags = read_eflags(); //替代作者的io_load_eflags() 28 | io_cli(); 29 | outb(0x03c8, start); //替代作者的io_out8() 30 | for(i=start; i<=end; i++){ 31 | outb(0x03c9,rgb[0]/4); 32 | outb(0x03c9,rgb[1]/4); 33 | outb(0x03c9,rgb[2]/4); 34 | rgb+=3; 35 | } 36 | write_eflags(eflags); //替代作者的io_store_eflags(eflags) 37 | return; 38 | } 39 | 40 | 41 | void init_palette(void){ 42 | //16种color,每个color三个字节。 43 | static unsigned char table_rgb[16*3]= 44 | { 45 | 0x00,0x00,0x00, /*0:black*/ 46 | 0xff,0x00,0x00, /*1:light red*/ 47 | 0x00,0xff,0x00, /*2:light green*/ 48 | 0xff,0xff,0x00, /*3:light yellow*/ 49 | 50 | 0x00,0x00,0xff, /*4:light blue*/ 51 | 0xff,0x00,0xff, /*5:light purper*/ 52 | 0x00,0xff,0xff, /*6:light blue*/ 53 | 0xff,0xff,0xff, /*7:white*/ 54 | 55 | 0xc6,0xc6,0xc6, /*8:light gray*/ 56 | 0x84,0x00,0x00, /*9:dark red*/ 57 | 0x00,0x84,0x00, /*10:dark green*/ 58 | 0x84,0x84,0x00, /*11:dark yellow*/ 59 | 60 | 0x00,0x00,0x84, /*12:dark 青*/ 61 | 0x84,0x00,0x84, /*13:dark purper*/ 62 | 0x00,0x84,0x84, /*14:light blue*/ 63 | 0x84,0x84,0x84, /*15:dark gray*/ 64 | }; 65 | set_palette(0,15,table_rgb); 66 | return; 67 | } 68 | 69 | void boxfill8(char* vram, int xsize, unsigned char c, int x0, int y0, int x1, int y1){ 70 | int x, y; 71 | for(y=y0; y<=y1; y++){ 72 | for(x=x0; x<=x1; x++){ 73 | vram[y*xsize+x]=c; 74 | } 75 | } 76 | return; 77 | } 78 | -------------------------------------------------------------------------------- /day4.8/makefile: -------------------------------------------------------------------------------- 1 | subdir=./kernel 2 | all: 3 | # nasm -o bootloader hello.nas 4 | nasm -o bootloader hello.nas 5 | @cd $(subdir);make clean;make addr=0xc400 6 | 7 | img: 8 | dd if=bootloader of=myos.img bs=512 count=1 9 | dd if=/dev/zero of=myos.img bs=512 seek=1 skip=1 count=2879 10 | 11 | copy:img 12 | mkdir -p /tmp/floppy 13 | mount -o loop myos.img /tmp/floppy -o fat=12 14 | sleep 1 15 | cp $(subdir)/kernel /tmp/floppy 16 | sleep 1 17 | umount /tmp/floppy 18 | run: 19 | qemu-system-i386 -drive file=myos.img,if=floppy 20 | clean: 21 | @rm -f bootloader myos.img haribote.bin 22 | -------------------------------------------------------------------------------- /day4.8/readme: -------------------------------------------------------------------------------- 1 | day4.10 2 | day4.9关于颜色的定义,手打吃力。。不过还是都在header.h中进行了define,然后把main.c修改的和书P86基本一致就OK了! 3 | -------------------------------------------------------------------------------- /day5.4/kernel/header.h: -------------------------------------------------------------------------------- 1 | #ifndef header 2 | #define header 3 | #include 4 | 5 | 6 | #define io_halt() asm("hlt") 7 | #define write_mem8(addr,data8) (*(volatile char *)(addr))=(char)data8 8 | 9 | #define io_cli() asm("cli") 10 | #define io_sti() asm("sti") 11 | 12 | #define COL8_000000 0 13 | #define COL8_FF0000 1 14 | #define COL8_00FF00 2 15 | #define COL8_FFFF00 3 16 | #define COL8_0000FF 4 17 | #define COL8_FF00FF 5 18 | #define COL8_00FFFF 6 19 | #define COL8_FFFFFF 7 20 | #define COL8_C6C6C6 8 21 | #define COL8_840000 9 22 | #define COL8_008400 10 23 | #define COL8_848400 11 24 | #define COL8_000084 12 25 | #define COL8_840084 13 26 | #define COL8_008484 14 27 | #define COL8_848484 15 28 | 29 | #define VRAM (0xa0000-0x280000) 30 | /* 31 | struct BOOTINFO{ 32 | char cylinder, led, color_mode; 33 | char reserved; 34 | short xsize, ysize; 35 | char *vram; 36 | }; 37 | */ 38 | struct BOOTINFO{ 39 | char cyls, leds, vmode, reserve; 40 | short scrnx, scrny; 41 | char *vram; 42 | }; 43 | 44 | extern void clear_screen(char color) ; //color=15 pure white color=40 red 45 | extern void color_screen(char color) ; 46 | 47 | extern void init_palette(void); 48 | extern void set_palette(int start, int end, unsigned char *rgb); 49 | 50 | extern void boxfill8(char* vram, int xsize, unsigned char c, int x0, int y0, int x1, int y1); 51 | //extern void init_screen(struct BOOTINFO* binfo); 52 | extern void init_screen(char *vram, int x, int y); 53 | extern void putfont8(char* vram, int xsize, int x, int y, char c, char *font); 54 | extern void draw_window(); 55 | 56 | #endif 57 | -------------------------------------------------------------------------------- /day5.4/kernel/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ================================================== 3 | * 4 | * Filename: bootpack.c 5 | * 6 | * Description: 7 | * 8 | * Version: 0.01 9 | * Created: 2014年03月01日 星期六 22时03分34秒 10 | * Author: ChrisZZ, zchrissirhcz@163.com 11 | * Company: ZJUT 12 | * 13 | * ================================================== 14 | */ 15 | #include 16 | /* 17 | void bootmain(void){ 18 | //注意这里的函数名字为bootmain,因为在entry.S中设定的入口名字也是bootmain,两者要保持一致 19 | clear_screen(40); 20 | 21 | char *vram; 22 | int xsize, ysize; 23 | struct BOOTINFO *binfo; 24 | binfo=(struct BOOTINFO *)0x0ff0; 25 | init_screen((struct BOOTINFO*)0x0ff0); 26 | 27 | draw_window(); 28 | 29 | init_palette(); 30 | 31 | vram=(char *)0x0ff0; 32 | 33 | char font_A[16]={ 34 | 0x00, 0x18, 0x18, 0x18, 0x18, 0x24, 0x24, 0x24, 35 | 0x24, 0x7e, 0x42, 0x42, 0x42, 0xe7, 0x00, 0x00 36 | }; 37 | 38 | //putfont8(vram, 20, 50, 50, 'd', font_A); 39 | putfont8(binfo->vram binfo->xsize, 8, 8, COL8_FFFFFF, font_A); 40 | } 41 | */ 42 | void bootmain(void){ 43 | struct BOOTINFO *binfo = (struct BOOTINFO*) 0x0ff0; 44 | static char font_A[16]={ 45 | 0x00, 0x18, 0x18, 0x18, 0x18, 0x24, 0x24, 0x24, 46 | 0x24, 0x7e, 0x42, 0x42, 0x42, 0xe7, 0x00, 0x00 47 | }; 48 | 49 | init_palette(); 50 | init_screen(binfo->vram, binfo->scrnx, binfo->scrny); 51 | putfont8(binfo->vram, binfo->scrnx, 10, 10, COL8_FFFFFF, font_A); 52 | 53 | while(1); 54 | } 55 | -------------------------------------------------------------------------------- /day5.4/kernel/makefile: -------------------------------------------------------------------------------- 1 | addr=0x7c00 2 | OBJDIR=. 3 | 4 | CFLAGS := $(CFLAGS) -O1 -fno-builtin -I$(OBJDIR) -MD 5 | CFLAGS += -fno-omit-frame-pointer 6 | CFLAGS += -Wall -Wno-format -Wno-unused -Werror -gstabs -m32 7 | 8 | 9 | LDFLAGS=-m elf_i386 10 | 11 | all:kernel 12 | 13 | kernel:entry.o main.o screen.o 14 | ld $(LDFLAGS) -N -e start -Ttext $(addr) -o $@.out $^ 15 | objdump -S $@.out >$@.asm 16 | # objcopy -S -O binary -j .text $@.out $@ 17 | objcopy -S -O binary $@.out $@ 18 | 19 | entry.o:entry.S 20 | gcc -nostdinc $(CFLAGS) -c -o $@ $< 21 | main.o:main.c 22 | @#对应《30天》中的bootpack.c 23 | gcc -nostdinc $(CFLAGS) -Os -c -o $@ $< 24 | screen.o:screen.c 25 | gcc -nostdinc $(CFLAGS) -c -o $@ $< 26 | 27 | run: 28 | qemu-system-i386 -drive file=kernel,if=floppy 29 | 30 | clean: 31 | @rm -f entry.o main.o screen.o kernel.out kernel.asm kernel *.d 32 | -------------------------------------------------------------------------------- /day5.4/makefile: -------------------------------------------------------------------------------- 1 | subdir=./kernel 2 | all: 3 | # nasm -o bootloader hello.nas 4 | nasm -o bootloader hello.nas 5 | @cd $(subdir);make clean;make addr=0xc400 6 | 7 | img: 8 | dd if=bootloader of=myos.img bs=512 count=1 9 | dd if=/dev/zero of=myos.img bs=512 seek=1 skip=1 count=2879 10 | 11 | copy:img 12 | mkdir -p /tmp/floppy 13 | mount -o loop myos.img /tmp/floppy -o fat=12 14 | sleep 1 15 | cp $(subdir)/kernel /tmp/floppy 16 | sleep 1 17 | umount /tmp/floppy 18 | run: 19 | qemu-system-i386 -drive file=myos.img,if=floppy 20 | clean: 21 | @rm -f bootloader myos.img haribote.bin 22 | -------------------------------------------------------------------------------- /day5.4/readme: -------------------------------------------------------------------------------- 1 | day5.1~5.4 2 | 进入C语言就比较容易了,因为学过C++。 3 | 显示字符和显示颜色的道理一样,因为这个字符是“画”出来的 4 | -------------------------------------------------------------------------------- /day5.6/kernel/font.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ================================================== 3 | * 4 | * Filename: font.c 5 | * 6 | * Description: 7 | * 8 | * Version: 0.01 9 | * Created: 2014年03月02日 星期日 12时28分42秒 10 | * Author: ChrisZZ, zchrissirhcz@163.com 11 | * Company: ZJUT 12 | * 13 | * ================================================== 14 | */ 15 | #include 16 | -------------------------------------------------------------------------------- /day5.6/kernel/font.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ================================================== 3 | * 4 | * Filename: font.h 5 | * 6 | * Description: 7 | * 8 | * Version: 0.01 9 | * Created: 2014年03月02日 星期日 12时27分31秒 10 | * Author: ChrisZZ, zchrissirhcz@163.com 11 | * Company: ZJUT 12 | * 13 | * ================================================== 14 | */ 15 | #ifndef font_header 16 | #define font_header 17 | 18 | extern unsigned short ASCII_Table[]; 19 | extern char hankaku[2048]; 20 | 21 | #endif 22 | -------------------------------------------------------------------------------- /day5.6/kernel/header.h: -------------------------------------------------------------------------------- 1 | #ifndef header 2 | #define header 3 | #include 4 | 5 | 6 | #define io_halt() asm("hlt") 7 | #define write_mem8(addr,data8) (*(volatile char *)(addr))=(char)data8 8 | 9 | #define io_cli() asm("cli") 10 | #define io_sti() asm("sti") 11 | 12 | #define COL8_000000 0 13 | #define COL8_FF0000 1 14 | #define COL8_00FF00 2 15 | #define COL8_FFFF00 3 16 | #define COL8_0000FF 4 17 | #define COL8_FF00FF 5 18 | #define COL8_00FFFF 6 19 | #define COL8_FFFFFF 7 20 | #define COL8_C6C6C6 8 21 | #define COL8_840000 9 22 | #define COL8_008400 10 23 | #define COL8_848400 11 24 | #define COL8_000084 12 25 | #define COL8_840084 13 26 | #define COL8_008484 14 27 | #define COL8_848484 15 28 | 29 | #define VRAM (0xa0000-0x280000) 30 | /* 31 | struct BOOTINFO{ 32 | char cylinder, led, color_mode; 33 | char reserved; 34 | short xsize, ysize; 35 | char *vram; 36 | }; 37 | */ 38 | struct BOOTINFO{ 39 | char cyls, leds, vmode, reserve; 40 | short scrnx, scrny; 41 | char *vram; 42 | }; 43 | 44 | extern void clear_screen(char color) ; //color=15 pure white color=40 red 45 | extern void color_screen(char color) ; 46 | 47 | extern void init_palette(void); 48 | extern void set_palette(int start, int end, unsigned char *rgb); 49 | 50 | extern void boxfill8(char* vram, int xsize, unsigned char c, int x0, int y0, int x1, int y1); 51 | //extern void init_screen(struct BOOTINFO* binfo); 52 | extern void init_screen(char *vram, int x, int y); 53 | extern void putfont8(char* vram, int xsize, int x, int y, char c, char *font); 54 | //extern void draw_window(); 55 | extern void putfont8_asc(char* vram, int xsize, int x, int y, char c, char *s); 56 | 57 | 58 | #endif 59 | -------------------------------------------------------------------------------- /day5.6/kernel/makefile: -------------------------------------------------------------------------------- 1 | addr=0x7c00 2 | OBJDIR=. 3 | 4 | CFLAGS := $(CFLAGS) -O1 -fno-builtin -I$(OBJDIR) -MD 5 | CFLAGS += -fno-omit-frame-pointer 6 | CFLAGS += -Wall -Wno-format -Wno-unused -Werror -gstabs -m32 7 | 8 | 9 | LDFLAGS=-m elf_i386 10 | 11 | all:kernel 12 | 13 | kernel:entry.o main.o screen.o 14 | ld $(LDFLAGS) -N -e start -Ttext $(addr) -o $@.out $^ 15 | objdump -S $@.out >$@.asm 16 | # objcopy -S -O binary -j .text $@.out $@ 17 | objcopy -S -O binary $@.out $@ 18 | 19 | entry.o:entry.S 20 | gcc -nostdinc $(CFLAGS) -c -o $@ $< 21 | main.o:main.c 22 | @#对应《30天》中的bootpack.c 23 | gcc -nostdinc $(CFLAGS) -Os -c -o $@ $< 24 | screen.o:screen.c 25 | gcc -nostdinc $(CFLAGS) -c -o $@ $< 26 | 27 | run: 28 | qemu-system-i386 -drive file=kernel,if=floppy 29 | 30 | clean: 31 | @rm -f entry.o main.o screen.o kernel.out kernel.asm kernel *.d 32 | -------------------------------------------------------------------------------- /day5.6/makefile: -------------------------------------------------------------------------------- 1 | subdir=./kernel 2 | all: 3 | # nasm -o bootloader hello.nas 4 | nasm -o bootloader hello.nas 5 | @cd $(subdir);make clean;make addr=0xc400 6 | 7 | img: 8 | dd if=bootloader of=myos.img bs=512 count=1 9 | dd if=/dev/zero of=myos.img bs=512 seek=1 skip=1 count=2879 10 | 11 | copy:img 12 | mkdir -p /tmp/floppy 13 | mount -o loop myos.img /tmp/floppy -o fat=12 14 | sleep 1 15 | cp $(subdir)/kernel /tmp/floppy 16 | sleep 1 17 | umount /tmp/floppy 18 | run: 19 | qemu-system-i386 -drive file=myos.img,if=floppy 20 | clean: 21 | @rm -f bootloader myos.img haribote.bin 22 | -------------------------------------------------------------------------------- /day5.6/readme: -------------------------------------------------------------------------------- 1 | day 5.5-5.6 2 | 对于书上提到的字体文件,感觉没有必要。自定义个巨大的数组就可以了,因为这个大的数组可以从网上找到。 3 | 因此,定义三个文件:font.h font.c fontascii.h 4 | 5 | -------------------------------------------------------------------------------- /day5.7/kernel/.print.c.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zchrissirhcz/osask-linux/a7afa7623205aaee125fc33676480d72c21e5323/day5.7/kernel/.print.c.swp -------------------------------------------------------------------------------- /day5.7/kernel/font.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ================================================== 3 | * 4 | * Filename: font.c 5 | * 6 | * Description: 7 | * 8 | * Version: 0.01 9 | * Created: 2014年03月02日 星期日 12时28分42秒 10 | * Author: ChrisZZ, zchrissirhcz@163.com 11 | * Company: ZJUT 12 | * 13 | * ================================================== 14 | */ 15 | #include 16 | -------------------------------------------------------------------------------- /day5.7/kernel/font.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ================================================== 3 | * 4 | * Filename: font.h 5 | * 6 | * Description: 7 | * 8 | * Version: 0.01 9 | * Created: 2014年03月02日 星期日 12时27分31秒 10 | * Author: ChrisZZ, zchrissirhcz@163.com 11 | * Company: ZJUT 12 | * 13 | * ================================================== 14 | */ 15 | #ifndef font_header 16 | #define font_header 17 | 18 | extern unsigned short ASCII_Table[]; 19 | extern char hankaku[2048]; 20 | 21 | #endif 22 | -------------------------------------------------------------------------------- /day5.7/kernel/header.h: -------------------------------------------------------------------------------- 1 | #ifndef header 2 | #define header 3 | #include 4 | 5 | 6 | #define io_halt() asm("hlt") 7 | #define write_mem8(addr,data8) (*(volatile char *)(addr))=(char)data8 8 | 9 | #define io_cli() asm("cli") 10 | #define io_sti() asm("sti") 11 | 12 | #define COL8_000000 0 13 | #define COL8_FF0000 1 14 | #define COL8_00FF00 2 15 | #define COL8_FFFF00 3 16 | #define COL8_0000FF 4 17 | #define COL8_FF00FF 5 18 | #define COL8_00FFFF 6 19 | #define COL8_FFFFFF 7 20 | #define COL8_C6C6C6 8 21 | #define COL8_840000 9 22 | #define COL8_008400 10 23 | #define COL8_848400 11 24 | #define COL8_000084 12 25 | #define COL8_840084 13 26 | #define COL8_008484 14 27 | #define COL8_848484 15 28 | 29 | #define VRAM (0xa0000-0x280000) 30 | /* 31 | struct BOOTINFO{ 32 | char cylinder, led, color_mode; 33 | char reserved; 34 | short xsize, ysize; 35 | char *vram; 36 | }; 37 | */ 38 | struct BOOTINFO{ 39 | char cyls, leds, vmode, reserve; 40 | short scrnx, scrny; 41 | char *vram; 42 | }; 43 | 44 | extern void clear_screen(char color) ; //color=15 pure white color=40 red 45 | extern void color_screen(char color) ; 46 | 47 | extern void init_palette(void); 48 | extern void set_palette(int start, int end, unsigned char *rgb); 49 | 50 | extern void boxfill8(char* vram, int xsize, unsigned char c, int x0, int y0, int x1, int y1); 51 | //extern void init_screen(struct BOOTINFO* binfo); 52 | extern void init_screen(char *vram, int x, int y); 53 | extern void putfont8(char* vram, int xsize, int x, int y, char c, char *font); 54 | //extern void draw_window(); 55 | extern void putfont8_asc(char* vram, int xsize, int x, int y, char c, char *s); 56 | extern void itoa(int value,char *buf); 57 | extern void sprintf(char *str,char *format ,...); 58 | 59 | 60 | #endif 61 | -------------------------------------------------------------------------------- /day5.7/kernel/makefile: -------------------------------------------------------------------------------- 1 | addr=0x7c00 2 | OBJDIR=. 3 | 4 | CFLAGS := $(CFLAGS) -O1 -fno-builtin -I$(OBJDIR) -MD 5 | CFLAGS += -fno-omit-frame-pointer 6 | CFLAGS += -Wall -Wno-format -Wno-unused -Werror -gstabs -m32 7 | 8 | 9 | LDFLAGS=-m elf_i386 10 | 11 | all:kernel 12 | 13 | kernel:entry.o main.o screen.o print.o 14 | ld $(LDFLAGS) -N -e start -Ttext $(addr) -o $@.out $^ 15 | objdump -S $@.out >$@.asm 16 | # objcopy -S -O binary -j .text $@.out $@ 17 | objcopy -S -O binary $@.out $@ 18 | 19 | entry.o:entry.S 20 | gcc -nostdinc $(CFLAGS) -c -o $@ $< 21 | main.o:main.c 22 | @#对应《30天》中的bootpack.c 23 | gcc -nostdinc $(CFLAGS) -Os -c -o $@ $< 24 | screen.o:screen.c 25 | gcc -nostdinc $(CFLAGS) -c -o $@ $< 26 | print.o:print.c 27 | gcc -nostdinc $(CFLAGS) -c -o $@ $< 28 | 29 | run: 30 | qemu-system-i386 -drive file=kernel,if=floppy 31 | 32 | clean: 33 | @rm -f entry.o main.o screen.o kernel.out kernel.asm kernel *.d 34 | -------------------------------------------------------------------------------- /day5.7/kernel/print.c: -------------------------------------------------------------------------------- 1 | #include 2 | void itoa(int value,char *buf){ 3 | char tmp_buf[10] = {0}; 4 | char *tbp = tmp_buf; 5 | if((value >> 31) & 0x1){ // neg num 6 | *buf++ = '-'; 7 | value = ~value + 1; 8 | } 9 | 10 | do{ 11 | *tbp++ = ('0' + (char)(value % 10)); 12 | value /= 10; 13 | }while(value); 14 | while(tmp_buf != tbp--) 15 | *buf++ = *tbp; 16 | *buf='\0'; 17 | } 18 | 19 | //实现可变参数的打印,主要是为了观察打印的变量。 20 | void sprintf(char *str,char *format ,...){ 21 | 22 | int *var=(int *)(&format)+1; //得到第一个可变参数的地址 23 | char buffer[10]; 24 | char *buf=buffer; 25 | while(*format){ 26 | if(*format!='%'){ 27 | *str++=*format++; 28 | continue; 29 | } 30 | else{ 31 | format++; 32 | switch (*format){ 33 | case 'd':itoa(*var,buf);while(*buf){*str++=*buf++;};var++;break; 34 | case 'x':break; 35 | case 's':break; 36 | } 37 | format++; 38 | } 39 | } 40 | *str='\0'; 41 | } 42 | -------------------------------------------------------------------------------- /day5.7/makefile: -------------------------------------------------------------------------------- 1 | subdir=./kernel 2 | all: 3 | # nasm -o bootloader hello.nas 4 | nasm -o bootloader hello.nas 5 | @cd $(subdir);make clean;make addr=0xc400 6 | 7 | img: 8 | dd if=bootloader of=myos.img bs=512 count=1 9 | dd if=/dev/zero of=myos.img bs=512 seek=1 skip=1 count=2879 10 | 11 | copy:img 12 | mkdir -p /tmp/floppy 13 | mount -o loop myos.img /tmp/floppy -o fat=12 14 | sleep 1 15 | cp $(subdir)/kernel /tmp/floppy 16 | sleep 1 17 | umount /tmp/floppy 18 | run: 19 | qemu-system-i386 -drive file=myos.img,if=floppy 20 | clean: 21 | @rm -f bootloader myos.img haribote.bin 22 | -------------------------------------------------------------------------------- /day5.7/readme: -------------------------------------------------------------------------------- 1 | day5.7 2 | 书本上说使用sprintf调式,不过因为makefile不是很会写的缘故(实际上是汇编、ld什么的不懂,所以不会),因此不能使用stdio.h中的sprintf 3 | 不过可以通过自定义sprintf来实现打印变量的值,用来调试 4 | 为了方便,sprintf的代码就从网上代码中copy了. 5 | -------------------------------------------------------------------------------- /day5.8/kernel/.print.c.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zchrissirhcz/osask-linux/a7afa7623205aaee125fc33676480d72c21e5323/day5.8/kernel/.print.c.swp -------------------------------------------------------------------------------- /day5.8/kernel/font.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ================================================== 3 | * 4 | * Filename: font.c 5 | * 6 | * Description: 7 | * 8 | * Version: 0.01 9 | * Created: 2014年03月02日 星期日 12时28分42秒 10 | * Author: ChrisZZ, zchrissirhcz@163.com 11 | * Company: ZJUT 12 | * 13 | * ================================================== 14 | */ 15 | #include 16 | -------------------------------------------------------------------------------- /day5.8/kernel/font.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ================================================== 3 | * 4 | * Filename: font.h 5 | * 6 | * Description: 7 | * 8 | * Version: 0.01 9 | * Created: 2014年03月02日 星期日 12时27分31秒 10 | * Author: ChrisZZ, zchrissirhcz@163.com 11 | * Company: ZJUT 12 | * 13 | * ================================================== 14 | */ 15 | #ifndef font_header 16 | #define font_header 17 | 18 | extern unsigned short ASCII_Table[]; 19 | extern char hankaku[2048]; 20 | 21 | #endif 22 | -------------------------------------------------------------------------------- /day5.8/kernel/header.h: -------------------------------------------------------------------------------- 1 | #ifndef header 2 | #define header 3 | #include 4 | 5 | 6 | #define io_halt() asm("hlt") 7 | #define write_mem8(addr,data8) (*(volatile char *)(addr))=(char)data8 8 | 9 | #define io_cli() asm("cli") 10 | #define io_sti() asm("sti") 11 | 12 | #define COL8_000000 0 13 | #define COL8_FF0000 1 14 | #define COL8_00FF00 2 15 | #define COL8_FFFF00 3 16 | #define COL8_0000FF 4 17 | #define COL8_FF00FF 5 18 | #define COL8_00FFFF 6 19 | #define COL8_FFFFFF 7 20 | #define COL8_C6C6C6 8 21 | #define COL8_840000 9 22 | #define COL8_008400 10 23 | #define COL8_848400 11 24 | #define COL8_000084 12 25 | #define COL8_840084 13 26 | #define COL8_008484 14 27 | #define COL8_848484 15 28 | 29 | #define VRAM (0xa0000-0x280000) 30 | /* 31 | struct BOOTINFO{ 32 | char cylinder, led, color_mode; 33 | char reserved; 34 | short xsize, ysize; 35 | char *vram; 36 | }; 37 | */ 38 | struct BOOTINFO{ 39 | char cyls, leds, vmode, reserve; 40 | short scrnx, scrny; 41 | char *vram; 42 | }; 43 | 44 | extern void clear_screen(char color) ; //color=15 pure white color=40 red 45 | extern void color_screen(char color) ; 46 | 47 | extern void init_palette(void); 48 | extern void set_palette(int start, int end, unsigned char *rgb); 49 | 50 | extern void boxfill8(char* vram, int xsize, unsigned char c, int x0, int y0, int x1, int y1); 51 | //extern void init_screen(struct BOOTINFO* binfo); 52 | extern void init_screen(char *vram, int x, int y); 53 | extern void putfont8(char* vram, int xsize, int x, int y, char c, char *font); 54 | //extern void draw_window(); 55 | extern void putfont8_asc(char* vram, int xsize, int x, int y, char c, char *s); 56 | extern void itoa(int value,char *buf); 57 | extern void sprintf(char *str,char *format ,...); 58 | extern void init_mouse_cursor8(char *mouse, char bg); 59 | extern void putblock8_8(char *vram,int xsize,int pxsize,int pysize,int px0,int py0,char *buf,int bxsize);//显示鼠标,display_mouse 60 | 61 | 62 | #endif 63 | -------------------------------------------------------------------------------- /day5.8/kernel/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ================================================== 3 | * 4 | * Filename: bootpack.c 5 | * 6 | * Description: 7 | * 8 | * Version: 0.01 9 | * Created: 2014年03月01日 星期六 22时03分34秒 10 | * Author: ChrisZZ, zchrissirhcz@163.com 11 | * Company: ZJUT 12 | * 13 | * ================================================== 14 | */ 15 | #include 16 | #include 17 | /* 18 | void bootmain(void){ 19 | //注意这里的函数名字为bootmain,因为在entry.S中设定的入口名字也是bootmain,两者要保持一致 20 | clear_screen(40); 21 | 22 | char *vram; 23 | int xsize, ysize; 24 | struct BOOTINFO *binfo; 25 | binfo=(struct BOOTINFO *)0x0ff0; 26 | init_screen((struct BOOTINFO*)0x0ff0); 27 | 28 | draw_window(); 29 | 30 | init_palette(); 31 | 32 | vram=(char *)0x0ff0; 33 | 34 | char font_A[16]={ 35 | 0x00, 0x18, 0x18, 0x18, 0x18, 0x24, 0x24, 0x24, 36 | 0x24, 0x7e, 0x42, 0x42, 0x42, 0xe7, 0x00, 0x00 37 | }; 38 | 39 | //putfont8(vram, 20, 50, 50, 'd', font_A); 40 | putfont8(binfo->vram binfo->xsize, 8, 8, COL8_FFFFFF, font_A); 41 | } 42 | */ 43 | void bootmain(void){ 44 | struct BOOTINFO *binfo = (struct BOOTINFO*) 0x0ff0; 45 | extern char hankaku[2048]; 46 | 47 | init_palette(); 48 | init_screen(binfo->vram, binfo->scrnx, binfo->scrny); 49 | 50 | //putfont8_asc(binfo->vram, binfo->scrnx, 8, 8, COL8_FFFFFF, "ChrisZZ"); 51 | //putfont8_asc(binfo->vram, binfo->scrnx, 31, 31, COL8_000000, "Haribote OS"); 52 | //putfont8_asc(binfo->vram, binfo->scrnx, 32, 32, COL8_FFFFFF, "Haribote OS"); 53 | 54 | char s[10]; 55 | //sprintf(s, "scrnx=%d", binfo->scrnx); 56 | //putfont8_asc(binfo->vram, binfo->scrnx, 16, 64, COL8_FFFFFF, s); 57 | 58 | char mcursor[256]; 59 | int mx, my; 60 | mx = (binfo->scrnx-16)/2; 61 | my = (binfo->scrny-28-16)/2; 62 | init_mouse_cursor8(mcursor, COL8_008484); 63 | putblock8_8(binfo->vram, binfo->scrnx, 16, 16, mx, my, mcursor, 16); 64 | 65 | sprintf(s, "(%d, %d)", mx, my); 66 | putfont8_asc(binfo->vram, binfo->scrnx, 0, 0, COL8_FFFFFF, s); 67 | 68 | while(1); 69 | } 70 | -------------------------------------------------------------------------------- /day5.8/kernel/makefile: -------------------------------------------------------------------------------- 1 | addr=0x7c00 2 | OBJDIR=. 3 | 4 | CFLAGS := $(CFLAGS) -O1 -fno-builtin -I$(OBJDIR) -MD 5 | CFLAGS += -fno-omit-frame-pointer 6 | CFLAGS += -Wall -Wno-format -Wno-unused -Werror -gstabs -m32 7 | 8 | 9 | LDFLAGS=-m elf_i386 10 | 11 | all:kernel 12 | 13 | kernel:entry.o main.o screen.o print.o 14 | ld $(LDFLAGS) -N -e start -Ttext $(addr) -o $@.out $^ 15 | objdump -S $@.out >$@.asm 16 | # objcopy -S -O binary -j .text $@.out $@ 17 | objcopy -S -O binary $@.out $@ 18 | 19 | entry.o:entry.S 20 | gcc -nostdinc $(CFLAGS) -c -o $@ $< 21 | main.o:main.c 22 | @#对应《30天》中的bootpack.c 23 | gcc -nostdinc $(CFLAGS) -Os -c -o $@ $< 24 | screen.o:screen.c 25 | gcc -nostdinc $(CFLAGS) -c -o $@ $< 26 | print.o:print.c 27 | gcc -nostdinc $(CFLAGS) -c -o $@ $< 28 | 29 | run: 30 | qemu-system-i386 -drive file=kernel,if=floppy 31 | 32 | clean: 33 | @rm -f entry.o main.o screen.o kernel.out kernel.asm kernel *.d 34 | -------------------------------------------------------------------------------- /day5.8/kernel/print.c: -------------------------------------------------------------------------------- 1 | #include 2 | void itoa(int value,char *buf){ 3 | char tmp_buf[10] = {0}; 4 | char *tbp = tmp_buf; 5 | if((value >> 31) & 0x1){ // neg num 6 | *buf++ = '-'; 7 | value = ~value + 1; 8 | } 9 | 10 | do{ 11 | *tbp++ = ('0' + (char)(value % 10)); 12 | value /= 10; 13 | }while(value); 14 | while(tmp_buf != tbp--) 15 | *buf++ = *tbp; 16 | *buf='\0'; 17 | } 18 | 19 | //实现可变参数的打印,主要是为了观察打印的变量。 20 | void sprintf(char *str,char *format ,...){ 21 | 22 | int *var=(int *)(&format)+1; //得到第一个可变参数的地址 23 | char buffer[10]; 24 | char *buf=buffer; 25 | while(*format){ 26 | if(*format!='%'){ 27 | *str++=*format++; 28 | continue; 29 | } 30 | else{ 31 | format++; 32 | switch (*format){ 33 | case 'd':itoa(*var,buf);while(*buf){*str++=*buf++;};var++;break; 34 | case 'x':break; 35 | case 's':break; 36 | } 37 | format++; 38 | } 39 | } 40 | *str='\0'; 41 | } 42 | -------------------------------------------------------------------------------- /day5.8/kernel/print.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zchrissirhcz/osask-linux/a7afa7623205aaee125fc33676480d72c21e5323/day5.8/kernel/print.o -------------------------------------------------------------------------------- /day5.8/makefile: -------------------------------------------------------------------------------- 1 | subdir=./kernel 2 | all: 3 | # nasm -o bootloader hello.nas 4 | nasm -o bootloader hello.nas 5 | @cd $(subdir);make clean;make addr=0xc400 6 | 7 | img: 8 | dd if=bootloader of=myos.img bs=512 count=1 9 | dd if=/dev/zero of=myos.img bs=512 seek=1 skip=1 count=2879 10 | 11 | copy:img 12 | mkdir -p /tmp/floppy 13 | mount -o loop myos.img /tmp/floppy -o fat=12 14 | sleep 1 15 | cp $(subdir)/kernel /tmp/floppy 16 | sleep 1 17 | umount /tmp/floppy 18 | run: 19 | qemu-system-i386 -drive file=myos.img,if=floppy 20 | clean: 21 | @rm -f bootloader myos.img haribote.bin 22 | -------------------------------------------------------------------------------- /day5.8/readme: -------------------------------------------------------------------------------- 1 | 根据书上P100,定义两个函数putblock8_8和init_mouse_curosr8,并在main.c中调用。 2 | 注意在header.h中用extern声明 3 | -------------------------------------------------------------------------------- /day5.9/kernel/.print.c.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zchrissirhcz/osask-linux/a7afa7623205aaee125fc33676480d72c21e5323/day5.9/kernel/.print.c.swp -------------------------------------------------------------------------------- /day5.9/kernel/entry.ld: -------------------------------------------------------------------------------- 1 | /* Simple linker script for the JOS kernel. 2 | See the GNU ld 'info' manual ("info ld") to learn the syntax. */ 3 | 4 | OUTPUT_FORMAT("elf32-i386", "elf32-i386", "elf32-i386") 5 | OUTPUT_ARCH(i386) 6 | ENTRY(_start) 7 | 8 | SECTIONS 9 | { 10 | /* Link the kernel at this address: "." means the current address */ 11 | . = 0xc400; 12 | 13 | /* AT(...) gives the load address of this section, which tells 14 | the boot loader where to load the kernel in physical memory */ 15 | .text : { 16 | *(.text .stub .text.* .gnu.linkonce.t.*) 17 | } 18 | .rodata : { 19 | *(.rodata .rodata.* .gnu.linkonce.r.*) 20 | } 21 | 22 | .data : { 23 | *(.data) 24 | } 25 | 26 | .bss : { 27 | *(.bss) 28 | } 29 | 30 | 31 | } 32 | -------------------------------------------------------------------------------- /day5.9/kernel/font.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ================================================== 3 | * 4 | * Filename: font.c 5 | * 6 | * Description: 7 | * 8 | * Version: 0.01 9 | * Created: 2014年03月02日 星期日 12时28分42秒 10 | * Author: ChrisZZ, zchrissirhcz@163.com 11 | * Company: ZJUT 12 | * 13 | * ================================================== 14 | */ 15 | #include 16 | -------------------------------------------------------------------------------- /day5.9/kernel/font.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ================================================== 3 | * 4 | * Filename: font.h 5 | * 6 | * Description: 7 | * 8 | * Version: 0.01 9 | * Created: 2014年03月02日 星期日 12时27分31秒 10 | * Author: ChrisZZ, zchrissirhcz@163.com 11 | * Company: ZJUT 12 | * 13 | * ================================================== 14 | */ 15 | #ifndef font_header 16 | #define font_header 17 | 18 | extern unsigned short ASCII_Table[]; 19 | extern char hankaku[2048]; 20 | 21 | #endif 22 | -------------------------------------------------------------------------------- /day5.9/kernel/idtgdt.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ================================================== 3 | * 4 | * Filename: idtgdt.h 5 | * 6 | * Description: 7 | * 8 | * Version: 0.01 9 | * Created: 2014年03月02日 星期日 14时37分36秒 10 | * Author: ChrisZZ, zchrissirhcz@163.com 11 | * Company: ZJUT 12 | * 13 | * ================================================== 14 | */ 15 | #ifndef idtgdt_header 16 | #define idtgdt_header 17 | 18 | 19 | #endif 20 | -------------------------------------------------------------------------------- /day5.9/kernel/kernel.ld: -------------------------------------------------------------------------------- 1 | /* Simple linker script for the JOS kernel. this file is for main.c 2 | See the GNU ld 'info' manual ("info ld") to learn the syntax. */ 3 | 4 | OUTPUT_FORMAT("elf32-i386", "elf32-i386", "elf32-i386") 5 | OUTPUT_ARCH(i386) 6 | ENTRY(_start) 7 | 8 | SECTIONS 9 | { 10 | /* Link the kernel at this address: "." means the current address */ 11 | . = 0x280000; 12 | .text : { 13 | *(.text) 14 | } 15 | .rodata : { 16 | *(.rodata) 17 | } 18 | 19 | .data : { 20 | *(.data) 21 | } 22 | 23 | .bss : { 24 | *(.bss) 25 | } 26 | 27 | 28 | } 29 | -------------------------------------------------------------------------------- /day5.9/kernel/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ================================================== 3 | * 4 | * Filename: bootpack.c 5 | * 6 | * Description: 7 | * 8 | * Version: 0.01 9 | * Created: 2014年03月01日 星期六 22时03分34秒 10 | * Author: ChrisZZ, zchrissirhcz@163.com 11 | * Company: ZJUT 12 | * 13 | * ================================================== 14 | */ 15 | #include 16 | #include 17 | /* 18 | void bootmain(void){ 19 | //注意这里的函数名字为bootmain,因为在entry.S中设定的入口名字也是bootmain,两者要保持一致 20 | clear_screen(40); 21 | 22 | char *vram; 23 | int xsize, ysize; 24 | struct BOOTINFO *binfo; 25 | binfo=(struct BOOTINFO *)0x0ff0; 26 | init_screen((struct BOOTINFO*)0x0ff0); 27 | 28 | draw_window(); 29 | 30 | init_palette(); 31 | 32 | vram=(char *)0x0ff0; 33 | 34 | char font_A[16]={ 35 | 0x00, 0x18, 0x18, 0x18, 0x18, 0x24, 0x24, 0x24, 36 | 0x24, 0x7e, 0x42, 0x42, 0x42, 0xe7, 0x00, 0x00 37 | }; 38 | 39 | //putfont8(vram, 20, 50, 50, 'd', font_A); 40 | putfont8(binfo->vram binfo->xsize, 8, 8, COL8_FFFFFF, font_A); 41 | } 42 | */ 43 | void bootmain(void){ 44 | struct BOOTINFO *binfo = (struct BOOTINFO*) 0x0ff0; 45 | extern char hankaku[2048]; 46 | 47 | init_palette(); 48 | init_screen(binfo->vram, binfo->scrnx, binfo->scrny); 49 | 50 | //putfont8_asc(binfo->vram, binfo->scrnx, 8, 8, COL8_FFFFFF, "ChrisZZ"); 51 | //putfont8_asc(binfo->vram, binfo->scrnx, 31, 31, COL8_000000, "Haribote OS"); 52 | //putfont8_asc(binfo->vram, binfo->scrnx, 32, 32, COL8_FFFFFF, "Haribote OS"); 53 | 54 | char s[10]; 55 | //sprintf(s, "scrnx=%d", binfo->scrnx); 56 | //putfont8_asc(binfo->vram, binfo->scrnx, 16, 64, COL8_FFFFFF, s); 57 | 58 | char mcursor[256]; 59 | int mx, my; 60 | mx = (binfo->scrnx-16)/2; 61 | my = (binfo->scrny-28-16)/2; 62 | init_mouse_cursor8(mcursor, COL8_008484); 63 | putblock8_8(binfo->vram, binfo->scrnx, 16, 16, mx, my, mcursor, 16); 64 | 65 | sprintf(s, "(%d, %d)", mx, my); 66 | putfonts8_asc(binfo->vram, binfo->scrnx, 0, 0, COL8_FFFFFF, s); 67 | 68 | while(1); 69 | } 70 | -------------------------------------------------------------------------------- /day5.9/kernel/makefile: -------------------------------------------------------------------------------- 1 | addr=0x7c00 2 | OBJDIR=. 3 | entry_objs=entry.o 4 | ccode_objs=main.o screen.o font.o print.o idtgdt.o 5 | 6 | CFLAGS := $(CFLAGS) -O1 -fno-builtin -I$(OBJDIR) -MD 7 | CFLAGS += -fno-omit-frame-pointer 8 | CFLAGS += -Wall -Wno-format -Wno-unused -Werror -gstabs -m32 9 | 10 | 11 | LDFLAGS=-m elf_i386 12 | 13 | all:kernel 14 | 15 | kernel:entry cobj 16 | cat cobj>>entry 17 | cp entry kernel 18 | 19 | %.o:%.S 20 | gcc -nostdinc $(CFLAGS) -c -o $@ $< 21 | %.o:%.c 22 | @#对应《30天》中的bootpack.c 23 | gcc -nostdinc $(CFLAGS) -Os -c -o $@ $< 24 | %.o:.asm 25 | nasm -f elf -o $@ $< 26 | 27 | entry:$(entry_objs) 28 | cobj:$(ccode_objs) 29 | 30 | entry: 31 | @echo "addr=$(addr)" 32 | ld $(LDFLAGS) -N -e entry -Ttext 0xc400 -o $@.out $^ 33 | objdump -S -D $@.out > $@.asm 34 | objcopy -S -O binary -j .text $@.out $@ 35 | 36 | cobj: 37 | ld $(LDFLAGS) -N -e bootmain -T kernel.ld -o $@.out $^ 38 | objdump -D $@.out >$@.asm 39 | objcopy -S -O binary $@.out $@ 40 | 41 | run: 42 | qemu-system-i386 -drive file=kernel,if=floppy 43 | 44 | clean: 45 | @rm -f *.d *.o *.out main.o kernel kernel.asm cobj* entry.o entry.asm entry 46 | -------------------------------------------------------------------------------- /day5.9/kernel/makefile.bak: -------------------------------------------------------------------------------- 1 | addr=0x7c00 2 | OBJDIR=. 3 | 4 | CFLAGS := $(CFLAGS) -O1 -fno-builtin -I$(OBJDIR) -MD 5 | CFLAGS += -fno-omit-frame-pointer 6 | CFLAGS += -Wall -Wno-format -Wno-unused -Werror -gstabs -m32 7 | 8 | 9 | LDFLAGS=-m elf_i386 10 | 11 | all:kernel 12 | 13 | kernel:entry.o main.o screen.o print.o idtgdt.o 14 | # ld $(LDFLAGS) -N -e start -Ttext $(addr) -o $@.out $^ 15 | ld $(LDFLAGS) -N -e start -Ttext 0xc400 -o $@.out $^ 16 | objdump -S $@.out >$@.asm 17 | # objcopy -S -O binary -j .text $@.out $@ 18 | objcopy -S -O binary $@.out $@ 19 | 20 | entry.o:entry.S 21 | gcc -nostdinc $(CFLAGS) -c -o $@ $< 22 | main.o:main.c 23 | @#对应《30天》中的bootpack.c 24 | gcc -nostdinc $(CFLAGS) -Os -c -o $@ $< 25 | screen.o:screen.c 26 | gcc -nostdinc $(CFLAGS) -c -o $@ $< 27 | print.o:print.c 28 | gcc -nostdinc $(CFLAGS) -c -o $@ $< 29 | idtgdt.o:idtgdt.c 30 | gcc -nostdinc $(CFLAGS) -c -o $@ $< 31 | 32 | run: 33 | qemu-system-i386 -drive file=kernel,if=floppy 34 | 35 | clean: 36 | @rm -f entry.o main.o screen.o kernel.out kernel.asm kernel *.d 37 | -------------------------------------------------------------------------------- /day5.9/kernel/print.c: -------------------------------------------------------------------------------- 1 | #include 2 | void itoa(int value,char *buf){ 3 | char tmp_buf[10] = {0}; 4 | char *tbp = tmp_buf; 5 | if((value >> 31) & 0x1){ // neg num 6 | *buf++ = '-'; 7 | value = ~value + 1; 8 | } 9 | 10 | do{ 11 | *tbp++ = ('0' + (char)(value % 10)); 12 | value /= 10; 13 | }while(value); 14 | while(tmp_buf != tbp--) 15 | *buf++ = *tbp; 16 | *buf='\0'; 17 | } 18 | 19 | //实现可变参数的打印,主要是为了观察打印的变量。 20 | void sprintf(char *str,char *format ,...){ 21 | 22 | int *var=(int *)(&format)+1; //得到第一个可变参数的地址 23 | char buffer[10]; 24 | char *buf=buffer; 25 | while(*format){ 26 | if(*format!='%'){ 27 | *str++=*format++; 28 | continue; 29 | } 30 | else{ 31 | format++; 32 | switch (*format){ 33 | case 'd':itoa(*var,buf);while(*buf){*str++=*buf++;};var++;break; 34 | case 'x':break; 35 | case 's':break; 36 | } 37 | format++; 38 | } 39 | } 40 | *str='\0'; 41 | } 42 | -------------------------------------------------------------------------------- /day5.9/makefile: -------------------------------------------------------------------------------- 1 | subdir=./kernel 2 | all: 3 | # nasm -o bootloader hello.nas 4 | nasm -o bootloader hello.nas 5 | @cd $(subdir);make clean;make addr=0xc400 6 | 7 | img: 8 | dd if=bootloader of=myos.img bs=512 count=1 9 | dd if=/dev/zero of=myos.img bs=512 seek=1 skip=1 count=2879 10 | 11 | copy:img 12 | mkdir -p /tmp/floppy 13 | mount -o loop myos.img /tmp/floppy -o fat=12 14 | sleep 1 15 | cp $(subdir)/kernel /tmp/floppy 16 | sleep 1 17 | umount /tmp/floppy 18 | run: 19 | qemu-system-i386 -drive file=myos.img,if=floppy 20 | clean: 21 | @rm -f bootloader myos.img haribote.bin 22 | -------------------------------------------------------------------------------- /day5.9/readme: -------------------------------------------------------------------------------- 1 | day5.9 2 | 这部分无法看到运行结果。 3 | 4 | 根据huangtao的代码在修改,定义了idtgdt.c文件.不过有几个地址和书上不一样,感觉会出问题。。。 5 | 6 | 内部makefile也修改了。ld的时候对应的ld文件一直不懂。先凑合用一下。 7 | 8 | load_gdtr()和load_idtr()函数,都在entry.S中进行了设定,其实还是汇编调用. 9 | x86.h,entry.S都根据huangtao的代码进行了拷贝 10 | -------------------------------------------------------------------------------- /day6/kernel/asmint32.S: -------------------------------------------------------------------------------- 1 | .global asm_inthandler21 2 | .global load_gdtr 3 | .global load_idtr 4 | .code32 5 | asm_inthandler21: 6 | pushw %es 7 | pushw %ds 8 | pushal 9 | movl %esp,%eax 10 | pushl %eax 11 | movw %ss,%ax 12 | movw %ax,%ds 13 | movw %ax,%es 14 | call inthandler21 15 | 16 | popl %eax 17 | popal 18 | popw %ds 19 | popW %es 20 | iret 21 | load_gdtr: #; void load_gdtr(int limit, int addr); 22 | mov 4(%esp) ,%ax 23 | mov %ax,6(%esp) 24 | lgdt 6(%esp) 25 | ret 26 | 27 | 28 | load_idtr: #; void load_idtr(int limit, int addr); 29 | mov 4(%esp) ,%ax 30 | mov %ax,6(%esp) 31 | lidt 6(%esp) 32 | ret -------------------------------------------------------------------------------- /day6/kernel/entry.ld: -------------------------------------------------------------------------------- 1 | /* Simple linker script for the JOS kernel. 2 | See the GNU ld 'info' manual ("info ld") to learn the syntax. */ 3 | 4 | OUTPUT_FORMAT("elf32-i386", "elf32-i386", "elf32-i386") 5 | OUTPUT_ARCH(i386) 6 | ENTRY(_start) 7 | 8 | SECTIONS 9 | { 10 | /* Link the kernel at this address: "." means the current address */ 11 | . = 0xc400; 12 | 13 | /* AT(...) gives the load address of this section, which tells 14 | the boot loader where to load the kernel in physical memory */ 15 | .text : { 16 | *(.text .stub .text.* .gnu.linkonce.t.*) 17 | } 18 | .rodata : { 19 | *(.rodata .rodata.* .gnu.linkonce.r.*) 20 | } 21 | 22 | .data : { 23 | *(.data) 24 | } 25 | 26 | .bss : { 27 | *(.bss) 28 | } 29 | 30 | 31 | } 32 | -------------------------------------------------------------------------------- /day6/kernel/font.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /day6/kernel/font.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ================================================== 3 | * 4 | * Filename: font.h 5 | * 6 | * Description: 7 | * 8 | * Version: 0.01 9 | * Created: 2014年03月02日 星期日 12时27分31秒 10 | * Author: ChrisZZ, zchrissirhcz@163.com 11 | * Company: ZJUT 12 | * 13 | * ================================================== 14 | */ 15 | #ifndef font_header 16 | #define font_header 17 | 18 | extern unsigned short ASCII_Table[]; 19 | extern char hankaku[2048]; 20 | 21 | #endif 22 | -------------------------------------------------------------------------------- /day6/kernel/kernel.ld: -------------------------------------------------------------------------------- 1 | /* Simple linker script for the JOS kernel. this file is for main.c 2 | See the GNU ld 'info' manual ("info ld") to learn the syntax. */ 3 | 4 | OUTPUT_FORMAT("elf32-i386", "elf32-i386", "elf32-i386") 5 | OUTPUT_ARCH(i386) 6 | ENTRY(_start) 7 | 8 | SECTIONS 9 | { 10 | /* Link the kernel at this address: "." means the current address */ 11 | . = 0x280000; 12 | .text : { 13 | *(.text) 14 | } 15 | .rodata : { 16 | *(.rodata) 17 | } 18 | 19 | .data : { 20 | *(.data) 21 | } 22 | 23 | .bss : { 24 | *(.bss) 25 | } 26 | 27 | 28 | } 29 | -------------------------------------------------------------------------------- /day6/kernel/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ================================================== 3 | * 4 | * Filename: bootpack.c 5 | * 6 | * Description: 7 | * 8 | * Version: 0.01 9 | * Created: 2014年03月01日 星期六 22时03分34秒 10 | * Author: ChrisZZ, zchrissirhcz@163.com 11 | * Company: ZJUT 12 | * 13 | * ================================================== 14 | */ 15 | #include 16 | #include 17 | /* 18 | void bootmain(void){ 19 | //注意这里的函数名字为bootmain,因为在entry.S中设定的入口名字也是bootmain,两者要保持一致 20 | clear_screen(40); 21 | 22 | char *vram; 23 | int xsize, ysize; 24 | struct BOOTINFO *binfo; 25 | binfo=(struct BOOTINFO *)0x0ff0; 26 | init_screen((struct BOOTINFO*)0x0ff0); 27 | 28 | draw_window(); 29 | 30 | init_palette(); 31 | 32 | vram=(char *)0x0ff0; 33 | 34 | char font_A[16]={ 35 | 0x00, 0x18, 0x18, 0x18, 0x18, 0x24, 0x24, 0x24, 36 | 0x24, 0x7e, 0x42, 0x42, 0x42, 0xe7, 0x00, 0x00 37 | }; 38 | 39 | //putfont8(vram, 20, 50, 50, 'd', font_A); 40 | putfont8(binfo->vram binfo->xsize, 8, 8, COL8_FFFFFF, font_A); 41 | } 42 | */ 43 | void bootmain(void){ 44 | struct BOOTINFO *binfo = (struct BOOTINFO*) 0x00000ff0; 45 | extern char hankaku[2048]; 46 | 47 | sti(); 48 | init_palette(); 49 | init_screen(binfo->vram, binfo->scrnx, binfo->scrny); 50 | char s[10]; 51 | //sprintf(s, "scrnx=%d", binfo->scrnx); 52 | //putfont8_asc(binfo->vram, binfo->scrnx, 16, 64, COL8_FFFFFF, s); 53 | 54 | char mcursor[256]; 55 | int mx, my; 56 | mx = (binfo->scrnx-16)/2; 57 | my = (binfo->scrny-28-16)/2; 58 | init_mouse_cursor8(mcursor, COL8_008484); 59 | putblock8_8(binfo->vram, binfo->scrnx, 16, 16, 60, 60, mcursor, 16); 60 | 61 | //sprintf(s, "(%d, %d)", mx, my); 62 | //putfont8_asc(binfo->vram, binfo->scrnx, 0, 0, COL8_FFFFFF, s); 63 | 64 | // while(1); 65 | 66 | init_gdtidt(); 67 | init_pic(); 68 | 69 | outb(PIC0_IMR, 0xf9); 70 | outb(PIC1_IMR, 0xef); 71 | /* 72 | int *addr=(int *)(0x0026f800+8*0x21); 73 | */ 74 | //sprintf(*(addr), 0); 75 | //sprintf(*(addr+1), 160); 76 | while(1); 77 | } 78 | -------------------------------------------------------------------------------- /day6/kernel/makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Makefile fragment for the JOS kernel. 3 | # This is NOT a complete makefile; 4 | # you must run GNU make in the top-level directory 5 | # where the GNUmakefile is located. 6 | # 7 | #entry.S main.c 8 | OBJCOPY := $(GCCPREFIX)objcopy 9 | OBJDUMP := $(GCCPREFIX)objdump 10 | OBJDIR=. 11 | entry_objs= entry.o 12 | ccode_objs=main.o screen.o font.o print.o idtgdt.o int.o asmint32.o 13 | CFLAGS := $(CFLAGS) -O1 -fno-builtin -I$(OBJDIR) -MD 14 | CFLAGS += -fno-omit-frame-pointer 15 | CFLAGS += -Wall -Wno-format -Wno-unused -gstabs -m32 -fno-stack-protector 16 | LDFLAGS = -m elf_i386 17 | 18 | %.o:%.c 19 | $(CC) -nostdinc $(CFLAGS) -Os -c -o $@ $< 20 | 21 | 22 | %.o:%.S 23 | $(CC) -nostdinc $(CFLAGS) -c -o $@ $< 24 | 25 | %.o:%.asm 26 | nasm -f elf -o $@ $< 27 | 28 | kernel:entry cobj 29 | cat cobj>>entry 30 | cp entry kernel 31 | 32 | entry:$(entry_objs) 33 | cobj:$(ccode_objs) 34 | 35 | 36 | 37 | 38 | entry: 39 | @echo "addr=$(addr)" 40 | $(LD) $(LDFLAGS) -N -e entry -Ttext 0xc400 -o $@.out $^ 41 | $(OBJDUMP) -S -D $@.out >$@.asm 42 | $(OBJCOPY) -S -O binary -j .text $@.out $@ 43 | 44 | cobj: 45 | @echo "addr=0x00" 46 | $(LD) $(LDFLAGS) -N -e bootmain -T kernel.ld -o $@.out $^ 47 | $(OBJDUMP) -S -D $@.out >$@.asm 48 | $(OBJCOPY) -S -O binary $@.out $@ 49 | 50 | 51 | clean: 52 | -rm -f *.d *.o *.out main.o kernel kernel.asm cobj* entry.o entry.asm entry 53 | 54 | run: 55 | # dd if=boot of=boot.img bs=512 count=1 56 | qemu -drive file=kernel,if=floppy -------------------------------------------------------------------------------- /day6/kernel/print.c: -------------------------------------------------------------------------------- 1 | #include 2 | void itoa(int value,char *buf){ 3 | char tmp_buf[10] = {0}; 4 | char *tbp = tmp_buf; 5 | if((value >> 31) & 0x1){ // neg num 6 | *buf++ = '-'; 7 | value = ~value + 1; 8 | } 9 | 10 | do{ 11 | *tbp++ = ('0' + (char)(value % 10)); 12 | value /= 10; 13 | }while(value); 14 | while(tmp_buf != tbp--) 15 | *buf++ = *tbp; 16 | *buf='\0'; 17 | } 18 | 19 | //实现可变参数的打印,主要是为了观察打印的变量。 20 | void sprintf(char *str,char *format ,...){ 21 | 22 | int *var=(int *)(&format)+1; //得到第一个可变参数的地址 23 | char buffer[10]; 24 | char *buf=buffer; 25 | while(*format){ 26 | if(*format!='%'){ 27 | *str++=*format++; 28 | continue; 29 | } 30 | else{ 31 | format++; 32 | switch (*format){ 33 | case 'd':itoa(*var,buf);while(*buf){*str++=*buf++;};var++;break; 34 | case 'x':break; 35 | case 's':break; 36 | } 37 | format++; 38 | } 39 | } 40 | *str='\0'; 41 | } 42 | -------------------------------------------------------------------------------- /day6/kernel/run: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | cd .. 3 | sudo make 4 | -------------------------------------------------------------------------------- /day6/makefile: -------------------------------------------------------------------------------- 1 | subdir=./kernel 2 | all:run 3 | 4 | asm: 5 | # nasm -o bootloader hello.nas 6 | nasm -o bootloader hello.nas 7 | @cd $(subdir);make clean;make addr=0xc400 8 | 9 | img:asm 10 | dd if=bootloader of=myos.img bs=512 count=1 11 | dd if=/dev/zero of=myos.img bs=512 seek=1 skip=1 count=2879 12 | 13 | copy:img 14 | mkdir -p /tmp/floppy 15 | mount -o loop myos.img /tmp/floppy -o fat=12 16 | sleep 1 17 | cp $(subdir)/kernel /tmp/floppy 18 | sleep 1 19 | umount /tmp/floppy 20 | run:copy 21 | qemu-system-i386 -drive file=myos.img,if=floppy 22 | clean: 23 | @rm -f bootloader myos.img haribote.bin 24 | -------------------------------------------------------------------------------- /day6/readme: -------------------------------------------------------------------------------- 1 | day6 2 | 这一天改了好多次,由于米油的代码中entry.S总是在变化,开始时没有认真看过entry.S的内容,被卡了好久。 3 | entry.S是来自ucore_pub的一段代码bootasm.S修改而来 4 | -------------------------------------------------------------------------------- /day7.1.1/kernel/asmint32.S: -------------------------------------------------------------------------------- 1 | .global asm_inthandler21 2 | .global load_gdtr 3 | .global load_idtr 4 | .code32 5 | asm_inthandler21: 6 | pushw %es 7 | pushw %ds 8 | pushal 9 | movl %esp,%eax 10 | pushl %eax 11 | movw %ss,%ax 12 | movw %ax,%ds 13 | movw %ax,%es 14 | call inthandler21 15 | 16 | popl %eax 17 | popal 18 | popw %ds 19 | popW %es 20 | iret 21 | load_gdtr: #; void load_gdtr(int limit, int addr); 22 | mov 4(%esp) ,%ax 23 | mov %ax,6(%esp) 24 | lgdt 6(%esp) 25 | ret 26 | 27 | 28 | load_idtr: #; void load_idtr(int limit, int addr); 29 | mov 4(%esp) ,%ax 30 | mov %ax,6(%esp) 31 | lidt 6(%esp) 32 | ret -------------------------------------------------------------------------------- /day7.1.1/kernel/entry.ld: -------------------------------------------------------------------------------- 1 | /* Simple linker script for the JOS kernel. 2 | See the GNU ld 'info' manual ("info ld") to learn the syntax. */ 3 | 4 | OUTPUT_FORMAT("elf32-i386", "elf32-i386", "elf32-i386") 5 | OUTPUT_ARCH(i386) 6 | ENTRY(_start) 7 | 8 | SECTIONS 9 | { 10 | /* Link the kernel at this address: "." means the current address */ 11 | . = 0xc400; 12 | 13 | /* AT(...) gives the load address of this section, which tells 14 | the boot loader where to load the kernel in physical memory */ 15 | .text : { 16 | *(.text .stub .text.* .gnu.linkonce.t.*) 17 | } 18 | .rodata : { 19 | *(.rodata .rodata.* .gnu.linkonce.r.*) 20 | } 21 | 22 | .data : { 23 | *(.data) 24 | } 25 | 26 | .bss : { 27 | *(.bss) 28 | } 29 | 30 | 31 | } 32 | -------------------------------------------------------------------------------- /day7.1.1/kernel/fifo.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ================================================== 3 | * 4 | * Filename: fifo.c 5 | * 6 | * Description: 7 | * 8 | * Version: 0.01 9 | * Created: 2014年03月06日 星期四 18时42分55秒 10 | * Author: ChrisZZ, zchrissirhcz@163.com 11 | * Company: ZJUT 12 | * 13 | * ================================================== 14 | */ 15 | #include 16 | #define FLAGS_OVERRUN 0x0001 17 | 18 | void fifo8_init(struct FIFO8* fifo, int size, char *buf){ 19 | fifo->size = size; 20 | fifo->buf = buf; 21 | fifo->free = size; 22 | fifo->flags = 0; 23 | fifo->p = 0; 24 | fifo->q = 0; 25 | return; 26 | } 27 | 28 | int fifo8_put(struct FIFO8* fifo, char data){ 29 | if(fifo->free==0){ 30 | fifo->flags |= FLAGS_OVERRUN; 31 | return -1; 32 | } 33 | fifo->buf[fifo->p] = data; 34 | fifo->p++; 35 | if(fifo->p == fifo->size){ 36 | fifo->p=0; 37 | } 38 | fifo->free--; 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /day7.1.1/kernel/font.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /day7.1.1/kernel/font.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ================================================== 3 | * 4 | * Filename: font.h 5 | * 6 | * Description: 7 | * 8 | * Version: 0.01 9 | * Created: 2014年03月02日 星期日 12时27分31秒 10 | * Author: ChrisZZ, zchrissirhcz@163.com 11 | * Company: ZJUT 12 | * 13 | * ================================================== 14 | */ 15 | #ifndef font_header 16 | #define font_header 17 | 18 | extern unsigned short ASCII_Table[]; 19 | extern char hankaku[2048]; 20 | 21 | #endif 22 | -------------------------------------------------------------------------------- /day7.1.1/kernel/int.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define PORT_KEYDAT 0x0060 4 | 5 | 6 | struct KEYBUF keybuf; 7 | 8 | void init_pic(void){ 9 | outb(PIC0_IMR, 0xff); 10 | outb(PIC1_IMR, 0xff); 11 | 12 | outb(PIC0_ICW1, 0x11); 13 | outb(PIC0_ICW2, 0x20); 14 | outb(PIC0_ICW3, 1<<2); 15 | outb(PIC0_ICW4, 0x01); 16 | 17 | outb(PIC1_ICW1, 0x11); 18 | outb(PIC1_ICW2, 0x28); 19 | outb(PIC1_ICW3, 2); 20 | outb(PIC1_ICW4, 0x01); 21 | 22 | outb(PIC0_IMR, 0xfb); 23 | outb(PIC1_IMR, 0xff); 24 | 25 | return; 26 | } 27 | 28 | 29 | void inthandler21(int *esp){ 30 | struct BOOTINFO *binfo = (struct BOOTINFO*)ADR_BOOTINFO; 31 | // putfonts8_asc(binfo->vram, binfo->scrnx, 0, 16, COL8_000084, ""); 32 | boxfill8(binfo->vram, binfo->scrnx, COL8_000084, 0, 16, 31, 31); 33 | unsigned char data; 34 | char s[4]; 35 | outb(PIC0_OCW2, 0x61); 36 | data = inb(PORT_KEYDAT); 37 | 38 | sprintf(s, "%x", data); 39 | // boxfill8(binfo->vram, binfo->scrnx, COL8_000084, 0, 16, 15, 31); 40 | putfonts8_asc(binfo->vram, binfo->scrnx, 0, 16, COL8_FFFFFF, s); 41 | return; 42 | } 43 | 44 | /* 45 | void inthandler21(int *esp){ 46 | char data; 47 | outb(PIC0_OCW2, 0x61); 48 | data = inb(PORT_KEYDAT); 49 | if(keybuf.next<32){ 50 | keybuf.data[keybuf.next] = data; 51 | keybuf.next++; 52 | } 53 | return; 54 | } 55 | */ 56 | /* 57 | void inthandler21(int *esp){ 58 | char data; 59 | outb(PIC0_OCW2, 0x61); 60 | data = inb(PORT_KEYDAT); 61 | if(keybuf.len < 32){ 62 | keybuf.data[keybuf.next_w] = data; 63 | keybuf.len++; 64 | keybuf.next_w++; 65 | if(keybuf.next_w==32){ 66 | keybuf.next_w = 0; 67 | } 68 | } 69 | return; 70 | } 71 | */ 72 | -------------------------------------------------------------------------------- /day7.1.1/kernel/kernel.ld: -------------------------------------------------------------------------------- 1 | /* Simple linker script for the JOS kernel. this file is for main.c 2 | See the GNU ld 'info' manual ("info ld") to learn the syntax. */ 3 | 4 | OUTPUT_FORMAT("elf32-i386", "elf32-i386", "elf32-i386") 5 | OUTPUT_ARCH(i386) 6 | ENTRY(_start) 7 | 8 | SECTIONS 9 | { 10 | /* Link the kernel at this address: "." means the current address */ 11 | . = 0x280000; 12 | .text : { 13 | *(.text) 14 | } 15 | .rodata : { 16 | *(.rodata) 17 | } 18 | 19 | .data : { 20 | *(.data) 21 | } 22 | 23 | .bss : { 24 | *(.bss) 25 | } 26 | 27 | 28 | } 29 | -------------------------------------------------------------------------------- /day7.1.1/kernel/log: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zchrissirhcz/osask-linux/a7afa7623205aaee125fc33676480d72c21e5323/day7.1.1/kernel/log -------------------------------------------------------------------------------- /day7.1.1/kernel/makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Makefile fragment for the JOS kernel. 3 | # This is NOT a complete makefile; 4 | # you must run GNU make in the top-level directory 5 | # where the GNUmakefile is located. 6 | # 7 | #entry.S main.c 8 | OBJCOPY := $(GCCPREFIX)objcopy 9 | OBJDUMP := $(GCCPREFIX)objdump 10 | OBJDIR=. 11 | entry_objs= entry.o 12 | ccode_objs=main.o screen.o font.o print.o idtgdt.o int.o asmint32.o 13 | CFLAGS := $(CFLAGS) -O1 -fno-builtin -I$(OBJDIR) -MD 14 | #CFLAGS := $(CFLAGS) -O1 -fno-builtin -MD 15 | CFLAGS += -fno-omit-frame-pointer 16 | CFLAGS += -Wall -Wno-format -Wno-unused -gstabs -m32 -fno-stack-protector 17 | LDFLAGS = -m elf_i386 18 | 19 | %.o:%.c 20 | $(CC) -nostdinc $(CFLAGS) -Os -c -o $@ $< 21 | 22 | 23 | %.o:%.S 24 | $(CC) -nostdinc $(CFLAGS) -c -o $@ $< 25 | 26 | %.o:%.asm 27 | nasm -f elf -o $@ $< 28 | 29 | kernel:entry cobj 30 | cat cobj>>entry 31 | cp entry kernel 32 | 33 | entry:$(entry_objs) 34 | cobj:$(ccode_objs) 35 | 36 | 37 | 38 | 39 | entry: 40 | @echo "addr=$(addr)" 41 | $(LD) $(LDFLAGS) -N -e entry -Ttext 0xc400 -o $@.out $^ 42 | $(OBJDUMP) -S -D $@.out >$@.asm 43 | $(OBJCOPY) -S -O binary -j .text $@.out $@ 44 | 45 | cobj: 46 | @echo "addr=0x00" 47 | $(LD) $(LDFLAGS) -N -e bootmain -T kernel.ld -o $@.out $^ 48 | $(OBJDUMP) -S -D $@.out >$@.asm 49 | $(OBJCOPY) -S -O binary $@.out $@ 50 | 51 | 52 | clean: 53 | -rm -f *.d *.o *.out main.o kernel kernel.asm cobj* entry.o entry.asm entry 54 | 55 | run: 56 | # dd if=boot of=boot.img bs=512 count=1 57 | qemu -drive file=kernel,if=floppy 58 | -------------------------------------------------------------------------------- /day7.1.1/kernel/run: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | cd .. 3 | sudo make 4 | -------------------------------------------------------------------------------- /day7.1.1/makefile: -------------------------------------------------------------------------------- 1 | subdir=./kernel 2 | all:run 3 | 4 | asm: 5 | # nasm -o bootloader hello.nas 6 | nasm -o bootloader hello.nas 7 | @cd $(subdir);make clean;make addr=0xc400 8 | 9 | img:asm 10 | dd if=bootloader of=myos.img bs=512 count=1 11 | dd if=/dev/zero of=myos.img bs=512 seek=1 skip=1 count=2879 12 | 13 | copy:img 14 | mkdir -p /tmp/floppy 15 | mount -o loop myos.img /tmp/floppy -o fat=12 16 | sleep 1 17 | cp $(subdir)/kernel /tmp/floppy 18 | sleep 1 19 | umount /tmp/floppy 20 | run:copy 21 | qemu-system-i386 -drive file=myos.img,if=floppy 22 | clean: 23 | @rm -f bootloader myos.img haribote.bin 24 | -------------------------------------------------------------------------------- /day7.1.1/readme: -------------------------------------------------------------------------------- 1 | day7.1.1 2 | 这天的代码终于有我自行修改的部分了! 3 | 对照《30天》和米油的代码,键盘每次都显示同样的字符:2X 这很诡异。。 4 | 按键后显示字符,这是因为键盘出发了中断,那么具体显示什么字符,就要看中断程序是如何写的。 5 | 键盘的中断是int.c中的inthandler21(int *esp)控制的 6 | 7 | 每次按键显示字符后,需要重新刷新页面: 8 | boxfill8(binfo->vram, binfo->scrnx, COL8_000084, 0, 16, 31, 31); 9 | -------------------------------------------------------------------------------- /day7.1/kernel/asmint32.S: -------------------------------------------------------------------------------- 1 | .global asm_inthandler21 2 | .global load_gdtr 3 | .global load_idtr 4 | .code32 5 | asm_inthandler21: 6 | pushw %es 7 | pushw %ds 8 | pushal 9 | movl %esp,%eax 10 | pushl %eax 11 | movw %ss,%ax 12 | movw %ax,%ds 13 | movw %ax,%es 14 | call inthandler21 15 | 16 | popl %eax 17 | popal 18 | popw %ds 19 | popW %es 20 | iret 21 | load_gdtr: #; void load_gdtr(int limit, int addr); 22 | mov 4(%esp) ,%ax 23 | mov %ax,6(%esp) 24 | lgdt 6(%esp) 25 | ret 26 | 27 | 28 | load_idtr: #; void load_idtr(int limit, int addr); 29 | mov 4(%esp) ,%ax 30 | mov %ax,6(%esp) 31 | lidt 6(%esp) 32 | ret -------------------------------------------------------------------------------- /day7.1/kernel/entry.ld: -------------------------------------------------------------------------------- 1 | /* Simple linker script for the JOS kernel. 2 | See the GNU ld 'info' manual ("info ld") to learn the syntax. */ 3 | 4 | OUTPUT_FORMAT("elf32-i386", "elf32-i386", "elf32-i386") 5 | OUTPUT_ARCH(i386) 6 | ENTRY(_start) 7 | 8 | SECTIONS 9 | { 10 | /* Link the kernel at this address: "." means the current address */ 11 | . = 0xc400; 12 | 13 | /* AT(...) gives the load address of this section, which tells 14 | the boot loader where to load the kernel in physical memory */ 15 | .text : { 16 | *(.text .stub .text.* .gnu.linkonce.t.*) 17 | } 18 | .rodata : { 19 | *(.rodata .rodata.* .gnu.linkonce.r.*) 20 | } 21 | 22 | .data : { 23 | *(.data) 24 | } 25 | 26 | .bss : { 27 | *(.bss) 28 | } 29 | 30 | 31 | } 32 | -------------------------------------------------------------------------------- /day7.1/kernel/font.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /day7.1/kernel/font.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ================================================== 3 | * 4 | * Filename: font.h 5 | * 6 | * Description: 7 | * 8 | * Version: 0.01 9 | * Created: 2014年03月02日 星期日 12时27分31秒 10 | * Author: ChrisZZ, zchrissirhcz@163.com 11 | * Company: ZJUT 12 | * 13 | * ================================================== 14 | */ 15 | #ifndef font_header 16 | #define font_header 17 | 18 | extern unsigned short ASCII_Table[]; 19 | extern char hankaku[2048]; 20 | 21 | #endif 22 | -------------------------------------------------------------------------------- /day7.1/kernel/int.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define PORT_KEYDAT 0x0060 4 | 5 | void init_pic(void){ 6 | outb(PIC0_IMR, 0xff); 7 | outb(PIC1_IMR, 0xff); 8 | 9 | outb(PIC0_ICW1, 0x11); 10 | outb(PIC0_ICW2, 0x20); 11 | outb(PIC0_ICW3, 1<<2); 12 | outb(PIC0_ICW4, 0x01); 13 | 14 | outb(PIC1_ICW1, 0x11); 15 | outb(PIC1_ICW2, 0x28); 16 | outb(PIC1_ICW3, 2); 17 | outb(PIC1_ICW4, 0x01); 18 | 19 | outb(PIC0_IMR, 0xfb); 20 | outb(PIC1_IMR, 0xff); 21 | 22 | return; 23 | } 24 | 25 | void inthandler21(int *esp){ 26 | struct BOOTINFO *binfo = (struct BOOTINFO*)ADR_BOOTINFO; 27 | char data, s[4]; 28 | outb(PIC0_OCW2, 0X61); 29 | data = inb(PORT_KEYDAT); 30 | 31 | sprintf(s, "%02X", data); 32 | boxfill8(binfo->vram, binfo->scrnx, COL8_000000, 0, 16, 15, 31); 33 | putfonts8_asc(binfo->vram, binfo->scrnx, 0, 16, COL8_FFFFFF, s); 34 | return; 35 | } 36 | -------------------------------------------------------------------------------- /day7.1/kernel/kernel.ld: -------------------------------------------------------------------------------- 1 | /* Simple linker script for the JOS kernel. this file is for main.c 2 | See the GNU ld 'info' manual ("info ld") to learn the syntax. */ 3 | 4 | OUTPUT_FORMAT("elf32-i386", "elf32-i386", "elf32-i386") 5 | OUTPUT_ARCH(i386) 6 | ENTRY(_start) 7 | 8 | SECTIONS 9 | { 10 | /* Link the kernel at this address: "." means the current address */ 11 | . = 0x280000; 12 | .text : { 13 | *(.text) 14 | } 15 | .rodata : { 16 | *(.rodata) 17 | } 18 | 19 | .data : { 20 | *(.data) 21 | } 22 | 23 | .bss : { 24 | *(.bss) 25 | } 26 | 27 | 28 | } 29 | -------------------------------------------------------------------------------- /day7.1/kernel/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ================================================== 3 | * 4 | * Filename: bootpack.c 5 | * 6 | * Description: 7 | * 8 | * Version: 0.01 9 | * Created: 2014年03月01日 星期六 22时03分34秒 10 | * Author: ChrisZZ, zchrissirhcz@163.com 11 | * Company: ZJUT 12 | * 13 | * ================================================== 14 | */ 15 | #include 16 | #include 17 | /* 18 | void bootmain(void){ 19 | //注意这里的函数名字为bootmain,因为在entry.S中设定的入口名字也是bootmain,两者要保持一致 20 | clear_screen(40); 21 | 22 | char *vram; 23 | int xsize, ysize; 24 | struct BOOTINFO *binfo; 25 | binfo=(struct BOOTINFO *)0x0ff0; 26 | init_screen((struct BOOTINFO*)0x0ff0); 27 | 28 | draw_window(); 29 | 30 | init_palette(); 31 | 32 | vram=(char *)0x0ff0; 33 | 34 | char font_A[16]={ 35 | 0x00, 0x18, 0x18, 0x18, 0x18, 0x24, 0x24, 0x24, 36 | 0x24, 0x7e, 0x42, 0x42, 0x42, 0xe7, 0x00, 0x00 37 | }; 38 | 39 | //putfont8(vram, 20, 50, 50, 'd', font_A); 40 | putfont8(binfo->vram binfo->xsize, 8, 8, COL8_FFFFFF, font_A); 41 | } 42 | */ 43 | void bootmain(void){ 44 | struct BOOTINFO *binfo = (struct BOOTINFO*) 0x00000ff0; 45 | extern char hankaku[2048]; 46 | 47 | sti(); 48 | init_palette(); 49 | init_screen(binfo->vram, binfo->scrnx, binfo->scrny); 50 | char s[10]; 51 | //sprintf(s, "scrnx=%d", binfo->scrnx); 52 | //putfont8_asc(binfo->vram, binfo->scrnx, 16, 64, COL8_FFFFFF, s); 53 | 54 | char mcursor[256]; 55 | int mx, my; 56 | mx = (binfo->scrnx-16)/2; 57 | my = (binfo->scrny-28-16)/2; 58 | init_mouse_cursor8(mcursor, COL8_008484); 59 | putblock8_8(binfo->vram, binfo->scrnx, 16, 16, 60, 60, mcursor, 16); 60 | 61 | //sprintf(s, "(%d, %d)", mx, my); 62 | //putfont8_asc(binfo->vram, binfo->scrnx, 0, 0, COL8_FFFFFF, s); 63 | 64 | // while(1); 65 | 66 | init_gdtidt(); 67 | init_pic(); 68 | 69 | outb(PIC0_IMR, 0xf9); 70 | outb(PIC1_IMR, 0xef); 71 | /* 72 | int *addr=(int *)(0x0026f800+8*0x21); 73 | */ 74 | //sprintf(*(addr), 0); 75 | //sprintf(*(addr+1), 160); 76 | while(1); 77 | } 78 | -------------------------------------------------------------------------------- /day7.1/kernel/makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Makefile fragment for the JOS kernel. 3 | # This is NOT a complete makefile; 4 | # you must run GNU make in the top-level directory 5 | # where the GNUmakefile is located. 6 | # 7 | #entry.S main.c 8 | OBJCOPY := $(GCCPREFIX)objcopy 9 | OBJDUMP := $(GCCPREFIX)objdump 10 | OBJDIR=. 11 | entry_objs= entry.o 12 | ccode_objs=main.o screen.o font.o print.o idtgdt.o int.o asmint32.o 13 | CFLAGS := $(CFLAGS) -O1 -fno-builtin -I$(OBJDIR) -MD 14 | CFLAGS += -fno-omit-frame-pointer 15 | CFLAGS += -Wall -Wno-format -Wno-unused -gstabs -m32 -fno-stack-protector 16 | LDFLAGS = -m elf_i386 17 | 18 | %.o:%.c 19 | $(CC) -nostdinc $(CFLAGS) -Os -c -o $@ $< 20 | 21 | 22 | %.o:%.S 23 | $(CC) -nostdinc $(CFLAGS) -c -o $@ $< 24 | 25 | %.o:%.asm 26 | nasm -f elf -o $@ $< 27 | 28 | kernel:entry cobj 29 | cat cobj>>entry 30 | cp entry kernel 31 | 32 | entry:$(entry_objs) 33 | cobj:$(ccode_objs) 34 | 35 | 36 | 37 | 38 | entry: 39 | @echo "addr=$(addr)" 40 | $(LD) $(LDFLAGS) -N -e entry -Ttext 0xc400 -o $@.out $^ 41 | $(OBJDUMP) -S -D $@.out >$@.asm 42 | $(OBJCOPY) -S -O binary -j .text $@.out $@ 43 | 44 | cobj: 45 | @echo "addr=0x00" 46 | $(LD) $(LDFLAGS) -N -e bootmain -T kernel.ld -o $@.out $^ 47 | $(OBJDUMP) -S -D $@.out >$@.asm 48 | $(OBJCOPY) -S -O binary $@.out $@ 49 | 50 | 51 | clean: 52 | -rm -f *.d *.o *.out main.o kernel kernel.asm cobj* entry.o entry.asm entry 53 | 54 | run: 55 | # dd if=boot of=boot.img bs=512 count=1 56 | qemu -drive file=kernel,if=floppy -------------------------------------------------------------------------------- /day7.1/kernel/run: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | cd .. 3 | sudo make 4 | -------------------------------------------------------------------------------- /day7.1/makefile: -------------------------------------------------------------------------------- 1 | subdir=./kernel 2 | all:run 3 | 4 | asm: 5 | # nasm -o bootloader hello.nas 6 | nasm -o bootloader hello.nas 7 | @cd $(subdir);make clean;make addr=0xc400 8 | 9 | img:asm 10 | dd if=bootloader of=myos.img bs=512 count=1 11 | dd if=/dev/zero of=myos.img bs=512 seek=1 skip=1 count=2879 12 | 13 | copy:img 14 | mkdir -p /tmp/floppy 15 | mount -o loop myos.img /tmp/floppy -o fat=12 16 | sleep 1 17 | cp $(subdir)/kernel /tmp/floppy 18 | sleep 1 19 | umount /tmp/floppy 20 | run:copy 21 | qemu-system-i386 -drive file=myos.img,if=floppy 22 | clean: 23 | @rm -f bootloader myos.img haribote.bin 24 | -------------------------------------------------------------------------------- /day7.2/kernel/asmint32.S: -------------------------------------------------------------------------------- 1 | .global asm_inthandler21 2 | .global load_gdtr 3 | .global load_idtr 4 | .code32 5 | asm_inthandler21: 6 | pushw %es 7 | pushw %ds 8 | pushal 9 | movl %esp,%eax 10 | pushl %eax 11 | movw %ss,%ax 12 | movw %ax,%ds 13 | movw %ax,%es 14 | call inthandler21 15 | 16 | popl %eax 17 | popal 18 | popw %ds 19 | popW %es 20 | iret 21 | load_gdtr: #; void load_gdtr(int limit, int addr); 22 | mov 4(%esp) ,%ax 23 | mov %ax,6(%esp) 24 | lgdt 6(%esp) 25 | ret 26 | 27 | 28 | load_idtr: #; void load_idtr(int limit, int addr); 29 | mov 4(%esp) ,%ax 30 | mov %ax,6(%esp) 31 | lidt 6(%esp) 32 | ret -------------------------------------------------------------------------------- /day7.2/kernel/entry.ld: -------------------------------------------------------------------------------- 1 | /* Simple linker script for the JOS kernel. 2 | See the GNU ld 'info' manual ("info ld") to learn the syntax. */ 3 | 4 | OUTPUT_FORMAT("elf32-i386", "elf32-i386", "elf32-i386") 5 | OUTPUT_ARCH(i386) 6 | ENTRY(_start) 7 | 8 | SECTIONS 9 | { 10 | /* Link the kernel at this address: "." means the current address */ 11 | . = 0xc400; 12 | 13 | /* AT(...) gives the load address of this section, which tells 14 | the boot loader where to load the kernel in physical memory */ 15 | .text : { 16 | *(.text .stub .text.* .gnu.linkonce.t.*) 17 | } 18 | .rodata : { 19 | *(.rodata .rodata.* .gnu.linkonce.r.*) 20 | } 21 | 22 | .data : { 23 | *(.data) 24 | } 25 | 26 | .bss : { 27 | *(.bss) 28 | } 29 | 30 | 31 | } 32 | -------------------------------------------------------------------------------- /day7.2/kernel/fifo.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ================================================== 3 | * 4 | * Filename: fifo.c 5 | * 6 | * Description: 7 | * 8 | * Version: 0.01 9 | * Created: 2014年03月06日 星期四 18时42分55秒 10 | * Author: ChrisZZ, zchrissirhcz@163.com 11 | * Company: ZJUT 12 | * 13 | * ================================================== 14 | */ 15 | #include 16 | #define FLAGS_OVERRUN 0x0001 17 | 18 | void fifo8_init(struct FIFO8* fifo, int size, char *buf){ 19 | fifo->size = size; 20 | fifo->buf = buf; 21 | fifo->free = size; 22 | fifo->flags = 0; 23 | fifo->p = 0; 24 | fifo->q = 0; 25 | return; 26 | } 27 | 28 | int fifo8_put(struct FIFO8* fifo, char data){ 29 | if(fifo->free==0){ 30 | fifo->flags |= FLAGS_OVERRUN; 31 | return -1; 32 | } 33 | fifo->buf[fifo->p] = data; 34 | fifo->p++; 35 | if(fifo->p == fifo->size){ 36 | fifo->p=0; 37 | } 38 | fifo->free--; 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /day7.2/kernel/font.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /day7.2/kernel/font.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ================================================== 3 | * 4 | * Filename: font.h 5 | * 6 | * Description: 7 | * 8 | * Version: 0.01 9 | * Created: 2014年03月02日 星期日 12时27分31秒 10 | * Author: ChrisZZ, zchrissirhcz@163.com 11 | * Company: ZJUT 12 | * 13 | * ================================================== 14 | */ 15 | #ifndef font_header 16 | #define font_header 17 | 18 | extern unsigned short ASCII_Table[]; 19 | extern char hankaku[2048]; 20 | 21 | #endif 22 | -------------------------------------------------------------------------------- /day7.2/kernel/int.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define PORT_KEYDAT 0x0060 4 | 5 | 6 | struct KEYBUF keybuf; 7 | 8 | void init_pic(void){ 9 | outb(PIC0_IMR, 0xff); 10 | outb(PIC1_IMR, 0xff); 11 | 12 | outb(PIC0_ICW1, 0x11); 13 | outb(PIC0_ICW2, 0x20); 14 | outb(PIC0_ICW3, 1<<2); 15 | outb(PIC0_ICW4, 0x01); 16 | 17 | outb(PIC1_ICW1, 0x11); 18 | outb(PIC1_ICW2, 0x28); 19 | outb(PIC1_ICW3, 2); 20 | outb(PIC1_ICW4, 0x01); 21 | 22 | outb(PIC0_IMR, 0xfb); 23 | outb(PIC1_IMR, 0xff); 24 | 25 | return; 26 | } 27 | 28 | 29 | void inthandler21(int *esp){ 30 | /// struct BOOTINFO *binfo = (struct BOOTINFO*)ADR_BOOTINFO; 31 | // putfonts8_asc(binfo->vram, binfo->scrnx, 0, 16, COL8_000084, ""); 32 | /// boxfill8(binfo->vram, binfo->scrnx, COL8_000084, 0, 16, 31, 31); 33 | unsigned char data; 34 | char s[4]; 35 | outb(PIC0_OCW2, 0x61); 36 | data = inb(PORT_KEYDAT); 37 | if(keybuf.flag==0){ 38 | keybuf.data = data; 39 | keybuf.flag = 1; 40 | } 41 | 42 | // sprintf(s, "%x", data); 43 | // boxfill8(binfo->vram, binfo->scrnx, COL8_000084, 0, 16, 15, 31); 44 | // putfonts8_asc(binfo->vram, binfo->scrnx, 0, 16, COL8_FFFFFF, s); 45 | return; 46 | } 47 | 48 | /* 49 | void inthandler21(int *esp){ 50 | char data; 51 | outb(PIC0_OCW2, 0x61); 52 | data = inb(PORT_KEYDAT); 53 | if(keybuf.next<32){ 54 | keybuf.data[keybuf.next] = data; 55 | keybuf.next++; 56 | } 57 | return; 58 | } 59 | */ 60 | /* 61 | void inthandler21(int *esp){ 62 | char data; 63 | outb(PIC0_OCW2, 0x61); 64 | data = inb(PORT_KEYDAT); 65 | if(keybuf.len < 32){ 66 | keybuf.data[keybuf.next_w] = data; 67 | keybuf.len++; 68 | keybuf.next_w++; 69 | if(keybuf.next_w==32){ 70 | keybuf.next_w = 0; 71 | } 72 | } 73 | return; 74 | } 75 | */ 76 | -------------------------------------------------------------------------------- /day7.2/kernel/kernel.ld: -------------------------------------------------------------------------------- 1 | /* Simple linker script for the JOS kernel. this file is for main.c 2 | See the GNU ld 'info' manual ("info ld") to learn the syntax. */ 3 | 4 | OUTPUT_FORMAT("elf32-i386", "elf32-i386", "elf32-i386") 5 | OUTPUT_ARCH(i386) 6 | ENTRY(_start) 7 | 8 | SECTIONS 9 | { 10 | /* Link the kernel at this address: "." means the current address */ 11 | . = 0x280000; 12 | .text : { 13 | *(.text) 14 | } 15 | .rodata : { 16 | *(.rodata) 17 | } 18 | 19 | .data : { 20 | *(.data) 21 | } 22 | 23 | .bss : { 24 | *(.bss) 25 | } 26 | 27 | 28 | } 29 | -------------------------------------------------------------------------------- /day7.2/kernel/log: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zchrissirhcz/osask-linux/a7afa7623205aaee125fc33676480d72c21e5323/day7.2/kernel/log -------------------------------------------------------------------------------- /day7.2/kernel/makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Makefile fragment for the JOS kernel. 3 | # This is NOT a complete makefile; 4 | # you must run GNU make in the top-level directory 5 | # where the GNUmakefile is located. 6 | # 7 | #entry.S main.c 8 | OBJCOPY := $(GCCPREFIX)objcopy 9 | OBJDUMP := $(GCCPREFIX)objdump 10 | OBJDIR=. 11 | entry_objs= entry.o 12 | ccode_objs=main.o screen.o font.o print.o idtgdt.o int.o asmint32.o 13 | CFLAGS := $(CFLAGS) -O1 -fno-builtin -I$(OBJDIR) -MD 14 | #CFLAGS := $(CFLAGS) -O1 -fno-builtin -MD 15 | CFLAGS += -fno-omit-frame-pointer 16 | CFLAGS += -Wall -Wno-format -Wno-unused -gstabs -m32 -fno-stack-protector 17 | LDFLAGS = -m elf_i386 18 | 19 | %.o:%.c 20 | $(CC) -nostdinc $(CFLAGS) -Os -c -o $@ $< 21 | 22 | 23 | %.o:%.S 24 | $(CC) -nostdinc $(CFLAGS) -c -o $@ $< 25 | 26 | %.o:%.asm 27 | nasm -f elf -o $@ $< 28 | 29 | kernel:entry cobj 30 | cat cobj>>entry 31 | cp entry kernel 32 | 33 | entry:$(entry_objs) 34 | cobj:$(ccode_objs) 35 | 36 | 37 | 38 | 39 | entry: 40 | @echo "addr=$(addr)" 41 | $(LD) $(LDFLAGS) -N -e entry -Ttext 0xc400 -o $@.out $^ 42 | $(OBJDUMP) -S -D $@.out >$@.asm 43 | $(OBJCOPY) -S -O binary -j .text $@.out $@ 44 | 45 | cobj: 46 | @echo "addr=0x00" 47 | $(LD) $(LDFLAGS) -N -e bootmain -T kernel.ld -o $@.out $^ 48 | $(OBJDUMP) -S -D $@.out >$@.asm 49 | $(OBJCOPY) -S -O binary $@.out $@ 50 | 51 | 52 | clean: 53 | -rm -f *.d *.o *.out main.o kernel kernel.asm cobj* entry.o entry.asm entry 54 | 55 | run: 56 | # dd if=boot of=boot.img bs=512 count=1 57 | qemu -drive file=kernel,if=floppy 58 | -------------------------------------------------------------------------------- /day7.2/kernel/run: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | cd .. 3 | sudo make 4 | -------------------------------------------------------------------------------- /day7.2/makefile: -------------------------------------------------------------------------------- 1 | subdir=./kernel 2 | all:run 3 | 4 | asm: 5 | # nasm -o bootloader hello.nas 6 | nasm -o bootloader hello.nas 7 | @cd $(subdir);make clean;make addr=0xc400 8 | 9 | img:asm 10 | dd if=bootloader of=myos.img bs=512 count=1 11 | dd if=/dev/zero of=myos.img bs=512 seek=1 skip=1 count=2879 12 | 13 | copy:img 14 | mkdir -p /tmp/floppy 15 | mount -o loop myos.img /tmp/floppy -o fat=12 16 | sleep 1 17 | cp $(subdir)/kernel /tmp/floppy 18 | sleep 1 19 | umount /tmp/floppy 20 | run:copy 21 | qemu-system-i386 -drive file=myos.img,if=floppy 22 | clean: 23 | @rm -f bootloader myos.img haribote.bin 24 | -------------------------------------------------------------------------------- /day7.2/readme: -------------------------------------------------------------------------------- 1 | day7.2 2 | 和7.1.1的问题是一样的,都需要手动添加一个“刷新按键对应的字符显示区”的操作,这次是在main.c函数中添加: 3 | boxfill8(binfo->vram, binfo->scrnx, COL8_000084, 0, 16, 31, 31); 4 | -------------------------------------------------------------------------------- /day7.3/kernel/asmint32.S: -------------------------------------------------------------------------------- 1 | .global asm_inthandler21 2 | .global load_gdtr 3 | .global load_idtr 4 | .code32 5 | asm_inthandler21: 6 | pushw %es 7 | pushw %ds 8 | pushal 9 | movl %esp,%eax 10 | pushl %eax 11 | movw %ss,%ax 12 | movw %ax,%ds 13 | movw %ax,%es 14 | call inthandler21 15 | 16 | popl %eax 17 | popal 18 | popw %ds 19 | popW %es 20 | iret 21 | load_gdtr: #; void load_gdtr(int limit, int addr); 22 | mov 4(%esp) ,%ax 23 | mov %ax,6(%esp) 24 | lgdt 6(%esp) 25 | ret 26 | 27 | 28 | load_idtr: #; void load_idtr(int limit, int addr); 29 | mov 4(%esp) ,%ax 30 | mov %ax,6(%esp) 31 | lidt 6(%esp) 32 | ret -------------------------------------------------------------------------------- /day7.3/kernel/entry.ld: -------------------------------------------------------------------------------- 1 | /* Simple linker script for the JOS kernel. 2 | See the GNU ld 'info' manual ("info ld") to learn the syntax. */ 3 | 4 | OUTPUT_FORMAT("elf32-i386", "elf32-i386", "elf32-i386") 5 | OUTPUT_ARCH(i386) 6 | ENTRY(_start) 7 | 8 | SECTIONS 9 | { 10 | /* Link the kernel at this address: "." means the current address */ 11 | . = 0xc400; 12 | 13 | /* AT(...) gives the load address of this section, which tells 14 | the boot loader where to load the kernel in physical memory */ 15 | .text : { 16 | *(.text .stub .text.* .gnu.linkonce.t.*) 17 | } 18 | .rodata : { 19 | *(.rodata .rodata.* .gnu.linkonce.r.*) 20 | } 21 | 22 | .data : { 23 | *(.data) 24 | } 25 | 26 | .bss : { 27 | *(.bss) 28 | } 29 | 30 | 31 | } 32 | -------------------------------------------------------------------------------- /day7.3/kernel/fifo.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ================================================== 3 | * 4 | * Filename: fifo.c 5 | * 6 | * Description: 7 | * 8 | * Version: 0.01 9 | * Created: 2014年03月06日 星期四 18时42分55秒 10 | * Author: ChrisZZ, zchrissirhcz@163.com 11 | * Company: ZJUT 12 | * 13 | * ================================================== 14 | */ 15 | #include 16 | #define FLAGS_OVERRUN 0x0001 17 | 18 | void fifo8_init(struct FIFO8* fifo, int size, char *buf){ 19 | fifo->size = size; 20 | fifo->buf = buf; 21 | fifo->free = size; 22 | fifo->flags = 0; 23 | fifo->p = 0; 24 | fifo->q = 0; 25 | return; 26 | } 27 | 28 | int fifo8_put(struct FIFO8* fifo, char data){ 29 | if(fifo->free==0){ 30 | fifo->flags |= FLAGS_OVERRUN; 31 | return -1; 32 | } 33 | fifo->buf[fifo->p] = data; 34 | fifo->p++; 35 | if(fifo->p == fifo->size){ 36 | fifo->p=0; 37 | } 38 | fifo->free--; 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /day7.3/kernel/font.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /day7.3/kernel/font.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ================================================== 3 | * 4 | * Filename: font.h 5 | * 6 | * Description: 7 | * 8 | * Version: 0.01 9 | * Created: 2014年03月02日 星期日 12时27分31秒 10 | * Author: ChrisZZ, zchrissirhcz@163.com 11 | * Company: ZJUT 12 | * 13 | * ================================================== 14 | */ 15 | #ifndef font_header 16 | #define font_header 17 | 18 | extern unsigned short ASCII_Table[]; 19 | extern char hankaku[2048]; 20 | 21 | #endif 22 | -------------------------------------------------------------------------------- /day7.3/kernel/int.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define PORT_KEYDAT 0x0060 4 | 5 | 6 | struct KEYBUF keybuf; 7 | 8 | void init_pic(void){ 9 | outb(PIC0_IMR, 0xff); 10 | outb(PIC1_IMR, 0xff); 11 | 12 | outb(PIC0_ICW1, 0x11); 13 | outb(PIC0_ICW2, 0x20); 14 | outb(PIC0_ICW3, 1<<2); 15 | outb(PIC0_ICW4, 0x01); 16 | 17 | outb(PIC1_ICW1, 0x11); 18 | outb(PIC1_ICW2, 0x28); 19 | outb(PIC1_ICW3, 2); 20 | outb(PIC1_ICW4, 0x01); 21 | 22 | outb(PIC0_IMR, 0xfb); 23 | outb(PIC1_IMR, 0xff); 24 | 25 | return; 26 | } 27 | 28 | 29 | void inthandler21(int *esp){ 30 | /// struct BOOTINFO *binfo = (struct BOOTINFO*)ADR_BOOTINFO; 31 | // putfonts8_asc(binfo->vram, binfo->scrnx, 0, 16, COL8_000084, ""); 32 | /// boxfill8(binfo->vram, binfo->scrnx, COL8_000084, 0, 16, 31, 31); 33 | unsigned char data; 34 | ////char s[4]; 35 | outb(PIC0_OCW2, 0x61); 36 | data = inb(PORT_KEYDAT); 37 | if(keybuf.next<32){ 38 | keybuf.data[keybuf.next] = data; 39 | keybuf.next++; 40 | } 41 | 42 | // sprintf(s, "%x", data); 43 | // boxfill8(binfo->vram, binfo->scrnx, COL8_000084, 0, 16, 15, 31); 44 | // putfonts8_asc(binfo->vram, binfo->scrnx, 0, 16, COL8_FFFFFF, s); 45 | return; 46 | } 47 | 48 | /* 49 | void inthandler21(int *esp){ 50 | char data; 51 | outb(PIC0_OCW2, 0x61); 52 | data = inb(PORT_KEYDAT); 53 | if(keybuf.next<32){ 54 | keybuf.data[keybuf.next] = data; 55 | keybuf.next++; 56 | } 57 | return; 58 | } 59 | */ 60 | /* 61 | void inthandler21(int *esp){ 62 | char data; 63 | outb(PIC0_OCW2, 0x61); 64 | data = inb(PORT_KEYDAT); 65 | if(keybuf.len < 32){ 66 | keybuf.data[keybuf.next_w] = data; 67 | keybuf.len++; 68 | keybuf.next_w++; 69 | if(keybuf.next_w==32){ 70 | keybuf.next_w = 0; 71 | } 72 | } 73 | return; 74 | } 75 | */ 76 | -------------------------------------------------------------------------------- /day7.3/kernel/kernel.ld: -------------------------------------------------------------------------------- 1 | /* Simple linker script for the JOS kernel. this file is for main.c 2 | See the GNU ld 'info' manual ("info ld") to learn the syntax. */ 3 | 4 | OUTPUT_FORMAT("elf32-i386", "elf32-i386", "elf32-i386") 5 | OUTPUT_ARCH(i386) 6 | ENTRY(_start) 7 | 8 | SECTIONS 9 | { 10 | /* Link the kernel at this address: "." means the current address */ 11 | . = 0x280000; 12 | .text : { 13 | *(.text) 14 | } 15 | .rodata : { 16 | *(.rodata) 17 | } 18 | 19 | .data : { 20 | *(.data) 21 | } 22 | 23 | .bss : { 24 | *(.bss) 25 | } 26 | 27 | 28 | } 29 | -------------------------------------------------------------------------------- /day7.3/kernel/log: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zchrissirhcz/osask-linux/a7afa7623205aaee125fc33676480d72c21e5323/day7.3/kernel/log -------------------------------------------------------------------------------- /day7.3/kernel/makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Makefile fragment for the JOS kernel. 3 | # This is NOT a complete makefile; 4 | # you must run GNU make in the top-level directory 5 | # where the GNUmakefile is located. 6 | # 7 | #entry.S main.c 8 | OBJCOPY := $(GCCPREFIX)objcopy 9 | OBJDUMP := $(GCCPREFIX)objdump 10 | OBJDIR=. 11 | entry_objs= entry.o 12 | ccode_objs=main.o screen.o font.o print.o idtgdt.o int.o asmint32.o 13 | CFLAGS := $(CFLAGS) -O1 -fno-builtin -I$(OBJDIR) -MD 14 | #CFLAGS := $(CFLAGS) -O1 -fno-builtin -MD 15 | CFLAGS += -fno-omit-frame-pointer 16 | CFLAGS += -Wall -Wno-format -Wno-unused -gstabs -m32 -fno-stack-protector 17 | LDFLAGS = -m elf_i386 18 | 19 | %.o:%.c 20 | $(CC) -nostdinc $(CFLAGS) -Os -c -o $@ $< 21 | 22 | 23 | %.o:%.S 24 | $(CC) -nostdinc $(CFLAGS) -c -o $@ $< 25 | 26 | %.o:%.asm 27 | nasm -f elf -o $@ $< 28 | 29 | kernel:entry cobj 30 | cat cobj>>entry 31 | cp entry kernel 32 | 33 | entry:$(entry_objs) 34 | cobj:$(ccode_objs) 35 | 36 | 37 | 38 | 39 | entry: 40 | @echo "addr=$(addr)" 41 | $(LD) $(LDFLAGS) -N -e entry -Ttext 0xc400 -o $@.out $^ 42 | $(OBJDUMP) -S -D $@.out >$@.asm 43 | $(OBJCOPY) -S -O binary -j .text $@.out $@ 44 | 45 | cobj: 46 | @echo "addr=0x00" 47 | $(LD) $(LDFLAGS) -N -e bootmain -T kernel.ld -o $@.out $^ 48 | $(OBJDUMP) -S -D $@.out >$@.asm 49 | $(OBJCOPY) -S -O binary $@.out $@ 50 | 51 | 52 | clean: 53 | -rm -f *.d *.o *.out main.o kernel kernel.asm cobj* entry.o entry.asm entry 54 | 55 | run: 56 | # dd if=boot of=boot.img bs=512 count=1 57 | qemu -drive file=kernel,if=floppy 58 | -------------------------------------------------------------------------------- /day7.3/kernel/run: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | cd .. 3 | sudo make 4 | -------------------------------------------------------------------------------- /day7.3/log.txt: -------------------------------------------------------------------------------- 1 | nasm -o bootloader hello.nas 2 | make[1]: 进入目录“/home/chris/workspace/os/version1/myos/kernel” 3 | rm -f *.d *.o *.out main.o kernel kernel.asm cobj* entry.o entry.asm entry 4 | make[1]: 离开目录“/home/chris/workspace/os/version1/myos/kernel” 5 | make[1]: 进入目录“/home/chris/workspace/os/version1/myos/kernel” 6 | cc -nostdinc -O1 -fno-builtin -I. -MD -fno-omit-frame-pointer -Wall -Wno-format -Wno-unused -gstabs -m32 -fno-stack-protector -c -o entry.o entry.S 7 | addr=0xc400 8 | ld -m elf_i386 -N -e entry -Ttext 0xc400 -o entry.out entry.o 9 | objdump -S -D entry.out >entry.asm 10 | objcopy -S -O binary -j .text entry.out entry 11 | cc -nostdinc -O1 -fno-builtin -I. -MD -fno-omit-frame-pointer -Wall -Wno-format -Wno-unused -gstabs -m32 -fno-stack-protector -Os -c -o main.o main.c 12 | make[1]: 离开目录“/home/chris/workspace/os/version1/myos/kernel” 13 | -------------------------------------------------------------------------------- /day7.3/makefile: -------------------------------------------------------------------------------- 1 | subdir=./kernel 2 | all:run 3 | 4 | asm: 5 | # nasm -o bootloader hello.nas 6 | nasm -o bootloader hello.nas 7 | @cd $(subdir);make clean;make addr=0xc400 8 | 9 | img:asm 10 | dd if=bootloader of=myos.img bs=512 count=1 11 | dd if=/dev/zero of=myos.img bs=512 seek=1 skip=1 count=2879 12 | 13 | copy:img 14 | mkdir -p /tmp/floppy 15 | mount -o loop myos.img /tmp/floppy -o fat=12 16 | sleep 1 17 | cp $(subdir)/kernel /tmp/floppy 18 | sleep 1 19 | umount /tmp/floppy 20 | run:copy 21 | qemu-system-i386 -drive file=myos.img,if=floppy 22 | clean: 23 | @rm -f bootloader myos.img haribote.bin 24 | -------------------------------------------------------------------------------- /day7.4/kernel/asmint32.S: -------------------------------------------------------------------------------- 1 | .global asm_inthandler21 2 | .global load_gdtr 3 | .global load_idtr 4 | .code32 5 | asm_inthandler21: 6 | pushw %es 7 | pushw %ds 8 | pushal 9 | movl %esp,%eax 10 | pushl %eax 11 | movw %ss,%ax 12 | movw %ax,%ds 13 | movw %ax,%es 14 | call inthandler21 15 | 16 | popl %eax 17 | popal 18 | popw %ds 19 | popW %es 20 | iret 21 | load_gdtr: #; void load_gdtr(int limit, int addr); 22 | mov 4(%esp) ,%ax 23 | mov %ax,6(%esp) 24 | lgdt 6(%esp) 25 | ret 26 | 27 | 28 | load_idtr: #; void load_idtr(int limit, int addr); 29 | mov 4(%esp) ,%ax 30 | mov %ax,6(%esp) 31 | lidt 6(%esp) 32 | ret -------------------------------------------------------------------------------- /day7.4/kernel/entry.ld: -------------------------------------------------------------------------------- 1 | /* Simple linker script for the JOS kernel. 2 | See the GNU ld 'info' manual ("info ld") to learn the syntax. */ 3 | 4 | OUTPUT_FORMAT("elf32-i386", "elf32-i386", "elf32-i386") 5 | OUTPUT_ARCH(i386) 6 | ENTRY(_start) 7 | 8 | SECTIONS 9 | { 10 | /* Link the kernel at this address: "." means the current address */ 11 | . = 0xc400; 12 | 13 | /* AT(...) gives the load address of this section, which tells 14 | the boot loader where to load the kernel in physical memory */ 15 | .text : { 16 | *(.text .stub .text.* .gnu.linkonce.t.*) 17 | } 18 | .rodata : { 19 | *(.rodata .rodata.* .gnu.linkonce.r.*) 20 | } 21 | 22 | .data : { 23 | *(.data) 24 | } 25 | 26 | .bss : { 27 | *(.bss) 28 | } 29 | 30 | 31 | } 32 | -------------------------------------------------------------------------------- /day7.4/kernel/fifo.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ================================================== 3 | * 4 | * Filename: fifo.c 5 | * 6 | * Description: 7 | * 8 | * Version: 0.01 9 | * Created: 2014年03月06日 星期四 18时42分55秒 10 | * Author: ChrisZZ, zchrissirhcz@163.com 11 | * Company: ZJUT 12 | * 13 | * ================================================== 14 | */ 15 | #include 16 | #define FLAGS_OVERRUN 0x0001 17 | 18 | void fifo8_init(struct FIFO8* fifo, int size, char *buf){ 19 | fifo->size = size; 20 | fifo->buf = buf; 21 | fifo->free = size; 22 | fifo->flags = 0; 23 | fifo->p = 0; 24 | fifo->q = 0; 25 | return; 26 | } 27 | 28 | int fifo8_put(struct FIFO8* fifo, char data){ 29 | if(fifo->free==0){ 30 | fifo->flags |= FLAGS_OVERRUN; 31 | return -1; 32 | } 33 | fifo->buf[fifo->p] = data; 34 | fifo->p++; 35 | if(fifo->p == fifo->size){ 36 | fifo->p=0; 37 | } 38 | fifo->free--; 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /day7.4/kernel/font.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /day7.4/kernel/font.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ================================================== 3 | * 4 | * Filename: font.h 5 | * 6 | * Description: 7 | * 8 | * Version: 0.01 9 | * Created: 2014年03月02日 星期日 12时27分31秒 10 | * Author: ChrisZZ, zchrissirhcz@163.com 11 | * Company: ZJUT 12 | * 13 | * ================================================== 14 | */ 15 | #ifndef font_header 16 | #define font_header 17 | 18 | extern unsigned short ASCII_Table[]; 19 | extern char hankaku[2048]; 20 | 21 | #endif 22 | -------------------------------------------------------------------------------- /day7.4/kernel/int.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define PORT_KEYDAT 0x0060 4 | 5 | 6 | struct KEYBUF keybuf; 7 | 8 | void init_pic(void){ 9 | outb(PIC0_IMR, 0xff); 10 | outb(PIC1_IMR, 0xff); 11 | 12 | outb(PIC0_ICW1, 0x11); 13 | outb(PIC0_ICW2, 0x20); 14 | outb(PIC0_ICW3, 1<<2); 15 | outb(PIC0_ICW4, 0x01); 16 | 17 | outb(PIC1_ICW1, 0x11); 18 | outb(PIC1_ICW2, 0x28); 19 | outb(PIC1_ICW3, 2); 20 | outb(PIC1_ICW4, 0x01); 21 | 22 | outb(PIC0_IMR, 0xfb); 23 | outb(PIC1_IMR, 0xff); 24 | 25 | return; 26 | } 27 | 28 | 29 | void inthandler21(int *esp){ 30 | /// struct BOOTINFO *binfo = (struct BOOTINFO*)ADR_BOOTINFO; 31 | // putfonts8_asc(binfo->vram, binfo->scrnx, 0, 16, COL8_000084, ""); 32 | /// boxfill8(binfo->vram, binfo->scrnx, COL8_000084, 0, 16, 31, 31); 33 | unsigned char data; 34 | ////char s[4]; 35 | outb(PIC0_OCW2, 0x61); 36 | data = inb(PORT_KEYDAT); 37 | if(keybuf.len<32){ 38 | keybuf.data[keybuf.next_w] = data; 39 | keybuf.len++; 40 | keybuf.next_w++; 41 | if(keybuf.next_w==32){ 42 | keybuf.next_w=0; 43 | } 44 | } 45 | 46 | // sprintf(s, "%x", data); 47 | // boxfill8(binfo->vram, binfo->scrnx, COL8_000084, 0, 16, 15, 31); 48 | // putfonts8_asc(binfo->vram, binfo->scrnx, 0, 16, COL8_FFFFFF, s); 49 | return; 50 | } 51 | 52 | /* 53 | void inthandler21(int *esp){ 54 | char data; 55 | outb(PIC0_OCW2, 0x61); 56 | data = inb(PORT_KEYDAT); 57 | if(keybuf.next<32){ 58 | keybuf.data[keybuf.next] = data; 59 | keybuf.next++; 60 | } 61 | return; 62 | } 63 | */ 64 | /* 65 | void inthandler21(int *esp){ 66 | char data; 67 | outb(PIC0_OCW2, 0x61); 68 | data = inb(PORT_KEYDAT); 69 | if(keybuf.len < 32){ 70 | keybuf.data[keybuf.next_w] = data; 71 | keybuf.len++; 72 | keybuf.next_w++; 73 | if(keybuf.next_w==32){ 74 | keybuf.next_w = 0; 75 | } 76 | } 77 | return; 78 | } 79 | */ 80 | -------------------------------------------------------------------------------- /day7.4/kernel/kernel.ld: -------------------------------------------------------------------------------- 1 | /* Simple linker script for the JOS kernel. this file is for main.c 2 | See the GNU ld 'info' manual ("info ld") to learn the syntax. */ 3 | 4 | OUTPUT_FORMAT("elf32-i386", "elf32-i386", "elf32-i386") 5 | OUTPUT_ARCH(i386) 6 | ENTRY(_start) 7 | 8 | SECTIONS 9 | { 10 | /* Link the kernel at this address: "." means the current address */ 11 | . = 0x280000; 12 | .text : { 13 | *(.text) 14 | } 15 | .rodata : { 16 | *(.rodata) 17 | } 18 | 19 | .data : { 20 | *(.data) 21 | } 22 | 23 | .bss : { 24 | *(.bss) 25 | } 26 | 27 | 28 | } 29 | -------------------------------------------------------------------------------- /day7.4/kernel/log: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zchrissirhcz/osask-linux/a7afa7623205aaee125fc33676480d72c21e5323/day7.4/kernel/log -------------------------------------------------------------------------------- /day7.4/kernel/makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Makefile fragment for the JOS kernel. 3 | # This is NOT a complete makefile; 4 | # you must run GNU make in the top-level directory 5 | # where the GNUmakefile is located. 6 | # 7 | #entry.S main.c 8 | OBJCOPY := $(GCCPREFIX)objcopy 9 | OBJDUMP := $(GCCPREFIX)objdump 10 | OBJDIR=. 11 | entry_objs= entry.o 12 | ccode_objs=main.o screen.o font.o print.o idtgdt.o int.o asmint32.o 13 | CFLAGS := $(CFLAGS) -O1 -fno-builtin -I$(OBJDIR) -MD 14 | #CFLAGS := $(CFLAGS) -O1 -fno-builtin -MD 15 | CFLAGS += -fno-omit-frame-pointer 16 | CFLAGS += -Wall -Wno-format -Wno-unused -gstabs -m32 -fno-stack-protector 17 | LDFLAGS = -m elf_i386 18 | 19 | %.o:%.c 20 | $(CC) -nostdinc $(CFLAGS) -Os -c -o $@ $< 21 | 22 | 23 | %.o:%.S 24 | $(CC) -nostdinc $(CFLAGS) -c -o $@ $< 25 | 26 | %.o:%.asm 27 | nasm -f elf -o $@ $< 28 | 29 | kernel:entry cobj 30 | cat cobj>>entry 31 | cp entry kernel 32 | 33 | entry:$(entry_objs) 34 | cobj:$(ccode_objs) 35 | 36 | 37 | 38 | 39 | entry: 40 | @echo "addr=$(addr)" 41 | $(LD) $(LDFLAGS) -N -e entry -Ttext 0xc400 -o $@.out $^ 42 | $(OBJDUMP) -S -D $@.out >$@.asm 43 | $(OBJCOPY) -S -O binary -j .text $@.out $@ 44 | 45 | cobj: 46 | @echo "addr=0x00" 47 | $(LD) $(LDFLAGS) -N -e bootmain -T kernel.ld -o $@.out $^ 48 | $(OBJDUMP) -S -D $@.out >$@.asm 49 | $(OBJCOPY) -S -O binary $@.out $@ 50 | 51 | 52 | clean: 53 | -rm -f *.d *.o *.out main.o kernel kernel.asm cobj* entry.o entry.asm entry 54 | 55 | run: 56 | # dd if=boot of=boot.img bs=512 count=1 57 | qemu -drive file=kernel,if=floppy 58 | -------------------------------------------------------------------------------- /day7.4/kernel/run: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | cd .. 3 | sudo make 4 | -------------------------------------------------------------------------------- /day7.4/makefile: -------------------------------------------------------------------------------- 1 | subdir=./kernel 2 | all:run 3 | 4 | asm: 5 | # nasm -o bootloader hello.nas 6 | nasm -o bootloader hello.nas 7 | @cd $(subdir);make clean;make addr=0xc400 8 | 9 | img:asm 10 | dd if=bootloader of=myos.img bs=512 count=1 11 | dd if=/dev/zero of=myos.img bs=512 seek=1 skip=1 count=2879 12 | 13 | copy:img 14 | mkdir -p /tmp/floppy 15 | mount -o loop myos.img /tmp/floppy -o fat=12 16 | sleep 1 17 | cp $(subdir)/kernel /tmp/floppy 18 | sleep 1 19 | umount /tmp/floppy 20 | run:copy 21 | qemu-system-i386 -drive file=myos.img,if=floppy 22 | clean: 23 | @rm -f bootloader myos.img haribote.bin 24 | -------------------------------------------------------------------------------- /day7.4/readme: -------------------------------------------------------------------------------- 1 | day7.4 2 | 仅仅是copy书上的代码 3 | -------------------------------------------------------------------------------- /day7.5/kernel/asmint32.S: -------------------------------------------------------------------------------- 1 | .global asm_inthandler21 2 | .global load_gdtr 3 | .global load_idtr 4 | .code32 5 | asm_inthandler21: 6 | pushw %es 7 | pushw %ds 8 | pushal 9 | movl %esp,%eax 10 | pushl %eax 11 | movw %ss,%ax 12 | movw %ax,%ds 13 | movw %ax,%es 14 | call inthandler21 15 | 16 | popl %eax 17 | popal 18 | popw %ds 19 | popW %es 20 | iret 21 | load_gdtr: #; void load_gdtr(int limit, int addr); 22 | mov 4(%esp) ,%ax 23 | mov %ax,6(%esp) 24 | lgdt 6(%esp) 25 | ret 26 | 27 | 28 | load_idtr: #; void load_idtr(int limit, int addr); 29 | mov 4(%esp) ,%ax 30 | mov %ax,6(%esp) 31 | lidt 6(%esp) 32 | ret -------------------------------------------------------------------------------- /day7.5/kernel/entry.ld: -------------------------------------------------------------------------------- 1 | /* Simple linker script for the JOS kernel. 2 | See the GNU ld 'info' manual ("info ld") to learn the syntax. */ 3 | 4 | OUTPUT_FORMAT("elf32-i386", "elf32-i386", "elf32-i386") 5 | OUTPUT_ARCH(i386) 6 | ENTRY(_start) 7 | 8 | SECTIONS 9 | { 10 | /* Link the kernel at this address: "." means the current address */ 11 | . = 0xc400; 12 | 13 | /* AT(...) gives the load address of this section, which tells 14 | the boot loader where to load the kernel in physical memory */ 15 | .text : { 16 | *(.text .stub .text.* .gnu.linkonce.t.*) 17 | } 18 | .rodata : { 19 | *(.rodata .rodata.* .gnu.linkonce.r.*) 20 | } 21 | 22 | .data : { 23 | *(.data) 24 | } 25 | 26 | .bss : { 27 | *(.bss) 28 | } 29 | 30 | 31 | } 32 | -------------------------------------------------------------------------------- /day7.5/kernel/fifo.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ================================================== 3 | * 4 | * Filename: fifo.c 5 | * 6 | * Description: 7 | * 8 | * Version: 0.01 9 | * Created: 2014年03月06日 星期四 18时42分55秒 10 | * Author: ChrisZZ, zchrissirhcz@163.com 11 | * Company: ZJUT 12 | * 13 | * ================================================== 14 | */ 15 | #include 16 | #define FLAGS_OVERRUN 0x0001 17 | 18 | void fifo8_init(struct FIFO8* fifo, int size, unsigned char *buf){ 19 | fifo->size = size; 20 | fifo->buf = buf; 21 | fifo->free = size; 22 | fifo->flags = 0; 23 | fifo->p = 0; 24 | fifo->q = 0; 25 | return; 26 | } 27 | 28 | int fifo8_put(struct FIFO8* fifo, char data){ 29 | if(fifo->free==0){ 30 | fifo->flags |= FLAGS_OVERRUN; 31 | return -1; 32 | } 33 | fifo->buf[fifo->p] = data; 34 | fifo->p++; 35 | if(fifo->p == fifo->size){ 36 | fifo->p=0; 37 | } 38 | fifo->free--; 39 | return 0; 40 | } 41 | 42 | int fifo8_get(struct FIFO8* fifo){ 43 | int data; 44 | if(fifo->free==fifo->size){ 45 | return -1; 46 | } 47 | data = fifo->buf[fifo->q]; 48 | fifo->q++; 49 | if(fifo->q==fifo->size){ 50 | fifo->q=0; 51 | } 52 | fifo->free++; 53 | return data; 54 | } 55 | 56 | int fifo8_status(struct FIFO8* fifo){ 57 | return fifo->size-fifo->free; 58 | } 59 | -------------------------------------------------------------------------------- /day7.5/kernel/font.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /day7.5/kernel/font.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ================================================== 3 | * 4 | * Filename: font.h 5 | * 6 | * Description: 7 | * 8 | * Version: 0.01 9 | * Created: 2014年03月02日 星期日 12时27分31秒 10 | * Author: ChrisZZ, zchrissirhcz@163.com 11 | * Company: ZJUT 12 | * 13 | * ================================================== 14 | */ 15 | #ifndef font_header 16 | #define font_header 17 | 18 | extern unsigned short ASCII_Table[]; 19 | extern char hankaku[2048]; 20 | 21 | #endif 22 | -------------------------------------------------------------------------------- /day7.5/kernel/int.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define PORT_KEYDAT 0x0060 4 | 5 | 6 | struct KEYBUF keybuf; 7 | struct FIFO8 keyfifo; 8 | 9 | void init_pic(void){ 10 | outb(PIC0_IMR, 0xff); 11 | outb(PIC1_IMR, 0xff); 12 | 13 | outb(PIC0_ICW1, 0x11); 14 | outb(PIC0_ICW2, 0x20); 15 | outb(PIC0_ICW3, 1<<2); 16 | outb(PIC0_ICW4, 0x01); 17 | 18 | outb(PIC1_ICW1, 0x11); 19 | outb(PIC1_ICW2, 0x28); 20 | outb(PIC1_ICW3, 2); 21 | outb(PIC1_ICW4, 0x01); 22 | 23 | outb(PIC0_IMR, 0xfb); 24 | outb(PIC1_IMR, 0xff); 25 | 26 | return; 27 | } 28 | 29 | 30 | void inthandler21(int *esp){ 31 | /// struct BOOTINFO *binfo = (struct BOOTINFO*)ADR_BOOTINFO; 32 | // putfonts8_asc(binfo->vram, binfo->scrnx, 0, 16, COL8_000084, ""); 33 | /// boxfill8(binfo->vram, binfo->scrnx, COL8_000084, 0, 16, 31, 31); 34 | unsigned char data; 35 | ////char s[4]; 36 | outb(PIC0_OCW2, 0x61); 37 | data = inb(PORT_KEYDAT); 38 | /* 39 | if(keybuf.len<32){ 40 | keybuf.data[keybuf.next_w] = data; 41 | keybuf.len++; 42 | keybuf.next_w++; 43 | if(keybuf.next_w==32){ 44 | keybuf.next_w=0; 45 | } 46 | }*/ 47 | fifo8_put(&keyfifo, data); 48 | 49 | // sprintf(s, "%x", data); 50 | // boxfill8(binfo->vram, binfo->scrnx, COL8_000084, 0, 16, 15, 31); 51 | // putfonts8_asc(binfo->vram, binfo->scrnx, 0, 16, COL8_FFFFFF, s); 52 | return; 53 | } 54 | 55 | /* 56 | void inthandler21(int *esp){ 57 | char data; 58 | outb(PIC0_OCW2, 0x61); 59 | data = inb(PORT_KEYDAT); 60 | if(keybuf.next<32){ 61 | keybuf.data[keybuf.next] = data; 62 | keybuf.next++; 63 | } 64 | return; 65 | } 66 | */ 67 | /* 68 | void inthandler21(int *esp){ 69 | char data; 70 | outb(PIC0_OCW2, 0x61); 71 | data = inb(PORT_KEYDAT); 72 | if(keybuf.len < 32){ 73 | keybuf.data[keybuf.next_w] = data; 74 | keybuf.len++; 75 | keybuf.next_w++; 76 | if(keybuf.next_w==32){ 77 | keybuf.next_w = 0; 78 | } 79 | } 80 | return; 81 | } 82 | */ 83 | -------------------------------------------------------------------------------- /day7.5/kernel/log: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zchrissirhcz/osask-linux/a7afa7623205aaee125fc33676480d72c21e5323/day7.5/kernel/log -------------------------------------------------------------------------------- /day7.5/kernel/makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Makefile fragment for the JOS kernel. 3 | # This is NOT a complete makefile; 4 | # you must run GNU make in the top-level directory 5 | # where the GNUmakefile is located. 6 | # 7 | #entry.S main.c 8 | OBJCOPY := $(GCCPREFIX)objcopy 9 | OBJDUMP := $(GCCPREFIX)objdump 10 | OBJDIR=. 11 | entry_objs= entry.o 12 | ccode_objs=main.o screen.o font.o print.o idtgdt.o int.o asmint32.o fifo.o 13 | CFLAGS := $(CFLAGS) -O1 -fno-builtin -I$(OBJDIR) -MD 14 | #CFLAGS := $(CFLAGS) -O1 -fno-builtin -MD 15 | CFLAGS += -fno-omit-frame-pointer 16 | CFLAGS += -Wall -Wno-format -Wno-unused -gstabs -m32 -fno-stack-protector 17 | LDFLAGS = -m elf_i386 18 | 19 | %.o:%.c 20 | $(CC) -nostdinc $(CFLAGS) -Os -c -o $@ $< 21 | 22 | 23 | %.o:%.S 24 | $(CC) -nostdinc $(CFLAGS) -c -o $@ $< 25 | 26 | %.o:%.asm 27 | nasm -f elf -o $@ $< 28 | 29 | kernel:entry cobj 30 | cat cobj>>entry 31 | cp entry kernel 32 | 33 | entry:$(entry_objs) 34 | cobj:$(ccode_objs) 35 | 36 | 37 | 38 | 39 | entry: 40 | @echo "addr=$(addr)" 41 | $(LD) $(LDFLAGS) -N -e entry -Ttext 0xc400 -o $@.out $^ 42 | $(OBJDUMP) -S -D $@.out >$@.asm 43 | $(OBJCOPY) -S -O binary -j .text $@.out $@ 44 | 45 | cobj: 46 | @echo "addr=0x00" 47 | $(LD) $(LDFLAGS) -N -e bootmain -T kernel.ld -o $@.out $^ 48 | $(OBJDUMP) -S -D $@.out >$@.asm 49 | $(OBJCOPY) -S -O binary $@.out $@ 50 | 51 | 52 | clean: 53 | -rm -f *.d *.o *.out main.o kernel kernel.asm cobj* entry.o entry.asm entry 54 | 55 | run: 56 | # dd if=boot of=boot.img bs=512 count=1 57 | qemu -drive file=kernel,if=floppy 58 | -------------------------------------------------------------------------------- /day7.5/kernel/run: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | cd .. 3 | sudo make 4 | -------------------------------------------------------------------------------- /day7.5/makefile: -------------------------------------------------------------------------------- 1 | subdir=./kernel 2 | all:run 3 | 4 | asm: 5 | # nasm -o bootloader hello.nas 6 | nasm -o bootloader hello.nas 7 | @cd $(subdir);make clean;make addr=0xc400 8 | 9 | img:asm 10 | dd if=bootloader of=myos.img bs=512 count=1 11 | dd if=/dev/zero of=myos.img bs=512 seek=1 skip=1 count=2879 12 | 13 | copy:img 14 | mkdir -p /tmp/floppy 15 | mount -o loop myos.img /tmp/floppy -o fat=12 16 | sleep 1 17 | cp $(subdir)/kernel /tmp/floppy 18 | sleep 1 19 | umount /tmp/floppy 20 | run:copy 21 | qemu-system-i386 -drive file=myos.img,if=floppy 22 | clean: 23 | @rm -f bootloader myos.img haribote.bin 24 | -------------------------------------------------------------------------------- /day7.5/readme: -------------------------------------------------------------------------------- 1 | day7.5 2 | FIFO8结构体的定义,开始时候写成了char *buf,后来发现fifo_get的结果是0xxxxxxxx这样的,有很多前置x。 3 | 修正为unsigned后,只显示两位,成功! 4 | -------------------------------------------------------------------------------- /day7.7/kernel/asmint32.S: -------------------------------------------------------------------------------- 1 | .global asm_inthandler21 2 | .global asm_inthandler2c 3 | .global load_gdtr 4 | .global load_idtr 5 | 6 | .code32 7 | asm_inthandler21: 8 | pushw %es 9 | pushw %ds 10 | pushal 11 | movl %esp,%eax 12 | pushl %eax 13 | movw %ss,%ax 14 | movw %ax,%ds 15 | movw %ax,%es 16 | call inthandler21 17 | 18 | popl %eax 19 | popal 20 | popw %ds 21 | popW %es 22 | iretl 23 | 24 | asm_inthandler2c: 25 | pushw %es 26 | pushw %ds 27 | pushal 28 | movl %esp, %eax 29 | pushl %eax 30 | movw %ss, %ax 31 | movw %ax, %ds 32 | movw %ax, %es 33 | call inthandler2c 34 | 35 | popl %eax 36 | popal 37 | popw %ds 38 | popw %es 39 | iretl 40 | 41 | 42 | load_gdtr: #; void load_gdtr(int limit, int addr); 43 | mov 4(%esp) ,%ax 44 | mov %ax,6(%esp) 45 | lgdt 6(%esp) 46 | ret 47 | 48 | 49 | load_idtr: #; void load_idtr(int limit, int addr); 50 | mov 4(%esp) ,%ax 51 | mov %ax,6(%esp) 52 | lidt 6(%esp) 53 | ret 54 | -------------------------------------------------------------------------------- /day7.7/kernel/entry.ld: -------------------------------------------------------------------------------- 1 | /* Simple linker script for the JOS kernel. 2 | See the GNU ld 'info' manual ("info ld") to learn the syntax. */ 3 | 4 | OUTPUT_FORMAT("elf32-i386", "elf32-i386", "elf32-i386") 5 | OUTPUT_ARCH(i386) 6 | ENTRY(_start) 7 | 8 | SECTIONS 9 | { 10 | /* Link the kernel at this address: "." means the current address */ 11 | . = 0xc400; 12 | 13 | /* AT(...) gives the load address of this section, which tells 14 | the boot loader where to load the kernel in physical memory */ 15 | .text : { 16 | *(.text .stub .text.* .gnu.linkonce.t.*) 17 | } 18 | .rodata : { 19 | *(.rodata .rodata.* .gnu.linkonce.r.*) 20 | } 21 | 22 | .data : { 23 | *(.data) 24 | } 25 | 26 | .bss : { 27 | *(.bss) 28 | } 29 | 30 | 31 | } 32 | -------------------------------------------------------------------------------- /day7.7/kernel/fifo.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ================================================== 3 | * 4 | * Filename: fifo.c 5 | * 6 | * Description: 7 | * 8 | * Version: 0.01 9 | * Created: 2014年03月06日 星期四 18时42分55秒 10 | * Author: ChrisZZ, zchrissirhcz@163.com 11 | * Company: ZJUT 12 | * 13 | * ================================================== 14 | */ 15 | #include 16 | #define FLAGS_OVERRUN 0x0001 17 | 18 | void fifo8_init(struct FIFO8* fifo, int size, unsigned char *buf){ 19 | fifo->size = size; 20 | fifo->buf = buf; 21 | fifo->free = size; 22 | fifo->flags = 0; 23 | fifo->p = 0; 24 | fifo->q = 0; 25 | return; 26 | } 27 | 28 | int fifo8_put(struct FIFO8* fifo, char data){ 29 | if(fifo->free==0){ 30 | fifo->flags |= FLAGS_OVERRUN; 31 | return -1; 32 | } 33 | fifo->buf[fifo->p] = data; 34 | fifo->p++; 35 | if(fifo->p == fifo->size){ 36 | fifo->p=0; 37 | } 38 | fifo->free--; 39 | return 0; 40 | } 41 | 42 | int fifo8_get(struct FIFO8* fifo){ 43 | int data; 44 | if(fifo->free==fifo->size){ 45 | return -1; 46 | } 47 | data = fifo->buf[fifo->q]; 48 | fifo->q++; 49 | if(fifo->q==fifo->size){ 50 | fifo->q=0; 51 | } 52 | fifo->free++; 53 | return data; 54 | } 55 | 56 | int fifo8_status(struct FIFO8* fifo){ 57 | return fifo->size-fifo->free; 58 | } 59 | -------------------------------------------------------------------------------- /day7.7/kernel/font.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /day7.7/kernel/font.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ================================================== 3 | * 4 | * Filename: font.h 5 | * 6 | * Description: 7 | * 8 | * Version: 0.01 9 | * Created: 2014年03月02日 星期日 12时27分31秒 10 | * Author: ChrisZZ, zchrissirhcz@163.com 11 | * Company: ZJUT 12 | * 13 | * ================================================== 14 | */ 15 | #ifndef font_header 16 | #define font_header 17 | 18 | extern unsigned short ASCII_Table[]; 19 | extern char hankaku[2048]; 20 | 21 | #endif 22 | -------------------------------------------------------------------------------- /day7.7/kernel/kernel.ld: -------------------------------------------------------------------------------- 1 | /* Simple linker script for the JOS kernel. this file is for main.c 2 | See the GNU ld 'info' manual ("info ld") to learn the syntax. */ 3 | 4 | OUTPUT_FORMAT("elf32-i386", "elf32-i386", "elf32-i386") 5 | OUTPUT_ARCH(i386) 6 | ENTRY(_start) 7 | 8 | SECTIONS 9 | { 10 | /* Link the kernel at this address: "." means the current address */ 11 | . = 0x280000; 12 | .text : { 13 | *(.text) 14 | } 15 | .rodata : { 16 | *(.rodata) 17 | } 18 | 19 | .data : { 20 | *(.data) 21 | } 22 | 23 | .bss : { 24 | *(.bss) 25 | } 26 | 27 | 28 | } 29 | -------------------------------------------------------------------------------- /day7.7/kernel/log: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zchrissirhcz/osask-linux/a7afa7623205aaee125fc33676480d72c21e5323/day7.7/kernel/log -------------------------------------------------------------------------------- /day7.7/kernel/makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Makefile fragment for the JOS kernel. 3 | # This is NOT a complete makefile; 4 | # you must run GNU make in the top-level directory 5 | # where the GNUmakefile is located. 6 | # 7 | #entry.S main.c 8 | OBJCOPY := $(GCCPREFIX)objcopy 9 | OBJDUMP := $(GCCPREFIX)objdump 10 | OBJDIR=. 11 | entry_objs= entry.o 12 | ccode_objs=main.o screen.o font.o print.o idtgdt.o int.o asmint32.o fifo.o mouse.o 13 | CFLAGS := $(CFLAGS) -O1 -fno-builtin -I$(OBJDIR) -MD 14 | #CFLAGS := $(CFLAGS) -O1 -fno-builtin -MD 15 | CFLAGS += -fno-omit-frame-pointer 16 | CFLAGS += -Wall -Wno-format -Wno-unused -gstabs -m32 -fno-stack-protector 17 | LDFLAGS = -m elf_i386 18 | 19 | %.o:%.c 20 | $(CC) -nostdinc $(CFLAGS) -Os -c -o $@ $< 21 | 22 | 23 | %.o:%.S 24 | $(CC) -nostdinc $(CFLAGS) -c -o $@ $< 25 | 26 | %.o:%.asm 27 | nasm -f elf -o $@ $< 28 | 29 | kernel:entry cobj 30 | cat cobj>>entry 31 | cp entry kernel 32 | 33 | entry:$(entry_objs) 34 | cobj:$(ccode_objs) 35 | 36 | 37 | 38 | 39 | entry: 40 | @echo "addr=$(addr)" 41 | $(LD) $(LDFLAGS) -N -e entry -Ttext 0xc400 -o $@.out $^ 42 | $(OBJDUMP) -S -D $@.out >$@.asm 43 | $(OBJCOPY) -S -O binary -j .text $@.out $@ 44 | 45 | cobj: 46 | @echo "addr=0x00" 47 | $(LD) $(LDFLAGS) -N -e bootmain -T kernel.ld -o $@.out $^ 48 | $(OBJDUMP) -S -D $@.out >$@.asm 49 | $(OBJCOPY) -S -O binary $@.out $@ 50 | 51 | 52 | clean: 53 | -rm -f *.d *.o *.out main.o kernel kernel.asm cobj* entry.o entry.asm entry 54 | 55 | run: 56 | # dd if=boot of=boot.img bs=512 count=1 57 | qemu -drive file=kernel,if=floppy 58 | -------------------------------------------------------------------------------- /day7.7/kernel/mouse.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ================================================== 3 | * 4 | * Filename: mouse.c 5 | * 6 | * Description: 7 | * 8 | * Version: 0.01 9 | * Created: 2014年03月07日 星期五 09时51分45秒 10 | * Author: ChrisZZ, zchrissirhcz@163.com 11 | * Company: ZJUT 12 | * 13 | * ================================================== 14 | */ 15 | #include 16 | void wait_KBC_sendready(void){ 17 | for(;;){ 18 | if((inb(PORT_KEYSTA) & KEYSTA_SEND_NOTREADY)==0){ 19 | break; 20 | } 21 | } 22 | return; 23 | } 24 | 25 | void init_keyboard(void){ 26 | wait_KBC_sendready(); 27 | outb(PORT_KEYCMD, KEYCMD_WRITE_MODE); 28 | wait_KBC_sendready(); 29 | outb(PORT_KEYDAT, KBC_MODE); 30 | return; 31 | } 32 | 33 | void enable_mouse(void){ 34 | wait_KBC_sendready(); 35 | outb(PORT_KEYCMD, KEYCMD_SENDTO_MOUSE); 36 | wait_KBC_sendready(); 37 | outb(PORT_KEYDAT, MOUSECMD_ENABLE); 38 | return; 39 | } 40 | -------------------------------------------------------------------------------- /day7.7/kernel/run: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | cd .. 3 | sudo make 4 | -------------------------------------------------------------------------------- /day7.7/makefile: -------------------------------------------------------------------------------- 1 | subdir=./kernel 2 | all:run 3 | 4 | asm: 5 | # nasm -o bootloader hello.nas 6 | nasm -o bootloader hello.nas 7 | @cd $(subdir);make clean;make addr=0xc400 8 | 9 | img:asm 10 | dd if=bootloader of=myos.img bs=512 count=1 11 | dd if=/dev/zero of=myos.img bs=512 seek=1 skip=1 count=2879 12 | 13 | copy:img 14 | mkdir -p /tmp/floppy 15 | mount -o loop myos.img /tmp/floppy -o fat=12 16 | sleep 1 17 | cp $(subdir)/kernel /tmp/floppy 18 | sleep 1 19 | umount /tmp/floppy 20 | run:copy 21 | qemu-system-i386 -drive file=myos.img,if=floppy 22 | clean: 23 | @rm -f bootloader myos.img haribote.bin 24 | -------------------------------------------------------------------------------- /day7.7/readme: -------------------------------------------------------------------------------- 1 | day7.7 2 | day7.6和day7.7鼠标的中断试水,和键盘中断类似。 3 | 在main.c中注意要给鼠标初始化,否则无法使用。 4 | 5 | 我的最终运行结果是,鼠标只能显示一个字符,也就是0。感觉川合秀实的书上似乎是鼠标都能动了,但是对比了我的代码和川合的代码,没有找到问题。 6 | 还有就是我使用的sprintf,是米油自己写的,感觉功能太弱了,书上的sprintf(s, "%02X", i)这种根本就不能正确处理,只能先舍弃格式,全部显示了: 7 | sprintf(s, "%x", i) 8 | -------------------------------------------------------------------------------- /day8.2/kernel/asmint32.S: -------------------------------------------------------------------------------- 1 | .global asm_inthandler21 2 | .global asm_inthandler2c 3 | .global load_gdtr 4 | .global load_idtr 5 | 6 | .code32 7 | asm_inthandler21: 8 | pushw %es 9 | pushw %ds 10 | pushal 11 | movl %esp,%eax 12 | pushl %eax 13 | movw %ss,%ax 14 | movw %ax,%ds 15 | movw %ax,%es 16 | call inthandler21 17 | 18 | popl %eax 19 | popal 20 | popw %ds 21 | popW %es 22 | iretl 23 | 24 | asm_inthandler2c: 25 | pushw %es 26 | pushw %ds 27 | pushal 28 | movl %esp, %eax 29 | pushl %eax 30 | movw %ss, %ax 31 | movw %ax, %ds 32 | movw %ax, %es 33 | call inthandler2c 34 | 35 | popl %eax 36 | popal 37 | popw %ds 38 | popw %es 39 | iretl 40 | 41 | 42 | load_gdtr: #; void load_gdtr(int limit, int addr); 43 | mov 4(%esp) ,%ax 44 | mov %ax,6(%esp) 45 | lgdt 6(%esp) 46 | ret 47 | 48 | 49 | load_idtr: #; void load_idtr(int limit, int addr); 50 | mov 4(%esp) ,%ax 51 | mov %ax,6(%esp) 52 | lidt 6(%esp) 53 | ret 54 | -------------------------------------------------------------------------------- /day8.2/kernel/entry.ld: -------------------------------------------------------------------------------- 1 | /* Simple linker script for the JOS kernel. 2 | See the GNU ld 'info' manual ("info ld") to learn the syntax. */ 3 | 4 | OUTPUT_FORMAT("elf32-i386", "elf32-i386", "elf32-i386") 5 | OUTPUT_ARCH(i386) 6 | ENTRY(_start) 7 | 8 | SECTIONS 9 | { 10 | /* Link the kernel at this address: "." means the current address */ 11 | . = 0xc400; 12 | 13 | /* AT(...) gives the load address of this section, which tells 14 | the boot loader where to load the kernel in physical memory */ 15 | .text : { 16 | *(.text .stub .text.* .gnu.linkonce.t.*) 17 | } 18 | .rodata : { 19 | *(.rodata .rodata.* .gnu.linkonce.r.*) 20 | } 21 | 22 | .data : { 23 | *(.data) 24 | } 25 | 26 | .bss : { 27 | *(.bss) 28 | } 29 | 30 | 31 | } 32 | -------------------------------------------------------------------------------- /day8.2/kernel/fifo.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ================================================== 3 | * 4 | * Filename: fifo.c 5 | * 6 | * Description: 7 | * 8 | * Version: 0.01 9 | * Created: 2014年03月06日 星期四 18时42分55秒 10 | * Author: ChrisZZ, zchrissirhcz@163.com 11 | * Company: ZJUT 12 | * 13 | * ================================================== 14 | */ 15 | #include 16 | #define FLAGS_OVERRUN 0x0001 17 | 18 | void fifo8_init(struct FIFO8* fifo, int size, unsigned char *buf){ 19 | fifo->size = size; 20 | fifo->buf = buf; 21 | fifo->free = size; 22 | fifo->flags = 0; 23 | fifo->p = 0; 24 | fifo->q = 0; 25 | return; 26 | } 27 | 28 | int fifo8_put(struct FIFO8* fifo, char data){ 29 | if(fifo->free==0){ 30 | fifo->flags |= FLAGS_OVERRUN; 31 | return -1; 32 | } 33 | fifo->buf[fifo->p] = data; 34 | fifo->p++; 35 | if(fifo->p == fifo->size){ 36 | fifo->p=0; 37 | } 38 | fifo->free--; 39 | return 0; 40 | } 41 | 42 | int fifo8_get(struct FIFO8* fifo){ 43 | int data; 44 | if(fifo->free==fifo->size){ 45 | return -1; 46 | } 47 | data = fifo->buf[fifo->q]; 48 | fifo->q++; 49 | if(fifo->q==fifo->size){ 50 | fifo->q=0; 51 | } 52 | fifo->free++; 53 | return data; 54 | } 55 | 56 | int fifo8_status(struct FIFO8* fifo){ 57 | return fifo->size-fifo->free; 58 | } 59 | -------------------------------------------------------------------------------- /day8.2/kernel/font.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /day8.2/kernel/font.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ================================================== 3 | * 4 | * Filename: font.h 5 | * 6 | * Description: 7 | * 8 | * Version: 0.01 9 | * Created: 2014年03月02日 星期日 12时27分31秒 10 | * Author: ChrisZZ, zchrissirhcz@163.com 11 | * Company: ZJUT 12 | * 13 | * ================================================== 14 | */ 15 | #ifndef font_header 16 | #define font_header 17 | 18 | extern unsigned short ASCII_Table[]; 19 | extern char hankaku[2048]; 20 | 21 | #endif 22 | -------------------------------------------------------------------------------- /day8.2/kernel/kernel.ld: -------------------------------------------------------------------------------- 1 | /* Simple linker script for the JOS kernel. this file is for main.c 2 | See the GNU ld 'info' manual ("info ld") to learn the syntax. */ 3 | 4 | OUTPUT_FORMAT("elf32-i386", "elf32-i386", "elf32-i386") 5 | OUTPUT_ARCH(i386) 6 | ENTRY(_start) 7 | 8 | SECTIONS 9 | { 10 | /* Link the kernel at this address: "." means the current address */ 11 | . = 0x280000; 12 | .text : { 13 | *(.text) 14 | } 15 | .rodata : { 16 | *(.rodata) 17 | } 18 | 19 | .data : { 20 | *(.data) 21 | } 22 | 23 | .bss : { 24 | *(.bss) 25 | } 26 | 27 | 28 | } 29 | -------------------------------------------------------------------------------- /day8.2/kernel/log: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zchrissirhcz/osask-linux/a7afa7623205aaee125fc33676480d72c21e5323/day8.2/kernel/log -------------------------------------------------------------------------------- /day8.2/kernel/makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Makefile fragment for the JOS kernel. 3 | # This is NOT a complete makefile; 4 | # you must run GNU make in the top-level directory 5 | # where the GNUmakefile is located. 6 | # 7 | #entry.S main.c 8 | OBJCOPY := $(GCCPREFIX)objcopy 9 | OBJDUMP := $(GCCPREFIX)objdump 10 | OBJDIR=. 11 | entry_objs= entry.o 12 | ccode_objs=main.o screen.o font.o print.o idtgdt.o int.o asmint32.o fifo.o mouse.o 13 | CFLAGS := $(CFLAGS) -O1 -fno-builtin -I$(OBJDIR) -MD 14 | #CFLAGS := $(CFLAGS) -O1 -fno-builtin -MD 15 | CFLAGS += -fno-omit-frame-pointer 16 | CFLAGS += -Wall -Wno-format -Wno-unused -gstabs -m32 -fno-stack-protector 17 | LDFLAGS = -m elf_i386 18 | 19 | %.o:%.c 20 | $(CC) -nostdinc $(CFLAGS) -Os -c -o $@ $< 21 | 22 | 23 | %.o:%.S 24 | $(CC) -nostdinc $(CFLAGS) -c -o $@ $< 25 | 26 | %.o:%.asm 27 | nasm -f elf -o $@ $< 28 | 29 | kernel:entry cobj 30 | cat cobj>>entry 31 | cp entry kernel 32 | 33 | entry:$(entry_objs) 34 | cobj:$(ccode_objs) 35 | 36 | 37 | 38 | 39 | entry: 40 | @echo "addr=$(addr)" 41 | $(LD) $(LDFLAGS) -N -e entry -Ttext 0xc400 -o $@.out $^ 42 | $(OBJDUMP) -S -D $@.out >$@.asm 43 | $(OBJCOPY) -S -O binary -j .text $@.out $@ 44 | 45 | cobj: 46 | @echo "addr=0x00" 47 | $(LD) $(LDFLAGS) -N -e bootmain -T kernel.ld -o $@.out $^ 48 | $(OBJDUMP) -S -D $@.out >$@.asm 49 | $(OBJCOPY) -S -O binary $@.out $@ 50 | 51 | 52 | clean: 53 | -rm -f *.d *.o *.out main.o kernel kernel.asm cobj* entry.o entry.asm entry 54 | 55 | run: 56 | # dd if=boot of=boot.img bs=512 count=1 57 | qemu -drive file=kernel,if=floppy 58 | -------------------------------------------------------------------------------- /day8.2/kernel/mouse.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ================================================== 3 | * 4 | * Filename: mouse.c 5 | * 6 | * Description: 7 | * 8 | * Version: 0.01 9 | * Created: 2014年03月07日 星期五 09时51分45秒 10 | * Author: ChrisZZ, zchrissirhcz@163.com 11 | * Company: ZJUT 12 | * 13 | * ================================================== 14 | */ 15 | #include 16 | void wait_KBC_sendready(void){ 17 | for(;;){ 18 | if((inb(PORT_KEYSTA) & KEYSTA_SEND_NOTREADY)==0){ 19 | break; 20 | } 21 | } 22 | return; 23 | } 24 | 25 | void init_keyboard(void){ 26 | wait_KBC_sendready(); 27 | outb(PORT_KEYCMD, KEYCMD_WRITE_MODE); 28 | wait_KBC_sendready(); 29 | outb(PORT_KEYDAT, KBC_MODE); 30 | return; 31 | } 32 | 33 | /* 34 | void enable_mouse(void){ 35 | wait_KBC_sendready(); 36 | outb(PORT_KEYCMD, KEYCMD_SENDTO_MOUSE); 37 | wait_KBC_sendready(); 38 | outb(PORT_KEYDAT, MOUSECMD_ENABLE); 39 | return; 40 | } 41 | */ 42 | void enable_mouse(struct MOUSE_DEC *mdec) { 43 | wait_KBC_sendready(); 44 | outb(PORT_KEYCMD, KEYCMD_SENDTO_MOUSE); 45 | wait_KBC_sendready(); 46 | outb(PORT_KEYDAT, MOUSECMD_ENABLE); 47 | mdec->phase = 0; 48 | return; 49 | } 50 | 51 | int mouse_decode(struct MOUSE_DEC *mdec, unsigned char dat){ 52 | if(mdec->phase==0){ 53 | if(dat==0xfa){ 54 | mdec->phase=1; 55 | } 56 | return 0; 57 | } 58 | if(mdec->phase==1){ 59 | mdec->buf[0]=dat; 60 | mdec->phase=2; 61 | return 0; 62 | } 63 | if(mdec->phase==2){ 64 | mdec->buf[1]=dat; 65 | mdec->phase=3; 66 | return 0; 67 | } 68 | if(mdec->phase==3){ 69 | mdec->buf[2]=dat; 70 | mdec->phase=1; 71 | return 1; 72 | } 73 | return -1; 74 | } 75 | -------------------------------------------------------------------------------- /day8.2/kernel/run: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | cd .. 3 | sudo make 4 | -------------------------------------------------------------------------------- /day8.2/makefile: -------------------------------------------------------------------------------- 1 | subdir=./kernel 2 | all:run 3 | 4 | asm: 5 | # nasm -o bootloader hello.nas 6 | nasm -o bootloader hello.nas 7 | @cd $(subdir);make clean;make addr=0xc400 8 | 9 | img:asm 10 | dd if=bootloader of=myos.img bs=512 count=1 11 | dd if=/dev/zero of=myos.img bs=512 seek=1 skip=1 count=2879 12 | 13 | copy:img 14 | mkdir -p /tmp/floppy 15 | mount -o loop myos.img /tmp/floppy -o fat=12 16 | sleep 1 17 | cp $(subdir)/kernel /tmp/floppy 18 | sleep 1 19 | umount /tmp/floppy 20 | run:copy 21 | qemu-system-i386 -drive file=myos.img,if=floppy 22 | clean: 23 | @rm -f bootloader myos.img haribote.bin 24 | -------------------------------------------------------------------------------- /day8.2/readme: -------------------------------------------------------------------------------- 1 | day8.1 2 | 根据书上的代码,调用鼠标的中断。会显示三个数字,分别表示滑动,左右移动,上下移动。 3 | 不过因为sprintf函数有问题,只有第一个数字能够正常显示,其他两个数字都是dead 4 | -------------------------------------------------------------------------------- /day8.3/kernel/asmint32.S: -------------------------------------------------------------------------------- 1 | .global asm_inthandler21 2 | .global asm_inthandler2c 3 | .global load_gdtr 4 | .global load_idtr 5 | 6 | .code32 7 | asm_inthandler21: 8 | pushw %es 9 | pushw %ds 10 | pushal 11 | movl %esp,%eax 12 | pushl %eax 13 | movw %ss,%ax 14 | movw %ax,%ds 15 | movw %ax,%es 16 | call inthandler21 17 | 18 | popl %eax 19 | popal 20 | popw %ds 21 | popW %es 22 | iretl 23 | 24 | asm_inthandler2c: 25 | pushw %es 26 | pushw %ds 27 | pushal 28 | movl %esp, %eax 29 | pushl %eax 30 | movw %ss, %ax 31 | movw %ax, %ds 32 | movw %ax, %es 33 | call inthandler2c 34 | 35 | popl %eax 36 | popal 37 | popw %ds 38 | popw %es 39 | iretl 40 | 41 | 42 | load_gdtr: #; void load_gdtr(int limit, int addr); 43 | mov 4(%esp) ,%ax 44 | mov %ax,6(%esp) 45 | lgdt 6(%esp) 46 | ret 47 | 48 | 49 | load_idtr: #; void load_idtr(int limit, int addr); 50 | mov 4(%esp) ,%ax 51 | mov %ax,6(%esp) 52 | lidt 6(%esp) 53 | ret 54 | -------------------------------------------------------------------------------- /day8.3/kernel/entry.ld: -------------------------------------------------------------------------------- 1 | /* Simple linker script for the JOS kernel. 2 | See the GNU ld 'info' manual ("info ld") to learn the syntax. */ 3 | 4 | OUTPUT_FORMAT("elf32-i386", "elf32-i386", "elf32-i386") 5 | OUTPUT_ARCH(i386) 6 | ENTRY(_start) 7 | 8 | SECTIONS 9 | { 10 | /* Link the kernel at this address: "." means the current address */ 11 | . = 0xc400; 12 | 13 | /* AT(...) gives the load address of this section, which tells 14 | the boot loader where to load the kernel in physical memory */ 15 | .text : { 16 | *(.text .stub .text.* .gnu.linkonce.t.*) 17 | } 18 | .rodata : { 19 | *(.rodata .rodata.* .gnu.linkonce.r.*) 20 | } 21 | 22 | .data : { 23 | *(.data) 24 | } 25 | 26 | .bss : { 27 | *(.bss) 28 | } 29 | 30 | 31 | } 32 | -------------------------------------------------------------------------------- /day8.3/kernel/fifo.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ================================================== 3 | * 4 | * Filename: fifo.c 5 | * 6 | * Description: 7 | * 8 | * Version: 0.01 9 | * Created: 2014年03月06日 星期四 18时42分55秒 10 | * Author: ChrisZZ, zchrissirhcz@163.com 11 | * Company: ZJUT 12 | * 13 | * ================================================== 14 | */ 15 | #include 16 | #define FLAGS_OVERRUN 0x0001 17 | 18 | void fifo8_init(struct FIFO8* fifo, int size, unsigned char *buf){ 19 | fifo->size = size; 20 | fifo->buf = buf; 21 | fifo->free = size; 22 | fifo->flags = 0; 23 | fifo->p = 0; 24 | fifo->q = 0; 25 | return; 26 | } 27 | 28 | int fifo8_put(struct FIFO8* fifo, char data){ 29 | if(fifo->free==0){ 30 | fifo->flags |= FLAGS_OVERRUN; 31 | return -1; 32 | } 33 | fifo->buf[fifo->p] = data; 34 | fifo->p++; 35 | if(fifo->p == fifo->size){ 36 | fifo->p=0; 37 | } 38 | fifo->free--; 39 | return 0; 40 | } 41 | 42 | int fifo8_get(struct FIFO8* fifo){ 43 | int data; 44 | if(fifo->free==fifo->size){ 45 | return -1; 46 | } 47 | data = fifo->buf[fifo->q]; 48 | fifo->q++; 49 | if(fifo->q==fifo->size){ 50 | fifo->q=0; 51 | } 52 | fifo->free++; 53 | return data; 54 | } 55 | 56 | int fifo8_status(struct FIFO8* fifo){ 57 | return fifo->size-fifo->free; 58 | } 59 | -------------------------------------------------------------------------------- /day8.3/kernel/font.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /day8.3/kernel/font.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ================================================== 3 | * 4 | * Filename: font.h 5 | * 6 | * Description: 7 | * 8 | * Version: 0.01 9 | * Created: 2014年03月02日 星期日 12时27分31秒 10 | * Author: ChrisZZ, zchrissirhcz@163.com 11 | * Company: ZJUT 12 | * 13 | * ================================================== 14 | */ 15 | #ifndef font_header 16 | #define font_header 17 | 18 | extern unsigned short ASCII_Table[]; 19 | extern char hankaku[2048]; 20 | 21 | #endif 22 | -------------------------------------------------------------------------------- /day8.3/kernel/kernel.ld: -------------------------------------------------------------------------------- 1 | /* Simple linker script for the JOS kernel. this file is for main.c 2 | See the GNU ld 'info' manual ("info ld") to learn the syntax. */ 3 | 4 | OUTPUT_FORMAT("elf32-i386", "elf32-i386", "elf32-i386") 5 | OUTPUT_ARCH(i386) 6 | ENTRY(_start) 7 | 8 | SECTIONS 9 | { 10 | /* Link the kernel at this address: "." means the current address */ 11 | . = 0x280000; 12 | .text : { 13 | *(.text) 14 | } 15 | .rodata : { 16 | *(.rodata) 17 | } 18 | 19 | .data : { 20 | *(.data) 21 | } 22 | 23 | .bss : { 24 | *(.bss) 25 | } 26 | 27 | 28 | } 29 | -------------------------------------------------------------------------------- /day8.3/kernel/log: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zchrissirhcz/osask-linux/a7afa7623205aaee125fc33676480d72c21e5323/day8.3/kernel/log -------------------------------------------------------------------------------- /day8.3/kernel/makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Makefile fragment for the JOS kernel. 3 | # This is NOT a complete makefile; 4 | # you must run GNU make in the top-level directory 5 | # where the GNUmakefile is located. 6 | # 7 | #entry.S main.c 8 | OBJCOPY := $(GCCPREFIX)objcopy 9 | OBJDUMP := $(GCCPREFIX)objdump 10 | OBJDIR=. 11 | entry_objs= entry.o 12 | ccode_objs=main.o screen.o font.o print.o idtgdt.o int.o asmint32.o fifo.o mouse.o 13 | CFLAGS := $(CFLAGS) -O1 -fno-builtin -I$(OBJDIR) -MD 14 | #CFLAGS := $(CFLAGS) -O1 -fno-builtin -MD 15 | CFLAGS += -fno-omit-frame-pointer 16 | CFLAGS += -Wall -Wno-format -Wno-unused -gstabs -m32 -fno-stack-protector 17 | LDFLAGS = -m elf_i386 18 | 19 | %.o:%.c 20 | $(CC) -nostdinc $(CFLAGS) -Os -c -o $@ $< 21 | 22 | 23 | %.o:%.S 24 | $(CC) -nostdinc $(CFLAGS) -c -o $@ $< 25 | 26 | %.o:%.asm 27 | nasm -f elf -o $@ $< 28 | 29 | kernel:entry cobj 30 | cat cobj>>entry 31 | cp entry kernel 32 | 33 | entry:$(entry_objs) 34 | cobj:$(ccode_objs) 35 | 36 | 37 | 38 | 39 | entry: 40 | @echo "addr=$(addr)" 41 | $(LD) $(LDFLAGS) -N -e entry -Ttext 0xc400 -o $@.out $^ 42 | $(OBJDUMP) -S -D $@.out >$@.asm 43 | $(OBJCOPY) -S -O binary -j .text $@.out $@ 44 | 45 | cobj: 46 | @echo "addr=0x00" 47 | $(LD) $(LDFLAGS) -N -e bootmain -T kernel.ld -o $@.out $^ 48 | $(OBJDUMP) -S -D $@.out >$@.asm 49 | $(OBJCOPY) -S -O binary $@.out $@ 50 | 51 | 52 | clean: 53 | -rm -f *.d *.o *.out main.o kernel kernel.asm cobj* entry.o entry.asm entry 54 | 55 | run: 56 | # dd if=boot of=boot.img bs=512 count=1 57 | qemu -drive file=kernel,if=floppy 58 | -------------------------------------------------------------------------------- /day8.3/kernel/mouse.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ================================================== 3 | * 4 | * Filename: mouse.c 5 | * 6 | * Description: 7 | * 8 | * Version: 0.01 9 | * Created: 2014年03月07日 星期五 09时51分45秒 10 | * Author: ChrisZZ, zchrissirhcz@163.com 11 | * Company: ZJUT 12 | * 13 | * ================================================== 14 | */ 15 | #include 16 | void wait_KBC_sendready(void){ 17 | for(;;){ 18 | if((inb(PORT_KEYSTA) & KEYSTA_SEND_NOTREADY)==0){ 19 | break; 20 | } 21 | } 22 | return; 23 | } 24 | 25 | void init_keyboard(void){ 26 | wait_KBC_sendready(); 27 | outb(PORT_KEYCMD, KEYCMD_WRITE_MODE); 28 | wait_KBC_sendready(); 29 | outb(PORT_KEYDAT, KBC_MODE); 30 | return; 31 | } 32 | 33 | /* 34 | void enable_mouse(void){ 35 | wait_KBC_sendready(); 36 | outb(PORT_KEYCMD, KEYCMD_SENDTO_MOUSE); 37 | wait_KBC_sendready(); 38 | outb(PORT_KEYDAT, MOUSECMD_ENABLE); 39 | return; 40 | } 41 | */ 42 | void enable_mouse(struct MOUSE_DEC *mdec) { 43 | wait_KBC_sendready(); 44 | outb(PORT_KEYCMD, KEYCMD_SENDTO_MOUSE); 45 | wait_KBC_sendready(); 46 | outb(PORT_KEYDAT, MOUSECMD_ENABLE); 47 | mdec->phase = 0; 48 | return; 49 | } 50 | 51 | int mouse_decode(struct MOUSE_DEC *mdec, unsigned char dat){ 52 | if(mdec->phase==0){ 53 | if(dat==0xfa){ 54 | mdec->phase=1; 55 | } 56 | return 0; 57 | } 58 | if(mdec->phase==1){ 59 | if((dat&0xc8)==0x08){ 60 | mdec->buf[0]=dat; 61 | mdec->phase=2; 62 | } 63 | return 0; 64 | } 65 | if(mdec->phase==2){ 66 | mdec->buf[1]=dat; 67 | mdec->phase=3; 68 | return 0; 69 | } 70 | if(mdec->phase==3){ 71 | mdec->buf[2]=dat; 72 | mdec->phase=1; 73 | mdec->btn=mdec->buf[0]&0x07; 74 | mdec->x=mdec->buf[1]; 75 | 76 | mdec->y=mdec->buf[2]; 77 | if((mdec->buf[0]&0x10)!=0){ 78 | mdec->x |= 0xffffff00; 79 | } 80 | if((mdec->buf[0]&0x20)!=0){ 81 | mdec->y |= 0xffffff00; 82 | } 83 | mdec->y = - mdec->y; 84 | return 1; 85 | } 86 | return -1; 87 | } 88 | -------------------------------------------------------------------------------- /day8.3/kernel/run: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | cd .. 3 | sudo make 4 | -------------------------------------------------------------------------------- /day8.3/makefile: -------------------------------------------------------------------------------- 1 | subdir=./kernel 2 | all:run 3 | 4 | asm: 5 | # nasm -o bootloader hello.nas 6 | nasm -o bootloader hello.nas 7 | @cd $(subdir);make clean;make addr=0xc400 8 | 9 | img:asm 10 | dd if=bootloader of=myos.img bs=512 count=1 11 | dd if=/dev/zero of=myos.img bs=512 seek=1 skip=1 count=2879 12 | 13 | copy:img 14 | mkdir -p /tmp/floppy 15 | mount -o loop myos.img /tmp/floppy -o fat=12 16 | sleep 1 17 | cp $(subdir)/kernel /tmp/floppy 18 | sleep 1 19 | umount /tmp/floppy 20 | run:copy 21 | qemu-system-i386 -drive file=myos.img,if=floppy 22 | clean: 23 | @rm -f bootloader myos.img haribote.bin 24 | -------------------------------------------------------------------------------- /day8.3/readme: -------------------------------------------------------------------------------- 1 | day8.3 2 | 这部分的效果是,鼠标左键、右键、滑轮按下后,调用相应的中断,并在屏幕上显示出来:左键用大写L,右键用大写R,滑轮用大写C,如果没有按下那么就显示小写的字母。 3 | 在main.c函数中,川合秀实用的是&的操作来判断鼠标的按键情况。。不过这里不能正常运行。去掉&就ok了。。 4 | -------------------------------------------------------------------------------- /day8.4/kernel/asmint32.S: -------------------------------------------------------------------------------- 1 | .global asm_inthandler21 2 | .global asm_inthandler2c 3 | .global load_gdtr 4 | .global load_idtr 5 | 6 | .code32 7 | asm_inthandler21: 8 | pushw %es 9 | pushw %ds 10 | pushal 11 | movl %esp,%eax 12 | pushl %eax 13 | movw %ss,%ax 14 | movw %ax,%ds 15 | movw %ax,%es 16 | call inthandler21 17 | 18 | popl %eax 19 | popal 20 | popw %ds 21 | popW %es 22 | iretl 23 | 24 | asm_inthandler2c: 25 | pushw %es 26 | pushw %ds 27 | pushal 28 | movl %esp, %eax 29 | pushl %eax 30 | movw %ss, %ax 31 | movw %ax, %ds 32 | movw %ax, %es 33 | call inthandler2c 34 | 35 | popl %eax 36 | popal 37 | popw %ds 38 | popw %es 39 | iretl 40 | 41 | 42 | load_gdtr: #; void load_gdtr(int limit, int addr); 43 | mov 4(%esp) ,%ax 44 | mov %ax,6(%esp) 45 | lgdt 6(%esp) 46 | ret 47 | 48 | 49 | load_idtr: #; void load_idtr(int limit, int addr); 50 | mov 4(%esp) ,%ax 51 | mov %ax,6(%esp) 52 | lidt 6(%esp) 53 | ret 54 | -------------------------------------------------------------------------------- /day8.4/kernel/entry.ld: -------------------------------------------------------------------------------- 1 | /* Simple linker script for the JOS kernel. 2 | See the GNU ld 'info' manual ("info ld") to learn the syntax. */ 3 | 4 | OUTPUT_FORMAT("elf32-i386", "elf32-i386", "elf32-i386") 5 | OUTPUT_ARCH(i386) 6 | ENTRY(_start) 7 | 8 | SECTIONS 9 | { 10 | /* Link the kernel at this address: "." means the current address */ 11 | . = 0xc400; 12 | 13 | /* AT(...) gives the load address of this section, which tells 14 | the boot loader where to load the kernel in physical memory */ 15 | .text : { 16 | *(.text .stub .text.* .gnu.linkonce.t.*) 17 | } 18 | .rodata : { 19 | *(.rodata .rodata.* .gnu.linkonce.r.*) 20 | } 21 | 22 | .data : { 23 | *(.data) 24 | } 25 | 26 | .bss : { 27 | *(.bss) 28 | } 29 | 30 | 31 | } 32 | -------------------------------------------------------------------------------- /day8.4/kernel/fifo.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ================================================== 3 | * 4 | * Filename: fifo.c 5 | * 6 | * Description: 7 | * 8 | * Version: 0.01 9 | * Created: 2014年03月06日 星期四 18时42分55秒 10 | * Author: ChrisZZ, zchrissirhcz@163.com 11 | * Company: ZJUT 12 | * 13 | * ================================================== 14 | */ 15 | #include 16 | #define FLAGS_OVERRUN 0x0001 17 | 18 | void fifo8_init(struct FIFO8* fifo, int size, unsigned char *buf){ 19 | fifo->size = size; 20 | fifo->buf = buf; 21 | fifo->free = size; 22 | fifo->flags = 0; 23 | fifo->p = 0; 24 | fifo->q = 0; 25 | return; 26 | } 27 | 28 | int fifo8_put(struct FIFO8* fifo, char data){ 29 | if(fifo->free==0){ 30 | fifo->flags |= FLAGS_OVERRUN; 31 | return -1; 32 | } 33 | fifo->buf[fifo->p] = data; 34 | fifo->p++; 35 | if(fifo->p == fifo->size){ 36 | fifo->p=0; 37 | } 38 | fifo->free--; 39 | return 0; 40 | } 41 | 42 | int fifo8_get(struct FIFO8* fifo){ 43 | int data; 44 | if(fifo->free==fifo->size){ 45 | return -1; 46 | } 47 | data = fifo->buf[fifo->q]; 48 | fifo->q++; 49 | if(fifo->q==fifo->size){ 50 | fifo->q=0; 51 | } 52 | fifo->free++; 53 | return data; 54 | } 55 | 56 | int fifo8_status(struct FIFO8* fifo){ 57 | return fifo->size-fifo->free; 58 | } 59 | -------------------------------------------------------------------------------- /day8.4/kernel/font.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /day8.4/kernel/font.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ================================================== 3 | * 4 | * Filename: font.h 5 | * 6 | * Description: 7 | * 8 | * Version: 0.01 9 | * Created: 2014年03月02日 星期日 12时27分31秒 10 | * Author: ChrisZZ, zchrissirhcz@163.com 11 | * Company: ZJUT 12 | * 13 | * ================================================== 14 | */ 15 | #ifndef font_header 16 | #define font_header 17 | 18 | extern unsigned short ASCII_Table[]; 19 | extern char hankaku[2048]; 20 | 21 | #endif 22 | -------------------------------------------------------------------------------- /day8.4/kernel/kernel.ld: -------------------------------------------------------------------------------- 1 | /* Simple linker script for the JOS kernel. this file is for main.c 2 | See the GNU ld 'info' manual ("info ld") to learn the syntax. */ 3 | 4 | OUTPUT_FORMAT("elf32-i386", "elf32-i386", "elf32-i386") 5 | OUTPUT_ARCH(i386) 6 | ENTRY(_start) 7 | 8 | SECTIONS 9 | { 10 | /* Link the kernel at this address: "." means the current address */ 11 | . = 0x280000; 12 | .text : { 13 | *(.text) 14 | } 15 | .rodata : { 16 | *(.rodata) 17 | } 18 | 19 | .data : { 20 | *(.data) 21 | } 22 | 23 | .bss : { 24 | *(.bss) 25 | } 26 | 27 | 28 | } 29 | -------------------------------------------------------------------------------- /day8.4/kernel/log: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zchrissirhcz/osask-linux/a7afa7623205aaee125fc33676480d72c21e5323/day8.4/kernel/log -------------------------------------------------------------------------------- /day8.4/kernel/makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Makefile fragment for the JOS kernel. 3 | # This is NOT a complete makefile; 4 | # you must run GNU make in the top-level directory 5 | # where the GNUmakefile is located. 6 | # 7 | #entry.S main.c 8 | OBJCOPY := $(GCCPREFIX)objcopy 9 | OBJDUMP := $(GCCPREFIX)objdump 10 | OBJDIR=. 11 | entry_objs= entry.o 12 | ccode_objs=main.o screen.o font.o print.o idtgdt.o int.o asmint32.o fifo.o mouse.o 13 | CFLAGS := $(CFLAGS) -O1 -fno-builtin -I$(OBJDIR) -MD 14 | #CFLAGS := $(CFLAGS) -O1 -fno-builtin -MD 15 | CFLAGS += -fno-omit-frame-pointer 16 | CFLAGS += -Wall -Wno-format -Wno-unused -gstabs -m32 -fno-stack-protector 17 | LDFLAGS = -m elf_i386 18 | 19 | %.o:%.c 20 | $(CC) -nostdinc $(CFLAGS) -Os -c -o $@ $< 21 | 22 | 23 | %.o:%.S 24 | $(CC) -nostdinc $(CFLAGS) -c -o $@ $< 25 | 26 | %.o:%.asm 27 | nasm -f elf -o $@ $< 28 | 29 | kernel:entry cobj 30 | cat cobj>>entry 31 | cp entry kernel 32 | 33 | entry:$(entry_objs) 34 | cobj:$(ccode_objs) 35 | 36 | 37 | 38 | 39 | entry: 40 | @echo "addr=$(addr)" 41 | $(LD) $(LDFLAGS) -N -e entry -Ttext 0xc400 -o $@.out $^ 42 | $(OBJDUMP) -S -D $@.out >$@.asm 43 | $(OBJCOPY) -S -O binary -j .text $@.out $@ 44 | 45 | cobj: 46 | @echo "addr=0x00" 47 | $(LD) $(LDFLAGS) -N -e bootmain -T kernel.ld -o $@.out $^ 48 | $(OBJDUMP) -S -D $@.out >$@.asm 49 | $(OBJCOPY) -S -O binary $@.out $@ 50 | 51 | 52 | clean: 53 | -rm -f *.d *.o *.out main.o kernel kernel.asm cobj* entry.o entry.asm entry 54 | 55 | run: 56 | # dd if=boot of=boot.img bs=512 count=1 57 | qemu -drive file=kernel,if=floppy 58 | -------------------------------------------------------------------------------- /day8.4/kernel/mouse.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ================================================== 3 | * 4 | * Filename: mouse.c 5 | * 6 | * Description: 7 | * 8 | * Version: 0.01 9 | * Created: 2014年03月07日 星期五 09时51分45秒 10 | * Author: ChrisZZ, zchrissirhcz@163.com 11 | * Company: ZJUT 12 | * 13 | * ================================================== 14 | */ 15 | #include 16 | void wait_KBC_sendready(void){ 17 | for(;;){ 18 | if((inb(PORT_KEYSTA) & KEYSTA_SEND_NOTREADY)==0){ 19 | break; 20 | } 21 | } 22 | return; 23 | } 24 | 25 | void init_keyboard(void){ 26 | wait_KBC_sendready(); 27 | outb(PORT_KEYCMD, KEYCMD_WRITE_MODE); 28 | wait_KBC_sendready(); 29 | outb(PORT_KEYDAT, KBC_MODE); 30 | return; 31 | } 32 | 33 | /* 34 | void enable_mouse(void){ 35 | wait_KBC_sendready(); 36 | outb(PORT_KEYCMD, KEYCMD_SENDTO_MOUSE); 37 | wait_KBC_sendready(); 38 | outb(PORT_KEYDAT, MOUSECMD_ENABLE); 39 | return; 40 | } 41 | */ 42 | void enable_mouse(struct MOUSE_DEC *mdec) { 43 | wait_KBC_sendready(); 44 | outb(PORT_KEYCMD, KEYCMD_SENDTO_MOUSE); 45 | wait_KBC_sendready(); 46 | outb(PORT_KEYDAT, MOUSECMD_ENABLE); 47 | mdec->phase = 0; 48 | return; 49 | } 50 | 51 | int mouse_decode(struct MOUSE_DEC *mdec, unsigned char dat){ 52 | if(mdec->phase==0){ 53 | if(dat==0xfa){ 54 | mdec->phase=1; 55 | } 56 | return 0; 57 | } 58 | if(mdec->phase==1){ 59 | if((dat&0xc8)==0x08){ 60 | mdec->buf[0]=dat; 61 | mdec->phase=2; 62 | } 63 | return 0; 64 | } 65 | if(mdec->phase==2){ 66 | mdec->buf[1]=dat; 67 | mdec->phase=3; 68 | return 0; 69 | } 70 | if(mdec->phase==3){ 71 | mdec->buf[2]=dat; 72 | mdec->phase=1; 73 | mdec->btn=mdec->buf[0]&0x07; 74 | mdec->x=mdec->buf[1]; 75 | 76 | mdec->y=mdec->buf[2]; 77 | if((mdec->buf[0]&0x10)!=0){ 78 | mdec->x |= 0xffffff00; 79 | } 80 | if((mdec->buf[0]&0x20)!=0){ 81 | mdec->y |= 0xffffff00; 82 | } 83 | mdec->y = - mdec->y; 84 | return 1; 85 | } 86 | return -1; 87 | } 88 | -------------------------------------------------------------------------------- /day8.4/kernel/run: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | cd .. 3 | sudo make 4 | -------------------------------------------------------------------------------- /day8.4/makefile: -------------------------------------------------------------------------------- 1 | subdir=./kernel 2 | all:run 3 | 4 | asm: 5 | # nasm -o bootloader hello.nas 6 | nasm -o bootloader hello.nas 7 | @cd $(subdir);make clean;make addr=0xc400 8 | 9 | img:asm 10 | dd if=bootloader of=myos.img bs=512 count=1 11 | dd if=/dev/zero of=myos.img bs=512 seek=1 skip=1 count=2879 12 | 13 | copy:img 14 | mkdir -p /tmp/floppy 15 | mount -o loop myos.img /tmp/floppy -o fat=12 16 | sleep 1 17 | cp $(subdir)/kernel /tmp/floppy 18 | sleep 1 19 | umount /tmp/floppy 20 | run:copy 21 | qemu-system-i386 -drive file=myos.img,if=floppy 22 | clean: 23 | @rm -f bootloader myos.img haribote.bin 24 | -------------------------------------------------------------------------------- /day8.4/readme: -------------------------------------------------------------------------------- 1 | day8.4 2 | 代码直接copy书上的就ok了。不过运行时发现鼠标不会动,需要在qemu的菜单项view中设定grab input(捕捉输入) 3 | -------------------------------------------------------------------------------- /day9.2/kernel/asmint32.S: -------------------------------------------------------------------------------- 1 | .global asm_inthandler21 2 | .global asm_inthandler2c 3 | .global load_gdtr 4 | .global load_idtr 5 | 6 | .code32 7 | asm_inthandler21: 8 | pushw %es 9 | pushw %ds 10 | pushal 11 | movl %esp,%eax 12 | pushl %eax 13 | movw %ss,%ax 14 | movw %ax,%ds 15 | movw %ax,%es 16 | call inthandler21 17 | 18 | popl %eax 19 | popal 20 | popw %ds 21 | popW %es 22 | iretl 23 | 24 | asm_inthandler2c: 25 | pushw %es 26 | pushw %ds 27 | pushal 28 | movl %esp, %eax 29 | pushl %eax 30 | movw %ss, %ax 31 | movw %ax, %ds 32 | movw %ax, %es 33 | call inthandler2c 34 | 35 | popl %eax 36 | popal 37 | popw %ds 38 | popw %es 39 | iretl 40 | 41 | 42 | load_gdtr: #; void load_gdtr(int limit, int addr); 43 | mov 4(%esp) ,%ax 44 | mov %ax,6(%esp) 45 | lgdt 6(%esp) 46 | ret 47 | 48 | 49 | load_idtr: #; void load_idtr(int limit, int addr); 50 | mov 4(%esp) ,%ax 51 | mov %ax,6(%esp) 52 | lidt 6(%esp) 53 | ret 54 | -------------------------------------------------------------------------------- /day9.2/kernel/entry.ld: -------------------------------------------------------------------------------- 1 | /* Simple linker script for the JOS kernel. 2 | See the GNU ld 'info' manual ("info ld") to learn the syntax. */ 3 | 4 | OUTPUT_FORMAT("elf32-i386", "elf32-i386", "elf32-i386") 5 | OUTPUT_ARCH(i386) 6 | ENTRY(_start) 7 | 8 | SECTIONS 9 | { 10 | /* Link the kernel at this address: "." means the current address */ 11 | . = 0xc400; 12 | 13 | /* AT(...) gives the load address of this section, which tells 14 | the boot loader where to load the kernel in physical memory */ 15 | .text : { 16 | *(.text .stub .text.* .gnu.linkonce.t.*) 17 | } 18 | .rodata : { 19 | *(.rodata .rodata.* .gnu.linkonce.r.*) 20 | } 21 | 22 | .data : { 23 | *(.data) 24 | } 25 | 26 | .bss : { 27 | *(.bss) 28 | } 29 | 30 | 31 | } 32 | -------------------------------------------------------------------------------- /day9.2/kernel/fifo.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ================================================== 3 | * 4 | * Filename: fifo.c 5 | * 6 | * Description: 7 | * 8 | * Version: 0.01 9 | * Created: 2014年03月06日 星期四 18时42分55秒 10 | * Author: ChrisZZ, zchrissirhcz@163.com 11 | * Company: ZJUT 12 | * 13 | * ================================================== 14 | */ 15 | #include 16 | #define FLAGS_OVERRUN 0x0001 17 | 18 | void fifo8_init(struct FIFO8* fifo, int size, unsigned char *buf){ 19 | fifo->size = size; 20 | fifo->buf = buf; 21 | fifo->free = size; 22 | fifo->flags = 0; 23 | fifo->p = 0; 24 | fifo->q = 0; 25 | return; 26 | } 27 | 28 | int fifo8_put(struct FIFO8* fifo, char data){ 29 | if(fifo->free==0){ 30 | fifo->flags |= FLAGS_OVERRUN; 31 | return -1; 32 | } 33 | fifo->buf[fifo->p] = data; 34 | fifo->p++; 35 | if(fifo->p == fifo->size){ 36 | fifo->p=0; 37 | } 38 | fifo->free--; 39 | return 0; 40 | } 41 | 42 | int fifo8_get(struct FIFO8* fifo){ 43 | int data; 44 | if(fifo->free==fifo->size){ 45 | return -1; 46 | } 47 | data = fifo->buf[fifo->q]; 48 | fifo->q++; 49 | if(fifo->q==fifo->size){ 50 | fifo->q=0; 51 | } 52 | fifo->free++; 53 | return data; 54 | } 55 | 56 | int fifo8_status(struct FIFO8* fifo){ 57 | return fifo->size-fifo->free; 58 | } 59 | -------------------------------------------------------------------------------- /day9.2/kernel/font.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /day9.2/kernel/font.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ================================================== 3 | * 4 | * Filename: font.h 5 | * 6 | * Description: 7 | * 8 | * Version: 0.01 9 | * Created: 2014年03月02日 星期日 12时27分31秒 10 | * Author: ChrisZZ, zchrissirhcz@163.com 11 | * Company: ZJUT 12 | * 13 | * ================================================== 14 | */ 15 | #ifndef font_header 16 | #define font_header 17 | 18 | extern unsigned short ASCII_Table[]; 19 | extern char hankaku[2048]; 20 | 21 | #endif 22 | -------------------------------------------------------------------------------- /day9.2/kernel/kernel.ld: -------------------------------------------------------------------------------- 1 | /* Simple linker script for the JOS kernel. this file is for main.c 2 | See the GNU ld 'info' manual ("info ld") to learn the syntax. */ 3 | 4 | OUTPUT_FORMAT("elf32-i386", "elf32-i386", "elf32-i386") 5 | OUTPUT_ARCH(i386) 6 | ENTRY(_start) 7 | 8 | SECTIONS 9 | { 10 | /* Link the kernel at this address: "." means the current address */ 11 | . = 0x280000; 12 | .text : { 13 | *(.text) 14 | } 15 | .rodata : { 16 | *(.rodata) 17 | } 18 | 19 | .data : { 20 | *(.data) 21 | } 22 | 23 | .bss : { 24 | *(.bss) 25 | } 26 | 27 | 28 | } 29 | -------------------------------------------------------------------------------- /day9.2/kernel/log: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zchrissirhcz/osask-linux/a7afa7623205aaee125fc33676480d72c21e5323/day9.2/kernel/log -------------------------------------------------------------------------------- /day9.2/kernel/makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Makefile fragment for the JOS kernel. 3 | # This is NOT a complete makefile; 4 | # you must run GNU make in the top-level directory 5 | # where the GNUmakefile is located. 6 | # 7 | #entry.S main.c 8 | OBJCOPY := $(GCCPREFIX)objcopy 9 | OBJDUMP := $(GCCPREFIX)objdump 10 | OBJDIR=. 11 | entry_objs= entry.o 12 | ccode_objs=main.o screen.o font.o print.o idtgdt.o int.o asmint32.o fifo.o mouse.o memory.o 13 | CFLAGS := $(CFLAGS) -O1 -fno-builtin -I$(OBJDIR) -MD 14 | #CFLAGS := $(CFLAGS) -O1 -fno-builtin -MD 15 | CFLAGS += -fno-omit-frame-pointer 16 | CFLAGS += -Wall -Wno-format -Wno-unused -gstabs -m32 -fno-stack-protector 17 | LDFLAGS = -m elf_i386 18 | 19 | %.o:%.c 20 | $(CC) -nostdinc $(CFLAGS) -Os -c -o $@ $< 21 | 22 | 23 | %.o:%.S 24 | $(CC) -nostdinc $(CFLAGS) -c -o $@ $< 25 | 26 | %.o:%.asm 27 | nasm -f elf -o $@ $< 28 | 29 | kernel:entry cobj 30 | cat cobj>>entry 31 | cp entry kernel 32 | 33 | entry:$(entry_objs) 34 | cobj:$(ccode_objs) 35 | 36 | 37 | 38 | 39 | entry: 40 | @echo "addr=$(addr)" 41 | $(LD) $(LDFLAGS) -N -e entry -Ttext 0xc400 -o $@.out $^ 42 | $(OBJDUMP) -S -D $@.out >$@.asm 43 | $(OBJCOPY) -S -O binary -j .text $@.out $@ 44 | 45 | cobj: 46 | @echo "addr=0x00" 47 | $(LD) $(LDFLAGS) -N -e bootmain -T kernel.ld -o $@.out $^ 48 | $(OBJDUMP) -S -D $@.out >$@.asm 49 | $(OBJCOPY) -S -O binary $@.out $@ 50 | 51 | 52 | clean: 53 | -rm -f *.d *.o *.out main.o kernel kernel.asm cobj* entry.o entry.asm entry 54 | 55 | run: 56 | # dd if=boot of=boot.img bs=512 count=1 57 | qemu -drive file=kernel,if=floppy 58 | -------------------------------------------------------------------------------- /day9.2/kernel/memory.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ================================================== 3 | * 4 | * Filename: memory.c 5 | * 6 | * Description: 7 | * 8 | * Version: 0.01 9 | * Created: 2014年03月07日 星期五 21时02分32秒 10 | * Author: ChrisZZ, zchrissirhcz@163.com 11 | * Company: ZJUT 12 | * 13 | * ================================================== 14 | */ 15 | #include 16 | 17 | unsigned int memtest(unsigned int start, unsigned int end){ 18 | char flg486 = 0; 19 | unsigned int eflg, cr0, i; 20 | eflg = io_load_eflags(); 21 | eflg |= EFLAGS_AC_BIT; 22 | io_store_eflags(eflg); 23 | eflg = io_load_eflags(); 24 | if((eflg & EFLAGS_AC_BIT)!=0){ 25 | flg486=1; 26 | } 27 | eflg &= ~EFLAGS_AC_BIT; 28 | io_store_eflags(eflg); 29 | 30 | if(flg486!=0){ 31 | cr0 = load_cr0(); 32 | cr0 |= CR0_CACHE_DISABLE; 33 | store_cr0(cr0); 34 | } 35 | 36 | i=memtest_sub(start, end); 37 | 38 | if(flg486!=0){ 39 | cr0 = load_cr0(); 40 | cr0 &= ~CR0_CACHE_DISABLE; 41 | store_cr0(cr0); 42 | } 43 | return i; 44 | } 45 | 46 | unsigned int memtest_sub(unsigned int start, unsigned int end){ 47 | unsigned int i, *p, old, pat0=0xaa55aa55, pat1=0x55aa55aa; 48 | for(i=start; i<=end; i+=0x1000){ 49 | p = (unsigned int *)(i+0xffc); 50 | old = *p; 51 | *p = pat0; 52 | *p ^= 0xffffffff; 53 | if(*p!=pat1){ 54 | not_memory: 55 | *p = old; 56 | break; 57 | } 58 | *p ^= 0xffffffff; 59 | if(*p!=pat0){ 60 | goto not_memory; 61 | } 62 | *p = old; 63 | } 64 | return i; 65 | } 66 | -------------------------------------------------------------------------------- /day9.2/kernel/mouse.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ================================================== 3 | * 4 | * Filename: mouse.c 5 | * 6 | * Description: 7 | * 8 | * Version: 0.01 9 | * Created: 2014年03月07日 星期五 09时51分45秒 10 | * Author: ChrisZZ, zchrissirhcz@163.com 11 | * Company: ZJUT 12 | * 13 | * ================================================== 14 | */ 15 | #include 16 | void wait_KBC_sendready(void){ 17 | for(;;){ 18 | if((inb(PORT_KEYSTA) & KEYSTA_SEND_NOTREADY)==0){ 19 | break; 20 | } 21 | } 22 | return; 23 | } 24 | 25 | void init_keyboard(void){ 26 | wait_KBC_sendready(); 27 | outb(PORT_KEYCMD, KEYCMD_WRITE_MODE); 28 | wait_KBC_sendready(); 29 | outb(PORT_KEYDAT, KBC_MODE); 30 | return; 31 | } 32 | 33 | /* 34 | void enable_mouse(void){ 35 | wait_KBC_sendready(); 36 | outb(PORT_KEYCMD, KEYCMD_SENDTO_MOUSE); 37 | wait_KBC_sendready(); 38 | outb(PORT_KEYDAT, MOUSECMD_ENABLE); 39 | return; 40 | } 41 | */ 42 | void enable_mouse(struct MOUSE_DEC *mdec) { 43 | wait_KBC_sendready(); 44 | outb(PORT_KEYCMD, KEYCMD_SENDTO_MOUSE); 45 | wait_KBC_sendready(); 46 | outb(PORT_KEYDAT, MOUSECMD_ENABLE); 47 | mdec->phase = 0; 48 | return; 49 | } 50 | 51 | int mouse_decode(struct MOUSE_DEC *mdec, unsigned char dat){ 52 | if(mdec->phase==0){ 53 | if(dat==0xfa){ 54 | mdec->phase=1; 55 | } 56 | return 0; 57 | } 58 | if(mdec->phase==1){ 59 | if((dat&0xc8)==0x08){ 60 | mdec->buf[0]=dat; 61 | mdec->phase=2; 62 | } 63 | return 0; 64 | } 65 | if(mdec->phase==2){ 66 | mdec->buf[1]=dat; 67 | mdec->phase=3; 68 | return 0; 69 | } 70 | if(mdec->phase==3){ 71 | mdec->buf[2]=dat; 72 | mdec->phase=1; 73 | mdec->btn=mdec->buf[0]&0x07; 74 | mdec->x=mdec->buf[1]; 75 | 76 | mdec->y=mdec->buf[2]; 77 | if((mdec->buf[0]&0x10)!=0){ 78 | mdec->x |= 0xffffff00; 79 | } 80 | if((mdec->buf[0]&0x20)!=0){ 81 | mdec->y |= 0xffffff00; 82 | } 83 | mdec->y = - mdec->y; 84 | return 1; 85 | } 86 | return -1; 87 | } 88 | -------------------------------------------------------------------------------- /day9.2/kernel/run: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | cd .. 3 | sudo make 4 | -------------------------------------------------------------------------------- /day9.2/makefile: -------------------------------------------------------------------------------- 1 | subdir=./kernel 2 | all:run 3 | 4 | asm: 5 | # nasm -o bootloader hello.nas 6 | nasm -o bootloader hello.nas 7 | @cd $(subdir);make clean;make addr=0xc400 8 | 9 | img:asm 10 | dd if=bootloader of=myos.img bs=512 count=1 11 | dd if=/dev/zero of=myos.img bs=512 seek=1 skip=1 count=2879 12 | 13 | copy:img 14 | mkdir -p /tmp/floppy 15 | mount -o loop myos.img /tmp/floppy -o fat=12 16 | sleep 1 17 | cp $(subdir)/kernel /tmp/floppy 18 | sleep 1 19 | umount /tmp/floppy 20 | run:copy 21 | qemu-system-i386 -drive file=myos.img,if=floppy 22 | clean: 23 | @rm -f bootloader myos.img haribote.bin 24 | -------------------------------------------------------------------------------- /day9.2/readme: -------------------------------------------------------------------------------- 1 | day9.2 2 | 为了和《30天》保持代码命名一致,把先前x86.h中关于eflags的读写与cr0的函数换成了和川合秀实一样的名字。 3 | 运行后显示3072MB内存。 4 | -------------------------------------------------------------------------------- /day9.3/kernel/asmint32.S: -------------------------------------------------------------------------------- 1 | .global asm_inthandler21 2 | .global asm_inthandler2c 3 | .global load_gdtr 4 | .global load_idtr 5 | 6 | .code32 7 | asm_inthandler21: 8 | pushw %es 9 | pushw %ds 10 | pushal 11 | movl %esp,%eax 12 | pushl %eax 13 | movw %ss,%ax 14 | movw %ax,%ds 15 | movw %ax,%es 16 | call inthandler21 17 | 18 | popl %eax 19 | popal 20 | popw %ds 21 | popW %es 22 | iretl 23 | 24 | asm_inthandler2c: 25 | pushw %es 26 | pushw %ds 27 | pushal 28 | movl %esp, %eax 29 | pushl %eax 30 | movw %ss, %ax 31 | movw %ax, %ds 32 | movw %ax, %es 33 | call inthandler2c 34 | 35 | popl %eax 36 | popal 37 | popw %ds 38 | popw %es 39 | iretl 40 | 41 | 42 | load_gdtr: #; void load_gdtr(int limit, int addr); 43 | mov 4(%esp) ,%ax 44 | mov %ax,6(%esp) 45 | lgdt 6(%esp) 46 | ret 47 | 48 | 49 | load_idtr: #; void load_idtr(int limit, int addr); 50 | mov 4(%esp) ,%ax 51 | mov %ax,6(%esp) 52 | lidt 6(%esp) 53 | ret 54 | -------------------------------------------------------------------------------- /day9.3/kernel/entry.ld: -------------------------------------------------------------------------------- 1 | /* Simple linker script for the JOS kernel. 2 | See the GNU ld 'info' manual ("info ld") to learn the syntax. */ 3 | 4 | OUTPUT_FORMAT("elf32-i386", "elf32-i386", "elf32-i386") 5 | OUTPUT_ARCH(i386) 6 | ENTRY(_start) 7 | 8 | SECTIONS 9 | { 10 | /* Link the kernel at this address: "." means the current address */ 11 | . = 0xc400; 12 | 13 | /* AT(...) gives the load address of this section, which tells 14 | the boot loader where to load the kernel in physical memory */ 15 | .text : { 16 | *(.text .stub .text.* .gnu.linkonce.t.*) 17 | } 18 | .rodata : { 19 | *(.rodata .rodata.* .gnu.linkonce.r.*) 20 | } 21 | 22 | .data : { 23 | *(.data) 24 | } 25 | 26 | .bss : { 27 | *(.bss) 28 | } 29 | 30 | 31 | } 32 | -------------------------------------------------------------------------------- /day9.3/kernel/fifo.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ================================================== 3 | * 4 | * Filename: fifo.c 5 | * 6 | * Description: 7 | * 8 | * Version: 0.01 9 | * Created: 2014年03月06日 星期四 18时42分55秒 10 | * Author: ChrisZZ, zchrissirhcz@163.com 11 | * Company: ZJUT 12 | * 13 | * ================================================== 14 | */ 15 | #include 16 | #define FLAGS_OVERRUN 0x0001 17 | 18 | void fifo8_init(struct FIFO8* fifo, int size, unsigned char *buf){ 19 | fifo->size = size; 20 | fifo->buf = buf; 21 | fifo->free = size; 22 | fifo->flags = 0; 23 | fifo->p = 0; 24 | fifo->q = 0; 25 | return; 26 | } 27 | 28 | int fifo8_put(struct FIFO8* fifo, char data){ 29 | if(fifo->free==0){ 30 | fifo->flags |= FLAGS_OVERRUN; 31 | return -1; 32 | } 33 | fifo->buf[fifo->p] = data; 34 | fifo->p++; 35 | if(fifo->p == fifo->size){ 36 | fifo->p=0; 37 | } 38 | fifo->free--; 39 | return 0; 40 | } 41 | 42 | int fifo8_get(struct FIFO8* fifo){ 43 | int data; 44 | if(fifo->free==fifo->size){ 45 | return -1; 46 | } 47 | data = fifo->buf[fifo->q]; 48 | fifo->q++; 49 | if(fifo->q==fifo->size){ 50 | fifo->q=0; 51 | } 52 | fifo->free++; 53 | return data; 54 | } 55 | 56 | int fifo8_status(struct FIFO8* fifo){ 57 | return fifo->size-fifo->free; 58 | } 59 | -------------------------------------------------------------------------------- /day9.3/kernel/font.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /day9.3/kernel/font.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ================================================== 3 | * 4 | * Filename: font.h 5 | * 6 | * Description: 7 | * 8 | * Version: 0.01 9 | * Created: 2014年03月02日 星期日 12时27分31秒 10 | * Author: ChrisZZ, zchrissirhcz@163.com 11 | * Company: ZJUT 12 | * 13 | * ================================================== 14 | */ 15 | #ifndef font_header 16 | #define font_header 17 | 18 | extern unsigned short ASCII_Table[]; 19 | extern char hankaku[2048]; 20 | 21 | #endif 22 | -------------------------------------------------------------------------------- /day9.3/kernel/kernel.ld: -------------------------------------------------------------------------------- 1 | /* Simple linker script for the JOS kernel. this file is for main.c 2 | See the GNU ld 'info' manual ("info ld") to learn the syntax. */ 3 | 4 | OUTPUT_FORMAT("elf32-i386", "elf32-i386", "elf32-i386") 5 | OUTPUT_ARCH(i386) 6 | ENTRY(_start) 7 | 8 | SECTIONS 9 | { 10 | /* Link the kernel at this address: "." means the current address */ 11 | . = 0x280000; 12 | .text : { 13 | *(.text) 14 | } 15 | .rodata : { 16 | *(.rodata) 17 | } 18 | 19 | .data : { 20 | *(.data) 21 | } 22 | 23 | .bss : { 24 | *(.bss) 25 | } 26 | 27 | 28 | } 29 | -------------------------------------------------------------------------------- /day9.3/kernel/log: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zchrissirhcz/osask-linux/a7afa7623205aaee125fc33676480d72c21e5323/day9.3/kernel/log -------------------------------------------------------------------------------- /day9.3/kernel/makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Makefile fragment for the JOS kernel. 3 | # This is NOT a complete makefile; 4 | # you must run GNU make in the top-level directory 5 | # where the GNUmakefile is located. 6 | # 7 | #entry.S main.c 8 | OBJCOPY := $(GCCPREFIX)objcopy 9 | OBJDUMP := $(GCCPREFIX)objdump 10 | OBJDIR=. 11 | entry_objs= entry.o 12 | ccode_objs=main.o screen.o font.o print.o idtgdt.o int.o asmint32.o fifo.o mouse.o memory.o 13 | CFLAGS := $(CFLAGS) -O1 -fno-builtin -I$(OBJDIR) -MD 14 | #CFLAGS := $(CFLAGS) -O1 -fno-builtin -MD 15 | CFLAGS += -fno-omit-frame-pointer 16 | CFLAGS += -Wall -Wno-format -Wno-unused -gstabs -m32 -fno-stack-protector 17 | LDFLAGS = -m elf_i386 18 | 19 | %.o:%.c 20 | $(CC) -nostdinc $(CFLAGS) -Os -c -o $@ $< 21 | 22 | 23 | %.o:%.S 24 | $(CC) -nostdinc $(CFLAGS) -c -o $@ $< 25 | 26 | %.o:%.asm 27 | nasm -f elf -o $@ $< 28 | 29 | kernel:entry cobj 30 | cat cobj>>entry 31 | cp entry kernel 32 | 33 | entry:$(entry_objs) 34 | cobj:$(ccode_objs) 35 | 36 | 37 | 38 | 39 | entry: 40 | @echo "addr=$(addr)" 41 | $(LD) $(LDFLAGS) -N -e entry -Ttext 0xc400 -o $@.out $^ 42 | $(OBJDUMP) -S -D $@.out >$@.asm 43 | $(OBJCOPY) -S -O binary -j .text $@.out $@ 44 | 45 | cobj: 46 | @echo "addr=0x00" 47 | $(LD) $(LDFLAGS) -N -e bootmain -T kernel.ld -o $@.out $^ 48 | $(OBJDUMP) -S -D $@.out >$@.asm 49 | $(OBJCOPY) -S -O binary $@.out $@ 50 | 51 | 52 | clean: 53 | -rm -f *.d *.o *.out kernel kernel.asm cobj* entry.o entry.asm entry 54 | 55 | run: 56 | # dd if=boot of=boot.img bs=512 count=1 57 | qemu -drive file=kernel,if=floppy 58 | -------------------------------------------------------------------------------- /day9.3/kernel/memory.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ================================================== 3 | * 4 | * Filename: memory.c 5 | * 6 | * Description: 7 | * 8 | * Version: 0.01 9 | * Created: 2014年03月07日 星期五 21时02分32秒 10 | * Author: ChrisZZ, zchrissirhcz@163.com 11 | * Company: ZJUT 12 | * 13 | * ================================================== 14 | */ 15 | #include 16 | 17 | unsigned int memtest(unsigned int start, unsigned int end){ 18 | char flg486 = 0; 19 | unsigned int eflg, cr0, i; 20 | eflg = io_load_eflags(); 21 | eflg |= EFLAGS_AC_BIT; 22 | io_store_eflags(eflg); 23 | eflg = io_load_eflags(); 24 | if((eflg & EFLAGS_AC_BIT)!=0){ 25 | flg486=1; 26 | } 27 | eflg &= ~EFLAGS_AC_BIT; 28 | io_store_eflags(eflg); 29 | 30 | if(flg486!=0){ 31 | cr0 = load_cr0(); 32 | cr0 |= CR0_CACHE_DISABLE; 33 | store_cr0(cr0); 34 | } 35 | 36 | i=memtest_sub(start, end); 37 | 38 | if(flg486!=0){ 39 | cr0 = load_cr0(); 40 | cr0 &= ~CR0_CACHE_DISABLE; 41 | store_cr0(cr0); 42 | } 43 | return i; 44 | } 45 | 46 | unsigned int memtest_sub(unsigned int start, unsigned int end){ 47 | unsigned int i, old, pat0=0xaa55aa55, pat1=0x55aa55aa; 48 | volatile unsigned int *p; 49 | for(i=start; i<=end; i+=0x1000){ 50 | p = (unsigned int *)(i+0xffc); 51 | old = *p; 52 | *p = pat0; 53 | *p ^= 0xffffffff; 54 | if(*p!=pat1){ 55 | not_memory: 56 | *p = old; 57 | break; 58 | } 59 | *p ^= 0xffffffff; 60 | if(*p!=pat0){ 61 | goto not_memory; 62 | } 63 | *p = old; 64 | } 65 | return i; 66 | } 67 | -------------------------------------------------------------------------------- /day9.3/kernel/mouse.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ================================================== 3 | * 4 | * Filename: mouse.c 5 | * 6 | * Description: 7 | * 8 | * Version: 0.01 9 | * Created: 2014年03月07日 星期五 09时51分45秒 10 | * Author: ChrisZZ, zchrissirhcz@163.com 11 | * Company: ZJUT 12 | * 13 | * ================================================== 14 | */ 15 | #include 16 | void wait_KBC_sendready(void){ 17 | for(;;){ 18 | if((inb(PORT_KEYSTA) & KEYSTA_SEND_NOTREADY)==0){ 19 | break; 20 | } 21 | } 22 | return; 23 | } 24 | 25 | void init_keyboard(void){ 26 | wait_KBC_sendready(); 27 | outb(PORT_KEYCMD, KEYCMD_WRITE_MODE); 28 | wait_KBC_sendready(); 29 | outb(PORT_KEYDAT, KBC_MODE); 30 | return; 31 | } 32 | 33 | /* 34 | void enable_mouse(void){ 35 | wait_KBC_sendready(); 36 | outb(PORT_KEYCMD, KEYCMD_SENDTO_MOUSE); 37 | wait_KBC_sendready(); 38 | outb(PORT_KEYDAT, MOUSECMD_ENABLE); 39 | return; 40 | } 41 | */ 42 | void enable_mouse(struct MOUSE_DEC *mdec) { 43 | wait_KBC_sendready(); 44 | outb(PORT_KEYCMD, KEYCMD_SENDTO_MOUSE); 45 | wait_KBC_sendready(); 46 | outb(PORT_KEYDAT, MOUSECMD_ENABLE); 47 | mdec->phase = 0; 48 | return; 49 | } 50 | 51 | int mouse_decode(struct MOUSE_DEC *mdec, unsigned char dat){ 52 | if(mdec->phase==0){ 53 | if(dat==0xfa){ 54 | mdec->phase=1; 55 | } 56 | return 0; 57 | } 58 | if(mdec->phase==1){ 59 | if((dat&0xc8)==0x08){ 60 | mdec->buf[0]=dat; 61 | mdec->phase=2; 62 | } 63 | return 0; 64 | } 65 | if(mdec->phase==2){ 66 | mdec->buf[1]=dat; 67 | mdec->phase=3; 68 | return 0; 69 | } 70 | if(mdec->phase==3){ 71 | mdec->buf[2]=dat; 72 | mdec->phase=1; 73 | mdec->btn=mdec->buf[0]&0x07; 74 | mdec->x=mdec->buf[1]; 75 | 76 | mdec->y=mdec->buf[2]; 77 | if((mdec->buf[0]&0x10)!=0){ 78 | mdec->x |= 0xffffff00; 79 | } 80 | if((mdec->buf[0]&0x20)!=0){ 81 | mdec->y |= 0xffffff00; 82 | } 83 | mdec->y = - mdec->y; 84 | return 1; 85 | } 86 | return -1; 87 | } 88 | -------------------------------------------------------------------------------- /day9.3/kernel/run: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | cd .. 3 | sudo make 4 | -------------------------------------------------------------------------------- /day9.3/makefile: -------------------------------------------------------------------------------- 1 | subdir=./kernel 2 | all:run 3 | 4 | asm: 5 | # nasm -o bootloader hello.nas 6 | nasm -o bootloader hello.nas 7 | @cd $(subdir);make clean;make addr=0xc400 8 | 9 | img:asm 10 | dd if=bootloader of=myos.img bs=512 count=1 11 | dd if=/dev/zero of=myos.img bs=512 seek=1 skip=1 count=2879 12 | 13 | copy:img 14 | mkdir -p /tmp/floppy 15 | mount -o loop myos.img /tmp/floppy -o fat=12 16 | sleep 1 17 | cp $(subdir)/kernel /tmp/floppy 18 | sleep 1 19 | umount /tmp/floppy 20 | run:copy 21 | qemu-system-i386 -drive file=myos.img,if=floppy 22 | clean: 23 | @rm -f bootloader myos.img haribote.bin 24 | -------------------------------------------------------------------------------- /day9.3/readme: -------------------------------------------------------------------------------- 1 | day9.3 2 | 存在编译器优化的问题。解决办法是把不希望优化的代码中的变量用volotile声明 3 | 可能是因为qemu的原因,我的memory显示的是128MB 4 | -------------------------------------------------------------------------------- /day9.4/kernel/asmint32.S: -------------------------------------------------------------------------------- 1 | .global asm_inthandler21 2 | .global asm_inthandler2c 3 | .global load_gdtr 4 | .global load_idtr 5 | 6 | .code32 7 | asm_inthandler21: 8 | pushw %es 9 | pushw %ds 10 | pushal 11 | movl %esp,%eax 12 | pushl %eax 13 | movw %ss,%ax 14 | movw %ax,%ds 15 | movw %ax,%es 16 | call inthandler21 17 | 18 | popl %eax 19 | popal 20 | popw %ds 21 | popW %es 22 | iretl 23 | 24 | asm_inthandler2c: 25 | pushw %es 26 | pushw %ds 27 | pushal 28 | movl %esp, %eax 29 | pushl %eax 30 | movw %ss, %ax 31 | movw %ax, %ds 32 | movw %ax, %es 33 | call inthandler2c 34 | 35 | popl %eax 36 | popal 37 | popw %ds 38 | popw %es 39 | iretl 40 | 41 | 42 | load_gdtr: #; void load_gdtr(int limit, int addr); 43 | mov 4(%esp) ,%ax 44 | mov %ax,6(%esp) 45 | lgdt 6(%esp) 46 | ret 47 | 48 | 49 | load_idtr: #; void load_idtr(int limit, int addr); 50 | mov 4(%esp) ,%ax 51 | mov %ax,6(%esp) 52 | lidt 6(%esp) 53 | ret 54 | -------------------------------------------------------------------------------- /day9.4/kernel/entry.ld: -------------------------------------------------------------------------------- 1 | /* Simple linker script for the JOS kernel. 2 | See the GNU ld 'info' manual ("info ld") to learn the syntax. */ 3 | 4 | OUTPUT_FORMAT("elf32-i386", "elf32-i386", "elf32-i386") 5 | OUTPUT_ARCH(i386) 6 | ENTRY(_start) 7 | 8 | SECTIONS 9 | { 10 | /* Link the kernel at this address: "." means the current address */ 11 | . = 0xc400; 12 | 13 | /* AT(...) gives the load address of this section, which tells 14 | the boot loader where to load the kernel in physical memory */ 15 | .text : { 16 | *(.text .stub .text.* .gnu.linkonce.t.*) 17 | } 18 | .rodata : { 19 | *(.rodata .rodata.* .gnu.linkonce.r.*) 20 | } 21 | 22 | .data : { 23 | *(.data) 24 | } 25 | 26 | .bss : { 27 | *(.bss) 28 | } 29 | 30 | 31 | } 32 | -------------------------------------------------------------------------------- /day9.4/kernel/fifo.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ================================================== 3 | * 4 | * Filename: fifo.c 5 | * 6 | * Description: 7 | * 8 | * Version: 0.01 9 | * Created: 2014年03月06日 星期四 18时42分55秒 10 | * Author: ChrisZZ, zchrissirhcz@163.com 11 | * Company: ZJUT 12 | * 13 | * ================================================== 14 | */ 15 | #include 16 | #define FLAGS_OVERRUN 0x0001 17 | 18 | void fifo8_init(struct FIFO8* fifo, int size, unsigned char *buf){ 19 | fifo->size = size; 20 | fifo->buf = buf; 21 | fifo->free = size; 22 | fifo->flags = 0; 23 | fifo->p = 0; 24 | fifo->q = 0; 25 | return; 26 | } 27 | 28 | int fifo8_put(struct FIFO8* fifo, char data){ 29 | if(fifo->free==0){ 30 | fifo->flags |= FLAGS_OVERRUN; 31 | return -1; 32 | } 33 | fifo->buf[fifo->p] = data; 34 | fifo->p++; 35 | if(fifo->p == fifo->size){ 36 | fifo->p=0; 37 | } 38 | fifo->free--; 39 | return 0; 40 | } 41 | 42 | int fifo8_get(struct FIFO8* fifo){ 43 | int data; 44 | if(fifo->free==fifo->size){ 45 | return -1; 46 | } 47 | data = fifo->buf[fifo->q]; 48 | fifo->q++; 49 | if(fifo->q==fifo->size){ 50 | fifo->q=0; 51 | } 52 | fifo->free++; 53 | return data; 54 | } 55 | 56 | int fifo8_status(struct FIFO8* fifo){ 57 | return fifo->size-fifo->free; 58 | } 59 | -------------------------------------------------------------------------------- /day9.4/kernel/font.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /day9.4/kernel/font.h: -------------------------------------------------------------------------------- 1 | /* 2 | * ================================================== 3 | * 4 | * Filename: font.h 5 | * 6 | * Description: 7 | * 8 | * Version: 0.01 9 | * Created: 2014年03月02日 星期日 12时27分31秒 10 | * Author: ChrisZZ, zchrissirhcz@163.com 11 | * Company: ZJUT 12 | * 13 | * ================================================== 14 | */ 15 | #ifndef font_header 16 | #define font_header 17 | 18 | extern unsigned short ASCII_Table[]; 19 | extern char hankaku[2048]; 20 | 21 | #endif 22 | -------------------------------------------------------------------------------- /day9.4/kernel/kernel.ld: -------------------------------------------------------------------------------- 1 | /* Simple linker script for the JOS kernel. this file is for main.c 2 | See the GNU ld 'info' manual ("info ld") to learn the syntax. */ 3 | 4 | OUTPUT_FORMAT("elf32-i386", "elf32-i386", "elf32-i386") 5 | OUTPUT_ARCH(i386) 6 | ENTRY(_start) 7 | 8 | SECTIONS 9 | { 10 | /* Link the kernel at this address: "." means the current address */ 11 | . = 0x280000; 12 | .text : { 13 | *(.text) 14 | } 15 | .rodata : { 16 | *(.rodata) 17 | } 18 | 19 | .data : { 20 | *(.data) 21 | } 22 | 23 | .bss : { 24 | *(.bss) 25 | } 26 | 27 | 28 | } 29 | -------------------------------------------------------------------------------- /day9.4/kernel/log: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zchrissirhcz/osask-linux/a7afa7623205aaee125fc33676480d72c21e5323/day9.4/kernel/log -------------------------------------------------------------------------------- /day9.4/kernel/makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Makefile fragment for the JOS kernel. 3 | # This is NOT a complete makefile; 4 | # you must run GNU make in the top-level directory 5 | # where the GNUmakefile is located. 6 | # 7 | #entry.S main.c 8 | OBJCOPY := $(GCCPREFIX)objcopy 9 | OBJDUMP := $(GCCPREFIX)objdump 10 | OBJDIR=. 11 | entry_objs= entry.o 12 | ccode_objs=main.o screen.o font.o print.o idtgdt.o int.o asmint32.o fifo.o mouse.o memory.o 13 | CFLAGS := $(CFLAGS) -O1 -fno-builtin -I$(OBJDIR) -MD 14 | #CFLAGS := $(CFLAGS) -O1 -fno-builtin -MD 15 | CFLAGS += -fno-omit-frame-pointer 16 | CFLAGS += -Wall -Wno-format -Wno-unused -gstabs -m32 -fno-stack-protector 17 | LDFLAGS = -m elf_i386 18 | 19 | %.o:%.c 20 | $(CC) -nostdinc $(CFLAGS) -Os -c -o $@ $< 21 | 22 | 23 | %.o:%.S 24 | $(CC) -nostdinc $(CFLAGS) -c -o $@ $< 25 | 26 | %.o:%.asm 27 | nasm -f elf -o $@ $< 28 | 29 | kernel:entry cobj 30 | cat cobj>>entry 31 | cp entry kernel 32 | 33 | entry:$(entry_objs) 34 | cobj:$(ccode_objs) 35 | 36 | 37 | 38 | 39 | entry: 40 | @echo "addr=$(addr)" 41 | $(LD) $(LDFLAGS) -N -e entry -Ttext 0xc400 -o $@.out $^ 42 | $(OBJDUMP) -S -D $@.out >$@.asm 43 | $(OBJCOPY) -S -O binary -j .text $@.out $@ 44 | 45 | cobj: 46 | @echo "addr=0x00" 47 | $(LD) $(LDFLAGS) -N -e bootmain -T kernel.ld -o $@.out $^ 48 | $(OBJDUMP) -S -D $@.out >$@.asm 49 | $(OBJCOPY) -S -O binary $@.out $@ 50 | 51 | 52 | clean: 53 | -rm -f *.d *.o *.out kernel kernel.asm cobj* entry.o entry.asm entry 54 | 55 | run: 56 | # dd if=boot of=boot.img bs=512 count=1 57 | qemu -drive file=kernel,if=floppy 58 | -------------------------------------------------------------------------------- /day9.4/kernel/mouse.c: -------------------------------------------------------------------------------- 1 | /* 2 | * ================================================== 3 | * 4 | * Filename: mouse.c 5 | * 6 | * Description: 7 | * 8 | * Version: 0.01 9 | * Created: 2014年03月07日 星期五 09时51分45秒 10 | * Author: ChrisZZ, zchrissirhcz@163.com 11 | * Company: ZJUT 12 | * 13 | * ================================================== 14 | */ 15 | #include 16 | void wait_KBC_sendready(void){ 17 | for(;;){ 18 | if((inb(PORT_KEYSTA) & KEYSTA_SEND_NOTREADY)==0){ 19 | break; 20 | } 21 | } 22 | return; 23 | } 24 | 25 | void init_keyboard(void){ 26 | wait_KBC_sendready(); 27 | outb(PORT_KEYCMD, KEYCMD_WRITE_MODE); 28 | wait_KBC_sendready(); 29 | outb(PORT_KEYDAT, KBC_MODE); 30 | return; 31 | } 32 | 33 | /* 34 | void enable_mouse(void){ 35 | wait_KBC_sendready(); 36 | outb(PORT_KEYCMD, KEYCMD_SENDTO_MOUSE); 37 | wait_KBC_sendready(); 38 | outb(PORT_KEYDAT, MOUSECMD_ENABLE); 39 | return; 40 | } 41 | */ 42 | void enable_mouse(struct MOUSE_DEC *mdec) { 43 | wait_KBC_sendready(); 44 | outb(PORT_KEYCMD, KEYCMD_SENDTO_MOUSE); 45 | wait_KBC_sendready(); 46 | outb(PORT_KEYDAT, MOUSECMD_ENABLE); 47 | mdec->phase = 0; 48 | return; 49 | } 50 | 51 | int mouse_decode(struct MOUSE_DEC *mdec, unsigned char dat){ 52 | if(mdec->phase==0){ 53 | if(dat==0xfa){ 54 | mdec->phase=1; 55 | } 56 | return 0; 57 | } 58 | if(mdec->phase==1){ 59 | if((dat&0xc8)==0x08){ 60 | mdec->buf[0]=dat; 61 | mdec->phase=2; 62 | } 63 | return 0; 64 | } 65 | if(mdec->phase==2){ 66 | mdec->buf[1]=dat; 67 | mdec->phase=3; 68 | return 0; 69 | } 70 | if(mdec->phase==3){ 71 | mdec->buf[2]=dat; 72 | mdec->phase=1; 73 | mdec->btn=mdec->buf[0]&0x07; 74 | mdec->x=mdec->buf[1]; 75 | 76 | mdec->y=mdec->buf[2]; 77 | if((mdec->buf[0]&0x10)!=0){ 78 | mdec->x |= 0xffffff00; 79 | } 80 | if((mdec->buf[0]&0x20)!=0){ 81 | mdec->y |= 0xffffff00; 82 | } 83 | mdec->y = - mdec->y; 84 | return 1; 85 | } 86 | return -1; 87 | } 88 | -------------------------------------------------------------------------------- /day9.4/kernel/run: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | cd .. 3 | sudo make 4 | -------------------------------------------------------------------------------- /day9.4/makefile: -------------------------------------------------------------------------------- 1 | subdir=./kernel 2 | all:run 3 | 4 | asm: 5 | # nasm -o bootloader hello.nas 6 | nasm -o bootloader hello.nas 7 | @cd $(subdir);make clean;make addr=0xc400 8 | 9 | img:asm 10 | dd if=bootloader of=myos.img bs=512 count=1 11 | dd if=/dev/zero of=myos.img bs=512 seek=1 skip=1 count=2879 12 | 13 | copy:img 14 | mkdir -p /tmp/floppy 15 | mount -o loop myos.img /tmp/floppy -o fat=12 16 | sleep 1 17 | cp $(subdir)/kernel /tmp/floppy 18 | sleep 1 19 | umount /tmp/floppy 20 | run:copy 21 | qemu-system-i386 -drive file=myos.img,if=floppy 22 | clean: 23 | @rm -f bootloader myos.img haribote.bin 24 | -------------------------------------------------------------------------------- /day9.4/readme: -------------------------------------------------------------------------------- 1 | day9.4 2 | 内存管理,用C语言编写,按照书上代码复制过来。不过我感觉实际中的内存管理没有这么简单吧。。 3 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | 《30天自制操作系统》在Linux下的实践。参考[CSDN米油](http://blog.csdn.net/h397916230)的代码完成(很不幸,此博客现在无法打开),代码命名方面尽量和《30天》中保持一致。 2 | 3 | 用到的技能: 4 | 1.简单的bash操作,包括`ls`, `cd`, `cp`, `mv`, `rm`, `mount`, `dd`, `cat`等 5 | 2.编译器gcc、nasm的基本使用 6 | 3.make工具的简单使用,[参考教程](https://zchrissirhcz.github.io/blog/post/62ce2c40.html) 7 | 4.C语言、汇编语法 8 | 9 | 用到的工具软件: 10 | gcc, make, nasm, qemu, vim(非必须) 11 | 12 | 运行环境: 13 | Fedora20-x86\_64下运行良好 14 | --------------------------------------------------------------------------------