From de49c86f2735942828d46d87346005b199c4bec3 Mon Sep 17 00:00:00 2001 From: fincs Date: Wed, 2 May 2018 21:19:57 +0200 Subject: [PATCH] Adapt hbl sources to compile with latest tools --- Makefile | 72 ++++++++++++--------------------------------------- source/main.c | 37 +++++++++++++++----------- source/nro.h | 42 ------------------------------ 3 files changed, 38 insertions(+), 113 deletions(-) delete mode 100644 source/nro.h diff --git a/Makefile b/Makefile index e6aeee7..629d2b3 100644 --- a/Makefile +++ b/Makefile @@ -2,12 +2,12 @@ .SUFFIXES: #--------------------------------------------------------------------------------- -ifeq ($(strip $(DEVKITA64)),) -$(error "Please set DEVKITA64 in your environment. export DEVKITA64=DEVKITA64") +ifeq ($(strip $(DEVKITPRO)),) +$(error "Please set DEVKITPRO in your environment. export DEVKITPRO=/devkitpro") endif TOPDIR ?= $(CURDIR) -include $(DEVKITA64)/switch_rules +include $(DEVKITPRO)/libnx/switch_rules #--------------------------------------------------------------------------------- # TARGET is the name of the output @@ -16,20 +16,8 @@ include $(DEVKITA64)/switch_rules # DATA is a list of directories containing data files # INCLUDES is a list of directories containing header files # EXEFS_SRC is the optional input directory containing data copied into exefs, if anything this normally should only contain "main.npdm". -# -# NO_ICON: if set to anything, do not use icon. -# NO_NACP: if set to anything, no .nacp file is generated. -# APP_TITLE is the name of the app stored in the .nacp file (Optional) -# APP_AUTHOR is the author of the app stored in the .nacp file (Optional) -# APP_VERSION is the version of the app stored in the .nacp file (Optional) -# APP_TITLEID is the titleID of the app stored in the .nacp file (Optional) -# ICON is the filename of the icon (.jpg), relative to the project folder. -# If not set, it attempts to use one of the following (in this order): -# - .jpg -# - icon.jpg -# - /default_icon.jpg #--------------------------------------------------------------------------------- -TARGET := $(notdir $(CURDIR)) +TARGET := hbl BUILD := build SOURCES := source DATA := data @@ -39,18 +27,17 @@ EXEFS_SRC := exefs_src #--------------------------------------------------------------------------------- # options for code generation #--------------------------------------------------------------------------------- -ARCH := -march=armv8-a -mtp=soft -fPIE +ARCH := -march=armv8-a -mtune=cortex-a57 -mtp=soft -fPIE -CFLAGS := -g -Wall -O2 \ - -ffast-math \ +CFLAGS := -g -Wall -O2 -ffunction-sections \ $(ARCH) $(DEFINES) -CFLAGS += $(INCLUDE) -DSWITCH +CFLAGS += $(INCLUDE) -D__SWITCH__ CXXFLAGS := $(CFLAGS) -fno-rtti -fno-exceptions -std=gnu++11 ASFLAGS := -g $(ARCH) -LDFLAGS = -specs=switch.specs -g $(ARCH) -Wl,-Map,$(notdir $*.map) +LDFLAGS = -specs=$(DEVKITPRO)/libnx/switch.specs -g $(ARCH) -Wl,-Map,$(notdir $*.map) LIBS := -lnx @@ -95,8 +82,10 @@ else endif #--------------------------------------------------------------------------------- -export OFILES := $(addsuffix .o,$(BINFILES)) \ - $(CPPFILES:.cpp=.o) $(CFILES:.c=.o) $(SFILES:.s=.o) +export OFILES_BIN := $(addsuffix .o,$(BINFILES)) +export OFILES_SRC := $(CPPFILES:.cpp=.o) $(CFILES:.c=.o) $(SFILES:.s=.o) +export OFILES := $(OFILES_BIN) $(OFILES_SRC) +export HFILES_BIN := $(addsuffix .h,$(subst .,_,$(BINFILES))) export INCLUDE := $(foreach dir,$(INCLUDES),-I$(CURDIR)/$(dir)) \ $(foreach dir,$(LIBDIRS),-I$(dir)/include) \ @@ -106,31 +95,6 @@ export LIBPATHS := $(foreach dir,$(LIBDIRS),-L$(dir)/lib) export BUILD_EXEFS_SRC := $(TOPDIR)/$(EXEFS_SRC) -ifeq ($(strip $(ICON)),) - icons := $(wildcard *.jpg) - ifneq (,$(findstring $(TARGET).jpg,$(icons))) - export APP_ICON := $(TOPDIR)/$(TARGET).jpg - else - ifneq (,$(findstring icon.jpg,$(icons))) - export APP_ICON := $(TOPDIR)/icon.jpg - endif - endif -else - export APP_ICON := $(TOPDIR)/$(ICON) -endif - -ifeq ($(strip $(NO_ICON)),) - export NROFLAGS += --icon=$(APP_ICON) -endif - -ifeq ($(strip $(NO_NACP)),) - export NROFLAGS += --nacp=$(CURDIR)/$(TARGET).nacp -endif - -ifneq ($(APP_TITLEID),) - export NACPFLAGS += --titleid=$(APP_TITLEID) -endif - .PHONY: $(BUILD) clean all #--------------------------------------------------------------------------------- @@ -143,7 +107,7 @@ $(BUILD): #--------------------------------------------------------------------------------- clean: @echo clean ... - @rm -fr $(BUILD) $(TARGET).pfs0 $(TARGET).nso $(TARGET).nro $(TARGET).nacp $(TARGET).elf + @rm -fr $(BUILD) $(TARGET).pfs0 $(TARGET).nso $(TARGET).elf #--------------------------------------------------------------------------------- @@ -155,20 +119,16 @@ DEPENDS := $(OFILES:.o=.d) #--------------------------------------------------------------------------------- # main targets #--------------------------------------------------------------------------------- -all : $(OUTPUT).pfs0 $(OUTPUT).nro +all : $(OUTPUT).pfs0 $(OUTPUT).pfs0 : $(OUTPUT).nso $(OUTPUT).nso : $(OUTPUT).elf -ifeq ($(strip $(NO_NACP)),) -$(OUTPUT).nro : $(OUTPUT).elf $(OUTPUT).nacp -else -$(OUTPUT).nro : $(OUTPUT).elf -endif - $(OUTPUT).elf : $(OFILES) +$(OFILES_SRC) : $(HFILES_BIN) + #--------------------------------------------------------------------------------- # you need a rule like this for each extension you use as binary data #--------------------------------------------------------------------------------- diff --git a/source/main.c b/source/main.c index 42432ca..317d0d4 100644 --- a/source/main.c +++ b/source/main.c @@ -1,7 +1,6 @@ #include #include #include -#include "nro.h" #define MODULE_HBL 347 @@ -47,7 +46,14 @@ void __appInit(void) if (R_FAILED(rc)) fatalSimple(MAKERESULT(MODULE_HBL, 2)); - fsdevInit(); + fsdevMountSdmc(); +} + +void __appExit(void) +{ + fsdevUnmountAll(); + fsExit(); + smExit(); } static void* g_heapAddr; @@ -136,6 +142,7 @@ void getOwnProcessHandle(void) fatalSimple(MAKERESULT(MODULE_HBL, 23)); IpcCommand ipc; + ipcInitialize(&ipc); ipcSendHandleCopy(&ipc, 0xffff8001); struct { @@ -171,26 +178,26 @@ void loadNro(void) { // Unmap previous NRO. header = &g_nroHeader; - rw_size = header->Segments[2].Size + header->bssSize; + rw_size = header->segments[2].size + header->bss_size; rw_size = (rw_size+0xFFF) & ~0xFFF; // .text rc = svcUnmapProcessCodeMemory( - g_procHandle, g_nroAddr + header->Segments[0].FileOff, ((u64) g_heapAddr) + header->Segments[0].FileOff, header->Segments[0].Size); + g_procHandle, g_nroAddr + header->segments[0].file_off, ((u64) g_heapAddr) + header->segments[0].file_off, header->segments[0].size); if (R_FAILED(rc)) fatalSimple(MAKERESULT(MODULE_HBL, 24)); // .rodata rc = svcUnmapProcessCodeMemory( - g_procHandle, g_nroAddr + header->Segments[1].FileOff, ((u64) g_heapAddr) + header->Segments[1].FileOff, header->Segments[1].Size); + g_procHandle, g_nroAddr + header->segments[1].file_off, ((u64) g_heapAddr) + header->segments[1].file_off, header->segments[1].size); if (R_FAILED(rc)) fatalSimple(MAKERESULT(MODULE_HBL, 25)); // .data + .bss rc = svcUnmapProcessCodeMemory( - g_procHandle, g_nroAddr + header->Segments[2].FileOff, ((u64) g_heapAddr) + header->Segments[2].FileOff, rw_size); + g_procHandle, g_nroAddr + header->segments[2].file_off, ((u64) g_heapAddr) + header->segments[2].file_off, rw_size); if (R_FAILED(rc)) fatalSimple(MAKERESULT(MODULE_HBL, 26)); @@ -225,7 +232,7 @@ void loadNro(void) if (fread(header, sizeof(*header), 1, f) != 1) fatalSimple(MAKERESULT(MODULE_HBL, 4)); - if(header->Magic != NROHEADER_MAGICNUM) + if(header->magic != NROHEADER_MAGIC) fatalSimple(MAKERESULT(MODULE_HBL, 5)); size_t rest_size = header->size - (sizeof(NroStart) + sizeof(NroHeader)); @@ -234,17 +241,17 @@ void loadNro(void) fclose(f); - size_t total_size = header->size + header->bssSize; + size_t total_size = header->size + header->bss_size; total_size = (total_size+0xFFF) & ~0xFFF; - rw_size = header->Segments[2].Size + header->bssSize; + rw_size = header->segments[2].size + header->bss_size; rw_size = (rw_size+0xFFF) & ~0xFFF; int i; for (i=0; i<3; i++) { - if (header->Segments[i].FileOff >= header->size || header->Segments[i].Size > header->size || - (header->Segments[i].FileOff + header->Segments[i].Size) > header->size) + if (header->segments[i].file_off >= header->size || header->segments[i].size > header->size || + (header->segments[i].file_off + header->segments[i].size) > header->size) { fatalSimple(MAKERESULT(MODULE_HBL, 6)); } @@ -269,26 +276,26 @@ void loadNro(void) // .text rc = svcSetProcessMemoryPermission( - g_procHandle, map_addr + header->Segments[0].FileOff, header->Segments[0].Size, PERM_R | PERM_X); + g_procHandle, map_addr + header->segments[0].file_off, header->segments[0].size, Perm_R | Perm_X); if (R_FAILED(rc)) fatalSimple(MAKERESULT(MODULE_HBL, 19)); // .rodata rc = svcSetProcessMemoryPermission( - g_procHandle, map_addr + header->Segments[1].FileOff, header->Segments[1].Size, PERM_R); + g_procHandle, map_addr + header->segments[1].file_off, header->segments[1].size, Perm_R); if (R_FAILED(rc)) fatalSimple(MAKERESULT(MODULE_HBL, 20)); // .data + .bss rc = svcSetProcessMemoryPermission( - g_procHandle, map_addr + header->Segments[2].FileOff, rw_size, PERM_RW); + g_procHandle, map_addr + header->segments[2].file_off, rw_size, Perm_Rw); if (R_FAILED(rc)) fatalSimple(MAKERESULT(MODULE_HBL, 21)); - u64 nro_size = header->Segments[2].FileOff + rw_size; + u64 nro_size = header->segments[2].file_off + rw_size; u64 nro_heap_start = ((u64) g_heapAddr) + nro_size; u64 nro_heap_size = g_heapSize + (u64) g_heapAddr - (u64) nro_heap_start; diff --git a/source/nro.h b/source/nro.h deleted file mode 100644 index 50b5510..0000000 --- a/source/nro.h +++ /dev/null @@ -1,42 +0,0 @@ -#pragma once - -#define NROHEADER_MAGICNUM 0x304f524e - -#define ASSETHEADER_MAGICNUM 0x54455341 -#define ASSETHEADER_VERSION 0 - -typedef struct { - u32 FileOff; - u32 Size; -} NsoSegment; - -typedef struct { - u32 unused; - u32 modOffset; - u8 Padding[8]; -} NroStart; - -typedef struct { - u32 Magic; - u32 Unk1; - u32 size; - u32 Unk2; - NsoSegment Segments[3]; - u32 bssSize; - u32 Unk3; - u8 BuildId[0x20]; - u8 Padding[0x20]; -} NroHeader; - -typedef struct { - u64 offset; - u64 size; -} AssetSection; - -typedef struct { - u32 magic; - u32 version; - AssetSection icon; - AssetSection nacp; - AssetSection romfs; -} AssetHeader;