:: commit 5197205697bf8aca23da6c44e9fc0ba186decada

mintsuki <mintsuki@protonmail.com> — 2020-12-31 03:39

parents: 18bfa311de

limine-install: Embed limine.bin inside limine-install

diff --git a/Makefile b/Makefile
index 4969da33..1252f6aa 100644
--- a/Makefile
+++ b/Makefile
@@ -1,20 +1,40 @@
 CC = cc
+OBJCOPY = objcopy
 CFLAGS = -O2 -pipe -Wall -Wextra
+PREFIX = /usr/local
+DESTDIR =
+
 PATH := $(shell pwd)/toolchain/bin:$(PATH)
 
-.PHONY: all clean stage2 stage2-clean decompressor decompressor-clean toolchain test.hdd echfs-test ext2-test fat32-test
+.PHONY: all clean install bootloader bootloader-clean distclean stage2 stage2-clean decompressor decompressor-clean toolchain test.hdd echfs-test ext2-test fat32-test
+
+all: limine-install
+
+limine-install: limine-install.c limine.o
+	$(CC) $(CFLAGS) limine.o limine-install.c -o limine-install
+
+limine.o: limine.bin
+	$(OBJCOPY) -I binary -O default limine.bin limine.o
 
-all: stage2 decompressor
+limine.bin: bootloader
+
+clean:
+	rm -f limine.o limine-install
+
+install: all
+	install -d $(DESTDIR)$(PREFIX)/bin
+	install -s limine-install $(DESTDIR)$(PREFIX)/bin/
+
+bootloader: stage2 decompressor
 	gzip -n -9 < stage2/stage2.bin > stage2/stage2.bin.gz
 	cd bootsect && nasm bootsect.asm -fbin -o ../limine.bin
 	cd pxeboot && nasm bootsect.asm -fbin -o ../limine-pxe.bin
 	cp stage2/stage2.map ./
 
-clean: stage2-clean decompressor-clean test-clean
+bootloader-clean: stage2-clean decompressor-clean test-clean
 	rm -f stage2/stage2.bin.gz test/stage2.map test.hdd
 
-distclean: clean
-	rm -f limine-install
+distclean: clean bootloader-clean
 
 stage2:
 	$(MAKE) -C stage2 all
@@ -34,16 +54,13 @@ test-clean:
 toolchain:
 	cd toolchain && ./make_toolchain.sh -j`nproc`
 
-limine-install: limine-install.c
-	$(CC) $(CFLAGS) limine-install.c -o limine-install
-
 test.hdd:
 	rm -f test.hdd
 	dd if=/dev/zero bs=1M count=0 seek=64 of=test.hdd
 	parted -s test.hdd mklabel gpt
 	parted -s test.hdd mkpart primary 2048s 100%
 
-echfs-test: all limine-install test.hdd
+echfs-test: all test.hdd
 	$(MAKE) -C test
 	echfs-utils -g -p0 test.hdd quick-format 512 > part_guid
 	sed "s/@GUID@/`cat part_guid`/g" < test/limine.cfg > limine.cfg.tmp
@@ -56,7 +73,7 @@ echfs-test: all limine-install test.hdd
 	./limine-install limine.bin test.hdd
 	qemu-system-x86_64 -net none -smp 4 -enable-kvm -cpu host -hda test.hdd -debugcon stdio
 
-ext2-test: all limine-install test.hdd
+ext2-test: all test.hdd
 	$(MAKE) -C test
 	cp stage2.map test/
 	rm -rf test_image/
@@ -74,7 +91,7 @@ ext2-test: all limine-install test.hdd
 	./limine-install limine.bin test.hdd
 	qemu-system-x86_64 -net none -smp 4 -enable-kvm -cpu host -hda test.hdd -debugcon stdio
 
-fat32-test: all limine-install test.hdd
+fat32-test: all test.hdd
 	$(MAKE) -C test
 	cp stage2.map test/
 	rm -rf test_image/
diff --git a/limine-install.c b/limine-install.c
index a92b727f..ed26bab5 100644
--- a/limine-install.c
+++ b/limine-install.c
@@ -230,39 +230,32 @@ static bool device_write(const void *buffer, uint64_t loc, size_t count) {
     return true;
 }
 
+extern uint8_t _binary_limine_bin_start[], _binary_limine_bin_end[];
+
 int main(int argc, char *argv[]) {
     int      ok = 1;
-    FILE    *bootloader_file = NULL;
-    uint8_t *bootloader_img = NULL;
+    uint8_t *bootloader_img = _binary_limine_bin_start;
+    size_t   bootloader_file_size =
+        (size_t)_binary_limine_bin_end - (size_t)_binary_limine_bin_start;
     uint8_t  orig_mbr[70], timestamp[6];
 
-    if (argc < 3) {
-        printf("Usage: %s <bootloader image> <device> [GPT partition index]\n", argv[0]);
-        return 1;
+    if (argc > 1 && strstr("limine.bin", argv[1]) != NULL) {
+        fprintf(stderr,
+            "WARNING: Passing the bootloader binary as a file argument is\n"
+            "         deprecated and should be avoided in the future.\n");
+        argc--;
+        argv[1] = argv[0];
+        argv++;
     }
 
-    bootloader_file = fopen(argv[1], "rb");
-    if (bootloader_file == NULL) {
-        perror("Error: ");
+    if (argc < 2) {
+        printf("Usage: %s <device> [GPT partition index]\n", argv[0]);
         return 1;
     }
 
-    fseek(bootloader_file, 0, SEEK_END);
-    size_t bootloader_file_size = ftell(bootloader_file);
-
-    bootloader_img = malloc(bootloader_file_size);
-    if (bootloader_img == NULL) {
-        perror("Error: ");
-        goto cleanup;
-    }
-
-    // Load in bootloader image
-    fseek(bootloader_file, 0, SEEK_SET);
-    fread(bootloader_img, 1, bootloader_file_size, bootloader_file);
-
-    device = open(argv[2], O_RDWR);
+    device = open(argv[1], O_RDWR);
     if (device == -1) {
-        perror("Error: ");
+        perror("Error");
         goto cleanup;
     }
 
@@ -454,10 +447,6 @@ cleanup:
         free(cache);
     if (device != -1)
         close(device);
-    if (bootloader_file)
-        fclose(bootloader_file);
-    if (bootloader_img)
-        free(bootloader_img);
 
     return ok;
 }
tab: 248 wrap: offon