Compare commits

..

No commits in common. "master" and "v1.0" have entirely different histories.
master ... v1.0

45 changed files with 6677 additions and 9743 deletions

7
.gitignore vendored
View File

@ -1,10 +1,11 @@
out out
CakeHax
CakeBrah
build build
loader/build
arm11/build
*.bin *.bin
*.3dsx *.3dsx
*.smdh *.smdh
*.o *.o
*.d *.d
*.elf *.elf
*.bat

@ -1 +1 @@
Subproject commit 1efda4e89476d34aeb307e0acd7a8dbcd1344601 Subproject commit 42ebe0d0bc075ba98fa631441590de9bd2733d2b

@ -1 +1 @@
Subproject commit 329212a8e09d4718e304cb9d94a0e10f66d9813d Subproject commit 6b8fca0b37a370a605f76b34b133da91a0b40f5e

View File

@ -1,76 +1,72 @@
rwildcard = $(foreach d, $(wildcard $1*), $(filter $(subst *, %, $2), $d) $(call rwildcard, $d/, $2)) rwildcard = $(foreach d, $(wildcard $1*), $(filter $(subst *, %, $2), $d) $(call rwildcard, $d/, $2))
ifeq ($(strip $(DEVKITARM)),) CC := arm-none-eabi-gcc
$(error "Please set DEVKITARM in your environment. export DEVKITARM=<path to>devkitARM") AS := arm-none-eabi-as
LD := arm-none-eabi-ld
OC := arm-none-eabi-objcopy
OPENSSL := openssl
PYTHON3 := python
PYTHON_VER_MAJOR := $(word 2, $(subst ., , $(shell python --version 2>&1)))
ifneq ($(PYTHON_VER_MAJOR), 3)
PYTHON3 := py -3
endif endif
include $(DEVKITARM)/base_tools
name := SafeA9LHInstaller name := SafeA9LHInstaller
revision := $(shell git describe --tags --match v[0-9]* --abbrev=8 | sed 's/-[0-9]*-g/-/i')
dir_source := source dir_source := source
dir_cakehax := CakeHax
dir_cakebrah := CakeBrah
dir_build := build dir_build := build
dir_mset := CakeHax
dir_out := out dir_out := out
dir_ninjhax := CakeBrah
ASFLAGS := -mcpu=arm946e-s ASFLAGS := -mlittle-endian -mcpu=arm946e-s -march=armv5te
CFLAGS := -Wall -Wextra -MMD -MP -marm $(ASFLAGS) -fno-builtin -fshort-wchar -std=c11 -Wno-main -O2 -flto -ffast-math CFLAGS := -Wall -Wextra -MMD -MP -marm $(ASFLAGS) -fno-builtin -fshort-wchar -std=c11 -Wno-main -O2 -ffast-math
LDFLAGS := -nostartfiles
FLAGS := name=$(name).dat dir_out=$(abspath $(dir_out)) ICON=$(abspath icon.png) APP_DESCRIPTION="Noob-proof ARM9LoaderHax installer/updater." APP_AUTHOR="Aurora Wright" --no-print-directory FLAGS := name=$(name).dat dir_out=$(abspath $(dir_out)) ICON=$(abspath icon.png) APP_DESCRIPTION="Noob-proof ARM9LoaderHax installer/updater." APP_AUTHOR="Aurora Wright" --no-print-directory
objects= $(patsubst $(dir_source)/%.s, $(dir_build)/%.o, \ objects= $(patsubst $(dir_source)/%.s, $(dir_build)/%.o, \
$(patsubst $(dir_source)/%.c, $(dir_build)/%.o, \ $(patsubst $(dir_source)/%.c, $(dir_build)/%.o, \
$(call rwildcard, $(dir_source), *.s *.c))) $(call rwildcard, $(dir_source), *.s *.c)))
.PHONY: all .PHONY: all
all: launcher a9lh cakebrah all: launcher a9lh ninjhax
.PHONY: launcher .PHONY: launcher
launcher: $(dir_out)/$(name).dat launcher: $(dir_out)/$(name).dat
.PHONY: a9lh .PHONY: a9lh
a9lh: $(dir_out)/arm9loaderhax.bin a9lh: $(dir_out)/arm9loaderhax.bin
.PHONY: cakebrah .PHONY: ninjhax
cakebrah: $(dir_out)/3ds/$(name) ninjhax: $(dir_out)/3ds/$(name)
.PHONY: release
release: $(dir_out)/$(name)$(revision).7z
.PHONY: clean .PHONY: clean
clean: clean:
@$(MAKE) $(FLAGS) -C $(dir_cakehax) clean @$(MAKE) $(FLAGS) -C $(dir_mset) clean
@$(MAKE) $(FLAGS) -C $(dir_cakebrah) clean @$(MAKE) $(FLAGS) -C $(dir_ninjhax) clean
@rm -rf $(dir_out) $(dir_build) @rm -rf $(dir_out) $(dir_build)
$(dir_out) $(dir_build): $(dir_out)/$(name).dat: $(dir_build)/main.bin
@mkdir -p "$@" @mkdir -p $(dir_out)
@$(MAKE) $(FLAGS) -C $(dir_mset) launcher
$(dir_out)/$(name).dat: $(dir_build)/main.bin $(dir_out)
@$(MAKE) $(FLAGS) -C $(dir_cakehax) launcher
dd if=$(dir_build)/main.bin of=$@ bs=512 seek=144 dd if=$(dir_build)/main.bin of=$@ bs=512 seek=144
$(dir_out)/arm9loaderhax.bin: $(dir_build)/main.bin $(dir_out) $(dir_out)/arm9loaderhax.bin: $(dir_build)/main.bin
@cp -av $(dir_build)/main.bin $@ @cp -av $(dir_build)/main.bin $@
$(dir_out)/3ds/$(name): $(dir_out) $(dir_out)/3ds/$(name):
@mkdir -p "$@" @mkdir -p $(dir_out)/3ds/$(name)
@$(MAKE) $(FLAGS) -C $(dir_cakebrah) @$(MAKE) $(FLAGS) -C $(dir_ninjhax)
@mv $(dir_out)/$(name).3dsx $(dir_out)/$(name).smdh $@ @mv $(dir_out)/$(name).3dsx $@
@mv $(dir_out)/$(name).smdh $@
$(dir_out)/$(name)$(revision).7z: all
@7z a -mx $@ ./$(@D)/*
$(dir_build)/main.bin: $(dir_build)/main.elf $(dir_build)/main.bin: $(dir_build)/main.elf
$(OBJCOPY) -S -O binary $< $@ $(OC) -S -O binary $< $@
$(dir_build)/main.elf: $(objects) $(dir_build)/main.elf: $(objects)
$(LINK.o) -T linker.ld $(OUTPUT_OPTION) $^ # FatFs requires libgcc for __aeabi_uidiv
$(CC) -nostartfiles $(LDFLAGS) -T linker.ld $(OUTPUT_OPTION) $^
$(dir_build)/memory.o $(dir_build)/strings.o: CFLAGS += -O3
$(dir_build)/installer.o: CFLAGS += -DTITLE="\"$(name) $(revision)\""
$(dir_build)/%.o: $(dir_source)/%.c $(dir_build)/%.o: $(dir_source)/%.c
@mkdir -p "$(@D)" @mkdir -p "$(@D)"
@ -79,3 +75,12 @@ $(dir_build)/%.o: $(dir_source)/%.c
$(dir_build)/%.o: $(dir_source)/%.s $(dir_build)/%.o: $(dir_source)/%.s
@mkdir -p "$(@D)" @mkdir -p "$(@D)"
$(COMPILE.s) $(OUTPUT_OPTION) $< $(COMPILE.s) $(OUTPUT_OPTION) $<
$(dir_build)/fatfs/%.o: $(dir_source)/fatfs/%.c
@mkdir -p "$(@D)"
$(COMPILE.c) -mthumb -mthumb-interwork -Wno-unused-function $(OUTPUT_OPTION) $<
$(dir_build)/fatfs/%.o: $(dir_source)/fatfs/%.s
@mkdir -p "$(@D)"
$(COMPILE.s) -mthumb -mthumb-interwork $(OUTPUT_OPTION) $<
include $(call rwildcard, $(dir_build), *.d)

View File

@ -1,23 +1,10 @@
# Safe A9LH Installer # Safe A9LH Installer
*A noob-proof ARM9LoaderHax installer/updater/uninstaller* *A noob-proof ARM9LoaderHax installer/updater*
**Usage / Features:** **Usage / Features:**
*DO NOT USE THIS VERSION TO INSTALL ON NEW 3DS UNLESS YOU HAVE AN HARDMOD, CAUSES RANDOM BRICKS! UNINSTALLATION IS FINE*
For a comprehensive guide to installing A9LH and to 3DS hacking in general, refer to [Plailect's guide](https://github.com/Plailect/Guide/wiki/Get-Started).
For other details about the program, refer to the [GBATemp thread](http://gbatemp.net/threads/safea9lhinstaller.419577/).
**Credits:** **Credits:**
* delebile for his A9LH implementation delebile for his A9LH implementation, everyone in #cakey, StandardBus for allowing this to happen by hardmodding my consoles.
* Everyone in #cakey Original exploit by plutoo.
* StandardBus for allowing this to happen by hardmodding my consoles
* Everyone who contributed to the discovery and research of OTPless A9LH
Code for writing to the screens is from CakesFW, several other code is from Luma3DS.
Original exploit by plutoo.
**Licensing:**
This software is licensed under the terms of the GPLv3.
You can find a copy of the license in the LICENSE.txt file.

View File

@ -1,15 +1,11 @@
OUTPUT_FORMAT("elf32-littlearm", "elf32-bigarm", "elf32-littlearm")
OUTPUT_ARCH(arm)
ENTRY(_start) ENTRY(_start)
SECTIONS SECTIONS
{ {
. = 0x23F00000; . = 0x23F00000;
.text.start : { *(.text.start) }
.text : ALIGN(4) { *(.text.start) *(.text*); . = ALIGN(4); } .text : { *(.text) }
.rodata : ALIGN(4) { *(.rodata*); . = ALIGN(4); } .data : { *(.data) }
.data : ALIGN(4) { *(.data*); . = ALIGN(4); } .bss : { *(.bss COMMON) }
.bss : ALIGN(8) { __bss_start = .; *(.bss* COMMON); . = ALIGN(8); __bss_end = .; } .rodata : { *(.rodata) }
. = ALIGN(4); . = ALIGN(4);
} }

View File

@ -1,161 +0,0 @@
/*
* This file is part of Luma3DS
* Copyright (C) 2016 Aurora Wright, TuxSH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Additional Terms 7.b of GPLv3 applies to this file: Requiring preservation of specified
* reasonable legal notices or author attributions in that material or in the Appropriate Legal
* Notices displayed by works containing it.
*/
/*
* Adapted from 3DBrew and https://github.com/mid-kid/CakesForeveryWan/blob/master/source/headers.h
*/
typedef struct __attribute__((packed))
{
u32 address;
u32 phyRegionSize;
u32 size;
} CodeSetInfo;
typedef struct __attribute__((packed))
{
u32 saveDataSize[2];
u32 jumpID[2];
u8 reserved[0x30];
} SystemInfo;
typedef struct __attribute__((packed))
{
char appTitle[8];
u8 reserved1[5];
u8 flag;
u8 remasterVersion[2];
CodeSetInfo textCodeSet;
u32 stackSize;
CodeSetInfo roCodeSet;
u8 reserved2[4];
CodeSetInfo dataCodeSet;
u32 bssSize;
char depends[0x180];
SystemInfo systemInfo;
} SystemControlInfo;
typedef struct __attribute__((packed))
{
SystemControlInfo systemControlInfo;
u8 aci[0x200];
u8 accessDescSig[0x100];
u8 ncchPubKey[0x100];
u8 aciLim[0x200];
} ExHeader;
typedef struct __attribute__((packed))
{
u8 sig[0x100]; //RSA-2048 signature of the NCCH header, using SHA-256
char magic[4]; //NCCH
u32 contentSize; //Media unit
u8 partitionId[8];
u8 makerCode[2];
u16 version;
u8 reserved1[4];
u8 programID[8];
u8 reserved2[0x10];
u8 logoHash[0x20]; //Logo Region SHA-256 hash
char productCode[0x10];
u8 exHeaderHash[0x20]; //Extended header SHA-256 hash
u32 exHeaderSize; //Extended header size
u32 reserved3;
u8 flags[8];
u32 plainOffset; //Media unit
u32 plainSize; //Media unit
u32 logoOffset; //Media unit
u32 logoSize; //Media unit
u32 exeFsOffset; //Media unit
u32 exeFsSize; //Media unit
u32 exeFsHashSize; //Media unit
u32 reserved4;
u32 romFsOffset; //Media unit
u32 romFsSize; //Media unit
u32 romFsHashSize; //Media unit
u32 reserved5;
u8 exeFsHash[0x20]; //ExeFS superblock SHA-256 hash
u8 romFsHash[0x20]; //RomFS superblock SHA-256 hash
} Ncch;
typedef struct __attribute__((packed))
{
Ncch ncch;
ExHeader exHeader;
} Cxi;
typedef struct __attribute__((packed))
{
char sigIssuer[0x40];
u8 eccPubKey[0x3C];
u8 version;
u8 caCrlVersion;
u8 signerCrlVersion;
u8 titleKey[0x10];
u8 reserved1;
u8 ticketId[8];
u8 consoleId[4];
u8 titleId[8];
u8 reserved2[2];
u16 ticketTitleVersion;
u8 reserved3[8];
u8 licenseType;
u8 ticketCommonKeyYIndex; //Ticket common keyY index, usually 0x1 for retail system titles.
u8 reserved4[0x2A];
u8 unk[4]; //eShop Account ID?
u8 reserved5;
u8 audit;
u8 reserved6[0x42];
u8 limits[0x40];
u8 contentIndex[0xAC];
} Ticket;
typedef struct __attribute__((packed))
{
u32 offset;
u8 *address;
u32 size;
u32 procType;
u8 hash[0x20];
} FirmSection;
typedef struct __attribute__((packed))
{
u32 magic;
u32 reserved1;
u8 *arm11Entry;
u8 *arm9Entry;
u8 reserved2[0x30];
FirmSection section[4];
} Firm;
typedef struct __attribute__((packed))
{
u8 keyX[0x10];
u8 keyY[0x10];
u8 ctr[0x10];
char size[8];
u8 reserved[8];
u8 ctlBlock[0x10];
char magic[4];
u8 reserved2[0xC];
u8 slot0x16keyX[0x10];
} Arm9Bin;

View File

@ -1,29 +0,0 @@
/*
* This file is part of Luma3DS
* Copyright (C) 2016 Aurora Wright, TuxSH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Additional Terms 7.b of GPLv3 applies to this file: Requiring preservation of specified
* reasonable legal notices or author attributions in that material or in the Appropriate Legal
* Notices displayed by works containing it.
*/
#pragma once
#include "types.h"
void flushEntireDCache(void); //actually: "clean and flush"
void flushDCacheRange(void *startAddress, u32 size);
void flushEntireICache(void);

View File

@ -1,74 +0,0 @@
@ This file is part of Luma3DS
@ Copyright (C) 2016 Aurora Wright, TuxSH
@
@ This program is free software: you can redistribute it and/or modify
@ it under the terms of the GNU General Public License as published by
@ the Free Software Foundation, either version 3 of the License, or
@ (at your option) any later version.
@
@ This program is distributed in the hope that 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.
@
@ You should have received a copy of the GNU General Public License
@ along with this program. If not, see <http://www.gnu.org/licenses/>.
@
@ Additional Terms 7.b of GPLv3 applies to this file: Requiring preservation of specified
@ reasonable legal notices or author attributions in that material or in the Appropriate Legal
@ Notices displayed by works containing it.
.text
.arm
.align 4
.global flushEntireDCache
.type flushEntireDCache, %function
flushEntireDCache:
@ Adapted from http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.ddi0155a/ch03s03s05.html,
@ and https://github.com/gemarcano/libctr9_io/blob/master/src/ctr_system_ARM.c#L39 as well
@ Note: ARM's example is actually for a 8KB DCache (which is what the 3DS has)
@ Implemented in bootROM at address 0xffff0830
mov r1, #0 @ segment counter
outer_loop:
mov r0, #0 @ line counter
inner_loop:
orr r2, r1, r0 @ generate segment and line address
mcr p15, 0, r2, c7, c14, 2 @ clean and flush the line
add r0, #0x20 @ increment to next line
cmp r0, #0x400
bne inner_loop
add r1, #0x40000000
cmp r1, #0
bne outer_loop
mcr p15, 0, r1, c7, c10, 4 @ drain write buffer
bx lr
.global flushDCacheRange
.type flushDCacheRange, %function
flushDCacheRange:
@ Implemented in bootROM at address 0xffff08a0
add r1, r0, r1 @ end address
bic r0, #0x1f @ align source address to cache line size (32 bytes)
flush_dcache_range_loop:
mcr p15, 0, r0, c7, c14, 1 @ clean and flush the line corresponding to the address r0 is holding
add r0, #0x20
cmp r0, r1
blo flush_dcache_range_loop
mov r0, #0
mcr p15, 0, r0, c7, c10, 4 @ drain write buffer
bx lr
.global flushEntireICache
.type flushEntireICache, %function
flushEntireICache:
@ Implemented in bootROM at address 0xffff0ab4
mov r0, #0
mcr p15, 0, r0, c7, c5, 0
bx lr

View File

@ -1,444 +1,360 @@
/* // From http://github.com/b1l1s/ctr
* crypto.c
*
* Crypto libs from http://github.com/b1l1s/ctr
*/
#include "crypto.h" #include "crypto.h"
#include "memory.h" #include "memory.h"
#include "fatfs/sdmmc/sdmmc.h" #include "fatfs/sdmmc/sdmmc.h"
/**************************************************************** /****************************************************************
* Crypto libs * Crypto Libs
****************************************************************/ ****************************************************************/
/* original version by megazig */ /* original version by megazig */
#ifndef __thumb__ #ifndef __thumb__
#define BSWAP32(x) {\ #define BSWAP32(x) {\
__asm__\ __asm__\
(\ (\
"eor r1, %1, %1, ror #16\n\t"\ "eor r1, %1, %1, ror #16\n\t"\
"bic r1, r1, #0xFF0000\n\t"\ "bic r1, r1, #0xFF0000\n\t"\
"mov %0, %1, ror #8\n\t"\ "mov %0, %1, ror #8\n\t"\
"eor %0, %0, r1, lsr #8\n\t"\ "eor %0, %0, r1, lsr #8\n\t"\
:"=r"(x)\ :"=r"(x)\
:"0"(x)\ :"0"(x)\
:"r1"\ :"r1"\
);\ );\
}; };
#define ADD_u128_u32(u128_0, u128_1, u128_2, u128_3, u32_0) {\ #define ADD_u128_u32(u128_0, u128_1, u128_2, u128_3, u32_0) {\
__asm__\ __asm__\
(\ (\
"adds %0, %4\n\t"\ "adds %0, %4\n\t"\
"addcss %1, %1, #1\n\t"\ "addcss %1, %1, #1\n\t"\
"addcss %2, %2, #1\n\t"\ "addcss %2, %2, #1\n\t"\
"addcs %3, %3, #1\n\t"\ "addcs %3, %3, #1\n\t"\
: "+r"(u128_0), "+r"(u128_1), "+r"(u128_2), "+r"(u128_3)\ : "+r"(u128_0), "+r"(u128_1), "+r"(u128_2), "+r"(u128_3)\
: "r"(u32_0)\ : "r"(u32_0)\
: "cc"\ : "cc"\
);\ );\
} }
#else #else
#define BSWAP32(x) {x = __builtin_bswap32(x);} #define BSWAP32(x) {x = __builtin_bswap32(x);}
#define ADD_u128_u32(u128_0, u128_1, u128_2, u128_3, u32_0) {\ #define ADD_u128_u32(u128_0, u128_1, u128_2, u128_3, u32_0) {\
__asm__\ __asm__\
(\ (\
"mov r4, #0\n\t"\ "mov r4, #0\n\t"\
"add %0, %0, %4\n\t"\ "add %0, %0, %4\n\t"\
"adc %1, %1, r4\n\t"\ "adc %1, %1, r4\n\t"\
"adc %2, %2, r4\n\t"\ "adc %2, %2, r4\n\t"\
"adc %3, %3, r4\n\t"\ "adc %3, %3, r4\n\t"\
: "+r"(u128_0), "+r"(u128_1), "+r"(u128_2), "+r"(u128_3)\ : "+r"(u128_0), "+r"(u128_1), "+r"(u128_2), "+r"(u128_3)\
: "r"(u32_0)\ : "r"(u32_0)\
: "cc", "r4"\ : "cc", "r4"\
);\ );\
} }
#endif /*__thumb__*/ #endif /*__thumb__*/
static void aes_setkey(u8 keyslot, const void *key, u32 keyType, u32 mode) static void aes_setkey(u8 keyslot, const void *key, u32 keyType, u32 mode)
{ {
if(keyslot <= 0x03) return; //Ignore TWL keys for now if(keyslot <= 0x03) return; // Ignore TWL keys for now
u32 *key32 = (u32 *)key; u32 *key32 = (u32 *)key;
*REG_AESCNT = (*REG_AESCNT & ~(AES_CNT_INPUT_ENDIAN | AES_CNT_INPUT_ORDER)) | mode; *REG_AESCNT = (*REG_AESCNT & ~(AES_CNT_INPUT_ENDIAN | AES_CNT_INPUT_ORDER)) | mode;
*REG_AESKEYCNT = (*REG_AESKEYCNT >> 6 << 6) | keyslot | AES_KEYCNT_WRITE; *REG_AESKEYCNT = (*REG_AESKEYCNT >> 6 << 6) | keyslot | AES_KEYCNT_WRITE;
REG_AESKEYFIFO[keyType] = key32[0]; REG_AESKEYFIFO[keyType] = key32[0];
REG_AESKEYFIFO[keyType] = key32[1]; REG_AESKEYFIFO[keyType] = key32[1];
REG_AESKEYFIFO[keyType] = key32[2]; REG_AESKEYFIFO[keyType] = key32[2];
REG_AESKEYFIFO[keyType] = key32[3]; REG_AESKEYFIFO[keyType] = key32[3];
} }
static void aes_use_keyslot(u8 keyslot) static void aes_use_keyslot(u8 keyslot)
{ {
if(keyslot > 0x3F) if(keyslot > 0x3F)
return; return;
*REG_AESKEYSEL = keyslot; *REG_AESKEYSEL = keyslot;
*REG_AESCNT = *REG_AESCNT | 0x04000000; /* mystery bit */ *REG_AESCNT = *REG_AESCNT | 0x04000000; /* mystery bit */
} }
static void aes_setiv(const void *iv, u32 mode) static void aes_setiv(const void *iv, u32 mode)
{ {
const u32 *iv32 = (const u32 *)iv; const u32 *iv32 = (const u32 *)iv;
*REG_AESCNT = (*REG_AESCNT & ~(AES_CNT_INPUT_ENDIAN | AES_CNT_INPUT_ORDER)) | mode; *REG_AESCNT = (*REG_AESCNT & ~(AES_CNT_INPUT_ENDIAN | AES_CNT_INPUT_ORDER)) | mode;
//Word order for IV can't be changed in REG_AESCNT and always default to reversed // Word order for IV can't be changed in REG_AESCNT and always default to reversed
if(mode & AES_INPUT_NORMAL) if(mode & AES_INPUT_NORMAL)
{ {
REG_AESCTR[0] = iv32[3]; REG_AESCTR[0] = iv32[3];
REG_AESCTR[1] = iv32[2]; REG_AESCTR[1] = iv32[2];
REG_AESCTR[2] = iv32[1]; REG_AESCTR[2] = iv32[1];
REG_AESCTR[3] = iv32[0]; REG_AESCTR[3] = iv32[0];
} }
else else
{ {
REG_AESCTR[0] = iv32[0]; REG_AESCTR[0] = iv32[0];
REG_AESCTR[1] = iv32[1]; REG_AESCTR[1] = iv32[1];
REG_AESCTR[2] = iv32[2]; REG_AESCTR[2] = iv32[2];
REG_AESCTR[3] = iv32[3]; REG_AESCTR[3] = iv32[3];
} }
} }
static void aes_advctr(void *ctr, u32 val, u32 mode) static void aes_advctr(void *ctr, u32 val, u32 mode)
{ {
u32 *ctr32 = (u32 *)ctr; u32 *ctr32 = (u32 *)ctr;
int i; int i;
if(mode & AES_INPUT_BE) if(mode & AES_INPUT_BE)
{ {
for(i = 0; i < 4; ++i) //Endian swap for(i = 0; i < 4; ++i) // Endian swap
BSWAP32(ctr32[i]); BSWAP32(ctr32[i]);
} }
if(mode & AES_INPUT_NORMAL) if(mode & AES_INPUT_NORMAL)
{ {
ADD_u128_u32(ctr32[3], ctr32[2], ctr32[1], ctr32[0], val); ADD_u128_u32(ctr32[3], ctr32[2], ctr32[1], ctr32[0], val);
} }
else else
{ {
ADD_u128_u32(ctr32[0], ctr32[1], ctr32[2], ctr32[3], val); ADD_u128_u32(ctr32[0], ctr32[1], ctr32[2], ctr32[3], val);
} }
if(mode & AES_INPUT_BE) if(mode & AES_INPUT_BE)
{ {
for(i = 0; i < 4; ++i) //Endian swap for(i = 0; i < 4; ++i) // Endian swap
BSWAP32(ctr32[i]); BSWAP32(ctr32[i]);
} }
} }
static void aes_change_ctrmode(void *ctr, u32 fromMode, u32 toMode) static void aes_change_ctrmode(void *ctr, u32 fromMode, u32 toMode)
{ {
u32 *ctr32 = (u32 *)ctr; u32 *ctr32 = (u32 *)ctr;
int i; int i;
if((fromMode ^ toMode) & AES_CNT_INPUT_ENDIAN) if((fromMode ^ toMode) & AES_CNT_INPUT_ENDIAN)
{ {
for(i = 0; i < 4; ++i) for(i = 0; i < 4; ++i)
BSWAP32(ctr32[i]); BSWAP32(ctr32[i]);
} }
if((fromMode ^ toMode) & AES_CNT_INPUT_ORDER) if((fromMode ^ toMode) & AES_CNT_INPUT_ORDER)
{ {
u32 temp = ctr32[0]; u32 temp = ctr32[0];
ctr32[0] = ctr32[3]; ctr32[0] = ctr32[3];
ctr32[3] = temp; ctr32[3] = temp;
temp = ctr32[1]; temp = ctr32[1];
ctr32[1] = ctr32[2]; ctr32[1] = ctr32[2];
ctr32[2] = temp; ctr32[2] = temp;
} }
} }
static void aes_batch(void *dst, const void *src, u32 blockCount) static void aes_batch(void *dst, const void *src, u32 blockCount)
{ {
*REG_AESBLKCNT = blockCount << 16; *REG_AESBLKCNT = blockCount << 16;
*REG_AESCNT |= AES_CNT_START; *REG_AESCNT |= AES_CNT_START;
const u32 *src32 = (const u32 *)src; const u32 *src32 = (const u32 *)src;
u32 *dst32 = (u32 *)dst; u32 *dst32 = (u32 *)dst;
u32 wbc = blockCount; u32 wbc = blockCount;
u32 rbc = blockCount; u32 rbc = blockCount;
while(rbc) while(rbc)
{ {
if(wbc && ((*REG_AESCNT & 0x1F) <= 0xC)) //There's space for at least 4 ints if(wbc && ((*REG_AESCNT & 0x1F) <= 0xC)) // There's space for at least 4 ints
{ {
*REG_AESWRFIFO = *src32++; *REG_AESWRFIFO = *src32++;
*REG_AESWRFIFO = *src32++; *REG_AESWRFIFO = *src32++;
*REG_AESWRFIFO = *src32++; *REG_AESWRFIFO = *src32++;
*REG_AESWRFIFO = *src32++; *REG_AESWRFIFO = *src32++;
wbc--; wbc--;
} }
if(rbc && ((*REG_AESCNT & (0x1F << 0x5)) >= (0x4 << 0x5))) //At least 4 ints available for read if(rbc && ((*REG_AESCNT & (0x1F << 0x5)) >= (0x4 << 0x5))) // At least 4 ints available for read
{ {
*dst32++ = *REG_AESRDFIFO; *dst32++ = *REG_AESRDFIFO;
*dst32++ = *REG_AESRDFIFO; *dst32++ = *REG_AESRDFIFO;
*dst32++ = *REG_AESRDFIFO; *dst32++ = *REG_AESRDFIFO;
*dst32++ = *REG_AESRDFIFO; *dst32++ = *REG_AESRDFIFO;
rbc--; rbc--;
} }
} }
} }
static void aes(void *dst, const void *src, u32 blockCount, void *iv, u32 mode, u32 ivMode) static void aes(void *dst, const void *src, u32 blockCount, void *iv, u32 mode, u32 ivMode)
{ {
*REG_AESCNT = mode | *REG_AESCNT = mode |
AES_CNT_INPUT_ORDER | AES_CNT_OUTPUT_ORDER | AES_CNT_INPUT_ORDER | AES_CNT_OUTPUT_ORDER |
AES_CNT_INPUT_ENDIAN | AES_CNT_OUTPUT_ENDIAN | AES_CNT_INPUT_ENDIAN | AES_CNT_OUTPUT_ENDIAN |
AES_CNT_FLUSH_READ | AES_CNT_FLUSH_WRITE; AES_CNT_FLUSH_READ | AES_CNT_FLUSH_WRITE;
u32 blocks; u32 blocks;
while(blockCount != 0) while(blockCount != 0)
{ {
if((mode & AES_ALL_MODES) != AES_ECB_ENCRYPT_MODE if((mode & AES_ALL_MODES) != AES_ECB_ENCRYPT_MODE
&& (mode & AES_ALL_MODES) != AES_ECB_DECRYPT_MODE) && (mode & AES_ALL_MODES) != AES_ECB_DECRYPT_MODE)
aes_setiv(iv, ivMode); aes_setiv(iv, ivMode);
blocks = (blockCount >= 0xFFFF) ? 0xFFFF : blockCount; blocks = (blockCount >= 0xFFFF) ? 0xFFFF : blockCount;
//Save the last block for the next decryption CBC batch's iv // Save the last block for the next decryption CBC batch's iv
if((mode & AES_ALL_MODES) == AES_CBC_DECRYPT_MODE) if((mode & AES_ALL_MODES) == AES_CBC_DECRYPT_MODE)
{ {
memcpy(iv, src + (blocks - 1) * AES_BLOCK_SIZE, AES_BLOCK_SIZE); memcpy(iv, src + (blocks - 1) * AES_BLOCK_SIZE, AES_BLOCK_SIZE);
aes_change_ctrmode(iv, AES_INPUT_BE | AES_INPUT_NORMAL, ivMode); aes_change_ctrmode(iv, AES_INPUT_BE | AES_INPUT_NORMAL, ivMode);
} }
//Process the current batch // Process the current batch
aes_batch(dst, src, blocks); aes_batch(dst, src, blocks);
//Save the last block for the next encryption CBC batch's iv // Save the last block for the next encryption CBC batch's iv
if((mode & AES_ALL_MODES) == AES_CBC_ENCRYPT_MODE) if((mode & AES_ALL_MODES) == AES_CBC_ENCRYPT_MODE)
{ {
memcpy(iv, dst + (blocks - 1) * AES_BLOCK_SIZE, AES_BLOCK_SIZE); memcpy(iv, dst + (blocks - 1) * AES_BLOCK_SIZE, AES_BLOCK_SIZE);
aes_change_ctrmode(iv, AES_INPUT_BE | AES_INPUT_NORMAL, ivMode); aes_change_ctrmode(iv, AES_INPUT_BE | AES_INPUT_NORMAL, ivMode);
} }
// Advance counter for CTR mode
else if((mode & AES_ALL_MODES) == AES_CTR_MODE)
aes_advctr(iv, blocks, ivMode);
//Advance counter for CTR mode src += blocks * AES_BLOCK_SIZE;
else if((mode & AES_ALL_MODES) == AES_CTR_MODE) dst += blocks * AES_BLOCK_SIZE;
aes_advctr(iv, blocks, ivMode); blockCount -= blocks;
}
src += blocks * AES_BLOCK_SIZE;
dst += blocks * AES_BLOCK_SIZE;
blockCount -= blocks;
}
} }
static void sha_wait_idle() void sha_wait_idle()
{ {
while(*REG_SHA_CNT & 1); while(*REG_SHA_CNT & 1);
} }
static void sha(void *res, const void *src, u32 size, u32 mode) void sha(void *res, const void *src, u32 size, u32 mode)
{ {
sha_wait_idle(); sha_wait_idle();
*REG_SHA_CNT = mode | SHA_CNT_OUTPUT_ENDIAN | SHA_NORMAL_ROUND; *REG_SHA_CNT = mode | SHA_CNT_OUTPUT_ENDIAN | SHA_NORMAL_ROUND;
const u32 *src32 = (const u32 *)src;
int i;
while(size >= 0x40)
{
sha_wait_idle();
for(i = 0; i < 4; ++i)
{
*REG_SHA_INFIFO = *src32++;
*REG_SHA_INFIFO = *src32++;
*REG_SHA_INFIFO = *src32++;
*REG_SHA_INFIFO = *src32++;
}
const u32 *src32 = (const u32 *)src; size -= 0x40;
int i; }
while(size >= 0x40)
{ sha_wait_idle();
sha_wait_idle(); memcpy((void *)REG_SHA_INFIFO, src32, size);
for(i = 0; i < 4; ++i)
{ *REG_SHA_CNT = (*REG_SHA_CNT & ~SHA_NORMAL_ROUND) | SHA_FINAL_ROUND;
*REG_SHA_INFIFO = *src32++;
*REG_SHA_INFIFO = *src32++; while(*REG_SHA_CNT & SHA_FINAL_ROUND);
*REG_SHA_INFIFO = *src32++; sha_wait_idle();
*REG_SHA_INFIFO = *src32++;
} u32 hashSize = SHA_256_HASH_SIZE;
if(mode == SHA_224_MODE)
hashSize = SHA_224_HASH_SIZE;
else if(mode == SHA_1_MODE)
hashSize = SHA_1_HASH_SIZE;
size -= 0x40; memcpy(res, (void *)REG_SHA_HASH, hashSize);
}
sha_wait_idle();
memcpy((void *)REG_SHA_INFIFO, src32, size);
*REG_SHA_CNT = (*REG_SHA_CNT & ~SHA_NORMAL_ROUND) | SHA_FINAL_ROUND;
while(*REG_SHA_CNT & SHA_FINAL_ROUND);
sha_wait_idle();
u32 hashSize = SHA_256_HASH_SIZE;
if(mode == SHA_224_MODE)
hashSize = SHA_224_HASH_SIZE;
else if(mode == SHA_1_MODE)
hashSize = SHA_1_HASH_SIZE;
memcpy(res, (void *)REG_SHA_HASH, hashSize);
} }
/*****************************************************************/ /****************************************************************
* Nand/FIRM Crypto stuff
****************************************************************/
static u8 __attribute__((aligned(4))) nandCtr[AES_BLOCK_SIZE]; static u8 CTR[0x10];
static u8 nandSlot;
static u32 fatStart; static const u8 a9lhKey2[0x10] = {
__attribute__((aligned(4))) const u8 key2s[5][AES_BLOCK_SIZE] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3B, 0xF5, 0xF6
{0x42, 0x3F, 0x81, 0x7A, 0x23, 0x52, 0x58, 0x31, 0x6E, 0x75, 0x8E, 0x3A, 0x39, 0x43, 0x2E, 0xD0},
{0xA5, 0x25, 0x87, 0x11, 0x8F, 0x42, 0x9F, 0x3D, 0x65, 0x1D, 0xDD, 0x58, 0x0B, 0x6D, 0xF2, 0x17},
{0x65, 0x29, 0x3E, 0x12, 0x56, 0x0C, 0x0B, 0xD1, 0xDD, 0xB5, 0x63, 0x1C, 0xB6, 0xD9, 0x52, 0x75},
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3B, 0xF5, 0xF6},
{0x08, 0x24, 0xD3, 0xCB, 0x4A, 0xE9, 0x4D, 0x62, 0x4D, 0xAA, 0x52, 0x60, 0x47, 0xC5, 0x93, 0x94}
},
devKey2s[2][AES_BLOCK_SIZE] = {
{0xFF, 0x77, 0xA0, 0x9A, 0x99, 0x81, 0xE9, 0x48, 0xEC, 0x51, 0xC9, 0x32, 0x5D, 0x14, 0xEC, 0x25},
{0x59, 0x5B, 0x98, 0x5B, 0xD7, 0x74, 0x58, 0x7F, 0x89, 0x30, 0x85, 0x70, 0xD6, 0x24, 0x49, 0x1E}
}; };
void getNandCtr(void) static const u8 key2[0x10] = {
{ 0x42, 0x3F, 0x81, 0x7A, 0x23, 0x52, 0x58, 0x31, 0x6E, 0x75, 0x8E, 0x3A, 0x39, 0x43, 0x2E, 0xD0
__attribute__((aligned(4))) u8 cid[AES_BLOCK_SIZE], };
shaSum[SHA_256_HASH_SIZE];
sdmmc_get_cid(1, (u32 *)cid); //Get Nand CTR key
sha(shaSum, cid, sizeof(cid), SHA_256_MODE); void getNandCTR(void){
memcpy(nandCtr, shaSum, sizeof(nandCtr)); u8 NandCid[0x10];
u8 shasum[0x20];
sdmmc_get_cid(1, (u32 *)NandCid);
sha(shasum, NandCid, 0x10, SHA_256_MODE);
memcpy(CTR, shasum, 0x10);
} }
void ctrNandInit(void) //Read and decrypt from the FIRM0 partition on NAND
{ void readFirm0(u8 *outbuf, u32 size){
getNandCtr(); u8 CTRtmp[sizeof(CTR)];
memcpy(CTRtmp, CTR, sizeof(CTR));
if(ISN3DS) aes_advctr(CTRtmp, 0x0B130000 / 0x10, AES_INPUT_BE | AES_INPUT_NORMAL);
{
__attribute__((aligned(4))) u8 keyY0x5[AES_BLOCK_SIZE] = {0x4D, 0x80, 0x4F, 0x4E, 0x99, 0x90, 0x19, 0x46, 0x13, 0xA2, 0x04, 0xAC, 0x58, 0x44, 0x60, 0xBE};
aes_setkey(0x05, keyY0x5, AES_KEYY, AES_INPUT_BE | AES_INPUT_NORMAL);
nandSlot = 0x05;
fatStart = 0x5CAD7;
}
else
{
nandSlot = 0x04;
fatStart = 0x5CAE5;
}
}
int ctrNandRead(u32 sector, u32 sectorCount, u8 *outbuf)
{
__attribute__((aligned(4))) u8 tmpCtr[sizeof(nandCtr)];
memcpy(tmpCtr, nandCtr, sizeof(nandCtr));
aes_advctr(tmpCtr, ((sector + fatStart) * 0x200) / AES_BLOCK_SIZE, AES_INPUT_BE | AES_INPUT_NORMAL);
//Read from NAND
int result = sdmmc_nand_readsectors(sector + fatStart, sectorCount, outbuf);
//Decrypt
aes_use_keyslot(nandSlot);
aes(outbuf, outbuf, sectorCount * 0x200 / AES_BLOCK_SIZE, tmpCtr, AES_CTR_MODE, AES_INPUT_BE | AES_INPUT_NORMAL);
return result;
}
void readFirm0(u8 *outbuf, u32 size)
{
__attribute__((aligned(4))) u8 tmpCtr[sizeof(nandCtr)];
memcpy(tmpCtr, nandCtr, sizeof(nandCtr));
aes_advctr(tmpCtr, 0x0B130000 / AES_BLOCK_SIZE, AES_INPUT_BE | AES_INPUT_NORMAL);
//Read from NAND
sdmmc_nand_readsectors(0x0B130000 / 0x200, size / 0x200, outbuf); sdmmc_nand_readsectors(0x0B130000 / 0x200, size / 0x200, outbuf);
//Decrypt
aes_use_keyslot(0x06); aes_use_keyslot(0x06);
aes(outbuf, outbuf, size / AES_BLOCK_SIZE, tmpCtr, AES_CTR_MODE, AES_INPUT_BE | AES_INPUT_NORMAL); aes(outbuf, outbuf, size / AES_BLOCK_SIZE, CTRtmp, AES_CTR_MODE, AES_INPUT_BE | AES_INPUT_NORMAL);
} }
void writeFirm(u8 *inbuf, bool isFirm1, u32 size) //Encrypt and write a FIRM partition to NAND
{ void writeFirm(u8 *inbuf, u32 firm, u32 size){
u32 offset = isFirm1 ? 0x0B530000 : 0x0B130000; u32 offset = firm ? 0x0B530000 : 0x0B130000;
__attribute__((aligned(4))) u8 tmpCtr[sizeof(nandCtr)]; u8 CTRtmp[sizeof(CTR)];
memcpy(tmpCtr, nandCtr, sizeof(nandCtr)); memcpy(CTRtmp, CTR, sizeof(CTR));
aes_advctr(tmpCtr, offset / AES_BLOCK_SIZE, AES_INPUT_BE | AES_INPUT_NORMAL);
//Encrypt aes_advctr(CTRtmp, offset / 0x10, AES_INPUT_BE | AES_INPUT_NORMAL);
aes_use_keyslot(0x06); aes_use_keyslot(0x06);
aes(inbuf, inbuf, size / AES_BLOCK_SIZE, tmpCtr, AES_CTR_MODE, AES_INPUT_BE | AES_INPUT_NORMAL); aes(inbuf, inbuf, size / AES_BLOCK_SIZE, CTRtmp, AES_CTR_MODE, AES_INPUT_BE | AES_INPUT_NORMAL);
//Write to NAND
sdmmc_nand_writesectors(offset / 0x200, size / 0x200, inbuf); sdmmc_nand_writesectors(offset / 0x200, size / 0x200, inbuf);
} }
void setupKeyslot0x11(const void *otp) void setupKeyslot0x11(const u8 *otp){
{ u8 shasum[0x20];
__attribute__((aligned(4))) u8 shasum[SHA_256_HASH_SIZE]; u8 keyX[0x10];
u8 keyY[0x10];
//If booting via A9LH, use the leftover contents of the SHA register //Set keyX and keyY for key sector encryption
if(ISA9LH) memcpy(shasum, (void *)REG_SHA_HASH, sizeof(shasum)); sha(shasum, otp, 0x90, SHA_256_MODE);
memcpy(keyX, shasum, 0x10);
//Otherwise, calculate the otp.bin hash memcpy(keyY, shasum + 0x10, 0x10);
else sha(shasum, otp, 0x90, SHA_256_MODE); aes_setkey(0x11, keyX, AES_KEYX, AES_INPUT_BE | AES_INPUT_NORMAL);
aes_setkey(0x11, keyY, AES_KEYY, AES_INPUT_BE | AES_INPUT_NORMAL);
//Set keyX and keyY
aes_setkey(0x11, shasum, AES_KEYX, AES_INPUT_BE | AES_INPUT_NORMAL);
aes_setkey(0x11, shasum + AES_BLOCK_SIZE, AES_KEYY, AES_INPUT_BE | AES_INPUT_NORMAL);
} }
void generateSector(u8 *keySector, u32 mode) //Get Nand CTR key
{ void generateSector(u8 *keySector){
//Inject key2 //Inject A9LH key2
switch(mode) memcpy(keySector + 0x10, a9lhKey2, 0x10);
{
case 0:
memcpy(keySector + AES_BLOCK_SIZE, !ISDEVUNIT ? key2s[1] : devKey2s[1], AES_BLOCK_SIZE);
break;
case 1:
memcpy(keySector + AES_BLOCK_SIZE, keySector, AES_BLOCK_SIZE);
return;
case 2:
memcpy(keySector + AES_BLOCK_SIZE, !ISDEVUNIT ? key2s[0] : devKey2s[0], AES_BLOCK_SIZE);
break;
}
//Encrypt key sector //Encrypt key sector
aes_use_keyslot(0x11); aes_use_keyslot(0x11);
for(u32 i = 0; i < 32; i++) for(u32 i = 0; i<32; i++)
aes(keySector + (AES_BLOCK_SIZE * i), keySector + (AES_BLOCK_SIZE * i), 1, NULL, AES_ECB_ENCRYPT_MODE, 0); aes(keySector + (0x10 * i), keySector + (0x10 * i), 1, NULL, AES_ECB_ENCRYPT_MODE, 0);
} }
void getSector(u8 *keySector) //Get Nand CTR key
{ u32 testOtp(u32 mode){
//Read keysector from NAND //Read keysector from NAND
sdmmc_nand_readsectors(0x96, 1, keySector); sdmmc_nand_readsectors(0x96, 0x1, (vu8 *)0x24500000);
if(!ISA9LH && !ISDEVUNIT) return; //Decrypt key2
//Decrypt key sector
aes_use_keyslot(0x11); aes_use_keyslot(0x11);
for(u32 i = 0; i < 32; i++) aes((void *)0x24500000 + 0x10, (void *)0x24500000 + 0x10, 1, NULL, AES_ECB_DECRYPT_MODE, 0);
aes(keySector + (AES_BLOCK_SIZE * i), keySector + (AES_BLOCK_SIZE * i), 1, NULL, AES_ECB_DECRYPT_MODE, 0);
//Test key2
if(memcmp((void *)0x24500000 + 0x10, mode ? key2 : a9lhKey2, 0x10) != 0) return 0;
return 1;
} }
bool verifyHash(const void *data, u32 size, const u8 *hash) u32 verifyHash(const void *data, u32 size, const u8 *hash){
{ u8 shasum[0x20];
__attribute__((aligned(4))) u8 shasum[SHA_256_HASH_SIZE];
sha(shasum, data, size, SHA_256_MODE); sha(shasum, data, size, SHA_256_MODE);
if(memcmp(shasum, hash, 0x20) != 0) return 0;
return memcmp(shasum, hash, sizeof(shasum)) == 0; return 1;
}
u32 decryptExeFs(Cxi *cxi)
{
u32 exeFsSize = 0;
if(memcmp(cxi->ncch.magic, "NCCH", 4) != 0) return exeFsSize;
u8 *exeFsOffset = (u8 *)cxi + (cxi->ncch.exeFsOffset + 1) * 0x200;
exeFsSize = (cxi->ncch.exeFsSize - 1) * 0x200;
__attribute__((aligned(4))) u8 ncchCtr[AES_BLOCK_SIZE] = {0};
for(u32 i = 0; i < 8; i++)
ncchCtr[7 - i] = cxi->ncch.partitionId[i];
ncchCtr[8] = 2;
aes_setkey(0x2C, cxi, AES_KEYY, AES_INPUT_BE | AES_INPUT_NORMAL);
aes_advctr(ncchCtr, 0x200 / AES_BLOCK_SIZE, AES_INPUT_BE | AES_INPUT_NORMAL);
aes_use_keyslot(0x2C);
aes(cxi, exeFsOffset, exeFsSize / AES_BLOCK_SIZE, ncchCtr, AES_CTR_MODE, AES_INPUT_BE | AES_INPUT_NORMAL);
if(memcmp(cxi, "FIRM", 4) != 0) exeFsSize = 0;
return exeFsSize;
} }

