From ffd7be38f87071026e024b54ac6cd8be9ba1fbae Mon Sep 17 00:00:00 2001
From: d0p1 <contact@d0p1.eu>
Date: Fri, 31 Jan 2025 11:51:12 +0100
Subject: [PATCH] chore: sync repo

---
 Makefile             |  3 +++
 bin/ar/main.c        |  5 ++++
 bin/cmd/builtins.inc | 10 ++++----
 bin/ld/coff.c        | 35 +++++++++++++++++++++++++++
 bin/ld/ld.h          | 13 ++++++++++
 bin/ld/main.c        |  2 --
 bin/motd/main.pls    | 56 ++++++++++++++++++++++----------------------
 bin/motd/motd.txt    | 20 ++++++++--------
 bin/ranlib/main.c    |  5 ++++
 bin/readcoff/main.c  | 49 ++++++++++++++++++++++++++++++++++++--
 boot/efi/memory.inc  | 32 +++++++++++++++++++++++++
 boot/efi/uefi.inc    | 42 +++++++++++++++++++++++++++++++++
 include/Makefile     |  2 +-
 include/ar.h         | 26 ++++++++++++++++++++
 include/coff.h       |  1 +
 include/ranlib.h     | 11 +++++++++
 kernel/kernel.asm    |  3 ++-
 tools/Makefile       | 19 ++++++++++-----
 18 files changed, 279 insertions(+), 55 deletions(-)
 create mode 100644 bin/ar/main.c
 create mode 100644 bin/ld/coff.c
 create mode 100644 bin/ld/ld.h
 create mode 100644 bin/ranlib/main.c
 create mode 100644 include/ar.h
 create mode 100644 include/ranlib.h

diff --git a/Makefile b/Makefile
index ddbe0cc..1ca8d44 100644
--- a/Makefile
+++ b/Makefile
@@ -15,7 +15,10 @@ export ASMDIR = /usr/asm
 
 export AS = fasm
 export CC ?= gcc
+export LD = $(TOOLSDIR)/ld
 export RM = rm -f
+export TOOL_CC ?= gcc
+export TOOL_LD ?= ld
 
 export MK_BUGREPORT := \"https://git.cute.engineering/d0p1/StupidOS/issues\"
 export MK_COMMIT    := \"$(shell git rev-parse --short HEAD)\"
diff --git a/bin/ar/main.c b/bin/ar/main.c
new file mode 100644
index 0000000..95485db
--- /dev/null
+++ b/bin/ar/main.c
@@ -0,0 +1,5 @@
+int
+main(void)
+{
+	return 0;
+}
diff --git a/bin/cmd/builtins.inc b/bin/cmd/builtins.inc
index 998055e..523e36d 100644
--- a/bin/cmd/builtins.inc
+++ b/bin/cmd/builtins.inc
@@ -1,6 +1,6 @@
-builtins:
-        db 2, 'cd'
-        db 1, '.'
-        db 3, 'set'
-        db 5, 'unset'
+builtins:
+        db 2, 'cd'
+        db 1, '.'
+        db 3, 'set'
+        db 5, 'unset'
         db 4, 'exit'
\ No newline at end of file
diff --git a/bin/ld/coff.c b/bin/ld/coff.c
new file mode 100644
index 0000000..2bb56f8
--- /dev/null
+++ b/bin/ld/coff.c
@@ -0,0 +1,35 @@
+#include <stdlib.h>
+#include <stdio.h>
+
+#include "coff.h"
+#include "ld.h"
+
+int
+coff_output(LDState *state, FILE *fp)
+{
+	FILHDR file_header;
+	AOUTHDR opt_header;
+
+	file_header.f_magic = F_MACH_I386;
+	file_header.f_timdat = 0;
+	file_header.f_opthdr = AOUTHSZ;
+	file_header.f_flags = F_RELFLG | F_EXEC | F_LNNO | F_LSYMS | F_LITTLE;
+
+	opt_header.magic = ZMAGIC;
+	opt_header.vstamp = 0;
+
+	fwrite(&file_header, FILHSZ, 1, fp);
+	fwrite(&opt_header, AOUTHSZ, 1, fp);
+
+	return (0);
+}
+
+int coff_load(LDState *state, FILE *fp)
+{
+	FILHDR file_header;
+	AOUTHDR opt_header;
+
+	fread(&file_header, FILHSZ, 1, fp);
+	
+	return (0);
+}
diff --git a/bin/ld/ld.h b/bin/ld/ld.h
new file mode 100644
index 0000000..ca00603
--- /dev/null
+++ b/bin/ld/ld.h
@@ -0,0 +1,13 @@
+#ifndef LD_H
+# define LD_H
+
+# include <stdio.h>
+
+typedef struct {
+
+} LDState;
+
+int coff_output(LDState *state, FILE *fp);
+int coff_load(LDState *state, FILE *fp);
+
+#endif /* !LD_H */
diff --git a/bin/ld/main.c b/bin/ld/main.c
index ee3ee82..faa0558 100644
--- a/bin/ld/main.c
+++ b/bin/ld/main.c
@@ -87,7 +87,5 @@ main(int argc, char **argv)
 
 	if (argc <= 1) usage(EXIT_FAILURE);
 
