Day21 done.

This commit is contained in:
ghosind 2021-05-07 22:50:06 +08:00
parent 5940814749
commit dc21565634
17 changed files with 71 additions and 230 deletions

@ -60,9 +60,9 @@ app/hello3.hrb: app/a_asm.o
$(CC) -Wall -Wno-format -Wno-unused -std=gnu99 -m32 -c app/hello3.c -o app/hello3.o
$(LD) -m elf_i386 --oformat binary -e main app/hello3.o app/a_asm.o -o app/hello3.hrb
app/crack1.hrb:
app/crack1.hrb: app/a_asm.o
$(CC) -Wall -Wno-format -Wno-unused -std=gnu99 -m32 -c app/crack1.c -o app/crack1.o
$(LD) -m elf_i386 --oformat binary -e main app/crack1.o -o app/crack1.hrb
$(LD) -m elf_i386 --oformat binary -e main app/crack1.o app/a_asm.o -o app/crack1.hrb
kernel.sys: ${K_OBJS} ${L_OBJS}
$(LD) -m elf_i386 --oformat binary -o kernel.sys -T kernel.ld $^

@ -1,9 +1,11 @@
#include "api.h"
#include "console.h"
#include "task.h"
void hrb_api(int edi, int esi, int ebp, int esp, int ebx, int edx, int ecx,
int *hrb_api(int edi, int esi, int ebp, int esp, int ebx, int edx, int ecx,
int eax) {
int cs_base = *((int *)0x0fe8);
struct Task *task = task_now();
struct Console *cons = (struct Console *)*((int *)0x0fec);
if (edx == 1) {
@ -12,5 +14,9 @@ void hrb_api(int edi, int esi, int ebp, int esp, int ebx, int edx, int ecx,
cons_putstr(cons, (char *)ebx + cs_base);
} else if (edx == 3) {
cons_putnstr(cons, (char *)ebx + cs_base, ecx);
} else if (edx == 4) {
return &(task->tss.esp0);
}
return 0;
}

@ -6,49 +6,24 @@
EXTERN hrb_api
asm_hrb_api:
STI
PUSH DS
PUSH ES
PUSHAD
MOV EAX, 1*8
PUSHAD
MOV AX, SS
MOV DS, AX
MOV ECX, [0x0fe4]
ADD ECX, -40
MOV [ECX+32], ESP
MOV [ECX+36], SS
MOV EDX, [ESP]
MOV EBX, [ESP+4]
MOV [ECX], EDX
MOV [ECX+4], EBX
MOV EDX, [ESP+8]
MOV EBX, [ESP+12]
MOV [ECX+4], EDX
MOV [ECX+12], EBX
MOV EDX, [ESP+16]
MOV EBX, [ESP+20]
MOV [ECX+16], EDX
MOV [ECX+20], EBX
MOV EDX, [ESP+24]
MOV EBX, [ESP+28]
MOV [ECX+24], EDX
MOV [ECX+28], EBX
MOV ES, AX
MOV SS, AX
MOV ESP, ECX
STI
CALL hrb_api
MOV ECX, [ESP+32]
MOV EAX, [ESP+36]
CLI
MOV SS, AX
MOV ESP, ECX
CMP EAX, 0
JNE .end_app
ADD ESP, 32
POPAD
POP ES
POP DS
IRETD
.end_app:
MOV ESP, [EAX]
POPAD
RET

@ -1,8 +1,12 @@
#include "app.h"
#include "console.h"
#include "task.h"
int int_handler0d(int *esp) {
int *int_handler0d(int *esp) {
struct Console *cons = (struct Console *)*((int *)0x0fec);
struct Task *task = task_now();
cons_putstr(cons, "\nINT 0D:\n General Protected Exception.\n");
return 1;
return &(task->tss.esp0);
}

@ -1,7 +1,10 @@
void api_putchar(int c);
void api_end(void);
int main(void) {
api_putchar('A');
api_end();
return 0;
}

@ -1,9 +1,13 @@
[BITS 32]
GLOBAL api_putchar
GLOBAL api_putchar, api_end
api_putchar:
MOV EDX, 1
MOV AL, [ESP+4]
INT 0x40
RET
api_end:
MOV EDX, 4
INT 0x40

@ -1,5 +1,9 @@
void api_end(void);
int main() {
*((char *) 0x00102600) = 0;
api_end();
return 0;
}

@ -3,4 +3,6 @@
MOV EAX, 1*8
MOV DS, AX
MOV BYTE [0x102600], 0
RETF
MOV EDX, 4
INT 0x40

@ -12,8 +12,8 @@ put_loop:
JMP put_loop
fin:
RETF
MOV EDX, 4
INT 0x40
msg:
DB "hello", 0
DB 0x41, 0x53

@ -3,8 +3,9 @@
MOV EDX, 2
MOV EBX, msg
INT 0x40
RETF
MOV EDX, 4
INT 0x40
msg:
DB "hello", 0
DB 0x41, 0x53

@ -1,4 +1,5 @@
void api_putchar(int c);
void api_end(void);
int main(void) {
api_putchar('h');
@ -7,5 +8,7 @@ int main(void) {
api_putchar('l');
api_putchar('o');
api_end();
return 0;
}

@ -2,40 +2,24 @@
GLOBAL start_app
start_app: ; void start_app(int eip, int cs, int esp, int ds);
start_app: ; void start_app(int eip, int cs, int esp, int ds, int *tss_esp0);
PUSHAD
MOV EAX, [ESP+36] ; EIP
MOV ECX, [ESP+40] ; CS
MOV EDX, [ESP+44] ; ESP
MOV EBX, [ESP+48] ; DS/SS
MOV [0x0fe4], ESP
CLI
MOV EBP, [ESP+52] ; tss.esp0
MOV [EBP], ESP
MOV [EBP+4], SS
MOV ES, BX
MOV SS, BX
MOV DS, BX
MOV FS, BX
MOV GS, BX
MOV ESP, EDX
STI
OR ECX, 3
OR EBX, 3
PUSH EBX
PUSH EDX
PUSH ECX
PUSH EAX
CALL FAR [ESP]
MOV EAX, 1*8
CLI
MOV ES, AX
MOV SS, AX
MOV DS, AX
MOV FS, AX
MOV GS, AX
MOV ESP, [0x0fe4]
STI
POPAD
RET
RETF

@ -84,6 +84,7 @@ int cmd_app(struct Console *cons, int *fat, char *cmdline) {
struct MemMan *memman = (struct MemMan *)MEMMAN_ADDR;
struct FileInfo *finfo;
struct SegmentDescriptor *gdt = (struct SegmentDescriptor *)ADR_GDT;
struct Task *task = task_now();
char name[18];
int i;
@ -108,31 +109,16 @@ int cmd_app(struct Console *cons, int *fat, char *cmdline) {
}
if (finfo) {
char *p = (char *)memman_alloc_4k(memman, finfo->size + 6);
char *p = (char *)memman_alloc_4k(memman, finfo->size);
char *q = (char *)memman_alloc_4k(memman, 64 * 1024);
*((int *)0x0fe8) = (int)p;
file_load_file(finfo->clustno, finfo->size, p + 6, fat,
file_load_file(finfo->clustno, finfo->size, p, fat,
(char *)(ADR_DISKIMG + 0x003e00));
if (p[finfo->size + 6 - 2] == 0x41 && p[finfo->size + 6 - 1] == 0x53) {
for (int j = 0; j < finfo->size - 1; j++) {
p[j] = p[j + 6];
}
set_segmdesc(gdt + 1003, finfo->size - 1, (int)p, AR_CODE32_ER);
} else {
p[0] = 0xe8;
p[1] = 0x01;
p[2] = 0x00;
p[3] = 0x00;
p[4] = 0x00;
p[5] = 0xcb;
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);
set_segmdesc(gdt + 1003, finfo->size - 1 + 6, (int)p, AR_CODE32_ER);
}
set_segmdesc(gdt + 1004, 64 * 1024 - 1, (int)q, AR_DATA32_RW);
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 + 6);
memman_free_4k(memman, (int)q, 64 * 1024);

@ -25,7 +25,7 @@ void init_gdtidt(void) {
set_gatedesc(idt + 0x21, (int)asm_int_handler21, 2 * 8, AR_INTGATE32);
set_gatedesc(idt + 0x27, (int)asm_int_handler27, 2 * 8, AR_INTGATE32);
set_gatedesc(idt + 0x2c, (int)asm_int_handler2c, 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);
}
void set_segmdesc(struct SegmentDescriptor *sd, unsigned int limit, int base,

@ -1,7 +1,7 @@
#ifndef _API_H_
#define _API_H_
void hrb_api(int edi, int esi, int ebp, int esp, int ebx, int edx, int ecx,
int *hrb_api(int edi, int esi, int ebp, int esp, int ebx, int edx, int ecx,
int eax);
void asm_hrb_api(int edi, int esi, int ebp, int esp, int ebx, int edx, int ecx,

@ -1,8 +1,8 @@
#ifndef _APP_H_
#define _APP_H_
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);
int int_handler0d(int *esp);
int *int_handler0d(int *esp);
#endif // _APP_H_

@ -11,58 +11,23 @@ asm_int_handler0d:
PUSH ES
PUSH DS
PUSHAD
MOV AX, SS
CMP AX, 1*8
JNE .from_app
MOV EAX, ESP
PUSH SS
PUSH EAX
MOV AX, SS
MOV DS, AX
MOV ES, AX
CALL int_handler0d
ADD ESP, 8
POPAD
POP DS
POP ES
ADD ESP, 4
IRETD
.from_app:
CLI
MOV EAX, 1*8
MOV DS, AX
MOV ECX, [0x0fe4]
ADD ECX, -8
MOV [ECX+4], SS
MOV [ECX], ESP
MOV SS, AX
MOV ES, AX
MOV ESP, ECX
STI
CALL int_handler0d
CLI
CMP EAX, 0
JNE .kill
POP ECX
JNE .end_app
POP EAX
MOV SS, AX
MOV ESP, ECX
POPAD
POP DS
POP ES
ADD ESP, 4
IRETD
.kill:
MOV EAX, 1*8
MOV ES, AX
MOV SS, AX
MOV DS, AX
MOV FS, AX
MOV GS, AX
MOV ESP, [0x0fe4]
STI
.end_app:
MOV ESP, [EAX]
POPAD
RET
@ -70,37 +35,13 @@ asm_int_handler20:
PUSH ES
PUSH DS
PUSHAD
MOV AX, SS
CMP AX, 1*8
JNE .from_app
MOV EAX, ESP
PUSH SS
PUSH EAX
MOV AX, SS
MOV DS, AX
MOV ES, AX
CALL int_handler20
ADD ESP, 8
POPAD
POP DS
POP ES
IRETD
.from_app:
MOV EAX, 1*8
MOV DS, AX
MOV ECX, [0x0fe4]
ADD ECX, -8
MOV [ECX+4], SS
MOV [ECX], ESP
MOV SS, AX
MOV ES, AX
MOV ESP, ECX
CALL int_handler20
POP ECX
POP EAX
MOV SS, AX
MOV ESP, ECX
POPAD
POP DS
POP ES
@ -110,37 +51,13 @@ asm_int_handler21:
PUSH ES
PUSH DS
PUSHAD
MOV AX, SS
CMP AX, 1*8
JNE .from_app
MOV EAX, ESP
PUSH SS
PUSH EAX
MOV AX, SS
MOV DS, AX
MOV ES, AX
CALL int_handler21
ADD ESP, 8
POPAD
POP DS
POP ES
IRETD
.from_app:
MOV EAX, 1*8
MOV DS, AX
MOV ECX, [0x0fe4]
ADD ECX, -8
MOV [ECX+4], SS
MOV [ECX], ESP
MOV SS, AX
MOV ES, AX
MOV ESP, ECX
CALL int_handler21
POP ECX
POP EAX
MOV SS, AX
MOV ESP, ECX
POPAD
POP DS
POP ES
@ -150,37 +67,13 @@ asm_int_handler27:
PUSH ES
PUSH DS
PUSHAD
MOV AX, SS
CMP AX, 1*8
JNE .from_app
MOV EAX, ESP
PUSH SS
PUSH EAX
MOV AX, SS
MOV DS, AX
MOV ES, AX
CALL int_handler27
ADD ESP, 8
POPAD
POP DS
POP ES
IRETD
.from_app:
MOV EAX, 1*8
MOV DS, AX
MOV ECX, [0x0fe4]
ADD ECX, -8
MOV [ECX+4], SS
MOV [ECX], ESP
MOV SS, AX
MOV ES, AX
MOV ESP, ECX
CALL int_handler27
POP ECX
POP EAX
MOV SS, AX
MOV ESP, ECX
POPAD
POP DS
POP ES
@ -190,37 +83,13 @@ asm_int_handler2c:
PUSH ES
PUSH DS
PUSHAD
MOV AX, SS
CMP AX, 1*8
JNE .from_app
MOV EAX, ESP
PUSH SS
PUSH EAX
MOV AX, SS
MOV DS, AX
MOV ES, AX
CALL int_handler2c
ADD ESP, 8
POPAD
POP DS
POP ES
IRETD
.from_app:
MOV EAX, 1*8
MOV DS, AX
MOV ECX, [0x0fe4]
ADD ECX, -8
MOV [ECX+4], SS
MOV [ECX], ESP
MOV SS, AX
MOV ES, AX
MOV ESP, ECX
CALL int_handler2c
POP ECX
POP EAX
MOV SS, AX
MOV ESP, ECX
POPAD
POP DS
POP ES