diff --git a/build.sh b/build.sh index 51b58b7..7b307ad 100755 --- a/build.sh +++ b/build.sh @@ -30,12 +30,12 @@ build_bootloader () { if [ "$FORCE_CLEAN" = true ]; then make clean fi - FLAGS="$USER_FLAGS" - if [ ! -z "${GIT_BRANCH+x}" ]; then FLAGS+=" -DGIT_BRANCH='\"$GIT_BRANCH\"'"; fi - if [ ! -z "${GIT_TAG+x}" ]; then FLAGS+=" -DGIT_TAG='\"$GIT_TAG\"'"; fi - if [ ! -z "${GIT_SHA+x}" ]; then FLAGS+=" -DGIT_SHA='\"$GIT_SHA\"'"; fi - if [ ! -z "${GIT_MESSAGE+x}" ]; then FLAGS+=" -DGIT_MESSAGE='\"$GIT_MESSAGE\"'"; fi - make all -j USER_FLAGS="$FLAGS" + VERSION="" + if [ ! -z "${GIT_BRANCH+x}" ]; then VERSION+=" -DGIT_BRANCH='\"$GIT_BRANCH\"'"; fi + if [ ! -z "${GIT_TAG+x}" ]; then VERSION+=" -DGIT_TAG='\"$GIT_TAG\"'"; fi + if [ ! -z "${GIT_SHA+x}" ]; then VERSION+=" -DGIT_SHA='\"$GIT_SHA\"'"; fi + if [ ! -z "${GIT_MESSAGE+x}" ]; then VERSION+=" -DGIT_MESSAGE='\"$GIT_MESSAGE\"'"; fi + make all -j VERSION="$VERSION" USER_FLAGS="$USER_FLAGS" popd > /dev/null BUILT_BOOTLOADER=true diff --git a/sw/bootloader/Makefile b/sw/bootloader/Makefile index 145d929..2b9f5c3 100644 --- a/sw/bootloader/Makefile +++ b/sw/bootloader/Makefile @@ -10,6 +10,7 @@ FLAGS = -march=vr4300 -mtune=vr4300 $(USER_FLAGS) CFLAGS = -Os -Wall -ffunction-sections -fdata-sections -ffreestanding -MMD -MP ASFLAGS = -Wa,-I$(N64_INST)/mips64-elf/lib LDFLAGS = -lc -nostartfiles -Wl,--gc-sections +VERSION ?= SRC_DIR = src ASSET_DIR = assets @@ -57,6 +58,9 @@ $(BUILD_DIR)/%.S.o: %.S $(BUILD_DIR)/%.c.o: %.c $(CC) $(FLAGS) $(CFLAGS) -c $< -o $@ +$(BUILD_DIR)/version.c.o: version.c .FORCE + $(CC) $(FLAGS) $(CFLAGS) $(VERSION) -c $< -o $@ + $(BUILD_DIR)/%.asset: $(ASSET_DIR)/%.png tools/asset_converter.py $(PYTHON) tools/asset_converter.py $< $@ --compress @@ -67,13 +71,9 @@ $(BUILD_DIR)/bootloader.elf: $(OBJS) N64.ld $(CXX) $(FLAGS) $(LDFLAGS) -TN64.ld $(OBJS) -o $@ @$(OBJDUMP) -S $@ > $(BUILD_DIR)/bootloader.lst -$(BUILD_DIR)/bootloader.bin: $(BUILD_DIR)/bootloader.elf tools/strip.py +$(BUILD_DIR)/bootloader.bin: $(BUILD_DIR)/bootloader.elf tools/finalize.py @$(OBJCOPY) -O binary $< $@ - @chksum64 $@ > /dev/null - @$(PYTHON) tools/strip.py $@ 1024 - -$(BUILD_DIR)/bootloader.hex: $(BUILD_DIR)/bootloader.bin - @$(OBJCOPY) -I binary -O ihex $< $@ + @$(PYTHON) tools/finalize.py $@ > /dev/null print_size: $(BUILD_DIR)/bootloader.elf @echo 'Size of modules:' @@ -81,13 +81,11 @@ print_size: $(BUILD_DIR)/bootloader.elf @echo 'Size of bootloader:' @$(SIZE) -B -d $< -all: $(BUILD_DIR)/bootloader.hex print_size +all: $(BUILD_DIR)/bootloader.bin print_size clean: @rm -rf ./$(BUILD_DIR)/* -$(BUILD_DIR)/version.c.o: .FORCE - .FORCE: .PHONY: .FORCE all clean print_size diff --git a/sw/bootloader/N64.ld b/sw/bootloader/N64.ld index 5044985..0d88ed9 100644 --- a/sw/bootloader/N64.ld +++ b/sw/bootloader/N64.ld @@ -45,12 +45,6 @@ SECTIONS { _ebss = .; } > ram - .fill : { - FILL(0xFF); - BYTE(0xFF); - . = ORIGIN(rom) + LENGTH(rom); - } > rom - _sheap = .; . = ORIGIN(ram) + LENGTH(ram) - __exception_stack_size - __stack_size; _eheap = .; diff --git a/sw/bootloader/assets/assets.S b/sw/bootloader/assets/assets.S index 8c43d78..b02f9e7 100644 --- a/sw/bootloader/assets/assets.S +++ b/sw/bootloader/assets/assets.S @@ -1,4 +1,5 @@ -.section .assets.@sym@ +.section .assets.@sym@, "a", %progbits +.type assets_@sym@, %object .global assets_@sym@ assets_@sym@: .incbin "@file@" diff --git a/sw/bootloader/src/display.c b/sw/bootloader/src/display.c index 4023f60..a87f867 100644 --- a/sw/bootloader/src/display.c +++ b/sw/bootloader/src/display.c @@ -14,7 +14,7 @@ #define LINE_SPACING (2) -static io32_t display_framebuffer[SCREEN_WIDTH * SCREEN_HEIGHT] __attribute__((section(".framebuffer"))); +static io32_t display_framebuffer[SCREEN_WIDTH * SCREEN_HEIGHT] __attribute__((section(".framebuffer, \"aw\", %nobits#"))); static int char_x; static int char_y; static const vi_regs_t vi_config[] = {{ diff --git a/sw/bootloader/src/exception.S b/sw/bootloader/src/exception.S index 0919434..7eb580c 100644 --- a/sw/bootloader/src/exception.S +++ b/sw/bootloader/src/exception.S @@ -7,6 +7,7 @@ #define VECTOR_SIZE (0x80) #define VECTOR_NUM (4) +#define ZR_OFFSET (0) #define AT_OFFSET (8) #define V0_OFFSET (16) #define V1_OFFSET (24) @@ -49,6 +50,7 @@ exception_handler: .set noat la $k0, (_esp - SAVE_REGISTERS_SIZE) + sd $zero, ZR_OFFSET($k0) sd $at, AT_OFFSET($k0) sd $v0, V0_OFFSET($k0) sd $v1, V1_OFFSET($k0) diff --git a/sw/bootloader/src/ipl2.S b/sw/bootloader/src/ipl2.S index ed074f0..23f9f6a 100644 --- a/sw/bootloader/src/ipl2.S +++ b/sw/bootloader/src/ipl2.S @@ -2,7 +2,8 @@ .set noreorder -.section .data.ipl2 +.section .text.ipl2, "ax", %progbits +.type ipl2, %object ipl2: .global ipl2 lui $t5, 0xBFC0 diff --git a/sw/bootloader/src/version.c b/sw/bootloader/src/version.c index 645dfc8..ce98e2b 100644 --- a/sw/bootloader/src/version.c +++ b/sw/bootloader/src/version.c @@ -6,21 +6,25 @@ static version_t version = { .git_branch = GIT_BRANCH, #else #warning "No GIT_BRANCH provided" + .git_branch = "GIT_BRANCH", #endif #ifdef GIT_TAG .git_tag = GIT_TAG, #else #warning "No GIT_TAG provided" + .git_tag = "GIT_TAG", #endif #ifdef GIT_SHA .git_sha = GIT_SHA, #else #warning "No GIT_SHA provided" + .git_sha = "GIT_SHA", #endif #ifdef GIT_MESSAGE .git_message = GIT_MESSAGE, #else #warning "No GIT_MESSAGE provided" + .git_message = "GIT_MESSAGE", #endif }; diff --git a/sw/bootloader/tools/asset_converter.py b/sw/bootloader/tools/asset_converter.py index b0f7da3..622273e 100644 --- a/sw/bootloader/tools/asset_converter.py +++ b/sw/bootloader/tools/asset_converter.py @@ -33,9 +33,9 @@ def compress(data: bytes, step_size: int=4) -> bytes: return compressed_data -if __name__ == "__main__": +if __name__ == '__main__': if (len(sys.argv) < 3): - print(f"Usage: python {sys.argv[0]} input_path output_path [--compress]") + print(f'Usage: python {sys.argv[0]} input_path output_path [--compress]') sys.exit(1) asset_input = sys.argv[1] @@ -47,17 +47,22 @@ if __name__ == "__main__": try: source_asset = Image.open(asset_input) - converted_asset = source_asset.convert("RGBA").tobytes() + converted_asset = source_asset.convert('RGBA').tobytes() + if (asset_compress): converted_asset = compress(converted_asset) - final_asset = open(asset_output, "wb") + + final_asset = open(asset_output, 'wb') final_asset.write(converted_asset) + except FileNotFoundError: - print(f"Couldn't open file \"{asset_input}\"") + print(f'Couldn\'t open file "{asset_input}"') sys.exit(2) + except Exception as e: print(e) sys.exit(3) + finally: if (source_asset): source_asset.close() diff --git a/sw/bootloader/tools/finalize.py b/sw/bootloader/tools/finalize.py new file mode 100644 index 0000000..4fa675d --- /dev/null +++ b/sw/bootloader/tools/finalize.py @@ -0,0 +1,50 @@ +#!/usr/bin/env python3 + +import subprocess +import sys + + + +if __name__ == '__main__': + if (len(sys.argv) != 2): + print(f'Usage: python {sys.argv[0]} file_path') + sys.exit(1) + + ALIGN = 1024 + CHECKSUM_SIZE = 0x101000 + + bin_file = sys.argv[1] + + try: + bin_data = b'' + + with open(bin_file, 'rb') as f: + bin_data = f.read() + + pad_size = CHECKSUM_SIZE - len(bin_data) + + if (pad_size > 0): + bin_data += b'\xFF' * pad_size + with open(bin_file, 'wb') as f: + f.write(bin_data) + + subprocess.run(['chksum64', bin_file]) + + with open(bin_file, 'rb') as f: + bin_data = f.read() + + bin_data = bin_data.strip(b'\xFF') + modulo = len(bin_data) % ALIGN + if (modulo > 0): + bin_data += b'\xFF' * (ALIGN - modulo) + + with open(bin_file, 'wb') as f: + f.write(bin_data) + + except FileNotFoundError as e: + print(f'Couldn\'t open file "{bin_file}" {e}') + sys.exit(2) + + except Exception as e: + print(e) + sys.exit(3) diff --git a/sw/bootloader/tools/strip.py b/sw/bootloader/tools/strip.py deleted file mode 100644 index 568e0ed..0000000 --- a/sw/bootloader/tools/strip.py +++ /dev/null @@ -1,28 +0,0 @@ -#!/usr/bin/env python3 - -import sys - - - -if __name__ == "__main__": - if (len(sys.argv) != 3): - print(f"Usage: python {sys.argv[0]} file_path align") - sys.exit(1) - - file = sys.argv[1] - align = int(sys.argv[2], base=0) - - try: - data = b'' - with open(file, 'rb') as f: - data = f.read().strip(b'\xFF') - with open(file, 'wb') as f: - modulo = (len(data) % align) if (align > 0) else 0 - data += b'\xFF' * ((align - modulo) if (modulo != 0) else 0) - f.write(data) - except FileNotFoundError: - print(f"Couldn't open file \"{file}\"") - sys.exit(2) - except Exception as e: - print(e) - sys.exit(3)