-	
-
 	return (EXIT_SUCCESS);
 }
diff --git a/bin/motd/main.pls b/bin/motd/main.pls
index 3633c3e..dc3876b 100644
--- a/bin/motd/main.pls
+++ b/bin/motd/main.pls
@@ -1,28 +1,28 @@
-/*
- * File: main.pls
- */
-
-/*
- * Procedure: main
- *
- * Program entry point.
- */
-
-
-PROC main;
-	DCL in AS File;
-	DCL line AS String;
-	DCL now AS DateTime;
-
-	now = [[DateTime allocate] now];
-	IF now == NIL THEN exit(1);
-	in = [[File allocate] initOpen:"/etc/motd"];
-	IF in == NIL THEN exit(1);
-
-	WHILE [in readLine:line] != EOF;
-		print(line);
-	END;
-
-	[in release];
-	[now release];
-END;
+/*
+ * File: main.pls
+ */
+
+/*
+ * Procedure: main
+ *
+ * Program entry point.
+ */
+
+
+PROC main;
+	DCL in AS File;
+	DCL line AS String;
+	DCL now AS DateTime;
+
+	now = [[DateTime allocate] now];
+	IF now == NIL THEN exit(1);
+	in = [[File allocate] initOpen:"/etc/motd"];
+	IF in == NIL THEN exit(1);
+
+	WHILE [in readLine:line] != EOF;
+		print(line);
+	END;
+
+	[in release];
+	[now release];
+END;
diff --git a/bin/motd/motd.txt b/bin/motd/motd.txt
index 48ff758..f7e74ac 100644
--- a/bin/motd/motd.txt
+++ b/bin/motd/motd.txt
@@ -1,10 +1,10 @@
-   _____ _               _     _ _____ _____ 
-  /  ___| |             (_)   | |  _  /  ___|
-  \ `--.| |_ _   _ _ __  _  __| | | | \ `--. 
-   `--. \ __| | | | '_ \| |/ _` | | | |`--. \
-  /\__/ / |_| |_| | |_) | | (_| \ \_/ /\__/ /
-  \____/ \__|\__,_| .__/|_|\__,_|\___/\____/ 
-                  | |                        
-                  |_|           v$(version)
-
-$(datetime)
+   _____ _               _     _ _____ _____ 
+  /  ___| |             (_)   | |  _  /  ___|
+  \ `--.| |_ _   _ _ __  _  __| | | | \ `--. 
+   `--. \ __| | | | '_ \| |/ _` | | | |`--. \
+  /\__/ / |_| |_| | |_) | | (_| \ \_/ /\__/ /
+  \____/ \__|\__,_| .__/|_|\__,_|\___/\____/ 
+                  | |                        
+                  |_|           v$(version)
+
+$(datetime)
diff --git a/bin/ranlib/main.c b/bin/ranlib/main.c
new file mode 100644
index 0000000..95485db
--- /dev/null
+++ b/bin/ranlib/main.c
@@ -0,0 +1,5 @@
+int
+main(void)
+{
+	return 0;
+}
diff --git a/bin/readcoff/main.c b/bin/readcoff/main.c
index 4f769cd..7137765 100644
--- a/bin/readcoff/main.c
+++ b/bin/readcoff/main.c
@@ -24,9 +24,38 @@ static const struct {
 	{F_MACH_UNKNOWN, "unknown"}
 };
 
