Skip to content

Commit

Permalink
chore: add 'coff.h' header
Browse files Browse the repository at this point in the history
  • Loading branch information
d0p1s4m4 committed Mar 20, 2024
1 parent 9aaad60 commit 03c95cb
Show file tree
Hide file tree
Showing 14 changed files with 282 additions and 57 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ vmstupid
vmstupid-dbg

/tools/fat
/tools/coff-ld
/sysroot

log.txt
Expand All @@ -26,4 +27,4 @@ a.out
bochsrc.bxrc
/tmp
*.tar.gz
*.sys
*.sys
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ RM = echo
MK_BUGREPORT := \"https://git.cute.engineering/d0p1/StupidOS/issues\"
MK_COMMIT := \"$(shell git rev-parse --short HEAD)\"

SUBDIRS := tools boot kernel lib bin
SUBDIRS := tools include boot kernel lib bin

TARGET = stupid.tar.gz floppy_boot.img
ifneq ($(OS),Windows_NT)
Expand Down
2 changes: 2 additions & 0 deletions bin/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ SUBDIRS = cmd

TOPGOALS = all clean install

SUBDIRS = cmd

.PHONY: $(SUBDIRS)
$(SUBDIRS):
@echo "📁 bin/$@"
Expand Down
3 changes: 2 additions & 1 deletion bin/cmd/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
all:
fasm cmd.asm

clean:

install:
install: all
13 changes: 8 additions & 5 deletions bin/cmd/cmd.asm
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
format ELF
entry start
format COFF

include 'builtins.inc'
section '.text' code

start:
public main
main:
int 0x2A

int 0x2A
section '.data' data

INCLUDE 'builtins.inc'
Binary file added bin/cmd/cmd.obj
Binary file not shown.
16 changes: 16 additions & 0 deletions include/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
INCDIR = $(DESTDIR)/usr/include

INCS = coff.h

.PHONY: all
all:

.PHONY: clean
clean:

.PHONY: install
install: $(INCS)
@ mkdir -p $(INCDIR)
install $< $(INCDIR)

.PHONY: all clean install
103 changes: 103 additions & 0 deletions include/coff.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#ifndef COFF_H
# define COFF_H 1

# include <stdint.h>

typedef struct filehdr
{
uint16_t f_magic;
uint16_t f_nscns;
int32_t f_timdat;
int32_t f_symptr;
int32_t f_nsyms;
uint16_t f_opthdr;
uint16_t f_flags;
} FILHDR;

# define FILHSZ sizeof(FILHDR)

# define F_MACH_I386 0x014c

# define F_RELFLG 0x0001
# define F_EXEC 0x0002
# define F_LNNO 0x0004
# define F_LSYMS 0x0008
# define F_LITTLE 0x0100
# define F_BIG 0x0200
# define F_SYMMERGE 0x1000

typedef struct aouthdr
{
int16_t magic;
int16_t vstamp;
int32_t tsize;
int32_t dsize;
int32_t bsize;
int32_t entry;
int32_t text_start;
int32_t data_start;
} AOUTHDR;

# define AOUTHSZ sizeof(AOUTHDR)

# define OMAGIC 0404
# define ZMAGIC 0413
# define STMAGIC 0401
# define SHMAGIC 0443

typedef struct scnhdr
{
int8_t s_name[8];
int32_t s_paddr;
int32_t s_vaddr;
int32_t s_size;
int32_t s_scnptr;
int32_t s_relptr;
int32_t s_lnnoptr;
uint16_t s_nreloc;
uint16_t s_nlnno;
int32_t s_flags;
} SCNHDR;

# define SCNHSZ sizeof(SCNHDR)

# define STYP_REG 0x000
# define STYP_DSECT 0x001
# define STYP_NOLOAD 0x002
# define STYP_GROUP 0x004
# define STYP_PAD 0x008
# define STYP_COPY 0x010
# define STYP_TEXT 0x020
# define STYP_DATA 0x040
# define STYP_BSS 0x080
# define STYP_INFO 0x200
# define STYP_OVER 0x400
# define STYP_LIB 0x800

typedef struct reloc
{
uint32_t r_vaddr;
uint32_t r_symndx;
uint16_t r_type;
} RELOC;
# define RELSZ 10

# define R_ABS 0x000
# define R_DIR16 0x001
# define R_REL16 0x002
# define R_DIR32 0x006
# define R_PCRLONG 0x024

typedef struct lineno
{
union
{
uint32_t l_symndx;
uint32_t l_paddr;
} l_addr;
uint16_t l_lnno;
} LINENO;

# define LINESZ 6

#endif /* !COFF_H */
50 changes: 25 additions & 25 deletions kernel/Makefile
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
AS = fasm
RM = rm -f
INSTALL = install

KERNEL = vmstupid.sys
SRCS = kernel.asm \
const.inc \
mm/mm.inc

.PHONY: all
all: $(KERNEL)

$(KERNEL): $(SRCS)
$(AS) kernel.asm $@

.PHONY: clean
clean:
$(RM) $(KERNEL)

.PHONY: install
install: $(KERNEL)
@ mkdir -p $(DESTDIR)
install $< $(DESTDIR)

.PHONY: all clean
AS = fasm
RM = rm -f
INSTALL = install

