:: commit 6739ff84ff3b4f47ac5ebba610bb82f51c937206

mintsuki <mintsuki@protonmail.com> — 2021-02-21 04:01

parents: 960549377b

tinf: Do not share decompressor and stage2/3 decompression code

diff --git a/Makefile b/Makefile
index 524aac02..59c862c4 100644
--- a/Makefile
+++ b/Makefile
@@ -26,7 +26,7 @@ install: all
 	install -d $(DESTDIR)$(PREFIX)/bin
 	install -s limine-install $(DESTDIR)$(PREFIX)/bin/
 
-bootloader: stage2 decompressor
+bootloader: | decompressor decompressor-clean stage2
 	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
diff --git a/decompressor/Makefile b/decompressor/Makefile
index eae06264..6646f811 100644
--- a/decompressor/Makefile
+++ b/decompressor/Makefile
@@ -5,6 +5,7 @@ OBJCOPY = i386-elf-objcopy
 CFLAGS = -flto -Os -pipe -Wall -Wextra -Werror
 
 INTERNAL_CFLAGS = \
+	-DIN_DECOMPRESSOR \
 	-std=gnu11 \
 	-ffreestanding \
 	-fno-stack-protector \
diff --git a/decompressor/gzip/tinfgzip.c b/decompressor/gzip/tinfgzip.c
index 7935a50e..44e86e08 100644
--- a/decompressor/gzip/tinfgzip.c
+++ b/decompressor/gzip/tinfgzip.c
@@ -23,6 +23,13 @@
  *      distribution.
  */
 