+static const struct {
+	int flag;
+	char *str;
+} FLAGS[] = {
+	{F_RELFLG,   "F_RELFLG"},
+	{F_EXEC,     "F_EXEC"},
+	{F_LNNO,     "F_LNNO"},
+	{F_LSYMS,    "F_LSYMS"},
+	{F_LITTLE,   "F_LITTLE"},
+	{F_BIG,      "F_BIG"},
+	{F_SYMMERGE, "F_SYMMERGE"},
+
+	{0, NULL}
+};
+
+static const struct {
+	int16_t mag;
+	char *str;
+} AOUTMAG[] = {
+	{OMAGIC,  "OMAGIC"},
+	{NMAGIC,  "NMAGIC"},
+	{ZMAGIC,  "ZMAGIC"},
+	{STMAGIC, "STMAGIC"},
+	{SHMAGIC, "SHMAGIC"},
+
+	{0, NULL}
+};
+
 /* */
 static const char *prg_name;
 static const char *mach = NULL;
+static const char *aoutmag = NULL;
 
 /* */
 static int display_header = 0;
@@ -138,7 +167,15 @@ main(int argc, char **argv)
 		printf("  Start of Symbol Table:     %d (bytes into file)\n", file_header.f_symptr);
 		printf("  Number of Symbols:         %d\n", file_header.f_nsyms);
 		printf("  Size of optional header:   %hu\n", file_header.f_opthdr);
-		printf("  Flags:                     0x%hx\n\n", file_header.f_flags);
+		printf("  Flags:                     ");
+		for (idx = 0; FLAGS[idx].str != NULL; idx++)
+		{
+			if (file_header.f_flags & FLAGS[idx].flag)
+			{
+				printf("%s ", FLAGS[idx].str);
+			}
+		}
+		printf("\n\n");
 	}
 
 	if (display_optional_header)
@@ -146,8 +183,16 @@ main(int argc, char **argv)
 		if (file_header.f_opthdr)
 		{
 			fread(&aout_header, 1, AOUTHSZ, fp);
+			for (idx = 0; AOUTMAG[idx].str != NULL; idx++)
+			{
+				if (aout_header.magic == AOUTMAG[idx].mag)
+				{
+					aoutmag = AOUTMAG[idx].str;
+					break;
+				} 
+			}
 			printf("A.out header\n");
-			printf("  Magic: %hx\n", aout_header.magic);
+			printf("  Magic: %s\n", aoutmag);
 			printf("  Entry: 0x%08X\n", aout_header.entry);
 			printf("\n");
 		}
diff --git a/boot/efi/memory.inc b/boot/efi/memory.inc
index 1998b4b..973afbd 100644
--- a/boot/efi/memory.inc
+++ b/boot/efi/memory.inc
@@ -13,6 +13,30 @@ efi_memory_init:
 	mov ecx, [eax + EFI_BOOT_SERVICES.GetMemoryMap]
 	mov [fnGetMemoryMap], ecx
 
+	call efi_get_memory_map
+
+	ret
+
+efi_get_memory_map:
+	push ebp
+	mov ebp, esp
+
+	sub esp, 4
+
+	lea eax, [ebp - 4]
+	EFI_CALL [fnGetMemoryMap], uMemMapSize, 0, eax, uDescSize, uDescVer
+
+	add dword [uMemMapSize], 4096
+
+	EFI_CALL [fnAllocatePool], EFI_LOADER_DATA, [uMemMapSize], pMemMap
+
+	LOG szRes2, eax
+
+	EFI_CALL [fnGetMemoryMap], uMemMapSize, [pMemMap], eax, uDescSize, uDescVer
+
+	LOG szRes, eax
+
+	leave
 	ret
 
 	section '.data' data readable writeable
@@ -23,3 +47,11 @@ fnAllocatePool  dd ?
 fnFreePool      dd ?
 	;; Variable: fnGetMemoryMap
 fnGetMemoryMap  dd ?
