Skip to content

Commit

Permalink
kernel: add reproducible build option in Kconfig and rename final bin…
Browse files Browse the repository at this point in the history
…ary name from Makefile

zeal8bit: os.bin file is now kept, and a new image containing both the OS and the romdisk is generated.
  • Loading branch information
Zeal8bit committed Dec 20, 2022
1 parent bdab0b4 commit ac1d0b4
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 30 deletions.
18 changes: 11 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ export MENUCONFIG_STYLE = aquatic
export OSCONFIG_ASM = include/osconfig.asm
# Output related
BIN=os.bin
# As the first section of the OS must be RST_VECTORS, the final binary is named os_RST_VECTORS.bin
BIN_GENERATED=os_RST_VECTORS.bin
BINDIR=build
FULLBIN=$(BINDIR)/$(BIN)
MAPFILE=os.map
Expand Down Expand Up @@ -54,7 +56,7 @@ endef
# - $2: Variable name where INCLUDES will be stored
# - $3: Variable name where PRECMD will be stored
# - $4: Variable name where POSTCMD will be stored
define IMPORT_subunitmk =
define IMPORT_subunitmk =
SUBMKFILE = $$(wildcard */$$(MKFILE))
TMP1 :=
TMP2 :=
Expand Down Expand Up @@ -87,15 +89,17 @@ LINKERFILE_BUILT=$(BINDIR)/$(LINKERFILE_OBJ)

all:$(KCONFIG_CONFIG) version packer precmd $(LINKERFILE_OBJ) $(OBJS)
$(CC) -o$(FULLBIN) -b -m -s $(LINKERFILE_BUILT) $(BUILTOBJS)
@mv $(BINDIR)/$(BIN_GENERATED) $(FULLBIN)
@echo "OS binary: $(FULLBIN)"
@echo "Executing post commands..."
$(POSTCMD)

# Generate a version file that will be used as a boilerplate
# when the system starts
# Generate a version file that will be used as a boilerplate when the system starts
# We add the build time to the file only if reproducible build is not enabled
version:
@echo Zeal 8-bit OS `git describe --tags` > version.txt
@echo Build time: `date +"%Y-%m-%d %H:%M"` >> version.txt
@[ -z "$(CONFIG_KERNEL_REPRODUCIBLE_BUILD)" ] && \
echo Build time: `date +"%Y-%m-%d %H:%M"` >> version.txt || true

packer:
@echo "Building packer"
Expand All @@ -105,7 +109,7 @@ precmd:
@echo "Executing pre commands..."
@$(PRECMD)

# Check if configuration file exists
# Check if configuration file exists
$(KCONFIG_CONFIG):
@test -e $@ || { echo "Configuration file $@ could not be found. Please run make menuconfig first"; exit 1; }

Expand Down Expand Up @@ -145,10 +149,10 @@ prepare_dirs:
@mkdir -p $(BINDIR)

dump:
$(DISASSEMBLER) -x $(BINDIR)/os.map $(BINDIR)/$(BIN) | less
$(DISASSEMBLER) -x $(BINDIR)/$(MAPFILE) $(BINDIR)/$(BIN) | less

fdump:
$(DISASSEMBLER) -x $(BINDIR)/os.map $(BINDIR)/$(BIN) > $(BINDIR)/os.dump
$(DISASSEMBLER) -x $(BINDIR)/$(MAPFILE) $(BINDIR)/$(BIN) > $(BINDIR)/os.dump

clean:
rm -rf $(OSCONFIG_ASM) $(BINDIR) $(KCONFIG_CONFIG) version.txt
16 changes: 12 additions & 4 deletions kernel/Kconfig
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
menu "Kernel configuration"


config KERNEL_REPRODUCIBLE_BUILD
bool "Generate a reproducible binary"
default n
help
When this option is enabled, compiling the project several times with
the same configuration will produce the exact same binary. Thus, information
like build time will be excluded from the binary.

config KERNEL_STACK_ADDR
hex "Kernel Stack virtual address"
default 0xFFFF
range 0x0000 0xFFFF
help
After initializing the MMU, the kernel will set the Stack Pointer (SP).
This value defines the top of the stack in the virtual memory.
This address must point to RAM.
This address must point to RAM.

config KERNEL_COLDBOOT_HOOK
bool "Hook routine at coldboot"
Expand Down Expand Up @@ -57,7 +65,7 @@ menu "Kernel configuration"
help
Maximum length of a single absolute file path. Incrementing this value will make
the kernel use more RAM.

