From 0a55862829bb2475cf6a3c67a43f0b0f0409e0b4 Mon Sep 17 00:00:00 2001 From: hinus Date: Sun, 22 Jan 2023 22:05:41 +0800 Subject: [PATCH 1/3] =?UTF-8?q?Issue:=20https://gitee.com/hinus/linux=5Fke?= =?UTF-8?q?rnel=5F012/issues/I6B61Z=20Description:=20=E5=9C=A8=E5=B1=8F?= =?UTF-8?q?=E5=B9=95=E4=B8=8A=E6=89=93=E5=8D=B0=E7=BA=A2=E8=89=B2=E7=9A=84?= =?UTF-8?q?Hello=20World?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bootsect.S | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 bootsect.S diff --git a/bootsect.S b/bootsect.S new file mode 100644 index 0000000..f39309b --- /dev/null +++ b/bootsect.S @@ -0,0 +1,33 @@ +BOOTSEG = 0x7c0 +.code16 +.text +.global _start + +_start: + jmpl $BOOTSEG, $start2 + +start2 : + movw $BOOTSEG, %ax + movw %ax, %ds + movw %ax, %es + movw %ax, %fs + movw %ax, %gs + + movw $msg, %ax + movw %ax, %bp + movw $0x01301, %ax + movw $0x0c, %bx # bh = 0,bl = 0c(红色) + movw $12, %cx # cx = 串长度 + movb $0, %dl + int $0x010 # 10h号中断,通知显卡刷新内容 + +loop: + jmp loop + +msg: +.ascii "Hello World!" + +.org 510 +boot_flag: + .word 0xaa55 + -- Gitee From c27f6237e038ffe479b526dc96f502bf47980152 Mon Sep 17 00:00:00 2001 From: hinus Date: Sun, 22 Jan 2023 22:16:55 +0800 Subject: [PATCH 2/3] =?UTF-8?q?Issue:=20https://gitee.com/hinus/linux=5Fke?= =?UTF-8?q?rnel=5F012/issues/I6B620=20Description:=20=E4=BD=BF=E7=94=A8Mak?= =?UTF-8?q?efile=E7=AE=A1=E7=90=86=E9=A1=B9=E7=9B=AE=EF=BC=8C=E8=8A=82?= =?UTF-8?q?=E7=BA=A6=E8=BE=93=E5=85=A5=E3=80=82=E7=AC=AC=E4=B8=80=E7=AB=A0?= =?UTF-8?q?=E7=9A=84=E6=9C=80=E5=90=8E=E4=B8=80=E8=8A=82=E8=A7=A3=E9=87=8A?= =?UTF-8?q?Makefile=E7=9A=84=E8=A7=84=E5=88=99=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Makefile | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 Makefile diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..e4d0ba4 --- /dev/null +++ b/Makefile @@ -0,0 +1,22 @@ +AS := as +#LD := ld -m elf_i386 +LD := ld -m elf_x86_64 + +LDFLAG := -Ttext 0x0 -s --oformat binary + +image : linux.img + +linux.img : bootsect + cat bootsect > linux.img + +bootsect : bootsect.o + $(LD) $(LDFLAG) -o $@ $< + +bootsect.o : bootsect.S + $(AS) -o $@ $< + +clean: + rm -f *.o + rm -f bootsect + rm -f linux.img + -- Gitee From 47e39e390642ea786e53fa1d59c36654eaf46b9f Mon Sep 17 00:00:00 2001 From: hinus Date: Sun, 22 Jan 2023 22:26:32 +0800 Subject: [PATCH 3/3] =?UTF-8?q?Issue:=20https://gitee.com/hinus/linux=5Fke?= =?UTF-8?q?rnel=5F012/issues/I6B623=20Description:=20=E5=BC=95=E5=85=A5set?= =?UTF-8?q?up.S=EF=BC=8C=E5=90=8C=E6=97=B6=E8=A6=81=E5=BC=95=E5=85=A5build?= =?UTF-8?q?.c=E4=BB=A5=E7=A1=AE=E4=BF=9Dsetup=E7=BC=96=E8=AF=91=E5=90=8E?= =?UTF-8?q?=E5=BF=85=E9=A1=BB=E5=8D=A0=E6=8D=AE4=E4=B8=AA=E7=A3=81?= =?UTF-8?q?=E7=9B=98=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Makefile | 15 ++++-- bootsect.S | 94 +++++++++++++++++++++++++---------- setup.S | 20 ++++++++ tools/build.c | 132 ++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 233 insertions(+), 28 deletions(-) create mode 100644 setup.S create mode 100644 tools/build.c diff --git a/Makefile b/Makefile index e4d0ba4..1a3ffdc 100644 --- a/Makefile +++ b/Makefile @@ -1,13 +1,15 @@ AS := as -#LD := ld -m elf_i386 LD := ld -m elf_x86_64 LDFLAG := -Ttext 0x0 -s --oformat binary image : linux.img -linux.img : bootsect - cat bootsect > linux.img +linux.img : tools/build bootsect setup + ./tools/build bootsect setup > $@ + +tools/build : tools/build.c + gcc -o $@ $< bootsect : bootsect.o $(LD) $(LDFLAG) -o $@ $< @@ -15,8 +17,15 @@ bootsect : bootsect.o bootsect.o : bootsect.S $(AS) -o $@ $< +setup : setup.o + $(LD) $(LDFLAG) -e _start_setup -o $@ $< + +setup.o : setup.S + $(AS) -o $@ $< clean: rm -f *.o rm -f bootsect + rm -f setup + rm -f tools/build rm -f linux.img diff --git a/bootsect.S b/bootsect.S index f39309b..440aadb 100644 --- a/bootsect.S +++ b/bootsect.S @@ -1,33 +1,77 @@ -BOOTSEG = 0x7c0 -.code16 +SETUPLEN = 4 + +BOOTSEG = 0x7c0 + +INITSEG = 0x9000 + +SETUPSEG = 0x9020 + +SYSSEG = 0x1000 + +ENDSEG = SYSSEG + SYSSIZE + +ROOT_DEV = 0x000 + +.code16 .text -.global _start +.global _start _start: - jmpl $BOOTSEG, $start2 - -start2 : - movw $BOOTSEG, %ax - movw %ax, %ds - movw %ax, %es - movw %ax, %fs - movw %ax, %gs - - movw $msg, %ax - movw %ax, %bp - movw $0x01301, %ax - movw $0x0c, %bx # bh = 0,bl = 0c(红色) - movw $12, %cx # cx = 串长度 - movb $0, %dl - int $0x010 # 10h号中断,通知显卡刷新内容 - -loop: - jmp loop + + jmpl $BOOTSEG, $start2 + +start2: + movw $BOOTSEG, %ax + movw %ax, %ds + movw $INITSEG, %ax + movw %ax, %es + movw $256, %cx + subw %si, %si + subw %di, %di + + rep + movsw + + jmpl $INITSEG, $go + +go: + movw %cs, %ax + movw %ax, %ds + movw %ax, %es + movw %ax, %ss + movw $0xFF00, %sp + +load_setup: + movw $0x0000, %dx + movw $0x0002, %cx + movw $0x0200, %bx + movb $SETUPLEN, %al + movb $0x02, %ah + int $0x13 + jnc ok_load_setup + movw $0x0000, %dx + movw $0x0000, %ax + int $0x13 + jmp load_setup + +ok_load_setup: + + movw $msg, %ax + movw %ax, %bp + movw $0x01301, %ax + movw $0x0c, %bx + movw $21, %cx + movb $0, %dl + int $0x010 + + jmpl $SETUPSEG, $0 msg: -.ascii "Hello World!" +.ascii "Setup has been loaded" -.org 510 +.org 508 +root_dev: + .word ROOT_DEV boot_flag: - .word 0xaa55 + .word 0xaa55 diff --git a/setup.S b/setup.S new file mode 100644 index 0000000..714f42b --- /dev/null +++ b/setup.S @@ -0,0 +1,20 @@ +.code16 +.text +.globl _start_setup + +_start_setup: + movw %cs, %ax + movw %ax, %ds + movw %ax, %es + + movw $setup_msg, %ax + movw %ax, %bp + movw $0x01301, %ax + movw $0x0c, %bx + movw $16, %cx + movb $3, %dh + movb $0, %dl + int $0x010 +setup_msg: + .ascii "setup is running" + diff --git a/tools/build.c b/tools/build.c new file mode 100644 index 0000000..7803c7d --- /dev/null +++ b/tools/build.c @@ -0,0 +1,132 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#define MINIX_HEADER 32 +#define GCC_HEADER 1024 + +#define SYS_SIZE 0x2000 + +#define DEFAULT_MAJOR_ROOT 3 +#define DEFAULT_MINOR_ROOT 6 + +#define SETUP_SECTS 4 + +#define STRINGIFY(x) #x + +#define ARG_LEN 3 + +void die(char * str) +{ + fprintf(stderr, "%s\n", str); + exit(1); +} + +void usage(void) +{ + die("Usage: build bootsect setup system [rootdev] [> image]"); +} + +int main(int argc, char **argv) +{ + int i, c, id; + char buf[1024]; + char major_root, minor_root; + struct stat sb; + + if ((argc != ARG_LEN) && (argc != ARG_LEN + 1)) + usage(); + + if (argc == ARG_LEN + 1) + { + if (strcmp(argv[ARG_LEN], "FLOPPY")) + { + if (stat(argv[ARG_LEN], &sb)) + { + perror(argv[ARG_LEN]); + die("Couldn't stat root device"); + } + major_root = sb.st_rdev & 0xff00; + minor_root = sb.st_rdev & 0x00ff; + } + else + { + major_root = 0; + minor_root = 0; + } + } + else + { + major_root = DEFAULT_MAJOR_ROOT; + minor_root = DEFAULT_MINOR_ROOT; + } + fprintf(stderr, "Root device is (%d, %d)\n", major_root, minor_root); + if ((major_root != 2) && (major_root != 3) && (major_root != 0)) + { + fprintf(stderr, "Illegal root device (major = %d)\n", major_root); + die("Bad root device --- major #"); + } + + for (i = 0; i < sizeof(buf); i++) + buf[i] = 0; + + if ((id = open(argv[1], O_RDONLY, 0)) < 0) + die("Unable to open 'boot'"); + + i = read(id, buf, sizeof(buf)); + fprintf(stderr, "Boot sector %d bytes.\n", i); + if (i != 512) + die("Boot block must be exactly 512 bytes"); + + buf[508] = (char) minor_root; + buf[509] = (char) major_root; + i = write(1, buf, 512); + if (i != 512) + die("Write call failed"); + close(id); + + if ((id = open(argv[2], O_RDONLY, 0)) < 0) + die("Unable to open 'setup'"); + + for (i = 0; (c = read(id, buf, sizeof(buf))) > 0; i += c) + if (write(1, buf, c) != c) + die("Write call failed"); + + close(id); + + if (i > SETUP_SECTS * 512) + die("Setup exceeds" STRINGIFY(SETUP_SECTS) " sectors - rewrite build/boot/setup"); + fprintf(stderr, "Setup is %d bytes.\n", i); + for (c = 0; c < sizeof(buf); c++) + buf[c] ='\0'; + while (i < SETUP_SECTS * 512) + { + c = SETUP_SECTS * 512 - i; + if (c > sizeof(buf)) + c = sizeof(buf); + + if (write(1, buf, c) != c) + die("Write call failed"); + i+=c; + } + + /* + if ((id = open(argv[3], O_RDONLY, 0)) < 0) + die("Unable to open 'system'"); + for (i = 0; (c = read(id, buf, sizeof(buf))) > 0; i += c) + if (write(1, buf, c) != c) + die("Write call failed"); + close(id); + fprintf(stderr, "System is %d bytes.\n", i); + if (i > SYS_SIZE * 16) + die("System is too big"); + */ + + return 0; +} + -- Gitee