Skip to content

Commit

Permalink
保护操作系统(4)
Browse files Browse the repository at this point in the history
  • Loading branch information
yourtion committed May 4, 2016
1 parent 4241eb2 commit e846dbd
Show file tree
Hide file tree
Showing 12 changed files with 98 additions and 244 deletions.
4 changes: 2 additions & 2 deletions 21_day/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ haribote.sys : asmhead.bin bootpack.hrb Makefile
copy /B asmhead.bin+bootpack.hrb haribote.sys

crack1.bim : crack1.obj Makefile
$(OBJ2BIM) @$(RULEFILE) out:crack1.bim map:crack1.map crack1.obj
$(OBJ2BIM) @$(RULEFILE) out:crack1.bim map:crack1.map crack1.obj a_nask.obj

crack1.hrb : crack1.bim Makefile
$(BIM2HRB) crack1.bim crack1.hrb 0
Expand All @@ -90,7 +90,7 @@ haribote.img : ipl10.bin haribote.sys Makefile \
copy from:crack1.hrb to:@: \
copy from:crack2.hrb to:@: \
imgout:haribote.img

# 其他指令

%.gas : %.c bootpack.h Makefile
Expand Down
3 changes: 2 additions & 1 deletion 21_day/a.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
void api_putchar(int c);
void api_end(void);

void HariMain(void)
{
api_putchar('A');
return;
api_end();
}
4 changes: 4 additions & 0 deletions 21_day/a_nask.nas
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
[FILE "a_nask.nas"] ; 源文件名信息

GLOBAL _api_putchar
GLOBAL _api_end

[SECTION .text]

Expand All @@ -12,3 +13,6 @@ _api_putchar: ; void api_putchar(int c);
MOV AL,[ESP+4] ; c
INT 0x40
RET
_api_end: ; void api_end(void);
MOV EDX,4
INT 0x40
6 changes: 3 additions & 3 deletions 21_day/bootpack.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ unsigned int memtest_sub(unsigned int start, unsigned int end);
void farjmp(int eip, int cs);
void farcall(int eip, int cs);
void asm_hrb_api(void);
void start_app(int eip, int cs, int esp, int ds);
void start_app(int eip, int cs, int esp, int ds, int *tss_esp0);