config KERNEL_INIT_EXECUTABLE
string "Executable to launch on boot"
default "A:/init.bin"
Expand All @@ -79,5 +87,5 @@ menu "Kernel configuration"
help
Kernel logger, by default, prints logs without any color. However, if this option
is enable, it will be able to print error messages in red, warning messages in yellow
and info messages in green.
and info messages in green.
endmenu
41 changes: 22 additions & 19 deletions target/zeal8bit/unit.mk
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ INCLUDES := ./ ./include

SRCS := pio.asm i2c.asm keyboard.asm romdisk.asm mmu.asm interrupt_vect.asm

# Add the suffix "_romdisk" to the full binary name
FULLBIN_W_ROMDISK = $(basename $(FULLBIN))_with_romdisk.img

# If the UART is configured as the standard output, it shall be the first driver to initialize
ifdef CONFIG_TARGET_STDOUT_UART
SRCS := uart.asm $(SRCS)
Expand All @@ -19,27 +22,27 @@ else
endif
endif

# Load the video driver first, in order to get an output early on

# Command to be executed before compiling the whole OS.
# In our case, compile the programs that will be part of ROMDISK and create it.
# After creation, get its size, thanks to `stat` command, and store it in a generated header file
# named `romdisk_info_h.asm`
# Command to be executed before compiling the whole OS.
# In our case, compile the programs that will be part of ROMDISK and create it.
# After creation, get its size, thanks to `stat` command, and store it in a generated header file
# named `romdisk_info_h.asm`
PRECMD := (cd $(PWD)/romdisk && make) && \
SIZE=$$(stat -c %s $(PWD)/romdisk/disk.img) && \
(echo -e "IFNDEF ROMDISK_H\nDEFINE ROMDISK_H\nDEFC ROMDISK_SIZE=$$SIZE\nENDIF" > $(PWD)/include/romdisk_info_h.asm) && \
unset SIZE
unset SIZE

# After compiling the whole OS, we need to remove the unecessary binaries:
# In our case, it's the binary containing BSS addresses, so we only have to keep
# the one containing the actual code. The filename comes from the linker's first
# section name: RST_VECTORS
# FULLBIN defines the expected final binary path/name.
# After selecting the rigth binary, we have to truncate it to a size that will let us
# easily concatenate the ROMDISK after it.
# Of course, the final step is to concatenate the ROMDISK to the final binary after that.
# After compiling the whole OS, we need to remove the unnecessary binaries:
# In our case, it's the binary containing BSS addresses, so we only have to keep
# the one containing the actual code. The filename comes from the linker's first
# section name: RST_VECTORS
# FULLBIN defines the expected final binary path/name.
# After selecting the right binary, we have to truncate it to a size that will let us
# easily concatenate the ROMDISK after it.
# Of course, the final step is to concatenate the ROMDISK to the final binary after that.
POSTCMD := @echo "RAM used by kernel: $$(du -bs $(BINDIR)/*KERNEL_BSS*.bin | cut -f1) bytes" && \
rm $(BINDIR)/*KERNEL_BSS*.bin && mv $(BINDIR)/*RST_VECTORS*.bin $(FULLBIN) && \
echo "OS size: $$(du -bs $(FULLBIN) | cut -f1) bytes" && \
truncate -s $$(( $(CONFIG_ROMDISK_ADDRESS) - $(CONFIG_KERNEL_PHYS_ADDRESS) )) $(FULLBIN) && \
cat $(PWD)/romdisk/disk.img >> $(FULLBIN)
rm $(BINDIR)/*KERNEL_BSS*.bin && \
echo "OS size: $$(du -bs $(FULLBIN) | cut -f1) bytes" && \
cp $(FULLBIN) $(FULLBIN_W_ROMDISK) && \
truncate -s $$(( $(CONFIG_ROMDISK_ADDRESS) - $(CONFIG_KERNEL_PHYS_ADDRESS) )) $(FULLBIN_W_ROMDISK) && \
cat $(PWD)/romdisk/disk.img >> $(FULLBIN_W_ROMDISK) && \
echo "Image size: $$(du -bs $(FULLBIN_W_ROMDISK) | cut -f1) bytes"

0 comments on commit ac1d0b4

Please sign in to comment.