View File

@ -1,8 +1,4 @@
/* // From http://github.com/b1l1s/ctr
* crypto.h
*
* Crypto libs from http://github.com/b1l1s/ctr
*/
#pragma once #pragma once
@ -79,13 +75,11 @@
#define SHA_224_HASH_SIZE (224 / 8) #define SHA_224_HASH_SIZE (224 / 8)
#define SHA_1_HASH_SIZE (160 / 8) #define SHA_1_HASH_SIZE (160 / 8)
void getNandCtr(void); //NAND/FIRM stuff
void ctrNandInit(void); void getNandCTR(void);
int ctrNandRead(u32 sector, u32 sectorCount, u8 *outbuf);
void readFirm0(u8 *outbuf, u32 size); void readFirm0(u8 *outbuf, u32 size);
void writeFirm(u8 *inbuf, bool isFirm1, u32 size); void writeFirm(u8 *inbuf, u32 offset, u32 size);
void setupKeyslot0x11(const void *otp); void setupKeyslot0x11(const u8 *otp);
void generateSector(u8 *keySector, u32 mode); void generateSector(u8 *keySector);
void getSector(u8 *keySector); u32 testOtp(u32 mode);
bool verifyHash(const void *data, u32 size, const u8 *hash); u32 verifyHash(const void *data, u32 size, const u8 *hash);
u32 decryptExeFs(Cxi *cxi);

View File

@ -1,83 +1,68 @@
/*
* This file is part of Luma3DS
* Copyright (C) 2016 Aurora Wright, TuxSH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Additional Terms 7.b of GPLv3 applies to this file: Requiring preservation of specified
* reasonable legal notices or author attributions in that material or in the Appropriate Legal
* Notices displayed by works containing it.
*/
/*
* Code to print to the screen by mid-kid @CakesFW
* https://github.com/mid-kid/CakesForeveryWan
*/
#include "draw.h" #include "draw.h"
#include "screen.h" #include "memory.h"
#include "strings.h"
#include "font.h" #include "font.h"
static void drawCharacter(char character, u32 posX, u32 posY, u32 color) #define SCREEN_TOP_WIDTH 400
#define SCREEN_TOP_HEIGHT 240
static const struct fb {
u8 *top_left;
u8 *top_right;
u8 *bottom;
} *const fb = (struct fb *)0x23FFFE00;
static int strlen(const char *string)
{
char *stringEnd = (char *)string;
while (*stringEnd) stringEnd++;
return stringEnd - string;
}
void clearScreens()
{
memset32(fb->top_left, 0, 0x46500);
memset32(fb->top_right, 0, 0x46500);
memset32(fb->bottom, 0, 0x38400);
}
void drawCharacter(char character, int pos_x, int pos_y, u32 color)
{ {
u8 *select = fb->top_left; u8 *select = fb->top_left;
for(u32 y = 0; y < 8; y++) for (int y = 0; y < 8; y++) {
{ unsigned char char_pos = font[character * 8 + y];
char charPos = font[character * 8 + y];
for(u32 x = 0; x < 8; x++) for (int x = 7; x >= 0; x--) {
if(((charPos >> (7 - x)) & 1) == 1) // I'll just assume both screens have the same height.
{ int screen_pos = (pos_x * SCREEN_TOP_HEIGHT * 3 + (SCREEN_TOP_HEIGHT - y - pos_y - 1) * 3) + (7 - x) * 3 * SCREEN_TOP_HEIGHT;
u32 screenPos = (posX * SCREEN_HEIGHT * 3 + (SCREEN_HEIGHT - y - posY - 1) * 3) + x * 3 * SCREEN_HEIGHT;
select[screenPos] = color >> 16; if ((char_pos >> x) & 1) {
select[screenPos + 1] = color >> 8; select[screen_pos] = color >> 16;
select[screenPos + 2] = color; select[screen_pos + 1] = color >> 8;
select[screen_pos + 2] = color;
} }
}
} }
} }
u32 drawString(const char *string, u32 posX, u32 posY, u32 color) int drawString(const char *string, int pos_x, int pos_y, u32 color)
{ {
for(u32 i = 0, line_i = 0; i < strlen(string); i++) int length = strlen(string);
switch(string[i])
{
case '\n':
posY += SPACING_Y;
line_i = 0;
break;
case '\t': for (int i = 0, line_i = 0; i < length; i++, line_i++) {
line_i += 2; if (string[i] == '\n') {
break; pos_y += SPACING_VERT;
line_i = 0;
default: i++;
//Make sure we never get out of the screen } else if (line_i >= (SCREEN_TOP_WIDTH - pos_x) / SPACING_HORIZ) {
if(line_i >= (SCREEN_TOP_WIDTH - posX) / SPACING_X) // Make sure we never get out of the screen.
{ pos_y += SPACING_VERT;
posY += SPACING_Y; line_i = 2; // Little offset so we know the same string continues.
line_i = 1; //Little offset so we know the same string continues if (string[i] == ' ') i++; // Spaces at the start look weird
if(string[i] == ' ') break; //Spaces at the start look weird
}
drawCharacter(string[i], posX + line_i * SPACING_X, posY, color);
line_i++;
break;
} }
return posY + SPACING_Y; drawCharacter(string[i], pos_x + line_i * SPACING_HORIZ, pos_y, color);
}
return pos_y + SPACING_VERT;
} }

View File

@ -1,35 +1,10 @@
/*
* This file is part of Luma3DS
* Copyright (C) 2016 Aurora Wright, TuxSH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Additional Terms 7.b of GPLv3 applies to this file: Requiring preservation of specified
* reasonable legal notices or author attributions in that material or in the Appropriate Legal
* Notices displayed by works containing it.
*/
/*
* Code to print to the screen by mid-kid @CakesFW
* https://github.com/mid-kid/CakesForeveryWan
*/
#pragma once #pragma once
#include "types.h" #include "types.h"
#define SPACING_Y 10 #define SPACING_VERT 10
#define SPACING_X 8 #define SPACING_HORIZ 8
u32 drawString(const char *string, u32 posX, u32 posY, u32 color); void clearScreens();
void drawCharacter(char character, int pos_x, int pos_y, u32 color);
int drawString(const char *string, int pos_x, int pos_y, u32 color);

View File

@ -1,21 +1,21 @@
FatFs Module Source Files R0.12c FatFs Module Source Files R0.11
FILES FILES
00readme.txt This file. 00readme.txt This file.
00history.txt Revision history. history.txt Revision history.
ff.c FatFs module. ffconf.h Configuration file for FatFs module.
ffconf.h Configuration file of FatFs module. ff.h Common include file for FatFs and application module.
ff.h Common include file for FatFs and application module. ff.c FatFs module.
diskio.h Common include file for FatFs and disk I/O module. diskio.h Common include file for FatFs and disk I/O module.
diskio.c An example of glue function to attach existing disk I/O module to FatFs. diskio.c An example of glue function to attach existing disk I/O module to FatFs.
integer.h Integer type definitions for FatFs. integer.h Integer type definitions for FatFs.
option Optional external modules. option Optional external functions.
Low level disk I/O module is not included in this archive because the FatFs Low level disk I/O module is not included in this archive because the FatFs
module is only a generic file system layer and it does not depend on any specific module is only a generic file system layer and not depend on any specific
storage device. You have to provide a low level disk I/O module written to storage device. You have to provide a low level disk I/O module that written
control the storage device that attached to the target system. to control the target storage device.

View File