+
+uMemMapSize dd 0
+uDescSize   dd 0
+uDescVer    dd 0
+pMemMap     dd 0
+
+szRes du "Result: %x", 0
+szRes2 du "Result: %x", 0
diff --git a/boot/efi/uefi.inc b/boot/efi/uefi.inc
index bfb3271..614ed22 100644
--- a/boot/efi/uefi.inc
+++ b/boot/efi/uefi.inc
@@ -287,6 +287,24 @@ struc EFI_BOOT_SERVICES
 }
 DEFN EFI_BOOT_SERVICES
 
+EFI_RESERVED_MEMORY_TYPE        = 0
+EFI_LOADER_CODE                 = 1
+EFI_LOADER_DATA                 = 2
+EFI_BOOT_SERVICES_CODE          = 3
+EFI_BOOT_SERVICES_DATA          = 4
+EFI_RUNTIME_SERVICES_CODE       = 5
+EFI_RUNTIME_SERVICES_DATA       = 6
+EFI_CONVENTIONAL_MEMORY         = 7
+EFI_UNUSABLE_MEMORY             = 8
+EFI_ACPI_RECLAIM_MEMORY         = 9
+EFI_ACPI_MEMORY_NVS             = 10
+EFI_MEMORY_MAPPED_IO            = 11
+EFI_MEMORY_MAPPED_IO_PORT_SPACE = 12
+EFI_PAL_CODE                    = 13
+EFI_PERSISTENT_MEMORY           = 14
+EFI_UNACCEPTED_MEMORY_TYPE      = 15
+EFI_MAX_MEMORY_TYPE             = 16
+
 EFI_LOCATE_SEARCH_ALL_HANDLES        = 0x0
 EFI_LOCATE_SEARCH_BY_REGISTER_NOTIFY = 0x1
 EFI_LOCATE_SEARCH_BY_PROTOCOL        = 0x2
@@ -298,6 +316,30 @@ EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER = 0x00000008
 EFI_OPEN_PROTOCOL_BY_DRIVER           = 0x00000010
 EFI_OPEN_PROTOCOL_EXCLUSIVE           = 0x00000020
 
+struc EFI_MEMORY_DESCRIPTOR {
+	.type UINT32
+	.physical_start UINT64
+	.virtual_start  UINT64
+	.number_of_pages UINT64
+	.attribute UINT64
+}
+
+EFI_MEMORY_DESCRIPTOR_VERSION = 1
+
+EFI_MEMORY_UC            = 0x0000000000000001
+EFI_MEMORY_WC            = 0x0000000000000002
+EFI_MEMORY_WT            = 0x0000000000000004
+EFI_MEMORY_WB            = 0x0000000000000008
+EFI_MEMORY_UCE           = 0x0000000000000010
+EFI_MEMORY_WP            = 0x0000000000001000
+EFI_MEMORY_RP            = 0x0000000000002000
+EFI_MEMORY_XP            = 0x0000000000004000
+EFI_MEMORY_NV            = 0x0000000000008000
+EFI_MEMORY_MORE_RELIABLE = 0x0000000000010000
+EFI_MEMORY_RO            = 0x0000000000020000
+EFI_MEMORY_SP            = 0x0000000000040000
+EFI_MEMORY_CPU_CRYPTO    = 0x0000000000080000
+EFI_MEMORY_RUNTIME       = 0x8000000000000000 
 	;; ========================================================================
 	;; EFI_RUNTIMES_SERVICES
 	;; ========================================================================
diff --git a/include/Makefile b/include/Makefile
index d0831c7..25c5742 100644
--- a/include/Makefile
+++ b/include/Makefile
@@ -1,4 +1,4 @@
 SYSINCS = errno.h
-INCS = coff.h elf.h $(addprefix sys/, $(SYSINCS))
+INCS = coff.h elf.h ar.h ranlib.h $(addprefix sys/, $(SYSINCS))
 
 include $(TOPDIR)/rules.mk