+#ifndef IN_DECOMPRESSOR
+#  include <lib/blib.h>
+#else
+#  define stage3_text
+#  define stage3_data
+#endif
+
 #include "tinf.h"
 
 typedef enum {
@@ -33,7 +40,7 @@ typedef enum {
     FCOMMENT = 16
 } tinf_gzip_flag;
 
-int tinf_gzip_uncompress(void *dest,
+stage3_text int tinf_gzip_uncompress(void *dest,
                          const void *source, unsigned int sourceLen) {
     const unsigned char *src = (const unsigned char *) source;
     unsigned char *dst = (unsigned char *) dest;
diff --git a/decompressor/gzip/tinflate.c b/decompressor/gzip/tinflate.c
index 79d43e83..b45e3b48 100644
--- a/decompressor/gzip/tinflate.c
+++ b/decompressor/gzip/tinflate.c
@@ -23,6 +23,13 @@
  *      distribution.
  */
 
+#ifndef IN_DECOMPRESSOR
+#  include <lib/blib.h>
+#else
+#  define stage3_text
+#  define stage3_data
+#endif
+
 #include "tinf.h"
 
 #include <limits.h>
@@ -55,13 +62,13 @@ struct tinf_data {
 
 /* -- Utility functions -- */
 
-static unsigned int read_le16(const unsigned char *p) {
+stage3_text static unsigned int read_le16(const unsigned char *p) {
     return ((unsigned int) p[0])
          | ((unsigned int) p[1] << 8);
 }
 
 /* Build fixed Huffman trees */
-static void tinf_build_fixed_trees(struct tinf_tree *lt, struct tinf_tree *dt) {
+stage3_text static void tinf_build_fixed_trees(struct tinf_tree *lt, struct tinf_tree *dt) {
     int i;
 
     /* Build fixed literal/length tree */
@@ -103,7 +110,7 @@ static void tinf_build_fixed_trees(struct tinf_tree *lt, struct tinf_tree *dt) {
 }
 
 /* Given an array of code lengths, build a tree */
-static int tinf_build_tree(struct tinf_tree *t, const unsigned char *lengths,
+stage3_text static int tinf_build_tree(struct tinf_tree *t, const unsigned char *lengths,
                            unsigned int num) {
     unsigned short offs[16];
     unsigned int i, num_codes, available;
@@ -167,7 +174,7 @@ static int tinf_build_tree(struct tinf_tree *t, const unsigned char *lengths,
 
 /* -- Decode functions -- */
 
-static void tinf_refill(struct tinf_data *d, int num) {
+stage3_text static void tinf_refill(struct tinf_data *d, int num) {
 
     /* Read bytes until at least num bits available */
     while (d->bitcount < num) {
@@ -182,7 +189,7 @@ static void tinf_refill(struct tinf_data *d, int num) {
 
 }
 
-static unsigned int tinf_getbits_no_refill(struct tinf_data *d, int num) {
+stage3_text static unsigned int tinf_getbits_no_refill(struct tinf_data *d, int num) {
     unsigned int bits;
 
 
@@ -197,18 +204,18 @@ static unsigned int tinf_getbits_no_refill(struct tinf_data *d, int num) {
 }
 
 /* Get num bits from source stream */
-static unsigned int tinf_getbits(struct tinf_data *d, int num) {
+stage3_text static unsigned int tinf_getbits(struct tinf_data *d, int num) {
     tinf_refill(d, num);
     return tinf_getbits_no_refill(d, num);
 }
 
 /* Read a num bit value from stream and add base */
-static unsigned int tinf_getbits_base(struct tinf_data *d, int num, int base) {
+stage3_text static unsigned int tinf_getbits_base(struct tinf_data *d, int num, int base) {
     return base + (num ? tinf_getbits(d, num) : 0);
 }
 
 /* Given a data stream and a tree, decode a symbol */
-static int tinf_decode_symbol(struct tinf_data *d, const struct tinf_tree *t) {
+stage3_text static int tinf_decode_symbol(struct tinf_data *d, const struct tinf_tree *t) {
     int base = 0, offs = 0;
     int len;
 
@@ -241,7 +248,7 @@ static int tinf_decode_symbol(struct tinf_data *d, const struct tinf_tree *t) {
 }
 
 /* Given a data stream, decode dynamic trees from it */
-static int tinf_decode_trees(struct tinf_data *d, struct tinf_tree *lt,
+stage3_text static int tinf_decode_trees(struct tinf_data *d, struct tinf_tree *lt,
                              struct tinf_tree *dt) {
     unsigned char lengths[288 + 32];
 
@@ -367,7 +374,7 @@ static int tinf_decode_trees(struct tinf_data *d, struct tinf_tree *lt,
 /* -- Block inflate functions -- */
 
 /* Given a stream and two trees, inflate a block of data */
-static int tinf_inflate_block_data(struct tinf_data *d, struct tinf_tree *lt,
+stage3_text static int tinf_inflate_block_data(struct tinf_data *d, struct tinf_tree *lt,
                                    struct tinf_tree *dt) {
     /* Extra bits and base tables for length codes */
     static const unsigned char length_bits[30] = {
@@ -452,7 +459,7 @@ static int tinf_inflate_block_data(struct tinf_data *d, struct tinf_tree *lt,
 }
 
 /* Inflate an uncompressed block of data */
-static int tinf_inflate_uncompressed_block(struct tinf_data *d) {
+stage3_text static int tinf_inflate_uncompressed_block(struct tinf_data *d) {
     unsigned int length, invlength;
 
     if (d->source_end - d->source < 4) {
@@ -485,7 +492,7 @@ static int tinf_inflate_uncompressed_block(struct tinf_data *d) {
 }
 
 /* Inflate a block of data compressed with fixed Huffman trees */
-static int tinf_inflate_fixed_block(struct tinf_data *d) {
+stage3_text static int tinf_inflate_fixed_block(struct tinf_data *d) {
     /* Build fixed Huffman trees */
     tinf_build_fixed_trees(&d->ltree, &d->dtree);
 
@@ -494,7 +501,7 @@ static int tinf_inflate_fixed_block(struct tinf_data *d) {
 }
 
 /* Inflate a block of data compressed with dynamic Huffman trees */
-static int tinf_inflate_dynamic_block(struct tinf_data *d) {
+stage3_text static int tinf_inflate_dynamic_block(struct tinf_data *d) {
     /* Decode trees from stream */
     int res = tinf_decode_trees(d, &d->ltree, &d->dtree);
 
@@ -509,7 +516,7 @@ static int tinf_inflate_dynamic_block(struct tinf_data *d) {
 /* -- Public functions -- */
 
 /* Inflate stream from source to dest */
-int tinf_uncompress(void *dest,
+stage3_text int tinf_uncompress(void *dest,
                     const void *source, unsigned int sourceLen) {
     struct tinf_data d;
     int bfinal;
diff --git a/decompressor/main.c b/decompressor/main.c
index de6c6f64..6ef54587 100644
--- a/decompressor/main.c
+++ b/decompressor/main.c
@@ -12,13 +12,12 @@ void entry(uint8_t *compressed_stage2, size_t stage2_size, uint8_t boot_drive, i
     asm volatile (
         "mov esp, 0x7c00\n\t"
         "xor ebp, ebp\n\t"
-        "push %2\n\t"
         "push %1\n\t"
         "push %0\n\t"
         "push 0\n\t"
         "jmp 0x8000\n\t"
         :
-        : "r" ((uint32_t)boot_drive), "r" (pxe), "r" (tinf_gzip_uncompress)
+        : "r" ((uint32_t)boot_drive), "r" (pxe)
         : "memory"
     );
 
diff --git a/limine-pxe.bin b/limine-pxe.bin
index 405cc4e7..56dd0c12 100644
Binary files a/limine-pxe.bin and b/limine-pxe.bin differ
diff --git a/limine.bin b/limine.bin
index 05615af0..27565ac1 100644
Binary files a/limine.bin and b/limine.bin differ
diff --git a/limine.sys b/limine.sys
index 54d47749..d560af4b 100644
Binary files a/limine.sys and b/limine.sys differ
diff --git a/stage2/Makefile b/stage2/Makefile
index 7fce155f..e184deff 100644
--- a/stage2/Makefile
+++ b/stage2/Makefile
@@ -40,8 +40,8 @@ INTERNAL_LDFLAGS = \
 
 .PHONY: all clean
 
-C_FILES := $(shell find ./ -type f -name '*.c' | sort)
-ASM_FILES := $(shell find ./ -type f -name '*.asm' | sort)
+C_FILES := $(shell find -L ./ -type f -name '*.c' | sort)
+ASM_FILES := $(shell find -L ./ -type f -name '*.asm' | sort)
 OBJ := $(ASM_FILES:.asm=.o) $(C_FILES:.c=.o)
 HEADER_DEPS := $(C_FILES:.c=.d)
 
diff --git a/stage2/lib/tinf.c b/stage2/lib/tinf.c
deleted file mode 100644
index fc743120..00000000
--- a/stage2/lib/tinf.c
+++ /dev/null
@@ -1,4 +0,0 @@
-#include <lib/tinf.h>
-
-int (*tinf_gzip_uncompress)(void *dest,
-                            const void *source, unsigned int sourceLen);
diff --git a/stage2/lib/tinf.h b/stage2/lib/tinf.h
deleted file mode 100644
index 313aa39c..00000000
--- a/stage2/lib/tinf.h
+++ /dev/null
@@ -1,7 +0,0 @@
-#ifndef __LIB__TINF_H__
-#define __LIB__TINF_H__
-
-extern int (*tinf_gzip_uncompress)(void *dest,
-                                   const void *source, unsigned int sourceLen);
-
-#endif
diff --git a/stage2/lib/uri.c b/stage2/lib/uri.c
index 152a76c3..0a495f9d 100644
--- a/stage2/lib/uri.c
+++ b/stage2/lib/uri.c
@@ -8,7 +8,7 @@
 #include <mm/pmm.h>
 #include <lib/print.h>
 #include <pxe/tftp.h>
-#include <lib/tinf.h>
+#include <tinf/tinf.h>
 
 // A URI takes the form of: resource://root/path
 // The following function splits up a URI into its componenets
diff --git a/stage2/main.c b/stage2/main.c
index d91657f6..1f3650a2 100644
--- a/stage2/main.c
+++ b/stage2/main.c
@@ -19,11 +19,9 @@
 #include <menu.h>
 #include <pxe/pxe.h>
 #include <pxe/tftp.h>
-#include <lib/tinf.h>
 
-void entry(uint8_t _boot_drive, int pxe_boot, void *_tinf_gzip_uncompress) {
+void entry(uint8_t _boot_drive, int pxe_boot) {
     boot_drive = _boot_drive;
-    tinf_gzip_uncompress = _tinf_gzip_uncompress;
 
     booted_from_pxe = pxe_boot;
 
diff --git a/stage2/tinf b/stage2/tinf
new file mode 120000
index 00000000..6939ed65
--- /dev/null
+++ b/stage2/tinf
@@ -0,0 +1 @@
+../decompressor/gzip/
\ No newline at end of file
tab: 248 wrap: offon