@ -9,11 +9,7 @@
#include "diskio.h" /* FatFs lower layer API */ #include "diskio.h" /* FatFs lower layer API */
#include "sdmmc/sdmmc.h" #include "sdmmc/sdmmc.h"
#include "../crypto.h"
/* Definitions of physical drive number for each media */
#define SDCARD 0
#define CTRNAND 1
/*-----------------------------------------------------------------------*/ /*-----------------------------------------------------------------------*/
/* Get Drive Status */ /* Get Drive Status */
@ -34,12 +30,12 @@ DSTATUS disk_status (
/*-----------------------------------------------------------------------*/ /*-----------------------------------------------------------------------*/
DSTATUS disk_initialize ( DSTATUS disk_initialize (
__attribute__((unused))
BYTE pdrv /* Physical drive nmuber to identify the drive */ BYTE pdrv /* Physical drive nmuber to identify the drive */
) )
{ {
if(pdrv == CTRNAND) ctrNandInit(); sdmmc_sdcard_init();
return RES_OK;
return 0;
} }
@ -49,14 +45,18 @@ DSTATUS disk_initialize (
/*-----------------------------------------------------------------------*/ /*-----------------------------------------------------------------------*/
DRESULT disk_read ( DRESULT disk_read (
__attribute__((unused))
BYTE pdrv, /* Physical drive nmuber to identify the drive */ BYTE pdrv, /* Physical drive nmuber to identify the drive */
BYTE *buff, /* Data buffer to store read data */ BYTE *buff, /* Data buffer to store read data */
DWORD sector, /* Sector address in LBA */ DWORD sector, /* Sector address in LBA */
UINT count /* Number of sectors to read */ UINT count /* Number of sectors to read */
) )
{ {
return ((pdrv == SDCARD && !sdmmc_sdcard_readsectors(sector, count, buff)) || if (sdmmc_sdcard_readsectors(sector, count, buff)) {
(pdrv == CTRNAND && !ctrNandRead(sector, count, buff))) ? RES_OK : RES_PARERR; return RES_PARERR;
}
return RES_OK;
} }
@ -67,13 +67,18 @@ DRESULT disk_read (
#if _USE_WRITE #if _USE_WRITE
DRESULT disk_write ( DRESULT disk_write (
__attribute__((unused))
BYTE pdrv, /* Physical drive nmuber to identify the drive */ BYTE pdrv, /* Physical drive nmuber to identify the drive */
const BYTE *buff, /* Data to be written */ const BYTE *buff, /* Data to be written */
DWORD sector, /* Sector address in LBA */ DWORD sector, /* Sector address in LBA */
UINT count /* Number of sectors to write */ UINT count /* Number of sectors to write */
) )
{ {
return (pdrv == SDCARD && !sdmmc_sdcard_writesectors(sector, count, buff)) ? RES_OK : RES_PARERR; if (sdmmc_sdcard_writesectors(sector, count, (BYTE *)buff)) {
return RES_PARERR;
}
return RES_OK;
} }
#endif #endif

File diff suppressed because it is too large Load Diff

View File

@ -1,361 +1,350 @@
/*----------------------------------------------------------------------------/ /*---------------------------------------------------------------------------/
/ FatFs - Generic FAT file system module R0.12c / / FatFs - FAT file system module include R0.11 (C)ChaN, 2015
/-----------------------------------------------------------------------------/ /----------------------------------------------------------------------------/
/ / FatFs module is a free software that opened under license policy of
/ Copyright (C) 2017, ChaN, all right reserved. / following conditions.
/ /
/ FatFs module is an open source software. Redistribution and use of FatFs in / Copyright (C) 2015, ChaN, all right reserved.
/ source and binary forms, with or without modification, are permitted provided /
/ that the following condition is met: / 1. Redistributions of source code must retain the above copyright notice,
/ this condition and the following disclaimer.
/ 1. Redistributions of source code must retain the above copyright notice, /
/ this condition and the following disclaimer. / This software is provided by the copyright holder and contributors "AS IS"
/ / and any warranties related to this software are DISCLAIMED.
/ This software is provided by the copyright holder and contributors "AS IS" / The copyright owner or contributors be NOT LIABLE for any damages caused
/ and any warranties related to this software are DISCLAIMED. / by use of this software.
/ The copyright owner or contributors be NOT LIABLE for any damages caused /---------------------------------------------------------------------------*/
/ by use of this software.
/----------------------------------------------------------------------------*/
#ifndef _FATFS
#define _FATFS 32020 /* Revision ID */
#ifndef _FATFS
#define _FATFS 68300 /* Revision ID */ #ifdef __cplusplus
extern "C" {
#ifdef __cplusplus #endif
extern "C" {
#endif #include "integer.h" /* Basic integer types */
#include "ffconf.h" /* FatFs configuration options */
#include "integer.h" /* Basic integer types */ #if _FATFS != _FFCONF
#include "ffconf.h" /* FatFs configuration options */ #error Wrong configuration file (ffconf.h).
#endif
#if _FATFS != _FFCONF
#error Wrong configuration file (ffconf.h).
#endif
/* Definitions of volume management */
#if _MULTI_PARTITION /* Multiple partition configuration */
/* Definitions of volume management */ typedef struct {
BYTE pd; /* Physical drive number */
#if _MULTI_PARTITION /* Multiple partition configuration */ BYTE pt; /* Partition: 0:Auto detect, 1-4:Forced partition) */
typedef struct { } PARTITION;
BYTE pd; /* Physical drive number */ extern PARTITION VolToPart[]; /* Volume - Partition resolution table */
BYTE pt; /* Partition: 0:Auto detect, 1-4:Forced partition) */ #define LD2PD(vol) (VolToPart[vol].pd) /* Get physical drive number */
} PARTITION; #define LD2PT(vol) (VolToPart[vol].pt) /* Get partition index */
extern PARTITION VolToPart[]; /* Volume - Partition resolution table */
#endif #else /* Single partition configuration */
#define LD2PD(vol) (BYTE)(vol) /* Each logical drive is bound to the same physical drive number */
#define LD2PT(vol) 0 /* Find first valid partition or in SFD */
/* Type of path name strings on FatFs API */ #endif
#if _LFN_UNICODE /* Unicode (UTF-16) string */
#if _USE_LFN == 0
#error _LFN_UNICODE must be 0 at non-LFN cfg. /* Type of path name strings on FatFs API */
#endif
#ifndef _INC_TCHAR #if _LFN_UNICODE /* Unicode string */
typedef WCHAR TCHAR; #if !_USE_LFN
#define _T(x) L ## x #error _LFN_UNICODE must be 0 at non-LFN cfg.
#define _TEXT(x) L ## x #endif
#endif #ifndef _INC_TCHAR
#else /* ANSI/OEM string */ typedef WCHAR TCHAR;
#ifndef _INC_TCHAR #define _T(x) L ## x
typedef char TCHAR; #define _TEXT(x) L ## x
#define _T(x) x #endif
#define _TEXT(x) x
#endif #else /* ANSI/OEM string */
#endif #ifndef _INC_TCHAR
typedef char TCHAR;
#define _T(x) x
#define _TEXT(x) x
/* Type of file size variables */ #endif
#if _FS_EXFAT #endif
#if _USE_LFN == 0
#error LFN must be enabled when enable exFAT
#endif
typedef QWORD FSIZE_t; /* File system object structure (FATFS) */
#else
typedef DWORD FSIZE_t; typedef struct {
#endif BYTE fs_type; /* FAT sub-type (0:Not mounted) */
BYTE drv; /* Physical drive number */
BYTE csize; /* Sectors per cluster (1,2,4...128) */
BYTE n_fats; /* Number of FAT copies (1 or 2) */
/* File system object structure (FATFS) */ BYTE wflag; /* win[] flag (b0:dirty) */
BYTE fsi_flag; /* FSINFO flags (b7:disabled, b0:dirty) */
typedef struct { WORD id; /* File system mount ID */
BYTE fs_type; /* File system type (0:N/A) */ WORD n_rootdir; /* Number of root directory entries (FAT12/16) */
BYTE drv; /* Physical drive number */ #if _MAX_SS != _MIN_SS
BYTE n_fats; /* Number of FATs (1 or 2) */ WORD ssize; /* Bytes per sector (512, 1024, 2048 or 4096) */
BYTE wflag; /* win[] flag (b0:dirty) */ #endif
BYTE fsi_flag; /* FSINFO flags (b7:disabled, b0:dirty) */ #if _FS_REENTRANT
WORD id; /* File system mount ID */ _SYNC_t sobj; /* Identifier of sync object */
WORD n_rootdir; /* Number of root directory entries (FAT12/16) */ #endif
WORD csize; /* Cluster size [sectors] */ #if !_FS_READONLY
#if _MAX_SS != _MIN_SS DWORD last_clust; /* Last allocated cluster */
WORD ssize; /* Sector size (512, 1024, 2048 or 4096) */ DWORD free_clust; /* Number of free clusters */
#endif #endif
#if _USE_LFN != 0 #if _FS_RPATH
WCHAR* lfnbuf; /* LFN working buffer */ DWORD cdir; /* Current directory start cluster (0:root) */
#endif #endif
#if _FS_EXFAT DWORD n_fatent; /* Number of FAT entries, = number of clusters + 2 */
BYTE* dirbuf; /* Directory entry block scratchpad buffer */ DWORD fsize; /* Sectors per FAT */
#endif DWORD volbase; /* Volume start sector */
#if _FS_REENTRANT DWORD fatbase; /* FAT start sector */
_SYNC_t sobj; /* Identifier of sync object */ DWORD dirbase; /* Root directory start sector (FAT32:Cluster#) */
#endif DWORD database; /* Data start sector */
#if !_FS_READONLY DWORD winsect; /* Current sector appearing in the win[] */
DWORD last_clst; /* Last allocated cluster */ BYTE win[_MAX_SS]; /* Disk access window for Directory, FAT (and file data at tiny cfg) */
DWORD free_clst; /* Number of free clusters */ } FATFS;
#endif
#if _FS_RPATH != 0
DWORD cdir; /* Current directory start cluster (0:root) */
#if _FS_EXFAT /* File object structure (FIL) */
DWORD cdc_scl; /* Containing directory start cluster (invalid when cdir is 0) */
DWORD cdc_size; /* b31-b8:Size of containing directory, b7-b0: Chain status */ typedef struct {
DWORD cdc_ofs; /* Offset in the containing directory (invalid when cdir is 0) */ FATFS* fs; /* Pointer to the related file system object (**do not change order**) */
#endif WORD id; /* Owner file system mount ID (**do not change order**) */
#endif BYTE flag; /* Status flags */
DWORD n_fatent; /* Number of FAT entries (number of clusters + 2) */ BYTE err; /* Abort flag (error code) */
DWORD fsize; /* Size of an FAT [sectors] */ DWORD fptr; /* File read/write pointer (Zeroed on file open) */
DWORD volbase; /* Volume base sector */ DWORD fsize; /* File size */
DWORD fatbase; /* FAT base sector */ DWORD sclust; /* File start cluster (0:no cluster chain, always 0 when fsize is 0) */
DWORD dirbase; /* Root directory base sector/cluster */ DWORD clust; /* Current cluster of fpter (not valid when fprt is 0) */
DWORD database; /* Data base sector */ DWORD dsect; /* Sector number appearing in buf[] (0:invalid) */
DWORD winsect; /* Current sector appearing in the win[] */ #if !_FS_READONLY
BYTE win[_MAX_SS]; /* Disk access window for Directory, FAT (and file data at tiny cfg) */ DWORD dir_sect; /* Sector number containing the directory entry */
} FATFS; BYTE* dir_ptr; /* Pointer to the directory entry in the win[] */
#endif
#if _USE_FASTSEEK
DWORD* cltbl; /* Pointer to the cluster link map table (Nulled on file open) */
/* Object ID and allocation information (_FDID) */ #endif
#if _FS_LOCK
typedef struct { UINT lockid; /* File lock ID origin from 1 (index of file semaphore table Files[]) */
FATFS* fs; /* Pointer to the owner file system object */ #endif
WORD id; /* Owner file system mount ID */ #if !_FS_TINY
BYTE attr; /* Object attribute */ BYTE buf[_MAX_SS]; /* File private data read/write window */
BYTE stat; /* Object chain status (b1-0: =0:not contiguous, =2:contiguous (no data on FAT), =3:flagmented in this session, b2:sub-directory stretched) */ #endif
DWORD sclust; /* Object start cluster (0:no cluster or root directory) */ } FIL;
FSIZE_t objsize; /* Object size (valid when sclust != 0) */
#if _FS_EXFAT
DWORD n_cont; /* Size of first fragment, clusters - 1 (valid when stat == 3) */
DWORD n_frag; /* Size of last fragment needs to be written (valid when not zero) */ /* Directory object structure (DIR) */
DWORD c_scl; /* Containing directory start cluster (valid when sclust != 0) */
DWORD c_size; /* b31-b8:Size of containing directory, b7-b0: Chain status (valid when c_scl != 0) */ typedef struct {
DWORD c_ofs; /* Offset in the containing directory (valid when sclust != 0 and non-directory object) */ FATFS* fs; /* Pointer to the owner file system object (**do not change order**) */
#endif WORD id; /* Owner file system mount ID (**do not change order**) */
#if _FS_LOCK != 0 WORD index; /* Current read/write index number */
UINT lockid; /* File lock ID origin from 1 (index of file semaphore table Files[]) */ DWORD sclust; /* Table start cluster (0:Root dir) */
#endif DWORD clust; /* Current cluster */
} _FDID; DWORD sect; /* Current sector */
BYTE* dir; /* Pointer to the current SFN entry in the win[] */
BYTE* fn; /* Pointer to the SFN (in/out) {file[8],ext[3],status[1]} */
#if _FS_LOCK
/* File object structure (FIL) */ UINT lockid; /* File lock ID (index of file semaphore table Files[]) */
#endif
typedef struct { #if _USE_LFN
_FDID obj; /* Object identifier (must be the 1st member to detect invalid object pointer) */ WCHAR* lfn; /* Pointer to the LFN working buffer */
BYTE flag; /* File status flags */ WORD lfn_idx; /* Last matched LFN index number (0xFFFF:No LFN) */
BYTE err; /* Abort flag (error code) */ #endif
FSIZE_t fptr; /* File read/write pointer (Zeroed on file open) */ #if _USE_FIND
DWORD clust; /* Current cluster of fpter (invalid when fptr is 0) */ const TCHAR* pat; /* Pointer to the name matching pattern */
DWORD sect; /* Sector number appearing in buf[] (0:invalid) */ #endif
#if !_FS_READONLY } DIR;
DWORD dir_sect; /* Sector number containing the directory entry */
BYTE* dir_ptr; /* Pointer to the directory entry in the win[] */
#endif
#if _USE_FASTSEEK /* File information structure (FILINFO) */
DWORD* cltbl; /* Pointer to the cluster link map table (nulled on open, set by application) */
#endif typedef struct {
#if !_FS_TINY DWORD fsize; /* File size */
BYTE buf[_MAX_SS]; /* File private data read/write window */ WORD fdate; /* Last modified date */
#endif WORD ftime; /* Last modified time */
} FIL; BYTE fattrib; /* Attribute */
TCHAR fname[13]; /* Short file name (8.3 format) */
#if _USE_LFN
TCHAR* lfname; /* Pointer to the LFN buffer */
/* Directory object structure (DIR) */ UINT lfsize; /* Size of LFN buffer in TCHAR */
#endif
typedef struct { } FILINFO;
_FDID obj; /* Object identifier */
DWORD dptr; /* Current read/write offset */
DWORD clust; /* Current cluster */
DWORD sect; /* Current sector (0:Read operation has terminated) */ /* File function return code (FRESULT) */
BYTE* dir; /* Pointer to the directory item in the win[] */
BYTE fn[12]; /* SFN (in/out) {body[8],ext[3],status[1]} */ typedef enum {
#if _USE_LFN != 0 FR_OK = 0, /* (0) Succeeded */
DWORD blk_ofs; /* Offset of current entry block being processed (0xFFFFFFFF:Invalid) */ FR_DISK_ERR, /* (1) A hard error occurred in the low level disk I/O layer */
#endif FR_INT_ERR, /* (2) Assertion failed */
#if _USE_FIND FR_NOT_READY, /* (3) The physical drive cannot work */
const TCHAR* pat; /* Pointer to the name matching pattern */ FR_NO_FILE, /* (4) Could not find the file */
#endif FR_NO_PATH, /* (5) Could not find the path */
} DIR; FR_INVALID_NAME, /* (6) The path name format is invalid */
FR_DENIED, /* (7) Access denied due to prohibited access or directory full */
FR_EXIST, /* (8) Access denied due to prohibited access */
FR_INVALID_OBJECT, /* (9) The file/directory object is invalid */
/* File information structure (FILINFO) */ FR_WRITE_PROTECTED, /* (10) The physical drive is write protected */
FR_INVALID_DRIVE, /* (11) The logical drive number is invalid */
typedef struct { FR_NOT_ENABLED, /* (12) The volume has no work area */
FSIZE_t fsize; /* File size */ FR_NO_FILESYSTEM, /* (13) There is no valid FAT volume */
WORD fdate; /* Modified date */ FR_MKFS_ABORTED, /* (14) The f_mkfs() aborted due to any parameter error */
WORD ftime; /* Modified time */ FR_TIMEOUT, /* (15) Could not get a grant to access the volume within defined period */
BYTE fattrib; /* File attribute */ FR_LOCKED, /* (16) The operation is rejected according to the file sharing policy */
#if _USE_LFN != 0 FR_NOT_ENOUGH_CORE, /* (17) LFN working buffer could not be allocated */
TCHAR altname[13]; /* Altenative file name */ FR_TOO_MANY_OPEN_FILES, /* (18) Number of open files > _FS_SHARE */
TCHAR fname[_MAX_LFN + 1]; /* Primary file name */ FR_INVALID_PARAMETER /* (19) Given parameter is invalid */
#else } FRESULT;
TCHAR fname[13]; /* File name */
#endif
} FILINFO;
/*--------------------------------------------------------------*/
/* FatFs module application interface */
/* File function return code (FRESULT) */ FRESULT f_open (FIL* fp, const TCHAR* path, BYTE mode); /* Open or create a file */
FRESULT f_close (FIL* fp); /* Close an open file object */
typedef enum { FRESULT f_read (FIL* fp, void* buff, UINT btr, UINT* br); /* Read data from a file */
FR_OK = 0, /* (0) Succeeded */ FRESULT f_write (FIL* fp, const void* buff, UINT btw, UINT* bw); /* Write data to a file */
FR_DISK_ERR, /* (1) A hard error occurred in the low level disk I/O layer */ FRESULT f_forward (FIL* fp, UINT(*func)(const BYTE*,UINT), UINT btf, UINT* bf); /* Forward data to the stream */
FR_INT_ERR, /* (2) Assertion failed */ FRESULT f_lseek (FIL* fp, DWORD ofs); /* Move file pointer of a file object */
FR_NOT_READY, /* (3) The physical drive cannot work */ FRESULT f_truncate (FIL* fp); /* Truncate file */
FR_NO_FILE, /* (4) Could not find the file */ FRESULT f_sync (FIL* fp); /* Flush cached data of a writing file */
FR_NO_PATH, /* (5) Could not find the path */ FRESULT f_opendir (DIR* dp, const TCHAR* path); /* Open a directory */
FR_INVALID_NAME, /* (6) The path name format is invalid */ FRESULT f_closedir (DIR* dp); /* Close an open directory */
FR_DENIED, /* (7) Access denied due to prohibited access or directory full */ FRESULT f_readdir (DIR* dp, FILINFO* fno); /* Read a directory item */
FR_EXIST, /* (8) Access denied due to prohibited access */ FRESULT f_findfirst (DIR* dp, FILINFO* fno, const TCHAR* path, const TCHAR* pattern); /* Find first file */
FR_INVALID_OBJECT, /* (9) The file/directory object is invalid */ FRESULT f_findnext (DIR* dp, FILINFO* fno); /* Find next file */
FR_WRITE_PROTECTED, /* (10) The physical drive is write protected */ FRESULT f_mkdir (const TCHAR* path); /* Create a sub directory */
FR_INVALID_DRIVE, /* (11) The logical drive number is invalid */ FRESULT f_unlink (const TCHAR* path); /* Delete an existing file or directory */
FR_NOT_ENABLED, /* (12) The volume has no work area */ FRESULT f_rename (const TCHAR* path_old, const TCHAR* path_new); /* Rename/Move a file or directory */
FR_NO_FILESYSTEM, /* (13) There is no valid FAT volume */ FRESULT f_stat (const TCHAR* path, FILINFO* fno); /* Get file status */
FR_MKFS_ABORTED, /* (14) The f_mkfs() aborted due to any problem */ FRESULT f_chmod (const TCHAR* path, BYTE attr, BYTE mask); /* Change attribute of the file/dir */
FR_TIMEOUT, /* (15) Could not get a grant to access the volume within defined period */ FRESULT f_utime (const TCHAR* path, const FILINFO* fno); /* Change times-tamp of the file/dir */
FR_LOCKED, /* (16) The operation is rejected according to the file sharing policy */ FRESULT f_chdir (const TCHAR* path); /* Change current directory */
FR_NOT_ENOUGH_CORE, /* (17) LFN working buffer could not be allocated */ FRESULT f_chdrive (const TCHAR* path); /* Change current drive */
FR_TOO_MANY_OPEN_FILES, /* (18) Number of open files > _FS_LOCK */ FRESULT f_getcwd (TCHAR* buff, UINT len); /* Get current directory */
FR_INVALID_PARAMETER /* (19) Given parameter is invalid */ FRESULT f_getfree (const TCHAR* path, DWORD* nclst, FATFS** fatfs); /* Get number of free clusters on the drive */
} FRESULT; FRESULT f_getlabel (const TCHAR* path, TCHAR* label, DWORD* vsn); /* Get volume label */
FRESULT f_setlabel (const TCHAR* label); /* Set volume label */
FRESULT f_mount (FATFS* fs, const TCHAR* path, BYTE opt); /* Mount/Unmount a logical drive */
FRESULT f_mkfs (const TCHAR* path, BYTE sfd, UINT au); /* Create a file system on the volume */
/*--------------------------------------------------------------*/ FRESULT f_fdisk (BYTE pdrv, const DWORD szt[], void* work); /* Divide a physical drive into some partitions */
/* FatFs module application interface */ int f_putc (TCHAR c, FIL* fp); /* Put a character to the file */
int f_puts (const TCHAR* str, FIL* cp); /* Put a string to the file */
FRESULT f_open (FIL* fp, const TCHAR* path, BYTE mode); /* Open or create a file */ int f_printf (FIL* fp, const TCHAR* str, ...); /* Put a formatted string to the file */
FRESULT f_close (FIL* fp); /* Close an open file object */ TCHAR* f_gets (TCHAR* buff, int len, FIL* fp); /* Get a string from the file */
FRESULT f_read (FIL* fp, void* buff, UINT btr, UINT* br); /* Read data from the file */
FRESULT f_write (FIL* fp, const void* buff, UINT btw, UINT* bw); /* Write data to the file */ #define f_eof(fp) ((int)((fp)->fptr == (fp)->fsize))
FRESULT f_lseek (FIL* fp, FSIZE_t ofs); /* Move file pointer of the file object */ #define f_error(fp) ((fp)->err)
FRESULT f_truncate (FIL* fp); /* Truncate the file */ #define f_tell(fp) ((fp)->fptr)
FRESULT f_sync (FIL* fp); /* Flush cached data of the writing file */ #define f_size(fp) ((fp)->fsize)
FRESULT f_opendir (DIR* dp, const TCHAR* path); /* Open a directory */ #define f_rewind(fp) f_lseek((fp), 0)
FRESULT f_closedir (DIR* dp); /* Close an open directory */ #define f_rewinddir(dp) f_readdir((dp), 0)
FRESULT f_readdir (DIR* dp, FILINFO* fno); /* Read a directory item */
FRESULT f_findfirst (DIR* dp, FILINFO* fno, const TCHAR* path, const TCHAR* pattern); /* Find first file */ #ifndef EOF
FRESULT f_findnext (DIR* dp, FILINFO* fno); /* Find next file */ #define EOF (-1)
FRESULT f_mkdir (const TCHAR* path); /* Create a sub directory */ #endif
FRESULT f_unlink (const TCHAR* path); /* Delete an existing file or directory */
FRESULT f_rename (const TCHAR* path_old, const TCHAR* path_new); /* Rename/Move a file or directory */
FRESULT f_stat (const TCHAR* path, FILINFO* fno); /* Get file status */
FRESULT f_chmod (const TCHAR* path, BYTE attr, BYTE mask); /* Change attribute of a file/dir */
FRESULT f_utime (const TCHAR* path, const FILINFO* fno); /* Change timestamp of a file/dir */ /*--------------------------------------------------------------*/
FRESULT f_chdir (const TCHAR* path); /* Change current directory */ /* Additional user defined functions */
FRESULT f_chdrive (const TCHAR* path); /* Change current drive */
FRESULT f_getcwd (TCHAR* buff, UINT len); /* Get current directory */ /* RTC function */
FRESULT f_getfree (const TCHAR* path, DWORD* nclst, FATFS** fatfs); /* Get number of free clusters on the drive */ #if !_FS_READONLY && !_FS_NORTC
FRESULT f_getlabel (const TCHAR* path, TCHAR* label, DWORD* vsn); /* Get volume label */ DWORD get_fattime (void);
FRESULT f_setlabel (const TCHAR* label); /* Set volume label */ #endif
FRESULT f_forward (FIL* fp, UINT(*func)(const BYTE*,UINT), UINT btf, UINT* bf); /* Forward data to the stream */
FRESULT f_expand (FIL* fp, FSIZE_t szf, BYTE opt); /* Allocate a contiguous block to the file */ /* Unicode support functions */
FRESULT f_mount (FATFS* fs, const TCHAR* path, BYTE opt); /* Mount/Unmount a logical drive */ #if _USE_LFN /* Unicode - OEM code conversion */
FRESULT f_mkfs (const TCHAR* path, BYTE opt, DWORD au, void* work, UINT len); /* Create a FAT volume */ WCHAR ff_convert (WCHAR chr, UINT dir); /* OEM-Unicode bidirectional conversion */
FRESULT f_fdisk (BYTE pdrv, const DWORD* szt, void* work); /* Divide a physical drive into some partitions */ WCHAR ff_wtoupper (WCHAR chr); /* Unicode upper-case conversion */
int f_putc (TCHAR c, FIL* fp); /* Put a character to the file */ #if _USE_LFN == 3 /* Memory functions */
int f_puts (const TCHAR* str, FIL* cp); /* Put a string to the file */ void* ff_memalloc (UINT msize); /* Allocate memory block */
int f_printf (FIL* fp, const TCHAR* str, ...); /* Put a formatted string to the file */ void ff_memfree (void* mblock); /* Free memory block */
TCHAR* f_gets (TCHAR* buff, int len, FIL* fp); /* Get a string from the file */ #endif
#endif
#define f_eof(fp) ((int)((fp)->fptr == (fp)->obj.objsize))
#define f_error(fp) ((fp)->err) /* Sync functions */
#define f_tell(fp) ((fp)->fptr) #if _FS_REENTRANT
#define f_size(fp) ((fp)->obj.objsize) int ff_cre_syncobj (BYTE vol, _SYNC_t* sobj); /* Create a sync object */
#define f_rewind(fp) f_lseek((fp), 0) int ff_req_grant (_SYNC_t sobj); /* Lock sync object */
#define f_rewinddir(dp) f_readdir((dp), 0) void ff_rel_grant (_SYNC_t sobj); /* Unlock sync object */
#define f_rmdir(path) f_unlink(path) int ff_del_syncobj (_SYNC_t sobj); /* Delete a sync object */
#endif
#ifndef EOF
#define EOF (-1)
#endif
/*--------------------------------------------------------------*/
/* Flags and offset address */
/*--------------------------------------------------------------*/
/* Additional user defined functions */ /* File access control and file status flags (FIL.flag) */
/* RTC function */ #define FA_READ 0x01
#if !_FS_READONLY && !_FS_NORTC #define FA_OPEN_EXISTING 0x00
DWORD get_fattime (void);
#endif #if !_FS_READONLY
#define FA_WRITE 0x02
/* Unicode support functions */ #define FA_CREATE_NEW 0x04
#if _USE_LFN != 0 /* Unicode - OEM code conversion */ #define FA_CREATE_ALWAYS 0x08
WCHAR ff_convert (WCHAR chr, UINT dir); /* OEM-Unicode bidirectional conversion */ #define FA_OPEN_ALWAYS 0x10
WCHAR ff_wtoupper (WCHAR chr); /* Unicode upper-case conversion */ #define FA__WRITTEN 0x20
#if _USE_LFN == 3 /* Memory functions */ #define FA__DIRTY 0x40
void* ff_memalloc (UINT msize); /* Allocate memory block */ #endif
void ff_memfree (void* mblock); /* Free memory block */
#endif
#endif /* FAT sub type (FATFS.fs_type) */
/* Sync functions */ #define FS_FAT12 1
#if _FS_REENTRANT #define FS_FAT16 2
int ff_cre_syncobj (BYTE vol, _SYNC_t* sobj); /* Create a sync object */ #define FS_FAT32 3
int ff_req_grant (_SYNC_t sobj); /* Lock sync object */
void ff_rel_grant (_SYNC_t sobj); /* Unlock sync object */
int ff_del_syncobj (_SYNC_t sobj); /* Delete a sync object */ /* File attribute bits for directory entry */
#endif
#define AM_RDO 0x01 /* Read only */
#define AM_HID 0x02 /* Hidden */
#define AM_SYS 0x04 /* System */
#define AM_VOL 0x08 /* Volume label */
/*--------------------------------------------------------------*/ #define AM_LFN 0x0F /* LFN entry */
/* Flags and offset address */ #define AM_DIR 0x10 /* Directory */
#define AM_ARC 0x20 /* Archive */
#define AM_MASK 0x3F /* Mask of defined bits */
/* File access mode and open method flags (3rd argument of f_open) */
#define FA_READ 0x01
#define FA_WRITE 0x02 /* Fast seek feature */
#define FA_OPEN_EXISTING 0x00 #define CREATE_LINKMAP 0xFFFFFFFF
#define FA_CREATE_NEW 0x04
#define FA_CREATE_ALWAYS 0x08
#define FA_OPEN_ALWAYS 0x10
#define FA_OPEN_APPEND 0x30 /*--------------------------------*/
/* Multi-byte word access macros */
/* Fast seek controls (2nd argument of f_lseek) */
#define CREATE_LINKMAP ((FSIZE_t)0 - 1) #if _WORD_ACCESS == 1 /* Enable word access to the FAT structure */
#define LD_WORD(ptr) (WORD)(*(WORD*)(BYTE*)(ptr))
/* Format options (2nd argument of f_mkfs) */ #define LD_DWORD(ptr) (DWORD)(*(DWORD*)(BYTE*)(ptr))
#define FM_FAT 0x01 #define ST_WORD(ptr,val) *(WORD*)(BYTE*)(ptr)=(WORD)(val)
#define FM_FAT32 0x02 #define ST_DWORD(ptr,val) *(DWORD*)(BYTE*)(ptr)=(DWORD)(val)
#define FM_EXFAT 0x04 #else /* Use byte-by-byte access to the FAT structure */
#define FM_ANY 0x07 #define LD_WORD(ptr) (WORD)(((WORD)*((BYTE*)(ptr)+1)<<8)|(WORD)*(BYTE*)(ptr))
#define FM_SFD 0x08 #define LD_DWORD(ptr) (DWORD)(((DWORD)*((BYTE*)(ptr)+3)<<24)|((DWORD)*((BYTE*)(ptr)+2)<<16)|((WORD)*((BYTE*)(ptr)+1)<<8)|*(BYTE*)(ptr))
#define ST_WORD(ptr,val) *(BYTE*)(ptr)=(BYTE)(val); *((BYTE*)(ptr)+1)=(BYTE)((WORD)(val)>>8)
/* Filesystem type (FATFS.fs_type) */ #define ST_DWORD(ptr,val) *(BYTE*)(ptr)=(BYTE)(val); *((BYTE*)(ptr)+1)=(BYTE)((WORD)(val)>>8); *((BYTE*)(ptr)+2)=(BYTE)((DWORD)(val)>>16); *((BYTE*)(ptr)+3)=(BYTE)((DWORD)(val)>>24)
#define FS_FAT12 1 #endif
#define FS_FAT16 2
#define FS_FAT32 3 #ifdef __cplusplus
#define FS_EXFAT 4 }
#endif
/* File attribute bits for directory entry (FILINFO.fattrib) */
#define AM_RDO 0x01 /* Read only */ #endif /* _FATFS */
#define AM_HID 0x02 /* Hidden */
#define AM_SYS 0x04 /* System */
#define AM_DIR 0x10 /* Directory */
#define AM_ARC 0x20 /* Archive */
#ifdef __cplusplus
}
#endif
#endif /* _FATFS */

View File

@ -1,268 +1,271 @@
/*---------------------------------------------------------------------------/ /*---------------------------------------------------------------------------/
/ FatFs - FAT file system module configuration file / FatFs - FAT file system module configuration file R0.11 (C)ChaN, 2015
/---------------------------------------------------------------------------*/ /---------------------------------------------------------------------------*/
#define _FFCONF 68300 /* Revision ID */ #define _FFCONF 32020 /* Revision ID */
/*---------------------------------------------------------------------------/ /*---------------------------------------------------------------------------/
/ Function Configurations / Functions and Buffer Configurations
/---------------------------------------------------------------------------*/ /---------------------------------------------------------------------------*/
#define _FS_READONLY 0 #define _FS_TINY 0
/* This option switches read-only configuration. (0:Read/Write or 1:Read-only) /* This option switches tiny buffer configuration. (0:Normal or 1:Tiny)
/ Read-only configuration removes writing API functions, f_write(), f_sync(), / At the tiny configuration, size of the file object (FIL) is reduced _MAX_SS
/ f_unlink(), f_mkdir(), f_chmod(), f_rename(), f_truncate(), f_getfree() / bytes. Instead of private sector buffer eliminated from the file object,
/ and optional writing functions as well. */ / common sector buffer in the file system object (FATFS) is used for the file
/ data transfer. */
#define _FS_MINIMIZE 0
/* This option defines minimization level to remove some basic API functions. #define _FS_READONLY 1
/ /* This option switches read-only configuration. (0:Read/Write or 1:Read-only)
/ 0: All basic functions are enabled. / Read-only configuration removes writing API functions, f_write(), f_sync(),
/ 1: f_stat(), f_getfree(), f_unlink(), f_mkdir(), f_truncate() and f_rename() / f_unlink(), f_mkdir(), f_chmod(), f_rename(), f_truncate(), f_getfree()
/ are removed. / and optional writing functions as well. */
/ 2: f_opendir(), f_readdir() and f_closedir() are removed in addition to 1.
/ 3: f_lseek() function is removed in addition to 2. */
#define _FS_MINIMIZE 3
/* This option defines minimization level to remove some basic API functions.
#define _USE_STRFUNC 0 /
/* This option switches string functions, f_gets(), f_putc(), f_puts() and / 0: All basic functions are enabled.
/ f_printf(). / 1: f_stat(), f_getfree(), f_unlink(), f_mkdir(), f_chmod(), f_utime(),
/ / f_truncate() and f_rename() function are removed.
/ 0: Disable string functions. / 2: f_opendir(), f_readdir() and f_closedir() are removed in addition to 1.
/ 1: Enable without LF-CRLF conversion. / 3: f_lseek() function is removed in addition to 2. */
/ 2: Enable with LF-CRLF conversion. */
#define _USE_STRFUNC 0
#define _USE_FIND 0 /* This option switches string functions, f_gets(), f_putc(), f_puts() and
/* This option switches filtered directory read functions, f_findfirst() and / f_printf().
/ f_findnext(). (0:Disable, 1:Enable 2:Enable with matching altname[] too) */ /
/ 0: Disable string functions.
/ 1: Enable without LF-CRLF conversion.
#define _USE_MKFS 0 / 2: Enable with LF-CRLF conversion. */
/* This option switches f_mkfs() function. (0:Disable or 1:Enable) */
#define _USE_FIND 0
#define _USE_FASTSEEK 0 /* This option switches filtered directory read feature and related functions,
/* This option switches fast seek function. (0:Disable or 1:Enable) */ / f_findfirst() and f_findnext(). (0:Disable or 1:Enable) */
#define _USE_EXPAND 0 #define _USE_MKFS 0
/* This option switches f_expand function. (0:Disable or 1:Enable) */ /* This option switches f_mkfs() function. (0:Disable or 1:Enable) */
#define _USE_CHMOD 0 #define _USE_FASTSEEK 0
/* This option switches attribute manipulation functions, f_chmod() and f_utime(). /* This option switches fast seek feature. (0:Disable or 1:Enable) */
/ (0:Disable or 1:Enable) Also _FS_READONLY needs to be 0 to enable this option. */
#define _USE_LABEL 0
#define _USE_LABEL 0 /* This option switches volume label functions, f_getlabel() and f_setlabel().
/* This option switches volume label functions, f_getlabel() and f_setlabel(). / (0:Disable or 1:Enable) */
/ (0:Disable or 1:Enable) */
#define _USE_FORWARD 0
#define _USE_FORWARD 0 /* This option switches f_forward() function. (0:Disable or 1:Enable)
/* This option switches f_forward() function. (0:Disable or 1:Enable) */ / To enable it, also _FS_TINY need to be set to 1. */
/*---------------------------------------------------------------------------/ /*---------------------------------------------------------------------------/
/ Locale and Namespace Configurations / Locale and Namespace Configurations
/---------------------------------------------------------------------------*/ /---------------------------------------------------------------------------*/
#define _CODE_PAGE 437 #define _CODE_PAGE 437
/* This option specifies the OEM code page to be used on the target system. /* This option specifies the OEM code page to be used on the target system.
/ Incorrect setting of the code page can cause a file open failure. / Incorrect setting of the code page can cause a file open failure.
/ /
/ 1 - ASCII (No support of extended character. Non-LFN cfg. only) / 1 - ASCII (No extended character. Non-LFN cfg. only)
/ 437 - U.S. / 437 - U.S.
/ 720 - Arabic / 720 - Arabic
/ 737 - Greek / 737 - Greek
/ 771 - KBL / 775 - Baltic
/ 775 - Baltic / 850 - Multilingual Latin 1
/ 850 - Latin 1 / 852 - Latin 2
/ 852 - Latin 2 / 855 - Cyrillic
/ 855 - Cyrillic / 857 - Turkish
/ 857 - Turkish / 858 - Multilingual Latin 1 + Euro
/ 860 - Portuguese / 862 - Hebrew
/ 861 - Icelandic / 866 - Russian
/ 862 - Hebrew / 874 - Thai
/ 863 - Canadian French / 932 - Japanese Shift_JIS (DBCS)
/ 864 - Arabic / 936 - Simplified Chinese GBK (DBCS)
/ 865 - Nordic / 949 - Korean (DBCS)
/ 866 - Russian / 950 - Traditional Chinese Big5 (DBCS)
/ 869 - Greek 2 */
/ 932 - Japanese (DBCS)
/ 936 - Simplified Chinese (DBCS)
/ 949 - Korean (DBCS) #define _USE_LFN 2
/ 950 - Traditional Chinese (DBCS) #define _MAX_LFN 255
*/ /* The _USE_LFN option switches the LFN feature.
/
/ 0: Disable LFN feature. _MAX_LFN has no effect.
#define _USE_LFN 2 / 1: Enable LFN with static working buffer on the BSS. Always NOT thread-safe.
#define _MAX_LFN 255 / 2: Enable LFN with dynamic working buffer on the STACK.
/* The _USE_LFN switches the support of long file name (LFN). / 3: Enable LFN with dynamic working buffer on the HEAP.
/ /
/ 0: Disable support of LFN. _MAX_LFN has no effect. / When enable the LFN feature, Unicode handling functions (option/unicode.c) must
/ 1: Enable LFN with static working buffer on the BSS. Always NOT thread-safe. / be added to the project. The LFN working buffer occupies (_MAX_LFN + 1) * 2 bytes.
/ 2: Enable LFN with dynamic working buffer on the STACK. / When use stack for the working buffer, take care on stack overflow. When use heap
/ 3: Enable LFN with dynamic working buffer on the HEAP. / memory for the working buffer, memory management functions, ff_memalloc() and
/ / ff_memfree(), must be added to the project. */
/ To enable the LFN, Unicode handling functions (option/unicode.c) must be added
/ to the project. The working buffer occupies (_MAX_LFN + 1) * 2 bytes and
/ additional 608 bytes at exFAT enabled. _MAX_LFN can be in range from 12 to 255. #define _LFN_UNICODE 0
/ It should be set 255 to support full featured LFN operations. /* This option switches character encoding on the API. (0:ANSI/OEM or 1:Unicode)
/ When use stack for the working buffer, take care on stack overflow. When use heap / To use Unicode string for the path name, enable LFN feature and set _LFN_UNICODE
/ memory for the working buffer, memory management functions, ff_memalloc() and / to 1. This option also affects behavior of string I/O functions. */
/ ff_memfree(), must be added to the project. */
#define _STRF_ENCODE 0
#define _LFN_UNICODE 0 /* When _LFN_UNICODE is 1, this option selects the character encoding on the file to
/* This option switches character encoding on the API. (0:ANSI/OEM or 1:UTF-16) / be read/written via string I/O functions, f_gets(), f_putc(), f_puts and f_printf().
/ To use Unicode string for the path name, enable LFN and set _LFN_UNICODE = 1. /
/ This option also affects behavior of string I/O functions. */ / 0: ANSI/OEM
/ 1: UTF-16LE
/ 2: UTF-16BE
#define _STRF_ENCODE 3 / 3: UTF-8
/* When _LFN_UNICODE == 1, this option selects the character encoding ON THE FILE to /
/ be read/written via string I/O functions, f_gets(), f_putc(), f_puts and f_printf(). / When _LFN_UNICODE is 0, this option has no effect. */
/
/ 0: ANSI/OEM
/ 1: UTF-16LE #define _FS_RPATH 0
/ 2: UTF-16BE /* This option configures relative path feature.
/ 3: UTF-8 /
/ / 0: Disable relative path feature and remove related functions.
/ This option has no effect when _LFN_UNICODE == 0. */ / 1: Enable relative path feature. f_chdir() and f_chdrive() are available.
/ 2: f_getcwd() function is available in addition to 1.
/
#define _FS_RPATH 0 / Note that directory items read via f_readdir() are affected by this option. */
/* This option configures support of relative path.
/
/ 0: Disable relative path and remove related functions. /*---------------------------------------------------------------------------/
/ 1: Enable relative path. f_chdir() and f_chdrive() are available. / Drive/Volume Configurations
/ 2: f_getcwd() function is available in addition to 1. /---------------------------------------------------------------------------*/
*/
#define _VOLUMES 1
/* Number of volumes (logical drives) to be used. */
/*---------------------------------------------------------------------------/
/ Drive/Volume Configurations
/---------------------------------------------------------------------------*/ #define _STR_VOLUME_ID 0
#define _VOLUME_STRS "RAM","NAND","CF","SD1","SD2","USB1","USB2","USB3"
#define _VOLUMES 2 /* _STR_VOLUME_ID option switches string volume ID feature.
/* Number of volumes (logical drives) to be used. (1-10) */ / When _STR_VOLUME_ID is set to 1, also pre-defined strings can be used as drive
/ number in the path name. _VOLUME_STRS defines the drive ID strings for each
/ logical drives. Number of items must be equal to _VOLUMES. Valid characters for
#define _STR_VOLUME_ID 0 / the drive ID strings are: A-Z and 0-9. */
#define _VOLUME_STRS "RAM","NAND","CF","SD","SD2","USB","USB2","USB3"
/* _STR_VOLUME_ID switches string support of volume ID.
/ When _STR_VOLUME_ID is set to 1, also pre-defined strings can be used as drive #define _MULTI_PARTITION 0
/ number in the path name. _VOLUME_STRS defines the drive ID strings for each /* This option switches multi-partition feature. By default (0), each logical drive
/ logical drives. Number of items must be equal to _VOLUMES. Valid characters for / number is bound to the same physical drive number and only an FAT volume found on
/ the drive ID strings are: A-Z and 0-9. */ / the physical drive will be mounted. When multi-partition feature is enabled (1),
/ each logical drive number is bound to arbitrary physical drive and partition
/ listed in the VolToPart[]. Also f_fdisk() funciton will be available. */
#define _MULTI_PARTITION 0
/* This option switches support of multi-partition on a physical drive.
/ By default (0), each logical drive number is bound to the same physical drive #define _MIN_SS 512
/ number and only an FAT volume found on the physical drive will be mounted. #define _MAX_SS 512
/ When multi-partition is enabled (1), each logical drive number can be bound to /* These options configure the range of sector size to be supported. (512, 1024,
/ arbitrary physical drive and partition listed in the VolToPart[]. Also f_fdisk() / 2048 or 4096) Always set both 512 for most systems, all type of memory cards and
/ funciton will be available. */ / harddisk. But a larger value may be required for on-board flash memory and some
/ type of optical media. When _MAX_SS is larger than _MIN_SS, FatFs is configured
/ to variable sector size and GET_SECTOR_SIZE command must be implemented to the
#define _MIN_SS 512 / disk_ioctl() function. */
#define _MAX_SS 512
/* These options configure the range of sector size to be supported. (512, 1024,
/ 2048 or 4096) Always set both 512 for most systems, generic memory card and #define _USE_TRIM 0
/ harddisk. But a larger value may be required for on-board flash memory and some /* This option switches ATA-TRIM feature. (0:Disable or 1:Enable)
/ type of optical media. When _MAX_SS is larger than _MIN_SS, FatFs is configured / To enable Trim feature, also CTRL_TRIM command should be implemented to the
/ to variable sector size and GET_SECTOR_SIZE command needs to be implemented to / disk_ioctl() function. */
/ the disk_ioctl() function. */
#define _FS_NOFSINFO 0
#define _USE_TRIM 0 /* If you need to know correct free space on the FAT32 volume, set bit 0 of this
/* This option switches support of ATA-TRIM. (0:Disable or 1:Enable) / option, and f_getfree() function at first time after volume mount will force
/ To enable Trim function, also CTRL_TRIM command should be implemented to the / a full FAT scan. Bit 1 controls the use of last allocated cluster number.
/ disk_ioctl() function. */ /
/ bit0=0: Use free cluster count in the FSINFO if available.
/ bit0=1: Do not trust free cluster count in the FSINFO.
#define _FS_NOFSINFO 0 / bit1=0: Use last allocated cluster number in the FSINFO if available.
/* If you need to know correct free space on the FAT32 volume, set bit 0 of this / bit1=1: Do not trust last allocated cluster number in the FSINFO.
/ option, and f_getfree() function at first time after volume mount will force */
/ a full FAT scan. Bit 1 controls the use of last allocated cluster number.
/
/ bit0=0: Use free cluster count in the FSINFO if available.
/ bit0=1: Do not trust free cluster count in the FSINFO. /*---------------------------------------------------------------------------/
/ bit1=0: Use last allocated cluster number in the FSINFO if available. / System Configurations
/ bit1=1: Do not trust last allocated cluster number in the FSINFO. /---------------------------------------------------------------------------*/
*/
#define _FS_NORTC 1
#define _NORTC_MON 2
#define _NORTC_MDAY 1
/*---------------------------------------------------------------------------/ #define _NORTC_YEAR 2015
/ System Configurations /* The _FS_NORTC option switches timestamp feature. If the system does not have
/---------------------------------------------------------------------------*/ / an RTC function or valid timestamp is not needed, set _FS_NORTC to 1 to disable
/ the timestamp feature. All objects modified by FatFs will have a fixed timestamp
#define _FS_TINY 0 / defined by _NORTC_MON, _NORTC_MDAY and _NORTC_YEAR.
/* This option switches tiny buffer configuration. (0:Normal or 1:Tiny) / When timestamp feature is enabled (_FS_NORTC == 0), get_fattime() function need
/ At the tiny configuration, size of file object (FIL) is shrinked _MAX_SS bytes. / to be added to the project to read current time form RTC. _NORTC_MON,
/ Instead of private sector buffer eliminated from the file object, common sector / _NORTC_MDAY and _NORTC_YEAR have no effect.
/ buffer in the file system object (FATFS) is used for the file data transfer. */ / These options have no effect at read-only configuration (_FS_READONLY == 1). */
#define _FS_EXFAT 0 #define _FS_LOCK 0
/* This option switches support of exFAT file system. (0:Disable or 1:Enable) /* The _FS_LOCK option switches file lock feature to control duplicated file open
/ When enable exFAT, also LFN needs to be enabled. (_USE_LFN >= 1) / and illegal operation to open objects. This option must be 0 when _FS_READONLY
/ Note that enabling exFAT discards ANSI C (C89) compatibility. */ / is 1.
/
/ 0: Disable file lock feature. To avoid volume corruption, application program
#define _FS_NORTC 1 / should avoid illegal open, remove and rename to the open objects.
#define _NORTC_MON 1 / >0: Enable file lock feature. The value defines how many files/sub-directories
#define _NORTC_MDAY 1 / can be opened simultaneously under file lock control. Note that the file
#define _NORTC_YEAR 2017 / lock feature is independent of re-entrancy. */
/* The option _FS_NORTC switches timestamp functiton. If the system does not have
/ any RTC function or valid timestamp is not needed, set _FS_NORTC = 1 to disable
/ the timestamp function. All objects modified by FatFs will have a fixed timestamp #define _FS_REENTRANT 0
/ defined by _NORTC_MON, _NORTC_MDAY and _NORTC_YEAR in local time. #define _FS_TIMEOUT 1000
/ To enable timestamp function (_FS_NORTC = 0), get_fattime() function need to be #define _SYNC_t HANDLE
/ added to the project to get current time form real-time clock. _NORTC_MON, /* The _FS_REENTRANT option switches the re-entrancy (thread safe) of the FatFs
/ _NORTC_MDAY and _NORTC_YEAR have no effect. / module itself. Note that regardless of this option, file access to different
/ These options have no effect at read-only configuration (_FS_READONLY = 1). */ / volume is always re-entrant and volume control functions, f_mount(), f_mkfs()
/ and f_fdisk() function, are always not re-entrant. Only file/directory access
/ to the same volume is under control of this feature.
#define _FS_LOCK 0 /
/* The option _FS_LOCK switches file lock function to control duplicated file open / 0: Disable re-entrancy. _FS_TIMEOUT and _SYNC_t have no effect.
/ and illegal operation to open objects. This option must be 0 when _FS_READONLY / 1: Enable re-entrancy. Also user provided synchronization handlers,
/ is 1. / ff_req_grant(), ff_rel_grant(), ff_del_syncobj() and ff_cre_syncobj()
/ / function, must be added to the project. Samples are available in
/ 0: Disable file lock function. To avoid volume corruption, application program / option/syscall.c.
/ should avoid illegal open, remove and rename to the open objects. /
/ >0: Enable file lock function. The value defines how many files/sub-directories / The _FS_TIMEOUT defines timeout period in unit of time tick.
/ can be opened simultaneously under file lock control. Note that the file / The _SYNC_t defines O/S dependent sync object type. e.g. HANDLE, ID, OS_EVENT*,
/ lock control is independent of re-entrancy. */ / SemaphoreHandle_t and etc.. A header file for O/S definitions needs to be
/ included somewhere in the scope of ff.c. */
#define _FS_REENTRANT 0
#define _FS_TIMEOUT 1000 #define _WORD_ACCESS 0
#define _SYNC_t HANDLE /* The _WORD_ACCESS option is an only platform dependent option. It defines
/* The option _FS_REENTRANT switches the re-entrancy (thread safe) of the FatFs / which access method is used to the word data on the FAT volume.
/ module itself. Note that regardless of this option, file access to different /
/ volume is always re-entrant and volume control functions, f_mount(), f_mkfs() / 0: Byte-by-byte access. Always compatible with all platforms.
/ and f_fdisk() function, are always not re-entrant. Only file/directory access / 1: Word access. Do not choose this unless under both the following conditions.
/ to the same volume is under control of this function. /
/ / * Address misaligned memory access is always allowed to ALL instructions.
/ 0: Disable re-entrancy. _FS_TIMEOUT and _SYNC_t have no effect. / * Byte order on the memory is little-endian.
/ 1: Enable re-entrancy. Also user provided synchronization handlers, /
/ ff_req_grant(), ff_rel_grant(), ff_del_syncobj() and ff_cre_syncobj() / If it is the case, _WORD_ACCESS can also be set to 1 to reduce code size.
/ function, must be added to the project. Samples are available in / Following table shows allowable settings of some processor types.
/ option/syscall.c. /
/ / ARM7TDMI 0 *2 ColdFire 0 *1 V850E 0 *2
/ The _FS_TIMEOUT defines timeout period in unit of time tick. / Cortex-M3 0 *3 Z80 0/1 V850ES 0/1
/ The _SYNC_t defines O/S dependent sync object type. e.g. HANDLE, ID, OS_EVENT*, / Cortex-M0 0 *2 x86 0/1 TLCS-870 0/1
/ SemaphoreHandle_t and etc. A header file for O/S definitions needs to be / AVR 0/1 RX600(LE) 0/1 TLCS-900 0/1
/ included somewhere in the scope of ff.h. */ / AVR32 0 *1 RL78 0 *2 R32C 0 *2
/ PIC18 0/1 SH-2 0 *1 M16C 0/1
/* #include <windows.h> // O/S definitions */ / PIC24 0 *2 H8S 0 *1 MSP430 0 *2
/ PIC32 0 *1 H8/300H 0 *1 8051 0/1
/
/ *1:Big-endian.
/*--- End of configuration options ---*/ / *2:Unaligned memory access is not supported.
/ *3:Some compilers generate LDM/STM for mem_cpy function.
*/

View File

@ -1,288 +1,180 @@
---------------------------------------------------------------------------- ----------------------------------------------------------------------------
Revision history of FatFs module Revision history of FatFs module
---------------------------------------------------------------------------- ----------------------------------------------------------------------------
R0.00 (February 26, 2006) R0.00 (February 26, 2006)
Prototype.
Prototype.
R0.01 (April 29, 2006)
First stable version.
R0.01 (April 29, 2006)
The first release. R0.02 (June 01, 2006)
Added FAT12 support.
Removed unbuffered mode.
Fixed a problem on small (<32M) partition.
R0.02 (June 01, 2006)
Added FAT12 support. R0.02a (June 10, 2006)
Removed unbuffered mode. Added a configuration option (_FS_MINIMUM).
Fixed a problem on small (<32M) partition.
R0.03 (September 22, 2006)
Added f_rename().
R0.02a (June 10, 2006) Changed option _FS_MINIMUM to _FS_MINIMIZE.
Added a configuration option (_FS_MINIMUM).
R0.03a (December 11, 2006)
Improved cluster scan algorithm to write files fast.
Fixed f_mkdir() creates incorrect directory on FAT32.
R0.03 (September 22, 2006)
Added f_rename(). R0.04 (February 04, 2007)
Changed option _FS_MINIMUM to _FS_MINIMIZE. Added f_mkfs().
Supported multiple drive system.
Changed some interfaces for multiple drive system.
Changed f_mountdrv() to f_mount().
R0.03a (December 11, 2006)
Improved cluster scan algorithm to write files fast. R0.04a (April 01, 2007)
Fixed f_mkdir() creates incorrect directory on FAT32. Supported multiple partitions on a physical drive.
Added a capability of extending file size to f_lseek().
Added minimization level 3.
Fixed an endian sensitive code in f_mkfs().
R0.04 (February 04, 2007)
Added f_mkfs(). R0.04b (May 05, 2007)
Supported multiple drive system. Added a configuration option _USE_NTFLAG.
Changed some interfaces for multiple drive system. Added FSINFO support.
Changed f_mountdrv() to f_mount(). Fixed DBCS name can result FR_INVALID_NAME.
Fixed short seek (<= csize) collapses the file object.
R0.04a (April 01, 2007) R0.05 (August 25, 2007)
Changed arguments of f_read(), f_write() and f_mkfs().
Supported multiple partitions on a physical drive. Fixed f_mkfs() on FAT32 creates incorrect FSINFO.
Added a capability of extending file size to f_lseek(). Fixed f_mkdir() on FAT32 creates incorrect directory.
Added minimization level 3.
Fixed an endian sensitive code in f_mkfs().
R0.05a (February 03, 2008)
Added f_truncate() and f_utime().
Fixed off by one error at FAT sub-type determination.
R0.04b (May 05, 2007) Fixed btr in f_read() can be mistruncated.
Fixed cached sector is not flushed when create and close without write.
Added a configuration option _USE_NTFLAG.
Added FSINFO support.
Fixed DBCS name can result FR_INVALID_NAME. R0.06 (April 01, 2008)
Fixed short seek (<= csize) collapses the file object. Added fputc(), fputs(), fprintf() and fgets().
Improved performance of f_lseek() on moving to the same or following cluster.
R0.05 (August 25, 2007) R0.07 (April 01, 2009)
Merged Tiny-FatFs as a configuration option. (_FS_TINY)
Changed arguments of f_read(), f_write() and f_mkfs(). Added long file name feature. (_USE_LFN)
Fixed f_mkfs() on FAT32 creates incorrect FSINFO. Added multiple code page feature. (_CODE_PAGE)
Fixed f_mkdir() on FAT32 creates incorrect directory. Added re-entrancy for multitask operation. (_FS_REENTRANT)
Added auto cluster size selection to f_mkfs().
Added rewind option to f_readdir().
Changed result code of critical errors.
R0.05a (February 03, 2008) Renamed string functions to avoid name collision.
Added f_truncate() and f_utime().
Fixed off by one error at FAT sub-type determination. R0.07a (April 14, 2009)
Fixed btr in f_read() can be mistruncated. Septemberarated out OS dependent code on reentrant cfg.
Fixed cached sector is not flushed when create and close without write. Added multiple sector size feature.
R0.07c (June 21, 2009)
R0.06 (April 01, 2008) Fixed f_unlink() can return FR_OK on error.
Fixed wrong cache control in f_lseek().
Added fputc(), fputs(), fprintf() and fgets(). Added relative path feature.
Improved performance of f_lseek() on moving to the same or following cluster. Added f_chdir() and f_chdrive().
Added proper case conversion to extended character.
R0.07 (April 01, 2009) R0.07e (November 03, 2009)
Septemberarated out configuration options from ff.h to ffconf.h.
Merged Tiny-FatFs as a configuration option. (_FS_TINY) Fixed f_unlink() fails to remove a sub-directory on _FS_RPATH.
Added long file name feature. (_USE_LFN) Fixed name matching error on the 13 character boundary.
Added multiple code page feature. (_CODE_PAGE) Added a configuration option, _LFN_UNICODE.
Added re-entrancy for multitask operation. (_FS_REENTRANT) Changed f_readdir() to return the SFN with always upper case on non-LFN cfg.
Added auto cluster size selection to f_mkfs().
Added rewind option to f_readdir().
Changed result code of critical errors. R0.08 (May 15, 2010)
Renamed string functions to avoid name collision. Added a memory configuration option. (_USE_LFN = 3)
Added file lock feature. (_FS_SHARE)
Added fast seek feature. (_USE_FASTSEEK)
Changed some types on the API, XCHAR->TCHAR.
R0.07a (April 14, 2009) Changed .fname in the FILINFO structure on Unicode cfg.
String functions support UTF-8 encoding files on Unicode cfg.
Septemberarated out OS dependent code on reentrant cfg.
Added multiple sector size feature.
R0.08a (August 16, 2010)
Added f_getcwd(). (_FS_RPATH = 2)
Added sector erase feature. (_USE_ERASE)
R0.07c (June 21, 2009) Moved file lock semaphore table from fs object to the bss.
Fixed f_mkfs() creates wrong FAT32 volume.
Fixed f_unlink() can return FR_OK on error.
Fixed wrong cache control in f_lseek().
Added relative path feature. R0.08b (January 15, 2011)
Added f_chdir() and f_chdrive(). Fast seek feature is also applied to f_read() and f_write().
Added proper case conversion to extended character. f_lseek() reports required table size on creating CLMP.
Extended format syntax of f_printf().
Ignores duplicated directory separators in given path name.
R0.07e (November 03, 2009)
R0.09 (September 06, 2011)
Septemberarated out configuration options from ff.h to ffconf.h. f_mkfs() supports multiple partition to complete the multiple partition feature.
Fixed f_unlink() fails to remove a sub-directory on _FS_RPATH. Added f_fdisk().
Fixed name matching error on the 13 character boundary.
Added a configuration option, _LFN_UNICODE.
Changed f_readdir() to return the SFN with always upper case on non-LFN cfg. R0.09a (August 27, 2012)
Changed f_open() and f_opendir() reject null object pointer to avoid crash.
Changed option name _FS_SHARE to _FS_LOCK.
Fixed assertion failure due to OS/2 EA on FAT12/16 volume.
R0.08 (May 15, 2010)
Added a memory configuration option. (_USE_LFN = 3) R0.09b (January 24, 2013)
Added file lock feature. (_FS_SHARE) Added f_setlabel() and f_getlabel().
Added fast seek feature. (_USE_FASTSEEK)
Changed some types on the API, XCHAR->TCHAR.
Changed .fname in the FILINFO structure on Unicode cfg. R0.10 (October 02, 2013)
String functions support UTF-8 encoding files on Unicode cfg. Added selection of character encoding on the file. (_STRF_ENCODE)
Added f_closedir().
Added forced full FAT scan for f_getfree(). (_FS_NOFSINFO)
Added forced mount feature with changes of f_mount().
R0.08a (August 16, 2010) Improved behavior of volume auto detection.
Improved write throughput of f_puts() and f_printf().
Added f_getcwd(). (_FS_RPATH = 2) Changed argument of f_chdrive(), f_mkfs(), disk_read() and disk_write().
Added sector erase feature. (_USE_ERASE) Fixed f_write() can be truncated when the file size is close to 4GB.
Moved file lock semaphore table from fs object to the bss. Fixed f_open(), f_mkdir() and f_setlabel() can return incorrect error code.
Fixed f_mkfs() creates wrong FAT32 volume.
R0.10a (January 15, 2014)
Added arbitrary strings as drive number in the path name. (_STR_VOLUME_ID)
R0.08b (January 15, 2011) Added a configuration option of minimum sector size. (_MIN_SS)
2nd argument of f_rename() can have a drive number and it will be ignored.
Fast seek feature is also applied to f_read() and f_write(). Fixed f_mount() with forced mount fails when drive number is >= 1. (appeared at R0.10)
f_lseek() reports required table size on creating CLMP. Fixed f_close() invalidates the file object without volume lock.
Extended format syntax of f_printf(). Fixed f_closedir() returns but the volume lock is left acquired. (appeared at R0.10)
Ignores duplicated directory separators in given path name. Fixed creation of an entry with LFN fails on too many SFN collisions. (appeared at R0.07)
R0.10b (May 19, 2014)
R0.09 (September 06, 2011) Fixed a hard error in the disk I/O layer can collapse the directory entry.
Fixed LFN entry is not deleted on delete/rename an object with lossy converted SFN. (appeared at R0.07)
f_mkfs() supports multiple partition to complete the multiple partition feature.
Added f_fdisk().
R0.10c (November 09, 2014)
Added a configuration option for the platforms without RTC. (_FS_NORTC)
Changed option name _USE_ERASE to _USE_TRIM.
R0.09a (August 27, 2012) Fixed volume label created by Mac OS X cannot be retrieved with f_getlabel(). (appeared at R0.09b)
Fixed a potential problem of FAT access that can appear on disk error.
Changed f_open() and f_opendir() reject null object pointer to avoid crash. Fixed null pointer dereference on attempting to delete the root direcotry. (appeared at R0.08)
Changed option name _FS_SHARE to _FS_LOCK.
Fixed assertion failure due to OS/2 EA on FAT12/16 volume.
R0.11 (February 09, 2015)
Added f_findfirst(), f_findnext() and f_findclose(). (_USE_FIND)
Fixed f_unlink() does not remove cluster chain of the file. (appeared at R0.10c)
R0.09b (January 24, 2013) Fixed _FS_NORTC option does not work properly. (appeared at R0.10c)
Added f_setlabel() and f_getlabel().
R0.10 (October 02, 2013)
Added selection of character encoding on the file. (_STRF_ENCODE)
Added f_closedir().
Added forced full FAT scan for f_getfree(). (_FS_NOFSINFO)
Added forced mount feature with changes of f_mount().
Improved behavior of volume auto detection.
Improved write throughput of f_puts() and f_printf().
Changed argument of f_chdrive(), f_mkfs(), disk_read() and disk_write().
Fixed f_write() can be truncated when the file size is close to 4GB.
Fixed f_open(), f_mkdir() and f_setlabel() can return incorrect value on error.
R0.10a (January 15, 2014)
Added arbitrary strings as drive number in the path name. (_STR_VOLUME_ID)
Added a configuration option of minimum sector size. (_MIN_SS)
2nd argument of f_rename() can have a drive number and it will be ignored.
Fixed f_mount() with forced mount fails when drive number is >= 1. (appeared at R0.10)
Fixed f_close() invalidates the file object without volume lock.
Fixed f_closedir() returns but the volume lock is left acquired. (appeared at R0.10)
Fixed creation of an entry with LFN fails on too many SFN collisions. (appeared at R0.07)
R0.10b (May 19, 2014)
Fixed a hard error in the disk I/O layer can collapse the directory entry.
Fixed LFN entry is not deleted when delete/rename an object with lossy converted SFN. (appeared at R0.07)
R0.10c (November 09, 2014)
Added a configuration option for the platforms without RTC. (_FS_NORTC)
Changed option name _USE_ERASE to _USE_TRIM.
Fixed volume label created by Mac OS X cannot be retrieved with f_getlabel(). (appeared at R0.09b)
Fixed a potential problem of FAT access that can appear on disk error.
Fixed null pointer dereference on attempting to delete the root direcotry. (appeared at R0.08)
R0.11 (February 09, 2015)
Added f_findfirst(), f_findnext() and f_findclose(). (_USE_FIND)
Fixed f_unlink() does not remove cluster chain of the file. (appeared at R0.10c)
Fixed _FS_NORTC option does not work properly. (appeared at R0.10c)
R0.11a (September 05, 2015)
Fixed wrong media change can lead a deadlock at thread-safe configuration.
Added code page 771, 860, 861, 863, 864, 865 and 869. (_CODE_PAGE)
Removed some code pages actually not exist on the standard systems. (_CODE_PAGE)
Fixed errors in the case conversion teble of code page 437 and 850 (ff.c).
Fixed errors in the case conversion teble of Unicode (cc*.c).
R0.12 (April 12, 2016)
Added support for exFAT file system. (_FS_EXFAT)
Added f_expand(). (_USE_EXPAND)
Changed some members in FINFO structure and behavior of f_readdir().
Added an option _USE_CHMOD.
Removed an option _WORD_ACCESS.
Fixed errors in the case conversion table of Unicode (cc*.c).
R0.12a (July 10, 2016)
Added support for creating exFAT volume with some changes of f_mkfs().
Added a file open method FA_OPEN_APPEND. An f_lseek() following f_open() is no longer needed.
f_forward() is available regardless of _FS_TINY.
Fixed f_mkfs() creates wrong volume. (appeared at R0.12)
Fixed wrong memory read in create_name(). (appeared at R0.12)
Fixed compilation fails at some configurations, _USE_FASTSEEK and _USE_FORWARD.
R0.12b (September 04, 2016)
Made f_rename() be able to rename objects with the same name but case.
Fixed an error in the case conversion teble of code page 866. (ff.c)
Fixed writing data is truncated at the file offset 4GiB on the exFAT volume. (appeared at R0.12)
Fixed creating a file in the root directory of exFAT volume can fail. (appeared at R0.12)
Fixed f_mkfs() creating exFAT volume with too small cluster size can collapse unallocated memory. (appeared at R0.12)
Fixed wrong object name can be returned when read directory at Unicode cfg. (appeared at R0.12)
Fixed large file allocation/removing on the exFAT volume collapses allocation bitmap. (appeared at R0.12)
Fixed some internal errors in f_expand() and f_lseek(). (appeared at R0.12)
R0.12c (March 04, 2017)
Improved write throughput at the fragmented file on the exFAT volume.
Made memory usage for exFAT be able to be reduced as decreasing _MAX_LFN.
Fixed successive f_getfree() can return wrong count on the FAT12/16 volume. (appeared at R0.12)
Fixed configuration option _VOLUMES cannot be set 10. (appeared at R0.10c)

View File

@ -1,38 +1,33 @@
/*-------------------------------------------*/ /*-------------------------------------------*/
/* Integer type definitions for FatFs module */ /* Integer type definitions for FatFs module */
/*-------------------------------------------*/ /*-------------------------------------------*/
#ifndef _FF_INTEGER #ifndef _FF_INTEGER
#define _FF_INTEGER #define _FF_INTEGER
#ifdef _WIN32 /* FatFs development platform */ #ifdef _WIN32 /* FatFs development platform */
#include <windows.h> #include <windows.h>
#include <tchar.h> #include <tchar.h>
typedef unsigned __int64 QWORD;
#else /* Embedded platform */
#else /* Embedded platform */ /* This type MUST be 8 bit */
typedef unsigned char BYTE;
/* These types MUST be 16-bit or 32-bit */
typedef int INT; /* These types MUST be 16 bit */
typedef unsigned int UINT; typedef short SHORT;
typedef unsigned short WORD;
/* This type MUST be 8-bit */ typedef unsigned short WCHAR;
typedef unsigned char BYTE;
/* These types MUST be 16 bit or 32 bit */
/* These types MUST be 16-bit */ typedef int INT;
typedef short SHORT; typedef unsigned int UINT;
typedef unsigned short WORD;
typedef unsigned short WCHAR; /* These types MUST be 32 bit */
typedef long LONG;
/* These types MUST be 32-bit */ typedef unsigned long DWORD;
typedef long LONG;
typedef unsigned long DWORD; #endif
/* This type MUST be 64-bit (Remove this for ANSI C (C89) compatibility) */ #endif
typedef unsigned long long QWORD;
#endif
#endif

View File

@ -1,388 +0,0 @@
/*------------------------------------------------------------------------*/
/* Unicode - Local code bidirectional converter (C)ChaN, 2015 */
/* (SBCS code pages) */
/*------------------------------------------------------------------------*/
/* 437 U.S.
/ 720 Arabic
/ 737 Greek
/ 771 KBL
/ 775 Baltic
/ 850 Latin 1
/ 852 Latin 2
/ 855 Cyrillic
/ 857 Turkish
/ 860 Portuguese
/ 861 Icelandic
/ 862 Hebrew
/ 863 Canadian French
/ 864 Arabic
/ 865 Nordic
/ 866 Russian
/ 869 Greek 2
*/
#include "../ff.h"
#if _CODE_PAGE == 437
#define _TBLDEF 1
static
const WCHAR Tbl[] = { /* CP437(0x80-0xFF) to Unicode conversion table */
0x00C7, 0x00FC, 0x00E9, 0x00E2, 0x00E4, 0x00E0, 0x00E5, 0x00E7, 0x00EA, 0x00EB, 0x00E8, 0x00EF, 0x00EE, 0x00EC, 0x00C4, 0x00C5,
0x00C9, 0x00E6, 0x00C6, 0x00F4, 0x00F6, 0x00F2, 0x00FB, 0x00F9, 0x00FF, 0x00D6, 0x00DC, 0x00A2, 0x00A3, 0x00A5, 0x20A7, 0x0192,
0x00E1, 0x00ED, 0x00F3, 0x00FA, 0x00F1, 0x00D1, 0x00AA, 0x00BA, 0x00BF, 0x2310, 0x00AC, 0x00BD, 0x00BC, 0x00A1, 0x00AB, 0x00BB,
0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x2561, 0x2562, 0x2556, 0x2555, 0x2563, 0x2551, 0x2557, 0x255D, 0x255C, 0x255B, 0x2510,
0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x255E, 0x255F, 0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x2567,
0x2568, 0x2564, 0x2565, 0x2559, 0x2558, 0x2552, 0x2553, 0x256B, 0x256A, 0x2518, 0x250C, 0x2588, 0x2584, 0x258C, 0x2590, 0x2580,
0x03B1, 0x00DF, 0x0393, 0x03C0, 0x03A3, 0x03C3, 0x00B5, 0x03C4, 0x03A6, 0x0398, 0x03A9, 0x03B4, 0x221E, 0x03C6, 0x03B5, 0x2229,
0x2261, 0x00B1, 0x2265, 0x2264, 0x2320, 0x2321, 0x00F7, 0x2248, 0x00B0, 0x2219, 0x00B7, 0x221A, 0x207F, 0x00B2, 0x25A0, 0x00A0
};
#elif _CODE_PAGE == 720
#define _TBLDEF 1
static
const WCHAR Tbl[] = { /* CP720(0x80-0xFF) to Unicode conversion table */
0x0000, 0x0000, 0x00E9, 0x00E2, 0x0000, 0x00E0, 0x0000, 0x00E7, 0x00EA, 0x00EB, 0x00E8, 0x00EF, 0x00EE, 0x0000, 0x0000, 0x0000,
0x0000, 0x0651, 0x0652, 0x00F4, 0x00A4, 0x0640, 0x00FB, 0x00F9, 0x0621, 0x0622, 0x0623, 0x0624, 0x00A3, 0x0625, 0x0626, 0x0627,
0x0628, 0x0629, 0x062A, 0x062B, 0x062C, 0x062D, 0x062E, 0x062F, 0x0630, 0x0631, 0x0632, 0x0633, 0x0634, 0x0635, 0x00AB, 0x00BB,
0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x2561, 0x2562, 0x2556, 0x2555, 0x2563, 0x2551, 0x2557, 0x255D, 0x255C, 0x255B, 0x2510,
0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x255E, 0x255F, 0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x2567,
0x2568, 0x2564, 0x2565, 0x2559, 0x2558, 0x2552, 0x2553, 0x256B, 0x256A, 0x2518, 0x250C, 0x2588, 0x2584, 0x258C, 0x2590, 0x2580,
0x0636, 0x0637, 0x0638, 0x0639, 0x063A, 0x0641, 0x00B5, 0x0642, 0x0643, 0x0644, 0x0645, 0x0646, 0x0647, 0x0648, 0x0649, 0x064A,
0x2261, 0x064B, 0x064C, 0x064D, 0x064E, 0x064F, 0x0650, 0x2248, 0x00B0, 0x2219, 0x00B7, 0x221A, 0x207F, 0x00B2, 0x25A0, 0x00A0
};
#elif _CODE_PAGE == 737
#define _TBLDEF 1
static
const WCHAR Tbl[] = { /* CP737(0x80-0xFF) to Unicode conversion table */
0x0391, 0x0392, 0x0393, 0x0394, 0x0395, 0x0396, 0x0397, 0x0398, 0x0399, 0x039A, 0x039B, 0x039C, 0x039D, 0x039E, 0x039F, 0x03A0,
0x03A1, 0x03A3, 0x03A4, 0x03A5, 0x03A6, 0x03A7, 0x03A8, 0x03A9, 0x03B1, 0x03B2, 0x03B3, 0x03B4, 0x03B5, 0x03B6, 0x03B7, 0x03B8,
0x03B9, 0x03BA, 0x03BB, 0x03BC, 0x03BD, 0x03BE, 0x03BF, 0x03C0, 0x03C1, 0x03C3, 0x03C2, 0x03C4, 0x03C5, 0x03C6, 0x03C7, 0x03C8,
0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x2561, 0x2562, 0x2556, 0x2555, 0x2563, 0x2551, 0x2557, 0x255D, 0x255C, 0x255B, 0x2510,
0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x255E, 0x255F, 0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x2567,
0x2568, 0x2564, 0x2565, 0x2559, 0x2558, 0x2552, 0x2553, 0x256B, 0x256A, 0x2518, 0x250C, 0x2588, 0x2584, 0x258C, 0x2590, 0x2580,
0x03C9, 0x03AC, 0x03AD, 0x03AE, 0x03CA, 0x03AF, 0x03CC, 0x03CD, 0x03CB, 0x03CE, 0x0386, 0x0388, 0x0389, 0x038A, 0x038C, 0x038E,
0x038F, 0x00B1, 0x2265, 0x2264, 0x03AA, 0x03AB, 0x00F7, 0x2248, 0x00B0, 0x2219, 0x00B7, 0x221A, 0x207F, 0x00B2, 0x25A0, 0x00A0
};
#elif _CODE_PAGE == 771
#define _TBLDEF 1
static
const WCHAR Tbl[] = { /* CP771(0x80-0xFF) to Unicode conversion table */
0x0410, 0x0411, 0x0412, 0x0413, 0x0414, 0x0415, 0x0416, 0x0417, 0x0418, 0x0419, 0x041A, 0x041B, 0x041C, 0x041D, 0x041E, 0x041F,
0x0420, 0x0421, 0x0422, 0x0423, 0x0424, 0x0425, 0x0426, 0x0427, 0x0428, 0x0429, 0x042A, 0x042B, 0x042C, 0x042D, 0x042E, 0x042F,
0x0430, 0x0431, 0x0432, 0x0433, 0x0434, 0x0435, 0x0436, 0x0437, 0x0438, 0x0439, 0x043A, 0x043B, 0x043C, 0x043D, 0x043E, 0x043F,
0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x2561, 0x2562, 0x2556, 0x2555, 0x2563, 0x2551, 0x2557, 0x255D, 0x255C, 0x2558, 0x2510,
0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x255E, 0x255F, 0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x2567,
0x2568, 0x2564, 0x2565, 0x2559, 0x2558, 0x2552, 0x2553, 0x256B, 0x256A, 0x2518, 0x250C, 0x2588, 0x0104, 0x0105, 0x010C, 0x010D,
0x0440, 0x0441, 0x0442, 0x0443, 0x0444, 0x0445, 0x0446, 0x0447, 0x0448, 0x0449, 0x044A, 0x044B, 0x044C, 0x044D, 0x044E, 0x044F,
0x0118, 0x0119, 0x0116, 0x0117, 0x012E, 0x012F, 0x0160, 0x0161, 0x0172, 0x0173, 0x016A, 0x016B, 0x017D, 0x017E, 0x25A0, 0x00A0
};
#elif _CODE_PAGE == 775
#define _TBLDEF 1
static
const WCHAR Tbl[] = { /* CP775(0x80-0xFF) to Unicode conversion table */
0x0106, 0x00FC, 0x00E9, 0x0101, 0x00E4, 0x0123, 0x00E5, 0x0107, 0x0142, 0x0113, 0x0156, 0x0157, 0x012B, 0x0179, 0x00C4, 0x00C5,
0x00C9, 0x00E6, 0x00C6, 0x014D, 0x00F6, 0x0122, 0x00A2, 0x015A, 0x015B, 0x00D6, 0x00DC, 0x00F8, 0x00A3, 0x00D8, 0x00D7, 0x00A4,
0x0100, 0x012A, 0x00F3, 0x017B, 0x017C, 0x017A, 0x201D, 0x00A6, 0x00A9, 0x00AE, 0x00AC, 0x00BD, 0x00BC, 0x0141, 0x00AB, 0x00BB,
0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x0104, 0x010C, 0x0118, 0x0116, 0x2563, 0x2551, 0x2557, 0x255D, 0x012E, 0x0160, 0x2510,
0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x0172, 0x016A, 0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x017D,
0x0105, 0x010D, 0x0119, 0x0117, 0x012F, 0x0161, 0x0173, 0x016B, 0x017E, 0x2518, 0x250C, 0x2588, 0x2584, 0x258C, 0x2590, 0x2580,
0x00D3, 0x00DF, 0x014C, 0x0143, 0x00F5, 0x00D5, 0x00B5, 0x0144, 0x0136, 0x0137, 0x013B, 0x013C, 0x0146, 0x0112, 0x0145, 0x2019,
0x00AD, 0x00B1, 0x201C, 0x00BE, 0x00B6, 0x00A7, 0x00F7, 0x201E, 0x00B0, 0x2219, 0x00B7, 0x00B9, 0x00B3, 0x00B2, 0x25A0, 0x00A0
};
#elif _CODE_PAGE == 850
#define _TBLDEF 1
static
const WCHAR Tbl[] = { /* CP850(0x80-0xFF) to Unicode conversion table */
0x00C7, 0x00FC, 0x00E9, 0x00E2, 0x00E4, 0x00E0, 0x00E5, 0x00E7, 0x00EA, 0x00EB, 0x00E8, 0x00EF, 0x00EE, 0x00EC, 0x00C4, 0x00C5,
0x00C9, 0x00E6, 0x00C6, 0x00F4, 0x00F6, 0x00F2, 0x00FB, 0x00F9, 0x00FF, 0x00D6, 0x00DC, 0x00F8, 0x00A3, 0x00D8, 0x00D7, 0x0192,
0x00E1, 0x00ED, 0x00F3, 0x00FA, 0x00F1, 0x00D1, 0x00AA, 0x00BA, 0x00BF, 0x00AE, 0x00AC, 0x00BD, 0x00BC, 0x00A1, 0x00AB, 0x00BB,
0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x00C1, 0x00C2, 0x00C0, 0x00A9, 0x2563, 0x2551, 0x2557, 0x255D, 0x00A2, 0x00A5, 0x2510,
0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x00E3, 0x00C3, 0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x00A4,
0x00F0, 0x00D0, 0x00CA, 0x00CB, 0x00C8, 0x0131, 0x00CD, 0x00CE, 0x00CF, 0x2518, 0x250C, 0x2588, 0x2584, 0x00A6, 0x00CC, 0x2580,
0x00D3, 0x00DF, 0x00D4, 0x00D2, 0x00F5, 0x00D5, 0x00B5, 0x00FE, 0x00DE, 0x00DA, 0x00DB, 0x00D9, 0x00FD, 0x00DD, 0x00AF, 0x00B4,
0x00AD, 0x00B1, 0x2017, 0x00BE, 0x00B6, 0x00A7, 0x00F7, 0x00B8, 0x00B0, 0x00A8, 0x00B7, 0x00B9, 0x00B3, 0x00B2, 0x25A0, 0x00A0
};
#elif _CODE_PAGE == 852
#define _TBLDEF 1
static
const WCHAR Tbl[] = { /* CP852(0x80-0xFF) to Unicode conversion table */
0x00C7, 0x00FC, 0x00E9, 0x00E2, 0x00E4, 0x016F, 0x0107, 0x00E7, 0x0142, 0x00EB, 0x0150, 0x0151, 0x00EE, 0x0179, 0x00C4, 0x0106,
0x00C9, 0x0139, 0x013A, 0x00F4, 0x00F6, 0x013D, 0x013E, 0x015A, 0x015B, 0x00D6, 0x00DC, 0x0164, 0x0165, 0x0141, 0x00D7, 0x010D,
0x00E1, 0x00ED, 0x00F3, 0x00FA, 0x0104, 0x0105, 0x017D, 0x017E, 0x0118, 0x0119, 0x00AC, 0x017A, 0x010C, 0x015F, 0x00AB, 0x00BB,
0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x00C1, 0x00C2, 0x011A, 0x015E, 0x2563, 0x2551, 0x2557, 0x255D, 0x017B, 0x017C, 0x2510,
0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x0102, 0x0103, 0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x00A4,
0x0111, 0x0110, 0x010E, 0x00CB, 0x010F, 0x0147, 0x00CD, 0x00CE, 0x011B, 0x2518, 0x250C, 0x2588, 0x2584, 0x0162, 0x016E, 0x2580,
0x00D3, 0x00DF, 0x00D4, 0x0143, 0x0144, 0x0148, 0x0160, 0x0161, 0x0154, 0x00DA, 0x0155, 0x0170, 0x00FD, 0x00DD, 0x0163, 0x00B4,
0x00AD, 0x02DD, 0x02DB, 0x02C7, 0x02D8, 0x00A7, 0x00F7, 0x00B8, 0x00B0, 0x00A8, 0x02D9, 0x0171, 0x0158, 0x0159, 0x25A0, 0x00A0
};
#elif _CODE_PAGE == 855
#define _TBLDEF 1
static
const WCHAR Tbl[] = { /* CP855(0x80-0xFF) to Unicode conversion table */
0x0452, 0x0402, 0x0453, 0x0403, 0x0451, 0x0401, 0x0454, 0x0404, 0x0455, 0x0405, 0x0456, 0x0406, 0x0457, 0x0407, 0x0458, 0x0408,
0x0459, 0x0409, 0x045A, 0x040A, 0x045B, 0x040B, 0x045C, 0x040C, 0x045E, 0x040E, 0x045F, 0x040F, 0x044E, 0x042E, 0x044A, 0x042A,
0x0430, 0x0410, 0x0431, 0x0411, 0x0446, 0x0426, 0x0434, 0x0414, 0x0435, 0x0415, 0x0444, 0x0424, 0x0433, 0x0413, 0x00AB, 0x00BB,
0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x0445, 0x0425, 0x0438, 0x0418, 0x2563, 0x2551, 0x2557, 0x255D, 0x0439, 0x0419, 0x2510,
0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x043A, 0x041A, 0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x00A4,
0x043B, 0x041B, 0x043C, 0x041C, 0x043D, 0x041D, 0x043E, 0x041E, 0x043F, 0x2518, 0x250C, 0x2588, 0x2584, 0x041F, 0x044F, 0x2580,
0x042F, 0x0440, 0x0420, 0x0441, 0x0421, 0x0442, 0x0422, 0x0443, 0x0423, 0x0436, 0x0416, 0x0432, 0x0412, 0x044C, 0x042C, 0x2116,
0x00AD, 0x044B, 0x042B, 0x0437, 0x0417, 0x0448, 0x0428, 0x044D, 0x042D, 0x0449, 0x0429, 0x0447, 0x0427, 0x00A7, 0x25A0, 0x00A0
};
#elif _CODE_PAGE == 857
#define _TBLDEF 1
static
const WCHAR Tbl[] = { /* CP857(0x80-0xFF) to Unicode conversion table */
0x00C7, 0x00FC, 0x00E9, 0x00E2, 0x00E4, 0x00E0, 0x00E5, 0x00E7, 0x00EA, 0x00EB, 0x00E8, 0x00EF, 0x00EE, 0x0131, 0x00C4, 0x00C5,
0x00C9, 0x00E6, 0x00C6, 0x00F4, 0x00F6, 0x00F2, 0x00FB, 0x00F9, 0x0130, 0x00D6, 0x00DC, 0x00F8, 0x00A3, 0x00D8, 0x015E, 0x015F,
0x00E1, 0x00ED, 0x00F3, 0x00FA, 0x00F1, 0x00D1, 0x011E, 0x011F, 0x00BF, 0x00AE, 0x00AC, 0x00BD, 0x00BC, 0x00A1, 0x00AB, 0x00BB,
0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x00C1, 0x00C2, 0x00C0, 0x00A9, 0x2563, 0x2551, 0x2557, 0x255D, 0x00A2, 0x00A5, 0x2510,
0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x00E3, 0x00C3, 0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x00A4,
0x00BA, 0x00AA, 0x00CA, 0x00CB, 0x00C8, 0x0000, 0x00CD, 0x00CE, 0x00CF, 0x2518, 0x250C, 0x2588, 0x2584, 0x00A6, 0x00CC, 0x2580,
0x00D3, 0x00DF, 0x00D4, 0x00D2, 0x00F5, 0x00D5, 0x00B5, 0x0000, 0x00D7, 0x00DA, 0x00DB, 0x00D9, 0x00EC, 0x00FF, 0x00AF, 0x00B4,
0x00AD, 0x00B1, 0x0000, 0x00BE, 0x00B6, 0x00A7, 0x00F7, 0x00B8, 0x00B0, 0x00A8, 0x00B7, 0x00B9, 0x00B3, 0x00B2, 0x25A0, 0x00A0
};
#elif _CODE_PAGE == 860
#define _TBLDEF 1
static
const WCHAR Tbl[] = { /* CP860(0x80-0xFF) to Unicode conversion table */
0x00C7, 0x00FC, 0x00E9, 0x00E2, 0x00E3, 0x00E0, 0x00C1, 0x00E7, 0x00EA, 0x00CA, 0x00E8, 0x00CD, 0x00D4, 0x00EC, 0x00C3, 0x00C2,
0x00C9, 0x00C0, 0x00C8, 0x00F4, 0x00F5, 0x00F2, 0x00DA, 0x00F9, 0x00CC, 0x00D5, 0x00DC, 0x00A2, 0x00A3, 0x00D9, 0x20A7, 0x00D3,
0x00E1, 0x00ED, 0x00F3, 0x00FA, 0x00F1, 0x00D1, 0x00AA, 0x00BA, 0x00BF, 0x00D2, 0x00AC, 0x00BD, 0x00BC, 0x00A1, 0x00AB, 0x00BB,
0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x2561, 0x2562, 0x2556, 0x2555, 0x2563, 0x2551, 0x2557, 0x255D, 0x255C, 0x2558, 0x2510,
0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x255E, 0x255F, 0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x2567,
0x2568, 0x2564, 0x2565, 0x2559, 0x2558, 0x2552, 0x2553, 0x256B, 0x256A, 0x2518, 0x250C, 0x2588, 0x2584, 0x258C, 0x2590, 0x2580,
0x03B1, 0x00DF, 0x0393, 0x03C0, 0x03A3, 0x03C3, 0x00B5, 0x03C4, 0x03A6, 0x0398, 0x03A9, 0x03B4, 0x221E, 0x03C6, 0x03B5, 0x2229,
0x2261, 0x00B1, 0x2265, 0x2264, 0x2320, 0x2321, 0x00F7, 0x2248, 0x00B0, 0x2219, 0x00B7, 0x221A, 0x207F, 0x00B2, 0x25A0, 0x00A0
};
#elif _CODE_PAGE == 861
#define _TBLDEF 1
static
const WCHAR Tbl[] = { /* CP861(0x80-0xFF) to Unicode conversion table */
0x00C7, 0x00FC, 0x00E9, 0x00E2, 0x00E4, 0x00E0, 0x00E6, 0x00E7, 0x00EA, 0x00EB, 0x00E8, 0x00D0, 0x00F0, 0x00DE, 0x00C4, 0x00C5,
0x00C9, 0x00E6, 0x00C6, 0x00F4, 0x00F6, 0x00FE, 0x00FB, 0x00DD, 0x00FD, 0x00D6, 0x00DC, 0x00F8, 0x00A3, 0x00D8, 0x20A7, 0x0192,
0x00E1, 0x00ED, 0x00F3, 0x00FA, 0x00C1, 0x00CD, 0x00D3, 0x00DA, 0x00BF, 0x2310, 0x00AC, 0x00BD, 0x00BC, 0x00A1, 0x00AB, 0x00BB,
0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x2561, 0x2562, 0x2556, 0x2555, 0x2563, 0x2551, 0x2557, 0x255D, 0x255C, 0x255B, 0x2510,
0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x255E, 0x255F, 0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x2567,
0x2568, 0x2564, 0x2565, 0x2559, 0x2558, 0x2552, 0x2553, 0x256B, 0x256A, 0x2518, 0x250C, 0x2588, 0x2584, 0x258C, 0x2590, 0x2580,
0x03B1, 0x00DF, 0x0393, 0x03C0, 0x03A3, 0x03C3, 0x00B5, 0x03C4, 0x03A6, 0x0398, 0x03A9, 0x03B4, 0x221E, 0x03C6, 0x03B5, 0x2229,
0x2261, 0x00B1, 0x2265, 0x2264, 0x2320, 0x2321, 0x00F7, 0x2248, 0x00B0, 0x2219, 0x00B7, 0x221A, 0x207F, 0x00B2, 0x25A0, 0x00A0
};
#elif _CODE_PAGE == 862
#define _TBLDEF 1
static
const WCHAR Tbl[] = { /* CP862(0x80-0xFF) to Unicode conversion table */
0x05D0, 0x05D1, 0x05D2, 0x05D3, 0x05D4, 0x05D5, 0x05D6, 0x05D7, 0x05D8, 0x05D9, 0x05DA, 0x05DB, 0x05DC, 0x05DD, 0x05DE, 0x05DF,
0x05E0, 0x05E1, 0x05E2, 0x05E3, 0x05E4, 0x05E5, 0x05E6, 0x05E7, 0x05E8, 0x05E9, 0x05EA, 0x00A2, 0x00A3, 0x00A5, 0x20A7, 0x0192,
0x00E1, 0x00ED, 0x00F3, 0x00FA, 0x00F1, 0x00D1, 0x00AA, 0x00BA, 0x00BF, 0x2310, 0x00AC, 0x00BD, 0x00BC, 0x00A1, 0x00AB, 0x00BB,
0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x2561, 0x2562, 0x2556, 0x2555, 0x2563, 0x2551, 0x2557, 0x255D, 0x255C, 0x255B, 0x2510,
0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x255E, 0x255F, 0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x2567,
0x2568, 0x2564, 0x2565, 0x2559, 0x2558, 0x2552, 0x2553, 0x256B, 0x256A, 0x2518, 0x250C, 0x2588, 0x2584, 0x258C, 0x2590, 0x2580,
0x03B1, 0x00DF, 0x0393, 0x03C0, 0x03A3, 0x03C3, 0x00B5, 0x03C4, 0x03A6, 0x0398, 0x03A9, 0x03B4, 0x221E, 0x03C6, 0x03B5, 0x2229,
0x2261, 0x00B1, 0x2265, 0x2264, 0x2320, 0x2321, 0x00F7, 0x2248, 0x00B0, 0x2219, 0x00B7, 0x221A, 0x207F, 0x00B2, 0x25A0, 0x00A0
};
#elif _CODE_PAGE == 863
#define _TBLDEF 1
static
const WCHAR Tbl[] = { /* CP863(0x80-0xFF) to Unicode conversion table */
0x00C7, 0x00FC, 0x00E9, 0x00E2, 0x00C2, 0x00E0, 0x00B6, 0x00E7, 0x00EA, 0x00EB, 0x00E8, 0x00EF, 0x00EE, 0x00EC, 0x2017, 0x00C0,
0x00C9, 0x00C8, 0x00CA, 0x00F4, 0x00CB, 0x00CF, 0x00FB, 0x00F9, 0x00A4, 0x00D4, 0x00DC, 0x00A2, 0x00A3, 0x00D9, 0x00DB, 0x0192,
0x00A6, 0x00B4, 0x00F3, 0x00FA, 0x00A8, 0x00BB, 0x00B3, 0x00AF, 0x00CE, 0x3210, 0x00AC, 0x00BD, 0x00BC, 0x00BE, 0x00AB, 0x00BB,
0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x2561, 0x2562, 0x2556, 0x2555, 0x2563, 0x2551, 0x2557, 0x255D, 0x255C, 0x255B, 0x2510,
0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x255E, 0x255F, 0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x2567,
0x2568, 0x2564, 0x2565, 0x2559, 0x2558, 0x2552, 0x2553, 0x256B, 0x256A, 0x2518, 0x250C, 0x2588, 0x2584, 0x258C, 0x2590, 0x2580,
0x03B1, 0x00DF, 0x0393, 0x03C0, 0x03A3, 0x03C3, 0x00B5, 0x03C4, 0x03A6, 0x0398, 0x03A9, 0x03B4, 0x221E, 0x03C6, 0x03B5, 0x2219,
0x2261, 0x00B1, 0x2265, 0x2264, 0x2320, 0x2321, 0x00F7, 0x2248, 0x00B0, 0x2219, 0x00B7, 0x221A, 0x207F, 0x00B2, 0x25A0, 0x00A0
};
#elif _CODE_PAGE == 864
#define _TBLDEF 1
static
const WCHAR Tbl[] = { /* CP864(0x80-0xFF) to Unicode conversion table */
0x00B0, 0x00B7, 0x2219, 0x221A, 0x2592, 0x2500, 0x2502, 0x253C, 0x2524, 0x252C, 0x251C, 0x2534, 0x2510, 0x250C, 0x2514, 0x2518,
0x03B2, 0x221E, 0x03C6, 0x00B1, 0x00BD, 0x00BC, 0x2248, 0x00AB, 0x00BB, 0xFEF7, 0xFEF8, 0x0000, 0x0000, 0xFEFB, 0xFEFC, 0x0000,
0x00A0, 0x00AD, 0xFE82, 0x00A3, 0x00A4, 0xFE84, 0x0000, 0x20AC, 0xFE8E, 0xFE8F, 0xFE95, 0xFE99, 0x060C, 0xFE9D, 0xFEA1, 0xFEA5,
0x0660, 0x0661, 0x0662, 0x0663, 0x0664, 0x0665, 0x0666, 0x0667, 0x0668, 0x0669, 0xFED1, 0x061B, 0xFEB1, 0xFEB5, 0xFEB9, 0x061F,
0x00A2, 0xFE80, 0xFE81, 0xFE83, 0xFE85, 0xFECA, 0xFE8B, 0xFE8D, 0xFE91, 0xFE93, 0xFE97, 0xFE9B, 0xFE9F, 0xFEA3, 0xFEA7, 0xFEA9,
0xFEAB, 0xFEAD, 0xFEAF, 0xFEB3, 0xFEB7, 0xFEBB, 0xFEBF, 0xFEC1, 0xFEC5, 0xFECB, 0xFECF, 0x00A6, 0x00AC, 0x00F7, 0x00D7, 0xFEC9,
0x0640, 0xFED3, 0xFED7, 0xFEDB, 0xFEDF, 0xFEE3, 0xFEE7, 0xFEEB, 0xFEED, 0xFEEF, 0xFEF3, 0xFEBD, 0xFECC, 0xFECE, 0xFECD, 0xFEE1,
0xFE7D, 0x0651, 0xFEE5, 0xFEE9, 0xFEEC, 0xFEF0, 0xFEF2, 0xFED0, 0xFED5, 0xFEF5, 0xFEF6, 0xFEDD, 0xFED9, 0xFEF1, 0x25A0, 0x0000
};
#elif _CODE_PAGE == 865
#define _TBLDEF 1
static
const WCHAR Tbl[] = { /* CP865(0x80-0xFF) to Unicode conversion table */
0x00C7, 0x00FC, 0x00E9, 0x00E2, 0x00E4, 0x00E0, 0x00E5, 0x00E7, 0x00EA, 0x00EB, 0x00E8, 0x00EF, 0x00EE, 0x00EC, 0x00C4, 0x00C5,
0x00C5, 0x00E6, 0x00C6, 0x00F4, 0x00F6, 0x00F2, 0x00FB, 0x00F9, 0x00FF, 0x00D6, 0x00DC, 0x00F8, 0x00A3, 0x00D8, 0x20A7, 0x0192,
0x00E1, 0x00ED, 0x00F3, 0x00FA, 0x00F1, 0x00D1, 0x00AA, 0x00BA, 0x00BF, 0x2310, 0x00AC, 0x00BD, 0x00BC, 0x00A1, 0x00AB, 0x00A4,
0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x2561, 0x2562, 0x2556, 0x2555, 0x2563, 0x2551, 0x2557, 0x255D, 0x255C, 0x2558, 0x2510,
0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x255E, 0x255F, 0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x2567,
0x2568, 0x2564, 0x2565, 0x2559, 0x2558, 0x2552, 0x2553, 0x256B, 0x256A, 0x2518, 0x250C, 0x2588, 0x2584, 0x258C, 0x2590, 0x2580,
0x03B1, 0x00DF, 0x0393, 0x03C0, 0x03A3, 0x03C3, 0x00B5, 0x03C4, 0x03A6, 0x0398, 0x03A9, 0x03B4, 0x221E, 0x03C6, 0x03B5, 0x2229,
0x2261, 0x00B1, 0x2265, 0x2264, 0x2320, 0x2321, 0x00F7, 0x2248, 0x00B0, 0x2219, 0x00B7, 0x221A, 0x207F, 0x00B2, 0x25A0, 0x00A0
};
#elif _CODE_PAGE == 866
#define _TBLDEF 1
static
const WCHAR Tbl[] = { /* CP866(0x80-0xFF) to Unicode conversion table */
0x0410, 0x0411, 0x0412, 0x0413, 0x0414, 0x0415, 0x0416, 0x0417, 0x0418, 0x0419, 0x041A, 0x041B, 0x041C, 0x041D, 0x041E, 0x041F,
0x0420, 0x0421, 0x0422, 0x0423, 0x0424, 0x0425, 0x0426, 0x0427, 0x0428, 0x0429, 0x042A, 0x042B, 0x042C, 0x042D, 0x042E, 0x042F,
0x0430, 0x0431, 0x0432, 0x0433, 0x0434, 0x0435, 0x0436, 0x0437, 0x0438, 0x0439, 0x043A, 0x043B, 0x043C, 0x043D, 0x043E, 0x043F,
0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x2561, 0x2562, 0x2556, 0x2555, 0x2563, 0x2551, 0x2557, 0x255D, 0x255C, 0x255B, 0x2510,
0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x255E, 0x255F, 0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x2567,
0x2568, 0x2564, 0x2565, 0x2559, 0x2558, 0x2552, 0x2553, 0x256B, 0x256A, 0x2518, 0x250C, 0x2588, 0x2584, 0x258C, 0x2590, 0x2580,
0x0440, 0x0441, 0x0442, 0x0443, 0x0444, 0x0445, 0x0446, 0x0447, 0x0448, 0x0449, 0x044A, 0x044B, 0x044C, 0x044D, 0x044E, 0x044F,
0x0401, 0x0451, 0x0404, 0x0454, 0x0407, 0x0457, 0x040E, 0x045E, 0x00B0, 0x2219, 0x00B7, 0x221A, 0x2116, 0x00A4, 0x25A0, 0x00A0
};
#elif _CODE_PAGE == 869
#define _TBLDEF 1
static
const WCHAR Tbl[] = { /* CP869(0x80-0xFF) to Unicode conversion table */
0x00B7, 0x00B7, 0x00B7, 0x00B7, 0x00B7, 0x00B7, 0x0386, 0x00B7, 0x00B7, 0x00AC, 0x00A6, 0x2018, 0x2019, 0x0388, 0x2015, 0x0389,
0x038A, 0x03AA, 0x038C, 0x00B7, 0x00B7, 0x038E, 0x03AB, 0x00A9, 0x038F, 0x00B2, 0x00B3, 0x03AC, 0x00A3, 0x03AD, 0x03AE, 0x03AF,
0x03CA, 0x0390, 0x03CC, 0x03CD, 0x0391, 0x0392, 0x0393, 0x0394, 0x0395, 0x0396, 0x0397, 0x00BD, 0x0398, 0x0399, 0x00AB, 0x00BB,
0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x039A, 0x039B, 0x039C, 0x039D, 0x2563, 0x2551, 0x2557, 0x255D, 0x039E, 0x039F, 0x2510,
0x2514, 0x2534, 0x252C, 0x251C, 0x2500, 0x253C, 0x0A30, 0x03A1, 0x255A, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256C, 0x03A3,
0x03A4, 0x03A5, 0x03A6, 0x03A7, 0x03A8, 0x03A9, 0x03B1, 0x03B2, 0x03B3, 0x2518, 0x250C, 0x2588, 0x2584, 0x03B4, 0x03B5, 0x2580,
0x03B6, 0x03B7, 0x03B8, 0x03B9, 0x03BA, 0x03BB, 0x03BC, 0x03BD, 0x03BE, 0x03BF, 0x03C0, 0x03C1, 0x03C3, 0x03C2, 0x03C4, 0x0384,
0x00AD, 0x00B1, 0x03C5, 0x03C6, 0x03C7, 0x00A7, 0x03C8, 0x0385, 0x00B0, 0x00A8, 0x03C9, 0x03CB, 0x03B0, 0x03CE, 0x25A0, 0x00A0
};
#endif
#if !_TBLDEF || !_USE_LFN
#error This file is not needed at current configuration. Remove from the project.
#endif
WCHAR ff_convert ( /* Converted character, Returns zero on error */
WCHAR chr, /* Character code to be converted */
UINT dir /* 0: Unicode to OEM code, 1: OEM code to Unicode */
)
{
WCHAR c;
if (chr < 0x80) { /* ASCII */
c = chr;
} else {
if (dir) { /* OEM code to Unicode */
c = (chr >= 0x100) ? 0 : Tbl[chr - 0x80];
} else { /* Unicode to OEM code */
for (c = 0; c < 0x80; c++) {
if (chr == Tbl[c]) break;
}
c = (c + 0x80) & 0xFF;
}
}
return c;
}
WCHAR ff_wtoupper ( /* Returns upper converted character */
WCHAR chr /* Unicode character to be upper converted (BMP only) */
)
{
/* Compressed upper conversion table */
static const WCHAR cvt1[] = { /* U+0000 - U+0FFF */
/* Basic Latin */
0x0061,0x031A,
/* Latin-1 Supplement */
0x00E0,0x0317, 0x00F8,0x0307, 0x00FF,0x0001,0x0178,
/* Latin Extended-A */
0x0100,0x0130, 0x0132,0x0106, 0x0139,0x0110, 0x014A,0x012E, 0x0179,0x0106,
/* Latin Extended-B */
0x0180,0x004D,0x0243,0x0181,0x0182,0x0182,0x0184,0x0184,0x0186,0x0187,0x0187,0x0189,0x018A,0x018B,0x018B,0x018D,0x018E,0x018F,0x0190,0x0191,0x0191,0x0193,0x0194,0x01F6,0x0196,0x0197,0x0198,0x0198,0x023D,0x019B,0x019C,0x019D,0x0220,0x019F,0x01A0,0x01A0,0x01A2,0x01A2,0x01A4,0x01A4,0x01A6,0x01A7,0x01A7,0x01A9,0x01AA,0x01AB,0x01AC,0x01AC,0x01AE,0x01AF,0x01AF,0x01B1,0x01B2,0x01B3,0x01B3,0x01B5,0x01B5,0x01B7,0x01B8,0x01B8,0x01BA,0x01BB,0x01BC,0x01BC,0x01BE,0x01F7,0x01C0,0x01C1,0x01C2,0x01C3,0x01C4,0x01C5,0x01C4,0x01C7,0x01C8,0x01C7,0x01CA,0x01CB,0x01CA,
0x01CD,0x0110, 0x01DD,0x0001,0x018E, 0x01DE,0x0112, 0x01F3,0x0003,0x01F1,0x01F4,0x01F4, 0x01F8,0x0128,
0x0222,0x0112, 0x023A,0x0009,0x2C65,0x023B,0x023B,0x023D,0x2C66,0x023F,0x0240,0x0241,0x0241, 0x0246,0x010A,
/* IPA Extensions */
0x0253,0x0040,0x0181,0x0186,0x0255,0x0189,0x018A,0x0258,0x018F,0x025A,0x0190,0x025C,0x025D,0x025E,0x025F,0x0193,0x0261,0x0262,0x0194,0x0264,0x0265,0x0266,0x0267,0x0197,0x0196,0x026A,0x2C62,0x026C,0x026D,0x026E,0x019C,0x0270,0x0271,0x019D,0x0273,0x0274,0x019F,0x0276,0x0277,0x0278,0x0279,0x027A,0x027B,0x027C,0x2C64,0x027E,0x027F,0x01A6,0x0281,0x0282,0x01A9,0x0284,0x0285,0x0286,0x0287,0x01AE,0x0244,0x01B1,0x01B2,0x0245,0x028D,0x028E,0x028F,0x0290,0x0291,0x01B7,
/* Greek, Coptic */
0x037B,0x0003,0x03FD,0x03FE,0x03FF, 0x03AC,0x0004,0x0386,0x0388,0x0389,0x038A, 0x03B1,0x0311,
0x03C2,0x0002,0x03A3,0x03A3, 0x03C4,0x0308, 0x03CC,0x0003,0x038C,0x038E,0x038F, 0x03D8,0x0118,
0x03F2,0x000A,0x03F9,0x03F3,0x03F4,0x03F5,0x03F6,0x03F7,0x03F7,0x03F9,0x03FA,0x03FA,
/* Cyrillic */
0x0430,0x0320, 0x0450,0x0710, 0x0460,0x0122, 0x048A,0x0136, 0x04C1,0x010E, 0x04CF,0x0001,0x04C0, 0x04D0,0x0144,
/* Armenian */
0x0561,0x0426,
0x0000
};
static const WCHAR cvt2[] = { /* U+1000 - U+FFFF */
/* Phonetic Extensions */
0x1D7D,0x0001,0x2C63,
/* Latin Extended Additional */
0x1E00,0x0196, 0x1EA0,0x015A,
/* Greek Extended */
0x1F00,0x0608, 0x1F10,0x0606, 0x1F20,0x0608, 0x1F30,0x0608, 0x1F40,0x0606,
0x1F51,0x0007,0x1F59,0x1F52,0x1F5B,0x1F54,0x1F5D,0x1F56,0x1F5F, 0x1F60,0x0608,
0x1F70,0x000E,0x1FBA,0x1FBB,0x1FC8,0x1FC9,0x1FCA,0x1FCB,0x1FDA,0x1FDB,0x1FF8,0x1FF9,0x1FEA,0x1FEB,0x1FFA,0x1FFB,
0x1F80,0x0608, 0x1F90,0x0608, 0x1FA0,0x0608, 0x1FB0,0x0004,0x1FB8,0x1FB9,0x1FB2,0x1FBC,
0x1FCC,0x0001,0x1FC3, 0x1FD0,0x0602, 0x1FE0,0x0602, 0x1FE5,0x0001,0x1FEC, 0x1FF2,0x0001,0x1FFC,
/* Letterlike Symbols */
0x214E,0x0001,0x2132,
/* Number forms */
0x2170,0x0210, 0x2184,0x0001,0x2183,
/* Enclosed Alphanumerics */
0x24D0,0x051A, 0x2C30,0x042F,
/* Latin Extended-C */
0x2C60,0x0102, 0x2C67,0x0106, 0x2C75,0x0102,
/* Coptic */
0x2C80,0x0164,
/* Georgian Supplement */
0x2D00,0x0826,
/* Full-width */
0xFF41,0x031A,
0x0000
};
const WCHAR *p;
WCHAR bc, nc, cmd;
p = chr < 0x1000 ? cvt1 : cvt2;
for (;;) {
bc = *p++; /* Get block base */
if (!bc || chr < bc) break;
nc = *p++; cmd = nc >> 8; nc &= 0xFF; /* Get processing command and block size */
if (chr < bc + nc) { /* In the block? */
switch (cmd) {
case 0: chr = p[chr - bc]; break; /* Table conversion */
case 1: chr -= (chr - bc) & 1; break; /* Case pairs */
case 2: chr -= 16; break; /* Shift -16 */
case 3: chr -= 32; break; /* Shift -32 */
case 4: chr -= 48; break; /* Shift -48 */
case 5: chr -= 26; break; /* Shift -26 */
case 6: chr += 8; break; /* Shift +8 */
case 7: chr -= 80; break; /* Shift -80 */
case 8: chr -= 0x1C60; break; /* Shift -0x1C60 */
}
break;
}
if (!cmd) p += nc;
}
return chr;
}

View File

@ -0,0 +1,3 @@
#pragma once
#include "../../types.h"

View File

@ -1,5 +1,9 @@
// Copyright 2014 Normmatt
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#pragma once #pragma once
#include "../../types.h" #include "common.h"
void waitcycles(u32 us); void ioDelay(u32 us);

View File

@ -1,16 +1,17 @@
.text // Copyright 2014 Normmatt
.arm // Licensed under GPLv2 or any later version
.align 4 // Refer to the license.txt file included.
.global waitcycles .arm
.type waitcycles, %function .global ioDelay
waitcycles: .type ioDelay STT_FUNC
push {r0-r2, lr}
str r0, [sp, #4] @ioDelay ( u32 us )
waitcycles_loop: ioDelay:
ldr r3, [sp, #4] ldr r1, =0x18000000 @ VRAM
subs r2, r3, #1 1:
str r2, [sp, #4] @ Loop doing uncached reads from VRAM to make loop timing more reliable
cmp r3, #0 ldr r2, [r1]
bne waitcycles_loop subs r0, #1
pop {r0-r2, pc} bgt 1b
bx lr

View File

@ -1,55 +1,30 @@
/* // Copyright 2014 Normmatt
* This Source Code Form is subject to the terms of the Mozilla Public // Licensed under GPLv2 or any later version
* License, v. 2.0. If a copy of the MPL was not distributed with this file, // Refer to the license.txt file included.
* You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Copyright (c) 2014-2015, Normmatt
*
* Alternatively, the contents of this file may be used under the terms
* of the GNU General Public License Version 2, as described below:
*
* This file is free software: you may copy, redistribute and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 2 of the License, or (at your
* option) any later version.
*
* This file is distributed in the hope that 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*/
#include "sdmmc.h" #include "sdmmc.h"
#include "delay.h" #include "delay.h"
static struct mmcdevice handleNAND; struct mmcdevice handleNAND;
static struct mmcdevice handleSD; struct mmcdevice handleSD;
static inline u16 sdmmc_read16(u16 reg) static inline u16 sdmmc_read16(u16 reg) {
{ return *(vu16*)(SDMMC_BASE + reg);
return *(vu16 *)(SDMMC_BASE + reg);
} }
static inline void sdmmc_write16(u16 reg, u16 val) static inline void sdmmc_write16(u16 reg, u16 val) {
{ *(vu16*)(SDMMC_BASE + reg) = val;
*(vu16 *)(SDMMC_BASE + reg) = val;
} }
static inline u32 sdmmc_read32(u16 reg) static inline u32 sdmmc_read32(u16 reg) {
{ return *(vu32*)(SDMMC_BASE + reg);
return *(vu32 *)(SDMMC_BASE + reg);
} }
static inline void sdmmc_write32(u16 reg, u32 val) static inline void sdmmc_write32(u16 reg, u32 val) {
{ *(vu32*)(SDMMC_BASE + reg) = val;
*(vu32 *)(SDMMC_BASE + reg) = val;
} }
static inline void sdmmc_mask16(u16 reg, const u16 clear, const u16 set) static inline void sdmmc_mask16(u16 reg, const u16 clear, const u16 set) {
{
u16 val = sdmmc_read16(reg); u16 val = sdmmc_read16(reg);
val &= ~clear; val &= ~clear;
val |= set; val |= set;
@ -63,259 +38,208 @@ static inline void setckl(u32 data)
sdmmc_mask16(REG_SDCLKCTL, 0x0, 0x100); sdmmc_mask16(REG_SDCLKCTL, 0x0, 0x100);
} }
/*
mmcdevice *getMMCDevice(int drive) mmcdevice *getMMCDevice(int drive)
{ {
if(drive == 0) return &handleNAND; if(drive==0) return &handleNAND;
return &handleSD; return &handleSD;
} }
*/
static int geterror(struct mmcdevice *ctx) static u32 __attribute__((noinline)) geterror(struct mmcdevice *ctx)
{ {
return (int)((ctx->error << 29) >> 31); return (ctx->error << 29) >> 31;
} }
static void inittarget(struct mmcdevice *ctx) static void __attribute__((noinline)) inittarget(struct mmcdevice *ctx)
{ {
sdmmc_mask16(REG_SDPORTSEL, 0x3, (u16)ctx->devicenumber); sdmmc_mask16(REG_SDPORTSEL,0x3,(u16)ctx->devicenumber);
setckl(ctx->clk); setckl(ctx->clk);
if(ctx->SDOPT == 0) sdmmc_mask16(REG_SDOPT, 0, 0x8000); if (ctx->SDOPT == 0) {
else sdmmc_mask16(REG_SDOPT, 0x8000, 0); sdmmc_mask16(REG_SDOPT, 0, 0x8000);
} else {
sdmmc_mask16(REG_SDOPT, 0x8000, 0);
}
} }
static void __attribute__((noinline)) sdmmc_send_command(struct mmcdevice *ctx, u32 cmd, u32 args) static void __attribute__((noinline)) sdmmc_send_command(struct mmcdevice *ctx, u32 cmd, u32 args)
{ {
u32 getSDRESP = (cmd << 15) >> 31; bool getSDRESP = (cmd << 15) >> 31;
u16 flags = (cmd << 15) >> 31; u16 flags = (cmd << 15) >> 31;
const int readdata = cmd & 0x20000; const bool readdata = cmd & 0x20000;
const int writedata = cmd & 0x40000; const bool writedata = cmd & 0x40000;
if(readdata || writedata) if (readdata || writedata)
flags |= TMIO_STAT0_DATAEND; flags |= TMIO_STAT0_DATAEND;
ctx->error = 0; ctx->error = 0;
while((sdmmc_read16(REG_SDSTATUS1) & TMIO_STAT1_CMD_BUSY)); //mmc working? while (sdmmc_read16(REG_SDSTATUS1) & TMIO_STAT1_CMD_BUSY); //mmc working?
sdmmc_write16(REG_SDIRMASK0, 0); sdmmc_write16(REG_SDIRMASK0,0);
sdmmc_write16(REG_SDIRMASK1, 0); sdmmc_write16(REG_SDIRMASK1,0);
sdmmc_write16(REG_SDSTATUS0, 0); sdmmc_write16(REG_SDSTATUS0,0);
sdmmc_write16(REG_SDSTATUS1, 0); sdmmc_write16(REG_SDSTATUS1,0);
sdmmc_mask16(REG_DATACTL32, 0x1800, 0); sdmmc_mask16(REG_SDDATACTL32,0x1800,0);
sdmmc_write16(REG_SDCMDARG0, args & 0xFFFF);
sdmmc_write16(REG_SDCMDARG1, args >> 16); sdmmc_write16(REG_SDCMDARG0,args &0xFFFF);
sdmmc_write16(REG_SDCMD, cmd & 0xFFFF); sdmmc_write16(REG_SDCMDARG1,args >> 16);
sdmmc_write16(REG_SDCMD,cmd &0xFFFF);
u32 size = ctx->size; u32 size = ctx->size;
u8 *rDataPtr = ctx->rData; vu8 *dataPtr = ctx->data;
const u8 *tDataPtr = ctx->tData;
bool rUseBuf = rDataPtr != NULL; bool useBuf = ( NULL != dataPtr );
bool tUseBuf = tDataPtr != NULL;
u16 status0 = 0; u16 status0 = 0;
while(true) while(true) {
{ u16 status1 = sdmmc_read16(REG_SDSTATUS1);
vu16 status1 = sdmmc_read16(REG_SDSTATUS1); if (status1 & TMIO_STAT1_RXRDY) {
vu16 ctl32 = sdmmc_read16(REG_DATACTL32); if (readdata && useBuf) {
if((ctl32 & 0x100)) sdmmc_mask16(REG_SDSTATUS1, TMIO_STAT1_RXRDY, 0);
{ //sdmmc_write16(REG_SDSTATUS1,~TMIO_STAT1_RXRDY);
if(readdata) if (size > 0x1FF) {
{ for(int i = 0; i<0x200; i+=2) {
if(rUseBuf) u16 data = sdmmc_read16(REG_SDFIFO);
{ *dataPtr++ = data & 0xFF;
sdmmc_mask16(REG_SDSTATUS1, TMIO_STAT1_RXRDY, 0); *dataPtr++ = data >> 8;
if(size > 0x1FF)
{
//Gabriel Marcano: This implementation doesn't assume alignment.
//I've removed the alignment check doen with former rUseBuf32 as a result
for(int i = 0; i < 0x200; i += 4)
{
u32 data = sdmmc_read32(REG_SDFIFO32);
*rDataPtr++ = data;
*rDataPtr++ = data >> 8;
*rDataPtr++ = data >> 16;
*rDataPtr++ = data >> 24;
}
size -= 0x200;
} }
size -= 0x200;
} }
sdmmc_mask16(REG_DATACTL32, 0x800, 0);
} }
} }
if(!(ctl32 & 0x200))
{
if(writedata)
{
if(tUseBuf)
{
sdmmc_mask16(REG_SDSTATUS1, TMIO_STAT1_TXRQ, 0);
if(size > 0x1FF)
{
for(int i = 0; i < 0x200; i += 4)
{
u32 data = *tDataPtr++;
data |= (u32)*tDataPtr++ << 8;
data |= (u32)*tDataPtr++ << 16;
data |= (u32)*tDataPtr++ << 24;
sdmmc_write32(REG_SDFIFO32, data);
}
size -= 0x200;
}
}
sdmmc_mask16(REG_DATACTL32, 0x1000, 0); if (status1 & TMIO_STAT1_TXRQ) {
if (writedata && useBuf) {
sdmmc_mask16(REG_SDSTATUS1, TMIO_STAT1_TXRQ, 0);
//sdmmc_write16(REG_SDSTATUS1,~TMIO_STAT1_TXRQ);
if (size > 0x1FF) {
for (int i = 0; i<0x200; i+=2) {
u16 data = *dataPtr++;
data |= *dataPtr++ << 8;
sdmmc_write16(REG_SDFIFO, data);
}
size -= 0x200;
}
} }
} }
if(status1 & TMIO_MASK_GW) if (status1 & TMIO_MASK_GW) {
{
ctx->error |= 4; ctx->error |= 4;
break; break;
} }
if(!(status1 & TMIO_STAT1_CMD_BUSY)) if (!(status1 & TMIO_STAT1_CMD_BUSY)) {
{
status0 = sdmmc_read16(REG_SDSTATUS0); status0 = sdmmc_read16(REG_SDSTATUS0);
if(sdmmc_read16(REG_SDSTATUS0) & TMIO_STAT0_CMDRESPEND) if (sdmmc_read16(REG_SDSTATUS0) & TMIO_STAT0_CMDRESPEND)
{
ctx->error |= 0x1; ctx->error |= 0x1;
} if (status0 & TMIO_STAT0_DATAEND)
if(status0 & TMIO_STAT0_DATAEND)
{
ctx->error |= 0x2; ctx->error |= 0x2;
}
if((status0 & flags) == flags) if ((status0 & flags) == flags)
break; break;
} }
} }
ctx->stat0 = sdmmc_read16(REG_SDSTATUS0); ctx->stat0 = sdmmc_read16(REG_SDSTATUS0);
ctx->stat1 = sdmmc_read16(REG_SDSTATUS1); ctx->stat1 = sdmmc_read16(REG_SDSTATUS1);
sdmmc_write16(REG_SDSTATUS0, 0); sdmmc_write16(REG_SDSTATUS0,0);
sdmmc_write16(REG_SDSTATUS1, 0); sdmmc_write16(REG_SDSTATUS1,0);
if(getSDRESP != 0) if (getSDRESP != 0) {
{ ctx->ret[0] = (u32)sdmmc_read16(REG_SDRESP0) | (u32)(sdmmc_read16(REG_SDRESP1) << 16);
ctx->ret[0] = (u32)(sdmmc_read16(REG_SDRESP0) | (sdmmc_read16(REG_SDRESP1) << 16)); ctx->ret[1] = (u32)sdmmc_read16(REG_SDRESP2) | (u32)(sdmmc_read16(REG_SDRESP3) << 16);
ctx->ret[1] = (u32)(sdmmc_read16(REG_SDRESP2) | (sdmmc_read16(REG_SDRESP3) << 16)); ctx->ret[2] = (u32)sdmmc_read16(REG_SDRESP4) | (u32)(sdmmc_read16(REG_SDRESP5) << 16);
ctx->ret[2] = (u32)(sdmmc_read16(REG_SDRESP4) | (sdmmc_read16(REG_SDRESP5) << 16)); ctx->ret[3] = (u32)sdmmc_read16(REG_SDRESP6) | (u32)(sdmmc_read16(REG_SDRESP7) << 16);
ctx->ret[3] = (u32)(sdmmc_read16(REG_SDRESP6) | (sdmmc_read16(REG_SDRESP7) << 16));
} }
} }
int __attribute__((noinline)) sdmmc_sdcard_writesectors(u32 sector_no, u32 numsectors, const u8 *in) u32 __attribute__((noinline)) sdmmc_sdcard_writesectors(u32 sector_no, u32 numsectors, vu8 *in)
{ {
if(handleSD.isSDHC == 0) sector_no <<= 9; if (handleSD.isSDHC == 0)
sector_no <<= 9;
inittarget(&handleSD); inittarget(&handleSD);
sdmmc_write16(REG_SDSTOP, 0x100); sdmmc_write16(REG_SDSTOP,0x100);
sdmmc_write16(REG_SDBLKCOUNT32, numsectors);
sdmmc_write16(REG_SDBLKLEN32, 0x200); sdmmc_write16(REG_SDBLKCOUNT,numsectors);
sdmmc_write16(REG_SDBLKCOUNT, numsectors); handleSD.data = in;
handleSD.tData = in;
handleSD.size = numsectors << 9; handleSD.size = numsectors << 9;
sdmmc_send_command(&handleSD, 0x52C19, sector_no); sdmmc_send_command(&handleSD,0x52C19,sector_no);
return geterror(&handleSD); return geterror(&handleSD);
} }
int __attribute__((noinline)) sdmmc_sdcard_readsectors(u32 sector_no, u32 numsectors, u8 *out) u32 __attribute__((noinline)) sdmmc_sdcard_readsectors(u32 sector_no, u32 numsectors, vu8 *out)
{ {
if(handleSD.isSDHC == 0) sector_no <<= 9; if (handleSD.isSDHC == 0)
sector_no <<= 9;
inittarget(&handleSD); inittarget(&handleSD);
sdmmc_write16(REG_SDSTOP, 0x100); sdmmc_write16(REG_SDSTOP,0x100);
sdmmc_write16(REG_SDBLKCOUNT32, numsectors);
sdmmc_write16(REG_SDBLKLEN32, 0x200); sdmmc_write16(REG_SDBLKCOUNT,numsectors);
sdmmc_write16(REG_SDBLKCOUNT, numsectors); handleSD.data = out;
handleSD.rData = out;
handleSD.size = numsectors << 9; handleSD.size = numsectors << 9;
sdmmc_send_command(&handleSD, 0x33C12, sector_no); sdmmc_send_command(&handleSD,0x33C12,sector_no);
return geterror(&handleSD); return geterror(&handleSD);
} }
int __attribute__((noinline)) sdmmc_nand_readsectors(u32 sector_no, u32 numsectors, u8 *out) u32 __attribute__((noinline)) sdmmc_nand_readsectors(u32 sector_no, u32 numsectors, vu8 *out)
{ {
if(handleNAND.isSDHC == 0) sector_no <<= 9; if (handleNAND.isSDHC == 0)
sector_no <<= 9;
inittarget(&handleNAND); inittarget(&handleNAND);
sdmmc_write16(REG_SDSTOP, 0x100); sdmmc_write16(REG_SDSTOP,0x100);
sdmmc_write16(REG_SDBLKCOUNT32, numsectors);
sdmmc_write16(REG_SDBLKLEN32, 0x200); sdmmc_write16(REG_SDBLKCOUNT,numsectors);
sdmmc_write16(REG_SDBLKCOUNT, numsectors);
handleNAND.rData = out; handleNAND.data = out;
handleNAND.size = numsectors << 9; handleNAND.size = numsectors << 9;
sdmmc_send_command(&handleNAND, 0x33C12, sector_no); sdmmc_send_command(&handleNAND,0x33C12,sector_no);
inittarget(&handleSD); inittarget(&handleSD);
return geterror(&handleNAND); return geterror(&handleNAND);
} }
int __attribute__((noinline)) sdmmc_nand_writesectors(u32 sector_no, u32 numsectors, const u8 *in) //experimental u32 __attribute__((noinline)) sdmmc_nand_writesectors(u32 sector_no, u32 numsectors, vu8 *in) //experimental
{ {
if(handleNAND.isSDHC == 0) sector_no <<= 9; if (handleNAND.isSDHC == 0)
sector_no <<= 9;
inittarget(&handleNAND); inittarget(&handleNAND);
sdmmc_write16(REG_SDSTOP, 0x100); sdmmc_write16(REG_SDSTOP,0x100);
sdmmc_write16(REG_SDBLKCOUNT32, numsectors);
sdmmc_write16(REG_SDBLKLEN32, 0x200); sdmmc_write16(REG_SDBLKCOUNT,numsectors);
sdmmc_write16(REG_SDBLKCOUNT, numsectors);
handleNAND.tData = in; handleNAND.data = in;
handleNAND.size = numsectors << 9; handleNAND.size = numsectors << 9;
sdmmc_send_command(&handleNAND, 0x52C19, sector_no); sdmmc_send_command(&handleNAND,0x52C19,sector_no);
inittarget(&handleSD); inittarget(&handleSD);
return geterror(&handleNAND); return geterror(&handleNAND);
} }
static u32 calcSDSize(u8 *csd, int type) static u32 calcSDSize(u8* csd, int type)
{ {
u32 result = 0; u32 result = 0;
if(type == -1) type = csd[14] >> 6; if (type == -1) type = csd[14] >> 6;
switch(type) switch (type) {
{
case 0: case 0:
{ {
u32 block_len = csd[9] & 0xF; u32 block_len = csd[9] & 0xf;
block_len = 1u << block_len; block_len = 1u << block_len;
u32 mult = (u32)((csd[4] >> 7) | ((csd[5] & 3) << 1)); u32 mult = (u32)(csd[4] >> 7) | (u32)((csd[5] & 3) << 1);
mult = 1u << (mult + 2); mult = 1u << (mult + 2);
result = csd[8] & 3; result = csd[8] & 3;
result = (result << 8) | csd[7]; result = (result << 8) | csd[7];
result = (result << 2) | (csd[6] >> 6); result = (result << 2) | (csd[6] >> 6);
result = (result + 1) * mult * block_len / 512; result = (result + 1) * mult * block_len / 512;
}
break; break;
}
case 1: case 1:
result = csd[7] & 0x3F; result = csd[7] & 0x3f;
result = (result << 8) | csd[6]; result = (result << 8) | csd[6];
result = (result << 8) | csd[5]; result = (result << 8) | csd[5];
result = (result + 1) * 1024; result = (result + 1) * 1024;
break; break;
default: default:
break; //Do nothing otherwise FIXME perhaps return some error? break; //Do nothing otherwise
} }
return result; return result;
} }
static void InitSD() static void InitSD()
{
*(vu16 *)0x10006100 &= 0xF7FFu; //SDDATACTL32
*(vu16 *)0x10006100 &= 0xEFFFu; //SDDATACTL32
*(vu16 *)0x10006100 |= 0x402u; //SDDATACTL32
*(vu16 *)0x100060D8 = (*(vu16 *)0x100060D8 & 0xFFDD) | 2;
*(vu16 *)0x10006100 &= 0xFFFFu; //SDDATACTL32
*(vu16 *)0x100060D8 &= 0xFFDFu; //SDDATACTL
*(vu16 *)0x10006104 = 512; //SDBLKLEN32
*(vu16 *)0x10006108 = 1; //SDBLKCOUNT32
*(vu16 *)0x100060E0 &= 0xFFFEu; //SDRESET
*(vu16 *)0x100060E0 |= 1u; //SDRESET
*(vu16 *)0x10006020 |= TMIO_MASK_ALL; //SDIR_MASK0
*(vu16 *)0x10006022 |= TMIO_MASK_ALL>>16; //SDIR_MASK1
*(vu16 *)0x100060FC |= 0xDBu; //SDCTL_RESERVED7
*(vu16 *)0x100060FE |= 0xDBu; //SDCTL_RESERVED8
*(vu16 *)0x10006002 &= 0xFFFCu; //SDPORTSEL
*(vu16 *)0x10006024 = 0x20;
*(vu16 *)0x10006028 = 0x40EE;
*(vu16 *)0x10006002 &= 0xFFFCu; ////SDPORTSEL
*(vu16 *)0x10006026 = 512; //SDBLKLEN
*(vu16 *)0x10006008 = 0; //SDSTOP
}
static int Nand_Init()
{ {
//NAND //NAND
handleNAND.isSDHC = 0; handleNAND.isSDHC = 0;
@ -325,50 +249,80 @@ static int Nand_Init()
handleNAND.clk = 0x80; handleNAND.clk = 0x80;
handleNAND.devicenumber = 1; handleNAND.devicenumber = 1;
//SD
handleSD.isSDHC = 0;
handleSD.SDOPT = 0;
handleSD.res = 0;
handleSD.initarg = 0;
handleSD.clk = 0x80;
handleSD.devicenumber = 0;
*(vu16*)0x10006100 &= 0xF7FFu; //SDDATACTL32
*(vu16*)0x10006100 &= 0xEFFFu; //SDDATACTL32
*(vu16*)0x10006100 |= 0x402u; //SDDATACTL32
*(vu16*)0x100060D8 = (*(vu16*)0x100060D8 & 0xFFDD) | 2;
*(vu16*)0x10006100 &= 0xFFFDu; //SDDATACTL32
*(vu16*)0x100060D8 &= 0xFFDDu; //SDDATACTL
*(vu16*)0x10006104 = 0; //SDBLKLEN32
*(vu16*)0x10006108 = 1; //SDBLKCOUNT32
*(vu16*)0x100060E0 &= 0xFFFEu; //SDRESET
*(vu16*)0x100060E0 |= 1u; //SDRESET
*(vu16*)0x10006020 |= TMIO_MASK_ALL; //SDIR_MASK0
*(vu16*)0x10006022 |= TMIO_MASK_ALL>>16; //SDIR_MASK1
*(vu16*)0x100060FC |= 0xDBu; //SDCTL_RESERVED7
*(vu16*)0x100060FE |= 0xDBu; //SDCTL_RESERVED8
*(vu16*)0x10006002 &= 0xFFFCu; //SDPORTSEL
*(vu16*)0x10006024 = 0x40; //Nintendo sets this to 0x20
*(vu16*)0x10006028 = 0x40EB; //Nintendo sets this to 0x40EE
*(vu16*)0x10006002 &= 0xFFFCu; ////SDPORTSEL
*(vu16*)0x10006026 = 512; //SDBLKLEN
*(vu16*)0x10006008 = 0; //SDSTOP
inittarget(&handleSD);
}
static int Nand_Init()
{
inittarget(&handleNAND); inittarget(&handleNAND);
waitcycles(0xF000); ioDelay(0xF000);
sdmmc_send_command(&handleNAND, 0, 0); sdmmc_send_command(&handleNAND,0,0);
do do {
{ do {
do sdmmc_send_command(&handleNAND,0x10701,0x100000);
{ } while ( !(handleNAND.error & 1) );
sdmmc_send_command(&handleNAND, 0x10701, 0x100000); } while((handleNAND.ret[0] & 0x80000000) == 0);
}
while(!(handleNAND.error & 1));
}
while((handleNAND.ret[0] & 0x80000000) == 0);
sdmmc_send_command(&handleNAND, 0x10602, 0x0); sdmmc_send_command(&handleNAND,0x10602,0x0);
if((handleNAND.error & 0x4)) return -1; if (handleNAND.error & 0x4) return -1;
sdmmc_send_command(&handleNAND, 0x10403, handleNAND.initarg << 0x10); sdmmc_send_command(&handleNAND,0x10403,handleNAND.initarg << 0x10);
if((handleNAND.error & 0x4)) return -1; if (handleNAND.error & 0x4) return -1;
sdmmc_send_command(&handleNAND, 0x10609, handleNAND.initarg << 0x10); sdmmc_send_command(&handleNAND,0x10609,handleNAND.initarg << 0x10);
if((handleNAND.error & 0x4)) return -1; if (handleNAND.error & 0x4) return -1;
handleNAND.total_size = calcSDSize((u8*)&handleNAND.ret[0], 0); handleNAND.total_size = calcSDSize((u8*)&handleNAND.ret[0],0);
handleNAND.clk = 1; handleNAND.clk = 1;
setckl(1); setckl(1);
sdmmc_send_command(&handleNAND, 0x10407, handleNAND.initarg << 0x10); sdmmc_send_command(&handleNAND,0x10407,handleNAND.initarg << 0x10);
if((handleNAND.error & 0x4)) return -1; if (handleNAND.error & 0x4) return -1;
handleNAND.SDOPT = 1; handleNAND.SDOPT = 1;
sdmmc_send_command(&handleNAND, 0x10506, 0x3B70100); sdmmc_send_command(&handleNAND,0x10506,0x3B70100);
if((handleNAND.error & 0x4)) return -1; if (handleNAND.error & 0x4) return -1;
sdmmc_send_command(&handleNAND, 0x10506, 0x3B90100); sdmmc_send_command(&handleNAND,0x10506,0x3B90100);
if((handleNAND.error & 0x4)) return -1; if (handleNAND.error & 0x4) return -1;
sdmmc_send_command(&handleNAND, 0x1040D, handleNAND.initarg << 0x10); sdmmc_send_command(&handleNAND,0x1040D,handleNAND.initarg << 0x10);
if((handleNAND.error & 0x4)) return -1; if (handleNAND.error & 0x4) return -1;
sdmmc_send_command(&handleNAND, 0x10410, 0x200); sdmmc_send_command(&handleNAND,0x10410,0x200);
if((handleNAND.error & 0x4)) return -1; if (handleNAND.error & 0x4) return -1;
handleNAND.clk |= 0x200; handleNAND.clk |= 0x200;
@ -379,104 +333,114 @@ static int Nand_Init()
static int SD_Init() static int SD_Init()
{ {
//SD
handleSD.isSDHC = 0;
handleSD.SDOPT = 0;
handleSD.res = 0;
handleSD.initarg = 0;
handleSD.clk = 0x80;
handleSD.devicenumber = 0;
inittarget(&handleSD); inittarget(&handleSD);
waitcycles(1u << 22); //Card needs a little bit of time to be detected, it seems FIXME test again to see what a good number is for the delay ioDelay(1u << 18); //Card needs a little bit of time to be detected, it seems
//If not inserted //If not inserted
if(!(*((vu16 *)(SDMMC_BASE + REG_SDSTATUS0)) & TMIO_STAT0_SIGSTATE)) return 5; if (!(*((vu16*)0x1000601c) & TMIO_STAT0_SIGSTATE)) return -1;
sdmmc_send_command(&handleSD, 0, 0); sdmmc_send_command(&handleSD,0,0);
sdmmc_send_command(&handleSD, 0x10408, 0x1AA); sdmmc_send_command(&handleSD,0x10408,0x1AA);
//u32 temp = (handleSD.ret[0] == 0x1AA) << 0x1E;
u32 temp = (handleSD.error & 0x1) << 0x1E; u32 temp = (handleSD.error & 0x1) << 0x1E;
//int count = 0;
u32 temp2 = 0; u32 temp2 = 0;
do do {
{ do {
do sdmmc_send_command(&handleSD,0x10437,handleSD.initarg << 0x10);
{ sdmmc_send_command(&handleSD,0x10769,0x00FF8000 | temp);
sdmmc_send_command(&handleSD, 0x10437, handleSD.initarg << 0x10);
sdmmc_send_command(&handleSD, 0x10769, 0x00FF8000 | temp);
temp2 = 1; temp2 = 1;
} } while ( !(handleSD.error & 1) );
while(!(handleSD.error & 1));
} } while((handleSD.ret[0] & 0x80000000) == 0);
while((handleSD.ret[0] & 0x80000000) == 0);
if(!((handleSD.ret[0] >> 30) & 1) || !temp) if(!((handleSD.ret[0] >> 30) & 1) || !temp)
temp2 = 0; temp2 = 0;
handleSD.isSDHC = temp2; handleSD.isSDHC = temp2;
sdmmc_send_command(&handleSD, 0x10602, 0); sdmmc_send_command(&handleSD,0x10602,0);
if((handleSD.error & 0x4)) return -1; if (handleSD.error & 0x4) return -1;
sdmmc_send_command(&handleSD, 0x10403, 0); sdmmc_send_command(&handleSD,0x10403,0);
if((handleSD.error & 0x4)) return -2; if (handleSD.error & 0x4) return -1;
handleSD.initarg = handleSD.ret[0] >> 0x10; handleSD.initarg = handleSD.ret[0] >> 0x10;
sdmmc_send_command(&handleSD, 0x10609, handleSD.initarg << 0x10); sdmmc_send_command(&handleSD,0x10609,handleSD.initarg << 0x10);
if((handleSD.error & 0x4)) return -3; if (handleSD.error & 0x4) return -1;
handleSD.total_size = calcSDSize((u8*)&handleSD.ret[0], -1); handleSD.total_size = calcSDSize((u8*)&handleSD.ret[0],-1);
handleSD.clk = 1; handleSD.clk = 1;
setckl(1); setckl(1);
sdmmc_send_command(&handleSD, 0x10507, handleSD.initarg << 0x10); sdmmc_send_command(&handleSD,0x10507,handleSD.initarg << 0x10);
if((handleSD.error & 0x4)) return -4; if (handleSD.error & 0x4) return -1;
sdmmc_send_command(&handleSD, 0x10437, handleSD.initarg << 0x10); sdmmc_send_command(&handleSD,0x10437,handleSD.initarg << 0x10);
if((handleSD.error & 0x4)) return -5; if (handleSD.error & 0x4) return -1;
handleSD.SDOPT = 1; handleSD.SDOPT = 1;
sdmmc_send_command(&handleSD, 0x10446, 0x2); sdmmc_send_command(&handleSD,0x10446,0x2);
if((handleSD.error & 0x4)) return -6; if (handleSD.error & 0x4) return -1;
sdmmc_send_command(&handleSD, 0x1040D, handleSD.initarg << 0x10); sdmmc_send_command(&handleSD,0x1040D,handleSD.initarg << 0x10);
if((handleSD.error & 0x4)) return -7; if (handleSD.error & 0x4) return -1;
sdmmc_send_command(&handleSD, 0x10410, 0x200); sdmmc_send_command(&handleSD,0x10410,0x200);
if((handleSD.error & 0x4)) return -8; if (handleSD.error & 0x4) return -1;
handleSD.clk |= 0x200; handleSD.clk |= 0x200;
return 0; return 0;
} }
void sdmmc_get_cid(bool isNand, u32 *info) int sdmmc_sdcard_init()
{
struct mmcdevice *device = isNand ? &handleNAND : &handleSD;
inittarget(device);
// use cmd7 to put sd card in standby mode
// CMD7
sdmmc_send_command(device, 0x10507, 0);
// get sd card info
// use cmd10 to read CID
sdmmc_send_command(device, 0x1060A, device->initarg << 0x10);
for(int i = 0; i < 4; ++i)
info[i] = device->ret[i];
// put sd card back to transfer mode
// CMD7
sdmmc_send_command(device, 0x10507, device->initarg << 0x10);
}
bool sdmmc_sdcard_init(bool initSd, bool initNand)
{ {
InitSD(); InitSD();
int ret = 0; int result = Nand_Init();
if(initNand) ret |= Nand_Init(); return result | SD_Init();
if(initSd) ret |= SD_Init(); }
return ret == 0;
}
int sdmmc_get_cid( int isNand, uint32_t *info)
{
struct mmcdevice *device;
if(isNand)
device = &handleNAND;
else
device = &handleSD;
inittarget(device);
// use cmd7 to put sd card in standby mode
// CMD7
{
sdmmc_send_command(device,0x10507,0);
//if((device->error & 0x4)) return -1;
}
// get sd card info
// use cmd10 to read CID
{
sdmmc_send_command(device,0x1060A,device->initarg << 0x10);
//if((device->error & 0x4)) return -2;
for( int i = 0; i < 4; ++i ) {
info[i] = device->ret[i];
}
}
// put sd card back to transfer mode
// CMD7
{
sdmmc_send_command(device,0x10507,device->initarg << 0x10);
//if((device->error & 0x4)) return -3;
}
if(isNand)
{
inittarget(&handleSD);
}
return 0;
}

View File

@ -1,48 +1,52 @@
// Copyright 2014 Normmatt
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#pragma once #pragma once
#include "../../types.h" #include "common.h"
#define SDMMC_BASE 0x10006000 #define SDMMC_BASE 0x10006000u
#define REG_SDCMD 0x00 #define REG_SDCMD 0x00
#define REG_SDPORTSEL 0x02 #define REG_SDPORTSEL 0x02
#define REG_SDCMDARG 0x04 #define REG_SDCMDARG 0x04
#define REG_SDCMDARG0 0x04 #define REG_SDCMDARG0 0x04
#define REG_SDCMDARG1 0x06 #define REG_SDCMDARG1 0x06
#define REG_SDSTOP 0x08 #define REG_SDSTOP 0x08
#define REG_SDBLKCOUNT 0x0A #define REG_SDBLKCOUNT 0x0a
#define REG_SDRESP0 0x0C #define REG_SDRESP0 0x0c
#define REG_SDRESP1 0x0E #define REG_SDRESP1 0x0e
#define REG_SDRESP2 0x10 #define REG_SDRESP2 0x10
#define REG_SDRESP3 0x12 #define REG_SDRESP3 0x12
#define REG_SDRESP4 0x14 #define REG_SDRESP4 0x14
#define REG_SDRESP5 0x16 #define REG_SDRESP5 0x16
#define REG_SDRESP6 0x18 #define REG_SDRESP6 0x18
#define REG_SDRESP7 0x1A #define REG_SDRESP7 0x1a
#define REG_SDSTATUS0 0x1C #define REG_SDSTATUS0 0x1c
#define REG_SDSTATUS1 0x1E #define REG_SDSTATUS1 0x1e
#define REG_SDIRMASK0 0x20 #define REG_SDIRMASK0 0x20
#define REG_SDIRMASK1 0x22 #define REG_SDIRMASK1 0x22
#define REG_SDCLKCTL 0x24 #define REG_SDCLKCTL 0x24
#define REG_SDBLKLEN 0x26 #define REG_SDBLKLEN 0x26
#define REG_SDOPT 0x28 #define REG_SDOPT 0x28
#define REG_SDFIFO 0x30 #define REG_SDFIFO 0x30
#define REG_DATACTL 0xD8 #define REG_SDDATACTL 0xd8
#define REG_SDRESET 0xE0 #define REG_SDRESET 0xe0
#define REG_SDPROTECTED 0xF6 //bit 0 determines if sd is protected or not? #define REG_SDPROTECTED 0xf6 //bit 0 determines if sd is protected or not?
#define REG_DATACTL32 0x100 #define REG_SDDATACTL32 0x100
#define REG_SDBLKLEN32 0x104 #define REG_SDBLKLEN32 0x104
#define REG_SDBLKCOUNT32 0x108 #define REG_SDBLKCOUNT32 0x108
#define REG_SDFIFO32 0x10C #define REG_SDFIFO32 0x10C
#define REG_CLK_AND_WAIT_CTL 0x138 #define REG_CLK_AND_WAIT_CTL 0x138
#define REG_RESET_SDIO 0x1E0 #define REG_RESET_SDIO 0x1e0
#define TMIO_STAT0_CMDRESPEND 0x0001 #define TMIO_STAT0_CMDRESPEND 0x0001
#define TMIO_STAT0_DATAEND 0x0004 #define TMIO_STAT0_DATAEND 0x0004
@ -66,7 +70,31 @@
#define TMIO_STAT1_CMD_BUSY 0x4000 #define TMIO_STAT1_CMD_BUSY 0x4000
#define TMIO_STAT1_ILL_ACCESS 0x8000 #define TMIO_STAT1_ILL_ACCESS 0x8000
#define TMIO_MASK_ALL 0x837F031D //Comes from TWLSDK mongoose.tef DWARF info
#define SDMC_NORMAL 0x00000000
#define SDMC_ERR_COMMAND 0x00000001
#define SDMC_ERR_CRC 0x00000002
#define SDMC_ERR_END 0x00000004
#define SDMC_ERR_TIMEOUT 0x00000008
#define SDMC_ERR_FIFO_OVF 0x00000010
#define SDMC_ERR_FIFO_UDF 0x00000020
#define SDMC_ERR_WP 0x00000040
#define SDMC_ERR_ABORT 0x00000080
#define SDMC_ERR_FPGA_TIMEOUT 0x00000100
#define SDMC_ERR_PARAM 0x00000200
#define SDMC_ERR_R1_STATUS 0x00000800
#define SDMC_ERR_NUM_WR_SECTORS 0x00001000
#define SDMC_ERR_RESET 0x00002000
#define SDMC_ERR_ILA 0x00004000
#define SDMC_ERR_INFO_DETECT 0x00008000
#define SDMC_STAT_ERR_UNKNOWN 0x00080000
#define SDMC_STAT_ERR_CC 0x00100000
#define SDMC_STAT_ERR_ECC_FAILED 0x00200000
#define SDMC_STAT_ERR_CRC 0x00800000
#define SDMC_STAT_ERR_OTHER 0xf9c70008
#define TMIO_MASK_ALL 0x837f031d
#define TMIO_MASK_GW (TMIO_STAT1_ILL_ACCESS | TMIO_STAT1_CMDTIMEOUT | TMIO_STAT1_TXUNDERRUN | TMIO_STAT1_RXOVERFLOW | \ #define TMIO_MASK_GW (TMIO_STAT1_ILL_ACCESS | TMIO_STAT1_CMDTIMEOUT | TMIO_STAT1_TXUNDERRUN | TMIO_STAT1_RXOVERFLOW | \
TMIO_STAT1_DATATIMEOUT | TMIO_STAT1_STOPBIT_ERR | TMIO_STAT1_CRCFAIL | TMIO_STAT1_CMD_IDX_ERR) TMIO_STAT1_DATATIMEOUT | TMIO_STAT1_STOPBIT_ERR | TMIO_STAT1_CRCFAIL | TMIO_STAT1_CMD_IDX_ERR)
@ -75,8 +103,7 @@
#define TMIO_MASK_WRITEOP (TMIO_STAT1_TXRQ | TMIO_STAT1_DATAEND) #define TMIO_MASK_WRITEOP (TMIO_STAT1_TXRQ | TMIO_STAT1_DATAEND)
typedef struct mmcdevice { typedef struct mmcdevice {
u8 *rData; vu8* data;
const u8 *tData;
u32 size; u32 size;
u32 error; u32 error;
u16 stat0; u16 stat0;
@ -91,10 +118,11 @@ typedef struct mmcdevice {
u32 res; u32 res;
} mmcdevice; } mmcdevice;
bool sdmmc_sdcard_init(bool initSd, bool initNand); int sdmmc_sdcard_init();
int sdmmc_sdcard_readsectors(u32 sector_no, u32 numsectors, u8 *out); u32 sdmmc_sdcard_readsectors(u32 sector_no, u32 numsectors, vu8 *out);
int sdmmc_sdcard_writesectors(u32 sector_no, u32 numsectors, const u8 *in); u32 sdmmc_sdcard_writesectors(u32 sector_no, u32 numsectors, vu8 *in);
int sdmmc_nand_readsectors(u32 sector_no, u32 numsectors, u8 *out); mmcdevice *getMMCDevice(int drive);
int sdmmc_nand_writesectors(u32 sector_no, u32 numsectors, const u8 *in); int sdmmc_get_cid( int isNand, uint32_t *info);
void sdmmc_get_cid(bool isNand, u32 *info);
//mmcdevice *getMMCDevice(int drive); u32 sdmmc_nand_readsectors(u32 sector_no, u32 numsectors, vu8 *out);
u32 sdmmc_nand_writesectors(u32 sector_no, u32 numsectors, vu8 *in);

View File

@ -1,141 +1,39 @@
/* /*
* This file is part of Luma3DS * fs.c
* Copyright (C) 2016 Aurora Wright, TuxSH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Additional Terms 7.b of GPLv3 applies to this file: Requiring preservation of specified
* reasonable legal notices or author attributions in that material or in the Appropriate Legal
* Notices displayed by works containing it.
*/ */
#include "fs.h" #include "fs.h"
#include "memory.h"
#include "strings.h"
#include "fatfs/ff.h" #include "fatfs/ff.h"
static FATFS fs; static FATFS fs;
bool mountFs(bool isSd) u32 mountSD(void){
{ if(f_mount(&fs, "0:", 1) != FR_OK) return 0;
return isSd ? f_mount(&fs, "0:", 1) == FR_OK : f_mount(&fs, "1:", 1) == FR_OK; return 1;
} }
u32 fileRead(void *dest, const char *path, u32 maxSize) u32 fileRead(u8 *dest, const char *path, u32 size){
{ FRESULT fr;
FIL file; FIL fp;
u32 ret = 0; unsigned int br = 0;
if(f_open(&file, path, FA_READ) != FR_OK) return ret; fr = f_open(&fp, path, FA_READ);
if(fr == FR_OK){
u32 size = f_size(&file); if(!size) size = f_size(&fp);
if(size <= maxSize) fr = f_read(&fp, dest, size, &br);
f_read(&file, dest, size, (unsigned int *)&ret);
f_close(&file);
return ret;
}
bool fileWrite(const void *buffer, const char *path, u32 size)
{
FIL file;
switch(f_open(&file, path, FA_WRITE | FA_OPEN_ALWAYS))
{
case FR_OK:
{
unsigned int written;
f_write(&file, buffer, size, &written);
f_truncate(&file);
f_close(&file);
return (u32)written == size;
}
case FR_NO_PATH:
for(u32 i = 1; path[i] != 0; i++)
if(path[i] == '/')
{
char folder[i + 1];
memcpy(folder, path, i);
folder[i] = 0;
f_mkdir(folder);
}
return fileWrite(buffer, path, size);
default:
return false;
}
}
void fileDelete(const char *path)
{
f_unlink(path);
}
void fileRename(const char *oldPath, const char *newPath)
{
f_rename(oldPath, newPath);
}
u32 firmRead(void *dest)
{
const char *firmFolders[] = {"00000002", "20000002"};
char path[48] = "1:/title/00040138/";
concatenateStrings(path, firmFolders[ISN3DS ? 1 : 0]);
concatenateStrings(path, "/content");
DIR dir;
u32 firmVersion = 0xFFFFFFFF,
ret = 0;
if(f_opendir(&dir, path) != FR_OK) goto exit;
FILINFO info;
//Parse the target directory
while(f_readdir(&dir, &info) == FR_OK && info.fname[0] != 0)
{
//Not a cxi
if(info.fname[9] != 'a' || strlen(info.fname) != 12) continue;
//Multiple cxis were found
if(firmVersion != 0xFFFFFFFF) ret = 1;
u32 tempVersion = hexAtoi(info.altname, 8);
//FIRM is newer than 11.3
if(!ISDEVUNIT && tempVersion > (ISN3DS ? 0x2D : 0x5C)) ret = 2;
//Found an older cxi
if(tempVersion < firmVersion) firmVersion = tempVersion;
} }
f_closedir(&dir); f_close(&fp);
return fr ? 0 : 1;
}
if(ret == 1 || firmVersion == 0xFFFFFFFF) goto exit; u32 fileSize(const char *path){
FIL fp;
u32 size = 0;
//Complete the string with the .app name if(f_open(&fp, path, FA_READ) == FR_OK)
concatenateStrings(path, "/00000000.app"); size = f_size(&fp);
//Convert back the .app name from integer to array f_close(&fp);
hexItoa(firmVersion, path + 35, 8); return size;
if(!fileRead(dest, path, 0x100000)) ret = 3;
exit:
if(firmVersion == 0xFFFFFFFF) ret = 4;
return ret;
} }

View File

@ -1,32 +1,11 @@
/* /*
* This file is part of Luma3DS * fs.h
* Copyright (C) 2016 Aurora Wright, TuxSH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Additional Terms 7.b of GPLv3 applies to this file: Requiring preservation of specified
* reasonable legal notices or author attributions in that material or in the Appropriate Legal
* Notices displayed by works containing it.
*/ */
#pragma once #pragma once
#include "types.h" #include "types.h"
bool mountFs(bool isSd); u32 mountSD(void);
u32 fileRead(void *dest, const char *path, u32 maxSize); u32 fileRead(u8 *dest, const char *path, u32 size);
bool fileWrite(const void *buffer, const char *path, u32 size); u32 fileSize(const char *path);
void fileDelete(const char *path);
void fileRename(const char *oldPath, const char *newPath);
u32 firmRead(void *dest);

View File

@ -10,58 +10,50 @@ static const struct { u8 bus_id, reg_addr; } dev_data[] = {
{2, 0xA4}, {2, 0x9A}, {2, 0xA0}, {2, 0xA4}, {2, 0x9A}, {2, 0xA0},
}; };
static inline u8 i2cGetDeviceBusId(u8 device_id) inline u8 i2cGetDeviceBusId(u8 device_id) {
{
return dev_data[device_id].bus_id; return dev_data[device_id].bus_id;
} }
static inline u8 i2cGetDeviceRegAddr(u8 device_id) inline u8 i2cGetDeviceRegAddr(u8 device_id) {
{
return dev_data[device_id].reg_addr; return dev_data[device_id].reg_addr;
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
static vu8 *reg_data_addrs[] = { static vu8* reg_data_addrs[] = {
(vu8 *)(I2C1_REG_OFF + I2C_REG_DATA), (vu8*)(I2C1_REG_OFF + I2C_REG_DATA),
(vu8 *)(I2C2_REG_OFF + I2C_REG_DATA), (vu8*)(I2C2_REG_OFF + I2C_REG_DATA),
(vu8 *)(I2C3_REG_OFF + I2C_REG_DATA), (vu8*)(I2C3_REG_OFF + I2C_REG_DATA),
}; };
static inline vu8 *i2cGetDataReg(u8 bus_id) inline vu8* i2cGetDataReg(u8 bus_id) {
{
return reg_data_addrs[bus_id]; return reg_data_addrs[bus_id];
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
static vu8 *reg_cnt_addrs[] = { static vu8* reg_cnt_addrs[] = {
(vu8 *)(I2C1_REG_OFF + I2C_REG_CNT), (vu8*)(I2C1_REG_OFF + I2C_REG_CNT),
(vu8 *)(I2C2_REG_OFF + I2C_REG_CNT), (vu8*)(I2C2_REG_OFF + I2C_REG_CNT),
(vu8 *)(I2C3_REG_OFF + I2C_REG_CNT), (vu8*)(I2C3_REG_OFF + I2C_REG_CNT),
}; };
static inline vu8 *i2cGetCntReg(u8 bus_id) inline vu8* i2cGetCntReg(u8 bus_id) {
{
return reg_cnt_addrs[bus_id]; return reg_cnt_addrs[bus_id];
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
static inline void i2cWaitBusy(u8 bus_id) inline void i2cWaitBusy(u8 bus_id) {
{
while (*i2cGetCntReg(bus_id) & 0x80); while (*i2cGetCntReg(bus_id) & 0x80);
} }
static inline bool i2cGetResult(u8 bus_id) inline bool i2cGetResult(u8 bus_id) {
{
i2cWaitBusy(bus_id); i2cWaitBusy(bus_id);
return (*i2cGetCntReg(bus_id) >> 4) & 1; return (*i2cGetCntReg(bus_id) >> 4) & 1;
} }
static void i2cStop(u8 bus_id, u8 arg0) void i2cStop(u8 bus_id, u8 arg0) {
{
*i2cGetCntReg(bus_id) = (arg0 << 5) | 0xC0; *i2cGetCntReg(bus_id) = (arg0 << 5) | 0xC0;
i2cWaitBusy(bus_id); i2cWaitBusy(bus_id);
*i2cGetCntReg(bus_id) = 0xC5; *i2cGetCntReg(bus_id) = 0xC5;
@ -69,45 +61,38 @@ static void i2cStop(u8 bus_id, u8 arg0)
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
static bool i2cSelectDevice(u8 bus_id, u8 dev_reg) bool i2cSelectDevice(u8 bus_id, u8 dev_reg) {
{
i2cWaitBusy(bus_id); i2cWaitBusy(bus_id);
*i2cGetDataReg(bus_id) = dev_reg; *i2cGetDataReg(bus_id) = dev_reg;
*i2cGetCntReg(bus_id) = 0xC2; *i2cGetCntReg(bus_id) = 0xC2;
return i2cGetResult(bus_id); return i2cGetResult(bus_id);
} }
static bool i2cSelectRegister(u8 bus_id, u8 reg) bool i2cSelectRegister(u8 bus_id, u8 reg) {
{
i2cWaitBusy(bus_id); i2cWaitBusy(bus_id);
*i2cGetDataReg(bus_id) = reg; *i2cGetDataReg(bus_id) = reg;
*i2cGetCntReg(bus_id) = 0xC0; *i2cGetCntReg(bus_id) = 0xC0;
return i2cGetResult(bus_id); return i2cGetResult(bus_id);
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
bool i2cWriteRegister(u8 dev_id, u8 reg, u8 data) bool i2cWriteRegister(u8 dev_id, u8 reg, u8 data) {
{ u8 bus_id = i2cGetDeviceBusId(dev_id);
u8 bus_id = i2cGetDeviceBusId(dev_id), u8 dev_addr = i2cGetDeviceRegAddr(dev_id);
dev_addr = i2cGetDeviceRegAddr(dev_id);
for(u32 i = 0; i < 8; i++) for (int i = 0; i < 8; i++) {
{ if (i2cSelectDevice(bus_id, dev_addr) && i2cSelectRegister(bus_id, reg)) {
if(i2cSelectDevice(bus_id, dev_addr) && i2cSelectRegister(bus_id, reg))
{
i2cWaitBusy(bus_id); i2cWaitBusy(bus_id);
*i2cGetDataReg(bus_id) = data; *i2cGetDataReg(bus_id) = data;
*i2cGetCntReg(bus_id) = 0xC1; *i2cGetCntReg(bus_id) = 0xC1;
i2cStop(bus_id, 0); i2cStop(bus_id, 0);
if (i2cGetResult(bus_id))
if(i2cGetResult(bus_id)) return true; return true;
} }
*i2cGetCntReg(bus_id) = 0xC5; *i2cGetCntReg(bus_id) = 0xC5;
i2cWaitBusy(bus_id); i2cWaitBusy(bus_id);
} }
return false; return false;
} }

View File

@ -15,4 +15,18 @@
#define I2C_DEV_GYRO 10 #define I2C_DEV_GYRO 10
#define I2C_DEV_IR 13 #define I2C_DEV_IR 13
u8 i2cGetDeviceBusId(u8 device_id);
u8 i2cGetDeviceRegAddr(u8 device_id);
vu8* i2cGetDataReg(u8 bus_id);
vu8* i2cGetCntReg(u8 bus_id);
void i2cWaitBusy(u8 bus_id);
bool i2cGetResult(u8 bus_id);
u8 i2cGetData(u8 bus_id);
void i2cStop(u8 bus_id, u8 arg0);
bool i2cSelectDevice(u8 bus_id, u8 dev_reg);
bool i2cSelectRegister(u8 bus_id, u8 reg);
bool i2cWriteRegister(u8 dev_id, u8 reg, u8 data); bool i2cWriteRegister(u8 dev_id, u8 reg, u8 data);

View File

@ -1,367 +1,118 @@
/* #include "installer.h"
* installer.c
*/
#include "memory.h" #include "memory.h"
#include "fs.h" #include "fs.h"
#include "crypto.h" #include "crypto.h"
#include "screen.h"
#include "draw.h" #include "draw.h"
#include "utils.h" #include "utils.h"
#include "types.h"
#include "installer.h"
#include "fatfs/sdmmc/sdmmc.h" #include "fatfs/sdmmc/sdmmc.h"
static const u8 sectorHashRetail[SHA_256_HASH_SIZE] = { static const u8 sectorHash[0x20] = {
0x82, 0xF2, 0x73, 0x0D, 0x2C, 0x2D, 0xA3, 0xF3, 0x01, 0x65, 0xF9, 0x87, 0xFD, 0xCC, 0xAC, 0x5C, 0x82, 0xF2, 0x73, 0x0D, 0x2C, 0x2D, 0xA3, 0xF3, 0x01, 0x65, 0xF9, 0x87, 0xFD, 0xCC, 0xAC, 0x5C,
0xBA, 0xB2, 0x4B, 0x4E, 0x5F, 0x65, 0xC9, 0x81, 0xCD, 0x7B, 0xE6, 0xF4, 0x38, 0xE6, 0xD9, 0xD3 0xBA, 0xB2, 0x4B, 0x4E, 0x5F, 0x65, 0xC9, 0x81, 0xCD, 0x7B, 0xE6, 0xF4, 0x38, 0xE6, 0xD9, 0xD3
},
firm0HashRetail[SHA_256_HASH_SIZE] = {
0x6E, 0x4D, 0x14, 0xAD, 0x51, 0x50, 0xA5, 0x9A, 0x87, 0x59, 0x62, 0xB7, 0x09, 0x0A, 0x3C, 0x74,
0x4F, 0x72, 0x4B, 0xBD, 0x97, 0x39, 0x33, 0xF2, 0x11, 0xC9, 0x35, 0x22, 0xC8, 0xBB, 0x1C, 0x7D
},
firm0A9lhHashRetail[SHA_256_HASH_SIZE] = {
0x79, 0x3D, 0x35, 0x7B, 0x8F, 0xF1, 0xFC, 0xF0, 0x8F, 0xB6, 0xDB, 0x51, 0x31, 0xD4, 0xA7, 0x74,
0x8E, 0xF0, 0x4A, 0xB1, 0xA6, 0x7F, 0xCD, 0xAB, 0x0C, 0x0A, 0xC0, 0x69, 0xA7, 0x9D, 0xC5, 0x04
},
firm090A9lhHash[SHA_256_HASH_SIZE] = {
0x68, 0x52, 0xCC, 0x21, 0x89, 0xAE, 0x28, 0x38, 0x1A, 0x75, 0x90, 0xE7, 0x38, 0x23, 0x48, 0x41,
0x8E, 0x80, 0x78, 0x75, 0x27, 0x64, 0x04, 0xD6, 0x28, 0xD6, 0xFA, 0x39, 0xA8, 0x6F, 0xB0, 0x3F
},
firm1HashRetail[SHA_256_HASH_SIZE] = {
0xD8, 0x2D, 0xB7, 0xB4, 0x38, 0x2B, 0x07, 0x88, 0x99, 0x77, 0x91, 0x0C, 0xC6, 0xEC, 0x6D, 0x87,
0x7D, 0x21, 0x79, 0x23, 0xD7, 0x60, 0xAF, 0x4E, 0x8B, 0x3A, 0xAB, 0xB2, 0x63, 0xE4, 0x21, 0xC6
},
sectorHashDev[SHA_256_HASH_SIZE] = {
0xB2, 0x91, 0xD9, 0xB1, 0x33, 0x05, 0x79, 0x0D, 0x47, 0xC6, 0x06, 0x98, 0x4C, 0x67, 0xC3, 0x70,
0x09, 0x54, 0xE3, 0x85, 0xDE, 0x47, 0x55, 0xAF, 0xC6, 0xCB, 0x1D, 0x8D, 0xC7, 0x84, 0x5A, 0x64
},
firm0HashDev[SHA_256_HASH_SIZE] = {
0xCD, 0x62, 0xA6, 0x58, 0x40, 0x1B, 0x8B, 0x8F, 0xD3, 0x2C, 0x72, 0x58, 0xD8, 0x24, 0x21, 0x36,
0xCF, 0x83, 0x40, 0xA3, 0x34, 0x8E, 0xED, 0x33, 0x0A, 0x1A, 0x16, 0x04, 0x49, 0xC9, 0x74, 0x3E
},
firm0A9lhHashDev[SHA_256_HASH_SIZE] = {
0x60, 0xBB, 0xD1, 0x35, 0x44, 0x2F, 0xBD, 0x47, 0x69, 0xBF, 0x36, 0x4B, 0x0B, 0x79, 0x6E, 0x4C,
0xE1, 0xB2, 0xDB, 0x7A, 0xAD, 0xF0, 0x04, 0x31, 0xCB, 0xBD, 0x54, 0xD3, 0x99, 0x8C, 0x9C, 0xD2
},
firm1HashDev[SHA_256_HASH_SIZE] = {
0xCD, 0x87, 0x85, 0x33, 0x76, 0xCA, 0x2A, 0x3F, 0xFC, 0x24, 0x4C, 0x29, 0x95, 0x8B, 0xA8, 0x34,
0xF2, 0x38, 0x14, 0x58, 0x10, 0x83, 0x56, 0x4F, 0x0D, 0x5A, 0xDB, 0x29, 0x12, 0xD8, 0xA9, 0x84
}; };
u32 posY; static const u8 firm0Hash[0x20] = {
0xD7, 0xBE, 0x76, 0xE1, 0x81, 0x3F, 0x39, 0x8D, 0xCE, 0xA8, 0x55, 0x72, 0xD0, 0xC0, 0x58, 0xF7,
0x95, 0x47, 0x61, 0xA1, 0xD5, 0xEA, 0x03, 0xB5, 0xEB, 0x50, 0x47, 0xAC, 0x63, 0xAC, 0x5D, 0x6B
};
static void drawTitle(void) static const u8 firm1Hash[0x20] = {
{ 0xD2, 0x53, 0xC1, 0xCC, 0x0A, 0x5F, 0xFA, 0xC6, 0xB3, 0x83, 0xDA, 0xC1, 0x82, 0x7C, 0xFB, 0x3B,
initScreens(); 0x2D, 0x3D, 0x56, 0x6C, 0x6A, 0x1A, 0x8E, 0x52, 0x54, 0xE3, 0x89, 0xC2, 0x95, 0x06, 0x23, 0xE5
};
posY = drawString(TITLE, 10, 10, COLOR_TITLE); static void installStage2(u32 mode, int pos_y){
posY = drawString("Thanks to delebile, #cakey and StandardBus", 10, posY + SPACING_Y, COLOR_WHITE); //Read stage2
char *path = "a9lh/payload_stage2.bin";
u32 size = fileSize(path);
if(!size || size > MAXSTAGE2SIZE)
shutdown(1, pos_y, "Error: stage2 doesn't exist or exceeds max size");
memset((u8 *)STAGE2OFFSET, 0, MAXSTAGE2SIZE);
fileRead((u8 *)STAGE2OFFSET, path, size);
if(mode) return;
sdmmc_nand_writesectors(0x5C000, 0x20, (vu8 *)STAGE2OFFSET);
shutdown(1, pos_y, "Success!");
} }
void main(void) void installer(void){
{ drawString("Safe A9LH Installer v1.0", 10, 10, 0x0000FF);
bool isOtpless = ISA9LH && *(vu32 *)0x80FD0FC == 0xEAFE4AA3 && magic == 0xDEADCAFE; int pos_y = drawString("Thanks to delebile, #cakey and StandardBus", 10, 40, 0xFFFFFF);
pos_y = drawString("Press SELECT for a full install", 10, pos_y + SPACING_VERT, 0xFFFFFF);
pos_y = drawString("Press START to only update stage2", 10, pos_y, 0xFFFFFF);
pos_y = drawString("Press any other button to shutdown", 10, pos_y, 0xFFFFFF);
if(!isOtpless) drawTitle(); u16 pressed = waitInput();
if(pressed == BUTTON_START) installStage2(0, pos_y);
if(pressed != BUTTON_SELECT) shutdown(0, 0, NULL);
if(!sdmmc_sdcard_init(!isOtpless, true) && !isOtpless) //Determine if booting with A9LH
shutdown(1, "Error: failed to initialize SD and NAND"); u32 a9lhBoot = (PDN_SPI_CNT == 0x0) ? 1 : 0;
//Detect the console being used
u32 pressed; u32 console = (PDN_MPCORE_CFG == 1) ? 0 : 1;
if(!isOtpless)
{
posY = drawString(ISA9LH ? "Press SELECT to update A9LH, START to uninstall" : "Press SELECT for a full install", 10, posY + SPACING_Y, COLOR_WHITE);
posY = drawString("Press any other button to shutdown", 10, posY, COLOR_WHITE);
pressed = waitInput();
}
if(isOtpless || pressed == BUTTON_SELECT) installer(isOtpless);
if(pressed == BUTTON_START && ISA9LH) uninstaller();
shutdown(0, NULL);
}
static inline void installer(bool isOtpless)
{
bool updateKey2 = false,
updateFirm0 = false,
updateFirm1 = false;
u8 otp[256] = {0},
keySector[512];
if(!isOtpless && !mountFs(true))
shutdown(1, "Error: failed to mount the SD card");
//If making a first install on O3DS, we need the OTP
if(!ISA9LH && (!ISN3DS || ISDEVUNIT))
{
const char otpPath[] = "a9lh/otp.bin";
//Prefer OTP from memory if available
if(memcmp((void *)OTP_FROM_MEM, otp, sizeof(otp)) == 0)
{
// Read OTP from file
if(fileRead(otp, otpPath, sizeof(otp)) != sizeof(otp))
shutdown(1, "Error: otp.bin doesn't exist and can't be dumped");
}
else
{
//Write OTP from memory to file
fileWrite((void *)OTP_FROM_MEM, otpPath, sizeof(otp));
memcpy(otp, (void *)OTP_FROM_MEM, sizeof(otp));
}
}
//Setup the key sector de/encryption with the SHA register or otp.bin
if(ISA9LH || !ISN3DS || ISDEVUNIT) setupKeyslot0x11(otp);
//Calculate the CTR for the 3DS partitions //Calculate the CTR for the 3DS partitions
getNandCtr(); getNandCTR();
//Get NAND FIRM0 and test that the CTR is correct //Test that the CTR is correct
if(!isOtpless) readFirm0((u8 *)TEMPOFFSET, 0x200);
{ if(memcmp((void *)TEMPOFFSET, "FIRM", 4) != 0)
readFirm0((u8 *)FIRM0_OFFSET, FIRM0_SIZE); shutdown(1, pos_y, "Error: couldn't setup NAND FIRM encryption");
if(memcmp((void *)FIRM0_OFFSET, "FIRM", 4) != 0)
shutdown(1, "Error: failed to setup FIRM encryption");
}
//If booting from A9LH or on N3DS, we can use the key sector from NAND //Read OTP
if(ISA9LH || ISN3DS) getSector(keySector); const char *path = "a9lh/otp.bin";
else if(fileSize(path) != 256)
{ shutdown(1, pos_y, "Error: otp.bin doesn't exist or has a wrong size");
//Read decrypted key sector fileRead((u8 *)OTPOFFSET, path, 256);
if(fileRead(keySector, "a9lh/secret_sector.bin", sizeof(keySector)) != sizeof(keySector))
shutdown(1, "Error: secret_sector.bin doesn't exist or has\na wrong size");
if(!verifyHash(keySector, sizeof(keySector), !ISDEVUNIT ? sectorHashRetail : sectorHashDev))
shutdown(1, "Error: secret_sector.bin is invalid or corrupted");
}
if(ISA9LH && !isOtpless) setupKeyslot0x11((u8 *)OTPOFFSET);
{ if((a9lhBoot && !testOtp(0)) || (!a9lhBoot && console && !testOtp(1)))
u32 i; shutdown(1, pos_y, "Error: your OTP is invalid or corrupted");
if(!ISDEVUNIT) //Read decrypted key sector
{ path = "a9lh/secret_sector.bin";
for(i = 1; i < 5; i++) if(fileSize(path) != 0x200)
if(memcmp(keySector + AES_BLOCK_SIZE, key2s[i], AES_BLOCK_SIZE) == 0) break; shutdown(1, pos_y, "Error: secret_sector.bin doesn't exist or has a wrong size");
} fileRead((u8 *)SECTOROFFSET, path, 0x200);
else i = memcmp(keySector + AES_BLOCK_SIZE, devKey2s[1], AES_BLOCK_SIZE) == 0 ? 1 : 5; if(!verifyHash((u8 *)SECTOROFFSET, 0x200, sectorHash))
shutdown(1, pos_y, "Error: secret_sector is invalid");
switch(i) //Generate and encrypt a per-console A9LH key sector
{ generateSector((void *)SECTOROFFSET);
case 5:
shutdown(1, "Error: the OTP hash or the NAND key sector\nare invalid");
break;
case 2:
case 3:
case 4:
updateFirm1 = true;
updateKey2 = true;
break;
case 1:
break;
}
if(!verifyHash((void *)FIRM0_OFFSET, SECTION2_POSITION, !ISDEVUNIT ? firm0A9lhHashRetail : firm0A9lhHashDev)) //Read FIRM0
{ path = "a9lh/firm0.bin";
if(ISDEVUNIT || !verifyHash((void *)FIRM0_OFFSET, SECTION2_POSITION, firm090A9lhHash)) u32 firm0Size = fileSize(path);
shutdown(1, "Error: NAND FIRM0 is invalid"); if(!firm0Size)
shutdown(1, pos_y, "Error: firm0.bin doesn't exist");
fileRead((u8 *)FIRM0OFFSET, path, firm0Size);
if(!verifyHash((u8 *)FIRM0OFFSET, firm0Size, firm0Hash))
shutdown(1, pos_y, "Error: firmo is invalid");
updateFirm0 = true; //Read FIRM1
} path = "a9lh/firm1.bin";
} u32 firm1Size = fileSize(path);
if(!firm1Size)
shutdown(1, pos_y, "Error: firm1.bin doesn't exist");
fileRead((u8 *)FIRM1OFFSET, path, firm1Size);
if(!verifyHash((u8 *)FIRM1OFFSET, firm1Size, firm1Hash))
shutdown(1, pos_y, "Error: firm1 is invalid");
if(!ISA9LH || updateKey2 || isOtpless) generateSector(keySector, (!ISA9LH && ISN3DS && !ISDEVUNIT) ? 1 : 0); //Inject stage1
path = "a9lh/payload_stage1.bin";
u32 size = fileSize(path);
if(!size || size > MAXSTAGE1SIZE)
shutdown(1, pos_y, "Error: stage1 doesn't exist or exceeds max size");
fileRead((u8 *)STAGE1OFFSET, path, size);
if(!ISA9LH || updateFirm0) installStage2(1, pos_y);
{
//Read FIRM0
if(fileRead((void *)FIRM0_OFFSET, "a9lh/firm0.bin", FIRM0_SIZE) != FIRM0_SIZE)
shutdown(1, "Error: firm0.bin doesn't exist or has a wrong size");
if(!verifyHash((void *)FIRM0_OFFSET, FIRM0_SIZE, !ISDEVUNIT ? firm0HashRetail : firm0HashDev))
shutdown(1, "Error: firm0.bin is invalid or corrupted");
}
if(!ISA9LH || updateFirm1) pos_y = drawString("All checks passed, installing...", 10, pos_y + SPACING_VERT, 0xFFFFFF);
{
//Read FIRM1
if(fileRead((void *)FIRM1_OFFSET, "a9lh/firm1.bin", FIRM1_SIZE) != FIRM1_SIZE)
shutdown(1, "Error: firm1.bin doesn't exist or has a wrong size");
if(!verifyHash((void *)FIRM1_OFFSET, FIRM1_SIZE, !ISDEVUNIT ? firm1HashRetail : firm1HashDev))
shutdown(1, "Error: firm1.bin is invalid or corrupted");
}
if(!ISA9LH && ISN3DS && !ISDEVUNIT)
{
magic = 0xDEADCAFE;
fileRename("arm9loaderhax.bin", "arm9loaderhax.bak");
if(!fileWrite((void *)0x23F00000, "arm9loaderhax.bin", 0x10000))
shutdown(1, "Error: couldn't write arm9loaderhax.bin");
}
if(!isOtpless)
{
bool missingStage1Hash,
missingStage2Hash;
u8 stageHash[SHA_256_HASH_SIZE];
//Inject stage1
memset32((void *)STAGE1_OFFSET, 0, MAX_STAGE1_SIZE);
u32 stageSize = fileRead((void *)STAGE1_OFFSET, "a9lh/payload_stage1.bin", MAX_STAGE1_SIZE);
if(!stageSize)
shutdown(1, "Error: payload_stage1.bin doesn't exist or\nexceeds max size");
const u8 zeroes[688] = {0};
if(memcmp(zeroes, (void *)STAGE1_OFFSET, 688) == 0)
shutdown(1, "Error: the payload_stage1.bin you're attempting\nto install is not compatible");
//Verify stage1
if(fileRead(stageHash, "a9lh/payload_stage1.bin.sha", sizeof(stageHash)) == sizeof(stageHash))
{
if(!verifyHash((void *)STAGE1_OFFSET, stageSize, stageHash))
shutdown(1, "Error: payload_stage1.bin is invalid\nor corrupted");
missingStage1Hash = false;
}
else missingStage1Hash = true;
//Read stage2
memset32((void *)STAGE2_OFFSET, 0, MAX_STAGE2_SIZE);
stageSize = fileRead((void *)STAGE2_OFFSET, "a9lh/payload_stage2.bin", MAX_STAGE2_SIZE);
if(!stageSize)
shutdown(1, "Error: payload_stage2.bin doesn't exist or\nexceeds max size");
//Verify stage2
if(fileRead(stageHash, "a9lh/payload_stage2.bin.sha", sizeof(stageHash)) == sizeof(stageHash))
{
if(!verifyHash((void *)STAGE2_OFFSET, stageSize, stageHash))
shutdown(1, "Error: payload_stage2.bin is invalid\nor corrupted");
missingStage2Hash = false;
}
else missingStage2Hash = true;
if(missingStage1Hash || missingStage2Hash)
{
posY = drawString("Couldn't verify stage1 and/or stage2 integrity!", 10, posY + SPACING_Y, COLOR_RED);
posY = drawString("Continuing might be dangerous!", 10, posY, COLOR_RED);
inputSequence();
}
posY = drawString("All checks passed, installing...", 10, posY + SPACING_Y, COLOR_WHITE);
sdmmc_nand_writesectors(0x5C000, MAX_STAGE2_SIZE / 0x200, (u8 *)STAGE2_OFFSET);
}
if(!ISA9LH || updateFirm1) writeFirm((u8 *)FIRM1_OFFSET, true, FIRM1_SIZE);
if(!ISA9LH || updateKey2 || isOtpless) sdmmc_nand_writesectors(0x96, 1, keySector);
if(!isOtpless) writeFirm((u8 *)FIRM0_OFFSET, false, FIRM0_SIZE);
else
{
*(vu32 *)0x80FD0FC = 0;
drawTitle();
if(sdmmc_sdcard_init(true, false) && mountFs(true))
{
fileDelete("arm9loaderhax.bin");
fileRename("arm9loaderhax.bak", "arm9loaderhax.bin");
}
else
{
posY = drawString("Couldn't remove arm9loaderhax.bin!", 10, posY + SPACING_Y, COLOR_RED);
posY = drawString("Do it yourself after the install ends", 10, posY, COLOR_RED);
}
}
if(!ISA9LH && ISN3DS && !ISDEVUNIT)
{
*(vu32 *)0x80FD0FC = 0xEAFE4AA3;
mcuReboot();
}
shutdown(2, ISA9LH && !isOtpless ? "Update: success!" : "Full install: success!");
}
static inline void uninstaller(void)
{
u8 keySector[512];
//New 3DSes need a key sector with a proper key2, Old 3DSes have a blank key sector
if(ISN3DS)
{
setupKeyslot0x11(NULL);
getSector(keySector);
u32 i;
if(!ISDEVUNIT)
{
for(i = 1; i < 5; i++)
if(memcmp(keySector + AES_BLOCK_SIZE, key2s[i], AES_BLOCK_SIZE) == 0) break;
}
else i = memcmp(keySector + AES_BLOCK_SIZE, devKey2s[1], AES_BLOCK_SIZE) == 0 ? 1 : 5;
if(i == 5) shutdown(1, "Error: the OTP hash or the NAND key sector\nare invalid");
generateSector(keySector, 2);
}
else memset32(keySector, 0, sizeof(keySector));
if(!mountFs(false))
shutdown(1, "Error: failed to mount CTRNAND");
//Read FIRM cxi from CTRNAND
u32 firmSize = 0,
result = firmRead((void *)FIRM0_OFFSET);
switch(result)
{
case 1:
shutdown(1, "Error: more than one FIRM has been detected");
break;
case 2:
posY = drawString("A FIRM newer than 11.3 has been detected!", 10, posY + SPACING_Y, COLOR_RED);
posY = drawString("You are about to uninstall A9LH!", 10, posY + SPACING_Y, COLOR_RED);
posY = drawString("To reinstall you'll need hardmod + NAND backup!", 10, posY, COLOR_RED);
break;
case 3:
shutdown(1, "Error: the CTRNAND FIRM is too large");
break;
case 4:
shutdown(1, "Error: couldn't read FIRM from CTRNAND");
break;
default:
posY = drawString("You are about to uninstall A9LH!", 10, posY + SPACING_Y, COLOR_RED);
posY = drawString("To reinstall you'll need 9.2 or lower!", 10, posY, COLOR_RED);
break;
}
inputSequence();
//Decrypt it and get its size
firmSize = decryptExeFs((Cxi *)FIRM0_OFFSET);
if(firmSize == 0) shutdown(1, "Error: couldn't decrypt the CTRNAND FIRM");
//writeFirm encrypts in-place, so we need two copies
memcpy((void *)FIRM1_OFFSET, (void *)FIRM0_OFFSET, firmSize);
//Zero out the stage2 space on NAND
memset32((void *)STAGE2_OFFSET, 0, MAX_STAGE2_SIZE);
posY = drawString("All checks passed, uninstalling...", 10, posY + SPACING_Y, COLOR_WHITE);
//Point of no return, install stuff in the safest order //Point of no return, install stuff in the safest order
sdmmc_nand_writesectors(0x96, 1, keySector); sdmmc_nand_writesectors(0x5C000, 0x20, (vu8 *)STAGE2OFFSET);
writeFirm((u8 *)FIRM0_OFFSET, false, firmSize); writeFirm((u8 *)FIRM1OFFSET, 1, firm1Size);
writeFirm((u8 *)FIRM1_OFFSET, true, firmSize); writeFirm((u8 *)FIRM0OFFSET, 0, firm0Size);
sdmmc_nand_writesectors(0x5C000, MAX_STAGE2_SIZE / 0x200, (u8 *)STAGE2_OFFSET); sdmmc_nand_writesectors(0x96, 0x1, (vu8 *)SECTOROFFSET);
shutdown(2, "Uninstall: success!"); shutdown(1, pos_y, "Success!");
} }

View File

@ -1,27 +1,20 @@
/* #include "types.h"
* installer.h
*/
#pragma once #pragma once
#include "types.h" #define PDN_MPCORE_CFG (*(vu8 *)0x10140FFC)
#define PDN_SPI_CNT (*(vu8 *)0x101401C0)
#define BUTTON_SELECT (1 << 2)
#define BUTTON_START (1 << 3)
#define OTP_FROM_MEM 0x10012000 #define OTPOFFSET 0x24000000
#define FIRM0_OFFSET 0x24000000 #define SECTOROFFSET 0x24100000
#define SECTION2_POSITION 0x66A00 #define FIRM0OFFSET 0x24200000
#define FIRM1_OFFSET 0x24100000 #define FIRM1OFFSET 0x24300000
#define FIRM0_SIZE 0xF3000 #define STAGE1OFFSET FIRM0OFFSET + 0xF0400
#define FIRM1_SIZE 0xF2000 #define STAGE2OFFSET 0x24400000
#define STAGE1_POSITION 0xF0590 #define MAXSTAGE1SIZE 0x1C00
#define STAGE1_OFFSET FIRM0_OFFSET + STAGE1_POSITION #define MAXSTAGE2SIZE 0x2800
#define STAGE2_OFFSET 0x24200000 #define TEMPOFFSET 0x24500000
#define MAX_STAGE1_SIZE 0x1E70
#define MAX_STAGE2_SIZE 0x89A00
extern u32 magic; void installer(void);
extern const u8 key2s[5][AES_BLOCK_SIZE],
devKey2s[2][AES_BLOCK_SIZE];
static inline void installer(bool isOtpless);
static inline void uninstaller(void);

22
source/main.c Normal file
View File

@ -0,0 +1,22 @@
/*
* main.c
* by Reisyukaku
* Copyright (c) 2015 All Rights Reserved
*
* Minimalist CFW for N3DS
*/
#include "fs.h"
#include "installer.h"
#include "screeninit.h"
#include "draw.h"
#include "types.h"
#define PDN_GPU_CNT (*(vu8 *)0x10141200)
void main(void){
mountSD();
if(PDN_GPU_CNT == 0x1) initLCD();
clearScreens();
installer();
}

View File

@ -1,38 +1,37 @@
/* /*
* memory.c * memory.c
* * by Reisyukaku
* memcpy, memset32 and memcmp adapted from https://github.com/mid-kid/CakesForeveryWan/blob/557a8e8605ab3ee173af6497486e8f22c261d0e2/source/memfuncs.c * Copyright (c) 2015 All Rights Reserved
*/ */
#include "memory.h" #include "memory.h"
void memcpy(void *dest, const void *src, u32 size) void memcpy(void *dest, const void *src, u32 size){
{
u8 *destc = (u8 *)dest; u8 *destc = (u8 *)dest;
const u8 *srcc = (const u8 *)src; const u8 *srcc = (const u8 *)src;
for(u32 i = 0; i < size; i++) for(u32 i = 0; i < size; i++)
destc[i] = srcc[i]; destc[i] = srcc[i];
} }
void memset32(void *dest, u32 filler, u32 size) void memset(void *dest, int filler, u32 size){
{ u8 *destc = (u8 *)dest;
u32 *dest32 = (u32 *)dest; for(u32 i = 0; i < size; i++)
destc[i] = (u8)filler;
for(u32 i = 0; i < size / 4; i++)
dest32[i] = filler;
} }
int memcmp(const void *buf1, const void *buf2, u32 size) void memset32(void *dest, u32 filler, u32 size){
{ u32 *dest32 = (u32 *)dest;
const u8 *buf1c = (const u8 *)buf1, for (u32 i = 0; i < size / 4; i++) {
*buf2c = (const u8 *)buf2; dest32[i] = filler;
for(u32 i = 0; i < size; i++)
{
int cmp = buf1c[i] - buf2c[i];
if(cmp != 0) return cmp;
} }
}
int memcmp(const void *buf1, const void *buf2, u32 size){
const u8 *buf1c = (const u8 *)buf1;
const u8 *buf2c = (const u8 *)buf2;
for(u32 i = 0; i < size; i++){
int cmp = buf1c[i] - buf2c[i];
if(cmp) return cmp;
}
return 0; return 0;
} }

View File

@ -1,7 +1,7 @@
/* /*
* memory.h * memory.h
* * by Reisyukaku
* memcpy, memset32 and memcmp adapted from https://github.com/mid-kid/CakesForeveryWan/blob/557a8e8605ab3ee173af6497486e8f22c261d0e2/source/memfuncs.c * Copyright (c) 2015 All Rights Reserved
*/ */
#pragma once #pragma once
@ -9,5 +9,6 @@
#include "types.h" #include "types.h"
void memcpy(void *dest, const void *src, u32 size); void memcpy(void *dest, const void *src, u32 size);
void memset(void *dest, int filler, u32 size);
void memset32(void *dest, u32 filler, u32 size); void memset32(void *dest, u32 filler, u32 size);
int memcmp(const void *buf1, const void *buf2, u32 size); int memcmp(const void *buf1, const void *buf2, u32 size);

View File

@ -1,196 +0,0 @@
/*
* This file is part of Luma3DS
* Copyright (C) 2016 Aurora Wright, TuxSH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Additional Terms 7.b of GPLv3 applies to this file: Requiring preservation of specified
* reasonable legal notices or author attributions in that material or in the Appropriate Legal
* Notices displayed by works containing it.
*/
/*
* Screen init code by dark_samus, bil1s, Normmatt, delebile and others
*/
/*
* About cache coherency:
*
* Flushing the data cache for all memory regions read from/written to by both processors is mandatory on the ARM9 processor.
* Thus, we make sure there'll be a cache miss on the ARM9 next time it's read.
* Otherwise the ARM9 won't see the changes made and things will break.
*
* On the ARM11, in the environment we're in, the MMU isn't enabled and nothing is cached.
*/
#include "screen.h"
#include "cache.h"
#include "utils.h"
#include "memory.h"
#include "i2c.h"
vu32 *arm11Entry = (vu32 *)BRAHMA_ARM11_ENTRY;
static void invokeArm11Function(void (*func)())
{
*arm11Entry = (u32)func;
while(*arm11Entry);
}
void clearScreens(void)
{
void __attribute__((naked)) ARM11(void)
{
//Setting up two simultaneous memory fills using the GPU
vu32 *REGs_PSC0 = (vu32 *)0x10400010,
*REGs_PSC1 = (vu32 *)0x10400020;
REGs_PSC0[0] = (u32)fb->top_left >> 3; //Start address
REGs_PSC0[1] = (u32)(fb->top_left + SCREEN_TOP_FBSIZE) >> 3; //End address
REGs_PSC0[2] = 0; //Fill value
REGs_PSC0[3] = (2 << 8) | 1; //32-bit pattern; start
REGs_PSC1[0] = (u32)fb->bottom >> 3; //Start address
REGs_PSC1[1] = (u32)(fb->bottom + SCREEN_BOTTOM_FBSIZE) >> 3; //End address
REGs_PSC1[2] = 0; //Fill value
REGs_PSC1[3] = (2 << 8) | 1; //32-bit pattern; start
while(!((REGs_PSC0[3] & 2) && (REGs_PSC1[3] & 2)));
WAIT_FOR_ARM9();
}
invokeArm11Function(ARM11);
}
void initScreens(void)
{
void __attribute__((naked)) initSequence(void)
{
//Disable interrupts
__asm(".word 0xF10C01C0");
*(vu32 *)0x10141200 = 0x1007F;
*(vu32 *)0x10202014 = 0x00000001;
*(vu32 *)0x1020200C &= 0xFFFEFFFE;
*(vu32 *)0x10202240 = 0x4C;
*(vu32 *)0x10202A40 = 0x4C;
*(vu32 *)0x10202244 = 0x1023E;
*(vu32 *)0x10202A44 = 0x1023E;
//Top screen
*(vu32 *)0x10400400 = 0x000001c2;
*(vu32 *)0x10400404 = 0x000000d1;
*(vu32 *)0x10400408 = 0x000001c1;
*(vu32 *)0x1040040c = 0x000001c1;
*(vu32 *)0x10400410 = 0x00000000;
*(vu32 *)0x10400414 = 0x000000cf;
*(vu32 *)0x10400418 = 0x000000d1;
*(vu32 *)0x1040041c = 0x01c501c1;
*(vu32 *)0x10400420 = 0x00010000;
*(vu32 *)0x10400424 = 0x0000019d;
*(vu32 *)0x10400428 = 0x00000002;
*(vu32 *)0x1040042c = 0x00000192;
*(vu32 *)0x10400430 = 0x00000192;
*(vu32 *)0x10400434 = 0x00000192;
*(vu32 *)0x10400438 = 0x00000001;
*(vu32 *)0x1040043c = 0x00000002;
*(vu32 *)0x10400440 = 0x01960192;
*(vu32 *)0x10400444 = 0x00000000;
*(vu32 *)0x10400448 = 0x00000000;
*(vu32 *)0x1040045C = 0x00f00190;
*(vu32 *)0x10400460 = 0x01c100d1;
*(vu32 *)0x10400464 = 0x01920002;
*(vu32 *)0x10400468 = 0x18300000;
*(vu32 *)0x10400470 = 0x80341;
*(vu32 *)0x10400474 = 0x00010501;
*(vu32 *)0x10400478 = 0;
*(vu32 *)0x10400490 = 0x000002D0;
*(vu32 *)0x1040049C = 0x00000000;
//Disco register
for(u32 i = 0; i < 256; i++)
*(vu32 *)0x10400484 = 0x10101 * i;
//Bottom screen
*(vu32 *)0x10400500 = 0x000001c2;
*(vu32 *)0x10400504 = 0x000000d1;
*(vu32 *)0x10400508 = 0x000001c1;
*(vu32 *)0x1040050c = 0x000001c1;
*(vu32 *)0x10400510 = 0x000000cd;
*(vu32 *)0x10400514 = 0x000000cf;
*(vu32 *)0x10400518 = 0x000000d1;
*(vu32 *)0x1040051c = 0x01c501c1;
*(vu32 *)0x10400520 = 0x00010000;
*(vu32 *)0x10400524 = 0x0000019d;
*(vu32 *)0x10400528 = 0x00000052;
*(vu32 *)0x1040052c = 0x00000192;
*(vu32 *)0x10400530 = 0x00000192;
*(vu32 *)0x10400534 = 0x0000004f;
*(vu32 *)0x10400538 = 0x00000050;
*(vu32 *)0x1040053c = 0x00000052;
*(vu32 *)0x10400540 = 0x01980194;
*(vu32 *)0x10400544 = 0x00000000;
*(vu32 *)0x10400548 = 0x00000011;
*(vu32 *)0x1040055C = 0x00f00140;
*(vu32 *)0x10400560 = 0x01c100d1;
*(vu32 *)0x10400564 = 0x01920052;
*(vu32 *)0x10400568 = 0x18300000 + 0x46500;
*(vu32 *)0x10400570 = 0x80301;
*(vu32 *)0x10400574 = 0x00010501;
*(vu32 *)0x10400578 = 0;
*(vu32 *)0x10400590 = 0x000002D0;
*(vu32 *)0x1040059C = 0x00000000;
//Disco register
for(u32 i = 0; i < 256; i++)
*(vu32 *)0x10400584 = 0x10101 * i;
WAIT_FOR_ARM9();
}
//Set CakeBrah framebuffers
void __attribute__((naked)) setupFramebuffers(void)
{
//Disable interrupts
__asm(".word 0xF10C01C0");
fb->top_left = (u8 *)0x18300000;
fb->top_right = (u8 *)0x18300000;
fb->bottom = (u8 *)0x18346500;
*(vu32 *)0x10400468 = (u32)fb->top_left;
*(vu32 *)0x1040046c = (u32)fb->top_left;
*(vu32 *)0x10400494 = (u32)fb->top_right;
*(vu32 *)0x10400498 = (u32)fb->top_right;
*(vu32 *)0x10400568 = (u32)fb->bottom;
*(vu32 *)0x1040056c = (u32)fb->bottom;
WAIT_FOR_ARM9();
}
if(!ARESCREENSINITED)
{
invokeArm11Function(initSequence);
//Turn on backlight
i2cWriteRegister(I2C_DEV_MCU, 0x22, 0x2A);
}
flushDCacheRange((void *)fb, sizeof(struct fb));
invokeArm11Function(setupFramebuffers);
clearScreens();
}

View File

@ -1,51 +0,0 @@
/*
* This file is part of Luma3DS
* Copyright (C) 2016 Aurora Wright, TuxSH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Additional Terms 7.b of GPLv3 applies to this file: Requiring preservation of specified
* reasonable legal notices or author attributions in that material or in the Appropriate Legal
* Notices displayed by works containing it.
*/
/*
* Screen init code by dark_samus, bil1s, Normmatt, delebile and others
*/
#pragma once
#include "types.h"
#define PDN_GPU_CNT (*(vu8 *)0x10141200)
#define ARESCREENSINITED (PDN_GPU_CNT != 1)
#define BRAHMA_ARM11_ENTRY 0x1FFFFFF8
#define WAIT_FOR_ARM9() *arm11Entry = 0; while(!*arm11Entry); ((void (*)())*arm11Entry)();
#define SCREEN_TOP_WIDTH 400
#define SCREEN_BOTTOM_WIDTH 320
#define SCREEN_HEIGHT 240
#define SCREEN_TOP_FBSIZE (3 * SCREEN_TOP_WIDTH * SCREEN_HEIGHT)
#define SCREEN_BOTTOM_FBSIZE (3 * SCREEN_BOTTOM_WIDTH * SCREEN_HEIGHT)
static struct fb {
u8 *top_left;
u8 *top_right;
u8 *bottom;
} *const fb = (struct fb *)0x23FFFE00;
void clearScreens(void);
void initScreens(void);

105
source/screeninit.c Normal file
View File

@ -0,0 +1,105 @@
#include "screeninit.h"
#include "i2c.h"
void initLCD(void){
vu32 *const arm11 = (vu32 *)0x1FFFFFF8;
void __attribute__((naked)) ARM11(void){
__asm(".word 0xF10C01C0");
*(vu32 *)0x10141200 = 0x1007F;
*(vu32 *)0x10202014 = 0x00000001;
*(vu32 *)0x1020200C &= 0xFFFEFFFE;
*(vu32 *)0x10202240 = 0x39;
*(vu32 *)0x10202A40 = 0x39;
*(vu32 *)0x10202244 = 0x1023E;
*(vu32 *)0x10202A44 = 0x1023E;
// Top screen
*(vu32 *)0x10400400 = 0x000001c2;
*(vu32 *)0x10400404 = 0x000000d1;
*(vu32 *)0x10400408 = 0x000001c1;
*(vu32 *)0x1040040c = 0x000001c1;
*(vu32 *)0x10400410 = 0x00000000;
*(vu32 *)0x10400414 = 0x000000cf;
*(vu32 *)0x10400418 = 0x000000d1;
*(vu32 *)0x1040041c = 0x01c501c1;
*(vu32 *)0x10400420 = 0x00010000;
*(vu32 *)0x10400424 = 0x0000019d;
*(vu32 *)0x10400428 = 0x00000002;
*(vu32 *)0x1040042c = 0x00000192;
*(vu32 *)0x10400430 = 0x00000192;
*(vu32 *)0x10400434 = 0x00000192;
*(vu32 *)0x10400438 = 0x00000001;
*(vu32 *)0x1040043c = 0x00000002;
*(vu32 *)0x10400440 = 0x01960192;
*(vu32 *)0x10400444 = 0x00000000;
*(vu32 *)0x10400448 = 0x00000000;
*(vu32 *)0x1040045C = 0x00f00190;
*(vu32 *)0x10400460 = 0x01c100d1;
*(vu32 *)0x10400464 = 0x01920002;
*(vu32 *)0x10400468 = 0x18300000;
*(vu32 *)0x10400470 = 0x80341;
*(vu32 *)0x10400474 = 0x00010501;
*(vu32 *)0x10400478 = 0;
*(vu32 *)0x10400490 = 0x000002D0;
*(vu32 *)0x1040049C = 0x00000000;
// Disco register
for(vu32 i = 0; i < 256; i++)
*(vu32 *)0x10400484 = 0x10101 * i;
// Bottom screen
*(vu32 *)0x10400500 = 0x000001c2;
*(vu32 *)0x10400504 = 0x000000d1;
*(vu32 *)0x10400508 = 0x000001c1;
*(vu32 *)0x1040050c = 0x000001c1;
*(vu32 *)0x10400510 = 0x000000cd;
*(vu32 *)0x10400514 = 0x000000cf;
*(vu32 *)0x10400518 = 0x000000d1;
*(vu32 *)0x1040051c = 0x01c501c1;
*(vu32 *)0x10400520 = 0x00010000;
*(vu32 *)0x10400524 = 0x0000019d;
*(vu32 *)0x10400528 = 0x00000052;
*(vu32 *)0x1040052c = 0x00000192;
*(vu32 *)0x10400530 = 0x00000192;
*(vu32 *)0x10400534 = 0x0000004f;
*(vu32 *)0x10400538 = 0x00000050;
*(vu32 *)0x1040053c = 0x00000052;
*(vu32 *)0x10400540 = 0x01980194;
*(vu32 *)0x10400544 = 0x00000000;
*(vu32 *)0x10400548 = 0x00000011;
*(vu32 *)0x1040055C = 0x00f00140;
*(vu32 *)0x10400560 = 0x01c100d1;
*(vu32 *)0x10400564 = 0x01920052;
*(vu32 *)0x10400568 = 0x18300000 + 0x46500;
*(vu32 *)0x10400570 = 0x80301;
*(vu32 *)0x10400574 = 0x00010501;
*(vu32 *)0x10400578 = 0;
*(vu32 *)0x10400590 = 0x000002D0;
*(vu32 *)0x1040059C = 0x00000000;
// Disco register
for(vu32 i = 0; i < 256; i++)
*(vu32 *)0x10400584 = 0x10101 * i;
// Enable backlight
i2cWriteRegister(I2C_DEV_MCU, 0x22, 0x2A);
*(vu32 *)0x10400468 = 0x18300000;
*(vu32 *)0x1040046c = 0x18300000;
*(vu32 *)0x10400494 = 0x18300000;
*(vu32 *)0x10400498 = 0x18300000;
*(vu32 *)0x10400568 = 0x18346500;
*(vu32 *)0x1040056c = 0x18346500;
//Set CakeBrah framebuffers
*((vu32 *)0x23FFFE00) = 0x18300000;
*((vu32 *)0x23FFFE04) = 0x18300000;
*((vu32 *)0x23FFFE08) = 0x18346500;
while(1);
}
*arm11 = (u32)ARM11;
while(*arm11);
}

5
source/screeninit.h Normal file
View File

@ -0,0 +1,5 @@
#pragma once
#include "types.h"
void initLCD(void);

View File

@ -1,70 +1,24 @@
@ This file is part of Luma3DS
@ Copyright (C) 2016 Aurora Wright, TuxSH
@
@ This program is free software: you can redistribute it and/or modify
@ it under the terms of the GNU General Public License as published by
@ the Free Software Foundation, either version 3 of the License, or
@ (at your option) any later version.
@
@ This program is distributed in the hope that 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.
@
@ You should have received a copy of the GNU General Public License
@ along with this program. If not, see <http://www.gnu.org/licenses/>.
@
@ Additional Terms 7.b of GPLv3 applies to this file: Requiring preservation of specified
@ reasonable legal notices or author attributions in that material or in the Appropriate Legal
@ Notices displayed by works containing it.
@ Thanks to the numerous people who took part in writing this file
.section .text.start .section .text.start
.align 4 .align 4
.global _start .global _start
_start: _start:
b start
.global magic
magic:
.word 0
start:
@ Disable interrupts
mrs r0, cpsr
orr r0, #0x1C0
msr cpsr_cx, r0
@ Change the stack pointer @ Change the stack pointer
mov sp, #0x27000000 mov sp, #0x27000000
@ Disable caches / MPU
mrc p15, 0, r0, c1, c0, 0 @ read control register
bic r0, #(1<<12) @ - instruction cache disable
bic r0, #(1<<2) @ - data cache disable
bic r0, #(1<<0) @ - mpu disable
mcr p15, 0, r0, c1, c0, 0 @ write control register
@ Flush caches
bl flushEntireDCache
bl flushEntireICache
@ Give read/write access to all the memory regions @ Give read/write access to all the memory regions
ldr r0, =0x3333333 ldr r0, =0x33333333
mcr p15, 0, r0, c5, c0, 2 @ write data access mcr p15, 0, r0, c5, c0, 2 @ write data access
mcr p15, 0, r0, c5, c0, 3 @ write instruction access mcr p15, 0, r0, c5, c0, 3 @ write instruction access
@ Set MPU permissions and cache settings @ Set MPU permissions
ldr r0, =0xFFFF001D @ ffff0000 32k | bootrom (unprotected part) ldr r0, =0xFFFF001D @ ffff0000 32k
ldr r1, =0x01FF801D @ 01ff8000 32k | itcm ldr r1, =0x01FF801D @ 01ff8000 32k
ldr r2, =0x08000029 @ 08000000 2M | arm9 mem (O3DS / N3DS) ldr r2, =0x08000027 @ 08000000 1M
ldr r3, =0x10000029 @ 10000000 2M | io mem (ARM9 / first 2MB) ldr r3, =0x10000021 @ 10000000 128k
ldr r4, =0x20000037 @ 20000000 256M | fcram (O3DS / N3DS) ldr r4, =0x10100025 @ 10100000 512k
ldr r5, =0x1FF00027 @ 1FF00000 1M | dsp / axi wram ldr r5, =0x20000035 @ 20000000 128M
ldr r6, =0x1800002D @ 18000000 8M | vram (+ 2MB) ldr r6, =0x1FF00027 @ 1FF00000 1M
mov r7, #0 ldr r7, =0x1800002D @ 18000000 8M
mov r8, #0x15
mcr p15, 0, r0, c6, c0, 0 mcr p15, 0, r0, c6, c0, 0
mcr p15, 0, r1, c6, c1, 0 mcr p15, 0, r1, c6, c1, 0
mcr p15, 0, r2, c6, c2, 0 mcr p15, 0, r2, c6, c2, 0
@ -73,29 +27,32 @@ start:
mcr p15, 0, r5, c6, c5, 0 mcr p15, 0, r5, c6, c5, 0
mcr p15, 0, r6, c6, c6, 0 mcr p15, 0, r6, c6, c6, 0
mcr p15, 0, r7, c6, c7, 0 mcr p15, 0, r7, c6, c7, 0
mcr p15, 0, r8, c3, c0, 0 @ Write bufferable 0, 2, 4
mcr p15, 0, r8, c2, c0, 0 @ Data cacheable 0, 2, 4
mcr p15, 0, r8, c2, c0, 1 @ Inst cacheable 0, 2, 4
@ Enable caches / MPU / ITCM @ Set cache settings
mov r0, #0x25
mcr p15, 0, r0, c2, c0, 0 @ data cacheable
mcr p15, 0, r0, c2, c0, 1 @ instruction cacheable
mcr p15, 0, r1, c3, c0, 0 @ data bufferable
@ Enable caches
mrc p15, 0, r0, c1, c0, 0 @ read control register mrc p15, 0, r0, c1, c0, 0 @ read control register
orr r0, r0, #(1<<18) @ - ITCM enable
orr r0, r0, #(1<<13) @ - alternate exception vectors enable
orr r0, r0, #(1<<12) @ - instruction cache enable orr r0, r0, #(1<<12) @ - instruction cache enable
orr r0, r0, #(1<<2) @ - data cache enable orr r0, r0, #(1<<2) @ - data cache enable
orr r0, r0, #(1<<0) @ - mpu enable orr r0, r0, #(1<<0) @ - mpu enable
mcr p15, 0, r0, c1, c0, 0 @ write control register mcr p15, 0, r0, c1, c0, 0 @ write control register
@ Flush caches
mov r0, #0
mcr p15, 0, r0, c7, c5, 0 @ flush I-cache
mcr p15, 0, r0, c7, c6, 0 @ flush D-cache
mcr p15, 0, r0, c7, c10, 4 @ drain write buffer
@ Fix mounting of SDMC @ Fix mounting of SDMC
ldr r0, =0x10000020 ldr r0, =0x10000020
mov r1, #0x340 mov r1, #0x340
str r1, [r0] str r1, [r0]
@ Clear BSS bl main
ldr r0, =__bss_start
mov r1, #0
ldr r2, =__bss_end
sub r2, r0
bl memset32
b main .die:
b .die

View File

@ -1,64 +0,0 @@
/*
* This file is part of Luma3DS
* Copyright (C) 2016 Aurora Wright, TuxSH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Additional Terms 7.b of GPLv3 applies to this file: Requiring preservation of specified
* reasonable legal notices or author attributions in that material or in the Appropriate Legal
* Notices displayed by works containing it.
*/
#include "strings.h"
#include "memory.h"
u32 strlen(const char *string)
{
char *stringEnd = (char *)string;
while(*stringEnd != 0) stringEnd++;
return stringEnd - string;
}
void concatenateStrings(char *destination, const char *source)
{
u32 i = strlen(source),
j = strlen(destination);
memcpy(&destination[j], source, i + 1);
}
void hexItoa(u32 number, char *out, u32 digits)
{
const char hexDigits[] = "0123456789ABCDEF";
u32 i;
for(i = 0; number > 0; i++)
{
out[digits - 1 - i] = hexDigits[number & 0xF];
number >>= 4;
}
}
u32 hexAtoi(const char *in, u32 digits)
{
u32 res = 0;
char *tmp = (char *)in;
for(u32 i = 0; i < digits && *tmp != 0; tmp++, i++)
res = (*tmp > '9' ? *tmp - 'A' + 10 : *tmp - '0') + (res << 4);
return res;
}

View File

@ -1,30 +0,0 @@
/*
* This file is part of Luma3DS
* Copyright (C) 2016 Aurora Wright, TuxSH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Additional Terms 7.b of GPLv3 applies to this file: Requiring preservation of specified
* reasonable legal notices or author attributions in that material or in the Appropriate Legal
* Notices displayed by works containing it.
*/
#pragma once
#include "types.h"
u32 strlen(const char *string);
void concatenateStrings(char *destination, const char *source);
void hexItoa(u32 number, char *out, u32 digits);
u32 hexAtoi(const char *in, u32 digits);

View File

@ -1,12 +1,14 @@
/* /*
* types.h * types.h
* by Reisyukaku
* Copyright (c) 2015 All Rights Reserved
*/ */
#pragma once #pragma once
#include <stdbool.h>
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdbool.h>
//Common data types //Common data types
typedef uint8_t u8; typedef uint8_t u8;
@ -16,14 +18,4 @@ typedef uint64_t u64;
typedef volatile u8 vu8; typedef volatile u8 vu8;
typedef volatile u16 vu16; typedef volatile u16 vu16;
typedef volatile u32 vu32; typedef volatile u32 vu32;
typedef volatile u64 vu64; typedef volatile u64 vu64;
#define CFG_UNITINFO (*(vu8 *)0x10010010)
#define PDN_MPCORE_CFG (*(vu32 *)0x10140FFC)
#define PDN_SPI_CNT (*(vu32 *)0x101401C0)
#define ISN3DS (PDN_MPCORE_CFG == 7)
#define ISDEVUNIT (CFG_UNITINFO != 0)
#define ISA9LH (!PDN_SPI_CNT)
#include "3dsheaders.h"

View File

@ -1,79 +1,36 @@
/*
* utils.c
*/
#include "utils.h" #include "utils.h"
#include "draw.h" #include "draw.h"
#include "screen.h"
#include "cache.h"
#include "i2c.h" #include "i2c.h"
u32 waitInput(void) u16 waitInput(void){
{ u32 pressedkey = 0;
u32 key, u16 key;
oldKey = HID_PAD;
while(true) //Wait for no keys to be pressed
{ while(HID_PAD);
do {
//Wait for a key to be pressed
while(!HID_PAD);
key = HID_PAD; key = HID_PAD;
if(!key) //Make sure it's pressed
{ for(u32 i = 0xFEE7; i; i--){
oldKey = 0; if (key != HID_PAD)
continue; break;
if(i==1) pressedkey = 1;
} }
} while (!pressedkey);
if(key == oldKey) continue;
//Make sure the key is pressed
u32 i;
for(i = 0; i < 0x13000 && key == HID_PAD; i++);
if(i == 0x13000) break;
}
return key; return key;
} }
void mcuReboot(void) void shutdown(u32 mode, int pos_y, char *message){
{ if(mode){
clearScreens(); pos_y = drawString(message, 10, pos_y + SPACING_VERT, 0xFFFFFF);
drawString("Press any button to shutdown", 10, pos_y, 0xFFFFFF);
//Ensure that all memory transfers have completed and that the data cache has been flushed
flushEntireDCache();
i2cWriteRegister(I2C_DEV_MCU, 0x20, 1 << 2);
while(true);
}
void inputSequence(void)
{
posY = drawString("If you would like to continue, press:", 10, posY, COLOR_WHITE);
posY = drawString("Up, Down, Left, Right, B, A, START, SELECT", 10, posY, COLOR_WHITE);
u32 unlockSequence[] = { BUTTON_UP, BUTTON_DOWN, BUTTON_LEFT, BUTTON_RIGHT, BUTTON_B, BUTTON_A, BUTTON_START, BUTTON_SELECT },
sequenceSize = sizeof(unlockSequence) / sizeof(u32);
for(u32 correctPresses = 0; correctPresses < sequenceSize; correctPresses++)
{
if(waitInput() != unlockSequence[correctPresses])
shutdown(1, "Button sequence not entered correctly");
}
}
void shutdown(u32 mode, const char *message)
{
if(mode != 0)
{
posY = drawString(message, 10, posY + SPACING_Y, mode == 1 ? COLOR_RED : COLOR_GREEN);
drawString("Press any button to shutdown", 10, posY, COLOR_WHITE);
waitInput(); waitInput();
} }
i2cWriteRegister(I2C_DEV_MCU, 0x20, 1);
clearScreens(); while(1);
//Ensure that all memory transfers have completed and that the data cache has been flushed
flushEntireDCache();
i2cWriteRegister(I2C_DEV_MCU, 0x20, 1 << 0);
while(true);
} }

View File

@ -1,33 +1,8 @@
/* #include "types.h"
* utils.h
*/
#pragma once #pragma once
#include "types.h" #define HID_PAD ((~*(vu16 *)0x10146000) & 0xFFF)
#define HID_PAD (*(vu16 *)0x10146000 ^ 0xFFF) u16 waitInput(void);
#define BUTTON_SELECT (1 << 2) void shutdown(u32 mode, int pos_y, char *message);
#define BUTTON_START (1 << 3)
#define BUTTON_A 1
#define BUTTON_B (1 << 1)
#define BUTTON_RIGHT (1 << 4)
#define BUTTON_LEFT (1 << 5)
#define BUTTON_UP (1 << 6)
#define BUTTON_DOWN (1 << 7)
#define COLOR_TITLE 0xFF9900
#define COLOR_WHITE 0xFFFFFF
#define COLOR_RED 0x0000FF
#define COLOR_GREEN 0x00FF00
#define TICKS_PER_SEC 67027964ULL
#define REG_TIMER_CNT(i) *(vu16 *)(0x10003002 + 4 * i)
#define REG_TIMER_VAL(i) *(vu16 *)(0x10003000 + 4 * i)
extern u32 posY;
u32 waitInput(void);
void mcuReboot(void);
void inputSequence(void);
void shutdown(u32 mode, const char *message);