Add mkinitrd
diff --git a/src/Makefile b/src/Makefile
index fc7b8e3..11ea55c 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -5,3 +5,5 @@
mkinitrd:
$(MAKE) -C mkinitrd
+
+.PHONY: kernel mkinitrd
diff --git a/src/mkinitrd/.gitignore b/src/mkinitrd/.gitignore
new file mode 100644
index 0000000..de6da76
--- /dev/null
+++ b/src/mkinitrd/.gitignore
@@ -0,0 +1 @@
+mkinitrd
\ No newline at end of file
diff --git a/src/mkinitrd/Makefile b/src/mkinitrd/Makefile
index e69de29..66b7893 100644
--- a/src/mkinitrd/Makefile
+++ b/src/mkinitrd/Makefile
@@ -0,0 +1,11 @@
+OBJECTS = main.o
+JAYROOT = ../../
+CFLAGS = -O2 -I$(JAYROOT)/include -Wall
+
+mkinitrd: $(OBJECTS)
+ $(CC) -o $@ $^
+
+clean:
+ rm -f *.o mkinitrd
+
+.PHONY: clean
diff --git a/src/mkinitrd/compile_commands.json b/src/mkinitrd/compile_commands.json
index 0637a08..97322af 100644
--- a/src/mkinitrd/compile_commands.json
+++ b/src/mkinitrd/compile_commands.json
@@ -1 +1,15 @@
-[]
\ No newline at end of file
+[
+ {
+ "arguments": [
+ "cc",
+ "-c",
+ "-O2",
+ "-I../..//include",
+ "-o",
+ "main.o",
+ "main.c"
+ ],
+ "directory": "/home/ch/dev/bluejay/src/mkinitrd",
+ "file": "main.c"
+ }
+]
\ No newline at end of file
diff --git a/src/mkinitrd/main.c b/src/mkinitrd/main.c
index ded5af0..228b04b 100644
--- a/src/mkinitrd/main.c
+++ b/src/mkinitrd/main.c
@@ -2,13 +2,65 @@
#include <stdio.h>
#include <string.h>
+int file_size(FILE *file)
+{
+ fseek(file, 0, SEEK_END);
+ int size = ftell(file);
+ fseek(file, 0, SEEK_SET);
+ return size;
+}
+
int main(int argc, char **argv)
{
if (argc == 1 || (argc > 1 && !strcmp(argv[1], "-h")))
{
- printf("%s <output> [input...]\n", argv[0]);
+ fprintf(stderr, "%s <output> [input...]\n", argv[0]);
return argc == 1;
}
- FILE *out = fopen(argv[1], "w");
+ FILE *out; /* output file */
+
+ if (!strcmp(argv[1], "-"))
+ out = stdin;
+ else
+ out = fopen(argv[1], "w");
+
+ if (!out)
+ {
+ fprintf(stderr, "Failed to open %s\n", argv[1]);
+ return 1;
+ }
+
+ /* Header */
+ int num_files = argc - 2;
+ struct initrd_global_header header = {
+ .magic = INITRD_MAGIC,
+ .num_files = num_files,
+ };
+
+ fwrite(&header, sizeof(header), 1, out);
+
+ for (int i = 0; i < num_files; i++)
+ {
+ FILE *in = fopen(argv[i + 2], "rb");
+ if (!in)
+ {
+ fprintf(stderr, "Failed to open %s\n", argv[i + 2]);
+ return 2;
+ }
+
+ struct initrd_file_header file = {
+ .size = file_size(in),
+ };
+
+ strncpy(file.name, 64, argv[i + 2]);
+
+ char c;
+ while ((c = getc(in)) != EOF)
+ {
+ putc(c, out);
+ }
+ fclose(in);
+ }
+ fclose(out);
}