diff --git a/include/ar.h b/include/ar.h
new file mode 100644
index 0000000..03bc7aa
--- /dev/null
+++ b/include/ar.h
@@ -0,0 +1,26 @@
+#ifndef AR_H
+# define AR_H
+
+#include <stdint.h>
+
+# define ARMAG "!<arch>\n"
+# define SARMAG 8
+
+# define ARFMAG "`\n"
+
+# define AR_SYMTAB_NAME "__.SYMDEF"
+
+typedef struct arhdr {
+	uint8_t ar_name[16];
+	uint8_t ar_date[12];
+	uint8_t ar_uid[6];
+	uint8_t ar_gid[6];
+	uint8_t ar_mode[8];
+	uint8_t ar_size[10];
+	uint8_t ar_fmag[2];
+} AR_HDR;
+
+# define SAR_HDR 60
+
+
+#endif /* !AR_H */
diff --git a/include/coff.h b/include/coff.h
index 6abb327..b49f73e 100644
--- a/include/coff.h
+++ b/include/coff.h
@@ -64,6 +64,7 @@ typedef struct aouthdr
 # define AOUTHSZ sizeof(AOUTHDR)
 
 # define OMAGIC  0404
+# define NMAGIC  0410
 # define ZMAGIC  0413
 # define STMAGIC 0401
 # define SHMAGIC 0443
diff --git a/include/ranlib.h b/include/ranlib.h
new file mode 100644
index 0000000..a7a1016
--- /dev/null
+++ b/include/ranlib.h
@@ -0,0 +1,11 @@
+#ifndef RANLIB_H
+# define RANLIB_H 1
+
+# define SYNTAB "__.SYMDEF"
+
+struct ranlib {
+	int ran_strx;
+	int ran_off;
+};
+
+#endif /* !RANLIB_H */
diff --git a/kernel/kernel.asm b/kernel/kernel.asm
index 85aad9a..9c0cf51 100644
--- a/kernel/kernel.asm
+++ b/kernel/kernel.asm
@@ -13,6 +13,8 @@
 
 	jmp short kmain
 
+	;; XXX: kernel header
+
 	;; Function: kmain
 	;; Kernel entry point
 	;;
@@ -132,7 +134,6 @@ kmain:
 szMsgKernelAlive db "Kernel (", VERSION_FULL , ") is alive", 0
 szMsgBuildDate db "Built ", BUILD_DATE, 0
 szErrorBootProtocol db "Error: wrong magic number", 0
-szKernelHeapStr db "KERNEL-HEAP", 0
 	;; Variable: stBootInfo
 	;; <BootInfo>
 stBootInfo BootInfo
diff --git a/tools/Makefile b/tools/Makefile
index 1838724..f12d9bd 100644
--- a/tools/Makefile
+++ b/tools/Makefile
@@ -1,22 +1,29 @@
-TARGET	= coff-ld$(EXEXT) parted$(EXEXT) readcoff$(EXEXT) elf2coff$(EXEXT)
+TARGET	= ld$(EXEXT) parted$(EXEXT) readcoff$(EXEXT) elf2coff$(EXEXT) ar$(EXEXT) ranlib$(EXEXT)
 
 .PHONY: all
 all: $(TARGET)
 
-coff-ld$(EXEXT): ../bin/ld/main.c
-	$(CC) -o $@ $^ $(CFLAGS) $(LDFLAGS)
+ld$(EXEXT): ../bin/ld/main.c
+	$(TOOL_CC) -o $@ $^ $(CFLAGS) $(LDFLAGS)
 
 readcoff$(EXEXT): ../bin/readcoff/main.c
-	$(CC) -o $@ $^ $(CFLAGS) $(LDFLAGS)
+	$(TOOL_CC) -o $@ $^ $(CFLAGS) $(LDFLAGS)
 
 elf2coff$(EXEXT): ../bin/elf2coff/main.c
-	$(CC) -o $@ $^ $(CFLAGS) $(LDFLAGS)
+	$(TOOL_CC) -o $@ $^ $(CFLAGS) $(LDFLAGS)
 
 parted$(EXEXT): ../sbin/parted/main.c
-	$(CC) -o $@ $^ $(CFLAGS) $(LDFLAGS)
+	$(TOOL_CC) -o $@ $^ $(CFLAGS) $(LDFLAGS)
+
+ranlib$(EXEXT): ../bin/ranlib/main.c
+	$(TOOL_CC) -o $@ $^ $(CFLAGS) $(LDFLAGS)
+
+ar$(EXEXT): ../bin/ar/main.c
+	$(TOOL_CC) -o $@ $^ $(CFLAGS) $(LDFLAGS)
 
 .PHONY: install
 install: $(TARGET)
 
 .PHONY: clean
 clean:
+	$(RM) $(TARGET)