KERNEL = vmstupid.sys
SRCS = kernel.asm \
const.inc \
mm/mm.inc

.PHONY: all
all: $(KERNEL)

$(KERNEL): $(SRCS)
$(AS) kernel.asm $@

.PHONY: clean
clean:
$(RM) $(KERNEL)

.PHONY: install
install: $(KERNEL)
@ mkdir -p $(DESTDIR)
install $< $(DESTDIR)

.PHONY: all clean install
36 changes: 18 additions & 18 deletions kernel/kernel.asm
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
INCLUDE 'const.inc'

ORG KBASE
USE32

jmp kmain

INCLUDE 'mm/mm.inc'

kmain:
nop

_edata:

; BSS
rb 0x4000

_end:
INCLUDE 'const.inc'

ORG KBASE
USE32

jmp kmain

INCLUDE 'mm/mm.inc'

kmain:
nop

_edata:

; BSS
rb 0x4000

_end:
7 changes: 5 additions & 2 deletions tools/Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
CC = gcc
RM = rm -f

TARGET = fat$(EXEXT)
TARGET = fat$(EXEXT) coff-ld$(EXEXT)

CFLAGS = -DMK_COMMIT="$(MK_COMMIT)" -DMK_BUGREPORT="$(MK_BUGREPORT)"
CFLAGS = -DMK_COMMIT="$(MK_COMMIT)" -DMK_BUGREPORT="$(MK_BUGREPORT)" -I../include
LDFLAGS =

.PHONY: all
Expand All @@ -12,5 +12,8 @@ all: $(TARGET)
fat$(EXEXT): fat.c
$(CC) -o $@ $^ $(CFLAGS) $(LDFLAGS)

coff-ld$(EXEXT): coff-ld.c
$(CC) -o $@ $^ $(CFLAGS) $(LDFLAGS)

.PHONY: install
install: $(TARGET)
95 changes: 95 additions & 0 deletions tools/coff-ld.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#include <stdlib.h>
#include <stdio.h>
#include <coff.h>

static char *prg_name;
static char *outfile = "a.out";

static void
usage(int retcode)
{
if (retcode == EXIT_FAILURE)
{
fprintf(stderr, "Try '%s -h' form more information.\n", prg_name);
}
else
{
printf("Usage: %s [-hV] [-o outfile] [OBJS...]\n", prg_name);
printf("\t-h\tdisplay this help and exit\n");
printf("\t-V\toutput version information\n");
printf("\t-o outfile\tout file (default: %s)\n", outfile);

printf("\nReport bugs to <%s>\n", MK_BUGREPORT);
}

exit(retcode);
}

static void
version(void)
{
printf("%s commit %s\n", prg_name, MK_COMMIT);
exit(EXIT_SUCCESS);
}

int
main(int argc, char **argv)
{
FILE *fp;
FILHDR fhdr;
SCNHDR shdr;
AOUTHDR aout;

prg_name = argv[0];

while ((argc > 1) && (argv[1][0] == '-'))
{
switch (argv[1][1])
{
case 'h':
usage(EXIT_SUCCESS);
break;
case 'V':
version();
break;
case 'o':
argv++;
argc--;
if (argc <= 1) usage(EXIT_FAILURE);
outfile = argv[1];
break;
}

argv++;
argc--;
}

if (argc <= 1) usage(EXIT_FAILURE);

fp = fopen(argv[1], "rb");
if (fp == NULL)
{
return (EXIT_FAILURE);
}

fread(&fhdr, 1, FILHSZ, fp);
printf("magic: 0x%hx\n", fhdr.f_magic);
printf("n section: %hd\n", fhdr.f_nscns);
printf("symtab: 0x%X\n", fhdr.f_symptr);
printf("sym entries: %d\n", fhdr.f_nsyms);
printf("optional hdr size: %hu\n", fhdr.f_opthdr);
printf("flags: 0x%hx\n", fhdr.f_flags);

if (fhdr.f_opthdr > 0)
{
fread(&aout, 1, AOUTHSZ, fp);
}

fread(&shdr, 1, SCNHSZ, fp);
printf("name: %c%c%c%c%c%c%c%c\n", shdr.s_name[0], shdr.s_name[1],shdr.s_name[2],shdr.s_name[3],shdr.s_name[4],shdr.s_name[5],shdr.s_name[6],shdr.s_name[7]);
printf("flags: 0x%x\n", shdr.s_flags);

fclose(fp);

return (EXIT_SUCCESS);
}
7 changes: 4 additions & 3 deletions tools/create-iso
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ EOF
gen_iso_file() {
mkdir -p "$2/boot/grub"
echo "$grub_config" > "$2/boot/grub/grub.cfg"
sha256sum "$2/vmstupid.sys" > "$2/boot/hashfile"
sha256sum "$2/stpdboot.sys" >> "$2/boot/hashfile"

(cd "$2";
sha256sum -b -t "vmstupid.sys" > "boot/hashfile";
sha256sum -b -t "stpdboot.sys" >> "boot/hashfile"
)
grub-file --is-x86-multiboot "$2/stpdboot.sys" || exit 1

grub-mkrescue -o $1 $2
Expand Down
Loading

0 comments on commit 03c95cb

Please sign in to comment.