diff --git a/Makefile b/Makefile index e72adf2f..f5888027 100644 --- a/Makefile +++ b/Makefile @@ -1,121 +1,121 @@ -rwildcard = $(foreach d, $(wildcard $1*), $(filter $(subst *, %, $2), $d) $(call rwildcard, $d/, $2)) - -ifeq ($(strip $(DEVKITARM)),) -$(error "Please set DEVKITARM in your environment. export DEVKITARM=devkitARM") -endif - -include $(DEVKITARM)/base_rules - -################################################################################ - -IPL_LOAD_ADDR := 0x40008000 -LPVERSION_MAJOR := 4 -LPVERSION_MINOR := 2 -LPVERSION_BUGFX := 0 -LPVERSION := \"$(LPVERSION_MAJOR).$(LPVERSION_MINOR).$(LPVERSION_BUGFX)\" - -################################################################################ - -TARGET := TegraExplorer -BUILDDIR := build -OUTPUTDIR := output -SOURCEDIR = source -BDKDIR := bdk -BDKINC := -I./$(BDKDIR) -LOADERDIR := ./loader -LZ77DIR := ./tools/lz -BIN2CDIR := ./tools/bin2c -VPATH = $(dir ./$(SOURCEDIR)/) $(dir $(wildcard ./$(SOURCEDIR)/*/)) $(dir $(wildcard ./$(SOURCEDIR)/*/*/)) -VPATH += $(dir $(wildcard ./$(BDKDIR)/)) $(dir $(wildcard ./$(BDKDIR)/*/)) $(dir $(wildcard ./$(BDKDIR)/*/*/)) - -OBJS = $(BUILDDIR)/$(TARGET)/script/builtin.c $(patsubst $(SOURCEDIR)/%.S, $(BUILDDIR)/$(TARGET)/%.o, \ - $(patsubst $(SOURCEDIR)/%.c, $(BUILDDIR)/$(TARGET)/%.o, \ - $(call rwildcard, $(SOURCEDIR), *.S *.c))) -OBJS += $(patsubst $(BDKDIR)/%.S, $(BUILDDIR)/$(TARGET)/%.o, \ - $(patsubst $(BDKDIR)/%.c, $(BUILDDIR)/$(TARGET)/%.o, \ - $(call rwildcard, $(BDKDIR), *.S *.c))) - -GFX_INC := '"../$(SOURCEDIR)/gfx/gfx.h"' -FFCFG_INC := '"../$(SOURCEDIR)/libs/fatfs/ffconf.h"' - -################################################################################ - -CUSTOMDEFINES := -DIPL_LOAD_ADDR=$(IPL_LOAD_ADDR) -CUSTOMDEFINES += -DLP_VER_MJ=$(LPVERSION_MAJOR) -DLP_VER_MN=$(LPVERSION_MINOR) -DLP_VER_BF=$(LPVERSION_BUGFX) -DLP_VER=$(LPVERSION) -CUSTOMDEFINES += -DGFX_INC=$(GFX_INC) -DFFCFG_INC=$(FFCFG_INC) - -# 0: UART_A, 1: UART_B. -#CUSTOMDEFINES += -DDEBUG_UART_PORT=0 - -#CUSTOMDEFINES += -DDEBUG - -ARCH := -march=armv4t -mtune=arm7tdmi -mthumb -mthumb-interwork -CFLAGS = $(ARCH) -Os -nostdlib -ffunction-sections -fdata-sections -fomit-frame-pointer -fno-inline -std=gnu11 -Wall -Wno-missing-braces $(CUSTOMDEFINES) -LDFLAGS = $(ARCH) -nostartfiles -lgcc -Wl,--nmagic,--gc-sections -Xlinker --defsym=IPL_LOAD_ADDR=$(IPL_LOAD_ADDR) - -################################################################################ - -.PHONY: all clean - -all: $(OUTPUTDIR)/$(TARGET)_small.bin - $(eval BIN_SIZE = $(shell wc -c < $(OUTPUTDIR)/$(TARGET).bin)) - @echo "Payload size is $(BIN_SIZE)" - $(eval COMPR_BIN_SIZE = $(shell wc -c < $(OUTPUTDIR)/$(TARGET)_small.bin)) - @echo "Compressed Payload size is $(COMPR_BIN_SIZE)" - - @echo "Max size is 126296 Bytes." - @if [ ${BIN_SIZE} -gt 126296 ]; then echo "\e[1;33mPayload size exceeds limit!\e[0m"; fi - @if [ ${COMPR_BIN_SIZE} -gt 126296 ]; then echo "\e[1;33mCompressed Payload size exceeds limit!\e[0m"; fi - -clean: - @rm -rf $(BUILDDIR) - @rm -rf $(OUTPUTDIR) - @rm -rf $(LOADERDIR)/payload_*.h - -$(OUTPUTDIR)/$(TARGET)_small.bin: $(OUTPUTDIR)/$(TARGET).bin - @$(MAKE) -C $(LZ77DIR) - @$(LZ77DIR)/lz77 $(OUTPUTDIR)/$(TARGET).bin - @$(MAKE) -C $(BIN2CDIR) - @$(BIN2CDIR)/bin2c $(OUTPUTDIR)/$(TARGET).bin.00.lz payload_00 > $(LOADERDIR)/payload_00.h - @$(BIN2CDIR)/bin2c $(OUTPUTDIR)/$(TARGET).bin.01.lz payload_01 > $(LOADERDIR)/payload_01.h - @rm -rf $(OUTPUTDIR)/$(TARGET).bin.*.lz - - $(MAKE) -C $(LOADERDIR) PAYLOAD_NAME=$(TARGET)_small - -$(OUTPUTDIR)/$(TARGET).bin: $(BUILDDIR)/$(TARGET)/$(TARGET).elf - @mkdir -p "$(@D)" - $(OBJCOPY) -S -O binary $< $@ - -$(BUILDDIR)/$(TARGET)/$(TARGET).elf: $(OBJS) - $(CC) $(LDFLAGS) -T $(SOURCEDIR)/link.ld $^ -o $@ - -$(BUILDDIR)/$(TARGET)/%.o: $(SOURCEDIR)/%.c - @mkdir -p "$(@D)" - $(CC) $(CFLAGS) $(BDKINC) -c $< -o $@ - -$(BUILDDIR)/$(TARGET)/%.o: $(SOURCEDIR)/%.S - @mkdir -p "$(@D)" - $(CC) $(CFLAGS) -c $< -o $@ - -$(BUILDDIR)/$(TARGET)/%.o: $(BDKDIR)/%.c - @mkdir -p "$(@D)" - $(CC) $(CFLAGS) $(BDKINC) -c $< -o $@ - -$(BUILDDIR)/$(TARGET)/%.o: $(BDKDIR)/%.S - @mkdir -p "$(@D)" - $(CC) $(CFLAGS) -c $< -o $@ - -$(BUILDDIR)/$(TARGET)/script/builtin.o: $(BUILDDIR)/$(TARGET)/script/builtin.c - @mkdir -p "$(@D)" - $(CC) $(CFLAGS) $(BDKINC) -c $< -o $@ - -$(BUILDDIR)/$(TARGET)/script/builtin.c: scripts/*.te - @mkdir -p "$(@D)" - @mkdir -p "$(BUILDDIR)/$(TARGET)/scripts" -ifeq ($(OS),Windows_NT) - @py ts-minifier.py --such-meme -d "$(BUILDDIR)/$(TARGET)/scripts" $(wildcard scripts/*.te) - @py te2c.py "$(BUILDDIR)/$(TARGET)/script/builtin" "$(BUILDDIR)/$(TARGET)/scripts" -else - @python3 ts-minifier.py --such-meme -d "$(BUILDDIR)/$(TARGET)/scripts" $(wildcard scripts/*.te) - @python3 te2c.py "$(BUILDDIR)/$(TARGET)/script/builtin" "$(BUILDDIR)/$(TARGET)/scripts" -endif +rwildcard = $(foreach d, $(wildcard $1*), $(filter $(subst *, %, $2), $d) $(call rwildcard, $d/, $2)) + +ifeq ($(strip $(DEVKITARM)),) +$(error "Please set DEVKITARM in your environment. export DEVKITARM=devkitARM") +endif + +include $(DEVKITARM)/base_rules + +################################################################################ + +IPL_LOAD_ADDR := 0x40008000 +LPVERSION_MAJOR := 4 +LPVERSION_MINOR := 3 +LPVERSION_BUGFX := 0 +LPVERSION := \"$(LPVERSION_MAJOR).$(LPVERSION_MINOR).$(LPVERSION_BUGFX)\" + +################################################################################ + +TARGET := TegraExplorer +BUILDDIR := build +OUTPUTDIR := output +SOURCEDIR = source +BDKDIR := bdk +BDKINC := -I./$(BDKDIR) +LOADERDIR := ./loader +LZ77DIR := ./tools/lz +BIN2CDIR := ./tools/bin2c +VPATH = $(dir ./$(SOURCEDIR)/) $(dir $(wildcard ./$(SOURCEDIR)/*/)) $(dir $(wildcard ./$(SOURCEDIR)/*/*/)) +VPATH += $(dir $(wildcard ./$(BDKDIR)/)) $(dir $(wildcard ./$(BDKDIR)/*/)) $(dir $(wildcard ./$(BDKDIR)/*/*/)) + +OBJS = $(BUILDDIR)/$(TARGET)/script/builtin.c $(patsubst $(SOURCEDIR)/%.S, $(BUILDDIR)/$(TARGET)/%.o, \ + $(patsubst $(SOURCEDIR)/%.c, $(BUILDDIR)/$(TARGET)/%.o, \ + $(call rwildcard, $(SOURCEDIR), *.S *.c))) +OBJS += $(patsubst $(BDKDIR)/%.S, $(BUILDDIR)/$(TARGET)/%.o, \ + $(patsubst $(BDKDIR)/%.c, $(BUILDDIR)/$(TARGET)/%.o, \ + $(call rwildcard, $(BDKDIR), *.S *.c))) + +GFX_INC := '"../$(SOURCEDIR)/gfx/gfx.h"' +FFCFG_INC := '"../$(SOURCEDIR)/libs/fatfs/ffconf.h"' + +################################################################################ + +CUSTOMDEFINES := -DIPL_LOAD_ADDR=$(IPL_LOAD_ADDR) +CUSTOMDEFINES += -DLP_VER_MJ=$(LPVERSION_MAJOR) -DLP_VER_MN=$(LPVERSION_MINOR) -DLP_VER_BF=$(LPVERSION_BUGFX) -DLP_VER=$(LPVERSION) +CUSTOMDEFINES += -DGFX_INC=$(GFX_INC) -DFFCFG_INC=$(FFCFG_INC) + +# 0: UART_A, 1: UART_B. +#CUSTOMDEFINES += -DDEBUG_UART_PORT=0 + +#CUSTOMDEFINES += -DDEBUG + +ARCH := -march=armv4t -mtune=arm7tdmi -mthumb -mthumb-interwork +CFLAGS = $(ARCH) -Os -nostdlib -ffunction-sections -fdata-sections -fomit-frame-pointer -fno-inline -std=gnu11 -Wall -Wno-missing-braces $(CUSTOMDEFINES) +LDFLAGS = $(ARCH) -nostartfiles -lgcc -Wl,--nmagic,--gc-sections -Xlinker --defsym=IPL_LOAD_ADDR=$(IPL_LOAD_ADDR) + +################################################################################ + +.PHONY: all clean + +all: $(OUTPUTDIR)/$(TARGET)_small.bin + $(eval BIN_SIZE = $(shell wc -c < $(OUTPUTDIR)/$(TARGET).bin)) + @echo "Payload size is $(BIN_SIZE)" + $(eval COMPR_BIN_SIZE = $(shell wc -c < $(OUTPUTDIR)/$(TARGET)_small.bin)) + @echo "Compressed Payload size is $(COMPR_BIN_SIZE)" + + @echo "Max size is 126296 Bytes." + @if [ ${BIN_SIZE} -gt 126296 ]; then echo "\e[1;33mPayload size exceeds limit!\e[0m"; fi + @if [ ${COMPR_BIN_SIZE} -gt 126296 ]; then echo "\e[1;33mCompressed Payload size exceeds limit!\e[0m"; fi + +clean: + @rm -rf $(BUILDDIR) + @rm -rf $(OUTPUTDIR) + @rm -rf $(LOADERDIR)/payload_*.h + +$(OUTPUTDIR)/$(TARGET)_small.bin: $(OUTPUTDIR)/$(TARGET).bin + @$(MAKE) -C $(LZ77DIR) + @$(LZ77DIR)/lz77 $(OUTPUTDIR)/$(TARGET).bin + @$(MAKE) -C $(BIN2CDIR) + @$(BIN2CDIR)/bin2c $(OUTPUTDIR)/$(TARGET).bin.00.lz payload_00 > $(LOADERDIR)/payload_00.h + @$(BIN2CDIR)/bin2c $(OUTPUTDIR)/$(TARGET).bin.01.lz payload_01 > $(LOADERDIR)/payload_01.h + @rm -rf $(OUTPUTDIR)/$(TARGET).bin.*.lz + + $(MAKE) -C $(LOADERDIR) PAYLOAD_NAME=$(TARGET)_small + +$(OUTPUTDIR)/$(TARGET).bin: $(BUILDDIR)/$(TARGET)/$(TARGET).elf + @mkdir -p "$(@D)" + $(OBJCOPY) -S -O binary $< $@ + +$(BUILDDIR)/$(TARGET)/$(TARGET).elf: $(OBJS) + $(CC) $(LDFLAGS) -T $(SOURCEDIR)/link.ld $^ -o $@ + +$(BUILDDIR)/$(TARGET)/%.o: $(SOURCEDIR)/%.c + @mkdir -p "$(@D)" + $(CC) $(CFLAGS) $(BDKINC) -c $< -o $@ + +$(BUILDDIR)/$(TARGET)/%.o: $(SOURCEDIR)/%.S + @mkdir -p "$(@D)" + $(CC) $(CFLAGS) -c $< -o $@ + +$(BUILDDIR)/$(TARGET)/%.o: $(BDKDIR)/%.c + @mkdir -p "$(@D)" + $(CC) $(CFLAGS) $(BDKINC) -c $< -o $@ + +$(BUILDDIR)/$(TARGET)/%.o: $(BDKDIR)/%.S + @mkdir -p "$(@D)" + $(CC) $(CFLAGS) -c $< -o $@ + +$(BUILDDIR)/$(TARGET)/script/builtin.o: $(BUILDDIR)/$(TARGET)/script/builtin.c + @mkdir -p "$(@D)" + $(CC) $(CFLAGS) $(BDKINC) -c $< -o $@ + +$(BUILDDIR)/$(TARGET)/script/builtin.c: scripts/*.te + @mkdir -p "$(@D)" + @mkdir -p "$(BUILDDIR)/$(TARGET)/scripts" +ifeq ($(OS),Windows_NT) + @py ts-minifier.py --such-meme -d "$(BUILDDIR)/$(TARGET)/scripts" $(wildcard scripts/*.te) + @py te2c.py "$(BUILDDIR)/$(TARGET)/script/builtin" "$(BUILDDIR)/$(TARGET)/scripts" +else + @python3 ts-minifier.py --such-meme -d "$(BUILDDIR)/$(TARGET)/scripts" $(wildcard scripts/*.te) + @python3 te2c.py "$(BUILDDIR)/$(TARGET)/script/builtin" "$(BUILDDIR)/$(TARGET)/scripts" +endif diff --git a/bdk/storage/sdmmc.c b/bdk/storage/sdmmc.c index c87db833..0d305123 100644 --- a/bdk/storage/sdmmc.c +++ b/bdk/storage/sdmmc.c @@ -235,10 +235,10 @@ int sdmmc_storage_end(sdmmc_storage_t *storage) return 1; } -static int _sdmmc_storage_readwrite(sdmmc_storage_t *storage, u32 sector, u32 num_sectors, void *buf, u32 is_write) +static int _sdmmc_storage_readwrite(sdmmc_storage_t *storage, u64 sector, u32 num_sectors, void *buf, u32 is_write) { u8 *bbuf = (u8 *)buf; - u32 sct_off = sector; + u64 sct_off = sector; u32 sct_total = num_sectors; bool first_reinit = true; @@ -308,7 +308,7 @@ static int _sdmmc_storage_readwrite(sdmmc_storage_t *storage, u32 sector, u32 nu return 1; } -int sdmmc_storage_read(sdmmc_storage_t *storage, u32 sector, u32 num_sectors, void *buf) +int sdmmc_storage_read(sdmmc_storage_t *storage, u64 sector, u32 num_sectors, void *buf) { // Ensure that buffer resides in DRAM and it's DMA aligned. if (((u32)buf >= DRAM_START) && !((u32)buf % 8)) @@ -326,7 +326,7 @@ int sdmmc_storage_read(sdmmc_storage_t *storage, u32 sector, u32 num_sectors, vo return 0; } -int sdmmc_storage_write(sdmmc_storage_t *storage, u32 sector, u32 num_sectors, void *buf) +int sdmmc_storage_write(sdmmc_storage_t *storage, u64 sector, u32 num_sectors, void *buf) { // Ensure that buffer resides in DRAM and it's DMA aligned. if (((u32)buf >= DRAM_START) && !((u32)buf % 8)) diff --git a/bdk/storage/sdmmc.h b/bdk/storage/sdmmc.h index 5dcd10f0..df8fff71 100644 --- a/bdk/storage/sdmmc.h +++ b/bdk/storage/sdmmc.h @@ -182,7 +182,7 @@ typedef struct _sdmmc_storage_t sdmmc_t *sdmmc; u32 rca; int has_sector_access; - u32 sec_cnt; + u64 sec_cnt; int is_low_voltage; u32 partition; int initialized; @@ -198,8 +198,8 @@ typedef struct _sdmmc_storage_t } sdmmc_storage_t; int sdmmc_storage_end(sdmmc_storage_t *storage); -int sdmmc_storage_read(sdmmc_storage_t *storage, u32 sector, u32 num_sectors, void *buf); -int sdmmc_storage_write(sdmmc_storage_t *storage, u32 sector, u32 num_sectors, void *buf); +int sdmmc_storage_read(sdmmc_storage_t *storage, u64 sector, u32 num_sectors, void *buf); +int sdmmc_storage_write(sdmmc_storage_t *storage, u64 sector, u32 num_sectors, void *buf); int sdmmc_storage_init_mmc(sdmmc_storage_t *storage, sdmmc_t *sdmmc, u32 bus_width, u32 type); int sdmmc_storage_set_mmc_partition(sdmmc_storage_t *storage, u32 partition); void sdmmc_storage_init_wait_sd(); diff --git a/source/storage/emummc.c b/source/storage/emummc.c index 65953ed4..72d93809 100644 --- a/source/storage/emummc.c +++ b/source/storage/emummc.c @@ -7,8 +7,8 @@ * * This program is distributed in the hope it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for - * more details. + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . @@ -64,9 +64,9 @@ void emummc_load_cfg() if (!strcmp("enabled", kv->key)) emu_cfg.enabled = atoi(kv->val); else if (!strcmp("sector", kv->key)) - emu_cfg.sector = strtol(kv->val, NULL, 16); + emu_cfg.sector = (u32)strtoul(kv->val, NULL, 16); else if (!strcmp("id", kv->key)) - emu_cfg.id = strtol(kv->val, NULL, 16); + emu_cfg.id = (u32)strtoul(kv->val, NULL, 16); else if (!strcmp("path", kv->key)) emu_cfg.path = kv->val; else if (!strcmp("nintendo_path", kv->key)) @@ -187,9 +187,17 @@ int emummc_storage_read(sdmmc_storage_t *storage, u32 sector, u32 num_sectors, v return sdmmc_storage_read(storage, sector, num_sectors, buf); else if (emu_cfg.sector) { - sector += emu_cfg.sector; - sector += emummc_raw_get_part_off(emu_cfg.active_part) * 0x2000; - return sdmmc_storage_read(&sd_storage, sector, num_sectors, buf); + const u32 part_off = emummc_raw_get_part_off(emu_cfg.active_part) * 0x2000; + const u32 base_sector = emu_cfg.sector + part_off; + const u32 abs_sector = base_sector + sector; + + // Safety: avoid reading past the end of the SD card on very large cards. + if ((u64)abs_sector + (u64)num_sectors > (u64)sd_storage.sec_cnt) + { + EPRINTF("emuMMC read OOR"); + return 0; + } + return sdmmc_storage_read(&sd_storage, abs_sector, num_sectors, buf); } else { @@ -232,9 +240,17 @@ int emummc_storage_write(sdmmc_storage_t *storage, u32 sector, u32 num_sectors, return sdmmc_storage_write(storage, sector, num_sectors, buf); else if (emu_cfg.sector) { - sector += emu_cfg.sector; - sector += emummc_raw_get_part_off(emu_cfg.active_part) * 0x2000; - return sdmmc_storage_write(&sd_storage, sector, num_sectors, buf); + const u32 part_off = emummc_raw_get_part_off(emu_cfg.active_part) * 0x2000; + const u32 base_sector = emu_cfg.sector + part_off; + const u32 abs_sector = base_sector + sector; + + // Safety: avoid writing past the end of the SD card. + if ((u64)abs_sector + (u64)num_sectors > (u64)sd_storage.sec_cnt) + { + EPRINTF("emuMMC write OOR"); + return 0; + } + return sdmmc_storage_write(&sd_storage, abs_sector, num_sectors, buf); } else {