/* fifo.c */
struct FIFO32 {
Expand Down Expand Up @@ -247,8 +247,8 @@ void cmd_cls(struct CONSOLE *cons);
void cmd_dir(struct CONSOLE *cons);
void cmd_type(struct CONSOLE *cons, int *fat, char *cmdline);
int cmd_app(struct CONSOLE *cons, int *fat, char *cmdline);
void hrb_api(int edi, int esi, int ebp, int esp, int ebx, int edx, int ecx, int eax);
int inthandler0d(int *esp);
int *hrb_api(int edi, int esi, int ebp, int esp, int ebx, int edx, int ecx, int eax);
int *inthandler0d(int *esp);

/* file.c */
struct FILEINFO {
Expand Down
22 changes: 13 additions & 9 deletions 21_day/console.c
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,6 @@ void cmd_type(struct CONSOLE *cons, int *fat, char *cmdline)
struct MEMMAN *memman = (struct MEMMAN *) MEMMAN_ADDR;
struct FILEINFO *finfo = file_search(cmdline + 5, (struct FILEINFO *) (ADR_DISKIMG + 0x002600), 224);
char *p;
int i;
if (finfo != 0) {
/*找到文件的情况*/
p = (char *) memman_alloc_4k(memman, finfo->size);
Expand All @@ -258,6 +257,7 @@ int cmd_app(struct CONSOLE *cons, int *fat, char *cmdline)
struct FILEINFO *finfo;
struct SEGMENT_DESCRIPTOR *gdt = (struct SEGMENT_DESCRIPTOR *) ADR_GDT;
char name[18], *p, *q;
struct TASK *task = task_now();
int i;

/*根据命令行生成文件名*/
Expand Down Expand Up @@ -287,8 +287,8 @@ int cmd_app(struct CONSOLE *cons, int *fat, char *cmdline)
q = (char *) memman_alloc_4k(memman, 64 * 1024);
*((int *) 0xfe8) = (int) p;
file_loadfile(finfo->clustno, finfo->size, p, fat, (char *) (ADR_DISKIMG + 0x003e00));
set_segmdesc(gdt + 1003, finfo->size - 1, (int) p, AR_CODE32_ER);
set_segmdesc(gdt + 1004, 64 * 1024 - 1, (int) q, AR_DATA32_RW);
set_segmdesc(gdt + 1003, finfo->size - 1, (int) p, AR_CODE32_ER + 0x60);
set_segmdesc(gdt + 1004, 64 * 1024 - 1, (int) q, AR_DATA32_RW + 0x60);
if (finfo->size >= 8 && strncmp(p + 4, "Hari", 4) == 0) {
p[0] = 0xe8;
p[1] = 0x16;
Expand All @@ -297,7 +297,7 @@ int cmd_app(struct CONSOLE *cons, int *fat, char *cmdline)
p[4] = 0x00;
p[5] = 0xcb;
}
start_app(0, 1003 * 8, 64 * 1024, 1004 * 8);
start_app(0, 1003 * 8, 64 * 1024, 1004 * 8, &(task->tss.esp0));
memman_free_4k(memman, (int) p, finfo->size);
memman_free_4k(memman, (int) q, 64 * 1024);
cons_newline(cons);
Expand All @@ -307,23 +307,27 @@ int cmd_app(struct CONSOLE *cons, int *fat, char *cmdline)
return 0;
}

void hrb_api(int edi, int esi, int ebp, int esp, int ebx, int edx, int ecx, int eax)
int *hrb_api(int edi, int esi, int ebp, int esp, int ebx, int edx, int ecx, int eax)
{
int cs_base = *((int *) 0xfe8);
struct TASK *task = task_now();
struct CONSOLE *cons = (struct CONSOLE *) *((int *) 0x0fec);
if (edx == 1) {
cons_putchar(cons, eax & 0xff, 1);
} else if (edx == 2) {
cons_putstr0(cons, (char *) ebx + cs_base);
} else if (edx == 3) {
cons_putstr1(cons, (char *) ebx + cs_base, ecx);
} else if (edx == 4) {
return &(task->tss.esp0);
}
return;
return 0;
}

int inthandler0d(int *esp)
int *inthandler0d(int *esp)
{
struct CONSOLE *cons = (struct CONSOLE *) *((int *) 0x0fec);
cons_putstr0(cons, "¥nINT 0D :¥n General Protected Exception.¥n");
return 1; /*强制结束程序*/
struct TASK *task = task_now();
cons_putstr0(cons, "\nINT 0D :\n General Protected Exception.\n");
return &(task->tss.esp0); /*强制结束程序*/
}
6 changes: 4 additions & 2 deletions 21_day/crack1.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
void api_end(void);

void HariMain(void)
{
*((char *) 0x00102600) = 0;
return;
}
api_end();
}
3 changes: 2 additions & 1 deletion 21_day/crack2.nas
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
MOV EAX,1*8 ; OS用的段号
MOV DS,AX ; 将其存入DS
MOV BYTE [0x102600],0
RETF
MOV EDX,4
INT 0x40
2 changes: 1 addition & 1 deletion 21_day/dsctbl.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ void init_gdtidt(void)
set_gatedesc(idt + 0x21, (int) asm_inthandler21, 2 * 8, AR_INTGATE32);
set_gatedesc(idt + 0x27, (int) asm_inthandler27, 2 * 8, AR_INTGATE32);
set_gatedesc(idt + 0x2c, (int) asm_inthandler2c, 2 * 8, AR_INTGATE32);
set_gatedesc(idt + 0x40, (int) asm_hrb_api, 2 * 8, AR_INTGATE32);
set_gatedesc(idt + 0x40, (int) asm_hrb_api, 2 * 8, AR_INTGATE32 + 0x60);

return;
}
Expand Down
3 changes: 2 additions & 1 deletion 21_day/hello.nas
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ putloop:
ADD ECX,1
JMP putloop
fin:
RETF
MOV EDX,4
INT 0x40
msg:
DB "hello",0
3 changes: 2 additions & 1 deletion 21_day/hello2.nas
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
MOV EDX,2
MOV EBX,msg
INT 0x40
RETF
MOV EDX,4
INT 0x40
msg:
DB "hello",0
3 changes: 2 additions & 1 deletion 21_day/hello3.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
void api_putchar(int c);
void api_end(void);

void HariMain(void)
{
Expand All @@ -7,5 +8,5 @@ void HariMain(void)
api_putchar('l');
api_putchar('l');
api_putchar('o');
return;
api_end();
}
Loading

0 comments on commit e846dbd

Please sign in to comment.