├── stage2 ├── arm9 │ ├── source │ │ ├── fatfs │ │ │ ├── sdmmc │ │ │ │ ├── delay.h │ │ │ │ ├── delay.s │ │ │ │ ├── sdmmc.h │ │ │ │ └── sdmmc.c │ │ │ ├── 00readme.txt │ │ │ ├── integer.h │ │ │ ├── diskio.h │ │ │ ├── diskio.c │ │ │ ├── ffsystem.c │ │ │ ├── 00history.txt │ │ │ ├── ffconf.h │ │ │ └── ff.h │ │ ├── fs.h │ │ ├── memory.h │ │ ├── types.h │ │ ├── memory.c │ │ ├── cache.h │ │ ├── utils.h │ │ ├── i2c.h │ │ ├── firm.h │ │ ├── buttons.h │ │ ├── fs.c │ │ ├── utils.c │ │ ├── main.c │ │ ├── cache.s │ │ ├── start.s │ │ ├── firm.c │ │ ├── crypto.h │ │ ├── i2c.c │ │ └── crypto.c │ ├── itcm_stub │ │ ├── linker.ld │ │ ├── source │ │ │ ├── cache.h │ │ │ ├── memory.h │ │ │ ├── memory.c │ │ │ ├── firm.h │ │ │ ├── types.h │ │ │ ├── firm.c │ │ │ ├── main.c │ │ │ ├── start.s │ │ │ └── cache.s │ │ └── Makefile │ ├── linker.ld │ └── Makefile └── arm11 │ ├── linker.ld │ ├── source │ ├── types.h │ ├── memory.h │ ├── start.s │ ├── memory.c │ └── main.c │ └── Makefile ├── .gitignore ├── Makefile ├── README.md ├── boot9strap.s └── LICENSE /stage2/arm9/source/fatfs/sdmmc/delay.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "../../types.h" 4 | 5 | void waitcycles(u32 us); 6 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | out 2 | build 3 | stage2/arm9/build 4 | stage2/arm9/itcm_stub/build 5 | stage2/arm9/out 6 | stage2/arm11/build 7 | stage2/arm11/out 8 | *.bin 9 | *.firm 10 | *.o 11 | *.elf 12 | *.d -------------------------------------------------------------------------------- /stage2/arm9/source/fs.h: -------------------------------------------------------------------------------- 1 | /* 2 | * fs.h 3 | */ 4 | 5 | #pragma once 6 | 7 | #include "types.h" 8 | 9 | bool mountSd(void); 10 | void unmountSd(void); 11 | bool mountCtrNand(void); 12 | u32 fileRead(void *dest, const char *path, u32 size, u32 maxSize); 13 | bool fileWrite(const void *buffer, const char *path, u32 size); 14 | bool fileDelete(const char *path); 15 | -------------------------------------------------------------------------------- /stage2/arm9/source/fatfs/sdmmc/delay.s: -------------------------------------------------------------------------------- 1 | .text 2 | .arm 3 | .align 4 4 | 5 | .global waitcycles 6 | .type waitcycles, %function 7 | waitcycles: 8 | push {r0-r2, lr} 9 | str r0, [sp, #4] 10 | waitcycles_loop: 11 | ldr r3, [sp, #4] 12 | subs r2, r3, #1 13 | str r2, [sp, #4] 14 | cmp r3, #0 15 | bne waitcycles_loop 16 | pop {r0-r2, pc} 17 | -------------------------------------------------------------------------------- /stage2/arm9/source/memory.h: -------------------------------------------------------------------------------- 1 | /* 2 | * memcpy adapted from https://github.com/mid-kid/CakesForeveryWan/blob/557a8e8605ab3ee173af6497486e8f22c261d0e2/source/memfuncs.c 3 | */ 4 | 5 | #pragma once 6 | 7 | #include "types.h" 8 | 9 | void memcpy(void *dest, const void *src, u32 size); 10 | int memcmp(const void *buf1, const void *buf2, u32 size); 11 | void memset32(void *dest, u32 filler, u32 size); 12 | -------------------------------------------------------------------------------- /stage2/arm11/linker.ld: -------------------------------------------------------------------------------- 1 | OUTPUT_FORMAT("elf32-littlearm", "elf32-bigarm", "elf32-littlearm") 2 | OUTPUT_ARCH(arm) 3 | 4 | ENTRY(_start) 5 | SECTIONS 6 | { 7 | . = 0x1FF80200; 8 | 9 | .text : ALIGN(4) { *(.text.start) *(.text*); . = ALIGN(4); } 10 | .rodata : ALIGN(4) { *(.rodata*); . = ALIGN(4); } 11 | .data : ALIGN(4) { *(.data*); . = ALIGN(4); } 12 | .bss : ALIGN(8) { __bss_start = .; *(.bss* COMMON); . = ALIGN(8); __bss_end = .; } 13 | 14 | __stack_top__ = 0x1FFFE000; 15 | . = ALIGN(4); 16 | } 17 | -------------------------------------------------------------------------------- /stage2/arm9/itcm_stub/linker.ld: -------------------------------------------------------------------------------- 1 | OUTPUT_FORMAT("elf32-littlearm", "elf32-bigarm", "elf32-littlearm") 2 | OUTPUT_ARCH(arm) 3 | 4 | ENTRY(_start) 5 | SECTIONS 6 | { 7 | . = 0x01FF8000; 8 | 9 | __start__ = ABSOLUTE(.); 10 | 11 | .text : ALIGN(4) { *(.text.start) *(.text*); . = ALIGN(4); } 12 | .rodata : ALIGN(4) { *(.rodata*); . = ALIGN(4); } 13 | .data : ALIGN(4) { *(.data*); . = ALIGN(8); *(.bss* COMMON); . = ALIGN(8); } 14 | 15 | . = ALIGN(4); 16 | 17 | __end__ = ABSOLUTE(.); 18 | 19 | __stack_top__ = 0x01FFB800; 20 | __stack_bottom__ = 0x01FFA800; 21 | } 22 | -------------------------------------------------------------------------------- /stage2/arm9/linker.ld: -------------------------------------------------------------------------------- 1 | OUTPUT_FORMAT("elf32-littlearm", "elf32-bigarm", "elf32-littlearm") 2 | OUTPUT_ARCH(arm) 3 | 4 | ENTRY(_start) 5 | SECTIONS 6 | { 7 | . = 0x08001000; 8 | 9 | __start__ = ABSOLUTE(.); 10 | 11 | .text : ALIGN(4) { *(.text.start) *(.text*); . = ALIGN(4); } 12 | .rodata : ALIGN(4) { *(.rodata*); . = ALIGN(4); } 13 | .bss : ALIGN(8) { __bss_start__ = .; *(.bss* COMMON); . = ALIGN(8); __bss_end__ = .; } 14 | 15 | . = ALIGN(4); 16 | 17 | __end__ = ABSOLUTE(.); 18 | 19 | __stack_top__ = 0x08080000; 20 | __stack_bottom__ = 0x0807F000; 21 | } 22 | -------------------------------------------------------------------------------- /stage2/arm9/source/types.h: -------------------------------------------------------------------------------- 1 | /* 2 | * types.h 3 | */ 4 | 5 | #pragma once 6 | 7 | #include 8 | #include 9 | #include 10 | 11 | //Common data types 12 | typedef uint8_t u8; 13 | typedef uint16_t u16; 14 | typedef uint32_t u32; 15 | typedef uint64_t u64; 16 | typedef volatile u8 vu8; 17 | typedef volatile u16 vu16; 18 | typedef volatile u32 vu32; 19 | typedef volatile u64 vu64; 20 | 21 | #define CFG9_SYSPROT9 (*(vu8 *)0x10000000) 22 | #define CFG9_SYSPROT11 (*(vu8 *)0x10000001) 23 | 24 | typedef enum 25 | { 26 | INIT_SCREENS = 0, 27 | WAIT_BOOTROM11_LOCKED, 28 | PREPARE_ARM11_FOR_FIRMLAUNCH, 29 | ARM11_READY 30 | } Arm11Operation; 31 | -------------------------------------------------------------------------------- /stage2/arm11/source/types.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | 6 | //Common data types 7 | typedef uint8_t u8; 8 | typedef uint16_t u16; 9 | typedef uint32_t u32; 10 | typedef uint64_t u64; 11 | typedef volatile u8 vu8; 12 | typedef volatile u16 vu16; 13 | typedef volatile u32 vu32; 14 | typedef volatile u64 vu64; 15 | 16 | #define SCREEN_TOP_WIDTH 400 17 | #define SCREEN_BOTTOM_WIDTH 320 18 | #define SCREEN_HEIGHT 240 19 | #define SCREEN_TOP_FBSIZE (3 * SCREEN_TOP_WIDTH * SCREEN_HEIGHT) 20 | #define SCREEN_BOTTOM_FBSIZE (3 * SCREEN_BOTTOM_WIDTH * SCREEN_HEIGHT) 21 | 22 | typedef enum 23 | { 24 | INIT_SCREENS = 0, 25 | WAIT_BOOTROM11_LOCKED, 26 | PREPARE_ARM11_FOR_FIRMLAUNCH, 27 | ARM11_READY 28 | } Arm11Operation; 29 | -------------------------------------------------------------------------------- /stage2/arm9/source/memory.c: -------------------------------------------------------------------------------- 1 | #include "memory.h" 2 | 3 | void memcpy(void *dest, const void *src, u32 size) 4 | { 5 | u8 *destc = (u8 *)dest; 6 | const u8 *srcc = (const u8 *)src; 7 | 8 | for(u32 i = 0; i < size; i++) 9 | destc[i] = srcc[i]; 10 | } 11 | 12 | int memcmp(const void *buf1, const void *buf2, u32 size) 13 | { 14 | const u8 *buf1c = (const u8 *)buf1, 15 | *buf2c = (const u8 *)buf2; 16 | 17 | for(u32 i = 0; i < size; i++) 18 | { 19 | int cmp = buf1c[i] - buf2c[i]; 20 | if(cmp != 0) return cmp; 21 | } 22 | 23 | return 0; 24 | } 25 | 26 | void memset32(void *dest, u32 filler, u32 size) 27 | { 28 | u32 *dest32 = (u32 *)dest; 29 | 30 | for(u32 i = 0; i < size / 4; i++) 31 | dest32[i] = filler; 32 | } 33 | -------------------------------------------------------------------------------- /stage2/arm9/source/fatfs/00readme.txt: -------------------------------------------------------------------------------- 1 | FatFs Module Source Files R0.13 2 | 3 | 4 | FILES 5 | 6 | 00readme.txt This file. 7 | 00history.txt Revision history. 8 | ff.c FatFs module. 9 | ffconf.h Configuration file of FatFs module. 10 | ff.h Common include file for FatFs and application module. 11 | diskio.h Common include file for FatFs and disk I/O module. 12 | diskio.c An example of glue function to attach existing disk I/O module to FatFs. 13 | integer.h Integer type definitions for FatFs. 14 | ffunicode.c Optional Unicode utility functions. 15 | ffsystem.c An example of optional O/S related functions. 16 | 17 | 18 | Low level disk I/O module is not included in this archive because the FatFs 19 | module is only a generic file system layer and it does not depend on any specific 20 | storage device. You need to provide a low level disk I/O module written to 21 | control the storage device that attached to the target system. 22 | 23 | -------------------------------------------------------------------------------- /stage2/arm9/source/fatfs/integer.h: -------------------------------------------------------------------------------- 1 | /*-------------------------------------------*/ 2 | /* Integer type definitions for FatFs module */ 3 | /*-------------------------------------------*/ 4 | 5 | #ifndef FF_INTEGER 6 | #define FF_INTEGER 7 | 8 | #ifdef _WIN32 /* FatFs development platform */ 9 | 10 | #include 11 | #include 12 | typedef unsigned __int64 QWORD; 13 | 14 | 15 | #else /* Embedded platform */ 16 | 17 | /* These types MUST be 16-bit or 32-bit */ 18 | typedef int INT; 19 | typedef unsigned int UINT; 20 | 21 | /* This type MUST be 8-bit */ 22 | typedef unsigned char BYTE; 23 | 24 | /* These types MUST be 16-bit */ 25 | typedef short SHORT; 26 | typedef unsigned short WORD; 27 | typedef unsigned short WCHAR; 28 | 29 | /* These types MUST be 32-bit */ 30 | typedef long LONG; 31 | typedef unsigned long DWORD; 32 | 33 | /* This type MUST be 64-bit (Remove this for ANSI C (C89) compatibility) */ 34 | typedef unsigned long long QWORD; 35 | 36 | #endif 37 | 38 | #endif 39 | -------------------------------------------------------------------------------- /stage2/arm9/itcm_stub/source/cache.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Luma3DS 3 | * Copyright (C) 2016 Aurora Wright, TuxSH 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | * 18 | * Additional Terms 7.b of GPLv3 applies to this file: Requiring preservation of specified 19 | * reasonable legal notices or author attributions in that material or in the Appropriate Legal 20 | * Notices displayed by works containing it. 21 | */ 22 | 23 | #pragma once 24 | 25 | #include "types.h" 26 | 27 | void flushCaches(void); -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | rwildcard = $(foreach d, $(wildcard $1*), $(filter $(subst *, %, $2), $d) $(call rwildcard, $d/, $2)) 2 | 3 | ifeq ($(strip $(DEVKITARM)),) 4 | $(error "Please set DEVKITARM in your environment. export DEVKITARM=devkitARM") 5 | endif 6 | 7 | include $(DEVKITARM)/base_tools 8 | 9 | name := boot9strap 10 | 11 | dir_arm9_stage2 := stage2/arm9 12 | dir_arm11_stage2 := stage2/arm11 13 | 14 | .PHONY: $(dir_out) 15 | .PHONY: $(dir_build) 16 | .PHONY: $(dir_arm9_stage2) 17 | .PHONY: $(dir_arm11_stage2) 18 | .PHONY: build_boot9strap_firm.py 19 | .PHONY: boot9strap.s 20 | 21 | .PHONY: all 22 | .PHONY: boot9strap 23 | .PHONY: clean 24 | 25 | all: boot9strap 26 | boot9strap: build_boot9strap_firm.py boot9strap.s $(dir_arm9_stage2)/out/arm9.bin $(dir_arm11_stage2)/out/arm11.bin 27 | @mkdir -p "out" 28 | @mkdir -p "build" 29 | @armips boot9strap.s 30 | @python $^ 31 | 32 | $(dir_arm9_stage2)/out/arm9.bin: $(dir_arm9_stage2) 33 | @$(MAKE) -C $< 34 | 35 | $(dir_arm11_stage2)/out/arm11.bin: $(dir_arm11_stage2) 36 | @$(MAKE) -C $< 37 | 38 | clean: 39 | @$(MAKE) -C $(dir_arm9_stage2) clean 40 | @$(MAKE) -C $(dir_arm11_stage2) clean 41 | rm -rf out 42 | rm -rf build -------------------------------------------------------------------------------- /stage2/arm11/Makefile: -------------------------------------------------------------------------------- 1 | rwildcard = $(foreach d, $(wildcard $1*), $(filter $(subst *, %, $2), $d) $(call rwildcard, $d/, $2)) 2 | 3 | ifeq ($(strip $(DEVKITARM)),) 4 | $(error "Please set DEVKITARM in your environment. export DEVKITARM=devkitARM") 5 | endif 6 | 7 | include $(DEVKITARM)/base_tools 8 | 9 | name := $(shell basename $(CURDIR)) 10 | 11 | dir_source := source 12 | dir_build := build 13 | dir_out := out 14 | 15 | ASFLAGS := -mcpu=mpcore 16 | CFLAGS := -Wall -Wextra -MMD -MP -marm $(ASFLAGS) -fno-builtin -std=c11 -Wno-main -O2 -flto -ffast-math 17 | LDFLAGS := -nostartfiles -Wl,--nmagic 18 | 19 | objects = $(patsubst $(dir_source)/%.s, $(dir_build)/%.o, \ 20 | $(patsubst $(dir_source)/%.c, $(dir_build)/%.o, \ 21 | $(call rwildcard, $(dir_source), *.s *.c))) 22 | 23 | .PHONY: all 24 | all: $(dir_out)/$(name).bin 25 | 26 | .PHONY: clean 27 | clean: 28 | @rm -rf $(dir_build) 29 | 30 | $(dir_out)/$(name).bin: $(dir_build)/$(name).elf 31 | @mkdir -p "$(@D)" 32 | $(OBJCOPY) -S -O binary $< $@ 33 | 34 | $(dir_build)/$(name).elf: $(objects) 35 | $(LINK.o) -T linker.ld $(OUTPUT_OPTION) $^ 36 | 37 | $(dir_build)/%.o: $(dir_source)/%.c 38 | @mkdir -p "$(@D)" 39 | $(COMPILE.c) $(OUTPUT_OPTION) $< 40 | 41 | $(dir_build)/%.o: $(dir_source)/%.s 42 | @mkdir -p "$(@D)" 43 | $(COMPILE.s) $(OUTPUT_OPTION) $< 44 | -------------------------------------------------------------------------------- /stage2/arm9/source/cache.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Luma3DS 3 | * Copyright (C) 2016 Aurora Wright, TuxSH 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | * 18 | * Additional Terms 7.b of GPLv3 applies to this file: Requiring preservation of specified 19 | * reasonable legal notices or author attributions in that material or in the Appropriate Legal 20 | * Notices displayed by works containing it. 21 | */ 22 | 23 | #pragma once 24 | 25 | #include "types.h" 26 | 27 | void flushEntireDCache(void); //actually: "clean and flush" 28 | void flushEntireICache(void); 29 | 30 | void flushDCacheRange(void *startAddress, u32 size); 31 | void flushICacheRange(void *startAddress, u32 size); -------------------------------------------------------------------------------- /stage2/arm9/itcm_stub/source/memory.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Luma3DS 3 | * Copyright (C) 2016 Aurora Wright, TuxSH 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | * 18 | * Additional Terms 7.b of GPLv3 applies to this file: Requiring preservation of specified 19 | * reasonable legal notices or author attributions in that material or in the Appropriate Legal 20 | * Notices displayed by works containing it. 21 | */ 22 | 23 | /* 24 | * memcpy adapted from https://github.com/mid-kid/CakesForeveryWan/blob/557a8e8605ab3ee173af6497486e8f22c261d0e2/source/memfuncs.c 25 | */ 26 | 27 | #pragma once 28 | 29 | #include "types.h" 30 | 31 | void memcpy(void *dest, const void *src, u32 size); -------------------------------------------------------------------------------- /stage2/arm9/itcm_stub/Makefile: -------------------------------------------------------------------------------- 1 | rwildcard = $(foreach d, $(wildcard $1*), $(filter $(subst *, %, $2), $d) $(call rwildcard, $d/, $2)) 2 | 3 | ifeq ($(strip $(DEVKITARM)),) 4 | $(error "Please set DEVKITARM in your environment. export DEVKITARM=devkitARM") 5 | endif 6 | 7 | include $(DEVKITARM)/base_tools 8 | 9 | name := $(shell basename $(CURDIR)) 10 | 11 | dir_source := source 12 | dir_build := build 13 | dir_out := ../$(dir_build) 14 | 15 | ASFLAGS := -mcpu=arm946e-s 16 | CFLAGS := -Wall -Wextra -marm $(ASFLAGS) -fno-builtin -std=c11 -Wno-main -O2 -flto -ffast-math 17 | LDFLAGS := -nostartfiles -Wl,--nmagic 18 | 19 | objects = $(patsubst $(dir_source)/%.s, $(dir_build)/%.o, \ 20 | $(patsubst $(dir_source)/%.c, $(dir_build)/%.o, \ 21 | $(call rwildcard, $(dir_source), *.s *.c))) 22 | 23 | .PHONY: all 24 | all: $(dir_out)/$(name).bin 25 | 26 | .PHONY: clean 27 | clean: 28 | @rm -rf $(dir_build) 29 | 30 | $(dir_out)/$(name).bin: $(dir_build)/$(name).elf 31 | $(OBJCOPY) -S -O binary $< $@ 32 | 33 | $(dir_build)/$(name).elf: $(objects) 34 | $(LINK.o) -T linker.ld $(OUTPUT_OPTION) $^ 35 | 36 | $(dir_build)/memory.o: CFLAGS += -O3 37 | 38 | $(dir_build)/%.o: $(dir_source)/%.c 39 | @mkdir -p "$(@D)" 40 | $(COMPILE.c) $(OUTPUT_OPTION) $< 41 | 42 | $(dir_build)/%.o: $(dir_source)/%.s 43 | @mkdir -p "$(@D)" 44 | $(COMPILE.s) $(OUTPUT_OPTION) $< 45 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Boot9strap (+bootonce support) 2 | ===== 3 | ![License](https://img.shields.io/badge/License-GPLv3-blue.svg) 4 | 5 | Boot9/Boot11 code execution. 6 | 7 | For more details, refer to the presentation [here](https://sciresm.github.io/33-and-a-half-c3/). 8 | 9 | Install via [SafeB9SInstaller](https://github.com/d0k3/SafeB9SInstaller). 10 | 11 | Launches "boot.firm" off of the SD card or CTRNAND. Hold Start + Select + X on boot to dump the bootroms/your OTP. 12 | 13 | **Support for bootonce:** 14 | 15 | [A9NC](https://github.com/d0k3/A9NC) and a certain feature in [GodMode9 v1.2.7](https://github.com/d0k3/GodMode9/releases/tag/v1.2.7) require bootonce support for full functionality. As bootonce support has since been [removed](https://github.com/SciresM/boot9strap/commit/ff16d59ff8fba431f5c5c934eea7db4d122eef1c) from the [official boot9strap source](https://github.com/SciresM/boot9strap), this fork exists. *Don't install this if you are not 100% sure you require bootonce support* - stay with the official release. 16 | 17 | **Credits:** 18 | 19 | [Normmatt](https://github.com/Normmatt): Theorizing the NDMA overwite exploit. 20 | [TuxSH](https://github.com/TuxSH): Help implementing bootrom payloads. 21 | [Luma3DS](https://github.com/AuroraWright/Luma3DS): Codebase used in the stage 2 FIRM loader. 22 | 23 | **Licensing:** 24 | 25 | This software is licensed under the terms of the GPLv3. 26 | You can find a copy of the license in the LICENSE file. 27 | -------------------------------------------------------------------------------- /stage2/arm11/source/memory.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Luma3DS 3 | * Copyright (C) 2016 Aurora Wright, TuxSH 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | * 18 | * Additional Terms 7.b of GPLv3 applies to this file: Requiring preservation of specified 19 | * reasonable legal notices or author attributions in that material or in the Appropriate Legal 20 | * Notices displayed by works containing it. 21 | */ 22 | 23 | /* 24 | * memcpy adapted from https://github.com/mid-kid/CakesForeveryWan/blob/557a8e8605ab3ee173af6497486e8f22c261d0e2/source/memfuncs.c 25 | */ 26 | 27 | #pragma once 28 | 29 | #include "types.h" 30 | 31 | void memcpy(void *dest, const void *src, u32 size); 32 | void memset(void *dest, u32 value, u32 size) __attribute__((used)); 33 | void memset32(void *dest, u32 filler, u32 size); 34 | -------------------------------------------------------------------------------- /stage2/arm9/itcm_stub/source/memory.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Luma3DS 3 | * Copyright (C) 2016 Aurora Wright, TuxSH 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | * 18 | * Additional Terms 7.b of GPLv3 applies to this file: Requiring preservation of specified 19 | * reasonable legal notices or author attributions in that material or in the Appropriate Legal 20 | * Notices displayed by works containing it. 21 | */ 22 | 23 | /* 24 | * memcpy adapted from https://github.com/mid-kid/CakesForeveryWan/blob/557a8e8605ab3ee173af6497486e8f22c261d0e2/source/memfuncs.c 25 | */ 26 | 27 | #include "memory.h" 28 | 29 | void memcpy(void *dest, const void *src, u32 size) 30 | { 31 | u8 *destc = (u8 *)dest; 32 | const u8 *srcc = (const u8 *)src; 33 | 34 | for(u32 i = 0; i < size; i++) 35 | destc[i] = srcc[i]; 36 | } -------------------------------------------------------------------------------- /stage2/arm9/source/utils.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Luma3DS 3 | * Copyright (C) 2016 Aurora Wright, TuxSH 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | * 18 | * Additional Terms 7.b of GPLv3 applies to this file: Requiring preservation of specified 19 | * reasonable legal notices or author attributions in that material or in the Appropriate Legal 20 | * Notices displayed by works containing it. 21 | */ 22 | 23 | /* 24 | * waitInput function based on code by d0k3 https://github.com/d0k3/Decrypt9WIP/blob/master/source/hid.c 25 | */ 26 | 27 | #pragma once 28 | 29 | #include "types.h" 30 | 31 | #define TICKS_PER_SEC 67027964ULL 32 | #define REG_TIMER_CNT(i) *(vu16 *)(0x10003002 + 4 * i) 33 | #define REG_TIMER_VAL(i) *(vu16 *)(0x10003000 + 4 * i) 34 | 35 | void mcuPowerOff(void); 36 | void wait(u64 amount); 37 | void error(const char *fmt, ...); 38 | -------------------------------------------------------------------------------- /stage2/arm11/source/start.s: -------------------------------------------------------------------------------- 1 | .section .text.start 2 | .align 4 3 | .global _start 4 | .type _start, %function 5 | _start: 6 | b start 7 | 8 | .global operation 9 | operation: 10 | .word 0 11 | 12 | start: 13 | cpsid aif 14 | 15 | @ Set the control register to reset default: everything disabled 16 | ldr r0, =0x54078 17 | mcr p15, 0, r0, c1, c0, 0 18 | 19 | @ Set the auxiliary control register to reset default. 20 | @ Enables instruction folding, static branch prediction, 21 | @ dynamic branch prediction, and return stack. 22 | mov r0, #0xF 23 | mcr p15, 0, r0, c1, c0, 1 24 | 25 | @ Invalidate all caches, flush the prefetch buffer and DSB 26 | mov r0, #0 27 | mcr p15, 0, r0, c7, c5, 4 28 | mcr p15, 0, r0, c7, c7, 0 29 | mcr p15, 0, r0, c7, c10, 4 30 | 31 | @ Clear BSS 32 | ldr r0, =__bss_start 33 | mov r1, #0 34 | ldr r2, =__bss_end 35 | sub r2, r0 36 | bl memset32 37 | 38 | ldr sp, =__stack_top__ 39 | b main 40 | 41 | .global prepareForFirmlaunch 42 | .type prepareForFirmlaunch, %function 43 | prepareForFirmlaunch: 44 | str r0, [r1] @ tell ARM9 we're done 45 | mov r0, #0x20000000 46 | 47 | _wait_for_core0_entrypoint_loop: 48 | ldr r1, [r0, #-4] @ check if core0's entrypoint is 0 49 | cmp r1, #0 50 | beq _wait_for_core0_entrypoint_loop 51 | 52 | bx r1 @ jump to core0's entrypoint 53 | prepareForFirmlaunchEnd: 54 | 55 | .global prepareForFirmlaunchSize 56 | prepareForFirmlaunchSize: .word prepareForFirmlaunchEnd - prepareForFirmlaunch 57 | -------------------------------------------------------------------------------- /stage2/arm9/itcm_stub/source/firm.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Luma3DS 3 | * Copyright (C) 2017 Aurora Wright, TuxSH 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | * 18 | * Additional Terms 7.b of GPLv3 applies to this file: Requiring preservation of specified 19 | * reasonable legal notices or author attributions in that material or in the Appropriate Legal 20 | * Notices displayed by works containing it. 21 | */ 22 | 23 | #pragma once 24 | 25 | #include "types.h" 26 | 27 | typedef struct __attribute__((packed)) 28 | { 29 | u32 offset; 30 | u8 *address; 31 | u32 size; 32 | u32 procType; 33 | u8 hash[0x20]; 34 | } FirmSection; 35 | 36 | typedef struct __attribute__((packed)) 37 | { 38 | char magic[4]; 39 | u32 reserved1; 40 | u8 *arm11Entry; 41 | u8 *arm9Entry; 42 | u8 reserved2[0x30]; 43 | FirmSection section[4]; 44 | } Firm; 45 | 46 | void launchFirm(Firm *firm, int argc, char **argv); 47 | -------------------------------------------------------------------------------- /stage2/arm9/itcm_stub/source/types.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Luma3DS 3 | * Copyright (C) 2016 Aurora Wright, TuxSH 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | * 18 | * Additional Terms 7.b of GPLv3 applies to this file: Requiring preservation of specified 19 | * reasonable legal notices or author attributions in that material or in the Appropriate Legal 20 | * Notices displayed by works containing it. 21 | */ 22 | 23 | #pragma once 24 | 25 | #include 26 | #include 27 | 28 | //Common data types 29 | typedef uint8_t u8; 30 | typedef uint16_t u16; 31 | typedef uint32_t u32; 32 | typedef uint64_t u64; 33 | typedef volatile u8 vu8; 34 | typedef volatile u16 vu16; 35 | typedef volatile u32 vu32; 36 | typedef volatile u64 vu64; 37 | 38 | #define CFG9_SYSPROT9 (*(vu8 *)0x10000000) 39 | #define CFG9_SYSPROT11 (*(vu8 *)0x10000001) 40 | 41 | struct fb { 42 | u8 *top_left; 43 | u8 *top_right; 44 | u8 *bottom; 45 | }; 46 | -------------------------------------------------------------------------------- /stage2/arm9/source/i2c.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Luma3DS 3 | * Copyright (C) 2016 Aurora Wright, TuxSH 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | * 18 | * Additional Terms 7.b of GPLv3 applies to this file: Requiring preservation of specified 19 | * reasonable legal notices or author attributions in that material or in the Appropriate Legal 20 | * Notices displayed by works containing it. 21 | */ 22 | 23 | /* 24 | * Thanks to the everyone who contributed in the development of this file 25 | */ 26 | 27 | #pragma once 28 | 29 | #include "types.h" 30 | 31 | #define I2C1_REG_OFF 0x10161000 32 | #define I2C2_REG_OFF 0x10144000 33 | #define I2C3_REG_OFF 0x10148000 34 | 35 | #define I2C_REG_DATA 0 36 | #define I2C_REG_CNT 1 37 | #define I2C_REG_CNTEX 2 38 | #define I2C_REG_SCL 4 39 | 40 | #define I2C_DEV_MCU 3 41 | #define I2C_DEV_GYRO 10 42 | #define I2C_DEV_IR 13 43 | 44 | u8 i2cReadRegister(u8 dev_id, u8 reg); 45 | bool i2cWriteRegister(u8 dev_id, u8 reg, u8 data); -------------------------------------------------------------------------------- /stage2/arm9/source/firm.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Luma3DS 3 | * Copyright (C) 2016 Aurora Wright, TuxSH 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | * 18 | * Additional Terms 7.b of GPLv3 applies to this file: Requiring preservation of specified 19 | * reasonable legal notices or author attributions in that material or in the Appropriate Legal 20 | * Notices displayed by works containing it. 21 | */ 22 | 23 | #pragma once 24 | 25 | #include "types.h" 26 | 27 | typedef struct __attribute__((packed)) 28 | { 29 | u32 offset; 30 | u8 *address; 31 | u32 size; 32 | u32 procType; 33 | u8 hash[0x20]; 34 | } FirmSection; 35 | 36 | typedef struct __attribute__((packed)) 37 | { 38 | char magic[4]; 39 | u32 reserved1; 40 | u8 *arm11Entry; 41 | u8 *arm9Entry; 42 | u8 reserved2[0x30]; 43 | FirmSection section[4]; 44 | } Firm; 45 | 46 | u32 checkFirmHeader(Firm *firmHeader, u32 firmBufferAddr, bool isPreLockout); 47 | bool checkSectionHashes(Firm *firm); 48 | -------------------------------------------------------------------------------- /stage2/arm9/itcm_stub/source/firm.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Luma3DS 3 | * Copyright (C) 2017 Aurora Wright, TuxSH 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | * 18 | * Additional Terms 7.b of GPLv3 applies to this file: Requiring preservation of specified 19 | * reasonable legal notices or author attributions in that material or in the Appropriate Legal 20 | * Notices displayed by works containing it. 21 | */ 22 | 23 | #include "firm.h" 24 | #include "memory.h" 25 | #include "cache.h" 26 | 27 | void disableMpuAndJumpToEntrypoints(int argc, char **argv, void *arm11Entry, void *arm9Entry); 28 | 29 | void launchFirm(Firm *firm, int argc, char **argv) 30 | { 31 | //Copy FIRM sections to respective memory locations 32 | for(u32 sectionNum = 0; sectionNum < 4; sectionNum++) 33 | memcpy(firm->section[sectionNum].address, (u8 *)firm + firm->section[sectionNum].offset, firm->section[sectionNum].size); 34 | 35 | disableMpuAndJumpToEntrypoints(argc, argv, firm->arm9Entry, firm->arm11Entry); 36 | 37 | __builtin_unreachable(); 38 | } 39 | -------------------------------------------------------------------------------- /stage2/arm9/source/buttons.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Luma3DS 3 | * Copyright (C) 2016 Aurora Wright, TuxSH 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | * 18 | * Additional Terms 7.b of GPLv3 applies to this file: Requiring preservation of specified 19 | * reasonable legal notices or author attributions in that material or in the Appropriate Legal 20 | * Notices displayed by works containing it. 21 | */ 22 | 23 | #pragma once 24 | 25 | #include "types.h" 26 | 27 | #define HID_PAD (*(vu32 *)0x10146000 ^ 0xFFF) 28 | 29 | #define BUTTON_R1 (1 << 8) 30 | #define BUTTON_L1 (1 << 9) 31 | #define BUTTON_A (1 << 0) 32 | #define BUTTON_B (1 << 1) 33 | #define BUTTON_X (1 << 10) 34 | #define BUTTON_Y (1 << 11) 35 | #define BUTTON_SELECT (1 << 2) 36 | #define BUTTON_START (1 << 3) 37 | #define BUTTON_RIGHT (1 << 4) 38 | #define BUTTON_LEFT (1 << 5) 39 | #define BUTTON_UP (1 << 6) 40 | #define BUTTON_DOWN (1 << 7) 41 | 42 | #define NTRBOOT_BUTTONS (BUTTON_START | BUTTON_SELECT | BUTTON_X) -------------------------------------------------------------------------------- /stage2/arm9/source/fs.c: -------------------------------------------------------------------------------- 1 | /* 2 | * fs.c 3 | */ 4 | 5 | #include "fs.h" 6 | #include 7 | #include "fatfs/ff.h" 8 | 9 | static FATFS fs; 10 | 11 | bool mountSd(void) 12 | { 13 | return f_mount(&fs, "0:", 1) == FR_OK; 14 | } 15 | 16 | void unmountSd(void) 17 | { 18 | f_mount(NULL, "0:", 1); 19 | } 20 | 21 | bool mountCtrNand(void) 22 | { 23 | return f_mount(&fs, "1:", 1) == FR_OK && f_chdrive("1:") == FR_OK; 24 | } 25 | 26 | u32 fileRead(void *dest, const char *path, u32 size, u32 maxSize) 27 | { 28 | FIL file; 29 | u32 ret = 0; 30 | 31 | if(f_open(&file, path, FA_READ) != FR_OK) return ret; 32 | 33 | if(!size) size = f_size(&file); 34 | if(!maxSize || size <= maxSize) 35 | f_read(&file, dest, size, (unsigned int *)&ret); 36 | f_close(&file); 37 | 38 | return ret; 39 | } 40 | 41 | bool fileWrite(const void *buffer, const char *path, u32 size) 42 | { 43 | FIL file; 44 | 45 | switch(f_open(&file, path, FA_WRITE | FA_OPEN_ALWAYS)) 46 | { 47 | case FR_OK: 48 | { 49 | unsigned int written; 50 | f_write(&file, buffer, size, &written); 51 | f_truncate(&file); 52 | f_close(&file); 53 | 54 | return (u32)written == size; 55 | } 56 | case FR_NO_PATH: 57 | for(u32 i = 1; path[i] != 0; i++) 58 | if(path[i] == '/') 59 | { 60 | char folder[i + 1]; 61 | memcpy(folder, path, i); 62 | folder[i] = 0; 63 | f_mkdir(folder); 64 | } 65 | 66 | return fileWrite(buffer, path, size); 67 | default: 68 | return false; 69 | } 70 | } 71 | 72 | bool fileDelete(const char *path) 73 | { 74 | return f_unlink(path) == FR_OK; 75 | } 76 | -------------------------------------------------------------------------------- /stage2/arm11/source/memory.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Luma3DS 3 | * Copyright (C) 2016 Aurora Wright, TuxSH 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | * 18 | * Additional Terms 7.b of GPLv3 applies to this file: Requiring preservation of specified 19 | * reasonable legal notices or author attributions in that material or in the Appropriate Legal 20 | * Notices displayed by works containing it. 21 | */ 22 | 23 | /* 24 | * memcpy adapted from https://github.com/mid-kid/CakesForeveryWan/blob/557a8e8605ab3ee173af6497486e8f22c261d0e2/source/memfuncs.c 25 | */ 26 | 27 | #include "memory.h" 28 | 29 | void memcpy(void *dest, const void *src, u32 size) 30 | { 31 | u8 *destc = (u8 *)dest; 32 | const u8 *srcc = (const u8 *)src; 33 | 34 | for(u32 i = 0; i < size; i++) 35 | destc[i] = srcc[i]; 36 | } 37 | 38 | void memset(void *dest, u32 filler, u32 size) 39 | { 40 | u8 *destc = (u8 *)dest; 41 | 42 | for(u32 i = 0; i < size; i++) 43 | destc[i] = (u8)filler; 44 | } 45 | 46 | void memset32(void *dest, u32 filler, u32 size) 47 | { 48 | u32 *dest32 = (u32 *)dest; 49 | 50 | for(u32 i = 0; i < size / 4; i++) 51 | dest32[i] = filler; 52 | } 53 | -------------------------------------------------------------------------------- /stage2/arm9/itcm_stub/source/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Luma3DS 3 | * Copyright (C) 2016 Aurora Wright, TuxSH 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | * 18 | * Additional Terms 7.b of GPLv3 applies to this file: Requiring preservation of specified 19 | * reasonable legal notices or author attributions in that material or in the Appropriate Legal 20 | * Notices displayed by works containing it. 21 | */ 22 | 23 | #include "firm.h" 24 | 25 | void main(Firm *firm, bool isNand) 26 | { 27 | u32 argc; 28 | char *argv[2]; 29 | struct fb fbs[2] = 30 | { 31 | { 32 | .top_left = (u8 *)0x18300000, 33 | .top_right = (u8 *)0x18300000, 34 | .bottom = (u8 *)0x18346500, 35 | }, 36 | { 37 | .top_left = (u8 *)0x18400000, 38 | .top_right = (u8 *)0x18400000, 39 | .bottom = (u8 *)0x18446500, 40 | }, 41 | }; 42 | 43 | argv[0] = isNand ? "nand:/boot.firm" : "sdmc:/boot.firm"; 44 | 45 | if(firm->reserved2[0] & 1) 46 | { 47 | argc = 2; 48 | argv[1] = (char *)&fbs; 49 | } 50 | else argc = 1; 51 | 52 | launchFirm(firm, argc, argv); 53 | } 54 | -------------------------------------------------------------------------------- /stage2/arm9/itcm_stub/source/start.s: -------------------------------------------------------------------------------- 1 | @ This file is part of Luma3DS 2 | @ Copyright (C) 2017 Aurora Wright, TuxSH 3 | @ 4 | @ This program is free software: you can redistribute it and/or modify 5 | @ it under the terms of the GNU General Public License as published by 6 | @ the Free Software Foundation, either version 3 of the License, or 7 | @ (at your option) any later version. 8 | @ 9 | @ This program is distributed in the hope that it will be useful, 10 | @ but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | @ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | @ GNU General Public License for more details. 13 | @ 14 | @ You should have received a copy of the GNU General Public License 15 | @ along with this program. If not, see . 16 | @ 17 | @ Additional Terms 7.b of GPLv3 applies to this file: Requiring preservation of specified 18 | @ reasonable legal notices or author attributions in that material or in the Appropriate Legal 19 | @ Notices displayed by works containing it. 20 | 21 | .arm 22 | 23 | .section .text.start 24 | .align 4 25 | .global _start 26 | _start: 27 | ldr sp, =__stack_top__ 28 | b main 29 | 30 | .text 31 | .balign 4 32 | .global disableMpuAndJumpToEntrypoints 33 | .type disableMpuAndJumpToEntrypoints, %function 34 | disableMpuAndJumpToEntrypoints: 35 | mov r4, r0 36 | mov r5, r1 37 | mov r6, r2 38 | mov r7, r3 39 | 40 | bl flushCaches 41 | 42 | @ Disable caches / MPU 43 | mrc p15, 0, r0, c1, c0, 0 @ read control register 44 | bic r0, #(1<<12) @ - instruction cache disable 45 | bic r0, #(1<<2) @ - data cache disable 46 | bic r0, #(1<<0) @ - MPU disable 47 | mcr p15, 0, r0, c1, c0, 0 @ write control register 48 | 49 | @ Set the ARM11 entrypoint 50 | mov r0, #0x20000000 51 | str r7, [r0, #-4] 52 | 53 | @ Jump to the ARM9 entrypoint 54 | mov r0, r4 55 | mov r1, r5 56 | ldr r2, =0x2BEEF 57 | bx r6 58 | -------------------------------------------------------------------------------- /stage2/arm9/Makefile: -------------------------------------------------------------------------------- 1 | rwildcard = $(foreach d, $(wildcard $1*), $(filter $(subst *, %, $2), $d) $(call rwildcard, $d/, $2)) 2 | 3 | ifeq ($(strip $(DEVKITARM)),) 4 | $(error "Please set DEVKITARM in your environment. export DEVKITARM=devkitARM") 5 | endif 6 | 7 | include $(DEVKITARM)/base_tools 8 | 9 | name := $(shell basename $(CURDIR)) 10 | 11 | dir_source := source 12 | dir_itcm_stub := itcm_stub 13 | dir_build := build 14 | dir_out := out 15 | 16 | ASFLAGS := -mcpu=arm946e-s 17 | CFLAGS := -Wall -Wextra -MMD -MP -marm $(ASFLAGS) -fno-builtin -std=c11 -Wno-main -O2 -flto -ffast-math 18 | LDFLAGS := -nostartfiles -Wl,--nmagic 19 | 20 | objects = $(patsubst $(dir_source)/%.s, $(dir_build)/%.o, \ 21 | $(patsubst $(dir_source)/%.c, $(dir_build)/%.o, \ 22 | $(call rwildcard, $(dir_source), *.s *.c))) 23 | 24 | bundled = $(dir_build)/itcm_stub.bin.o 25 | 26 | define bin2o 27 | bin2s $< | $(AS) -o $(@) 28 | endef 29 | 30 | .PHONY: all 31 | all: $(dir_out)/$(name).bin 32 | 33 | .PHONY: clean 34 | clean: 35 | @$(MAKE) -C $(dir_itcm_stub) clean 36 | @rm -rf $(dir_build) 37 | @rm -rf $(dir_out) 38 | 39 | $(dir_out)/$(name).bin: $(dir_build)/$(name).elf 40 | @mkdir -p "$(@D)" 41 | $(OBJCOPY) -S -O binary $< $@ 42 | 43 | $(dir_build)/bundled.h: $(bundled) 44 | @$(foreach f, $(bundled),\ 45 | echo "extern const u8" `(echo $(basename $(notdir $(f))) | sed -e 's/^\([0-9]\)/_\1/' | tr . _)`"[];" >> $@;\ 46 | echo "extern const u32" `(echo $(basename $(notdir $(f)))| sed -e 's/^\([0-9]\)/_\1/' | tr . _)`_size";" >> $@;\ 47 | ) 48 | 49 | $(dir_build)/$(name).elf: $(bundled) $(objects) 50 | $(LINK.o) -T linker.ld $(OUTPUT_OPTION) $^ 51 | 52 | $(dir_build)/%.bin.o: $(dir_build)/%.bin 53 | @$(bin2o) 54 | 55 | $(dir_build)/itcm_stub.bin: $(dir_itcm_stub) 56 | @mkdir -p "$(@D)" 57 | @$(MAKE) -C $< 58 | 59 | $(dir_build)/memory.o: CFLAGS += -O3 60 | 61 | $(dir_build)/%.o: $(dir_source)/%.c $(dir_build)/bundled.h 62 | @mkdir -p "$(@D)" 63 | $(COMPILE.c) $(OUTPUT_OPTION) $< 64 | 65 | $(dir_build)/%.o: $(dir_source)/%.s 66 | @mkdir -p "$(@D)" 67 | $(COMPILE.s) $(OUTPUT_OPTION) $< 68 | -------------------------------------------------------------------------------- /stage2/arm9/itcm_stub/source/cache.s: -------------------------------------------------------------------------------- 1 | @ This file is part of Luma3DS 2 | @ Copyright (C) 2016 Aurora Wright, TuxSH 3 | @ 4 | @ This program is free software: you can redistribute it and/or modify 5 | @ it under the terms of the GNU General Public License as published by 6 | @ the Free Software Foundation, either version 3 of the License, or 7 | @ (at your option) any later version. 8 | @ 9 | @ This program is distributed in the hope that it will be useful, 10 | @ but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | @ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | @ GNU General Public License for more details. 13 | @ 14 | @ You should have received a copy of the GNU General Public License 15 | @ along with this program. If not, see . 16 | @ 17 | @ Additional Terms 7.b of GPLv3 applies to this file: Requiring preservation of specified 18 | @ reasonable legal notices or author attributions in that material or in the Appropriate Legal 19 | @ Notices displayed by works containing it. 20 | 21 | .text 22 | .arm 23 | .align 4 24 | 25 | .global flushCaches 26 | .type flushCaches, %function 27 | flushCaches: 28 | @ Clean and flush both the data cache and instruction caches 29 | 30 | @ Adpated from http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.ddi0155a/ch03s03s05.html , 31 | @ and https://github.com/gemarcano/libctr9_io/blob/master/src/ctr_system_ARM.c#L39 as well 32 | @ Note: ARM's example is actually for a 8KB DCache (which is what the 3DS has) 33 | 34 | @ Implemented in bootROM at addresses 0xffff0830 (DCache) and 0xffff0ab4 (ICache) 35 | 36 | mov r1, #0 @ segment counter 37 | outer_loop: 38 | mov r0, #0 @ line counter 39 | 40 | inner_loop: 41 | orr r2, r1, r0 @ generate segment and line address 42 | mcr p15, 0, r2, c7, c14, 2 @ clean and flush the line 43 | add r0, #0x20 @ increment to next line 44 | cmp r0, #0x400 45 | bne inner_loop 46 | 47 | add r1, #0x40000000 48 | cmp r1, #0 49 | bne outer_loop 50 | 51 | mcr p15, 0, r1, c7, c10, 4 @ drain write buffer 52 | 53 | @ Flush instruction cache 54 | mcr p15, 0, r1, c7, c5, 0 55 | 56 | bx lr 57 | -------------------------------------------------------------------------------- /stage2/arm9/source/utils.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Luma3DS 3 | * Copyright (C) 2016 Aurora Wright, TuxSH 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | * 18 | * Additional Terms 7.b of GPLv3 applies to this file: Requiring preservation of specified 19 | * reasonable legal notices or author attributions in that material or in the Appropriate Legal 20 | * Notices displayed by works containing it. 21 | */ 22 | 23 | /* 24 | * waitInput function based on code by d0k3 https://github.com/d0k3/Decrypt9WIP/blob/master/source/hid.c 25 | */ 26 | 27 | #include "utils.h" 28 | #include "i2c.h" 29 | #include "cache.h" 30 | 31 | static inline void startChrono(void) 32 | { 33 | static bool isChronoStarted = false; 34 | 35 | if(isChronoStarted) return; 36 | 37 | REG_TIMER_CNT(0) = 0; //67MHz 38 | for(u32 i = 1; i < 4; i++) REG_TIMER_CNT(i) = 4; //Count-up 39 | 40 | for(u32 i = 0; i < 4; i++) REG_TIMER_VAL(i) = 0; 41 | 42 | REG_TIMER_CNT(0) = 0x80; //67MHz; enabled 43 | for(u32 i = 1; i < 4; i++) REG_TIMER_CNT(i) = 0x84; //Count-up; enabled 44 | 45 | isChronoStarted = true; 46 | } 47 | 48 | static u64 chrono(void) 49 | { 50 | u64 res = 0; 51 | for(u32 i = 0; i < 4; i++) res |= REG_TIMER_VAL(i) << (16 * i); 52 | 53 | res /= (TICKS_PER_SEC / 1000); 54 | 55 | return res; 56 | } 57 | 58 | void mcuPowerOff(void) 59 | { 60 | //Ensure that all memory transfers have completed and that the data cache has been flushed 61 | flushEntireDCache(); 62 | 63 | i2cWriteRegister(I2C_DEV_MCU, 0x20, 1 << 0); 64 | while(true); 65 | } 66 | 67 | void wait(u64 amount) 68 | { 69 | startChrono(); 70 | 71 | u64 initialValue = chrono(); 72 | 73 | while(chrono() - initialValue < amount); 74 | } 75 | -------------------------------------------------------------------------------- /stage2/arm9/source/fatfs/diskio.h: -------------------------------------------------------------------------------- 1 | /*-----------------------------------------------------------------------/ 2 | / Low level disk interface modlue include file (C)ChaN, 2014 / 3 | /-----------------------------------------------------------------------*/ 4 | 5 | #ifndef _DISKIO_DEFINED 6 | #define _DISKIO_DEFINED 7 | 8 | #ifdef __cplusplus 9 | extern "C" { 10 | #endif 11 | 12 | #define _USE_WRITE 1 /* 1: Enable disk_write function */ 13 | #define _USE_IOCTL 1 /* 1: Enable disk_ioctl fucntion */ 14 | 15 | #include "integer.h" 16 | 17 | 18 | /* Status of Disk Functions */ 19 | typedef BYTE DSTATUS; 20 | 21 | /* Results of Disk Functions */ 22 | typedef enum { 23 | RES_OK = 0, /* 0: Successful */ 24 | RES_ERROR, /* 1: R/W Error */ 25 | RES_WRPRT, /* 2: Write Protected */ 26 | RES_NOTRDY, /* 3: Not Ready */ 27 | RES_PARERR /* 4: Invalid Parameter */ 28 | } DRESULT; 29 | 30 | 31 | /*---------------------------------------*/ 32 | /* Prototypes for disk control functions */ 33 | 34 | 35 | DSTATUS disk_initialize (BYTE pdrv); 36 | DSTATUS disk_status (BYTE pdrv); 37 | DRESULT disk_read (BYTE pdrv, BYTE* buff, DWORD sector, UINT count); 38 | DRESULT disk_write (BYTE pdrv, const BYTE* buff, DWORD sector, UINT count); 39 | DRESULT disk_ioctl (BYTE pdrv, BYTE cmd, void* buff); 40 | 41 | 42 | /* Disk Status Bits (DSTATUS) */ 43 | 44 | #define STA_NOINIT 0x01 /* Drive not initialized */ 45 | #define STA_NODISK 0x02 /* No medium in the drive */ 46 | #define STA_PROTECT 0x04 /* Write protected */ 47 | 48 | 49 | /* Command code for disk_ioctrl fucntion */ 50 | 51 | /* Generic command (Used by FatFs) */ 52 | #define CTRL_SYNC 0 /* Complete pending write process (needed at _FS_READONLY == 0) */ 53 | #define GET_SECTOR_COUNT 1 /* Get media size (needed at _USE_MKFS == 1) */ 54 | #define GET_SECTOR_SIZE 2 /* Get sector size (needed at _MAX_SS != _MIN_SS) */ 55 | #define GET_BLOCK_SIZE 3 /* Get erase block size (needed at _USE_MKFS == 1) */ 56 | #define CTRL_TRIM 4 /* Inform device that the data on the block of sectors is no longer used (needed at _USE_TRIM == 1) */ 57 | 58 | /* Generic command (Not used by FatFs) */ 59 | #define CTRL_POWER 5 /* Get/Set power status */ 60 | #define CTRL_LOCK 6 /* Lock/Unlock media removal */ 61 | #define CTRL_EJECT 7 /* Eject media */ 62 | #define CTRL_FORMAT 8 /* Create physical format on the media */ 63 | 64 | /* MMC/SDC specific ioctl command */ 65 | #define MMC_GET_TYPE 10 /* Get card type */ 66 | #define MMC_GET_CSD 11 /* Get CSD */ 67 | #define MMC_GET_CID 12 /* Get CID */ 68 | #define MMC_GET_OCR 13 /* Get OCR */ 69 | #define MMC_GET_SDSTAT 14 /* Get SD status */ 70 | 71 | /* ATA/CF specific ioctl command */ 72 | #define ATA_GET_REV 20 /* Get F/W revision */ 73 | #define ATA_GET_MODEL 21 /* Get model name */ 74 | #define ATA_GET_SN 22 /* Get serial number */ 75 | 76 | #ifdef __cplusplus 77 | } 78 | #endif 79 | 80 | #endif 81 | -------------------------------------------------------------------------------- /stage2/arm9/source/fatfs/sdmmc/sdmmc.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "../../types.h" 4 | 5 | #define SDMMC_BASE 0x10006000 6 | 7 | #define REG_SDCMD 0x00 8 | #define REG_SDPORTSEL 0x02 9 | #define REG_SDCMDARG 0x04 10 | #define REG_SDCMDARG0 0x04 11 | #define REG_SDCMDARG1 0x06 12 | #define REG_SDSTOP 0x08 13 | #define REG_SDBLKCOUNT 0x0A 14 | 15 | #define REG_SDRESP0 0x0C 16 | #define REG_SDRESP1 0x0E 17 | #define REG_SDRESP2 0x10 18 | #define REG_SDRESP3 0x12 19 | #define REG_SDRESP4 0x14 20 | #define REG_SDRESP5 0x16 21 | #define REG_SDRESP6 0x18 22 | #define REG_SDRESP7 0x1A 23 | 24 | #define REG_SDSTATUS0 0x1C 25 | #define REG_SDSTATUS1 0x1E 26 | 27 | #define REG_SDIRMASK0 0x20 28 | #define REG_SDIRMASK1 0x22 29 | #define REG_SDCLKCTL 0x24 30 | 31 | #define REG_SDBLKLEN 0x26 32 | #define REG_SDOPT 0x28 33 | #define REG_SDFIFO 0x30 34 | 35 | #define REG_DATACTL 0xD8 36 | #define REG_SDRESET 0xE0 37 | #define REG_SDPROTECTED 0xF6 //bit 0 determines if sd is protected or not? 38 | 39 | #define REG_DATACTL32 0x100 40 | #define REG_SDBLKLEN32 0x104 41 | #define REG_SDBLKCOUNT32 0x108 42 | #define REG_SDFIFO32 0x10C 43 | 44 | #define REG_CLK_AND_WAIT_CTL 0x138 45 | #define REG_RESET_SDIO 0x1E0 46 | 47 | #define TMIO_STAT0_CMDRESPEND 0x0001 48 | #define TMIO_STAT0_DATAEND 0x0004 49 | #define TMIO_STAT0_CARD_REMOVE 0x0008 50 | #define TMIO_STAT0_CARD_INSERT 0x0010 51 | #define TMIO_STAT0_SIGSTATE 0x0020 52 | #define TMIO_STAT0_WRPROTECT 0x0080 53 | #define TMIO_STAT0_CARD_REMOVE_A 0x0100 54 | #define TMIO_STAT0_CARD_INSERT_A 0x0200 55 | #define TMIO_STAT0_SIGSTATE_A 0x0400 56 | #define TMIO_STAT1_CMD_IDX_ERR 0x0001 57 | #define TMIO_STAT1_CRCFAIL 0x0002 58 | #define TMIO_STAT1_STOPBIT_ERR 0x0004 59 | #define TMIO_STAT1_DATATIMEOUT 0x0008 60 | #define TMIO_STAT1_RXOVERFLOW 0x0010 61 | #define TMIO_STAT1_TXUNDERRUN 0x0020 62 | #define TMIO_STAT1_CMDTIMEOUT 0x0040 63 | #define TMIO_STAT1_RXRDY 0x0100 64 | #define TMIO_STAT1_TXRQ 0x0200 65 | #define TMIO_STAT1_ILL_FUNC 0x2000 66 | #define TMIO_STAT1_CMD_BUSY 0x4000 67 | #define TMIO_STAT1_ILL_ACCESS 0x8000 68 | 69 | #define TMIO_MASK_ALL 0x837F031D 70 | 71 | #define TMIO_MASK_GW (TMIO_STAT1_ILL_ACCESS | TMIO_STAT1_CMDTIMEOUT | TMIO_STAT1_TXUNDERRUN | TMIO_STAT1_RXOVERFLOW | \ 72 | TMIO_STAT1_DATATIMEOUT | TMIO_STAT1_STOPBIT_ERR | TMIO_STAT1_CRCFAIL | TMIO_STAT1_CMD_IDX_ERR) 73 | 74 | #define TMIO_MASK_READOP (TMIO_STAT1_RXRDY | TMIO_STAT1_DATAEND) 75 | #define TMIO_MASK_WRITEOP (TMIO_STAT1_TXRQ | TMIO_STAT1_DATAEND) 76 | 77 | typedef struct mmcdevice { 78 | u8 *rData; 79 | const u8 *tData; 80 | u32 size; 81 | u32 error; 82 | u16 stat0; 83 | u16 stat1; 84 | u32 ret[4]; 85 | u32 initarg; 86 | u32 isSDHC; 87 | u32 clk; 88 | u32 SDOPT; 89 | u32 devicenumber; 90 | u32 total_size; //size in sectors of the device 91 | u32 res; 92 | } mmcdevice; 93 | 94 | u32 sdmmc_sdcard_init(); 95 | int sdmmc_sdcard_readsectors(u32 sector_no, u32 numsectors, u8 *out); 96 | int sdmmc_sdcard_writesectors(u32 sector_no, u32 numsectors, const u8 *in); 97 | int sdmmc_nand_readsectors(u32 sector_no, u32 numsectors, u8 *out); 98 | //int sdmmc_nand_writesectors(u32 sector_no, u32 numsectors, const u8 *in); 99 | void sdmmc_get_cid(bool isNand, u32 *info); 100 | //mmcdevice *getMMCDevice(int drive); -------------------------------------------------------------------------------- /stage2/arm9/source/main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * main.c 3 | */ 4 | 5 | #include "types.h" 6 | #include "memory.h" 7 | #include "crypto.h" 8 | #include "i2c.h" 9 | #include "fs.h" 10 | #include "firm.h" 11 | #include "utils.h" 12 | #include "buttons.h" 13 | #include "../build/bundled.h" 14 | 15 | static void (*const itcmStub)(Firm *firm, bool isNand) = (void (*const)(Firm *, bool))0x01FF8000; 16 | static volatile Arm11Operation *operation = (volatile Arm11Operation *)0x1FF80204; 17 | 18 | static void invokeArm11Function(Arm11Operation op) 19 | { 20 | while(*operation != ARM11_READY); 21 | *operation = op; 22 | while(*operation != ARM11_READY); 23 | } 24 | 25 | static void loadFirm(bool isNand, bool bootOnce) 26 | { 27 | Firm *firmHeader = (Firm *)0x080A0000; 28 | const char *firmName = bootOnce ? "bootonce.firm" : "boot.firm"; 29 | 30 | if(fileRead(firmHeader, firmName, 0x200, 0) != 0x200) return; 31 | 32 | bool isPreLockout = ((firmHeader->reserved2[0] & 2) != 0), 33 | isScreenInit = ((firmHeader->reserved2[0] & 1) != 0); 34 | 35 | Firm *firm; 36 | u32 maxFirmSize; 37 | 38 | if(!isPreLockout) 39 | { 40 | //Lockout 41 | while(!(CFG9_SYSPROT9 & 1)) CFG9_SYSPROT9 |= 1; 42 | while(!(CFG9_SYSPROT11 & 1)) CFG9_SYSPROT11 |= 1; 43 | invokeArm11Function(WAIT_BOOTROM11_LOCKED); 44 | 45 | firm = (Firm *)0x20001000; 46 | maxFirmSize = 0x07FFF000; //around 127MB (although we don't enable ext FCRAM on N3DS, beware!) 47 | } 48 | else 49 | { 50 | //Uncached area, shouldn't affect performance too much, though 51 | firm = (Firm *)0x18000000; 52 | maxFirmSize = 0x300000; //3MB 53 | } 54 | 55 | u32 calculatedFirmSize = checkFirmHeader(firmHeader, (u32)firm, isPreLockout); 56 | 57 | if(!calculatedFirmSize) mcuPowerOff(); 58 | 59 | if(fileRead(firm, firmName, 0, maxFirmSize) < calculatedFirmSize || !checkSectionHashes(firm)) mcuPowerOff(); 60 | if(bootOnce) fileDelete(firmName); 61 | 62 | if(isScreenInit) 63 | { 64 | invokeArm11Function(INIT_SCREENS); 65 | i2cWriteRegister(I2C_DEV_MCU, 0x22, 0x2A); //Turn on backlight 66 | } 67 | 68 | memcpy((void *)itcmStub, itcm_stub_bin, itcm_stub_bin_size); 69 | 70 | //Launch firm 71 | invokeArm11Function(PREPARE_ARM11_FOR_FIRMLAUNCH); 72 | itcmStub(firm, isNand); 73 | } 74 | 75 | void main(void) 76 | { 77 | setupKeyslots(); 78 | 79 | if(mountSd()) 80 | { 81 | /* I believe this is the canonical secret key combination. */ 82 | if(HID_PAD == NTRBOOT_BUTTONS) 83 | { 84 | fileWrite((void *)0x08080000, "boot9strap/boot9.bin", 0x10000); 85 | fileWrite((void *)0x08090000, "boot9strap/boot11.bin", 0x10000); 86 | fileWrite((void *)0x10012000, "boot9strap/otp.bin", 0x100); 87 | 88 | /* Wait until buttons are not held, for compatibility. */ 89 | while(HID_PAD & NTRBOOT_BUTTONS); 90 | wait(1000ULL); 91 | } 92 | 93 | loadFirm(false, true); 94 | loadFirm(false, false); 95 | unmountSd(); 96 | } 97 | 98 | if(mountCtrNand()) 99 | { 100 | /* Wait until buttons are not held, for compatibility. */ 101 | if(HID_PAD == NTRBOOT_BUTTONS) 102 | { 103 | while(HID_PAD & NTRBOOT_BUTTONS); 104 | wait(1000ULL); 105 | } 106 | loadFirm(true, false); 107 | } 108 | 109 | mcuPowerOff(); 110 | } 111 | -------------------------------------------------------------------------------- /stage2/arm9/source/cache.s: -------------------------------------------------------------------------------- 1 | @ This file is part of Luma3DS 2 | @ Copyright (C) 2016 Aurora Wright, TuxSH 3 | @ 4 | @ This program is free software: you can redistribute it and/or modify 5 | @ it under the terms of the GNU General Public License as published by 6 | @ the Free Software Foundation, either version 3 of the License, or 7 | @ (at your option) any later version. 8 | @ 9 | @ This program is distributed in the hope that it will be useful, 10 | @ but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | @ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | @ GNU General Public License for more details. 13 | @ 14 | @ You should have received a copy of the GNU General Public License 15 | @ along with this program. If not, see . 16 | @ 17 | @ Additional Terms 7.b of GPLv3 applies to this file: Requiring preservation of specified 18 | @ reasonable legal notices or author attributions in that material or in the Appropriate Legal 19 | @ Notices displayed by works containing it. 20 | 21 | .text 22 | .arm 23 | .align 4 24 | 25 | .global flushEntireDCache 26 | .type flushEntireDCache, %function 27 | flushEntireDCache: 28 | @ Adapted from http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.ddi0155a/ch03s03s05.html, 29 | @ and https://github.com/gemarcano/libctr9_io/blob/master/src/ctr_system_ARM.c#L39 as well 30 | @ Note: ARM's example is actually for a 8KB DCache (which is what the 3DS has) 31 | 32 | @ Implemented in bootROM at address 0xffff0830 33 | mov r1, #0 @ segment counter 34 | outer_loop: 35 | mov r0, #0 @ line counter 36 | 37 | inner_loop: 38 | orr r2, r1, r0 @ generate segment and line address 39 | mcr p15, 0, r2, c7, c14, 2 @ clean and flush the line 40 | add r0, #0x20 @ increment to next line 41 | cmp r0, #0x400 42 | bne inner_loop 43 | 44 | add r1, #0x40000000 45 | cmp r1, #0 46 | bne outer_loop 47 | 48 | mcr p15, 0, r1, c7, c10, 4 @ drain write buffer 49 | bx lr 50 | 51 | .global flushDCacheRange 52 | .type flushDCacheRange, %function 53 | flushDCacheRange: 54 | @ Implemented in bootROM at address 0xffff08a0 55 | add r1, r0, r1 @ end address 56 | bic r0, #0x1f @ align source address to cache line size (32 bytes) 57 | 58 | flush_dcache_range_loop: 59 | mcr p15, 0, r0, c7, c14, 1 @ clean and flush the line corresponding to the address r0 is holding 60 | add r0, #0x20 61 | cmp r0, r1 62 | blo flush_dcache_range_loop 63 | 64 | mov r0, #0 65 | mcr p15, 0, r0, c7, c10, 4 @ drain write buffer 66 | bx lr 67 | 68 | 69 | .global flushEntireICache 70 | .type flushEntireICache, %function 71 | flushEntireICache: 72 | @ Implemented in bootROM at address 0xffff0ab4 73 | mov r0, #0 74 | mcr p15, 0, r0, c7, c5, 0 75 | bx lr 76 | 77 | .global flushICacheRange 78 | .type flushICacheRange, %function 79 | flushICacheRange: 80 | @ Implemented in bootROM at address 0xffff0ac0 81 | add r1, r0, r1 @ end address 82 | bic r0, #0x1f @ align source address to cache line size (32 bytes) 83 | 84 | flush_icache_range_loop: 85 | mcr p15, 0, r0, c7, c5, 1 @ flush the line corresponding to the address r0 is holding 86 | add r0, #0x20 87 | cmp r0, r1 88 | blo flush_icache_range_loop 89 | 90 | bx lr 91 | -------------------------------------------------------------------------------- /stage2/arm9/source/start.s: -------------------------------------------------------------------------------- 1 | @ This file is part of Luma3DS 2 | @ Copyright (C) 2016 Aurora Wright, TuxSH 3 | @ 4 | @ This program is free software: you can redistribute it and/or modify 5 | @ it under the terms of the GNU General Public License as published by 6 | @ the Free Software Foundation, either version 3 of the License, or 7 | @ (at your option) any later version. 8 | @ 9 | @ This program is distributed in the hope that it will be useful, 10 | @ but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | @ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | @ GNU General Public License for more details. 13 | @ 14 | @ You should have received a copy of the GNU General Public License 15 | @ along with this program. If not, see . 16 | @ 17 | @ Additional Terms 7.b of GPLv3 applies to this file: Requiring preservation of specified 18 | @ reasonable legal notices or author attributions in that material or in the Appropriate Legal 19 | @ Notices displayed by works containing it. 20 | 21 | @ Thanks to the numerous people who took part in writing this file 22 | 23 | .section .text.start 24 | .align 4 25 | .global _start 26 | _start: 27 | @ Disable interrupts and switch to supervisor mode (also clear flags) 28 | mov r4, #0x13 29 | orr r4, #0x1C0 30 | msr cpsr_cxsf, r4 31 | 32 | @ Change the stack pointer 33 | ldr sp, =__stack_top__ 34 | 35 | @ Disable caches / MPU 36 | mrc p15, 0, r0, c1, c0, 0 @ read control register 37 | bic r0, #(1<<12) @ - instruction cache disable 38 | bic r0, #(1<<2) @ - data cache disable 39 | bic r0, #(1<<0) @ - mpu disable 40 | mcr p15, 0, r0, c1, c0, 0 @ write control register 41 | 42 | @ Invalidate both caches, discarding any data they may contain, 43 | @ then drain the write buffer 44 | mov r4, #0 45 | mcr p15, 0, r4, c7, c5, 0 46 | mcr p15, 0, r4, c7, c6, 0 47 | mcr p15, 0, r4, c7, c10, 4 48 | 49 | @ Give read/write access to all the memory regions 50 | ldr r0, =0x33333333 51 | mcr p15, 0, r0, c5, c0, 2 @ write data access 52 | mcr p15, 0, r0, c5, c0, 3 @ write instruction access 53 | 54 | @ Set MPU permissions and cache settings 55 | ldr r0, =0xFFFF001D @ ffff0000 32k | bootrom (unprotected part) 56 | ldr r1, =0xFFF0001B @ fff00000 16k | dtcm 57 | ldr r2, =0x01FF801D @ 01ff8000 32k | itcm 58 | ldr r3, =0x08000027 @ 08000000 1M | arm9 mem 59 | ldr r4, =0x10000029 @ 10000000 2M | io mem (ARM9 / first 2MB) 60 | ldr r5, =0x20000035 @ 20000000 128M | fcram 61 | ldr r6, =0x1FF00027 @ 1FF00000 1M | dsp / axi wram 62 | ldr r7, =0x1800002D @ 18000000 8M | vram (+ 2MB) 63 | mov r8, #0x29 64 | mcr p15, 0, r0, c6, c0, 0 65 | mcr p15, 0, r1, c6, c1, 0 66 | mcr p15, 0, r2, c6, c2, 0 67 | mcr p15, 0, r3, c6, c3, 0 68 | mcr p15, 0, r4, c6, c4, 0 69 | mcr p15, 0, r5, c6, c5, 0 70 | mcr p15, 0, r6, c6, c6, 0 71 | mcr p15, 0, r7, c6, c7, 0 72 | mcr p15, 0, r8, c3, c0, 0 @ Write bufferable 0, 3, 5 73 | mcr p15, 0, r8, c2, c0, 0 @ Data cacheable 0, 3, 5 74 | mcr p15, 0, r8, c2, c0, 1 @ Inst cacheable 0, 3, 5 75 | 76 | @ Enable caches / MPU. ITCM and DTCM are already enabled, same as alternate exception vectors 77 | mrc p15, 0, r0, c1, c0, 0 @ read control register 78 | orr r0, r0, #(1<<12) @ - instruction cache enable 79 | orr r0, r0, #(1<<2) @ - data cache enable 80 | orr r0, r0, #(1<<0) @ - mpu enable 81 | mcr p15, 0, r0, c1, c0, 0 @ write control register 82 | 83 | @ Clear BSS 84 | ldr r0, =__bss_start__ 85 | mov r1, #0 86 | ldr r2, =__bss_end__ 87 | sub r2, r0 88 | bl memset32 89 | 90 | b main 91 | -------------------------------------------------------------------------------- /stage2/arm9/source/firm.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Luma3DS 3 | * Copyright (C) 2016 Aurora Wright, TuxSH 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | * 18 | * Additional Terms 7.b of GPLv3 applies to this file: Requiring preservation of specified 19 | * reasonable legal notices or author attributions in that material or in the Appropriate Legal 20 | * Notices displayed by works containing it. 21 | */ 22 | 23 | #include "firm.h" 24 | #include "memory.h" 25 | #include "crypto.h" 26 | 27 | static __attribute__((noinline)) bool overlaps(u32 as, u32 ae, u32 bs, u32 be) 28 | { 29 | if(as <= bs && bs <= ae) 30 | return true; 31 | if(bs <= as && as <= be) 32 | return true; 33 | return false; 34 | } 35 | 36 | static __attribute__((noinline)) bool inRange(u32 as, u32 ae, u32 bs, u32 be) 37 | { 38 | if(as >= bs && ae <= be) 39 | return true; 40 | return false; 41 | } 42 | 43 | u32 checkFirmHeader(Firm *firmHeader, u32 firmBufferAddr, bool isPreLockout) 44 | { 45 | if(memcmp(firmHeader->magic, "FIRM", 4) != 0 || firmHeader->arm9Entry == NULL) //Allow for the ARM11 entrypoint to be zero in which case nothing is done on the ARM11 side 46 | return 0; 47 | 48 | bool arm9EpFound = false, 49 | arm11EpFound = false; 50 | 51 | u32 size = 0x200; 52 | for(u32 i = 0; i < 4; i++) 53 | size += firmHeader->section[i].size; 54 | 55 | for(u32 i = 0; i < 4; i++) 56 | { 57 | FirmSection *section = &firmHeader->section[i]; 58 | 59 | //Allow empty sections 60 | if(section->size == 0) 61 | continue; 62 | 63 | if((section->offset < 0x200) || 64 | (section->address + section->size < section->address) || //Overflow check 65 | ((u32)section->address & 3) || (section->offset & 0x1FF) || (section->size & 0x1FF) || //Alignment check 66 | (overlaps((u32)section->address, (u32)section->address + section->size, firmBufferAddr, firmBufferAddr + size)) || 67 | ((!inRange((u32)section->address, (u32)section->address + section->size, 0x08000000, 0x08000000 + 0x00100000)) && 68 | (!inRange((u32)section->address, (u32)section->address + section->size, 0x18000000, 0x18000000 + 0x00600000)) && 69 | (!inRange((u32)section->address, (u32)section->address + section->size, 0x1FF00000, 0x1FFFFC00)) && 70 | (!(!isPreLockout && inRange((u32)section->address, (u32)section->address + section->size, 0x20000000, 0x20000000 + 0x8000000))))) 71 | return 0; 72 | 73 | if(firmHeader->arm9Entry >= section->address && firmHeader->arm9Entry < (section->address + section->size)) 74 | arm9EpFound = true; 75 | 76 | if(firmHeader->arm11Entry >= section->address && firmHeader->arm11Entry < (section->address + section->size)) 77 | arm11EpFound = true; 78 | } 79 | 80 | return (arm9EpFound && (firmHeader->arm11Entry == NULL || arm11EpFound)) ? size : 0; 81 | } 82 | 83 | bool checkSectionHashes(Firm *firm) 84 | { 85 | for(u32 i = 0; i < 4; i++) 86 | { 87 | FirmSection *section = &firm->section[i]; 88 | 89 | if(section->size == 0) 90 | continue; 91 | 92 | __attribute__((aligned(4))) u8 hash[0x20]; 93 | 94 | sha(hash, (u8 *)firm + section->offset, section->size, SHA_256_MODE); 95 | 96 | if(memcmp(hash, section->hash, 0x20) != 0) 97 | return false; 98 | } 99 | 100 | return true; 101 | } 102 | -------------------------------------------------------------------------------- /stage2/arm9/source/fatfs/diskio.c: -------------------------------------------------------------------------------- 1 | /*-----------------------------------------------------------------------*/ 2 | /* Low level disk I/O module skeleton for FatFs (C)ChaN, 2014 */ 3 | /*-----------------------------------------------------------------------*/ 4 | /* If a working storage control module is available, it should be */ 5 | /* attached to the FatFs via a glue function rather than modifying it. */ 6 | /* This is an example of glue functions to attach various exsisting */ 7 | /* storage control modules to the FatFs module with a defined API. */ 8 | /*-----------------------------------------------------------------------*/ 9 | 10 | #include "diskio.h" /* FatFs lower layer API */ 11 | #include "sdmmc/sdmmc.h" 12 | #include "../crypto.h" 13 | 14 | /* Definitions of physical drive number for each media */ 15 | #define SDCARD 0 16 | #define CTRNAND 1 17 | 18 | /*-----------------------------------------------------------------------*/ 19 | /* Get Drive Status */ 20 | /*-----------------------------------------------------------------------*/ 21 | 22 | DSTATUS disk_status ( 23 | __attribute__((unused)) 24 | BYTE pdrv /* Physical drive nmuber to identify the drive */ 25 | ) 26 | { 27 | return RES_OK; 28 | } 29 | 30 | 31 | 32 | /*-----------------------------------------------------------------------*/ 33 | /* Inidialize a Drive */ 34 | /*-----------------------------------------------------------------------*/ 35 | 36 | DSTATUS disk_initialize ( 37 | BYTE pdrv /* Physical drive nmuber to identify the drive */ 38 | ) 39 | { 40 | static u32 sdmmcInitResult = 4; 41 | 42 | if(sdmmcInitResult == 4) sdmmcInitResult = sdmmc_sdcard_init(); 43 | 44 | return ((pdrv == SDCARD && !(sdmmcInitResult & 2)) || 45 | (pdrv == CTRNAND && !(sdmmcInitResult & 1) && !ctrNandInit())) ? 0 : STA_NOINIT; 46 | } 47 | 48 | 49 | /*-----------------------------------------------------------------------*/ 50 | /* Read Sector(s) */ 51 | /*-----------------------------------------------------------------------*/ 52 | 53 | /*-----------------------------------------------------------------------*/ 54 | /* Read Sector(s) */ 55 | /*-----------------------------------------------------------------------*/ 56 | 57 | DRESULT disk_read ( 58 | BYTE pdrv, /* Physical drive nmuber to identify the drive */ 59 | BYTE *buff, /* Data buffer to store read data */ 60 | DWORD sector, /* Sector address in LBA */ 61 | UINT count /* Number of sectors to read */ 62 | ) 63 | { 64 | return ((pdrv == SDCARD && !sdmmc_sdcard_readsectors(sector, count, buff)) || 65 | (pdrv == CTRNAND && !ctrNandRead(sector, count, buff))) ? RES_OK : RES_PARERR; 66 | } 67 | 68 | 69 | 70 | /*-----------------------------------------------------------------------*/ 71 | /* Write Sector(s) */ 72 | /*-----------------------------------------------------------------------*/ 73 | 74 | #if _USE_WRITE 75 | DRESULT disk_write ( 76 | __attribute__((unused)) 77 | BYTE pdrv, /* Physical drive nmuber to identify the drive */ 78 | __attribute__((unused)) 79 | const BYTE *buff, /* Data to be written */ 80 | __attribute__((unused)) 81 | DWORD sector, /* Sector address in LBA */ 82 | __attribute__((unused)) 83 | UINT count /* Number of sectors to write */ 84 | ) 85 | { 86 | return (pdrv == SDCARD && !sdmmc_sdcard_writesectors(sector, count, buff)) ? RES_OK : RES_PARERR; 87 | } 88 | #endif 89 | 90 | 91 | 92 | /*-----------------------------------------------------------------------*/ 93 | /* Miscellaneous Functions */ 94 | /*-----------------------------------------------------------------------*/ 95 | 96 | #if _USE_IOCTL 97 | DRESULT disk_ioctl ( 98 | __attribute__((unused)) 99 | BYTE pdrv, /* Physical drive nmuber (0..) */ 100 | __attribute__((unused)) 101 | BYTE cmd, /* Control code */ 102 | __attribute__((unused)) 103 | void *buff /* Buffer to send/receive control data */ 104 | ) 105 | { 106 | return RES_PARERR; 107 | } 108 | #endif 109 | -------------------------------------------------------------------------------- /stage2/arm9/source/crypto.h: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Luma3DS 3 | * Copyright (C) 2016 Aurora Wright, TuxSH 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | * 18 | * Additional Terms 7.b of GPLv3 applies to this file: Requiring preservation of specified 19 | * reasonable legal notices or author attributions in that material or in the Appropriate Legal 20 | * Notices displayed by works containing it. 21 | */ 22 | 23 | /* 24 | * Crypto libs from http://github.com/b1l1s/ctr 25 | * kernel9Loader code originally adapted from https://github.com/Reisyukaku/ReiNand/blob/228c378255ba693133dec6f3368e14d386f2cde7/source/crypto.c#L233 26 | */ 27 | 28 | #pragma once 29 | 30 | #include "types.h" 31 | 32 | /**************************AES****************************/ 33 | #define REG_AESCNT ((vu32 *)0x10009000) 34 | #define REG_AESBLKCNT ((vu32 *)0x10009004) 35 | #define REG_AESWRFIFO ((vu32 *)0x10009008) 36 | #define REG_AESRDFIFO ((vu32 *)0x1000900C) 37 | #define REG_AESKEYSEL ((vu8 *)0x10009010) 38 | #define REG_AESKEYCNT ((vu8 *)0x10009011) 39 | #define REG_AESCTR ((vu32 *)0x10009020) 40 | 41 | #define REG_AESKEYFIFO ((vu32 *)0x10009100) 42 | #define REG_AESKEYXFIFO ((vu32 *)0x10009104) 43 | #define REG_AESKEYYFIFO ((vu32 *)0x10009108) 44 | 45 | #define REGs_AESTWLKEYS (*((vu32 (*)[4][3][4])0x10009040)) 46 | 47 | #define AES_CCM_DECRYPT_MODE (0u << 27) 48 | #define AES_CCM_ENCRYPT_MODE (1u << 27) 49 | #define AES_CTR_MODE (2u << 27) 50 | #define AES_CTR_MODE (2u << 27) 51 | #define AES_CBC_DECRYPT_MODE (4u << 27) 52 | #define AES_CBC_ENCRYPT_MODE (5u << 27) 53 | #define AES_ECB_DECRYPT_MODE (6u << 27) 54 | #define AES_ECB_ENCRYPT_MODE (7u << 27) 55 | #define AES_ALL_MODES (7u << 27) 56 | 57 | #define AES_CNT_START 0x80000000 58 | #define AES_CNT_INPUT_ORDER 0x02000000 59 | #define AES_CNT_OUTPUT_ORDER 0x01000000 60 | #define AES_CNT_INPUT_ENDIAN 0x00800000 61 | #define AES_CNT_OUTPUT_ENDIAN 0x00400000 62 | #define AES_CNT_FLUSH_READ 0x00000800 63 | #define AES_CNT_FLUSH_WRITE 0x00000400 64 | 65 | #define AES_INPUT_BE (AES_CNT_INPUT_ENDIAN) 66 | #define AES_INPUT_LE 0 67 | #define AES_INPUT_NORMAL (AES_CNT_INPUT_ORDER) 68 | #define AES_INPUT_REVERSED 0 69 | #define AES_INPUT_TWLNORMAL 0 70 | #define AES_INPUT_TWLREVERSED (AES_CNT_INPUT_ORDER) 71 | 72 | #define AES_BLOCK_SIZE 0x10 73 | 74 | #define AES_KEYCNT_WRITE (1 << 0x7) 75 | #define AES_KEYNORMAL 0 76 | #define AES_KEYX 1 77 | #define AES_KEYY 2 78 | 79 | /**************************SHA****************************/ 80 | #define REG_SHA_CNT ((vu32 *)0x1000A000) 81 | #define REG_SHA_BLKCNT ((vu32 *)0x1000A004) 82 | #define REG_SHA_HASH ((vu32 *)0x1000A040) 83 | #define REG_SHA_INFIFO ((vu32 *)0x1000A080) 84 | 85 | #define SHA_CNT_STATE 0x00000003 86 | #define SHA_CNT_UNK2 0x00000004 87 | #define SHA_CNT_OUTPUT_ENDIAN 0x00000008 88 | #define SHA_CNT_MODE 0x00000030 89 | #define SHA_CNT_ENABLE 0x00010000 90 | #define SHA_CNT_ACTIVE 0x00020000 91 | 92 | #define SHA_HASH_READY 0x00000000 93 | #define SHA_NORMAL_ROUND 0x00000001 94 | #define SHA_FINAL_ROUND 0x00000002 95 | 96 | #define SHA_OUTPUT_BE SHA_CNT_OUTPUT_ENDIAN 97 | #define SHA_OUTPUT_LE 0 98 | 99 | #define SHA_256_MODE 0 100 | #define SHA_224_MODE 0x00000010 101 | #define SHA_1_MODE 0x00000020 102 | 103 | #define SHA_256_HASH_SIZE (256 / 8) 104 | #define SHA_224_HASH_SIZE (224 / 8) 105 | #define SHA_1_HASH_SIZE (160 / 8) 106 | 107 | #define CFG_SYSPROT9 (*(vu8 *)0x10000000) 108 | #define CFG_BOOTENV (*(vu32 *)0x10010000) 109 | #define CFG_UNITINFO (*(vu8 *)0x10010010) 110 | #define CFG_TWLUNITINFO (*(vu8 *)0x10010014) 111 | #define OTP_DEVCONSOLEID (*(vu64 *)0x10012000) 112 | #define OTP_TWLCONSOLEID (*(vu64 *)0x10012100) 113 | #define CFG11_SOCINFO (*(vu32 *)0x10140FFC) 114 | 115 | #define ISN3DS (CFG11_SOCINFO & 2) 116 | #define ISDEVUNIT (CFG_UNITINFO != 0) 117 | 118 | void sha(void *res, const void *src, u32 size, u32 mode); 119 | 120 | int ctrNandInit(void); 121 | int ctrNandRead(u32 sector, u32 sectorCount, u8 *outbuf); 122 | void setupKeyslots(void); 123 | -------------------------------------------------------------------------------- /stage2/arm9/source/i2c.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Luma3DS 3 | * Copyright (C) 2016 Aurora Wright, TuxSH 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | * 18 | * Additional Terms 7.b of GPLv3 applies to this file: Requiring preservation of specified 19 | * reasonable legal notices or author attributions in that material or in the Appropriate Legal 20 | * Notices displayed by works containing it. 21 | */ 22 | 23 | /* 24 | * Thanks to the everyone who contributed in the development of this file 25 | */ 26 | 27 | #include "utils.h" 28 | #include "i2c.h" 29 | 30 | //----------------------------------------------------------------------------- 31 | 32 | static const struct { u8 bus_id, reg_addr; } dev_data[] = { 33 | {0, 0x4A}, {0, 0x7A}, {0, 0x78}, 34 | {1, 0x4A}, {1, 0x78}, {1, 0x2C}, 35 | {1, 0x2E}, {1, 0x40}, {1, 0x44}, 36 | {2, 0xD6}, {2, 0xD0}, {2, 0xD2}, 37 | {2, 0xA4}, {2, 0x9A}, {2, 0xA0}, 38 | }; 39 | 40 | static inline u8 i2cGetDeviceBusId(u8 device_id) 41 | { 42 | return dev_data[device_id].bus_id; 43 | } 44 | 45 | static inline u8 i2cGetDeviceRegAddr(u8 device_id) 46 | { 47 | return dev_data[device_id].reg_addr; 48 | } 49 | 50 | //----------------------------------------------------------------------------- 51 | 52 | static vu8 *reg_data_addrs[] = { 53 | (vu8 *)(I2C1_REG_OFF + I2C_REG_DATA), 54 | (vu8 *)(I2C2_REG_OFF + I2C_REG_DATA), 55 | (vu8 *)(I2C3_REG_OFF + I2C_REG_DATA), 56 | }; 57 | 58 | static inline vu8 *i2cGetDataReg(u8 bus_id) 59 | { 60 | return reg_data_addrs[bus_id]; 61 | } 62 | 63 | //----------------------------------------------------------------------------- 64 | 65 | static vu8 *reg_cnt_addrs[] = { 66 | (vu8 *)(I2C1_REG_OFF + I2C_REG_CNT), 67 | (vu8 *)(I2C2_REG_OFF + I2C_REG_CNT), 68 | (vu8 *)(I2C3_REG_OFF + I2C_REG_CNT), 69 | }; 70 | 71 | static inline vu8 *i2cGetCntReg(u8 bus_id) 72 | { 73 | return reg_cnt_addrs[bus_id]; 74 | } 75 | 76 | //----------------------------------------------------------------------------- 77 | 78 | static inline void i2cWaitBusy(u8 bus_id) 79 | { 80 | while (*i2cGetCntReg(bus_id) & 0x80); 81 | } 82 | 83 | static inline bool i2cGetResult(u8 bus_id) 84 | { 85 | i2cWaitBusy(bus_id); 86 | 87 | return (*i2cGetCntReg(bus_id) >> 4) & 1; 88 | } 89 | 90 | static void i2cStop(u8 bus_id, u8 arg0) 91 | { 92 | *i2cGetCntReg(bus_id) = (arg0 << 5) | 0xC0; 93 | i2cWaitBusy(bus_id); 94 | *i2cGetCntReg(bus_id) = 0xC5; 95 | } 96 | 97 | //----------------------------------------------------------------------------- 98 | 99 | static bool i2cSelectDevice(u8 bus_id, u8 dev_reg) 100 | { 101 | i2cWaitBusy(bus_id); 102 | *i2cGetDataReg(bus_id) = dev_reg; 103 | *i2cGetCntReg(bus_id) = 0xC2; 104 | 105 | return i2cGetResult(bus_id); 106 | } 107 | 108 | static bool i2cSelectRegister(u8 bus_id, u8 reg) 109 | { 110 | i2cWaitBusy(bus_id); 111 | *i2cGetDataReg(bus_id) = reg; 112 | *i2cGetCntReg(bus_id) = 0xC0; 113 | 114 | return i2cGetResult(bus_id); 115 | } 116 | 117 | //----------------------------------------------------------------------------- 118 | 119 | u8 i2cReadRegister(u8 dev_id, u8 reg) 120 | { 121 | u8 bus_id = i2cGetDeviceBusId(dev_id), 122 | dev_addr = i2cGetDeviceRegAddr(dev_id), 123 | ret = 0xFF; 124 | 125 | for(u32 i = 0; i < 8 && ret == 0xFF; i++) 126 | { 127 | if(i2cSelectDevice(bus_id, dev_addr) && i2cSelectRegister(bus_id, reg)) 128 | { 129 | if(i2cSelectDevice(bus_id, dev_addr | 1)) 130 | { 131 | i2cWaitBusy(bus_id); 132 | i2cStop(bus_id, 1); 133 | i2cWaitBusy(bus_id); 134 | 135 | ret = *i2cGetDataReg(bus_id); 136 | } 137 | } 138 | *i2cGetCntReg(bus_id) = 0xC5; 139 | i2cWaitBusy(bus_id); 140 | } 141 | 142 | wait(3ULL); 143 | 144 | return ret; 145 | } 146 | 147 | bool i2cWriteRegister(u8 dev_id, u8 reg, u8 data) 148 | { 149 | u8 bus_id = i2cGetDeviceBusId(dev_id), 150 | dev_addr = i2cGetDeviceRegAddr(dev_id); 151 | 152 | bool ret = false; 153 | 154 | for(u32 i = 0; i < 8 && !ret; i++) 155 | { 156 | if(i2cSelectDevice(bus_id, dev_addr) && i2cSelectRegister(bus_id, reg)) 157 | { 158 | i2cWaitBusy(bus_id); 159 | *i2cGetDataReg(bus_id) = data; 160 | *i2cGetCntReg(bus_id) = 0xC1; 161 | i2cStop(bus_id, 0); 162 | 163 | if(i2cGetResult(bus_id)) ret = true; 164 | } 165 | *i2cGetCntReg(bus_id) = 0xC5; 166 | i2cWaitBusy(bus_id); 167 | } 168 | 169 | wait(3ULL); 170 | 171 | return ret; 172 | } -------------------------------------------------------------------------------- /stage2/arm11/source/main.c: -------------------------------------------------------------------------------- 1 | #include "types.h" 2 | #include "memory.h" 3 | 4 | #define BRIGHTNESS 0x39 5 | 6 | void prepareForFirmlaunch(void); 7 | extern u32 prepareForFirmlaunchSize; 8 | 9 | extern volatile Arm11Operation operation; 10 | 11 | static void initScreens(void) 12 | { 13 | *(vu32 *)0x10141200 = 0x1007F; 14 | *(vu32 *)0x10202014 = 0x00000001; 15 | *(vu32 *)0x1020200C &= 0xFFFEFFFE; 16 | 17 | *(vu32 *)0x10202240 = BRIGHTNESS; 18 | *(vu32 *)0x10202A40 = BRIGHTNESS; 19 | *(vu32 *)0x10202244 = 0x1023E; 20 | *(vu32 *)0x10202A44 = 0x1023E; 21 | 22 | //Top screen 23 | *(vu32 *)0x10400400 = 0x000001c2; 24 | *(vu32 *)0x10400404 = 0x000000d1; 25 | *(vu32 *)0x10400408 = 0x000001c1; 26 | *(vu32 *)0x1040040c = 0x000001c1; 27 | *(vu32 *)0x10400410 = 0x00000000; 28 | *(vu32 *)0x10400414 = 0x000000cf; 29 | *(vu32 *)0x10400418 = 0x000000d1; 30 | *(vu32 *)0x1040041c = 0x01c501c1; 31 | *(vu32 *)0x10400420 = 0x00010000; 32 | *(vu32 *)0x10400424 = 0x0000019d; 33 | *(vu32 *)0x10400428 = 0x00000002; 34 | *(vu32 *)0x1040042c = 0x00000192; 35 | *(vu32 *)0x10400430 = 0x00000192; 36 | *(vu32 *)0x10400434 = 0x00000192; 37 | *(vu32 *)0x10400438 = 0x00000001; 38 | *(vu32 *)0x1040043c = 0x00000002; 39 | *(vu32 *)0x10400440 = 0x01960192; 40 | *(vu32 *)0x10400444 = 0x00000000; 41 | *(vu32 *)0x10400448 = 0x00000000; 42 | *(vu32 *)0x1040045C = 0x00f00190; 43 | *(vu32 *)0x10400460 = 0x01c100d1; 44 | *(vu32 *)0x10400464 = 0x01920002; 45 | *(vu32 *)0x10400468 = 0x18300000; 46 | *(vu32 *)0x10400470 = 0x80341; 47 | *(vu32 *)0x10400474 = 0x00010501; 48 | *(vu32 *)0x10400478 = 0; 49 | *(vu32 *)0x10400490 = 0x000002D0; 50 | *(vu32 *)0x1040049C = 0x00000000; 51 | 52 | //Disco register 53 | for(u32 i = 0; i < 256; i++) 54 | *(vu32 *)0x10400484 = 0x10101 * i; 55 | 56 | //Bottom screen 57 | *(vu32 *)0x10400500 = 0x000001c2; 58 | *(vu32 *)0x10400504 = 0x000000d1; 59 | *(vu32 *)0x10400508 = 0x000001c1; 60 | *(vu32 *)0x1040050c = 0x000001c1; 61 | *(vu32 *)0x10400510 = 0x000000cd; 62 | *(vu32 *)0x10400514 = 0x000000cf; 63 | *(vu32 *)0x10400518 = 0x000000d1; 64 | *(vu32 *)0x1040051c = 0x01c501c1; 65 | *(vu32 *)0x10400520 = 0x00010000; 66 | *(vu32 *)0x10400524 = 0x0000019d; 67 | *(vu32 *)0x10400528 = 0x00000052; 68 | *(vu32 *)0x1040052c = 0x00000192; 69 | *(vu32 *)0x10400530 = 0x00000192; 70 | *(vu32 *)0x10400534 = 0x0000004f; 71 | *(vu32 *)0x10400538 = 0x00000050; 72 | *(vu32 *)0x1040053c = 0x00000052; 73 | *(vu32 *)0x10400540 = 0x01980194; 74 | *(vu32 *)0x10400544 = 0x00000000; 75 | *(vu32 *)0x10400548 = 0x00000011; 76 | *(vu32 *)0x1040055C = 0x00f00140; 77 | *(vu32 *)0x10400560 = 0x01c100d1; 78 | *(vu32 *)0x10400564 = 0x01920052; 79 | *(vu32 *)0x10400568 = 0x18300000 + 0x46500; 80 | *(vu32 *)0x10400570 = 0x80301; 81 | *(vu32 *)0x10400574 = 0x00010501; 82 | *(vu32 *)0x10400578 = 0; 83 | *(vu32 *)0x10400590 = 0x000002D0; 84 | *(vu32 *)0x1040059C = 0x00000000; 85 | 86 | //Disco register 87 | for(u32 i = 0; i < 256; i++) 88 | *(vu32 *)0x10400584 = 0x10101 * i; 89 | 90 | *(vu32 *)0x10400468 = 0x18300000; 91 | *(vu32 *)0x1040046c = 0x18400000; 92 | *(vu32 *)0x10400494 = 0x18300000; 93 | *(vu32 *)0x10400498 = 0x18400000; 94 | *(vu32 *)0x10400568 = 0x18346500; 95 | *(vu32 *)0x1040056c = 0x18446500; 96 | 97 | //Clear both framebuffer sets 98 | vu32 *REGs_PSC0 = (vu32 *)0x10400010, 99 | *REGs_PSC1 = (vu32 *)0x10400020; 100 | 101 | REGs_PSC0[0] = 0x18300000 >> 3; //Start address 102 | REGs_PSC0[1] = (0x18300000 + SCREEN_TOP_FBSIZE) >> 3; //End address 103 | REGs_PSC0[2] = 0; //Fill value 104 | REGs_PSC0[3] = (2 << 8) | 1; //32-bit pattern; start 105 | 106 | REGs_PSC1[0] = 0x18346500 >> 3; //Start address 107 | REGs_PSC1[1] = (0x18346500 + SCREEN_BOTTOM_FBSIZE) >> 3; //End address 108 | REGs_PSC1[2] = 0; //Fill value 109 | REGs_PSC1[3] = (2 << 8) | 1; //32-bit pattern; start 110 | 111 | while(!((REGs_PSC0[3] & 2) && (REGs_PSC1[3] & 2))); 112 | 113 | REGs_PSC0[0] = 0x18400000 >> 3; //Start address 114 | REGs_PSC0[1] = (0x18400000 + SCREEN_TOP_FBSIZE) >> 3; //End address 115 | REGs_PSC0[2] = 0; //Fill value 116 | REGs_PSC0[3] = (2 << 8) | 1; //32-bit pattern; start 117 | 118 | REGs_PSC1[0] = 0x18446500 >> 3; //Start address 119 | REGs_PSC1[1] = (0x18446500 + SCREEN_BOTTOM_FBSIZE) >> 3; //End address 120 | REGs_PSC1[2] = 0; //Fill value 121 | REGs_PSC1[3] = (2 << 8) | 1; //32-bit pattern; start 122 | 123 | while(!((REGs_PSC0[3] & 2) && (REGs_PSC1[3] & 2))); 124 | } 125 | 126 | static void waitBootromLocked(void) 127 | { 128 | while(*(vu64 *)0x18000 != 0ULL); 129 | } 130 | 131 | void main(void) 132 | { 133 | operation = ARM11_READY; 134 | 135 | while(true) 136 | { 137 | switch(operation) 138 | { 139 | case ARM11_READY: 140 | continue; 141 | case INIT_SCREENS: 142 | initScreens(); 143 | break; 144 | case WAIT_BOOTROM11_LOCKED: 145 | waitBootromLocked(); 146 | break; 147 | case PREPARE_ARM11_FOR_FIRMLAUNCH: 148 | memcpy((void *)0x1FFFFC00, (void *)prepareForFirmlaunch, prepareForFirmlaunchSize); 149 | *(vu32 *)0x1FFFFFFC = 0; 150 | ((void (*)(u32, volatile Arm11Operation *))0x1FFFFC00)(ARM11_READY, &operation); 151 | } 152 | 153 | operation = ARM11_READY; 154 | } 155 | } 156 | -------------------------------------------------------------------------------- /stage2/arm9/source/fatfs/ffsystem.c: -------------------------------------------------------------------------------- 1 | /*------------------------------------------------------------------------*/ 2 | /* Sample code of OS dependent controls for FatFs */ 3 | /* (C)ChaN, 2017 */ 4 | /*------------------------------------------------------------------------*/ 5 | 6 | 7 | #include "ff.h" 8 | 9 | 10 | 11 | #if FF_USE_LFN == 3 /* Dynamic memory allocation */ 12 | 13 | /*------------------------------------------------------------------------*/ 14 | /* Allocate a memory block */ 15 | /*------------------------------------------------------------------------*/ 16 | 17 | void* ff_memalloc ( /* Returns pointer to the allocated memory block (null on not enough core) */ 18 | UINT msize /* Number of bytes to allocate */ 19 | ) 20 | { 21 | return malloc(msize); /* Allocate a new memory block with POSIX API */ 22 | } 23 | 24 | 25 | /*------------------------------------------------------------------------*/ 26 | /* Free a memory block */ 27 | /*------------------------------------------------------------------------*/ 28 | 29 | void ff_memfree ( 30 | void* mblock /* Pointer to the memory block to free */ 31 | ) 32 | { 33 | free(mblock); /* Free the memory block with POSIX API */ 34 | } 35 | 36 | #endif 37 | 38 | 39 | 40 | #if FF_FS_REENTRANT /* Mutal exclusion */ 41 | 42 | /*------------------------------------------------------------------------*/ 43 | /* Create a Synchronization Object */ 44 | /*------------------------------------------------------------------------*/ 45 | /* This function is called in f_mount() function to create a new 46 | / synchronization object for the volume, such as semaphore and mutex. 47 | / When a 0 is returned, the f_mount() function fails with FR_INT_ERR. 48 | */ 49 | 50 | //const osMutexDef_t Mutex[FF_VOLUMES]; /* CMSIS-RTOS */ 51 | 52 | 53 | int ff_cre_syncobj ( /* 1:Function succeeded, 0:Could not create the sync object */ 54 | BYTE vol, /* Corresponding volume (logical drive number) */ 55 | FF_SYNC_t *sobj /* Pointer to return the created sync object */ 56 | ) 57 | { 58 | /* Win32 */ 59 | *sobj = CreateMutex(NULL, FALSE, NULL); 60 | return (int)(*sobj != INVALID_HANDLE_VALUE); 61 | 62 | /* uITRON */ 63 | // T_CSEM csem = {TA_TPRI,1,1}; 64 | // *sobj = acre_sem(&csem); 65 | // return (int)(*sobj > 0); 66 | 67 | /* uC/OS-II */ 68 | // OS_ERR err; 69 | // *sobj = OSMutexCreate(0, &err); 70 | // return (int)(err == OS_NO_ERR); 71 | 72 | /* FreeRTOS */ 73 | // *sobj = xSemaphoreCreateMutex(); 74 | // return (int)(*sobj != NULL); 75 | 76 | /* CMSIS-RTOS */ 77 | // *sobj = osMutexCreate(Mutex + vol); 78 | // return (int)(*sobj != NULL); 79 | } 80 | 81 | 82 | /*------------------------------------------------------------------------*/ 83 | /* Delete a Synchronization Object */ 84 | /*------------------------------------------------------------------------*/ 85 | /* This function is called in f_mount() function to delete a synchronization 86 | / object that created with ff_cre_syncobj() function. When a 0 is returned, 87 | / the f_mount() function fails with FR_INT_ERR. 88 | */ 89 | 90 | int ff_del_syncobj ( /* 1:Function succeeded, 0:Could not delete due to an error */ 91 | FF_SYNC_t sobj /* Sync object tied to the logical drive to be deleted */ 92 | ) 93 | { 94 | /* Win32 */ 95 | return (int)CloseHandle(sobj); 96 | 97 | /* uITRON */ 98 | // return (int)(del_sem(sobj) == E_OK); 99 | 100 | /* uC/OS-II */ 101 | // OS_ERR err; 102 | // OSMutexDel(sobj, OS_DEL_ALWAYS, &err); 103 | // return (int)(err == OS_NO_ERR); 104 | 105 | /* FreeRTOS */ 106 | // vSemaphoreDelete(sobj); 107 | // return 1; 108 | 109 | /* CMSIS-RTOS */ 110 | // return (int)(osMutexDelete(sobj) == osOK); 111 | } 112 | 113 | 114 | /*------------------------------------------------------------------------*/ 115 | /* Request Grant to Access the Volume */ 116 | /*------------------------------------------------------------------------*/ 117 | /* This function is called on entering file functions to lock the volume. 118 | / When a 0 is returned, the file function fails with FR_TIMEOUT. 119 | */ 120 | 121 | int ff_req_grant ( /* 1:Got a grant to access the volume, 0:Could not get a grant */ 122 | FF_SYNC_t sobj /* Sync object to wait */ 123 | ) 124 | { 125 | /* Win32 */ 126 | return (int)(WaitForSingleObject(sobj, FF_FS_TIMEOUT) == WAIT_OBJECT_0); 127 | 128 | /* uITRON */ 129 | // return (int)(wai_sem(sobj) == E_OK); 130 | 131 | /* uC/OS-II */ 132 | // OS_ERR err; 133 | // OSMutexPend(sobj, FF_FS_TIMEOUT, &err)); 134 | // return (int)(err == OS_NO_ERR); 135 | 136 | /* FreeRTOS */ 137 | // return (int)(xSemaphoreTake(sobj, FF_FS_TIMEOUT) == pdTRUE); 138 | 139 | /* CMSIS-RTOS */ 140 | // return (int)(osMutexWait(sobj, FF_FS_TIMEOUT) == osOK); 141 | } 142 | 143 | 144 | /*------------------------------------------------------------------------*/ 145 | /* Release Grant to Access the Volume */ 146 | /*------------------------------------------------------------------------*/ 147 | /* This function is called on leaving file functions to unlock the volume. 148 | */ 149 | 150 | void ff_rel_grant ( 151 | FF_SYNC_t sobj /* Sync object to be signaled */ 152 | ) 153 | { 154 | /* Win32 */ 155 | ReleaseMutex(sobj); 156 | 157 | /* uITRON */ 158 | // sig_sem(sobj); 159 | 160 | /* uC/OS-II */ 161 | // OSMutexPost(sobj); 162 | 163 | /* FreeRTOS */ 164 | // xSemaphoreGive(sobj); 165 | 166 | /* CMSIS-RTOS */ 167 | // osMutexRelease(sobj); 168 | } 169 | 170 | #endif 171 | 172 | -------------------------------------------------------------------------------- /boot9strap.s: -------------------------------------------------------------------------------- 1 | .arm.little 2 | 3 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 4 | ; Useful constant addresses. 5 | 6 | b9_memcpy equ 0xFFFF03F0 7 | b9_store_addr equ 0x08080000 8 | b11_store_addr equ 0x08090000 9 | b11_axi_addr equ 0x1FFC0000 10 | 11 | code_11_load_addr equ 0x1FF80000 12 | 13 | arm9mem_dabrt_loc equ 0x08000028 14 | 15 | .create "build/code9.bin",0x08000200 16 | 17 | .area 0x1F0 18 | 19 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 20 | ; Boot9 Data Abort handler: Overwrites two boot9 function pointers, then returns. 21 | dabort_handler: 22 | ldr r3, =0x080003F8 ; r3 = flag_loc 23 | ldr r2, [r3] ; r2 = *(flag_loc) 24 | cmp r2, #0x0 ; did we do this yet? 25 | bne handler_done ; if so, were done here. 26 | 27 | str r2, [r3] ; write to "did we do this yet?" flag. 28 | ldr r3, =0xFFF00058 ; dtcm funcptr_1 29 | ldr r2, =b9_hook_1 ; r2 = b9_hook_1 30 | str r2, [r3] ; Overwrite first function pointer 31 | ldr r2, =b9_hook_2 ; r2 = b9_hook_2 32 | str r2, [r3, #0x4] ; Overwrite second function pointer 33 | handler_done: 34 | mov r2, #0x0 35 | mov r3, #0x0 36 | subs pc, lr, #0x4 ; Skip the offending dabrt instruction 37 | 38 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 39 | ; b9_hook_1: Wait for boot11 to finish a task, then overwrite a function pointer 40 | ; it will call just before lockout. Then, setup the MPU, and return. 41 | b9_hook_1: 42 | pwn_b11: 43 | stmfd sp!, {r0-r6, lr} 44 | ldr r0, =0x1FFE802C ; r0 = b11_funcptr_address 45 | ldr r1, =code_11_load_addr ; r1 = our_boot11_hook 46 | str r1, [r0] ; overwrite value 47 | wait_loop: ; This is actually a ToCToU race condition. 48 | ldr r2, [r0] ; Derefence 49 | cmp r2, r1 ; Has stored value changed? 50 | beq wait_loop ; If not, go back. 51 | str r1, [r0] ; Overwrite final funcptr. 52 | 53 | ; setup_mpu: 54 | setup_mpu: 55 | ldr r0, =0x33333333 56 | mcr p15,0,r0,c5,c0,3 57 | mcr p15,0,r0,c5,c0,2 58 | 59 | mov r0, #0x0 60 | ldr r1, =b11_axi_addr 61 | str r0, [r1, #-0x4] ; Ensure that b11 knows when we are ready. 62 | ldmfd sp!, {r0-r6, pc} 63 | 64 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 65 | ; b9_hook_2: Dump the arm11 bootrom, synchronize with boot11, dump the arm9 66 | ; bootrom, and then return to execution. 67 | b9_hook_2: 68 | mov r11, r0 69 | 70 | ; Dump boot11 71 | ldr r0, =b11_axi_addr 72 | wait_for_b11_exec: ; Wait for boot11 hook to set a flag in axiwram 73 | ldr r1, [r0, #-0x4] 74 | cmp r1, #0x0 75 | beq wait_for_b11_exec 76 | 77 | ldr r1, =b11_store_addr ; memcpy the arm11 bootrom into safe arm9mem 78 | mov r2, #0x10000 79 | ldr r3, =b9_memcpy 80 | blx r3 81 | 82 | ; Let Boot11 know weve copied it. 83 | ldr r0, =b11_axi_addr 84 | mov r1, #0x0 85 | str r1, [r0, #-0x4] 86 | 87 | ; Dump boot9 88 | ldr r0, =0xFFFF0000 89 | ldr r1, =b9_store_addr 90 | mov r2, #0x10000 91 | ldr r3, =b9_memcpy 92 | blx r3 93 | 94 | bx r11 ; Jump to entrypoint 95 | 96 | .pool 97 | 98 | .endarea 99 | 100 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 101 | ; dabrt_vector: The data abort vector copied over by our NDMA write. 102 | .org 0x080003F0 103 | .area 0x10 104 | dabrt_vector: 105 | ldr pc, [pc, #-0x4] 106 | .dw dabort_handler 107 | .dw 0 ; has dabort handler run flag 108 | .dw 0 109 | .endarea 110 | 111 | 112 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 113 | ; stage 2: Load stage 2 payload to 0x08001000. 114 | .org 0x08001000 115 | .area 0x10000 116 | .incbin "stage2/arm9/out/arm9.bin" 117 | .endarea 118 | .align 0x200 119 | 120 | .close 121 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 122 | 123 | .create "build/code11.bin",code_11_load_addr 124 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 125 | ; boot11_hook: This code is called by boot11 just before lockout. 126 | ; It copies the bootrom to axi_wram, then syncs with 127 | ; boot9 hook. 128 | boot11_hook: 129 | mov r11, r0 130 | 131 | ldr r1, =b11_axi_addr 132 | ldr r0, =0x10000 133 | mov r2, #0x0 134 | b11_copy_loop: ; Simple memcpy loop from boot11 to axiwram. 135 | ldr r3, [r0, r2] 136 | str r3, [r1, r2] 137 | add r2, r2, #0x4 138 | cmp r2, r0 139 | blt b11_copy_loop 140 | 141 | ldr r1, =b11_axi_addr ; Let boot9 know that we are done. 142 | mov r0, #0x1 143 | str r0, [r1, #-0x4] 144 | 145 | wait_for_b9_copy: ; Wait for boot9 to confirm it received our dump. 146 | ldr r0, [r1, #-0x4] 147 | cmp r0, #0x0 148 | bne wait_for_b9_copy 149 | 150 | bx r11 ; Jump to entrypoint 151 | 152 | .pool 153 | 154 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 155 | ; arm11 stage 2 156 | .org (code_11_load_addr+0x200) 157 | 158 | ; this only runs on core0 159 | 160 | .area 0x10000 161 | .incbin "stage2/arm11/out/arm11.bin" 162 | .endarea 163 | .align 0x200 164 | 165 | .close 166 | 167 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 168 | ; NDMA section: This generates the NDMA overwrite file. 169 | 170 | .create "build/NDMA.bin",0 171 | .area 0x200 172 | .dw 0x00000000 ; NDMA Global CNT 173 | .dw dabrt_vector ; Source Address 174 | .dw arm9mem_dabrt_loc ; Destination Address 175 | .dw 0x00000000 ; Unused Total Repeat Length 176 | .dw 0x00000002 ; Transfer 2 words 177 | .dw 0x00000000 ; Transfer until completed 178 | .dw 0x00000000 ; Unused Fill Data 179 | .dw 0x90010000 ; Start Immediately/Transfer 2 words at a time/Enable 180 | .endarea 181 | .align 0x200 182 | .close 183 | 184 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 185 | ; Data abort section: This is just a single sector causes boot9 to data abort. 186 | 187 | .create "build/dabrt.bin",0 188 | .area 0x200, 0xFF 189 | .endarea 190 | .close 191 | -------------------------------------------------------------------------------- /stage2/arm9/source/fatfs/00history.txt: -------------------------------------------------------------------------------- 1 | ---------------------------------------------------------------------------- 2 | Revision history of FatFs module 3 | ---------------------------------------------------------------------------- 4 | 5 | R0.00 (February 26, 2006) 6 | 7 | Prototype. 8 | 9 | 10 | 11 | R0.01 (April 29, 2006) 12 | 13 | The first release. 14 | 15 | 16 | 17 | R0.02 (June 01, 2006) 18 | 19 | Added FAT12 support. 20 | Removed unbuffered mode. 21 | Fixed a problem on small (<32M) partition. 22 | 23 | 24 | 25 | R0.02a (June 10, 2006) 26 | 27 | Added a configuration option (_FS_MINIMUM). 28 | 29 | 30 | 31 | R0.03 (September 22, 2006) 32 | 33 | Added f_rename(). 34 | Changed option _FS_MINIMUM to _FS_MINIMIZE. 35 | 36 | 37 | 38 | R0.03a (December 11, 2006) 39 | 40 | Improved cluster scan algorithm to write files fast. 41 | Fixed f_mkdir() creates incorrect directory on FAT32. 42 | 43 | 44 | 45 | R0.04 (February 04, 2007) 46 | 47 | Added f_mkfs(). 48 | Supported multiple drive system. 49 | Changed some interfaces for multiple drive system. 50 | Changed f_mountdrv() to f_mount(). 51 | 52 | 53 | 54 | R0.04a (April 01, 2007) 55 | 56 | Supported multiple partitions on a physical drive. 57 | Added a capability of extending file size to f_lseek(). 58 | Added minimization level 3. 59 | Fixed an endian sensitive code in f_mkfs(). 60 | 61 | 62 | 63 | R0.04b (May 05, 2007) 64 | 65 | Added a configuration option _USE_NTFLAG. 66 | Added FSINFO support. 67 | Fixed DBCS name can result FR_INVALID_NAME. 68 | Fixed short seek (<= csize) collapses the file object. 69 | 70 | 71 | 72 | R0.05 (August 25, 2007) 73 | 74 | Changed arguments of f_read(), f_write() and f_mkfs(). 75 | Fixed f_mkfs() on FAT32 creates incorrect FSINFO. 76 | Fixed f_mkdir() on FAT32 creates incorrect directory. 77 | 78 | 79 | 80 | R0.05a (February 03, 2008) 81 | 82 | Added f_truncate() and f_utime(). 83 | Fixed off by one error at FAT sub-type determination. 84 | Fixed btr in f_read() can be mistruncated. 85 | Fixed cached sector is not flushed when create and close without write. 86 | 87 | 88 | 89 | R0.06 (April 01, 2008) 90 | 91 | Added fputc(), fputs(), fprintf() and fgets(). 92 | Improved performance of f_lseek() on moving to the same or following cluster. 93 | 94 | 95 | 96 | R0.07 (April 01, 2009) 97 | 98 | Merged Tiny-FatFs as a configuration option. (_FS_TINY) 99 | Added long file name feature. (_USE_LFN) 100 | Added multiple code page feature. (_CODE_PAGE) 101 | Added re-entrancy for multitask operation. (_FS_REENTRANT) 102 | Added auto cluster size selection to f_mkfs(). 103 | Added rewind option to f_readdir(). 104 | Changed result code of critical errors. 105 | Renamed string functions to avoid name collision. 106 | 107 | 108 | 109 | R0.07a (April 14, 2009) 110 | 111 | Septemberarated out OS dependent code on reentrant cfg. 112 | Added multiple sector size feature. 113 | 114 | 115 | 116 | R0.07c (June 21, 2009) 117 | 118 | Fixed f_unlink() can return FR_OK on error. 119 | Fixed wrong cache control in f_lseek(). 120 | Added relative path feature. 121 | Added f_chdir() and f_chdrive(). 122 | Added proper case conversion to extended character. 123 | 124 | 125 | 126 | R0.07e (November 03, 2009) 127 | 128 | Septemberarated out configuration options from ff.h to ffconf.h. 129 | Fixed f_unlink() fails to remove a sub-directory on _FS_RPATH. 130 | Fixed name matching error on the 13 character boundary. 131 | Added a configuration option, _LFN_UNICODE. 132 | Changed f_readdir() to return the SFN with always upper case on non-LFN cfg. 133 | 134 | 135 | 136 | R0.08 (May 15, 2010) 137 | 138 | Added a memory configuration option. (_USE_LFN = 3) 139 | Added file lock feature. (_FS_SHARE) 140 | Added fast seek feature. (_USE_FASTSEEK) 141 | Changed some types on the API, XCHAR->TCHAR. 142 | Changed .fname in the FILINFO structure on Unicode cfg. 143 | String functions support UTF-8 encoding files on Unicode cfg. 144 | 145 | 146 | 147 | R0.08a (August 16, 2010) 148 | 149 | Added f_getcwd(). (_FS_RPATH = 2) 150 | Added sector erase feature. (_USE_ERASE) 151 | Moved file lock semaphore table from fs object to the bss. 152 | Fixed f_mkfs() creates wrong FAT32 volume. 153 | 154 | 155 | 156 | R0.08b (January 15, 2011) 157 | 158 | Fast seek feature is also applied to f_read() and f_write(). 159 | f_lseek() reports required table size on creating CLMP. 160 | Extended format syntax of f_printf(). 161 | Ignores duplicated directory separators in given path name. 162 | 163 | 164 | 165 | R0.09 (September 06, 2011) 166 | 167 | f_mkfs() supports multiple partition to complete the multiple partition feature. 168 | Added f_fdisk(). 169 | 170 | 171 | 172 | R0.09a (August 27, 2012) 173 | 174 | Changed f_open() and f_opendir() reject null object pointer to avoid crash. 175 | Changed option name _FS_SHARE to _FS_LOCK. 176 | Fixed assertion failure due to OS/2 EA on FAT12/16 volume. 177 | 178 | 179 | 180 | R0.09b (January 24, 2013) 181 | 182 | Added f_setlabel() and f_getlabel(). 183 | 184 | 185 | 186 | R0.10 (October 02, 2013) 187 | 188 | Added selection of character encoding on the file. (_STRF_ENCODE) 189 | Added f_closedir(). 190 | Added forced full FAT scan for f_getfree(). (_FS_NOFSINFO) 191 | Added forced mount feature with changes of f_mount(). 192 | Improved behavior of volume auto detection. 193 | Improved write throughput of f_puts() and f_printf(). 194 | Changed argument of f_chdrive(), f_mkfs(), disk_read() and disk_write(). 195 | Fixed f_write() can be truncated when the file size is close to 4GB. 196 | Fixed f_open(), f_mkdir() and f_setlabel() can return incorrect value on error. 197 | 198 | 199 | 200 | R0.10a (January 15, 2014) 201 | 202 | Added arbitrary strings as drive number in the path name. (_STR_VOLUME_ID) 203 | Added a configuration option of minimum sector size. (_MIN_SS) 204 | 2nd argument of f_rename() can have a drive number and it will be ignored. 205 | Fixed f_mount() with forced mount fails when drive number is >= 1. (appeared at R0.10) 206 | Fixed f_close() invalidates the file object without volume lock. 207 | Fixed f_closedir() returns but the volume lock is left acquired. (appeared at R0.10) 208 | Fixed creation of an entry with LFN fails on too many SFN collisions. (appeared at R0.07) 209 | 210 | 211 | 212 | R0.10b (May 19, 2014) 213 | 214 | Fixed a hard error in the disk I/O layer can collapse the directory entry. 215 | Fixed LFN entry is not deleted when delete/rename an object with lossy converted SFN. (appeared at R0.07) 216 | 217 | 218 | 219 | R0.10c (November 09, 2014) 220 | 221 | Added a configuration option for the platforms without RTC. (_FS_NORTC) 222 | Changed option name _USE_ERASE to _USE_TRIM. 223 | Fixed volume label created by Mac OS X cannot be retrieved with f_getlabel(). (appeared at R0.09b) 224 | Fixed a potential problem of FAT access that can appear on disk error. 225 | Fixed null pointer dereference on attempting to delete the root direcotry. (appeared at R0.08) 226 | 227 | 228 | 229 | R0.11 (February 09, 2015) 230 | 231 | Added f_findfirst(), f_findnext() and f_findclose(). (_USE_FIND) 232 | Fixed f_unlink() does not remove cluster chain of the file. (appeared at R0.10c) 233 | Fixed _FS_NORTC option does not work properly. (appeared at R0.10c) 234 | 235 | 236 | 237 | R0.11a (September 05, 2015) 238 | 239 | Fixed wrong media change can lead a deadlock at thread-safe configuration. 240 | Added code page 771, 860, 861, 863, 864, 865 and 869. (_CODE_PAGE) 241 | Removed some code pages actually not exist on the standard systems. (_CODE_PAGE) 242 | Fixed errors in the case conversion teble of code page 437 and 850 (ff.c). 243 | Fixed errors in the case conversion teble of Unicode (cc*.c). 244 | 245 | 246 | 247 | R0.12 (April 12, 2016) 248 | 249 | Added support for exFAT file system. (_FS_EXFAT) 250 | Added f_expand(). (_USE_EXPAND) 251 | Changed some members in FINFO structure and behavior of f_readdir(). 252 | Added an option _USE_CHMOD. 253 | Removed an option _WORD_ACCESS. 254 | Fixed errors in the case conversion table of Unicode (cc*.c). 255 | 256 | 257 | 258 | R0.12a (July 10, 2016) 259 | 260 | Added support for creating exFAT volume with some changes of f_mkfs(). 261 | Added a file open method FA_OPEN_APPEND. An f_lseek() following f_open() is no longer needed. 262 | f_forward() is available regardless of _FS_TINY. 263 | Fixed f_mkfs() creates wrong volume. (appeared at R0.12) 264 | Fixed wrong memory read in create_name(). (appeared at R0.12) 265 | Fixed compilation fails at some configurations, _USE_FASTSEEK and _USE_FORWARD. 266 | 267 | 268 | 269 | R0.12b (September 04, 2016) 270 | 271 | Made f_rename() be able to rename objects with the same name but case. 272 | Fixed an error in the case conversion teble of code page 866. (ff.c) 273 | Fixed writing data is truncated at the file offset 4GiB on the exFAT volume. (appeared at R0.12) 274 | Fixed creating a file in the root directory of exFAT volume can fail. (appeared at R0.12) 275 | Fixed f_mkfs() creating exFAT volume with too small cluster size can collapse unallocated memory. (appeared at R0.12) 276 | Fixed wrong object name can be returned when read directory at Unicode cfg. (appeared at R0.12) 277 | Fixed large file allocation/removing on the exFAT volume collapses allocation bitmap. (appeared at R0.12) 278 | Fixed some internal errors in f_expand() and f_lseek(). (appeared at R0.12) 279 | 280 | 281 | 282 | R0.12c (March 04, 2017) 283 | 284 | Improved write throughput at the fragmented file on the exFAT volume. 285 | Made memory usage for exFAT be able to be reduced as decreasing _MAX_LFN. 286 | Fixed successive f_getfree() can return wrong count on the FAT12/16 volume. (appeared at R0.12) 287 | Fixed configuration option _VOLUMES cannot be set 10. (appeared at R0.10c) 288 | 289 | 290 | 291 | R0.13 (May 21, 2017) 292 | 293 | Changed heading character of configuration keywords "_" to "FF_". 294 | Removed ASCII-only configuration, FF_CODE_PAGE = 1. Use FF_CODE_PAGE = 437 instead. 295 | Added f_setcp(), run-time code page configuration. (FF_CODE_PAGE = 0) 296 | Improved cluster allocation time on stretch a deep buried cluster chain. 297 | Improved processing time of f_mkdir() with large cluster size by using FF_USE_LFN = 3. 298 | Improved NoFatChain flag of the fragmented file to be set after it is truncated and got contiguous. 299 | Fixed archive attribute is left not set when a file on the exFAT volume is renamed. (appeared at R0.12) 300 | Fixed exFAT FAT entry can be collapsed when write or lseek operation to the existing file is done. (appeared at R0.12c) 301 | Fixed creating a file can fail when a new cluster allocation to the exFAT directory occures. (appeared at R0.12c) 302 | 303 | -------------------------------------------------------------------------------- /stage2/arm9/source/fatfs/ffconf.h: -------------------------------------------------------------------------------- 1 | /*---------------------------------------------------------------------------/ 2 | / FatFs - Configuration file 3 | /---------------------------------------------------------------------------*/ 4 | 5 | #define FFCONF_DEF 87030 /* Revision ID */ 6 | 7 | /*---------------------------------------------------------------------------/ 8 | / Function Configurations 9 | /---------------------------------------------------------------------------*/ 10 | 11 | #define FF_FS_READONLY 0 12 | /* This option switches read-only configuration. (0:Read/Write or 1:Read-only) 13 | / Read-only configuration removes writing API functions, f_write(), f_sync(), 14 | / f_unlink(), f_mkdir(), f_chmod(), f_rename(), f_truncate(), f_getfree() 15 | / and optional writing functions as well. */ 16 | 17 | 18 | #define FF_FS_MINIMIZE 0 19 | /* This option defines minimization level to remove some basic API functions. 20 | / 21 | / 0: All basic functions are enabled. 22 | / 1: f_stat(), f_getfree(), f_unlink(), f_mkdir(), f_truncate() and f_rename() 23 | / are removed. 24 | / 2: f_opendir(), f_readdir() and f_closedir() are removed in addition to 1. 25 | / 3: f_lseek() function is removed in addition to 2. */ 26 | 27 | 28 | #define FF_USE_STRFUNC 0 29 | /* This option switches string functions, f_gets(), f_putc(), f_puts() and f_printf(). 30 | / 31 | / 0: Disable string functions. 32 | / 1: Enable without LF-CRLF conversion. 33 | / 2: Enable with LF-CRLF conversion. */ 34 | 35 | 36 | #define FF_USE_FIND 0 37 | /* This option switches filtered directory read functions, f_findfirst() and 38 | / f_findnext(). (0:Disable, 1:Enable 2:Enable with matching altname[] too) */ 39 | 40 | 41 | #define FF_USE_MKFS 0 42 | /* This option switches f_mkfs() function. (0:Disable or 1:Enable) */ 43 | 44 | 45 | #define FF_USE_FASTSEEK 0 46 | /* This option switches fast seek function. (0:Disable or 1:Enable) */ 47 | 48 | 49 | #define FF_USE_EXPAND 0 50 | /* This option switches f_expand function. (0:Disable or 1:Enable) */ 51 | 52 | 53 | #define FF_USE_CHMOD 0 54 | /* This option switches attribute manipulation functions, f_chmod() and f_utime(). 55 | / (0:Disable or 1:Enable) Also FF_FS_READONLY needs to be 0 to enable this option. */ 56 | 57 | 58 | #define FF_USE_LABEL 0 59 | /* This option switches volume label functions, f_getlabel() and f_setlabel(). 60 | / (0:Disable or 1:Enable) */ 61 | 62 | 63 | #define FF_USE_FORWARD 0 64 | /* This option switches f_forward() function. (0:Disable or 1:Enable) */ 65 | 66 | 67 | /*---------------------------------------------------------------------------/ 68 | / Locale and Namespace Configurations 69 | /---------------------------------------------------------------------------*/ 70 | 71 | #define FF_CODE_PAGE 437 72 | /* This option specifies the OEM code page to be used on the target system. 73 | / Incorrect code page setting can cause a file open failure. 74 | / 75 | / 437 - U.S. 76 | / 720 - Arabic 77 | / 737 - Greek 78 | / 771 - KBL 79 | / 775 - Baltic 80 | / 850 - Latin 1 81 | / 852 - Latin 2 82 | / 855 - Cyrillic 83 | / 857 - Turkish 84 | / 860 - Portuguese 85 | / 861 - Icelandic 86 | / 862 - Hebrew 87 | / 863 - Canadian French 88 | / 864 - Arabic 89 | / 865 - Nordic 90 | / 866 - Russian 91 | / 869 - Greek 2 92 | / 932 - Japanese (DBCS) 93 | / 936 - Simplified Chinese (DBCS) 94 | / 949 - Korean (DBCS) 95 | / 950 - Traditional Chinese (DBCS) 96 | / 0 - Include all code pages above and configured by f_setcp() 97 | */ 98 | 99 | 100 | #define FF_USE_LFN 2 101 | #define FF_MAX_LFN 255 102 | /* The FF_USE_LFN switches the support for LFN (long file name). 103 | / 104 | / 0: Disable LFN. FF_MAX_LFN has no effect. 105 | / 1: Enable LFN with static working buffer on the BSS. Always NOT thread-safe. 106 | / 2: Enable LFN with dynamic working buffer on the STACK. 107 | / 3: Enable LFN with dynamic working buffer on the HEAP. 108 | / 109 | / To enable the LFN, Unicode handling functions (option/unicode.c) must be added 110 | / to the project. The working buffer occupies (FF_MAX_LFN + 1) * 2 bytes and 111 | / additional 608 bytes at exFAT enabled. FF_MAX_LFN can be in range from 12 to 255. 112 | / It should be set 255 to support full featured LFN operations. 113 | / When use stack for the working buffer, take care on stack overflow. When use heap 114 | / memory for the working buffer, memory management functions, ff_memalloc() and 115 | / ff_memfree(), must be added to the project. */ 116 | 117 | 118 | #define FF_LFN_UNICODE 0 119 | /* This option switches character encoding on the API, 0:ANSI/OEM or 1:UTF-16, 120 | / when LFN is enabled. Also behavior of string I/O functions will be affected by 121 | / this option. When LFN is not enabled, this option has no effect. 122 | */ 123 | 124 | 125 | #define FF_STRF_ENCODE 3 126 | /* When FF_LFN_UNICODE = 1 with LFN enabled, string I/O functions, f_gets(), 127 | / f_putc(), f_puts and f_printf() convert the character encoding in it. 128 | / This option selects assumption of character encoding ON THE FILE to be 129 | / read/written via those functions. 130 | / 131 | / 0: ANSI/OEM 132 | / 1: UTF-16LE 133 | / 2: UTF-16BE 134 | / 3: UTF-8 135 | */ 136 | 137 | 138 | #define FF_FS_RPATH 1 139 | /* This option configures support for relative path. 140 | / 141 | / 0: Disable relative path and remove related functions. 142 | / 1: Enable relative path. f_chdir() and f_chdrive() are available. 143 | / 2: f_getcwd() function is available in addition to 1. 144 | */ 145 | 146 | 147 | /*---------------------------------------------------------------------------/ 148 | / Drive/Volume Configurations 149 | /---------------------------------------------------------------------------*/ 150 | 151 | #define FF_VOLUMES 2 152 | /* Number of volumes (logical drives) to be used. (1-10) */ 153 | 154 | 155 | #define FF_STR_VOLUME_ID 0 156 | #define FF_VOLUME_STRS "RAM","NAND","CF","SD","SD2","USB","USB2","USB3" 157 | /* FF_STR_VOLUME_ID switches string support for volume ID. 158 | / When FF_STR_VOLUME_ID is set to 1, also pre-defined strings can be used as drive 159 | / number in the path name. FF_VOLUME_STRS defines the drive ID strings for each 160 | / logical drives. Number of items must be equal to FF_VOLUMES. Valid characters for 161 | / the drive ID strings are: A-Z and 0-9. */ 162 | 163 | 164 | #define FF_MULTI_PARTITION 0 165 | /* This option switches support for multiple volumes on the physical drive. 166 | / By default (0), each logical drive number is bound to the same physical drive 167 | / number and only an FAT volume found on the physical drive will be mounted. 168 | / When this function is enabled (1), each logical drive number can be bound to 169 | / arbitrary physical drive and partition listed in the VolToPart[]. Also f_fdisk() 170 | / funciton will be available. */ 171 | 172 | 173 | #define FF_MIN_SS 512 174 | #define FF_MAX_SS 512 175 | /* This set of options configures the range of sector size to be supported. (512, 176 | / 1024, 2048 or 4096) Always set both 512 for most systems, generic memory card and 177 | / harddisk. But a larger value may be required for on-board flash memory and some 178 | / type of optical media. When FF_MAX_SS is larger than FF_MIN_SS, FatFs is configured 179 | / for variable sector size mode and disk_ioctl() function needs to implement 180 | / GET_SECTOR_SIZE command. */ 181 | 182 | 183 | #define FF_USE_TRIM 0 184 | /* This option switches support for ATA-TRIM. (0:Disable or 1:Enable) 185 | / To enable Trim function, also CTRL_TRIM command should be implemented to the 186 | / disk_ioctl() function. */ 187 | 188 | 189 | #define FF_FS_NOFSINFO 0 190 | /* If you need to know correct free space on the FAT32 volume, set bit 0 of this 191 | / option, and f_getfree() function at first time after volume mount will force 192 | / a full FAT scan. Bit 1 controls the use of last allocated cluster number. 193 | / 194 | / bit0=0: Use free cluster count in the FSINFO if available. 195 | / bit0=1: Do not trust free cluster count in the FSINFO. 196 | / bit1=0: Use last allocated cluster number in the FSINFO if available. 197 | / bit1=1: Do not trust last allocated cluster number in the FSINFO. 198 | */ 199 | 200 | 201 | 202 | /*---------------------------------------------------------------------------/ 203 | / System Configurations 204 | /---------------------------------------------------------------------------*/ 205 | 206 | #define FF_FS_TINY 0 207 | /* This option switches tiny buffer configuration. (0:Normal or 1:Tiny) 208 | / At the tiny configuration, size of file object (FIL) is shrinked FF_MAX_SS bytes. 209 | / Instead of private sector buffer eliminated from the file object, common sector 210 | / buffer in the filesystem object (FATFS) is used for the file data transfer. */ 211 | 212 | 213 | #define FF_FS_EXFAT 0 214 | /* This option switches support for exFAT filesystem. (0:Disable or 1:Enable) 215 | / When enable exFAT, also LFN needs to be enabled. 216 | / Note that enabling exFAT discards ANSI C (C89) compatibility. */ 217 | 218 | 219 | #define FF_FS_NORTC 1 220 | #define FF_NORTC_MON 5 221 | #define FF_NORTC_MDAY 1 222 | #define FF_NORTC_YEAR 2017 223 | /* The option FF_FS_NORTC switches timestamp functiton. If the system does not have 224 | / any RTC function or valid timestamp is not needed, set FF_FS_NORTC = 1 to disable 225 | / the timestamp function. All objects modified by FatFs will have a fixed timestamp 226 | / defined by FF_NORTC_MON, FF_NORTC_MDAY and FF_NORTC_YEAR in local time. 227 | / To enable timestamp function (FF_FS_NORTC = 0), get_fattime() function need to be 228 | / added to the project to read current time form real-time clock. FF_NORTC_MON, 229 | / FF_NORTC_MDAY and FF_NORTC_YEAR have no effect. 230 | / These options have no effect at read-only configuration (FF_FS_READONLY = 1). */ 231 | 232 | 233 | #define FF_FS_LOCK 0 234 | /* The option FF_FS_LOCK switches file lock function to control duplicated file open 235 | / and illegal operation to open objects. This option must be 0 when FF_FS_READONLY 236 | / is 1. 237 | / 238 | / 0: Disable file lock function. To avoid volume corruption, application program 239 | / should avoid illegal open, remove and rename to the open objects. 240 | / >0: Enable file lock function. The value defines how many files/sub-directories 241 | / can be opened simultaneously under file lock control. Note that the file 242 | / lock control is independent of re-entrancy. */ 243 | 244 | 245 | #define FF_FS_REENTRANT 0 246 | #define FF_FS_TIMEOUT 1000 247 | #define FF_SYNC_t HANDLE 248 | /* The option FF_FS_REENTRANT switches the re-entrancy (thread safe) of the FatFs 249 | / module itself. Note that regardless of this option, file access to different 250 | / volume is always re-entrant and volume control functions, f_mount(), f_mkfs() 251 | / and f_fdisk() function, are always not re-entrant. Only file/directory access 252 | / to the same volume is under control of this function. 253 | / 254 | / 0: Disable re-entrancy. FF_FS_TIMEOUT and FF_SYNC_t have no effect. 255 | / 1: Enable re-entrancy. Also user provided synchronization handlers, 256 | / ff_req_grant(), ff_rel_grant(), ff_del_syncobj() and ff_cre_syncobj() 257 | / function, must be added to the project. Samples are available in 258 | / option/syscall.c. 259 | / 260 | / The FF_FS_TIMEOUT defines timeout period in unit of time tick. 261 | / The FF_SYNC_t defines O/S dependent sync object type. e.g. HANDLE, ID, OS_EVENT*, 262 | / SemaphoreHandle_t and etc. A header file for O/S definitions needs to be 263 | / included somewhere in the scope of ff.h. */ 264 | 265 | /* #include // O/S definitions */ 266 | 267 | 268 | 269 | /*--- End of configuration options ---*/ 270 | -------------------------------------------------------------------------------- /stage2/arm9/source/fatfs/ff.h: -------------------------------------------------------------------------------- 1 | /*----------------------------------------------------------------------------/ 2 | / FatFs - Generic FAT Filesystem module R0.13 / 3 | /-----------------------------------------------------------------------------/ 4 | / 5 | / Copyright (C) 2017, ChaN, all right reserved. 6 | / 7 | / FatFs module is an open source software. Redistribution and use of FatFs in 8 | / source and binary forms, with or without modification, are permitted provided 9 | / that the following condition is met: 10 | 11 | / 1. Redistributions of source code must retain the above copyright notice, 12 | / this condition and the following disclaimer. 13 | / 14 | / This software is provided by the copyright holder and contributors "AS IS" 15 | / and any warranties related to this software are DISCLAIMED. 16 | / The copyright owner or contributors be NOT LIABLE for any damages caused 17 | / by use of this software. 18 | / 19 | /----------------------------------------------------------------------------*/ 20 | 21 | 22 | #ifndef FF_DEFINED 23 | #define FF_DEFINED 87030 /* Revision ID */ 24 | 25 | #ifdef __cplusplus 26 | extern "C" { 27 | #endif 28 | 29 | #include "integer.h" /* Basic integer types */ 30 | #include "ffconf.h" /* FatFs configuration options */ 31 | 32 | #if FF_DEFINED != FFCONF_DEF 33 | #error Wrong configuration file (ffconf.h). 34 | #endif 35 | 36 | 37 | 38 | /* Definitions of volume management */ 39 | 40 | #if FF_MULTI_PARTITION /* Multiple partition configuration */ 41 | typedef struct { 42 | BYTE pd; /* Physical drive number */ 43 | BYTE pt; /* Partition: 0:Auto detect, 1-4:Forced partition) */ 44 | } PARTITION; 45 | extern PARTITION VolToPart[]; /* Volume - Partition resolution table */ 46 | #endif 47 | 48 | 49 | 50 | /* Type of path name strings on FatFs API */ 51 | 52 | #if FF_LFN_UNICODE && FF_USE_LFN /* Unicode (UTF-16) string */ 53 | #ifndef _INC_TCHAR 54 | typedef WCHAR TCHAR; 55 | #define _T(x) L ## x 56 | #define _TEXT(x) L ## x 57 | #define _INC_TCHAR 58 | #endif 59 | #else /* ANSI/OEM string */ 60 | #ifndef _INC_TCHAR 61 | typedef char TCHAR; 62 | #define _T(x) x 63 | #define _TEXT(x) x 64 | #define _INC_TCHAR 65 | #endif 66 | #endif 67 | 68 | 69 | 70 | /* Type of file size variables */ 71 | 72 | #if FF_FS_EXFAT 73 | #if !FF_USE_LFN 74 | #error LFN must be enabled when enable exFAT 75 | #endif 76 | typedef QWORD FSIZE_t; 77 | #else 78 | typedef DWORD FSIZE_t; 79 | #endif 80 | 81 | 82 | 83 | /* Filesystem object structure (FATFS) */ 84 | 85 | typedef struct { 86 | BYTE fs_type; /* Filesystem type (0:N/A) */ 87 | BYTE pdrv; /* Physical drive number */ 88 | BYTE n_fats; /* Number of FATs (1 or 2) */ 89 | BYTE wflag; /* win[] flag (b0:dirty) */ 90 | BYTE fsi_flag; /* FSINFO flags (b7:disabled, b0:dirty) */ 91 | WORD id; /* Volume mount ID */ 92 | WORD n_rootdir; /* Number of root directory entries (FAT12/16) */ 93 | WORD csize; /* Cluster size [sectors] */ 94 | #if FF_MAX_SS != FF_MIN_SS 95 | WORD ssize; /* Sector size (512, 1024, 2048 or 4096) */ 96 | #endif 97 | #if FF_USE_LFN 98 | WCHAR* lfnbuf; /* LFN working buffer */ 99 | #endif 100 | #if FF_FS_EXFAT 101 | BYTE* dirbuf; /* Directory entry block scratchpad buffer for exFAT */ 102 | #endif 103 | #if FF_FS_REENTRANT 104 | FF_SYNC_t sobj; /* Identifier of sync object */ 105 | #endif 106 | #if !FF_FS_READONLY 107 | DWORD last_clst; /* Last allocated cluster */ 108 | DWORD free_clst; /* Number of free clusters */ 109 | #endif 110 | #if FF_FS_RPATH 111 | DWORD cdir; /* Current directory start cluster (0:root) */ 112 | #if FF_FS_EXFAT 113 | DWORD cdc_scl; /* Containing directory start cluster (invalid when cdir is 0) */ 114 | DWORD cdc_size; /* b31-b8:Size of containing directory, b7-b0: Chain status */ 115 | DWORD cdc_ofs; /* Offset in the containing directory (invalid when cdir is 0) */ 116 | #endif 117 | #endif 118 | DWORD n_fatent; /* Number of FAT entries (number of clusters + 2) */ 119 | DWORD fsize; /* Size of an FAT [sectors] */ 120 | DWORD volbase; /* Volume base sector */ 121 | DWORD fatbase; /* FAT base sector */ 122 | DWORD dirbase; /* Root directory base sector/cluster */ 123 | DWORD database; /* Data base sector */ 124 | DWORD winsect; /* Current sector appearing in the win[] */ 125 | BYTE win[FF_MAX_SS]; /* Disk access window for Directory, FAT (and file data at tiny cfg) */ 126 | } FATFS; 127 | 128 | 129 | 130 | /* Object ID and allocation information (FFOBJID) */ 131 | 132 | typedef struct { 133 | FATFS* fs; /* Pointer to the hosting volume of this object */ 134 | WORD id; /* Hosting volume mount ID */ 135 | BYTE attr; /* Object attribute */ 136 | BYTE stat; /* Object chain status (b1-0: =0:not contiguous, =2:contiguous, =3:flagmented in this session, b2:sub-directory stretched) */ 137 | DWORD sclust; /* Object data start cluster (0:no cluster or root directory) */ 138 | FSIZE_t objsize; /* Object size (valid when sclust != 0) */ 139 | #if FF_FS_EXFAT 140 | DWORD n_cont; /* Size of first fragment - 1 (valid when stat == 3) */ 141 | DWORD n_frag; /* Size of last fragment needs to be written to FAT (valid when not zero) */ 142 | DWORD c_scl; /* Containing directory start cluster (valid when sclust != 0) */ 143 | DWORD c_size; /* b31-b8:Size of containing directory, b7-b0: Chain status (valid when c_scl != 0) */ 144 | DWORD c_ofs; /* Offset in the containing directory (valid when file object and sclust != 0) */ 145 | #endif 146 | #if FF_FS_LOCK 147 | UINT lockid; /* File lock ID origin from 1 (index of file semaphore table Files[]) */ 148 | #endif 149 | } FFOBJID; 150 | 151 | 152 | 153 | /* File object structure (FIL) */ 154 | 155 | typedef struct { 156 | FFOBJID obj; /* Object identifier (must be the 1st member to detect invalid object pointer) */ 157 | BYTE flag; /* File status flags */ 158 | BYTE err; /* Abort flag (error code) */ 159 | FSIZE_t fptr; /* File read/write pointer (Zeroed on file open) */ 160 | DWORD clust; /* Current cluster of fpter (invalid when fptr is 0) */ 161 | DWORD sect; /* Sector number appearing in buf[] (0:invalid) */ 162 | #if !FF_FS_READONLY 163 | DWORD dir_sect; /* Sector number containing the directory entry (not used at exFAT) */ 164 | BYTE* dir_ptr; /* Pointer to the directory entry in the win[] (not used at exFAT) */ 165 | #endif 166 | #if FF_USE_FASTSEEK 167 | DWORD* cltbl; /* Pointer to the cluster link map table (nulled on open, set by application) */ 168 | #endif 169 | #if !FF_FS_TINY 170 | BYTE buf[FF_MAX_SS]; /* File private data read/write window */ 171 | #endif 172 | } FIL; 173 | 174 | 175 | 176 | /* Directory object structure (DIR) */ 177 | 178 | typedef struct { 179 | FFOBJID obj; /* Object identifier */ 180 | DWORD dptr; /* Current read/write offset */ 181 | DWORD clust; /* Current cluster */ 182 | DWORD sect; /* Current sector (0:Read operation has terminated) */ 183 | BYTE* dir; /* Pointer to the directory item in the win[] */ 184 | BYTE fn[12]; /* SFN (in/out) {body[8],ext[3],status[1]} */ 185 | #if FF_USE_LFN 186 | DWORD blk_ofs; /* Offset of current entry block being processed (0xFFFFFFFF:Invalid) */ 187 | #endif 188 | #if FF_USE_FIND 189 | const TCHAR* pat; /* Pointer to the name matching pattern */ 190 | #endif 191 | } DIR; 192 | 193 | 194 | 195 | /* File information structure (FILINFO) */ 196 | 197 | typedef struct { 198 | FSIZE_t fsize; /* File size */ 199 | WORD fdate; /* Modified date */ 200 | WORD ftime; /* Modified time */ 201 | BYTE fattrib; /* File attribute */ 202 | #if FF_USE_LFN 203 | TCHAR altname[13]; /* Altenative file name */ 204 | TCHAR fname[FF_MAX_LFN + 1]; /* Primary file name */ 205 | #else 206 | TCHAR fname[13]; /* File name */ 207 | #endif 208 | } FILINFO; 209 | 210 | 211 | 212 | /* File function return code (FRESULT) */ 213 | 214 | typedef enum { 215 | FR_OK = 0, /* (0) Succeeded */ 216 | FR_DISK_ERR, /* (1) A hard error occurred in the low level disk I/O layer */ 217 | FR_INT_ERR, /* (2) Assertion failed */ 218 | FR_NOT_READY, /* (3) The physical drive cannot work */ 219 | FR_NO_FILE, /* (4) Could not find the file */ 220 | FR_NO_PATH, /* (5) Could not find the path */ 221 | FR_INVALID_NAME, /* (6) The path name format is invalid */ 222 | FR_DENIED, /* (7) Access denied due to prohibited access or directory full */ 223 | FR_EXIST, /* (8) Access denied due to prohibited access */ 224 | FR_INVALID_OBJECT, /* (9) The file/directory object is invalid */ 225 | FR_WRITE_PROTECTED, /* (10) The physical drive is write protected */ 226 | FR_INVALID_DRIVE, /* (11) The logical drive number is invalid */ 227 | FR_NOT_ENABLED, /* (12) The volume has no work area */ 228 | FR_NO_FILESYSTEM, /* (13) There is no valid FAT volume */ 229 | FR_MKFS_ABORTED, /* (14) The f_mkfs() aborted due to any problem */ 230 | FR_TIMEOUT, /* (15) Could not get a grant to access the volume within defined period */ 231 | FR_LOCKED, /* (16) The operation is rejected according to the file sharing policy */ 232 | FR_NOT_ENOUGH_CORE, /* (17) LFN working buffer could not be allocated */ 233 | FR_TOO_MANY_OPEN_FILES, /* (18) Number of open files > FF_FS_LOCK */ 234 | FR_INVALID_PARAMETER /* (19) Given parameter is invalid */ 235 | } FRESULT; 236 | 237 | 238 | 239 | /*--------------------------------------------------------------*/ 240 | /* FatFs module application interface */ 241 | 242 | FRESULT f_open (FIL* fp, const TCHAR* path, BYTE mode); /* Open or create a file */ 243 | FRESULT f_close (FIL* fp); /* Close an open file object */ 244 | FRESULT f_read (FIL* fp, void* buff, UINT btr, UINT* br); /* Read data from the file */ 245 | FRESULT f_write (FIL* fp, const void* buff, UINT btw, UINT* bw); /* Write data to the file */ 246 | FRESULT f_lseek (FIL* fp, FSIZE_t ofs); /* Move file pointer of the file object */ 247 | FRESULT f_truncate (FIL* fp); /* Truncate the file */ 248 | FRESULT f_sync (FIL* fp); /* Flush cached data of the writing file */ 249 | FRESULT f_opendir (DIR* dp, const TCHAR* path); /* Open a directory */ 250 | FRESULT f_closedir (DIR* dp); /* Close an open directory */ 251 | FRESULT f_readdir (DIR* dp, FILINFO* fno); /* Read a directory item */ 252 | FRESULT f_findfirst (DIR* dp, FILINFO* fno, const TCHAR* path, const TCHAR* pattern); /* Find first file */ 253 | FRESULT f_findnext (DIR* dp, FILINFO* fno); /* Find next file */ 254 | FRESULT f_mkdir (const TCHAR* path); /* Create a sub directory */ 255 | FRESULT f_unlink (const TCHAR* path); /* Delete an existing file or directory */ 256 | FRESULT f_rename (const TCHAR* path_old, const TCHAR* path_new); /* Rename/Move a file or directory */ 257 | FRESULT f_stat (const TCHAR* path, FILINFO* fno); /* Get file status */ 258 | FRESULT f_chmod (const TCHAR* path, BYTE attr, BYTE mask); /* Change attribute of a file/dir */ 259 | FRESULT f_utime (const TCHAR* path, const FILINFO* fno); /* Change timestamp of a file/dir */ 260 | FRESULT f_chdir (const TCHAR* path); /* Change current directory */ 261 | FRESULT f_chdrive (const TCHAR* path); /* Change current drive */ 262 | FRESULT f_getcwd (TCHAR* buff, UINT len); /* Get current directory */ 263 | FRESULT f_getfree (const TCHAR* path, DWORD* nclst, FATFS** fatfs); /* Get number of free clusters on the drive */ 264 | FRESULT f_getlabel (const TCHAR* path, TCHAR* label, DWORD* vsn); /* Get volume label */ 265 | FRESULT f_setlabel (const TCHAR* label); /* Set volume label */ 266 | FRESULT f_forward (FIL* fp, UINT(*func)(const BYTE*,UINT), UINT btf, UINT* bf); /* Forward data to the stream */ 267 | FRESULT f_expand (FIL* fp, FSIZE_t szf, BYTE opt); /* Allocate a contiguous block to the file */ 268 | FRESULT f_mount (FATFS* fs, const TCHAR* path, BYTE opt); /* Mount/Unmount a logical drive */ 269 | FRESULT f_mkfs (const TCHAR* path, BYTE opt, DWORD au, void* work, UINT len); /* Create a FAT volume */ 270 | FRESULT f_fdisk (BYTE pdrv, const DWORD* szt, void* work); /* Divide a physical drive into some partitions */ 271 | FRESULT f_setcp (WORD cp); /* Set current code page */ 272 | int f_putc (TCHAR c, FIL* fp); /* Put a character to the file */ 273 | int f_puts (const TCHAR* str, FIL* cp); /* Put a string to the file */ 274 | int f_printf (FIL* fp, const TCHAR* str, ...); /* Put a formatted string to the file */ 275 | TCHAR* f_gets (TCHAR* buff, int len, FIL* fp); /* Get a string from the file */ 276 | 277 | #define f_eof(fp) ((int)((fp)->fptr == (fp)->obj.objsize)) 278 | #define f_error(fp) ((fp)->err) 279 | #define f_tell(fp) ((fp)->fptr) 280 | #define f_size(fp) ((fp)->obj.objsize) 281 | #define f_rewind(fp) f_lseek((fp), 0) 282 | #define f_rewinddir(dp) f_readdir((dp), 0) 283 | #define f_rmdir(path) f_unlink(path) 284 | #define f_unmount(path) f_mount(0, path, 0) 285 | 286 | #ifndef EOF 287 | #define EOF (-1) 288 | #endif 289 | 290 | 291 | 292 | 293 | /*--------------------------------------------------------------*/ 294 | /* Additional user defined functions */ 295 | 296 | /* RTC function */ 297 | #if !FF_FS_READONLY && !FF_FS_NORTC 298 | DWORD get_fattime (void); 299 | #endif 300 | 301 | /* LFN support functions */ 302 | #if FF_USE_LFN /* Code conversion (defined in unicode.c) */ 303 | WCHAR ff_oem2uni (WCHAR oem, WORD cp); /* OEM code to Unicode conversion */ 304 | WCHAR ff_uni2oem (WCHAR uni, WORD cp); /* Unicode to OEM code conversion */ 305 | WCHAR ff_wtoupper (WCHAR uni); /* Unicode upper-case conversion */ 306 | #endif 307 | #if FF_USE_LFN == 3 /* Dynamic memory allocation */ 308 | void* ff_memalloc (UINT msize); /* Allocate memory block */ 309 | void ff_memfree (void* mblock); /* Free memory block */ 310 | #endif 311 | 312 | /* Sync functions */ 313 | #if FF_FS_REENTRANT 314 | int ff_cre_syncobj (BYTE vol, FF_SYNC_t* sobj); /* Create a sync object */ 315 | int ff_req_grant (FF_SYNC_t sobj); /* Lock sync object */ 316 | void ff_rel_grant (FF_SYNC_t sobj); /* Unlock sync object */ 317 | int ff_del_syncobj (FF_SYNC_t sobj); /* Delete a sync object */ 318 | #endif 319 | 320 | 321 | 322 | 323 | /*--------------------------------------------------------------*/ 324 | /* Flags and offset address */ 325 | 326 | 327 | /* File access mode and open method flags (3rd argument of f_open) */ 328 | #define FA_READ 0x01 329 | #define FA_WRITE 0x02 330 | #define FA_OPEN_EXISTING 0x00 331 | #define FA_CREATE_NEW 0x04 332 | #define FA_CREATE_ALWAYS 0x08 333 | #define FA_OPEN_ALWAYS 0x10 334 | #define FA_OPEN_APPEND 0x30 335 | 336 | /* Fast seek controls (2nd argument of f_lseek) */ 337 | #define CREATE_LINKMAP ((FSIZE_t)0 - 1) 338 | 339 | /* Format options (2nd argument of f_mkfs) */ 340 | #define FM_FAT 0x01 341 | #define FM_FAT32 0x02 342 | #define FM_EXFAT 0x04 343 | #define FM_ANY 0x07 344 | #define FM_SFD 0x08 345 | 346 | /* Filesystem type (FATFS.fs_type) */ 347 | #define FS_FAT12 1 348 | #define FS_FAT16 2 349 | #define FS_FAT32 3 350 | #define FS_EXFAT 4 351 | 352 | /* File attribute bits for directory entry (FILINFO.fattrib) */ 353 | #define AM_RDO 0x01 /* Read only */ 354 | #define AM_HID 0x02 /* Hidden */ 355 | #define AM_SYS 0x04 /* System */ 356 | #define AM_DIR 0x10 /* Directory */ 357 | #define AM_ARC 0x20 /* Archive */ 358 | 359 | 360 | #ifdef __cplusplus 361 | } 362 | #endif 363 | 364 | #endif /* FF_DEFINED */ 365 | -------------------------------------------------------------------------------- /stage2/arm9/source/fatfs/sdmmc/sdmmc.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This Source Code Form is subject to the terms of the Mozilla Public 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, 4 | * You can obtain one at http://mozilla.org/MPL/2.0/. 5 | * 6 | * Copyright (c) 2014-2015, Normmatt 7 | * 8 | * Alternatively, the contents of this file may be used under the terms 9 | * of the GNU General Public License Version 2, as described below: 10 | * 11 | * This file is free software: you may copy, redistribute and/or modify 12 | * it under the terms of the GNU General Public License as published by the 13 | * Free Software Foundation, either version 2 of the License, or (at your 14 | * option) any later version. 15 | * 16 | * This file is distributed in the hope that it will be useful, but 17 | * WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 19 | * Public License for more details. 20 | * 21 | * You should have received a copy of the GNU General Public License 22 | * along with this program. If not, see http://www.gnu.org/licenses/. 23 | */ 24 | 25 | #include "sdmmc.h" 26 | #include "delay.h" 27 | 28 | static struct mmcdevice handleNAND; 29 | static struct mmcdevice handleSD; 30 | 31 | static inline u16 sdmmc_read16(u16 reg) 32 | { 33 | return *(vu16 *)(SDMMC_BASE + reg); 34 | } 35 | 36 | static inline void sdmmc_write16(u16 reg, u16 val) 37 | { 38 | *(vu16 *)(SDMMC_BASE + reg) = val; 39 | } 40 | 41 | static inline u32 sdmmc_read32(u16 reg) 42 | { 43 | return *(vu32 *)(SDMMC_BASE + reg); 44 | } 45 | 46 | static inline void sdmmc_write32(u16 reg, u32 val) 47 | { 48 | *(vu32 *)(SDMMC_BASE + reg) = val; 49 | } 50 | 51 | static inline void sdmmc_mask16(u16 reg, const u16 clear, const u16 set) 52 | { 53 | u16 val = sdmmc_read16(reg); 54 | val &= ~clear; 55 | val |= set; 56 | sdmmc_write16(reg, val); 57 | } 58 | 59 | static inline void setckl(u32 data) 60 | { 61 | sdmmc_mask16(REG_SDCLKCTL, 0x100, 0); 62 | sdmmc_mask16(REG_SDCLKCTL, 0x2FF, data & 0x2FF); 63 | sdmmc_mask16(REG_SDCLKCTL, 0x0, 0x100); 64 | } 65 | 66 | /* 67 | mmcdevice *getMMCDevice(int drive) 68 | { 69 | if(drive == 0) return &handleNAND; 70 | return &handleSD; 71 | } 72 | */ 73 | 74 | static int geterror(struct mmcdevice *ctx) 75 | { 76 | return (int)((ctx->error << 29) >> 31); 77 | } 78 | 79 | static void inittarget(struct mmcdevice *ctx) 80 | { 81 | sdmmc_mask16(REG_SDPORTSEL, 0x3, (u16)ctx->devicenumber); 82 | setckl(ctx->clk); 83 | if(ctx->SDOPT == 0) sdmmc_mask16(REG_SDOPT, 0, 0x8000); 84 | else sdmmc_mask16(REG_SDOPT, 0x8000, 0); 85 | } 86 | 87 | static void __attribute__((noinline)) sdmmc_send_command(struct mmcdevice *ctx, u32 cmd, u32 args) 88 | { 89 | u32 getSDRESP = (cmd << 15) >> 31; 90 | u16 flags = (cmd << 15) >> 31; 91 | const int readdata = cmd & 0x20000; 92 | const int writedata = cmd & 0x40000; 93 | 94 | if(readdata || writedata) 95 | flags |= TMIO_STAT0_DATAEND; 96 | 97 | ctx->error = 0; 98 | while((sdmmc_read16(REG_SDSTATUS1) & TMIO_STAT1_CMD_BUSY)); //mmc working? 99 | sdmmc_write16(REG_SDIRMASK0, 0); 100 | sdmmc_write16(REG_SDIRMASK1, 0); 101 | sdmmc_write16(REG_SDSTATUS0, 0); 102 | sdmmc_write16(REG_SDSTATUS1, 0); 103 | sdmmc_mask16(REG_DATACTL32, 0x1800, 0); 104 | sdmmc_write16(REG_SDCMDARG0, args & 0xFFFF); 105 | sdmmc_write16(REG_SDCMDARG1, args >> 16); 106 | sdmmc_write16(REG_SDCMD, cmd & 0xFFFF); 107 | 108 | u32 size = ctx->size; 109 | u8 *rDataPtr = ctx->rData; 110 | const u8 *tDataPtr = ctx->tData; 111 | 112 | bool rUseBuf = rDataPtr != NULL; 113 | bool tUseBuf = tDataPtr != NULL; 114 | 115 | u16 status0 = 0; 116 | while(true) 117 | { 118 | vu16 status1 = sdmmc_read16(REG_SDSTATUS1); 119 | vu16 ctl32 = sdmmc_read16(REG_DATACTL32); 120 | if((ctl32 & 0x100)) 121 | { 122 | if(readdata) 123 | { 124 | if(rUseBuf) 125 | { 126 | sdmmc_mask16(REG_SDSTATUS1, TMIO_STAT1_RXRDY, 0); 127 | if(size > 0x1FF) 128 | { 129 | //Gabriel Marcano: This implementation doesn't assume alignment. 130 | //I've removed the alignment check doen with former rUseBuf32 as a result 131 | for(int i = 0; i < 0x200; i += 4) 132 | { 133 | u32 data = sdmmc_read32(REG_SDFIFO32); 134 | *rDataPtr++ = data; 135 | *rDataPtr++ = data >> 8; 136 | *rDataPtr++ = data >> 16; 137 | *rDataPtr++ = data >> 24; 138 | } 139 | size -= 0x200; 140 | } 141 | } 142 | 143 | sdmmc_mask16(REG_DATACTL32, 0x800, 0); 144 | } 145 | } 146 | if(!(ctl32 & 0x200)) 147 | { 148 | if(writedata) 149 | { 150 | if(tUseBuf) 151 | { 152 | sdmmc_mask16(REG_SDSTATUS1, TMIO_STAT1_TXRQ, 0); 153 | if(size > 0x1FF) 154 | { 155 | for(int i = 0; i < 0x200; i += 4) 156 | { 157 | u32 data = *tDataPtr++; 158 | data |= (u32)*tDataPtr++ << 8; 159 | data |= (u32)*tDataPtr++ << 16; 160 | data |= (u32)*tDataPtr++ << 24; 161 | sdmmc_write32(REG_SDFIFO32, data); 162 | } 163 | size -= 0x200; 164 | } 165 | } 166 | 167 | sdmmc_mask16(REG_DATACTL32, 0x1000, 0); 168 | } 169 | } 170 | if(status1 & TMIO_MASK_GW) 171 | { 172 | ctx->error |= 4; 173 | break; 174 | } 175 | 176 | if(!(status1 & TMIO_STAT1_CMD_BUSY)) 177 | { 178 | status0 = sdmmc_read16(REG_SDSTATUS0); 179 | if(sdmmc_read16(REG_SDSTATUS0) & TMIO_STAT0_CMDRESPEND) 180 | { 181 | ctx->error |= 0x1; 182 | } 183 | if(status0 & TMIO_STAT0_DATAEND) 184 | { 185 | ctx->error |= 0x2; 186 | } 187 | 188 | if((status0 & flags) == flags) 189 | break; 190 | } 191 | } 192 | ctx->stat0 = sdmmc_read16(REG_SDSTATUS0); 193 | ctx->stat1 = sdmmc_read16(REG_SDSTATUS1); 194 | sdmmc_write16(REG_SDSTATUS0, 0); 195 | sdmmc_write16(REG_SDSTATUS1, 0); 196 | 197 | if(getSDRESP != 0) 198 | { 199 | ctx->ret[0] = (u32)(sdmmc_read16(REG_SDRESP0) | (sdmmc_read16(REG_SDRESP1) << 16)); 200 | ctx->ret[1] = (u32)(sdmmc_read16(REG_SDRESP2) | (sdmmc_read16(REG_SDRESP3) << 16)); 201 | ctx->ret[2] = (u32)(sdmmc_read16(REG_SDRESP4) | (sdmmc_read16(REG_SDRESP5) << 16)); 202 | ctx->ret[3] = (u32)(sdmmc_read16(REG_SDRESP6) | (sdmmc_read16(REG_SDRESP7) << 16)); 203 | } 204 | } 205 | 206 | 207 | int __attribute__((noinline)) sdmmc_sdcard_writesectors(u32 sector_no, u32 numsectors, const u8 *in) 208 | { 209 | if(handleSD.isSDHC == 0) sector_no <<= 9; 210 | inittarget(&handleSD); 211 | sdmmc_write16(REG_SDSTOP, 0x100); 212 | sdmmc_write16(REG_SDBLKCOUNT32, numsectors); 213 | sdmmc_write16(REG_SDBLKLEN32, 0x200); 214 | sdmmc_write16(REG_SDBLKCOUNT, numsectors); 215 | handleSD.tData = in; 216 | handleSD.size = numsectors << 9; 217 | sdmmc_send_command(&handleSD, 0x52C19, sector_no); 218 | return geterror(&handleSD); 219 | } 220 | 221 | 222 | int __attribute__((noinline)) sdmmc_sdcard_readsectors(u32 sector_no, u32 numsectors, u8 *out) 223 | { 224 | if(handleSD.isSDHC == 0) sector_no <<= 9; 225 | inittarget(&handleSD); 226 | sdmmc_write16(REG_SDSTOP, 0x100); 227 | sdmmc_write16(REG_SDBLKCOUNT32, numsectors); 228 | sdmmc_write16(REG_SDBLKLEN32, 0x200); 229 | sdmmc_write16(REG_SDBLKCOUNT, numsectors); 230 | handleSD.rData = out; 231 | handleSD.size = numsectors << 9; 232 | sdmmc_send_command(&handleSD, 0x33C12, sector_no); 233 | return geterror(&handleSD); 234 | } 235 | 236 | int __attribute__((noinline)) sdmmc_nand_readsectors(u32 sector_no, u32 numsectors, u8 *out) 237 | { 238 | if(handleNAND.isSDHC == 0) sector_no <<= 9; 239 | inittarget(&handleNAND); 240 | sdmmc_write16(REG_SDSTOP, 0x100); 241 | sdmmc_write16(REG_SDBLKCOUNT32, numsectors); 242 | sdmmc_write16(REG_SDBLKLEN32, 0x200); 243 | sdmmc_write16(REG_SDBLKCOUNT, numsectors); 244 | handleNAND.rData = out; 245 | handleNAND.size = numsectors << 9; 246 | sdmmc_send_command(&handleNAND, 0x33C12, sector_no); 247 | inittarget(&handleSD); 248 | return geterror(&handleNAND); 249 | } 250 | 251 | /* 252 | int __attribute__((noinline)) sdmmc_nand_writesectors(u32 sector_no, u32 numsectors, const u8 *in) //experimental 253 | { 254 | if(handleNAND.isSDHC == 0) sector_no <<= 9; 255 | inittarget(&handleNAND); 256 | sdmmc_write16(REG_SDSTOP, 0x100); 257 | sdmmc_write16(REG_SDBLKCOUNT32, numsectors); 258 | sdmmc_write16(REG_SDBLKLEN32, 0x200); 259 | sdmmc_write16(REG_SDBLKCOUNT, numsectors); 260 | handleNAND.tData = in; 261 | handleNAND.size = numsectors << 9; 262 | sdmmc_send_command(&handleNAND, 0x52C19, sector_no); 263 | inittarget(&handleSD); 264 | return geterror(&handleNAND); 265 | } 266 | */ 267 | 268 | static u32 calcSDSize(u8 *csd, int type) 269 | { 270 | u32 result = 0; 271 | if(type == -1) type = csd[14] >> 6; 272 | switch(type) 273 | { 274 | case 0: 275 | { 276 | u32 block_len = csd[9] & 0xF; 277 | block_len = 1u << block_len; 278 | u32 mult = (u32)((csd[4] >> 7) | ((csd[5] & 3) << 1)); 279 | mult = 1u << (mult + 2); 280 | result = csd[8] & 3; 281 | result = (result << 8) | csd[7]; 282 | result = (result << 2) | (csd[6] >> 6); 283 | result = (result + 1) * mult * block_len / 512; 284 | break; 285 | } 286 | case 1: 287 | result = csd[7] & 0x3F; 288 | result = (result << 8) | csd[6]; 289 | result = (result << 8) | csd[5]; 290 | result = (result + 1) * 1024; 291 | break; 292 | default: 293 | break; //Do nothing otherwise FIXME perhaps return some error? 294 | } 295 | return result; 296 | } 297 | 298 | static void InitSD() 299 | { 300 | *(vu32 *)0x10000020 = 0; //InitFS stuff 301 | *(vu32 *)0x10000020 = 0x200; //InitFS stuff 302 | *(vu16 *)0x10006100 &= 0xF7FFu; //SDDATACTL32 303 | *(vu16 *)0x10006100 &= 0xEFFFu; //SDDATACTL32 304 | *(vu16 *)0x10006100 |= 0x402u; //SDDATACTL32 305 | *(vu16 *)0x100060D8 = (*(vu16 *)0x100060D8 & 0xFFDD) | 2; 306 | *(vu16 *)0x10006100 &= 0xFFFFu; //SDDATACTL32 307 | *(vu16 *)0x100060D8 &= 0xFFDFu; //SDDATACTL 308 | *(vu16 *)0x10006104 = 512; //SDBLKLEN32 309 | *(vu16 *)0x10006108 = 1; //SDBLKCOUNT32 310 | *(vu16 *)0x100060E0 &= 0xFFFEu; //SDRESET 311 | *(vu16 *)0x100060E0 |= 1u; //SDRESET 312 | *(vu16 *)0x10006020 |= TMIO_MASK_ALL; //SDIR_MASK0 313 | *(vu16 *)0x10006022 |= TMIO_MASK_ALL>>16; //SDIR_MASK1 314 | *(vu16 *)0x100060FC |= 0xDBu; //SDCTL_RESERVED7 315 | *(vu16 *)0x100060FE |= 0xDBu; //SDCTL_RESERVED8 316 | *(vu16 *)0x10006002 &= 0xFFFCu; //SDPORTSEL 317 | *(vu16 *)0x10006024 = 0x20; 318 | *(vu16 *)0x10006028 = 0x40EE; 319 | *(vu16 *)0x10006002 &= 0xFFFCu; ////SDPORTSEL 320 | *(vu16 *)0x10006026 = 512; //SDBLKLEN 321 | *(vu16 *)0x10006008 = 0; //SDSTOP 322 | } 323 | 324 | static int Nand_Init() 325 | { 326 | //NAND 327 | handleNAND.isSDHC = 0; 328 | handleNAND.SDOPT = 0; 329 | handleNAND.res = 0; 330 | handleNAND.initarg = 1; 331 | handleNAND.clk = 0x80; 332 | handleNAND.devicenumber = 1; 333 | 334 | inittarget(&handleNAND); 335 | waitcycles(0xF000); 336 | 337 | sdmmc_send_command(&handleNAND, 0, 0); 338 | 339 | do 340 | { 341 | do 342 | { 343 | sdmmc_send_command(&handleNAND, 0x10701, 0x100000); 344 | } 345 | while(!(handleNAND.error & 1)); 346 | } 347 | while((handleNAND.ret[0] & 0x80000000) == 0); 348 | 349 | sdmmc_send_command(&handleNAND, 0x10602, 0x0); 350 | if((handleNAND.error & 0x4)) return -1; 351 | 352 | sdmmc_send_command(&handleNAND, 0x10403, handleNAND.initarg << 0x10); 353 | if((handleNAND.error & 0x4)) return -1; 354 | 355 | sdmmc_send_command(&handleNAND, 0x10609, handleNAND.initarg << 0x10); 356 | if((handleNAND.error & 0x4)) return -1; 357 | 358 | handleNAND.total_size = calcSDSize((u8*)&handleNAND.ret[0], 0); 359 | handleNAND.clk = 1; 360 | setckl(1); 361 | 362 | sdmmc_send_command(&handleNAND, 0x10407, handleNAND.initarg << 0x10); 363 | if((handleNAND.error & 0x4)) return -1; 364 | 365 | handleNAND.SDOPT = 1; 366 | 367 | sdmmc_send_command(&handleNAND, 0x10506, 0x3B70100); 368 | if((handleNAND.error & 0x4)) return -1; 369 | 370 | sdmmc_send_command(&handleNAND, 0x10506, 0x3B90100); 371 | if((handleNAND.error & 0x4)) return -1; 372 | 373 | sdmmc_send_command(&handleNAND, 0x1040D, handleNAND.initarg << 0x10); 374 | if((handleNAND.error & 0x4)) return -1; 375 | 376 | sdmmc_send_command(&handleNAND, 0x10410, 0x200); 377 | if((handleNAND.error & 0x4)) return -1; 378 | 379 | handleNAND.clk |= 0x200; 380 | 381 | inittarget(&handleSD); 382 | 383 | return 0; 384 | } 385 | 386 | static int SD_Init() 387 | { 388 | //SD 389 | handleSD.isSDHC = 0; 390 | handleSD.SDOPT = 0; 391 | handleSD.res = 0; 392 | handleSD.initarg = 0; 393 | handleSD.clk = 0x80; 394 | handleSD.devicenumber = 0; 395 | 396 | inittarget(&handleSD); 397 | 398 | 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 399 | 400 | //If not inserted 401 | if(!(*((vu16 *)(SDMMC_BASE + REG_SDSTATUS0)) & TMIO_STAT0_SIGSTATE)) return 5; 402 | 403 | sdmmc_send_command(&handleSD, 0, 0); 404 | sdmmc_send_command(&handleSD, 0x10408, 0x1AA); 405 | u32 temp = (handleSD.error & 0x1) << 0x1E; 406 | 407 | u32 temp2 = 0; 408 | do 409 | { 410 | do 411 | { 412 | sdmmc_send_command(&handleSD, 0x10437, handleSD.initarg << 0x10); 413 | sdmmc_send_command(&handleSD, 0x10769, 0x00FF8000 | temp); 414 | temp2 = 1; 415 | } 416 | while(!(handleSD.error & 1)); 417 | } 418 | while((handleSD.ret[0] & 0x80000000) == 0); 419 | 420 | if(!((handleSD.ret[0] >> 30) & 1) || !temp) 421 | temp2 = 0; 422 | 423 | handleSD.isSDHC = temp2; 424 | 425 | sdmmc_send_command(&handleSD, 0x10602, 0); 426 | if((handleSD.error & 0x4)) return -1; 427 | 428 | sdmmc_send_command(&handleSD, 0x10403, 0); 429 | if((handleSD.error & 0x4)) return -2; 430 | handleSD.initarg = handleSD.ret[0] >> 0x10; 431 | 432 | sdmmc_send_command(&handleSD, 0x10609, handleSD.initarg << 0x10); 433 | if((handleSD.error & 0x4)) return -3; 434 | 435 | handleSD.total_size = calcSDSize((u8*)&handleSD.ret[0], -1); 436 | handleSD.clk = 1; 437 | setckl(1); 438 | 439 | sdmmc_send_command(&handleSD, 0x10507, handleSD.initarg << 0x10); 440 | if((handleSD.error & 0x4)) return -4; 441 | 442 | sdmmc_send_command(&handleSD, 0x10437, handleSD.initarg << 0x10); 443 | if((handleSD.error & 0x4)) return -5; 444 | 445 | handleSD.SDOPT = 1; 446 | sdmmc_send_command(&handleSD, 0x10446, 0x2); 447 | if((handleSD.error & 0x4)) return -6; 448 | 449 | sdmmc_send_command(&handleSD, 0x1040D, handleSD.initarg << 0x10); 450 | if((handleSD.error & 0x4)) return -7; 451 | 452 | sdmmc_send_command(&handleSD, 0x10410, 0x200); 453 | if((handleSD.error & 0x4)) return -8; 454 | handleSD.clk |= 0x200; 455 | 456 | return 0; 457 | } 458 | 459 | void sdmmc_get_cid(bool isNand, u32 *info) 460 | { 461 | struct mmcdevice *device = isNand ? &handleNAND : &handleSD; 462 | 463 | inittarget(device); 464 | 465 | // use cmd7 to put sd card in standby mode 466 | // CMD7 467 | sdmmc_send_command(device, 0x10507, 0); 468 | 469 | // get sd card info 470 | // use cmd10 to read CID 471 | sdmmc_send_command(device, 0x1060A, device->initarg << 0x10); 472 | 473 | for(int i = 0; i < 4; ++i) 474 | info[i] = device->ret[i]; 475 | 476 | // put sd card back to transfer mode 477 | // CMD7 478 | sdmmc_send_command(device, 0x10507, device->initarg << 0x10); 479 | } 480 | 481 | u32 sdmmc_sdcard_init() 482 | { 483 | u32 ret = 0; 484 | InitSD(); 485 | if(Nand_Init() != 0) ret &= 1; 486 | if(SD_Init() != 0) ret &= 2; 487 | return ret; 488 | } -------------------------------------------------------------------------------- /stage2/arm9/source/crypto.c: -------------------------------------------------------------------------------- 1 | /* 2 | * This file is part of Luma3DS 3 | * Copyright (C) 2016 Aurora Wright, TuxSH 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see . 17 | * 18 | * Additional Terms 7.b of GPLv3 applies to this file: Requiring preservation of specified 19 | * reasonable legal notices or author attributions in that material or in the Appropriate Legal 20 | * Notices displayed by works containing it. 21 | */ 22 | 23 | /* 24 | * Crypto libs from http://github.com/b1l1s/ctr 25 | * kernel9Loader code originally adapted from https://github.com/Reisyukaku/ReiNand/blob/228c378255ba693133dec6f3368e14d386f2cde7/source/crypto.c#L233 26 | */ 27 | 28 | #include "crypto.h" 29 | #include "memory.h" 30 | #include "fatfs/sdmmc/sdmmc.h" 31 | 32 | /**************************************************************** 33 | * Crypto libs 34 | ****************************************************************/ 35 | 36 | /* original version by megazig */ 37 | 38 | #ifndef __thumb__ 39 | #define BSWAP32(x) {\ 40 | __asm__\ 41 | (\ 42 | "eor r1, %1, %1, ror #16\n\t"\ 43 | "bic r1, r1, #0xFF0000\n\t"\ 44 | "mov %0, %1, ror #8\n\t"\ 45 | "eor %0, %0, r1, lsr #8\n\t"\ 46 | :"=r"(x)\ 47 | :"0"(x)\ 48 | :"r1"\ 49 | );\ 50 | }; 51 | 52 | #define ADD_u128_u32(u128_0, u128_1, u128_2, u128_3, u32_0) {\ 53 | __asm__\ 54 | (\ 55 | "adds %0, %4\n\t"\ 56 | "addcss %1, %1, #1\n\t"\ 57 | "addcss %2, %2, #1\n\t"\ 58 | "addcs %3, %3, #1\n\t"\ 59 | : "+r"(u128_0), "+r"(u128_1), "+r"(u128_2), "+r"(u128_3)\ 60 | : "r"(u32_0)\ 61 | : "cc"\ 62 | );\ 63 | } 64 | #else 65 | #define BSWAP32(x) {x = __builtin_bswap32(x);} 66 | 67 | #define ADD_u128_u32(u128_0, u128_1, u128_2, u128_3, u32_0) {\ 68 | __asm__\ 69 | (\ 70 | "mov r4, #0\n\t"\ 71 | "add %0, %0, %4\n\t"\ 72 | "adc %1, %1, r4\n\t"\ 73 | "adc %2, %2, r4\n\t"\ 74 | "adc %3, %3, r4\n\t"\ 75 | : "+r"(u128_0), "+r"(u128_1), "+r"(u128_2), "+r"(u128_3)\ 76 | : "r"(u32_0)\ 77 | : "cc", "r4"\ 78 | );\ 79 | } 80 | #endif /*__thumb__*/ 81 | 82 | static void aes_setkey(u8 keyslot, const void *key, u32 keyType, u32 mode) 83 | { 84 | u32 *key32 = (u32 *)key; 85 | *REG_AESCNT = (*REG_AESCNT & ~(AES_CNT_INPUT_ENDIAN | AES_CNT_INPUT_ORDER)) | mode; 86 | 87 | if(keyslot <= 3) 88 | { 89 | if((mode & AES_CNT_INPUT_ORDER) == AES_INPUT_TWLREVERSED) 90 | { 91 | REGs_AESTWLKEYS[keyslot][keyType][0] = key32[3]; 92 | REGs_AESTWLKEYS[keyslot][keyType][1] = key32[2]; 93 | REGs_AESTWLKEYS[keyslot][keyType][2] = key32[1]; 94 | REGs_AESTWLKEYS[keyslot][keyType][3] = key32[0]; 95 | } 96 | else 97 | { 98 | REGs_AESTWLKEYS[keyslot][keyType][0] = key32[0]; 99 | REGs_AESTWLKEYS[keyslot][keyType][1] = key32[1]; 100 | REGs_AESTWLKEYS[keyslot][keyType][2] = key32[2]; 101 | REGs_AESTWLKEYS[keyslot][keyType][3] = key32[3]; 102 | } 103 | } 104 | 105 | else if(keyslot < 0x40) 106 | { 107 | *REG_AESKEYCNT = (*REG_AESKEYCNT >> 6 << 6) | keyslot | AES_KEYCNT_WRITE; 108 | 109 | REG_AESKEYFIFO[keyType] = key32[0]; 110 | REG_AESKEYFIFO[keyType] = key32[1]; 111 | REG_AESKEYFIFO[keyType] = key32[2]; 112 | REG_AESKEYFIFO[keyType] = key32[3]; 113 | } 114 | } 115 | 116 | 117 | static void aes_use_keyslot(u8 keyslot) 118 | { 119 | if(keyslot > 0x3F) 120 | return; 121 | 122 | *REG_AESKEYSEL = keyslot; 123 | *REG_AESCNT = *REG_AESCNT | 0x04000000; /* mystery bit */ 124 | } 125 | 126 | static void aes_setiv(const void *iv, u32 mode) 127 | { 128 | const u32 *iv32 = (const u32 *)iv; 129 | *REG_AESCNT = (*REG_AESCNT & ~(AES_CNT_INPUT_ENDIAN | AES_CNT_INPUT_ORDER)) | mode; 130 | 131 | //Word order for IV can't be changed in REG_AESCNT and always default to reversed 132 | if(mode & AES_INPUT_NORMAL) 133 | { 134 | REG_AESCTR[0] = iv32[3]; 135 | REG_AESCTR[1] = iv32[2]; 136 | REG_AESCTR[2] = iv32[1]; 137 | REG_AESCTR[3] = iv32[0]; 138 | } 139 | else 140 | { 141 | REG_AESCTR[0] = iv32[0]; 142 | REG_AESCTR[1] = iv32[1]; 143 | REG_AESCTR[2] = iv32[2]; 144 | REG_AESCTR[3] = iv32[3]; 145 | } 146 | } 147 | 148 | static void __attribute__((optimize("O1"))) aes_advctr(void *ctr, u32 val, u32 mode) 149 | { 150 | u32 *ctr32 = (u32 *)ctr; 151 | 152 | int i; 153 | if(mode & AES_INPUT_BE) 154 | { 155 | for(i = 0; i < 4; ++i) //Endian swap 156 | BSWAP32(ctr32[i]); 157 | } 158 | 159 | if(mode & AES_INPUT_NORMAL) 160 | { 161 | ADD_u128_u32(ctr32[3], ctr32[2], ctr32[1], ctr32[0], val); 162 | } 163 | else 164 | { 165 | ADD_u128_u32(ctr32[0], ctr32[1], ctr32[2], ctr32[3], val); 166 | } 167 | 168 | if(mode & AES_INPUT_BE) 169 | { 170 | for(i = 0; i < 4; ++i) //Endian swap 171 | BSWAP32(ctr32[i]); 172 | } 173 | } 174 | 175 | static void aes_change_ctrmode(void *ctr, u32 fromMode, u32 toMode) 176 | { 177 | u32 *ctr32 = (u32 *)ctr; 178 | int i; 179 | if((fromMode ^ toMode) & AES_CNT_INPUT_ENDIAN) 180 | { 181 | for(i = 0; i < 4; ++i) 182 | BSWAP32(ctr32[i]); 183 | } 184 | 185 | if((fromMode ^ toMode) & AES_CNT_INPUT_ORDER) 186 | { 187 | u32 temp = ctr32[0]; 188 | ctr32[0] = ctr32[3]; 189 | ctr32[3] = temp; 190 | 191 | temp = ctr32[1]; 192 | ctr32[1] = ctr32[2]; 193 | ctr32[2] = temp; 194 | } 195 | } 196 | 197 | static void aes_batch(void *dst, const void *src, u32 blockCount) 198 | { 199 | *REG_AESBLKCNT = blockCount << 16; 200 | *REG_AESCNT |= AES_CNT_START; 201 | 202 | const u32 *src32 = (const u32 *)src; 203 | u32 *dst32 = (u32 *)dst; 204 | 205 | u32 wbc = blockCount; 206 | u32 rbc = blockCount; 207 | 208 | while(rbc) 209 | { 210 | if(wbc && ((*REG_AESCNT & 0x1F) <= 0xC)) //There's space for at least 4 ints 211 | { 212 | *REG_AESWRFIFO = *src32++; 213 | *REG_AESWRFIFO = *src32++; 214 | *REG_AESWRFIFO = *src32++; 215 | *REG_AESWRFIFO = *src32++; 216 | wbc--; 217 | } 218 | 219 | if(rbc && ((*REG_AESCNT & (0x1F << 0x5)) >= (0x4 << 0x5))) //At least 4 ints available for read 220 | { 221 | *dst32++ = *REG_AESRDFIFO; 222 | *dst32++ = *REG_AESRDFIFO; 223 | *dst32++ = *REG_AESRDFIFO; 224 | *dst32++ = *REG_AESRDFIFO; 225 | rbc--; 226 | } 227 | } 228 | } 229 | 230 | static void aes(void *dst, const void *src, u32 blockCount, void *iv, u32 mode, u32 ivMode) 231 | { 232 | *REG_AESCNT = mode | 233 | AES_CNT_INPUT_ORDER | AES_CNT_OUTPUT_ORDER | 234 | AES_CNT_INPUT_ENDIAN | AES_CNT_OUTPUT_ENDIAN | 235 | AES_CNT_FLUSH_READ | AES_CNT_FLUSH_WRITE; 236 | 237 | u32 blocks; 238 | while(blockCount != 0) 239 | { 240 | if((mode & AES_ALL_MODES) != AES_ECB_ENCRYPT_MODE 241 | && (mode & AES_ALL_MODES) != AES_ECB_DECRYPT_MODE) 242 | aes_setiv(iv, ivMode); 243 | 244 | blocks = (blockCount >= 0xFFFF) ? 0xFFFF : blockCount; 245 | 246 | //Save the last block for the next decryption CBC batch's iv 247 | if((mode & AES_ALL_MODES) == AES_CBC_DECRYPT_MODE) 248 | { 249 | memcpy(iv, src + (blocks - 1) * AES_BLOCK_SIZE, AES_BLOCK_SIZE); 250 | aes_change_ctrmode(iv, AES_INPUT_BE | AES_INPUT_NORMAL, ivMode); 251 | } 252 | 253 | //Process the current batch 254 | aes_batch(dst, src, blocks); 255 | 256 | //Save the last block for the next encryption CBC batch's iv 257 | if((mode & AES_ALL_MODES) == AES_CBC_ENCRYPT_MODE) 258 | { 259 | memcpy(iv, dst + (blocks - 1) * AES_BLOCK_SIZE, AES_BLOCK_SIZE); 260 | aes_change_ctrmode(iv, AES_INPUT_BE | AES_INPUT_NORMAL, ivMode); 261 | } 262 | 263 | //Advance counter for CTR mode 264 | else if((mode & AES_ALL_MODES) == AES_CTR_MODE) 265 | aes_advctr(iv, blocks, ivMode); 266 | 267 | src += blocks * AES_BLOCK_SIZE; 268 | dst += blocks * AES_BLOCK_SIZE; 269 | blockCount -= blocks; 270 | } 271 | } 272 | 273 | static void sha_wait_idle() 274 | { 275 | while(*REG_SHA_CNT & 1); 276 | } 277 | 278 | void sha(void *res, const void *src, u32 size, u32 mode) 279 | { 280 | sha_wait_idle(); 281 | *REG_SHA_CNT = mode | SHA_CNT_OUTPUT_ENDIAN | SHA_NORMAL_ROUND; 282 | 283 | const u32 *src32 = (const u32 *)src; 284 | int i; 285 | while(size >= 0x40) 286 | { 287 | sha_wait_idle(); 288 | for(i = 0; i < 4; ++i) 289 | { 290 | *REG_SHA_INFIFO = *src32++; 291 | *REG_SHA_INFIFO = *src32++; 292 | *REG_SHA_INFIFO = *src32++; 293 | *REG_SHA_INFIFO = *src32++; 294 | } 295 | 296 | size -= 0x40; 297 | } 298 | 299 | sha_wait_idle(); 300 | memcpy((void *)REG_SHA_INFIFO, src32, size); 301 | 302 | *REG_SHA_CNT = (*REG_SHA_CNT & ~SHA_NORMAL_ROUND) | SHA_FINAL_ROUND; 303 | 304 | while(*REG_SHA_CNT & SHA_FINAL_ROUND); 305 | sha_wait_idle(); 306 | 307 | u32 hashSize = SHA_256_HASH_SIZE; 308 | if(mode == SHA_224_MODE) 309 | hashSize = SHA_224_HASH_SIZE; 310 | else if(mode == SHA_1_MODE) 311 | hashSize = SHA_1_HASH_SIZE; 312 | 313 | memcpy(res, (void *)REG_SHA_HASH, hashSize); 314 | } 315 | 316 | /*****************************************************************/ 317 | 318 | __attribute__((aligned(4))) static u8 nandCtr[AES_BLOCK_SIZE]; 319 | static u8 nandSlot; 320 | static u32 fatStart = 0; 321 | 322 | int ctrNandInit(void) 323 | { 324 | __attribute__((aligned(4))) u8 cid[AES_BLOCK_SIZE], 325 | shaSum[SHA_256_HASH_SIZE]; 326 | 327 | sdmmc_get_cid(1, (u32 *)cid); 328 | sha(shaSum, cid, sizeof(cid), SHA_256_MODE); 329 | memcpy(nandCtr, shaSum, sizeof(nandCtr)); 330 | 331 | nandSlot = ISN3DS ? 0x05 : 0x04; 332 | 333 | int result; 334 | u8 __attribute__((aligned(4))) temp[0x200]; 335 | 336 | //Read NCSD header 337 | result = sdmmc_nand_readsectors(0, 1, temp); 338 | 339 | if(!result) 340 | { 341 | u32 partitionNum = 1; //TWL partitions need to be first 342 | for(u8 *partitionId = temp + 0x111; *partitionId != 1; partitionId++, partitionNum++); 343 | 344 | u32 ctrMbrOffset = *((u32 *)(temp + 0x120) + (2 * partitionNum)); 345 | 346 | //Read CTR MBR 347 | result = ctrNandRead(ctrMbrOffset, 1, temp); 348 | 349 | //Calculate final CTRNAND FAT offset 350 | if(!result) fatStart = ctrMbrOffset + *(u32 *)(temp + 0x1C6); 351 | } 352 | 353 | return result; 354 | } 355 | 356 | int ctrNandRead(u32 sector, u32 sectorCount, u8 *outbuf) 357 | { 358 | __attribute__((aligned(4))) u8 tmpCtr[sizeof(nandCtr)]; 359 | memcpy(tmpCtr, nandCtr, sizeof(nandCtr)); 360 | aes_advctr(tmpCtr, ((sector + fatStart) * 0x200) / AES_BLOCK_SIZE, AES_INPUT_BE | AES_INPUT_NORMAL); 361 | 362 | //Read 363 | int result = sdmmc_nand_readsectors(sector + fatStart, sectorCount, outbuf); 364 | 365 | //Decrypt 366 | aes_use_keyslot(nandSlot); 367 | aes(outbuf, outbuf, sectorCount * 0x200 / AES_BLOCK_SIZE, tmpCtr, AES_CTR_MODE, AES_INPUT_BE | AES_INPUT_NORMAL); 368 | 369 | return result; 370 | } 371 | 372 | static inline void twlConsoleInfoInit(void) 373 | { 374 | u64 twlConsoleId = ISDEVUNIT ? OTP_DEVCONSOLEID : (0x80000000ULL | (*(vu64 *)0x01FFB808 ^ 0x8C267B7B358A6AFULL)); 375 | CFG_TWLUNITINFO = CFG_UNITINFO; 376 | OTP_TWLCONSOLEID = twlConsoleId; 377 | 378 | *REG_AESCNT = 0; 379 | 380 | vu32 *k3X = REGs_AESTWLKEYS[3][1], 381 | *k1X = REGs_AESTWLKEYS[1][1]; 382 | 383 | k3X[0] = (u32)twlConsoleId; 384 | k3X[3] = (u32)(twlConsoleId >> 32); 385 | 386 | k1X[2] = (u32)(twlConsoleId >> 32); 387 | k1X[3] = (u32)twlConsoleId; 388 | 389 | aes_setkey(2, (u8 *)0x01FFD398, AES_KEYX, AES_INPUT_TWLNORMAL); 390 | if(CFG_TWLUNITINFO != 0) 391 | { 392 | __attribute__((aligned(4))) static const u8 key2YDev[AES_BLOCK_SIZE] = {0x3B, 0x06, 0x86, 0x57, 0x33, 0x04, 0x88, 0x11, 0x49, 0x04, 0x6B, 0x33, 0x12, 0x02, 0xAC, 0xF3}, 393 | key3YDev[AES_BLOCK_SIZE] = {0xAA, 0xBF, 0x76, 0xF1, 0x7A, 0xB8, 0xE8, 0x66, 0x97, 0x64, 0x6A, 0x26, 0x05, 0x00, 0xA0, 0xE1}; 394 | 395 | k3X[1] = 0xEE7A4B1E; 396 | k3X[2] = 0xAF42C08B; 397 | aes_setkey(2, key2YDev, AES_KEYY, AES_INPUT_TWLNORMAL); 398 | aes_setkey(3, key3YDev, AES_KEYY, AES_INPUT_TWLNORMAL); 399 | } 400 | else 401 | { 402 | u32 last3YWord = 0xE1A00005; 403 | __attribute__((aligned(4))) u8 key3YRetail[AES_BLOCK_SIZE]; 404 | 405 | memcpy(key3YRetail, (u8 *)0x01FFD3C8, 12); 406 | memcpy(key3YRetail + 12, &last3YWord, 4); 407 | 408 | k3X[1] = *(vu32 *)0x01FFD3A8; //"NINT" 409 | k3X[2] = *(vu32 *)0x01FFD3AC; //"ENDO" 410 | aes_setkey(2, (u8 *)0x01FFD220, AES_KEYY, AES_INPUT_TWLNORMAL); 411 | aes_setkey(3, key3YRetail, AES_KEYY, AES_INPUT_TWLNORMAL); 412 | } 413 | } 414 | 415 | void setupKeyslots(void) 416 | { 417 | //Setup 0x24 KeyY 418 | __attribute__((aligned(4))) static const u8 keyY0x24[AES_BLOCK_SIZE] = {0x74, 0xCA, 0x07, 0x48, 0x84, 0xF4, 0x22, 0x8D, 0xEB, 0x2A, 0x1C, 0xA7, 0x2D, 0x28, 0x77, 0x62}; 419 | aes_setkey(0x24, keyY0x24, AES_KEYY, AES_INPUT_BE | AES_INPUT_NORMAL); 420 | 421 | //Setup 0x25 KeyX and 0x2F KeyY 422 | __attribute__((aligned(4))) static const u8 keyX0x25s[2][AES_BLOCK_SIZE] = { 423 | {0xCE, 0xE7, 0xD8, 0xAB, 0x30, 0xC0, 0x0D, 0xAE, 0x85, 0x0E, 0xF5, 0xE3, 0x82, 0xAC, 0x5A, 0xF3}, 424 | {0x81, 0x90, 0x7A, 0x4B, 0x6F, 0x1B, 0x47, 0x32, 0x3A, 0x67, 0x79, 0x74, 0xCE, 0x4A, 0xD7, 0x1B} 425 | }, 426 | keyY0x2Fs[2][AES_BLOCK_SIZE] = { 427 | {0xC3, 0x69, 0xBA, 0xA2, 0x1E, 0x18, 0x8A, 0x88, 0xA9, 0xAA, 0x94, 0xE5, 0x50, 0x6A, 0x9F, 0x16}, 428 | {0x73, 0x25, 0xC4, 0xEB, 0x14, 0x3A, 0x0D, 0x5F, 0x5D, 0xB6, 0xE5, 0xC5, 0x7A, 0x21, 0x95, 0xAC} 429 | }; 430 | 431 | aes_setkey(0x25, keyX0x25s[ISDEVUNIT ? 1 : 0], AES_KEYX, AES_INPUT_BE | AES_INPUT_NORMAL); 432 | aes_setkey(0x2F, keyY0x2Fs[ISDEVUNIT ? 1 : 0], AES_KEYY, AES_INPUT_BE | AES_INPUT_NORMAL); 433 | 434 | if(ISN3DS) 435 | { 436 | //Setup 0x05 KeyY 437 | __attribute__((aligned(4))) static const u8 keyY0x5[AES_BLOCK_SIZE] = {0x4D, 0x80, 0x4F, 0x4E, 0x99, 0x90, 0x19, 0x46, 0x13, 0xA2, 0x04, 0xAC, 0x58, 0x44, 0x60, 0xBE}; 438 | aes_setkey(0x05, keyY0x5, AES_KEYY, AES_INPUT_BE | AES_INPUT_NORMAL); 439 | } 440 | 441 | //Setup TWL keys 442 | twlConsoleInfoInit(); 443 | 444 | //Set 0x11 keyslot 445 | __attribute__((aligned(4))) static const u8 key1s[2][AES_BLOCK_SIZE] = { 446 | {0x07, 0x29, 0x44, 0x38, 0xF8, 0xC9, 0x75, 0x93, 0xAA, 0x0E, 0x4A, 0xB4, 0xAE, 0x84, 0xC1, 0xD8}, 447 | {0xA2, 0xF4, 0x00, 0x3C, 0x7A, 0x95, 0x10, 0x25, 0xDF, 0x4E, 0x9E, 0x74, 0xE3, 0x0C, 0x92, 0x99} 448 | }, 449 | key2s[2][AES_BLOCK_SIZE] = { 450 | {0x42, 0x3F, 0x81, 0x7A, 0x23, 0x52, 0x58, 0x31, 0x6E, 0x75, 0x8E, 0x3A, 0x39, 0x43, 0x2E, 0xD0}, 451 | {0xFF, 0x77, 0xA0, 0x9A, 0x99, 0x81, 0xE9, 0x48, 0xEC, 0x51, 0xC9, 0x32, 0x5D, 0x14, 0xEC, 0x25} 452 | }; 453 | 454 | 455 | __attribute__((aligned(4))) u8 keyBlocks[2][AES_BLOCK_SIZE] = { 456 | {0xA4, 0x8D, 0xE4, 0xF1, 0x0B, 0x36, 0x44, 0xAA, 0x90, 0x31, 0x28, 0xFF, 0x4D, 0xCA, 0x76, 0xDF}, 457 | {0xDD, 0xDA, 0xA4, 0xC6, 0x2C, 0xC4, 0x50, 0xE9, 0xDA, 0xB6, 0x9B, 0x0D, 0x9D, 0x2A, 0x21, 0x98} 458 | }, decKey[AES_BLOCK_SIZE]; 459 | 460 | //Initialize Key 0x18 461 | aes_setkey(0x11, key1s[ISDEVUNIT ? 1 : 0], AES_KEYNORMAL, AES_INPUT_BE | AES_INPUT_NORMAL); 462 | aes_use_keyslot(0x11); 463 | aes(decKey, keyBlocks[0], 1, NULL, AES_ECB_DECRYPT_MODE, 0); 464 | aes_setkey(0x18, decKey, AES_KEYX, AES_INPUT_BE | AES_INPUT_NORMAL); 465 | 466 | //Initialize Key 0x19-0x1F 467 | aes_setkey(0x11, key2s[ISDEVUNIT ? 1 : 0], AES_KEYNORMAL, AES_INPUT_BE | AES_INPUT_NORMAL); 468 | aes_use_keyslot(0x11); 469 | for(u8 slot = 0x19; slot < 0x20; slot++, keyBlocks[1][0xF]++) 470 | { 471 | aes(decKey, keyBlocks[1], 1, NULL, AES_ECB_DECRYPT_MODE, 0); 472 | aes_setkey(slot, decKey, AES_KEYX, AES_INPUT_BE | AES_INPUT_NORMAL); 473 | } 474 | } 475 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | {one line to give the program's name and a brief idea of what it does.} 635 | Copyright (C) {year} {name of author} 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | {project} Copyright (C) {year} {fullname} 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . --------------------------------------------------------------------------------